1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
|
/*
* Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)login.c 5.25 (Berkeley) 1/6/89";
#endif /* not lint */
/*
* login [ name ]
* login -r hostname (for rlogind)
* login -h hostname (for telnetd, etc.)
* login -f name (for pre-authenticated login: datakit, xterm, etc.)
* ifdef KERBEROS
* login -e name (for pre-authenticated encrypted, must do term
* negotiation)
* login -k hostname (for Kerberos rlogind with password access)
* login -K hostname (for Kerberos rlogind with restricted access)
* endif KERBEROS
*/
#include <sys/param.h>
#ifndef VFS
#include <sys/quota.h>
#endif VFS
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <utmp.h>
#include <signal.h>
#include <lastlog.h>
#include <errno.h>
#ifndef NOTTYENT
#include <ttyent.h>
#endif /* NOTTYENT */
#include <syslog.h>
#include <grp.h>
#include <pwd.h>
#include <setjmp.h>
#include <stdio.h>
#include <strings.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "gssapi_defs.h"
#define TOKEN_MAJIC_NUMBER_BYTE0 1
#define TOKEN_MAJIC_NUMBER_BYTE1 1
char userfullname[GSS_C_MAX_PRINTABLE_NAME];
char userlocalname[GSS_C_MAX_PRINTABLE_NAME];
gss_cred_id_t gss_delegated_cred_handle;
#ifdef UIDGID_T
uid_t getuid();
#define uid_type uid_t
#define gid_type gid_t
#else
int getuid();
#define uid_type int
#define gid_type int
#endif /* UIDGID_T */
#define TTYGRPNAME "tty" /* name of group to own ttys */
#define MOTDFILE "/etc/motd"
#define MAILDIR "/usr/spool/mail"
#define NOLOGIN "/etc/nologin"
#define HUSHLOGIN ".hushlogin"
#define LASTLOG "/usr/adm/lastlog"
#define BSHELL "/bin/sh"
#ifdef VFS
#define QUOTAWARN "/usr/ucb/quota" /* warn user about quotas */
#endif VFS
#define UT_HOSTSIZE sizeof(((struct utmp *)0)->ut_host)
#define UT_NAMESIZE sizeof(((struct utmp *)0)->ut_name)
/*
* This bounds the time given to login. Not a define so it can
* be patched on machines where it's too small.
*/
int timeout = 300;
struct passwd *pwd;
char term[64], *hostname, *username;
gss_ctx_id_t context_handle;
struct sgttyb sgttyb;
struct tchars tc = {
CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
};
struct ltchars ltc = {
CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
};
extern int errno;
#ifdef POSIX
typedef void sigtype;
#else
typedef int sigtype;
#endif /* POSIX */
#define EXCL_TEST if (rflag || kflag || Kflag || eflag || \
fflag || hflag) { \
fprintf(stderr, \
"login: only one of -r, -k, -K, -e,
-h and -f allowed.\n"); \
exit(1);\
}
main(argc, argv)
int argc;
char **argv;
{
extern int optind;
extern char *optarg, **environ;
struct group *gr;
register int ch;
register char *p;
int gflag;
int fflag, hflag, pflag, rflag, cnt;
int kflag, Kflag, eflag;
int quietlog, passwd_req, ioctlval, major_status, minor_status;
sigtype timedout();
char *domain, *salt, *envinit[1], *ttyn, *tty;
char tbuf[MAXPATHLEN + 2];
char *ttyname(), *stypeof(), *crypt(), *getpass();
time_t time();
off_t lseek();
(void)signal(SIGALRM, timedout);
(void)alarm((u_int)timeout);
(void)signal(SIGQUIT, SIG_IGN);
(void)signal(SIGINT, SIG_IGN);
(void)setpriority(PRIO_PROCESS, 0, 0);
#ifndef VFS
(void)quota(Q_SETUID, 0, 0, 0);
#endif VFS
/*
* -s is used by flogind to cause the SPX autologin protocol;
* -p is used by getty to tell login not to destroy the environment
* -r is used by rlogind to cause the autologin protocol;
* -f is used to skip a second login authentication
* -e is used to skip a second login authentication, but allows
* login as root.
* -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
* -k is used by klogind to cause the Kerberos autologin protocol;
* -K is used by klogind to cause the Kerberos autologin protocol with
* restricted access.;
*/
(void)gethostname(tbuf, sizeof(tbuf));
domain = index(tbuf, '.');
fflag = hflag = pflag = rflag = kflag = Kflag = eflag = 0;
passwd_req = 1;
while ((ch = getopt(argc, argv, "feh:pr:k:K:g:")) != EOF)
switch (ch) {
case 'f':
EXCL_TEST;
fflag = 1;
break;
case 'h':
EXCL_TEST;
if (getuid()) {
fprintf(stderr,
"login: -h for super-user only.\n");
exit(1);
}
hflag = 1;
if (domain && (p = index(optarg, '.')) &&
strcmp(p, domain) == 0)
*p = 0;
hostname = optarg;
break;
case 'p':
pflag = 1;
break;
case 'r':
EXCL_TEST;
if (getuid()) {
fprintf(stderr,
"login: -r for super-user only.\n");
exit(1);
}
/* "-r hostname" must be last args */
if (optind != argc) {
fprintf(stderr, "Syntax error.\n");
exit(1);
}
rflag = 1;
passwd_req = (doremotelogin(optarg) == -1);
if (domain && (p = index(optarg, '.')) &&
!strcmp(p, domain))
*p = '\0';
hostname = optarg;
break;
case 'g':
if (optind != argc) {
fprintf(stderr, "Syntax error.\n");
exit(1);
}
gflag = do_gss_login(optarg);
if (gflag == 1) passwd_req = 0;
else {
(void)ioctl(0, TIOCHPCL, (char *)0);
sleepexitnew(1,1);
}
hostname = optarg;
break;
case '?':
default:
fprintf(stderr, "usage: login [-fp] [username]\n");
exit(1);
}
argc -= optind;
argv += optind;
if (*argv)
username = *argv;
ioctlval = 0;
(void)ioctl(0, TIOCLSET, (char *)&ioctlval);
(void)ioctl(0, TIOCNXCL, (char *)0);
(void)fcntl(0, F_SETFL, ioctlval);
(void)ioctl(0, TIOCGETP, (char *)&sgttyb);
/*
* If talking to an rlogin process, propagate the terminal type and
* baud rate across the network.
*/
if (rflag || kflag || Kflag || eflag || gflag)
doremoteterm(&sgttyb);
sgttyb.sg_erase = CERASE;
sgttyb.sg_kill = CKILL;
(void)ioctl(0, TIOCSLTC, (char *)<c);
(void)ioctl(0, TIOCSETC, (char *)&tc);
(void)ioctl(0, TIOCSETP, (char *)&sgttyb);
for (cnt = getdtablesize(); cnt > 2; cnt--)
(void) close(cnt);
ttyn = ttyname(0);
if (ttyn == NULL || *ttyn == '\0')
ttyn = "/dev/tty??";
if (tty = rindex(ttyn, '/'))
++tty;
else
tty = ttyn;
for (cnt = 0;; username = NULL) {
ioctlval = 0;
(void)ioctl(0, TIOCSETD, (char *)&ioctlval);
if (username == NULL) {
fflag = 0;
getloginname();
}
if (pwd = getpwnam(username))
salt = pwd->pw_passwd;
else
salt = "xx";
/* if user not super-user, check for disabled logins */
if (pwd == NULL || pwd->pw_uid)
checknologin();
/*
* Disallow automatic login to root; if not invoked by
* root, disallow if the uid's differ.
*/
if (fflag && pwd) {
int uid = (int) getuid();
passwd_req = pwd->pw_uid == 0 ||
(uid && uid != pwd->pw_uid);
}
/*
* If no remote login authentication and a password exists
* for this user, prompt for one and verify it.
*/
if (!passwd_req || pwd && !*pwd->pw_passwd)
break;
(void) setpriority(PRIO_PROCESS, 0, -4);
p = crypt(getpass("password:"), salt);
(void) setpriority(PRIO_PROCESS, 0, 0);
if (pwd && !strcmp(p, pwd->pw_passwd))
break;
printf("Login incorrect\n");
if (++cnt >= 5) {
if (hostname)
syslog(LOG_ERR,
"REPEATED LOGIN FAILURES ON %s FROM %.*s, %.*s",
tty, UT_HOSTSIZE, hostname, UT_NAMESIZE,
username);
else
syslog(LOG_ERR,
"REPEATED LOGIN FAILURES ON %s, %.*s",
tty, UT_NAMESIZE, username);
(void)ioctl(0, TIOCHPCL, (char *)0);
sleepexit(1);
}
}
/* committed to login -- turn off timeout */
(void)alarm((u_int)0);
/*
* If valid so far and root is logging in, see if root logins on
* this terminal are permitted.
*/
#ifndef SPX_CHALLENGE
if (pwd->pw_uid == 0 && !rootterm(tty)) {
if (hostname)
syslog(LOG_ERR, "ROOT LOGIN REFUSED ON %s FROM %.*s",
tty, UT_HOSTSIZE, hostname);
else
syslog(LOG_ERR, "ROOT LOGIN REFUSED ON %s", tty);
printf("Login incorrect\n");
sleepexit(1);
}
#else
if (pwd->pw_uid == 0) {
syslog(LOG_INFO, "%s (%s)", userfullname, userlocalname);
}
#endif /* SPX_CHALLENGE */
#ifndef VFS
if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
switch(errno) {
case EUSERS:
fprintf(stderr,
"Too many users logged on already.\nTry again later.\n");
break;
case EPROCLIM:
fprintf(stderr,
"You have too many processes running.\n");
break;
default:
perror("quota (Q_SETUID)");
}
sleepexit(0);
}
#endif /* !VFS */
if (chdir(pwd->pw_dir) < 0) {
printf("No directory %s!\n", pwd->pw_dir);
if (chdir("/"))
exit(0);
pwd->pw_dir = "/";
printf("Logging in with home = \"/\".\n");
}
/* nothing else left to fail -- really log in */
{
struct utmp utmp;
(void)time(&utmp.ut_time);
(void) strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
if (hostname)
(void) strncpy(utmp.ut_host, hostname,
sizeof(utmp.ut_host));
else
bzero(utmp.ut_host, sizeof(utmp.ut_host));
(void) strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
login(&utmp);
}
quietlog = access(HUSHLOGIN, F_OK) == 0;
dolastlog(quietlog, tty);
if (!hflag && !rflag && !kflag && !Kflag && !eflag && !gflag) { /* XXX */
static struct winsize win = { 0, 0, 0, 0 };
(void)ioctl(0, TIOCSWINSZ, (char *)&win);
}
(void)chown(ttyn, pwd->pw_uid,
(gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
(void)chmod(ttyn, 0620);
(void)setgid((gid_type) pwd->pw_gid);
(void) initgroups(username, pwd->pw_gid);
#ifndef VFS
quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
#endif
(void)setuid((uid_type) pwd->pw_uid);
if (*pwd->pw_shell == '\0')
pwd->pw_shell = BSHELL;
/* turn on new line discipline for the csh */
else if (!strcmp(pwd->pw_shell, "/bin/csh")) {
ioctlval = NTTYDISC;
(void)ioctl(0, TIOCSETD, (char *)&ioctlval);
}
/* destroy environment unless user has requested preservation */
if (!pflag)
environ = envinit;
(void)setenv("HOME", pwd->pw_dir, 1);
(void)setenv("SHELL", pwd->pw_shell, 1);
if (term[0] == '\0')
(void) strncpy(term, stypeof(tty), sizeof(term));
(void)setenv("TERM", term, 0);
(void)setenv("USER", pwd->pw_name, 1);
(void)setenv("PATH", "/usr/ucb:/bin:/usr/bin:/usr/local/bin:", 0);
major_status = gss__stash_default_cred(&minor_status,
gss_delegated_cred_handle);
if (tty[sizeof("tty")-1] == 'd')
syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
if (pwd->pw_uid == 0)
if (hostname)
syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s",
tty, UT_HOSTSIZE, hostname);
else
syslog(LOG_NOTICE, "ROOT LOGIN %s", tty);
if (!quietlog) {
struct stat st;
motd();
(void)sprintf(tbuf, "%s/%s", MAILDIR, pwd->pw_name);
if (stat(tbuf, &st) == 0 && st.st_size != 0)
printf("You have %smail.\n",
(st.st_mtime > st.st_atime) ? "new " : "");
}
#ifdef VFS
if (! access( QUOTAWARN, X_OK)) (void) system(QUOTAWARN);
#endif VFS
(void)signal(SIGALRM, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGTSTP, SIG_IGN);
tbuf[0] = '-';
(void) strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
p + 1 : pwd->pw_shell);
execlp(pwd->pw_shell, tbuf, 0);
fprintf(stderr, "login: no shell: ");
perror(pwd->pw_shell);
exit(0);
}
getloginname()
{
register int ch;
register char *p;
static char nbuf[UT_NAMESIZE + 1];
for (;;) {
printf("login: ");
for (p = nbuf; (ch = getchar()) != '\n'; ) {
if (ch == EOF)
exit(0);
if (p < nbuf + UT_NAMESIZE)
*p++ = ch;
}
if (p > nbuf)
if (nbuf[0] == '-')
fprintf(stderr,
"login names may not start with '-'.\n");
else {
*p = '\0';
username = nbuf;
break;
}
}
}
sigtype
timedout()
{
fprintf(stderr, "Login timed out after %d seconds\n", timeout);
exit(0);
}
#ifdef NOTTYENT
int root_tty_security = 0;
#endif
rootterm(tty)
char *tty;
{
#ifdef NOTTYENT
return(root_tty_security);
#else
struct ttyent *t;
return((t = getttynam(tty)) && t->ty_status&TTY_SECURE);
#endif NOTTYENT
}
jmp_buf motdinterrupt;
motd()
{
register int fd, nchars;
sigtype (*oldint)(), sigint();
char tbuf[8192];
if ((fd = open(MOTDFILE, O_RDONLY, 0)) < 0)
return;
signal(SIGINT, sigint);
if (setjmp(motdinterrupt) == 0)
while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
(void)write(fileno(stdout), tbuf, nchars);
(void)close(fd);
}
sigtype
sigint()
{
longjmp(motdinterrupt, 1);
}
checknologin()
{
register int fd, nchars;
char tbuf[8192];
if ((fd = open(NOLOGIN, O_RDONLY, 0)) >= 0) {
while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
(void)write(fileno(stdout), tbuf, nchars);
sleepexit(0);
}
}
dolastlog(quiet, tty)
int quiet;
char *tty;
{
struct lastlog ll;
int fd;
if ((fd = open(LASTLOG, O_RDWR, 0)) >= 0) {
(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
if (!quiet) {
if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
ll.ll_time != 0) {
printf("Last login: %.*s ",
24-5, (char *)ctime(&ll.ll_time));
if (*ll.ll_host != '\0')
printf("from %.*s\n",
sizeof(ll.ll_host), ll.ll_host);
else
printf("on %.*s\n",
sizeof(ll.ll_line), ll.ll_line);
}
(void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
}
(void)time(&ll.ll_time);
(void) strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
if (hostname)
(void) strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
else
(void) bzero(ll.ll_host, sizeof(ll.ll_host));
(void)write(fd, (char *)&ll, sizeof(ll));
(void)close(fd);
}
}
#undef UNKNOWN
#define UNKNOWN "su"
char *
stypeof(ttyid)
char *ttyid;
{
#ifdef NOTTYENT
return(UNKNOWN);
#else
struct ttyent *t;
return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
#endif
}
doremotelogin(host)
char *host;
{
static char lusername[UT_NAMESIZE+1];
char rusername[UT_NAMESIZE+1];
getstr(rusername, sizeof(rusername), "remuser");
getstr(lusername, sizeof(lusername), "locuser");
getstr(term, sizeof(term), "Terminal type");
username = lusername;
pwd = getpwnam(username);
if (pwd == NULL)
return(-1);
return(ruserok(host, (pwd->pw_uid == 0), rusername, username));
}
do_gss_login(host)
char *host;
{
int j, tokenlen, partlen, numbuf, i, debugflag = 0, auth_valid;
unsigned char token[GSS_C_MAX_TOKEN], *charp, *cp;
unsigned char tokenheader[4], send_tokenheader[4];
char targ_printable[GSS_C_MAX_PRINTABLE_NAME];
char lhostname[GSS_C_MAX_PRINTABLE_NAME];
unsigned char chanbinding[8];
int chanbinding_len;
static char lusername[UT_NAMESIZE+1], rusername[UT_NAMESIZE+1];
int hostlen, xcc, need_to_exit = 0;
/*
* GSS API support
*/
gss_OID_set actual_mechs;
gss_OID actual_mech_type, output_name_type;
int major_status, status, msg_ctx = 0, new_status;
int req_flags = 0, ret_flags, lifetime_rec;
gss_cred_id_t gss_cred_handle;
gss_ctx_id_t actual_ctxhandle;
gss_buffer_desc output_token, input_token, input_name_buffer;
gss_buffer_desc status_string;
gss_name_t desired_targname, src_name;
gss_channel_bindings input_chan_bindings;
j = sphinx_net_read(3, tokenheader, 4);
if ((tokenheader[0] != TOKEN_MAJIC_NUMBER_BYTE0) ||
(tokenheader[1] != TOKEN_MAJIC_NUMBER_BYTE1)) {
exit(0);
}
tokenlen = tokenheader[2] * 256 + tokenheader[3];
if (tokenlen > sizeof(token)) {
syslog(LOG_INFO, "token is too large, size is %d, buffer size
is %d", tokenlen, sizeof(token));
exit(0);
}
charp = token;
j = sphinx_net_read(3, token, tokenlen);
if (j != tokenlen)
syslog(LOG_INFO,"%d = read(3, token, %d)",j, tokenlen);
close(3);
gethostname(lhostname, sizeof(lhostname));
strcpy(targ_printable, "SERVICE:rlogin@");
strcat(targ_printable, lhostname);
/*
strcpy(targetname, lhostname);
if ((cp = index(targetname, '.')) != 0) *cp = '\0';
*/
input_name_buffer.length = strlen(targ_printable);
input_name_buffer.value = targ_printable;
major_status = gss_import_name(&status,
&input_name_buffer,
GSS_C_NULL_OID,
&desired_targname);
major_status = gss_acquire_cred(&status,
desired_targname,
0,
GSS_C_NULL_OID_SET,
GSS_C_ACCEPT,
&gss_cred_handle,
&actual_mechs,
&lifetime_rec);
major_status = gss_release_name(&status, desired_targname);
if (major_status != GSS_S_COMPLETE) {
xcc = write(0, "AuthentError", 12);
if (xcc <= 0)
syslog(LOG_INFO, "write(0, resp, 12): %m");
gss_display_status(&new_status,
status,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msg_ctx,
&status_string);
fprintf(stderr, "%s - ", status_string.value);
return(0);
}
getstr(rusername, sizeof (rusername), "remuser");
getstr(lusername, sizeof (lusername), "locuser");
getstr(term, sizeof(term), "Terminal type");
username = lusername;
pwd = getpwnam(lusername);
if (pwd == NULL) {
syslog(LOG_INFO,"passwd entry for '%s' is NULL",lusername);
/*
xcc = write(0, "Auth Error ", 12);
if (xcc <= 0)
syslog(LOG_INFO, "write(0, resp, 12): %m");
fprintf(stderr, "SPX : user account '%s' doesn't exist - ", lusername);
*/
}
if (major_status != GSS_S_COMPLETE) {
xcc = write(0, "AuthentError", 12);
if (xcc <= 0)
syslog(LOG_INFO, "write(0, resp, 12): %m");
gss_display_status(&new_status,
status,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msg_ctx,
&status_string);
fprintf(stderr, "%s - ", status_string.value);
return(0);
}
if (pwd != NULL) seteuid(pwd->pw_uid);
{
char myhost[32];
int from_addr=0, to_addr=0, myhostlen, j;
struct hostent *my_hp, *from_hp;
struct sockaddr_in sin, sin2;
from_hp=gethostbyname(host);
if (from_hp != 0) {
bcopy(from_hp->h_addr_list[0],
(caddr_t)&sin.sin_addr, from_hp->h_length);
#ifdef ultrix
from_addr = sin.sin_addr.S_un.S_addr;
#else
from_addr = sin.sin_addr.s_addr;
#endif
} else {
from_addr = inet_addr(host);
}
from_addr = htonl(from_addr);
j=gethostname(myhost, sizeof(myhost));
my_hp=gethostbyname(myhost);
if (my_hp != 0) {
bcopy(my_hp->h_addr_list[0],
(caddr_t)&sin2.sin_addr, my_hp->h_length);
#ifdef ultrix
to_addr = sin2.sin_addr.S_un.S_addr;
#else
to_addr = sin2.sin_addr.s_addr;
#endif
to_addr = htonl(to_addr);
}
input_chan_bindings = (gss_channel_bindings)
malloc(sizeof(gss_channel_bindings_desc));
input_chan_bindings->initiator_addrtype = GSS_C_AF_INET;
input_chan_bindings->initiator_address.length = 4;
input_chan_bindings->initiator_address.value = (char *) malloc(4);
input_chan_bindings->initiator_address.value[0] = ((from_addr
& 0xff000000) >> 24);
input_chan_bindings->initiator_address.value[1] = ((from_addr
& 0xff0000) >> 16);
input_chan_bindings->initiator_address.value[2] = ((from_addr
& 0xff00) >> 8);
input_chan_bindings->initiator_address.value[3] = (from_addr & 0xff);
input_chan_bindings->acceptor_addrtype = GSS_C_AF_INET;
input_chan_bindings->acceptor_address.length = 4;
input_chan_bindings->acceptor_address.value = (char *) malloc(4);
input_chan_bindings->acceptor_address.value[0] = ((to_addr &
0xff000000) >> 24);
input_chan_bindings->acceptor_address.value[1] = ((to_addr &
0xff0000) >> 16);
input_chan_bindings->acceptor_address.value[2] = ((to_addr &
0xff00) >> 8);
input_chan_bindings->acceptor_address.value[3] = (to_addr & 0xff);
input_chan_bindings->application_data.length = 0;
}
input_token.length = tokenlen;
input_token.value = token;
major_status = gss_accept_sec_context(&status,
&context_handle,
gss_cred_handle,
&input_token,
input_chan_bindings,
&src_name,
&actual_mech_type,
&output_token,
&ret_flags,
&lifetime_rec,
&gss_delegated_cred_handle);
if (output_token.length != 0) {
send_tokenheader[0] = TOKEN_MAJIC_NUMBER_BYTE0;
send_tokenheader[1] = TOKEN_MAJIC_NUMBER_BYTE1;
send_tokenheader[2] = ((output_token.length & 0xff00) >> 8);
send_tokenheader[3] = (output_token.length & 0xff);
xcc = write(0, (char *) send_tokenheader, 4);
if (xcc != 4)
syslog(LOG_INFO, "write(0, send_tokenheader, 4): %m");
xcc = write(0, (char *) output_token.value, output_token.length);
if (xcc <= 0)
syslog(LOG_INFO, "write(0, resp, %d): %m",output_token.length);
}
if (pwd == NULL) {
fprintf(stderr, "SPX : user account '%s' doesn't exist - ", lusername);
return(-1);
}
if (getuid()) {
syslog(LOG_INFO,"getuid() is 0, so return nouser");
return(0);
}
if (major_status != GSS_S_COMPLETE) {
syslog(LOG_INFO, "got error on accept\n");
gss_display_status(&new_status,
status,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msg_ctx,
&status_string);
fprintf(stderr, "%s - ", status_string.value);
return(-1);
}
#ifdef SPX_CHALLENGE
/*
* if trying to login to root account, then we need to verify response
* proving that the user is interactive.
*
*/
if (strcmp(lusername, "root")==0) {
j = sphinx_net_read(0, tokenheader, 4);
if (j != 4)
syslog(LOG_INFO,"%d = read(0, token, 4)",j);
if ((tokenheader[0] != TOKEN_MAJIC_NUMBER_BYTE0) ||
(tokenheader[1] != TOKEN_MAJIC_NUMBER_BYTE1)) {
exit(0);
}
tokenlen = tokenheader[2] * 256 + tokenheader[3];
if (tokenlen > sizeof(token)) {
syslog(LOG_INFO, "token too large, %d/%d",tokenlen,sizeof(token));
exit(0);
}
charp = token;
j = sphinx_net_read(0, token, tokenlen);
if (j != tokenlen)
syslog(LOG_INFO,"%d = read(0, token, %d)",j, tokenlen);
major_status = spx_verify_response(&status,
context_handle,
gss_cred_handle,
token,
tokenlen);
if (major_status != GSS_S_COMPLETE) {
gss_display_status(&new_status,
status,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msg_ctx,
&status_string);
fprintf(stderr, "%s - ", status_string.value);
return(0);
}
}
#endif /* SPX_CHALLENGE */
seteuid(0);
{
gss_buffer_desc fullname_buffer, luser_buffer, acl_file_buffer;
gss_buffer_desc service_buffer, resource_buffer;
gss_OID fullname_type;
int access_mode;
char acl_file[160], service[60], resource[160];
major_status = gss_display_name(&status,
src_name,
&fullname_buffer,
&fullname_type);
luser_buffer.value = lusername;
luser_buffer.length = strlen(lusername);
strcpy(acl_file, pwd->pw_dir);
strcat(acl_file, "/.sphinx");
acl_file_buffer.value = acl_file;
acl_file_buffer.length = strlen(acl_file);
strcpy(service, "flogin");
service_buffer.value = service;
service_buffer.length = 6;
resource[0] = '\0';
resource_buffer.value = resource;
resource_buffer.length = 0;
access_mode = GSS_C_READ | GSS_C_WRITE;
major_status = gss__check_authorization(&status,
&fullname_buffer,
&luser_buffer,
&acl_file_buffer,
&service_buffer,
access_mode,
&resource_buffer);
if (major_status != GSS_S_COMPLETE) {
if (strcmp(lusername, "root")==0)
syslog(LOG_INFO, "root authorization denied - '%s'", src_name);
fprintf(stderr, "SPX : authorization denied to user account
'%s' - ", lusername);
return(-1);
} else {
strcpy(userfullname, src_name);
strcpy(userlocalname, rusername);
}
major_status = gss_release_buffer(&status, &fullname_buffer);
return(1);
}
}
getstr(buf, cnt, err)
char *buf, *err;
int cnt;
{
char ch;
do {
if (read(0, &ch, sizeof(ch)) != sizeof(ch))
exit(1);
if (--cnt < 0) {
fprintf(stderr, "%s too long\r\n", err);
sleepexit(1);
}
*buf++ = ch;
} while (ch);
}
char *speeds[] = {
"0", "50", "75", "110", "134", "150", "200", "300", "600",
"1200", "1800", "2400", "4800", "9600", "19200", "38400",
};
#define NSPEEDS (sizeof(speeds) / sizeof(speeds[0]))
doremoteterm(tp)
struct sgttyb *tp;
{
register char *cp = index(term, '/'), **cpp;
char *speed;
if (cp) {
*cp++ = '\0';
speed = cp;
cp = index(speed, '/');
if (cp)
*cp++ = '\0';
for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
if (strcmp(*cpp, speed) == 0) {
tp->sg_ispeed = tp->sg_ospeed = cpp-speeds;
break;
}
}
tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
}
sleepexitnew(eval, interval)
int eval, interval;
{
sleep((u_int)interval);
exit(eval);
}
sleepexit(eval)
int eval;
{
sleep((u_int)5);
exit(eval);
}
|