Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
parsing.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <string.h>
19
20#include <openssl/evp.h>
21
22#include "parsing.h"
23
24#define READ_STR_PROMPT_BASE "Enter %s: "
25
26bool read_string(const char *name, char *str_buf, size_t str_buf_len,
27 bool hidden) {
28
29 char prompt[sizeof(READ_STR_PROMPT_BASE) + 32] = {0};
30 int ret;
31
32 if (str_buf_len < 1) {
33 fprintf(stderr, "Unable to read %s: buffer too small\n", name);
34 return false;
35 }
36
37 ret = snprintf(prompt, sizeof(prompt), READ_STR_PROMPT_BASE, name);
38 if (ret < 0 || ((unsigned int) ret) > (sizeof(prompt) - 1)) {
39 fprintf(stderr, "Unable to read %s: snprintf failed\n", name);
40 return false;
41 }
42
43 if (hidden == true) {
44 ret = EVP_read_pw_string(str_buf, str_buf_len, prompt, true);
45 if (ret != 0) {
46 fprintf(stderr, "Retrieving %s failed (%d)\n", name, ret);
47 return false;
48 }
49 } else {
50 fprintf(stdout, "%s", prompt);
51 str_buf = fgets(str_buf, str_buf_len, stdin);
52 if (str_buf == NULL) {
53 return false;
54 }
55 str_buf[strlen(str_buf) - 1] = '\0';
56 }
57
58 return true;
59}
60
61bool parse_pw(const char *prompt, char *pw, char *parsed, size_t *parsed_len) {
62 if (strlen(pw) > *parsed_len) {
63 fprintf(stderr, "Unable to read name, buffer too small\n");
64 return false;
65 }
66
67 if (strlen(pw) == 0) {
68 if (read_string(prompt, parsed, *parsed_len, true) == false) {
69 return false;
70 }
71 } else {
72 strncpy(parsed, pw, *parsed_len);
73 }
74
75 *parsed_len = strlen(parsed);
76
77 return true;
78}
std::string name
bool read_string(const char *name, char *str_buf, size_t str_buf_len, bool hidden)
Definition parsing.c:26
bool parse_pw(const char *prompt, char *pw, char *parsed, size_t *parsed_len)
Definition parsing.c:61
#define READ_STR_PROMPT_BASE
Definition parsing.c:24
CK_RV ret