- int uid, gid; /* derived ones go in here */
-
- /* check to see if we have to switch users */
- if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
- if ( seteuid ( uid ) < 0 )
- syslog ( LOG_ERR, "set_uidgid: unable to seteuid '%s': %m",
- (this_volume)->v_forceuid );
- } /* end of checking for (this_volume)->v_forceuid */
-
- /* check to see if we have to switch groups */
- if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
- if ( seteuid ( gid ) < 0 )
- syslog ( LOG_ERR, "set_uidgid: unable to setegid '%s': %m",
- (this_volume)->v_forcegid );
- } /* end of checking for (this_volume)->v_forcegid */
+ int uid, gid; /* derived ones go in here */
+
+ /* check to see if we have to switch users */
+ uid = user_to_uid ( (this_volume)->v_forceuid);
+ gid = group_to_gid ( (this_volume)->v_forcegid);
+
+ if ((!uid || uid == geteuid()) && (!gid || gid == getegid()))
+ return;
+
+ if ( seteuid(0) < 0) {
+ LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
+ strerror(errno));
+ return;
+ }
+
+ /* check to see if we have to switch groups */
+ if ( gid ) {
+ if ( setegid ( gid ) < 0 )
+ LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
+ (this_volume)->v_forcegid, strerror(errno) );
+ } /* end of checking for (this_volume)->v_forcegid */
+
+ if ( uid) {
+ if ( seteuid ( uid ) < 0 )
+ LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
+ (this_volume)->v_forceuid, strerror(errno) );
+ else
+ uuid = uid; /* ugly hack for utommode */
+
+ } /* end of checking for (this_volume)->v_forceuid */