xref: /freebsd/usr.sbin/chkgrp/chkgrp.c (revision b728350ee67c01f96c3c5121774536fee81ad176)
183bc6a11SDag-Erling Smørgrav /*-
283bc6a11SDag-Erling Smørgrav  * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav
383bc6a11SDag-Erling Smørgrav  * All rights reserved.
483bc6a11SDag-Erling Smørgrav  *
583bc6a11SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
683bc6a11SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
783bc6a11SDag-Erling Smørgrav  * are met:
883bc6a11SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
983bc6a11SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer
1083bc6a11SDag-Erling Smørgrav  *    in this position and unchanged.
1183bc6a11SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
1283bc6a11SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
1383bc6a11SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
1483bc6a11SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote products
15068f8619SDag-Erling Smørgrav  *    derived from this software without specific prior written permission
1683bc6a11SDag-Erling Smørgrav  *
1783bc6a11SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1883bc6a11SDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1983bc6a11SDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2083bc6a11SDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2183bc6a11SDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2283bc6a11SDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2383bc6a11SDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2483bc6a11SDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2583bc6a11SDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2683bc6a11SDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2783bc6a11SDag-Erling Smørgrav  */
2883bc6a11SDag-Erling Smørgrav 
29b728350eSDavid E. O'Brien #include <sys/cdefs.h>
30b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3183bc6a11SDag-Erling Smørgrav 
3283bc6a11SDag-Erling Smørgrav #include <err.h>
3383bc6a11SDag-Erling Smørgrav #include <ctype.h>
3483bc6a11SDag-Erling Smørgrav #include <stdio.h>
350afcb087SDag-Erling Smørgrav #include <stdlib.h>
3683bc6a11SDag-Erling Smørgrav #include <string.h>
3783bc6a11SDag-Erling Smørgrav #include <sysexits.h>
3883bc6a11SDag-Erling Smørgrav 
390afcb087SDag-Erling Smørgrav static void
4083bc6a11SDag-Erling Smørgrav usage(void)
4183bc6a11SDag-Erling Smørgrav {
42068f8619SDag-Erling Smørgrav     fprintf(stderr, "usage: chkgrp [groupfile]\n");
4383bc6a11SDag-Erling Smørgrav     exit(EX_USAGE);
4483bc6a11SDag-Erling Smørgrav }
4583bc6a11SDag-Erling Smørgrav 
4683bc6a11SDag-Erling Smørgrav int
4783bc6a11SDag-Erling Smørgrav main(int argc, char *argv[])
4883bc6a11SDag-Erling Smørgrav {
499c7ef644SMatt Jacob     unsigned int i;
509c7ef644SMatt Jacob     size_t len;
510afcb087SDag-Erling Smørgrav     int n = 0, k, e = 0;
520afcb087SDag-Erling Smørgrav     char *line, *f[4], *p;
530afcb087SDag-Erling Smørgrav     const char *gfn;
5483bc6a11SDag-Erling Smørgrav     FILE *gf;
5583bc6a11SDag-Erling Smørgrav 
5683bc6a11SDag-Erling Smørgrav     /* check arguments */
5783bc6a11SDag-Erling Smørgrav     switch (argc) {
5883bc6a11SDag-Erling Smørgrav     case 1:
5983bc6a11SDag-Erling Smørgrav 	gfn = "/etc/group";
6083bc6a11SDag-Erling Smørgrav 	break;
6183bc6a11SDag-Erling Smørgrav     case 2:
6283bc6a11SDag-Erling Smørgrav 	gfn = argv[1];
6383bc6a11SDag-Erling Smørgrav 	break;
6483bc6a11SDag-Erling Smørgrav     default:
6583bc6a11SDag-Erling Smørgrav 	gfn = NULL; /* silence compiler */
6683bc6a11SDag-Erling Smørgrav 	usage();
6783bc6a11SDag-Erling Smørgrav     }
6883bc6a11SDag-Erling Smørgrav 
6983bc6a11SDag-Erling Smørgrav     /* open group file */
7083bc6a11SDag-Erling Smørgrav     if ((gf = fopen(gfn, "r")) == NULL)
717f69f4fdSPhilippe Charnier 	err(EX_IOERR, "%s", gfn); /* XXX - is IO_ERR the correct exit code? */
7283bc6a11SDag-Erling Smørgrav 
7383bc6a11SDag-Erling Smørgrav     /* check line by line */
7483bc6a11SDag-Erling Smørgrav     while (++n) {
7583bc6a11SDag-Erling Smørgrav 	if ((line = fgetln(gf, &len)) == NULL)
7683bc6a11SDag-Erling Smørgrav 	    break;
7783bc6a11SDag-Erling Smørgrav 	while (len && isspace(line[len-1]))
7883bc6a11SDag-Erling Smørgrav 	    len--;
7983bc6a11SDag-Erling Smørgrav 
8083bc6a11SDag-Erling Smørgrav 	/* ignore blank lines and comments */
8183bc6a11SDag-Erling Smørgrav 	for (p = line; p < (line + len); p++)
8283bc6a11SDag-Erling Smørgrav 	    if (!isspace(*p)) break;
8383bc6a11SDag-Erling Smørgrav 	if (!len || (*p == '#')) {
8483bc6a11SDag-Erling Smørgrav #if 0
8583bc6a11SDag-Erling Smørgrav 	    /* entry is correct, so print it */
8683bc6a11SDag-Erling Smørgrav 	    printf("%*.*s\n", len, len, line);
8783bc6a11SDag-Erling Smørgrav #endif
8883bc6a11SDag-Erling Smørgrav 	    continue;
8983bc6a11SDag-Erling Smørgrav 	}
9083bc6a11SDag-Erling Smørgrav 
9183bc6a11SDag-Erling Smørgrav 	/*
9283bc6a11SDag-Erling Smørgrav 	 * A correct group entry has four colon-separated fields, the third
9383bc6a11SDag-Erling Smørgrav 	 * of which must be entirely numeric and the fourth of which may
9483bc6a11SDag-Erling Smørgrav 	 * be empty.
9583bc6a11SDag-Erling Smørgrav 	 */
9683bc6a11SDag-Erling Smørgrav 	for (i = k = 0; k < 4; k++) {
9783bc6a11SDag-Erling Smørgrav 	    for (f[k] = line+i; (i < len) && (line[i] != ':'); i++)
9883bc6a11SDag-Erling Smørgrav 		/* nothing */ ;
9983bc6a11SDag-Erling Smørgrav 	    if ((k < 3) && (line[i] != ':'))
10083bc6a11SDag-Erling Smørgrav 		break;
10183bc6a11SDag-Erling Smørgrav 	    line[i++] = 0;
10283bc6a11SDag-Erling Smørgrav 	}
10383bc6a11SDag-Erling Smørgrav 	if (k < 4) {
10483bc6a11SDag-Erling Smørgrav 	    warnx("%s: line %d: missing field(s)", gfn, n);
10583bc6a11SDag-Erling Smørgrav 	    e++;
10683bc6a11SDag-Erling Smørgrav 	    continue;
10783bc6a11SDag-Erling Smørgrav 	}
10883bc6a11SDag-Erling Smørgrav 
10983bc6a11SDag-Erling Smørgrav 	/* check if fourth field ended with a colon */
11083bc6a11SDag-Erling Smørgrav 	if (i < len) {
11183bc6a11SDag-Erling Smørgrav 	    warnx("%s: line %d: too many fields", gfn, n);
11283bc6a11SDag-Erling Smørgrav 	    e++;
11383bc6a11SDag-Erling Smørgrav 	    continue;
11483bc6a11SDag-Erling Smørgrav 	}
11583bc6a11SDag-Erling Smørgrav 
11683bc6a11SDag-Erling Smørgrav 	/* check that none of the fields contain whitespace */
11783bc6a11SDag-Erling Smørgrav 	for (k = 0; k < 4; k++)
11883bc6a11SDag-Erling Smørgrav 	    if (strcspn(f[k], " \t") != strlen(f[k]))
11983bc6a11SDag-Erling Smørgrav 		warnx("%s: line %d: field %d contains whitespace",
12083bc6a11SDag-Erling Smørgrav 		      gfn, n, k+1);
12183bc6a11SDag-Erling Smørgrav 
12283bc6a11SDag-Erling Smørgrav 	/* check that the GID is numeric */
12383bc6a11SDag-Erling Smørgrav 	if (strspn(f[2], "0123456789") != strlen(f[2])) {
12483bc6a11SDag-Erling Smørgrav 	    warnx("%s: line %d: GID is not numeric", gfn, n);
12583bc6a11SDag-Erling Smørgrav 	    e++;
12683bc6a11SDag-Erling Smørgrav 	    continue;
12783bc6a11SDag-Erling Smørgrav 	}
12883bc6a11SDag-Erling Smørgrav 
12983bc6a11SDag-Erling Smørgrav #if 0
13083bc6a11SDag-Erling Smørgrav 	/* entry is correct, so print it */
13183bc6a11SDag-Erling Smørgrav 	printf("%s:%s:%s:%s\n", f[0], f[1], f[2], f[3]);
13283bc6a11SDag-Erling Smørgrav #endif
13383bc6a11SDag-Erling Smørgrav     }
13483bc6a11SDag-Erling Smørgrav 
13583bc6a11SDag-Erling Smørgrav     /* check what broke the loop */
13683bc6a11SDag-Erling Smørgrav     if (ferror(gf))
13783bc6a11SDag-Erling Smørgrav 	err(EX_IOERR, "%s: line %d", gfn, n);
13883bc6a11SDag-Erling Smørgrav 
13983bc6a11SDag-Erling Smørgrav     /* done */
14083bc6a11SDag-Erling Smørgrav     fclose(gf);
14183bc6a11SDag-Erling Smørgrav     exit(e ? EX_DATAERR : EX_OK);
14283bc6a11SDag-Erling Smørgrav }
143