summaryrefslogtreecommitdiffstats
path: root/lib/libutil/login_auth.c
blob: 695b006b68639d2059888b4ac8812ecc11e89ffc (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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*-
 * Copyright (c) 1996 by
 * Sean Eric Fagan <sef@kithrup.com>
 * David Nugent <davidn@blaze.net.au>
 * All rights reserved.
 *
 * Portions copyright (c) 1995,1997 by
 * Berkeley Software Design, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. This work was done expressly for inclusion into FreeBSD.  Other use
 *    is permitted provided this notation is included.
 * 4. Absolutely no warranty of function or purpose is made by the authors.
 * 5. Modifications may be freely made to this file providing the above
 *    conditions are met.
 *
 * Low-level routines relating to the user capabilities database
 *
 *	$Id: login_auth.c,v 1.6 1997/02/22 15:08:18 peter Exp $
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <login_cap.h>
#include <stdarg.h>
#include <paths.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <err.h>
#include <libutil.h>

#ifdef	LOGIN_CAP_AUTH
/*
 * Comment from BSDI's authenticate.c module:
 * NOTE: THIS MODULE IS TO BE DEPRECATED.  FUTURE VERSIONS OF BSD/OS WILL
 * HAVE AN UPDATED API, THOUGH THESE FUNCTIONS WILL CONTINUE TO BE AVAILABLE
 * FOR BACKWARDS COMPATABILITY
 */


#define AUTHMAXSPOOL	(8 * 1024) /* Max size of authentication data */
#define	AUTHCOMM_FD	3	   /* Handle used to read/write auth data */

struct rmfiles {
    struct rmfiles  *next;
    char	    file[1];
};

struct authopts {
    struct authopts *next;
    char	    opt[1];
};

static char *spoolbuf = NULL;
static int spoolidx = 0;
static struct rmfiles *rmfirst = NULL;
static struct authopts *optfirst = NULL;


/*
 * Setup a known environment for all authentication scripts.
 */

static char *auth_environ[] = {
    "PATH=" _PATH_DEFPATH,
    "SHELL=" _PATH_BSHELL,
    NULL,
};



/*
 * nextline()
 * Get the next line from the data buffer collected from
 * the authentication program. This function relies on the
 * fact that lines are nul terminated.
 */

static char *
nextline(int *idx)
{
    char    *ptr = NULL;

    if (spoolbuf != NULL && *idx < spoolidx) {
	ptr = spoolbuf + *idx;
	*idx += strlen(ptr) + 1;
    }
    return ptr;
}


/*
 * spooldata()
 * Read data returned on authentication backchannel and
 * stuff it into our spool buffer. We also replace \n with nul
 * to make parsing easier later.
 */

static int
spooldata(int fd)
{

    if (spoolbuf)
	free(spoolbuf);
    spoolidx = 0;

    if (spoolbuf == NULL && (spoolbuf = malloc(AUTHMAXSPOOL)) == NULL)
	syslog(LOG_ERR, "authbuffer malloc: %m");

    else while (spoolidx < sizeof(spoolbuf) - 1) {
	int	r = read(fd, spoolbuf + spoolidx, sizeof(spoolbuf)-spoolidx);
	char	*b;

	if (r <= 0) {
	    spoolbuf[spoolidx] = '\0';
	    return 0;
	}
	/*
	 * Convert newlines into NULs to allow
	 * easier scanning of the file.
	 */
	while ((b = memchr(spoolbuf + spoolidx, '\n', r)) != NULL)
	    *b = '\0';
	spoolidx += r;
    }
    return -1;
}


/*
 * auth_check()
 * Starts an auth_script() for the given <user>, with a class <class>,
 * style <style>, and service <service>.  <style> is necessary,
 * as are <user> and <class>, but <service> is optional -- it defaults
 * to "login".
 * Since auth_script() expects an execl'able program name, authenticate()
 * also concatenates <style> to _PATH_AUTHPROG.
 * Lastly, calls auth_scan(0) to see if there are any "reject" statements,
 * or lack of "auth" statements.
 * Returns -1 on error, 0 on rejection, and >0 on success.
 * (See AUTH_* for the return values.)
 *
 */

int
auth_check(const char *name, const char *clss, const char *style,
	   const char *service, int *status)
{
    int	    _status;

    if (status == NULL)
	status = &_status;
    *status = 0;

    if (style != NULL) {
	char	path[MAXPATHLEN];

	if (service == NULL)
	    service = LOGIN_DEFSERVICE;

	snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style);
	if (auth_script(path, style, "-s", service, name, clss, 0))
	    status = 0;
	else
	    *status = auth_scan(0);

	return *status & AUTH_ALLOW;
    }
    return -1;
}


