]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uid.c
added some LOG messages
[netatalk.git] / etc / afpd / uid.c
1 /*
2  * $Id: uid.c,v 1.12 2002-03-24 01:23:41 sibaz 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 void save_uidgid ( pair )
32 uidgidset **pair;
33 {
34     /* allocate the memory */
35     pair = malloc ( sizeof ( uidgidset ) );
36
37     /* then assign the values */
38     (*pair)->uid = geteuid ();
39     (*pair)->gid = getegid ();
40 } /* end function void save_uidgid ( pair ) */
41
42 void restore_uidgid ( pair )
43 uidgidset **pair;
44 {
45     if ( seteuid ( (*pair)->uid ) < 0 )
46         LOG(log_error, logtype_afpd, "restore_uidgid: unable to seteuid '%s': %s",
47             (*pair)->uid, strerror(errno) );
48     if ( setegid ( (*pair)->gid ) < 0 )
49         LOG(log_error, logtype_afpd, "restore_uidgid: unable to setegid '%s': %s",
50             (*pair)->gid, strerror(errno) );
51 } /* end function void restore_uidgid ( pair ) */
52
53 void set_uidgid ( this_volume )
54 const struct vol        *this_volume;
55 {
56     int         uid, gid;   /* derived ones go in here */
57
58     /* check to see if we have to switch users */
59     if ( uid = user_to_uid ( (this_volume)->v_forceuid ) ) {
60         if ( seteuid ( uid ) < 0 )
61             LOG(log_error, logtype_afpd, "set_uidgid: unable to seteuid '%s': %s",
62                 (this_volume)->v_forceuid, strerror(errno) );
63     } /* end of checking for (this_volume)->v_forceuid */
64
65     /* check to see if we have to switch groups */
66     if ( gid = group_to_gid ( (this_volume)->v_forcegid ) ) {
67         if ( seteuid ( gid ) < 0 )
68             LOG(log_error, logtype_afpd, "set_uidgid: unable to setegid '%s': %s",
69                 (this_volume)->v_forcegid, strerror(errno) );
70     } /* end of checking for (this_volume)->v_forcegid */
71
72 } /* end function void set_uidgid ( username, group ) */
73
74 int user_to_uid ( username )
75 char    *username;
76 {
77     struct passwd *this_passwd;
78
79     /* free memory for pointer */
80     this_passwd = malloc ( sizeof ( struct passwd ) );
81
82     /* check for anything */
83     if ( strlen ( username ) < 1 ) return 0;
84
85     /* grab the /etc/passwd record relating to username */
86     this_passwd = getpwnam ( username );
87
88     /* return false if there is no structure returned */
89     if (this_passwd == NULL) return 0;
90
91     /* return proper uid */
92     return this_passwd->pw_uid;
93
94 } /* end function int user_to_uid ( username ) */
95
96 int group_to_gid ( group )
97 char    *group;
98 {
99     struct group *this_group;
100
101     /* free memory for pointer */
102     this_group = malloc ( sizeof ( struct group ) );
103
104     /* check for anything */
105     if ( strlen ( group ) < 1 ) return 0;
106
107     /* grab the /etc/groups record relating to group */
108     this_group = getgrnam ( group );
109
110     /* return false if there is no structure returned */
111     if (this_group == NULL) return 0;
112
113     /* return proper gid */
114     return this_group->gr_gid;
115
116 } /* end function int group_to_gid ( group ) */
117
118 #endif /* FORCE_UIDGID */