143310Skfall /*- 243310Skfall * Copyright (c) 1990 The Regents of the University of California. 343310Skfall * All rights reserved. 443310Skfall * 543310Skfall * %sccs.include.redist.c% 643310Skfall */ 743310Skfall 843310Skfall #ifndef lint 9*49958Skarels static char sccsid[] = "@(#)klogin.c 5.9 (Berkeley) 06/02/91"; 1043310Skfall #endif /* not lint */ 1143310Skfall 1243310Skfall #ifdef KERBEROS 1343310Skfall #include <sys/param.h> 1443310Skfall #include <sys/syslog.h> 1543310Skfall #include <kerberosIV/des.h> 1643310Skfall #include <kerberosIV/krb.h> 1743310Skfall #include <pwd.h> 1843310Skfall #include <netdb.h> 1943310Skfall 2043310Skfall #define INITIAL_TICKET "krbtgt" 2143310Skfall #define VERIFY_SERVICE "rcmd" 2243310Skfall 2343310Skfall extern int notickets; 24*49958Skarels extern char *krbtkfile_env; 2543310Skfall 2643310Skfall /* 2743367Skfall * Attempt to log the user in using Kerberos authentication 2843367Skfall * 2943367Skfall * return 0 on success (will be logged in) 3043367Skfall * 1 if Kerberos failed (try local password in login) 3143310Skfall */ 3243310Skfall 3349956Skarels klogin(pw, instance, localhost, password) 3443310Skfall struct passwd *pw; 3549956Skarels char *instance, *localhost, *password; 3643310Skfall { 3743310Skfall int kerror; 3843310Skfall AUTH_DAT authdata; 3943310Skfall KTEXT_ST ticket; 4043310Skfall struct hostent *hp; 4143310Skfall unsigned long faddr; 4243367Skfall char realm[REALM_SZ], savehost[MAXHOSTNAMELEN]; 4343367Skfall char tkt_location[MAXPATHLEN]; 4443310Skfall 4543310Skfall /* 4649956Skarels * Root logins don't use Kerberos. 4749956Skarels * If we have a realm, try getting a ticket-granting ticket 4849956Skarels * and using it to authenticate. Otherwise, return 4949956Skarels * failure so that we can try the normal passwd file 5049956Skarels * for a password. If that's ok, log the user in 5149956Skarels * without issuing any tickets. 5243310Skfall */ 5349956Skarels if (strcmp(pw->pw_name, "root") == 0 || 5449956Skarels krb_get_lrealm(realm, 0) != KSUCCESS) 5549956Skarels return (1); 5643310Skfall 5743310Skfall /* 5843367Skfall * get TGT for local realm 5949956Skarels * tickets are stored in a file named TKT_ROOT plus uid 60*49958Skarels * except for user.root tickets. 6143310Skfall */ 6243310Skfall 63*49958Skarels if (strcmp(instance, "root") != 0) 64*49958Skarels (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid); 65*49958Skarels else { 66*49958Skarels (void)sprintf(tkt_location, "%s_root_%d", TKT_ROOT, pw->pw_uid); 67*49958Skarels krbtkfile_env = tkt_location; 68*49958Skarels } 6943367Skfall (void)krb_set_tkt_string(tkt_location); 7043310Skfall 71*49958Skarels /* 72*49958Skarels * Set real as well as effective ID to 0 for the moment, 73*49958Skarels * to make the kerberos library do the right thing. 74*49958Skarels */ 75*49958Skarels if (setuid(0) < 0) { 76*49958Skarels perror("login: setuid"); 77*49958Skarels return (1); 78*49958Skarels } 7949956Skarels kerror = krb_get_pw_in_tkt(pw->pw_name, instance, 8043367Skfall realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password); 8143310Skfall /* 8243310Skfall * If we got a TGT, get a local "rcmd" ticket and check it so as to 8343310Skfall * ensure that we are not talking to a bogus Kerberos server. 8443310Skfall * 8543310Skfall * There are 2 cases where we still allow a login: 8643310Skfall * 1: the VERIFY_SERVICE doesn't exist in the KDC 8743310Skfall * 2: local host has no srvtab, as (hopefully) indicated by a 8843310Skfall * return value of RD_AP_UNDEC from krb_rd_req(). 8943310Skfall */ 9043310Skfall if (kerror != INTK_OK) { 9149956Skarels if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) { 9243310Skfall syslog(LOG_ERR, "Kerberos intkt error: %s", 9343310Skfall krb_err_txt[kerror]); 9447683Skfall dest_tkt(); 9547683Skfall } 9649956Skarels return (1); 9743310Skfall } 9843310Skfall 9943367Skfall if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0) 10043367Skfall syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE); 10143310Skfall 10243310Skfall (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost)); 10343310Skfall savehost[sizeof(savehost)-1] = NULL; 10443310Skfall 10543310Skfall /* 10643310Skfall * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host, 10743310Skfall * still allow login with tickets, but log the error condition. 10843310Skfall */ 10943367Skfall 11043367Skfall kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33); 11143310Skfall if (kerror == KDC_PR_UNKNOWN) { 11249956Skarels syslog(LOG_NOTICE, 11349956Skarels "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?", 11449956Skarels krb_err_txt[kerror], VERIFY_SERVICE, savehost); 11543310Skfall notickets = 0; 11643310Skfall return(0); 11743310Skfall } 11843310Skfall 11943310Skfall if (kerror != KSUCCESS) { 12043310Skfall (void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]); 12143310Skfall syslog(LOG_NOTICE, "unable to use TGT: (%s)", 12243310Skfall krb_err_txt[kerror]); 12343310Skfall dest_tkt(); 12443367Skfall return(1); 12543310Skfall } 12643310Skfall 12743310Skfall if (!(hp = gethostbyname(localhost))) { 12843310Skfall syslog(LOG_ERR, "couldn't get local host address"); 12943367Skfall dest_tkt(); 13043367Skfall return(1); 13143310Skfall } 13243367Skfall 13343310Skfall bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr)); 13443310Skfall 13543310Skfall kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr, 13643310Skfall &authdata, ""); 13743367Skfall 13843310Skfall if (kerror == KSUCCESS) { 13943310Skfall notickets = 0; 14043310Skfall return(0); 14143310Skfall } 14243367Skfall 14343367Skfall /* undecipherable: probably didn't have a srvtab on the local host */ 14443310Skfall if (kerror = RD_AP_UNDEC) { 14543310Skfall syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]); 14643367Skfall dest_tkt(); 14743367Skfall return(1); 14843310Skfall } 14943367Skfall /* failed for some other reason */ 15043310Skfall (void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE, 15143310Skfall krb_err_txt[kerror]); 15243310Skfall syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE, 15343310Skfall krb_err_txt[kerror]); 15443367Skfall dest_tkt(); 15543367Skfall return(1); 15643310Skfall } 15743310Skfall #endif 158