Imported Upstream version 1.8.1p2
[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 <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 "sudoers.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(void)
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(void)
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(void)
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(struct passwd *pw)
210 {
211 #if defined(HAVE_INITGROUPS) && defined(HAVE_GETGROUPS)
212     if (pw != sudo_user.pw) {
213 # ifdef HAVE_SETAUTHDB
214         aix_setauthdb(pw->pw_name);
215 # endif
216         if (initgroups(pw->pw_name, pw->pw_gid) == -1)
217             log_error(USE_ERRNO|MSG_ONLY, "can't reset group vector");
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
230 }
231
232 static int
233 output(const char *buf)
234 {
235     struct sudo_conv_message msg;
236     struct sudo_conv_reply repl;
237
238     /* Call conversation function */
239     memset(&msg, 0, sizeof(msg));
240     msg.msg_type = SUDO_CONV_INFO_MSG;
241     msg.msg = buf;
242     memset(&repl, 0, sizeof(repl));
243     if (sudo_conv(1, &msg, &repl) == -1)
244         return 0;
245     return (int)strlen(buf);
246 }
247
248 /*
249  * Print out privileges for the specified user.
250  * We only get here if the user is allowed to run something on this host.
251  */
252 void
253 display_privs(struct sudo_nss_list *snl, struct passwd *pw)
254 {
255     struct sudo_nss *nss;
256     struct lbuf defs, privs;
257     int count, olen;
258
259     /* Reset group vector so group matching works correctly. */
260     reset_groups(pw);
261
262     lbuf_init(&defs, output, 4, NULL, sudo_user.cols);
263     lbuf_init(&privs, output, 4, NULL, sudo_user.cols);
264
265     /* Display defaults from all sources. */
266     lbuf_append(&defs, "Matching Defaults entries for ", pw->pw_name,
267         " on this host:\n", NULL);
268     count = 0;
269     tq_foreach_fwd(snl, nss) {
270         count += nss->display_defaults(nss, pw, &defs);
271     }
272     if (count)
273         lbuf_append(&defs, "\n\n", NULL);
274     else
275         defs.len = 0;
276
277     /* Display Runas and Cmnd-specific defaults from all sources. */
278     olen = defs.len;
279     lbuf_append(&defs, "Runas and Command-specific defaults for ", pw->pw_name,
280         ":\n", NULL);
281     count = 0;
282     tq_foreach_fwd(snl, nss) {
283         count += nss->display_bound_defaults(nss, pw, &defs);
284     }
285     if (count)
286         lbuf_append(&defs, "\n\n", NULL);
287     else
288         defs.len = olen;
289
290     /* Display privileges from all sources. */
291     lbuf_append(&privs, "User ", pw->pw_name,
292         " may run the following commands on this host:\n", NULL);
293     count = 0;
294     tq_foreach_fwd(snl, nss) {
295         count += nss->display_privs(nss, pw, &privs);
296     }
297     if (count) {
298         lbuf_print(&defs);
299         lbuf_print(&privs);
300     } else {
301         printf("User %s is not allowed to run sudo on %s.\n", pw->pw_name,
302             user_shost);
303     }
304
305     lbuf_destroy(&defs);
306     lbuf_destroy(&privs);
307 }
308
309 /*
310  * Check user_cmnd against sudoers and print the matching entry if the
311  * command is allowed.
312  * Returns TRUE if the command is allowed, else FALSE.
313  */
314 int
315 display_cmnd(struct sudo_nss_list *snl, struct passwd *pw)
316 {
317     struct sudo_nss *nss;
318
319     /* Reset group vector so group matching works correctly. */
320     reset_groups(pw);
321
322     tq_foreach_fwd(snl, nss) {
323         if (nss->display_cmnd(nss, pw) == 0)
324             return TRUE;
325     }
326     return FALSE;
327 }