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