]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uid.c
cleaning: FORCE_UIDGID is was 100% broken before, it's always broken but with
[netatalk.git] / etc / afpd / uid.c
1 /*
2  * $Id: uid.c,v 1.13 2002-08-30 19:32:41 didg 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 /* HAVE_CONFIG_H */
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 <errno.h>
20 #include <atalk/logger.h>
21
22 /* functions for username and group */
23 #include <pwd.h>
24 #include <grp.h>
25 #include "uid.h"
26
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif /* HAVE_UNISTD_H */
30
31 extern uid_t    uuid; 
32
33 void save_uidgid ( pair )
34 uidgidset *pair;
35 {
36     pair->uid = geteuid ();
37     pair->gid = getegid ();
38
39
40 void restore_uidgid ( pair )
41 uidgidset *pair;
42 {
43     int         uid, gid;   
44     
45     uid = geteuid ();
46     gid = getegid ();
47
48     if (uid == pair->uid && gid == pair->gid)
49        return;
50
51     if (seteuid(0) < 0) {
52         LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
53                                 strerror(errno));
54     }
55
56     if ( setegid ( pair->gid ) < 0 )
57         LOG(log_error, logtype_afpd, "restore_uidgid: unable to setegid '%s': %s",
58             pair->gid, strerror(errno) );
59
60     if ( seteuid ( pair->uid ) < 0 )
61         LOG(log_error, logtype_afpd, "restore_uidgid: unable to seteuid '%s': %s",
62             pair->uid, strerror(errno) );
63     else
64         uuid = pair->uid;       /* ugly hack for utommode */
65 }
66
67 void set_uidgid ( this_volume )
68 const struct vol        *this_volume;
69 {
70     int         uid, gid;   /* derived ones go in here */
71
72     /* check to see if we have to switch users */
73     uid = user_to_uid ( (this_volume)->v_forceuid);
74     gid = group_to_gid ( (this_volume)->v_forcegid);
75
76     if ((!uid || uid == geteuid()) && (!gid || gid == getegid()))
77        return;
78
79     if ( seteuid(0) < 0) {
80         LOG(log_error, logtype_afpd, "set_uidgid: Could not switch back to root: %s",
81                                 strerror(errno));
82         return;
83     }
84
85     /* check to see if we have to switch groups */
86     if ( gid ) {
87         if ( setegid ( gid ) < 0 )
88             LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
89                 (this_volume)->v_forcegid, strerror(errno) );
90     } /* end of checking for (this_volume)->v_forcegid */
91
92     if ( uid) {
93         if ( seteuid ( uid ) < 0 )
94             LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
95                 (this_volume)->v_forceuid, strerror(errno) );
96         else
97             uuid = uid; /* ugly hack for utommode */
98
99     } /* end of checking for (this_volume)->v_forceuid */
100
101 } /* end function void set_uidgid ( username, group ) */
102
103 int user_to_uid ( username )
104 char    *username;
105 {
106     struct passwd *this_passwd;
107
108     /* check for anything */
109     if ( !username || strlen ( username ) < 1 ) return 0;
110
111     /* grab the /etc/passwd record relating to username */
112     this_passwd = getpwnam ( username );
113
114     /* return false if there is no structure returned */
115     if (this_passwd == NULL) return 0;
116
117     /* return proper uid */
118     return this_passwd->pw_uid;
119
120 } /* end function int user_to_uid ( username ) */
121
122 int group_to_gid ( group )
123 char    *group;
124 {
125     struct group *this_group;
126
127     /* check for anything */
128     if ( !group || strlen ( group ) < 1 ) return 0;
129
130     /* grab the /etc/groups record relating to group */
131     this_group = getgrnam ( group );
132
133     /* return false if there is no structure returned */
134     if (this_group == NULL) return 0;
135
136     /* return proper gid */
137     return this_group->gr_gid;
138
139 } /* end function int group_to_gid ( group ) */
140
141 #endif /* FORCE_UIDGID */