int
auth_response(const char *name, const char *class, const char *style,
	      const char *service, int *status,
	      const char *challenge, const char *response)
{
    int	    _status;

    if (status == NULL)
	status = &_status;
    *status = 0;

    if (style != NULL) {
	int	datalen;
	char    *data;

	if (service == NULL)
	    service = LOGIN_DEFSERVICE;

	datalen = strlen(challenge) + strlen(response) + 2;

	if ((data = malloc(datalen)) == NULL) {
	    syslog(LOG_ERR, "auth_response: %m");
	    warnx("internal resource failure");
	} else {
	    char    path[MAXPATHLEN];

	    snprintf(data, datalen, "%s%c%s", challenge, 0, response);
	    snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style);
	    if (auth_script_data(data, datalen, path, style, "-s", service,
				 name, class, 0))
		*status = 0;
	    else
		*status = auth_scan(0);
	    free(data);
	    return (*status & AUTH_ALLOW);
	}
    }
    return -1;
}


int
auth_approve(login_cap_t *lc, const char *name, const char *service)
{
    int	    r = -1;
    char    path[MAXPATHLEN];

    if (lc == NULL) {
	if (strlen(name) > MAXPATHLEN) {
	    syslog(LOG_ERR, "%s: username too long", name);
	    warnx("username too long");
	} else {
	    struct passwd   *pwd;
	    char	    *p;

	    pwd = getpwnam(name);
	    if (pwd == NULL && (p = strchr(name, '.')) != NULL) {
		int	i = p - name;

		if (i >= MAXPATHLEN)
		    i = MAXPATHLEN - 1;
		strncpy(path, name, i);
		path[i] = '\0';
		pwd = getpwnam(path); /* Fixed bug in BSDI code... */
	    }
	    if ((lc = login_getpwclass(pwd ? pwd->pw_class : NULL)) == NULL)
		warnx("unable to classify user '%s'", name);
	}
    }

    if (lc != NULL) {
	char	*approve;
	char	*s;

	if (service != NULL)
		service = LOGIN_DEFSERVICE;

	snprintf(path, sizeof(path), "approve-%s", service);

        if ((approve = login_getcapstr(lc, s = path, NULL, NULL)) == NULL &&
	    (approve = login_getcapstr(lc, s = "approve", NULL, NULL)) == NULL)
	    r = AUTH_OKAY;
	else {

	    if (approve[0] != '/') {
		syslog(LOG_ERR, "Invalid %s script: %s", s, approve);
		warnx("invalid path to approval script");
	    } else {
		char	*s;

		s = strrchr(approve, '/') + 1;
		if (auth_script(approve, s, name,
				lc->lc_class, service, 0) == 0 &&
		    (r = auth_scan(AUTH_OKAY) & AUTH_ALLOW) != 0)
		    auth_env();
	    }
	}
    }
    return r;
}


void
auth_env(void)
{
    int	    idx = 0;
    char    *line;

    while ((line = nextline(&idx)) != NULL) {
	if (!strncasecmp(line, BI_SETENV, sizeof(BI_SETENV)-1)) {
	    line += sizeof(BI_SETENV) - 1;
	    if (*line && isspace(*line)) {
		char	*name;
		char	ch, *p;

		while (*line && isspace(*line))
		    ++line;
		name = line;
		while (*line && !isspace(*line))
		    ++line;
		ch = *(p = line);
		if (*line)
		    ++line;
		if (setenv(name, line, 1))
		    warn("setenv(%s, %s)", name, line);
		*p = ch;
	    }
	}
    }
}


char *
auth_value(const char *what)
{
    int	    idx = 0;
    char    *line;

    while ((line = nextline(&idx)) != NULL) {
	if (!strncasecmp(line, BI_VALUE, sizeof(BI_VALUE)-1)) {
	    char    *name;

	    line += sizeof(BI_VALUE) - 1;
	    while (*line && isspace(*line))
		++line;
	    name = line;
	    if (*line) {
		int	i;
		char	ch, *p;

		ch = *(p = line);
		*line++ = '\0';
		i = strcmp(name, what);
		*p = ch;
		if (i == 0)
		    return auth_mkvalue(line);
	    }
	}
    }
    return NULL;
}

