Imported Upstream version 1.8.2
[debian/sudo] / plugins / sudoers / match.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2011
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18  *
19  * Sponsored in part by the Defense Advanced Research Projects
20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #ifdef STDC_HEADERS
32 # include <stdlib.h>
33 # include <stddef.h>
34 #else
35 # ifdef HAVE_STDLIB_H
36 #  include <stdlib.h>
37 # endif
38 #endif /* STDC_HEADERS */
39 #ifdef HAVE_STRING_H
40 # include <string.h>
41 #endif /* HAVE_STRING_H */
42 #ifdef HAVE_STRINGS_H
43 # include <strings.h>
44 #endif /* HAVE_STRINGS_H */
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif /* HAVE_UNISTD_H */
48 #ifdef HAVE_FNMATCH
49 # include <fnmatch.h>
50 #endif /* HAVE_FNMATCH */
51 #ifdef HAVE_EXTENDED_GLOB
52 # include <glob.h>
53 #endif /* HAVE_EXTENDED_GLOB */
54 #ifdef HAVE_NETGROUP_H
55 # include <netgroup.h>
56 #endif /* HAVE_NETGROUP_H */
57 #include <ctype.h>
58 #include <pwd.h>
59 #include <grp.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <netdb.h>
63 #ifdef HAVE_DIRENT_H
64 # include <dirent.h>
65 # define NAMLEN(dirent) strlen((dirent)->d_name)
66 #else
67 # define dirent direct
68 # define NAMLEN(dirent) (dirent)->d_namlen
69 # ifdef HAVE_SYS_NDIR_H
70 #  include <sys/ndir.h>
71 # endif
72 # ifdef HAVE_SYS_DIR_H
73 #  include <sys/dir.h>
74 # endif
75 # ifdef HAVE_NDIR_H
76 #  include <ndir.h>
77 # endif
78 #endif
79
80 #include "sudoers.h"
81 #include "interfaces.h"
82 #include "parse.h"
83 #include <gram.h>
84
85 #ifndef HAVE_FNMATCH
86 # include "compat/fnmatch.h"
87 #endif /* HAVE_FNMATCH */
88 #ifndef HAVE_EXTENDED_GLOB
89 # include "compat/glob.h"
90 #endif /* HAVE_EXTENDED_GLOB */
91
92 static struct member_list empty;
93
94 static int command_matches_dir(char *, size_t);
95 static int command_matches_glob(char *, char *);
96 static int command_matches_fnmatch(char *, char *);
97 static int command_matches_normal(char *, char *);
98
99 /*
100  * Returns TRUE if string 's' contains meta characters.
101  */
102 #define has_meta(s)     (strpbrk(s, "\\?*[]") != NULL)
103
104 /*
105  * Check for user described by pw in a list of members.
106  * Returns ALLOW, DENY or UNSPEC.
107  */
108 static int
109 _userlist_matches(struct passwd *pw, struct member_list *list)
110 {
111     struct member *m;
112     struct alias *a;
113     int rval, matched = UNSPEC;
114
115     tq_foreach_rev(list, m) {
116         switch (m->type) {
117             case ALL:
118                 matched = !m->negated;
119                 break;
120             case NETGROUP:
121                 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
122                     matched = !m->negated;
123                 break;
124             case USERGROUP:
125                 if (usergr_matches(m->name, pw->pw_name, pw))
126                     matched = !m->negated;
127                 break;
128             case ALIAS:
129                 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
130                     rval = _userlist_matches(pw, &a->members);
131                     if (rval != UNSPEC)
132                         matched = m->negated ? !rval : rval;
133                     break;
134                 }
135                 /* FALLTHROUGH */
136             case WORD:
137                 if (userpw_matches(m->name, pw->pw_name, pw))
138                     matched = !m->negated;
139                 break;
140         }
141         if (matched != UNSPEC)
142             break;
143     }
144     return matched;
145 }
146
147 int
148 userlist_matches(struct passwd *pw, struct member_list *list)
149 {
150     alias_seqno++;
151     return _userlist_matches(pw, list);
152 }
153
154 /*
155  * Check for user described by pw in a list of members.
156  * If both lists are empty compare against def_runas_default.
157  * Returns ALLOW, DENY or UNSPEC.
158  */
159 static int
160 _runaslist_matches(struct member_list *user_list, struct member_list *group_list)
161 {
162     struct member *m;
163     struct alias *a;
164     int rval;
165     int user_matched = UNSPEC;
166     int group_matched = UNSPEC;
167
168     if (runas_pw != NULL) {
169         /* If no runas user or runas group listed in sudoers, use default. */
170         if (tq_empty(user_list) && tq_empty(group_list))
171             return userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw);
172
173         tq_foreach_rev(user_list, m) {
174             switch (m->type) {
175                 case ALL:
176                     user_matched = !m->negated;
177                     break;
178                 case NETGROUP:
179                     if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
180                         user_matched = !m->negated;
181                     break;
182                 case USERGROUP:
183                     if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
184                         user_matched = !m->negated;
185                     break;
186                 case ALIAS:
187                     if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
188                         rval = _runaslist_matches(&a->members, &empty);
189                         if (rval != UNSPEC)
190                             user_matched = m->negated ? !rval : rval;
191                         break;
192                     }
193                     /* FALLTHROUGH */
194                 case WORD:
195                     if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
196                         user_matched = !m->negated;
197                     break;
198             }
199             if (user_matched != UNSPEC)
200                 break;
201         }
202     }
203
204     if (runas_gr != NULL) {
205         if (user_matched == UNSPEC) {
206             if (runas_pw == NULL || strcmp(runas_pw->pw_name, user_name) == 0)
207                 user_matched = ALLOW;   /* only changing group */
208         }
209         tq_foreach_rev(group_list, m) {
210             switch (m->type) {
211                 case ALL:
212                     group_matched = !m->negated;
213                     break;
214                 case ALIAS:
215                     if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
216                         rval = _runaslist_matches(&a->members, &empty);
217                         if (rval != UNSPEC)
218                             group_matched = m->negated ? !rval : rval;
219                         break;
220                     }
221                     /* FALLTHROUGH */
222                 case WORD:
223                     if (group_matches(m->name, runas_gr))
224                         group_matched = !m->negated;
225                     break;
226             }
227             if (group_matched != UNSPEC)
228                 break;
229         }
230         if (group_matched == UNSPEC) {
231             if (runas_pw != NULL && runas_pw->pw_gid == runas_gr->gr_gid)
232                 group_matched = ALLOW;  /* runas group matches passwd db */
233         }
234     }
235
236     if (user_matched == DENY || group_matched == DENY)
237         return DENY;
238     if (user_matched == group_matched || runas_gr == NULL)
239         return user_matched;
240     return UNSPEC;
241 }
242
243 int
244 runaslist_matches(struct member_list *user_list, struct member_list *group_list)
245 {
246     alias_seqno++;
247     return _runaslist_matches(user_list ? user_list : &empty,
248         group_list ? group_list : &empty);
249 }
250
251 /*
252  * Check for host and shost in a list of members.
253  * Returns ALLOW, DENY or UNSPEC.
254  */
255 static int
256 _hostlist_matches(struct member_list *list)
257 {
258     struct member *m;
259     struct alias *a;
260     int rval, matched = UNSPEC;
261
262     tq_foreach_rev(list, m) {
263         switch (m->type) {
264             case ALL:
265                 matched = !m->negated;
266                 break;
267             case NETGROUP:
268                 if (netgr_matches(m->name, user_host, user_shost, NULL))
269                     matched = !m->negated;
270                 break;
271             case NTWKADDR:
272                 if (addr_matches(m->name))
273                     matched = !m->negated;
274                 break;
275             case ALIAS:
276                 if ((a = alias_find(m->name, HOSTALIAS)) != NULL) {
277                     rval = _hostlist_matches(&a->members);
278                     if (rval != UNSPEC)
279                         matched = m->negated ? !rval : rval;
280                     break;
281                 }
282                 /* FALLTHROUGH */
283             case WORD:
284                 if (hostname_matches(user_shost, user_host, m->name))
285                     matched = !m->negated;
286                 break;
287         }
288         if (matched != UNSPEC)
289             break;
290     }
291     return matched;
292 }
293
294 int
295 hostlist_matches(struct member_list *list)
296 {
297     alias_seqno++;
298     return _hostlist_matches(list);
299 }
300
301 /*
302  * Check for cmnd and args in a list of members.
303  * Returns ALLOW, DENY or UNSPEC.
304  */
305 static int
306 _cmndlist_matches(struct member_list *list)
307 {
308     struct member *m;
309     int matched = UNSPEC;
310
311     tq_foreach_rev(list, m) {
312         matched = cmnd_matches(m);
313         if (matched != UNSPEC)
314             break;
315     }
316     return matched;
317 }
318
319 int
320 cmndlist_matches(struct member_list *list)
321 {
322     alias_seqno++;
323     return _cmndlist_matches(list);
324 }
325
326 /*
327  * Check cmnd and args.
328  * Returns ALLOW, DENY or UNSPEC.
329  */
330 int
331 cmnd_matches(struct member *m)
332 {
333     struct alias *a;
334     struct sudo_command *c;
335     int rval, matched = UNSPEC;
336
337     switch (m->type) {
338         case ALL:
339             matched = !m->negated;
340             break;
341         case ALIAS:
342             alias_seqno++;
343             if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
344                 rval = _cmndlist_matches(&a->members);
345                 if (rval != UNSPEC)
346                     matched = m->negated ? !rval : rval;
347             }
348             break;
349         case COMMAND:
350             c = (struct sudo_command *)m->name;
351             if (command_matches(c->cmnd, c->args))
352                 matched = !m->negated;
353             break;
354     }
355     return matched;
356 }
357
358 static int
359 command_args_match(sudoers_cmnd, sudoers_args)
360     char *sudoers_cmnd;
361     char *sudoers_args;
362 {
363     int flags = 0;
364
365     /*
366      * If no args specified in sudoers, any user args are allowed.
367      * If the empty string is specified in sudoers, no user args are allowed.
368      */
369     if (!sudoers_args ||
370         (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)))
371         return TRUE;
372     /*
373      * If args are specified in sudoers, they must match the user args.
374      * If running as sudoedit, all args are assumed to be paths.
375      */
376     if (sudoers_args) {
377         /* For sudoedit, all args are assumed to be pathnames. */
378         if (strcmp(sudoers_cmnd, "sudoedit") == 0)
379             flags = FNM_PATHNAME;
380         if (fnmatch(sudoers_args, user_args ? user_args : "", flags) == 0)
381             return TRUE;
382     }
383     return FALSE;
384 }
385
386 /*
387  * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
388  * otherwise, return TRUE if user_cmnd names one of the inodes in path.
389  */
390 int
391 command_matches(char *sudoers_cmnd, char *sudoers_args)
392 {
393     /* Check for pseudo-commands */
394     if (sudoers_cmnd[0] != '/') {
395         /*
396          * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
397          *  a) there are no args in sudoers OR
398          *  b) there are no args on command line and none req by sudoers OR
399          *  c) there are args in sudoers and on command line and they match
400          */
401         if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
402             strcmp(user_cmnd, "sudoedit") != 0)
403             return FALSE;
404         if (command_args_match(sudoers_cmnd, sudoers_args)) {
405             efree(safe_cmnd);
406             safe_cmnd = estrdup(sudoers_cmnd);
407             return TRUE;
408         } else
409             return FALSE;
410     }
411
412     if (has_meta(sudoers_cmnd)) {
413         /*
414          * If sudoers_cmnd has meta characters in it, we need to
415          * use glob(3) and/or fnmatch(3) to do the matching.
416          */
417         if (def_fast_glob)
418             return command_matches_fnmatch(sudoers_cmnd, sudoers_args);
419         return command_matches_glob(sudoers_cmnd, sudoers_args);
420     }
421     return command_matches_normal(sudoers_cmnd, sudoers_args);
422 }
423
424 static int
425 command_matches_fnmatch(char *sudoers_cmnd, char *sudoers_args)
426 {
427     /*
428      * Return true if fnmatch(3) succeeds AND
429      *  a) there are no args in sudoers OR
430      *  b) there are no args on command line and none required by sudoers OR
431      *  c) there are args in sudoers and on command line and they match
432      * else return false.
433      */
434     if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
435         return FALSE;
436     if (command_args_match(sudoers_cmnd, sudoers_args)) {
437         if (safe_cmnd)
438             free(safe_cmnd);
439         safe_cmnd = estrdup(user_cmnd);
440         return TRUE;
441     } else
442         return FALSE;
443 }
444
445 static int
446 command_matches_glob(char *sudoers_cmnd, char *sudoers_args)
447 {
448     struct stat sudoers_stat;
449     size_t dlen;
450     char **ap, *base, *cp;
451     glob_t gl;
452
453     /*
454      * First check to see if we can avoid the call to glob(3).
455      * Short circuit if there are no meta chars in the command itself
456      * and user_base and basename(sudoers_cmnd) don't match.
457      */
458     dlen = strlen(sudoers_cmnd);
459     if (sudoers_cmnd[dlen - 1] != '/') {
460         if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
461             base++;
462             if (!has_meta(base) && strcmp(user_base, base) != 0)
463                 return FALSE;
464         }
465     }
466     /*
467      * Return true if we find a match in the glob(3) results AND
468      *  a) there are no args in sudoers OR
469      *  b) there are no args on command line and none required by sudoers OR
470      *  c) there are args in sudoers and on command line and they match
471      * else return false.
472      */
473 #define GLOB_FLAGS      (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
474     if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0 || gl.gl_pathc == 0) {
475         globfree(&gl);
476         return FALSE;
477     }
478     /* For each glob match, compare basename, st_dev and st_ino. */
479     for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
480         /* If it ends in '/' it is a directory spec. */
481         dlen = strlen(cp);
482         if (cp[dlen - 1] == '/') {
483             if (command_matches_dir(cp, dlen))
484                 return TRUE;
485             continue;
486         }
487
488         /* Only proceed if user_base and basename(cp) match */
489         if ((base = strrchr(cp, '/')) != NULL)
490             base++;
491         else
492             base = cp;
493         if (strcmp(user_base, base) != 0 ||
494             stat(cp, &sudoers_stat) == -1)
495             continue;
496         if (user_stat == NULL ||
497             (user_stat->st_dev == sudoers_stat.st_dev &&
498             user_stat->st_ino == sudoers_stat.st_ino)) {
499             efree(safe_cmnd);
500             safe_cmnd = estrdup(cp);
501             break;
502         }
503     }
504     globfree(&gl);
505     if (cp == NULL)
506         return FALSE;
507
508     if (command_args_match(sudoers_cmnd, sudoers_args)) {
509         efree(safe_cmnd);
510         safe_cmnd = estrdup(user_cmnd);
511         return TRUE;
512     }
513     return FALSE;
514 }
515
516 static int
517 command_matches_normal(char *sudoers_cmnd, char *sudoers_args)
518 {
519     struct stat sudoers_stat;
520     char *base;
521     size_t dlen;
522
523     /* If it ends in '/' it is a directory spec. */
524     dlen = strlen(sudoers_cmnd);
525     if (sudoers_cmnd[dlen - 1] == '/')
526         return command_matches_dir(sudoers_cmnd, dlen);
527
528     /* Only proceed if user_base and basename(sudoers_cmnd) match */
529     if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
530         base = sudoers_cmnd;
531     else
532         base++;
533     if (strcmp(user_base, base) != 0 ||
534         stat(sudoers_cmnd, &sudoers_stat) == -1)
535         return FALSE;
536
537     /*
538      * Return true if inode/device matches AND
539      *  a) there are no args in sudoers OR
540      *  b) there are no args on command line and none req by sudoers OR
541      *  c) there are args in sudoers and on command line and they match
542      */
543     if (user_stat != NULL &&
544         (user_stat->st_dev != sudoers_stat.st_dev ||
545         user_stat->st_ino != sudoers_stat.st_ino))
546         return FALSE;
547     if (command_args_match(sudoers_cmnd, sudoers_args)) {
548         efree(safe_cmnd);
549         safe_cmnd = estrdup(sudoers_cmnd);
550         return TRUE;
551     }
552     return FALSE;
553 }
554
555 /*
556  * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
557  */
558 static int
559 command_matches_dir(char *sudoers_dir, size_t dlen)
560 {
561     struct stat sudoers_stat;
562     struct dirent *dent;
563     char buf[PATH_MAX];
564     DIR *dirp;
565
566     /*
567      * Grot through directory entries, looking for user_base.
568      */
569     dirp = opendir(sudoers_dir);
570     if (dirp == NULL)
571         return FALSE;
572
573     if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf)) {
574         closedir(dirp);
575         return FALSE;
576     }
577     while ((dent = readdir(dirp)) != NULL) {
578         /* ignore paths > PATH_MAX (XXX - log) */
579         buf[dlen] = '\0';
580         if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
581             continue;
582
583         /* only stat if basenames are the same */
584         if (strcmp(user_base, dent->d_name) != 0 ||
585             stat(buf, &sudoers_stat) == -1)
586             continue;
587         if (user_stat == NULL ||
588             (user_stat->st_dev == sudoers_stat.st_dev &&
589             user_stat->st_ino == sudoers_stat.st_ino)) {
590             efree(safe_cmnd);
591             safe_cmnd = estrdup(buf);
592             break;
593         }
594     }
595
596     closedir(dirp);
597     return dent != NULL;
598 }
599
600 static int
601 addr_matches_if(char *n)
602 {
603     union sudo_in_addr_un addr;
604     struct interface *ifp;
605 #ifdef HAVE_IN6_ADDR
606     int j;
607 #endif
608     int family;
609
610 #ifdef HAVE_IN6_ADDR
611     if (inet_pton(AF_INET6, n, &addr.ip6) > 0) {
612         family = AF_INET6;
613     } else
614 #endif
615     {
616         family = AF_INET;
617         addr.ip4.s_addr = inet_addr(n);
618     }
619
620     for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
621         if (ifp->family != family)
622             continue;
623         switch(family) {
624             case AF_INET:
625                 if (ifp->addr.ip4.s_addr == addr.ip4.s_addr ||
626                     (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
627                     == addr.ip4.s_addr)
628                     return TRUE;
629                 break;
630 #ifdef HAVE_IN6_ADDR
631             case AF_INET6:
632                 if (memcmp(ifp->addr.ip6.s6_addr, addr.ip6.s6_addr,
633                     sizeof(addr.ip6.s6_addr)) == 0)
634                     return TRUE;
635                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
636                     if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
637                         break;
638                 }
639                 if (j == sizeof(addr.ip6.s6_addr))
640                     return TRUE;
641 #endif
642         }
643     }
644
645     return FALSE;
646 }
647
648 static int
649 addr_matches_if_netmask(char *n, char *m)
650 {
651     int i;
652     union sudo_in_addr_un addr, mask;
653     struct interface *ifp;
654 #ifdef HAVE_IN6_ADDR
655     int j;
656 #endif
657     int family;
658
659 #ifdef HAVE_IN6_ADDR
660     if (inet_pton(AF_INET6, n, &addr.ip6) > 0)
661         family = AF_INET6;
662     else
663 #endif
664     {
665         family = AF_INET;
666         addr.ip4.s_addr = inet_addr(n);
667     }
668
669     if (family == AF_INET) {
670         if (strchr(m, '.'))
671             mask.ip4.s_addr = inet_addr(m);
672         else {
673             i = 32 - atoi(m);
674             mask.ip4.s_addr = 0xffffffff;
675             mask.ip4.s_addr >>= i;
676             mask.ip4.s_addr <<= i;
677             mask.ip4.s_addr = htonl(mask.ip4.s_addr);
678         }
679     }
680 #ifdef HAVE_IN6_ADDR
681     else {
682         if (inet_pton(AF_INET6, m, &mask.ip6) <= 0) {
683             j = atoi(m);
684             for (i = 0; i < 16; i++) {
685                 if (j < i * 8)
686                     mask.ip6.s6_addr[i] = 0;
687                 else if (i * 8 + 8 <= j)
688                     mask.ip6.s6_addr[i] = 0xff;
689                 else
690                     mask.ip6.s6_addr[i] = 0xff00 >> (j - i * 8);
691             }
692         }
693     }
694 #endif /* HAVE_IN6_ADDR */
695
696     for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
697         if (ifp->family != family)
698             continue;
699         switch(family) {
700             case AF_INET:
701                 if ((ifp->addr.ip4.s_addr & mask.ip4.s_addr) == addr.ip4.s_addr)
702                     return TRUE;
703 #ifdef HAVE_IN6_ADDR
704             case AF_INET6:
705                 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
706                     if ((ifp->addr.ip6.s6_addr[j] & mask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
707                         break;
708                 }
709                 if (j == sizeof(addr.ip6.s6_addr))
710                     return TRUE;
711 #endif /* HAVE_IN6_ADDR */
712         }
713     }
714
715     return FALSE;
716 }
717
718 /*
719  * Returns TRUE if "n" is one of our ip addresses or if
720  * "n" is a network that we are on, else returns FALSE.
721  */
722 int
723 addr_matches(char *n)
724 {
725     char *m;
726     int retval;
727
728     /* If there's an explicit netmask, use it. */
729     if ((m = strchr(n, '/'))) {
730         *m++ = '\0';
731         retval = addr_matches_if_netmask(n, m);
732         *(m - 1) = '/';
733     } else
734         retval = addr_matches_if(n);
735
736     return retval;
737 }
738
739 /*
740  * Returns TRUE if the hostname matches the pattern, else FALSE
741  */
742 int
743 hostname_matches(char *shost, char *lhost, char *pattern)
744 {
745     if (has_meta(pattern)) {
746         if (strchr(pattern, '.'))
747             return !fnmatch(pattern, lhost, FNM_CASEFOLD);
748         else
749             return !fnmatch(pattern, shost, FNM_CASEFOLD);
750     } else {
751         if (strchr(pattern, '.'))
752             return !strcasecmp(lhost, pattern);
753         else
754             return !strcasecmp(shost, pattern);
755     }
756 }
757
758 /*
759  *  Returns TRUE if the user/uid from sudoers matches the specified user/uid,
760  *  else returns FALSE.
761  */
762 int
763 userpw_matches(char *sudoers_user, char *user, struct passwd *pw)
764 {
765     if (pw != NULL && *sudoers_user == '#') {
766         uid_t uid = (uid_t) atoi(sudoers_user + 1);
767         if (uid == pw->pw_uid)
768             return TRUE;
769     }
770     return strcmp(sudoers_user, user) == 0;
771 }
772
773 /*
774  *  Returns TRUE if the group/gid from sudoers matches the specified group/gid,
775  *  else returns FALSE.
776  */
777 int
778 group_matches(char *sudoers_group, struct group *gr)
779 {
780     if (*sudoers_group == '#') {
781         gid_t gid = (gid_t) atoi(sudoers_group + 1);
782         if (gid == gr->gr_gid)
783             return TRUE;
784     }
785     return strcmp(gr->gr_name, sudoers_group) == 0;
786 }
787
788 /*
789  *  Returns TRUE if the given user belongs to the named group,
790  *  else returns FALSE.
791  */
792 int
793 usergr_matches(char *group, char *user, struct passwd *pw)
794 {
795     int matched = FALSE;
796     struct passwd *pw0 = NULL;
797
798     /* make sure we have a valid usergroup, sudo style */
799     if (*group++ != '%')
800         goto done;
801
802     if (*group == ':' && def_group_plugin) {
803         matched = group_plugin_query(user, group + 1, pw);
804         goto done;
805     }
806
807     /* look up user's primary gid in the passwd file */
808     if (pw == NULL) {
809         if ((pw0 = sudo_getpwnam(user)) == NULL)
810             goto done;
811         pw = pw0;
812     }
813
814     if (user_in_group(pw, group)) {
815         matched = TRUE;
816         goto done;
817     }
818
819     /* not a Unix group, could be an external group */
820     if (def_group_plugin && group_plugin_query(user, group, pw)) {
821         matched = TRUE;
822         goto done;
823     }
824
825 done:
826     if (pw0 != NULL)
827         pw_delref(pw0);
828
829     return matched;
830 }
831
832 /*
833  * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
834  * else return FALSE.  Either of "host", "shost" or "user" may be NULL
835  * in which case that argument is not checked...
836  *
837  * XXX - swap order of host & shost
838  */
839 int
840 netgr_matches(char *netgr, char *lhost, char *shost, char *user)
841 {
842     static char *domain;
843 #ifdef HAVE_GETDOMAINNAME
844     static int initialized;
845 #endif
846
847     /* make sure we have a valid netgroup, sudo style */
848     if (*netgr++ != '+')
849         return FALSE;
850
851 #ifdef HAVE_GETDOMAINNAME
852     /* get the domain name (if any) */
853     if (!initialized) {
854         domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
855         if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
856             efree(domain);
857             domain = NULL;
858         }
859         initialized = 1;
860     }
861 #endif /* HAVE_GETDOMAINNAME */
862
863 #ifdef HAVE_INNETGR
864     if (innetgr(netgr, lhost, user, domain))
865         return TRUE;
866     else if (lhost != shost && innetgr(netgr, shost, user, domain))
867         return TRUE;
868 #endif /* HAVE_INNETGR */
869
870     return FALSE;
871 }