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