summaryrefslogtreecommitdiff
path: root/src/auth_distccd.c
blob: a26d168014a97a81372d98032e769da30813df40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* Copyright (C) 2008 CERN
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

/* Author: Ian Baker */

#include <config.h>

#ifdef HAVE_GSSAPI
#include <arpa/inet.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "auth.h"
#include "distcc.h"
#include "dopt.h"
#include "exitcode.h"
#include "netutil.h"
#include "trace.h"

/*Maximum length of principal name in black/white list.*/
#define MAX_NAME_LENGTH 50
/*Key not found during binary search*/
#define KEY_NOT_FOUND -1

static int dcc_gssapi_accept_secure_context(int to_net_sd,
					    int from_net_sd,
					    OM_uint32 *ret_flags,
					    char **principal);
static int dcc_gssapi_recv_handshake(int from_net_sd, int to_net_sd);
static int dcc_gssapi_check_list(char *principal, int sd);
static int dcc_gssapi_bin_search(char *key);
static int dcc_gssapi_notify_client(int sd, char status);
static int dcc_gssapi_compare_strings(const void *string_one,
				      const void *string_two);

/*Global credentials so they're only required and released once*/
/*in the most suitable place.*/
gss_cred_id_t creds;
/*Global security context in case other services*/
/*are implemented in the future.*/
gss_ctx_id_t distccd_ctx_handle = GSS_C_NO_CONTEXT;
/*Global sorted list of principal names from either a specified*/
/*blacklist or a whitelist available to all children*/
char **list = NULL;
/*Global count of the number of principal names in the sorted list.*/
int list_count = 0;

/*
 * Perform any requested security.
 *
 * @param to_net_sd.	Socket to write to.
 *
 * @param from_net_sd.	Socket to read from.
 *
 * @param ret_flags.	A representation of the services requested
 *			by the client.
 *
 * @param principal.	The name of the client principal.
 *
 * Returns 0 on success, otherwise error.
 */
int dcc_gssapi_check_client(int to_net_sd, int from_net_sd) {
    char *principal = NULL;
    int ret;
    OM_uint32 ret_flags;

    if ((ret = dcc_gssapi_accept_secure_context(to_net_sd,
					       from_net_sd,
					       &ret_flags,
					       &principal)) != 0) {
        return ret;
    }

    if ((ret = dcc_gssapi_compare_flags(GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG, ret_flags)) != 0) {
	dcc_gssapi_delete_ctx(&distccd_ctx_handle);
        return ret;
    }

    if (opt_blacklist_enabled || opt_whitelist_enabled) {
	rs_log_info("Checking %s against %slist %s.",
				principal,
				(opt_blacklist_enabled) ? "black" : "white",
				arg_list_file);
        if ((ret = dcc_gssapi_check_list(principal, to_net_sd)) != 0) {
	    dcc_gssapi_delete_ctx(&distccd_ctx_handle);
	    free(principal);
            return ret;
        }

	free(principal);
    } else {
	rs_log_info("Notifying client.");

	if ((ret = dcc_gssapi_notify_client(to_net_sd, ACCESS)) != 0) {
	    dcc_gssapi_delete_ctx(&distccd_ctx_handle);
	    return ret;
	}
    }

    return 0;
}

/*
 * Accept a secure context using the GSS-API.  A handshake is attempted
 * in order to detect a non-authenticating client.
 *
 * @param to_net_sd.	Socket to write to.
 *
 * @param from_net_sd.	Socket to read from.
 *
 * @param ret_flags.	A representation of the security services
 *			requested by the client to be returned to
 *			the invoking function.
 *
 * @param principal.	The name of the client principal to be returned
 *			to the invoking function.
 *
 * Returns 0 on success, otherwise error.
 */
