10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*5890Sjjj * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include "packer.h"
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * This file steers the creation of the Crack Dictionary Database.
320Sstevel@tonic-gate * Based on a list of source dictionaries specified by the administrator,
330Sstevel@tonic-gate * we create the Database by sorting each dictionary (in memory, one at
340Sstevel@tonic-gate * a time), writing the sorted result to a temporary file, and merging
350Sstevel@tonic-gate * all the temporary files into the Database.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * The current implementation has a number of limitations
380Sstevel@tonic-gate * - each single source dictionary has to fit in memory
390Sstevel@tonic-gate * - each single source dictionary has to be smaller than 2GByte
400Sstevel@tonic-gate * - each single source dictionary can only hold up to 4GB words
410Sstevel@tonic-gate * None of these seem real, practical, problems to me.
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * All of this is meant to be run by one thread per host. The caller is
440Sstevel@tonic-gate * responsible for locking things appropriately (as make_dict_database
450Sstevel@tonic-gate * in dict.c does).
460Sstevel@tonic-gate */
470Sstevel@tonic-gate
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate #include <ctype.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <errno.h>
540Sstevel@tonic-gate #include <sys/stat.h>
550Sstevel@tonic-gate #include <fcntl.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate /* Stuff used for sorting the dictionary */
580Sstevel@tonic-gate static char *buf; /* used to hold the source dictionary */
590Sstevel@tonic-gate static uint_t *offsets; /* array of word-offsets into "buf" */
600Sstevel@tonic-gate static uint_t off_idx = 0; /* first free index in offsets array */
610Sstevel@tonic-gate static size_t off_size = 0; /* offsets array size */
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* stuff to keep track of the temporary files */
640Sstevel@tonic-gate #define FNAME_TEMPLATE "/var/tmp/authtok_check.XXXXXX"
650Sstevel@tonic-gate #define MAXTMP 64
660Sstevel@tonic-gate static FILE *tmpfp[MAXTMP]; /* FILE *'s to (unlinked) temporary files */
670Sstevel@tonic-gate static int tmpfp_idx = 0; /* points to first free entry in tmpfp */
680Sstevel@tonic-gate
690Sstevel@tonic-gate #define MODNAME "pam_authtok_check::packer"
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * int writeout(void)
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * Write the sorted wordlist to disk. We create a temporary file
750Sstevel@tonic-gate * (in /var/tmp), and immediately unlink() it. We keep an open
760Sstevel@tonic-gate * FILE pointer to it in tmpfp[] for later use.
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * returns 0 on success, -1 on failure (can't create file/output failure).
790Sstevel@tonic-gate */
800Sstevel@tonic-gate int
writeout(void)810Sstevel@tonic-gate writeout(void)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate int i = 0;
840Sstevel@tonic-gate char tmpname[sizeof (FNAME_TEMPLATE)];
850Sstevel@tonic-gate int fd;
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (tmpfp_idx == MAXTMP) {
880Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": too many temporary "
890Sstevel@tonic-gate "files (maximum %d exceeded)", MAXTMP);
900Sstevel@tonic-gate return (-1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate (void) strcpy(tmpname, FNAME_TEMPLATE);
940Sstevel@tonic-gate if ((fd = mkstemp(tmpname)) == -1) {
950Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": mkstemp() failed: %s\n",
960Sstevel@tonic-gate strerror(errno));
970Sstevel@tonic-gate return (-1);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate (void) unlink(tmpname);
1000Sstevel@tonic-gate
1011914Scasper if ((tmpfp[tmpfp_idx] = fdopen(fd, "w+F")) == NULL) {
1020Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": fdopen failed: %s",
1030Sstevel@tonic-gate strerror(errno));
1040Sstevel@tonic-gate (void) close(fd);
1050Sstevel@tonic-gate return (-1);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /* write words to file */
1090Sstevel@tonic-gate while (i < off_idx) {
1100Sstevel@tonic-gate if (fprintf(tmpfp[tmpfp_idx], "%s\n", &buf[offsets[i++]]) < 0) {
1110Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": write to file failed: %s",
1120Sstevel@tonic-gate strerror(errno));
1130Sstevel@tonic-gate (void) close(fd);
1140Sstevel@tonic-gate return (-1);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /* we have one extra tmpfp */
1190Sstevel@tonic-gate tmpfp_idx++;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * int insert_word(int off)
1260Sstevel@tonic-gate *
1270Sstevel@tonic-gate * insert an offset into the offsets-array. If the offsets-array is out of
1280Sstevel@tonic-gate * space, we allocate additional space (in CHUNKs)
1290Sstevel@tonic-gate *
1300Sstevel@tonic-gate * returns 0 on success, -1 on failure (out of memory)
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate int
insert_word(int off)1330Sstevel@tonic-gate insert_word(int off)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate #define CHUNK 10000
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate if (off_idx == off_size) {
1380Sstevel@tonic-gate uint_t *tmp;
1390Sstevel@tonic-gate off_size += CHUNK;
1400Sstevel@tonic-gate tmp = realloc(offsets, sizeof (uint_t) * off_size);
1410Sstevel@tonic-gate if (tmp == NULL) {
1420Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": out of memory");
1430Sstevel@tonic-gate free(offsets);
1440Sstevel@tonic-gate off_idx = off_size = 0;
1450Sstevel@tonic-gate offsets = NULL;
1460Sstevel@tonic-gate return (-1);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate offsets = tmp;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate offsets[off_idx++] = off;
1520Sstevel@tonic-gate return (0);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * translate(buf, size)
1570Sstevel@tonic-gate *
1580Sstevel@tonic-gate * perform "tr '[A-Z]' '[a-z]' | tr -cd '\012[a-z][0-9]'" on the
1590Sstevel@tonic-gate * words in "buf" and insert each of them into the offsets-array.
1600Sstevel@tonic-gate * We refrain from using 'isupper' and 'islower' to keep this strictly
1610Sstevel@tonic-gate * ASCII-only, as is the original Cracklib code.
1620Sstevel@tonic-gate *
1630Sstevel@tonic-gate * returns 0 on success, -1 on failure (failure of insert_word)
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate int
translate(char * buf,size_t size)1660Sstevel@tonic-gate translate(char *buf, size_t size)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate char *p, *q, *e;
1690Sstevel@tonic-gate char c;
1700Sstevel@tonic-gate int wordstart;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate e = &buf[size];
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate wordstart = 0;
1750Sstevel@tonic-gate for (p = buf, q = buf; q < e; q++) {
1760Sstevel@tonic-gate c = *q;
1770Sstevel@tonic-gate if (c >= 'A' && c <= 'Z') {
1780Sstevel@tonic-gate *(p++) = tolower(c);
1790Sstevel@tonic-gate } else if (c == '\n') {
1800Sstevel@tonic-gate *(p++) = '\0';
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * make sure we only insert words consisting of
1830Sstevel@tonic-gate * MAXWORDLEN-1 bytes or less
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate if (p-&buf[wordstart] > MAXWORDLEN)
1860Sstevel@tonic-gate buf[wordstart+MAXWORDLEN-1] = '\0';
1870Sstevel@tonic-gate if (insert_word(wordstart) != 0)
1880Sstevel@tonic-gate return (-1);
1890Sstevel@tonic-gate wordstart = p-buf;
1900Sstevel@tonic-gate } else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
1910Sstevel@tonic-gate *(p++) = c;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate return (0);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate * int compare(a, b)
1990Sstevel@tonic-gate *
2000Sstevel@tonic-gate * helper-routine used for quicksort. we compate two words in the
2010Sstevel@tonic-gate * buffer, one start starts at index "a", and the other one that starts
2020Sstevel@tonic-gate * at index "b"
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate int
compare(const void * a,const void * b)2050Sstevel@tonic-gate compare(const void *a, const void *b)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate int idx_a = *(uint_t *)a, idx_b = *(uint_t *)b;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate return (strcmp(&buf[idx_a], &buf[idx_b]));
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate *
2140Sstevel@tonic-gate * int sort_file(fname)
2150Sstevel@tonic-gate *
2160Sstevel@tonic-gate * We sort the file in memory: we read the dictionary file, translate all
2170Sstevel@tonic-gate * newlines to '\0's, all uppercase ASCII characters to lowercase characters
2180Sstevel@tonic-gate * and removing all characters but '[a-z][0-9]'.
2190Sstevel@tonic-gate * We maintain an array of offsets into the buffer where each word starts
2200Sstevel@tonic-gate * and sort this array using qsort().
2210Sstevel@tonic-gate *
2220Sstevel@tonic-gate * This implements the original cracklib code that did an execl of
2230Sstevel@tonic-gate * sh -c "/usr/bin/cat <list of files> |
2240Sstevel@tonic-gate * /usr/bin/tr '[A-Z]' '[a-z]' | /usr/bin/tr -cd '\012[a-z][0-9]' |
2250Sstevel@tonic-gate * sort -o tmfpfile
2260Sstevel@tonic-gate *
2270Sstevel@tonic-gate * returns 0 on success, -1 on failure.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate int
sort_file(char * fname)2300Sstevel@tonic-gate sort_file(char *fname)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate int fd;
2330Sstevel@tonic-gate struct stat statbuf;
2340Sstevel@tonic-gate ssize_t n;
2350Sstevel@tonic-gate int ret = -1;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) == -1) {
2380Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": failed to open %s: %s",
2390Sstevel@tonic-gate fname, strerror(errno));
2400Sstevel@tonic-gate return (-1);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate if (fstat(fd, &statbuf) == -1) {
2440Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": fstat() failed (%s)",
2450Sstevel@tonic-gate strerror(errno));
2460Sstevel@tonic-gate (void) close(fd);
2470Sstevel@tonic-gate return (-1);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate if ((buf = malloc(statbuf.st_size + 1)) == NULL) {
2500Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": out of memory");
2510Sstevel@tonic-gate goto error;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate n = read(fd, buf, statbuf.st_size);
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (n == -1) {
2570Sstevel@tonic-gate if (errno == EINVAL)
2580Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": %s is too big. "
2590Sstevel@tonic-gate "Split the file into smaller files.", fname);
2600Sstevel@tonic-gate else
2610Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": read failed: %s",
2620Sstevel@tonic-gate strerror(errno));
2630Sstevel@tonic-gate goto error;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate if (translate(buf, n) == 0) {
2670Sstevel@tonic-gate qsort((void *)offsets, off_idx, sizeof (int), compare);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if (writeout() == 0)
2700Sstevel@tonic-gate ret = 0;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate error:
2740Sstevel@tonic-gate (void) close(fd);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate if (buf != NULL)
2770Sstevel@tonic-gate free(buf);
2780Sstevel@tonic-gate if (offsets != NULL)
2790Sstevel@tonic-gate free(offsets);
2800Sstevel@tonic-gate offsets = NULL;
2810Sstevel@tonic-gate off_size = 0;
2820Sstevel@tonic-gate off_idx = 0;
2830Sstevel@tonic-gate return (ret);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate * We merge the temporary files created by previous calls to sort_file()
2880Sstevel@tonic-gate * and insert the thus sorted words into the cracklib database
289*5890Sjjj *
290*5890Sjjj * returns 0 on success, -1 on failure.
2910Sstevel@tonic-gate */
292*5890Sjjj int
merge_files(PWDICT * pwp)2930Sstevel@tonic-gate merge_files(PWDICT *pwp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate int ti;
2960Sstevel@tonic-gate char *words[MAXTMP];
2970Sstevel@tonic-gate char lastword[MAXWORDLEN];
2980Sstevel@tonic-gate int choice;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate lastword[0] = '\0';
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate for (ti = 0; ti < tmpfp_idx; ti++)
303*5890Sjjj if ((words[ti] = malloc(MAXWORDLEN)) == NULL) {
304*5890Sjjj while (--ti >= 0)
305*5890Sjjj free(words[ti]);
306*5890Sjjj return (-1);
307*5890Sjjj }
308*5890Sjjj
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * we read the first word of each of the temp-files into words[].
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate for (ti = 0; ti < tmpfp_idx; ti++) {
3130Sstevel@tonic-gate (void) fseek(tmpfp[ti], 0, SEEK_SET);
3140Sstevel@tonic-gate (void) fgets(words[ti], MAXWORDLEN, tmpfp[ti]);
3150Sstevel@tonic-gate words[ti][MAXWORDLEN-1] = '\0';
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate * next, we emit the word that comes first (lexicographically),
3200Sstevel@tonic-gate * and replace that word with a new word from the file it
3210Sstevel@tonic-gate * came from. If the file is exhausted, we close the fp and
3220Sstevel@tonic-gate * swap the fp with the last fp in tmpfp[].
3230Sstevel@tonic-gate * we then decrease tmpfp_idx and continue with what's left until
3240Sstevel@tonic-gate * we run out of open FILE pointers.
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate while (tmpfp_idx != 0) {
3270Sstevel@tonic-gate choice = 0;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate for (ti = 1; ti < tmpfp_idx; ti++)
3300Sstevel@tonic-gate if (strcmp(words[choice], words[ti]) > 0)
3310Sstevel@tonic-gate choice = ti;
3320Sstevel@tonic-gate /* Insert word in Cracklib database */
3330Sstevel@tonic-gate (void) Chomp(words[choice]);
3340Sstevel@tonic-gate if (words[choice][0] != '\0' &&
3350Sstevel@tonic-gate strcmp(lastword, words[choice]) != 0) {
3360Sstevel@tonic-gate (void) PutPW(pwp, words[choice]);
3370Sstevel@tonic-gate (void) strncpy(lastword, words[choice], MAXWORDLEN);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate if (fgets(words[choice], MAXWORDLEN, tmpfp[choice]) == NULL) {
3410Sstevel@tonic-gate (void) fclose(tmpfp[choice]);
3420Sstevel@tonic-gate tmpfp[choice] = tmpfp[tmpfp_idx - 1];
3430Sstevel@tonic-gate tmpfp_idx--;
3440Sstevel@tonic-gate } else
3450Sstevel@tonic-gate words[choice][MAXWORDLEN-1] = '\0';
3460Sstevel@tonic-gate }
347*5890Sjjj return (0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * int packer(list)
3520Sstevel@tonic-gate *
3530Sstevel@tonic-gate * sort all dictionaries in "list", and feed the words into the Crack
3540Sstevel@tonic-gate * Password Database.
3550Sstevel@tonic-gate *
3560Sstevel@tonic-gate * returns 0 on sucess, -1 on failure.
3570Sstevel@tonic-gate */
3580Sstevel@tonic-gate int
packer(char * list,char * path)3590Sstevel@tonic-gate packer(char *list, char *path)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate PWDICT *pwp;
3620Sstevel@tonic-gate char *listcopy, *fname;
3630Sstevel@tonic-gate int ret = 0;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if ((listcopy = strdup(list)) == NULL) {
3660Sstevel@tonic-gate syslog(LOG_ERR, MODNAME ": out of memory");
3670Sstevel@tonic-gate return (-1);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3701914Scasper if (!(pwp = PWOpen(path, "wF")))
3710Sstevel@tonic-gate return (-1);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate fname = strtok(listcopy, " \t,");
3740Sstevel@tonic-gate while (ret == 0 && fname != NULL) {
3750Sstevel@tonic-gate if ((ret = sort_file(fname)) == 0)
3760Sstevel@tonic-gate fname = strtok(NULL, " \t,");
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate free(listcopy);
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate if (ret == 0)
381*5890Sjjj ret = merge_files(pwp);
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate (void) PWClose(pwp);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate return (ret);
3860Sstevel@tonic-gate }
387