static char *sccsid
= "@(#)login.c 4.30 (Berkeley) 83/06/13";
* login -r hostname (for rlogind)
* login -h hostname (for telnetd, etc.)
#include <sys/resource.h>
#define SCPYN(a, b) strncpy(a, b, sizeof(a))
#define NMAX sizeof(utmp.ut_name)
char nolog
[] = "/etc/nologin";
char qlog
[] = ".hushlogin";
char securetty
[] = "/etc/securetty";
char maildir
[30] = "/usr/spool/mail/";
char lastlog
[] = "/usr/adm/lastlog";
struct passwd nouser
= {"", "nope", -1, -1, -1, "", "", "", "" };
* This bounds the time given to login. We initialize it here
* so it can be patched on machines where it's too small.
char homedir
[64] = "HOME=";
char shell
[64] = "SHELL=";
{ homedir
, shell
, "PATH=:/usr/ucb:/bin:/usr/bin", term
, user
, 0 };
struct passwd
*getpwnam();
char *strcat(), *rindex(), *index();
CINTR
, CQUIT
, CSTART
, CSTOP
, CEOT
, CBRK
CSUSP
, CDSUSP
, CRPRNT
, CFLUSH
, CWERASE
, CLNEXT
char rusername
[NMAX
+1], lusername
[NMAX
+1];
signal(SIGALRM
, timedout
);
signal(SIGQUIT
, SIG_IGN
);
setpriority(PRIO_PROCESS
, 0, 0);
quota(Q_SETUID
, 0, 0, 0);
* -r is used by rlogind to cause the autologin protocol;
* -h is used by other servers to pass the name of the
* remote host to login so that it may be placed in utmp and wtmp
if (strcmp(argv
[1], "-r") == 0) {
rflag
= doremotelogin(argv
[2]);
SCPYN(utmp
.ut_host
, argv
[2]);
if (strcmp(argv
[1], "-h") == 0 && getuid() == 0) {
SCPYN(utmp
.ut_host
, argv
[2]);
ioctl(0, TIOCLSET
, &zero
);
ioctl(0, FIONBIO
, &zero
);
ioctl(0, FIOASYNC
, &zero
);
ioctl(0, TIOCGETP
, &ttyb
);
* If talking to an rlogin process,
* propagate the terminal type and
* baud rate across the network.
doremoteterm(term
, &ttyb
);
ioctl(0, TIOCSLTC
, <c
);
ioctl(0, TIOCSETP
, &ttyb
);
for (t
= getdtablesize(); t
> 3; t
--)
ioctl(0, TIOCSETD
, &ldisc
);
* Name specified, take it.
SCPYN(utmp
.ut_name
, argv
[1]);
* If remote login take given name,
* otherwise prompt user for something.
SCPYN(utmp
.ut_name
, lusername
);
/* autologin failed, prompt for passwd */
if (!strcmp(pwd
->pw_shell
, "/bin/csh")) {
ioctl(0, TIOCSETD
, &ldisc
);
* If no remote login authentication and
* a password exists for this user, prompt
if (!rflag
&& *pwd
->pw_passwd
!= '\0') {
setpriority(PRIO_PROCESS
, 0, -4);
pp
= getpass("Password:");
namep
= crypt(pp
, pwd
->pw_passwd
);
setpriority(PRIO_PROCESS
, 0, 0);
if (strcmp(namep
, pwd
->pw_passwd
))
* If user not super-user, check for logins disabled.
if (pwd
->pw_uid
!= 0 && (nlfd
= fopen(nolog
, "r")) > 0) {
while ((c
= getc(nlfd
)) != EOF
)
* If valid so far and root is logging in,
* see if root logins on this terminal are permitted.
if (!invalid
&& pwd
->pw_uid
== 0 &&
!rootterm(ttyn
+sizeof("/dev/")-1)) {
logerr("ROOT LOGIN REFUSED %s",
printf("Login incorrect\n");
if (ttyn
[sizeof("/dev/tty")-1] == 'd')
logerr("BADDIALUP %s %s\n",
ttyn
+sizeof("/dev/")-1, utmp
.ut_name
);
if (*pwd
->pw_shell
== '\0')
pwd
->pw_shell
= "/bin/sh";
i
= strlen(pwd
->pw_shell
);
if (chdir(pwd
->pw_dir
) < 0 && !invalid
) {
printf("No directory!\n");
printf("No directory! %s\n",
"Logging in with home=/");
* Remote login invalid must have been because
* of a restriction of some sort, no extra chances.
/* committed to login turn off timeout */
if (quota(Q_SETUID
, pwd
->pw_uid
, 0, 0) < 0) {
"Too many users logged on already",
else if (errno
== EPROCLIM
)
printf("You have too many processes running.\n");
if (t
> 0 && (f
= open("/etc/utmp", 1)) >= 0) {
lseek(f
, (long)(t
*sizeof(utmp
)), 0);
SCPYN(utmp
.ut_line
, rindex(ttyn
, '/')+1);
write(f
, (char *)&utmp
, sizeof(utmp
));
if (t
> 0 && (f
= open("/usr/adm/wtmp", 1)) >= 0) {
write(f
, (char *)&utmp
, sizeof(utmp
));
quietlog
= access(qlog
, 0) == 0;
if ((f
= open(lastlog
, 2)) >= 0) {
lseek(f
, (long)pwd
->pw_uid
* sizeof (struct lastlog
), 0);
if (read(f
, (char *) &ll
, sizeof ll
) == sizeof ll
&&
ll
.ll_time
!= 0 && !quietlog
) {
printf("Last login: %.*s ",
24-5, (char *)ctime(&ll
.ll_time
));
sizeof (ll
.ll_host
), ll
.ll_host
);
sizeof (ll
.ll_line
), ll
.ll_line
);
lseek(f
, (long)pwd
->pw_uid
* sizeof (struct lastlog
), 0);
SCPYN(ll
.ll_line
, rindex(ttyn
, '/')+1);
SCPYN(ll
.ll_host
, utmp
.ut_host
);
write(f
, (char *) &ll
, sizeof ll
);
chown(ttyn
, pwd
->pw_uid
, pwd
->pw_gid
);
strncpy(name
, utmp
.ut_name
, NMAX
);
initgroups(name
, pwd
->pw_gid
);
quota(Q_DOWARN
, pwd
->pw_uid
, (dev_t
)-1, 0);
strncat(homedir
, pwd
->pw_dir
, sizeof(homedir
)-6);
strncat(shell
, pwd
->pw_shell
, sizeof(shell
)-7);
if (term
[strlen("TERM=")] == 0)
strncat(term
, stypeof(ttyn
), sizeof(term
)-6);
strncat(user
, pwd
->pw_name
, sizeof(user
)-6);
if ((namep
= rindex(pwd
->pw_shell
, '/')) == NULL
)
if (ttyn
[sizeof("/dev/tty")-1] == 'd')
logerr("DIALUP %s %s\n", ttyn
+sizeof("/dev/")-1, pwd
->pw_name
);
strcat(maildir
, pwd
->pw_name
);
if (access(maildir
,4)==0) {
printf("You have mail.\n");
signal(SIGALRM
, SIG_DFL
);
signal(SIGQUIT
, SIG_DFL
);
signal(SIGTSTP
, SIG_IGN
);
execlp(pwd
->pw_shell
, minusnam
, 0);
register struct utmp
*up
;
while (up
->ut_name
[0] == '\0') {
while ((c
= getchar()) != '\n') {
if (namep
< up
->ut_name
+NMAX
)
if ((pwd
= getpwnam(utmp
.ut_name
)) == NULL
)
printf("Login timed out after %d seconds\n", timeout
);
if ((fd
= fopen(securetty
, "r")) == NULL
)
while (fgets(buf
, sizeof buf
, fd
) != NULL
) {
buf
[strlen(buf
)-1] = '\0';
if (strcmp(tty
, buf
) == 0) {
if ((mf
= fopen("/etc/motd","r")) != NULL
) {
while ((c
= getc(mf
)) != EOF
&& stopmotd
== 0)
register char *p
, *t
, *q
;
f
= fopen("/etc/ttytype", "r");
/* split off end of name */
for (p
= q
= ttyid
; *p
!= 0; p
++)
while (fgets(buf
, sizeof buf
, f
) != NULL
) {
for (t
= buf
; *t
!= ' ' && *t
!= '\t'; t
++)
while (*t
== ' ' || *t
== '\t')
for (p
= t
; *p
> ' '; p
++)
getstr(rusername
, sizeof (rusername
), "remuser");
getstr(lusername
, sizeof (lusername
), "locuser");
getstr(term
+5, sizeof(term
)-5, "Terminal type");
pwd
= getpwnam(lusername
);
hostf
= pwd
->pw_uid
? fopen("/etc/hosts.equiv", "r") : 0;
while (fgets(ahost
, sizeof (ahost
), hostf
)) {
if ((user
= index(ahost
, '\n')) != 0)
if ((user
= index(ahost
, ' ')) != 0)
if (!strcmp(host
, ahost
) &&
!strcmp(rusername
, user
? user
: lusername
)) {
char *rhosts
= ".rhosts";
if (chdir(pwd
->pw_dir
) < 0)
if (lstat(rhosts
, &sbuf
) < 0)
if ((sbuf
.st_mode
& S_IFMT
) == S_IFLNK
) {
printf("login: .rhosts is a soft link.\r\n");
hostf
= fopen(rhosts
, "r");
fstat(fileno(hostf
), &sbuf
);
if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
) {
printf("login: Bad .rhosts ownership.\r\n");
printf("%s too long\r\n", err
);
{ "0", "50", "75", "110", "134", "150", "200", "300",
"600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
#define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
char *cp
= index(term
, '/');
for (i
= 0; i
< NSPEEDS
; i
++)
if (!strcmp(speeds
[i
], cp
)) {
tp
->sg_ispeed
= tp
->sg_ospeed
= i
;
tp
->sg_flags
= ECHO
|CRMOD
|ANYP
|XTABS
;
char *fmt
, *a1
, *a2
, *a3
;
FILE *cons
= fopen("/dev/console", "w");
fprintf(cons
, fmt
, a1
, a2
, a3
);