Merge branch 'vendor/BINUTILS220'
[dragonfly.git] / lib / libutil / login_auth.c
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  * $FreeBSD: src/lib/libutil/login_auth.c,v 1.11 1999/08/28 00:05:45 peter Exp $
29  * $DragonFly: src/lib/libutil/login_auth.c,v 1.3 2005/03/04 04:31:11 cpressey Exp $
30  */
31
32 #include <sys/types.h>
33
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include "login_cap.h"
41
42
43 /*
44  * auth_checknologin()
45  * Checks for the existance of a nologin file in the login_cap
46  * capability <lc>.  If there isn't one specified, then it checks
47  * to see if this class should just ignore nologin files.  Lastly,
48  * it tries to print out the default nologin file, and, if such
49  * exists, it exits.
50  */
51
52 void
53 auth_checknologin(login_cap_t *lc)
54 {
55   char *file;
56
57   /* Do we ignore a nologin file? */
58   if (login_getcapbool(lc, "ignorenologin", 0))
59     return;
60
61   /* Note that <file> will be "" if there is no nologin capability */
62   if ((file = login_getcapstr(lc, "nologin", "", NULL)) == NULL)
63     exit(1);
64
65   /*
66    * *file is true IFF there was a "nologin" capability
67    * Note that auth_cat() returns 1 only if the specified
68    * file exists, and is readable.  E.g., /.nologin exists.
69    */
70   if ((*file && auth_cat(file)) || auth_cat(_PATH_NOLOGIN))
71     exit(1);
72 }
73
74
75 /*
76  * auth_cat()
77  * Checks for the readability of <file>; if it can be opened for
78  * reading, it prints it out to stdout, and then exits.  Otherwise,
79  * it returns 0 (meaning no nologin file).
80  */
81
82 int
83 auth_cat(const char *file)
84 {
85   int fd, count;
86   char buf[BUFSIZ];
87
88   if ((fd = open(file, O_RDONLY)) < 0)
89     return 0;
90   while ((count = read(fd, buf, sizeof(buf))) > 0)
91     (void)write(fileno(stdout), buf, count);
92   close(fd);
93   sleep(5);     /* wait an arbitrary time to drain */
94   return 1;
95 }