]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uid.c
small fix to load mtab table initially if supported to fix mtab DID support
[netatalk.git] / etc / afpd / uid.c
1 /*
2  * $Id: uid.c,v 1.3 2001-02-23 22:16:15 rufustfirefly Exp $
3  * code: jeff@univrel.pr.uconn.edu
4  *
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
7  * across the package).
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 /* don't compile this file at all unless FORCE_UIDGID is set */
15 #ifdef FORCE_UIDGID
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <syslog.h>
20
21 /* functions for username and group */
22 #include <pwd.h>
23 #include <grp.h>
24 #include "uid.h"
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 void save_uidgid ( pair )
31         uidgidset *pair;
32 {
33         (pair)->uid = geteuid ();
34         (pair)->gid = getegid ();
35 } /* end function void save_uidgid ( pair ) */
36
37 void restore_uidgid ( pair )
38         uidgidset *pair;
39 {
40         if ( seteuid ( (pair)->uid ) < 0 )
41                 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
42                         (pair)->uid );
43         if ( setegid ( (pair)->gid ) < 0 )
44                 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
45                         (pair)->gid );
46 } /* end function void restore_uidgid ( pair ) */
47
48 void set_uidgid ( this_volume )
49         struct vol      *this_volume;
50 {
51         int             uid, gid;   /* derived ones go in here */
52
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 */
59
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 */
66
67 } /* end function void set_uidgid ( username, group ) */
68
69 int user_to_uid ( username )
70         char    *username;
71 {
72         struct passwd *this_passwd;
73
74         /* check for anything */
75         if ( strlen ( username ) < 1 ) return 0;
76
77         /* grab the /etc/passwd record relating to username */
78         this_passwd = getpwnam ( username );
79
80         /* return false if there is no structure returned */
81         if (this_passwd == NULL) return 0;
82
83         /* return proper uid */
84         return this_passwd->pw_uid;
85
86 } /* end function int user_to_uid ( username ) */
87
88 int group_to_gid ( group )
89         char    *group;
90 {
91         struct group *this_group;
92
93         /* check for anything */
94         if ( strlen ( group ) < 1 ) return 0;
95
96         /* grab the /etc/groups record relating to group */
97         this_group = getgrnam ( group );
98
99         /* return false if there is no structure returned */
100         if (this_group == NULL) return 0;
101
102         /* return proper gid */
103         return this_group->gr_gid;
104
105 } /* end function int group_to_gid ( group ) */
106
107 #endif FORCE_UIDGID