2 * $Id: uid.c,v 1.6 2001-12-03 05:03:38 jmarcus 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 */
21 /* functions for username and group */
28 #endif /* HAVE_UNISTD_H */
30 void save_uidgid ( pair )
33 /* allocate the memory */
34 pair = malloc ( sizeof ( uidgidset ) );
36 /* then assign the values */
37 (*pair)->uid = geteuid ();
38 (*pair)->gid = getegid ();
39 } /* end function void save_uidgid ( pair ) */
41 void restore_uidgid ( pair )
44 if ( seteuid ( (*pair)->uid ) < 0 )
45 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
47 if ( setegid ( (*pair)->gid ) < 0 )
48 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
50 } /* end function void restore_uidgid ( pair ) */
52 void set_uidgid ( this_volume )
53 const struct vol *this_volume;
55 int uid, gid; /* derived ones go in here */
57 /* check to see if we have to switch users */
58 if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
59 if ( seteuid ( uid ) < 0 )
60 syslog ( LOG_ERR, "set_uidgid: unable to seteuid '%s': %m",
61 (this_volume)->v_forceuid );
62 } /* end of checking for (this_volume)->v_forceuid */
64 /* check to see if we have to switch groups */
65 if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
66 if ( seteuid ( gid ) < 0 )
67 syslog ( LOG_ERR, "set_uidgid: unable to setegid '%s': %m",
68 (this_volume)->v_forcegid );
69 } /* end of checking for (this_volume)->v_forcegid */
71 } /* end function void set_uidgid ( username, group ) */
73 int user_to_uid ( username )
76 struct passwd *this_passwd;
78 /* free memory for pointer */
79 this_passwd = malloc ( sizeof ( struct passwd ) );
81 /* check for anything */
82 if ( strlen ( username ) < 1 ) return 0;
84 /* grab the /etc/passwd record relating to username */
85 this_passwd = getpwnam ( username );
87 /* return false if there is no structure returned */
88 if (this_passwd == NULL) return 0;
90 /* return proper uid */
91 return this_passwd->pw_uid;
93 } /* end function int user_to_uid ( username ) */
95 int group_to_gid ( group )
98 struct group *this_group;
100 /* free memory for pointer */
101 this_group = malloc ( sizeof ( struct group ) );
103 /* check for anything */
104 if ( strlen ( group ) < 1 ) return 0;
106 /* grab the /etc/groups record relating to group */
107 this_group = getgrnam ( group );
109 /* return false if there is no structure returned */
110 if (this_group == NULL) return 0;
112 /* return proper gid */
113 return this_group->gr_gid;
115 } /* end function int group_to_gid ( group ) */
117 #endif /* FORCE_UIDGID */