2 * $Id: uid.c,v 1.12 2002-03-24 01:23:41 sibaz 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 */
31 void save_uidgid ( pair )
34 /* allocate the memory */
35 pair = malloc ( sizeof ( uidgidset ) );
37 /* then assign the values */
38 (*pair)->uid = geteuid ();
39 (*pair)->gid = getegid ();
40 } /* end function void save_uidgid ( pair ) */
42 void restore_uidgid ( pair )
45 if ( seteuid ( (*pair)->uid ) < 0 )
46 LOG(log_error, logtype_afpd, "restore_uidgid: unable to seteuid '%s': %s",
47 (*pair)->uid, strerror(errno) );
48 if ( setegid ( (*pair)->gid ) < 0 )
49 LOG(log_error, logtype_afpd, "restore_uidgid: unable to setegid '%s': %s",
50 (*pair)->gid, strerror(errno) );
51 } /* end function void restore_uidgid ( pair ) */
53 void set_uidgid ( this_volume )
54 const struct vol *this_volume;
56 int uid, gid; /* derived ones go in here */
58 /* check to see if we have to switch users */
59 if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
60 if ( seteuid ( uid ) < 0 )
61 LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
62 (this_volume)->v_forceuid, strerror(errno) );
63 } /* end of checking for (this_volume)->v_forceuid */
65 /* check to see if we have to switch groups */
66 if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
67 if ( seteuid ( gid ) < 0 )
68 LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
69 (this_volume)->v_forcegid, strerror(errno) );
70 } /* end of checking for (this_volume)->v_forcegid */
72 } /* end function void set_uidgid ( username, group ) */
74 int user_to_uid ( username )
77 struct passwd *this_passwd;
79 /* free memory for pointer */
80 this_passwd = malloc ( sizeof ( struct passwd ) );
82 /* check for anything */
83 if ( strlen ( username ) < 1 ) return 0;
85 /* grab the /etc/passwd record relating to username */
86 this_passwd = getpwnam ( username );
88 /* return false if there is no structure returned */
89 if (this_passwd == NULL) return 0;
91 /* return proper uid */
92 return this_passwd->pw_uid;
94 } /* end function int user_to_uid ( username ) */
96 int group_to_gid ( group )
99 struct group *this_group;
101 /* free memory for pointer */
102 this_group = malloc ( sizeof ( struct group ) );
104 /* check for anything */
105 if ( strlen ( group ) < 1 ) return 0;
107 /* grab the /etc/groups record relating to group */
108 this_group = getgrnam ( group );
110 /* return false if there is no structure returned */
111 if (this_group == NULL) return 0;
113 /* return proper gid */
114 return this_group->gr_gid;
116 } /* end function int group_to_gid ( group ) */
118 #endif /* FORCE_UIDGID */