]> granicus.if.org Git - shadow/blob - libmisc/console.c
Define is_listed() as static and add its prototype.
[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 /* local function prototypes */
38 static int is_listed (const char *cfgin, const char *tty, int def);
39
40 /*
41  * This is now rather generic function which decides if "tty" is listed
42  * under "cfgin" in config (directly or indirectly). Fallback to default if
43  * something is bad.
44  */
45 static int is_listed (const char *cfgin, const char *tty, int def)
46 {
47         FILE *fp;
48         char buf[200], *cons, *s;
49
50         /*
51          * If the CONSOLE configuration definition isn't given,
52          * fallback to default.
53          */
54
55         if ((cons = getdef_str (cfgin)) == NULL)
56                 return def;
57
58         /*
59          * If this isn't a filename, then it is a ":" delimited list of
60          * console devices upon which root logins are allowed.
61          */
62
63         if (*cons != '/') {
64                 cons = strcpy (buf, cons);
65                 while ((s = strtok (cons, ":")) != NULL) {
66                         if (strcmp (s, tty) == 0)
67                                 return 1;
68
69                         cons = NULL;
70                 }
71                 return 0;
72         }
73
74         /*
75          * If we can't open the console list, then call everything a
76          * console - otherwise root will never be allowed to login.
77          */
78
79         if ((fp = fopen (cons, "r")) == NULL)
80                 return def;
81
82         /*
83          * See if this tty is listed in the console file.
84          */
85
86         while (fgets (buf, sizeof (buf), fp) != NULL) {
87                 buf[strlen (buf) - 1] = '\0';
88                 if (strcmp (buf, tty) == 0) {
89                         (void) fclose (fp);
90                         return 1;
91                 }
92         }
93
94         /*
95          * This tty isn't a console.
96          */
97
98         (void) fclose (fp);
99         return 0;
100 }
101
102 /*
103  * console - return 1 if the "tty" is a console device, else 0.
104  *
105  * Note - we need to take extreme care here to avoid locking out root logins
106  * if something goes awry.  That's why we do things like call everything a
107  * console if the consoles file can't be opened.  Because of this, we must
108  * warn the user to protect against the remove of the consoles file since
109  * that would allow an unauthorized root login.
110  */
111
112 int console (const char *tty)
113 {
114         return is_listed ("CONSOLE", tty, 1);
115 }