source: roaraudio/roard/auth.c @ 4478:b5ee5b9431ae

Last change on this file since 4478:b5ee5b9431ae was 4478:b5ee5b9431ae, checked in by phi, 14 years ago

implemented trust and cookie auth

File size: 5.6 KB
Line 
1//auth.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of roard 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 *  RoarAudio 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 */
25
26#include "roard.h"
27
28#define _NONE ROAR_AUTH_T_AUTO
29
30int auth_init (void) {
31 int i;
32
33 memset(g_auth_keyring, 0, sizeof(g_auth_keyring));
34
35 for (i = 0; i < AUTH_KEYRING_LEN; i++) {
36  g_auth_keyring[i].type = _NONE;
37 }
38
39 // enable guest access.
40 if ( auth_addkey_anonymous(ACCLEV_ALL) == -1 )
41  return -1;
42
43#if 0
44 // test password for API tests...
45 auth_addkey_password(ACCLEV_ALL, "test");
46#endif
47
48#if 0
49 // test trust for API tests...
50 auth_addkey_trust(ACCLEV_ALL, -1, 0, getuid()+1, -1, getgid()+1, -1);
51#endif
52
53 return 0;
54}
55
56int auth_free (void) {
57 return 0;
58}
59
60union auth_typeunion * auth_regkey_simple(int type, enum roard_client_acclev acclev) {
61 struct auth_key * key;
62 int i;
63
64 for (i = 0; i < AUTH_KEYRING_LEN; i++) {
65  if ( (key = &(g_auth_keyring[i]))->type == _NONE ) {
66   memset(key, 0, sizeof(struct auth_key));
67   key->type   = type;
68   key->acclev = acclev;
69   return &(key->at_data);
70  }
71 }
72
73 return NULL;
74}
75
76static int _ck_cookie(struct auth_key * key, struct roar_auth_message * authmes) {
77 if ( key->at_data.cookie.len == authmes->len ) {
78  if ( memcmp(key->at_data.cookie.cookie, authmes->data, authmes->len) ) {
79   return -1;
80  } else {
81   return 1;
82  }
83 }
84
85 return -1;
86}
87
88static int _ck_password(struct auth_key * key, struct roar_auth_message * authmes) {
89 size_t len = strlen(key->at_data.password.password);
90
91 // need to check here if we have a padding \0-byte.
92
93 if ( len == authmes->len ) {
94  if ( memcmp(key->at_data.password.password, authmes->data, len) ) {
95   return -1;
96  } else {
97   return 1;
98  }
99 }
100
101 return -1;
102}
103
104static int _ck_trust(struct auth_key * key, struct roar_auth_message * authmes, struct roar_client_server * cs) {
105 struct at_trust * t = &(key->at_data.trust);
106 size_t i;
107
108 // we ship pids at the moment as cs does not contain a verifyed one.
109
110 for (i = 0; i < t->uids_len; i++)
111  if ( t->uids[i] == ROAR_CLIENT(cs)->uid )
112   return 1;
113
114 for (i = 0; i < t->gids_len; i++)
115  if ( t->gids[i] == ROAR_CLIENT(cs)->gid )
116   return 1;
117
118 return -1;
119}
120
121int auth_client_ckeck(struct roar_client_server * cs, struct roar_auth_message * authmes) {
122 struct auth_key * key;
123 int i;
124 int ret;
125
126 if ( cs == NULL || authmes == NULL )
127  return -1;
128
129 for (i = 0; i < AUTH_KEYRING_LEN; i++) {
130  if ( (key = &(g_auth_keyring[i]))->type == authmes->type ) {
131    ret = -1;
132   switch (key->type) {
133    case ROAR_AUTH_T_NONE:
134      ret = 1;
135     break;
136    case ROAR_AUTH_T_PASSWORD:
137      ret = _ck_password(key, authmes);
138     break;
139    case ROAR_AUTH_T_COOKIE:
140      ret = _ck_cookie(key, authmes);
141     break;
142    case ROAR_AUTH_T_TRUST:
143      ret = _ck_trust(key, authmes, cs);
144     break;
145    case ROAR_AUTH_T_SYSUSER:
146    case ROAR_AUTH_T_RHOST:
147    default:
148      /* don't know what to do... */
149      return -1;
150     break;
151   }
152   switch (ret) {
153    case -1:
154      /* ignore this case and continue */
155     break;
156    case 0:
157      return 0;
158     break;
159    case 1:
160      cs->acclev = key->acclev;
161      return 1;
162     break;
163    default: /* error! */
164      return -1;
165     break;
166   }
167  }
168 }
169
170 return -1;
171}
172
173int auth_addkey_anonymous(enum roard_client_acclev acclev) {
174 if ( auth_regkey_simple(ROAR_AUTH_T_NONE, acclev) == NULL )
175  return -1;
176 return 0;
177}
178
179int auth_addkey_password(enum roard_client_acclev acclev, const char * password) {
180 union auth_typeunion * pw;
181
182 if ( (pw = auth_regkey_simple(ROAR_AUTH_T_PASSWORD, acclev)) == NULL )
183  return -1;
184
185 pw->password.password = password;
186
187 return 0;
188}
189
190int auth_addkey_cookie(enum roard_client_acclev acclev, const void * cookie, const size_t len) {
191 union auth_typeunion * key;
192
193 if ( (key = auth_regkey_simple(ROAR_AUTH_T_COOKIE, acclev)) == NULL )
194  return -1;
195
196 key->cookie.cookie = (void*)cookie;
197 key->cookie.len    = len;
198
199 return 0;
200}
201
202int auth_addkey_trust(enum roard_client_acclev acclev, ...) {
203 union auth_typeunion * key;
204 size_t i;
205 va_list va;
206 pid_t pid;
207 uid_t uid;
208 gid_t gid;
209 int err = 0;
210
211 if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, acclev)) == NULL )
212  return -1;
213
214 // zerosize all counters.
215 memset(key, 0, sizeof(union auth_typeunion));
216
217 va_start(va, acclev);
218
219 do { // eval block we can leave with continue.
220
221#define _block(var,type,array)  i = 0; \
222                                do { \
223                                 if ( i == AT_TRUST_MAX_ENTRYS ) { err = 1; continue; } \
224                                 var = va_arg(va, type); \
225                                 key->trust.array[i] = var; \
226                                 i++; \
227                                } while (var != -1); \
228                                if ( err ) continue; \
229                                key->trust.array ## _len = i;
230
231
232 _block(pid, pid_t, pids);
233 _block(uid, uid_t, uids);
234 _block(gid, gid_t, gids);
235
236#undef _block
237
238 } while(0);
239
240 va_end(va);
241
242 if ( !err )
243  return 0;
244
245 memset(key, 0, sizeof(union auth_typeunion));
246
247 return -1;
248}
249
250//ll
Note: See TracBrowser for help on using the repository browser.