1 /* $NetBSD: main.c,v 1.64 2013/08/12 13:54:33 joerg Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "from: @(#)main.c 8.1 (Berkeley) 6/20/93";
43 __RCSID("$NetBSD: main.c,v 1.64 2013/08/12 13:54:33 joerg Exp $");
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/utsname.h>
71 #include "pathnames.h"
74 extern char editedhost
[];
77 * Set the amount of running time that getty should accumulate
78 * before deciding that something is wrong and exit.
80 #define GETTY_TIMEOUT 60 /* seconds */
82 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
84 #define PPP_FRAME 0x7e /* PPP Framing character */
85 #define PPP_STATION 0xff /* "All Station" character */
86 #define PPP_ESCAPE 0x7d /* Escape Character */
87 #define PPP_CONTROL 0x03 /* PPP Control Field */
88 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
89 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
90 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
92 struct termios tmode
, omode
;
94 int crmod
, digit_or_punc
, lower
, upper
;
96 char hostname
[MAXHOSTNAMELEN
+ 1];
97 struct utsname kerninfo
;
98 char name
[LOGIN_NAME_MAX
];
99 char dev
[] = _PATH_DEV
;
106 #define TABBUFSIZ 512
108 char defent
[TABBUFSIZ
];
109 char tabent
[TABBUFSIZ
];
113 const unsigned char partab
[] = {
114 0001,0201,0201,0001,0201,0001,0001,0201,
115 0202,0004,0003,0205,0005,0206,0201,0001,
116 0201,0001,0001,0201,0001,0201,0201,0001,
117 0001,0201,0201,0001,0201,0001,0001,0201,
118 0200,0000,0000,0200,0000,0200,0200,0000,
119 0000,0200,0200,0000,0200,0000,0000,0200,
120 0000,0200,0200,0000,0200,0000,0000,0200,
121 0200,0000,0000,0200,0000,0200,0200,0000,
122 0200,0000,0000,0200,0000,0200,0200,0000,
123 0000,0200,0200,0000,0200,0000,0000,0200,
124 0000,0200,0200,0000,0200,0000,0000,0200,
125 0200,0000,0000,0200,0000,0200,0200,0000,
126 0000,0200,0200,0000,0200,0000,0000,0200,
127 0200,0000,0000,0200,0000,0200,0200,0000,
128 0200,0000,0000,0200,0000,0200,0200,0000,
129 0000,0200,0200,0000,0200,0000,0000,0201
132 #define ERASE tmode.c_cc[VERASE]
133 #define KILL tmode.c_cc[VKILL]
134 #define EOT tmode.c_cc[VEOF]
136 static void clearscreen(void);
146 (void)signal(SIGALRM
, SIG_DFL
);
147 siglongjmp(timeout
, 1);
157 (void)signal(SIGINT
, interrupt
);
158 siglongjmp(intrupt
, 1);
161 #if !defined(__minix)
163 * Action to take when getty is running too long.
167 timeoverrun(int signo
)
170 syslog(LOG_ERR
, "getty exiting due to excessive running time");
173 #endif /* !defined(__minix) */
175 static int getname(void);
176 static void oflush(void);
177 static void prompt(void);
178 static int putchr(int);
179 static void putf(const char *);
180 static void xputs(const char *);
182 #define putpad(s) tputs(s, 1, putchr)
185 main(int argc
, char *argv
[], char *envp
[])
187 const char *progname
;
188 int repcnt
= 0, failopenlogged
= 0, first_time
= 1;
192 /* this is used past the siglongjmp, so make sure it is not cached
193 in registers that might become invalid. */
194 volatile int uugetty
= 0;
195 const char * volatile tname
= "default";
197 (void)signal(SIGINT
, SIG_IGN
);
198 openlog("getty", LOG_PID
, LOG_AUTH
);
199 (void)gethostname(hostname
, sizeof(hostname
));
200 hostname
[sizeof(hostname
) - 1] = '\0';
201 if (hostname
[0] == '\0')
202 (void)strlcpy(hostname
, "Amnesiac", sizeof(hostname
));
203 (void)uname(&kerninfo
);
205 progname
= getprogname();
206 if (progname
[0] == 'u' && progname
[1] == 'u')
210 * Find id of uucp login (if present) so we can chown tty properly.
212 if (uugetty
&& (pw
= getpwnam("uucp")))
213 ttyowner
= pw
->pw_uid
;
218 * Limit running time to deal with broken or dead lines.
220 #if !defined(__minix)
221 (void)signal(SIGXCPU
, timeoverrun
);
222 #endif /* !defined(__minix) */
223 limit
.rlim_max
= RLIM_INFINITY
;
224 limit
.rlim_cur
= GETTY_TIMEOUT
;
225 (void)setrlimit(RLIMIT_CPU
, &limit
);
228 * The following is a work around for vhangup interactions
229 * which cause great problems getting window systems started.
230 * If the tty line is "-", we do the old style getty presuming
231 * that the file descriptors are already set up for us.
232 * J. Gettys - MIT Project Athena.
234 if (argc
<= 2 || strcmp(argv
[2], "-") == 0) {
235 (void)strlcpy(ttyn
, ttyname(0), sizeof(ttyn
));
241 (void)strlcpy(ttyn
, dev
, sizeof(ttyn
));
242 (void)strlcat(ttyn
, argv
[2], sizeof(ttyn
));
244 (void)chown(ttyn
, ttyowner
, 0);
245 (void)strlcpy(lockfile
, _PATH_LOCK
,
247 (void)strlcat(lockfile
, argv
[2],
250 * wait for lockfiles to go away before we try
253 if (pidlock(lockfile
, 0, 0, 0) != 0) {
255 "%s: can't create lockfile", ttyn
);
258 (void)unlink(lockfile
);
260 if (strcmp(argv
[0], "+") != 0) {
261 (void)chown(ttyn
, ttyowner
, 0);
262 (void)chmod(ttyn
, 0600);
263 #if !defined(__minix)
265 #endif /* !defined(__minix) */
266 if (ttyaction(ttyn
, "getty", "root"))
267 syslog(LOG_WARNING
, "%s: ttyaction failed",
270 * Delay the open so DTR stays down long enough
274 while ((i
= open(ttyn
, O_RDWR
)) == -1) {
275 if ((repcnt
% 10 == 0) &&
276 (errno
!= ENXIO
|| !failopenlogged
)) {
277 syslog(LOG_WARNING
, "%s: %m", ttyn
);
284 if (uugetty
&& pidlock(lockfile
, 0, 0, 0) != 0) {
285 syslog(LOG_ERR
, "%s: can't create lockfile",
290 (void)chown(lockfile
, ttyowner
, 0);
295 /* Start with default tty settings */
296 if (tcgetattr(0, &tmode
) < 0) {
297 syslog(LOG_ERR
, "%s: %m", ttyn
);
302 gettable("default", defent
);
310 gettable(tname
, tabent
);
311 if (OPset
|| EPset
|| APset
)
312 APset
++, OPset
++, EPset
++;
315 (void)tcflush(0, TCIOFLUSH
); /* clear out the crap */
317 (void)ioctl(0, FIONBIO
, &off
); /* turn off non-blocking mode */
318 (void)ioctl(0, FIOASYNC
, &off
); /* ditto for async mode */
319 #endif /* !defined(__minix) */
322 (void)cfsetispeed(&tmode
, (speed_t
)IS
);
324 (void)cfsetispeed(&tmode
, (speed_t
)SP
);
326 (void)cfsetospeed(&tmode
, (speed_t
)OS
);
328 (void)cfsetospeed(&tmode
, (speed_t
)SP
);
331 if (tcsetattr(0, TCSANOW
, &tmode
) < 0) {
332 syslog(LOG_ERR
, "%s: %m", ttyn
);
340 tname
= portselector();
350 * If this is the first time through this, and an
351 * issue file has been given, then send it.
353 if (first_time
!= 0 && IF
!= NULL
) {
354 char buf
[_POSIX2_LINE_MAX
];
357 if ((fp
= fopen(IF
, "r")) != NULL
) {
358 while (fgets(buf
, sizeof(buf
) - 1, fp
) != NULL
)
368 if (sigsetjmp(timeout
, 1)) {
369 tmode
.c_ispeed
= tmode
.c_ospeed
= 0;
370 (void)tcsetattr(0, TCSANOW
, &tmode
);
374 (void)signal(SIGALRM
, dingdong
);
375 (void)alarm((unsigned int)TO
);
380 upper
= digit_or_punc
= 0;
385 while (*p
&& q
< &name
[sizeof name
- 1]) {
386 if (isupper((unsigned char)*p
))
388 else if (islower((unsigned char)*p
))
390 else if (isdigit((unsigned char)*p
))
394 } else if ((rval
= getname()) == 2) {
396 (void)execle(PP
, "ppplogin", ttyn
, (char *) 0, env
);
397 syslog(LOG_ERR
, "%s: %m", PP
);
401 if (rval
|| AL
|| NN
) {
406 (void)signal(SIGALRM
, SIG_DFL
);
407 if (name
[0] == '-') {
408 xputs("user names may not start with '-'.");
411 if (!(upper
|| lower
|| digit_or_punc
))
415 tmode
.c_iflag
|= ICRNL
;
416 tmode
.c_oflag
|= ONLCR
;
420 tmode
.sg_flags
|= LCASE
;
422 tmode
.sg_flags
&= ~LCASE
;
424 if (tcsetattr(0, TCSANOW
, &tmode
) < 0) {
425 syslog(LOG_ERR
, "%s: %m", ttyn
);
428 (void)signal(SIGINT
, SIG_DFL
);
429 for (i
= 0; envp
[i
] != NULL
; i
++)
433 limit
.rlim_max
= RLIM_INFINITY
;
434 limit
.rlim_cur
= RLIM_INFINITY
;
435 (void)setrlimit(RLIMIT_CPU
, &limit
);
437 (void)execle(LO
, "login", AL
? "-fp" : "-p",
440 (void)execle(LO
, "login", AL
? "-fp" : "-p",
441 "--", name
, NULL
, env
);
442 syslog(LOG_ERR
, "%s: %m", LO
);
446 (void)signal(SIGALRM
, SIG_DFL
);
447 (void)signal(SIGINT
, SIG_IGN
);
451 (void)unlink(lockfile
);
461 int ppp_state
, ppp_connection
;
464 * Interrupt may happen if we use CBREAK mode
466 if (sigsetjmp(intrupt
, 1)) {
467 (void)signal(SIGINT
, SIG_IGN
);
470 (void)signal(SIGINT
, interrupt
);
475 (void)sleep((unsigned int)PF
);
478 if (tcsetattr(0, TCSANOW
, &tmode
) < 0) {
479 syslog(LOG_ERR
, "%s: %m", ttyn
);
482 crmod
= digit_or_punc
= lower
= upper
= 0;
483 ppp_state
= ppp_connection
= 0;
487 if (read(STDIN_FILENO
, &cs
, 1) <= 0)
489 if ((c
= cs
&0177) == 0)
493 * PPP detection state machine..
494 * Look for sequences:
495 * PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
496 * PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
498 * Derived from code from Michael Hancock <michaelh@cet.co.jp>
499 * and Erik 'PPP' Olson <eriko@wrq.com>
501 if (PP
&& cs
== PPP_FRAME
) {
503 } else if (ppp_state
== 1 && cs
== PPP_STATION
) {
505 } else if (ppp_state
== 2 && cs
== PPP_ESCAPE
) {
507 } else if ((ppp_state
== 2 && cs
== PPP_CONTROL
) ||
508 (ppp_state
== 3 && cs
== PPP_CONTROL_ESCAPED
)) {
510 } else if (ppp_state
== 4 && cs
== PPP_LCP_HI
) {
512 } else if (ppp_state
== 5 && cs
== PPP_LCP_LOW
) {
521 if (c
== '\r' || c
== '\n' ||
522 np
>= &name
[LOGIN_NAME_MAX
- 1]) {
531 else if (c
== ERASE
|| c
== '#' || c
== '\b') {
534 if (cfgetospeed(&tmode
) >= 1200)
540 } else if (c
== KILL
|| c
== '@') {
543 if (cfgetospeed(&tmode
) < 1200)
545 /* this is the way they do it down under ... */
552 } else if (isdigit(c
) || c
== '_')
554 if (IG
&& (c
<= ' ' || c
> 0176))
560 * An MS-Windows direct connect PPP "client" won't send its
561 * first PPP packet until we respond to its "CLIENT" poll
562 * with a CRLF sequence. We cater to yet another broken
563 * implementation of a previously-standard protocol...
566 if (strstr(name
, "CLIENT"))
569 (void)signal(SIGINT
, SIG_IGN
);
573 if ((upper
&& !lower
&& !LC
) || UC
)
574 for (np
= name
; *np
; np
++)
575 *np
= tolower((unsigned char)*np
);
576 return (1 + ppp_connection
);
586 char outbuf
[OBUFSIZ
];
596 c
|= partab
[c
&0177] & 0200;
601 outbuf
[obufcnt
++] = c
;
602 if (obufcnt
>= OBUFSIZ
)
606 return write(STDOUT_FILENO
, &c
, 1);
613 (void)write(STDOUT_FILENO
, outbuf
, obufcnt
);
630 char *slash
, db
[100];
640 if ((slash
= strstr(ttyn
, "/pts/")) == NULL
)
641 slash
= strrchr(ttyn
, '/');
654 (void)strftime(db
, sizeof(db
),
655 "%l:%M%p on %A, %d %B %Y", localtime(&t
));
660 xputs(kerninfo
.sysname
);
664 xputs(kerninfo
.machine
);
668 xputs(kerninfo
.release
);
672 xputs(kerninfo
.version
);
693 typ
= getttynam(rawttyn
);
695 if ((typ
== NULL
) || (typ
->ty_type
== NULL
) ||
696 (typ
->ty_type
[0] == 0))
699 if (setupterm(typ
->ty_type
, 0, &err
) == ERR
)
703 putpad(clear_screen
);
705 del_curterm(cur_term
);