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*45209Skfall static char sccsid[] = "@(#)klogin.c 5.4 (Berkeley) 09/05/90"; 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 PRINCIPAL_NAME pw->pw_name 2143310Skfall #define PRINCIPAL_INST "" 2243310Skfall #define INITIAL_TICKET "krbtgt" 2343310Skfall #define VERIFY_SERVICE "rcmd" 2443310Skfall 2543310Skfall extern int notickets; 2643310Skfall 2743310Skfall /* 2843367Skfall * Attempt to log the user in using Kerberos authentication 2943367Skfall * 3043367Skfall * return 0 on success (will be logged in) 3143367Skfall * 1 if Kerberos failed (try local password in login) 3243310Skfall */ 3343310Skfall 3443367Skfall klogin(pw, localhost, password) 3543310Skfall struct passwd *pw; 3643367Skfall char *localhost, *password; 3743310Skfall { 3843310Skfall int kerror; 3943310Skfall AUTH_DAT authdata; 4043310Skfall KTEXT_ST ticket; 4143310Skfall struct hostent *hp; 4243310Skfall unsigned long faddr; 4343367Skfall char realm[REALM_SZ], savehost[MAXHOSTNAMELEN]; 4443367Skfall char tkt_location[MAXPATHLEN]; 4543310Skfall 4643310Skfall /* 4743310Skfall * If we aren't Kerberos-authenticated, try the normal pw file 4843310Skfall * for a password. If that's ok, log the user in without issueing 4943310Skfall * any tickets. 5043310Skfall */ 51*45209Skfall if (krb_get_lrealm(realm, 0) != KSUCCESS) { 52*45209Skfall syslog(LOG_ERR, "couldn't get local Kerberos realm"); 5343310Skfall return(1); 54*45209Skfall } 5543310Skfall 5643310Skfall /* 5743367Skfall * get TGT for local realm 5843367Skfall * tickets are stored in a file determined by calling tkt_string() 5943310Skfall */ 6043310Skfall 6143367Skfall (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid); 6243367Skfall (void)krb_set_tkt_string(tkt_location); 6343367Skfall (void)dest_tkt(); 6443310Skfall 6543367Skfall kerror = krb_get_pw_in_tkt(PRINCIPAL_NAME, PRINCIPAL_INST, 6643367Skfall realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password); 6743310Skfall /* 6843310Skfall * If we got a TGT, get a local "rcmd" ticket and check it so as to 6943310Skfall * ensure that we are not talking to a bogus Kerberos server. 7043310Skfall * 7143310Skfall * There are 2 cases where we still allow a login: 7243310Skfall * 1: the VERIFY_SERVICE doesn't exist in the KDC 7343310Skfall * 2: local host has no srvtab, as (hopefully) indicated by a 7443310Skfall * return value of RD_AP_UNDEC from krb_rd_req(). 7543310Skfall */ 7643310Skfall if (kerror != INTK_OK) { 7743367Skfall dest_tkt(); 7843310Skfall if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) 7943310Skfall syslog(LOG_ERR, "Kerberos intkt error: %s", 8043310Skfall krb_err_txt[kerror]); 8143367Skfall return(1); 8243310Skfall } 8343310Skfall 8443367Skfall if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0) 8543367Skfall syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE); 8643310Skfall 8743310Skfall (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost)); 8843310Skfall savehost[sizeof(savehost)-1] = NULL; 8943310Skfall 9043310Skfall /* 9143310Skfall * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host, 9243310Skfall * still allow login with tickets, but log the error condition. 9343310Skfall */ 9443367Skfall 9543367Skfall kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33); 9643310Skfall if (kerror == KDC_PR_UNKNOWN) { 9743310Skfall syslog(LOG_NOTICE, "warning: TGT not verified (%s)", 9843310Skfall krb_err_txt[kerror]); 9943310Skfall notickets = 0; 10043310Skfall return(0); 10143310Skfall } 10243310Skfall 10343310Skfall if (kerror != KSUCCESS) { 10443310Skfall (void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]); 10543310Skfall syslog(LOG_NOTICE, "unable to use TGT: (%s)", 10643310Skfall krb_err_txt[kerror]); 10743310Skfall dest_tkt(); 10843367Skfall return(1); 10943310Skfall } 11043310Skfall 11143310Skfall if (!(hp = gethostbyname(localhost))) { 11243310Skfall syslog(LOG_ERR, "couldn't get local host address"); 11343367Skfall dest_tkt(); 11443367Skfall return(1); 11543310Skfall } 11643367Skfall 11743310Skfall bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr)); 11843310Skfall 11943310Skfall kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr, 12043310Skfall &authdata, ""); 12143367Skfall 12243310Skfall if (kerror == KSUCCESS) { 12343310Skfall notickets = 0; 12443310Skfall return(0); 12543310Skfall } 12643367Skfall 12743367Skfall /* undecipherable: probably didn't have a srvtab on the local host */ 12843310Skfall if (kerror = RD_AP_UNDEC) { 12943310Skfall syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]); 13043367Skfall dest_tkt(); 13143367Skfall return(1); 13243310Skfall } 13343367Skfall /* failed for some other reason */ 13443310Skfall (void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE, 13543310Skfall krb_err_txt[kerror]); 13643310Skfall syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE, 13743310Skfall krb_err_txt[kerror]); 13843367Skfall dest_tkt(); 13943367Skfall return(1); 14043310Skfall } 14143310Skfall #endif 142