source: roaraudio/roard/auth.c @ 5146:716400712348

Last change on this file since 5146:716400712348 was 5146:716400712348, checked in by phi, 13 years ago

Moved error frame handling into proto functions (pr0)

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