2 * $Id: uid.c,v 1.2 2001-01-27 17:54:26 itlm019 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
10 /* don't compile this file at all unless FORCE_UIDGID is set */
21 /* functions for username and group */
30 void save_uidgid ( pair )
33 (pair)->uid = geteuid ();
34 (pair)->gid = getegid ();
35 } /* end function void save_uidgid ( pair ) */
37 void restore_uidgid ( pair )
40 if ( seteuid ( (pair)->uid ) < 0 )
41 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
43 if ( setegid ( (pair)->gid ) < 0 )
44 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
46 } /* end function void restore_uidgid ( pair ) */
48 void set_uidgid ( this_volume )
49 struct vol *this_volume;
51 int uid, gid; /* derived ones go in here */
53 /* check to see if we have to switch users */
54 if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
55 if ( seteuid ( uid ) < 0 )
56 syslog ( LOG_ERR, "set_uidgid: unable to seteuid '%s': %m",
57 (this_volume)->v_forceuid );
58 } /* end of checking for (this_volume)->v_forceuid */
60 /* check to see if we have to switch groups */
61 if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
62 if ( seteuid ( gid ) < 0 )
63 syslog ( LOG_ERR, "set_uidgid: unable to setegid '%s': %m",
64 (this_volume)->v_forcegid );
65 } /* end of checking for (this_volume)->v_forcegid */
67 } /* end function void set_uidgid ( username, group ) */
69 int user_to_uid ( username )
72 struct passwd *this_passwd;
74 /* check for anything */
75 if ( strlen ( username ) < 1 ) return 0;
77 /* grab the /etc/passwd record relating to username */
78 this_passwd = getpwnam ( username );
80 /* return false if there is no structure returned */
81 if (this_passwd == NULL) return 0;
83 /* return proper uid */
84 return this_passwd->pw_uid;
86 } /* end function int user_to_uid ( username ) */
88 int group_to_gid ( group )
91 struct group *this_group;
93 /* check for anything */
94 if ( strlen ( group ) < 1 ) return 0;
96 /* grab the /etc/groups record relating to group */
97 this_group = getgrnam ( group );
99 /* return false if there is no structure returned */
100 if (this_group == NULL) return 0;
102 /* return proper gid */
103 return this_group->gr_gid;
105 } /* end function int group_to_gid ( group ) */