chkgrp.c revision 79160
1/*-
2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef lint
30static const char rcsid[] =
31  "$FreeBSD: head/usr.sbin/chkgrp/chkgrp.c 79160 2001-07-03 21:40:34Z mjacob $";
32#endif /* not lint */
33
34#include <err.h>
35#include <ctype.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sysexits.h>
40
41static void
42usage(void)
43{
44    fprintf(stderr, "usage: chkgrp [groupfile]\n");
45    exit(EX_USAGE);
46}
47
48int
49main(int argc, char *argv[])
50{
51    unsigned int i;
52    size_t len;
53    int n = 0, k, e = 0;
54    char *line, *f[4], *p;
55    const char *gfn;
56    FILE *gf;
57
58    /* check arguments */
59    switch (argc) {
60    case 1:
61	gfn = "/etc/group";
62	break;
63    case 2:
64	gfn = argv[1];
65	break;
66    default:
67	gfn = NULL; /* silence compiler */
68	usage();
69    }
70
71    /* open group file */
72    if ((gf = fopen(gfn, "r")) == NULL)
73	err(EX_IOERR, "%s", gfn); /* XXX - is IO_ERR the correct exit code? */
74
75    /* check line by line */
76    while (++n) {
77	if ((line = fgetln(gf, &len)) == NULL)
78	    break;
79	while (len && isspace(line[len-1]))
80	    len--;
81
82	/* ignore blank lines and comments */
83	for (p = line; p < (line + len); p++)
84	    if (!isspace(*p)) break;
85	if (!len || (*p == '#')) {
86#if 0
87	    /* entry is correct, so print it */
88	    printf("%*.*s\n", len, len, line);
89#endif
90	    continue;
91	}
92
93	/*
94	 * A correct group entry has four colon-separated fields, the third
95	 * of which must be entirely numeric and the fourth of which may
96	 * be empty.
97	 */
98	for (i = k = 0; k < 4; k++) {
99	    for (f[k] = line+i; (i < len) && (line[i] != ':'); i++)
100		/* nothing */ ;
101	    if ((k < 3) && (line[i] != ':'))
102		break;
103	    line[i++] = 0;
104	}
105	if (k < 4) {
106	    warnx("%s: line %d: missing field(s)", gfn, n);
107	    e++;
108	    continue;
109	}
110
111	/* check if fourth field ended with a colon */
112	if (i < len) {
113	    warnx("%s: line %d: too many fields", gfn, n);
114	    e++;
115	    continue;
116	}
117
118	/* check that none of the fields contain whitespace */
119	for (k = 0; k < 4; k++)
120	    if (strcspn(f[k], " \t") != strlen(f[k]))
121		warnx("%s: line %d: field %d contains whitespace",
122		      gfn, n, k+1);
123
124	/* check that the GID is numeric */
125	if (strspn(f[2], "0123456789") != strlen(f[2])) {
126	    warnx("%s: line %d: GID is not numeric", gfn, n);
127	    e++;
128	    continue;
129	}
130
131#if 0
132	/* entry is correct, so print it */
133	printf("%s:%s:%s:%s\n", f[0], f[1], f[2], f[3]);
134#endif
135    }
136
137    /* check what broke the loop */
138    if (ferror(gf))
139	err(EX_IOERR, "%s: line %d", gfn, n);
140
141    /* done */
142    fclose(gf);
143    exit(e ? EX_DATAERR : EX_OK);
144}
145