2 * $Id: uid.c,v 1.14 2005-04-28 20:49:45 bfernhomberg Exp $
3 * code: jeff@univrel.pr.uconn.edu
5 * These functions are abstracted here, so that all calls for resolving
6 * user/group names can be centrally changed (good for OS dependant calls
12 #endif /* HAVE_CONFIG_H */
14 /* don't compile this file at all unless FORCE_UIDGID is set */
20 #include <atalk/logger.h>
22 /* functions for username and group */
29 #endif /* HAVE_UNISTD_H */
33 void save_uidgid ( pair )
36 pair->uid = geteuid ();
37 pair->gid = getegid ();
40 void restore_uidgid ( pair )
49 if (uid == pair->uid && gid == pair->gid)
53 LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
57 if ( setegid ( pair->gid ) < 0 )
58 LOG(log_error, logtype_afpd, "restore_uidgid: unable to setegid '%s': %s",
59 pair->gid, strerror(errno) );
61 if ( seteuid ( pair->uid ) < 0 )
62 LOG(log_error, logtype_afpd, "restore_uidgid: unable to seteuid '%s': %s",
63 pair->uid, strerror(errno) );
65 uuid = pair->uid; /* ugly hack for utommode */
68 void set_uidgid ( this_volume )
69 const struct vol *this_volume;
71 int uid, gid; /* derived ones go in here */
73 /* check to see if we have to switch users */
74 uid = user_to_uid ( (this_volume)->v_forceuid);
75 gid = group_to_gid ( (this_volume)->v_forcegid);
77 if ((!uid || uid == geteuid()) && (!gid || gid == getegid()))
80 if ( seteuid(0) < 0) {
81 LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
86 /* check to see if we have to switch groups */
88 if ( setegid ( gid ) < 0 )
89 LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
90 (this_volume)->v_forcegid, strerror(errno) );
91 } /* end of checking for (this_volume)->v_forcegid */
94 if ( seteuid ( uid ) < 0 )
95 LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
96 (this_volume)->v_forceuid, strerror(errno) );
98 uuid = uid; /* ugly hack for utommode */
100 } /* end of checking for (this_volume)->v_forceuid */
102 } /* end function void set_uidgid ( username, group ) */
104 int user_to_uid ( username )
107 struct passwd *this_passwd;
109 /* check for anything */
110 if ( !username || strlen ( username ) < 1 ) return 0;
112 /* grab the /etc/passwd record relating to username */
113 this_passwd = getpwnam ( username );
115 /* return false if there is no structure returned */
116 if (this_passwd == NULL) return 0;
118 /* return proper uid */
119 return this_passwd->pw_uid;
121 } /* end function int user_to_uid ( username ) */
123 int group_to_gid ( group )
126 struct group *this_group;
128 /* check for anything */
129 if ( !group || strlen ( group ) < 1 ) return 0;
131 /* grab the /etc/groups record relating to group */
132 this_group = getgrnam ( group );
134 /* return false if there is no structure returned */
135 if (this_group == NULL) return 0;
137 /* return proper gid */
138 return this_group->gr_gid;
140 } /* end function int group_to_gid ( group ) */
142 #endif /* FORCE_UIDGID */