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