1 /*
2  * Copyright (c) 2008
3  *      Matt Harris.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Mr. Harris AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL Mr. Harris OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include "localconfig.h"
36 #include <pwstor.h>
37 
38 
39 #define OPT_NOBASE64		0x001
40 #define OPT_MULTI		0x002
41 
42 
43 extern char *optarg;
44 extern int optind;
45 uint16_t Options;
46 
47 static char *pwtool_prompt(uint32_t l);
48 
49 
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52 	char goc;
53 	char *pass;
54 	size_t pass_len;
55 	uint32_t max_len;
56 	PasswordData *data;
57 
58 	Options = 0;
59 	pass = (char *)NULL;
60 	pass_len = 0;
61 	max_len = 0;
62 	data = (PasswordData *)NULL;
63 
64 	while ((goc = getopt(argc, argv, "hvbp:")) != -1)
65 	{
66 		switch (goc)
67 		{
68 			case 'h':
69 			{
70 				break;
71 			}
72 			case 'v':
73 			{
74 				fprintf(stdout, "[%s] pwtool built with libpwstor version %s\n\n", argv[0], pws_version());
75 				fprintf(stdout, "Copyright 2008 Matt Harris.  All rights reserved.  \nSee the LICENSE file in the source distribution for terms of use.\n");
76 				fprintf(stdout, "\tURL: http://www.sourceforge.net/projects/kageki/\n\n");
77 				return(0);
78 				break;
79 			}
80 			case 'p':
81 			{
82 				pass = (char *)malloc(strlen(optarg) + 1);
83 				if (!pass)
84 				{
85 					fprintf(stdout, "Error: malloc(pass): %s\n", strerror(errno));
86 					return(1);
87 				}
88 				memset(pass, 0, (strlen(optarg) + 1));
89 				pass_len = strlen(optarg);
90 				snprintf(pass, (strlen(optarg) + 1), "%s", optarg);
91 				break;
92 			}
93 			case 'b':
94 			{
95 				Options |= OPT_NOBASE64;
96 				break;
97 			}
98 			default:
99 			{
100 				fprintf(stdout, "Error: Invalid argument \"-%c\".\n", goc);
101 				return(3);
102 				break;
103 			}
104 		}
105 	}
106 
107 	if (!pass)
108 	{
109 		pass = pwtool_prompt(max_len);
110 		if (!pass)
111 		{
112 			fprintf(stdout, "Error: pwtool_prompt(): %s\n", strerror(errno));
113 			return(2);
114 		}
115 	}
116 
117 	data = (PasswordData *)calloc(1, sizeof(PasswordData));
118 	if (!data)
119 	{
120 		fprintf(stdout, "Error: malloc(data): %s\n", strerror(errno));
121 		return(1);
122 	}
123 
124 	if (pws_passwords_encodemulti(pass, data) < 0)
125 	{
126 		if (pass)
127 		{
128 			pws_memnuke((volatile void *)pass, pass_len);
129 			free(pass);
130 		}
131 
132 		fprintf(stdout, "Error: pws_passwords_encodemulti(): %s\n", strerror(errno));
133 		return(2);
134 	}
135 
136 	if (Options & OPT_NOBASE64)
137 	{
138 	}
139 	else
140 	{
141 		fprintf(stdout, "%c|%s|%s", data->version, data->salt_b64, data->hash_b64);
142 	}
143 
144 	if (pass)
145 	{
146 		pws_memnuke((volatile void *)pass, pass_len);
147 		free(pass);
148 	}
149 
150 	fputc('\n', stdout);
151 
152 	return(0);
153 }
154 
pwtool_prompt(uint32_t l)155 char *pwtool_prompt(uint32_t l)
156 {
157 	return((char *)NULL);
158 }
159