xref: /freebsd/lib/libc/gen/getttyent.c (revision 5b20c5e1f8d59f14901f0c0e4b6b577eaa3f9569)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)getttyent.c	8.1 (Berkeley) 6/4/93");
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/sysctl.h>
39 
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ttyent.h>
45 
46 static char zapchar;
47 static FILE *tf;
48 static size_t lbsize;
49 static char *line;
50 
51 #define	MALLOCCHUNK	100
52 
53 static char *skip(char *);
54 static char *value(char *);
55 
56 struct ttyent *
57 getttynam(const char *tty)
58 {
59 	struct ttyent *t;
60 
61 	if (strncmp(tty, "/dev/", 5) == 0)
62 		tty += 5;
63 	setttyent();
64 	while ( (t = getttyent()) )
65 		if (!strcmp(tty, t->ty_name))
66 			break;
67 	endttyent();
68 	return (t);
69 }
70 
71 static int
72 auto_tty_status(const char *ty_name)
73 {
74 	size_t len;
75 	char *buf, *cons, *nextcons;
76 	int rv;
77 
78 	rv = TTY_IFCONSOLE;
79 
80 	/* Check if this is an enabled kernel console line */
81 	buf = NULL;
82 	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
83 		return (rv); /* Errors mean don't enable */
84 	buf = malloc(len);
85 	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
86 		goto done;
87 
88 	if ((cons = strchr(buf, '/')) == NULL)
89 		goto done;
90 	*cons = '\0';
91 	nextcons = buf;
92 	while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
93 		if (strcmp(cons, ty_name) == 0) {
94 			rv |= TTY_ON;
95 			break;
96 		}
97 	}
98 
99 done:
100 	free(buf);
101 	return (rv);
102 }
103 
104 static int
105 auto_exists_status(const char *ty_name)
106 {
107 	struct stat sb;
108 	char *dev;
109 	int rv;
110 
111 	rv = TTY_IFEXISTS;
112 	if (*ty_name == '/')
113 		asprintf(&dev, "%s", ty_name);
114 	else
115 		asprintf(&dev, "/dev/%s", ty_name);
116 	if (dev != NULL && stat(dev, &sb) == 0)
117 		rv |= TTY_ON;
118 	free(dev);
119 	return (rv);
120 }
121 
122 struct ttyent *
123 getttyent(void)
124 {
125 	static struct ttyent tty;
126 	char *p;
127 	int c;
128 	size_t i;
129 
130 	if (!tf && !setttyent())
131 		return (NULL);
132 	for (;;) {
133 		if (!fgets(p = line, lbsize, tf))
134 			return (NULL);
135 		/* extend buffer if line was too big, and retry */
136 		while (!strchr(p, '\n') && !feof(tf)) {
137 			i = strlen(p);
138 			lbsize += MALLOCCHUNK;
139 			if ((p = realloc(line, lbsize)) == NULL) {
140 				(void)endttyent();
141 				return (NULL);
142 			}
143 			line = p;
144 			if (!fgets(&line[i], lbsize - i, tf))
145 				return (NULL);
146 		}
147 		while (isspace((unsigned char)*p))
148 			++p;
149 		if (*p && *p != '#')
150 			break;
151 	}
152 
153 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
154 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
155 
156 	zapchar = 0;
157 	tty.ty_name = p;
158 	tty.ty_status = 0;
159 	tty.ty_window = NULL;
160 	tty.ty_group  = _TTYS_NOGROUP;
161 
162 	p = skip(p);
163 	if (!*(tty.ty_getty = p))
164 		tty.ty_getty = tty.ty_type = NULL;
165 	else {
166 		p = skip(p);
167 		if (!*(tty.ty_type = p))
168 			tty.ty_type = NULL;
169 		else {
170 			/* compatibility kludge: handle network/dialup specially */
171 			if (scmp(_TTYS_DIALUP))
172 				tty.ty_status |= TTY_DIALUP;
173 			else if (scmp(_TTYS_NETWORK))
174 				tty.ty_status |= TTY_NETWORK;
175 			p = skip(p);
176 		}
177 	}
178 
179 	for (; *p; p = skip(p)) {
180 		if (scmp(_TTYS_OFF))
181 			tty.ty_status &= ~TTY_ON;
182 		else if (scmp(_TTYS_ON))
183 			tty.ty_status |= TTY_ON;
184 		else if (scmp(_TTYS_ONIFCONSOLE))
185 			tty.ty_status |= auto_tty_status(tty.ty_name);
186 		else if (scmp(_TTYS_ONIFEXISTS))
187 			tty.ty_status |= auto_exists_status(tty.ty_name);
188 		else if (scmp(_TTYS_SECURE))
189 			tty.ty_status |= TTY_SECURE;
190 		else if (scmp(_TTYS_INSECURE))
191 			tty.ty_status &= ~TTY_SECURE;
192 		else if (scmp(_TTYS_DIALUP))
193 			tty.ty_status |= TTY_DIALUP;
194 		else if (scmp(_TTYS_NETWORK))
195 			tty.ty_status |= TTY_NETWORK;
196 		else if (vcmp(_TTYS_WINDOW))
197 			tty.ty_window = value(p);
198 		else if (vcmp(_TTYS_GROUP))
199 			tty.ty_group = value(p);
200 		else
201 			break;
202 	}
203 
204 	if (zapchar == '#' || *p == '#')
205 		while ((c = *++p) == ' ' || c == '\t')
206 			;
207 	tty.ty_comment = p;
208 	if (*p == 0)
209 		tty.ty_comment = 0;
210 	if ((p = strchr(p, '\n')))
211 		*p = '\0';
212 	return (&tty);
213 }
214 
215 #define	QUOTED	1
216 
217 /*
218  * Skip over the current field, removing quotes, and return a pointer to
219  * the next field.
220  */
221 static char *
222 skip(char *p)
223 {
224 	char *t;
225 	int c, q;
226 
227 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
228 		if (c == '"') {
229 			q ^= QUOTED;	/* obscure, but nice */
230 			continue;
231 		}
232 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
233 			p++;
234 		*t++ = *p;
235 		if (q == QUOTED)
236 			continue;
237 		if (c == '#') {
238 			zapchar = c;
239 			*p = 0;
240 			break;
241 		}
242 		if (c == '\t' || c == ' ' || c == '\n') {
243 			zapchar = c;
244 			*p++ = 0;
245 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
246 				p++;
247 			break;
248 		}
249 	}
250 	*--t = '\0';
251 	return (p);
252 }
253 
254 static char *
255 value(char *p)
256 {
257 
258 	return ((p = strchr(p, '=')) ? ++p : NULL);
259 }
260 
261 int
262 setttyent(void)
263 {
264 
265 	if (line == NULL) {
266 		if ((line = malloc(MALLOCCHUNK)) == NULL)
267 			return (0);
268 		lbsize = MALLOCCHUNK;
269 	}
270 	if (tf) {
271 		rewind(tf);
272 		return (1);
273 	} else if ( (tf = fopen(_PATH_TTYS, "re")) )
274 		return (1);
275 	return (0);
276 }
277 
278 int
279 endttyent(void)
280 {
281 	int rval;
282 
283 	/*
284          * NB: Don't free `line' because getttynam()
285 	 * may still be referencing it
286 	 */
287 	if (tf) {
288 		rval = (fclose(tf) != EOF);
289 		tf = NULL;
290 		return (rval);
291 	}
292 	return (1);
293 }
294 
295 static int
296 isttystat(const char *tty, int flag)
297 {
298 	struct ttyent *t;
299 
300 	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
301 }
302 
303 
304 int
305 isdialuptty(const char *tty)
306 {
307 
308 	return isttystat(tty, TTY_DIALUP);
309 }
310 
311 int
312 isnettty(const char *tty)
313 {
314 
315 	return isttystat(tty, TTY_NETWORK);
316 }
317