19
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
1 /*
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
3 *
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
11 *
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software.
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
14 *
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
22 */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
23
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
24 #include "compat.h"
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
25
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
26 #ifdef HAVE_ERR_H
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
27 #include <err.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
28 #endif /* HAVE_ERR_H */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
29 #include <errno.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
30 #include <setjmp.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
31 #include <signal.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
32 #include <stdarg.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
33 #include <stdio.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
34 #include <stdio.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
35 #include <string.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
36 #include <unistd.h>
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
37
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
38 #include "io.h"
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
39 #include "util.h"
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
40 #include "pwm.h"
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
41
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
42 static sigjmp_buf signal_env;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
43
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
44 static void
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
45 signal_handler(int signal_no)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
46 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
47 siglongjmp(signal_env, signal_no);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
48 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
49
|
22
|
50 void
|
|
51 io_trim_nl(char *s)
|
|
52 {
|
|
53 size_t len;
|
|
54
|
|
55 len = strlen(s);
|
|
56 if ((len > 0) && (s[len - 1] == '\n')) {
|
|
57 s[len - 1] = '\0';
|
|
58 }
|
|
59 }
|
|
60
|
19
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
61 int
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
62 io_gl_complete_nothing(WordCompletion *cpl, void *data, const char *line,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
63 int word_end)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
64 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
65 return (0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
66 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
67
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
68 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
69 io_get_char(const char *prompt, int *cp)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
70 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
71 enum io_status retval = IO_OK;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
72 GetLine *gl = NULL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
73
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
74 gl = new_GetLine(16, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
75 if (gl== NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
76 err(1, "new_GetLine");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
77 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
78 gl_catch_blocked(gl);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
79
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
80 /* prompt with echo off */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
81 gl_echo_mode(gl, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
82 if ((*cp = gl_query_char(gl, prompt, '\0')) == EOF) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
83 switch (gl_return_status(gl)) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
84 case GLR_SIGNAL:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
85 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
86 break;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
87 case GLR_ERROR:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
88 errx(1, "gl_get_line: %s",
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
89 gl_error_message(gl, NULL, 0));
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
90 default:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
91 errx(1, "unknown error in gl_get_line");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
92 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
93 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
94
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
95 /* erase prompt */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
96 if (io_printf("\r%*s\r", (int)strlen(prompt), "") == IO_SIGNAL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
97 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
98 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
99
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
100 del_GetLine(gl);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
101
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
102 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
103 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
104
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
105 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
106 io_get_line(GetLine *gl, const char *prompt, int with_history,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
107 const char *start_line, int start_pos, size_t buf_size, char *buf)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
108 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
109 enum io_status retval = IO_OK;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
110 GetLine *gl_private = NULL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
111 GlHistoryState state;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
112 char *line;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
113
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
114 if (gl == NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
115 gl = gl_private = new_GetLine(buf_size - 1, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
116 if (gl_private == NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
117 err(1, "new_GetLine");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
118 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
119 gl_catch_blocked(gl_private);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
120 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
121
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
122 gl_state_of_history(gl, &state);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
123 gl_toggle_history(gl, with_history);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
124
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
125 line = gl_get_line(gl, prompt, start_line, start_pos);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
126 if (line == NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
127 switch (gl_return_status(gl)) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
128 case GLR_BLOCKED:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
129 break;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
130 case GLR_SIGNAL:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
131 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
132 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
133 case GLR_EOF:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
134 retval = IO_EOF;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
135 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
136 case GLR_ERROR:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
137 errx(1, "gl_get_line: %s",
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
138 gl_error_message(gl, NULL, 0));
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
139 default:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
140 errx(1, "unknown error in gl_get_line");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
141 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
142 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
143
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
144 if (snprintf(buf, buf_size, "%s", line) >= (int)buf_size) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
145 retval = IO_TRUNCATED;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
146 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
147
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
148 out:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
149 if (gl != NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
150 gl_toggle_history(gl, state.enabled);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
151 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
152 del_GetLine(gl_private);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
153
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
154 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
155 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
156
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
157 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
158 io_get_password(const char *prompt, const char *confirm_prompt,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
159 size_t buf_size, char *buf)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
160 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
161 enum io_status retval = IO_OK;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
162 GetLine *gl = NULL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
163 size_t len;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
164 char *password_buf = NULL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
165 char *confirm_buf = NULL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
166
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
167 gl = new_GetLine(buf_size - 1, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
168 if (gl == NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
169 err(1, "new_GetLine");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
170 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
171 /* disable default filename completion */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
172 gl_customize_completion(gl, NULL, io_gl_complete_nothing);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
173 gl_echo_mode(gl, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
174
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
175 password_buf = xmalloc(buf_size);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
176
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
177 if (io_get_line(gl, prompt, 0, NULL, 0, buf_size, password_buf) ==
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
178 IO_SIGNAL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
179 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
180 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
181 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
182 len = strlen(password_buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
183 /* strip trailing newline */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
184 if ((len > 0) && (password_buf[len - 1] == '\n')) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
185 password_buf[--len] = '\0';
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
186 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
187 if (len == 0) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
188 retval = IO_PASSWORD_EMPTY;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
189 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
190 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
191
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
192 if (confirm_prompt != NULL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
193 if (io_printf("\n") == IO_SIGNAL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
194 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
195 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
196 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
197
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
198 /* confirm new password */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
199 confirm_buf = xmalloc(buf_size);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
200 if (io_get_line(gl, confirm_prompt, 0, NULL, 0,
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
201 buf_size, confirm_buf) == IO_SIGNAL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
202 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
203 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
204 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
205 len = strlen(confirm_buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
206 /* strip trailing newline */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
207 if ((len > 0) && (confirm_buf[len - 1] == '\n')) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
208 confirm_buf[--len] = '\0';
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
209 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
210 if (strcmp(password_buf, confirm_buf) != 0) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
211 retval = IO_PASSWORD_MISMATCH;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
212 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
213 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
214 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
215
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
216 strcpy(buf, password_buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
217
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
218 out:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
219 if (io_printf("\n") == IO_SIGNAL) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
220 retval = IO_SIGNAL;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
221 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
222 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
223 free(password_buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
224 free(confirm_buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
225 del_GetLine(gl);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
226
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
227 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
228 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
229
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
230 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
231 io_dputs(int fd, const char *s)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
232 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
233 struct sigaction action;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
234 struct sigaction oaction;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
235 int signal_no = 0;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
236 const char *p = s;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
237 size_t remaining;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
238 ssize_t n;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
239
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
240 /* install signal handlers */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
241 action.sa_handler = signal_handler;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
242 action.sa_flags = 0;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
243 sigemptyset(&action.sa_mask);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
244 sigaddset(&action.sa_mask, SIGINT);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
245 sigaddset(&action.sa_mask, SIGTERM);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
246 sigaddset(&action.sa_mask, SIGHUP);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
247 sigaddset(&action.sa_mask, SIGQUIT);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
248 if ((sigaction(SIGINT, &action, &oaction) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
249 (sigaction(SIGTERM, &action, &oaction) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
250 (sigaction(SIGHUP, &action, &oaction) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
251 (sigaction(SIGQUIT, &action, &oaction) != 0)) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
252 err(1, "sigaction");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
253 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
254
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
255 if ((signal_no = sigsetjmp(signal_env, 1)) != 0) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
256 /* signal received, signal mask has been restored */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
257 goto out;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
258 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
259
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
260 remaining = strlen(s);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
261 while (remaining > 0) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
262 pwm_unblock_signals();
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
263 n = write(fd, p, remaining);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
264 if ((n < (int)remaining) && (errno != EINTR)) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
265 err(1, "write");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
266 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
267 pwm_block_signals();
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
268 remaining -= MAX(n, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
269 p += MAX(n, 0);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
270 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
271
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
272 out:
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
273 /* restore signal handlers */
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
274 if ((sigaction(SIGINT, &oaction, NULL) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
275 (sigaction(SIGTERM, &oaction, NULL) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
276 (sigaction(SIGHUP, &oaction, NULL) != 0) ||
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
277 (sigaction(SIGQUIT, &oaction, NULL) != 0)) {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
278 err(1, "sigaction");
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
279 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
280
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
281 return ((signal_no == 0) ? IO_OK : IO_SIGNAL);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
282 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
283
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
284 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
285 io_vdprintf(int fd, const char *fmt, va_list args)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
286 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
287 enum io_status retval;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
288 char *buf;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
289
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
290 xvasprintf(&buf, fmt, args);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
291 retval = io_dputs(fd, buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
292 free(buf);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
293
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
294 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
295 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
296
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
297 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
298 io_dprintf(int fd, const char *fmt, ...)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
299 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
300 enum io_status retval;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
301 va_list args;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
302
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
303 va_start(args, fmt);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
304 retval = io_vdprintf(fd, fmt, args);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
305 va_end(args);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
306
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
307 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
308 }
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
309
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
310 enum io_status
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
311 io_printf(const char *fmt, ...)
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
312 {
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
313 enum io_status retval;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
314 va_list args;
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
315
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
316 va_start(args, fmt);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
317 retval = io_vdprintf(STDOUT_FILENO, fmt, args);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
318 va_end(args);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
319
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
320 return (retval);
|
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
321 }
|