]>
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 | */ | |
58 | #include <openssl/opensslconf.h> | |
59 | ||
60 | #ifndef OPENSSL_NO_SRP | |
6e81b270 AP |
61 | # include <stdio.h> |
62 | # include <stdlib.h> | |
63 | # include <string.h> | |
64 | # include <openssl/conf.h> | |
65 | # include <openssl/bio.h> | |
66 | # include <openssl/err.h> | |
67 | # include <openssl/txt_db.h> | |
68 | # include <openssl/buffer.h> | |
69 | # include <openssl/srp.h> | |
70 | ||
71 | # include "apps.h" | |
72 | ||
6e81b270 AP |
73 | # define BASE_SECTION "srp" |
74 | # define CONFIG_FILE "openssl.cnf" | |
75 | ||
76 | # define ENV_RANDFILE "RANDFILE" | |
77 | ||
78 | # define ENV_DATABASE "srpvfile" | |
79 | # define ENV_DEFAULT_SRP "default_srp" | |
80 | ||
6e81b270 AP |
81 | static int get_index(CA_DB *db, char *id, char type) |
82 | { | |
83 | char **pp; | |
84 | int i; | |
85 | if (id == NULL) | |
86 | return -1; | |
87 | if (type == DB_SRP_INDEX) | |
88 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
89 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
90 | if (pp[DB_srptype][0] == DB_SRP_INDEX | |
86885c28 | 91 | && strcmp(id, pp[DB_srpid]) == 0) |
6e81b270 AP |
92 | return i; |
93 | } else | |
94 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
95 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
96 | ||
97 | if (pp[DB_srptype][0] != DB_SRP_INDEX | |
86885c28 | 98 | && strcmp(id, pp[DB_srpid]) == 0) |
6e81b270 AP |
99 | return i; |
100 | } | |
101 | ||
102 | return -1; | |
103 | } | |
edc032b5 | 104 | |
ecf3a1fb | 105 | static void print_entry(CA_DB *db, int indx, int verbose, char *s) |
6e81b270 AP |
106 | { |
107 | if (indx >= 0 && verbose) { | |
108 | int j; | |
109 | char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx); | |
ecf3a1fb | 110 | BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]); |
6e81b270 AP |
111 | for (j = 0; j < DB_NUMBER; j++) { |
112 | BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]); | |
113 | } | |
114 | } | |
115 | } | |
edc032b5 | 116 | |
ecf3a1fb | 117 | static void print_index(CA_DB *db, int indexindex, int verbose) |
6e81b270 | 118 | { |
ecf3a1fb | 119 | print_entry(db, indexindex, verbose, "g N entry"); |
6e81b270 | 120 | } |
edc032b5 | 121 | |
ecf3a1fb | 122 | static void print_user(CA_DB *db, int userindex, int verbose) |
6e81b270 AP |
123 | { |
124 | if (verbose > 0) { | |
125 | char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
edc032b5 | 126 | |
6e81b270 | 127 | if (pp[DB_srptype][0] != 'I') { |
ecf3a1fb RS |
128 | print_entry(db, userindex, verbose, "User entry"); |
129 | print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose, | |
6e81b270 AP |
130 | "g N entry"); |
131 | } | |
edc032b5 | 132 | |
6e81b270 AP |
133 | } |
134 | } | |
edc032b5 | 135 | |
ecf3a1fb | 136 | static int update_index(CA_DB *db, char **row) |
6e81b270 AP |
137 | { |
138 | char **irow; | |
139 | int i; | |
140 | ||
b4faea50 | 141 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers"); |
6e81b270 AP |
142 | for (i = 0; i < DB_NUMBER; i++) { |
143 | irow[i] = row[i]; | |
144 | row[i] = NULL; | |
145 | } | |
146 | irow[DB_NUMBER] = NULL; | |
147 | ||
148 | if (!TXT_DB_insert(db->db, irow)) { | |
ecf3a1fb RS |
149 | BIO_printf(bio_err, "failed to update srpvfile\n"); |
150 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
6e81b270 AP |
151 | OPENSSL_free(irow); |
152 | return 0; | |
153 | } | |
154 | return 1; | |
155 | } | |
edc032b5 BL |
156 | |
157 | static void lookup_fail(const char *name, const char *tag) | |
6e81b270 AP |
158 | { |
159 | BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag); | |
160 | } | |
edc032b5 BL |
161 | |
162 | static char *srp_verify_user(const char *user, const char *srp_verifier, | |
6e81b270 | 163 | char *srp_usersalt, const char *g, const char *N, |
ecf3a1fb | 164 | const char *passin, int verbose) |
6e81b270 AP |
165 | { |
166 | char password[1024]; | |
167 | PW_CB_DATA cb_tmp; | |
168 | char *verifier = NULL; | |
169 | char *gNid = NULL; | |
170 | ||
171 | cb_tmp.prompt_info = user; | |
172 | cb_tmp.password = passin; | |
173 | ||
174 | if (password_callback(password, 1024, 0, &cb_tmp) > 0) { | |
7e1b7485 | 175 | if (verbose) |
ecf3a1fb | 176 | BIO_printf(bio_err, |
7e1b7485 RS |
177 | "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n", |
178 | user, srp_verifier, srp_usersalt, g, N); | |
ecf3a1fb | 179 | BIO_printf(bio_err, "Pass %s\n", password); |
6e81b270 AP |
180 | |
181 | OPENSSL_assert(srp_usersalt != NULL); | |
7e1b7485 RS |
182 | if (! |
183 | (gNid = | |
184 | SRP_create_verifier(user, password, &srp_usersalt, &verifier, N, | |
185 | g))) { | |
ecf3a1fb | 186 | BIO_printf(bio_err, "Internal error validating SRP verifier\n"); |
6e81b270 AP |
187 | } else { |
188 | if (strcmp(verifier, srp_verifier)) | |
189 | gNid = NULL; | |
190 | OPENSSL_free(verifier); | |
191 | } | |
192 | } | |
193 | return gNid; | |
194 | } | |
edc032b5 | 195 | |
71fa4513 | 196 | static char *srp_create_user(char *user, char **srp_verifier, |
6e81b270 | 197 | char **srp_usersalt, char *g, char *N, |
ecf3a1fb | 198 | char *passout, int verbose) |
6e81b270 AP |
199 | { |
200 | char password[1024]; | |
201 | PW_CB_DATA cb_tmp; | |
202 | char *gNid = NULL; | |
203 | char *salt = NULL; | |
204 | cb_tmp.prompt_info = user; | |
205 | cb_tmp.password = passout; | |
206 | ||
207 | if (password_callback(password, 1024, 1, &cb_tmp) > 0) { | |
7e1b7485 | 208 | if (verbose) |
ecf3a1fb | 209 | BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n", |
7e1b7485 RS |
210 | user, g, N); |
211 | if (! | |
212 | (gNid = | |
213 | SRP_create_verifier(user, password, &salt, srp_verifier, N, | |
214 | g))) { | |
ecf3a1fb | 215 | BIO_printf(bio_err, "Internal error creating SRP verifier\n"); |
6e81b270 AP |
216 | } else |
217 | *srp_usersalt = salt; | |
7e1b7485 | 218 | if (verbose > 1) |
ecf3a1fb | 219 | BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n", gNid, |
7e1b7485 | 220 | salt, *srp_verifier); |
6e81b270 AP |
221 | |
222 | } | |
223 | return gNid; | |
224 | } | |
edc032b5 | 225 | |
7e1b7485 RS |
226 | typedef enum OPTION_choice { |
227 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
228 | OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD, | |
229 | OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO, | |
230 | OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE | |
231 | } OPTION_CHOICE; | |
232 | ||
233 | OPTIONS srp_options[] = { | |
234 | {"help", OPT_HELP, '-', "Display this summary"}, | |
235 | {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"}, | |
236 | {"config", OPT_CONFIG, '<', "A config file"}, | |
237 | {"name", OPT_NAME, 's', "The particular srp definition to use"}, | |
238 | {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"}, | |
239 | {"add", OPT_ADD, '-', "Add a user and srp verifier"}, | |
240 | {"modify", OPT_MODIFY, '-', | |
241 | "Modify the srp verifier of an existing user"}, | |
242 | {"delete", OPT_DELETE, '-', "Delete user from verifier file"}, | |
243 | {"list", OPT_LIST, '-', "List users"}, | |
244 | {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"}, | |
245 | {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"}, | |
246 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
247 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
6e81b270 | 248 | # ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 249 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
6e81b270 | 250 | # endif |
7e1b7485 RS |
251 | {NULL} |
252 | }; | |
253 | ||
254 | int srp_main(int argc, char **argv) | |
255 | { | |
256 | CA_DB *db = NULL; | |
6e81b270 | 257 | DB_ATTR db_attr; |
7e1b7485 RS |
258 | CONF *conf = NULL; |
259 | int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = | |
260 | 0, i, doupdatedb = 0; | |
261 | int mode = OPT_ERR; | |
262 | char *user = NULL, *passinarg = NULL, *passoutarg = NULL; | |
263 | char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL; | |
333b070e | 264 | char *randfile = NULL, *tofree = NULL, *section = NULL; |
7e1b7485 RS |
265 | char **gNrow = NULL, *configfile = NULL, *dbfile = NULL, **pp, *prog; |
266 | long errorline = -1; | |
267 | OPTION_CHOICE o; | |
6e81b270 | 268 | |
7e1b7485 RS |
269 | prog = opt_init(argc, argv, srp_options); |
270 | while ((o = opt_next()) != OPT_EOF) { | |
271 | switch (o) { | |
272 | case OPT_EOF: | |
273 | case OPT_ERR: | |
274 | opthelp: | |
275 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
276 | goto end; | |
277 | case OPT_HELP: | |
278 | opt_help(srp_options); | |
279 | ret = 0; | |
280 | goto end; | |
281 | case OPT_VERBOSE: | |
6e81b270 | 282 | verbose++; |
6e81b270 | 283 | break; |
7e1b7485 RS |
284 | case OPT_CONFIG: |
285 | configfile = opt_arg(); | |
6e81b270 | 286 | break; |
7e1b7485 RS |
287 | case OPT_NAME: |
288 | section = opt_arg(); | |
289 | break; | |
290 | case OPT_SRPVFILE: | |
291 | dbfile = opt_arg(); | |
292 | break; | |
293 | case OPT_ADD: | |
294 | case OPT_DELETE: | |
295 | case OPT_MODIFY: | |
296 | case OPT_LIST: | |
297 | if (mode != OPT_ERR) { | |
298 | BIO_printf(bio_err, | |
299 | "%s: Only one of -add/delete-modify/-list\n", | |
300 | prog); | |
301 | goto opthelp; | |
302 | } | |
303 | mode = o; | |
304 | break; | |
305 | case OPT_GN: | |
306 | gN = opt_arg(); | |
307 | break; | |
308 | case OPT_USERINFO: | |
309 | userinfo = opt_arg(); | |
310 | break; | |
311 | case OPT_PASSIN: | |
312 | passinarg = opt_arg(); | |
313 | break; | |
314 | case OPT_PASSOUT: | |
315 | passoutarg = opt_arg(); | |
316 | break; | |
317 | case OPT_ENGINE: | |
333b070e | 318 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
319 | break; |
320 | } | |
6e81b270 | 321 | } |
7e1b7485 RS |
322 | argc = opt_num_rest(); |
323 | argv = opt_rest(); | |
6e81b270 AP |
324 | |
325 | if (dbfile && configfile) { | |
326 | BIO_printf(bio_err, | |
327 | "-dbfile and -configfile cannot be specified together.\n"); | |
7e1b7485 | 328 | goto end; |
6e81b270 | 329 | } |
7e1b7485 RS |
330 | if (mode == OPT_ERR) { |
331 | BIO_printf(bio_err, | |
332 | "Exactly one of the options -add, -delete, -modify -list must be specified.\n"); | |
333 | goto opthelp; | |
6e81b270 | 334 | } |
5a80d9fb RS |
335 | if ((mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) |
336 | && argc < 1) { | |
7e1b7485 RS |
337 | BIO_printf(bio_err, |
338 | "Need at least one user for options -add, -delete, -modify. \n"); | |
339 | goto opthelp; | |
6e81b270 AP |
340 | } |
341 | if ((passin || passout) && argc != 1) { | |
342 | BIO_printf(bio_err, | |
343 | "-passin, -passout arguments only valid with one user.\n"); | |
7e1b7485 | 344 | goto opthelp; |
6e81b270 | 345 | } |
6e81b270 | 346 | |
7e1b7485 | 347 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
6e81b270 | 348 | BIO_printf(bio_err, "Error getting passwords\n"); |
7e1b7485 | 349 | goto end; |
6e81b270 AP |
350 | } |
351 | ||
352 | if (!dbfile) { | |
353 | ||
354 | /*****************************************************************/ | |
355 | tofree = NULL; | |
356 | if (configfile == NULL) | |
357 | configfile = getenv("OPENSSL_CONF"); | |
358 | if (configfile == NULL) | |
359 | configfile = getenv("SSLEAY_CONF"); | |
360 | if (configfile == NULL) { | |
361 | const char *s = X509_get_default_cert_area(); | |
68dc6824 | 362 | size_t len = strlen(s) + 1 + sizeof(CONFIG_FILE); |
6e81b270 | 363 | |
68dc6824 | 364 | tofree = app_malloc(len, "config filename space"); |
6e81b270 | 365 | # ifdef OPENSSL_SYS_VMS |
6e81b270 AP |
366 | strcpy(tofree, s); |
367 | # else | |
6e81b270 AP |
368 | BUF_strlcpy(tofree, s, len); |
369 | BUF_strlcat(tofree, "/", len); | |
370 | # endif | |
371 | BUF_strlcat(tofree, CONFIG_FILE, len); | |
372 | configfile = tofree; | |
373 | } | |
374 | ||
7e1b7485 RS |
375 | if (verbose) |
376 | BIO_printf(bio_err, "Using configuration from %s\n", configfile); | |
6e81b270 AP |
377 | conf = NCONF_new(NULL); |
378 | if (NCONF_load(conf, configfile, &errorline) <= 0) { | |
379 | if (errorline <= 0) | |
380 | BIO_printf(bio_err, "error loading the config file '%s'\n", | |
381 | configfile); | |
382 | else | |
383 | BIO_printf(bio_err, "error on line %ld of config file '%s'\n", | |
384 | errorline, configfile); | |
7e1b7485 | 385 | goto end; |
6e81b270 | 386 | } |
b548a1f1 RS |
387 | OPENSSL_free(tofree); |
388 | tofree = NULL; | |
6e81b270 | 389 | |
6e81b270 AP |
390 | /* Lets get the config section we are using */ |
391 | if (section == NULL) { | |
7e1b7485 RS |
392 | if (verbose) |
393 | BIO_printf(bio_err, | |
394 | "trying to read " ENV_DEFAULT_SRP | |
395 | " in \" BASE_SECTION \"\n"); | |
6e81b270 AP |
396 | |
397 | section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_SRP); | |
398 | if (section == NULL) { | |
399 | lookup_fail(BASE_SECTION, ENV_DEFAULT_SRP); | |
7e1b7485 | 400 | goto end; |
6e81b270 AP |
401 | } |
402 | } | |
403 | ||
404 | if (randfile == NULL && conf) | |
405 | randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE"); | |
406 | ||
7e1b7485 RS |
407 | if (verbose) |
408 | BIO_printf(bio_err, | |
409 | "trying to read " ENV_DATABASE " in section \"%s\"\n", | |
410 | section); | |
6e81b270 AP |
411 | |
412 | if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) { | |
413 | lookup_fail(section, ENV_DATABASE); | |
7e1b7485 | 414 | goto end; |
6e81b270 AP |
415 | } |
416 | ||
417 | } | |
418 | if (randfile == NULL) | |
419 | ERR_clear_error(); | |
420 | else | |
7e1b7485 | 421 | app_RAND_load_file(randfile, 0); |
6e81b270 | 422 | |
7e1b7485 RS |
423 | if (verbose) |
424 | BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n", | |
425 | dbfile); | |
6e81b270 AP |
426 | |
427 | db = load_index(dbfile, &db_attr); | |
428 | if (db == NULL) | |
7e1b7485 | 429 | goto end; |
6e81b270 AP |
430 | |
431 | /* Lets check some fields */ | |
432 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
433 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
434 | ||
435 | if (pp[DB_srptype][0] == DB_SRP_INDEX) { | |
436 | maxgN = i; | |
86885c28 | 437 | if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0) |
6e81b270 AP |
438 | gNindex = i; |
439 | ||
ecf3a1fb | 440 | print_index(db, i, verbose > 1); |
6e81b270 AP |
441 | } |
442 | } | |
443 | ||
7e1b7485 RS |
444 | if (verbose) |
445 | BIO_printf(bio_err, "Database initialised\n"); | |
6e81b270 AP |
446 | |
447 | if (gNindex >= 0) { | |
448 | gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex); | |
ecf3a1fb | 449 | print_entry(db, gNindex, verbose > 1, "Default g and N"); |
6e81b270 AP |
450 | } else if (maxgN > 0 && !SRP_get_default_gN(gN)) { |
451 | BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN); | |
7e1b7485 | 452 | goto end; |
6e81b270 | 453 | } else { |
7e1b7485 RS |
454 | if (verbose) |
455 | BIO_printf(bio_err, "Database has no g N information.\n"); | |
6e81b270 AP |
456 | gNrow = NULL; |
457 | } | |
458 | ||
7e1b7485 RS |
459 | if (verbose > 1) |
460 | BIO_printf(bio_err, "Starting user processing\n"); | |
6e81b270 AP |
461 | |
462 | if (argc > 0) | |
463 | user = *(argv++); | |
464 | ||
7e1b7485 | 465 | while (mode == OPT_LIST || user) { |
6e81b270 AP |
466 | int userindex = -1; |
467 | if (user) | |
7e1b7485 RS |
468 | if (verbose > 1) |
469 | BIO_printf(bio_err, "Processing user \"%s\"\n", user); | |
6e81b270 | 470 | if ((userindex = get_index(db, user, 'U')) >= 0) { |
ecf3a1fb | 471 | print_user(db, userindex, (verbose > 0) |
7e1b7485 | 472 | || mode == OPT_LIST); |
6e81b270 AP |
473 | } |
474 | ||
7e1b7485 | 475 | if (mode == OPT_LIST) { |
6e81b270 AP |
476 | if (user == NULL) { |
477 | BIO_printf(bio_err, "List all users\n"); | |
478 | ||
479 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
ecf3a1fb | 480 | print_user(db, i, 1); |
6e81b270 | 481 | } |
6e81b270 AP |
482 | } else if (userindex < 0) { |
483 | BIO_printf(bio_err, | |
484 | "user \"%s\" does not exist, ignored. t\n", user); | |
485 | errors++; | |
486 | } | |
7e1b7485 | 487 | } else if (mode == OPT_ADD) { |
6e81b270 AP |
488 | if (userindex >= 0) { |
489 | /* reactivation of a new user */ | |
490 | char **row = | |
491 | sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
492 | BIO_printf(bio_err, "user \"%s\" reactivated.\n", user); | |
493 | row[DB_srptype][0] = 'V'; | |
494 | ||
495 | doupdatedb = 1; | |
496 | } else { | |
497 | char *row[DB_NUMBER]; | |
498 | char *gNid; | |
499 | row[DB_srpverifier] = NULL; | |
500 | row[DB_srpsalt] = NULL; | |
501 | row[DB_srpinfo] = NULL; | |
7e1b7485 RS |
502 | if (! |
503 | (gNid = | |
504 | srp_create_user(user, &(row[DB_srpverifier]), | |
505 | &(row[DB_srpsalt]), | |
506 | gNrow ? gNrow[DB_srpsalt] : gN, | |
507 | gNrow ? gNrow[DB_srpverifier] : NULL, | |
ecf3a1fb | 508 | passout, verbose))) { |
6e81b270 | 509 | BIO_printf(bio_err, |
7e1b7485 RS |
510 | "Cannot create srp verifier for user \"%s\", operation abandoned .\n", |
511 | user); | |
6e81b270 | 512 | errors++; |
7e1b7485 | 513 | goto end; |
6e81b270 AP |
514 | } |
515 | row[DB_srpid] = BUF_strdup(user); | |
516 | row[DB_srptype] = BUF_strdup("v"); | |
517 | row[DB_srpgN] = BUF_strdup(gNid); | |
518 | ||
519 | if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype] | |
b548a1f1 RS |
520 | || !row[DB_srpverifier] || !row[DB_srpsalt] |
521 | || (userinfo && | |
522 | (!(row [DB_srpinfo] = BUF_strdup (userinfo)))) | |
ecf3a1fb | 523 | || !update_index(db, row)) { |
b548a1f1 RS |
524 | OPENSSL_free(row[DB_srpid]); |
525 | OPENSSL_free(row[DB_srpgN]); | |
526 | OPENSSL_free(row[DB_srpinfo]); | |
527 | OPENSSL_free(row[DB_srptype]); | |
528 | OPENSSL_free(row[DB_srpverifier]); | |
529 | OPENSSL_free(row[DB_srpsalt]); | |
7e1b7485 | 530 | goto end; |
6e81b270 AP |
531 | } |
532 | doupdatedb = 1; | |
533 | } | |
7e1b7485 | 534 | } else if (mode == OPT_MODIFY) { |
6e81b270 AP |
535 | if (userindex < 0) { |
536 | BIO_printf(bio_err, | |
537 | "user \"%s\" does not exist, operation ignored.\n", | |
538 | user); | |
539 | errors++; | |
540 | } else { | |
541 | ||
542 | char **row = | |
543 | sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
544 | char type = row[DB_srptype][0]; | |
545 | if (type == 'v') { | |
546 | BIO_printf(bio_err, | |
547 | "user \"%s\" already updated, operation ignored.\n", | |
548 | user); | |
549 | errors++; | |
550 | } else { | |
551 | char *gNid; | |
552 | ||
553 | if (row[DB_srptype][0] == 'V') { | |
554 | int user_gN; | |
555 | char **irow = NULL; | |
7e1b7485 RS |
556 | if (verbose) |
557 | BIO_printf(bio_err, | |
558 | "Verifying password for user \"%s\"\n", | |
559 | user); | |
6e81b270 AP |
560 | if ((user_gN = |
561 | get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0) | |
562 | irow = | |
563 | sk_OPENSSL_PSTRING_value(db->db->data, | |
564 | userindex); | |
565 | ||
566 | if (!srp_verify_user | |
567 | (user, row[DB_srpverifier], row[DB_srpsalt], | |
568 | irow ? irow[DB_srpsalt] : row[DB_srpgN], | |
569 | irow ? irow[DB_srpverifier] : NULL, passin, | |
ecf3a1fb | 570 | verbose)) { |
6e81b270 AP |
571 | BIO_printf(bio_err, |
572 | "Invalid password for user \"%s\", operation abandoned.\n", | |
573 | user); | |
574 | errors++; | |
7e1b7485 | 575 | goto end; |
6e81b270 AP |
576 | } |
577 | } | |
7e1b7485 RS |
578 | if (verbose) |
579 | BIO_printf(bio_err, "Password for user \"%s\" ok.\n", | |
580 | user); | |
581 | ||
582 | if (! | |
583 | (gNid = | |
584 | srp_create_user(user, &(row[DB_srpverifier]), | |
585 | &(row[DB_srpsalt]), | |
586 | gNrow ? gNrow[DB_srpsalt] : NULL, | |
587 | gNrow ? gNrow[DB_srpverifier] : NULL, | |
ecf3a1fb | 588 | passout, verbose))) { |
6e81b270 | 589 | BIO_printf(bio_err, |
7e1b7485 RS |
590 | "Cannot create srp verifier for user \"%s\", operation abandoned.\n", |
591 | user); | |
6e81b270 | 592 | errors++; |
7e1b7485 | 593 | goto end; |
6e81b270 AP |
594 | } |
595 | ||
596 | row[DB_srptype][0] = 'v'; | |
597 | row[DB_srpgN] = BUF_strdup(gNid); | |
598 | ||
599 | if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype] | |
600 | || !row[DB_srpverifier] || !row[DB_srpsalt] | |
601 | || (userinfo | |
602 | && (!(row[DB_srpinfo] = BUF_strdup(userinfo))))) | |
7e1b7485 | 603 | goto end; |
6e81b270 AP |
604 | |
605 | doupdatedb = 1; | |
606 | } | |
607 | } | |
7e1b7485 | 608 | } else if (mode == OPT_DELETE) { |
6e81b270 AP |
609 | if (userindex < 0) { |
610 | BIO_printf(bio_err, | |
611 | "user \"%s\" does not exist, operation ignored. t\n", | |
612 | user); | |
613 | errors++; | |
614 | } else { | |
615 | char **xpp = | |
616 | sk_OPENSSL_PSTRING_value(db->db->data, userindex); | |
617 | BIO_printf(bio_err, "user \"%s\" revoked. t\n", user); | |
618 | ||
619 | xpp[DB_srptype][0] = 'R'; | |
620 | ||
621 | doupdatedb = 1; | |
622 | } | |
623 | } | |
624 | if (--argc > 0) | |
625 | user = *(argv++); | |
626 | else { | |
627 | user = NULL; | |
6e81b270 AP |
628 | } |
629 | } | |
630 | ||
7e1b7485 RS |
631 | if (verbose) |
632 | BIO_printf(bio_err, "User procession done.\n"); | |
6e81b270 AP |
633 | |
634 | if (doupdatedb) { | |
635 | /* Lets check some fields */ | |
636 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
637 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
638 | ||
639 | if (pp[DB_srptype][0] == 'v') { | |
640 | pp[DB_srptype][0] = 'V'; | |
ecf3a1fb | 641 | print_user(db, i, verbose); |
6e81b270 AP |
642 | } |
643 | } | |
644 | ||
7e1b7485 RS |
645 | if (verbose) |
646 | BIO_printf(bio_err, "Trying to update srpvfile.\n"); | |
6e81b270 | 647 | if (!save_index(dbfile, "new", db)) |
7e1b7485 | 648 | goto end; |
6e81b270 | 649 | |
7e1b7485 RS |
650 | if (verbose) |
651 | BIO_printf(bio_err, "Temporary srpvfile created.\n"); | |
6e81b270 | 652 | if (!rotate_index(dbfile, "new", "old")) |
7e1b7485 | 653 | goto end; |
6e81b270 | 654 | |
7e1b7485 RS |
655 | if (verbose) |
656 | BIO_printf(bio_err, "srpvfile updated.\n"); | |
6e81b270 AP |
657 | } |
658 | ||
659 | ret = (errors != 0); | |
7e1b7485 | 660 | end: |
6e81b270 | 661 | if (errors != 0) |
7e1b7485 RS |
662 | if (verbose) |
663 | BIO_printf(bio_err, "User errors %d.\n", errors); | |
6e81b270 | 664 | |
7e1b7485 RS |
665 | if (verbose) |
666 | BIO_printf(bio_err, "SRP terminating with code %d.\n", ret); | |
b548a1f1 | 667 | OPENSSL_free(tofree); |
6e81b270 AP |
668 | if (ret) |
669 | ERR_print_errors(bio_err); | |
670 | if (randfile) | |
7e1b7485 | 671 | app_RAND_write_file(randfile); |
25aaa98a RS |
672 | NCONF_free(conf); |
673 | free_index(db); | |
6e81b270 | 674 | OBJ_cleanup(); |
7e1b7485 | 675 | return (ret); |
6e81b270 | 676 | } |
edc032b5 BL |
677 | |
678 | #endif |