char *
auth_mkvalue(const char *value)
{
    char *big, *p;

    big = malloc(strlen(value) * 4 + 1);
    if (big != NULL) {
	for (p = big; *value; ++value) {
	    switch (*value) {
	    case '\r':
		*p++ = '\\';
		*p++ = 'r';
		break;
	    case '\n':
		*p++ = '\\';
		*p++ = 'n';
		break;
	    case '\\':
		*p++ = '\\';
		*p++ = *value;
		break;
	    case '\t':
	    case ' ':
		if (p == big)
		    *p++ = '\\';
		*p++ = *value;
		break;
	    default:
		if (!isprint(*value)) {
		    *p++ = '\\';
		    *p++ = ((*value >> 6) & 0x3) + '0';
		    *p++ = ((*value >> 3) & 0x7) + '0';
		    *p++ = ((*value     ) & 0x7) + '0';
		} else
		    *p++ = *value;
		break;
	    }
	}
	*p = '\0';
	big = realloc(big, strlen(big) + 1);
    }
    return big;
}


#define NARGC	63
static int
_auth_script(const char *data, int nbytes, const char *path, va_list ap)
{
    int		    r, argc, status;
    int		    pfd[2];
    pid_t	    pid;
    struct authopts *e;
    char	    *argv[NARGC+1];

    r = -1;
    argc = 0;
    for (e = optfirst; argc < (NARGC - 1) && e != NULL; e = e->next) {
	argv[argc++] = "-v";
	argv[argc++] = e->opt;
    }
    while (argc < NARGC && (argv[argc] = va_arg(ap, char *)) != NULL)
	++argc;
    argv[argc] = NULL;

    if (argc >= NARGC && va_arg(ap, char *))
	syslog(LOG_ERR, "too many arguments");
    else if (_secure_path(path, 0, 0) < 0) {
	syslog(LOG_ERR, "%s: path not secure", path);
	warnx("invalid script: %s", path);
    } else if (socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd) < 0) {
	syslog(LOG_ERR, "unable to create backchannel %m");
	warnx("internal resource failure");
    } else switch (pid = fork()) {
    case -1:			/* fork() failure */
	close(pfd[0]);
	close(pfd[1]);
	syslog(LOG_ERR, "fork %s: %m", path);
	warnx("internal resource failure");
	break;
    case 0:			/* child process */
	close(pfd[0]);
	if (pfd[1] != AUTHCOMM_FD) {
	    if (dup2(pfd[1], AUTHCOMM_FD) < 0)
		err(1, "dup backchannel");
	    close(pfd[1]);
	}
	for (r = getdtablesize(); --r > AUTHCOMM_FD; )
	    close(r);
	execve(path, argv, auth_environ);
	syslog(LOG_ERR, "exec %s: %m", path);
	err(1, path);
    default:			/* parent */
	close(pfd[1]);
	if (data && nbytes)
	    write(pfd[0], data, nbytes);
	r = spooldata(pfd[0]);
	close(pfd[0]);
	if (waitpid(pid, &status, 0) < 0) {
	    syslog(LOG_ERR, "%s: waitpid: %m", path);
	    warnx("internal failure");
	    r = -1;
	} else {
	    if (r != 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
		r = -1;
	}
	/* kill the buffer if it is of no use */
	if (r != 0) {
	    free(spoolbuf);
	    spoolbuf = NULL;
	    spoolidx = 0;
	}
	break;
    }
    return r;
}



/*
 * auth_script()
 * Runs an authentication program with specified arguments.
 * It sets up file descriptor 3 for the program to write to;
 * it stashes the output somewhere.  The output of the program
 * consists of statements:
 *	reject [challenge|silent]
 *	authorize [root|secure]
 *	setenv <name> [<value>]
 *	remove <file>
 *
 * Terribly exciting, isn't it?
 * Output cannot exceed AUTHMAXSPOOL characters.
 */

int
auth_script(const char *path, ...)
{
    int		r;
    va_list	ap;

    va_start(ap, path);
    r = _auth_script(NULL, 0, path, ap);
    va_end(ap);
    return r;
}


