]> granicus.if.org Git - shadow/blob - libmisc/console.c
* NEWS, libmisc/chowntty.c: Fix a race condition that could lead to
[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 bool is_listed (const char *cfgin, const char *tty, bool 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 bool is_listed (const char *cfgin, const char *tty, bool 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         cons = getdef_str (cfgin);
61         if (NULL == cons) {
62                 return def;
63         }
64
65         /*
66          * If this isn't a filename, then it is a ":" delimited list of
67          * console devices upon which root logins are allowed.
68          */
69
70         if (*cons != '/') {
71                 cons = strcpy (buf, cons);
72                 while ((s = strtok (cons, ":")) != NULL) {
73                         if (strcmp (s, tty) == 0) {
74                                 return true;
75                         }
76
77                         cons = NULL;
78                 }
79                 return false;
80         }
81
82         /*
83          * If we can't open the console list, then call everything a
84          * console - otherwise root will never be allowed to login.
85          */
86
87         fp = fopen (cons, "r");
88         if (NULL == fp) {
89                 return def;
90         }
91
92         /*
93          * See if this tty is listed in the console file.
94          */
95
96         while (fgets (buf, (int) sizeof (buf), fp) != NULL) {
97                 buf[strlen (buf) - 1] = '\0';
98                 if (strcmp (buf, tty) == 0) {
99                         (void) fclose (fp);
100                         return true;
101                 }
102         }
103
104         /*
105          * This tty isn't a console.
106          */
107
108         (void) fclose (fp);
109         return false;
110 }
111
112 /*
113  * console - return 1 if the "tty" is a console device, else 0.
114  *
115  * Note - we need to take extreme care here to avoid locking out root logins
116  * if something goes awry.  That's why we do things like call everything a
117  * console if the consoles file can't be opened.  Because of this, we must
118  * warn the user to protect against the remove of the consoles file since
119  * that would allow an unauthorized root login.
120  */
121
122 bool console (const char *tty)
123 {
124         return is_listed ("CONSOLE", tty, true);
125 }
126