183bc6a11SDag-Erling Smørgrav /*- 2f76b3199SUlrich Spörlein * 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> 332ad87454SEitan Adler #include <errno.h> 3483bc6a11SDag-Erling Smørgrav #include <ctype.h> 352ad87454SEitan Adler #include <limits.h> 362ad87454SEitan Adler #include <stdint.h> 3783bc6a11SDag-Erling Smørgrav #include <stdio.h> 380afcb087SDag-Erling Smørgrav #include <stdlib.h> 3983bc6a11SDag-Erling Smørgrav #include <string.h> 4083bc6a11SDag-Erling Smørgrav #include <sysexits.h> 4183bc6a11SDag-Erling Smørgrav 42a2c16fbcSColin Percival static char empty[] = { 0 }; 43a2c16fbcSColin Percival 440afcb087SDag-Erling Smørgrav static void 4583bc6a11SDag-Erling Smørgrav usage(void) 4683bc6a11SDag-Erling Smørgrav { 47068f8619SDag-Erling Smørgrav fprintf(stderr, "usage: chkgrp [groupfile]\n"); 4883bc6a11SDag-Erling Smørgrav exit(EX_USAGE); 4983bc6a11SDag-Erling Smørgrav } 5083bc6a11SDag-Erling Smørgrav 5183bc6a11SDag-Erling Smørgrav int 5283bc6a11SDag-Erling Smørgrav main(int argc, char *argv[]) 5383bc6a11SDag-Erling Smørgrav { 549c7ef644SMatt Jacob unsigned int i; 559c7ef644SMatt Jacob size_t len; 560afcb087SDag-Erling Smørgrav int n = 0, k, e = 0; 570afcb087SDag-Erling Smørgrav char *line, *f[4], *p; 5841b27a91SOllivier Robert const char *cp, *gfn; 5983bc6a11SDag-Erling Smørgrav FILE *gf; 6083bc6a11SDag-Erling Smørgrav 6183bc6a11SDag-Erling Smørgrav /* check arguments */ 6283bc6a11SDag-Erling Smørgrav switch (argc) { 6383bc6a11SDag-Erling Smørgrav case 1: 6483bc6a11SDag-Erling Smørgrav gfn = "/etc/group"; 6583bc6a11SDag-Erling Smørgrav break; 6683bc6a11SDag-Erling Smørgrav case 2: 6783bc6a11SDag-Erling Smørgrav gfn = argv[1]; 6883bc6a11SDag-Erling Smørgrav break; 6983bc6a11SDag-Erling Smørgrav default: 7083bc6a11SDag-Erling Smørgrav gfn = NULL; /* silence compiler */ 7183bc6a11SDag-Erling Smørgrav usage(); 7283bc6a11SDag-Erling Smørgrav } 7383bc6a11SDag-Erling Smørgrav 7483bc6a11SDag-Erling Smørgrav /* open group file */ 7583bc6a11SDag-Erling Smørgrav if ((gf = fopen(gfn, "r")) == NULL) 76*93b4da8aSEitan Adler err(EX_NOINPUT, "%s", gfn); 7783bc6a11SDag-Erling Smørgrav 7883bc6a11SDag-Erling Smørgrav /* check line by line */ 7983bc6a11SDag-Erling Smørgrav while (++n) { 8083bc6a11SDag-Erling Smørgrav if ((line = fgetln(gf, &len)) == NULL) 8183bc6a11SDag-Erling Smørgrav break; 82f02cd7c7SPeter Pentchev if (len > 0 && line[len - 1] != '\n') { 83f02cd7c7SPeter Pentchev warnx("%s: line %d: no newline character", gfn, n); 84f02cd7c7SPeter Pentchev e++; 85f02cd7c7SPeter Pentchev } 8683bc6a11SDag-Erling Smørgrav while (len && isspace(line[len-1])) 8783bc6a11SDag-Erling Smørgrav len--; 8883bc6a11SDag-Erling Smørgrav 8983bc6a11SDag-Erling Smørgrav /* ignore blank lines and comments */ 9083bc6a11SDag-Erling Smørgrav for (p = line; p < (line + len); p++) 9183bc6a11SDag-Erling Smørgrav if (!isspace(*p)) break; 9283bc6a11SDag-Erling Smørgrav if (!len || (*p == '#')) { 9383bc6a11SDag-Erling Smørgrav #if 0 9483bc6a11SDag-Erling Smørgrav /* entry is correct, so print it */ 9583bc6a11SDag-Erling Smørgrav printf("%*.*s\n", len, len, line); 9683bc6a11SDag-Erling Smørgrav #endif 9783bc6a11SDag-Erling Smørgrav continue; 9883bc6a11SDag-Erling Smørgrav } 9983bc6a11SDag-Erling Smørgrav 10083bc6a11SDag-Erling Smørgrav /* 10183bc6a11SDag-Erling Smørgrav * A correct group entry has four colon-separated fields, the third 10283bc6a11SDag-Erling Smørgrav * of which must be entirely numeric and the fourth of which may 10383bc6a11SDag-Erling Smørgrav * be empty. 10483bc6a11SDag-Erling Smørgrav */ 10583bc6a11SDag-Erling Smørgrav for (i = k = 0; k < 4; k++) { 10683bc6a11SDag-Erling Smørgrav for (f[k] = line+i; (i < len) && (line[i] != ':'); i++) 10783bc6a11SDag-Erling Smørgrav /* nothing */ ; 10883bc6a11SDag-Erling Smørgrav if ((k < 3) && (line[i] != ':')) 10983bc6a11SDag-Erling Smørgrav break; 11083bc6a11SDag-Erling Smørgrav line[i++] = 0; 11183bc6a11SDag-Erling Smørgrav } 11241b27a91SOllivier Robert 113a2c16fbcSColin Percival if (k < 4) { 114a2c16fbcSColin Percival warnx("%s: line %d: missing field(s)", gfn, n); 115a2c16fbcSColin Percival for ( ; k < 4; k++) 116a2c16fbcSColin Percival f[k] = empty; 117a2c16fbcSColin Percival e++; 118a2c16fbcSColin Percival } 119a2c16fbcSColin Percival 12041b27a91SOllivier Robert for (cp = f[0] ; *cp ; cp++) { 12165530690SOllivier Robert if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' && 12265530690SOllivier Robert (cp > f[0] || *cp != '+')) { 12341b27a91SOllivier Robert warnx("%s: line %d: '%c' invalid character", gfn, n, *cp); 12441b27a91SOllivier Robert e++; 12541b27a91SOllivier Robert } 12641b27a91SOllivier Robert } 12741b27a91SOllivier Robert 12841b27a91SOllivier Robert for (cp = f[3] ; *cp ; cp++) { 12941b27a91SOllivier Robert if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' && 13041b27a91SOllivier Robert *cp != ',') { 13141b27a91SOllivier Robert warnx("%s: line %d: '%c' invalid character", gfn, n, *cp); 13241b27a91SOllivier Robert e++; 13341b27a91SOllivier Robert } 13441b27a91SOllivier Robert } 13541b27a91SOllivier Robert 13683bc6a11SDag-Erling Smørgrav /* check if fourth field ended with a colon */ 13783bc6a11SDag-Erling Smørgrav if (i < len) { 13883bc6a11SDag-Erling Smørgrav warnx("%s: line %d: too many fields", gfn, n); 13983bc6a11SDag-Erling Smørgrav e++; 14083bc6a11SDag-Erling Smørgrav } 14183bc6a11SDag-Erling Smørgrav 14283bc6a11SDag-Erling Smørgrav /* check that none of the fields contain whitespace */ 14341b27a91SOllivier Robert for (k = 0; k < 4; k++) { 14441b27a91SOllivier Robert if (strcspn(f[k], " \t") != strlen(f[k])) { 14583bc6a11SDag-Erling Smørgrav warnx("%s: line %d: field %d contains whitespace", 14683bc6a11SDag-Erling Smørgrav gfn, n, k+1); 14741b27a91SOllivier Robert e++; 14841b27a91SOllivier Robert } 14941b27a91SOllivier Robert } 15083bc6a11SDag-Erling Smørgrav 15183bc6a11SDag-Erling Smørgrav /* check that the GID is numeric */ 15283bc6a11SDag-Erling Smørgrav if (strspn(f[2], "0123456789") != strlen(f[2])) { 15383bc6a11SDag-Erling Smørgrav warnx("%s: line %d: GID is not numeric", gfn, n); 15483bc6a11SDag-Erling Smørgrav e++; 15583bc6a11SDag-Erling Smørgrav } 15683bc6a11SDag-Erling Smørgrav 1572ad87454SEitan Adler /* check the range of the group id */ 1582ad87454SEitan Adler errno = 0; 1592ad87454SEitan Adler unsigned long groupid = strtoul(f[2], NULL, 10); 1602ad87454SEitan Adler if (errno != 0) { 1612ad87454SEitan Adler warnx("%s: line %d: strtoul failed", gfn, n); 1622ad87454SEitan Adler } 1632ad87454SEitan Adler else if (groupid > GID_MAX) { 1642ad87454SEitan Adler warnx("%s: line %d: group id is too large (> %ju)", 1652ad87454SEitan Adler gfn, n, (uintmax_t)GID_MAX); 1662ad87454SEitan Adler e++; 1672ad87454SEitan Adler } 1682ad87454SEitan Adler 16983bc6a11SDag-Erling Smørgrav #if 0 17083bc6a11SDag-Erling Smørgrav /* entry is correct, so print it */ 17183bc6a11SDag-Erling Smørgrav printf("%s:%s:%s:%s\n", f[0], f[1], f[2], f[3]); 17283bc6a11SDag-Erling Smørgrav #endif 17383bc6a11SDag-Erling Smørgrav } 17483bc6a11SDag-Erling Smørgrav 17583bc6a11SDag-Erling Smørgrav /* check what broke the loop */ 17683bc6a11SDag-Erling Smørgrav if (ferror(gf)) 17783bc6a11SDag-Erling Smørgrav err(EX_IOERR, "%s: line %d", gfn, n); 17883bc6a11SDag-Erling Smørgrav 17983bc6a11SDag-Erling Smørgrav /* done */ 18083bc6a11SDag-Erling Smørgrav fclose(gf); 18141b27a91SOllivier Robert if (e == 0) 18241b27a91SOllivier Robert printf("%s is fine\n", gfn); 18383bc6a11SDag-Erling Smørgrav exit(e ? EX_DATAERR : EX_OK); 18483bc6a11SDag-Erling Smørgrav } 185