int
auth_script_data(const char *data, int nbytes, const char *path, ...)
{
    int		r;
    va_list	ap;

    va_start(ap, path);
    r = _auth_script(data, nbytes, path, ap);
    va_end(ap);
    return r;
}


static void
add_rmlist(const char *file)
{
    struct rmfiles *rm;

    if ((rm = malloc(sizeof(struct rmfiles) + strlen(file) + 1)) == NULL)
	syslog(LOG_ERR, "add_rmfile malloc: %m");
    else {
	strcpy(rm->file, file);
	rm->next = rmfirst;
	rmfirst = rm;
    }
}


int
auth_scan(int okay)
{
    int	    idx = 0;
    char    *line;

    while ((line = nextline(&idx)) != NULL) {
	if (!strncasecmp(line, BI_REJECT, sizeof(BI_REJECT)-1)) {
	    line += sizeof(BI_REJECT) - 1;
	    while (*line && isspace(*line))
		++line;
	    if (*line) {
		if (!strcasecmp(line, "silent"))
		    return AUTH_SILENT;
		if (!strcasecmp(line, "challenge"))
		    return AUTH_CHALLENGE;
	    }
	    return 0;
	} else if (!strncasecmp(line, BI_AUTH, sizeof(BI_AUTH)-1)) {
	    line += sizeof(BI_AUTH) - 1;
	    while (*line && isspace(*line))
		++line;
	    if (*line == '\0')
		okay |= AUTH_OKAY;
	    else if (!strcasecmp(line, "root"))
		okay |= AUTH_ROOTOKAY;
	    else if (!strcasecmp(line, "secure"))
		okay |= AUTH_SECURE;
	}
	else if (!strncasecmp(line, BI_REMOVE, sizeof(BI_REMOVE)-1)) {
	    line += sizeof(BI_REMOVE) - 1;
	    while (*line && isspace(*line))
		++line;
	    if (*line)
		add_rmlist(line);
	}
    }

    return okay;
}


int
auth_setopt(const char *n, const char *v)
{
    int		    r;
    struct authopts *e;

    if ((e = malloc(sizeof(*e) + strlen(n) + strlen(v) + 1)) == NULL)
	r = -1;
    else {
	sprintf(e->opt, "%s=%s", n, v);
	e->next = optfirst;
	optfirst = e;
	r = 0;
    }
    return r;
}


void
auth_clropts(void)
{
    struct authopts *e;

    while ((e = optfirst) != NULL) {
	optfirst = e->next;
	free(e);
    }
}


void
auth_rmfiles(void)
{
    struct rmfiles  *rm;

    while ((rm = rmfirst) != NULL) {
	unlink(rm->file);
	rmfirst = rm->next;
	free(rm);
    }
}

#endif


/*
 * auth_checknologin()
 * Checks for the existance of a nologin file in the login_cap
 * capability <lc>.  If there isn't one specified, then it checks
 * to see if this class should just ignore nologin files.  Lastly,
 * it tries to print out the default nologin file, and, if such
 * exists, it exits.
 */

void
auth_checknologin(login_cap_t *lc)
{
  char *file;

  /* Do we ignore a nologin file? */
  if (login_getcapbool(lc, "ignorenologin", 0))
    return;

  /* Note that <file> will be "" if there is no nologin capability */
  if ((file = login_getcapstr(lc, "nologin", "", NULL)) == NULL)
    exit(1);

  /*
   * *file is true IFF there was a "nologin" capability
   * Note that auth_cat() returns 1 only if the specified
   * file exists, and is readable.  E.g., /.nologin exists.
   */
  if ((*file && auth_cat(file)) || auth_cat(_PATH_NOLOGIN))
    exit(1);
}


/*
 * auth_cat()
 * Checks for the readability of <file>; if it can be opened for
 * reading, it prints it out to stdout, and then exits.  Otherwise,
 * it returns 0 (meaning no nologin file).
 */

int
auth_cat(const char *file)
{
  int fd, count;
  char buf[BUFSIZ];

  if ((fd = open(file, O_RDONLY)) < 0)
    return 0;
  while ((count = read(fd, buf, sizeof(buf))) > 0)
    (void)write(fileno(stdout), buf, count);
  close(fd);
  return 1;
}
OpenPOWER on IntegriCloud