]> granicus.if.org Git - shadow/blob - libmisc/console.c
112d78b48ded2d490bd15ee0a7b62c9fad528916
[shadow] / libmisc / console.c
1 /*
2  * Copyright 1991, Julianne Frances Haugh and Chip Rosenthal
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31 #include "defines.h"
32 #include <stdio.h>
33 #include "getdef.h"
34
35 #ident "$Id$"
36
37
38 /*
39  * This is now rather generic function which decides if "tty" is listed
40  * under "cfgin" in config (directly or indirectly). Fallback to default if
41  * something is bad.
42  */
43 int is_listed (const char *cfgin, const char *tty, int def)
44 {
45         FILE *fp;
46         char buf[200], *cons, *s;
47
48         /*
49          * If the CONSOLE configuration definition isn't given,
50          * fallback to default.
51          */
52
53         if ((cons = getdef_str (cfgin)) == NULL)
54                 return def;
55
56         /*
57          * If this isn't a filename, then it is a ":" delimited list of
58          * console devices upon which root logins are allowed.
59          */
60
61         if (*cons != '/') {
62                 cons = strcpy (buf, cons);
63                 while ((s = strtok (cons, ":")) != NULL) {
64                         if (strcmp (s, tty) == 0)
65                                 return 1;
66
67                         cons = NULL;
68                 }
69                 return 0;
70         }
71
72         /*
73          * If we can't open the console list, then call everything a
74          * console - otherwise root will never be allowed to login.
75          */
76
77         if ((fp = fopen (cons, "r")) == NULL)
78                 return def;
79
80         /*
81          * See if this tty is listed in the console file.
82          */
83
84         while (fgets (buf, sizeof (buf), fp) != NULL) {
85                 buf[strlen (buf) - 1] = '\0';
86                 if (strcmp (buf, tty) == 0) {
87                         (void) fclose (fp);
88                         return 1;
89                 }
90         }
91
92         /*
93          * This tty isn't a console.
94          */
95
96         (void) fclose (fp);
97         return 0;
98 }
99
100 /*
101  * console - return 1 if the "tty" is a console device, else 0.
102  *
103  * Note - we need to take extreme care here to avoid locking out root logins
104  * if something goes awry.  That's why we do things like call everything a
105  * console if the consoles file can't be opened.  Because of this, we must
106  * warn the user to protect against the remove of the consoles file since
107  * that would allow an unauthorized root login.
108  */
109
110 int console (const char *tty)
111 {
112         return is_listed ("CONSOLE", tty, 1);
113 }