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