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