2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
50 fprintf(stderr, "usage: chkgrp [-q] [groupfile]\n");
55 main(int argc, char *argv[])
63 const char *cp, *f[4], *gfn, *p;
67 while ((opt = getopt(argc, argv, "q")) != -1) {
88 if ((gf = fopen(gfn, "r")) == NULL)
89 err(EX_NOINPUT, "%s", gfn);
91 /* check line by line */
93 if ((line = fgetln(gf, &len)) == NULL)
95 if (len > 0 && line[len - 1] != '\n') {
96 warnx("%s: line %d: no newline character", gfn, n);
99 while (len && isspace(line[len-1]))
102 /* ignore blank lines and comments */
103 for (p = line; p < line + len; p++)
104 if (!isspace(*p)) break;
105 if (!len || *p == '#')
109 * Hack: special case for + line
111 if (strncmp(line, "+:::", len) == 0 ||
112 strncmp(line, "+:*::", len) == 0)
116 * A correct group entry has four colon-separated fields,
117 * the third of which must be entirely numeric and the
118 * fourth of which may be empty.
120 for (i = k = 0; k < 4; k++) {
121 for (f[k] = line + i; i < len && line[i] != ':'; i++)
123 if (k < 3 && line[i] != ':')
129 warnx("%s: line %d: missing field(s)", gfn, n);
135 for (cp = f[0] ; *cp ; cp++) {
136 if (!isalnum(*cp) && *cp != '.' && *cp != '_' &&
137 *cp != '-' && (cp > f[0] || *cp != '+')) {
138 warnx("%s: line %d: '%c' invalid character",
144 for (cp = f[3] ; *cp ; cp++) {
145 if (!isalnum(*cp) && *cp != '.' && *cp != '_' &&
146 *cp != '-' && *cp != ',') {
147 warnx("%s: line %d: '%c' invalid character",
153 /* check if fourth field ended with a colon */
155 warnx("%s: line %d: too many fields", gfn, n);
159 /* check that none of the fields contain whitespace */
160 for (k = 0; k < 4; k++) {
161 if (strcspn(f[k], " \t") != strlen(f[k])) {
162 warnx("%s: line %d: field %d contains whitespace",
168 /* check that the GID is numeric */
169 if (strspn(f[2], "0123456789") != strlen(f[2])) {
170 warnx("%s: line %d: group id is not numeric", gfn, n);
174 /* check the range of the group id */
176 gid = strtoul(f[2], NULL, 10);
178 warnx("%s: line %d: strtoul failed", gfn, n);
179 } else if (gid > GID_MAX) {
180 warnx("%s: line %d: group id is too large (%ju > %ju)",
181 gfn, n, (uintmax_t)gid, (uintmax_t)GID_MAX);
186 /* check what broke the loop */
188 err(EX_IOERR, "%s: line %d", gfn, n);
192 if (e == 0 && quiet == 0)
193 printf("%s is fine\n", gfn);
194 exit(e ? EX_DATAERR : EX_OK);