2 * $Id: uid.c,v 1.1 2001-01-02 23:00:35 rufustfirefly 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
18 /* functions for username and group */
27 void save_uidgid ( pair )
30 (pair)->uid = geteuid ();
31 (pair)->gid = getegid ();
32 } /* end function void save_uidgid ( pair ) */
34 void restore_uidgid ( pair )
37 if ( seteuid ( (pair)->uid ) < 0 )
38 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
40 if ( setegid ( (pair)->gid ) < 0 )
41 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
43 } /* end function void restore_uidgid ( pair ) */
45 void set_uidgid ( this_volume )
46 struct vol *this_volume;
48 int uid, gid; /* derived ones go in here */
50 /* check to see if we have to switch users */
51 if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
52 if ( seteuid ( uid ) < 0 )
53 syslog ( LOG_ERR, "set_uidgid: unable to seteuid '%s': %m",
54 (this_volume)->v_forceuid );
55 } /* end of checking for (this_volume)->v_forceuid */
57 /* check to see if we have to switch groups */
58 if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
59 if ( seteuid ( gid ) < 0 )
60 syslog ( LOG_ERR, "set_uidgid: unable to setegid '%s': %m",
61 (this_volume)->v_forcegid );
62 } /* end of checking for (this_volume)->v_forcegid */
64 } /* end function void set_uidgid ( username, group ) */
66 int user_to_uid ( username )
69 struct passwd *this_passwd;
71 /* check for anything */
72 if ( strlen ( username ) < 1 ) return 0;
74 /* grab the /etc/passwd record relating to username */
75 this_passwd = getpwnam ( username );
77 /* return false if there is no structure returned */
78 if (this_passwd == NULL) return 0;
80 /* return proper uid */
81 return this_passwd->pw_uid;
83 } /* end function int user_to_uid ( username ) */
85 int group_to_gid ( group )
88 struct group *this_group;
90 /* check for anything */
91 if ( strlen ( group ) < 1 ) return 0;
93 /* grab the /etc/groups record relating to group */
94 this_group = getgrnam ( group );
96 /* return false if there is no structure returned */
97 if (this_group == NULL) return 0;
99 /* return proper gid */
100 return this_group->gr_gid;
102 } /* end function int group_to_gid ( group ) */