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