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