static int dcc_gssapi_accept_secure_context(int to_net_sd,
					    int from_net_sd,
					    OM_uint32 *ret_flags,
					    char **principal) {
    gss_buffer_desc input_tok = GSS_C_EMPTY_BUFFER;
    gss_buffer_desc name_buffer = GSS_C_EMPTY_BUFFER;
    gss_buffer_desc output_tok = GSS_C_EMPTY_BUFFER;
    gss_name_t int_client_name;
    gss_OID name_type;
    int ret;
    OM_uint32 major_status, minor_status, return_status;

    input_tok.value = NULL;
    input_tok.length = 0;
    output_tok.value = NULL;
    output_tok.length = 0;

    if ((ret = dcc_gssapi_recv_handshake(from_net_sd, to_net_sd)) != 0) {
        return ret;
    }

    do {
            if ((ret = recv_token(from_net_sd, &input_tok)) != 0) {
		rs_log_error("Error receiving token.");
		rs_log_info("(eof may indicate a client error during ctx init).");

                return ret;
            }

            major_status = gss_accept_sec_context(&minor_status,
						  &distccd_ctx_handle,
						  creds,
						  &input_tok,
						  GSS_C_NO_CHANNEL_BINDINGS,
						  &int_client_name,
						  NULL,
						  &output_tok,
						  ret_flags,
						  NULL,
						  NULL);

            if (GSS_ERROR(major_status)) {
		        rs_log_crit("Failed to accept secure context.");
		        dcc_gssapi_status_to_log(major_status, GSS_C_GSS_CODE);
	            dcc_gssapi_status_to_log(minor_status, GSS_C_MECH_CODE);

                if ((return_status = gss_release_buffer(&minor_status,
						  &input_tok)) != GSS_S_COMPLETE) {
                    rs_log_error("Failed to release buffer.");
                }

	            return EXIT_GSSAPI_FAILED;
            }

            if (output_tok.length > 0) {
                if ((ret = send_token(to_net_sd,
				     &output_tok)) != 0) {
                    dcc_gssapi_cleanup(&input_tok,
				       &output_tok,
				       &int_client_name);
		    return ret;
                }

                if ((return_status = gss_release_buffer(&minor_status,
						  &output_tok)) != GSS_S_COMPLETE) {
                    rs_log_error("Failed to release buffer.");
                }
            }

            if (input_tok.length > 0) {
                if ((return_status = gss_release_buffer(&minor_status,
						  &input_tok)) != GSS_S_COMPLETE) {
                    rs_log_error("Failed to release buffer.");
                }
            }


    } while (major_status != GSS_S_COMPLETE);

    if ((major_status = gss_display_name(&minor_status,
					int_client_name,
					&name_buffer,
					&name_type)) != GSS_S_COMPLETE) {
        rs_log_error("Failed to convert name.");
    }

    rs_log_info("Successfully authenticated %s.", (char *) name_buffer.value);

    if ((*principal = malloc(strlen((char *) name_buffer.value) + 1 )) == NULL) {
        rs_log_error("malloc failed : %ld bytes: out of memory.",
                                        (long) (strlen((char *) name_buffer.value) + 1));
        return EXIT_OUT_OF_MEMORY;
    }

    strcpy(*principal, (char *) name_buffer.value);
    dcc_gssapi_cleanup(&input_tok, &output_tok, &int_client_name);

    return 0;
}

/*
 * Attempt handshake exchange with the client to indicate server's
 * desire to authentciate.
 *
 * @param from_net_sd.	Socket to read from.
 *
 * @param to_net_sd.	Socket to write to.
 *
 * Returns 0 on success, otherwise error.
 */
static int dcc_gssapi_recv_handshake(int from_net_sd, int to_net_sd) {
    char auth;
    int ret;

    rs_log_info("Receiving handshake.");

    if ((ret = dcc_readx(from_net_sd, &auth, sizeof(auth))) != 0) {
        return ret;
    }

    rs_log_info("Received %c.", auth);

    if (auth != HANDSHAKE) {
	rs_log_crit("No client handshake - did the client require authentication?");
	return EXIT_GSSAPI_FAILED;
    }

    rs_log_info("Sending handshake.");

    if ((ret = dcc_writex(to_net_sd, &auth, sizeof(auth))) != 0) {
        return ret;
    }

    rs_log_info("Sent %c.", auth);

    return 0;
}

/*
 * Check the name of the connecting client principal against the sorted
 * list of principal names using a binary search to determine access
 * rights depending upon the type of list used.  The client is then
 * notified of the outcome.
 *
 * @param principal.	The name of the connecting client principal.
 *
 * @param sd.		Socket to write notification to.
 *
 * Returns 0 on success, otherwise access deinied.
 */
