xref: /freebsd/lib/libc/gen/getttyent.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
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 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41 
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ttyent.h>
47 
48 static char zapchar;
49 static FILE *tf;
50 static size_t lbsize;
51 static char *line;
52 
53 #define	MALLOCCHUNK	100
54 
55 static char *skip(char *);
56 static char *value(char *);
57 
58 struct ttyent *
59 getttynam(const char *tty)
60 {
61 	struct ttyent *t;
62 
63 	if (strncmp(tty, "/dev/", 5) == 0)
64 		tty += 5;
65 	setttyent();
66 	while ( (t = getttyent()) )
67 		if (!strcmp(tty, t->ty_name))
68 			break;
69 	endttyent();
70 	return (t);
71 }
72 
73 static int
74 auto_tty_status(const char *ty_name)
75 {
76 	size_t len;
77 	char *buf, *cons, *nextcons;
78 	int rv;
79 
80 	rv = TTY_IFCONSOLE;
81 
82 	/* Check if this is an enabled kernel console line */
83 	buf = NULL;
84 	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
85 		return (rv); /* Errors mean don't enable */
86 	buf = malloc(len);
87 	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
88 		goto done;
89 
90 	if ((cons = strchr(buf, '/')) == NULL)
91 		goto done;
92 	*cons = '\0';
93 	nextcons = buf;
94 	while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
95 		if (strcmp(cons, ty_name) == 0) {
96 			rv |= TTY_ON;
97 			break;
98 		}
99 	}
100 
101 done:
102 	free(buf);
103 	return (rv);
104 }
105 
106 static int
107 auto_exists_status(const char *ty_name)
108 {
109 	struct stat sb;
110 	char *dev;
111 	int rv;
112 
113 	rv = TTY_IFEXISTS;
114 	if (*ty_name == '/')
115 		asprintf(&dev, "%s", ty_name);
116 	else
117 		asprintf(&dev, "/dev/%s", ty_name);
118 	if (dev != NULL && stat(dev, &sb) == 0)
119 		rv |= TTY_ON;
120 	free(dev);
121 	return (rv);
122 }
123 
124 struct ttyent *
125 getttyent(void)
126 {
127 	static struct ttyent tty;
128 	char *p;
129 	int c;
130 	size_t i;
131 
132 	if (!tf && !setttyent())
133 		return (NULL);
134 	for (;;) {
135 		if (!fgets(p = line, lbsize, tf))
136 			return (NULL);
137 		/* extend buffer if line was too big, and retry */
138 		while (!strchr(p, '\n') && !feof(tf)) {
139 			i = strlen(p);
140 			lbsize += MALLOCCHUNK;
141 			if ((p = realloc(line, lbsize)) == NULL) {
142 				(void)endttyent();
143 				return (NULL);
144 			}
145 			line = p;
146 			if (!fgets(&line[i], lbsize - i, tf))
147 				return (NULL);
148 		}
149 		while (isspace((unsigned char)*p))
150 			++p;
151 		if (*p && *p != '#')
152 			break;
153 	}
154 
155 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
156 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
157 
158 	zapchar = 0;
159 	tty.ty_name = p;
160 	tty.ty_status = 0;
161 	tty.ty_window = NULL;
162 	tty.ty_group  = _TTYS_NOGROUP;
163 
164 	p = skip(p);
165 	if (!*(tty.ty_getty = p))
166 		tty.ty_getty = tty.ty_type = NULL;
167 	else {
168 		p = skip(p);
169 		if (!*(tty.ty_type = p))
170 			tty.ty_type = NULL;
171 		else {
172 			/* compatibility kludge: handle network/dialup specially */
173 			if (scmp(_TTYS_DIALUP))
174 				tty.ty_status |= TTY_DIALUP;
175 			else if (scmp(_TTYS_NETWORK))
176 				tty.ty_status |= TTY_NETWORK;
177 			p = skip(p);
178 		}
179 	}
180 
181 	for (; *p; p = skip(p)) {
182 		if (scmp(_TTYS_OFF))
183 			tty.ty_status &= ~TTY_ON;
184 		else if (scmp(_TTYS_ON))
185 			tty.ty_status |= TTY_ON;
186 		else if (scmp(_TTYS_ONIFCONSOLE))
187 			tty.ty_status |= auto_tty_status(tty.ty_name);
188 		else if (scmp(_TTYS_ONIFEXISTS))
189 			tty.ty_status |= auto_exists_status(tty.ty_name);
190 		else if (scmp(_TTYS_SECURE))
191 			tty.ty_status |= TTY_SECURE;
192 		else if (scmp(_TTYS_INSECURE))
193 			tty.ty_status &= ~TTY_SECURE;
194 		else if (scmp(_TTYS_DIALUP))
195 			tty.ty_status |= TTY_DIALUP;
196 		else if (scmp(_TTYS_NETWORK))
197 			tty.ty_status |= TTY_NETWORK;
198 		else if (vcmp(_TTYS_WINDOW))
199 			tty.ty_window = value(p);
200 		else if (vcmp(_TTYS_GROUP))
201 			tty.ty_group = value(p);
202 		else
203 			break;
204 	}
205 
206 	if (zapchar == '#' || *p == '#')
207 		while ((c = *++p) == ' ' || c == '\t')
208 			;
209 	tty.ty_comment = p;
210 	if (*p == 0)
211 		tty.ty_comment = 0;
212 	if ((p = strchr(p, '\n')))
213 		*p = '\0';
214 	return (&tty);
215 }
216 
217 #define	QUOTED	1
218 
219 /*
220  * Skip over the current field, removing quotes, and return a pointer to
221  * the next field.
222  */
223 static char *
224 skip(char *p)
225 {
226 	char *t;
227 	int c, q;
228 
229 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
230 		if (c == '"') {
231 			q ^= QUOTED;	/* obscure, but nice */
232 			continue;
233 		}
234 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
235 			p++;
236 		*t++ = *p;
237 		if (q == QUOTED)
238 			continue;
239 		if (c == '#') {
240 			zapchar = c;
241 			*p = 0;
242 			break;
243 		}
244 		if (c == '\t' || c == ' ' || c == '\n') {
245 			zapchar = c;
246 			*p++ = 0;
247 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
248 				p++;
249 			break;
250 		}
251 	}
252 	*--t = '\0';
253 	return (p);
254 }
255 
256 static char *
257 value(char *p)
258 {
259 
260 	return ((p = strchr(p, '=')) ? ++p : NULL);
261 }
262 
263 int
264 setttyent(void)
265 {
266 
267 	if (line == NULL) {
268 		if ((line = malloc(MALLOCCHUNK)) == NULL)
269 			return (0);
270 		lbsize = MALLOCCHUNK;
271 	}
272 	if (tf) {
273 		rewind(tf);
274 		return (1);
275 	} else if ( (tf = fopen(_PATH_TTYS, "re")) )
276 		return (1);
277 	return (0);
278 }
279 
280 int
281 endttyent(void)
282 {
283 	int rval;
284 
285 	/*
286          * NB: Don't free `line' because getttynam()
287 	 * may still be referencing it
288 	 */
289 	if (tf) {
290 		rval = (fclose(tf) != EOF);
291 		tf = NULL;
292 		return (rval);
293 	}
294 	return (1);
295 }
296 
297 static int
298 isttystat(const char *tty, int flag)
299 {
300 	struct ttyent *t;
301 
302 	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
303 }
304 
305 
306 int
307 isdialuptty(const char *tty)
308 {
309 
310 	return isttystat(tty, TTY_DIALUP);
311 }
312 
313 int
314 isnettty(const char *tty)
315 {
316 
317 	return isttystat(tty, TTY_NETWORK);
318 }
319