xref: /illumos-gate/usr/src/lib/pam_modules/authtok_check/packer.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "packer.h"
27 
28 /*
29  * This file steers the creation of the Crack Dictionary Database.
30  * Based on a list of source dictionaries specified by the administrator,
31  * we create the Database by sorting each dictionary (in memory, one at
32  * a time), writing the sorted result to a temporary file, and merging
33  * all the temporary files into the Database.
34  *
35  * The current implementation has a number of limitations
36  *   - each single source dictionary has to fit in memory
37  *   - each single source dictionary has to be smaller than 2GByte
38  *   - each single source dictionary can only hold up to 4GB words
39  * None of these seem real, practical, problems to me.
40  *
41  * All of this is meant to be run by one thread per host. The caller is
42  * responsible for locking things appropriately (as make_dict_database
43  * in dict.c does).
44  */
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 
55 /* Stuff used for sorting the dictionary */
56 static char	*buf;		/* used to hold the source dictionary */
57 static uint_t	*offsets;	/* array of word-offsets into "buf" */
58 static uint_t	off_idx = 0;	/* first free index in offsets array */
59 static size_t	off_size = 0;	/* offsets array size */
60 
61 /* stuff to keep track of the temporary files */
62 #define	FNAME_TEMPLATE	"/var/tmp/authtok_check.XXXXXX"
63 #define	MAXTMP		64
64 static FILE	*tmpfp[MAXTMP];	/* FILE *'s to (unlinked) temporary files */
65 static int	tmpfp_idx = 0;	/* points to first free entry in tmpfp */
66 
67 #define	MODNAME "pam_authtok_check::packer"
68 
69 /*
70  * int writeout(void)
71  *
72  * Write the sorted wordlist to disk. We create a temporary file
73  * (in /var/tmp), and immediately unlink() it. We keep an open
74  * FILE pointer to it in tmpfp[] for later use.
75  *
76  * returns 0 on success, -1 on failure (can't create file/output failure).
77  */
78 int
79 writeout(void)
80 {
81 	int i = 0;
82 	char tmpname[sizeof (FNAME_TEMPLATE)];
83 	int fd;
84 
85 	if (tmpfp_idx == MAXTMP) {
86 		syslog(LOG_ERR, MODNAME ": too many temporary "
87 		    "files (maximum %d exceeded)", MAXTMP);
88 		return (-1);
89 	}
90 
91 	(void) strcpy(tmpname, FNAME_TEMPLATE);
92 	if ((fd = mkstemp(tmpname)) == -1) {
93 		syslog(LOG_ERR, MODNAME ": mkstemp() failed: %s\n",
94 		    strerror(errno));
95 		return (-1);
96 	}
97 	(void) unlink(tmpname);
98 
99 	if ((tmpfp[tmpfp_idx] = fdopen(fd, "w+F")) == NULL) {
100 		syslog(LOG_ERR, MODNAME ": fdopen failed: %s",
101 		    strerror(errno));
102 		(void) close(fd);
103 		return (-1);
104 	}
105 
106 	/* write words to file */
107 	while (i < off_idx) {
108 		if (fprintf(tmpfp[tmpfp_idx], "%s\n", &buf[offsets[i++]]) < 0) {
109 			syslog(LOG_ERR, MODNAME ": write to file failed: %s",
110 			    strerror(errno));
111 			(void) close(fd);
112 			return (-1);
113 		}
114 	}
115 
116 	/* we have one extra tmpfp */
117 	tmpfp_idx++;
118 
119 	return (0);
120 }
121 
122 /*
123  * int insert_word(int off)
124  *
125  * insert an offset into the offsets-array. If the offsets-array is out of
126  * space, we allocate additional space (in CHUNKs)
127  *
128  * returns 0 on success, -1 on failure (out of memory)
129  */
130 int
131 insert_word(int off)
132 {
133 #define	CHUNK 10000
134 
135 	if (off_idx == off_size) {
136 		uint_t *tmp;
137 		off_size += CHUNK;
138 		tmp = realloc(offsets, sizeof (uint_t) * off_size);
139 		if (tmp == NULL) {
140 			syslog(LOG_ERR, MODNAME ": out of memory");
141 			free(offsets);
142 			off_idx = off_size = 0;
143 			offsets = NULL;
144 			return (-1);
145 		}
146 		offsets = tmp;
147 	}
148 
149 	offsets[off_idx++] = off;
150 	return (0);
151 }
152 
153 /*
154  * translate(buf, size)
155  *
156  * perform "tr '[A-Z]' '[a-z]' | tr -cd '\012[a-z][0-9]'" on the
157  * words in "buf" and insert each of them into the offsets-array.
158  * We refrain from using 'isupper' and 'islower' to keep this strictly
159  * ASCII-only, as is the original Cracklib code.
160  *
161  * returns 0 on success, -1 on failure (failure of insert_word)
162  */
163 int
164 translate(char *buf, size_t size)
165 {
166 	char *p, *q, *e;
167 	char c;
168 	int wordstart;
169 
170 	e = &buf[size];
171 
172 	wordstart = 0;
173 	for (p = buf, q = buf; q < e; q++) {
174 		c = *q;
175 		if (c >= 'A' && c <= 'Z') {
176 			*(p++) = tolower(c);
177 		} else if (c == '\n') {
178 			*(p++) = '\0';
179 			/*
180 			 * make sure we only insert words consisting of
181 			 * MAXWORDLEN-1 bytes or less
182 			 */
183 			if (p-&buf[wordstart] > MAXWORDLEN)
184 				buf[wordstart+MAXWORDLEN-1] = '\0';
185 			if (insert_word(wordstart) != 0)
186 				return (-1);
187 			wordstart = p-buf;
188 		} else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
189 			*(p++) = c;
190 		}
191 	}
192 	return (0);
193 }
194 
195 /*
196  * int compare(a, b)
197  *
198  * helper-routine used for quicksort. we compate two words in the
199  * buffer, one start starts at index "a", and the other one that starts
200  * at index "b"
201  */
202 int
203 compare(const void *a, const void *b)
204 {
205 	int idx_a = *(uint_t *)a, idx_b = *(uint_t *)b;
206 
207 	return (strcmp(&buf[idx_a], &buf[idx_b]));
208 }
209 
210 /*
211  *
212  * int sort_file(fname)
213  *
214  * We sort the file in memory: we read the dictionary file, translate all
215  * newlines to '\0's, all uppercase ASCII characters to lowercase characters
216  * and removing all characters but '[a-z][0-9]'.
217  * We maintain an array of offsets into the buffer where each word starts
218  * and sort this array using qsort().
219  *
220  * This implements the original cracklib code that did an execl of
221  *    sh -c "/usr/bin/cat <list of files> |
222  *       /usr/bin/tr '[A-Z]' '[a-z]' | /usr/bin/tr -cd '\012[a-z][0-9]' |
223  *       sort -o tmfpfile
224  *
225  * returns 0 on success, -1 on failure.
226  */
227 int
228 sort_file(char *fname)
229 {
230 	int fd;
231 	struct stat statbuf;
232 	ssize_t n;
233 	int ret = -1;
234 
235 	if ((fd = open(fname, O_RDONLY)) == -1) {
236 		syslog(LOG_ERR, MODNAME ": failed to open %s: %s",
237 		    fname, strerror(errno));
238 		return (-1);
239 	}
240 
241 	if (fstat(fd, &statbuf) == -1) {
242 		syslog(LOG_ERR, MODNAME ": fstat() failed (%s)",
243 		    strerror(errno));
244 		(void) close(fd);
245 		return (-1);
246 	}
247 	if ((buf = malloc(statbuf.st_size + 1)) == NULL) {
248 		syslog(LOG_ERR, MODNAME ": out of memory");
249 		goto error;
250 	}
251 
252 	n = read(fd, buf, statbuf.st_size);
253 
254 	if (n == -1) {
255 		if (errno == EINVAL)
256 			syslog(LOG_ERR, MODNAME ": %s is too big. "
257 			    "Split the file into smaller files.", fname);
258 		else
259 			syslog(LOG_ERR, MODNAME ": read failed: %s",
260 			    strerror(errno));
261 		goto error;
262 	}
263 
264 	if (translate(buf, n) == 0) {
265 		qsort((void *)offsets, off_idx, sizeof (int), compare);
266 
267 		if (writeout() == 0)
268 			ret = 0;
269 	}
270 
271 error:
272 	(void) close(fd);
273 
274 	if (buf != NULL)
275 		free(buf);
276 	if (offsets != NULL)
277 		free(offsets);
278 	offsets = NULL;
279 	off_size = 0;
280 	off_idx = 0;
281 	return (ret);
282 }
283 
284 /*
285  * We merge the temporary files created by previous calls to sort_file()
286  * and insert the thus sorted words into the cracklib database
287  *
288  * returns 0 on success, -1 on failure.
289  */
290 int
291 merge_files(PWDICT *pwp)
292 {
293 	int ti;
294 	char *words[MAXTMP];
295 	char lastword[MAXWORDLEN];
296 	int choice;
297 
298 	lastword[0] = '\0';
299 
300 	for (ti = 0; ti < tmpfp_idx; ti++)
301 		if ((words[ti] = malloc(MAXWORDLEN)) == NULL) {
302 			while (--ti >= 0)
303 				free(words[ti]);
304 			return (-1);
305 		}
306 
307 	/*
308 	 * we read the first word of each of the temp-files into words[].
309 	 */
310 	for (ti = 0; ti < tmpfp_idx; ti++) {
311 		(void) fseek(tmpfp[ti], 0, SEEK_SET);
312 		(void) fgets(words[ti], MAXWORDLEN, tmpfp[ti]);
313 		words[ti][MAXWORDLEN-1] = '\0';
314 	}
315 
316 	/*
317 	 * next, we emit the word that comes first (lexicographically),
318 	 * and replace that word with a new word from the file it
319 	 * came from. If the file is exhausted, we close the fp and
320 	 * swap the fp with the last fp in tmpfp[].
321 	 * we then decrease tmpfp_idx and continue with what's left until
322 	 * we run out of open FILE pointers.
323 	 */
324 	while (tmpfp_idx != 0) {
325 		choice = 0;
326 
327 		for (ti = 1; ti < tmpfp_idx; ti++)
328 			if (strcmp(words[choice], words[ti]) > 0)
329 				choice = ti;
330 		/* Insert word in Cracklib database */
331 		(void) Chomp(words[choice]);
332 		if (words[choice][0] != '\0' &&
333 		    strcmp(lastword, words[choice]) != 0) {
334 			(void) PutPW(pwp, words[choice]);
335 			(void) strncpy(lastword, words[choice], MAXWORDLEN);
336 		}
337 
338 		if (fgets(words[choice], MAXWORDLEN, tmpfp[choice]) == NULL) {
339 			(void) fclose(tmpfp[choice]);
340 			tmpfp[choice] = tmpfp[tmpfp_idx - 1];
341 			tmpfp_idx--;
342 		} else
343 			words[choice][MAXWORDLEN-1] = '\0';
344 	}
345 	return (0);
346 }
347 
348 /*
349  * int packer(list)
350  *
351  * sort all dictionaries in "list", and feed the words into the Crack
352  * Password Database.
353  *
354  * returns 0 on sucess, -1 on failure.
355  */
356 int
357 packer(char *list, char *path)
358 {
359 	PWDICT *pwp;
360 	char *listcopy, *fname;
361 	int ret = 0;
362 
363 	if ((listcopy = strdup(list)) == NULL) {
364 		syslog(LOG_ERR, MODNAME ": out of memory");
365 		return (-1);
366 	}
367 
368 	if (!(pwp = PWOpen(path, "wF")))
369 		return (-1);
370 
371 	fname = strtok(listcopy, " \t,");
372 	while (ret == 0 && fname != NULL) {
373 		if ((ret = sort_file(fname)) == 0)
374 			fname = strtok(NULL, " \t,");
375 	}
376 	free(listcopy);
377 
378 	if (ret == 0)
379 		ret = merge_files(pwp);
380 
381 	(void) PWClose(pwp);
382 
383 	return (ret);
384 }
385