]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Peter Sylvester (peter.sylvester@edelweb.fr) for the EdelKey | |
3 | * project and contributed to the OpenSSL project 2004. | |
edc032b5 BL |
4 | */ |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
edc032b5 BL |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
effaf4de | 58 | |
edc032b5 | 59 | #include <openssl/opensslconf.h> |
effaf4de RS |
60 | #ifdef OPENSSL_NO_SRP |
61 | NON_EMPTY_TRANSLATION_UNIT | |
62 | #else | |
edc032b5 | 63 | |
effaf4de RS |
64 | # include <stdio.h> |
65 | # include <stdlib.h> | |
66 | # include <string.h> | |
67 | # include <openssl/conf.h> | |
68 | # include <openssl/bio.h> | |
69 | # include <openssl/err.h> | |
70 | # include <openssl/txt_db.h> | |
71 | # include <openssl/buffer.h> | |
72 | # include <openssl/srp.h> | |
73 | # include "apps.h" | |
6e81b270 | 74 | |
6e81b270 AP |
75 | # define BASE_SECTION "srp" |
76 | # define CONFIG_FILE "openssl.cnf" | |
77 | ||
78 | # define ENV_RANDFILE "RANDFILE" | |
79 | ||
80 | # define ENV_DATABASE "srpvfile" | |
81 | # define ENV_DEFAULT_SRP "default_srp" | |
82 | ||
6e81b270 AP |
83 | static int get_index(CA_DB *db, char *id, char type) |
84 | { | |
85 | char **pp; | |
86 | int i; | |
87 | if (id == NULL) | |
88 | return -1; | |
89 | if (type == DB_SRP_INDEX) | |
90 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
91 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
92 | if (pp[DB_srptype][0] == DB_SRP_INDEX | |
86885c28 | 93 | && strcmp(id, pp[DB_srpid]) == 0) |
6e81b270 AP |
94 | return i; |
95 | } else | |
96 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
97 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
98 | ||
99 | if (pp[DB_srptype][0] != DB_SRP_INDEX | |
86885c28 | 100 | && strcmp(id, pp[DB_srpid]) == 0) |
6e81b270 AP |
101 | return i; |
102 | } | |
103 | ||
104 | return -1; | |
105 | } | |
edc032b5 | 106 | |
ecf3a1fb | 107 | static void print_entry(CA_DB *db, int indx, int verbose, char *s) |
6e81b270 AP |
108 | { |
109 | if (indx >= 0 && verbose) { | |
110 | int j; | |
111 | char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx); | |
ecf3a1fb | 112 | BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]); |
6e81b270 AP |
113 | for (j = 0; j < DB_NUMBER; j++) { |
114 | BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]); | |
115 | } | |
116 | } | |
117 | } | |
edc032b5 | 118 | |
ecf3a1fb | 119 | static void print_index(CA_DB *db, int indexindex, int verbose) |
6e81b270 | 120 | { |
ecf3a1fb | 121 | print_entry(db, indexindex, verbose, "g N entry"); |
6e81b270 | 122 | } |
edc032b5 | 123 | |
ecf3a1fb | 124 | static void print_user(CA_DB *db, int userindex, int verbose) |
6e81b270 AP |
125 | { |
126 | if (verbose > 0) { | |
127 | char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
edc032b5 | 128 | |
6e81b270 | 129 | if (pp[DB_srptype][0] != 'I') { |
ecf3a1fb RS |
130 | print_entry(db, userindex, verbose, "User entry"); |
131 | print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose, | |
6e81b270 AP |
132 | "g N entry"); |
133 | } | |
edc032b5 | 134 | |
6e81b270 AP |
135 | } |
136 | } | |
edc032b5 | 137 | |
ecf3a1fb | 138 | static int update_index(CA_DB *db, char **row) |
6e81b270 AP |
139 | { |
140 | char **irow; | |
141 | int i; | |
142 | ||
b4faea50 | 143 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers"); |
6e81b270 AP |
144 | for (i = 0; i < DB_NUMBER; i++) { |
145 | irow[i] = row[i]; | |
146 | row[i] = NULL; | |
147 | } | |
148 | irow[DB_NUMBER] = NULL; | |
149 | ||
150 | if (!TXT_DB_insert(db->db, irow)) { | |
ecf3a1fb RS |
151 | BIO_printf(bio_err, "failed to update srpvfile\n"); |
152 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
6e81b270 AP |
153 | OPENSSL_free(irow); |
154 | return 0; | |
155 | } | |
156 | return 1; | |
157 | } | |
edc032b5 BL |
158 | |
159 | static void lookup_fail(const char *name, const char *tag) | |
6e81b270 AP |
160 | { |
161 | BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag); | |
162 | } | |
edc032b5 BL |
163 | |
164 | static char *srp_verify_user(const char *user, const char *srp_verifier, | |
6e81b270 | 165 | char *srp_usersalt, const char *g, const char *N, |
ecf3a1fb | 166 | const char *passin, int verbose) |
6e81b270 AP |
167 | { |
168 | char password[1024]; | |
169 | PW_CB_DATA cb_tmp; | |
170 | char *verifier = NULL; | |
171 | char *gNid = NULL; | |
172 | ||
173 | cb_tmp.prompt_info = user; | |
174 | cb_tmp.password = passin; | |
175 | ||
176 | if (password_callback(password, 1024, 0, &cb_tmp) > 0) { | |
7e1b7485 | 177 | if (verbose) |
ecf3a1fb | 178 | BIO_printf(bio_err, |
7e1b7485 RS |
179 | "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n", |
180 | user, srp_verifier, srp_usersalt, g, N); | |
ecf3a1fb | 181 | BIO_printf(bio_err, "Pass %s\n", password); |
6e81b270 AP |
182 | |
183 | OPENSSL_assert(srp_usersalt != NULL); | |
7e1b7485 RS |
184 | if (! |
185 | (gNid = | |
186 | SRP_create_verifier(user, password, &srp_usersalt, &verifier, N, | |
187 | g))) { | |
ecf3a1fb | 188 | BIO_printf(bio_err, "Internal error validating SRP verifier\n"); |
6e81b270 AP |
189 | } else { |
190 | if (strcmp(verifier, srp_verifier)) | |
191 | gNid = NULL; | |
192 | OPENSSL_free(verifier); | |
193 | } | |
194 | } | |
195 | return gNid; | |
196 | } | |
edc032b5 | 197 | |
71fa4513 | 198 | static char *srp_create_user(char *user, char **srp_verifier, |
6e81b270 | 199 | char **srp_usersalt, char *g, char *N, |
ecf3a1fb | 200 | char *passout, int verbose) |
6e81b270 AP |
201 | { |
202 | char password[1024]; | |
203 | PW_CB_DATA cb_tmp; | |
204 | char *gNid = NULL; | |
205 | char *salt = NULL; | |
206 | cb_tmp.prompt_info = user; | |
207 | cb_tmp.password = passout; | |
208 | ||
209 | if (password_callback(password, 1024, 1, &cb_tmp) > 0) { | |
7e1b7485 | 210 | if (verbose) |
ecf3a1fb | 211 | BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n", |
7e1b7485 RS |
212 | user, g, N); |
213 | if (! | |
214 | (gNid = | |
215 | SRP_create_verifier(user, password, &salt, srp_verifier, N, | |
216 | g))) { | |
ecf3a1fb | 217 | BIO_printf(bio_err, "Internal error creating SRP verifier\n"); |
6e81b270 AP |
218 | } else |
219 | *srp_usersalt = salt; | |
7e1b7485 | 220 | if (verbose > 1) |
ecf3a1fb | 221 | BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid, |
7e1b7485 | 222 | salt, *srp_verifier); |
6e81b270 AP |
223 | |
224 | } | |
225 | return gNid; | |
226 | } | |
edc032b5 | 227 | |
7e1b7485 RS |
228 | typedef enum OPTION_choice { |
229 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
230 | OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD, | |
231 | OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO, | |
232 | OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE | |
233 | } OPTION_CHOICE; | |
234 | ||
235 | OPTIONS srp_options[] = { | |
236 | {"help", OPT_HELP, '-', "Display this summary"}, | |
237 | {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"}, | |
238 | {"config", OPT_CONFIG, '<', "A config file"}, | |
239 | {"name", OPT_NAME, 's', "The particular srp definition to use"}, | |
240 | {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"}, | |
241 | {"add", OPT_ADD, '-', "Add a user and srp verifier"}, | |
242 | {"modify", OPT_MODIFY, '-', | |
243 | "Modify the srp verifier of an existing user"}, | |
244 | {"delete", OPT_DELETE, '-', "Delete user from verifier file"}, | |
245 | {"list", OPT_LIST, '-', "List users"}, | |
246 | {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"}, | |
247 | {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"}, | |
248 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
249 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
6e81b270 | 250 | # ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 251 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
6e81b270 | 252 | # endif |
7e1b7485 RS |
253 | {NULL} |
254 | }; | |
255 | ||
256 | int srp_main(int argc, char **argv) | |
257 | { | |
258 | CA_DB *db = NULL; | |
6e81b270 | 259 | DB_ATTR db_attr; |
7e1b7485 | 260 | CONF *conf = NULL; |
cc01d217 RS |
261 | int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i; |
262 | int doupdatedb = 0, mode = OPT_ERR; | |
7e1b7485 RS |
263 | char *user = NULL, *passinarg = NULL, *passoutarg = NULL; |
264 | char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL; | |
333b070e | 265 | char *randfile = NULL, *tofree = NULL, *section = NULL; |
dfd6211c MC |
266 | char **gNrow = NULL, *configfile = NULL; |
267 | char *srpvfile = NULL, **pp, *prog; | |
7e1b7485 | 268 | OPTION_CHOICE o; |
6e81b270 | 269 | |
7e1b7485 RS |
270 | prog = opt_init(argc, argv, srp_options); |
271 | while ((o = opt_next()) != OPT_EOF) { | |
272 | switch (o) { | |
273 | case OPT_EOF: | |
274 | case OPT_ERR: | |
275 | opthelp: | |
276 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
277 | goto end; | |
278 | case OPT_HELP: | |
279 | opt_help(srp_options); | |
280 | ret = 0; | |
281 | goto end; | |
282 | case OPT_VERBOSE: | |
6e81b270 | 283 | verbose++; |
6e81b270 | 284 | break; |
7e1b7485 RS |
285 | case OPT_CONFIG: |
286 | configfile = opt_arg(); | |
6e81b270 | 287 | break; |
7e1b7485 RS |
288 | case OPT_NAME: |
289 | section = opt_arg(); | |
290 | break; | |
291 | case OPT_SRPVFILE: | |
dfd6211c | 292 | srpvfile = opt_arg(); |
7e1b7485 RS |
293 | break; |
294 | case OPT_ADD: | |
295 | case OPT_DELETE: | |
296 | case OPT_MODIFY: | |
297 | case OPT_LIST: | |
298 | if (mode != OPT_ERR) { | |
299 | BIO_printf(bio_err, | |
300 | "%s: Only one of -add/delete-modify/-list\n", | |
301 | prog); | |
302 | goto opthelp; | |
303 | } | |
304 | mode = o; | |
305 | break; | |
306 | case OPT_GN: | |
307 | gN = opt_arg(); | |
308 | break; | |
309 | case OPT_USERINFO: | |
310 | userinfo = opt_arg(); | |
311 | break; | |
312 | case OPT_PASSIN: | |
313 | passinarg = opt_arg(); | |
314 | break; | |
315 | case OPT_PASSOUT: | |
316 | passoutarg = opt_arg(); | |
317 | break; | |
318 | case OPT_ENGINE: | |
333b070e | 319 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
320 | break; |
321 | } | |
6e81b270 | 322 | } |
7e1b7485 RS |
323 | argc = opt_num_rest(); |
324 | argv = opt_rest(); | |
6e81b270 | 325 | |
dfd6211c | 326 | if (srpvfile && configfile) { |
6e81b270 | 327 | BIO_printf(bio_err, |
dfd6211c | 328 | "-srpvfile and -configfile cannot be specified together.\n"); |
7e1b7485 | 329 | goto end; |
6e81b270 | 330 | } |
7e1b7485 RS |
331 | if (mode == OPT_ERR) { |
332 | BIO_printf(bio_err, | |
333 | "Exactly one of the options -add, -delete, -modify -list must be specified.\n"); | |
334 | goto opthelp; | |
6e81b270 | 335 | } |
5a80d9fb RS |
336 | if ((mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) |
337 | && argc < 1) { | |
7e1b7485 RS |
338 | BIO_printf(bio_err, |
339 | "Need at least one user for options -add, -delete, -modify. \n"); | |
340 | goto opthelp; | |
6e81b270 AP |
341 | } |
342 | if ((passin || passout) && argc != 1) { | |
343 | BIO_printf(bio_err, | |
344 | "-passin, -passout arguments only valid with one user.\n"); | |
7e1b7485 | 345 | goto opthelp; |
6e81b270 | 346 | } |
6e81b270 | 347 | |
7e1b7485 | 348 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
6e81b270 | 349 | BIO_printf(bio_err, "Error getting passwords\n"); |
7e1b7485 | 350 | goto end; |
6e81b270 AP |
351 | } |
352 | ||
dfd6211c MC |
353 | if (!srpvfile) { |
354 | if (!configfile) | |
355 | configfile = default_config_file; | |
356 | ||
7e1b7485 | 357 | if (verbose) |
cc01d217 RS |
358 | BIO_printf(bio_err, "Using configuration from %s\n", |
359 | configfile); | |
360 | conf = app_load_config(configfile); | |
361 | if (conf == NULL) | |
7e1b7485 | 362 | goto end; |
dfd6211c | 363 | if (!app_load_modules(conf)) |
296f54ee | 364 | goto end; |
6e81b270 | 365 | |
6e81b270 AP |
366 | /* Lets get the config section we are using */ |
367 | if (section == NULL) { | |
7e1b7485 RS |
368 | if (verbose) |
369 | BIO_printf(bio_err, | |
370 | "trying to read " ENV_DEFAULT_SRP | |
44fdf1c2 | 371 | " in " BASE_SECTION "\n"); |
6e81b270 AP |
372 | |
373 | section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP); | |
374 | if (section == NULL) { | |
375 | lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP); | |
7e1b7485 | 376 | goto end; |
6e81b270 AP |
377 | } |
378 | } | |
379 | ||
380 | if (randfile == NULL && conf) | |
381 | randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE"); | |
382 | ||
7e1b7485 RS |
383 | if (verbose) |
384 | BIO_printf(bio_err, | |
385 | "trying to read " ENV_DATABASE " in section \"%s\"\n", | |
386 | section); | |
6e81b270 | 387 | |
dfd6211c MC |
388 | if ((srpvfile = NCONF_get_string(conf, section, ENV_DATABASE)) |
389 | == NULL) { | |
6e81b270 | 390 | lookup_fail(section, ENV_DATABASE); |
7e1b7485 | 391 | goto end; |
6e81b270 AP |
392 | } |
393 | ||
394 | } | |
395 | if (randfile == NULL) | |
396 | ERR_clear_error(); | |
397 | else | |
7e1b7485 | 398 | app_RAND_load_file(randfile, 0); |
6e81b270 | 399 | |
7e1b7485 RS |
400 | if (verbose) |
401 | BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n", | |
dfd6211c | 402 | srpvfile); |
6e81b270 | 403 | |
dfd6211c | 404 | db = load_index(srpvfile, &db_attr); |
6e81b270 | 405 | if (db == NULL) |
7e1b7485 | 406 | goto end; |
6e81b270 AP |
407 | |
408 | /* Lets check some fields */ | |
409 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
410 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
411 | ||
412 | if (pp[DB_srptype][0] == DB_SRP_INDEX) { | |
413 | maxgN = i; | |
86885c28 | 414 | if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0) |
6e81b270 AP |
415 | gNindex = i; |
416 | ||
ecf3a1fb | 417 | print_index(db, i, verbose > 1); |
6e81b270 AP |
418 | } |
419 | } | |
420 | ||
7e1b7485 RS |
421 | if (verbose) |
422 | BIO_printf(bio_err, "Database initialised\n"); | |
6e81b270 AP |
423 | |
424 | if (gNindex >= 0) { | |
425 | gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex); | |
ecf3a1fb | 426 | print_entry(db, gNindex, verbose > 1, "Default g and N"); |
6e81b270 AP |
427 | } else if (maxgN > 0 && !SRP_get_default_gN(gN)) { |
428 | BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN); | |
7e1b7485 | 429 | goto end; |
6e81b270 | 430 | } else { |
7e1b7485 RS |
431 | if (verbose) |
432 | BIO_printf(bio_err, "Database has no g N information.\n"); | |
6e81b270 AP |
433 | gNrow = NULL; |
434 | } | |
435 | ||
7e1b7485 RS |
436 | if (verbose > 1) |
437 | BIO_printf(bio_err, "Starting user processing\n"); | |
6e81b270 AP |
438 | |
439 | if (argc > 0) | |
440 | user = *(argv++); | |
441 | ||
7e1b7485 | 442 | while (mode == OPT_LIST || user) { |
6e81b270 AP |
443 | int userindex = -1; |
444 | if (user) | |
7e1b7485 RS |
445 | if (verbose > 1) |
446 | BIO_printf(bio_err, "Processing user \"%s\"\n", user); | |
6e81b270 | 447 | if ((userindex = get_index(db, user, 'U')) >= 0) { |
ecf3a1fb | 448 | print_user(db, userindex, (verbose > 0) |
7e1b7485 | 449 | || mode == OPT_LIST); |
6e81b270 AP |
450 | } |
451 | ||
7e1b7485 | 452 | if (mode == OPT_LIST) { |
6e81b270 AP |
453 | if (user == NULL) { |
454 | BIO_printf(bio_err, "List all users\n"); | |
455 | ||
456 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
ecf3a1fb | 457 | print_user(db, i, 1); |
6e81b270 | 458 | } |
6e81b270 AP |
459 | } else if (userindex < 0) { |
460 | BIO_printf(bio_err, | |
461 | "user \"%s\" does not exist, ignored. t\n", user); | |
462 | errors++; | |
463 | } | |
7e1b7485 | 464 | } else if (mode == OPT_ADD) { |
6e81b270 AP |
465 | if (userindex >= 0) { |
466 | /* reactivation of a new user */ | |
467 | char **row = | |
468 | sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
469 | BIO_printf(bio_err, "user \"%s\" reactivated.\n", user); | |
470 | row[DB_srptype][0] = 'V'; | |
471 | ||
472 | doupdatedb = 1; | |
473 | } else { | |
474 | char *row[DB_NUMBER]; | |
475 | char *gNid; | |
476 | row[DB_srpverifier] = NULL; | |
477 | row[DB_srpsalt] = NULL; | |
478 | row[DB_srpinfo] = NULL; | |
7e1b7485 RS |
479 | if (! |
480 | (gNid = | |
481 | srp_create_user(user, &(row[DB_srpverifier]), | |
482 | &(row[DB_srpsalt]), | |
483 | gNrow ? gNrow[DB_srpsalt] : gN, | |
484 | gNrow ? gNrow[DB_srpverifier] : NULL, | |
ecf3a1fb | 485 | passout, verbose))) { |
6e81b270 | 486 | BIO_printf(bio_err, |
7e1b7485 RS |
487 | "Cannot create srp verifier for user \"%s\", operation abandoned .\n", |
488 | user); | |
6e81b270 | 489 | errors++; |
7e1b7485 | 490 | goto end; |
6e81b270 | 491 | } |
7644a9ae RS |
492 | row[DB_srpid] = OPENSSL_strdup(user); |
493 | row[DB_srptype] = OPENSSL_strdup("v"); | |
494 | row[DB_srpgN] = OPENSSL_strdup(gNid); | |
6e81b270 | 495 | |
75ebbd9a RS |
496 | if ((row[DB_srpid] == NULL) |
497 | || (row[DB_srpgN] == NULL) | |
498 | || (row[DB_srptype] == NULL) | |
499 | || (row[DB_srpverifier] == NULL) | |
500 | || (row[DB_srpsalt] == NULL) | |
501 | || (userinfo | |
7644a9ae | 502 | && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL)) |
ecf3a1fb | 503 | || !update_index(db, row)) { |
b548a1f1 RS |
504 | OPENSSL_free(row[DB_srpid]); |
505 | OPENSSL_free(row[DB_srpgN]); | |
506 | OPENSSL_free(row[DB_srpinfo]); | |
507 | OPENSSL_free(row[DB_srptype]); | |
508 | OPENSSL_free(row[DB_srpverifier]); | |
509 | OPENSSL_free(row[DB_srpsalt]); | |
7e1b7485 | 510 | goto end; |
6e81b270 AP |
511 | } |
512 | doupdatedb = 1; | |
513 | } | |
7e1b7485 | 514 | } else if (mode == OPT_MODIFY) { |
6e81b270 AP |
515 | if (userindex < 0) { |
516 | BIO_printf(bio_err, | |
517 | "user \"%s\" does not exist, operation ignored.\n", | |
518 | user); | |
519 | errors++; | |
520 | } else { | |
521 | ||
522 | char **row = | |
523 | sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
524 | char type = row[DB_srptype][0]; | |
525 | if (type == 'v') { | |
526 | BIO_printf(bio_err, | |
527 | "user \"%s\" already updated, operation ignored.\n", | |
528 | user); | |
529 | errors++; | |
530 | } else { | |
531 | char *gNid; | |
532 | ||
533 | if (row[DB_srptype][0] == 'V') { | |
534 | int user_gN; | |
535 | char **irow = NULL; | |
7e1b7485 RS |
536 | if (verbose) |
537 | BIO_printf(bio_err, | |
538 | "Verifying password for user \"%s\"\n", | |
539 | user); | |
6e81b270 AP |
540 | if ((user_gN = |
541 | get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0) | |
542 | irow = | |
543 | sk_OPENSSL_PSTRING_value(db->db->data, | |
544 | userindex); | |
545 | ||
546 | if (!srp_verify_user | |
547 | (user, row[DB_srpverifier], row[DB_srpsalt], | |
548 | irow ? irow[DB_srpsalt] : row[DB_srpgN], | |
549 | irow ? irow[DB_srpverifier] : NULL, passin, | |
ecf3a1fb | 550 | verbose)) { |
6e81b270 AP |
551 | BIO_printf(bio_err, |
552 | "Invalid password for user \"%s\", operation abandoned.\n", | |
553 | user); | |
554 | errors++; | |
7e1b7485 | 555 | goto end; |
6e81b270 AP |
556 | } |
557 | } | |
7e1b7485 RS |
558 | if (verbose) |
559 | BIO_printf(bio_err, "Password for user \"%s\" ok.\n", | |
560 | user); | |
561 | ||
562 | if (! | |
563 | (gNid = | |
564 | srp_create_user(user, &(row[DB_srpverifier]), | |
565 | &(row[DB_srpsalt]), | |
566 | gNrow ? gNrow[DB_srpsalt] : NULL, | |
567 | gNrow ? gNrow[DB_srpverifier] : NULL, | |
ecf3a1fb | 568 | passout, verbose))) { |
6e81b270 | 569 | BIO_printf(bio_err, |
7e1b7485 RS |
570 | "Cannot create srp verifier for user \"%s\", operation abandoned.\n", |
571 | user); | |
6e81b270 | 572 | errors++; |
7e1b7485 | 573 | goto end; |
6e81b270 AP |
574 | } |
575 | ||
576 | row[DB_srptype][0] = 'v'; | |
7644a9ae | 577 | row[DB_srpgN] = OPENSSL_strdup(gNid); |
6e81b270 | 578 | |
75ebbd9a RS |
579 | if (row[DB_srpid] == NULL |
580 | || row[DB_srpgN] == NULL | |
581 | || row[DB_srptype] == NULL | |
582 | || row[DB_srpverifier] == NULL | |
583 | || row[DB_srpsalt] == NULL | |
6e81b270 | 584 | || (userinfo |
7644a9ae | 585 | && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) |
75ebbd9a | 586 | == NULL))) |
7e1b7485 | 587 | goto end; |
6e81b270 AP |
588 | |
589 | doupdatedb = 1; | |
590 | } | |
591 | } | |
7e1b7485 | 592 | } else if (mode == OPT_DELETE) { |
6e81b270 AP |
593 | if (userindex < 0) { |
594 | BIO_printf(bio_err, | |
595 | "user \"%s\" does not exist, operation ignored. t\n", | |
596 | user); | |
597 | errors++; | |
598 | } else { | |
75ebbd9a | 599 | char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex); |
6e81b270 | 600 | |
75ebbd9a | 601 | BIO_printf(bio_err, "user \"%s\" revoked. t\n", user); |
6e81b270 | 602 | xpp[DB_srptype][0] = 'R'; |
6e81b270 AP |
603 | doupdatedb = 1; |
604 | } | |
605 | } | |
606 | if (--argc > 0) | |
607 | user = *(argv++); | |
608 | else { | |
609 | user = NULL; | |
6e81b270 AP |
610 | } |
611 | } | |
612 | ||
7e1b7485 RS |
613 | if (verbose) |
614 | BIO_printf(bio_err, "User procession done.\n"); | |
6e81b270 AP |
615 | |
616 | if (doupdatedb) { | |
617 | /* Lets check some fields */ | |
618 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
619 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
620 | ||
621 | if (pp[DB_srptype][0] == 'v') { | |
622 | pp[DB_srptype][0] = 'V'; | |
ecf3a1fb | 623 | print_user(db, i, verbose); |
6e81b270 AP |
624 | } |
625 | } | |
626 | ||
7e1b7485 RS |
627 | if (verbose) |
628 | BIO_printf(bio_err, "Trying to update srpvfile.\n"); | |
dfd6211c | 629 | if (!save_index(srpvfile, "new", db)) |
7e1b7485 | 630 | goto end; |
6e81b270 | 631 | |
7e1b7485 RS |
632 | if (verbose) |
633 | BIO_printf(bio_err, "Temporary srpvfile created.\n"); | |
dfd6211c | 634 | if (!rotate_index(srpvfile, "new", "old")) |
7e1b7485 | 635 | goto end; |
6e81b270 | 636 | |
7e1b7485 RS |
637 | if (verbose) |
638 | BIO_printf(bio_err, "srpvfile updated.\n"); | |
6e81b270 AP |
639 | } |
640 | ||
641 | ret = (errors != 0); | |
7e1b7485 | 642 | end: |
6e81b270 | 643 | if (errors != 0) |
7e1b7485 RS |
644 | if (verbose) |
645 | BIO_printf(bio_err, "User errors %d.\n", errors); | |
6e81b270 | 646 | |
7e1b7485 RS |
647 | if (verbose) |
648 | BIO_printf(bio_err, "SRP terminating with code %d.\n", ret); | |
b548a1f1 | 649 | OPENSSL_free(tofree); |
6e81b270 AP |
650 | if (ret) |
651 | ERR_print_errors(bio_err); | |
652 | if (randfile) | |
7e1b7485 | 653 | app_RAND_write_file(randfile); |
25aaa98a RS |
654 | NCONF_free(conf); |
655 | free_index(db); | |
6e81b270 | 656 | OBJ_cleanup(); |
7e1b7485 | 657 | return (ret); |
6e81b270 | 658 | } |
edc032b5 | 659 | #endif |