1 /* dircproxy-crypt
2 * Copyright (C) 2000-2003 Scott James Remnant <scott at netsplit dot com>
3 *
4 * Copyright (C) 2004-2008 Francois Harvey <contact at francoisharvey dot ca>
5 *
6 * Copyright (C) 2008-2009 Noel Shrum <noel dot w8tvi at gmail dot com>
7 * Francois Harvey <contact at francoisharvey dot ca>
8 *
9 *
10 * main.c
11 * - Encrypt a password taken from stdin or the command line
12 *
13 * --
14 * @(#) $Id: main.c,v 1.4 2002/12/29 21:30:10 scott Exp $
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #else /* HAVE_CONFIG_H */
39 #define PACKAGE "dircproxy"
40 #define VERSION "-debug"
41 #endif /* HAVE_CONFIG_H */
42
43 #ifdef HAVE_CRYPT_H
44 #include <crypt.h>
45 #else /* HAVE_CRYPT_H */
46 #include <unistd.h>
47 #endif /* HAVE_CRYPT_H */
48
49 #include "getopt/getopt.h"
50
51 /* forward declarations */
52 static void _encrypt(const char *);
53 static void _encrypt_md5(const char *);
54 static void _saltchar(char *);
55 static int _print_usage(void);
56 static int _print_version(void);
57 static int _print_help(void);
58
59 /* This is so "ident" and "what" can query version etc - userful (not) */
60 const char *rcsid = "@(#) $Id: main.c,v 1.4 2002/12/29 21:30:10 scott Exp $";
61
62 /* The name of the program */
63 static char *progname;
64
65 /* Long options */
66 static struct option long_opts[] = {
67 { "help", 0, NULL, 'h' },
68 { "md5", 0, NULL, 'm' },
69 { "version", 0, NULL, 'v' },
70 { 0, 0, 0, 0}
71 };
72
73 /* Options */
74 #define GETOPTIONS "hmv"
75
76 /* The main func */
main(int argc,char * argv[])77 int main(int argc, char *argv[]) {
78 int optc, show_help, show_version, show_usage, use_md5;
79
80 /* Get arguments */
81 progname = argv[0];
82 show_help = show_version = show_usage = use_md5 = 0;
83 while ((optc = getopt_long(argc, argv, GETOPTIONS, long_opts, NULL)) != -1) {
84 switch (optc) {
85 case 'h':
86 show_help = 1;
87 break;
88 case 'm':
89 use_md5 = 1;
90 break;
91 case 'v':
92 show_version = 1;
93 break;
94 default:
95 show_usage = 1;
96 break;
97 }
98 }
99
100 if (show_usage) {
101 _print_usage();
102 return 1;
103 }
104
105 if (show_version) {
106 _print_version();
107 if (!show_help)
108 return 0;
109 }
110
111 if (show_help) {
112 _print_help();
113 return 0;
114 }
115
116 /* Randomize */
117 srand(time(0));
118
119 if (optind < argc) {
120 while (optind < argc) {
121 if (use_md5)
122 _encrypt_md5(argv[optind]);
123 else
124 _encrypt(argv[optind]);
125 optind++;
126 }
127 } else {
128 char pass[80], *ret;
129
130 printf("Password: ");
131 ret = fgets(pass, sizeof(pass), stdin);
132 printf("\n");
133 if (ret) {
134 char *ptr;
135
136 ptr = pass + strlen(pass);
137 while ((ptr >= ret) && (*ptr <= 32)) *(ptr--) = 0;
138 if (use_md5)
139 _encrypt_md5(pass);
140 else
141 _encrypt(pass);
142 }
143 }
144
145 return 0;
146 }
147
148 /* Encrypt a password */
_encrypt(const char * pass)149 static void _encrypt(const char *pass) {
150 char salt[3], *enc;
151
152 _saltchar(&(salt[0]));
153 _saltchar(&(salt[1]));
154 salt[2] = 0;
155
156 enc = crypt(pass, salt);
157 printf("(DES) %s = %s\n", pass, enc);
158 }
159
160 /* Encrypt a password */
_encrypt_md5(const char * pass)161 static void _encrypt_md5(const char *pass)
162 {
163
164 char salt[13], *enc;
165 salt[0] = '$';
166 salt[1] = '1';
167 salt[2] = '$';
168 _saltchar(&(salt[3]));
169 _saltchar(&(salt[4]));
170 _saltchar(&(salt[5]));
171 _saltchar(&(salt[6]));
172 _saltchar(&(salt[7]));
173 _saltchar(&(salt[8]));
174 _saltchar(&(salt[9]));
175 _saltchar(&(salt[10]));
176 salt[11] = '$';
177 salt[12] = 0;
178
179 enc = crypt(pass, salt);
180 printf("(MD5) %s = %s\n", pass, enc);
181 }
182
183
184
185 /* Pick a random salt character */
_saltchar(char * c)186 static void _saltchar(char *c) {
187 static char *chars = "abcdefghijklmnopqrstuvwxyz"
188 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
189 "01234567890";
190 int ran;
191
192 ran = (int)((double)(strlen(chars) - 1) * (rand() / (RAND_MAX + 1.0)));
193 *c = chars[ran];
194 }
195
196 /* Print the usage instructions to stderr */
_print_usage(void)197 static int _print_usage(void) {
198 fprintf(stderr, "%s: Try '%s --help' for more information.\n",
199 progname, progname);
200
201 return 0;
202 }
203
204 /* Print the version number to stderr */
_print_version(void)205 static int _print_version(void) {
206 fprintf(stderr, "dircproxy-crypt (%s %s)\n", PACKAGE, VERSION);
207
208 return 0;
209 }
210
211 /* Print the help to stderr */
_print_help(void)212 static int _print_help(void) {
213 fprintf(stderr, "dircproxy-crypt. Encyrpt passwords for %s.\n\n", PACKAGE);
214 fprintf(stderr, "Usage: %s [OPTION]... [PASSWORD]...\n\n", progname);
215 fprintf(stderr, "If a long option shows an argument as mandatory, then "
216 "it is mandatory\nfor the equivalent short option also. "
217 "Similarly for optional arguments.\n\n");
218 fprintf(stderr, " -h, --help Print a summary of the options\n");
219 fprintf(stderr, " -m, --md5 Generate a md5 password\n");
220 fprintf(stderr, " -v, --version Print the version number\n\n");
221 fprintf(stderr, " PASSWORD Plaintext password to crypt\n\n");
222 fprintf(stderr, "If no passwords are given on the command line, you will "
223 "be asked for one\non standard input.\n\n");
224
225 return 0;
226 }
227