1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)klogin.c 5.7 (Berkeley) 05/09/91"; 10 #endif /* not lint */ 11 12 #ifdef KERBEROS 13 #include <sys/param.h> 14 #include <sys/syslog.h> 15 #include <kerberosIV/des.h> 16 #include <kerberosIV/krb.h> 17 #include <pwd.h> 18 #include <netdb.h> 19 20 #define PRINCIPAL_NAME pw->pw_name 21 #define PRINCIPAL_INST (rootlogin ? "root" : "") 22 #define INITIAL_TICKET "krbtgt" 23 #define VERIFY_SERVICE "rcmd" 24 25 extern int notickets; 26 extern int rootlogin; 27 28 /* 29 * Attempt to log the user in using Kerberos authentication 30 * 31 * return 0 on success (will be logged in) 32 * 1 if Kerberos failed (try local password in login) 33 */ 34 35 klogin(pw, localhost, password) 36 struct passwd *pw; 37 char *localhost, *password; 38 { 39 int kerror; 40 AUTH_DAT authdata; 41 KTEXT_ST ticket; 42 struct hostent *hp; 43 unsigned long faddr; 44 char realm[REALM_SZ], savehost[MAXHOSTNAMELEN]; 45 char tkt_location[MAXPATHLEN]; 46 47 /* 48 * If we aren't Kerberos-authenticated, try the normal pw file 49 * for a password. If that's ok, log the user in without issueing 50 * any tickets. 51 */ 52 if (krb_get_lrealm(realm, 0) != KSUCCESS) { 53 syslog(LOG_ERR, "couldn't get local Kerberos realm"); 54 return(1); 55 } 56 57 /* 58 * get TGT for local realm 59 * tickets are stored in a file determined by calling tkt_string() 60 */ 61 62 (void)sprintf(tkt_location, "%s%d", TKT_ROOT, pw->pw_uid); 63 (void)krb_set_tkt_string(tkt_location); 64 65 kerror = krb_get_pw_in_tkt(PRINCIPAL_NAME, PRINCIPAL_INST, 66 realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password); 67 /* 68 * If we got a TGT, get a local "rcmd" ticket and check it so as to 69 * ensure that we are not talking to a bogus Kerberos server. 70 * 71 * There are 2 cases where we still allow a login: 72 * 1: the VERIFY_SERVICE doesn't exist in the KDC 73 * 2: local host has no srvtab, as (hopefully) indicated by a 74 * return value of RD_AP_UNDEC from krb_rd_req(). 75 */ 76 if (kerror != INTK_OK) { 77 if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN && 78 kerror != KFAILURE) { 79 syslog(LOG_ERR, "Kerberos intkt error: %s", 80 krb_err_txt[kerror]); 81 dest_tkt(); 82 } 83 return(1); 84 } 85 86 if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0) 87 syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE); 88 89 (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost)); 90 savehost[sizeof(savehost)-1] = NULL; 91 92 /* 93 * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host, 94 * still allow login with tickets, but log the error condition. 95 */ 96 97 kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33); 98 if (kerror == KDC_PR_UNKNOWN) { 99 syslog(LOG_NOTICE, "warning: TGT not verified (%s)", 100 krb_err_txt[kerror]); 101 notickets = 0; 102 return(0); 103 } 104 105 if (kerror != KSUCCESS) { 106 (void)printf("unable to use TGT: (%s)\n", krb_err_txt[kerror]); 107 syslog(LOG_NOTICE, "unable to use TGT: (%s)", 108 krb_err_txt[kerror]); 109 dest_tkt(); 110 return(1); 111 } 112 113 if (!(hp = gethostbyname(localhost))) { 114 syslog(LOG_ERR, "couldn't get local host address"); 115 dest_tkt(); 116 return(1); 117 } 118 119 bcopy((void *)hp->h_addr, (void *)&faddr, sizeof(faddr)); 120 121 kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr, 122 &authdata, ""); 123 124 if (kerror == KSUCCESS) { 125 notickets = 0; 126 return(0); 127 } 128 129 /* undecipherable: probably didn't have a srvtab on the local host */ 130 if (kerror = RD_AP_UNDEC) { 131 syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]); 132 dest_tkt(); 133 return(1); 134 } 135 /* failed for some other reason */ 136 (void)printf("unable to verify %s ticket: (%s)\n", VERIFY_SERVICE, 137 krb_err_txt[kerror]); 138 syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE, 139 krb_err_txt[kerror]); 140 dest_tkt(); 141 return(1); 142 } 143 #endif 144