Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/pwck/pwck.c
+++ new/usr/src/cmd/pwck/pwck.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 + * Copyright (c) 2013 Gary Mills
23 + *
22 24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 25 * Use is subject to license terms.
24 26 */
25 27
26 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 29 /* All Rights Reserved */
28 30
29 31
30 -#pragma ident "%Z%%M% %I% %E% SMI"
31 -
32 32 #include <sys/types.h>
33 33 #include <sys/param.h>
34 34 #include <sys/signal.h>
35 35 #include <sys/sysmacros.h>
36 36 #include <sys/stat.h>
37 37 #include <stdio.h>
38 38 #include <stdlib.h>
39 39 #include <string.h>
40 40 #include <ctype.h>
41 41 #include <locale.h>
42 42 #include <errno.h>
43 43 #include <unistd.h>
44 +#include <limits.h>
44 45
46 +#ifdef LOGNAME_MAX_ILLUMOS
47 +#define _LOGNAME_MAX LOGNAME_MAX_ILLUMOS
48 +#else /* LOGNAME_MAX_ILLUMOS */
49 +#define _LOGNAME_MAX LOGNAME_MAX
50 +#endif /* LOGNAME_MAX_ILLUMOS */
51 +
45 52 #define ERROR1 "Too many/few fields"
46 53 #define ERROR2 "Bad character(s) in logname"
47 54 #define ERROR2a "First char in logname not alphabetic"
48 55 #define ERROR2b "Logname field NULL"
49 56 #define ERROR2c "Logname contains no lower-case letters"
50 57 #define ERROR3 "Logname too long/short"
51 58 #define ERROR4 "Invalid UID"
52 59 #define ERROR5 "Invalid GID"
53 60 #define ERROR6 "Login directory not found"
54 61 #define ERROR6a "Login directory null"
55 62 #define ERROR7 "Optional shell file not found"
56 63
57 64 static int eflag, code = 0;
58 65 static int badc;
59 66 static int lc;
60 67 static char buf[512];
61 68 static void error(char *);
62 69
63 70 int
64 71 main(int argc, char **argv)
65 72 {
66 73 int delim[512];
67 74 char logbuf[512];
68 75 FILE *fptr;
69 76 struct stat obuf;
70 77 uid_t uid;
71 78 gid_t gid;
72 79 int i, j, colons;
73 80 char *pw_file;
74 81 struct stat stat_buf;
75 82 char *str, *lastc;
76 83
77 84 (void) setlocale(LC_ALL, "");
78 85
79 86 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
80 87 #define TEXT_DOMAIN "SYS_TEST"
81 88 #endif
82 89 (void) textdomain(TEXT_DOMAIN);
83 90
84 91 if (argc == 1)
85 92 pw_file = "/etc/passwd";
86 93 else
87 94 pw_file = argv[1];
88 95
89 96 if ((fptr = fopen(pw_file, "r")) == NULL) {
90 97 (void) fprintf(stderr, gettext("cannot open %s\n"), pw_file);
91 98 exit(1);
92 99 }
93 100
94 101 if (fstat(fileno(fptr), &stat_buf) < 0) {
95 102 (void) fprintf(stderr, gettext("fstat failed for %s\n"),
96 103 pw_file);
97 104 (void) fclose(fptr);
98 105 exit(1);
99 106 }
100 107
101 108 if (stat_buf.st_size == 0) {
102 109 (void) fprintf(stderr, gettext("file %s is empty\n"), pw_file);
103 110 (void) fclose(fptr);
104 111 exit(1);
105 112 }
106 113
107 114 while (fgets(buf, sizeof (buf), fptr) != NULL) {
108 115
109 116 colons = 0;
110 117 badc = 0;
111 118 lc = 0;
112 119 eflag = 0;
113 120
114 121 /* Check that entry is not a nameservice redirection */
115 122
116 123 if (buf[0] == '+' || buf[0] == '-') {
117 124 /*
118 125 * Should set flag here to allow special case checking
119 126 * in the rest of the code,
120 127 * but for now, we'll just ignore this entry.
121 128 */
122 129 continue;
123 130 }
124 131
125 132 /* Check number of fields */
126 133
127 134 for (i = 0; buf[i] != NULL; i++)
128 135 if (buf[i] == ':') {
129 136 delim[colons] = i;
130 137 ++colons;
131 138 }
132 139
133 140 if (colons != 6) {
134 141 error(ERROR1);
135 142 continue;
136 143 }
137 144 delim[6] = i - 1;
138 145 delim[7] = NULL;
139 146
140 147 /*
141 148 * Check the first char is alpha; the rest alphanumeric;
142 149 * and that the name does not consist solely of uppercase
143 150 * alpha chars
144 151 */
145 152 if (buf[0] == ':')
146 153 error(ERROR2b);
147 154 else if (!isalpha(buf[0]))
148 155 error(ERROR2a);
149 156
150 157 for (i = 0; buf[i] != ':'; i++) {
151 158 if (!isalnum(buf[i]) &&
152 159 buf[i] != '_' &&
153 160 buf[i] != '-' &&
154 161 buf[i] != '.')
155 162 badc++;
↓ open down ↓ |
101 lines elided |
↑ open up ↑ |
156 163 else if (islower(buf[i]))
157 164 lc++;
158 165 }
159 166 if (lc == 0)
160 167 error(ERROR2c);
161 168 if (badc > 0)
162 169 error(ERROR2);
163 170
164 171 /* Check for valid number of characters in logname */
165 172
166 - if (i <= 0 || i > 8)
173 + if (i <= 0 || i > _LOGNAME_MAX)
167 174 error(ERROR3);
168 175
169 176 /* Check that UID is numeric and <= MAXUID */
170 177
171 178 errno = 0;
172 179 str = &buf[delim[1] + 1];
173 180 uid = strtol(str, &lastc, 10);
174 181 if (lastc != str + (delim[2] - delim[1]) - 1 ||
175 182 uid > MAXUID || errno == ERANGE)
176 183 error(ERROR4);
177 184
178 185 /* Check that GID is numeric and <= MAXUID */
179 186
180 187 errno = 0;
181 188 str = &buf[delim[2] + 1];
182 189 gid = strtol(str, &lastc, 10);
183 190 if (lastc != str + (delim[3] - delim[2]) - 1 ||
184 191 gid > MAXUID || errno == ERANGE)
185 192 error(ERROR5);
186 193
187 194 /* Check initial working directory */
188 195
189 196 for (j = 0, i = (delim[4] + 1); i < delim[5]; j++, i++)
190 197 logbuf[j] = buf[i];
191 198 logbuf[j] = '\0';
192 199
193 200 if (logbuf[0] == NULL)
194 201 error(ERROR6a);
195 202 else if ((stat(logbuf, &obuf)) == -1)
196 203 error(ERROR6);
197 204
198 205 /* Check program to use as shell */
199 206
200 207 if ((buf[(delim[5] + 1)]) != '\n') {
201 208
202 209 for (j = 0, i = (delim[5] + 1); i < delim[6]; j++, i++)
203 210 logbuf[j] = buf[i];
204 211 logbuf[j] = '\0';
205 212
206 213 if (strcmp(logbuf, "*") == 0) /* subsystem login */
207 214 continue;
208 215
209 216 if ((stat(logbuf, &obuf)) == -1)
210 217 error(ERROR7);
211 218
212 219 for (j = 0; j < 512; j++)
213 220 logbuf[j] = NULL;
214 221 }
215 222 }
216 223 (void) fclose(fptr);
217 224 return (code);
218 225 }
219 226
220 227 /* Error printing routine */
221 228
222 229 static void
223 230 error(char *msg)
224 231 {
225 232 if (!eflag) {
226 233 (void) fprintf(stderr, "\n%s", buf);
227 234 code = 1;
228 235 ++eflag;
229 236 }
230 237 if (!badc)
231 238 (void) fprintf(stderr, "\t%s\n", gettext(msg));
232 239 else {
233 240 (void) fprintf(stderr, "\t%d %s\n", badc, gettext(msg));
234 241 badc = 0;
235 242 }
236 243 }
↓ open down ↓ |
60 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX