]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/srp.c
Update copyright year
[thirdparty/openssl.git] / apps / srp.c
CommitLineData
0f113f3e 1/*
4333b89f 2 * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
3b855b1f 3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
edc032b5 4 *
dffa7520 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
3b855b1f
TH
9 *
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
edc032b5 12 */
effaf4de 13
edc032b5
BL
14#include <openssl/opensslconf.h>
15
1ae56f2f
RS
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <openssl/conf.h>
20#include <openssl/bio.h>
21#include <openssl/err.h>
22#include <openssl/txt_db.h>
23#include <openssl/buffer.h>
24#include <openssl/srp.h>
25#include "apps.h"
26#include "progs.h"
6e81b270 27
1ae56f2f
RS
28#define BASE_SECTION "srp"
29#define CONFIG_FILE "openssl.cnf"
6e81b270 30
6e81b270 31
1ae56f2f
RS
32#define ENV_DATABASE "srpvfile"
33#define ENV_DEFAULT_SRP "default_srp"
6e81b270 34
6e81b270
AP
35static int get_index(CA_DB *db, char *id, char type)
36{
37 char **pp;
38 int i;
39 if (id == NULL)
40 return -1;
5ec3210f 41 if (type == DB_SRP_INDEX) {
6e81b270
AP
42 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
43 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
44 if (pp[DB_srptype][0] == DB_SRP_INDEX
86885c28 45 && strcmp(id, pp[DB_srpid]) == 0)
6e81b270 46 return i;
5ec3210f
DSC
47 }
48 } else {
6e81b270
AP
49 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
50 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
51
52 if (pp[DB_srptype][0] != DB_SRP_INDEX
86885c28 53 && strcmp(id, pp[DB_srpid]) == 0)
6e81b270
AP
54 return i;
55 }
5ec3210f 56 }
6e81b270
AP
57
58 return -1;
59}
edc032b5 60
ecf3a1fb 61static void print_entry(CA_DB *db, int indx, int verbose, char *s)
6e81b270
AP
62{
63 if (indx >= 0 && verbose) {
64 int j;
65 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, indx);
ecf3a1fb 66 BIO_printf(bio_err, "%s \"%s\"\n", s, pp[DB_srpid]);
6e81b270
AP
67 for (j = 0; j < DB_NUMBER; j++) {
68 BIO_printf(bio_err, " %d = \"%s\"\n", j, pp[j]);
69 }
70 }
71}
edc032b5 72
ecf3a1fb 73static void print_index(CA_DB *db, int indexindex, int verbose)
6e81b270 74{
ecf3a1fb 75 print_entry(db, indexindex, verbose, "g N entry");
6e81b270 76}
edc032b5 77
ecf3a1fb 78static void print_user(CA_DB *db, int userindex, int verbose)
6e81b270
AP
79{
80 if (verbose > 0) {
81 char **pp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
edc032b5 82
6e81b270 83 if (pp[DB_srptype][0] != 'I') {
ecf3a1fb
RS
84 print_entry(db, userindex, verbose, "User entry");
85 print_entry(db, get_index(db, pp[DB_srpgN], 'I'), verbose,
6e81b270
AP
86 "g N entry");
87 }
edc032b5 88
6e81b270
AP
89 }
90}
edc032b5 91
ecf3a1fb 92static int update_index(CA_DB *db, char **row)
6e81b270
AP
93{
94 char **irow;
95 int i;
96
b4faea50 97 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
0fbaef9e 98 for (i = 0; i < DB_NUMBER; i++)
6e81b270 99 irow[i] = row[i];
6e81b270
AP
100 irow[DB_NUMBER] = NULL;
101
102 if (!TXT_DB_insert(db->db, irow)) {
ecf3a1fb
RS
103 BIO_printf(bio_err, "failed to update srpvfile\n");
104 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
6e81b270
AP
105 OPENSSL_free(irow);
106 return 0;
107 }
108 return 1;
109}
edc032b5 110
edbff8da 111static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
6e81b270 112{
edbff8da
F
113 char *entry = NCONF_get_string(conf, section, tag);
114 if (entry == NULL)
115 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
116 return entry;
6e81b270 117}
edc032b5
BL
118
119static char *srp_verify_user(const char *user, const char *srp_verifier,
6e81b270 120 char *srp_usersalt, const char *g, const char *N,
ecf3a1fb 121 const char *passin, int verbose)
6e81b270 122{
0e83981d 123 char password[1025];
6e81b270
AP
124 PW_CB_DATA cb_tmp;
125 char *verifier = NULL;
126 char *gNid = NULL;
0e83981d 127 int len;
6e81b270
AP
128
129 cb_tmp.prompt_info = user;
130 cb_tmp.password = passin;
131
0e83981d
DSC
132 len = password_callback(password, sizeof(password)-1, 0, &cb_tmp);
133 if (len > 0) {
134 password[len] = 0;
7e1b7485 135 if (verbose)
ecf3a1fb 136 BIO_printf(bio_err,
7e1b7485
RS
137 "Validating\n user=\"%s\"\n srp_verifier=\"%s\"\n srp_usersalt=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
138 user, srp_verifier, srp_usersalt, g, N);
0e83981d
DSC
139 if (verbose > 1)
140 BIO_printf(bio_err, "Pass %s\n", password);
6e81b270
AP
141
142 OPENSSL_assert(srp_usersalt != NULL);
2234212c
PY
143 if ((gNid = SRP_create_verifier(user, password, &srp_usersalt,
144 &verifier, N, g)) == NULL) {
ecf3a1fb 145 BIO_printf(bio_err, "Internal error validating SRP verifier\n");
6e81b270
AP
146 } else {
147 if (strcmp(verifier, srp_verifier))
148 gNid = NULL;
149 OPENSSL_free(verifier);
150 }
0e83981d 151 OPENSSL_cleanse(password, len);
6e81b270
AP
152 }
153 return gNid;
154}
edc032b5 155
71fa4513 156static char *srp_create_user(char *user, char **srp_verifier,
6e81b270 157 char **srp_usersalt, char *g, char *N,
ecf3a1fb 158 char *passout, int verbose)
6e81b270 159{
0e83981d 160 char password[1025];
6e81b270
AP
161 PW_CB_DATA cb_tmp;
162 char *gNid = NULL;
163 char *salt = NULL;
0e83981d 164 int len;
6e81b270
AP
165 cb_tmp.prompt_info = user;
166 cb_tmp.password = passout;
167
0e83981d
DSC
168 len = password_callback(password, sizeof(password)-1, 1, &cb_tmp);
169 if (len > 0) {
170 password[len] = 0;
7e1b7485 171 if (verbose)
ecf3a1fb 172 BIO_printf(bio_err, "Creating\n user=\"%s\"\n g=\"%s\"\n N=\"%s\"\n",
7e1b7485 173 user, g, N);
2234212c
PY
174 if ((gNid = SRP_create_verifier(user, password, &salt,
175 srp_verifier, N, g)) == NULL) {
ecf3a1fb 176 BIO_printf(bio_err, "Internal error creating SRP verifier\n");
0e83981d 177 } else {
6e81b270 178 *srp_usersalt = salt;
0e83981d
DSC
179 }
180 OPENSSL_cleanse(password, len);
7e1b7485 181 if (verbose > 1)
0e83981d
DSC
182 BIO_printf(bio_err, "gNid=%s salt =\"%s\"\n verifier =\"%s\"\n",
183 gNid, salt, *srp_verifier);
6e81b270
AP
184
185 }
186 return gNid;
187}
edc032b5 188
7e1b7485
RS
189typedef enum OPTION_choice {
190 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
191 OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SRPVFILE, OPT_ADD,
192 OPT_DELETE, OPT_MODIFY, OPT_LIST, OPT_GN, OPT_USERINFO,
6bd4e3f2 193 OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, OPT_R_ENUM, OPT_PROV_ENUM
7e1b7485
RS
194} OPTION_CHOICE;
195
44c83ebd 196const OPTIONS srp_options[] = {
92de469f
RS
197 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [user...]\n"},
198
5388f986 199 OPT_SECTION("General"),
7e1b7485
RS
200 {"help", OPT_HELP, '-', "Display this summary"},
201 {"verbose", OPT_VERBOSE, '-', "Talk a lot while doing things"},
202 {"config", OPT_CONFIG, '<', "A config file"},
203 {"name", OPT_NAME, 's', "The particular srp definition to use"},
1ae56f2f 204#ifndef OPENSSL_NO_ENGINE
5388f986 205 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
1ae56f2f 206#endif
5388f986
RS
207
208 OPT_SECTION("Action"),
7e1b7485 209 {"add", OPT_ADD, '-', "Add a user and srp verifier"},
5388f986 210 {"modify", OPT_MODIFY, '-', "Modify the srp verifier of an existing user"},
7e1b7485
RS
211 {"delete", OPT_DELETE, '-', "Delete user from verifier file"},
212 {"list", OPT_LIST, '-', "List users"},
5388f986
RS
213
214 OPT_SECTION("Configuration"),
215 {"srpvfile", OPT_SRPVFILE, '<', "The srp verifier file name"},
7e1b7485
RS
216 {"gn", OPT_GN, 's', "Set g and N values to be used for new verifier"},
217 {"userinfo", OPT_USERINFO, 's', "Additional info to be set for user"},
218 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
219 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
5388f986 220
3ee1eac2 221 OPT_R_OPTIONS,
6bd4e3f2 222 OPT_PROV_OPTIONS,
92de469f
RS
223
224 OPT_PARAMETERS(),
225 {"user", 0, 0, "Username(s) to process (optional)"},
7e1b7485
RS
226 {NULL}
227};
228
229int srp_main(int argc, char **argv)
230{
dd1abd44 231 ENGINE *e = NULL;
7e1b7485 232 CA_DB *db = NULL;
7e1b7485 233 CONF *conf = NULL;
cc01d217
RS
234 int gNindex = -1, maxgN = -1, ret = 1, errors = 0, verbose = 0, i;
235 int doupdatedb = 0, mode = OPT_ERR;
7e1b7485
RS
236 char *user = NULL, *passinarg = NULL, *passoutarg = NULL;
237 char *passin = NULL, *passout = NULL, *gN = NULL, *userinfo = NULL;
3ee1eac2 238 char *section = NULL;
dfd6211c
MC
239 char **gNrow = NULL, *configfile = NULL;
240 char *srpvfile = NULL, **pp, *prog;
7e1b7485 241 OPTION_CHOICE o;
6e81b270 242
7e1b7485
RS
243 prog = opt_init(argc, argv, srp_options);
244 while ((o = opt_next()) != OPT_EOF) {
245 switch (o) {
246 case OPT_EOF:
247 case OPT_ERR:
248 opthelp:
249 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
250 goto end;
251 case OPT_HELP:
252 opt_help(srp_options);
253 ret = 0;
254 goto end;
255 case OPT_VERBOSE:
6e81b270 256 verbose++;
6e81b270 257 break;
7e1b7485
RS
258 case OPT_CONFIG:
259 configfile = opt_arg();
6e81b270 260 break;
7e1b7485
RS
261 case OPT_NAME:
262 section = opt_arg();
263 break;
264 case OPT_SRPVFILE:
dfd6211c 265 srpvfile = opt_arg();
7e1b7485
RS
266 break;
267 case OPT_ADD:
268 case OPT_DELETE:
269 case OPT_MODIFY:
270 case OPT_LIST:
271 if (mode != OPT_ERR) {
272 BIO_printf(bio_err,
0e83981d 273 "%s: Only one of -add/-delete/-modify/-list\n",
7e1b7485
RS
274 prog);
275 goto opthelp;
276 }
277 mode = o;
278 break;
279 case OPT_GN:
280 gN = opt_arg();
281 break;
282 case OPT_USERINFO:
283 userinfo = opt_arg();
284 break;
285 case OPT_PASSIN:
286 passinarg = opt_arg();
287 break;
288 case OPT_PASSOUT:
289 passoutarg = opt_arg();
290 break;
291 case OPT_ENGINE:
dd1abd44 292 e = setup_engine(opt_arg(), 0);
7e1b7485 293 break;
3ee1eac2
RS
294 case OPT_R_CASES:
295 if (!opt_rand(o))
296 goto end;
297 break;
6bd4e3f2
P
298 case OPT_PROV_CASES:
299 if (!opt_provider(o))
300 goto end;
301 break;
7e1b7485 302 }
6e81b270 303 }
021410ea
RS
304
305 /* Optional parameters are usernames. */
7e1b7485
RS
306 argc = opt_num_rest();
307 argv = opt_rest();
6e81b270 308
2234212c 309 if (srpvfile != NULL && configfile != NULL) {
6e81b270 310 BIO_printf(bio_err,
dfd6211c 311 "-srpvfile and -configfile cannot be specified together.\n");
7e1b7485 312 goto end;
6e81b270 313 }
7e1b7485
RS
314 if (mode == OPT_ERR) {
315 BIO_printf(bio_err,
316 "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
317 goto opthelp;
6e81b270 318 }
c27363f5
RS
319 if (mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD) {
320 if (argc == 0) {
321 BIO_printf(bio_err, "Need at least one user.\n");
322 goto opthelp;
323 }
324 user = *argv++;
6e81b270 325 }
2234212c 326 if ((passinarg != NULL || passoutarg != NULL) && argc != 1) {
6e81b270
AP
327 BIO_printf(bio_err,
328 "-passin, -passout arguments only valid with one user.\n");
7e1b7485 329 goto opthelp;
6e81b270 330 }
6e81b270 331
7e1b7485 332 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
6e81b270 333 BIO_printf(bio_err, "Error getting passwords\n");
7e1b7485 334 goto end;
6e81b270
AP
335 }
336
2234212c
PY
337 if (srpvfile == NULL) {
338 if (configfile == NULL)
dfd6211c
MC
339 configfile = default_config_file;
340
15795943 341 conf = app_load_config_verbose(configfile, verbose);
cc01d217 342 if (conf == NULL)
7e1b7485 343 goto end;
c821defc 344 if (configfile != default_config_file && !app_load_modules(conf))
296f54ee 345 goto end;
6e81b270 346
6e81b270
AP
347 /* Lets get the config section we are using */
348 if (section == NULL) {
7e1b7485
RS
349 if (verbose)
350 BIO_printf(bio_err,
351 "trying to read " ENV_DEFAULT_SRP
44fdf1c2 352 " in " BASE_SECTION "\n");
6e81b270 353
edbff8da
F
354 section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_SRP);
355 if (section == NULL)
7e1b7485 356 goto end;
6e81b270
AP
357 }
358
3ee1eac2 359 app_RAND_load_conf(conf, BASE_SECTION);
6e81b270 360
7e1b7485
RS
361 if (verbose)
362 BIO_printf(bio_err,
363 "trying to read " ENV_DATABASE " in section \"%s\"\n",
364 section);
6e81b270 365
edbff8da
F
366 srpvfile = lookup_conf(conf, section, ENV_DATABASE);
367 if (srpvfile == NULL)
7e1b7485 368 goto end;
6e81b270 369 }
6e81b270 370
7e1b7485
RS
371 if (verbose)
372 BIO_printf(bio_err, "Trying to read SRP verifier file \"%s\"\n",
dfd6211c 373 srpvfile);
6e81b270 374
9d7ec809 375 db = load_index(srpvfile, NULL);
6e81b270 376 if (db == NULL)
7e1b7485 377 goto end;
6e81b270
AP
378
379 /* Lets check some fields */
380 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
381 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
382
383 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
384 maxgN = i;
86885c28 385 if ((gNindex < 0) && (gN != NULL) && strcmp(gN, pp[DB_srpid]) == 0)
6e81b270
AP
386 gNindex = i;
387
ecf3a1fb 388 print_index(db, i, verbose > 1);
6e81b270
AP
389 }
390 }
391
7e1b7485
RS
392 if (verbose)
393 BIO_printf(bio_err, "Database initialised\n");
6e81b270
AP
394
395 if (gNindex >= 0) {
396 gNrow = sk_OPENSSL_PSTRING_value(db->db->data, gNindex);
ecf3a1fb 397 print_entry(db, gNindex, verbose > 1, "Default g and N");
6e81b270
AP
398 } else if (maxgN > 0 && !SRP_get_default_gN(gN)) {
399 BIO_printf(bio_err, "No g and N value for index \"%s\"\n", gN);
7e1b7485 400 goto end;
6e81b270 401 } else {
7e1b7485
RS
402 if (verbose)
403 BIO_printf(bio_err, "Database has no g N information.\n");
6e81b270
AP
404 gNrow = NULL;
405 }
406
7e1b7485
RS
407 if (verbose > 1)
408 BIO_printf(bio_err, "Starting user processing\n");
6e81b270 409
c27363f5 410 while (mode == OPT_LIST || user != NULL) {
6e81b270 411 int userindex = -1;
edbff8da
F
412
413 if (user != NULL && verbose > 1)
414 BIO_printf(bio_err, "Processing user \"%s\"\n", user);
2234212c 415 if ((userindex = get_index(db, user, 'U')) >= 0)
edbff8da 416 print_user(db, userindex, (verbose > 0) || mode == OPT_LIST);
6e81b270 417
7e1b7485 418 if (mode == OPT_LIST) {
6e81b270
AP
419 if (user == NULL) {
420 BIO_printf(bio_err, "List all users\n");
421
2234212c 422 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++)
ecf3a1fb 423 print_user(db, i, 1);
6e81b270
AP
424 } else if (userindex < 0) {
425 BIO_printf(bio_err,
426 "user \"%s\" does not exist, ignored. t\n", user);
427 errors++;
428 }
7e1b7485 429 } else if (mode == OPT_ADD) {
6e81b270
AP
430 if (userindex >= 0) {
431 /* reactivation of a new user */
432 char **row =
433 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
434 BIO_printf(bio_err, "user \"%s\" reactivated.\n", user);
435 row[DB_srptype][0] = 'V';
436
437 doupdatedb = 1;
438 } else {
439 char *row[DB_NUMBER];
440 char *gNid;
441 row[DB_srpverifier] = NULL;
442 row[DB_srpsalt] = NULL;
443 row[DB_srpinfo] = NULL;
7e1b7485
RS
444 if (!
445 (gNid =
446 srp_create_user(user, &(row[DB_srpverifier]),
447 &(row[DB_srpsalt]),
448 gNrow ? gNrow[DB_srpsalt] : gN,
449 gNrow ? gNrow[DB_srpverifier] : NULL,
ecf3a1fb 450 passout, verbose))) {
6e81b270 451 BIO_printf(bio_err,
7e1b7485
RS
452 "Cannot create srp verifier for user \"%s\", operation abandoned .\n",
453 user);
6e81b270 454 errors++;
7e1b7485 455 goto end;
6e81b270 456 }
7644a9ae
RS
457 row[DB_srpid] = OPENSSL_strdup(user);
458 row[DB_srptype] = OPENSSL_strdup("v");
459 row[DB_srpgN] = OPENSSL_strdup(gNid);
6e81b270 460
75ebbd9a
RS
461 if ((row[DB_srpid] == NULL)
462 || (row[DB_srpgN] == NULL)
463 || (row[DB_srptype] == NULL)
464 || (row[DB_srpverifier] == NULL)
465 || (row[DB_srpsalt] == NULL)
466 || (userinfo
7644a9ae 467 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo)) == NULL))
ecf3a1fb 468 || !update_index(db, row)) {
b548a1f1
RS
469 OPENSSL_free(row[DB_srpid]);
470 OPENSSL_free(row[DB_srpgN]);
471 OPENSSL_free(row[DB_srpinfo]);
472 OPENSSL_free(row[DB_srptype]);
473 OPENSSL_free(row[DB_srpverifier]);
474 OPENSSL_free(row[DB_srpsalt]);
7e1b7485 475 goto end;
6e81b270
AP
476 }
477 doupdatedb = 1;
478 }
7e1b7485 479 } else if (mode == OPT_MODIFY) {
6e81b270
AP
480 if (userindex < 0) {
481 BIO_printf(bio_err,
482 "user \"%s\" does not exist, operation ignored.\n",
483 user);
484 errors++;
485 } else {
486
487 char **row =
488 sk_OPENSSL_PSTRING_value(db->db->data, userindex);
489 char type = row[DB_srptype][0];
490 if (type == 'v') {
491 BIO_printf(bio_err,
492 "user \"%s\" already updated, operation ignored.\n",
493 user);
494 errors++;
495 } else {
496 char *gNid;
497
498 if (row[DB_srptype][0] == 'V') {
499 int user_gN;
500 char **irow = NULL;
7e1b7485
RS
501 if (verbose)
502 BIO_printf(bio_err,
503 "Verifying password for user \"%s\"\n",
504 user);
6e81b270
AP
505 if ((user_gN =
506 get_index(db, row[DB_srpgN], DB_SRP_INDEX)) >= 0)
507 irow =
508 sk_OPENSSL_PSTRING_value(db->db->data,
509 userindex);
510
511 if (!srp_verify_user
512 (user, row[DB_srpverifier], row[DB_srpsalt],
513 irow ? irow[DB_srpsalt] : row[DB_srpgN],
514 irow ? irow[DB_srpverifier] : NULL, passin,
ecf3a1fb 515 verbose)) {
6e81b270
AP
516 BIO_printf(bio_err,
517 "Invalid password for user \"%s\", operation abandoned.\n",
518 user);
519 errors++;
7e1b7485 520 goto end;
6e81b270
AP
521 }
522 }
7e1b7485
RS
523 if (verbose)
524 BIO_printf(bio_err, "Password for user \"%s\" ok.\n",
525 user);
526
527 if (!
528 (gNid =
529 srp_create_user(user, &(row[DB_srpverifier]),
530 &(row[DB_srpsalt]),
531 gNrow ? gNrow[DB_srpsalt] : NULL,
532 gNrow ? gNrow[DB_srpverifier] : NULL,
ecf3a1fb 533 passout, verbose))) {
6e81b270 534 BIO_printf(bio_err,
7e1b7485
RS
535 "Cannot create srp verifier for user \"%s\", operation abandoned.\n",
536 user);
6e81b270 537 errors++;
7e1b7485 538 goto end;
6e81b270
AP
539 }
540
541 row[DB_srptype][0] = 'v';
7644a9ae 542 row[DB_srpgN] = OPENSSL_strdup(gNid);
6e81b270 543
75ebbd9a
RS
544 if (row[DB_srpid] == NULL
545 || row[DB_srpgN] == NULL
546 || row[DB_srptype] == NULL
547 || row[DB_srpverifier] == NULL
548 || row[DB_srpsalt] == NULL
6e81b270 549 || (userinfo
7644a9ae 550 && ((row[DB_srpinfo] = OPENSSL_strdup(userinfo))
75ebbd9a 551 == NULL)))
7e1b7485 552 goto end;
6e81b270
AP
553
554 doupdatedb = 1;
555 }
556 }
7e1b7485 557 } else if (mode == OPT_DELETE) {
6e81b270
AP
558 if (userindex < 0) {
559 BIO_printf(bio_err,
560 "user \"%s\" does not exist, operation ignored. t\n",
561 user);
562 errors++;
563 } else {
75ebbd9a 564 char **xpp = sk_OPENSSL_PSTRING_value(db->db->data, userindex);
6e81b270 565
75ebbd9a 566 BIO_printf(bio_err, "user \"%s\" revoked. t\n", user);
6e81b270 567 xpp[DB_srptype][0] = 'R';
6e81b270
AP
568 doupdatedb = 1;
569 }
570 }
c27363f5
RS
571 user = *argv++;
572 if (user == NULL) {
5ec3210f
DSC
573 /* no more processing in any mode if no users left */
574 break;
6e81b270
AP
575 }
576 }
577
7e1b7485
RS
578 if (verbose)
579 BIO_printf(bio_err, "User procession done.\n");
6e81b270
AP
580
581 if (doupdatedb) {
582 /* Lets check some fields */
583 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
584 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
585
586 if (pp[DB_srptype][0] == 'v') {
587 pp[DB_srptype][0] = 'V';
ecf3a1fb 588 print_user(db, i, verbose);
6e81b270
AP
589 }
590 }
591
7e1b7485
RS
592 if (verbose)
593 BIO_printf(bio_err, "Trying to update srpvfile.\n");
dfd6211c 594 if (!save_index(srpvfile, "new", db))
7e1b7485 595 goto end;
6e81b270 596
7e1b7485
RS
597 if (verbose)
598 BIO_printf(bio_err, "Temporary srpvfile created.\n");
dfd6211c 599 if (!rotate_index(srpvfile, "new", "old"))
7e1b7485 600 goto end;
6e81b270 601
7e1b7485
RS
602 if (verbose)
603 BIO_printf(bio_err, "srpvfile updated.\n");
6e81b270
AP
604 }
605
606 ret = (errors != 0);
7e1b7485 607 end:
6e81b270 608 if (errors != 0)
7e1b7485
RS
609 if (verbose)
610 BIO_printf(bio_err, "User errors %d.\n", errors);
6e81b270 611
7e1b7485
RS
612 if (verbose)
613 BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
8bf78043 614
7b0ee135
MC
615 OPENSSL_free(passin);
616 OPENSSL_free(passout);
6e81b270
AP
617 if (ret)
618 ERR_print_errors(bio_err);
25aaa98a
RS
619 NCONF_free(conf);
620 free_index(db);
dd1abd44 621 release_engine(e);
26a7d938 622 return ret;
6e81b270 623}