2 * $Id: uid.c,v 1.13 2002-08-30 19:32:41 didg 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 )
48 if (uid == pair->uid && gid == pair->gid)
52 LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
56 if ( setegid ( pair->gid ) < 0 )
57 LOG(log_error, logtype_afpd, "restore_uidgid: unable to setegid '%s': %s",
58 pair->gid, strerror(errno) );
60 if ( seteuid ( pair->uid ) < 0 )
61 LOG(log_error, logtype_afpd, "restore_uidgid: unable to seteuid '%s': %s",
62 pair->uid, strerror(errno) );
64 uuid = pair->uid; /* ugly hack for utommode */
67 void set_uidgid ( this_volume )
68 const struct vol *this_volume;
70 int uid, gid; /* derived ones go in here */
72 /* check to see if we have to switch users */
73 uid = user_to_uid ( (this_volume)->v_forceuid);
74 gid = group_to_gid ( (this_volume)->v_forcegid);
76 if ((!uid || uid == geteuid()) && (!gid || gid == getegid()))
79 if ( seteuid(0) < 0) {
80 LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
85 /* check to see if we have to switch groups */
87 if ( setegid ( gid ) < 0 )
88 LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
89 (this_volume)->v_forcegid, strerror(errno) );
90 } /* end of checking for (this_volume)->v_forcegid */
93 if ( seteuid ( uid ) < 0 )
94 LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
95 (this_volume)->v_forceuid, strerror(errno) );
97 uuid = uid; /* ugly hack for utommode */
99 } /* end of checking for (this_volume)->v_forceuid */
101 } /* end function void set_uidgid ( username, group ) */
103 int user_to_uid ( username )
106 struct passwd *this_passwd;
108 /* check for anything */
109 if ( !username || strlen ( username ) < 1 ) return 0;
111 /* grab the /etc/passwd record relating to username */
112 this_passwd = getpwnam ( username );
114 /* return false if there is no structure returned */
115 if (this_passwd == NULL) return 0;
117 /* return proper uid */
118 return this_passwd->pw_uid;
120 } /* end function int user_to_uid ( username ) */
122 int group_to_gid ( group )
125 struct group *this_group;
127 /* check for anything */
128 if ( !group || strlen ( group ) < 1 ) return 0;
130 /* grab the /etc/groups record relating to group */
131 this_group = getgrnam ( group );
133 /* return false if there is no structure returned */
134 if (this_group == NULL) return 0;
136 /* return proper gid */
137 return this_group->gr_gid;
139 } /* end function int group_to_gid ( group ) */
141 #endif /* FORCE_UIDGID */