PageRenderTime 85ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/roard/auth.c

https://github.com/roaraudio/roaraudio
C | 246 lines | 163 code | 52 blank | 31 comment | 41 complexity | f279e220e1283d1606c82bf2d023e9c6 MD5 | raw file
  1. //auth.c:
  2. /*
  3. * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
  4. *
  5. * This file is part of roard a part of RoarAudio,
  6. * a cross-platform sound system for both, home and professional use.
  7. * See README for details.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 3
  11. * as published by the Free Software Foundation.
  12. *
  13. * RoarAudio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this software; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. *
  23. */
  24. #include "roard.h"
  25. #define _NONE ROAR_AUTH_T_AUTO
  26. int auth_init (void) {
  27. int i;
  28. memset(g_auth_keyring, 0, sizeof(g_auth_keyring));
  29. for (i = 0; i < AUTH_KEYRING_LEN; i++) {
  30. g_auth_keyring[i].type = _NONE;
  31. }
  32. #if 0
  33. // test password for API tests...
  34. auth_addkey_password(ACCLEV_ALL, "test");
  35. #endif
  36. #if 0
  37. // test trust for API tests...
  38. auth_addkey_trust(ACCLEV_ALL, -1, 0, getuid()+1, -1, getgid()+1, -1);
  39. #endif
  40. return 0;
  41. }
  42. int auth_free (void) {
  43. return 0;
  44. }
  45. union auth_typeunion * auth_regkey_simple(int type, enum roard_client_acclev acclev) {
  46. struct auth_key * key;
  47. int i;
  48. for (i = 0; i < AUTH_KEYRING_LEN; i++) {
  49. if ( (key = &(g_auth_keyring[i]))->type == _NONE ) {
  50. memset(key, 0, sizeof(struct auth_key));
  51. key->type = type;
  52. key->acclev = acclev;
  53. return &(key->at_data);
  54. }
  55. }
  56. return NULL;
  57. }
  58. static int _ck_cookie(struct auth_key * key, struct roar_auth_message * authmes) {
  59. if ( key->at_data.cookie.len == authmes->len ) {
  60. if ( memcmp(key->at_data.cookie.cookie, authmes->data, authmes->len) ) {
  61. return -1;
  62. } else {
  63. return 1;
  64. }
  65. }
  66. return -1;
  67. }
  68. static int _ck_password(struct auth_key * key, struct roar_auth_message * authmes) {
  69. size_t len = strlen(key->at_data.password.password);
  70. // need to check here if we have a padding \0-byte.
  71. if ( len == authmes->len ) {
  72. if ( memcmp(key->at_data.password.password, authmes->data, len) ) {
  73. return -1;
  74. } else {
  75. return 1;
  76. }
  77. }
  78. return -1;
  79. }
  80. static int _ck_trust(struct auth_key * key, struct roar_auth_message * authmes, struct roar_client_server * cs) {
  81. struct at_trust * t = &(key->at_data.trust);
  82. size_t i;
  83. // we ship pids at the moment as cs does not contain a verifyed one.
  84. for (i = 0; i < t->uids_len; i++)
  85. if ( t->uids[i] == ROAR_CLIENT(cs)->uid )
  86. return 1;
  87. for (i = 0; i < t->gids_len; i++)
  88. if ( t->gids[i] == ROAR_CLIENT(cs)->gid )
  89. return 1;
  90. return -1;
  91. }
  92. int auth_client_ckeck(struct roar_client_server * cs, struct roar_auth_message * authmes) {
  93. struct auth_key * key;
  94. int i;
  95. int ret;
  96. if ( cs == NULL || authmes == NULL )
  97. return -1;
  98. for (i = 0; i < AUTH_KEYRING_LEN; i++) {
  99. if ( (key = &(g_auth_keyring[i]))->type == authmes->type ) {
  100. ret = -1;
  101. switch (key->type) {
  102. case ROAR_AUTH_T_NONE:
  103. ret = 1;
  104. break;
  105. case ROAR_AUTH_T_PASSWORD:
  106. ret = _ck_password(key, authmes);
  107. break;
  108. case ROAR_AUTH_T_COOKIE:
  109. ret = _ck_cookie(key, authmes);
  110. break;
  111. case ROAR_AUTH_T_TRUST:
  112. ret = _ck_trust(key, authmes, cs);
  113. break;
  114. case ROAR_AUTH_T_SYSUSER:
  115. case ROAR_AUTH_T_RHOST:
  116. default:
  117. /* don't know what to do... */
  118. return -1;
  119. break;
  120. }
  121. switch (ret) {
  122. case -1:
  123. /* ignore this case and continue */
  124. break;
  125. case 0:
  126. return 0;
  127. break;
  128. case 1:
  129. cs->acclev = key->acclev;
  130. return 1;
  131. break;
  132. default: /* error! */
  133. return -1;
  134. break;
  135. }
  136. }
  137. }
  138. return -1;
  139. }
  140. int auth_addkey_anonymous(enum roard_client_acclev acclev) {
  141. if ( auth_regkey_simple(ROAR_AUTH_T_NONE, acclev) == NULL )
  142. return -1;
  143. return 0;
  144. }
  145. int auth_addkey_password(enum roard_client_acclev acclev, const char * password) {
  146. union auth_typeunion * pw;
  147. if ( (pw = auth_regkey_simple(ROAR_AUTH_T_PASSWORD, acclev)) == NULL )
  148. return -1;
  149. pw->password.password = password;
  150. return 0;
  151. }
  152. int auth_addkey_cookie(enum roard_client_acclev acclev, const void * cookie, const size_t len) {
  153. union auth_typeunion * key;
  154. if ( (key = auth_regkey_simple(ROAR_AUTH_T_COOKIE, acclev)) == NULL )
  155. return -1;
  156. key->cookie.cookie = (void*)cookie;
  157. key->cookie.len = len;
  158. return 0;
  159. }
  160. int auth_addkey_trust(enum roard_client_acclev acclev, ...) {
  161. union auth_typeunion * key;
  162. size_t i;
  163. va_list va;
  164. pid_t pid;
  165. uid_t uid;
  166. gid_t gid;
  167. int err = 0;
  168. if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, acclev)) == NULL )
  169. return -1;
  170. // zerosize all counters.
  171. memset(key, 0, sizeof(union auth_typeunion));
  172. va_start(va, acclev);
  173. do { // eval block we can leave with continue.
  174. #define _block(var,type,array) i = 0; \
  175. do { \
  176. if ( i == AT_TRUST_MAX_ENTRYS ) { err = 1; continue; } \
  177. var = va_arg(va, type); \
  178. key->trust.array[i] = var; \
  179. i++; \
  180. } while (var != -1); \
  181. if ( err ) continue; \
  182. key->trust.array ## _len = i;
  183. _block(pid, pid_t, pids);
  184. _block(uid, uid_t, uids);
  185. _block(gid, gid_t, gids);
  186. #undef _block
  187. } while(0);
  188. va_end(va);
  189. if ( !err )
  190. return 0;
  191. memset(key, 0, sizeof(union auth_typeunion));
  192. return -1;
  193. }
  194. //ll