add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / sudo_nss.c
1 /*
2  * Copyright (c) 2007-2009 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 #else
33 # ifdef HAVE_STRINGS_H
34 #  include <strings.h>
35 # endif
36 #endif /* HAVE_STRING_H */
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif /* HAVE_UNISTD_H */
40 #include <pwd.h>
41 #include <grp.h>
42 #include <ctype.h>
43
44 #include "sudo.h"
45 #include "lbuf.h"
46
47 extern struct sudo_nss sudo_nss_file;
48 #ifdef HAVE_LDAP
49 extern struct sudo_nss sudo_nss_ldap;
50 #endif
51
52 #if defined(HAVE_LDAP) && defined(_PATH_NSSWITCH_CONF)
53 /*
54  * Read in /etc/nsswitch.conf
55  * Returns a tail queue of matches.
56  */
57 struct sudo_nss_list *
58 sudo_read_nss()
59 {
60     FILE *fp;
61     char *cp;
62     int saw_files = FALSE;
63     int saw_ldap = FALSE;
64     int got_match = FALSE;
65     static struct sudo_nss_list snl;
66
67     if ((fp = fopen(_PATH_NSSWITCH_CONF, "r")) == NULL)
68         goto nomatch;
69
70     while ((cp = sudo_parseln(fp)) != NULL) {
71         /* Skip blank or comment lines */
72         if (*cp == '\0')
73             continue;
74
75         /* Look for a line starting with "sudoers:" */
76         if (strncasecmp(cp, "sudoers:", 8) != 0)
77             continue;
78
79         /* Parse line */
80         for ((cp = strtok(cp + 8, " \t")); cp != NULL; (cp = strtok(NULL, " \t"))) {
81             if (strcasecmp(cp, "files") == 0 && !saw_files) {
82                 tq_append(&snl, &sudo_nss_file);
83                 got_match = TRUE;
84             } else if (strcasecmp(cp, "ldap") == 0 && !saw_ldap) {
85                 tq_append(&snl, &sudo_nss_ldap);
86                 got_match = TRUE;
87             } else if (strcasecmp(cp, "[NOTFOUND=return]") == 0 && got_match) {
88                 /* NOTFOUND affects the most recent entry */
89                 tq_last(&snl)->ret_if_notfound = TRUE;
90                 got_match = FALSE;
91             } else
92                 got_match = FALSE;
93         }
94         /* Only parse the first "sudoers:" line */
95         break;
96     }
97     fclose(fp);
98
99 nomatch:
100     /* Default to files only if no matches */
101     if (tq_empty(&snl))
102         tq_append(&snl, &sudo_nss_file);
103
104     return(&snl);
105 }
106
107 #else /* HAVE_LDAP && _PATH_NSSWITCH_CONF */
108
109 # if defined(HAVE_LDAP) && defined(_PATH_NETSVC_CONF)
110
111 /*
112  * Read in /etc/netsvc.conf (like nsswitch.conf on AIX)
113  * Returns a tail queue of matches.
114  */
115 struct sudo_nss_list *
116 sudo_read_nss()
117 {
118     FILE *fp;
119     char *cp, *ep;
120     int saw_files = FALSE;
121     int saw_ldap = FALSE;
122     int got_match = FALSE;
123     static struct sudo_nss_list snl;
124
125     if ((fp = fopen(_PATH_NETSVC_CONF, "r")) == NULL)
126         goto nomatch;
127
128     while ((cp = sudo_parseln(fp)) != NULL) {
129         /* Skip blank or comment lines */
130         if (*cp == '\0')
131             continue;
132
133         /* Look for a line starting with "sudoers = " */
134         if (strncasecmp(cp, "sudoers", 7) != 0)
135             continue;
136         cp += 7;
137         while (isspace((unsigned char)*cp))
138             cp++;
139         if (*cp++ != '=')
140             continue;
141
142         /* Parse line */
143         for ((cp = strtok(cp, ",")); cp != NULL; (cp = strtok(NULL, ","))) {
144             /* Trim leading whitespace. */
145             while (isspace((unsigned char)*cp))
146                 cp++;
147
148             if (!saw_files && strncasecmp(cp, "files", 5) == 0 &&
149                 (isspace((unsigned char)cp[5]) || cp[5] == '\0')) {
150                 tq_append(&snl, &sudo_nss_file);
151                 got_match = TRUE;
152                 ep = &cp[5];
153             } else if (!saw_ldap && strncasecmp(cp, "ldap", 4) == 0 &&
154                 (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
155                 tq_append(&snl, &sudo_nss_ldap);
156                 got_match = TRUE;
157                 ep = &cp[4];
158             } else {
159                 got_match = FALSE;
160             }
161
162             /* check for = auth qualifier */
163             if (got_match && *ep) {
164                 cp = ep;
165                 while (isspace((unsigned char)*cp) || *cp == '=')
166                     cp++;
167                 if (strncasecmp(cp, "auth", 4) == 0 &&
168                     (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
169                     tq_last(&snl)->ret_if_found = TRUE;
170                 }
171             }
172         }
173         /* Only parse the first "sudoers" line */
174         break;
175     }
176     fclose(fp);
177
178 nomatch:
179     /* Default to files only if no matches */
180     if (tq_empty(&snl))
181         tq_append(&snl, &sudo_nss_file);
182
183     return(&snl);
184 }
185
186 # else /* !_PATH_NETSVC_CONF && !_PATH_NSSWITCH_CONF */
187
188 /*
189  * Non-nsswitch.conf version with hard-coded order.
190  */
191 struct sudo_nss_list *
192 sudo_read_nss()
193 {
194     static struct sudo_nss_list snl;
195
196 #  ifdef HAVE_LDAP
197     tq_append(&snl, &sudo_nss_ldap);
198 #  endif
199     tq_append(&snl, &sudo_nss_file);
200
201     return(&snl);
202 }
203
204 # endif /* !HAVE_LDAP || !_PATH_NETSVC_CONF */
205
206 #endif /* HAVE_LDAP && _PATH_NSSWITCH_CONF */
207
208 /* Reset user_groups based on passwd entry. */
209 static void
210 reset_groups(pw)
211     struct passwd *pw;
212 {
213 #if defined(HAVE_INITGROUPS) && defined(HAVE_GETGROUPS)
214     if (pw != sudo_user.pw) {
215         (void) initgroups(pw->pw_name, pw->pw_gid);
216         if ((user_ngroups = getgroups(0, NULL)) > 0) {
217             user_groups = erealloc3(user_groups, user_ngroups,
218                 sizeof(GETGROUPS_T));
219             if (getgroups(user_ngroups, user_groups) < 0)
220                 log_error(USE_ERRNO|MSG_ONLY, "can't get group vector");
221         } else {
222             user_ngroups = 0;
223             efree(user_groups);
224         }
225     }
226 #endif
227 }
228
229 /*
230  * Print out privileges for the specified user.
231  * We only get here if the user is allowed to run something on this host.
232  */
233 void
234 display_privs(snl, pw)
235     struct sudo_nss_list *snl;
236     struct passwd *pw;
237 {
238     struct sudo_nss *nss;
239     struct lbuf lbuf;
240     int count;
241
242     /* Reset group vector so group matching works correctly. */
243     reset_groups(pw);
244
245     lbuf_init(&lbuf, NULL, 4, 0);
246
247     /* Display defaults from all sources. */
248     count = 0;
249     tq_foreach_fwd(snl, nss)
250         count += nss->display_defaults(nss, pw, &lbuf);
251     if (count) {
252         printf("Matching Defaults entries for %s on this host:\n", pw->pw_name);
253         lbuf_print(&lbuf);
254         putchar('\n');
255     }
256
257     /* Display Runas and Cmnd-specific defaults from all sources. */
258     count = 0;
259     tq_foreach_fwd(snl, nss)
260         count += nss->display_bound_defaults(nss, pw, &lbuf);
261     if (count) {
262         printf("Runas and Command-specific defaults for %s:\n", pw->pw_name);
263         lbuf_print(&lbuf);
264         putchar('\n');
265     }
266
267     /* Display privileges from all sources. */
268     printf("User %s may run the following commands on this host:\n",
269         pw->pw_name);
270     tq_foreach_fwd(snl, nss)
271         (void) nss->display_privs(nss, pw, &lbuf);
272     if (lbuf.len != 0)
273         lbuf_print(&lbuf);              /* print remainder, if any */
274     lbuf_destroy(&lbuf);
275 }
276
277 /*
278  * Check user_cmnd against sudoers and print the matching entry if the
279  * command is allowed.
280  */
281 int
282 display_cmnd(snl, pw)
283     struct sudo_nss_list *snl;
284     struct passwd *pw;
285 {
286     struct sudo_nss *nss;
287
288     /* Reset group vector so group matching works correctly. */
289     reset_groups(pw);
290
291     tq_foreach_fwd(snl, nss) {
292         if (nss->display_cmnd(nss, pw) == 0)
293             return(0);
294     }
295     return(1);
296 }