getttyent.c revision 333796
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  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 * 4. Neither the name of the University 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 THE REGENTS 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 THE REGENTS 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#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/lib/libc/gen/getttyent.c 333796 2018-05-18 14:57:58Z gjb $");
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
46static char zapchar;
47static FILE *tf;
48static size_t lbsize;
49static char *line;
50
51#define	MALLOCCHUNK	100
52
53static char *skip(char *);
54static char *value(char *);
55
56struct ttyent *
57getttynam(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
71static int
72auto_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
99done:
100	free(buf);
101	return (rv);
102}
103
104static int
105auto_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
122struct ttyent *
123getttyent(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 */
221static char *
222skip(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
254static char *
255value(char *p)
256{
257
258	return ((p = strchr(p, '=')) ? ++p : NULL);
259}
260
261int
262setttyent(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
278int
279endttyent(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
295static int
296isttystat(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
304int
305isdialuptty(const char *tty)
306{
307
308	return isttystat(tty, TTY_DIALUP);
309}
310
311int
312isnettty(const char *tty)
313{
314
315	return isttystat(tty, TTY_NETWORK);
316}
317