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 39a2c16fbcSColin Percival static char empty[] = { 0 }; 40a2c16fbcSColin Percival 410afcb087SDag-Erling Smørgrav static void 4283bc6a11SDag-Erling Smørgrav usage(void) 4383bc6a11SDag-Erling Smørgrav { 44068f8619SDag-Erling Smørgrav fprintf(stderr, "usage: chkgrp [groupfile]\n"); 4583bc6a11SDag-Erling Smørgrav exit(EX_USAGE); 4683bc6a11SDag-Erling Smørgrav } 4783bc6a11SDag-Erling Smørgrav 4883bc6a11SDag-Erling Smørgrav int 4983bc6a11SDag-Erling Smørgrav main(int argc, char *argv[]) 5083bc6a11SDag-Erling Smørgrav { 519c7ef644SMatt Jacob unsigned int i; 529c7ef644SMatt Jacob size_t len; 530afcb087SDag-Erling Smørgrav int n = 0, k, e = 0; 540afcb087SDag-Erling Smørgrav char *line, *f[4], *p; 5541b27a91SOllivier Robert const char *cp, *gfn; 5683bc6a11SDag-Erling Smørgrav FILE *gf; 5783bc6a11SDag-Erling Smørgrav 5883bc6a11SDag-Erling Smørgrav /* check arguments */ 5983bc6a11SDag-Erling Smørgrav switch (argc) { 6083bc6a11SDag-Erling Smørgrav case 1: 6183bc6a11SDag-Erling Smørgrav gfn = "/etc/group"; 6283bc6a11SDag-Erling Smørgrav break; 6383bc6a11SDag-Erling Smørgrav case 2: 6483bc6a11SDag-Erling Smørgrav gfn = argv[1]; 6583bc6a11SDag-Erling Smørgrav break; 6683bc6a11SDag-Erling Smørgrav default: 6783bc6a11SDag-Erling Smørgrav gfn = NULL; /* silence compiler */ 6883bc6a11SDag-Erling Smørgrav usage(); 6983bc6a11SDag-Erling Smørgrav } 7083bc6a11SDag-Erling Smørgrav 7183bc6a11SDag-Erling Smørgrav /* open group file */ 7283bc6a11SDag-Erling Smørgrav if ((gf = fopen(gfn, "r")) == NULL) 737f69f4fdSPhilippe Charnier err(EX_IOERR, "%s", gfn); /* XXX - is IO_ERR the correct exit code? */ 7483bc6a11SDag-Erling Smørgrav 7583bc6a11SDag-Erling Smørgrav /* check line by line */ 7683bc6a11SDag-Erling Smørgrav while (++n) { 7783bc6a11SDag-Erling Smørgrav if ((line = fgetln(gf, &len)) == NULL) 7883bc6a11SDag-Erling Smørgrav break; 79f02cd7c7SPeter Pentchev if (len > 0 && line[len - 1] != '\n') { 80f02cd7c7SPeter Pentchev warnx("%s: line %d: no newline character", gfn, n); 81f02cd7c7SPeter Pentchev e++; 82f02cd7c7SPeter Pentchev } 8383bc6a11SDag-Erling Smørgrav while (len && isspace(line[len-1])) 8483bc6a11SDag-Erling Smørgrav len--; 8583bc6a11SDag-Erling Smørgrav 8683bc6a11SDag-Erling Smørgrav /* ignore blank lines and comments */ 8783bc6a11SDag-Erling Smørgrav for (p = line; p < (line + len); p++) 8883bc6a11SDag-Erling Smørgrav if (!isspace(*p)) break; 8983bc6a11SDag-Erling Smørgrav if (!len || (*p == '#')) { 9083bc6a11SDag-Erling Smørgrav #if 0 9183bc6a11SDag-Erling Smørgrav /* entry is correct, so print it */ 9283bc6a11SDag-Erling Smørgrav printf("%*.*s\n", len, len, line); 9383bc6a11SDag-Erling Smørgrav #endif 9483bc6a11SDag-Erling Smørgrav continue; 9583bc6a11SDag-Erling Smørgrav } 9683bc6a11SDag-Erling Smørgrav 9783bc6a11SDag-Erling Smørgrav /* 9883bc6a11SDag-Erling Smørgrav * A correct group entry has four colon-separated fields, the third 9983bc6a11SDag-Erling Smørgrav * of which must be entirely numeric and the fourth of which may 10083bc6a11SDag-Erling Smørgrav * be empty. 10183bc6a11SDag-Erling Smørgrav */ 10283bc6a11SDag-Erling Smørgrav for (i = k = 0; k < 4; k++) { 10383bc6a11SDag-Erling Smørgrav for (f[k] = line+i; (i < len) && (line[i] != ':'); i++) 10483bc6a11SDag-Erling Smørgrav /* nothing */ ; 10583bc6a11SDag-Erling Smørgrav if ((k < 3) && (line[i] != ':')) 10683bc6a11SDag-Erling Smørgrav break; 10783bc6a11SDag-Erling Smørgrav line[i++] = 0; 10883bc6a11SDag-Erling Smørgrav } 10941b27a91SOllivier Robert 110a2c16fbcSColin Percival if (k < 4) { 111a2c16fbcSColin Percival warnx("%s: line %d: missing field(s)", gfn, n); 112a2c16fbcSColin Percival for ( ; k < 4; k++) 113a2c16fbcSColin Percival f[k] = empty; 114a2c16fbcSColin Percival e++; 115a2c16fbcSColin Percival } 116a2c16fbcSColin Percival 11741b27a91SOllivier Robert for (cp = f[0] ; *cp ; cp++) { 11865530690SOllivier Robert if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' && 11965530690SOllivier Robert (cp > f[0] || *cp != '+')) { 12041b27a91SOllivier Robert warnx("%s: line %d: '%c' invalid character", gfn, n, *cp); 12141b27a91SOllivier Robert e++; 12241b27a91SOllivier Robert } 12341b27a91SOllivier Robert } 12441b27a91SOllivier Robert 12541b27a91SOllivier Robert for (cp = f[3] ; *cp ; cp++) { 12641b27a91SOllivier Robert if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' && 12741b27a91SOllivier Robert *cp != ',') { 12841b27a91SOllivier Robert warnx("%s: line %d: '%c' invalid character", gfn, n, *cp); 12941b27a91SOllivier Robert e++; 13041b27a91SOllivier Robert } 13141b27a91SOllivier Robert } 13241b27a91SOllivier Robert 13383bc6a11SDag-Erling Smørgrav /* check if fourth field ended with a colon */ 13483bc6a11SDag-Erling Smørgrav if (i < len) { 13583bc6a11SDag-Erling Smørgrav warnx("%s: line %d: too many fields", gfn, n); 13683bc6a11SDag-Erling Smørgrav e++; 13783bc6a11SDag-Erling Smørgrav } 13883bc6a11SDag-Erling Smørgrav 13983bc6a11SDag-Erling Smørgrav /* check that none of the fields contain whitespace */ 14041b27a91SOllivier Robert for (k = 0; k < 4; k++) { 14141b27a91SOllivier Robert if (strcspn(f[k], " \t") != strlen(f[k])) { 14283bc6a11SDag-Erling Smørgrav warnx("%s: line %d: field %d contains whitespace", 14383bc6a11SDag-Erling Smørgrav gfn, n, k+1); 14441b27a91SOllivier Robert e++; 14541b27a91SOllivier Robert } 14641b27a91SOllivier Robert } 14783bc6a11SDag-Erling Smørgrav 14883bc6a11SDag-Erling Smørgrav /* check that the GID is numeric */ 14983bc6a11SDag-Erling Smørgrav if (strspn(f[2], "0123456789") != strlen(f[2])) { 15083bc6a11SDag-Erling Smørgrav warnx("%s: line %d: GID is not numeric", gfn, n); 15183bc6a11SDag-Erling Smørgrav e++; 15283bc6a11SDag-Erling Smørgrav } 15383bc6a11SDag-Erling Smørgrav 15483bc6a11SDag-Erling Smørgrav #if 0 15583bc6a11SDag-Erling Smørgrav /* entry is correct, so print it */ 15683bc6a11SDag-Erling Smørgrav printf("%s:%s:%s:%s\n", f[0], f[1], f[2], f[3]); 15783bc6a11SDag-Erling Smørgrav #endif 15883bc6a11SDag-Erling Smørgrav } 15983bc6a11SDag-Erling Smørgrav 16083bc6a11SDag-Erling Smørgrav /* check what broke the loop */ 16183bc6a11SDag-Erling Smørgrav if (ferror(gf)) 16283bc6a11SDag-Erling Smørgrav err(EX_IOERR, "%s: line %d", gfn, n); 16383bc6a11SDag-Erling Smørgrav 16483bc6a11SDag-Erling Smørgrav /* done */ 16583bc6a11SDag-Erling Smørgrav fclose(gf); 16641b27a91SOllivier Robert if (e == 0) 16741b27a91SOllivier Robert printf("%s is fine\n", gfn); 16883bc6a11SDag-Erling Smørgrav exit(e ? EX_DATAERR : EX_OK); 16983bc6a11SDag-Erling Smørgrav } 170