patch from Svante Signell to fix build problem on GNU/Hurd, closes: #611290
[debian/sudo] / sudo_nss.c
1 /*
2  * Copyright (c) 2007-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <stdio.h>
22 #ifdef STDC_HEADERS
23 # include <stdlib.h>
24 # include <stddef.h>
25 #else
26 # ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 # endif
29 #endif /* STDC_HEADERS */
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #endif /* HAVE_STRING_H */
33 #ifdef HAVE_STRINGS_H
34 # include <strings.h>
35 #endif /* HAVE_STRINGS_H */
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39 #include <pwd.h>
40 #include <grp.h>
41 #include <ctype.h>
42
43 #include "sudo.h"
44 #include "lbuf.h"
45
46 extern struct sudo_nss sudo_nss_file;
47 #ifdef HAVE_LDAP
48 extern struct sudo_nss sudo_nss_ldap;
49 #endif
50
51 #if defined(HAVE_LDAP) && defined(_PATH_NSSWITCH_CONF)
52 /*
53  * Read in /etc/nsswitch.conf
54  * Returns a tail queue of matches.
55  */
56 struct sudo_nss_list *
57 sudo_read_nss()
58 {
59     FILE *fp;
60     char *cp;
61     int saw_files = FALSE;
62     int saw_ldap = FALSE;
63     int got_match = FALSE;
64     static struct sudo_nss_list snl;
65
66     if ((fp = fopen(_PATH_NSSWITCH_CONF, "r")) == NULL)
67         goto nomatch;
68
69     while ((cp = sudo_parseln(fp)) != NULL) {
70         /* Skip blank or comment lines */
71         if (*cp == '\0')
72             continue;
73
74         /* Look for a line starting with "sudoers:" */
75         if (strncasecmp(cp, "sudoers:", 8) != 0)
76             continue;
77
78         /* Parse line */
79         for ((cp = strtok(cp + 8, " \t")); cp != NULL; (cp = strtok(NULL, " \t"))) {
80             if (strcasecmp(cp, "files") == 0 && !saw_files) {
81                 tq_append(&snl, &sudo_nss_file);
82                 got_match = TRUE;
83             } else if (strcasecmp(cp, "ldap") == 0 && !saw_ldap) {
84                 tq_append(&snl, &sudo_nss_ldap);
85                 got_match = TRUE;
86             } else if (strcasecmp(cp, "[NOTFOUND=return]") == 0 && got_match) {
87                 /* NOTFOUND affects the most recent entry */
88                 tq_last(&snl)->ret_if_notfound = TRUE;
89                 got_match = FALSE;
90             } else
91                 got_match = FALSE;
92         }
93         /* Only parse the first "sudoers:" line */
94         break;
95     }
96     fclose(fp);
97
98 nomatch:
99     /* Default to files only if no matches */
100     if (tq_empty(&snl))
101         tq_append(&snl, &sudo_nss_file);
102
103     return(&snl);
104 }
105
106 #else /* HAVE_LDAP && _PATH_NSSWITCH_CONF */
107
108 # if defined(HAVE_LDAP) && defined(_PATH_NETSVC_CONF)
109
110 /*
111  * Read in /etc/netsvc.conf (like nsswitch.conf on AIX)
112  * Returns a tail queue of matches.
113  */
114 struct sudo_nss_list *
115 sudo_read_nss()
116 {
117     FILE *fp;
118     char *cp, *ep;
119     int saw_files = FALSE;
120     int saw_ldap = FALSE;
121     int got_match = FALSE;
122     static struct sudo_nss_list snl;
123
124     if ((fp = fopen(_PATH_NETSVC_CONF, "r")) == NULL)
125         goto nomatch;
126
127     while ((cp = sudo_parseln(fp)) != NULL) {
128         /* Skip blank or comment lines */
129         if (*cp == '\0')
130             continue;
131
132         /* Look for a line starting with "sudoers = " */
133         if (strncasecmp(cp, "sudoers", 7) != 0)
134             continue;
135         cp += 7;
136         while (isspace((unsigned char)*cp))
137             cp++;
138         if (*cp++ != '=')
139             continue;
140
141         /* Parse line */
142         for ((cp = strtok(cp, ",")); cp != NULL; (cp = strtok(NULL, ","))) {
143             /* Trim leading whitespace. */
144             while (isspace((unsigned char)*cp))
145                 cp++;
146
147             if (!saw_files && strncasecmp(cp, "files", 5) == 0 &&
148                 (isspace((unsigned char)cp[5]) || cp[5] == '\0')) {
149                 tq_append(&snl, &sudo_nss_file);
150                 got_match = TRUE;
151                 ep = &cp[5];
152             } else if (!saw_ldap && strncasecmp(cp, "ldap", 4) == 0 &&
153                 (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
154                 tq_append(&snl, &sudo_nss_ldap);
155                 got_match = TRUE;
156                 ep = &cp[4];
157             } else {
158                 got_match = FALSE;
159             }
160
161             /* check for = auth qualifier */
162             if (got_match && *ep) {
163                 cp = ep;
164                 while (isspace((unsigned char)*cp) || *cp == '=')
165                     cp++;
166                 if (strncasecmp(cp, "auth", 4) == 0 &&
167                     (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
168                     tq_last(&snl)->ret_if_found = TRUE;
169                 }
170             }
171         }
172         /* Only parse the first "sudoers" line */
173         break;
174     }
175     fclose(fp);
176
177 nomatch:
178     /* Default to files only if no matches */
179     if (tq_empty(&snl))
180         tq_append(&snl, &sudo_nss_file);
181
182     return(&snl);
183 }
184
185 # else /* !_PATH_NETSVC_CONF && !_PATH_NSSWITCH_CONF */
186
187 /*
188  * Non-nsswitch.conf version with hard-coded order.
189  */
190 struct sudo_nss_list *
191 sudo_read_nss()
192 {
193     static struct sudo_nss_list snl;
194
195 #  ifdef HAVE_LDAP
196     tq_append(&snl, &sudo_nss_ldap);
197 #  endif
198     tq_append(&snl, &sudo_nss_file);
199
200     return(&snl);
201 }
202
203 # endif /* !HAVE_LDAP || !_PATH_NETSVC_CONF */
204
205 #endif /* HAVE_LDAP && _PATH_NSSWITCH_CONF */
206
207 /* Reset user_groups based on passwd entry. */
208 static void
209 reset_groups(pw)
210     struct passwd *pw;
211 {
212 #if defined(HAVE_INITGROUPS) && defined(HAVE_GETGROUPS)
213     if (pw != sudo_user.pw) {
214 # ifdef HAVE_SETAUTHDB
215         aix_setauthdb(pw->pw_name);
216 # endif
217         (void) initgroups(pw->pw_name, pw->pw_gid);
218         efree(user_groups);
219         user_groups = NULL;
220         if ((user_ngroups = getgroups(0, NULL)) > 0) {
221             user_groups = emalloc2(user_ngroups, sizeof(GETGROUPS_T));
222             if (getgroups(user_ngroups, user_groups) < 0)
223                 log_error(USE_ERRNO|MSG_ONLY, "can't get group vector");
224         }
225 # ifdef HAVE_SETAUTHDB
226         aix_restoreauthdb();
227 # endif
228     }
229 #endif /* HAVE_INITGROUPS && HAVE_GETGROUPS */
230 }
231
232 static int
233 output(buf)
234     const char *buf;
235 {
236     return fputs(buf, stdout);
237 }
238
239 /*
240  * Print out privileges for the specified user.
241  * We only get here if the user is allowed to run something on this host.
242  */
243 void
244 display_privs(snl, pw)
245     struct sudo_nss_list *snl;
246     struct passwd *pw;
247 {
248     struct sudo_nss *nss;
249     struct lbuf lbuf;
250     int count;
251
252     /* Reset group vector so group matching works correctly. */
253     reset_groups(pw);
254
255     lbuf_init(&lbuf, output, 4, NULL);
256
257     /* Display defaults from all sources. */
258     lbuf_append(&lbuf, "Matching Defaults entries for ", pw->pw_name,
259         " on this host:\n", NULL);
260     count = 0;
261     tq_foreach_fwd(snl, nss) {
262         count += nss->display_defaults(nss, pw, &lbuf);
263     }
264     if (count) {
265         lbuf_append(&lbuf, "\n\n", NULL);
266         lbuf_print(&lbuf);
267     }
268
269     /* Display Runas and Cmnd-specific defaults from all sources. */
270     lbuf.len = 0;
271     lbuf_append(&lbuf, "Runas and Command-specific defaults for ", pw->pw_name,
272         ":\n", NULL);
273     count = 0;
274     tq_foreach_fwd(snl, nss) {
275         count += nss->display_bound_defaults(nss, pw, &lbuf);
276     }
277     if (count) {
278         lbuf_append(&lbuf, "\n\n", NULL);
279         lbuf_print(&lbuf);
280     }
281
282     /* Display privileges from all sources. */
283     lbuf.len = 0;
284     lbuf_append(&lbuf, "User ", pw->pw_name,
285         " may run the following commands on this host:\n", NULL);
286     count = 0;
287     tq_foreach_fwd(snl, nss) {
288         count += nss->display_privs(nss, pw, &lbuf);
289     }
290     if (count) {
291         lbuf_print(&lbuf);
292     }
293
294     lbuf_destroy(&lbuf);
295 }
296
297 /*
298  * Check user_cmnd against sudoers and print the matching entry if the
299  * command is allowed.
300  */
301 int
302 display_cmnd(snl, pw)
303     struct sudo_nss_list *snl;
304     struct passwd *pw;
305 {
306     struct sudo_nss *nss;
307
308     /* Reset group vector so group matching works correctly. */
309     reset_groups(pw);
310
311     tq_foreach_fwd(snl, nss) {
312         if (nss->display_cmnd(nss, pw) == 0)
313             return(0);
314     }
315     return(1);
316 }