83a3fe912a5d51794a198c8b697a1b62498a5309
[debian/sudo] / plugins / sudoers / sudo_nss.c
1 /*
2  * Copyright (c) 2007-2011 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 <sys/stat.h>
22
23 #include <stdio.h>
24 #ifdef STDC_HEADERS
25 # include <stdlib.h>
26 # include <stddef.h>
27 #else
28 # ifdef HAVE_STDLIB_H
29 #  include <stdlib.h>
30 # endif
31 #endif /* STDC_HEADERS */
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #endif /* HAVE_STRING_H */
35 #ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 #endif /* HAVE_STRINGS_H */
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif /* HAVE_UNISTD_H */
41 #include <pwd.h>
42 #include <grp.h>
43 #include <ctype.h>
44
45 #include "sudoers.h"
46 #include "lbuf.h"
47
48 extern struct sudo_nss sudo_nss_file;
49 #ifdef HAVE_LDAP
50 extern struct sudo_nss sudo_nss_ldap;
51 #endif
52 #ifdef HAVE_SSSD
53 extern struct sudo_nss sudo_nss_sss;
54 #endif
55
56 #if (defined(HAVE_LDAP) || defined(HAVE_SSSD)) && defined(_PATH_NSSWITCH_CONF)
57 /*
58  * Read in /etc/nsswitch.conf
59  * Returns a tail queue of matches.
60  */
61 struct sudo_nss_list *
62 sudo_read_nss(void)
63 {
64     FILE *fp;
65     char *cp;
66 #ifdef HAVE_SSSD
67     bool saw_sss = false;
68 #endif
69     bool saw_files = false;
70     bool saw_ldap = false;
71     bool got_match = false;
72     static struct sudo_nss_list snl;
73     debug_decl(sudo_read_nss, SUDO_DEBUG_NSS)
74
75     if ((fp = fopen(_PATH_NSSWITCH_CONF, "r")) == NULL)
76         goto nomatch;
77
78     while ((cp = sudo_parseln(fp)) != NULL) {
79         /* Skip blank or comment lines */
80         if (*cp == '\0')
81             continue;
82
83         /* Look for a line starting with "sudoers:" */
84         if (strncasecmp(cp, "sudoers:", 8) != 0)
85             continue;
86
87         /* Parse line */
88         for ((cp = strtok(cp + 8, " \t")); cp != NULL; (cp = strtok(NULL, " \t"))) {
89             if (strcasecmp(cp, "files") == 0 && !saw_files) {
90                 tq_append(&snl, &sudo_nss_file);
91                 got_match = true;
92 #ifdef HAVE_LDAP
93             } else if (strcasecmp(cp, "ldap") == 0 && !saw_ldap) {
94                 tq_append(&snl, &sudo_nss_ldap);
95                 got_match = true;
96 #endif
97 #ifdef HAVE_SSSD
98             } else if (strcasecmp(cp, "sss") == 0 && !saw_sss) {
99                 tq_append(&snl, &sudo_nss_sss);
100                 got_match = true;
101 #endif
102             } else if (strcasecmp(cp, "[NOTFOUND=return]") == 0 && got_match) {
103                 /* NOTFOUND affects the most recent entry */
104                 tq_last(&snl)->ret_if_notfound = true;
105                 got_match = false;
106             } else if (strcasecmp(cp, "[SUCCESS=return]") == 0 && got_match) {
107                 /* SUCCESS affects the most recent entry */
108                 tq_last(&snl)->ret_if_found = true;
109                 got_match = false;
110             } else
111                 got_match = false;
112         }
113         /* Only parse the first "sudoers:" line */
114         break;
115     }
116     fclose(fp);
117
118 nomatch:
119     /* Default to files only if no matches */
120     if (tq_empty(&snl))
121         tq_append(&snl, &sudo_nss_file);
122
123     debug_return_ptr(&snl);
124 }
125
126 #else /* (HAVE_LDAP || HAVE_SSSD) && _PATH_NSSWITCH_CONF */
127
128 # if (defined(HAVE_LDAP) || defined(HAVE_SSSD)) && defined(_PATH_NETSVC_CONF)
129
130 /*
131  * Read in /etc/netsvc.conf (like nsswitch.conf on AIX)
132  * Returns a tail queue of matches.
133  */
134 struct sudo_nss_list *
135 sudo_read_nss(void)
136 {
137     FILE *fp;
138     char *cp, *ep;
139 #ifdef HAVE_SSSD
140     bool saw_sss = false;
141 #endif
142     bool saw_files = false;
143     bool saw_ldap = false;
144     bool got_match = false;
145     static struct sudo_nss_list snl;
146     debug_decl(sudo_read_nss, SUDO_DEBUG_NSS)
147
148     if ((fp = fopen(_PATH_NETSVC_CONF, "r")) == NULL)
149         goto nomatch;
150
151     while ((cp = sudo_parseln(fp)) != NULL) {
152         /* Skip blank or comment lines */
153         if (*cp == '\0')
154             continue;
155
156         /* Look for a line starting with "sudoers = " */
157         if (strncasecmp(cp, "sudoers", 7) != 0)
158             continue;
159         cp += 7;
160         while (isspace((unsigned char)*cp))
161             cp++;
162         if (*cp++ != '=')
163             continue;
164
165         /* Parse line */
166         for ((cp = strtok(cp, ",")); cp != NULL; (cp = strtok(NULL, ","))) {
167             /* Trim leading whitespace. */
168             while (isspace((unsigned char)*cp))
169                 cp++;
170
171             if (!saw_files && strncasecmp(cp, "files", 5) == 0 &&
172                 (isspace((unsigned char)cp[5]) || cp[5] == '\0')) {
173                 tq_append(&snl, &sudo_nss_file);
174                 got_match = true;
175                 ep = &cp[5];
176 #ifdef HAVE_LDAP
177             } else if (!saw_ldap && strncasecmp(cp, "ldap", 4) == 0 &&
178                 (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
179                 tq_append(&snl, &sudo_nss_ldap);
180                 got_match = true;
181                 ep = &cp[4];
182 #endif
183 #ifdef HAVE_SSSD
184             } else if (!saw_sss && strncasecmp(cp, "sss", 3) == 0 &&
185                 (isspace((unsigned char)cp[3]) || cp[3] == '\0')) {
186                 tq_append(&snl, &sudo_nss_sss);
187                 got_match = true;
188                 ep = &cp[3];
189 #endif
190             } else {
191                 got_match = false;
192             }
193
194             /* check for = auth qualifier */
195             if (got_match && *ep) {
196                 cp = ep;
197                 while (isspace((unsigned char)*cp) || *cp == '=')
198                     cp++;
199                 if (strncasecmp(cp, "auth", 4) == 0 &&
200                     (isspace((unsigned char)cp[4]) || cp[4] == '\0')) {
201                     tq_last(&snl)->ret_if_found = true;
202                 }
203             }
204         }
205         /* Only parse the first "sudoers" line */
206         break;
207     }
208     fclose(fp);
209
210 nomatch:
211     /* Default to files only if no matches */
212     if (tq_empty(&snl))
213         tq_append(&snl, &sudo_nss_file);
214
215     debug_return_ptr(&snl);
216 }
217
218 # else /* !_PATH_NETSVC_CONF && !_PATH_NSSWITCH_CONF */
219
220 /*
221  * Non-nsswitch.conf version with hard-coded order.
222  */
223 struct sudo_nss_list *
224 sudo_read_nss(void)
225 {
226     static struct sudo_nss_list snl;
227     debug_decl(sudo_read_nss, SUDO_DEBUG_NSS)
228
229 #  ifdef HAVE_SSSD
230     tq_append(&snl, &sudo_nss_sss);
231 #  endif
232 #  ifdef HAVE_LDAP
233     tq_append(&snl, &sudo_nss_ldap);
234 #  endif
235     tq_append(&snl, &sudo_nss_file);
236
237     debug_return_ptr(&snl);
238 }
239
240 # endif /* !HAVE_LDAP || !_PATH_NETSVC_CONF */
241
242 #endif /* HAVE_LDAP && _PATH_NSSWITCH_CONF */
243
244 static int
245 output(const char *buf)
246 {
247     struct sudo_conv_message msg;
248     struct sudo_conv_reply repl;
249     debug_decl(output, SUDO_DEBUG_NSS)
250
251     /* Call conversation function */
252     memset(&msg, 0, sizeof(msg));
253     msg.msg_type = SUDO_CONV_INFO_MSG;
254     msg.msg = buf;
255     memset(&repl, 0, sizeof(repl));
256     if (sudo_conv(1, &msg, &repl) == -1)
257         debug_return_int(0);
258     debug_return_int(strlen(buf));
259 }
260
261 /*
262  * Print out privileges for the specified user.
263  * We only get here if the user is allowed to run something on this host.
264  */
265 void
266 display_privs(struct sudo_nss_list *snl, struct passwd *pw)
267 {
268     struct sudo_nss *nss;
269     struct lbuf defs, privs;
270     struct stat sb;
271     int cols, count, olen;
272     debug_decl(display_privs, SUDO_DEBUG_NSS)
273
274     cols = sudo_user.cols;
275     if (fstat(STDOUT_FILENO, &sb) == 0 && S_ISFIFO(sb.st_mode))
276         cols = 0;
277     lbuf_init(&defs, output, 4, NULL, cols);
278     lbuf_init(&privs, output, 4, NULL, cols);
279
280     /* Display defaults from all sources. */
281     lbuf_append(&defs, _("Matching Defaults entries for %s on this host:\n"),
282         pw->pw_name);
283     count = 0;
284     tq_foreach_fwd(snl, nss) {
285         count += nss->display_defaults(nss, pw, &defs);
286     }
287     if (count)
288         lbuf_append(&defs, "\n\n");
289     else
290         defs.len = 0;
291
292     /* Display Runas and Cmnd-specific defaults from all sources. */
293     olen = defs.len;
294     lbuf_append(&defs, _("Runas and Command-specific defaults for %s:\n"),
295         pw->pw_name);
296     count = 0;
297     tq_foreach_fwd(snl, nss) {
298         count += nss->display_bound_defaults(nss, pw, &defs);
299     }
300     if (count)
301         lbuf_append(&defs, "\n\n");
302     else
303         defs.len = olen;
304
305     /* Display privileges from all sources. */
306     lbuf_append(&privs,
307         _("User %s may run the following commands on this host:\n"),
308         pw->pw_name);
309     count = 0;
310     tq_foreach_fwd(snl, nss) {
311         count += nss->display_privs(nss, pw, &privs);
312     }
313     if (count == 0) {
314         defs.len = 0;
315         privs.len = 0;
316         lbuf_append(&privs, _("User %s is not allowed to run sudo on %s.\n"),
317             pw->pw_name, user_shost);
318     }
319     lbuf_print(&defs);
320     lbuf_print(&privs);
321
322     lbuf_destroy(&defs);
323     lbuf_destroy(&privs);
324
325     debug_return;
326 }
327
328 /*
329  * Check user_cmnd against sudoers and print the matching entry if the
330  * command is allowed.
331  * Returns true if the command is allowed, else false.
332  */
333 bool
334 display_cmnd(struct sudo_nss_list *snl, struct passwd *pw)
335 {
336     struct sudo_nss *nss;
337     debug_decl(display_cmnd, SUDO_DEBUG_NSS)
338
339     tq_foreach_fwd(snl, nss) {
340         if (nss->display_cmnd(nss, pw) == 0)
341             debug_return_bool(true);
342     }
343     debug_return_bool(false);
344 }