chiark / gitweb /
Release 1.2.2.
[checkpath] / utils.c
1 /* -*-c-*-
2  *
3  * Utilities not worth librarifying
4  *
5  * (c) 2008 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of chkpath.
11  *
12  * chkpath is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * chkpath is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with chkpath; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "config.h"
30
31 #include <ctype.h>
32
33 #include <sys/types.h>
34
35 #include <grp.h>
36
37 #include <mLib/macros.h>
38 #include <mLib/report.h>
39
40 #include "checkpath.h"
41 #include "utils.h"
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @allowgroup@ --- *
46  *
47  * Arguments:   @struct checkpath *cp@ = pointer to structure to mess with
48  *              @const char *gname@ = trust group @gname@
49  *
50  * Returns:     ---
51  *
52  * Use:         Adds the gid corresponding to @gname@ (which may be a number)
53  *              to the list of things we trust.
54  */
55
56 void allowgroup(struct checkpath *cp, const char *gname)
57 {
58   struct group *gr;
59   const char *p;
60   gid_t g;
61
62   /* --- Check for numeric group spec --- */
63
64   for (p = gname; *p; p++) {
65     if (!isdigit((unsigned char)*p))
66       goto lookup;
67   }
68   g = atoi(gname);
69   goto insert;
70
71   /* --- Look up a group by name --- */
72
73 lookup:
74   if ((gr = getgrnam(gname)) == 0)
75     die(1, "group %s not found", gname);
76   g = gr->gr_gid;
77
78   /* --- Insert the group into the table --- */
79
80 insert:
81   if (cp->cp_gids >= N(cp->cp_gid))
82     die(1, "too many groups");
83   cp->cp_gid[cp->cp_gids++] = g;
84 }
85
86 /*----- That's all, folks -------------------------------------------------*/