1 | //pinentry.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | * NOTE for everyone want's to change something and send patches: |
---|
24 | * read README and HACKING! There a addition information on |
---|
25 | * the license of this document you need to read before you send |
---|
26 | * any patches. |
---|
27 | * |
---|
28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
29 | * or libpulse*: |
---|
30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "libroar.h" |
---|
36 | |
---|
37 | int roar_pinentry_open (struct roar_pinentry * pe, int flags, char * display, char * tty, char * term) { |
---|
38 | #ifdef ROAR_HAVE_BIN_PINENTRY |
---|
39 | int in[2], out[2]; |
---|
40 | |
---|
41 | if ( pe == NULL ) |
---|
42 | return -1; |
---|
43 | |
---|
44 | memset(pe, 0, sizeof(struct roar_pinentry)); |
---|
45 | pe->in = -1; |
---|
46 | pe->out = -1; |
---|
47 | |
---|
48 | if ( display == NULL ) |
---|
49 | display = getenv("DISPLAY"); |
---|
50 | |
---|
51 | if ( display == NULL ) |
---|
52 | display = "(NONE)"; |
---|
53 | |
---|
54 | if ( tty == NULL ) |
---|
55 | tty = ttyname(ROAR_STDIN); |
---|
56 | |
---|
57 | if ( tty == NULL ) |
---|
58 | #ifdef ROAR_DEFAULT_TTY |
---|
59 | tty = ROAR_DEFAULT_TTY; |
---|
60 | #else |
---|
61 | return -1; |
---|
62 | #endif |
---|
63 | |
---|
64 | if ( term == NULL ) |
---|
65 | term = getenv("TERM"); |
---|
66 | |
---|
67 | if ( term == NULL ) |
---|
68 | term = "dumb"; |
---|
69 | |
---|
70 | // open some pipes... |
---|
71 | if ( pipe(in) != 0 ) |
---|
72 | return -1; |
---|
73 | |
---|
74 | if ( pipe(out) != 0 ) { |
---|
75 | close(in[0]); |
---|
76 | close(in[1]); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | pe->pid = fork(); |
---|
81 | |
---|
82 | switch (pe->pid) { |
---|
83 | case -1: |
---|
84 | close(in[0]); |
---|
85 | close(in[1]); |
---|
86 | close(out[0]); |
---|
87 | close(out[1]); |
---|
88 | return -1; |
---|
89 | break; |
---|
90 | case 0: |
---|
91 | close(in[0]); |
---|
92 | close(out[1]); |
---|
93 | close(ROAR_STDIN); |
---|
94 | close(ROAR_STDOUT); |
---|
95 | |
---|
96 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
97 | _exit(1); |
---|
98 | |
---|
99 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
100 | _exit(1); |
---|
101 | |
---|
102 | execlp(ROAR_HAVE_BIN_PINENTRY, "RoarAudio", "--display", display, "--ttytype", term, "--ttyname", tty, NULL); |
---|
103 | |
---|
104 | _exit(1); |
---|
105 | break; |
---|
106 | } |
---|
107 | |
---|
108 | close(in[1]); |
---|
109 | close(out[0]); |
---|
110 | |
---|
111 | pe->in = in[0]; |
---|
112 | pe->out = out[1]; |
---|
113 | |
---|
114 | pe->fin = fdopen(in[0], "r"); |
---|
115 | |
---|
116 | roar_pinentry_recv(pe, NULL, NULL); |
---|
117 | |
---|
118 | return 0; |
---|
119 | #else |
---|
120 | return -1; |
---|
121 | #endif |
---|
122 | } |
---|
123 | |
---|
124 | int roar_pinentry_simple_open(struct roar_pinentry * pe) { |
---|
125 | return roar_pinentry_open(pe, 0, NULL, NULL, NULL); |
---|
126 | } |
---|
127 | |
---|
128 | int roar_pinentry_close(struct roar_pinentry * pe) { |
---|
129 | int status; |
---|
130 | |
---|
131 | if ( pe == NULL ) |
---|
132 | return -1; |
---|
133 | |
---|
134 | if ( pe->opened == 0 ) |
---|
135 | return 0; |
---|
136 | |
---|
137 | if ( pe->out != -1 ) |
---|
138 | close(pe->out); |
---|
139 | |
---|
140 | if ( pe->fin != NULL ) |
---|
141 | fclose(pe->fin); |
---|
142 | |
---|
143 | if ( pe->in != -1 ) |
---|
144 | close(pe->in); |
---|
145 | |
---|
146 | waitpid(pe->pid, &status, 0); |
---|
147 | |
---|
148 | memset(pe, 0, sizeof(struct roar_pinentry)); |
---|
149 | |
---|
150 | return 0; |
---|
151 | } |
---|
152 | |
---|
153 | int roar_pinentry_send (struct roar_pinentry * pe, char * cmd, char * args) { |
---|
154 | size_t len; |
---|
155 | |
---|
156 | if ( pe == NULL ) |
---|
157 | return -1; |
---|
158 | |
---|
159 | if ( cmd == NULL ) |
---|
160 | return -1; |
---|
161 | |
---|
162 | len = strlen(cmd); |
---|
163 | |
---|
164 | if ( write(pe->out, cmd, len) != len ) |
---|
165 | return -1; |
---|
166 | |
---|
167 | if ( args != NULL ) { |
---|
168 | if ( write(pe->out, " ", 1) != 1 ) |
---|
169 | return -1; |
---|
170 | |
---|
171 | len = strlen(args); |
---|
172 | |
---|
173 | if ( write(pe->out, args, len) != len ) |
---|
174 | return -1; |
---|
175 | } |
---|
176 | |
---|
177 | if ( write(pe->out, "\n", 1) != 1 ) |
---|
178 | return -1; |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | #define MAX_LINE_SIZE 2048 |
---|
185 | int roar_pinentry_recv (struct roar_pinentry * pe, char ** line, char ** opts) { |
---|
186 | char realbuf[MAX_LINE_SIZE]; |
---|
187 | char * tp; |
---|
188 | |
---|
189 | if ( pe == NULL ) |
---|
190 | return -1; |
---|
191 | |
---|
192 | if ( pe->fin == NULL ) |
---|
193 | return -1; |
---|
194 | |
---|
195 | if ( fgets(realbuf, MAX_LINE_SIZE, pe->fin) == NULL ) |
---|
196 | return -1; |
---|
197 | |
---|
198 | tp = realbuf + strlen(realbuf) - 1; |
---|
199 | |
---|
200 | for (; *tp == '\r' || *tp == '\n'; tp--) |
---|
201 | *tp = 0; |
---|
202 | |
---|
203 | if ( (tp = strstr(realbuf, " ")) == NULL ) { |
---|
204 | if ( line != NULL ) |
---|
205 | *line = strdup(realbuf); |
---|
206 | |
---|
207 | if ( opts != NULL ) |
---|
208 | *opts = NULL; |
---|
209 | |
---|
210 | if ( !strcmp(realbuf, "OK") ) { |
---|
211 | return 0; |
---|
212 | } else if ( !strcmp(realbuf, "ERR") ) { |
---|
213 | return 1; |
---|
214 | } else { |
---|
215 | return -1; |
---|
216 | } |
---|
217 | } else { |
---|
218 | *tp = 0; |
---|
219 | |
---|
220 | if ( !strcmp(realbuf, "D") ) { |
---|
221 | if ( opts != NULL ) |
---|
222 | *opts = strdup(tp+1); |
---|
223 | |
---|
224 | return roar_pinentry_recv(pe, line, NULL); |
---|
225 | } |
---|
226 | |
---|
227 | if ( line != NULL ) |
---|
228 | *line = strdup(realbuf); |
---|
229 | |
---|
230 | if ( opts != NULL ) |
---|
231 | *opts = NULL; |
---|
232 | |
---|
233 | if ( !strcmp(realbuf, "OK") ) { |
---|
234 | return 0; |
---|
235 | } else if ( !strcmp(realbuf, "ERR") ) { |
---|
236 | return 1; |
---|
237 | } else { |
---|
238 | return -1; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | return -1; |
---|
243 | } |
---|
244 | |
---|
245 | int roar_pinentry_req (struct roar_pinentry * pe, char * cmd, char * args, char ** line, char ** opts) { |
---|
246 | if ( pe == NULL ) |
---|
247 | return -1; |
---|
248 | |
---|
249 | if ( roar_pinentry_send(pe, cmd, args) != 0 ) |
---|
250 | return -1; |
---|
251 | |
---|
252 | return roar_pinentry_recv(pe, line, opts); |
---|
253 | } |
---|
254 | |
---|
255 | int roar_pinentry_set_desc (struct roar_pinentry * pe, char * desc) { |
---|
256 | return roar_pinentry_set(pe, "DESC", desc); |
---|
257 | } |
---|
258 | |
---|
259 | int roar_pinentry_set_prompt(struct roar_pinentry * pe, char * prompt) { |
---|
260 | return roar_pinentry_set(pe, "PROMPT", prompt); |
---|
261 | } |
---|
262 | |
---|
263 | int roar_pinentry_set_yes (struct roar_pinentry * pe, char * yes) { |
---|
264 | return roar_pinentry_set(pe, "OK", yes); |
---|
265 | } |
---|
266 | |
---|
267 | int roar_pinentry_set_no (struct roar_pinentry * pe, char * no) { |
---|
268 | return roar_pinentry_set(pe, "CANCEL", no); |
---|
269 | } |
---|
270 | |
---|
271 | int roar_pinentry_set (struct roar_pinentry * pe, char * obj, char * text) { |
---|
272 | char req[80] = "SET"; |
---|
273 | |
---|
274 | if ( pe == NULL ) |
---|
275 | return -1; |
---|
276 | |
---|
277 | if ( obj == NULL ) |
---|
278 | return -1; |
---|
279 | |
---|
280 | if ( strlen(obj) > (80-(1 /* \0 */ + 3 /* "SET" */)) ) |
---|
281 | return -1; |
---|
282 | |
---|
283 | strncat(req, obj, 80-4); |
---|
284 | |
---|
285 | return roar_pinentry_req(pe, req, text, NULL, NULL); |
---|
286 | } |
---|
287 | |
---|
288 | int roar_pinentry_getpin (struct roar_pinentry * pe, char ** pw, char * desc, char * prompt) { |
---|
289 | if ( pe == NULL ) |
---|
290 | return -1; |
---|
291 | |
---|
292 | if ( pw == NULL ) |
---|
293 | return -1; |
---|
294 | |
---|
295 | if ( desc != NULL ) |
---|
296 | if ( roar_pinentry_set_desc(pe, desc) != 0 ) |
---|
297 | return -1; |
---|
298 | |
---|
299 | if ( prompt != NULL ) |
---|
300 | if ( roar_pinentry_set_prompt(pe, prompt) != 0 ) |
---|
301 | return -1; |
---|
302 | |
---|
303 | if ( roar_pinentry_req(pe, "GETPIN", NULL, NULL, pw) == -1 ) |
---|
304 | return -1; |
---|
305 | |
---|
306 | return 0; |
---|
307 | } |
---|
308 | |
---|
309 | int roar_pinentry_confirm (struct roar_pinentry * pe, char * desc, char * yes, char * no) { |
---|
310 | if ( pe == NULL ) |
---|
311 | return -1; |
---|
312 | |
---|
313 | if ( desc != NULL ) |
---|
314 | if ( roar_pinentry_set_desc(pe, desc) != 0 ) |
---|
315 | return -1; |
---|
316 | |
---|
317 | if ( yes != NULL ) |
---|
318 | if ( roar_pinentry_set_yes(pe, yes) != 0 ) |
---|
319 | return -1; |
---|
320 | |
---|
321 | if ( no != NULL ) |
---|
322 | if ( roar_pinentry_set_no(pe, no) != 0 ) |
---|
323 | return -1; |
---|
324 | |
---|
325 | return roar_pinentry_req(pe, "CONFIRM", NULL, NULL, NULL); |
---|
326 | } |
---|
327 | |
---|
328 | //ll |
---|