static int dcc_gssapi_check_list(char *principal, int sd) {
    char *pos = NULL;
    int location, ret;

    if ((pos = strchr(principal, '@')) != NULL) {
        *pos = '\0';
    }

    location = dcc_gssapi_bin_search(principal);

    if (opt_blacklist_enabled) {	/*blacklist*/
        if (location >= 0) {
	        rs_log_info("Access denied - %s blacklisted.", principal);
	        rs_log_info("Notifying client.");
	        dcc_gssapi_notify_client(sd, NO_ACCESS);
	        return EXIT_GSSAPI_FAILED;
	    } else {
	        rs_log_info("Access granted - %s not blacklisted.", principal);
	        rs_log_info("Notifying client.");

	        if ((ret = dcc_gssapi_notify_client(sd, ACCESS)) != 0) {
	            return ret;
	        }

	        return 0;
	    }
    } else {	/*whitelist*/
	    if (location >= 0) {
	        rs_log_info("Access granted - %s whitelisted.", principal);
	        rs_log_info("Notifying client.");

            if ((ret = dcc_gssapi_notify_client(sd, ACCESS)) != 0) {
	            return ret;
	        }

	        return 0;
	    } else {
	        rs_log_info("Access denied - %s not whitelisted.", principal);
	        rs_log_info("Notifying client.");
	        dcc_gssapi_notify_client(sd, NO_ACCESS);
	        return EXIT_GSSAPI_FAILED;
	    }
    }
}

/*
 * Perform a binary search on a sorted list for a key.
 *
 * @param key.	The search key.
 *
 * Returns index if key in list, otherwise
 * KEY_NOT_FOUND condition.
 */
static int dcc_gssapi_bin_search(char *key) {
    int bottom = 0;
    int middle, res;
    int top = list_count - 1;

    while (bottom <= top) {
        middle = (bottom + top) / 2;
        res = strcmp(key, list[middle]);

        if (res < 0) {
            top = middle - 1;
        } else if (res > 0) {
            bottom = middle + 1;
        } else {
            return middle;
        }
    }

    return KEY_NOT_FOUND;
}

/*
 * Send notification of access/no access to client.
 *
 * @param sd.		Socket to write notification to.
 *
 * @param status.	Status of access request.
 *			Either 'y' or 'n'
 *
 * Returns 0 on success, otherwise error.
 */
static int dcc_gssapi_notify_client(int sd, char status) {
    int ret;

    if ((ret = dcc_writex(sd, &status, sizeof(status))) != 0) {
	rs_log_crit("Failed to notify client.");
	return ret;
    }

    return 0;
}

/*
 * Acquire credentials for the distccd daemon.  We attempt to extract
 * the server principal name from the environment and ascertain the
 * name type.
 *
 * Returns 0 on success, otherwise error.
 */
int dcc_gssapi_acquire_credentials(void) {
    char *princ_env_val = NULL;
    gss_buffer_desc name_buffer = GSS_C_EMPTY_BUFFER;
    gss_name_t int_princ_name;
    gss_OID name_type;
    OM_uint32 major_status, minor_status;

    princ_env_val = getenv("DISTCCD_PRINCIPAL");

    if (princ_env_val == NULL) {
        rs_log_error("No principal name specified.");
        return EXIT_GSSAPI_FAILED;
    }

    if (strchr(princ_env_val, '@') != NULL) {
        name_type = GSS_C_NT_HOSTBASED_SERVICE;
    } else {
        name_type = GSS_C_NT_USER_NAME;
    }

    name_buffer.value = princ_env_val;
    name_buffer.length = strlen(princ_env_val);

    rs_log_info("Acquiring credentials.");

    name_buffer.length = strlen(name_buffer.value);

    if ((major_status = gss_import_name(&minor_status,
				       &name_buffer,
				       name_type,
				       &int_princ_name)) != GSS_S_COMPLETE) {
	rs_log_error("Failed to import princ name (%s) to internal GSS-API format.",
							(char *) name_buffer.value);
        return EXIT_GSSAPI_FAILED;
    }

    major_status = gss_acquire_cred(&minor_status,
                                    int_princ_name,
                                    0,
                                    GSS_C_NO_OID_SET,
                                    GSS_C_ACCEPT,
                                    &creds,
                                    NULL,
                                    NULL);

    if (major_status != GSS_S_COMPLETE) {
        rs_log_crit("Failed to acquire credentials.");
        dcc_gssapi_status_to_log(major_status, GSS_C_GSS_CODE);
	    dcc_gssapi_status_to_log(minor_status, GSS_C_MECH_CODE);

	    if ((major_status = gss_release_name(&minor_status,
					    &int_princ_name)) != GSS_S_COMPLETE) {
	        rs_log_error("Failed to release GSS-API buffer.");
        }

        return EXIT_GSSAPI_FAILED;
    }

    if ((major_status = gss_release_name(&minor_status,
					&int_princ_name)) != GSS_S_COMPLETE) {
	rs_log_error("Failed to release GSS-API buffer.");
    }

    rs_log_info("Credentials successfully acquired for %s.",
						(char *) name_buffer.value);

    name_buffer.value = NULL;

    if ((major_status = gss_release_buffer(&minor_status,
					  &name_buffer)) != GSS_S_COMPLETE) {
        rs_log_error("Failed to release GSS-API buffer.");
    }

    return 0;
}

