]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uid.c
half of FORCE_UIDGID commits for afpd (STILL HORRENDOUSLY BROKEN, PROBABLY)
[netatalk.git] / etc / afpd / uid.c
1 /*
2  * $Id: uid.c,v 1.1 2001-01-02 23:00:35 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 #include <stdio.h>
15 #include <string.h>
16 #include <syslog.h>
17
18 /* functions for username and group */
19 #include <pwd.h>
20 #include <grp.h>
21 #include "uid.h"
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 void save_uidgid ( pair )
28         uidgidset *pair;
29 {
30         (pair)->uid = geteuid ();
31         (pair)->gid = getegid ();
32 } /* end function void save_uidgid ( pair ) */
33
34 void restore_uidgid ( pair )
35         uidgidset *pair;
36 {
37         if ( seteuid ( (pair)->uid ) < 0 )
38                 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
39                         (pair)->uid );
40         if ( setegid ( (pair)->gid ) < 0 )
41                 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
42                         (pair)->gid );
43 } /* end function void restore_uidgid ( pair ) */
44
45 void set_uidgid ( this_volume )
46         struct vol      *this_volume;
47 {
48         int             uid, gid;   /* derived ones go in here */
49
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 */
56
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 */
63
64 } /* end function void set_uidgid ( username, group ) */
65
66 int user_to_uid ( username )
67         char    *username;
68 {
69         struct passwd *this_passwd;
70
71         /* check for anything */
72         if ( strlen ( username ) < 1 ) return 0;
73
74         /* grab the /etc/passwd record relating to username */
75         this_passwd = getpwnam ( username );
76
77         /* return false if there is no structure returned */
78         if (this_passwd == NULL) return 0;
79
80         /* return proper uid */
81         return this_passwd->pw_uid;
82
83 } /* end function int user_to_uid ( username ) */
84
85 int group_to_gid ( group )
86         char    *group;
87 {
88         struct group *this_group;
89
90         /* check for anything */
91         if ( strlen ( group ) < 1 ) return 0;
92
93         /* grab the /etc/groups record relating to group */
94         this_group = getgrnam ( group );
95
96         /* return false if there is no structure returned */
97         if (this_group == NULL) return 0;
98
99         /* return proper gid */
100         return this_group->gr_gid;
101
102 } /* end function int group_to_gid ( group ) */