source: roaraudio/libroar/pinentry.c @ 1235:e53f5bf8f19b

Last change on this file since 1235:e53f5bf8f19b was 1235:e53f5bf8f19b, checked in by phi, 15 years ago

fixed some bugs...

File size: 6.5 KB
Line 
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
37int roar_pinentry_open (struct roar_pinentry * pe, int flags, char * display, char * tty, char * term) {
38 int in[2], out[2];
39
40 if ( pe == NULL )
41  return -1;
42
43 memset(pe, 0, sizeof(struct roar_pinentry));
44 pe->in  = -1;
45 pe->out = -1;
46
47#ifdef ROAR_HAVE_BIN_PINENTRY
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  tty = "/dev/tty"; // TODO: make compile time config parameter out of this
59
60 if ( term == NULL )
61  term = getenv("TERM");
62
63 if ( term == NULL )
64  term = "dumb";
65
66 // open some pipes...
67 if ( pipe(in) != 0 )
68  return -1;
69
70 if ( pipe(out) != 0 ) {
71  close(in[0]);
72  close(in[1]);
73  return -1;
74 }
75
76 pe->pid = fork();
77
78 switch (pe->pid) {
79  case -1:
80    close(in[0]);
81    close(in[1]);
82    close(out[0]);
83    close(out[1]);
84    return -1;
85   break;
86  case 0:
87    close(in[0]);
88    close(out[1]);
89    close(ROAR_STDIN);
90    close(ROAR_STDOUT);
91
92    if ( dup2(out[0], ROAR_STDIN) == -1 )
93     _exit(1);
94
95    if ( dup2(in[1], ROAR_STDOUT) == -1 )
96     _exit(1);
97
98    execlp(ROAR_HAVE_BIN_PINENTRY, "RoarAudio", "--display", display, "--ttytype", term, "--ttyname", tty, NULL);
99
100    _exit(1);
101   break;
102 }
103
104 close(in[1]);
105 close(out[0]);
106
107 pe->in  = in[0];
108 pe->out = out[1];
109
110 pe->fin = fdopen(in[0], "r");
111
112 roar_pinentry_recv(pe, NULL, NULL);
113
114 return 0;
115#else
116 return -1;
117#endif
118}
119
120int roar_pinentry_simple_open(struct roar_pinentry * pe) {
121 return roar_pinentry_open(pe, 0, NULL, NULL, NULL);
122}
123
124int roar_pinentry_close(struct roar_pinentry * pe) {
125 if ( pe == NULL )
126  return -1;
127
128 if ( pe->opened == 0 )
129  return 0;
130
131 if ( pe->out != -1 )
132  close(pe->out);
133
134 if ( pe->fin != NULL )
135  fclose(pe->fin);
136
137 if ( pe->in  != -1 )
138  close(pe->in);
139
140 memset(pe, 0, sizeof(struct roar_pinentry));
141
142 return 0;
143}
144
145int roar_pinentry_send (struct roar_pinentry * pe, char * cmd,  char * args) {
146 size_t len;
147
148 if ( pe == NULL )
149  return -1;
150
151 if ( cmd == NULL )
152  return -1;
153
154 len = strlen(cmd);
155
156 if ( write(pe->out, cmd, len) != len )
157  return -1;
158
159 if ( args != NULL ) {
160  len = strlen(args);
161
162  if ( write(pe->out, args, len) != len )
163   return -1;
164 }
165
166 if ( write(pe->out, "\n", 1) != 1 )
167  return -1;
168
169 return 0;
170}
171
172
173#define MAX_LINE_SIZE 2048
174int roar_pinentry_recv (struct roar_pinentry * pe, char ** line, char ** opts) {
175 char realbuf[MAX_LINE_SIZE];
176 char * tp;
177
178 if ( pe == NULL )
179  return -1;
180
181 if ( pe->fin == NULL )
182  return -1;
183
184 if ( fgets(realbuf, MAX_LINE_SIZE, pe->fin) == NULL )
185  return -1;
186
187 tp = realbuf + strlen(realbuf) - 1;
188
189 for (; *tp == '\r' || *tp == '\n'; tp--)
190  *tp = 0;
191
192 if ( (tp = strstr(realbuf, " ")) == NULL ) {
193  if ( line != NULL )
194   *line = strdup(realbuf);
195
196  if ( opts != NULL )
197   *opts = NULL;
198
199  if ( !strcmp(realbuf, "OK") ) {
200   return 0;
201  } else if ( !strcmp(realbuf, "ERR") ) {
202   return 1;
203  } else {
204   return -1;
205  }
206 } else {
207  *tp = 0;
208
209  if ( !strcmp(realbuf, "D") ) {
210   if ( opts != NULL )
211    *opts = strdup(tp+1);
212
213   return roar_pinentry_recv(pe, line, NULL);
214  }
215
216  if ( line != NULL )
217   *line = strdup(realbuf);
218
219  if ( opts != NULL )
220   *opts = NULL;
221
222  if ( !strcmp(realbuf, "OK") ) {
223   return 0;
224  } else if ( !strcmp(realbuf, "ERR") ) {
225   return 1;
226  } else {
227   return -1;
228  }
229 }
230
231 return -1;
232}
233
234int roar_pinentry_req  (struct roar_pinentry * pe, char * cmd,  char * args, char ** line, char ** opts) {
235 if ( pe == NULL )
236  return -1;
237
238 if ( roar_pinentry_send(pe, cmd, args) != 0 )
239  return -1;
240
241 return roar_pinentry_recv(pe, line, opts);
242}
243
244int roar_pinentry_set_desc (struct roar_pinentry * pe, char * desc) {
245 return roar_pinentry_set(pe, "DESC", desc);
246}
247
248int roar_pinentry_set_prompt(struct roar_pinentry * pe, char * prompt) {
249 return roar_pinentry_set(pe, "PROMPT", prompt);
250}
251
252int roar_pinentry_set_yes  (struct roar_pinentry * pe, char * yes) {
253 return roar_pinentry_set(pe, "OK", yes);
254}
255
256int roar_pinentry_set_no   (struct roar_pinentry * pe, char * no) {
257 return roar_pinentry_set(pe, "CANCEL", no);
258}
259
260int roar_pinentry_set      (struct roar_pinentry * pe, char * obj, char * text) {
261 char req[80] = "SET";
262
263 if ( pe == NULL )
264  return -1;
265
266 if ( obj == NULL )
267  return -1;
268
269 if ( strlen(obj) > (80-1 /* \0 */ + 3 /* "SET" */ + 1 /* " " */) )
270  return -1;
271
272 strncat(req, obj, 80-5);
273 strncat(req, " ", 2);
274
275 return roar_pinentry_req(pe, req, text, NULL, NULL);
276}
277
278int roar_pinentry_getpin   (struct roar_pinentry * pe, char ** pw, char * desc, char * prompt) {
279 if ( pe == NULL )
280  return -1;
281
282 if ( pw == NULL )
283  return -1;
284
285 if ( desc != NULL )
286  if ( roar_pinentry_set_desc(pe, desc) != 0 )
287   return -1;
288
289 if ( prompt != NULL )
290  if ( roar_pinentry_set_prompt(pe, prompt) != 0 )
291   return -1;
292
293 if ( roar_pinentry_req(pe, "GETPIN", NULL, NULL, pw) == -1 )
294  return -1;
295
296 return 0;
297}
298
299int roar_pinentry_confirm  (struct roar_pinentry * pe, char * desc, char * yes, char * no) {
300 if ( pe == NULL )
301  return -1;
302
303 if ( desc != NULL )
304  if ( roar_pinentry_set_desc(pe, desc) != 0 )
305   return -1;
306
307 if ( yes != NULL )
308  if ( roar_pinentry_set_yes(pe, yes) != 0 )
309   return -1;
310
311 if ( no != NULL )
312  if ( roar_pinentry_set_no(pe, no) != 0 )
313   return -1;
314
315 return roar_pinentry_req(pe, "CONFIRM", NULL, NULL, NULL);
316}
317
318//ll
Note: See TracBrowser for help on using the repository browser.