/*
 * Release acquired credentials.
 */
void dcc_gssapi_release_credentials(void) {
    OM_uint32 major_status, minor_status;

    if ((major_status = gss_release_cred(&minor_status,
					&creds)) != GSS_S_COMPLETE) {
	rs_log_error("Failed to release credentials.");
    }

    rs_log_info("Credentials released successfully.");
}

/*
 * Read the set of principal names from the specified file
 * to the list global variable and apply a qsort.  If the
 * list file can not be opened we exit with error as a
 * requested security feature can not be implemented.
 *
 * @param mode.	Indicates the type of the list, either
 *		black or white.  Used for the log file.
 *
 * Returns 0 on success, otherwise error.
 */
int dcc_gssapi_obtain_list(int mode) {
    char **head = NULL;
    char *line = NULL;
    char *pos = NULL;
    FILE *file;
    int ret;
    size_t length = 0;
    ssize_t read;

    if (!(file = fopen(arg_list_file, "r"))) {
        rs_log_error("Failed to open list file: %s: %s.", arg_list_file,
                                                        strerror(errno));
        return EXIT_GSSAPI_FAILED;
    }

    rs_log_info("Using file %s as a %slist.", arg_list_file,
					                        (mode) ? "black" : "white");

    while ((read = getline(&line, &length, file)) != -1) {
        list_count++;
    }

    if ((ret = fseek(file, 0, SEEK_SET)) != 0) {
        rs_log_error("fseek failed: %s.", strerror(errno));

        /* If seeking to the start of the file fails,
         * try achieving the same effect by closing and reopening the file. */

	    if ((ret = fclose(file)) != 0) {
            rs_log_error("fclose failed: %s.", strerror(errno));
        }

	    if (!(file = fopen(arg_list_file, "r"))) {
            rs_log_error("Failed to open list file: %s: %s.", arg_list_file,
							                               strerror(errno));
            return EXIT_GSSAPI_FAILED;
        }
    }

    if ((list = malloc(list_count * sizeof(char *))) == NULL) {
        rs_log_error("malloc failed : %ld bytes: out of memory.",
                                        (long) (list_count * sizeof(char *)));
        return EXIT_OUT_OF_MEMORY;
    }

    head = list;

    while ((getline(&line, &length, file)) != -1) {
        if ((pos = strchr(line, '\n')) != NULL) {
            *pos = '\0';
        }

	    if ((*list = malloc(strlen(line) + 1)) == NULL) {
            rs_log_error("malloc failed : %ld bytes: out of memory.",
                                            (long) (strlen(line) + 1));
            return EXIT_OUT_OF_MEMORY;
        }

	    strcpy(*list, line);
	    list++;
    }

    list = head;
    qsort(list, list_count, sizeof(char *), dcc_gssapi_compare_strings);

    if ((ret = fclose(file)) != 0) {
        rs_log_error("fclose failed: %s.", strerror(errno));
    }

    free(line);

    return 0;
}

/*
 * Comparison function used by qsort, simply compares
 * the two specified array elements containing two
 * principal names.
 *
 * @param string_one.	First element to be compared.
 *
 * @param string_two.	Second element to be compared.
 *
 * Returns the result of the comparison.
 */
static int dcc_gssapi_compare_strings(const void *string_one,
				                      const void *string_two) {
    const char **s1 = (const char **) string_one;
    const char **s2 = (const char **) string_two;

    return strcmp(*s1, *s2);
}

/*
 * Free the dynamically allocated memory used by the
 * list global variable.  First free the individual
 * strings then the array itself.
 */
void dcc_gssapi_free_list(void) {
    int i;

    for (i = 0; i < list_count; i++) {
        free(list[i]);
    }

    free(list);
}