]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uid.c
2078de1d6a84b1fe0eb5a59cd53f97d8cc7caef9
[netatalk.git] / etc / afpd / uid.c
1 /*
2  * $Id: uid.c,v 1.4 2001-06-20 18:33:04 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 /* 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 <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 /* HAVE_UNISTD_H */
29
30 void save_uidgid ( pair )
31         uidgidset *pair;
32 {
33         /* allocate the memory */
34         pair = malloc ( sizeof ( uidgidset ) );
35
36         /* then assign the values */
37         (pair)->uid = geteuid ();
38         (pair)->gid = getegid ();
39 } /* end function void save_uidgid ( pair ) */
40
41 void restore_uidgid ( pair )
42         uidgidset *pair;
43 {
44         if ( seteuid ( (pair)->uid ) < 0 )
45                 syslog ( LOG_ERR, "restore_uidgid: unable to seteuid '%s': %m",
46                         (pair)->uid );
47         if ( setegid ( (pair)->gid ) < 0 )
48                 syslog ( LOG_ERR, "restore_uidgid: unable to setegid '%s': %m",
49                         (pair)->gid );
50 } /* end function void restore_uidgid ( pair ) */
51
52 void set_uidgid ( this_volume )
53         struct vol      *this_volume;
54 {
55         int             uid, gid;   /* derived ones go in here */
56
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 */
63
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 */
70
71 } /* end function void set_uidgid ( username, group ) */
72
73 int user_to_uid ( username )
74         char    *username;
75 {
76         struct passwd *this_passwd;
77
78         /* free memory for pointer */
79         this_passwd = malloc ( sizeof ( struct passwd ) );
80
81         /* check for anything */
82         if ( strlen ( username ) < 1 ) return 0;
83
84         /* grab the /etc/passwd record relating to username */
85         this_passwd = getpwnam ( username );
86
87         /* return false if there is no structure returned */
88         if (this_passwd == NULL) return 0;
89
90         /* return proper uid */
91         return this_passwd->pw_uid;
92
93 } /* end function int user_to_uid ( username ) */
94
95 int group_to_gid ( group )
96         char    *group;
97 {
98         struct group *this_group;
99
100         /* free memory for pointer */
101         this_group = malloc ( sizeof ( struct group ) );
102
103         /* check for anything */
104         if ( strlen ( group ) < 1 ) return 0;
105
106         /* grab the /etc/groups record relating to group */
107         this_group = getgrnam ( group );
108
109         /* return false if there is no structure returned */
110         if (this_group == NULL) return 0;
111
112         /* return proper gid */
113         return this_group->gr_gid;
114
115 } /* end function int group_to_gid ( group ) */
116
117 #endif /* FORCE_UIDGID */