login_auth.c revision 49696
1/*-
2 * Copyright (c) 1996 by
3 * Sean Eric Fagan <sef@kithrup.com>
4 * David Nugent <davidn@blaze.net.au>
5 * All rights reserved.
6 *
7 * Portions copyright (c) 1995,1997 by
8 * Berkeley Software Design, Inc.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice immediately at the beginning of the file, without modification,
16 *    this list of conditions, and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. This work was done expressly for inclusion into FreeBSD.  Other use
21 *    is permitted provided this notation is included.
22 * 4. Absolutely no warranty of function or purpose is made by the authors.
23 * 5. Modifications may be freely made to this file providing the above
24 *    conditions are met.
25 *
26 * Low-level routines relating to the user capabilities database
27 *
28 *	$Id: login_auth.c,v 1.9 1998/09/16 04:17:47 imp Exp $
29 */
30
31#include <sys/types.h>
32#include <sys/time.h>
33#include <sys/resource.h>
34#include <sys/stat.h>
35#include <sys/param.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <limits.h>
39#include <stdio.h>
40#include <ctype.h>
41#include <pwd.h>
42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45#include <unistd.h>
46#include <login_cap.h>
47#include <stdarg.h>
48#include <paths.h>
49#include <sys/socket.h>
50#include <sys/wait.h>
51#include <err.h>
52#include <libutil.h>
53
54
55/*
56 * auth_checknologin()
57 * Checks for the existance of a nologin file in the login_cap
58 * capability <lc>.  If there isn't one specified, then it checks
59 * to see if this class should just ignore nologin files.  Lastly,
60 * it tries to print out the default nologin file, and, if such
61 * exists, it exits.
62 */
63
64void
65auth_checknologin(login_cap_t *lc)
66{
67  char *file;
68
69  /* Do we ignore a nologin file? */
70  if (login_getcapbool(lc, "ignorenologin", 0))
71    return;
72
73  /* Note that <file> will be "" if there is no nologin capability */
74  if ((file = login_getcapstr(lc, "nologin", "", NULL)) == NULL)
75    exit(1);
76
77  /*
78   * *file is true IFF there was a "nologin" capability
79   * Note that auth_cat() returns 1 only if the specified
80   * file exists, and is readable.  E.g., /.nologin exists.
81   */
82  if ((*file && auth_cat(file)) || auth_cat(_PATH_NOLOGIN))
83    exit(1);
84}
85
86
87/*
88 * auth_cat()
89 * Checks for the readability of <file>; if it can be opened for
90 * reading, it prints it out to stdout, and then exits.  Otherwise,
91 * it returns 0 (meaning no nologin file).
92 */
93
94int
95auth_cat(const char *file)
96{
97  int fd, count;
98  char buf[BUFSIZ];
99
100  if ((fd = open(file, O_RDONLY)) < 0)
101    return 0;
102  while ((count = read(fd, buf, sizeof(buf))) > 0)
103    (void)write(fileno(stdout), buf, count);
104  close(fd);
105  sleep(5);	/* wait an arbitrary time to drain */
106  return 1;
107}
108