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