source: roaraudio/libroar/auth.c @ 4788:482fc1a96c7a

Last change on this file since 4788:482fc1a96c7a was 4788:482fc1a96c7a, checked in by phi, 13 years ago

Added simple authfile support to roard (pr1)

File size: 11.7 KB
Line 
1//auth.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illegal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38/* How auth works:
39 * 0) set stage to zero
40 * 1) get server address and local node name (from uname())
41 * 2) look up authfile/authdb/authservice for the server+local address + stage.
42 *    if no data was found send NONE-Auth.
43 * 3) send data to server
44 * 4) read answer from server
45 * 5) if stage of server response is non-zero increment stage to server stage+1
46 *    and repeat from step 2)
47 * 6) check if we got an OK or an ERROR, return correct value
48 */
49
50/* The protocol:
51 * Auth request:
52 * Byte 0: auth type
53 * Byte 1: stage
54 * Byte 2: reserved (must be zero)
55 * Byte 3: reserved (must be zero)
56 * Byte 4-end: auth type depending data.
57 *
58 * If no data is to be send bytes 2 and 3 can be omitted.
59 * If no data is to be send and stage is zero bytes 1, 2 and 3 can be omitted.
60 *
61 * Auth response:
62 * The same as the auth request.
63 * if the server sends an zero size message back it means the server accepted our connection
64 * and no additional stage is needed.
65 * if the message type is OK the server accepted our auth.
66 * if the message type is ERROR the server rejected us. we may try other auth methods.
67 * if the server accepted our data and the stage is non-zero we need to continue with the next
68 * stage of the auth.
69 * if the server rejected us the auth type value of the response is a suggested next auth type
70 * we should try if possible. This may help the client to find a working auth type.
71 */
72
73/* The protocol by auth type:
74 *
75 * --- NONE:
76 * No data is send, the server accepts the connect or rejects it depending on some
77 * magic within the server. we do not care about this.
78 * The data block is not used.
79 *
80 * --- COOKIE:
81 * We send cookies for all stages the server ask us to provide a cookie.
82 * if a cookie is wrong the server rejects us or asks us for another.
83 * The cookie is send as binary data in the data block.
84 *
85 * --- TRUST:
86 * We ask the server to auth us based on our UID/GID/PID.
87 * The server may reject this because we are not allowed or because it is not
88 * supported by the transport.
89 * If we get rejected we may try to continue with IDENT then RHOST before we use NONE.
90 * The data block is not used.
91 *
92 * --- PASSWORD:
93 * This is technically the same as COOKIE just that the cookie is limited to
94 * printable ASCII chars and that the user should be asked to provide the password.
95 * This may be done via a GUI popup window.
96 *
97 * --- SYSUSER:
98 * We provide a Username + Password for a system user.
99 * The data block contains of two main parts:
100 * The first part is a one byte long subtype.
101 * The value must be 0x01 for username+password.
102 * future versions may define other types.
103 * the second part is the actual data block.
104 * for username+password it is splited into two fields, both terminated with \0.
105 * the first is the username the last one the password as clear text.
106 * Example: char data[] = "\001MyUser\0MyPassword\0";
107 *
108 * --- OPENPGP_SIGN:
109 *
110 * --- OPENPGP_ENCRYPT:
111 *
112 * --- OPENPGP_AUTH:
113 *
114 * --- KERBEROS:
115 * We use Kerberos to auth.
116 *
117 * --- RHOST:
118 * The server is asked to auth us based on our source address.
119 * The data block is not used.
120 *
121 * --- XAUTH:
122 * We send an X11 Cookie.
123 *
124 * --- IDENT:
125 * The server is asked to auth us based on our source address using the IDENT protocol.
126 * The data block is not used.
127 *
128 */
129
130static int roar_auth_ask_server (struct roar_connection * con, struct roar_auth_message * authmes) {
131 struct roar_error_frame error_frame;
132 struct roar_message     mes;
133 char                  * header = mes.data;
134 int                     ret;
135
136 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
137
138 mes.cmd     = ROAR_CMD_AUTH;
139 mes.datalen = 4 + authmes->len;
140
141 if ( mes.datalen > sizeof(mes.data) )
142  return -1;
143
144 header[0] = authmes->type;
145 header[1] = authmes->stage;
146 header[2] = authmes->reserved.c[0];
147 header[3] = authmes->reserved.c[1];
148
149 if ( authmes->len ) {
150  memcpy(mes.data + 4, authmes->data, authmes->len);
151 }
152
153 if ( (ret = roar_req(con, &mes, NULL)) == -1 ) {
154  authmes->type = -1;
155  return -1;
156 }
157
158 if ( mes.cmd != ROAR_CMD_OK ) {
159  ret = -1;
160  if ( roar_err_parsemsg(&mes, &error_frame) == -1 ) {
161   authmes->type = -1;
162   return -1;
163  }
164
165  header = error_frame.data;
166  mes.datalen = error_frame.datalen;
167 }
168
169 if ( mes.datalen < 4 ) {
170  memset(header+mes.datalen, 0, 4-mes.datalen);
171  authmes->type          = -1;
172 } else {
173  authmes->type          = header[0];
174 }
175
176 authmes->stage         = header[1];
177 authmes->reserved.c[0] = header[2];
178 authmes->reserved.c[1] = header[3];
179
180 return ret;
181}
182
183static void roar_auth_mes_init(struct roar_auth_message * authmes, int type) {
184 memset(authmes, 0, sizeof(struct roar_auth_message));
185
186 authmes->type  = type;
187 authmes->stage = 0;
188 authmes->data  = NULL;
189 authmes->len   = 0;
190}
191
192
193static int try_password (struct roar_connection * con, int * next) {
194 struct roar_message mes;
195 struct roar_auth_message authmes;
196 char * pw;
197
198 // TODO: add support for *next.
199
200 roar_auth_mes_init(&authmes, ROAR_AUTH_T_PASSWORD);
201
202 if ( roar_passwd_simple_ask_pw(&pw, "Password for RoarAudio Server?", NULL) == -1 ) {
203  return -1;
204 }
205
206 authmes.len = strlen(pw);
207
208 if ( roar_auth_init_mes(&mes, &authmes) == -1 ) {
209  roar_mm_free(pw);
210  return -1;
211 }
212
213 // do not use strcpy() because that would copy \0, too.
214 memcpy(authmes.data, pw, authmes.len);
215
216 roar_mm_free(pw);
217
218 if ( roar_req(con, &mes, NULL) == -1 )
219  return -1;
220
221 if ( mes.cmd != ROAR_CMD_OK )
222  return -1;
223
224 if ( roar_auth_from_mes(&authmes, &mes, NULL) == -1 )
225  return -1;
226
227 if ( authmes.stage == 0 )
228  return 0;
229
230 return -1;
231}
232
233static int try_cookie (struct roar_connection * con, int * next) {
234 struct roar_libroar_config * config = roar_libroar_get_config();
235 struct roar_auth_message authmes;
236 struct roar_authfile * authfile;
237 struct roar_authfile_key * key;
238 int idx;
239 int done = 0;
240
241 roar_auth_mes_init(&authmes, ROAR_AUTH_T_COOKIE);
242
243 if ( (authfile = roar_authfile_open(ROAR_AUTHFILE_TYPE_AUTO, config->authfile, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL )
244  return -1;
245
246 for (idx = 0; !done; idx++) {
247  if ( (key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_COOKIE, idx, NULL)) == NULL )
248   break;
249
250  authmes.data = key->data;
251  authmes.len  = key->len;
252
253  if ( roar_auth_ask_server(con, &authmes) != -1 )
254   done = 1;
255
256  roar_authfile_key_unref(key);
257 }
258
259 roar_authfile_close(authfile);
260
261 return done ? 0 : -1;
262}
263
264#define _EOL ROAR_AUTH_T_AUTO
265int roar_auth   (struct roar_connection * con) {
266 struct roar_auth_message authmes;
267 int ret;
268 int i;
269 int cur, next;
270 int done;
271 int ltt[] = {
272              ROAR_AUTH_T_TRUST,
273              ROAR_AUTH_T_IDENT,
274              ROAR_AUTH_T_RHOST,
275//              ROAR_AUTH_T_PASSWORD,
276              ROAR_AUTH_T_COOKIE,
277              ROAR_AUTH_T_NONE,
278              _EOL
279             };
280
281 for (i = 0; ltt[i] != _EOL; i++) {
282  next = ltt[i];
283  while (next != -1) {
284   done = 1;
285
286   cur  = next;
287   next = -1;
288
289   switch (cur) {
290    case ROAR_AUTH_T_PASSWORD:
291      if ( (ret = try_password(con, &next)) == -1 )
292       done = 0;
293      break;
294    case ROAR_AUTH_T_TRUST:
295    case ROAR_AUTH_T_IDENT:
296    case ROAR_AUTH_T_RHOST:
297    case ROAR_AUTH_T_NONE:
298      roar_auth_mes_init(&authmes, ltt[i]);
299      if ( (ret = roar_auth_ask_server(con, &authmes)) == -1 )
300       done = 0;
301
302      next = authmes.type;
303     break;
304    case ROAR_AUTH_T_COOKIE:
305      if ( (ret = try_cookie(con, &next)) == -1 )
306       done = 0;
307      break;
308     break;
309    default: /* Bad error! */
310      return -1;
311     break;
312   }
313
314   if ( authmes.stage != 0 )
315    done = 0;
316
317   if ( done )
318    return 0;
319  }
320 }
321
322 return -1;
323}
324
325
326int roar_auth_from_mes(struct roar_auth_message * ames, struct roar_message * mes, void * data) {
327 void * ibuf;
328 char header[4] = {0, 0, 0, 0};
329
330 if ( ames == NULL || mes == NULL )
331  return -1;
332
333 if ( data != NULL ) {
334  ibuf = data;
335 } else {
336  ibuf = mes->data;
337 }
338
339 memset(ames, 0, sizeof(struct roar_auth_message));
340
341 memcpy(header, ibuf, mes->datalen < 4 ? mes->datalen : 4);
342
343 ames->type          = header[0];
344 ames->stage         = header[1];
345 ames->reserved.c[0] = header[2];
346 ames->reserved.c[1] = header[3];
347
348 if ( mes->datalen > 4 ) {
349  ames->data = ibuf + 4;
350  ames->len  = mes->datalen - 4;
351 } else {
352  ames->data = NULL;
353  ames->len  = 0;
354 }
355
356 return 0;
357}
358
359int roar_auth_to_mes(struct roar_message * mes, void ** data, struct roar_auth_message * ames) {
360 char * obuf;
361
362 if ( mes == NULL || ames == NULL )
363  return -1;
364
365 if ( data != NULL )
366  *data = NULL;
367
368 memset(mes, 0, sizeof(struct roar_message));
369
370 mes->cmd = ROAR_CMD_AUTH;
371
372 if ( (ames->len + 4) > sizeof(mes->data) ) {
373  *data = malloc(ames->len + 4);
374  if ( *data == NULL )
375   return -1;
376  obuf = *data;
377 } else {
378  obuf = mes->data;
379 }
380
381 obuf[0] = ames->type;
382 obuf[1] = ames->stage;
383 obuf[2] = ames->reserved.c[0];
384 obuf[3] = ames->reserved.c[1];
385
386 memcpy(obuf + 8, ames->data, ames->len);
387
388 mes->datalen = ames->len + 4;
389
390 return 0;
391}
392
393int roar_auth_init_mes(struct roar_message * mes, struct roar_auth_message * ames) {
394 if ( mes == NULL || ames == NULL )
395  return -1;
396
397 if ( (ames->len + 4) > sizeof(mes->data) )
398  return -1;
399
400 memset(mes, 0, sizeof(struct roar_message));
401
402 mes->cmd = ROAR_CMD_AUTH;
403
404 mes->data[0] = ames->type;
405 mes->data[1] = ames->stage;
406 mes->data[2] = ames->reserved.c[0];
407 mes->data[3] = ames->reserved.c[1];
408
409 ames->data = &(mes->data[4]);
410
411 mes->datalen = ames->len + 4;
412
413 return 0;
414}
415
416
417
418// String functions:
419static struct {
420 int    type;
421 const char * name;
422} _g_authts[] = {
423// grep ^'#define ROAR_AUTH_T_' auth.h | while read d t d; do n=$(cut -d_ -f4 <<<$t | tr A-Z a-z); printf ' {%-28s %-10s},\n' $t, \"$n\"; done
424 {ROAR_AUTH_T_NONE,            "none"    },
425 {ROAR_AUTH_T_COOKIE,          "cookie"  },
426 {ROAR_AUTH_T_TRUST,           "trust"   },
427 {ROAR_AUTH_T_PASSWORD,        "password"},
428 {ROAR_AUTH_T_SYSUSER,         "sysuser" },
429 {ROAR_AUTH_T_OPENPGP_SIGN,    "openpgp" },
430 {ROAR_AUTH_T_OPENPGP_ENCRYPT, "openpgp" },
431 {ROAR_AUTH_T_OPENPGP_AUTH,    "openpgp" },
432 {ROAR_AUTH_T_KERBEROS,        "kerberos"},
433 {ROAR_AUTH_T_RHOST,           "rhost"   },
434 {ROAR_AUTH_T_XAUTH,           "xauth"   },
435 {ROAR_AUTH_T_IDENT,           "ident"   },
436 {-1, NULL}
437};
438
439int    roar_str2autht(const char * str) {
440 int i;
441
442 for (i = 0; _g_authts[i].name != NULL; i++)
443  if ( !strcasecmp(_g_authts[i].name, str) )
444   return _g_authts[i].type;
445
446 return -1;
447}
448
449const char * roar_autht2str(const int auth) {
450 int i;
451
452 for (i = 0; _g_authts[i].name != NULL; i++)
453  if ( _g_authts[i].type == auth )
454   return _g_authts[i].name;
455
456 return "(UNKNOWN)";
457}
458
459//ll
Note: See TracBrowser for help on using the repository browser.