[0] | 1 | //auth.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[690] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[690] | 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 |
---|
[3740] | 32 | * and are therefore GPL. Because of this it may be illegal to use |
---|
[690] | 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
[0] | 36 | #include "libroar.h" |
---|
| 37 | |
---|
[3220] | 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) |
---|
[3740] | 47 | * 6) check if we got an OK or an ERROR, return correct value |
---|
[3220] | 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. |
---|
[3227] | 63 | * if the server sends an zero size message back it means the server accepted our connection |
---|
| 64 | * and no additional stage is needed. |
---|
[3220] | 65 | * if the message type is OK the server accepted our auth. |
---|
[3740] | 66 | * if the message type is ERROR the server rejected us. we may try other auth methods. |
---|
[3220] | 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. |
---|
[3740] | 82 | * if a cookie is wrong the server rejects us or asks us for another. |
---|
[3220] | 83 | * The cookie is send as binary data in the data block. |
---|
| 84 | * |
---|
| 85 | * --- TRUST: |
---|
[3740] | 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 |
---|
[3220] | 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: |
---|
[3740] | 93 | * This is technically the same as COOKIE just that the cookie is limited to |
---|
[3220] | 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. |
---|
[3740] | 102 | * future versions may define other types. |
---|
| 103 | * the second part is the actual data block. |
---|
[3220] | 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 | |
---|
[3227] | 130 | static int roar_auth_ask_server (struct roar_connection * con, struct roar_auth_message * authmes) { |
---|
[4745] | 131 | struct roar_error_frame error_frame; |
---|
| 132 | struct roar_message mes; |
---|
| 133 | char * header = mes.data; |
---|
| 134 | int ret; |
---|
[0] | 135 | |
---|
[131] | 136 | memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 137 | |
---|
[0] | 138 | mes.cmd = ROAR_CMD_AUTH; |
---|
[3227] | 139 | mes.datalen = 4; |
---|
| 140 | |
---|
| 141 | header[0] = authmes->type; |
---|
| 142 | header[1] = authmes->stage; |
---|
| 143 | header[2] = authmes->reserved.c[0]; |
---|
| 144 | header[3] = authmes->reserved.c[1]; |
---|
| 145 | |
---|
[4745] | 146 | if ( (ret = roar_req(con, &mes, NULL)) == -1 ) { |
---|
| 147 | authmes->type = -1; |
---|
[3227] | 148 | return -1; |
---|
[4745] | 149 | } |
---|
[3227] | 150 | |
---|
[4745] | 151 | if ( mes.cmd != ROAR_CMD_OK ) { |
---|
| 152 | ret = -1; |
---|
| 153 | if ( roar_err_parsemsg(&mes, &error_frame) == -1 ) { |
---|
| 154 | authmes->type = -1; |
---|
| 155 | return -1; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | header = error_frame.data; |
---|
| 159 | mes.datalen = error_frame.datalen; |
---|
| 160 | } |
---|
[4473] | 161 | |
---|
[3227] | 162 | if ( mes.datalen < 4 ) { |
---|
| 163 | memset(header+mes.datalen, 0, 4-mes.datalen); |
---|
[4745] | 164 | authmes->type = -1; |
---|
| 165 | } else { |
---|
| 166 | authmes->type = header[0]; |
---|
[3227] | 167 | } |
---|
| 168 | |
---|
| 169 | authmes->stage = header[1]; |
---|
| 170 | authmes->reserved.c[0] = header[2]; |
---|
| 171 | authmes->reserved.c[1] = header[3]; |
---|
| 172 | |
---|
[4745] | 173 | return ret; |
---|
[3227] | 174 | } |
---|
[0] | 175 | |
---|
[3227] | 176 | static void roar_auth_mes_init(struct roar_auth_message * authmes, int type) { |
---|
| 177 | memset(authmes, 0, sizeof(struct roar_auth_message)); |
---|
| 178 | |
---|
| 179 | authmes->type = type; |
---|
| 180 | authmes->stage = 0; |
---|
| 181 | authmes->data = NULL; |
---|
| 182 | authmes->len = 0; |
---|
| 183 | } |
---|
| 184 | |
---|
[4475] | 185 | |
---|
[4745] | 186 | static int try_password (struct roar_connection * con, int * next) { |
---|
[4475] | 187 | struct roar_message mes; |
---|
| 188 | struct roar_auth_message authmes; |
---|
| 189 | char * pw; |
---|
| 190 | |
---|
[4745] | 191 | // TODO: add support for *next. |
---|
| 192 | |
---|
[4475] | 193 | roar_auth_mes_init(&authmes, ROAR_AUTH_T_PASSWORD); |
---|
| 194 | |
---|
| 195 | if ( roar_passwd_simple_ask_pw(&pw, "Password for RoarAudio Server?", NULL) == -1 ) { |
---|
| 196 | return -1; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | authmes.len = strlen(pw); |
---|
| 200 | |
---|
| 201 | if ( roar_auth_init_mes(&mes, &authmes) == -1 ) { |
---|
| 202 | roar_mm_free(pw); |
---|
| 203 | return -1; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | // do not use strcpy() because that would copy \0, too. |
---|
| 207 | memcpy(authmes.data, pw, authmes.len); |
---|
| 208 | |
---|
| 209 | roar_mm_free(pw); |
---|
| 210 | |
---|
| 211 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
| 212 | return -1; |
---|
| 213 | |
---|
| 214 | if ( mes.cmd != ROAR_CMD_OK ) |
---|
| 215 | return -1; |
---|
| 216 | |
---|
| 217 | if ( roar_auth_from_mes(&authmes, &mes, NULL) == -1 ) |
---|
| 218 | return -1; |
---|
| 219 | |
---|
| 220 | if ( authmes.stage == 0 ) |
---|
| 221 | return 0; |
---|
| 222 | |
---|
| 223 | return -1; |
---|
| 224 | } |
---|
| 225 | |
---|
[3228] | 226 | #define _EOL ROAR_AUTH_T_AUTO |
---|
[3227] | 227 | int roar_auth (struct roar_connection * con) { |
---|
| 228 | struct roar_auth_message authmes; |
---|
| 229 | int ret; |
---|
[3228] | 230 | int i; |
---|
[4745] | 231 | int cur, next; |
---|
| 232 | int done; |
---|
[3228] | 233 | int ltt[] = { |
---|
| 234 | ROAR_AUTH_T_TRUST, |
---|
| 235 | ROAR_AUTH_T_IDENT, |
---|
| 236 | ROAR_AUTH_T_RHOST, |
---|
[4486] | 237 | // ROAR_AUTH_T_PASSWORD, |
---|
[3228] | 238 | ROAR_AUTH_T_NONE, |
---|
| 239 | _EOL |
---|
| 240 | }; |
---|
[3227] | 241 | |
---|
[3228] | 242 | for (i = 0; ltt[i] != _EOL; i++) { |
---|
[4745] | 243 | next = ltt[i]; |
---|
| 244 | while (next != -1) { |
---|
| 245 | done = 1; |
---|
| 246 | |
---|
| 247 | cur = next; |
---|
| 248 | next = -1; |
---|
| 249 | |
---|
| 250 | switch (cur) { |
---|
| 251 | case ROAR_AUTH_T_PASSWORD: |
---|
| 252 | if ( (ret = try_password(con, &next)) == -1 ) |
---|
| 253 | done = 0; |
---|
| 254 | break; |
---|
| 255 | case ROAR_AUTH_T_TRUST: |
---|
| 256 | case ROAR_AUTH_T_IDENT: |
---|
| 257 | case ROAR_AUTH_T_RHOST: |
---|
| 258 | case ROAR_AUTH_T_NONE: |
---|
| 259 | roar_auth_mes_init(&authmes, ltt[i]); |
---|
| 260 | if ( (ret = roar_auth_ask_server(con, &authmes)) == -1 ) |
---|
| 261 | done = 0; |
---|
| 262 | |
---|
| 263 | next = authmes.type; |
---|
[4475] | 264 | break; |
---|
[4745] | 265 | default: /* Bad error! */ |
---|
| 266 | return -1; |
---|
| 267 | break; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | if ( authmes.stage != 0 ) |
---|
| 271 | done = 0; |
---|
| 272 | |
---|
| 273 | if ( done ) |
---|
| 274 | return 0; |
---|
[4475] | 275 | } |
---|
[3228] | 276 | } |
---|
| 277 | |
---|
| 278 | return -1; |
---|
[0] | 279 | } |
---|
| 280 | |
---|
[4470] | 281 | |
---|
| 282 | int roar_auth_from_mes(struct roar_auth_message * ames, struct roar_message * mes, void * data) { |
---|
| 283 | void * ibuf; |
---|
| 284 | char header[4] = {0, 0, 0, 0}; |
---|
| 285 | |
---|
| 286 | if ( ames == NULL || mes == NULL ) |
---|
| 287 | return -1; |
---|
| 288 | |
---|
[4471] | 289 | if ( data != NULL ) { |
---|
[4470] | 290 | ibuf = data; |
---|
| 291 | } else { |
---|
| 292 | ibuf = mes->data; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | memset(ames, 0, sizeof(struct roar_auth_message)); |
---|
| 296 | |
---|
| 297 | memcpy(header, ibuf, mes->datalen < 4 ? mes->datalen : 4); |
---|
| 298 | |
---|
| 299 | ames->type = header[0]; |
---|
| 300 | ames->stage = header[1]; |
---|
| 301 | ames->reserved.c[0] = header[2]; |
---|
| 302 | ames->reserved.c[1] = header[3]; |
---|
| 303 | |
---|
| 304 | if ( mes->datalen > 4 ) { |
---|
| 305 | ames->data = ibuf + 4; |
---|
| 306 | ames->len = mes->datalen - 4; |
---|
| 307 | } else { |
---|
| 308 | ames->data = NULL; |
---|
| 309 | ames->len = 0; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | int roar_auth_to_mes(struct roar_message * mes, void ** data, struct roar_auth_message * ames) { |
---|
| 316 | char * obuf; |
---|
| 317 | |
---|
| 318 | if ( mes == NULL || ames == NULL ) |
---|
| 319 | return -1; |
---|
| 320 | |
---|
| 321 | if ( data != NULL ) |
---|
| 322 | *data = NULL; |
---|
| 323 | |
---|
| 324 | memset(mes, 0, sizeof(struct roar_message)); |
---|
| 325 | |
---|
[4475] | 326 | mes->cmd = ROAR_CMD_AUTH; |
---|
| 327 | |
---|
[4470] | 328 | if ( (ames->len + 4) > sizeof(mes->data) ) { |
---|
| 329 | *data = malloc(ames->len + 4); |
---|
| 330 | if ( *data == NULL ) |
---|
| 331 | return -1; |
---|
| 332 | obuf = *data; |
---|
| 333 | } else { |
---|
| 334 | obuf = mes->data; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | obuf[0] = ames->type; |
---|
| 338 | obuf[1] = ames->stage; |
---|
| 339 | obuf[2] = ames->reserved.c[0]; |
---|
| 340 | obuf[3] = ames->reserved.c[1]; |
---|
| 341 | |
---|
| 342 | memcpy(obuf + 8, ames->data, ames->len); |
---|
| 343 | |
---|
| 344 | mes->datalen = ames->len + 4; |
---|
| 345 | |
---|
| 346 | return 0; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | int roar_auth_init_mes(struct roar_message * mes, struct roar_auth_message * ames) { |
---|
| 350 | if ( mes == NULL || ames == NULL ) |
---|
| 351 | return -1; |
---|
| 352 | |
---|
| 353 | if ( (ames->len + 4) > sizeof(mes->data) ) |
---|
| 354 | return -1; |
---|
| 355 | |
---|
| 356 | memset(mes, 0, sizeof(struct roar_message)); |
---|
| 357 | |
---|
[4475] | 358 | mes->cmd = ROAR_CMD_AUTH; |
---|
| 359 | |
---|
[4470] | 360 | mes->data[0] = ames->type; |
---|
| 361 | mes->data[1] = ames->stage; |
---|
| 362 | mes->data[2] = ames->reserved.c[0]; |
---|
| 363 | mes->data[3] = ames->reserved.c[1]; |
---|
| 364 | |
---|
| 365 | ames->data = &(mes->data[4]); |
---|
| 366 | |
---|
| 367 | mes->datalen = ames->len + 4; |
---|
| 368 | |
---|
| 369 | return 0; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | |
---|
[3225] | 374 | // String functions: |
---|
| 375 | static struct { |
---|
| 376 | int type; |
---|
[4296] | 377 | const char * name; |
---|
[3225] | 378 | } _g_authts[] = { |
---|
| 379 | // 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 |
---|
| 380 | {ROAR_AUTH_T_NONE, "none" }, |
---|
| 381 | {ROAR_AUTH_T_COOKIE, "cookie" }, |
---|
| 382 | {ROAR_AUTH_T_TRUST, "trust" }, |
---|
| 383 | {ROAR_AUTH_T_PASSWORD, "password"}, |
---|
| 384 | {ROAR_AUTH_T_SYSUSER, "sysuser" }, |
---|
| 385 | {ROAR_AUTH_T_OPENPGP_SIGN, "openpgp" }, |
---|
| 386 | {ROAR_AUTH_T_OPENPGP_ENCRYPT, "openpgp" }, |
---|
| 387 | {ROAR_AUTH_T_OPENPGP_AUTH, "openpgp" }, |
---|
| 388 | {ROAR_AUTH_T_KERBEROS, "kerberos"}, |
---|
| 389 | {ROAR_AUTH_T_RHOST, "rhost" }, |
---|
| 390 | {ROAR_AUTH_T_XAUTH, "xauth" }, |
---|
| 391 | {ROAR_AUTH_T_IDENT, "ident" }, |
---|
| 392 | {-1, NULL} |
---|
| 393 | }; |
---|
| 394 | |
---|
[4296] | 395 | int roar_str2autht(const char * str) { |
---|
[3225] | 396 | int i; |
---|
| 397 | |
---|
| 398 | for (i = 0; _g_authts[i].name != NULL; i++) |
---|
| 399 | if ( !strcasecmp(_g_authts[i].name, str) ) |
---|
| 400 | return _g_authts[i].type; |
---|
| 401 | |
---|
| 402 | return -1; |
---|
| 403 | } |
---|
| 404 | |
---|
[4296] | 405 | const char * roar_autht2str(const int auth) { |
---|
[3225] | 406 | int i; |
---|
| 407 | |
---|
| 408 | for (i = 0; _g_authts[i].name != NULL; i++) |
---|
| 409 | if ( _g_authts[i].type == auth ) |
---|
| 410 | return _g_authts[i].name; |
---|
| 411 | |
---|
| 412 | return "(UNKNOWN)"; |
---|
| 413 | } |
---|
| 414 | |
---|
[0] | 415 | //ll |
---|