xref: /titanic_51/usr/src/cmd/mkpwdict/mkpwdict.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <deflt.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include "packer.h"
37 
38 char options[] = "s:d:";
39 
40 char *pname;
41 
42 void
43 fatal(char *msg)
44 {
45 	(void) fprintf(stderr, "%s: Fatal error: %s. Database not remade.\n",
46 	    pname, msg);
47 	exit(-1);
48 }
49 
50 void
51 usage(void)
52 {
53 	(void) fprintf(stderr,
54 	    "usage: %s [-s dict1,...,dictn ] [-d dest-path ]\n", pname);
55 	exit(-1);
56 }
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	char   *default_dbdst = NULL;
62 	char   *default_dbsrc = NULL;
63 	char   *p;
64 
65 	char   *dbdst = NULL;
66 	char   *dbsrc = NULL;
67 	size_t dbsrc_len = 0;
68 	int    c;
69 	int    result;
70 
71 	(void) setlocale(LC_ALL, "");
72 
73 	if ((pname = strrchr(argv[0], '/')) == NULL)
74 		pname = argv[0];
75 	else
76 		pname++;
77 
78 	if (defopen(PWADMIN) == 0) {
79 		if ((p = defread("DICTIONLIST=")) != NULL)
80 			default_dbsrc = strdup(p);
81 		if ((p = defread("DICTIONDBDIR=")) != NULL)
82 			default_dbdst = strdup(p);
83 		(void) defopen(NULL);
84 	}
85 
86 	if (default_dbdst == NULL)
87 		default_dbdst = CRACK_DIR;
88 
89 	while ((c = getopt(argc, argv, options)) != EOF) {
90 		switch (c) {
91 		case 's':
92 			if (dbsrc != NULL) {
93 				dbsrc_len += strlen(optarg) + 2; /* ',' + \0 */
94 				if ((dbsrc = realloc(dbsrc, dbsrc_len)) == NULL)
95 					fatal(strerror(errno));
96 				(void) strlcat(dbsrc, ",", dbsrc_len);
97 				(void) strlcat(dbsrc, optarg, dbsrc_len);
98 			} else {
99 				if ((dbsrc = strdup(optarg)) == NULL)
100 					fatal(strerror(errno));
101 				dbsrc_len = strlen(optarg) + 1;
102 			}
103 			break;
104 		case 'd':
105 			dbdst = optarg;
106 			break;
107 		default:
108 			usage();
109 			break;
110 		}
111 	}
112 	if (optind != argc)
113 		usage();
114 
115 	if (dbdst == NULL) {
116 		(void) fprintf(stderr,
117 		    gettext("%s: using default database location: %s.\n"),
118 		    pname, default_dbdst);
119 		dbdst = default_dbdst;
120 	}
121 
122 	if (dbsrc == NULL)
123 		if ((dbsrc = default_dbsrc) == NULL)
124 			fatal(gettext("No source databases defined"));
125 		else
126 			(void) fprintf(stderr,
127 			    gettext("%s: using default dictionary list: %s.\n"),
128 			    pname, default_dbsrc);
129 
130 	if ((result = lock_db(dbdst)) == 0) {
131 		PWRemove(dbdst);
132 		result = build_dict_database(dbsrc, dbdst);
133 		unlock_db();
134 	}
135 	if (result != 0)
136 		fatal(strerror(errno));
137 	return (0);
138 }
139