Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / grpck / grpck.c
blob040eb24bc6d8deb2ae0ffd60ab28c8a8dce02a7f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <locale.h>
40 #include <limits.h>
42 #define BADLINE "Too many/few fields"
43 #define TOOLONG "Line too long"
44 #define NONAME "No group name"
45 #define BADNAME "Bad character(s) in group name"
46 #define BADGID "Invalid GID"
47 #define NULLNAME "Null login name"
48 #define NOTFOUND "Logname not found in password file"
49 #define DUPNAME "Duplicate logname entry"
50 #define DUPNAME2 "Duplicate logname entry (gid first occurs in passwd entry)"
51 #define NOMEM "Out of memory"
52 #define NGROUPS "Maximum groups exceeded for logname "
53 #define BLANKLINE "Blank line detected. Please remove line"
54 #define LONGNAME "Group name too long"
56 int eflag, badchar, baddigit, badlognam, colons, len;
57 static int longnam = 0;
58 int code;
60 #define MYBUFSIZE (LINE_MAX) /* max line length including newline and null */
61 #define NUM_COLONS 3
63 char *buf;
64 char *nptr;
65 char *cptr;
66 FILE *fptr;
67 gid_t gid;
68 void error(char *msg);
70 struct group {
71 struct group *nxt;
72 int cnt;
73 gid_t grp;
76 struct node {
77 struct node *next;
78 int ngroups;
79 struct group *groups;
80 char user[1];
83 void *
84 emalloc(size_t size)
86 void *vp;
87 vp = malloc(size);
88 if (vp == NULL) {
89 fprintf(stderr, "%s\n", gettext(NOMEM));
90 exit(1);
92 return (vp);
95 int
96 main(int argc, char *argv[])
98 struct passwd *pwp;
99 struct node *root = NULL;
100 struct node *t;
101 struct group *gp;
102 int ngroups_max;
103 int ngroups = 0;
104 int listlen;
105 int i;
106 int lineno = 0;
107 char *buf_off, *tmpbuf;
108 int delim[NUM_COLONS + 1], buf_len, bufsize;
110 (void) setlocale(LC_ALL, "");
112 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
113 #define TEXT_DOMAIN "SYS_TEST"
114 #endif
115 (void) textdomain(TEXT_DOMAIN);
117 code = 0;
118 ngroups_max = sysconf(_SC_NGROUPS_MAX);
120 if (argc == 1)
121 argv[1] = "/etc/group";
122 else if (argc != 2) {
123 fprintf(stderr, gettext("usage: %s filename\n"), *argv);
124 exit(1);
127 if ((fptr = fopen(argv[1], "r")) == NULL) {
128 fprintf(stderr, gettext("cannot open file %s: %s\n"), argv[1],
129 strerror(errno));
130 exit(1);
133 #ifdef ORIG_SVR4
134 while ((pwp = getpwent()) != NULL) {
135 t = (struct node *)emalloc(sizeof (*t) + strlen(pwp->pw_name));
136 t->next = root;
137 root = t;
138 strcpy(t->user, pwp->pw_name);
139 t->ngroups = 1;
140 if (!ngroups_max)
141 t->groups = NULL;
142 else {
143 t->groups = (struct group *)
144 emalloc(sizeof (struct group));
145 t->groups->grp = pwp->pw_gid;
146 t->groups->cnt = 1;
147 t->groups->nxt = NULL;
150 #endif
152 bufsize = MYBUFSIZE;
153 if ((buf = malloc(bufsize)) == NULL) {
154 (void) fprintf(stderr, gettext(NOMEM));
155 exit(1);
157 while (!feof(fptr) && !ferror(fptr)) {
158 buf_len = 0;
159 buf_off = buf;
160 while (fgets(buf_off, (bufsize - buf_len), fptr) != NULL) {
161 buf_len += strlen(buf_off);
162 if (buf[buf_len - 1] == '\n' || feof(fptr))
163 break;
164 tmpbuf = realloc(buf, (bufsize + MYBUFSIZE));
165 if (tmpbuf == NULL) {
166 (void) fprintf(stderr, gettext(NOMEM));
167 exit(1);
169 bufsize += MYBUFSIZE;
170 buf = tmpbuf;
171 buf_off = buf + buf_len;
173 if (buf_len == 0)
174 continue;
176 /* Report error to be consistent with libc */
177 if ((buf_len + 1) > LINE_MAX)
178 error(TOOLONG);
180 lineno++;
181 if (buf[0] == '\n') /* blank lines are ignored */
183 code = 1; /* exit with error code = 1 */
184 eflag = 0; /* force print of "blank" line */
185 fprintf(stderr, "\n%s %d\n", gettext(BLANKLINE),
186 lineno);
187 continue;
190 if (buf[buf_len - 1] == '\n') {
191 if ((tmpbuf = strdup(buf)) == NULL) {
192 (void) fprintf(stderr, gettext(NOMEM));
193 exit(1);
195 tmpbuf[buf_len - 1] = ',';
196 } else {
197 if ((tmpbuf = malloc(buf_len + 2)) == NULL) {
198 (void) fprintf(stderr, gettext(NOMEM));
199 exit(1);
201 (void) strcpy(tmpbuf, buf);
202 tmpbuf[buf_len++] = ',';
203 tmpbuf[buf_len] = '\0';
206 colons = 0;
207 eflag = 0;
208 badchar = 0;
209 baddigit = 0;
210 badlognam = 0;
211 gid = 0;
213 ngroups++; /* Increment number of groups found */
214 /* Check that entry is not a nameservice redirection */
216 if (buf[0] == '+' || buf[0] == '-') {
218 * Should set flag here to allow special case checking
219 * in the rest of the code,
220 * but for now, we'll just ignore this entry.
222 free(tmpbuf);
223 continue;
226 /* Check number of fields */
228 for (i = 0; buf[i] != '\0'; i++) {
229 if (buf[i] == ':') {
230 delim[colons] = i;
231 if (++colons > NUM_COLONS)
232 break;
235 if (colons != NUM_COLONS) {
236 error(BADLINE);
237 free(tmpbuf);
238 continue;
241 /* check to see that group name is at least 1 character */
242 /* and that all characters are lowrcase or digits. */
244 if (buf[0] == ':')
245 error(NONAME);
246 else {
247 for (i = 0; buf[i] != ':'; i++) {
248 if (i >= LOGNAME_MAX)
249 longnam++;
250 if (!(islower(buf[i]) || isdigit(buf[i])))
251 badchar++;
253 if (longnam > 0)
254 error(LONGNAME);
255 if (badchar > 0)
256 error(BADNAME);
259 /* check that GID is numeric and <= 31 bits */
261 len = (delim[2] - delim[1]) - 1;
263 if (len > 10 || len < 1)
264 error(BADGID);
265 else {
266 for (i = (delim[1]+1); i < delim[2]; i++) {
267 if (! (isdigit(buf[i])))
268 baddigit++;
269 else if (baddigit == 0)
270 gid = gid * 10 + (gid_t)(buf[i] - '0');
271 /* converts ascii GID to decimal */
273 if (baddigit > 0)
274 error(BADGID);
275 else if (gid > (gid_t)MAXUID)
276 error(BADGID);
279 /* check that logname appears in the passwd file */
281 nptr = &tmpbuf[delim[2]];
282 nptr++;
284 listlen = strlen(nptr) - 1;
286 while ((cptr = strchr(nptr, ',')) != NULL) {
287 *cptr = '\0';
288 if (*nptr == '\0') {
289 if (listlen)
290 error(NULLNAME);
291 nptr++;
292 continue;
295 for (t = root; t != NULL; t = t->next) {
296 if (strcmp(t->user, nptr) == 0)
297 break;
299 if (t == NULL) {
300 #ifndef ORIG_SVR4
302 * User entry not found, so check if in
303 * password file
305 struct passwd *pwp;
307 if ((pwp = getpwnam(nptr)) == NULL) {
308 #endif
309 badlognam++;
310 error(NOTFOUND);
311 goto getnext;
312 #ifndef ORIG_SVR4
315 /* Usrname found, so add entry to user-list */
316 t = (struct node *)
317 emalloc(sizeof (*t) + strlen(nptr));
318 t->next = root;
319 root = t;
320 strcpy(t->user, nptr);
321 t->ngroups = 1;
322 if (!ngroups_max)
323 t->groups = NULL;
324 else {
325 t->groups = (struct group *)
326 emalloc(sizeof (struct group));
327 t->groups->grp = pwp->pw_gid;
328 t->groups->cnt = 1;
329 t->groups->nxt = NULL;
332 #endif
333 if (!ngroups_max)
334 goto getnext;
336 t->ngroups++;
339 * check for duplicate logname in group
342 for (gp = t->groups; gp != NULL; gp = gp->nxt) {
343 if (gid == gp->grp) {
344 if (gp->cnt++ == 1) {
345 badlognam++;
346 if (gp->nxt == NULL)
347 error(DUPNAME2);
348 else
349 error(DUPNAME);
351 goto getnext;
355 gp = (struct group *)emalloc(sizeof (struct group));
356 gp->grp = gid;
357 gp->cnt = 1;
358 gp->nxt = t->groups;
359 t->groups = gp;
360 getnext:
361 nptr = ++cptr;
363 free(tmpbuf);
366 if (ngroups == 0) {
367 fprintf(stderr, gettext("Group file '%s' is empty\n"), argv[1]);
368 code = 1;
371 if (ngroups_max) {
372 for (t = root; t != NULL; t = t->next) {
373 if (t->ngroups > ngroups_max) {
374 fprintf(stderr, "\n\n%s%s (%d)\n",
375 NGROUPS, t->user, t->ngroups);
376 code = 1;
380 return (code);
383 /* Error printing routine */
385 void
386 error(char *msg)
388 code = 1;
389 if (eflag == 0) {
390 fprintf(stderr, "\n\n%s", buf);
391 eflag = 1;
393 if (longnam != 0) {
394 fprintf(stderr, "\t%s\n", gettext(msg));
395 longnam = 0;
396 return;
398 if (badchar != 0) {
399 fprintf(stderr, "\t%d %s\n", badchar, gettext(msg));
400 badchar = 0;
401 return;
402 } else if (baddigit != 0) {
403 fprintf(stderr, "\t%s\n", gettext(msg));
404 baddigit = 0;
405 return;
406 } else if (badlognam != 0) {
407 fprintf(stderr, "\t%s - %s\n", nptr, gettext(msg));
408 badlognam = 0;
409 return;
410 } else {
411 fprintf(stderr, "\t%s\n", gettext(msg));
412 return;