Debian Codegraph @

src/common/cockpitauthorize.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitauthorize.h"
9 #include "cockpitbase64.h"
10 #include "cockpitmemory.h"
11
12 #include <assert.h>
13 #include <ctype.h>
14 #include <crypt.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 /* ----------------------------------------------------------------------------
25 * Tools
26 */
27
28 #ifndef debug
29 #define debug(format, ...) \
30 do { if (logger_verbose) \
31 message ("debug: " format, ##__VA_ARGS__); \
32 } while (0)
33 #endif
34
35 static int logger_verbose = 0;
36 static void (* logger) (const char *data);
37
38 #ifndef message
39 #if __GNUC__ > 2
40 static void
41 message (const char *format, ...)
42 __attribute__((__format__(__printf__, 1, 2)));
43 #endif
44
45 static void
46 message (const char *format, ...)
47 {
48 va_list va;
49 char *data;
50 int res;
51
52 if (!logger)
53 return;
54
55 /* Fast path for simple messages */
56 if (!strchr (format, '%'))
57 {
58 logger (format);
59 return;
60 }
61
62 va_start (va, format);
63 res = vasprintf (&data, format, va);
64 va_end (va);
65
66 if (res < 0)
67 {
68 logger ("out of memory printing message");
69 return;
70 }
71
72 logger (data);
73 free (data);
74 }
75 #endif
76
77 void
78 cockpit_authorize_logger (void (* func) (const char *data),
79 int verbose)
80 {
81 logger_verbose = verbose;
82 logger = func;
83 }
84
85 void *
86 cockpit_authorize_nonce (size_t length)
87 {
88 unsigned char *key;
89 int errn = 0;
90 int fd;
91 ssize_t read_bytes;
92 ssize_t read_result;
93
94 fd = open ("/dev/urandom", O_RDONLY, 0);
95 if (fd < 0)
96 return NULL;
97
98 key = malloc (length);
99 if (!key)
100 {
101 close (fd);
102 errno = ENOMEM;
103 return NULL;
104 }
105
106 read_bytes = 0;
107 do
108 {
109 errno = 0;
110 read_result = read (fd, key + read_bytes, length - read_bytes);
111 if (read_result <= 0)
112 {
113 if (errno == EAGAIN || errno == EINTR)
114 continue;
115 errn = errno;
116 break;
117 }
118 read_bytes += read_result;
119 }
120 while (read_bytes < length);
121 close (fd);
122
123 if (read_bytes < length)
124 {
125 free (key);
126 key = NULL;
127 errno = errn;
128 }
129
130 return key;
131 }
132
133 const char *
134 cockpit_authorize_type (const char *challenge,
135 char **type)
136 {
137 size_t i, len = 0;
138
139 /*
140 * Either a space or a colon is the delimiter
141 * that splits the type from the remainder
142 * of the content.
143 */
144 if (challenge)
145 len = strcspn (challenge, ": ");
146 if (len == 0)
147 {
148 debug ("invalid \"authorize\" message");
149 errno = EINVAL;
150 return NULL;
151 }
152
153 if (type)
154 {
155 *type = strndup (challenge, len);
156 if (*type == NULL)
157 {
158 message ("couldn't allocate memory for \"authorize\" challenge");
159 errno = ENOMEM;
160 return NULL;
161 }
162 for (i = 0; i < len; i++)
163 (*type)[i] = tolower ((unsigned char) (*type)[i]);
164 }
165
166 if (challenge[len])
167 len++;
168 while (challenge[len] == ' ')
169 len++;
170 return challenge + len;
171 }
172
173 const char *
174 cockpit_authorize_subject (const char *challenge,
175 char **subject)
176 {
177 size_t len;
178
179 challenge = cockpit_authorize_type (challenge, NULL);
180 if (!challenge)
181 return NULL;
182
183 len = strcspn (challenge, ": ");
184 if (len == 0)
185 {
186 message ("invalid \"authorize\" message \"challenge\": no subject");
187 errno = EINVAL;
188 return NULL;
189 }
190
191 if (subject)
192 {
193 *subject = strndup (challenge, len);
194 if (!*subject)
195 {
196 message ("couldn't allocate memory for \"authorize\" message \"challenge\"");
197 errno = ENOMEM;
198 return NULL;
199 }
200 }
201
202 if (challenge[len])
203 len++;
204 while (challenge[len] == ' ')
205 len++;
206 return challenge + len;
207 }
208
209 char *
210 cockpit_authorize_parse_basic (const char *challenge,
211 char **user)
212 {
213 unsigned char *buf = NULL;
214 char *type = NULL;
215 size_t len;
216 ssize_t res;
217 int errn = 0;
218 size_t off;
219
220 challenge = cockpit_authorize_type (challenge, &type);
221 if (!challenge)
222 return NULL;
223
224 if (strcmp (type, "basic") != 0)
225 {
226 message ("invalid prefix in Basic header");
227 errn = EINVAL;
228 goto out;
229 }
230
231 len = strcspn (challenge, " ");
232 buf = malloc (len + 1);
233 if (!buf)
234 {
235 message ("couldn't allocate memory for Basic header");
236 errn = ENOMEM;
237 goto out;
238 }
239
240 /* No value */
241 if (len == 0)
242 {
243 buf[0] = 0;
244 if (user)
245 *user = NULL;
246 goto out;
247 }
248
249 /* Decode and find split point */
250 res = cockpit_base64_pton (challenge, len, buf, len);
251 if (res < 0)
252 {
253 message ("invalid base64 data in Basic header");
254 errn = EINVAL;
255 goto out;
256 }
257 assert (res <= len);
258 buf[res] = 0;
259
260 off = strcspn ((char *)buf, ":");
261 if (off == res)
262 {
263 message ("invalid base64 data in Basic header");
264 errn = EINVAL;
265 goto out;
266 }
267
268 if (user)
269 {
270 *user = strndup ((char *)buf, off);
271 if (!*user)
272 {
273 message ("couldn't allocate memory for user name");
274 errn = ENOMEM;
275 goto out;
276 }
277 }
278
279 memmove (buf, buf + off + 1, res - off);
280
281 out:
282 free (type);
283 if (errn != 0)
284 {
285 errno = errn;
286 free (buf);
287 buf = NULL;
288 }
289 return (char *)buf;
290 }
291
292 char *
293 cockpit_authorize_build_basic (const char *user,
294 const char *password)
295 {
296 char *content = NULL;
297 char *encoded = NULL;
298 char *response = NULL;
299 size_t elen, clen;
300 int errn = 0;
301
302 if (!user)
303 user = "";
304 if (!password)
305 password = "";
306
307 if (asprintf (&content, "%s:%s", user, password) < 0)
308 {
309 errn = errno;
310 message ("could not build basic response");
311 goto out;
312 }
313
314 clen = strlen (content);
315 elen = cockpit_base64_size (clen);
316 encoded = malloc (elen + 1);
317 if (!encoded)
318 {
319 errn = ENOMEM;
320 message ("could not allocate memory for basic response");
321 goto out;
322 }
323 if (cockpit_base64_ntop ((unsigned char *)content, clen, encoded, elen) < 0)
324 {
325 errn = errno;
326 message ("could not encode basic response");
327 goto out;
328 }
329
330 if (asprintf (&response, "Basic %s", encoded) < 0)
331 {
332 errn = errno;
333 message ("could not build basic response");
334 response = NULL;
335 goto out;
336 }
337
338 out:
339 free (encoded);
340 if (content)
341 cockpit_memory_clear (content, -1);
342 free (content);
343 if (!response)
344 errno = errn;
345 return response;
346 }
347
348 void *
349 cockpit_authorize_parse_negotiate (const char *challenge,
350 size_t *length)
351 {
352 unsigned char *buf = NULL;
353 size_t len;
354 ssize_t res;
355 int negotiate;
356 char *type;
357
358 challenge = cockpit_authorize_type (challenge, &type);
359 if (!challenge)
360 return NULL;
361
362 negotiate = strcmp (type, "negotiate") == 0;
363 free (type);
364
365 if (!negotiate)
366 {
367 message ("invalid prefix in Negotiate header");
368 errno = EINVAL;
369 return NULL;
370 }
371
372 len = strcspn (challenge, " ");
373 buf = malloc (len + 1);
374 if (!buf)
375 {
376 message ("couldn't allocate memory for Negotiate header");
377 errno = ENOMEM;
378 return NULL;
379 }
380
381 /* Decode data */
382 res = cockpit_base64_pton (challenge, len, buf, len);
383 if (res < 0)
384 {
385 message ("invalid base64 data in Negotiate header");
386 free (buf);
387 errno = EINVAL;
388 return NULL;
389 }
390
391 if (length)
392 *length = res;
393 return buf;
394 }
395
396 char *
397 cockpit_authorize_build_negotiate (const void *input,
398 size_t length)
399 {
400 char *encoded = NULL;
401 char *response = NULL;
402 size_t elen;
403 int errn = 0;
404
405 if (!input)
406 length = 0;
407
408 if (length > 0)
409 {
410 elen = cockpit_base64_size (length);
411 encoded = malloc (elen);
412 if (!encoded)
413 {
414 errn = ENOMEM;
415 message ("could not allocate memory for negotiate challenge");
416 goto out;
417 }
418 if (cockpit_base64_ntop ((unsigned char *)input, length, encoded, elen) < 0)
419 {
420 errn = errno;
421 message ("could not encode negotiate prompt");
422 goto out;
423 }
424 }
425
426 if (asprintf (&response, "Negotiate%s%s", encoded ? " " : "", encoded ? encoded : "") < 0)
427 {
428 errn = errno;
429 message ("could not build negotiate challenge");
430 response = NULL;
431 goto out;
432 }
433
434 out:
435 free (encoded);
436 if (!response)
437 errno = errn;
438 return response;
439 }
440
441 char *
442 cockpit_authorize_parse_x_conversation (const char *challenge,
443 char **conversation)
444 {
445 unsigned char *buf = NULL;
446 int x_conversation;
447 char *type;
448 size_t len;
449 ssize_t res;
450
451 if (!cockpit_authorize_type (challenge, &type))
452 return NULL;
453
454 x_conversation = strcmp (type, "x-conversation") == 0;
455 free (type);
456
457 if (!x_conversation)
458 {
459 message ("invalid prefix in X-Conversation header");
460 errno = EINVAL;
461 return NULL;
462 }
463
464 challenge = cockpit_authorize_subject (challenge, conversation);
465 if (!challenge)
466 return NULL;
467
468 len = strcspn (challenge, " ");
469 buf = malloc (len + 1);
470 if (!buf)
471 {
472 message ("couldn't allocate memory for X-Conversation header");
473 errno = ENOMEM;
474 return NULL;
475 }
476
477 res = cockpit_base64_pton (challenge, len, buf, len);
478 if (res < 0)
479 {
480 message ("invalid base64 data in X-Conversation header");
481 free (buf);
482 errno = EINVAL;
483 return NULL;
484 }
485
486 /* Null terminate the thing */
487 buf[res] = '\0';
488 return (char *)buf;
489 }
490
491 char *
492 cockpit_authorize_build_x_conversation (const char *prompt,
493 char **conversation)
494 {
495 const size_t nlen = 128;
496 unsigned char *nonce = NULL;
497 char *encoded = NULL;
498 char *response = NULL;
499 char *conv = NULL;
500 size_t plen, elen, clen;
501 char *alloc = NULL;
502 int errn = 0;
503
504 if (!prompt)
505 prompt = "";
506
507 /* Reuse a conversation we get */
508 if (conversation)
509 conv = *conversation;
510
511 if (!conv)
512 {
513 nonce = cockpit_authorize_nonce (nlen);
514 if (!nonce)
515 {
516 errn = errno;
517 message ("could not generate nonce");
518 goto out;
519 }
520
521 clen = cockpit_base64_size (nlen);
522 conv = alloc = malloc (clen);
523 if (!conv)
524 {
525 errn = ENOMEM;
526 message ("could not allocate memory for conversation");
527 goto out;
528 }
529 if (cockpit_base64_ntop (nonce, nlen, conv, clen) < 0)
530 {
531 errn = errno;
532 message ("could not encode conversation nonce");
533 goto out;
534 }
535 }
536
537 if (strlen (conv) == 0)
538 {
539 message ("invalid conversation nonce");
540 errn = EINVAL;
541 goto out;
542 }
543
544 plen = strlen (prompt);
545 if (plen > 0)
546 {
547 elen = cockpit_base64_size (plen);
548 encoded = malloc (elen);
549 if (!encoded)
550 {
551 errn = ENOMEM;
552 message ("could not allocate memory for conversation");
553 goto out;
554 }
555 if (cockpit_base64_ntop ((unsigned char *)prompt, plen, encoded, elen) < 0)
556 {
557 errn = errno;
558 message ("could not encode conversation prompt");
559 goto out;
560 }
561 }
562
563 if (asprintf (&response, "X-Conversation %s%s%s", conv, encoded ? " " : "", encoded ? encoded : "") < 0)
564 {
565 errn = errno;
566 message ("could not build conversation challenge");
567 response = NULL;
568 goto out;
569 }
570
571 if (conversation)
572 {
573 *conversation = conv;
574 conv = alloc = NULL;
575 }
576
577 out:
578 free (nonce);
579 free (alloc);
580 free (encoded);
581 if (!response)
582 errno = errn;
583 return response;
584 }