183bc6a11SDag-Erling Smørgrav /*-
21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni *
4*e738085bSDag-Erling Smørgrav * Copyright (c) 1998 Dag-Erling Smørgrav
583bc6a11SDag-Erling Smørgrav * All rights reserved.
683bc6a11SDag-Erling Smørgrav *
783bc6a11SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
883bc6a11SDag-Erling Smørgrav * modification, are permitted provided that the following conditions
983bc6a11SDag-Erling Smørgrav * are met:
1083bc6a11SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
1183bc6a11SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer
1283bc6a11SDag-Erling Smørgrav * in this position and unchanged.
1383bc6a11SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
1483bc6a11SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
1583bc6a11SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
1683bc6a11SDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote products
17068f8619SDag-Erling Smørgrav * derived from this software without specific prior written permission
1883bc6a11SDag-Erling Smørgrav *
1983bc6a11SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2083bc6a11SDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2183bc6a11SDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2283bc6a11SDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2383bc6a11SDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2483bc6a11SDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2583bc6a11SDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2683bc6a11SDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2783bc6a11SDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2883bc6a11SDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2983bc6a11SDag-Erling Smørgrav */
3083bc6a11SDag-Erling Smørgrav
31b728350eSDavid E. O'Brien #include <sys/cdefs.h>
3283bc6a11SDag-Erling Smørgrav #include <err.h>
332ad87454SEitan Adler #include <errno.h>
3483bc6a11SDag-Erling Smørgrav #include <ctype.h>
35e622418cSBaptiste Daroussin #include <inttypes.h>
362ad87454SEitan Adler #include <limits.h>
372ad87454SEitan Adler #include <stdint.h>
3883bc6a11SDag-Erling Smørgrav #include <stdio.h>
390afcb087SDag-Erling Smørgrav #include <stdlib.h>
4083bc6a11SDag-Erling Smørgrav #include <string.h>
41ce643be1SEitan Adler #include <unistd.h>
4283bc6a11SDag-Erling Smørgrav #include <sysexits.h>
4383bc6a11SDag-Erling Smørgrav
44ce643be1SEitan Adler static void __dead2
usage(void)4583bc6a11SDag-Erling Smørgrav usage(void)
4683bc6a11SDag-Erling Smørgrav {
47cc13f05dSDag-Erling Smørgrav
48cc13f05dSDag-Erling Smørgrav fprintf(stderr, "usage: chkgrp [-q] [groupfile]\n");
4983bc6a11SDag-Erling Smørgrav exit(EX_USAGE);
5083bc6a11SDag-Erling Smørgrav }
5183bc6a11SDag-Erling Smørgrav
5283bc6a11SDag-Erling Smørgrav int
main(int argc,char * argv[])5383bc6a11SDag-Erling Smørgrav main(int argc, char *argv[])
5483bc6a11SDag-Erling Smørgrav {
55cc13f05dSDag-Erling Smørgrav FILE *gf;
56cc13f05dSDag-Erling Smørgrav unsigned long gid;
579c7ef644SMatt Jacob unsigned int i;
589c7ef644SMatt Jacob size_t len;
59cc13f05dSDag-Erling Smørgrav int opt, quiet;
600afcb087SDag-Erling Smørgrav int n = 0, k, e = 0;
61cc13f05dSDag-Erling Smørgrav const char *cp, *f[4], *gfn, *p;
62cc13f05dSDag-Erling Smørgrav char *line;
6383bc6a11SDag-Erling Smørgrav
64ce643be1SEitan Adler quiet = 0;
65cc13f05dSDag-Erling Smørgrav while ((opt = getopt(argc, argv, "q")) != -1) {
66cc13f05dSDag-Erling Smørgrav switch (opt) {
67ce643be1SEitan Adler case 'q':
68ce643be1SEitan Adler quiet = 1;
6983bc6a11SDag-Erling Smørgrav break;
7083bc6a11SDag-Erling Smørgrav default:
7183bc6a11SDag-Erling Smørgrav usage();
7283bc6a11SDag-Erling Smørgrav }
73ce643be1SEitan Adler }
74ce643be1SEitan Adler
75cc13f05dSDag-Erling Smørgrav argc -= optind;
76cc13f05dSDag-Erling Smørgrav argv += optind;
77cc13f05dSDag-Erling Smørgrav
78cc13f05dSDag-Erling Smørgrav if (argc == 0)
79ce643be1SEitan Adler gfn = "/etc/group";
80cc13f05dSDag-Erling Smørgrav else if (argc == 1)
81cc13f05dSDag-Erling Smørgrav gfn = argv[0];
82ce643be1SEitan Adler else
83ce643be1SEitan Adler usage();
8483bc6a11SDag-Erling Smørgrav
8583bc6a11SDag-Erling Smørgrav /* open group file */
8683bc6a11SDag-Erling Smørgrav if ((gf = fopen(gfn, "r")) == NULL)
8793b4da8aSEitan Adler err(EX_NOINPUT, "%s", gfn);
8883bc6a11SDag-Erling Smørgrav
8983bc6a11SDag-Erling Smørgrav /* check line by line */
9083bc6a11SDag-Erling Smørgrav while (++n) {
9183bc6a11SDag-Erling Smørgrav if ((line = fgetln(gf, &len)) == NULL)
9283bc6a11SDag-Erling Smørgrav break;
93f02cd7c7SPeter Pentchev if (len > 0 && line[len - 1] != '\n') {
94f02cd7c7SPeter Pentchev warnx("%s: line %d: no newline character", gfn, n);
95c71c7ce7SEitan Adler e = 1;
96f02cd7c7SPeter Pentchev }
9783bc6a11SDag-Erling Smørgrav while (len && isspace(line[len-1]))
9883bc6a11SDag-Erling Smørgrav len--;
9983bc6a11SDag-Erling Smørgrav
10083bc6a11SDag-Erling Smørgrav /* ignore blank lines and comments */
101cc13f05dSDag-Erling Smørgrav for (p = line; p < line + len; p++)
10283bc6a11SDag-Erling Smørgrav if (!isspace(*p)) break;
103cc13f05dSDag-Erling Smørgrav if (!len || *p == '#')
10483bc6a11SDag-Erling Smørgrav continue;
10583bc6a11SDag-Erling Smørgrav
10683bc6a11SDag-Erling Smørgrav /*
107cc13f05dSDag-Erling Smørgrav * Hack: special case for + line
108cc13f05dSDag-Erling Smørgrav */
109f4e060d0SBaptiste Daroussin if (strncmp(line, "+:::", len) == 0 ||
110f4e060d0SBaptiste Daroussin strncmp(line, "+:*::", len) == 0)
111cc13f05dSDag-Erling Smørgrav continue;
112cc13f05dSDag-Erling Smørgrav
113cc13f05dSDag-Erling Smørgrav /*
114cc13f05dSDag-Erling Smørgrav * A correct group entry has four colon-separated fields,
115cc13f05dSDag-Erling Smørgrav * the third of which must be entirely numeric and the
116cc13f05dSDag-Erling Smørgrav * fourth of which may be empty.
11783bc6a11SDag-Erling Smørgrav */
11883bc6a11SDag-Erling Smørgrav for (i = k = 0; k < 4; k++) {
119cc13f05dSDag-Erling Smørgrav for (f[k] = line + i; i < len && line[i] != ':'; i++)
12083bc6a11SDag-Erling Smørgrav /* nothing */ ;
121cc13f05dSDag-Erling Smørgrav if (k < 3 && line[i] != ':')
12283bc6a11SDag-Erling Smørgrav break;
12383bc6a11SDag-Erling Smørgrav line[i++] = 0;
12483bc6a11SDag-Erling Smørgrav }
12541b27a91SOllivier Robert
126a2c16fbcSColin Percival if (k < 4) {
127a2c16fbcSColin Percival warnx("%s: line %d: missing field(s)", gfn, n);
128cc13f05dSDag-Erling Smørgrav while (k < 4)
129cc13f05dSDag-Erling Smørgrav f[k++] = "";
130c71c7ce7SEitan Adler e = 1;
131a2c16fbcSColin Percival }
132a2c16fbcSColin Percival
13341b27a91SOllivier Robert for (cp = f[0] ; *cp ; cp++) {
134cc13f05dSDag-Erling Smørgrav if (!isalnum(*cp) && *cp != '.' && *cp != '_' &&
135cc13f05dSDag-Erling Smørgrav *cp != '-' && (cp > f[0] || *cp != '+')) {
136cc13f05dSDag-Erling Smørgrav warnx("%s: line %d: '%c' invalid character",
137cc13f05dSDag-Erling Smørgrav gfn, n, *cp);
138c71c7ce7SEitan Adler e = 1;
13941b27a91SOllivier Robert }
14041b27a91SOllivier Robert }
14141b27a91SOllivier Robert
14241b27a91SOllivier Robert for (cp = f[3] ; *cp ; cp++) {
143cc13f05dSDag-Erling Smørgrav if (!isalnum(*cp) && *cp != '.' && *cp != '_' &&
144cc13f05dSDag-Erling Smørgrav *cp != '-' && *cp != ',') {
145cc13f05dSDag-Erling Smørgrav warnx("%s: line %d: '%c' invalid character",
146cc13f05dSDag-Erling Smørgrav gfn, n, *cp);
147c71c7ce7SEitan Adler e = 1;
14841b27a91SOllivier Robert }
14941b27a91SOllivier Robert }
15041b27a91SOllivier Robert
15183bc6a11SDag-Erling Smørgrav /* check if fourth field ended with a colon */
15283bc6a11SDag-Erling Smørgrav if (i < len) {
15383bc6a11SDag-Erling Smørgrav warnx("%s: line %d: too many fields", gfn, n);
154c71c7ce7SEitan Adler e = 1;
15583bc6a11SDag-Erling Smørgrav }
15683bc6a11SDag-Erling Smørgrav
15783bc6a11SDag-Erling Smørgrav /* check that none of the fields contain whitespace */
15841b27a91SOllivier Robert for (k = 0; k < 4; k++) {
15941b27a91SOllivier Robert if (strcspn(f[k], " \t") != strlen(f[k])) {
16083bc6a11SDag-Erling Smørgrav warnx("%s: line %d: field %d contains whitespace",
16183bc6a11SDag-Erling Smørgrav gfn, n, k+1);
162c71c7ce7SEitan Adler e = 1;
16341b27a91SOllivier Robert }
16441b27a91SOllivier Robert }
16583bc6a11SDag-Erling Smørgrav
16683bc6a11SDag-Erling Smørgrav /* check that the GID is numeric */
16783bc6a11SDag-Erling Smørgrav if (strspn(f[2], "0123456789") != strlen(f[2])) {
168cc13f05dSDag-Erling Smørgrav warnx("%s: line %d: group id is not numeric", gfn, n);
169c71c7ce7SEitan Adler e = 1;
17083bc6a11SDag-Erling Smørgrav }
17183bc6a11SDag-Erling Smørgrav
1722ad87454SEitan Adler /* check the range of the group id */
1732ad87454SEitan Adler errno = 0;
174f2164ae0SBaptiste Daroussin gid = strtoul(f[2], NULL, 10);
1752ad87454SEitan Adler if (errno != 0) {
176f2164ae0SBaptiste Daroussin warnx("%s: line %d: strtoul failed", gfn, n);
177cc13f05dSDag-Erling Smørgrav } else if (gid > GID_MAX) {
178cc13f05dSDag-Erling Smørgrav warnx("%s: line %d: group id is too large (%ju > %ju)",
179cc13f05dSDag-Erling Smørgrav gfn, n, (uintmax_t)gid, (uintmax_t)GID_MAX);
180c71c7ce7SEitan Adler e = 1;
1812ad87454SEitan Adler }
18283bc6a11SDag-Erling Smørgrav }
18383bc6a11SDag-Erling Smørgrav
18483bc6a11SDag-Erling Smørgrav /* check what broke the loop */
18583bc6a11SDag-Erling Smørgrav if (ferror(gf))
18683bc6a11SDag-Erling Smørgrav err(EX_IOERR, "%s: line %d", gfn, n);
18783bc6a11SDag-Erling Smørgrav
18883bc6a11SDag-Erling Smørgrav /* done */
18983bc6a11SDag-Erling Smørgrav fclose(gf);
190ce643be1SEitan Adler if (e == 0 && quiet == 0)
19141b27a91SOllivier Robert printf("%s is fine\n", gfn);
19283bc6a11SDag-Erling Smørgrav exit(e ? EX_DATAERR : EX_OK);
19383bc6a11SDag-Erling Smørgrav }
194