xref: /illumos-gate/usr/src/cmd/mkpwdict/mkpwdict.c (revision 2a8bcb4e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <deflt.h>
32 #include <locale.h>
33 #include <libintl.h>
34 #include "packer.h"
35 
36 char options[] = "s:d:";
37 
38 char *pname;
39 
40 void
fatal(char * msg)41 fatal(char *msg)
42 {
43 	(void) fprintf(stderr, "%s: Fatal error: %s. Database not remade.\n",
44 	    pname, msg);
45 	exit(-1);
46 }
47 
48 void
usage(void)49 usage(void)
50 {
51 	(void) fprintf(stderr,
52 	    "usage: %s [-s dict1,...,dictn ] [-d dest-path ]\n", pname);
53 	exit(-1);
54 }
55 
56 int
main(int argc,char * argv[])57 main(int argc, char *argv[])
58 {
59 	char   *default_dbdst = NULL;
60 	char   *default_dbsrc = NULL;
61 	char   *p;
62 
63 	char   *dbdst = NULL;
64 	char   *dbsrc = NULL;
65 	size_t dbsrc_len = 0;
66 	int    c;
67 	int    result;
68 
69 	(void) setlocale(LC_ALL, "");
70 
71 	if ((pname = strrchr(argv[0], '/')) == NULL)
72 		pname = argv[0];
73 	else
74 		pname++;
75 
76 	if (defopen(PWADMIN) == 0) {
77 		if ((p = defread("DICTIONLIST=")) != NULL)
78 			default_dbsrc = strdup(p);
79 		if ((p = defread("DICTIONDBDIR=")) != NULL)
80 			default_dbdst = strdup(p);
81 		(void) defopen(NULL);
82 	}
83 
84 	if (default_dbdst == NULL)
85 		default_dbdst = CRACK_DIR;
86 
87 	while ((c = getopt(argc, argv, options)) != EOF) {
88 		switch (c) {
89 		case 's':
90 			if (dbsrc != NULL) {
91 				dbsrc_len += strlen(optarg) + 2; /* ',' + \0 */
92 				if ((dbsrc = realloc(dbsrc, dbsrc_len)) == NULL)
93 					fatal(strerror(errno));
94 				(void) strlcat(dbsrc, ",", dbsrc_len);
95 				(void) strlcat(dbsrc, optarg, dbsrc_len);
96 			} else {
97 				if ((dbsrc = strdup(optarg)) == NULL)
98 					fatal(strerror(errno));
99 				dbsrc_len = strlen(optarg) + 1;
100 			}
101 			break;
102 		case 'd':
103 			dbdst = optarg;
104 			break;
105 		default:
106 			usage();
107 			break;
108 		}
109 	}
110 	if (optind != argc)
111 		usage();
112 
113 	if (dbdst == NULL) {
114 		(void) fprintf(stderr,
115 		    gettext("%s: using default database location: %s.\n"),
116 		    pname, default_dbdst);
117 		dbdst = default_dbdst;
118 	}
119 
120 	if (dbsrc == NULL)
121 		if ((dbsrc = default_dbsrc) == NULL)
122 			fatal(gettext("No source databases defined"));
123 		else
124 			(void) fprintf(stderr,
125 			    gettext("%s: using default dictionary list: %s.\n"),
126 			    pname, default_dbsrc);
127 
128 	if ((result = lock_db(dbdst)) == 0) {
129 		PWRemove(dbdst);
130 		result = build_dict_database(dbsrc, dbdst);
131 		unlock_db();
132 	}
133 	if (result != 0)
134 		fatal(strerror(errno));
135 	return (0);
136 }
137