home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp4 / ckuath.c < prev    next >
C/C++ Source or Header  |  2009-10-16  |  422KB  |  13,361 lines

  1. char *ckathv = "Authentication, 9.0.234, 16 Oct 2009";
  2. /*
  3.   C K U A T H . C  --  Authentication for C-Kermit
  4.  
  5.   Copyright (C) 1999, 2009,
  6.     Trustees of Columbia University in the City of New York.
  7.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  8.     copyright text in the ckcmai.c module for disclaimer and permissions.
  9.  
  10.     Author:  Jeffrey E Altman (jaltman@secure-endpoints.com)
  11.                Secure Endpoints Inc., New York City
  12. */
  13. /*
  14.  * Additional copyrights included with affected code.
  15.  */
  16.  
  17. #ifdef HEIMDAL
  18. /*
  19.   Turned off User to User support
  20.   Turned off KDESTROY support
  21.   Turned off KLIST support
  22.   Turned off krb5_prompter() support
  23.   Turned off ticket validation
  24.   Turned off ticket renewal
  25.   Turned off alternative cache support in k5_get_ccache()
  26.  
  27.   Remaining link problems:
  28.  
  29.   ckuath.o: In function `ck_krb5_initTGT':
  30.   ckuath.o(.text+0x50c2): undefined reference to `krb5_string_to_deltat'
  31.   ckuath.o(.text+0x516d): undefined reference to `krb5_string_to_deltat'
  32.   ckuath.o(.text+0x51ef): undefined reference to `krb5_string_to_deltat'
  33. */
  34. #endif /* HEIMDAL */
  35.  
  36. /*
  37.  * Implements Kerberos 4/5, SRP, SSL, NTLM authentication and START_TLS
  38.  */
  39.  
  40. #include "ckcsym.h"
  41. #include "ckcdeb.h"
  42.  
  43. #ifdef CK_SECURITY
  44.  
  45. #define CKUATH_C
  46. #include "ckcker.h"
  47. #include "ckuusr.h"
  48. #include "ckucmd.h"                             /* For struct keytab */
  49. #include "ckcnet.h"
  50. #include "ckctel.h"
  51.  
  52. char szUserNameRequested[UIDBUFLEN+1];    /* for incoming connections */
  53. char szUserNameAuthenticated[UIDBUFLEN+1];/* for incoming connections */
  54. char szHostName[UIDBUFLEN+1];
  55. char szUserName[UIDBUFLEN+1];
  56. static char szIP[16];
  57. static int  validUser = AUTH_REJECT;    /* User starts out invalid */
  58. int authentication_version = AUTHTYPE_NULL;
  59. int accept_complete = 0;
  60.  
  61. #ifdef CK_AUTHENTICATION
  62. #ifdef CK_SSL
  63. #ifdef KRB5
  64. #define TLS_VERIFY
  65. #endif /* KRB5 */
  66. #endif /* CK_SSL */
  67.  
  68. #ifdef CK_DES
  69. #ifdef CK_SSL
  70. #ifndef LIBDES
  71. #define LIBDES
  72. #endif /* LIBDES */
  73. #endif /* CK_SSL */
  74. #endif /* CK_DES */
  75.  
  76. #ifdef CRYPT_DLL
  77. #ifndef LIBDES
  78. #define LIBDES
  79. #endif /* LIBDES */
  80. #ifdef OS2
  81. #ifdef NT
  82. #include <windows.h>
  83. #else /* NT */
  84. #define INCL_DOSMODULEMGR
  85. #include <os2.h>
  86. #endif /* NT */
  87. #endif /* OS2 */
  88. #endif /* CRYPT_DLL */
  89.  
  90. #ifdef NT
  91. #define KRB5_AUTOCONF__
  92. #define NTLM
  93. #endif /* NT */
  94.  
  95. #ifdef CK_KERBEROS
  96. #define KINIT
  97. #ifndef HEIMDAL
  98. #define KLIST
  99. #define KDESTROY
  100. #endif /* HEIMDAL */
  101. #define CHECKADDRS
  102. #else /* CK_KERBEROS */
  103. #ifdef KRB4
  104. #undef KRB4
  105. #endif /* KRB4 */
  106. #ifdef KRB5
  107. #undef KRB5
  108. #endif /* KRB5 */
  109. #ifdef KRB524
  110. #undef KRB524
  111. #endif /* KRB524 */
  112. #endif /* CK_KERBEROS */
  113.  
  114. #include <stdlib.h>
  115. #include <string.h>
  116. #include <stdio.h>
  117. #include <time.h>
  118. #include <fcntl.h>
  119. #include <errno.h>
  120. #ifndef malloc
  121. #ifndef VMS
  122. #ifndef FREEBSD4
  123. #ifndef OpenBSD
  124. #ifdef MACOSX
  125. #include <sys/malloc.h>
  126. #else /* MACOSX */
  127. #include <malloc.h>
  128. #endif /* MACOSX */
  129. #endif /* OpenBSD */
  130. #endif /* FREEBSD4 */
  131. #endif /* VMS */
  132. #endif /* malloc */
  133. #ifdef OS2
  134. #include <io.h>
  135. #endif /* OS2 */
  136.  
  137. #ifdef KRB5
  138. #ifdef HEIMDAL
  139. #ifdef printf
  140. #define saveprintf printf
  141. #undef printf
  142. #endif /* printf */
  143. #include "krb5.h"
  144.  
  145. #include "com_err.h"
  146.  
  147. #ifdef saveprintf
  148. #define printf saveprintf
  149. #endif /* saveprintf */
  150. #else /* HEIMDAL */
  151. #include "krb5.h"
  152. #include "profile.h"
  153. #include "com_err.h"
  154. #ifdef KRB5_GET_INIT_CREDS_OPT_TKT_LIFE
  155. #define KRB5_HAVE_GET_INIT_CREDS
  156. #else
  157. #define krb5_free_unparsed_name(con,val) krb5_xfree((char *)(val))
  158. #endif
  159. #ifndef KRB5_HAVE_GET_INIT_CREDS
  160. #define krb5_free_data_contents(c,v) krb5_xfree((char *)(v)->data)
  161. #endif
  162. #endif /* HEIMDAL */
  163. #ifdef HAVE_PWD_H
  164. #include <pwd.h>
  165. #endif
  166. #endif /* KRB5 */
  167.  
  168. #ifdef KRB4
  169. #define  des_cblock Block
  170. #define  const_des_cblock const Block
  171. #define  des_key_schedule Schedule
  172. #ifdef KRB524
  173. #ifdef NT
  174. #define _WINDOWS
  175. #endif /* NT */
  176. #include "kerberosIV/krb.h"
  177. #ifndef OS2
  178. #ifdef KRB524_CONV
  179. #include "krb524.h"
  180. #endif /* KRB524_CONV */
  181. _PROTOTYP(const char * krb_get_err_text_entry, (int));
  182. #endif /* OS2 */
  183. #else /* KRB524 */
  184. #ifdef SOLARIS
  185. #ifndef sun
  186. /* for some reason the Makefile entries for the Solaris systems have -Usun */
  187. #define sun
  188. #endif /* sun */
  189. #endif /* SOLARIS */
  190. #include "krb.h"
  191. #define krb_get_err_text_entry krb_get_err_text
  192. #endif /* KRB524 */
  193. #else /* KRB4 */
  194. #ifdef CK_SSL
  195. #define  des_cblock Block
  196. #ifdef COMMENT
  197. #define  const_des_cblock const Block
  198. #endif /* COMMENT */
  199. #define  des_key_schedule Schedule
  200. #endif /* CK_SSL */
  201. #endif /* KRB4 */
  202.  
  203. #include "ckuath.h"
  204. #ifdef CK_KERBEROS
  205. #ifndef KRB5
  206. #define NOBLOCKDEF
  207. #else /* KRB5 */
  208. #ifdef KRB524
  209. #define NOBLOCKDEF
  210. #endif /* KRB524 */
  211. #endif /* KRB5 */
  212. #endif /* CK_KERBEROS */
  213. #include "ckuat2.h"
  214.  
  215. #ifdef CK_SSL
  216. #ifdef LIBDES
  217. #ifdef OPENSSL_097
  218. #define OPENSSL_ENABLE_OLD_DES_SUPPORT
  219. #include <openssl/des.h>
  220. #endif /* OPENSSL_097 */
  221. #ifndef HEADER_DES_H
  222. #define HEADER_DES_H
  223. #endif /* HEADER_DES_H */
  224. #endif /* LIBDES */
  225. #include "ck_ssl.h"
  226. extern int ssl_finished_messages;
  227. #endif /* SSL */
  228.  
  229. #define PWD_SZ 128
  230.  
  231. #ifndef LIBDES
  232. #ifdef UNIX
  233. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  234. #endif /* UNIX */
  235. #else /* LIBDES */
  236. #define des_fixup_key_parity des_set_odd_parity
  237. #endif /* LIBDES */
  238.  
  239. #ifdef OS2
  240. #ifdef CK_ENCRYPTION
  241. #define MAP_DES
  242. #endif /* CK_ENCRYPTION */
  243. #ifdef KRB4
  244. #define MAP_KRB4
  245. #endif /* KRB4 */
  246. #ifdef SRPDLL
  247. #define MAP_SRP
  248. #endif /* SRPDLL */
  249. #ifdef KRB5
  250. #define MAP_KRB5
  251. #endif /* KRB5 */
  252. #ifdef CRYPT_DLL
  253. #define MAP_CRYPT
  254. #endif /* CRYPT_DLL */
  255. #define MAP_NTLM
  256. #include "ckoath.h"
  257. #include "ckosyn.h"
  258. #endif /* OS2 */
  259.  
  260. /*
  261.  * Globals
  262.  */
  263. int auth_type_user[AUTHTYPLSTSZ] = {AUTHTYPE_AUTO, AUTHTYPE_NULL};
  264. int auth_how=0;
  265. int auth_crypt=0;
  266. int auth_fwd=0;
  267.  
  268. /* These are state completion variables */
  269. static int mutual_complete = 0;
  270.  
  271. #ifdef KRB4
  272. #ifdef OS2
  273. static LEASH_CREDENTIALS cred;
  274. #else /* OS2 */
  275. static CREDENTIALS cred;
  276. #endif /* OS2 */
  277. static KTEXT_ST k4_auth;
  278. static char     k4_name[ANAME_SZ];
  279. static AUTH_DAT k4_adat  = { 0 };
  280. static MSG_DAT  k4_msg_data;
  281. #ifdef CK_ENCRYPTION
  282. static Block    k4_session_key     = { 0 };
  283. static Schedule k4_sched;
  284. static Block    k4_challenge       = { 0 };
  285. #ifdef MIT_CURRENT
  286. static krb5_keyblock k4_krbkey;
  287. #endif /* MIT_CURRENT */
  288. #endif /* ENCRYPTION */
  289. #define KRB4_SERVICE_NAME    "rcmd"
  290.  
  291. _PROTOTYP(static int k4_auth_send,(VOID));
  292. _PROTOTYP(static int k4_auth_reply,(unsigned char *, int));
  293. _PROTOTYP(static int k4_auth_is,(unsigned char *, int));
  294. #endif /* KRB4 */
  295.  
  296. #ifdef KRB5
  297. static krb5_data          k5_auth;
  298. static krb5_auth_context  auth_context;
  299. static krb5_keyblock     *k5_session_key = NULL;
  300. static krb5_ticket       *k5_ticket = NULL;
  301. #ifndef KRB5_SERVICE_NAME
  302. #define KRB5_SERVICE_NAME    "host"
  303. #ifdef MACOSX
  304. #define MIT_CURRENT 1
  305. #define decode_krb5_ticket  krb5_decode_ticket
  306. #define krb5_read_message   ck_krb5_read_message
  307. #define krb5_write_message  ck_krb5_write_message
  308. #endif /* MACOSX */
  309. #endif
  310.  
  311. _PROTOTYP(static int k5_auth_send,(int,int,int));
  312. _PROTOTYP(static int k5_auth_reply,(int, unsigned char *, int));
  313. _PROTOTYP(static int k5_auth_is,(int,unsigned char *, int));
  314. _PROTOTYP(static int SendK5AuthSB,(int, void *, int));
  315. #ifdef TLS_VERIFY
  316. static int krb5_tls_verified = 0;
  317. #endif /* TLS_VERIFY */
  318. #endif /* KRB5 */
  319.  
  320. #ifdef GSSAPI_KRB5
  321. #include <gssapi/gssapi.h>
  322. #include <gssapi/gssapi_generic.h>
  323. #include <gssapi/gssapi_krb5.h>
  324.  
  325. static gss_ctx_id_t gcontext;
  326. #define GSS_BUFSIZ 4096
  327. static gss_buffer_desc gss_send_tok, gss_recv_tok, *gss_token_ptr;
  328. static char gss_stbuf[GSS_BUFSIZ];
  329. static gss_name_t gss_target_name;
  330. static struct gss_channel_bindings_struct gss_chan;
  331.  
  332. _PROTOTYP(static int gssk5_auth_send,(int,int,int));
  333. _PROTOTYP(static int gssk5_auth_reply,(int, unsigned char *, int));
  334. _PROTOTYP(static int gssk5_auth_is,(int,unsigned char *, int));
  335. _PROTOTYP(static int SendGSSK5AuthSB,(int, void *, int));
  336. #endif /* GSSAPI_KRB5 */
  337.  
  338. #ifdef CK_SRP
  339. #ifdef PRE_SRP_1_7_3
  340. _PROTOTYP(static int srp_reply,(int, unsigned char *, int));
  341. _PROTOTYP(static int srp_is,(int, unsigned char *, int));
  342. #else /* PRE_SRP_1_7_3 */
  343. _PROTOTYP(static int new_srp_reply,(int, unsigned char *, int));
  344. _PROTOTYP(static int new_srp_is,(int, unsigned char *, int));
  345. #endif /* PRE_SRP_1_7_3 */
  346. #endif /* SRP */
  347.  
  348. #ifdef CK_ENCRYPTION
  349. int encrypt_flag = 1;
  350. #endif
  351. #ifdef FORWARD
  352. int forward_flag = 0;              /* forward tickets? */
  353. int forwardable_flag = 1;          /* get forwardable tickets to forward? */
  354. int forwarded_tickets = 0;         /* were tickets forwarded? */
  355. #endif
  356.  
  357. static unsigned char str_data[4096] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
  358.                                         AUTHTYPE_KERBEROS_V5, };
  359. #define AUTHTMPBL 2048
  360. static char strTmp[AUTHTMPBL+1];
  361. static char szLocalHostName[UIDBUFLEN+1];
  362. static kstream g_kstream=NULL;
  363.  
  364. #ifdef KRB5
  365. krb5_context k5_context=NULL;
  366. static krb5_creds * ret_cred=NULL;
  367. static krb5_context telnet_context=NULL;
  368. static char * telnet_krb5_realm = NULL;
  369. static krb5_principal fwd_server = NULL;
  370. #endif /* KRB5 */
  371.  
  372. #ifdef CK_SRP
  373. #ifdef PRE_SRP_1_4_4
  374. #ifndef PRE_SRP_1_4_5
  375. #define PRE_SRP_1_4_5
  376. #endif /* PRE_SRP_1_4_5 */
  377. #endif /* PRE_SRP_1_4_5 */
  378. #ifdef PRE_SRP_1_4_5
  379. #ifndef PRE_SRP_1_7_3
  380. #define PRE_SRP_1_7_3
  381. #endif /* PRE_SRP_1_7_3 */
  382. #endif /* PRE_SRP_1_4_5 */
  383. #include <t_pwd.h>
  384. #include <t_client.h>
  385. #include <t_server.h>
  386. static struct t_server * ts = NULL;
  387. static struct t_client * tc = NULL;
  388. #ifdef PRE_SRP_1_4_4
  389. static struct t_pw * tpw = NULL;
  390. static struct t_conf * tconf = NULL;
  391. #endif /* PRE_SRP_1_4_4 */
  392. #ifndef PRE_SRP_1_7_3
  393. #ifndef STDC_HEADERS
  394. #define STDC_HEADERS 1
  395. #endif /* STDC_HEADERS */
  396. #include <srp.h>
  397. static SRP * s_srp = NULL;
  398. static cstr * s_key = NULL;
  399. static SRP * c_srp = NULL;
  400. static cstr * c_key = NULL;
  401. #endif /* PRE_SRP_1_7_3 */
  402. static int srp_waitresp = 0;    /* Flag to indicate readiness for response */
  403. static char srp_passwd[PWD_SZ];
  404. #endif /* CK_SRP */
  405.  
  406. #ifdef CK_KERBEROS
  407. #ifdef RLOGCODE
  408. #define OPTS_FORWARD_CREDS           0x00000020
  409. #define OPTS_FORWARDABLE_CREDS       0x00000010
  410. #define KCMD_KEYUSAGE                1026
  411.  
  412. #define RLOG_BUFSIZ 5120
  413. static int rlog_encrypt = 0;
  414. char des_inbuf[2*RLOG_BUFSIZ];       /* needs to be > largest read size */
  415. char des_outpkt[2*RLOG_BUFSIZ+4];    /* needs to be > largest write size */
  416. #ifdef KRB5
  417. krb5_data desinbuf,desoutbuf;
  418. krb5_encrypt_block eblock;             /* eblock for encrypt/decrypt */
  419. static krb5_data encivec_i[2], encivec_o[2];
  420.  
  421. enum krb5_kcmd_proto {
  422.   /* Old protocol: DES encryption only.  No subkeys.  No protection
  423.      for cleartext length.  No ivec supplied.  OOB hacks used for
  424.      rlogin.  Checksum may be omitted at connection startup.  */
  425.   KCMD_OLD_PROTOCOL = 1,
  426.   /* New protocol: Any encryption scheme.  Client-generated subkey
  427.      required.  Prepend cleartext-length to cleartext data (but don't
  428.      include it in count).  Starting ivec defined, chained.  In-band
  429.      signalling.  Checksum required.  */
  430.   KCMD_NEW_PROTOCOL,
  431.   /* Hack: Get credentials, and use the old protocol iff the session
  432.      key type is single-DES.  */
  433.   KCMD_PROTOCOL_COMPAT_HACK,
  434.   KCMD_UNKNOWN_PROTOCOL
  435. };
  436. enum krb5_kcmd_proto krb5_rlog_ver = KCMD_PROTOCOL_COMPAT_HACK;
  437. #endif /* KRB5 */
  438. #endif /* RLOGCODE */
  439. static char storage[65536];            /* storage for the decryption */
  440. static int nstored = 0;
  441. static char *store_ptr = storage;
  442.  
  443. extern char * krb5_d_principal;         /* Default principal */
  444. extern char * krb5_d_instance;          /* Default instance */
  445. extern char * krb5_d_realm;             /* Default realm */
  446. extern char * krb5_d_cc;                /* Default credentials cache */
  447. extern char * krb5_d_srv;               /* Default service name */
  448. extern int    krb5_d_lifetime;          /* Default lifetime */
  449. extern int    krb5_d_forwardable;
  450. extern int    krb5_d_proxiable;
  451. extern int    krb5_d_renewable;
  452. extern int    krb5_autoget;
  453. extern int    krb5_checkaddrs;
  454. extern int    krb5_d_getk4;
  455. extern int    krb5_d_no_addresses;
  456. extern char * k5_keytab;
  457.  
  458. extern int    krb5_errno;
  459. extern char * krb5_errmsg;
  460.  
  461. extern char * krb4_d_principal;         /* Default principal */
  462. extern char * krb4_d_realm;             /* Default realm */
  463. extern char * krb4_d_srv;               /* Default service name */
  464. extern int    krb4_d_lifetime;          /* Default lifetime */
  465. extern int    krb4_d_preauth;
  466. extern char * krb4_d_instance;
  467. extern int    krb4_autoget;
  468. extern int    krb4_checkaddrs;
  469. extern char * k4_keytab;
  470.  
  471. extern int    krb4_errno;
  472. extern char * krb4_errmsg;
  473. #endif /* CK_KERBEROS */
  474.  
  475. extern char tn_msg[], hexbuf[];         /* from ckcnet.c */
  476. extern CHAR pwbuf[];
  477. extern int  pwflg, pwcrypt;
  478. extern int deblog, debses, tn_deb;
  479. extern int sstelnet, inserver;
  480. #ifdef CK_LOGIN
  481. extern int ckxanon;
  482. #endif /* CK_LOGIN */
  483. extern int tn_auth_how;
  484. extern int tn_auth_enc;
  485. #ifdef CK_ENCRYPTION
  486. extern int cx_type;
  487. #endif /* CK_ENCRYPTION */
  488. extern int quiet, ttyfd, ttnproto;
  489.  
  490. int
  491. ck_gssapi_is_installed()
  492. {
  493. #ifdef KRB5
  494. #ifdef OS2
  495.     return(hGSSAPI != NULL);
  496. #else /* OS2 */
  497.     return(1);
  498. #endif /* OS2 */
  499. #else /* KRB5 */
  500.     return(0);
  501. #endif /* KRB5 */
  502. }
  503.  
  504. int
  505. ck_krb5_is_installed()
  506. {
  507. #ifdef KRB5
  508. #ifdef OS2
  509.     return(hKRB5_32 != NULL);
  510. #else /* OS2 */
  511.     return(1);
  512. #endif /* OS2 */
  513. #else /* KRB5 */
  514.     return(0);
  515. #endif /* KRB5 */
  516. }
  517.  
  518.  
  519. int
  520. ck_krb5_is_installed_as_server()
  521. {
  522. #ifdef KRB5
  523. #ifdef HEIMDAL
  524.     krb5_error_code ret;
  525.     krb5_keytab kt;
  526.     krb5_kt_cursor cursor;
  527.  
  528.     ret = krb5_kt_default(k5_context, &kt);
  529.     if ( ret ) {
  530.         krb5_kt_close(k5_context, kt);
  531.         return(0);
  532.     } else {
  533.         krb5_kt_end_seq_get(k5_context, kt, &cursor);
  534.         krb5_kt_close(k5_context, kt);
  535.         return(1);
  536.     }
  537. #else /* HEIMDAL */
  538. #ifndef COMMENT
  539.     char ktname[CKMAXPATH]="";
  540.  
  541.     if ( k5_keytab ) {
  542.         ckstrncpy(ktname,k5_keytab,CKMAXPATH);
  543.     } else {
  544.         krb5_error_code code;
  545.  
  546.         if ( k5_context == NULL)
  547.             if (krb5_init_context(&k5_context))
  548.                 return(0);
  549.  
  550.         code = krb5_kt_default_name(k5_context,ktname,CKMAXPATH);
  551.         debug(F101,"krb5_kt_default_name","",code);
  552.         if ( code ) {
  553.             /* We can't check the existence of the file since we can't   */
  554.             /* determine the file name.  So we return TRUE and let       */
  555.             /* Krb5 be offered to the user even though it may fail later */
  556.             return(1);
  557.         }
  558.     }
  559.  
  560.     if ( !strncmp("FILE:",ktname,5) ) {
  561.         if ( zchki(&ktname[5]) > 0 )
  562.             return(1);
  563.         else
  564.             return(0);
  565.     } else {
  566.         if (ktname[0])
  567.             return(1);
  568.         else
  569.             return(0);
  570.     }
  571. #else /* COMMENT */
  572.     krb5_error_code             krb5rc = KRB5KRB_ERR_GENERIC;
  573.     krb5_context                krb5context = NULL;
  574.     krb5_ccache                 krb5ccdef = NULL;
  575.     krb5_creds                  krb5creds, *krb5credsp = NULL;
  576.     int                         rc = 0;
  577.  
  578.     if ( !ck_krb5_is_installed() )
  579.         return(0);
  580.  
  581.     memset((char *)&krb5creds, 0, sizeof(krb5creds));
  582.  
  583.     if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  584.         goto err;
  585.  
  586.     if ((krb5rc = krb5_sname_to_principal(krb5context,
  587.                                           szHostName,
  588.                                           krb5_d_srv ?
  589.                                           krb5_d_srv :
  590.                                           KRB5_SERVICE_NAME,
  591.                                           KRB5_NT_SRV_HST,
  592.                                           &krb5creds.server)) != 0)
  593.       goto err;
  594.  
  595.     if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  596.         goto err;
  597.  
  598.     if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  599.                                          &krb5creds.client)) != 0)
  600.         goto err;
  601.  
  602.     if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  603.                                         &krb5creds, &krb5credsp)) != 0)
  604.         goto err;
  605.     rc = 1;
  606.  
  607.   err:
  608.  
  609.     if (krb5creds.client)
  610.       krb5_free_principal(krb5context, krb5creds.client);
  611.     if (krb5creds.server)
  612.       krb5_free_principal(krb5context, krb5creds.server);
  613.     if (krb5context)
  614.       krb5_free_context(krb5context);
  615.     return(rc);
  616.  
  617. #endif /* COMMENT */
  618. #endif /* HEIMDAL */
  619. #else /* KRB5 */
  620.     return(0);
  621. #endif /* KRB5 */
  622. }
  623.  
  624. int
  625. ck_krb4_is_installed()
  626. {
  627. #ifdef KRB4
  628. #ifdef OS2
  629.     return(hKRB4_32 != NULL);
  630. #else /* OS2 */
  631.     return(1);
  632. #endif /* OS2 */
  633. #else /* KRB4 */
  634.     return(0);
  635. #endif /* KRB4 */
  636. }
  637.  
  638. int
  639. ck_krb4_is_installed_as_server()
  640. {
  641.     if ( !ck_krb4_is_installed() )
  642.         return(0);
  643.  
  644. #ifdef KRB4
  645.     if ( !k4_keytab ) {
  646. #ifdef NT
  647.         char name[CKMAXPATH]="";
  648.         DWORD len = CKMAXPATH;
  649.  
  650.         len = GetWindowsDirectory(name,len);
  651.         if ( len > 0 )
  652.             ckstrncat(name,"/srvtab",CKMAXPATH);
  653.         if ( name[0] )
  654.             makestr(&k4_keytab,name);
  655. #else /* NT */
  656.         makestr(&k4_keytab,"/etc/srvtab");
  657. #endif /* NT */
  658.     }
  659.  
  660.     if ( !k4_keytab )
  661.         return(0);
  662.  
  663.     if ( zchki(k4_keytab) > 0 )
  664.         return(1);
  665. #ifdef KRB524
  666.     else if (ck_krb5_is_installed_as_server())
  667.         return(1);
  668. #endif /* KRB524 */
  669.     else
  670.         return(0);
  671. #endif /* KRB4 */
  672. }
  673.  
  674. int
  675. ck_srp_is_installed_as_server()
  676. {
  677. #ifdef CK_SRP
  678. #ifdef SRPDLL
  679.     if ( hSRP == NULL )
  680.         return(0);
  681. #endif /* SRPDLL */
  682. #ifdef COMMENT
  683.     /* This is the new API as of 1.7.4.  However, all it does
  684.        is allocate a data structure.  It can never fail.
  685.      */
  686.     {
  687.         SRP * s_srp = SRP_new(SRP_RFC2945_server_method());
  688.         if ( s_srp ) {
  689.             SRP_free(s_srp);
  690.             s_srp = NULL;
  691.             return(1);
  692.         }
  693.         return(0);
  694.     }
  695. #else /* COMMENT */
  696.     {
  697.         struct t_pw * tpw = NULL;
  698.         struct t_conf * tconf = NULL;
  699.         if((tconf = t_openconf(NULL)) == NULL)
  700.             return(0);
  701.         if((tpw = t_openpw(NULL)) == NULL) {
  702.             t_closeconf(tconf);
  703.             return(0);
  704.         }
  705.         t_closeconf(tconf);
  706.         t_closepw(tpw);
  707.         return(1);
  708.     }
  709. #endif /* COMMENT */
  710. #else /* SRP */
  711.     return(0);
  712. #endif /* SRP */
  713. }
  714.  
  715. int
  716. ck_srp_is_installed()
  717. {
  718. #ifdef CK_SRP
  719. #ifdef SRPDLL
  720.     if ( hSRP == NULL )
  721.         return(0);
  722. #endif /* SRPDLL */
  723.     return(1);
  724. #else /* CK_SRP */
  725.     return(0);
  726. #endif /* CK_SRP */
  727. }
  728.  
  729. int
  730. ck_krypto_is_installed()
  731. {
  732. #ifdef CK_SRP
  733. #ifdef OS2
  734.     if ( hLIBKRYPTO == NULL )
  735.         return(0);
  736. #endif /* OS2 */
  737.     return(1);
  738. #else /* CK_SRP */
  739.     return(0);
  740. #endif /* CK_SRP */
  741. }
  742.  
  743. int
  744. ck_crypt_is_installed()
  745. {
  746. #ifdef CK_ENCRYPTION
  747. #ifdef CRYPT_DLL
  748.     return(hCRYPT != NULL);
  749. #else /* CRYPT_DLL */
  750.     return(1);
  751. #endif /* CRYPT_DLL */
  752. #else /* ENCRYPTION */
  753.     return(0);
  754. #endif /* ENCRYPTION */
  755. }
  756.  
  757. int
  758. ck_ntlm_is_installed()
  759. {
  760. #ifdef NT
  761.     return(hSSPI != NULL);
  762. #else /* NT */
  763.     return(0);
  764. #endif /* NT */
  765. }
  766.  
  767. int
  768. ck_tn_auth_valid()
  769. {
  770.     return(validUser);
  771. }
  772.  
  773. /* C K _ K R B _ A U T H _ I N _ P R O G R E S S
  774.  *
  775.  * Is an authentication negotiation still in progress?
  776.  *
  777.  */
  778.  
  779. int
  780. #ifdef CK_ANSIC
  781. ck_tn_auth_in_progress(void)
  782. #else
  783. ck_tn_auth_in_progress()
  784. #endif
  785. {
  786.     switch (authentication_version) {
  787.     case AUTHTYPE_AUTO:
  788.         return(1);
  789.     case AUTHTYPE_NULL:
  790.         return(0);
  791. #ifdef KRB4
  792.     case AUTHTYPE_KERBEROS_V4:
  793.         if (!accept_complete) {
  794.             debug(F100,"ck_auth_in_progress() Kerberos 4 !accept_complete",
  795.                    "",0);
  796.             return(1);
  797.         }
  798.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  799.             debug(F100,"ck_auth_in_progress() Kerberos 4 !mutual_complete",
  800.                    "",0);
  801.             return(1);
  802.         }
  803.         else
  804.             return(0);
  805. #endif /* KRB4 */
  806. #ifdef KRB5
  807.     case AUTHTYPE_KERBEROS_V5:
  808.         if (!accept_complete) {
  809.             debug(F100,"ck_auth_in_progress() Kerberos 5 !accept_complete",
  810.                    "",0);
  811.             return(1);
  812.         }
  813.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  814.             debug(F100,"ck_auth_in_progress() Kerberos 5 !mutual_complete",
  815.                    "",0);
  816.             return(1);
  817.         }
  818.         else
  819.             return(0);
  820. #ifdef GSSAPI_K5
  821.     case AUTHTYPE_GSSAPI_KRB5:
  822.         if (!accept_complete) {
  823.             debug(F100,
  824.           "ck_auth_in_progress() GSSAPI Kerberos 5 !accept_complete",
  825.           "",
  826.           0
  827.           );
  828.             return(1);
  829.         }
  830.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  831.             debug(F100,
  832.           "ck_auth_in_progress() GSSAPI Kerberos 5 !mutual_complete",
  833.           "",
  834.           0
  835.           );
  836.             return(1);
  837.         } else
  838.       return(0);
  839.         break;
  840. #endif /* GSSAPI_K5 */
  841. #endif /* KRB5 */
  842. #ifdef CK_SRP
  843.     case AUTHTYPE_SRP:
  844.         if (!accept_complete || srp_waitresp)
  845.             return(1);
  846.         else
  847.             return(0);
  848. #endif /* CK_SRP */
  849. #ifdef NTLM
  850.     case AUTHTYPE_NTLM:
  851.         if (!accept_complete) {
  852.             debug(F100,"ck_auth_in_progress() NTLM !accept_complete",
  853.                    "",0);
  854.             return(1);
  855.         }
  856.         else
  857.             return(0);
  858. #endif /* NTLM */
  859.     case AUTHTYPE_SSL:
  860.         if (!accept_complete) {
  861.             debug(F100,"ck_auth_in_progress() SSL !accept_complete",
  862.                    "",0);
  863.             return(1);
  864.         }
  865.         else
  866.             return(0);
  867.     default:
  868.         return(0);
  869.     }
  870.     return(0);
  871. }
  872.  
  873.  
  874. /*  C K _ K R B _ T N _ A U T H _ R E Q U E S T
  875.  *
  876.  *  Builds a Telnet Authentication Send Negotiation providing the
  877.  *  list of supported authentication methods.  To be used only
  878.  *  when accepting incoming connections as only the server (DO) side of the
  879.  *  Telnet negotiation is allowed to send an AUTH SEND.
  880.  *
  881.  *  Returns: 0 on success and -1 on failure
  882.  */
  883.  
  884. static unsigned char str_request[64] = { IAC, SB,
  885.                                              TELOPT_AUTHENTICATION,
  886.                                              TELQUAL_SEND };
  887. #ifdef GSSAPI_K5
  888. static int
  889. ck_tn_auth_request_gsskrb5(int i)
  890. {
  891.     if (ck_gssapi_is_installed() && ck_krb5_is_installed_as_server()) {
  892.         if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  893.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  894.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  895.                tn_auth_enc == TN_AUTH_ENC_EXCH) ) {
  896.             str_request[i++] = AUTHTYPE_KERBEROS_V5;
  897.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  898.             str_request[i] |= AUTH_ENCRYPT_AFTER_EXCHANGE;
  899.  
  900.             if ( deblog || tn_deb || debses )
  901.                 ckstrncat(tn_msg,
  902.                 "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_AFTER_EXCHANGE ",
  903.                           TN_MSG_LEN);
  904.             i++;
  905.         }
  906.     }
  907. }
  908. #endif /* GSSAPI_K5 */
  909.  
  910. #ifdef KRB5
  911. static int
  912. ck_tn_auth_request_krb5(int i)
  913. {
  914.     if (ck_krb5_is_installed_as_server()) {
  915. #ifdef CK_SSL
  916.         if ( ck_ssleay_is_installed() &&
  917.              (tls_active_flag || ssl_active_flag) &&
  918.              ssl_finished_messages )
  919.         {
  920. #ifdef USE_INI_CRED_FWD
  921.             if ( forward_flag &&
  922.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  923.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  924.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  925.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  926.                  )
  927.             {
  928.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  929.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  930.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  931.                 str_request[i] |= INI_CRED_FWD_ON;
  932.  
  933.                 if ( deblog || tn_deb || debses )
  934.                     ckstrncat(tn_msg,
  935.  "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS|INI_CRED_FWD_ON ",
  936.                                TN_MSG_LEN);
  937.                 i++;
  938.             }
  939. #endif /* USE_INI_CRED_FWD */
  940.             if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  941.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  942.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  943.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  944.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  945.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  946.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  947.  
  948.                 if ( deblog || tn_deb || debses )
  949.                     ckstrncat(tn_msg,
  950.                       "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS ",
  951.                               TN_MSG_LEN);
  952.                 i++;
  953.             }
  954.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  955.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  956.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  957.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  958.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  959.  
  960.                 if ( deblog || tn_deb || debses )
  961.                     ckstrncat(tn_msg,
  962.                     "KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  963.                                TN_MSG_LEN);
  964.                 i++;
  965.             }
  966.         }
  967. #ifdef CK_ENCRYPTION
  968.         else
  969.         {
  970. #endif /* CK_ENCRYPTION */
  971. #endif /* CK_SSL */
  972. #ifdef CK_ENCRYPTION
  973. #ifdef USE_INI_CRED_FWD
  974.             if ( forward_flag &&
  975.                  TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  976.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  977.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  978.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  979.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  980.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  981.                  )
  982.             {
  983.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  984.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  985.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  986.                 str_request[i] |= INI_CRED_FWD_ON;
  987.  
  988.                 if ( deblog || tn_deb || debses )
  989.                     ckstrncat(tn_msg,
  990.   "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT|INI_CRED_FWD_ON ",
  991.                                TN_MSG_LEN);
  992.                 i++;
  993.             }
  994. #endif /* USE_INI_CRED_FWD */
  995.  
  996.             if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  997.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  998.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  999.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1000.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1001.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  1002.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1003.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1004.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1005.  
  1006.                 if ( deblog || tn_deb || debses )
  1007.                     ckstrncat(tn_msg,
  1008.               "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT ",
  1009.                                TN_MSG_LEN);
  1010.                 i++;
  1011.             }
  1012. #ifdef CK_SSL
  1013.         }
  1014. #endif /* CK_SSL */
  1015. #endif /* CK_ENCRYPTION */
  1016.  
  1017.         if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1018.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1019.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1020.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1021. #ifdef CK_SSL
  1022.              && !(ck_ssleay_is_installed() &&
  1023.                    (tls_active_flag || ssl_active_flag) &&
  1024.                    tls_is_anon(0))
  1025. #endif /* CK_SSL */
  1026.              )
  1027.         {
  1028. #ifdef CK_ENCRYPTION
  1029.             /* Can't perform mutual authentication without encryption */
  1030.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1031.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1032.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1033.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1034.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1035.  
  1036.                 if ( deblog || tn_deb || debses )
  1037.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL ",
  1038.                                TN_MSG_LEN);
  1039.                 i++;
  1040.             }
  1041. #endif /* CK_ENCRYPTION */
  1042.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1043.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1044.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1045.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1046.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1047.  
  1048.                 if ( deblog || tn_deb || debses )
  1049.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY ",
  1050.                                TN_MSG_LEN);
  1051.                 i++;
  1052.             }
  1053.         }
  1054.     }
  1055.     return(i);
  1056. }
  1057. #endif /* KRB5 */
  1058. #ifdef KRB4
  1059. static int
  1060. ck_tn_auth_request_krb4(int i)
  1061. {
  1062.     if (ck_krb4_is_installed_as_server()) {
  1063. #ifdef CK_ENCRYPTION
  1064.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1065.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1066.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1067.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1068.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1069.                tn_auth_enc == TN_AUTH_ENC_TELOPT) )
  1070.         {
  1071.             str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1072.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1073.             str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1074.  
  1075.             if ( deblog || tn_deb || debses )
  1076.               ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL|ENCRYPT ",
  1077.                         TN_MSG_LEN);
  1078.             i++;
  1079.         }
  1080. #endif /* CK_ENCRYPTION */
  1081.  
  1082.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1083.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1084.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1085.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1086.         {
  1087. #ifdef CK_ENCRYPTION
  1088.             /* Can't perform mutual authentication without encryption */
  1089.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1090.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1091.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1092.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1093.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1094.  
  1095.                 if ( deblog || tn_deb || debses )
  1096.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL ",
  1097.                                TN_MSG_LEN);
  1098.                 i++;
  1099.             }
  1100. #endif /* CK_ENCRYPTION */
  1101.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1102.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1103.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1104.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1105.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1106.  
  1107.                 if ( deblog || tn_deb || debses )
  1108.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|ONE_WAY ",
  1109.                                TN_MSG_LEN);
  1110.                 i++;
  1111.             }
  1112.         }
  1113.     }
  1114.  
  1115.     return(i);
  1116. }
  1117. #endif /* KRB4 */
  1118.  
  1119. #ifdef CK_SRP
  1120. static int
  1121. ck_tn_auth_request_srp(int i)
  1122. {
  1123.     if (ck_srp_is_installed_as_server()) {
  1124. #ifndef PRE_SRP_1_4_5
  1125.         /* Dont' do this yet.  SRP when it uses the ENCRYPT_USING_TELOPT   */
  1126.         /* flag it must perform a checksum of the auth-type-pair but there */
  1127.         /* is no mechansim to do that yet.                                 */
  1128. #ifdef CK_SSL
  1129.         if ( ck_ssleay_is_installed() &&
  1130.              (tls_active_flag || ssl_active_flag) &&
  1131.              ssl_finished_messages &&
  1132.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1133.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1134.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1135.                    tn_auth_enc == TN_AUTH_ENC_TELOPT))
  1136.         {
  1137.             str_request[i++] = AUTHTYPE_SRP;
  1138.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1139.             str_request[i] |= AUTH_ENCRYPT_START_TLS;
  1140.  
  1141.             if ( deblog || tn_deb || debses )
  1142.                 ckstrncat(tn_msg,
  1143.                            "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  1144.                            TN_MSG_LEN);
  1145.             i++;
  1146.         }
  1147. #ifdef CK_ENCRYPTION
  1148.         else {
  1149. #endif /* CK_ENCRYPTION */
  1150. #endif /* CK_SSL */
  1151. #ifdef CK_ENCRYPTION
  1152.             if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1153.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1154.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1155.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1156.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1157.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  1158.                  ) {
  1159.                 str_request[i++] = AUTHTYPE_SRP;
  1160.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1161.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1162.  
  1163.                 if ( deblog || tn_deb || debses )
  1164.                     ckstrncat(tn_msg,
  1165.                     "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_USING_TELOPT ",
  1166.                                TN_MSG_LEN);
  1167.                 i++;
  1168.             }
  1169. #ifdef CK_SSL
  1170.         }
  1171. #endif /* CK_SSL */
  1172. #endif /* CK_ENCRYPTION */
  1173. #endif /* PRE_SRP_1_4_5 */
  1174.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1175.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1176.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1177.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1178.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1179.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1180. #ifdef CK_SSL
  1181.              && !(ck_ssleay_is_installed() &&
  1182.                    (tls_active_flag || ssl_active_flag) &&
  1183.                    tls_is_anon(0))
  1184. #endif /* CK_SSL */
  1185.              )
  1186.         {
  1187.             str_request[i++] = AUTHTYPE_SRP;
  1188.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1189.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1190.  
  1191.             if ( deblog || tn_deb || debses )
  1192.                 ckstrncat(tn_msg,"SRP CLIENT_TO_SERVER|ONE_WAY ",
  1193.                            TN_MSG_LEN);
  1194.             i++;
  1195.         }
  1196.     }
  1197.  
  1198.     return(i);
  1199. }
  1200. #endif /* CK_SRP */
  1201.  
  1202. #ifdef CK_SSL
  1203. static int
  1204. ck_tn_auth_request_ssl(int i)
  1205. {
  1206.     if (ck_ssleay_is_installed()
  1207.          && !tls_active_flag && !ssl_active_flag && ssl_initialized
  1208.          ) {
  1209.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1210.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1211.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1212.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1213.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1214.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1215.         {
  1216.             str_request[i++] = AUTHTYPE_SSL;
  1217.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1218.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1219.             if ( deblog || tn_deb || debses )
  1220.                 ckstrncat(tn_msg,"SSL CLIENT_TO_SERVER|ONE_WAY ",
  1221.                            TN_MSG_LEN);
  1222.             i++;
  1223.         }
  1224.     }
  1225.  
  1226.     return(i);
  1227. }
  1228. #endif /* CK_SSL */
  1229. #ifdef NTLM
  1230. static int
  1231. ck_tn_auth_request_ntlm(int i)
  1232. {
  1233.     /* Microsoft's Telnet client won't perform authentication if */
  1234.     /* NTLM is not first.                                        */
  1235.     if ( ck_ntlm_is_valid(1) ) {
  1236.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1237.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1238.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1239.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1240.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1241.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1242.         {
  1243.             str_request[i++] = AUTHTYPE_NTLM;
  1244.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1245.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1246.             if ( deblog || tn_deb || debses )
  1247.                 ckstrncat(tn_msg,"NTLM CLIENT_TO_SERVER|ONE_WAY ",
  1248.                            TN_MSG_LEN);
  1249.             i++;
  1250.         }
  1251.     }
  1252.  
  1253.     return(i);
  1254. }
  1255. #endif /* NTLM */
  1256. int
  1257. #ifdef CK_ANSIC
  1258. ck_tn_auth_request(void)
  1259. #else
  1260. ck_tn_auth_request()
  1261. #endif
  1262. {
  1263.     int i = 4, rc = -1;
  1264.  
  1265. #ifdef CK_SSL
  1266.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1267.         return(0);
  1268.     }
  1269. #endif /* CK_SSL */
  1270.  
  1271.     if ( deblog || tn_deb || debses )
  1272.         strcpy(tn_msg,"TELNET SENT SB AUTHENTICATION SEND ");
  1273.  
  1274.     /* Create a list of acceptable Authentication types to send to */
  1275.     /* the client and let it choose find one that we support       */
  1276.  
  1277.     /* For those authentication methods that support Encryption or */
  1278.     /* Credentials Forwarding we must send all of the appropriate  */
  1279.     /* combinations based upon the state of                        */
  1280.     /* TELOPT_x_MODE(TELOPT_ENCRYPTION) and forward_flag.          */
  1281.  
  1282.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  1283. #ifdef GSSAPI_K5
  1284.         i = ck_tn_auth_request_gsskrb5(i);
  1285. #endif /* GSSAPI_K5 */
  1286. #ifdef KRB5
  1287.         i = ck_tn_auth_request_krb5(i);
  1288. #endif /* KRB5 */
  1289. #ifdef KRB4
  1290.         i = ck_tn_auth_request_krb4(i);
  1291. #endif /* KRB4 */
  1292. #ifdef CK_SRP
  1293.         i = ck_tn_auth_request_srp(i);
  1294. #endif /* SRP */
  1295. #ifdef CK_SSL
  1296.         i = ck_tn_auth_request_ssl(i);
  1297. #endif /* CK_SSL */
  1298. #ifdef NTLM
  1299.         i = ck_tn_auth_request_ntlm(i);
  1300. #endif /* NTLM */
  1301.     } else {
  1302.         int j;
  1303.         for ( j=0;
  1304.               j<AUTHTYPLSTSZ && auth_type_user[j] != AUTHTYPE_NULL;
  1305.               j++) {
  1306. #ifdef NTLM
  1307.             if (auth_type_user[j] == AUTHTYPE_NTLM)
  1308.                 i = ck_tn_auth_request_ntlm(i);
  1309. #endif /* NTLM */
  1310. #ifdef CK_SSL
  1311.             if ( auth_type_user[j] == AUTHTYPE_SSL )
  1312.                 i = ck_tn_auth_request_ssl(i);
  1313. #endif /* CK_SSL */
  1314. #ifdef CK_SRP
  1315.             if ( auth_type_user[j] == AUTHTYPE_SRP )
  1316.                 i = ck_tn_auth_request_srp(i);
  1317. #endif /* SRP */
  1318. #ifdef GSSAPI_K5
  1319.             if ( auth_type_user[j] == AUTHTYPE_GSSAPI_KRB5 )
  1320.                 i = ck_tn_auth_request_gsskrb5(i);
  1321. #endif /* GSSAPI_K5 */
  1322. #ifdef KRB5
  1323.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V5 )
  1324.                 i = ck_tn_auth_request_krb5(i);
  1325. #endif /* KRB5 */
  1326. #ifdef KRB4
  1327.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V4 )
  1328.                 i = ck_tn_auth_request_krb4(i);
  1329. #endif /* KRB4 */
  1330.         }
  1331.     }
  1332.  
  1333.     str_request[i++] = IAC;
  1334.     str_request[i++] = SE;
  1335.     if ( deblog || tn_deb || debses ) {
  1336.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1337.         debug(F100,tn_msg,"",0);
  1338.         if (tn_deb || debses) tn_debug(tn_msg);
  1339.     }
  1340.  
  1341.     /* Send data */
  1342. #ifdef OS2
  1343.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1344. #endif
  1345.     rc = ttol((CHAR *)str_request, i);
  1346. #ifdef OS2
  1347.     ReleaseTelnetMutex();
  1348. #endif
  1349.     if ( rc == i )
  1350.         return(0);
  1351.     else
  1352.         return(-1);
  1353. }
  1354.  
  1355. #ifdef CK_ENCRYPTION
  1356. VOID
  1357. ck_tn_enc_start()
  1358. {
  1359.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1360.         return;
  1361.     if (!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop &&
  1362.          (!encrypt_is_decrypting() || !encrypt_is_encrypting())) {
  1363.         debug(F110,"ck_tn_enc_start","nothing to do",0);
  1364.         return;
  1365.     }
  1366.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1367.     if (TELOPT_ME(TELOPT_ENCRYPTION) && !encrypt_is_encrypting()) {
  1368.         debug(F110,"ck_tn_enc_start","encrypt_request_start",0);
  1369.         encrypt_request_start();
  1370.     }
  1371.     if (TELOPT_U(TELOPT_ENCRYPTION) && !encrypt_is_decrypting()) {
  1372.         debug(F110,"ck_tn_enc_start","encrypt_send_request_start",0);
  1373.         encrypt_send_request_start();
  1374.     }
  1375.     tn_wait("encrypt start");
  1376.     tn_push();
  1377. }
  1378.  
  1379. VOID
  1380. ck_tn_enc_stop()
  1381. {
  1382.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1383.         return;
  1384.     if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop ||
  1385.          !(encrypt_is_decrypting() || encrypt_is_encrypting())) {
  1386.         debug(F110,"ck_tn_enc_stop","nothing to do",0);
  1387.       return;
  1388.     }
  1389.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1390.     if (TELOPT_U(TELOPT_ENCRYPTION) && encrypt_is_decrypting()) {
  1391.         debug(F110,"ck_tn_enc_stop","encrypt_send_request_end",0);
  1392.         encrypt_send_request_end();
  1393.     }
  1394.     if (TELOPT_ME(TELOPT_ENCRYPTION) && encrypt_is_encrypting()) {
  1395.         debug(F110,"ck_tn_enc_stop","encrypt_send_end",0);
  1396.         encrypt_send_end();
  1397.     }
  1398.     tn_wait("encrypt stop");
  1399.     tn_push();
  1400. }
  1401. #endif /* CK_ENCRYPTION */
  1402.  
  1403. /*  C K _ K R B _ T N _ S B _ A U T H
  1404.  *  An interface between the C-Kermit Telnet Command Parser and the Authent-
  1405.  *  ication option parser implemented in the Kerberos Telnet client.
  1406.  *
  1407.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1408.  *  len  - the length of the buffer
  1409.  *
  1410.  *  Returns: 0 on success and -1 on failure
  1411.  */
  1412.  
  1413. int
  1414. #ifdef CK_ANSIC
  1415. ck_tn_sb_auth(char * sb, int len)
  1416. #else /* CK_ANSIC */
  1417. ck_tn_sb_auth(sb,len) char * sb; int len;
  1418. #endif /* CK_ANSIC */
  1419. {
  1420.     /* auth_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1421.     /* and it wants the length to exclude the IAC SE bytes               */
  1422.     CHAR * buf;
  1423.     int rc = -1;
  1424.  
  1425.     buf = malloc(len-1);
  1426.     if ( !buf ) return(-1);
  1427.  
  1428.     buf[0] = SB;
  1429.     memcpy( &buf[1], sb, len-2 );
  1430.     rc = auth_parse(buf,len-1);
  1431.     free(buf);
  1432.     debug(F111,"ck_tn_sb_auth","rc",rc);
  1433.     if (rc == AUTH_FAILURE) {
  1434.         authentication_version = AUTHTYPE_NULL;
  1435. #ifndef NOLOCAL
  1436. #ifdef OS2
  1437.         ipadl25();
  1438. #endif /* OS2 */
  1439. #endif /* NOLOCAL */
  1440.         return(-1);
  1441.     }
  1442. #ifndef NOLOCAL
  1443. #ifdef OS2
  1444.     ipadl25();
  1445. #endif /* OS2 */
  1446. #endif /* NOLOCAL */
  1447.     return(0);
  1448. }
  1449.  
  1450. /*  C K _ K R B _ T N _ S B _ E N C R Y P T
  1451.  *  An interface between the C-Kermit Telnet Command Parser and the Encryption
  1452.  *  option parser implemented in the Kerberos Telnet client.
  1453.  *
  1454.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1455.  *  len  - the length of the buffer
  1456.  *
  1457.  *  Returns: Always returns 0 for success since encrypt_parse is void
  1458.  */
  1459.  
  1460.  
  1461. int
  1462. #ifdef CK_ANSIC
  1463. ck_tn_sb_encrypt(char * sb, int len)
  1464. #else
  1465. ck_tn_sb_encrypt(sb,len) char * sb; int len;
  1466. #endif /* CK_ANSIC */
  1467. {
  1468.     /* encrypt_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1469.     /* and it wants the length to exclude the IAC SE bytes                  */
  1470. #ifdef CK_ENCRYPTION
  1471.     char * buf;
  1472.     int rc = -1;
  1473.  
  1474.     buf = malloc(len-1);
  1475.     if ( !buf ) return(-1);
  1476.  
  1477.     buf[0] = SB;
  1478.     memcpy( &buf[1], sb, len-2 );
  1479.     rc = encrypt_parse((CHAR *)buf,len-1);
  1480.  
  1481.     if (rc < 0) {
  1482.         free(buf);
  1483.         return(-1);
  1484.     }
  1485.  
  1486.     /* This is a hack.  It does not belong here but should really be in */
  1487.     /* encrypt_parse() but in K95 the encrypt_parse() routine does not  */
  1488.     /* have access to the telopt_states array.                          */
  1489.     if ( buf[1] == ENCRYPT_REQEND )
  1490.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1491.     else if ( buf[1] == ENCRYPT_REQSTART )
  1492.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1493. #ifndef NOLOCAL
  1494. #ifdef OS2
  1495.     ipadl25();
  1496. #endif /* OS2 */
  1497. #endif /* NOLOCAL */
  1498.     free(buf);
  1499. #endif /* ENCRYPTION */
  1500.     return(0);
  1501. }
  1502.  
  1503.  
  1504. /*  C K _ K R B _ E N C R Y P T I N G
  1505.  *  Returns 1 if we are encrypting and 0 if we are not
  1506.  */
  1507.  
  1508. int
  1509. #ifdef CK_ANSIC
  1510. ck_tn_encrypting(VOID)
  1511. #else /* CK_ANSIC */
  1512. ck_tn_encrypting()
  1513. #endif /* CK_ANSIC */
  1514. {
  1515. #ifdef CK_ENCRYPTION
  1516.     if ( g_kstream == NULL )
  1517.         return(0);
  1518.     if ( g_kstream->encrypt && encrypt_is_encrypting()) {
  1519.         debug(F111,"ck_tn_encrypting","encrypting",
  1520.                g_kstream->encrypt_type);
  1521.         return(g_kstream->encrypt_type);
  1522.     }
  1523. #endif /* CK_ENCRYPTION */
  1524.     debug(F110,"ck_tn_encrypting","not encrypting",0);
  1525.     return(0);
  1526. }
  1527.  
  1528. /*  C K _ K R B _ D E C R Y P T I N G
  1529.  *  Returns 1 if we are decrypting and 0 if we are not
  1530.  */
  1531.  
  1532. int
  1533. #ifdef CK_ANSIC
  1534. ck_tn_decrypting(VOID)
  1535. #else
  1536. ck_tn_decrypting()
  1537. #endif /* CK_ANSIC */
  1538. {
  1539. #ifdef CK_ENCRYPTION
  1540.     if ( g_kstream == NULL )
  1541.         return(0);
  1542.     if ( g_kstream->decrypt && encrypt_is_decrypting()) {
  1543.         debug(F111,"ck_tn_decrypting","decrypting",
  1544.                g_kstream->decrypt_type);
  1545.         return(g_kstream->decrypt_type);
  1546.     }
  1547. #endif /* CK_ENCRYPTION */
  1548.     debug(F110,"ck_tn_decrypting","not decrypting",0);
  1549.     return(0);
  1550. }
  1551.  
  1552. /*  C K _ K R B _ A U T H E N T I C A T E D
  1553.  *  Returns the authentication type: AUTHTYPE_NULL, AUTHTYPE_KERBEROS4,
  1554.  *  or AUTHTYPE_KERBEROS5, AUTHTYPE_SRP, ... (see ckctel.h)
  1555.  */
  1556.  
  1557. int
  1558. #ifdef CK_ANSIC
  1559. ck_tn_authenticated(VOID)
  1560. #else
  1561. ck_tn_authenticated()
  1562. #endif
  1563. {
  1564.     return(authentication_version);
  1565. }
  1566.  
  1567. /*  C K _ K R B _ E N C R Y P T
  1568.  *  encrypts n characters in s if we are encrypting
  1569.  */
  1570.  
  1571. VOID
  1572. #ifdef CK_ANSIC
  1573. ck_tn_encrypt( char * s, int n )
  1574. #else
  1575. ck_tn_encrypt( s,n ) char * s; int n;
  1576. #endif
  1577. {
  1578. #ifdef CK_ENCRYPTION
  1579.     struct kstream_data_block i;
  1580.  
  1581.     if (g_kstream->encrypt && encrypt_is_encrypting()) {
  1582. #ifdef DEBUG
  1583.       hexdump("from plaintext", s, n);
  1584. #endif
  1585.         i.ptr = s;
  1586.         i.length = n;
  1587.         g_kstream->encrypt(&i, NULL);
  1588. #ifdef DEBUG
  1589.         hexdump("to cyphertext", s, n);
  1590. #endif
  1591.     }
  1592.     else debug(F101,"ck_tn_encrypt not encrypting","",n);
  1593. #endif /* ENCRYPTION */
  1594. }
  1595.  
  1596. /*  C K _ K R B _ D E C R Y P T
  1597.  *  decrypts n characters in s if we are decrypting
  1598.  */
  1599.  
  1600. VOID
  1601. #ifdef CK_ANSIC
  1602. ck_tn_decrypt( char * s, int n )
  1603. #else
  1604. ck_tn_decrypt( s,n ) char * s; int n;
  1605. #endif
  1606. {
  1607. #ifdef CK_ENCRYPTION
  1608.     struct kstream_data_block i;
  1609.  
  1610.     if (g_kstream->decrypt && encrypt_is_decrypting()) {
  1611.  
  1612. #ifdef DEBUG
  1613.         hexdump("from cyphertext", s, n);
  1614. #endif
  1615.  
  1616.         i.ptr = s;
  1617.         i.length = n;
  1618.         g_kstream->decrypt(&i, NULL);
  1619. #ifdef DEBUG
  1620.         hexdump("to plaintext", s, n);
  1621. #endif
  1622.     }
  1623.     else debug(F101,"ck_tn_decrypt not decrypting","",n);
  1624. #endif /* ENCRYPTION */
  1625. }
  1626.  
  1627. /*  S E N D K 5 A U T H S B
  1628.  *  Send a Kerberos 5 Authentication Subnegotiation to host and
  1629.  *  output appropriate Telnet Debug messages
  1630.  *
  1631.  *  type - Sub Negotiation type
  1632.  *  data - ptr to buffer containing data
  1633.  *  len  - len of buffer if not NUL terminated
  1634.  *
  1635.  *  returns number of characters sent or error value
  1636.  */
  1637.  
  1638. static int
  1639. #ifdef CK_ANSIC
  1640. SendK5AuthSB(int type, void *data, int len)
  1641. #else
  1642. SendK5AuthSB(type,data,len) int type; void *data; int len;
  1643. #endif
  1644. {
  1645.     int rc;
  1646.     unsigned char *p = str_data + 3;
  1647.     unsigned char *cd = (unsigned char *)data;
  1648.     extern int sstelnet;
  1649.  
  1650. #ifdef CK_SSL
  1651.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1652.         if (ttchk() < 0)
  1653.           return(0);
  1654.         else
  1655.           return(1);
  1656.     }
  1657. #endif /* CK_SSL */
  1658.  
  1659.     if ( type < 0 || type > 7 )         /* Check for invalid values */
  1660.         return(0);
  1661.  
  1662.     if (!cd) {
  1663.         cd = (unsigned char *)"";
  1664.         len = 0;
  1665.     }
  1666.  
  1667.     if (len == -1)                        /* Use strlen() for len */
  1668.         len = strlen((char *)cd);
  1669.  
  1670.     /* Construct Message */
  1671.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1672.     *p++ = AUTHTYPE_KERBEROS_V5;
  1673.     *p = AUTH_CLIENT_TO_SERVER;
  1674.     *p |= auth_how;
  1675. #ifdef CK_ENCRYPTION
  1676.     *p |= auth_crypt;
  1677. #endif
  1678. #ifdef USE_INI_CRED_FWD
  1679.     if (auth_fwd)
  1680.         *p |= INI_CRED_FWD_ON;
  1681. #endif /* USE_INI_CRED_FWD */
  1682.     p++;
  1683.     *p++ = type;
  1684.     while (len-- > 0) {
  1685.         if ((*p++ = *cd++) == IAC)
  1686.             *p++ = IAC;
  1687.     }
  1688.     *p++ = IAC;
  1689.     *p++ = SE;
  1690.  
  1691.     /* Handle Telnet Debugging Messages */
  1692.     if (deblog || tn_deb || debses) {
  1693.         int i;
  1694.         int deblen=p-str_data-2;
  1695.         char *s=NULL;
  1696.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1697.             auth_crypt
  1698. #ifdef USE_INI_CRED_FWD
  1699.               | (auth_fwd?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  1700. #endif /* USE_INI_CRED_FWD */
  1701.                     ;
  1702.  
  1703.         switch (type) {
  1704.         case 0:
  1705.             s = "AUTH";
  1706.             break;
  1707.         case 1:
  1708.             s = "REJECT";
  1709.             break;
  1710.         case 2:
  1711.             s = "ACCEPT";
  1712.             break;
  1713.         case 3:
  1714.             s = "RESPONSE";
  1715.             break;
  1716.         case 4:
  1717.             s = "FORWARD";
  1718.             break;
  1719.         case 5:
  1720.             s = "FORWARD_ACCEPT";
  1721.             break;
  1722.         case 6:
  1723.             s = "FORWARD_REJECT";
  1724.             break;
  1725.         case 7:
  1726.             s = "TLS_VERIFY";
  1727.                 break;
  1728.         }
  1729.  
  1730.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1731.                   "TELNET SENT SB ",
  1732.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1733.                  str_data[3] == TELQUAL_IS ? "IS" :
  1734.                  str_data[3] == TELQUAL_REPLY ? "REPLY" : "???"," ",
  1735.                  AUTHTYPE_NAME(authentication_version)," ",
  1736.                  AUTHMODE_NAME(mode)," ",
  1737.                  s," ",NULL);
  1738.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1739.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1740.         debug(F100,tn_msg,"",0);
  1741.         if (tn_deb || debses) tn_debug(tn_msg);
  1742.     }
  1743.  
  1744.     /* Send data */
  1745. #ifdef OS2
  1746.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1747. #endif
  1748.     rc = ttol((CHAR *)str_data, p - str_data);
  1749. #ifdef OS2
  1750.     ReleaseTelnetMutex();
  1751. #endif
  1752.     debug(F111,"SendK5AuthSB","ttol()",rc);
  1753.     return(rc);
  1754. }
  1755.  
  1756. /*  S E N D K 4 A U T H S B
  1757.  *  Send a Kerberos 4 Authentication Subnegotiation to host and
  1758.  *  output appropriate Telnet Debug messages
  1759.  *
  1760.  *  type - Sub Negotiation type
  1761.  *  data - ptr to buffer containing data
  1762.  *  len  - len of buffer if not NUL terminated
  1763.  *
  1764.  *  returns number of characters sent or error value
  1765.  */
  1766.  
  1767. static int
  1768. #ifdef CK_ANSIC
  1769. SendK4AuthSB(int type, void *data, int len)
  1770. #else
  1771. SendK4AuthSB(type,data,len) int type; void *data; int len;
  1772. #endif
  1773. {
  1774.     int rc;
  1775.     unsigned char *p = str_data + 3;
  1776.     unsigned char *cd = (unsigned char *)data;
  1777.     extern int sstelnet;
  1778.     int mode = (auth_how & AUTH_HOW_MASK) |
  1779.         auth_crypt;
  1780.  
  1781.     if ( type < 0 || type > 4 )         /* Check for invalid values */
  1782.         return(0);
  1783.  
  1784. #ifdef CK_SSL
  1785.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1786.         if (ttchk() < 0)
  1787.           return(0);
  1788.         else
  1789.           return(1);
  1790.     }
  1791. #endif /* CK_SSL */
  1792.  
  1793.     if (!cd) {
  1794.         cd = (unsigned char *)"";
  1795.         len = 0;
  1796.     }
  1797.  
  1798.     if (len == -1)                        /* Use strlen() for len */
  1799.         len = strlen((char *)cd);
  1800.  
  1801.  
  1802.     /* Construct Message */
  1803.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1804.     *p++ = AUTHTYPE_KERBEROS_V4;
  1805.     *p = AUTH_CLIENT_TO_SERVER;
  1806.     *p |= mode;
  1807.     p++;
  1808.     *p++ = type;
  1809.     while (len-- > 0) {
  1810.         if ((*p++ = *cd++) == IAC)
  1811.             *p++ = IAC;
  1812.         }
  1813.     *p++ = IAC;
  1814.     *p++ = SE;
  1815.  
  1816.     /* Handle Telnet Debugging Messages */
  1817.     if (deblog || tn_deb || debses) {
  1818.         int i;
  1819.         int deblen=p-str_data-2;
  1820.         char *s=NULL;
  1821.  
  1822.         switch (type) {
  1823.         case 0:
  1824.             s = "AUTH";
  1825.             break;
  1826.         case 1:
  1827.             s = "REJECT";
  1828.             break;
  1829.         case 2:
  1830.             s = "ACCEPT";
  1831.             break;
  1832.         case 3:
  1833.             s = "CHALLENGE";
  1834.             break;
  1835.         case 4:
  1836.             s = "RESPONSE";
  1837.             break;
  1838.         }
  1839.  
  1840.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  1841.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1842.                  str_data[3] == TELQUAL_IS ? "IS" :
  1843.                  (str_data[3] == TELQUAL_REPLY ? "REPLY" : "???")," ",
  1844.                  AUTHTYPE_NAME(authentication_version)," ",
  1845.                  AUTHMODE_NAME(mode)," ",
  1846.                  s," ",NULL);
  1847.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1848.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1849.         debug(F100,tn_msg,"",0);
  1850.         if (tn_deb || debses) tn_debug(tn_msg);
  1851.     }
  1852.  
  1853.     /* Send data */
  1854. #ifdef OS2
  1855.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1856. #endif
  1857.     rc = ttol((CHAR *)str_data, p - str_data);
  1858. #ifdef OS2
  1859.     ReleaseTelnetMutex();
  1860. #endif
  1861.     debug(F111,"SendK4AuthSB","ttol()",rc);
  1862.     return(rc);
  1863. }
  1864.  
  1865. /*  S E N D S R P A U T H S B
  1866.  *  Send a SRP Authentication Subnegotiation to host and
  1867.  *  output appropriate Telnet Debug messages
  1868.  *
  1869.  *  type - Sub Negotiation type
  1870.  *  data - ptr to buffer containing data
  1871.  *  len  - len of buffer if not NUL terminated
  1872.  *
  1873.  *  returns number of characters sent or error value
  1874.  */
  1875.  
  1876. static int
  1877. #ifdef CK_ANSIC
  1878. SendSRPAuthSB(int type, void *data, int len)
  1879. #else
  1880. SendSRPAuthSB(type,data,len) int type; void *data; int len;
  1881. #endif
  1882. {
  1883.     int rc;
  1884.     unsigned char *p = str_data + 3;
  1885.     unsigned char *cd = (unsigned char *)data;
  1886.     extern int sstelnet;
  1887.  
  1888.     /* Check for invalid values */
  1889.     if ( type != SRP_EXP && type != SRP_RESPONSE &&
  1890.          type != SRP_REJECT && type != SRP_ACCEPT &&
  1891.          type != SRP_CHALLENGE && type != SRP_PARAMS &&
  1892.          type != SRP_AUTH)
  1893.         return(0);
  1894.  
  1895.     if (len == -1)                        /* Use strlen() for len */
  1896.         len = strlen((char *)cd);
  1897.  
  1898.     /* Construct Message */
  1899.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1900.     *p++ = AUTHTYPE_SRP;
  1901.     *p = AUTH_CLIENT_TO_SERVER;
  1902.     *p |= auth_how;
  1903. #ifdef CK_ENCRYPTION
  1904.     *p |= auth_crypt;
  1905. #endif
  1906.     p++;
  1907.     *p++ = type;
  1908.     while (len-- > 0) {
  1909.         if ((*p++ = *cd++) == IAC)
  1910.             *p++ = IAC;
  1911.         }
  1912.     *p++ = IAC;
  1913.     *p++ = SE;
  1914.  
  1915.     /* Handle Telnet Debugging Messages */
  1916.     if (deblog || tn_deb || debses) {
  1917.         int i;
  1918.         int deblen=p-str_data-2;
  1919.         char *s=NULL;
  1920.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1921.             auth_crypt;
  1922.  
  1923.         switch (type) {
  1924.         case 0:
  1925.             s = "AUTH";
  1926.             break;
  1927.         case 1:
  1928.             s = "REJECT";
  1929.             break;
  1930.         case 2:
  1931.             s = "ACCEPT";
  1932.             break;
  1933.         case 3:
  1934.             s = "CHALLENGE";
  1935.             break;
  1936.         case 4:
  1937.             s = "RESPONSE";
  1938.             break;
  1939.         case 5:
  1940.             s = "FORWARD";
  1941.             break;
  1942.         case 6:
  1943.             s = "FORWARD_ACCEPT";
  1944.             break;
  1945.         case 7:
  1946.             s = "FORWARD_REJECT";
  1947.             break;
  1948.         case 8:
  1949.             s = "EXP";
  1950.             break;
  1951.         case 9:
  1952.             s = "PARAMS";
  1953.             break;
  1954.         }
  1955.  
  1956.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1957.                   "TELNET SENT SB ",
  1958.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1959.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  1960.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  1961.                  AUTHTYPE_NAME(authentication_version)," ",
  1962.                  AUTHMODE_NAME(mode)," ",
  1963.                  s," ",NULL);
  1964.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1965.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1966.         debug(F100,tn_msg,"",0);
  1967.         if (tn_deb || debses) tn_debug(tn_msg);
  1968.     }
  1969.  
  1970.     /* Send data */
  1971. #ifdef OS2
  1972.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1973. #endif
  1974.     rc = ttol((CHAR *)str_data, p - str_data);
  1975. #ifdef OS2
  1976.     ReleaseTelnetMutex();
  1977. #endif
  1978.     return(rc);
  1979. }
  1980.  
  1981. #ifdef CK_ENCRYPTION
  1982. /*
  1983.  * Function: Enable or disable the encryption process.
  1984.  *
  1985.  * Parameters:
  1986.  *      enable - TRUE to enable, FALSE to disable.
  1987.  */
  1988. static VOID
  1989. #ifdef CK_ANSIC
  1990. auth_encrypt_enable(BOOL enable)
  1991. #else
  1992. auth_encrypt_enable(enable) BOOL enable;
  1993. #endif
  1994. {
  1995.   encrypt_flag = enable;
  1996. }
  1997. #endif
  1998.  
  1999. /*
  2000.  * Function: Abort the authentication process
  2001.  *
  2002.  * Parameters:
  2003.  */
  2004. static VOID
  2005. #ifdef CK_ANSIC
  2006. auth_abort(char *errmsg, long r)
  2007. #else
  2008. auth_abort(errmsg,r) char *errmsg; long r;
  2009. #endif
  2010. {
  2011.     char buf[9];
  2012.     extern int sstelnet;
  2013.  
  2014. #ifdef CK_SSL
  2015.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2016.         return;
  2017.     }
  2018. #endif /* CK_SSL */
  2019.     debug(F111,"auth_abort",errmsg,r);
  2020.  
  2021.     /* Construct Telnet Debugging messages */
  2022.     if (deblog || tn_deb || debses) {
  2023.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2024.                   "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2025.                   " IS ",AUTHTYPE_NAME(AUTHTYPE_NULL)," ",
  2026.                    AUTHTYPE_NAME(AUTHTYPE_NULL)," IAC SE",
  2027.                    NULL,NULL,NULL,NULL,NULL
  2028.                  );
  2029.         debug(F100,tn_msg,"",0);
  2030.         if (tn_deb || debses) tn_debug(tn_msg);
  2031.     }
  2032.  
  2033.     /* Construct the Abort message to send to the host   */
  2034.     /* Basicly we change the authentication type to NULL */
  2035.     sprintf(buf, "%c%c%c%c%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2036.              sstelnet ? TELQUAL_REPLY : TELQUAL_IS, AUTHTYPE_NULL,
  2037.              AUTHTYPE_NULL, IAC, SE);   /* safe */
  2038. #ifdef OS2
  2039.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2040. #endif
  2041.     ttol((CHAR *)buf, 8);
  2042. #ifdef OS2
  2043.     ReleaseTelnetMutex();
  2044. #endif
  2045.  
  2046.     /* If there is an error message, and error number construct */
  2047.     /* an explanation to display to the user                    */
  2048.     if (errmsg != NULL) {
  2049.         ckstrncpy(strTmp, errmsg, AUTHTMPBL);
  2050.     } else
  2051.         strTmp[0] = '\0';
  2052.  
  2053.  
  2054.     if (r != AUTH_SUCCESS) {
  2055.         ckstrncat(strTmp, "\r\n",AUTHTMPBL);
  2056. #ifdef KRB4
  2057.         if ( authentication_version == AUTHTYPE_KERBEROS_V4 ) {
  2058.             ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),
  2059.                        AUTHTMPBL);
  2060.             debug(F111,"auth_abort",(char *)krb_get_err_text_entry(r),r);
  2061.         }
  2062. #endif
  2063. #ifdef KRB5
  2064.         if ( authentication_version == AUTHTYPE_KERBEROS_V5 ) {
  2065.             ckstrncat(strTmp, error_message(r),AUTHTMPBL);
  2066.             debug(F111,"auth_abort",error_message(r),r);
  2067.         }
  2068. #endif
  2069.     }
  2070.     printf("Authentication failed: %s\r\n",strTmp);
  2071. #ifdef CKSYSLOG
  2072.     if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  2073.         cksyslog(SYSLG_LI, 0, "Telnet authentication failure",
  2074.                   (char *) szUserNameRequested,
  2075.                   strTmp);
  2076.     }
  2077. #endif /* CKSYSLOG */
  2078.     authentication_version = AUTHTYPE_NULL;
  2079. }
  2080.  
  2081.  
  2082. /*
  2083.  * Function: Copy data to buffer, doubling IAC character if present.
  2084.  *
  2085.  */
  2086. int
  2087. #ifdef CK_ANSIC
  2088. copy_for_net(unsigned char *to, unsigned char *from, int c)
  2089. #else
  2090. copy_for_net(to,from,c) unsigned char *to; unsigned char *from; int c;
  2091. #endif
  2092. {
  2093.     int n;
  2094.  
  2095.     n = c;
  2096.     debug(F111,"copy_for_net","before",n);
  2097.     while (c-- > 0) {
  2098.         if ((*to++ = *from++) == IAC) {
  2099.             n++;
  2100.             *to++ = IAC;
  2101.         }
  2102.     }
  2103.     debug(F111,"copy_for_net","after",n);
  2104.     return n;
  2105. }
  2106.  
  2107. #ifdef CK_SSL
  2108. /*  S E N D S S L A U T H S B
  2109.  *  Send a SSL Authentication Subnegotiation to host and
  2110.  *  output appropriate Telnet Debug messages
  2111.  *
  2112.  *  type - Sub Negotiation type
  2113.  *  data - ptr to buffer containing data
  2114.  *  len  - len of buffer if not NUL terminated
  2115.  *
  2116.  *  returns number of characters sent or error value
  2117.  */
  2118.  
  2119. int
  2120. #ifdef CK_ANSIC
  2121. SendSSLAuthSB(int type, void *data, int len)
  2122. #else
  2123. SendSSLAuthSB(type,data,len) int type; void *data; int len;
  2124. #endif
  2125. {
  2126.     int rc;
  2127.     unsigned char *p = str_data + 3;
  2128.     unsigned char *cd = (unsigned char *)data;
  2129.     extern int sstelnet;
  2130.  
  2131.     /* Check for invalid values */
  2132.     if ( type != SSL_START && type != SSL_ACCEPT &&
  2133.          type != SSL_REJECT)
  2134.         return(0);
  2135.  
  2136.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2137.         if (ttchk() < 0)
  2138.           return(0);
  2139.         else
  2140.           return(1);
  2141.     }
  2142.  
  2143.     if (len == -1)                        /* Use strlen() for len */
  2144.         len = strlen((char *)cd);
  2145.  
  2146.     /* Construct Message */
  2147.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  2148.     *p++ = AUTHTYPE_SSL;
  2149.     *p = AUTH_CLIENT_TO_SERVER;
  2150.     *p |= auth_how;
  2151. #ifdef CK_ENCRYPTION
  2152.     *p |= auth_crypt;
  2153. #endif
  2154.     p++;
  2155.     *p++ = type;
  2156.     while (len-- > 0) {
  2157.         if ((*p++ = *cd++) == IAC)
  2158.             *p++ = IAC;
  2159.         }
  2160.     *p++ = IAC;
  2161.     *p++ = SE;
  2162.  
  2163.     /* Handle Telnet Debugging Messages */
  2164.     if (deblog || tn_deb || debses) {
  2165.         int i;
  2166.         int deblen=p-str_data-2;
  2167.         char *s=NULL;
  2168.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  2169.             (auth_crypt?AUTH_ENCRYPT_USING_TELOPT:AUTH_ENCRYPT_OFF);
  2170.  
  2171.         switch (type) {
  2172.         case SSL_START:
  2173.             s = "START";
  2174.             break;
  2175.         case SSL_ACCEPT:
  2176.             s = "ACCEPT";
  2177.             break;
  2178.         case SSL_REJECT:
  2179.             s = "REJECT";
  2180.             break;
  2181.         }
  2182.  
  2183.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2184.                   "TELNET SENT SB ",
  2185.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  2186.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  2187.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  2188.                  AUTHTYPE_NAME(authentication_version)," ",
  2189.                  AUTHMODE_NAME(mode)," ",
  2190.                  s," ",NULL);
  2191.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  2192.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2193.         debug(F100,tn_msg,"",0);
  2194.         if (tn_deb || debses) tn_debug(tn_msg);
  2195.     }
  2196.  
  2197.     /* Send data */
  2198. #ifdef OS2
  2199.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2200. #endif
  2201.     rc = ttol((CHAR *)str_data, p - str_data);
  2202. #ifdef OS2
  2203.     ReleaseTelnetMutex();
  2204. #endif
  2205.     return(rc);
  2206. }
  2207. #endif  /* CK_SSL */
  2208.  
  2209. int
  2210. tn_how_ok(int how)
  2211. {
  2212.     switch ( tn_auth_how ) {
  2213.     case TN_AUTH_HOW_ANY:
  2214.         return(1);
  2215.     case TN_AUTH_HOW_ONE_WAY:
  2216.         return((how & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY);
  2217.     case TN_AUTH_HOW_MUTUAL:
  2218.         return((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL);
  2219.     default:
  2220.         return(0);
  2221.     }
  2222. }
  2223.  
  2224. int
  2225. tn_enc_ok(int enc)
  2226. {
  2227.     switch ( tn_auth_enc ) {
  2228.     case TN_AUTH_ENC_ANY:
  2229.         if ((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  2230.             (!ck_ssleay_is_installed()
  2231. #ifdef CK_SSL
  2232.              || !ssl_finished_messages ||
  2233.              !(tls_active_flag || ssl_active_flag)
  2234. #endif /* CK_SSL */
  2235.              )) {
  2236. #ifdef CK_SSL
  2237.             if (!ssl_finished_messages)
  2238.                 debug(F100,"tn_enc_ok !ssl_finished_messages","",0);
  2239. #endif /* CK_SSL */
  2240.             return(0);
  2241.         }
  2242.         return(1);
  2243.     case TN_AUTH_ENC_NONE:
  2244.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_OFF);
  2245.     case TN_AUTH_ENC_TELOPT:
  2246.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT);
  2247.     case TN_AUTH_ENC_EXCH:
  2248.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_AFTER_EXCHANGE);
  2249.     case TN_AUTH_ENC_TLS:
  2250.         return(((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) &&
  2251.                ck_ssleay_is_installed()
  2252. #ifdef CK_SSL
  2253.                && ssl_finished_messages &&
  2254.                (tls_active_flag || ssl_active_flag)
  2255. #endif /* CK_SSL */
  2256.            );
  2257.     default:
  2258.         return(0);
  2259.     }
  2260. }
  2261.  
  2262. static int
  2263. atok(int at) {
  2264.     int i;
  2265.     if ( auth_type_user[0] == AUTHTYPE_AUTO )
  2266.         return(1);
  2267.     if ( auth_type_user[0] == AUTHTYPE_NULL )
  2268.         return(0);
  2269.  
  2270.     for ( i=0;
  2271.           i<AUTHTYPLSTSZ && auth_type_user[i] != AUTHTYPE_NULL;
  2272.           i++ ) {
  2273.         if ( auth_type_user[i] == at )
  2274.             return(1);
  2275.     }
  2276.  
  2277.     return(0);
  2278. }
  2279.  
  2280.  
  2281. /*
  2282.  * Function: Parse authentication send command
  2283.  *
  2284.  * Parameters:
  2285.  *  parsedat - the sub-command data.
  2286.  *
  2287.  *      end_sub - index of the character in the 'parsedat' array which
  2288.  *              is the last byte in a sub-negotiation
  2289.  *
  2290.  * Returns: Kerberos error code.
  2291.  */
  2292.  
  2293. static unsigned char send_list[512];
  2294. static int  send_len = 0;
  2295.  
  2296. _PROTOTYP(static int auth_send, (unsigned char *parsedat, int end_sub));
  2297.  
  2298. static int
  2299. #ifdef CK_ANSIC
  2300. auth_resend(int type)
  2301. #else
  2302. auth_resend(type) int type;
  2303. #endif /* CK_ANSIC */
  2304. {
  2305.     int i=2;
  2306.     while (i+1 <= send_len) {
  2307.         if (send_list[i] == type) {
  2308.             int j;
  2309.             send_len -= 2;
  2310.             for (j = i; j < send_len; j++)
  2311.                 send_list[j] = send_list[j+2];
  2312.         } else {
  2313.             i += 2;
  2314.         }
  2315.     }
  2316.     return(auth_send(send_list,send_len));
  2317. }
  2318.  
  2319. static int
  2320. #ifdef CK_ANSIC
  2321. auth_send(unsigned char *parsedat, int end_sub)
  2322. #else
  2323. auth_send(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  2324. #endif
  2325. {
  2326.     static unsigned char buf[4096];
  2327.     unsigned char *pname;
  2328.     int plen;
  2329.     int r;
  2330.     int i;
  2331.     int mode;
  2332. #ifdef MIT_CURRENT
  2333. #ifdef CK_ENCRYPTION
  2334.     krb5_data data;
  2335.     krb5_enc_data encdata;
  2336.     krb5_error_code code;
  2337.     krb5_keyblock random_key;
  2338. #endif /* ENCRYPTION */
  2339. #endif /* MIT_CURRENT */
  2340. #ifdef KRB5
  2341.     int krb5_msg = 0;
  2342. #endif /* KRB5 */
  2343. #ifdef KRB4
  2344.     int krb4_msg = 0;
  2345. #endif /* KRB4 */
  2346. #ifdef GSSAPI_KRB5
  2347.     int gssk5_msg = 0;
  2348. #endif /* GSSAPI_KRB5 */
  2349.     int iaccnt=0;
  2350.  
  2351. #ifdef CK_SSL
  2352.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2353.         return(AUTH_SUCCESS);
  2354. #endif /* CK_SSL */
  2355.  
  2356.     auth_how = -1;              /* We have not found an auth method  */
  2357.     auth_crypt = 0;             /* We are not using encryption (yet) */
  2358.     send_len = end_sub > 512 ? 512 : end_sub;
  2359.     memcpy(send_list,parsedat,send_len);
  2360.  
  2361.     /* Search the list of acceptable Authentication types sent from */
  2362.     /* the host and find one that we support                        */
  2363.  
  2364.     /* For Kerberos authentications, try to determine if we have a  */
  2365.     /* valid TGT, if not skip over the authentication type because  */
  2366.     /* we wouldn't be able to successfully login anyway.  Perhaps   */
  2367.     /* there is another supported authentication which we could use */
  2368.  
  2369. #ifdef NO_FTP_AUTH
  2370.     /* If the userid is "ftp" or "anonymous" refuse to perform AUTH */
  2371.     /* for Kerberos or SRP.                                         */
  2372. #endif /* NO_FTP_AUTH */
  2373.  
  2374.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  2375.     for (i = 2; i+1 <= end_sub; i += 2) {
  2376. #ifdef NTLM
  2377.         if (parsedat[i] == AUTHTYPE_NTLM &&
  2378.              ck_ntlm_is_valid(1) &&
  2379.              ntlm_auth_send() == 0) {
  2380.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2381.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2382. #ifdef CK_ENCRYPTION
  2383.                 /* NTLM does not support Telnet Encryption */
  2384.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2385.                     continue;
  2386.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2387. #endif /* CK_ENCRYPTION */
  2388.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2389.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2390.                 authentication_version = AUTHTYPE_NTLM;
  2391.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2392.                 break;
  2393.             }
  2394.         }
  2395. #endif /* NTLM */
  2396. #ifdef CK_SSL
  2397.         if ( parsedat[i] == AUTHTYPE_SSL && ssl_initialized &&
  2398. #ifdef SSLDLL
  2399.              ck_ssleay_is_installed() &&
  2400. #endif /* SSLDLL */
  2401.              !tls_active_flag && !ssl_active_flag
  2402. #ifndef USE_CERT_CB
  2403.              && tls_load_certs(ssl_ctx,ssl_con,0)
  2404. #endif /* USE_CERT_CB */
  2405.              ) {
  2406.  
  2407.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2408.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2409. #ifdef CK_ENCRYPTION
  2410.                 /* SSL does not support Telnet Encryption */
  2411.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2412.                     continue;
  2413.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2414. #endif /* CK_ENCRYPTION */
  2415.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2416.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2417.                 authentication_version = AUTHTYPE_SSL;
  2418.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2419.                 break;
  2420.             }
  2421.         }
  2422. #endif /* SSL */
  2423. #ifdef CK_SRP
  2424.         if ( parsedat[i] == AUTHTYPE_SRP
  2425. #ifdef SRPDLL
  2426.              && hSRP
  2427. #endif /* SRPDLL */
  2428. #ifdef NO_FTP_AUTH
  2429.              && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2430. #endif /* NO_FTP_AUTH */
  2431.              ) {
  2432.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2433.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2434. #ifdef PRE_SRP_1_4_5
  2435.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2436.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2437.                     continue;
  2438. #endif /* PRE_SRP_1_4_5 */
  2439.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2440.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2441.                     (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2442.                      TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2443.                     continue;
  2444.  
  2445.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2446. #ifdef CK_ENCRYPTION
  2447.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2448.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2449.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2450.                 }
  2451. #endif /* CK_ENCRYPTION */
  2452. #ifdef CK_SSL
  2453.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2454.                      ck_ssleay_is_installed() &&
  2455.                      (tls_active_flag || ssl_active_flag) ) {
  2456.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2457.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2458.                 }
  2459. #endif /* CK_SSL */
  2460.                 authentication_version = AUTHTYPE_SRP;
  2461.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2462.                 break;
  2463.             }
  2464.         }
  2465. #endif /* SRP */
  2466. #ifdef GSSAPI_KRB5
  2467.         if (parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2468.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2469.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2470. #ifdef OS2
  2471.             hGSSAPI &&
  2472. #endif /* OS2 */
  2473. #ifdef NO_FTP_AUTH
  2474.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2475. #endif /* NO_FTP_AUTH */
  2476.              ck_gssapi_is_installed() && !gssk5_msg)
  2477.         {
  2478.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2479.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2480.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2481.             {
  2482.                 /* If we are auto-getting TGTs, try */
  2483.                 if ( !ck_krb5_is_tgt_valid() ) {
  2484.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2485.                 }
  2486.                 gssk5_msg = 1;
  2487.             }
  2488.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2489.                       AUTH_CLIENT_TO_SERVER &&
  2490.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2491.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2492. #ifdef CK_ENCRYPTION
  2493.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2494.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2495.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2496.                 }
  2497. #endif /* CK_ENCRYPTION */
  2498.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2499.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2500.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2501.                 break;
  2502.             }
  2503.         }
  2504. #endif /* GSSAPI_KRB5 */
  2505. #ifdef KRB5
  2506.         if (parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2507. #ifdef OS2
  2508.              hKRB5_32 &&
  2509. #endif /* OS2 */
  2510. #ifdef NO_FTP_AUTH
  2511.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2512. #endif /* NO_FTP_AUTH */
  2513.              ck_krb5_is_installed() && !krb5_msg) {
  2514.  
  2515.             /* Without encryption we can't perform mutual authentication */
  2516.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2517.                  !ck_crypt_is_installed())
  2518.                 continue;
  2519.  
  2520.             /* Skip over entries that request credential forwarding */
  2521.             /* if we are not forwarding.                            */
  2522.             if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2523.                 (forward_flag &&
  2524.                   ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2525.                 continue;
  2526.  
  2527.             if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2528.                                 parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2529.                                 parsedat[i+1] & INI_CRED_FWD_MASK) )
  2530.             {
  2531.                 /* If we are auto-getting TGTs, try */
  2532.                 if ( !ck_krb5_is_tgt_valid() ) {
  2533.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2534.                 }
  2535.                 krb5_msg = 1;
  2536.             }
  2537.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2538.                       AUTH_CLIENT_TO_SERVER &&
  2539.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2540.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2541.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2542.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2543.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2544.                     continue;
  2545.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2546.                      AUTH_ENCRYPT_START_TLS) &&
  2547.                      (!ck_ssleay_is_installed()
  2548. #ifdef CK_SSL
  2549.                        || !(tls_active_flag || ssl_active_flag)
  2550. #endif /* CK_SSL */
  2551.                        ))
  2552.                     continue;
  2553.  
  2554.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2555. #ifdef CK_ENCRYPTION
  2556.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2557.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2558.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2559.                 }
  2560. #endif /* CK_ENCRYPTION */
  2561. #ifdef CK_SSL
  2562.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2563.                      ck_ssleay_is_installed() &&
  2564.                      (tls_active_flag || ssl_active_flag) ) {
  2565.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2566.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2567.                 }
  2568. #endif /* CK_SSL */
  2569.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2570.                 authentication_version = AUTHTYPE_KERBEROS_V5;
  2571.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2572.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2573.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2574.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2575.                 }
  2576.                 break;
  2577.             }
  2578.         }
  2579. #endif /* KRB5 */
  2580. #ifdef KRB4
  2581.         if (parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2582. #ifdef OS2
  2583.              hKRB4_32 &&
  2584. #endif /* OS2 */
  2585. #ifdef NO_FTP_AUTH
  2586.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2587. #endif /* NO_FTP_AUTH */
  2588.              ck_krb4_is_installed() && !krb4_msg) {
  2589.             int rc = 0;
  2590.  
  2591.             /* Without encryption we can't perform mutual authentication */
  2592.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2593.                  !ck_crypt_is_installed() )
  2594.                 continue;
  2595.  
  2596.             if ( !k4_auth_send() )
  2597.             {
  2598.                 /* If we are auto-getting TGTs, try */
  2599.                 if ( !ck_krb4_is_tgt_valid() ) {
  2600.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2601.                 }
  2602.                 krb4_msg = 1;
  2603.             }
  2604.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2605.                       AUTH_CLIENT_TO_SERVER &&
  2606.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2607. #ifdef CK_ENCRYPTION
  2608.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2609.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2610.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2611.                     continue;
  2612.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2613.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2614.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2615.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2616.                 }
  2617. #endif /* CK_ENCRYPTION */
  2618.                 authentication_version = AUTHTYPE_KERBEROS_V4;
  2619.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2620.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2621.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2622.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2623.                 }
  2624.                 break;
  2625.             }
  2626.         }
  2627. #endif /* KRB4 */
  2628.     }
  2629.     } else {
  2630.         for (i = 2; i+1 <= end_sub; i += 2) {
  2631. #ifdef CK_SSL
  2632.             if ( atok(AUTHTYPE_SSL) && parsedat[i] == AUTHTYPE_SSL &&
  2633. #ifdef SSLDLL
  2634.                  ck_ssleay_is_installed() &&
  2635. #endif /* SSLDLL */
  2636.                  !tls_active_flag && !ssl_active_flag && ssl_initialized
  2637. #ifndef USE_CERT_CB
  2638.                  && tls_load_certs(ssl_ctx,ssl_con,0)
  2639. #endif /* USE_CERT_CB */
  2640.                  )
  2641.             {
  2642.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2643.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2644. #ifdef CK_ENCRYPTION
  2645.                     /* SSL does not support Telnet Encryption */
  2646.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2647.                         continue;
  2648.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2649. #endif /* CK_ENCRYPTION */
  2650.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2651.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2652.                     authentication_version = AUTHTYPE_SSL;
  2653.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2654.                     break;
  2655.                 }
  2656.             }
  2657. #endif /* SSL */
  2658. #ifdef CK_SRP
  2659.             if ( atok(AUTHTYPE_SRP) &&
  2660.                  parsedat[i] == AUTHTYPE_SRP
  2661. #ifdef SRPDLL
  2662.                  && hSRP
  2663. #endif /* SRPDLL */
  2664. #ifdef NO_FTP_AUTH
  2665.                  && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2666. #endif /* NO_FTP_AUTH */
  2667.                  ) {
  2668.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2669.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2670. #ifdef PRE_SRP_1_4_5
  2671.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2672.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2673.                     continue;
  2674. #endif /* PRE_SRP_1_4_5 */
  2675.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2676.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2677.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2678.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2679.                     continue;
  2680.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2681.                      AUTH_ENCRYPT_START_TLS) &&
  2682.                      (!ck_ssleay_is_installed()
  2683. #ifdef CK_SSL
  2684.                        || !(tls_active_flag || ssl_active_flag)
  2685. #endif /* CK_SSL */
  2686.                        ))
  2687.                     continue;
  2688.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2689. #ifdef CK_ENCRYPTION
  2690.                     if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2691.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2692.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2693.                     }
  2694. #endif /* CK_ENCRYPTION */
  2695. #ifdef CK_SSL
  2696.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2697.                      ck_ssleay_is_installed() &&
  2698.                      (tls_active_flag || ssl_active_flag) ) {
  2699.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2700.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2701.                 }
  2702. #endif /* CK_SSL */
  2703.                     authentication_version = AUTHTYPE_SRP;
  2704.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2705.                     break;
  2706.                 }
  2707.             }
  2708. #endif /* SRP */
  2709. #ifdef GSSAPI_KRB5
  2710.         if (atok(AUTHTYPE_GSSAPI_KRB5) &&
  2711.             parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2712.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2713.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2714. #ifdef OS2
  2715.             hGSSAPI &&
  2716. #endif /* OS2 */
  2717. #ifdef NO_FTP_AUTH
  2718.             strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2719. #endif /* NO_FTP_AUTH */
  2720.             ck_gssapi_is_installed() && !gssk5_msg)
  2721.         {
  2722.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2723.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2724.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2725.             {
  2726.                 /* If we are auto-getting TGTs, try */
  2727.                 if ( !ck_krb5_is_tgt_valid() ) {
  2728.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2729.                 }
  2730.                 gssk5_msg = 1;
  2731.             }
  2732.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2733.                       AUTH_CLIENT_TO_SERVER &&
  2734.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2735.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2736. #ifdef CK_ENCRYPTION
  2737.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2738.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2739.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2740.                 }
  2741. #endif /* CK_ENCRYPTION */
  2742.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2743.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2744.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2745.                 break;
  2746.             }
  2747.         }
  2748. #endif /* GSSAPI_KRB5 */
  2749. #ifdef KRB5
  2750.             if ( atok(AUTHTYPE_KERBEROS_V5) &&
  2751.                  parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2752. #ifdef OS2
  2753.                  hKRB5_32 &&
  2754. #endif /* OS2 */
  2755. #ifdef NO_FTP_AUTH
  2756.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2757. #endif /* NO_FTP_AUTH */
  2758.                  ck_krb5_is_installed() && !krb5_msg) {
  2759.  
  2760.                 /* Without encryption we can't perform mutual authentication */
  2761.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2762.                      !ck_crypt_is_installed())
  2763.                     continue;
  2764.  
  2765.                 /* Skip over entries that request credential forwarding */
  2766.                 /* if we are not forwarding.                            */
  2767.                 if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2768.                      (forward_flag &&
  2769.                        ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2770.                     continue;
  2771.  
  2772.                 if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2773.                                     parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2774.                                     parsedat[i+1] & INI_CRED_FWD_MASK) )
  2775.                 {
  2776.                     /* If we are auto-getting TGTs, try */
  2777.                     if ( !ck_krb5_is_tgt_valid() ) {
  2778.                         printf(
  2779.                            "Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2780.                     }
  2781.                     krb5_msg = 1;
  2782.                 }
  2783.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2784.                           AUTH_CLIENT_TO_SERVER &&
  2785.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2786.                 {
  2787.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2788.                          AUTH_ENCRYPT_USING_TELOPT) &&
  2789.                          (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2790.                            TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2791.                         continue;
  2792.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2793.                          AUTH_ENCRYPT_START_TLS) &&
  2794.                          (!ck_ssleay_is_installed()
  2795. #ifdef CK_SSL
  2796.                            || !(tls_active_flag || ssl_active_flag)
  2797. #endif /* CK_SSL */
  2798.                            ))
  2799.                         continue;
  2800.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2801. #ifdef CK_ENCRYPTION
  2802.                     if (auth_crypt) {
  2803.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2804.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2805.                     }
  2806. #endif /* CK_ENCRYPTION */
  2807. #ifdef CK_SSL
  2808.                     if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2809.                          ck_ssleay_is_installed() &&
  2810.                          (tls_active_flag || ssl_active_flag) ) {
  2811.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2812.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2813.                     }
  2814. #endif /* CK_SSL */
  2815.                     authentication_version = AUTHTYPE_KERBEROS_V5;
  2816.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2817.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2818.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2819.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2820.                     }
  2821.                     break;
  2822.                 }
  2823.             }
  2824. #endif /* KRB5 */
  2825. #ifdef KRB4
  2826.             if ( atok(AUTHTYPE_KERBEROS_V4) &&
  2827.                  parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2828. #ifdef OS2
  2829.                  hKRB4_32 &&
  2830. #endif /* OS2 */
  2831. #ifdef NO_FTP_AUTH
  2832.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2833. #endif /* NO_FTP_AUTH */
  2834.                  ck_krb4_is_installed() && !krb4_msg) {
  2835.                 int rc = 0;
  2836.  
  2837.                 /* Without encryption we can't perform mutual authentication */
  2838.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2839.                      !ck_crypt_is_installed())
  2840.                     continue;
  2841.  
  2842.                 if ( !k4_auth_send() )
  2843.                 {
  2844.                     /* If we are auto-getting TGTs, try */
  2845.                     if ( !ck_krb4_is_tgt_valid() ) {
  2846.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2847.                     }
  2848.                     krb4_msg = 1;
  2849.                 }
  2850.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2851.                           AUTH_CLIENT_TO_SERVER &&
  2852.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2853.                 {
  2854. #ifdef CK_ENCRYPTION
  2855.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2856.                         (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2857.                          TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2858.                       continue;
  2859.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2860.                     if (auth_crypt) {
  2861.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2862.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2863.                     }
  2864. #endif /* CK_ENCRYPTION */
  2865.                     authentication_version = AUTHTYPE_KERBEROS_V4;
  2866.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2867.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2868.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2869.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2870.                     }
  2871.                     break;
  2872.                 }
  2873.             }
  2874. #endif /* KRB4 */
  2875. #ifdef NTLM
  2876.         if ( atok(AUTHTYPE_NTLM) &&
  2877.              parsedat[i] == AUTHTYPE_NTLM &&
  2878.              ck_ntlm_is_valid(1) &&
  2879.              ntlm_auth_send() == 0) {
  2880.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2881.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2882. #ifdef CK_ENCRYPTION
  2883.                 /* NTLM does not support Telnet Encryption */
  2884.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2885.                     continue;
  2886.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2887. #endif /* CK_ENCRYPTION */
  2888.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2889.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2890.                 authentication_version = AUTHTYPE_NTLM;
  2891.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2892.                 break;
  2893.             }
  2894.         }
  2895. #endif /* NTLM */
  2896.         }
  2897.     }
  2898.  
  2899.     if (auth_how == -1) {               /* Did we find one? */
  2900.         switch ( auth_type_user[0] ) {  /* If not, abort the negotiation */
  2901.         case AUTHTYPE_NULL:
  2902.             auth_abort("User refused to accept any authentication method",0);
  2903.             break;
  2904.         case AUTHTYPE_AUTO:
  2905.             auth_abort("No authentication method available", 0);
  2906.             break;
  2907.         default: {
  2908.             char msg[80];
  2909.             ckmakmsg(msg,80,AUTHTYPE_NAME(auth_type_user[0]),
  2910.                       " could not be negotiated",NULL,NULL
  2911.                      );
  2912.             auth_abort(msg, 0);
  2913.         }
  2914.         }
  2915.         auth_finished(AUTH_REJECT);
  2916.         return AUTH_FAILURE;
  2917.     }
  2918.  
  2919.     printf("Authenticating with %s\r\n",
  2920.             AUTHTYPE_NAME(authentication_version));
  2921.  
  2922.     /* Send Telnet Auth Name message (if necessary) */
  2923.     switch ( authentication_version ) {
  2924.     case AUTHTYPE_SRP:
  2925.     case AUTHTYPE_KERBEROS_V4:
  2926.     case AUTHTYPE_KERBEROS_V5:
  2927.     case AUTHTYPE_GSSAPI_KRB5:
  2928.         /* if we do not have a name to login with get one now. */
  2929.         while ( szUserName[0] == '\0' ) {
  2930.             extern char * tn_pr_uid;
  2931.             int ok = uq_txt(NULL,
  2932.                      tn_pr_uid && tn_pr_uid[0] ? tn_pr_uid : "Host Userid: ",
  2933.                             1, NULL, szUserName, 63, NULL,DEFAULT_UQ_TIMEOUT);
  2934.             if ( !ok )
  2935.                 return AUTH_FAILURE;
  2936.         }
  2937.         plen = strlen(szUserName);
  2938.         pname = (unsigned char *) szUserName;
  2939.  
  2940.         /* Construct Telnet Debugging Message */
  2941.         if (deblog || tn_deb || debses) {
  2942.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2943.                        "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2944.                        " NAME ",(char *)pname," IAC SE",NULL,
  2945.                        NULL,NULL,NULL,NULL,NULL,NULL
  2946.                      );
  2947.             debug(F100,tn_msg,"",0);
  2948.             if (tn_deb || debses) tn_debug(tn_msg);
  2949.         }
  2950.  
  2951.         /* Construct and send Authentication Name subnegotiation */
  2952.         if ( plen < sizeof(buf) - 6 ) {
  2953.             sprintf((char *)buf, "%c%c%c%c", IAC, SB, 
  2954.                      TELOPT_AUTHENTICATION,
  2955.                      TELQUAL_NAME);
  2956.             memcpy(&buf[4], pname, plen);               /* safe */
  2957.             sprintf((char *)&buf[plen + 4], "%c%c", IAC, SE);   /* safe */
  2958. #ifdef OS2
  2959.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2960. #endif
  2961.             ttol((CHAR *)buf, plen+6);
  2962. #ifdef OS2
  2963.             ReleaseTelnetMutex();
  2964. #endif
  2965.         } else {
  2966.             sprintf((char *)buf, "%c%c%c%c%c%c", IAC, SB, 
  2967.                      TELOPT_AUTHENTICATION,
  2968.                      TELQUAL_NAME, IAC, SE);    /* safe */
  2969. #ifdef OS2
  2970.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2971. #endif
  2972.             ttol((CHAR *)buf, 6);
  2973. #ifdef OS2
  2974.             ReleaseTelnetMutex();
  2975. #endif
  2976.         }
  2977.     }
  2978.  
  2979.     /* Construct Authentication Mode subnegotiation message (if necessary) */
  2980.     switch ( authentication_version ) {
  2981.     case AUTHTYPE_SRP:
  2982.     case AUTHTYPE_KERBEROS_V4:
  2983.     case AUTHTYPE_KERBEROS_V5:
  2984.     case AUTHTYPE_GSSAPI_KRB5:
  2985.     case AUTHTYPE_NTLM:
  2986.         mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) | auth_crypt
  2987. #ifdef USE_INI_CRED_FWD
  2988.                | (((authentication_version == AUTHTYPE_KERBEROS_V5) &&
  2989.                   auth_fwd)?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  2990. #endif /* USE_INI_CRED_FWD */
  2991.                ;
  2992.         sprintf((char *)buf, "%c%c%c%c%c%c%c",
  2993.                  IAC, SB, TELOPT_AUTHENTICATION,
  2994.                  TELQUAL_IS,
  2995.                  authentication_version,
  2996.                  mode,
  2997.                  KRB_AUTH);     /* safe */
  2998.         break;
  2999.     }
  3000.  
  3001.     /* Send initial authentication data */
  3002.     switch ( authentication_version ) {
  3003. #ifdef CK_SSL
  3004.     case AUTHTYPE_SSL:
  3005.         SendSSLAuthSB(SSL_START,NULL,0);
  3006.         break;
  3007. #endif /* SSL */
  3008. #ifdef CK_SRP
  3009.     case AUTHTYPE_SRP:
  3010.         sprintf(&buf[7], "%c%c", IAC, SE);      /* safe */
  3011.         if (deblog || tn_deb || debses) {
  3012.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3013.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3014.                       " IS ",AUTHTYPE_NAME(authentication_version),
  3015.                       " AUTH ",AUTHMODE_NAME(mode)," IAC SE",
  3016.                       NULL,NULL,NULL,NULL,NULL
  3017.                      );
  3018.             debug(F100,tn_msg,"",0);
  3019.             if (tn_deb || debses) tn_debug(tn_msg);
  3020.         }
  3021. #ifdef OS2
  3022.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3023. #endif
  3024.         ttol((CHAR *)buf, 9);
  3025. #ifdef OS2
  3026.         ReleaseTelnetMutex();
  3027. #endif
  3028.         break;
  3029. #endif /* SRP */
  3030. #ifdef NTLM
  3031.     case AUTHTYPE_NTLM: {
  3032.         int length = 0;
  3033.  
  3034.         for ( i=0 ; i<NTLMSecBuf[0].cbBuffer ; i++ ) {
  3035.             if ( ((char *)NTLMSecBuf[0].pvBuffer)[i] == IAC )
  3036.                 iaccnt++;
  3037.         }
  3038.  
  3039.         if ( ( 2*sizeof(ULONG) + NTLMSecBuf[0].cbBuffer + iaccnt + 10)  <
  3040.              sizeof(buf) ) {
  3041.             length = copy_for_net(&buf[7],(char *)&NTLMSecBuf[0],
  3042.                                    2*sizeof(ULONG));
  3043.             length += copy_for_net(&buf[7+length], NTLMSecBuf[0].pvBuffer,
  3044.                                   NTLMSecBuf[0].cbBuffer);
  3045.         }
  3046.         sprintf(&buf[7+length], "%c%c", IAC, SE);
  3047.  
  3048.         if (deblog || tn_deb || debses) {
  3049.             int i;
  3050.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3051.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3052.                       " IS ",AUTHTYPE_NAME(authentication_version)," ",
  3053.                       AUTHMODE_NAME(mode)," NTLM_AUTH ",
  3054.                        NULL,NULL,NULL,NULL,NULL
  3055.                       );
  3056.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],length);
  3057.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3058.             debug(F100,tn_msg,"",0);
  3059.             if (tn_deb || debses) tn_debug(tn_msg);
  3060.         }
  3061. #ifdef OS2
  3062.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3063. #endif
  3064.         ttol((CHAR *)buf, length+9);
  3065. #ifdef OS2
  3066.         ReleaseTelnetMutex();
  3067. #endif
  3068.         break;
  3069.     }
  3070. #endif /* NTLM */
  3071. #ifdef KRB4
  3072.     case AUTHTYPE_KERBEROS_V4:
  3073.         for ( i=0 ; i<k4_auth.length ; i++ ) {
  3074.             if ( k4_auth.dat[i] == IAC )
  3075.                 iaccnt++;
  3076.         }
  3077.  
  3078.         if ( k4_auth.length + iaccnt + 10 < sizeof(buf) )
  3079.           k4_auth.length = copy_for_net(&buf[7], k4_auth.dat, k4_auth.length);
  3080.         else
  3081.             k4_auth.length = 0;
  3082.         sprintf(&buf[k4_auth.length+7], "%c%c", IAC, SE);
  3083.  
  3084.         if (deblog || tn_deb || debses) {
  3085.             int i;
  3086.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3087.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3088.                       AUTHTYPE_NAME(authentication_version)," ",
  3089.                       AUTHMODE_NAME(mode)," AUTH ",
  3090.                       NULL,NULL,NULL,NULL,NULL
  3091.                      );
  3092.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],k4_auth.length);
  3093.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3094.             debug(F100,tn_msg,"",0);
  3095.             if (tn_deb || debses) tn_debug(tn_msg);
  3096.         }
  3097. #ifdef OS2
  3098.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3099. #endif
  3100.         ttol((CHAR *)buf, k4_auth.length+9);
  3101. #ifdef OS2
  3102.         ReleaseTelnetMutex();
  3103. #endif
  3104.  
  3105. #ifndef REMOVE_FOR_EXPORT
  3106. #ifdef CK_ENCRYPTION
  3107.         /*
  3108.          * If we are doing mutual authentication, get set up to send
  3109.          * the challenge, and verify it when the response comes back.
  3110.          */
  3111.         if ((auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  3112.             register int i;
  3113.             int rc = 0;
  3114.  
  3115. #ifdef MIT_CURRENT
  3116.             data.data = cred.session;
  3117.             data.length = 8; /* sizeof(cred.session) */;
  3118.  
  3119.             if (code = krb5_c_random_seed(k5_context, &data)) {
  3120.                 com_err("libtelnet", code,
  3121.                          "while seeding random number generator");
  3122.                 return(0);
  3123.             }
  3124.  
  3125.             if (code = krb5_c_make_random_key(k5_context,
  3126.                                                ENCTYPE_DES_CBC_RAW,
  3127.                                                &random_key)) {
  3128.                 com_err("libtelnet", code,
  3129.                          "while creating random session key");
  3130.                 return(0);
  3131.             }
  3132.  
  3133.             /* the krb4 code uses ecb mode, but on a single block
  3134.             with a zero ivec, ecb and cbc are the same */
  3135.             k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  3136.             k4_krbkey.length = 8;
  3137.             k4_krbkey.contents = cred.session;
  3138.  
  3139.             encdata.ciphertext.data = random_key.contents;
  3140.             encdata.ciphertext.length = random_key.length;
  3141.             encdata.enctype = ENCTYPE_UNKNOWN;
  3142.  
  3143.             data.data = k4_session_key;
  3144.             data.length = 8;
  3145.  
  3146.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3147.                                    &encdata, &data);
  3148.  
  3149.             krb5_free_keyblock_contents(k5_context, &random_key);
  3150.  
  3151.             if (code) {
  3152.                 com_err("libtelnet", code, "while encrypting random key");
  3153.                 return(0);
  3154.             }
  3155.  
  3156.             encdata.ciphertext.data = k4_session_key;
  3157.             encdata.ciphertext.length = 8;
  3158.             encdata.enctype = ENCTYPE_UNKNOWN;
  3159.  
  3160.             data.data = k4_challenge;
  3161.             data.length = 8;
  3162.  
  3163.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3164.                                    &encdata, &data);
  3165. #else /* MIT_CURRENT */
  3166.             memset(k4_sched,0,sizeof(Schedule));
  3167.             hexdump("auth_send",cred.session,8);
  3168.             rc = des_key_sched(cred.session, k4_sched);
  3169.             if ( rc == -1 ) {
  3170.                 printf("?Invalid DES key specified in credentials\r\n");
  3171.                 debug(F110,"auth_send",
  3172.                       "invalid DES Key specified in credentials",0);
  3173.             } else if ( rc == -2 ) {
  3174.                 printf("?Weak DES key specified in credentials\r\n");
  3175.                 debug(F110,"auth_send",
  3176.                       "weak DES Key specified in credentials",0);
  3177.             } else if ( rc != 0 ) {
  3178.                 printf("?DES Key Schedule not set by credentials\r\n");
  3179.                 debug(F110,"auth_send",
  3180.                       "DES Key Schedule not set by credentials",0);
  3181.             }
  3182.             hexdump("auth_send schedule",k4_sched,8*16);
  3183.  
  3184.             des_set_random_generator_seed(cred.session);
  3185.  
  3186.             do {
  3187.                 des_new_random_key(k4_session_key);
  3188.                 des_fixup_key_parity(k4_session_key);
  3189.             } while ( ck_des_is_weak_key(k4_session_key) );
  3190.  
  3191.             hexdump("auth_send des_new_random_key(k4_session_key)",
  3192.                      k4_session_key,8);
  3193.  
  3194.             /* Decrypt the session key so that we can send it to the */
  3195.             /* host as a challenge                                   */
  3196. #ifdef NT
  3197.             des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 0);
  3198. #else /* NT */
  3199.             des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 0);
  3200. #endif /* NT */
  3201.             hexdump(
  3202.                 "auth_send des_ecb_encrypt(k4_session_key,k4_session_key,0)",
  3203.                 k4_session_key,8
  3204.                     );
  3205.             /* Prepare the result of the challenge */
  3206.             /* Decrypt the session_key, add 1, and then encrypt it */
  3207.             /* The result stored in k4_challenge should match the  */
  3208.             /* KRB4_RESPONSE value from the host.                  */
  3209. #ifdef NT
  3210.             des_ecb_encrypt(k4_session_key, k4_challenge, k4_sched, 0);
  3211. #else /* NT */
  3212.             des_ecb_encrypt(&k4_session_key, &k4_challenge, k4_sched, 0);
  3213. #endif /* NT */
  3214.  
  3215.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,0)",
  3216.                      k4_challenge,8);
  3217. #endif /* MIT_CURRENT */
  3218.             /*
  3219.             * Increment the challenge by 1, and encrypt it for
  3220.             * later comparison.
  3221.             */
  3222.             for (i = 7; i >= 0; --i) {
  3223.                 register int x;
  3224.                 x = (unsigned int)k4_challenge[i] + 1;
  3225.                 k4_challenge[i] = x;    /* ignore overflow */
  3226.                 if (x < 256)            /* if no overflow, all done */
  3227.                     break;
  3228.             }
  3229.             hexdump("auth_send k4_challenge+1",k4_challenge,8);
  3230. #ifdef MIT_CURRENT
  3231.             data.data = k4_challenge;
  3232.             data.length = 8;
  3233.  
  3234.             encdata.ciphertext.data = k4_challenge;
  3235.             encdata.ciphertext.length = 8;
  3236.             encdata.enctype = ENCTYPE_UNKNOWN;
  3237.  
  3238.             if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0, &data,
  3239.                                        &encdata)) {
  3240.                 com_err("libtelnet", code, "while encrypting random key");
  3241.                 return(0);
  3242.             }
  3243. #else /* MIT_CURRENT */
  3244. #ifdef NT
  3245.             des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  3246. #else /* NT */
  3247.             des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  3248. #endif /* NT */
  3249.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,1)",
  3250.                      k4_challenge,8);
  3251. #endif /* MIT_CURRENT */
  3252.         }
  3253. #endif  /* ENCRYPTION */
  3254. #endif /* REMOVE_FOR_EXPORT */
  3255.         break;
  3256. #endif /* KRB4 */
  3257. #ifdef GSSAPI_KRB5
  3258.     case AUTHTYPE_GSSAPI_KRB5:
  3259.         for ( i=0 ; i<gss_send_tok.length ; i++ ) {
  3260.             if ( ((char *)gss_send_tok.value)[i] == IAC )
  3261.                 iaccnt++;
  3262.         }
  3263.  
  3264.         if ( gss_send_tok.length + iaccnt + 10 < sizeof(buf) )
  3265.             gss_send_tok.length = copy_for_net(&buf[7], gss_send_tok.value,
  3266.                                                gss_send_tok.length);
  3267.         else
  3268.             gss_send_tok.length = 0;
  3269.         sprintf(&buf[gss_send_tok.length+7], "%c%c", IAC, SE);       /* safe */
  3270.         if (deblog || tn_deb || debses) {
  3271.             int i;
  3272.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3273.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3274.                       AUTHTYPE_NAME(authentication_version)," ",
  3275.                       AUTHMODE_NAME(mode)," AUTH ",
  3276.                       NULL,NULL,NULL,NULL,NULL
  3277.                      );
  3278.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],gss_send_tok.length);
  3279.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3280.             debug(F100,tn_msg,"",0);
  3281.             if (tn_deb || debses) tn_debug(tn_msg);
  3282.         }
  3283. #ifdef OS2
  3284.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3285. #endif
  3286.         ttol((CHAR *)buf, gss_send_tok.length+9);
  3287. #ifdef OS2
  3288.         ReleaseTelnetMutex();
  3289. #endif
  3290.         break;
  3291. #endif /* GSSAPI_KRB5 */
  3292. #ifdef KRB5
  3293.     case AUTHTYPE_KERBEROS_V5:
  3294.         debug(F111,"auth_send KRB5","k5_auth.length",k5_auth.length);
  3295.         for ( i=0 ; i<k5_auth.length ; i++ ) {
  3296.             if ( (char *)k5_auth.data[i] == IAC )
  3297.                 iaccnt++;
  3298.         }
  3299.         if ( k5_auth.length + iaccnt + 10 < sizeof(buf) ) {
  3300.         k5_auth.length = copy_for_net(&buf[7],
  3301.                       (CHAR *)k5_auth.data,
  3302.                       k5_auth.length);
  3303.     } else {
  3304.           debug(F100,"auth_send() KRB5 auth data too large for buffer","",0);
  3305.           k5_auth.length = 0;
  3306.         }
  3307.  
  3308.         sprintf((char *)&buf[k5_auth.length+7], "%c%c", IAC, SE); /* safe */
  3309.         if (deblog || tn_deb || debses) {
  3310.             int i;
  3311.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3312.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3313.                       AUTHTYPE_NAME(authentication_version)," ",
  3314.                       AUTHMODE_NAME(mode)," AUTH ",
  3315.                       NULL,NULL,NULL,NULL,NULL
  3316.                      );
  3317.             tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&buf[7],k5_auth.length);
  3318.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3319.             debug(F100,tn_msg,"",0);
  3320.             if (tn_deb || debses) tn_debug(tn_msg);
  3321.         }
  3322. #ifdef OS2
  3323.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3324. #endif
  3325.         ttol((CHAR *)buf, k5_auth.length+9);
  3326. #ifdef OS2
  3327.         ReleaseTelnetMutex();
  3328. #endif
  3329. #ifdef HEIMDAL
  3330.         krb5_data_free(&k5_auth);
  3331. #else /* HEIMDAL */
  3332.         krb5_free_data_contents(k5_context,&k5_auth);
  3333.         memset(&k5_auth,0,sizeof(krb5_data));
  3334. #endif /* HEIMDAL */
  3335.         break;
  3336. #endif /* KRB5 */
  3337.     }
  3338.     return AUTH_SUCCESS;
  3339. }
  3340.  
  3341. /*
  3342.  * Function: Parse authentication REPLY command
  3343.  *
  3344.  * Parameters:
  3345.  *  parsedat - the sub-command data.
  3346.  *
  3347.  *      end_sub - index of the character in the 'parsedat' array which
  3348.  *              is the last byte in a sub-negotiation
  3349.  *
  3350.  * Returns: Kerberos error code.
  3351.  */
  3352. static int
  3353. #ifdef CK_ANSIC
  3354. auth_reply(unsigned char *parsedat, int end_sub)
  3355. #else
  3356. auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3357. #endif
  3358. {
  3359.     int n = AUTH_FAILURE;
  3360.  
  3361.     if ( parsedat[2] != authentication_version ) {
  3362.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3363.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3364.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3365.         auth_finished(AUTH_REJECT);
  3366.         return(AUTH_FAILURE);
  3367.     }
  3368.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3369.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3370.                 AUTHMODE_NAME(parsedat[3]),
  3371.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3372.         auth_finished(AUTH_REJECT);
  3373.         return(AUTH_FAILURE);
  3374.     }
  3375.  
  3376. #ifdef KRB4
  3377.     if (authentication_version == AUTHTYPE_KERBEROS_V4)
  3378.         n = k4_auth_reply(parsedat, end_sub);
  3379. #endif
  3380. #ifdef KRB5
  3381.     if (authentication_version == AUTHTYPE_KERBEROS_V5)
  3382.         n = k5_auth_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3383. #endif
  3384. #ifdef CK_SRP
  3385.     if (authentication_version == AUTHTYPE_SRP) {
  3386. #ifndef PRE_SRP_1_7_3
  3387.         n = new_srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3388. #else /* PRE_SRP_1_7_3 */
  3389.         n = srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3390. #endif /* PRE_SRP_1_7_3 */
  3391.     }
  3392. #endif /* SRP */
  3393. #ifdef CK_SSL
  3394.     if (authentication_version == AUTHTYPE_SSL)
  3395.         n = ssl_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3396. #endif /* SSL */
  3397. #ifdef NTLM
  3398.     if (authentication_version == AUTHTYPE_NTLM)
  3399.         n = ntlm_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3400. #endif /* NTLM */
  3401.     return n;
  3402. }
  3403.  
  3404.  
  3405. /*
  3406.  * Function: Parse authentication IS command
  3407.  *
  3408.  * Parameters:
  3409.  *  parsedat - the sub-command data.
  3410.  *
  3411.  *      end_sub - index of the character in the 'parsedat' array which
  3412.  *              is the last byte in a sub-negotiation
  3413.  *
  3414.  * Returns: Kerberos error code.
  3415.  */
  3416. static int
  3417. #ifdef CK_ANSIC
  3418. auth_is(unsigned char *parsedat, int end_sub)
  3419. #else
  3420. auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3421. #endif
  3422. {
  3423.     int n = AUTH_FAILURE;
  3424.  
  3425.     if ( parsedat[2] == AUTHTYPE_NULL ) {
  3426.         auth_finished(AUTH_REJECT);
  3427.         return(AUTH_FAILURE);
  3428.     }
  3429.  
  3430.     /*
  3431.      *  If CLIENT_CHOOSE_ONCE is selected the server will not allow the
  3432.      *  client to switch to an alternate authentication method if the one
  3433.      *  it originally selected fails.  (ie, if the host's SRP parameters
  3434.      *  are invalid.)  However, I think this is a bit of a security risk
  3435.      *  since allowing that functionality means that it is impossible to
  3436.      *  detect if an attack is being carried out on
  3437.      */
  3438. #define CLIENT_CHOOSE_ONCE
  3439. #ifdef CLIENT_CHOOSE_ONCE
  3440.     if ( authentication_version == AUTHTYPE_AUTO )
  3441. #endif /* CLIENT_CHOOSE_ONCE */
  3442.     {
  3443.         /* this block of code needs to check the initial parameters */
  3444.         /* to ensure that those returned match one of the sets that */
  3445.         /* were sent to the client in the first place.              */
  3446.  
  3447.         int i=0;
  3448.         for ( i=4; str_request[i] != IAC ; i+=2) {
  3449.             if (str_request[i] == parsedat[2] &&
  3450.                  str_request[i+1] == parsedat[3])
  3451.                 break;
  3452.         }
  3453.  
  3454.         if ( str_request[i] == IAC ) {
  3455.             printf("Invalid authentication type pair (%s,%s)\r\n",
  3456.                     AUTHTYPE_NAME(parsedat[2]),
  3457.                     AUTHMODE_NAME(parsedat[3]));
  3458.             auth_finished(AUTH_REJECT);
  3459.             return(AUTH_FAILURE);
  3460.         }
  3461.  
  3462.         if (authentication_version != parsedat[2]) {
  3463.             authentication_version = parsedat[2];
  3464.             auth_how = (parsedat[3] & AUTH_HOW_MASK);
  3465.             auth_crypt = (parsedat[3] & AUTH_ENCRYPT_MASK);
  3466.             auth_fwd = (parsedat[3] & INI_CRED_FWD_MASK);
  3467.             debug(F111,"auth_is","authentication_version",
  3468.                   authentication_version);
  3469.             debug(F111,"auth_is","auth_how",auth_how);
  3470.             debug(F111,"auth_is","auth_crypt",auth_crypt);
  3471.             debug(F111,"auth_is","auth_fwd",auth_fwd);
  3472.         }
  3473.     }
  3474.  
  3475. #ifdef CLIENT_CHOOSE_ONCE
  3476.     if ( parsedat[2] != authentication_version ) {
  3477.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3478.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3479.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3480.         auth_finished(AUTH_REJECT);
  3481.         return(AUTH_FAILURE);
  3482.     }
  3483.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3484.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3485.                 AUTHMODE_NAME(parsedat[3]),
  3486.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3487.         auth_finished(AUTH_REJECT);
  3488.         return(AUTH_FAILURE);
  3489.     }
  3490. #endif /* CLIENT_CHOOSE_ONCE */
  3491.  
  3492.     switch (authentication_version) {
  3493. #ifdef KRB4
  3494.     case AUTHTYPE_KERBEROS_V4:
  3495.         n = k4_auth_is(parsedat, end_sub);
  3496.         break;
  3497. #endif
  3498. #ifdef KRB5
  3499.     case AUTHTYPE_KERBEROS_V5:
  3500.         n = k5_auth_is(parsedat[3],parsedat, end_sub);
  3501.         break;
  3502. #endif
  3503. #ifdef CK_SRP
  3504.     case AUTHTYPE_SRP:
  3505. #ifndef PRE_SRP_1_7_3
  3506.         n = new_srp_is(parsedat[3], parsedat, end_sub);
  3507. #else /* PRE_SRP_1_7_3 */
  3508.         n = srp_is(parsedat[3], parsedat, end_sub);
  3509. #endif /* PRE_SRP_1_7_3 */
  3510.         break;
  3511. #endif /* SRP */
  3512. #ifdef CK_SSL
  3513.     case AUTHTYPE_SSL:
  3514.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3515.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3516.         n = ssl_is(parsedat, end_sub);
  3517.         break;
  3518. #endif /* SSL */
  3519. #ifdef NTLM
  3520.     case AUTHTYPE_NTLM:
  3521.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3522.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3523.         n = ntlm_is(parsedat, end_sub);
  3524.         break;
  3525. #endif /* NTLM */
  3526.     case AUTHTYPE_NULL:
  3527.     default:
  3528.         n = AUTH_FAILURE;
  3529.     }
  3530.     debug(F111,"auth_is","n",n);
  3531.     return n;
  3532. }
  3533.  
  3534. /*
  3535.  * Function: Parse authentication NAME command
  3536.  *
  3537.  * Parameters:
  3538.  *  parsedat - the sub-command data.
  3539.  *
  3540.  *      end_sub - index of the character in the 'parsedat' array which
  3541.  *              is the last byte in a sub-negotiation
  3542.  *
  3543.  * Returns: Kerberos error code.
  3544.  */
  3545. static int
  3546. #ifdef CK_ANSIC
  3547. auth_name(unsigned char *parsedat, int end_sub)
  3548. #else
  3549. auth_name(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3550. #endif
  3551. {
  3552.     int len = (end_sub-2) > 63 ? 63 : (end_sub-2);
  3553.     if ( len > 0 && (len + 1) < sizeof(szUserNameRequested)) {
  3554.         memcpy(szUserNameRequested,&parsedat[2],len);           /* safe */
  3555.         szUserNameRequested[len] = '\0';
  3556.     } else
  3557.       szUserNameRequested[0] = '\0';
  3558.     debug(F111,"auth_name szUserNameRequested",szUserNameRequested,len);
  3559.     return(AUTH_SUCCESS);
  3560. }
  3561.  
  3562. /*
  3563.  * Function: Parse the athorization sub-options and reply.
  3564.  *
  3565.  * Parameters:
  3566.  *      parsedat - sub-option string to parse.
  3567.  *
  3568.  *      end_sub - last charcter position in parsedat.
  3569.  */
  3570. int
  3571. auth_parse(unsigned char *parsedat, int end_sub)
  3572. {
  3573.     int rc = AUTH_FAILURE;
  3574.     switch (parsedat[1]) {
  3575.     case TELQUAL_SEND:
  3576.         rc = auth_send(parsedat, end_sub);
  3577.         break;
  3578.     case TELQUAL_REPLY:
  3579.         rc= auth_reply(parsedat, end_sub);
  3580.         break;
  3581.     case TELQUAL_IS:
  3582.         rc = auth_is(parsedat, end_sub);
  3583.         break;
  3584.     case TELQUAL_NAME:
  3585.         rc = auth_name(parsedat, end_sub);
  3586.         break;
  3587.     }
  3588.     debug(F111,"auth_parse","rc",rc);
  3589.     return(rc);
  3590. }
  3591.  
  3592.  
  3593. /*
  3594.  * Function: Initialization routine called kstream encryption system.
  3595.  *
  3596.  * Parameters:
  3597.  *  data - user data.
  3598.  */
  3599. int
  3600. #ifdef CK_ANSIC
  3601. auth_init(kstream ks)
  3602. #else
  3603. auth_init(ks) kstream_ptr ks;
  3604. #endif
  3605. {
  3606. #ifdef FORWARD
  3607.     forwarded_tickets = 0;  /* were tickets forwarded? */
  3608. #endif /* FORWARD */
  3609. #ifdef CK_ENCRYPTION
  3610.     encrypt_init(ks,cx_type);
  3611. #endif
  3612.     return 0;
  3613. }
  3614.  
  3615.  
  3616. /*
  3617.  * Function: Destroy routine called kstream encryption system.
  3618.  *
  3619.  * Parameters:
  3620.  *  data - user data.
  3621.  */
  3622. VOID
  3623. #ifdef CK_ANSIC
  3624. auth_destroy(void)
  3625. #else
  3626. auth_destroy()
  3627. #endif
  3628. {
  3629. }
  3630.  
  3631.  
  3632. /*
  3633.  * Function: Callback to encrypt a block of characters
  3634.  *
  3635.  * Parameters:
  3636.  *  out - return as pointer to converted buffer.
  3637.  *
  3638.  *  in - the buffer to convert
  3639.  *
  3640.  * Returns: number of characters converted.
  3641.  */
  3642. int
  3643. #ifdef CK_ANSIC
  3644. auth_encrypt(struct kstream_data_block *out,
  3645.              struct kstream_data_block *in)
  3646. #else
  3647. auth_encrypt(out,in)
  3648.     struct kstream_data_block *out; struct kstream_data_block *in;
  3649. #endif
  3650. {
  3651.     out->ptr = in->ptr;
  3652.  
  3653.     out->length = in->length;
  3654.  
  3655.     return(out->length);
  3656. }
  3657.  
  3658.  
  3659. /*
  3660.  * Function: Callback to decrypt a block of characters
  3661.  *
  3662.  * Parameters:
  3663.  *  out - return as pointer to converted buffer.
  3664.  *
  3665.  *  in - the buffer to convert
  3666.  *
  3667.  * Returns: number of characters converted.
  3668.  */
  3669. int
  3670. #ifdef CK_ANSIC
  3671. auth_decrypt(struct kstream_data_block *out,
  3672.              struct kstream_data_block *in)
  3673. #else
  3674. auth_decrypt(out,in)
  3675.     struct kstream_data_block *out; struct kstream_data_block *in;
  3676. #endif
  3677. {
  3678.     out->ptr = in->ptr;
  3679.  
  3680.     out->length = in->length;
  3681.  
  3682.     return(out->length);
  3683. }
  3684.  
  3685. #ifdef KRB4
  3686. #ifdef NT
  3687. void
  3688. ck_krb4_debug(int x)
  3689. {
  3690.     set_krb_debug(x);
  3691.     set_krb_ap_req_debug(x);
  3692. }
  3693. #endif /* NT */
  3694. int
  3695. ck_krb4_autoget_TGT(char * realm)
  3696. {
  3697.     extern struct krb_op_data krb_op;
  3698.     extern struct krb4_init_data krb4_init;
  3699.     char passwd[PWD_SZ];
  3700.     char prompt[256];
  3701.     char * saverealm=NULL;
  3702.     int  rc = -1;
  3703.     extern char * k4prprompt;
  3704.     extern char * k4pwprompt;
  3705.  
  3706.     ini_kerb();         /* Place defaults in above structs */
  3707.     passwd[0] = '\0';
  3708.  
  3709.     if ( krb4_init.principal == NULL ||
  3710.          krb4_init.principal[0] == '\0') {
  3711.         int ok = uq_txt(NULL, 
  3712.                  k4prprompt && k4prprompt[0] ?
  3713.                  k4prprompt :
  3714.                  "Kerberos 4 Principal: ",
  3715.                  2, NULL, passwd,PWD_SZ-1, NULL, DEFAULT_UQ_TIMEOUT);
  3716.         if ( ok && passwd[0] )
  3717.             makestr(&krb4_init.principal,passwd);
  3718.         else
  3719.             return(0);
  3720.     }
  3721.  
  3722.     /* Save realm in init structure so it can be restored */
  3723.     if ( realm ) {
  3724.         saverealm = krb4_init.realm;
  3725.         krb4_init.realm = realm;
  3726.     }
  3727.  
  3728.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  3729.         int ok;
  3730.         if ( k4pwprompt && k4pwprompt[0] &&
  3731.              (strlen(k4pwprompt) + strlen(krb4_init.principal) +
  3732.                strlen(krb4_init.realm) - 4) < sizeof(prompt)) {
  3733.             sprintf(prompt,k4pwprompt,krb4_init.principal,krb4_init.realm);
  3734.         } else
  3735.             ckmakxmsg(prompt,sizeof(prompt),
  3736.                   "Kerberos 4 Password for ",krb4_init.principal,"@",
  3737.                   krb4_init.realm,": ",
  3738.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3739.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL,
  3740.             DEFAULT_UQ_TIMEOUT);
  3741.         if ( !ok )
  3742.             passwd[0] = '\0';
  3743.     } else {
  3744.         ckstrncpy(passwd,pwbuf,sizeof(passwd));
  3745. #ifdef OS2
  3746.         if ( pwcrypt )
  3747.             ck_encrypt((char *)passwd);
  3748. #endif /* OS2 */
  3749.     }
  3750.  
  3751.     if ( passwd[0] ) {
  3752.         makestr(&krb4_init.password,passwd);
  3753.         rc = ck_krb4_initTGT(&krb_op, &krb4_init);
  3754.         free(krb4_init.password);
  3755.         krb4_init.password = NULL;
  3756.     }
  3757.  
  3758.     krb4_init.password = NULL;
  3759.     memset(passwd,0,PWD_SZ);
  3760.  
  3761.     /* restore realm to init structure if needed */
  3762.     if ( saverealm )
  3763.         krb4_init.realm = saverealm;
  3764.     return(rc == 0);
  3765. }
  3766.  
  3767. char *
  3768. ck_krb4_realmofhost(char *host)
  3769. {
  3770.     return (char *)krb_realmofhost(host);
  3771. }
  3772.  
  3773. /*
  3774.  *
  3775.  * K4_auth_send - gets authentication bits we need to send to KDC.
  3776.  *
  3777.  * Result is left in auth
  3778.  *
  3779.  * Returns: 0 on failure, 1 on success
  3780.  */
  3781. static int
  3782. #ifdef CK_ANSIC
  3783. k4_auth_send(void)
  3784. #else
  3785. k4_auth_send()
  3786. #endif
  3787. {
  3788.     int r=0;                                    /* Return value */
  3789.     char instance[INST_SZ+1]="";
  3790.     char *realm=NULL;
  3791.     char tgt[4*REALM_SZ+1];
  3792.  
  3793.     memset(instance, 0, sizeof(instance));
  3794.  
  3795. #ifdef COMMENT
  3796.     /* we only need to call krb_get_phost if the hostname */
  3797.     /* is not fully qualified.  But we have already done  */
  3798.     /* this in netopen() call.  This will save a round of */
  3799.     /* DNS queries.                                       */
  3800.     debug(F110,"k4_auth_send","krb_get_phost",0);
  3801.     if (realm = (char *)krb_get_phost(szHostName)) {
  3802.         ckstrncpy(instance, realm, INST_SZ);
  3803.     }
  3804. #else /* COMMENT */
  3805.     {
  3806.         char *p;
  3807.         ckstrncpy(instance, szHostName, INST_SZ);
  3808.         for ( p=instance; *p && *p != '.' ; p++ );
  3809.         *p = '\0';
  3810.     }
  3811. #endif /* COMMENT */
  3812.  
  3813.     debug(F110,"k4_auth_send","krb_get_realmofhost",0);
  3814.     realm = (char *)krb_realmofhost(szHostName);
  3815.  
  3816.     if (!realm) {
  3817.         strcpy(strTmp, "Can't find realm for host \"");
  3818.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  3819.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  3820.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3821.         krb4_errno = r;
  3822.         makestr(&krb4_errmsg,strTmp);
  3823.         return(0);
  3824.     }
  3825.  
  3826.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  3827.     r = ck_krb4_tkt_isvalid(tgt);
  3828.  
  3829.     if ( r <= 0 && krb4_autoget )
  3830.         ck_krb4_autoget_TGT(realm);
  3831.  
  3832.     debug(F110,"k4_auth_send","krb_mk_req",0);
  3833.     r = krb_mk_req(&k4_auth, krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3834.                     instance, realm, 0);
  3835.  
  3836.     if (r == 0) {
  3837.         debug(F110,"k4_auth_send","krb_get_cred",0);
  3838.         r = krb_get_cred(krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3839.                           instance, realm, &cred);
  3840.         if (r)
  3841.             debug(F111,"k4_auth_send","krb_get_cred() failed",r);
  3842.     }
  3843.     else
  3844.         debug(F111,"k4_auth_send","krb_mk_req() failed",r);
  3845.  
  3846.     if (r) {
  3847.         strcpy(strTmp, "Can't get \"");
  3848.         ckstrncat(strTmp,
  3849.                   krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,AUTHTMPBL);
  3850.         if (instance[0] != 0) {
  3851.             ckstrncat(strTmp, ".",AUTHTMPBL);
  3852.             ckstrncat(strTmp, instance,AUTHTMPBL);
  3853.         }
  3854.         ckstrncat(strTmp, "@",AUTHTMPBL);
  3855.         ckstrncat(strTmp, realm,AUTHTMPBL);
  3856.         ckstrncat(strTmp, "\" ticket\r\n  ",AUTHTMPBL);
  3857.         ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),AUTHTMPBL);
  3858.         debug(F111,"k4_auth_send",(char *)krb_get_err_text_entry(r),r);
  3859.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3860.         krb4_errno = r;
  3861.         makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3862.         return(0);
  3863.     }
  3864.  
  3865. #ifdef OS2
  3866.     if ( !szUserName[0] || !stricmp(szUserName,cred.pname) ) {
  3867.         ckstrncpy(szUserName, cred.pname, UIDBUFLEN);
  3868.     }
  3869. #endif /* OS2 */
  3870.     krb4_errno = r;
  3871.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3872.     debug(F110,"k4_auth_send",krb4_errmsg,0);
  3873.     return(1);
  3874. }
  3875.  
  3876. /*
  3877.  * Function: K4 parse authentication reply command
  3878.  *
  3879.  * Parameters:
  3880.  *  parsedat - the sub-command data.
  3881.  *
  3882.  *  end_sub - index of the character in the 'parsedat' array which
  3883.  *              is the last byte in a sub-negotiation
  3884.  *
  3885.  * Returns: Kerberos error code.
  3886.  */
  3887. static int
  3888. #ifdef CK_ANSIC
  3889. k4_auth_reply(unsigned char *parsedat, int end_sub)
  3890. #else
  3891. k4_auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3892. #endif
  3893. {
  3894. #ifdef CK_ENCRYPTION
  3895.     Session_Key skey;
  3896. #ifdef MIT_CURRENT
  3897.     krb5_data kdata;
  3898.     krb5_enc_data encdata;
  3899.     krb5_error_code code;
  3900. #endif /* MIT_CURRENT */
  3901. #endif
  3902.     time_t t;
  3903.     int x;
  3904.     int i;
  3905.  
  3906.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  3907.         auth_finished(AUTH_REJECT);
  3908.         return AUTH_FAILURE;
  3909.     }
  3910.  
  3911.     if (parsedat[4] == KRB_REJECT) {
  3912.         strTmp[0] = 0;
  3913.  
  3914.         for (i = 5; i <= end_sub; i++) {
  3915.             if (parsedat[i] == IAC)
  3916.                 break;
  3917.             strTmp[i-5] = parsedat[i];
  3918.             strTmp[i-4] = 0;
  3919.         }
  3920.  
  3921.         if (!strTmp[0])
  3922.             strcpy(strTmp, "Authentication rejected by remote machine!");
  3923.         printf("Kerberos V4 authentication failed!\r\n%s\r\n",strTmp);
  3924.         krb4_errno = -1;
  3925.         makestr(&krb4_errmsg,strTmp);
  3926.         auth_finished(AUTH_REJECT);
  3927.         return AUTH_FAILURE;
  3928.     }
  3929.  
  3930.     if (parsedat[4] == KRB_ACCEPT) {
  3931.         int net_len;
  3932.         if ((parsedat[3] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
  3933.             ckmakmsg(strTmp,sizeof(strTmp),"Kerberos V4 accepts you as ",
  3934.                       szUserName,NULL,NULL);
  3935.             printf("%s\r\n",strTmp);
  3936.             accept_complete = 1;
  3937.             krb4_errno = 0;
  3938.             makestr(&krb4_errmsg,strTmp);
  3939.             auth_finished(AUTH_USER);
  3940.             return AUTH_SUCCESS;
  3941.         }
  3942.  
  3943.         if ((parsedat[3] & AUTH_HOW_MASK) != AUTH_HOW_MUTUAL) {
  3944.             printf("Kerberos V4 authentication failed!\r\n");
  3945.             ckstrncpy(strTmp,
  3946.         "Kerberos V4 accepted you, but didn't provide mutual authentication",
  3947.                        sizeof(strTmp));
  3948.             printf("%s\r\n",strTmp);
  3949.             krb4_errno = -1;
  3950.             makestr(&krb4_errmsg,strTmp);
  3951.             auth_finished(AUTH_REJECT);
  3952.             return AUTH_FAILURE;
  3953.         }
  3954.  
  3955. #ifndef REMOVE_FOR_EXPORT
  3956. #ifdef CK_ENCRYPTION
  3957.         SendK4AuthSB(KRB4_CHALLENGE,k4_session_key,sizeof(k4_session_key));
  3958.  
  3959.         /* We have sent the decrypted session key to the host as a challenge */
  3960.         /* now encrypt it to restore it to its original valid DES key value */
  3961. #ifdef MIT_CURRENT
  3962.         kdata.data = k4_session_key;
  3963.         kdata.length = 8;
  3964.  
  3965.         encdata.ciphertext.data = k4_session_key;
  3966.         encdata.ciphertext.length = 8;
  3967.         encdata.enctype = ENCTYPE_UNKNOWN;
  3968.  
  3969.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey,
  3970.                                    0, 0, &kdata, &encdata)) {
  3971.             com_err("k4_auth_reply", code,
  3972.                      "while encrypting session_key");
  3973.             auth_finished(AUTH_REJECT);
  3974.             return AUTH_FAILURE;
  3975.         }
  3976. #else /* MIT_CURRENT */
  3977. #ifdef NT
  3978.         des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 1);
  3979. #else /* NT */
  3980.         des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 1);
  3981. #endif /* NT */
  3982.         hexdump(
  3983.             "k4_auth_reply des_ecb_encrypt(k4_session_key,k4_session_key,1)",
  3984.              k4_session_key,
  3985.              8
  3986.                 );
  3987. #endif /* MIT_CURRENT */
  3988.  
  3989. #ifdef CK_SSL
  3990.         if (!(ssl_active_flag || tls_active_flag))
  3991. #endif /* CK_SSL */
  3992.         {
  3993.         /* And then use it to configure the encryption state machine. */
  3994.             skey.type = SK_DES;
  3995.             skey.length = 8;
  3996.             skey.data = k4_session_key;
  3997.             encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  3998.         }
  3999. #endif /* ENCRYPTION */
  4000. #endif /* REMOVE_FOR_EXPORT */
  4001.         accept_complete = 1;
  4002.         ckmakmsg(strTmp,sizeof(strTmp),
  4003.                  "Kerberos V4 accepts you as ",szUserName,NULL,NULL);
  4004.         printf("%s\r\n",strTmp);
  4005.         krb4_errno = 0;
  4006.         makestr(&krb4_errmsg,strTmp);
  4007.         auth_finished(AUTH_USER);
  4008.         return AUTH_SUCCESS;
  4009.     }
  4010.  
  4011.     if (parsedat[4] == KRB4_RESPONSE) {
  4012.         if (end_sub < 12) {
  4013.             auth_finished(AUTH_REJECT);
  4014.             return AUTH_FAILURE;
  4015.         }
  4016.  
  4017.         hexdump("KRB4_RESPONSE &parsedat[5]",&parsedat[5],8);
  4018. #ifdef CK_ENCRYPTION
  4019.         hexdump("KRB4_RESPONSE k4_challenge",k4_challenge,8);
  4020.  
  4021.         /* The datablock returned from the host should match the value */
  4022.         /* we stored in k4_challenge.                                  */
  4023.         if (memcmp(&parsedat[5], k4_challenge, sizeof(k4_challenge)) != 0) {
  4024.             printf("Kerberos V4 authentication failed!\r\n%s\r\n",
  4025.             "Remote machine is being impersonated!");
  4026.             krb4_errno = -1;
  4027.             makestr(&krb4_errmsg,"Remote machine is being impersonated!");
  4028.             auth_finished(AUTH_REJECT);
  4029.             return AUTH_FAILURE;
  4030.         }
  4031. #else /* ENCRYPTION */
  4032.         makestr(&krb4_errmsg,"Kermit built without support for encryption.");
  4033.         return AUTH_FAILURE;
  4034. #endif /* ENCRYPTION */
  4035.         mutual_complete = 1;
  4036.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  4037.                    sizeof(strTmp));
  4038.         printf("%s\r\n",strTmp);
  4039.         krb4_errno = 0;
  4040.         makestr(&krb4_errmsg,strTmp);
  4041.         auth_finished(AUTH_USER);
  4042.         return AUTH_SUCCESS;
  4043.     }
  4044.     auth_finished(AUTH_REJECT);
  4045.     return AUTH_FAILURE;
  4046. }
  4047.  
  4048. /*
  4049.  * Function: K4 parse authentication IS command
  4050.  *
  4051.  * Parameters:
  4052.  *  parsedat - the sub-command data.
  4053.  *
  4054.  *  end_sub - index of the character in the 'parsedat' array which
  4055.  *            is the last byte in a sub-negotiation
  4056.  *
  4057.  * Returns: Kerberos error code.
  4058.  */
  4059.  
  4060. static int
  4061. #ifdef CK_ANSIC
  4062. k4_auth_is(unsigned char *parsedat, int end_sub)
  4063. #else
  4064. k4_auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  4065. #endif
  4066. {
  4067. #ifdef CK_ENCRYPTION
  4068.     Session_Key skey;
  4069. #ifdef MIT_CURRENT
  4070.     Block datablock, tmpkey;
  4071.     krb5_data kdata;
  4072.     krb5_enc_data encdata;
  4073.     krb5_error_code code;
  4074. #else /* MIT_CURRENT */
  4075.     Block datablock;
  4076. #endif /* MIT_CURRENT */
  4077. #endif  /* ENCRYPTION */
  4078.     char realm[REALM_SZ+1];
  4079.     char instance[INST_SZ];
  4080.     int r = 0;
  4081.     char * data = &parsedat[5];
  4082.     int    cnt = end_sub - 5;
  4083.     extern char myipaddr[];
  4084.     struct hostent *host;
  4085.     struct in_addr inaddr;
  4086.     int i;
  4087.  
  4088.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  4089.         debug(F110,"k4_auth_is","Not kerberos v4",0);
  4090.         auth_finished(AUTH_REJECT);
  4091.         return AUTH_FAILURE;
  4092.     }
  4093.  
  4094.     switch (parsedat[4]) {
  4095.     case KRB_AUTH:
  4096.         debug(F110,"k4_auth_is","KRB_AUTH",0);
  4097.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ+1);
  4098.         if (realm[0] == '\0') {
  4099.             SendK4AuthSB(KRB_REJECT, (void *)"No local V4 Realm.", -1);
  4100.             printf("\r\n? Kerberos 4 - No Local Realm\r\n");
  4101.             debug(F110,"k4_auth_is","No local realm",0);
  4102.             krb4_errno = -1;
  4103.             makestr(&krb4_errmsg,"No local realm");
  4104.             auth_finished(AUTH_REJECT);
  4105.             return AUTH_FAILURE;
  4106.         }
  4107.         debug(F110,"k4_auth_is",realm,0);
  4108.         if ( cnt < sizeof(k4_auth.dat) ) {
  4109.             k4_auth.length = cnt;
  4110.             memcpy((void *)k4_auth.dat, (void *)data, k4_auth.length);
  4111.         } else
  4112.             k4_auth.length = 0;
  4113.         hexdump("k4_auth.dat",k4_auth.dat, k4_auth.length);
  4114.  
  4115.         /* Get Instance */
  4116.         inaddr.s_addr = inet_addr(myipaddr);
  4117.         host = gethostbyaddr((unsigned char *)&inaddr,4,PF_INET);
  4118.         if ( host ) {
  4119. #ifdef HADDRLIST
  4120.             host = ck_copyhostent(host);
  4121. #endif /* HADDRLIST */
  4122.             ckstrncpy(instance,host->h_name,INST_SZ);
  4123.             for ( i=0;i<INST_SZ;i++ ) {
  4124.                 if ( instance[i] == '.' )
  4125.                     instance[i] = '\0';
  4126.                 else
  4127.                     instance[i] = tolower(instance[i]);
  4128.             }
  4129.         } else {
  4130.             instance[0] = '*';
  4131.             instance[1] = 0;
  4132.         }
  4133.  
  4134.         if (r = krb_rd_req(&k4_auth,
  4135.                             krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  4136.                             instance, 0, &k4_adat, k4_keytab)) {
  4137.  
  4138.             hexdump("k4_adat", &k4_adat, sizeof(AUTH_DAT));
  4139.             krb_kntoln(&k4_adat, k4_name);
  4140.             ckmakmsg(strTmp,sizeof(strTmp),
  4141.                      "Kerberos failed him as ", k4_name,NULL,NULL);
  4142.             printf("%s\r\n",strTmp);
  4143.             krb4_errno = r;
  4144.             makestr(&krb4_errmsg,strTmp);
  4145.             SendK4AuthSB(KRB_REJECT, (void *)krb_get_err_text_entry(r), -1);
  4146.             auth_finished(AUTH_REJECT);
  4147.             return AUTH_FAILURE;
  4148.         }
  4149.  
  4150. #ifdef CK_ENCRYPTION
  4151.         memcpy((void *)k4_session_key, (void *)k4_adat.session,
  4152.                 sizeof(Block));                 /* safe */
  4153.         hexdump("k4_auth_is k4_session_key",k4_session_key,sizeof(Block));
  4154. #endif /* ENCRYPTION */
  4155.         krb_kntoln(&k4_adat, k4_name);
  4156.  
  4157.         ckstrncpy(szUserNameAuthenticated,k4_name,UIDBUFLEN);
  4158.         if (szUserNameRequested && !kuserok(&k4_adat, k4_name)) {
  4159.             SendK4AuthSB(KRB_ACCEPT, (void *)0, 0);
  4160.             if ( !strcmp(k4_name,szUserNameRequested) )
  4161.                 auth_finished(AUTH_VALID);
  4162.             else
  4163.                 auth_finished(AUTH_USER);
  4164.             accept_complete = 1;
  4165.         }
  4166.         else {
  4167.             SendK4AuthSB(KRB_REJECT,
  4168.                   (void *)"user is not authorized", -1);
  4169.             auth_finished(AUTH_REJECT);
  4170.             krb4_errno = r;
  4171.             makestr(&krb4_errmsg,"user is not authorized");
  4172.             return(AUTH_FAILURE);
  4173.         }
  4174.         break;
  4175.  
  4176.     case KRB4_CHALLENGE:
  4177.         debug(F110,"k4_auth_is","KRB_CHALLENGE",0);
  4178. #ifndef CK_ENCRYPTION
  4179.         SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4180. #else   /* ENCRYPTION */
  4181.         if (!VALIDKEY(k4_session_key)) {
  4182.             /*
  4183.             * We don't have a valid session key, so just
  4184.             * send back a response with an empty session
  4185.             * key.
  4186.             */
  4187.             SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4188.             mutual_complete = 1;
  4189.             break;
  4190.         }
  4191.  
  4192.         /*
  4193.         * Initialize the random number generator since it's
  4194.         * used later on by the encryption routine.
  4195.         */
  4196. #ifdef MIT_CURRENT
  4197.         kdata.data = k4_session_key;
  4198.         kdata.length = 8;
  4199.  
  4200.         if (code = krb5_c_random_seed(k5_context, &kdata)) {
  4201.             com_err("k4_auth_is", code,
  4202.                      "while seeding random number generator");
  4203.             auth_finished(AUTH_REJECT);
  4204.             return AUTH_FAILURE;
  4205.         }
  4206.  
  4207.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4208.         /*
  4209.         * Take the received encrypted challenge, and encrypt
  4210.         * it again to get a unique session_key for the
  4211.         * ENCRYPT option.
  4212.         */
  4213.         k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  4214.         k4_krbkey.length = 8;
  4215.         k4_krbkey.contents = k4_session_key;
  4216.  
  4217.         kdata.data = datablock;
  4218.         kdata.length = 8;
  4219.  
  4220.         encdata.ciphertext.data = tmpkey;
  4221.         encdata.ciphertext.length = 8;
  4222.         encdata.enctype = ENCTYPE_UNKNOWN;
  4223.  
  4224.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4225.                                    &kdata, &encdata)) {
  4226.             com_err("k4_auth_is", code, "while encrypting random key");
  4227.             auth_finished(AUTH_REJECT);
  4228.             return AUTH_FAILURE;
  4229.         }
  4230.  
  4231. #ifdef CK_SSL
  4232.         if (!(ssl_active_flag || tls_active_flag))
  4233. #endif /* CK_SSL */
  4234.         {
  4235.             skey.type = SK_DES;
  4236.             skey.length = 8;
  4237.             skey.data = tmpkey;
  4238.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4239.         }
  4240.         /*
  4241.         * Now decrypt the received encrypted challenge,
  4242.         * increment by one, re-encrypt it and send it back.
  4243.         */
  4244.         encdata.ciphertext.data = datablock;
  4245.         encdata.ciphertext.length = 8;
  4246.         encdata.enctype = ENCTYPE_UNKNOWN;
  4247.  
  4248.         kdata.data = k4_challenge;
  4249.         kdata.length = 8;
  4250.  
  4251.         if (code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  4252.                                    &encdata, &kdata)) {
  4253.             com_err("k4_auth_is", code, "while decrypting challenge");
  4254.             auth_finished(AUTH_REJECT);
  4255.             return AUTH_FAILURE;
  4256.         }
  4257. #else /* MIT_CURRENT */
  4258.         des_set_random_generator_seed(k4_session_key);
  4259.         r = des_key_sched(k4_session_key, k4_sched);
  4260.         if ( r == -1 ) {
  4261.             printf("?Invalid DES key specified in credentials\r\n");
  4262.             debug(F110,"auth_is CHALLENGE",
  4263.                    "invalid DES Key specified in credentials",0);
  4264.         } else if ( r == -2 ) {
  4265.             printf("?Weak DES key specified in credentials\r\n");
  4266.             debug(F110,"auth_is CHALLENGE",
  4267.                    "weak DES Key specified in credentials",0);
  4268.         } else if ( r != 0 ) {
  4269.             printf("?DES Key Schedule not set by credentials\r\n");
  4270.             debug(F110,"auth_is CHALLENGE",
  4271.                    "DES Key Schedule not set by credentials",0);
  4272.         }
  4273.         hexdump("auth_is schedule",k4_sched,8*16);
  4274.  
  4275.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4276.         hexdump("auth_is challege",datablock,sizeof(Block));
  4277.  
  4278.         /*
  4279.         * Take the received encrypted challenge, and encrypt
  4280.         * it again to get a unique k4_session_key for the
  4281.         * ENCRYPT option.
  4282.         */
  4283. #ifdef NT
  4284.         des_ecb_encrypt(datablock, k4_session_key, k4_sched, 1);
  4285. #else /* NT */
  4286.         des_ecb_encrypt(&datablock, &k4_session_key, k4_sched, 1);
  4287. #endif /* NT */
  4288.         hexdump("auth_is des_ecb_encrypt(datablock,k4_session_key,1)",
  4289.                  k4_session_key,8);
  4290.  
  4291. #ifdef CK_SSL
  4292.         if (!(ssl_active_flag || tls_active_flag))
  4293. #endif /* CK_SSL */
  4294.         {
  4295.             skey.type = SK_DES;
  4296.             skey.length = 8;
  4297.             skey.data = k4_session_key;
  4298.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4299.         }
  4300.         /*
  4301.         * Now decrypt the received encrypted challenge,
  4302.         * increment by one, re-encrypt it and send it back.
  4303.         */
  4304. #ifdef NT
  4305.         des_ecb_encrypt(datablock, k4_challenge, k4_sched, 0);
  4306. #else /* NT */
  4307.         des_ecb_encrypt(&datablock, &k4_challenge, k4_sched, 0);
  4308. #endif /* NT */
  4309.         hexdump("auth_is des_ecb_encrypt(datablock,k4_challenge,0)",
  4310.                  k4_session_key,8);
  4311. #endif /* MIT_CURRENT */
  4312.         for (r = 7; r >= 0; r--) {
  4313.             register int t;
  4314.             t = (unsigned int)k4_challenge[r] + 1;
  4315.             k4_challenge[r] = t;        /* ignore overflow */
  4316.             if (t < 256)                /* if no overflow, all done */
  4317.                 break;
  4318.         }
  4319.         hexdump("auth_is k4_challenge+1",k4_challenge,8);
  4320.  
  4321. #ifdef MIT_CURRENT
  4322.         kdata.data = k4_challenge;
  4323.         kdata.length = 8;
  4324.  
  4325.         encdata.ciphertext.data = k4_challenge;
  4326.         encdata.ciphertext.length = 8;
  4327.         encdata.enctype = ENCTYPE_UNKNOWN;
  4328.  
  4329.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4330.                                    &kdata, &encdata)) {
  4331.             com_err("k4_auth_is", code, "while decrypting challenge");
  4332.             auth_finished(AUTH_REJECT);
  4333.             return AUTH_FAILURE;
  4334.         }
  4335. #else /* MIT_CURRENT */
  4336. #ifdef NT
  4337.         des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  4338. #else /* NT */
  4339.         des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  4340. #endif /* NT */
  4341.         hexdump("auth_is des_ecb_encrypt(k4_challenge_key,k4_challenge,1)",
  4342.                  k4_challenge,8);
  4343.  
  4344. #endif /* MIT_CURRENT */
  4345.         SendK4AuthSB(KRB4_RESPONSE,(void *)k4_challenge,sizeof(k4_challenge));
  4346. #endif  /* ENCRYPTION */
  4347.         mutual_complete = 1;
  4348.         break;
  4349.  
  4350.     default:
  4351.         if (1)
  4352.             printf("Unknown Kerberos option %d\r\n", data[-1]);
  4353.         SendK4AuthSB(KRB_REJECT, 0, 0);
  4354.         return(AUTH_FAILURE);
  4355.     }
  4356.     krb4_errno = r;
  4357.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  4358.     return(AUTH_SUCCESS);
  4359. }
  4360. #endif /* KRB4 */
  4361.  
  4362. #ifdef KRB5
  4363. int
  4364. ck_krb5_autoget_TGT(char * realm)
  4365. {
  4366.     extern struct krb_op_data krb_op;
  4367.     extern struct krb5_init_data krb5_init;
  4368.     char passwd[PWD_SZ];
  4369.     char prompt[64];
  4370.     char * saverealm=NULL;
  4371.     int  rc = -1;
  4372.     extern char * k5prprompt;
  4373.     extern char * k5pwprompt;
  4374.  
  4375.     ini_kerb();         /* Place defaults in above structs */
  4376.     passwd[0] = '\0';
  4377.  
  4378.     if ( krb5_init.principal == NULL ||
  4379.          krb5_init.principal[0] == '\0') {
  4380.         int ok = uq_txt(NULL,k5prprompt && k5prprompt[0] ? k5prprompt :
  4381.                   "Kerberos 5 Principal: ",2,NULL,passwd,PWD_SZ-1,NULL,
  4382.             DEFAULT_UQ_TIMEOUT);
  4383.         if ( ok && passwd[0] )
  4384.             makestr(&krb5_init.principal,passwd);
  4385.         else
  4386.             return(0);
  4387.     }
  4388.  
  4389.     /* Save realm in init structure so it can be restored */
  4390.     if ( realm ) {
  4391.         saverealm = krb5_init.realm;
  4392.         krb5_init.realm = realm;
  4393.     }
  4394.  
  4395.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  4396.         int ok;
  4397.         if ( k5pwprompt && k5pwprompt[0] &&
  4398.              (strlen(k5pwprompt) + strlen(krb5_init.principal) +
  4399.               strlen(krb5_init.realm) - 4) < sizeof(prompt)) {
  4400.             sprintf(prompt,k5pwprompt,krb5_init.principal,krb5_init.realm);
  4401.         } else
  4402.         ckmakxmsg(prompt,sizeof(prompt),
  4403.                   k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  4404.                   "Kerberos 5 Password for ",
  4405.                   krb5_init.principal,"@",krb5_init.realm,": ",
  4406.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL
  4407.                  );
  4408.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL,
  4409.             DEFAULT_UQ_TIMEOUT);
  4410.         if ( !ok )
  4411.             passwd[0] = '\0';
  4412.     } else {
  4413.         ckstrncpy(passwd,(char *)pwbuf,sizeof(passwd));
  4414. #ifdef OS2
  4415.         if ( pwcrypt )
  4416.             ck_encrypt((char *)passwd);
  4417. #endif /* OS2 */
  4418.     }
  4419.  
  4420.     if ( passwd[0] ) {
  4421.         extern struct krb4_init_data krb4_init;
  4422.         char * savek4realm=NULL;
  4423.  
  4424.         makestr(&krb5_init.password,passwd);
  4425.  
  4426.         if ( krb5_d_getk4 ) {
  4427.             krb5_init.getk4 = 1;
  4428.             makestr(&krb4_init.principal,krb5_init.principal);
  4429.             makestr(&krb4_init.password,passwd);
  4430.             if ( realm ) {
  4431.                 savek4realm = krb4_init.realm;
  4432.                 krb4_init.realm = realm;
  4433.             }
  4434.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,&krb4_init);
  4435.  
  4436.             if ( savek4realm )
  4437.                 krb4_init.realm = savek4realm;
  4438.             free(krb4_init.password);
  4439.             krb4_init.password = NULL;
  4440.         } else {
  4441.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,NULL);
  4442.         }
  4443.  
  4444.         free(krb5_init.password);
  4445.         krb5_init.password = NULL;
  4446.  
  4447.         memset(passwd,0,PWD_SZ);
  4448.     }
  4449.  
  4450.     /* restore realm to init structure if needed */
  4451.     if ( saverealm )
  4452.         krb5_init.realm = saverealm;
  4453.     return(rc == 0);
  4454. }
  4455.  
  4456. static krb5_error_code
  4457. #ifdef CK_ANSIC
  4458. k5_get_ccache( krb5_context k5_context, krb5_ccache * p_ccache,
  4459.                char * cc_name )
  4460. #else  /* CK_ANSIC */
  4461. k5_get_ccache(k5_context, p_ccache, cc_name)
  4462.     krb5_context k5_context;
  4463.     krb5_ccache * p_ccache;
  4464.     char * cc_name;
  4465. #endif /* CK_ANSIC */
  4466. {
  4467.     krb5_error_code r=0;
  4468.     char cc_tmp[CKMAXPATH+1];
  4469.     const char * def_name = NULL;
  4470.  
  4471. #ifndef HEIMDAL
  4472.     if ( cc_name ) {
  4473.         if ( strncmp("FILE:",cc_name,5) &&
  4474.              strncmp("MEMORY:",cc_name,7) &&
  4475.              strncmp("API:",cc_name,4) &&
  4476.              strncmp("STDIO:",cc_name,6) &&
  4477.          strncmp("MSLSA:",cc_name,6))
  4478. #ifdef NT
  4479.             ckmakmsg(cc_tmp,CKMAXPATH,"API:",cc_name,NULL,NULL);
  4480. #else /* NT */
  4481.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",cc_name,NULL,NULL);
  4482. #endif /* NT */
  4483.         else {
  4484.             ckstrncpy(cc_tmp,cc_name,CKMAXPATH);
  4485.         }
  4486.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4487.         if (r != 0) {
  4488.             com_err("k5_get_ccache resolving ccache",r,
  4489.                      cc_tmp);
  4490.         } else {
  4491.             /* Make sure GSSAPI sees the same cache we are using */
  4492.             char buf[128];
  4493.             ckmakmsg((char *)buf,128,"KRB5CCNAME=",cc_tmp,NULL,NULL);
  4494.             putenv(buf);
  4495.         }
  4496.     } else if ( krb5_d_cc ) {
  4497.         if ( strncmp("FILE:",krb5_d_cc,5) &&
  4498.              strncmp("MEMORY:",krb5_d_cc,7) &&
  4499.              strncmp("API:",krb5_d_cc,4) &&
  4500.              strncmp("STDIO:",krb5_d_cc,6) &&
  4501.          strncmp("MSLSA:", krb5_d_cc,6))
  4502. #ifdef NT
  4503.             ckmakmsg(cc_tmp,CKMAXPATH,"API:",krb5_d_cc,NULL,NULL);
  4504. #else /* NT */
  4505.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",krb5_d_cc,NULL,NULL);
  4506. #endif /* NT */
  4507.         else {
  4508.             ckstrncpy(cc_tmp,krb5_d_cc,CKMAXPATH);
  4509.         }
  4510.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4511.         if (r != 0) {
  4512.             com_err("k5_get_ccache resolving ccache",r,
  4513.                      krb5_d_cc);
  4514.         } else {
  4515.             /* Make sure GSSAPI sees the same cache we are using */
  4516.             char buf[128];
  4517.             ckmakmsg((char *)buf,128,"KRB5CCNAME=",cc_tmp,NULL,NULL);
  4518.             putenv(buf);
  4519.         }
  4520.     } else
  4521. #endif /* HEIMDAL */
  4522.     {
  4523.         if ((r = krb5_cc_default(k5_context, p_ccache))) {
  4524.             com_err("k5_get_ccache",r,"while getting default ccache");
  4525.         }
  4526.     }
  4527.     /* do not set krb5_errno/krb5_errmsg here since the value returned */
  4528.     /* is being passed internally within the krb5 functions.           */
  4529.     return(r);
  4530. }
  4531.  
  4532.  
  4533. char *
  4534. ck_krb5_realmofhost(char *host)
  4535. {
  4536.     char ** realmlist=NULL;
  4537.     krb5_context private_context=NULL;
  4538.     static char * realm = NULL;
  4539.  
  4540.     if ( !host )
  4541.         return NULL;
  4542.  
  4543.     if ( realm ) {
  4544.         free(realm);
  4545.         realm = NULL;
  4546.     }
  4547.  
  4548.     /* create private_context */
  4549.     if (krb5_init_context(&private_context)) {
  4550.         debug(F110,"ck_krb5_realmofhost()","unable to init_context",0);
  4551.         return(NULL);
  4552.     }
  4553.  
  4554.     krb5_get_host_realm(private_context,host,&realmlist);
  4555.     if (realmlist && realmlist[0]) {
  4556.         makestr(&realm,realmlist[0]);
  4557.         krb5_free_host_realm(private_context,realmlist);
  4558.         realmlist = NULL;
  4559.     }
  4560.  
  4561.     if ( private_context ) {
  4562.         krb5_free_context(private_context);
  4563.         private_context = NULL;
  4564.     }
  4565.  
  4566.     if (ckstrchr(realm,'.') == NULL) {
  4567.         int n = 0;
  4568.         char * p = host;
  4569.         while ( (p = ckstrchr(p,'.')) != NULL ) {
  4570.             n++;
  4571.             p++;
  4572.         }
  4573.         if (n == 1) {
  4574.             makestr(&realm,host);
  4575.             ckupper(realm);
  4576.         } else {
  4577.             free(realm);
  4578.             realm = NULL;
  4579.         }
  4580.     }
  4581.     return(realm);
  4582. }
  4583.  
  4584. /*
  4585.  *
  4586.  * K5_auth_send - gets authentication bits we need to send to KDC.
  4587.  *
  4588.  * Code lifted from telnet sample code in the appl directory.
  4589.  *
  4590.  * Result is left in k5_auth
  4591.  *
  4592.  * Returns: 0 on failure, 1 on success
  4593.  *
  4594.  */
  4595.  
  4596. static int
  4597. #ifdef CK_ANSIC
  4598. k5_auth_send(int how, int encrypt, int forward)
  4599. #else
  4600. k5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  4601. #endif
  4602. {
  4603.     krb5_error_code r=0;
  4604.     krb5_ccache ccache=NULL;
  4605. #ifndef HEIMDAL
  4606.     krb5_creds creds;
  4607. #endif /* HEIMDAL */
  4608.     krb5_creds * new_creds=NULL;
  4609. #ifdef CK_ENCRYPTION
  4610.     krb5_keyblock *newkey = 0;
  4611. #endif /* CK_ENCRYPTION */
  4612.     krb5_flags ap_opts, auth_flags;
  4613.     char type_check[32];
  4614.     krb5_data checksum;
  4615.     int len=0;
  4616.     char * realm = NULL;
  4617.     char tgt[256];
  4618.  
  4619.     realm = ck_krb5_realmofhost(szHostName);
  4620.     if (!realm) {
  4621.         ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  4622.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  4623.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  4624.         printf("?Kerberos 5 error: %s\r\n",strTmp);
  4625.         krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  4626.         makestr(&krb5_errmsg,strTmp);
  4627.         return(0);
  4628.     }
  4629.  
  4630.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  4631.     debug(F110,"k5_auth_send TGT",tgt,0);
  4632.     if ( krb5_autoget &&
  4633.          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  4634.           (ck_krb5_is_tgt_valid() > 0)) )
  4635.         ck_krb5_autoget_TGT(realm);
  4636.  
  4637.     r = k5_get_ccache(k5_context,&ccache,NULL);
  4638.     if ( r ) {
  4639.         com_err(NULL, r, "while authorizing (0).");
  4640.         krb5_errno = r;
  4641.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4642.         return(0);
  4643.     }
  4644.  
  4645. #ifndef HEIMDAL
  4646.     memset((char *)&creds, 0, sizeof(creds));
  4647.     if (r = krb5_sname_to_principal(k5_context, szHostName,
  4648.                                 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4649.                                 KRB5_NT_SRV_HST, &creds.server)) {
  4650.         com_err(NULL, r, "while authorizing (1).");
  4651.         krb5_errno = r;
  4652.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4653.         return(0);
  4654.     }
  4655.  
  4656.     if (forward_flag) {
  4657.         if (fwd_server) {
  4658.             krb5_free_principal(k5_context,fwd_server);
  4659.             fwd_server = NULL;
  4660.         }
  4661.         krb5_copy_principal(k5_context,creds.server,&fwd_server);
  4662.     }
  4663.  
  4664.     r = krb5_cc_get_principal(k5_context, ccache, &creds.client);
  4665.     if (r) {
  4666.         com_err(NULL, r, "while authorizing (2).");
  4667.         krb5_free_cred_contents(k5_context, &creds);
  4668.         krb5_errno = r;
  4669.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4670.         return(0);
  4671.     }
  4672.  
  4673.     if (szUserName[0] == '\0') {                /* Get user name now */
  4674.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4675.         if ( len < sizeof(szUserName) ) {
  4676.             memcpy(szUserName,
  4677.                     krb5_princ_component(k5_context, creds.client, 0)->data,
  4678.                     len);                       /* safe */
  4679.         } else
  4680.             len = 0;
  4681.         szUserName[len] = '\0';
  4682.     } else {
  4683.         char * name = NULL;
  4684.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4685.         if ( len == strlen(szUserName) ) {
  4686.             name = krb5_princ_component(k5_context, creds.client, 0)->data;
  4687. #ifdef OS2
  4688.             if ( !strnicmp(szUserName,name,len) )
  4689.                 memcpy(szUserName,name,len);    /* safe */
  4690. #endif /* OS2 */
  4691.         }
  4692.     }
  4693.     if ( tn_auth_krb5_des_bug ) {   /* !ALLOW_KRB_3DES_ENCRYPT */
  4694.         /* Not sure if this is necessary anymore.  What impact does it have
  4695.          * on Win2000 TGTs that use DES_CBC_MD5 or RC4_HMAC?
  4696.          *
  4697.          * This prevents using 3DES Service Tickets.
  4698.          */
  4699.         creds.keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  4700.     }
  4701.  
  4702.     if (r = krb5_get_credentials(k5_context, 0,
  4703.                                   ccache, &creds, &new_creds)) {
  4704.         com_err(NULL, r, "while authorizing (3).");
  4705.         krb5_free_cred_contents(k5_context, &creds);
  4706.         krb5_errno = r;
  4707.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4708.         return(0);
  4709.     }
  4710. #endif /* HEIMDAL */
  4711.  
  4712.     if (auth_context) {
  4713.         krb5_auth_con_free(k5_context, auth_context);
  4714.         auth_context = 0;
  4715.     }
  4716.     if (r = krb5_auth_con_init(k5_context, &auth_context)) {
  4717.         com_err(NULL, r, "while initializing auth context");
  4718.         krb5_errno = r;
  4719.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4720.         return(0);
  4721.     }
  4722.  
  4723.     /* UPDATE for START_TLS.  AUTH_ENCRYPT_START_TLS and inclusion of */
  4724.     /* client and then server finished messages.                      */
  4725.  
  4726.     type_check[0] = AUTHTYPE_KERBEROS_V5;
  4727.     type_check[1] = AUTH_CLIENT_TO_SERVER |
  4728.         (how ? AUTH_HOW_MUTUAL : AUTH_HOW_ONE_WAY) |
  4729.         (encrypt) |
  4730.         (forward ? INI_CRED_FWD_ON : INI_CRED_FWD_OFF);
  4731. #ifdef CK_SSL
  4732.     if (encrypt == AUTH_ENCRYPT_START_TLS) {
  4733.         ssl_get_client_finished(&type_check[2],12);
  4734.         ssl_get_server_finished(&type_check[14],12);
  4735.     }
  4736. #endif /* CK_SSL */
  4737.  
  4738. #ifndef HEIMDAL
  4739.     checksum.magic = KV5M_DATA;
  4740. #endif /* HEIMDAL */
  4741.     checksum.length =
  4742. #ifdef CK_SSL
  4743.         (encrypt == AUTH_ENCRYPT_START_TLS) ? 26 :
  4744. #endif /* CK_SSL */
  4745.         2;
  4746.     checksum.data = (char *)&type_check;
  4747.  
  4748.     ap_opts = 0;
  4749.     if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
  4750.         ap_opts |= AP_OPTS_MUTUAL_REQUIRED;
  4751.  
  4752. #ifdef HEIMDAL
  4753.     r = krb5_auth_setkeytype(k5_context, auth_context, KEYTYPE_DES);
  4754.     if (r)
  4755.         com_err(NULL, r, "while setting auth keytype");
  4756.     r = krb5_auth_con_setaddrs_from_fd(k5_context,auth_context, &ttyfd);
  4757.     if (r)
  4758.         com_err(NULL, r, "while setting auth addrs");
  4759.     r = krb5_mk_req(k5_context, &auth_context, ap_opts,
  4760.                     krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4761.                     szHostName, &checksum, ccache, &k5_auth);
  4762.     if (r)
  4763.         com_err(NULL, r, "while making request");
  4764. #else /* HEIMDAL */
  4765.     auth_flags = KRB5_AUTH_CONTEXT_RET_TIME;
  4766. #ifdef CK_ENCRYPTION
  4767.     ap_opts |= AP_OPTS_USE_SUBKEY;
  4768. #endif /* CK_ENCRYPTION */
  4769. #ifdef TLS_VERIFY
  4770.     if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  4771.         auth_flags |= KRB5_AUTH_CONTEXT_DO_SEQUENCE;
  4772.         if (!krb5_d_no_addresses)
  4773.             r = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  4774.                                  KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  4775.     }
  4776. #endif /* CK_SSL */
  4777.     krb5_auth_con_setflags(k5_context, auth_context, auth_flags);
  4778.     r = krb5_mk_req_extended(k5_context, &auth_context, ap_opts,
  4779.                               &checksum, new_creds, &k5_auth);
  4780. #endif /* HEIMDAL */
  4781.  
  4782. #ifdef CK_ENCRYPTION
  4783.     if (!r) {
  4784.         r = krb5_auth_con_getlocalsubkey(k5_context, auth_context, &newkey);
  4785.         if (r)
  4786.             r = krb5_auth_con_getkey(k5_context, auth_context, &newkey);
  4787.  
  4788.         if (k5_session_key) {
  4789.             krb5_free_keyblock(k5_context, k5_session_key);
  4790.             k5_session_key = 0;
  4791.         }
  4792.     }
  4793.     if (newkey) {
  4794.         /*
  4795.         * keep the key in our private storage, but don't use it
  4796.         * yet---see kerberos5_reply() below
  4797.         */
  4798. #ifdef HEIMDAL
  4799.         if ((newkey->keytype == ETYPE_DES_CBC_CRC) ||
  4800.              (newkey->keytype == ETYPE_DES_CBC_MD5) ||
  4801.              (newkey->keytype == ETYPE_DES_CBC_MD4))
  4802.         {
  4803.             debug(F111,"k5_auth_send()","newkey->keytype",newkey->keytype);
  4804.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4805.         }
  4806. #else /* HEIMDAL */
  4807.         /* look for all possible DES keys first - just for compatibility */
  4808.         /* other key types are much less likely to be available          */
  4809.         if ((newkey->enctype == ENCTYPE_DES_CBC_CRC) ||
  4810.              (newkey->enctype == ENCTYPE_DES_CBC_MD5) ||
  4811.              (newkey->enctype == ENCTYPE_DES_CBC_MD4))
  4812.         {
  4813.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4814.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4815.         }
  4816.         else if ((new_creds->keyblock.enctype == ENCTYPE_DES_CBC_CRC) ||
  4817.                  (new_creds->keyblock.enctype == ENCTYPE_DES_CBC_MD5))
  4818.         {
  4819.             /* use the session key in credentials instead */
  4820.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4821.                    new_creds->keyblock.enctype);
  4822.             krb5_copy_keyblock(k5_context,
  4823.                                 &new_creds->keyblock, &k5_session_key);
  4824.         }
  4825.         else if (newkey->enctype != 0)
  4826.         {
  4827.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4828.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4829.         }
  4830.         else if (new_creds->keyblock.enctype != 0)
  4831.         {
  4832.             /* use the session key in credentials instead */
  4833.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4834.                    new_creds->keyblock.enctype);
  4835.             krb5_copy_keyblock(k5_context,
  4836.                                 &new_creds->keyblock, &k5_session_key);
  4837.         }
  4838.         else {
  4839.             debug(F110,"k5_auth_send()","NO KEY in newkey",0);
  4840.         }
  4841. #endif /* HEIMDAL */
  4842.         krb5_free_keyblock(k5_context, newkey);
  4843.     }
  4844. #endif /* CK_ENCRYPTION */
  4845. #ifndef HEIMDAL
  4846.     krb5_free_cred_contents(k5_context, &creds);
  4847.     krb5_free_creds(k5_context, new_creds);
  4848. #endif /* HEIMDAL */
  4849.     krb5_cc_close(k5_context,ccache);
  4850.  
  4851.     if (r) {
  4852.         com_err(NULL, r, "while authorizing (4).");
  4853.         krb5_errno = r;
  4854.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4855.         return(0);
  4856.     }
  4857.     krb5_errno = 0;
  4858.     makestr(&krb5_errmsg,"OK");
  4859.     return(1);
  4860. }
  4861.  
  4862. /*
  4863.  * K5_auth_reply -- checks the reply for mutual authentication.
  4864.  */
  4865. static int
  4866. #ifdef CK_ANSIC
  4867. k5_auth_reply(int how, unsigned char *data, int cnt)
  4868. #else
  4869. k5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  4870. #endif
  4871. {
  4872. #ifdef CK_ENCRYPTION
  4873.     Session_Key skey;
  4874. #endif /* CK_ENCRYPTION */
  4875.  
  4876.     data += 4;                                  /* Point to status byte */
  4877.     cnt -=5;
  4878.  
  4879.     switch (*data++) {
  4880.     case KRB_REJECT:
  4881.         if (cnt > 0) {
  4882.             char *s;
  4883.             int len;
  4884.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication because\r\n",
  4885.                       sizeof(strTmp));
  4886.             len = strlen(strTmp);
  4887.             if ( len + cnt < sizeof(strTmp) ) {
  4888.                 s = strTmp + strlen(strTmp);
  4889.                 memcpy(s, data, cnt);           /* safe */
  4890.                 s[cnt] = 0;
  4891.             }
  4892.         } else
  4893.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication",
  4894.                       sizeof(strTmp));
  4895.         krb5_errno = -1;
  4896.         makestr(&krb5_errmsg,strTmp);
  4897.         printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4898.         auth_finished(AUTH_REJECT);
  4899.         return AUTH_FAILURE;
  4900.  
  4901.     case KRB_ACCEPT:
  4902.         if (!mutual_complete) {
  4903.             if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL && !mutual_complete) {
  4904.                 ckstrncpy(strTmp,
  4905.                           "Kerberos V5 accepted you, but didn't provide"
  4906.                           " mutual authentication",sizeof(strTmp));
  4907.                 printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4908.                 krb5_errno = -1;
  4909.                 makestr(&krb5_errmsg,strTmp);
  4910.                 auth_finished(AUTH_REJECT);
  4911.                 return AUTH_FAILURE;
  4912.             }
  4913.  
  4914. #ifdef CK_ENCRYPTION
  4915.             if (k5_session_key) {
  4916.                 if ( tn_auth_krb5_des_bug ) {   /* !ALLOW_KRB_3DES_ENCRYPT */
  4917.                     skey.type = SK_DES;
  4918.                     skey.length = 8;
  4919. #ifdef HEIMDAL
  4920.                     skey.data = k5_session_key->keyvalue.data;
  4921. #else /* HEIMDAL */
  4922.                     skey.data = k5_session_key->contents;
  4923. #endif /* HEIMDAL */
  4924.                 } else {
  4925. #ifdef HEIMDAL
  4926.                     switch ( k5_session_key->keytype ) {
  4927.                     case ETYPE_DES_CBC_CRC:
  4928.                     case ETYPE_DES_CBC_MD5:
  4929.                     case ETYPE_DES_CBC_MD4:
  4930.                         skey.type = SK_DES;
  4931.                         skey.length = 8;
  4932.                         break;
  4933.                     default:
  4934.                         skey.type = SK_GENERIC;
  4935.                         skey.length = k5_session_key->length;
  4936.                         encrypt_dont_support(ENCTYPE_DES_CFB64);
  4937.                         encrypt_dont_support(ENCTYPE_DES_OFB64);
  4938.                     }
  4939.                     skey.data = k5_session_key->keyvalue.data;
  4940. #else /* HEIMDAL */
  4941.                     switch ( k5_session_key->enctype ) {
  4942.                     case ENCTYPE_DES_CBC_CRC:
  4943.                     case ENCTYPE_DES_CBC_MD5:
  4944.                     case ENCTYPE_DES_CBC_MD4:
  4945.                         skey.type = SK_DES;
  4946.                         skey.length = 8;
  4947.                     default:
  4948.                         skey.type = SK_GENERIC;
  4949.                         skey.length = k5_session_key->length;
  4950.                         encrypt_dont_support(ENCTYPE_DES_CFB64);
  4951.                         encrypt_dont_support(ENCTYPE_DES_OFB64);
  4952.                     }
  4953.                     skey.data = k5_session_key->contents;
  4954. #endif /* HEIMDAL */
  4955.                 }
  4956.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  4957.             }
  4958. #endif /* CK_ENCRYPTION */
  4959.         }
  4960.         if ( cnt > 0 ) {
  4961.             char *s;
  4962.             int len;
  4963.             ckstrncpy(strTmp,"Kerberos V5 accepts you as ",sizeof(strTmp));
  4964.             len = strlen(strTmp);
  4965.             if ( len + cnt < sizeof(strTmp) ) {
  4966.                 s = strTmp + strlen(strTmp);
  4967.                 memcpy(s,data,cnt);
  4968.                 s[cnt] = 0;
  4969.             }
  4970.         }
  4971.         accept_complete = 1;
  4972.         printf("%s\r\n",strTmp);
  4973.  
  4974. #ifdef FORWARD
  4975.         if (forward_flag
  4976. #ifdef COMMENT
  4977.              /* Marc Horowitz <marc@mit.edu> has successfully argued
  4978.                 that it is indeed safe to send Forwarded credentials
  4979.                 to an untrusted host.
  4980.               */
  4981.              && (auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL
  4982. #endif /* COMMENT */
  4983.              )
  4984.             kerberos5_forward();
  4985. #endif /* FORWARD */
  4986.         krb5_errno = 0;
  4987.         makestr(&krb5_errmsg,strTmp);
  4988.         auth_finished(AUTH_USER);
  4989.         return AUTH_SUCCESS;
  4990.  
  4991.     case KRB5_RESPONSE:
  4992. #ifdef TLS_VERIFY
  4993.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  4994.             !krb5_tls_verified) {
  4995.             printf(
  4996.     "Man in the middle attack detected.  Session terminated.\r\n");
  4997. #ifndef BETATEST
  4998.             netclos();
  4999. #endif /* BETATEST */
  5000.             krb5_errno = -1;
  5001.             makestr(&krb5_errmsg,"TLS not verified");
  5002.             auth_finished(AUTH_REJECT);
  5003.             return AUTH_FAILURE;
  5004.         }
  5005.         if((ssl_active_flag || tls_active_flag) &&
  5006.             (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5007.             printf("TLS session parameters verified by Kerberos 5\r\n");
  5008.         }
  5009. #endif /* TLS_VERIFY */
  5010.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  5011.             /* the rest of the reply should contain a krb_ap_rep */
  5012.             krb5_ap_rep_enc_part *reply;
  5013.             krb5_data inbuf;
  5014.             krb5_error_code r;
  5015.  
  5016.             inbuf.length = cnt;
  5017.             inbuf.data = (char *)data;
  5018.  
  5019.             if (r = krb5_rd_rep(k5_context, auth_context, &inbuf, &reply)) {
  5020.                 com_err(NULL, r, "while authorizing. (5)");
  5021.                 krb5_errno = r;
  5022.                 makestr(&krb5_errmsg,error_message(krb5_errno));
  5023.                 auth_finished(AUTH_REJECT);
  5024.                 return AUTH_FAILURE;
  5025.             }
  5026.             krb5_free_ap_rep_enc_part(k5_context, reply);
  5027.  
  5028. #ifdef CK_ENCRYPTION
  5029.             if (encrypt_flag && k5_session_key) {
  5030.                 if ( tn_auth_krb5_des_bug ) {   /* !ALLOW_KRB_3DES_ENCRYPT */
  5031.                     skey.type = SK_DES;
  5032.                     skey.length = 8;
  5033. #ifdef HEIMDAL
  5034.                     skey.data = k5_session_key->keyvalue.data;
  5035. #else /* HEIMDAL */
  5036.                     skey.data = k5_session_key->contents;
  5037. #endif /* HEIMDAL */
  5038.                 } else {
  5039. #ifdef HEIMDAL
  5040.                     switch ( k5_session_key->keytype ) {
  5041.                     case ETYPE_DES_CBC_CRC:
  5042.                     case ETYPE_DES_CBC_MD5:
  5043.                     case ETYPE_DES_CBC_MD4:
  5044.                         skey.type = SK_DES;
  5045.                         skey.length = 8;
  5046.                     default:
  5047.                         skey.type = SK_GENERIC;
  5048.                         skey.length = k5_session_key->length;
  5049.                     }
  5050.                     skey.data = k5_session_key->keyvalue.data;
  5051. #else /* HEIMDAL */
  5052.                     switch ( k5_session_key->enctype ) {
  5053.                     case ENCTYPE_DES_CBC_CRC:
  5054.                     case ENCTYPE_DES_CBC_MD5:
  5055.                     case ENCTYPE_DES_CBC_MD4:
  5056.                         skey.type = SK_DES;
  5057.                         skey.length = 8;
  5058.                         break;
  5059.                     default:
  5060.                         skey.type = SK_GENERIC;
  5061.                         skey.length = k5_session_key->length;
  5062.                     }
  5063.                     skey.data = k5_session_key->contents;
  5064. #endif /* HEIMDAL */
  5065.                 }
  5066.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  5067.             }
  5068. #endif /* ENCRYPTION */
  5069.             mutual_complete = 1;
  5070.         }
  5071.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  5072.                   sizeof(strTmp));
  5073.         krb5_errno = 0;
  5074.         makestr(&krb5_errmsg,strTmp);
  5075.         printf("%s\r\n",strTmp);
  5076.         auth_finished(AUTH_USER);
  5077.         return AUTH_SUCCESS;
  5078.  
  5079. #ifdef FORWARD
  5080.     case KRB5_FORWARD_ACCEPT:
  5081.         forwarded_tickets = 1;
  5082.         ckstrncpy(strTmp,"Remote machine has accepted forwarded credentials",
  5083.                   sizeof(strTmp));
  5084.         krb5_errno = 0;
  5085.         makestr(&krb5_errmsg,strTmp);
  5086.         printf("%s\r\n",strTmp);
  5087.         return AUTH_SUCCESS;
  5088.  
  5089.     case KRB5_FORWARD_REJECT:
  5090.         forwarded_tickets = 0;
  5091.         if (cnt > 0) {
  5092.             char *s;
  5093.             int len;
  5094.             len = ckstrncpy(strTmp,
  5095.                       "Kerberos V5 refuses forwarded credentials because ",
  5096.                        sizeof(strTmp));
  5097.             if ( len + cnt < sizeof(strTmp) ) {
  5098.                 s = strTmp + strlen(strTmp);
  5099.                 memcpy(s, data, cnt);
  5100.                 s[cnt] = 0;
  5101.             }
  5102.         } else
  5103.             ckstrncpy(strTmp, "Kerberos V5 refuses forwarded credentials",
  5104.                       sizeof(strTmp));
  5105.  
  5106.         printf("%s\r\n",strTmp);
  5107.         krb5_errno = -1;
  5108.         makestr(&krb5_errmsg,strTmp);
  5109.         return AUTH_SUCCESS;
  5110. #endif  /* FORWARD */
  5111.  
  5112. #ifdef TLS_VERIFY
  5113.     case KRB5_TLS_VERIFY:
  5114.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5115.             krb5_data reply, msg;
  5116.             char tls_verify[24];
  5117.             krb5_replay_data repdata;
  5118.             krb5_error_code r;
  5119.  
  5120.             ssl_get_server_finished(&tls_verify[0],12);
  5121.             ssl_get_client_finished(&tls_verify[12],12);
  5122.  
  5123.             reply.data = (char *)data;
  5124.             reply.length = cnt;
  5125.  
  5126.             krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5127.                                    KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  5128.  
  5129.             if (r = krb5_rd_safe(k5_context,auth_context,&reply,&msg,&repdata))
  5130.               {
  5131.                 com_err("", r, "decoding tls verifier");
  5132.                 krb5_errno = r;
  5133.                 makestr(&krb5_errmsg,"TLS verify failure");
  5134.                 auth_finished(AUTH_REJECT);
  5135.                 return(AUTH_FAILURE);
  5136.             }
  5137.             if ( msg.length == 24 && !memcmp(msg.data,tls_verify,24) )
  5138.                  krb5_tls_verified = 1;
  5139.             krb5_free_data_contents(k5_context,&msg);
  5140.             if (krb5_tls_verified)
  5141.                 return(AUTH_SUCCESS);
  5142.         }
  5143.         printf("Man in the middle attack detected.  Session terminated.\r\n");
  5144.         netclos();
  5145.         krb5_errno = -1;
  5146.         makestr(&krb5_errmsg,"TLS verify failure");
  5147.         auth_finished(AUTH_REJECT);
  5148.         return(AUTH_FAILURE);
  5149. #endif /* CK_SSL */
  5150.  
  5151.     default:
  5152.         krb5_errno = -1;
  5153.         makestr(&krb5_errmsg,"Unknown reply type");
  5154.         auth_finished(AUTH_REJECT);
  5155.         return AUTH_FAILURE;                        /* Unknown reply type */
  5156.     }
  5157. }
  5158.  
  5159. #ifdef FORWARD
  5160. /* Decode, decrypt and store the forwarded creds in the local ccache. */
  5161. /* Needed for KRB5_FORWARD                                            */
  5162. static krb5_error_code
  5163. rd_and_store_for_creds(context, auth_context, inbuf, client)
  5164.     krb5_context context;
  5165.     krb5_auth_context auth_context;
  5166.     krb5_data *inbuf;
  5167.     krb5_const_principal client;
  5168. {
  5169.     krb5_creds ** creds=NULL;
  5170.     krb5_error_code retval;
  5171.     krb5_ccache ccache=NULL;
  5172.  
  5173. #ifdef HEIMDAL
  5174.     /*
  5175.     Heimdal Telnetd creates the cache file at this point and sets
  5176.     the KRB5CCNAME environment variable.
  5177.  
  5178.     struct passwd *pwd;
  5179.     char ccname[1024];
  5180.  
  5181.     pwd = getpwnam(szUserNameRequested);
  5182.     if (pwd == NULL)
  5183.         break;
  5184.     snprintf(ccname, sizeof(ccname)-1, "FILE:/tmp/krb5cc_%u",pwd->pw_uid);
  5185.     retval = krb5_cc_resolve(context,ccname,&ccache);
  5186.  
  5187.     chown(ccname + 5, pwd->pw_uid, -1);
  5188.     */
  5189. #endif /* HEIMDAL */
  5190.  
  5191.     if (retval = k5_get_ccache(context,&ccache,NULL))
  5192.         return(retval);
  5193.  
  5194. #ifdef HEIMDAL
  5195.     if ((retval = krb5_cc_initialize(context, ccache, client)))
  5196.         return(retval);
  5197.  
  5198.     if ((retval = krb5_rd_cred(context, auth_context, ccache, inbuf)))
  5199.         return(retval);
  5200. #else /* HEIMDAL */
  5201.     if ((retval = krb5_rd_cred(context, auth_context, inbuf, &creds, NULL)))
  5202.         return(retval);
  5203.  
  5204.     if ((retval = krb5_cc_initialize(context, ccache, creds[0]->client)))
  5205.         goto cleanup;
  5206.  
  5207.     if ((retval = krb5_cc_store_cred(context, ccache, creds[0])))
  5208.         goto cleanup;
  5209.  
  5210.     if ((retval = krb5_cc_close(context, ccache)))
  5211.         goto cleanup;
  5212.  
  5213.   cleanup:
  5214.     krb5_free_tgt_creds(context, creds);
  5215. #endif /* HEIMDAL */
  5216.     return retval;
  5217. }
  5218. #endif /* FORWARD */
  5219.  
  5220. /*
  5221.  *
  5222.  * K5_auth_is.
  5223.  *
  5224.  */
  5225.  
  5226. static int
  5227. #ifdef CK_ANSIC
  5228. k5_auth_is(int how, unsigned char *data, int cnt)
  5229. #else
  5230. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  5231. #endif
  5232. {
  5233.     int r = 0;
  5234.     krb5_principal server;
  5235.     krb5_keyblock *newkey = NULL;
  5236.     krb5_data outbuf;
  5237.     char errbuf[128]="";
  5238.     char *getenv();
  5239. #ifndef HEIMDAL
  5240.     krb5_authenticator *authenticator;
  5241.     krb5_keytab keytabid = 0;
  5242. #endif /* HEIMDAL */
  5243.     krb5_data inbuf;
  5244. #ifdef CK_ENCRYPTION
  5245.     Session_Key skey;
  5246. #endif /* CK_ENCRYPTION */
  5247.     char princ[256]="";
  5248.     int len;
  5249.  
  5250.     data += 4;                                  /* Point to status byte */
  5251.     cnt -= 4;
  5252.  
  5253.     hexdump("k5_auth_is data",data,cnt);
  5254.     debug(F111,"k5_auth_is","how",how);
  5255.  
  5256.     if (cnt-- < 1) {
  5257.         auth_finished(AUTH_REJECT);
  5258.         return AUTH_FAILURE;
  5259.     }
  5260.     switch (*data++) {
  5261.     case KRB_AUTH:
  5262.         k5_auth.data = (char *)data;
  5263.         k5_auth.length = cnt;
  5264.  
  5265.         debug(F110,"k5_auth_is","KRB_AUTH",0);
  5266.         debug(F111,"k5_auth_is","auth_context",auth_context);
  5267.  
  5268.         if (!r && !auth_context) {
  5269.             r = krb5_auth_con_init(k5_context, &auth_context);
  5270.             debug(F111,"k5_auth_is","krb5_auth_con_init",r);
  5271.         }
  5272.  
  5273. #ifdef HEIMDAL
  5274.         if (!r)
  5275.             r = krb5_auth_con_setaddrs_from_fd(k5_context,
  5276.                            auth_context,
  5277.                            &ttyfd);
  5278.         if (!r)
  5279.             r = krb5_sock_to_principal(k5_context,
  5280.                        0,
  5281.                        "host",
  5282.                                        KRB5_NT_SRV_HST,
  5283.                        &server);
  5284.         if (!r)
  5285. #else /* HEIMDAL */
  5286.         if (!r) {
  5287.             krb5_rcache rcache = NULL;
  5288.  
  5289.             r = krb5_auth_con_getrcache(k5_context, auth_context,
  5290.                                          &rcache);
  5291.             debug(F111,"k5_auth_is","krb5_auth_con_getrcache",r);
  5292.  
  5293.             if (!r && !rcache) {
  5294.                 /* Do not resolve server's principal name, we will check */
  5295.                 /* for validity after the krb5_rd_req() call.            */
  5296.                 r = krb5_sname_to_principal(k5_context, 0, 0,
  5297.                                              KRB5_NT_SRV_HST, &server);
  5298.                 debug(F111,"k5_auth_is","krb5_sname_to_principal",r);
  5299.  
  5300.                 if (!r) {
  5301.                     r = krb5_get_server_rcache(k5_context,
  5302.                         krb5_princ_component(k5_context, server, 0),
  5303.                                                 &rcache);
  5304.                     debug(F111,"k5_auth_is","krb5_get_server_rcache",r);
  5305.                     krb5_free_principal(k5_context, server);
  5306.                 }
  5307.             }
  5308.             if (!r) {
  5309.                 r = krb5_auth_con_setrcache(k5_context,
  5310.                                              auth_context, rcache);
  5311.                 debug(F111,"k5_auth_is","krb5_auth_con_setrcache",r);
  5312.             }
  5313.         }
  5314.         if (!r && k5_keytab) {
  5315.             r = krb5_kt_resolve(k5_context,
  5316.                                  k5_keytab, &keytabid);
  5317.             debug(F111,"k5_auth_is","krb5_kt_resolve",r);
  5318.         }
  5319. #endif /* HEIMDAL */
  5320.         if (!r) {
  5321.             r = krb5_rd_req(k5_context, &auth_context, &k5_auth,
  5322. #ifdef HEIMDAL
  5323.                              server, NULL, NULL,
  5324. #else /* HEIMDAL */
  5325.                              NULL, keytabid, NULL,
  5326. #endif /* HEIMDAL */
  5327.                              &k5_ticket);
  5328.             debug(F111,"k5_auth_is","krb5_rd_req",r);
  5329.         }
  5330.         if (r) {
  5331.             (void) ckstrncpy(errbuf, "krb5_rd_req failed: ",sizeof(errbuf));
  5332.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5333.             goto errout;
  5334.         }
  5335. #ifdef HEIMDAL
  5336.         krb5_free_principal(k5_context, server);
  5337.  
  5338.         {
  5339.             char type_check[26];
  5340.  
  5341.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5342.             /* client and then server finished messages. */
  5343.  
  5344.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5345.             type_check[1] = how;        /* not broken into parts */
  5346. #ifdef CK_SSL
  5347.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5348.                 ssl_get_client_finished(&type_check[2],12);
  5349.                 ssl_get_server_finished(&type_check[14],12);
  5350.                 hexdump("k5_auth_is type_check",type_check,26);
  5351.             }
  5352. #endif /* CK_SSL */
  5353.  
  5354.             r = krb5_verify_authenticator_checksum(k5_context,
  5355.                                                     auth_context,
  5356.                                                     type_check,
  5357. #ifdef CK_SSL
  5358.                 ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5359. #endif /* CK_SSL */
  5360.                                                     2);
  5361.         }
  5362. #else /* HEIMDAL */
  5363.         len = krb5_princ_component(k5_context,k5_ticket->server,0)->length;
  5364.         if (len < 256)
  5365.         {
  5366.             memcpy(princ,
  5367.                    krb5_princ_component(k5_context,k5_ticket->server,0)->data,
  5368.                    len);
  5369.             princ[len] = '\0';
  5370.         }
  5371.         if ( strcmp((krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME), princ) )
  5372.         {
  5373.             debug(F110,"k5_auth_is incorrect service name",princ,0);
  5374.             ckstrncpy(errbuf,"incorrect service name: ",sizeof(errbuf));
  5375.             ckstrncat(errbuf,krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5376.                      sizeof(errbuf));
  5377.             ckstrncat(errbuf," != ",sizeof(errbuf));
  5378.             ckstrncat(errbuf,princ,sizeof(errbuf));
  5379.             goto errout;
  5380.         }
  5381.  
  5382.         r = krb5_auth_con_getauthenticator(k5_context,
  5383.                                             auth_context,
  5384.                                             &authenticator);
  5385.         debug(F111,"k5_auth_is","krb5_auth_con_getauthenticator",r);
  5386.         if (r) {
  5387.             (void) ckstrncpy(errbuf,
  5388.                              "krb5_auth_con_getauthenticator failed: ",
  5389.                              sizeof(errbuf)
  5390.                              );
  5391.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5392.             goto errout;
  5393.         }
  5394.  
  5395.         if (authenticator->checksum) {
  5396.             char type_check[26];
  5397.             krb5_checksum *cksum = authenticator->checksum;
  5398.             krb5_keyblock *key;
  5399.  
  5400.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5401.             /* client and then server finished messages. */
  5402.  
  5403.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5404.             type_check[1] = how;        /* not broken into parts */
  5405. #ifdef CK_SSL
  5406.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5407.                 ssl_get_client_finished(&type_check[2],12);
  5408.                 ssl_get_server_finished(&type_check[14],12);
  5409.                 hexdump("k5_auth_is type_check",type_check,26);
  5410.             }
  5411. #endif /* CK_SSL */
  5412.  
  5413.             r = krb5_auth_con_getkey(k5_context, auth_context,
  5414.                                       &key);
  5415.             debug(F111,"k5_auth_is","krb5_auth_con_getkey",r);
  5416.             if (r) {
  5417.                 (void) ckstrncpy(errbuf, "krb5_auth_con_getkey failed: ",
  5418.                                   sizeof(errbuf));
  5419.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5420.                 goto errout;
  5421.             }
  5422.  
  5423.             r = krb5_verify_checksum(k5_context,
  5424.                                       cksum->checksum_type,
  5425.                                       cksum,
  5426.                                       &type_check,
  5427.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5428.                                       2,
  5429.                                       key->contents,
  5430.                                       key->length
  5431.                                       );
  5432.             debug(F111,"k5_auth_is","krb5_verify_checksum",r);
  5433.             if (r) {
  5434.                 (void) ckstrncpy(errbuf,
  5435.                                  "checksum verification failed: ",
  5436.                                  sizeof(errbuf)
  5437.                                  );
  5438.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5439.                 goto errout;
  5440.             }
  5441.             krb5_free_keyblock(k5_context, key);
  5442.         } else {
  5443.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT) {
  5444.                 (void) strcpy(errbuf,
  5445.                                "authenticator is missing required checksum");
  5446.                 goto errout;
  5447.             }
  5448.         }
  5449.  
  5450.         krb5_free_authenticator(k5_context, authenticator);
  5451. #endif /* HEIMDAL */
  5452.  
  5453. #ifdef TLS_VERIFY
  5454.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5455.             krb5_data in, msg;
  5456.             char tls_verify[24];
  5457.             krb5_replay_data repdata;
  5458.  
  5459.             ssl_get_server_finished(&tls_verify[0],12);
  5460.             ssl_get_client_finished(&tls_verify[12],12);
  5461.  
  5462.             in.data = tls_verify;
  5463.             in.length = 24;
  5464.  
  5465.             krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5466.                                    KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
  5467.             if (r = krb5_mk_safe(k5_context,auth_context,&in,&msg,&repdata)) {
  5468.                 com_err("", r, "encoding tls verifier");
  5469.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5470.                 goto errout;
  5471.             }
  5472.             SendK5AuthSB(KRB5_TLS_VERIFY, msg.data, msg.length);
  5473.             krb5_free_data_contents(k5_context,&msg);
  5474.         }
  5475. #endif /* CK_SSL */
  5476.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  5477.             /* do ap_rep stuff here */
  5478.             if ((r = krb5_mk_rep(k5_context,
  5479. #ifdef HEIMDAL
  5480.                                   &auth_context,
  5481. #else /* HEIMDAL */
  5482.                                   auth_context,
  5483. #endif /* HEIMDAL */
  5484.                                   &outbuf))) {
  5485.                 debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5486.                 (void) ckstrncpy(errbuf, "Make reply failed: ",sizeof(errbuf));
  5487.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5488.                 goto errout;
  5489.             }
  5490.             debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5491.  
  5492.             SendK5AuthSB(KRB5_RESPONSE, outbuf.data, outbuf.length);
  5493.             mutual_complete = 1;
  5494.         }
  5495.  
  5496. #ifdef HEIMDAL
  5497.         {
  5498.             char * name = NULL;
  5499.             if (krb5_unparse_name(k5_context, k5_ticket->client,
  5500.                                    &name))
  5501.             {
  5502.                 szUserNameAuthenticated[0] = '\0';
  5503.             } else {
  5504.                 ckstrncpy(szUserNameAuthenticated,UIDBUFLEN,name);
  5505.                 free(name);
  5506.             }
  5507.         }
  5508. #else /* HEIMDAL */
  5509.         if ( krb5_aname_to_localname(k5_context,
  5510.                                       k5_ticket->enc_part2->client,
  5511.                                       UIDBUFLEN,szUserNameAuthenticated) )
  5512.             szUserNameAuthenticated[0] = '\0';
  5513. #endif /* HEIMDAL */
  5514.  
  5515.         SendK5AuthSB(KRB_ACCEPT, szUserNameAuthenticated,
  5516.                       szUserNameAuthenticated[0] ? -1 : 0);
  5517.         accept_complete = 1;
  5518.         ckmakmsg(strTmp,sizeof(strTmp),
  5519.                  "Kerberos5 identifies him as ``",
  5520.                  szUserNameAuthenticated,"''",NULL);
  5521.         printf("%s\r\n",strTmp);
  5522.  
  5523.         if (szUserNameRequested[0] &&
  5524.             krb5_kuserok(k5_context,
  5525. #ifdef HEIMDAL
  5526.                           k5_ticket->client,
  5527. #else /* HEIMDAL */
  5528.                           k5_ticket->enc_part2->client,
  5529. #endif /* HEIMDAL */
  5530.                           szUserNameRequested))
  5531.             auth_finished(AUTH_VALID);
  5532.         else
  5533.             auth_finished(AUTH_USER);
  5534.  
  5535.         krb5_auth_con_getremotesubkey(k5_context, auth_context,
  5536.                                        &newkey);
  5537.         if (k5_session_key) {
  5538.             krb5_free_keyblock(k5_context, k5_session_key);
  5539.             k5_session_key = 0;
  5540.         }
  5541.         if (newkey) {
  5542.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  5543.             krb5_free_keyblock(k5_context, newkey);
  5544.         } else {
  5545.             krb5_copy_keyblock(k5_context,
  5546. #ifdef HEIMDAL
  5547.                                 &k5_ticket->ticket.key,
  5548. #else /* HEIMDAL */
  5549.                                 k5_ticket->enc_part2->session,
  5550. #endif /* HEIMDAL */
  5551.                                 &k5_session_key);
  5552.         }
  5553.  
  5554. #ifdef CK_ENCRYPTION
  5555. #ifdef HEIMDAL
  5556.         skey.type = k5_session_key->keyvalue.length == 8 ? SK_DES : SK_GENERIC;
  5557.         skey.length = k5_session_key->keyvalue.length;
  5558.         skey.data = k5_session_key->keyvalue.data;
  5559. #else /* HEIMDAL */
  5560.         skey.type = k5_session_key->length == 8 ? SK_DES : SK_GENERIC;
  5561.         skey.length = k5_session_key->length;
  5562.         skey.data = k5_session_key->contents;
  5563. #endif /* HEIMDAL */
  5564.         encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  5565. #endif /* CK_ENCRYPTION */
  5566.         debug(F100,"k5_auth_is AUTH_SUCCESS","",0);
  5567.         krb5_errno = r;
  5568.         if ( krb5_errno )
  5569.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5570.         else
  5571.             makestr(&krb5_errmsg,strTmp);
  5572.         return AUTH_SUCCESS;
  5573.  
  5574. #ifdef FORWARD
  5575.     case KRB5_FORWARD:
  5576.         if ( !forward_flag ) {
  5577.             SendK5AuthSB(KRB5_FORWARD_REJECT,
  5578.                           "forwarded credentials are being refused.",
  5579.                           -1);
  5580.             return(AUTH_SUCCESS);
  5581.         }
  5582.  
  5583.         inbuf.length = cnt;
  5584.         inbuf.data = (char *)data;
  5585.         if (
  5586. #ifndef HEIMDAL
  5587.             (!krb5_d_no_addresses &&
  5588.             (r = krb5_auth_con_genaddrs(k5_context,auth_context,g_kstream->fd,
  5589.                               KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))) ||
  5590. #endif /* HEIMDAL */
  5591.             (r = rd_and_store_for_creds(k5_context, auth_context,&inbuf,
  5592. #ifdef HEIMDAL
  5593.                                          k5_ticket->client
  5594. #else /* HEIMDAL */
  5595.                                          k5_ticket->enc_part2->client
  5596. #endif /* HEIMDAL */
  5597.                                          ))) {
  5598.             (void) ckstrncpy(errbuf, "Read forwarded creds failed: ",
  5599.                               sizeof(errbuf));
  5600.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5601.             SendK5AuthSB(KRB5_FORWARD_REJECT, errbuf, -1);
  5602.             printf("Could not read forwarded credentials\r\n");
  5603.             krb5_errno = r;
  5604.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5605.         }
  5606.         else {
  5607.             SendK5AuthSB(KRB5_FORWARD_ACCEPT, 0, 0);
  5608.             ckstrncpy(strTmp,"Forwarded credentials obtained",sizeof(strTmp));
  5609.             printf("%s\r\n",strTmp);
  5610.             krb5_errno = 0;
  5611.             makestr(&krb5_errmsg,strTmp);
  5612.         }
  5613.         /* A failure to accept forwarded credentials is not an */
  5614.         /* authentication failure.                             */
  5615.         return AUTH_SUCCESS;
  5616. #endif  /* FORWARD */
  5617.     default:
  5618.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  5619.         SendK5AuthSB(KRB_REJECT, 0, 0);
  5620.         break;
  5621.     }
  5622.     auth_finished(AUTH_REJECT);
  5623.     return AUTH_FAILURE;
  5624.  
  5625.   errout:
  5626.     SendK5AuthSB(KRB_REJECT, errbuf, -1);
  5627.     krb5_errno = r;
  5628.     makestr(&krb5_errmsg,errbuf);
  5629.     printf("%s\r\n", errbuf);
  5630.     if (auth_context) {
  5631.         krb5_auth_con_free(k5_context, auth_context);
  5632.         auth_context = 0;
  5633.     }
  5634.     auth_finished(AUTH_REJECT);
  5635.     return AUTH_FAILURE;
  5636. }
  5637.  
  5638. #ifdef FORWARD
  5639. int
  5640. #ifdef CK_ANSIC
  5641. kerberos5_forward(void)
  5642. #else
  5643. kerberos5_forward()
  5644. #endif
  5645. {
  5646.     krb5_error_code r;
  5647.     krb5_ccache ccache=NULL;
  5648.     krb5_principal client = 0;
  5649.     krb5_principal server = 0;
  5650.     krb5_data forw_creds;
  5651. #ifdef HEIMDAL
  5652.     krb5_creds      creds;
  5653. #endif /* HEIMDAL */
  5654.  
  5655.     forw_creds.data = 0;
  5656.  
  5657.     r = k5_get_ccache(k5_context,&ccache,NULL);
  5658.     if ( r ) {
  5659.         com_err(NULL, r, "Kerberos V5: could not get default ccache");
  5660.         krb5_errno = r;
  5661.         makestr(&krb5_errmsg,error_message(krb5_errno));
  5662.         return(AUTH_FAILURE);
  5663.     }
  5664.  
  5665.     if ((r = krb5_cc_get_principal(k5_context, ccache, &client))) {
  5666.         com_err(NULL, r, "Kerberos V5: could not get default principal");
  5667.         goto cleanup;
  5668.     }
  5669.  
  5670. #ifdef HEIMDAL
  5671.     memset(&creds, 0, sizeof(creds));
  5672.     creds.client = client;
  5673.  
  5674.     if (r = krb5_build_principal(k5_context,
  5675.                              &creds.server,
  5676.                              strlen(client->realm),
  5677.                               client->realm,
  5678.                               "krbtgt",
  5679.                               client->realm,
  5680.                                   NULL)) {
  5681.         com_err(NULL, r, "Kerberos V5: could not get principal");
  5682.         goto cleanup;
  5683.     }
  5684.  
  5685.     creds.times.endtime = 0;
  5686.  
  5687.     if (r = krb5_get_forwarded_creds(k5_context,
  5688.                                       auth_context,
  5689.                                       ccache,
  5690.                                       0,
  5691.                                       szHostName,
  5692.                                       &creds,
  5693.                                       &forw_creds)) {
  5694.         com_err(NULL, r, "Kerberos V5: error getting forwarded creds");
  5695.         goto cleanup;
  5696.     }
  5697. #else /* HEIMDAL */
  5698.     /* we should not need to make this call since we are storing the */
  5699.     /* server's principal in fwd_server from our call to             */
  5700.     /* krb5_sname_to_principal() in k5_auth_send()                   */
  5701.     if (fwd_server == NULL) {
  5702.         if ((r = krb5_sname_to_principal(k5_context, szHostName,
  5703.                                  krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5704.                                           KRB5_NT_SRV_HST, &server))) {
  5705.             com_err(NULL, r, "Kerberos V5: could not make server principal");
  5706.             goto cleanup;
  5707.         }
  5708.     }
  5709.  
  5710.     if (!krb5_d_no_addresses &&
  5711.         (r = krb5_auth_con_genaddrs(k5_context, auth_context, g_kstream->fd,
  5712.                              KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
  5713.     {
  5714.         com_err(NULL, r, "Kerberos V5: could not gen local full address");
  5715.         goto cleanup;
  5716.     }
  5717.  
  5718.     if (r = krb5_fwd_tgt_creds(k5_context, auth_context, 0, client,
  5719.                                 fwd_server ? fwd_server : server,
  5720.                                 ccache, forwardable_flag, &forw_creds)) {
  5721.         com_err(NULL, r, "Kerberos V5: error getting forwardable credentials");
  5722.         goto cleanup;
  5723.     }
  5724. #endif /* HEIMDAL */
  5725.  
  5726.     /* Send forwarded credentials */
  5727.     if (!SendK5AuthSB(KRB5_FORWARD, forw_creds.data, forw_creds.length)) {
  5728.         printf("Kerberos V5 forwarding error!\r\n%s\r\n",
  5729.                     "Not enough room for authentication data");
  5730.     }
  5731.  
  5732. cleanup:
  5733.     if (client)
  5734.         krb5_free_principal(k5_context, client);
  5735.     if (server)
  5736.         krb5_free_principal(k5_context, server);
  5737. #ifdef HEIMDAL
  5738.     krb5_data_free(&forw_creds);
  5739. #else /* HEIMDAL */
  5740.     krb5_free_data_contents(k5_context,&forw_creds);
  5741. #endif /* HEIMDAL */
  5742.     krb5_cc_close(k5_context, ccache);
  5743.  
  5744.     krb5_errno = r;
  5745.     makestr(&krb5_errmsg,krb5_errno?error_message(krb5_errno):"OK");
  5746.     return(r?AUTH_FAILURE:AUTH_SUCCESS);
  5747. }
  5748. #endif /* FORWARD */
  5749. #else /* KRB5 */
  5750. int
  5751. ck_krb5_autoget_TGT(char * dummy)
  5752. {
  5753.     return(0);
  5754. }
  5755. #ifdef CK_KERBEROS
  5756. int
  5757. #ifdef CK_ANSIC
  5758. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  5759.                  struct krb4_init_data * k4_init)
  5760. #else
  5761. ck_krb5_initTGT(op,init,k4_init)
  5762.     krb_op_data * op; struct krb5_init_data * init;
  5763.     struct krb4_init_data * k4_init;
  5764. #endif /* CK_ANSIC*/
  5765. {
  5766.     return(-1);
  5767. }
  5768.  
  5769. int
  5770. #ifdef CK_ANSIC
  5771. ck_krb5_destroy(struct krb_op_data * op)
  5772. #else
  5773. ck_krb5_destroy(op) struct krb_op_data * op;
  5774. #endif
  5775. {
  5776.     return(-1);
  5777. }
  5778.  
  5779. int
  5780. #ifdef CK_ANSIC
  5781. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  5782. #else
  5783. ck_krb5_list_creds(op,lc)
  5784.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  5785. #endif
  5786. {
  5787.     return(-1);
  5788. }
  5789. #else /* CK_KERBEROS */
  5790. int
  5791. #ifdef CK_ANSIC
  5792. ck_krb5_initTGT(void * op, void * init, void * k4_init )
  5793. #else
  5794. ck_krb5_initTGT(op,init,k4_init)
  5795.     void * op; void * init; void * k4_init;
  5796. #endif /* CK_ANSIC*/
  5797. {
  5798.     return(-1);
  5799. }
  5800.  
  5801. int
  5802. #ifdef CK_ANSIC
  5803. ck_krb5_destroy(void * op)
  5804. #else
  5805. ck_krb5_destroy(op) void * op;
  5806. #endif
  5807. {
  5808.     return(-1);
  5809. }
  5810.  
  5811. int
  5812. #ifdef CK_ANSIC
  5813. ck_krb5_list_creds(void * op, void * lc)
  5814. #else
  5815. ck_krb5_list_creds(op,lc)
  5816.     void * op; void * lc;
  5817. #endif
  5818. {
  5819.     return(-1);
  5820. }
  5821. #endif /* CK_KERBEROS */
  5822. #endif /* KRB5 */
  5823.  
  5824. #ifdef GSSAPI_KRB5
  5825. /*
  5826.  *
  5827.  * gssk5_auth_send - gets authentication bits we need to send to KDC.
  5828.  *
  5829.  * Result is left in k5_auth
  5830.  *
  5831.  * Returns: 0 on failure, 1 on success
  5832.  *
  5833.  */
  5834.  
  5835. static int
  5836. #ifdef CK_ANSIC
  5837. gssk5_auth_send(int how, int encrypt, int forward)
  5838. #else
  5839. gssk5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  5840. #endif
  5841. {
  5842.     OM_uint32 maj_stat, min_stat;
  5843. #ifdef KRB5
  5844.     char * realm = NULL;
  5845.     char tgt[256];
  5846. #endif /* KRB5 */
  5847.  
  5848.     gss_chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  5849.     gss_chan.initiator_address.length = 4;
  5850.     gss_chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  5851.     gss_chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  5852.     gss_chan.acceptor_address.length = 4;
  5853.     gss_chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  5854.     gss_chan.application_data.length = 0;
  5855.     gss_chan.application_data.value = 0;
  5856.  
  5857. #ifdef KRB5
  5858.     realm = ck_krb5_realmofhost(ftp_host);
  5859.     if (realm) {
  5860.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  5861.         debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  5862.         if ( krb5_autoget &&
  5863.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  5864.                 (ck_krb5_is_tgt_valid() > 0)) )
  5865.             ck_krb5_autoget_TGT(realm);
  5866.     }
  5867. #endif /* KRB5 */
  5868.  
  5869.     /* Blob from gss-client */
  5870.     /* host@hostname */
  5871.     /* the V5 GSSAPI binding canonicalizes this for us... */
  5872.     ckmakmsg(gss_stbuf,GSS_BUFSIZ,
  5873.              krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5874.              "@",
  5875.              szHostName,
  5876.               NULL
  5877.               );
  5878.     fprintf(stderr, "Authenticating to <%s>...\n", gss_stbuf);
  5879.     gss_send_tok.value = gss_stbuf;
  5880.     gss_send_tok.length = strlen(gss_stbuf);
  5881.     maj_stat = gss_import_name(&min_stat, &gss_send_tok,
  5882.                                 gss_nt_service_name,
  5883.                                 &gss_target_name
  5884.                                 );
  5885.     if (maj_stat != GSS_S_COMPLETE) {
  5886.         user_gss_error(maj_stat, min_stat, "parsing name");
  5887.         secure_error("name parsed <%s>\n", gss_stbuf);
  5888.         return(0);
  5889.     }
  5890.     token_ptr = GSS_C_NO_BUFFER;
  5891.     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  5892.  
  5893.     fprintf(stderr, "calling gss_init_sec_context\n");
  5894.     maj_stat =
  5895.         gss_init_sec_context(&min_stat,
  5896.                               GSS_C_NO_CREDENTIAL,
  5897.                               &gcontext,
  5898.                               gss_target_name,
  5899.                               gss_mech_krb5,
  5900.                               GSS_C_MUTUAL_FLAG |
  5901.                               GSS_C_REPLAY_FLAG |
  5902.                               ((forward && forward_flag) ?
  5903.                                 GSS_C_DELEG_FLAG : 0),
  5904.                               0,
  5905.                               (krb5_d_no_addresses ? /* channel bindings */
  5906.                                 GSS_C_NO_CHANNEL_BINDINGS :
  5907.                                 &gss_chan),
  5908.                               gss_token_ptr,
  5909.                               NULL,     /* ignore mech type */
  5910.                               &gss_send_tok,
  5911.                               NULL,     /* ignore ret_flags */
  5912.                               NULL
  5913.                               );        /* ignore time_rec */
  5914.  
  5915.  
  5916.         if (maj_stat != GSS_S_COMPLETE &&
  5917.              maj_stat != GSS_S_CONTINUE_NEEDED) {
  5918.             user_gss_error(maj_stat,
  5919.                             min_stat,
  5920.                             "initializing context"
  5921.                             );
  5922.             gss_release_name(&min_stat, &gss_target_name);
  5923.             return(0);
  5924.         }
  5925.         return(1);
  5926. }
  5927.  
  5928. /*
  5929.  * gssk5_auth_reply -- checks the reply for mutual authentication.
  5930.  */
  5931. static int
  5932. #ifdef CK_ANSIC
  5933. gssk5_auth_reply(int how, unsigned char *data, int cnt)
  5934. #else
  5935. gssk5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  5936. #endif
  5937. {
  5938.     data += 4;                                  /* Point to status byte */
  5939.     cnt -=5;
  5940.  
  5941.     switch (*data++) {
  5942.     case GSS_REJECT:
  5943.         if (cnt > 0) {
  5944.             char *s;
  5945.             int len;
  5946.             ckstrncpy(strTmp,"GSSAPI refuses authentication because\r\n",
  5947.                       sizeof(strTmp));
  5948.             len = strlen(strTmp);
  5949.             if ( len + cnt < sizeof(strTmp) ) {
  5950.                 s = strTmp + strlen(strTmp);
  5951.                 memcpy(s, data, cnt);           /* safe */
  5952.                 s[cnt] = 0;
  5953.             }
  5954.         } else
  5955.             ckstrncpy(strTmp,"GSSAPI refuses authentication",
  5956.                       sizeof(strTmp));
  5957.         printf("GSSAPI authentication failed!\r\n%s\r\n",strTmp);
  5958.         auth_finished(AUTH_REJECT);
  5959.         return AUTH_FAILURE;
  5960.  
  5961.     case GSS_ACCEPT:
  5962.         if ( cnt > 0 ) {
  5963.             char *s;
  5964.             int len;
  5965.             ckstrncpy(strTmp,"GSSAPI accepts you as ",sizeof(strTmp));
  5966.             len = strlen(strTmp);
  5967.             if ( len + cnt < sizeof(strTmp) ) {
  5968.                 s = strTmp + strlen(strTmp);
  5969.                 memcpy(s,data,cnt);
  5970.                 s[cnt] = 0;
  5971.             }
  5972.         }
  5973.         accept_complete = 1;
  5974.         printf("%s\r\n",strTmp);
  5975.         auth_finished(AUTH_USER);
  5976.         return AUTH_SUCCESS;
  5977.  
  5978.     case GSS_RESPONSE:
  5979.         gss_token_ptr = &gss_recv_tok;
  5980.         gss_recv_tok.value = data;
  5981.         gss_recv_tok.length = cnt;
  5982.  
  5983.         maj_stat =
  5984.             gss_init_sec_context(&min_stat,
  5985.                                   GSS_C_NO_CREDENTIAL,
  5986.                                   &gcontext,
  5987.                                   gss_target_name,
  5988.                                   gss_krb5_mech,
  5989.                                   GSS_C_MUTUAL_FLAG |
  5990.                                   GSS_C_REPLAY_FLAG |
  5991.                                   (forward_flag ?
  5992.                                     GSS_C_DELEG_FLAG : 0),
  5993.                                   0,
  5994.                                   (krb5_d_no_addresses ? /* channel bindings */
  5995.                                     GSS_C_NO_CHANNEL_BINDINGS :
  5996.                                     &gss_chan),
  5997.                                   gss_token_ptr,
  5998.                                   NULL, /* ignore mech type */
  5999.                                   &gss_send_tok,
  6000.                                   NULL, /* ignore ret_flags */
  6001.                                   NULL
  6002.                                   );    /* ignore time_rec */
  6003.  
  6004.         if ( maj_stat == GSS_S_COMPLETE )
  6005.         {
  6006.  
  6007.         } else if ( maj_stat == CSS_S_CONTINUE_NEEDED ) {
  6008.         } else {
  6009.         }
  6010.  
  6011.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  6012.                   sizeof(strTmp));
  6013.         printf("%s\r\n",strTmp);
  6014.         auth_finished(AUTH_USER);
  6015.         return AUTH_SUCCESS;
  6016.  
  6017.     default:
  6018.         auth_finished(AUTH_REJECT);
  6019.         return AUTH_FAILURE;                        /* Unknown reply type */
  6020.     }
  6021. }
  6022.  
  6023. /*
  6024.  *
  6025.  * gssk5_auth_is.
  6026.  *
  6027.  */
  6028.  
  6029. static int
  6030. #ifdef CK_ANSIC
  6031. k5_auth_is(int how, unsigned char *data, int cnt)
  6032. #else
  6033. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6034. #endif
  6035. {
  6036.     int replied = 0;
  6037.     gss_cred_id_t server_creds, deleg_creds;
  6038.     gss_name_t client;
  6039.     int ret_flags;
  6040.     gss_buffer_desc name_buf;
  6041.     gss_name_t server_name;
  6042.     OM_uint32 acquire_maj,
  6043.       acquire_min,
  6044.       accept_maj,
  6045.       accept_min,
  6046.       stat_maj,
  6047.       stat_min;
  6048.     gss_OID mechid;
  6049.     gss_buffer_desc tok, out_tok;
  6050.     char gbuf[GSS_BUFSIZ];
  6051.     u_char gout_buf[GSS_BUFSIZ];
  6052.     char localname[MAXHOSTNAMELEN];
  6053.     char service_name[MAXHOSTNAMELEN+10];
  6054.     char **service;
  6055.     struct hostent *hp;
  6056.  
  6057.     data += 4;                                  /* Point to status byte */
  6058.     cnt -= 4;
  6059.  
  6060.     hexdump("gssk5_auth_is data",data,cnt);
  6061.     debug(F111,"gssk5_auth_is","how",how);
  6062.  
  6063.     if (cnt-- < 1) {
  6064.         auth_finished(AUTH_REJECT);
  6065.         return AUTH_FAILURE;
  6066.     }
  6067.     switch (*data++) {
  6068.     case GSS_AUTH:
  6069.         gss_chan.initiator_addrtype = GSS_C_AF_INET;
  6070.         gss_chan.initiator_address.length = 4;
  6071.         gss_chan.initiator_address.value = &his_addr.sin_addr.s_addr;
  6072.         gss_chan.acceptor_addrtype = GSS_C_AF_INET;
  6073.         gss_chan.acceptor_address.length = 4;
  6074.         gss_chan.acceptor_address.value = &ctrl_addr.sin_addr.s_addr;
  6075.         gss_chan.application_data.length = 0;
  6076.         gss_chan.application_data.value = 0;
  6077.  
  6078.         tok.value = data;
  6079.         tok.length = cnt;
  6080.  
  6081.         if (gethostname(localname, MAXHOSTNAMELEN)) {
  6082.             auth_finished(AUTH_REJECT);
  6083.             return AUTH_FAILURE;
  6084.         }
  6085.         if (!(hp = gethostbyname(localname))) {
  6086.             auth_finished(AUTH_REJECT);
  6087.             return AUTH_FAILURE;
  6088.         }
  6089. #ifdef HADDRLIST
  6090.         hp = ck_copyhostent(hp);
  6091. #endif /* HADDRLIST */
  6092.         strncpy(localname, hp->h_name, sizeof(localname) - 1);
  6093.         localname[sizeof(localname) - 1] = '\0';
  6094.  
  6095.         sprintf(service_name, "%s@%s", *service, localname);
  6096.         name_buf.value = service_name;
  6097.         name_buf.length = strlen(name_buf.value) + 1;
  6098.         stat_maj = gss_import_name(&stat_min, &name_buf,
  6099.                                     gss_nt_service_name,
  6100.                                     &server_name);
  6101.         if (stat_maj != GSS_S_COMPLETE) {
  6102.             auth_finished(AUTH_REJECT);
  6103.             return AUTH_FAILURE;
  6104.         }
  6105.  
  6106.         acquire_maj = gss_acquire_cred(&acquire_min, server_name, 0,
  6107.                                         GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
  6108.                                         &server_creds, NULL, NULL);
  6109.         (void) gss_release_name(&stat_min, &server_name);
  6110.  
  6111.         if (acquire_maj != GSS_S_COMPLETE) {
  6112.             reply_gss_error(535, accept_maj, accept_min,
  6113.                                  "accepting context");
  6114.             syslog(LOG_ERR, "failed accepting context");
  6115.             (void) gss_release_cred(&stat_min, &server_creds);
  6116.             if (ret_flags & GSS_C_DELEG_FLAG)
  6117.                 (void) gss_release_cred(&stat_min,
  6118.                                          &deleg_creds);
  6119.             return 0;
  6120.         }
  6121.  
  6122.         gcontext = GSS_C_NO_CONTEXT;
  6123.         accept_maj = gss_accept_sec_context(&accept_min,
  6124.                                             &gcontext, /* context_handle */
  6125.                                             /* verifier_cred_handle */
  6126.                                             server_creds,
  6127.                                             &tok, /* input_token */
  6128.                         (krb5_d_no_addresses ?
  6129.                          /* channel bindings */
  6130.                                                GSS_C_NO_CHANNEL_BINDINGS :
  6131.                                                &gss_chan),
  6132.                                              &client, /* src_name */
  6133.                                             &mechid, /* mech_type */
  6134.                                             &out_tok, /* output_token */
  6135.                                             &ret_flags,
  6136.                                             NULL,       /* ignore time_rec */
  6137.                                             /* forwarded credentials */
  6138.                                             &deleg_creds
  6139.                                             );
  6140.  
  6141.         if (accept_maj!=GSS_S_COMPLETE && accept_maj!=GSS_S_CONTINUE_NEEDED) {
  6142.             reply_gss_error(535, accept_maj, accept_min,
  6143.                              "accepting context");
  6144.             syslog(LOG_ERR, "failed accepting context");
  6145.             (void) gss_release_cred(&stat_min, &server_creds);
  6146.             if (ret_flags & GSS_C_DELEG_FLAG)
  6147.                 (void) gss_release_cred(&stat_min,
  6148.                                          &deleg_creds);
  6149.             return 0;
  6150.         }
  6151.  
  6152.         if (out_tok.length) {
  6153.             if (kerror = radix_encode(out_tok.value,gbuf,&out_tok.length, 0)) {
  6154.                 secure_error("Couldn't encode ADAT reply (%s)",
  6155.                              radix_error(kerror));
  6156.                 syslog(LOG_ERR, "couldn't encode ADAT reply");
  6157.                 (void) gss_release_cred(&stat_min, &server_creds);
  6158.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6159.                         (void) gss_release_cred(&stat_min,
  6160.                                                 &deleg_creds);
  6161.                 return(0);
  6162.             }
  6163.             if (stat_maj == GSS_S_COMPLETE) {
  6164.                 reply(235, "ADAT=%s", gbuf);
  6165.                 replied = 1;
  6166.             } else {
  6167.                 /* If the server accepts the security data, and
  6168.                    requires additional data, it should respond
  6169.                    with reply code 335. */
  6170.                 reply(335, "ADAT=%s", gbuf);
  6171.             }
  6172.             (void) gss_release_buffer(&stat_min, &out_tok);
  6173.         }
  6174.  
  6175.         if (stat_maj == GSS_S_COMPLETE) {
  6176.             /* GSSAPI authentication succeeded */
  6177.             stat_maj = gss_display_name(&stat_min, client,
  6178.                                          &client_name, &mechid);
  6179.             if (stat_maj != GSS_S_COMPLETE) {
  6180.                 /* "If the server rejects the security data (if
  6181.                    a checksum fails, for instance), it should
  6182.                    respond with reply code 535." */
  6183.                 reply_gss_error(535, stat_maj, stat_min,
  6184.                                 "extracting GSSAPI identity name");
  6185.                 syslog(LOG_ERR, "gssapi error extracting identity");
  6186.                 (void) gss_release_cred(&stat_min, &server_creds);
  6187.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6188.                         (void) gss_release_cred(&stat_min,
  6189.                                                 &deleg_creds);
  6190.                 return 0;
  6191.             }
  6192.             auth_type = temp_auth_type;
  6193.             temp_auth_type = NULL;
  6194.  
  6195.             (void) gss_release_cred(&stat_min, &server_creds);
  6196.             if (ret_flags & GSS_C_DELEG_FLAG) {
  6197.                 if (want_creds)
  6198.                     ftpd_gss_convert_creds(client_name.value,
  6199.                                             deleg_creds);
  6200.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6201.             }
  6202.  
  6203.             /* If the server accepts the security data, but does
  6204.                not require any additional data (i.e., the security
  6205.                data exchange has completed successfully), it must
  6206.                respond with reply code 235. */
  6207.             if (!replied)
  6208.             {
  6209.                 if (ret_flags & GSS_C_DELEG_FLAG && !have_creds)
  6210.                   reply(235,
  6211.  "GSSAPI Authentication succeeded, but could not accept forwarded credentials"
  6212.                         );
  6213.                 else
  6214.                   reply(235, "GSSAPI Authentication succeeded");
  6215.             }
  6216.             return(1);
  6217.         } else if (stat_maj == GSS_S_CONTINUE_NEEDED) {
  6218.             /* If the server accepts the security data, and
  6219.             requires additional data, it should respond with
  6220.             reply code 335. */
  6221.             reply(335, "more data needed");
  6222.             (void) gss_release_cred(&stat_min, &server_creds);
  6223.             if (ret_flags & GSS_C_DELEG_FLAG)
  6224.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6225.             return(0);
  6226.         } else {
  6227.             /* "If the server rejects the security data (if
  6228.             a checksum fails, for instance), it should
  6229.             respond with reply code 535." */
  6230.             reply_gss_error(535, stat_maj, stat_min,
  6231.                              "GSSAPI failed processing ADAT");
  6232.             syslog(LOG_ERR, "GSSAPI failed processing ADAT");
  6233.             (void) gss_release_cred(&stat_min, &server_creds);
  6234.             if (ret_flags & GSS_C_DELEG_FLAG)
  6235.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6236.             return(0);
  6237.         }
  6238.  
  6239.         debug(F100,"gssk5_auth_is AUTH_SUCCESS","",0);
  6240.         krb5_errno = r;
  6241.         if ( krb5_errno )
  6242.             makestr(&krb5_errmsg,error_message(krb5_errno));
  6243.         else
  6244.             makestr(&krb5_errmsg,strTmp);
  6245.         return AUTH_SUCCESS;
  6246.  
  6247.     default:
  6248.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  6249.         SendGSSK5AuthSB(GSS_REJECT, 0, 0);
  6250.         break;
  6251.     }
  6252.     auth_finished(AUTH_REJECT);
  6253.     return AUTH_FAILURE;
  6254. }
  6255. #endif /* GSSAPI_KRB5 */
  6256.  
  6257. #ifdef CK_SRP
  6258. /*
  6259.  * Copyright (c) 1997 Stanford University
  6260.  *
  6261.  * The use of this software for revenue-generating purposes may require a
  6262.  * license from the owners of the underlying intellectual property.
  6263.  * Specifically, the SRP-3 protocol may not be used for revenue-generating
  6264.  * purposes without a license.
  6265.  *
  6266.  * NOTE: Columbia University has a license.
  6267.  *
  6268.  * Within that constraint, permission to use, copy, modify, and distribute
  6269.  * this software and its documentation for any purpose is hereby granted
  6270.  * without fee, provided that the above copyright notices and this permission
  6271.  * notice appear in all copies of the software and related documentation.
  6272.  *
  6273.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  6274.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  6275.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  6276.  *
  6277.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  6278.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  6279.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  6280.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  6281.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  6282.  */
  6283.  
  6284. static void
  6285. srp_encode_length(data, num)
  6286.     unsigned char * data;
  6287.     int num;
  6288. {
  6289.     *data = (num >> 8) & 0xff;
  6290.     *++data = num & 0xff;
  6291. }
  6292.  
  6293. static int
  6294. srp_decode_length(data)
  6295.     unsigned char * data;
  6296. {
  6297.     return (((int) *data & 0xff) << 8) | (*(data + 1) & 0xff);
  6298. }
  6299.  
  6300. #ifdef PRE_SRP_1_7_3
  6301. static int
  6302. #ifdef CK_ANSIC
  6303. srp_reply(int how, unsigned char *data, int cnt)
  6304. #else
  6305. srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6306. #endif
  6307. {
  6308.     struct t_num n;
  6309.     struct t_num g;
  6310.     struct t_num s;
  6311.     struct t_num B;
  6312.     struct t_num * A;
  6313.     char type_check[26];
  6314.     int pflag;
  6315.  
  6316. #ifdef CK_ENCRYPTION
  6317.     Session_Key skey;
  6318. #endif /* ENCRYPTION */
  6319.  
  6320.     char * str=NULL;
  6321.  
  6322.     data += 4;                          /* Point to status byte */
  6323.     cnt  -= 4;
  6324.  
  6325.     if(cnt-- < 1) {
  6326.         auth_finished(AUTH_REJECT);
  6327.         return AUTH_FAILURE;
  6328.     }
  6329.  
  6330.     switch(*data++) {
  6331.     case SRP_REJECT:
  6332.         ckmakmsg(strTmp,sizeof(strTmp),
  6333.                   "SRP refuses authentication for '",szUserName,
  6334.                   "'\r\n",NULL);
  6335.         if (cnt > 0) {
  6336.             int len = strlen(strTmp);
  6337.             if ( len + cnt < sizeof(strTmp) ) {
  6338.                 str = strTmp + strlen(strTmp);
  6339.                 memcpy(str,data,cnt);
  6340.                 str[cnt] = 0;
  6341.             }
  6342.         }
  6343.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6344.         if (tc != NULL) {
  6345.             t_clientclose(tc);
  6346.             tc = NULL;
  6347.         }
  6348.         auth_finished(AUTH_REJECT);
  6349.         return AUTH_FAILURE;
  6350.  
  6351.     case SRP_ACCEPT:
  6352.         if(cnt < RESPONSE_LEN || !srp_waitresp ||
  6353.             tc == NULL
  6354.             ) {
  6355.             printf("SRP Protocol error\r\n");
  6356.             return(auth_resend(AUTHTYPE_SRP));
  6357.         }
  6358.         srp_waitresp = 0;
  6359.  
  6360.         if(t_clientverify(tc, data) == 0) {
  6361.             printf("SRP accepts you as %s\r\n",szUserName);
  6362. #ifdef CK_SSL
  6363.             if((ssl_active_flag || tls_active_flag) &&
  6364.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6365.                 printf("TLS session parameters verified by SRP\r\n");
  6366.             } else
  6367. #endif /* CK_SSL */
  6368.  
  6369. #ifdef CK_ENCRYPTION
  6370.             {
  6371.                 skey.type = SK_GENERIC;
  6372.                 skey.length = SESSION_KEY_LEN;
  6373.                 skey.data = tc->session_key;
  6374.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6375.             }
  6376. #endif /* ENCRYPTION */
  6377.             t_clientclose(tc);
  6378.             tc = NULL;
  6379.             accept_complete = 1;
  6380.             auth_finished(AUTH_VALID);
  6381.             return AUTH_SUCCESS;
  6382.         } else {
  6383.             printf("SRP server authentication failed!\r\n");
  6384.             t_clientclose(tc);
  6385.             tc = NULL;
  6386.             return(auth_resend(AUTHTYPE_SRP));
  6387.         }
  6388.         break;
  6389.  
  6390.     case SRP_PARAMS:
  6391.         if(!szUserName) {
  6392.             printf("No username available\r\n");
  6393.             return(auth_resend(AUTHTYPE_SRP));
  6394.         }
  6395.  
  6396.         n.len = srp_decode_length(data);
  6397.         data += 2;
  6398.         cnt -= 2;
  6399.         if(n.len > cnt) {
  6400.             printf("n too long\r\n");
  6401.             return(auth_resend(AUTHTYPE_SRP));
  6402.         }
  6403.         n.data = data;
  6404.         data += n.len;
  6405.         cnt -= n.len;
  6406.  
  6407.         g.len = srp_decode_length(data);
  6408.         data += 2;
  6409.         cnt -= 2;
  6410.         if(g.len > cnt) {
  6411.             printf("g too long\r\n");
  6412.             return(auth_resend(AUTHTYPE_SRP));
  6413.         }
  6414.         g.data = data;
  6415.         data += g.len;
  6416.         cnt -= g.len;
  6417.  
  6418.         s.len = srp_decode_length(data);
  6419.         data += 2;
  6420.         cnt -= 2;
  6421.         if(s.len > cnt) {
  6422.             printf("salt too long\r\n");
  6423.             return(auth_resend(AUTHTYPE_SRP));
  6424.         }
  6425.         s.data = data;
  6426.         data += s.len;
  6427.         cnt -= s.len;
  6428.  
  6429.         /* If the parameters provided by the server cannot be
  6430.          * validated the following function will fail.
  6431.          */
  6432.         tc = t_clientopen(szUserName, &n, &g, &s);
  6433.         if (tc == NULL) {
  6434.             printf("SRP parameter initialization error\r\n");
  6435.             return(auth_resend(AUTHTYPE_SRP));
  6436.         }
  6437.         A = t_clientgenexp(tc);
  6438.         if(A == NULL) {
  6439.             printf("SRP protocol error\r\n");
  6440.             return(auth_resend(AUTHTYPE_SRP));
  6441.         }
  6442.         SendSRPAuthSB(SRP_EXP, A->data, A->len);
  6443.  
  6444.         if ( pwbuf[0] && pwflg ) {
  6445.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6446.                    8 * n.len,
  6447.                    szUserName
  6448.                    );
  6449.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6450. #ifdef OS2
  6451.             if ( pwcrypt )
  6452.                 ck_encrypt((char *)srp_passwd);
  6453. #endif /* OS2 */
  6454.         } else {
  6455.             extern char * srppwprompt;
  6456.             char preface[128];
  6457.             int ok;
  6458.  
  6459.             if (srppwprompt && srppwprompt[0] &&
  6460.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6461.         sizeof(preface)) {
  6462.                 sprintf(preface,srppwprompt,szUserName);
  6463.             } else {
  6464.                 ckmakxmsg( preface,sizeof(preface),
  6465.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6466.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6467.                           NULL, NULL);
  6468.             }
  6469.             ok = uq_txt( preface,"Password: ",2,NULL,
  6470.                          srp_passwd,sizeof(srp_passwd)-1,NULL,
  6471.              DEFAULT_UQ_TIMEOUT);
  6472.             if ( !ok )
  6473.                 srp_passwd[0] = '\0';
  6474.         }
  6475.  
  6476.         t_clientpasswd(tc, srp_passwd);
  6477.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6478.         return AUTH_SUCCESS;
  6479.  
  6480.     case SRP_CHALLENGE:
  6481.         if(tc == NULL) {
  6482.             printf("SRP protocol error\r\n");
  6483.             return(auth_resend(AUTHTYPE_SRP));
  6484.         }
  6485.  
  6486. #ifndef PRE_SRP_1_4_5
  6487.         /*
  6488.          * The original SRP AUTH implementation did not protect against
  6489.          * tampering of the auth-type-pairs.  Therefore, when the
  6490.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6491.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6492.          * is set we also insert the SSL/TLS client and server finished
  6493.          * messages to ensure that there is no man in the middle attack
  6494.          * underway on the SSL/TLS connection.
  6495.          */
  6496.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6497.             type_check[0] = AUTHTYPE_SRP;
  6498.             type_check[1] = how;
  6499. #ifdef CK_SSL
  6500.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6501.                 ssl_get_client_finished(&type_check[2],12);
  6502.                 ssl_get_server_finished(&type_check[14],12);
  6503.                 t_clientaddexdata(tc,type_check,26);
  6504.             } else
  6505. #endif /* CK_SSL */
  6506.                 t_clientaddexdata(tc,type_check,2);
  6507.         }
  6508. #endif /* PRE_SRP_1_4_5 */
  6509.  
  6510.         B.data = data;
  6511.         B.len = cnt;
  6512.         t_clientgetkey(tc, &B);
  6513.  
  6514.         SendSRPAuthSB(SRP_RESPONSE, t_clientresponse(tc), RESPONSE_LEN);
  6515.         srp_waitresp = 1;
  6516.         return AUTH_SUCCESS;
  6517.  
  6518.     default:
  6519.         return(auth_resend(AUTHTYPE_SRP));
  6520.     }
  6521.     return AUTH_FAILURE;
  6522. }
  6523.  
  6524. static int
  6525. #ifdef CK_ANSIC
  6526. srp_is(int how, unsigned char *data, int cnt)
  6527. #else
  6528. srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6529. #endif
  6530. {
  6531.     char * pbuf = NULL;
  6532.     char * ptr;
  6533. #ifdef CK_ENCRYPTION
  6534.     Session_Key skey;
  6535. #endif
  6536.     struct t_num A;
  6537.     struct t_pw * tpw = NULL;
  6538.     struct t_conf * tconf = NULL;
  6539.     struct passwd * pass;
  6540.     static struct t_num * B = NULL;     /* Holder for B */
  6541. #ifdef CK_SSL
  6542.     char type_check[26];
  6543. #else
  6544.     char type_check[2];
  6545. #endif /* CK_SSL */
  6546.  
  6547.     if ((cnt -= 4) < 1) {
  6548.         auth_finished(AUTH_REJECT);
  6549.         return AUTH_FAILURE;
  6550.     }
  6551.  
  6552.     data += 4;
  6553.     cnt  -= 1;
  6554.     switch(*data++) {
  6555.     case SRP_AUTH:
  6556.         /* Send parameters back to client */
  6557.         if(ts != NULL) {
  6558.             t_serverclose(ts);
  6559.             ts = NULL;
  6560.         }
  6561.         if(!szUserNameRequested[0]) {
  6562.             if (1)
  6563.                 printf("No username available\r\n");
  6564.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6565.             auth_finished(AUTH_REJECT);
  6566.             return(AUTH_FAILURE);
  6567.         }
  6568. #ifdef IKSD
  6569. #ifdef CK_LOGIN
  6570.         if (inserver && ckxanon &&
  6571.              !strcmp(szUserNameRequested,"anonymous")) {
  6572.             SendSRPAuthSB(SRP_REJECT, (void *)
  6573.             "anonymous login cannot be performed with Secure Remote Password",
  6574.             -1);
  6575.             auth_finished(AUTH_REJECT);
  6576.             return(AUTH_FAILURE);
  6577.         }
  6578. #endif /* CK_LOGIN */
  6579. #endif /* IKSD */
  6580. #ifndef PRE_SRP_1_4_4
  6581.         if(tpw == NULL) {
  6582.             if((tpw = t_openpw(NULL)) == NULL) {
  6583.                 if (1)
  6584.                     printf("Unable to open password file\r\n");
  6585.                 SendSRPAuthSB(SRP_REJECT, (void *) "No password file", -1);
  6586.                 return(AUTH_FAILURE);
  6587.             }
  6588.         }
  6589.         if(tconf == NULL) {
  6590.             if((tconf = t_openconf(NULL)) == NULL) {
  6591.                 if (1)
  6592.                   printf("Unable to open configuration file\r\n");
  6593.                 SendSRPAuthSB(SRP_REJECT, (void *)"No configuration file", -1);
  6594.                 return(AUTH_FAILURE);
  6595.             }
  6596.         }
  6597.         ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6598.         t_closepw(tpw);
  6599.         tpw = NULL;
  6600.         t_closeconf(tconf);
  6601.         tconf = NULL;
  6602. #else /* PRE_SRP_1_4_4 */
  6603. #ifdef COMMENT
  6604.         /* the code in this block should no longer be necessary on OS/2
  6605.            or Windows because I have added functionality to libsrp.lib
  6606.            to find the srp files.   4/22/2000
  6607.         */
  6608.  
  6609.         /* On Windows and OS/2 there is no well defined place for the */
  6610.         /* ETC directory.  So we look for either an SRP_ETC or ETC    */
  6611.         /* environment variable in that order.  If we find one we     */
  6612.         /* attempt to open the files manually.                        */
  6613.         /* We will reuse the strTmp[] for the file names. */
  6614.         ptr = getenv("SRP_ETC");
  6615.         if ( !ptr )
  6616.             ptr = getenv("ETC");
  6617. #ifdef NT
  6618.         if ( !ptr ) {
  6619.             DWORD len;
  6620.             len = AUTHTMPBL;
  6621.  
  6622.             len = GetWindowsDirectory(strTmp,len);
  6623.             if ( len > 0 && len < AUTHTMPBL) {
  6624.                 if ( !isWin95() ) {
  6625.                     if ( len == 1 )
  6626.               ckstrncat(strTmp,"SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6627.                     else
  6628.               ckstrncat(strTmp,"/SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6629.                 }
  6630.             }
  6631.             ptr = strTmp;
  6632.         }
  6633. #endif /* NT */
  6634.         if ( ptr ) {
  6635.             int len = strlen(ptr);
  6636.             int i;
  6637.         if (ptr != strTmp)
  6638.         strcpy(strTmp,ptr);
  6639.             for ( i=0;i<len;i++ ) {
  6640.                 if ( strTmp[i] == '\\' )
  6641.                     strTmp[i] = '/';
  6642.             }
  6643.             if ( strTmp[len-1] != '/' )
  6644.                 ckstrncat(strTmp,"/tpasswd",sizeof(strTmp));
  6645.             else
  6646.                 ckstrncat(strTmp,"tpasswd",sizeof(strTmp));
  6647.             tpw = t_openpwbyname(strTmp);
  6648.  
  6649.             ckstrncat(strTmp,".conf",sizeof(strTmp));
  6650.             tconf = t_openconfbyname(strTmp);
  6651.         }
  6652.  
  6653.         if ( tpw && tconf )
  6654.             ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6655.         else
  6656.             ts = t_serveropen(szUserNameRequested);
  6657.         if ( tpw ) {
  6658.             t_closepw(tpw);
  6659.             tpw = NULL;
  6660.         }
  6661.         if ( tconf ) {
  6662.             t_closeconf(tconf);
  6663.             tconf = NULL;
  6664.         }
  6665. #else /* COMMENT */
  6666.         ts = t_serveropen(szUserNameRequested);
  6667. #endif /* COMMENT */
  6668. #endif /* PRE_SRP_1_4_4 */
  6669.  
  6670.         if( ts == NULL ) {
  6671.             printf("User %s not found\r\n", szUserNameRequested);
  6672.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  6673.             return(AUTH_FAILURE);
  6674.         }
  6675.  
  6676.     pbuf = (char *)malloc(ts->n.len + ts->g.len + ts->s.len + 7);
  6677.     ptr = pbuf;
  6678.  
  6679.         srp_encode_length(ptr, ts->n.len);
  6680.         ptr += 2;
  6681.         memcpy(ptr, ts->n.data, ts->n.len);     /* safe */
  6682.         ptr += ts->n.len;
  6683.  
  6684.         srp_encode_length(ptr, ts->g.len);
  6685.         ptr += 2;
  6686.         memcpy(ptr, ts->g.data, ts->g.len);     /* safe */
  6687.         ptr += ts->g.len;
  6688.  
  6689.         srp_encode_length(ptr, ts->s.len);
  6690.         ptr += 2;
  6691.         memcpy(ptr, ts->s.data, ts->s.len);     /* safe */
  6692.         ptr += ts->s.len;
  6693.  
  6694.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  6695.     free(pbuf); pbuf = NULL;
  6696.  
  6697.         B = t_servergenexp(ts);
  6698.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  6699.         return AUTH_SUCCESS;
  6700.  
  6701.     case SRP_EXP:
  6702.         /* Client is sending A to us, compute challenge & expected response. */
  6703.         if (ts == NULL || B == NULL) {
  6704.             printf("Protocol error: SRP_EXP unexpected\r\n");
  6705.             SendSRPAuthSB(SRP_REJECT,
  6706.                           (void *) "Protocol error: unexpected EXP",
  6707.                           -1
  6708.                           );
  6709.             return(AUTH_FAILURE);
  6710.         }
  6711.  
  6712.         /* Wait until now to send B, since it contains the key to "u" */
  6713.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->len);
  6714.         B = NULL;
  6715.  
  6716. #ifndef PRE_SRP_1_4_5
  6717.         /*
  6718.          * The original SRP AUTH implementation did not protect against
  6719.          * tampering of the auth-type-pairs.  Therefore, when the
  6720.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6721.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6722.          * is set we also insert the SSL/TLS client and server finished
  6723.          * messages to ensure that there is no man in the middle attack
  6724.          * underway on the SSL/TLS connection.
  6725.          */
  6726.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  6727.             type_check[0] = AUTHTYPE_SRP;
  6728.             type_check[1] = how;
  6729. #ifdef CK_SSL
  6730.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6731.                 ssl_get_client_finished(&type_check[2],12);
  6732.                 ssl_get_server_finished(&type_check[14],12);
  6733.             }
  6734. #endif /* CK_SSL */
  6735.             t_serveraddexdata(ts,type_check,
  6736. #ifdef CK_SSL
  6737.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  6738. #endif /* CK_SSL */
  6739.                                2);
  6740.         }
  6741. #endif /* PRE_SRP_1_4_5 */
  6742.  
  6743.         A.data = data;
  6744.         A.len = cnt;
  6745.         ptr = t_servergetkey(ts, &A);
  6746.  
  6747.         if(ptr == NULL) {
  6748.             if (1)
  6749.               printf("Security alert: Trivial session key attempted\r\n");
  6750.             SendSRPAuthSB(SRP_REJECT,
  6751.                           (void *) "Trivial session key detected",
  6752.                           -1
  6753.                           );
  6754.             return(AUTH_FAILURE);
  6755.         }
  6756.         srp_waitresp = 1;
  6757.         return AUTH_SUCCESS;
  6758.  
  6759.     case SRP_RESPONSE:
  6760.         /* Got the response; see if it's correct */
  6761.         if (!srp_waitresp ||
  6762.              ts == NULL
  6763.              ) {
  6764.             if (1)
  6765.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  6766.             SendSRPAuthSB(SRP_REJECT,
  6767.                           (void *) "Protocol error: unexpected RESPONSE",
  6768.                           -1
  6769.                           );
  6770.             return(AUTH_FAILURE);
  6771.         }
  6772.         srp_waitresp = 0;       /* we got a response */
  6773.  
  6774.         if (cnt < RESPONSE_LEN) {
  6775.             if (1)
  6776.               printf("Protocol error: malformed response\r\n");
  6777.             SendSRPAuthSB(SRP_REJECT,
  6778.                           (void *) "Protocol error: malformed response",
  6779.                           -1
  6780.                           );
  6781.             return(AUTH_FAILURE);
  6782.         }
  6783.  
  6784.         if (t_serververify(ts, data) == 0) {
  6785.             SendSRPAuthSB(SRP_ACCEPT, t_serverresponse(ts), RESPONSE_LEN);
  6786.             accept_complete = 1;
  6787. #ifdef CK_ENCRYPTION
  6788. #ifdef CK_SSL
  6789.             if (!(ssl_active_flag || tls_active_flag))
  6790. #endif /* CK_SSL */
  6791.             {
  6792.                 hexdump("SRP_RESPONSE ts",ts,sizeof(ts));
  6793.                 hexdump("SRP_RESPONSE session_key",
  6794.                          ts->session_key,
  6795.                          SESSION_KEY_LEN
  6796.                          );
  6797.                 skey.type = SK_GENERIC;
  6798.                 skey.length = SESSION_KEY_LEN;
  6799.                 skey.data = ts->session_key;
  6800.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  6801.             }
  6802. #endif /* CK_ENCRYPTION */
  6803.             auth_finished(AUTH_VALID);
  6804.         }
  6805.         else {
  6806.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  6807.             auth_finished(AUTH_REJECT);
  6808.             return(AUTH_FAILURE);
  6809.         }
  6810.         return AUTH_SUCCESS;
  6811.  
  6812.     default:
  6813.         printf("Unknown SRP option %d\r\n", data[-1]);
  6814.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  6815.         return(AUTH_FAILURE);
  6816.     }
  6817. }
  6818. #else /* PRE_SRP_1_7_3 */
  6819. static int
  6820. #ifdef CK_ANSIC
  6821. new_srp_reply(int how, unsigned char *data, int cnt)
  6822. #else
  6823. new_srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6824. #endif
  6825. {
  6826.     data += 4;                          /* Point to status byte */
  6827.     cnt  -= 4;
  6828.  
  6829.     if(cnt-- < 1) {                     /* Matches with data++ */
  6830.         auth_finished(AUTH_REJECT);
  6831.         return AUTH_FAILURE;
  6832.     }
  6833.  
  6834.     switch(*data++) {
  6835.     case SRP_PARAMS: {
  6836.         struct t_num n;
  6837.         struct t_num g;
  6838.         struct t_num s;
  6839.         cstr * A;
  6840.  
  6841.         if(!szUserName) {
  6842.             printf("No username available\r\n");
  6843.             return(auth_resend(AUTHTYPE_SRP));
  6844.         }
  6845.  
  6846.         n.len = srp_decode_length(data);
  6847.         data += 2;
  6848.         cnt -= 2;
  6849.         if(n.len > cnt) {
  6850.             printf("n too long\r\n");
  6851.             return(auth_resend(AUTHTYPE_SRP));
  6852.         }
  6853.         n.data = data;
  6854.         data += n.len;
  6855.         cnt -= n.len;
  6856.  
  6857.         g.len = srp_decode_length(data);
  6858.         data += 2;
  6859.         cnt -= 2;
  6860.         if(g.len > cnt) {
  6861.             printf("g too long\r\n");
  6862.             return(auth_resend(AUTHTYPE_SRP));
  6863.         }
  6864.         g.data = data;
  6865.         data += g.len;
  6866.         cnt -= g.len;
  6867.  
  6868.         s.len = srp_decode_length(data);
  6869.         data += 2;
  6870.         cnt -= 2;
  6871.         if(s.len != cnt) {
  6872.             printf("invalid salt\r\n");
  6873.             return(auth_resend(AUTHTYPE_SRP));
  6874.         }
  6875.         s.data = data;
  6876.         data += s.len;
  6877.         cnt -= s.len;
  6878.  
  6879.         /* If the parameters provided by the server cannot be
  6880.          * validated the following function will fail.
  6881.          */
  6882.         c_srp = SRP_new(SRP_RFC2945_client_method());
  6883.         if (c_srp == NULL ||
  6884.         SRP_set_username(c_srp, szUserName) != SRP_SUCCESS ||
  6885.         SRP_set_params(c_srp,n.data,n.len,g.data,g.len,s.data,s.len) !=
  6886.         SRP_SUCCESS) {
  6887.             printf("SRP Parameter initialization error\r\n");
  6888.             return(auth_resend(AUTHTYPE_SRP));
  6889.         }
  6890.  
  6891.         A = cstr_new();
  6892.         if(SRP_gen_pub(c_srp, &A) != SRP_SUCCESS) {
  6893.             printf("SRP Error generating key exchange\r\n");
  6894.             return(auth_resend(AUTHTYPE_SRP));
  6895.         }
  6896.  
  6897.         SendSRPAuthSB(SRP_EXP, A->data, A->length);
  6898.         cstr_free(A);
  6899.  
  6900.         if ( pwbuf[0] && pwflg ) {
  6901.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6902.                    8 * n.len,
  6903.                    szUserName
  6904.                    );
  6905.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6906. #ifdef OS2
  6907.             if ( pwcrypt )
  6908.                 ck_encrypt((char *)srp_passwd);
  6909. #endif /* OS2 */
  6910.         } else {
  6911.             extern char * srppwprompt;
  6912.             char preface[128];
  6913.             int ok;
  6914.  
  6915.             if (srppwprompt && srppwprompt[0] &&
  6916.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6917.         sizeof(preface)) {
  6918.                 sprintf(preface,srppwprompt,szUserName);
  6919.             } else {
  6920.                 ckmakxmsg( preface,sizeof(preface),
  6921.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6922.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6923.                           NULL, NULL);
  6924.             }
  6925.             ok = uq_txt(preface,"Password: ",2,NULL,
  6926.                         srp_passwd,sizeof(srp_passwd)-1,NULL,
  6927.             DEFAULT_UQ_TIMEOUT);
  6928.             if ( !ok )
  6929.                 srp_passwd[0] = '\0';
  6930.         }
  6931.  
  6932.         if(SRP_set_auth_password(c_srp, srp_passwd) != SRP_SUCCESS) {
  6933.             memset(srp_passwd, 0, sizeof(srp_passwd));
  6934.             printf("SRP Error setting client password\r\n");
  6935.             return(auth_resend(AUTHTYPE_SRP));
  6936.         }
  6937.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6938.         return AUTH_SUCCESS;
  6939.     }
  6940.     case SRP_CHALLENGE: {
  6941.         char type_check[26];
  6942.         cstr * resp = NULL;
  6943.  
  6944.         if(c_srp == NULL) {
  6945.             printf("SRP protocol error\r\n");
  6946.             return(auth_resend(AUTHTYPE_SRP));
  6947.         }
  6948.  
  6949.         /*
  6950.          * The original SRP AUTH implementation did not protect against
  6951.          * tampering of the auth-type-pairs.  Therefore, when the
  6952.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6953.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6954.          * is set we also insert the SSL/TLS client and server finished
  6955.          * messages to ensure that there is no man in the middle attack
  6956.          * underway on the SSL/TLS connection.
  6957.          */
  6958.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6959.             type_check[0] = AUTHTYPE_SRP;
  6960.             type_check[1] = how;
  6961. #ifdef CK_SSL
  6962.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6963.                 ssl_get_client_finished(&type_check[2],12);
  6964.                 ssl_get_server_finished(&type_check[14],12);
  6965.                 SRP_add_ex_data(c_srp, type_check, 26);
  6966.             } else
  6967. #endif /* CK_SSL */
  6968.                 SRP_add_ex_data(c_srp, type_check, 2);
  6969.         }
  6970.  
  6971.         if(SRP_compute_key(c_srp, &c_key, data, cnt) != SRP_SUCCESS) {
  6972.             printf("SRP ERROR: unable to compute client key\r\n");
  6973.             return(auth_resend(AUTHTYPE_SRP));
  6974.         }
  6975.  
  6976.         resp = cstr_new();
  6977.         if(SRP_respond(c_srp, &resp) != SRP_SUCCESS) {
  6978.             printf("SRP ERROR: unable to compute client response\r\n");
  6979.             return(auth_resend(AUTHTYPE_SRP));
  6980.         }
  6981.         SendSRPAuthSB(SRP_RESPONSE, resp->data, resp->length);
  6982.         cstr_free(resp);
  6983.         srp_waitresp = 1;
  6984.         return AUTH_SUCCESS;
  6985.     }
  6986.     case SRP_ACCEPT: {
  6987. #ifdef CK_ENCRYPTION
  6988.         Session_Key skey;
  6989. #endif /* ENCRYPTION */
  6990.  
  6991.         if(cnt < RESPONSE_LEN || !srp_waitresp || c_srp == NULL) {
  6992.             printf("SRP Protocol error\r\n");
  6993.             return(auth_resend(AUTHTYPE_SRP));
  6994.         }
  6995.         srp_waitresp = 0;
  6996.  
  6997.         if(SRP_verify(c_srp, data, cnt) == SRP_SUCCESS) {
  6998.             printf("SRP accepts you as %s\r\n",szUserName);
  6999.  
  7000. #ifdef CK_SSL
  7001.             if((ssl_active_flag || tls_active_flag) &&
  7002.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7003.                 printf("TLS session parameters verified by SRP\r\n");
  7004.             } else
  7005. #endif /* CK_SSL */
  7006. #ifdef CK_ENCRYPTION
  7007.             {
  7008.                 skey.type = SK_GENERIC;
  7009.                 skey.length = c_key->length;
  7010.                 skey.data = c_key->data;
  7011.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  7012.                 cstr_clear_free(c_key);
  7013.                 c_key = NULL;
  7014.             }
  7015. #endif /* CK_ENCRYPTION */
  7016.             accept_complete = 1;
  7017.             auth_finished(AUTH_VALID);
  7018.             SRP_free(c_srp);
  7019.             c_srp = NULL;
  7020.             return AUTH_SUCCESS;
  7021.         }
  7022.         else {
  7023.             printf("[ Error: SRP server authentication failed ]\r\n");
  7024.             return(auth_resend(AUTHTYPE_SRP));
  7025.         }
  7026.     }
  7027.     case SRP_REJECT: {
  7028.         char * str=NULL;
  7029.  
  7030.         ckmakmsg(strTmp,sizeof(strTmp),
  7031.                   "SRP refuses authentication for '",szUserName,
  7032.                   "'\r\n",NULL);
  7033.         if (cnt > 0) {
  7034.             int len = strlen(strTmp);
  7035.             if ( len + cnt < sizeof(strTmp) ) {
  7036.                 str = strTmp + strlen(strTmp);
  7037.                 memcpy(str,data,cnt);
  7038.                 str[cnt] = 0;
  7039.             }
  7040.         }
  7041.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  7042.         auth_finished(AUTH_REJECT);
  7043.         return AUTH_FAILURE;
  7044.     }
  7045.     default:
  7046.         printf("Unknown SRP option %d\r\n", data[-1]);
  7047.         return(auth_resend(AUTHTYPE_SRP));
  7048.     }
  7049.     /* NEVER REACHED */
  7050. }
  7051.  
  7052. static int
  7053. #ifdef CK_ANSIC
  7054. new_srp_is(int how, unsigned char *data, int cnt)
  7055. #else
  7056. new_srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  7057. #endif
  7058. {
  7059.     char * pbuf = NULL;
  7060.     char * ptr;
  7061. #ifdef CK_ENCRYPTION
  7062.     Session_Key skey;
  7063. #endif
  7064.     static cstr * B = NULL;             /* Holder for B */
  7065.     struct t_passwd * pass;
  7066.     cstr * resp;
  7067.     char type_check[26];
  7068.  
  7069.     if ((cnt -= 4) < 1) {
  7070.         auth_finished(AUTH_REJECT);
  7071.         return AUTH_FAILURE;
  7072.     }
  7073.  
  7074.     data += 4;
  7075.     cnt  -= 1;
  7076.     switch(*data++) {
  7077.     case SRP_AUTH:
  7078.         /* Send parameters back to client */
  7079.         if(s_srp != NULL) {
  7080.             SRP_free(s_srp);
  7081.             s_srp = NULL;
  7082.         }
  7083.         if (B != NULL) {
  7084.             cstr_free(B);
  7085.             B = NULL;
  7086.         }
  7087.         if(!szUserNameRequested[0]) {
  7088.             if (1)
  7089.                 printf("No username available\r\n");
  7090.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  7091.             auth_finished(AUTH_REJECT);
  7092.             return(AUTH_FAILURE);
  7093.         }
  7094. #ifdef IKSD
  7095. #ifdef CK_LOGIN
  7096.         if (inserver && ckxanon &&
  7097.              !strcmp(szUserNameRequested,"anonymous")) {
  7098.             SendSRPAuthSB(SRP_REJECT, (void *)
  7099.             "anonymous login cannot be performed with Secure Remote Password",
  7100.             -1);
  7101.             auth_finished(AUTH_REJECT);
  7102.             return(AUTH_FAILURE);
  7103.         }
  7104. #endif /* CK_LOGIN */
  7105. #endif /* IKSD */
  7106.         s_srp = SRP_new(SRP_RFC2945_server_method());
  7107.         if(s_srp == NULL) {
  7108.             printf("Error initializing SRP server\r\n");
  7109.             SendSRPAuthSB(SRP_REJECT,
  7110.                           (void *) "SRP server init failed",
  7111.                           -1
  7112.                           );
  7113.             return(AUTH_FAILURE);
  7114.         }
  7115.         pass = gettpnam(szUserNameRequested);
  7116.         if(pass == NULL) {
  7117.             printf("User %s not found\r\n", szUserNameRequested);
  7118.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  7119.             return(AUTH_FAILURE);
  7120.         }
  7121.         if(SRP_set_username(s_srp, szUserNameRequested) != SRP_SUCCESS ||
  7122.        SRP_set_params(s_srp, pass->tc.modulus.data,
  7123.               pass->tc.modulus.len,
  7124.               pass->tc.generator.data,
  7125.               pass->tc.generator.len,
  7126.               pass->tp.salt.data,
  7127.               pass->tp.salt.len) != SRP_SUCCESS ||
  7128.        SRP_set_authenticator(s_srp,
  7129.                  pass->tp.password.data,
  7130.                  pass->tp.password.len) != SRP_SUCCESS) {
  7131.             printf("Error initializing SRP parameters\r\n");
  7132.             SendSRPAuthSB(SRP_REJECT,(void *)"SRP parameter init failed", -1);
  7133.             return(AUTH_FAILURE);
  7134.         }
  7135.  
  7136.     pbuf = (char *)malloc(pass->tc.modulus.len + pass->tc.generator.len +
  7137.                    pass->tp.salt.len + 7);
  7138.         ptr = pbuf;
  7139.  
  7140.         srp_encode_length(ptr, pass->tc.modulus.len);
  7141.         ptr += 2;
  7142.         memcpy(ptr, pass->tc.modulus.data, pass->tc.modulus.len);
  7143.         ptr += pass->tc.modulus.len;
  7144.  
  7145.         srp_encode_length(ptr, pass->tc.generator.len);
  7146.         ptr += 2;
  7147.         memcpy(ptr, pass->tc.generator.data, pass->tc.generator.len);
  7148.         ptr += pass->tc.generator.len;
  7149.  
  7150.         srp_encode_length(ptr, pass->tp.salt.len);
  7151.         ptr += 2;
  7152.         memcpy(ptr, pass->tp.salt.data, pass->tp.salt.len);
  7153.         ptr += pass->tp.salt.len;
  7154.  
  7155.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  7156.     free(pbuf);
  7157.     pbuf = NULL;
  7158.  
  7159.         if(SRP_gen_pub(s_srp, &B) != SRP_SUCCESS) {
  7160.             printf("Error generating SRP public value\r\n");
  7161.             SendSRPAuthSB(SRP_REJECT, (void *) "SRP_gen_pub failed", -1);
  7162.             return(AUTH_FAILURE);
  7163.         }
  7164.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  7165.         return AUTH_SUCCESS;
  7166.  
  7167.     case SRP_EXP:
  7168.       /* Client is sending A to us, compute challenge and expected response. */
  7169.         if (s_srp == NULL || B == NULL) {
  7170.         printf("Protocol error: SRP_EXP unexpected\r\n");
  7171.         SendSRPAuthSB(SRP_REJECT,
  7172.               (void *)"Protocol error: unexpected EXP", -1);
  7173.         return(AUTH_FAILURE);
  7174.     }
  7175.         /* Wait until now to send B, since it contains the key to "u" */
  7176.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->length);
  7177.         cstr_free(B);
  7178.         B = NULL;
  7179.  
  7180.         /*
  7181.          * The original SRP AUTH implementation did not protect against
  7182.          * tampering of the auth-type-pairs.  Therefore, when the
  7183.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  7184.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  7185.          * is set we also insert the SSL/TLS client and server finished
  7186.          * messages to ensure that there is no man in the middle attack
  7187.          * underway on the SSL/TLS connection.
  7188.          */
  7189.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  7190.             type_check[0] = AUTHTYPE_SRP;
  7191.             type_check[1] = how;
  7192. #ifdef CK_SSL
  7193.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7194.                 ssl_get_client_finished(&type_check[2],12);
  7195.                 ssl_get_server_finished(&type_check[14],12);
  7196.                 SRP_add_ex_data(s_srp, type_check, 26);
  7197.             } else
  7198. #endif /* CK_SSL */
  7199.                 SRP_add_ex_data(s_srp, type_check, 2);
  7200.         }
  7201.  
  7202.         if(SRP_compute_key(s_srp, &s_key, data, cnt) != SRP_SUCCESS) {
  7203.             printf("Security alert: Trivial session key attempted\r\n");
  7204.             SendSRPAuthSB(SRP_REJECT,
  7205.               (void *) "Trivial session key detected", -1);
  7206.             return(AUTH_FAILURE);
  7207.         }
  7208.         srp_waitresp = 1;
  7209.         return AUTH_SUCCESS;
  7210.  
  7211.     case SRP_RESPONSE:
  7212.         /* Got the response; see if it's correct */
  7213.         if (!srp_waitresp || s_srp == NULL) {
  7214.             if (1)
  7215.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  7216.             SendSRPAuthSB(SRP_REJECT,
  7217.                           (void *) "Protocol error: unexpected RESPONSE",
  7218.                           -1
  7219.                           );
  7220.             return(AUTH_FAILURE);
  7221.         }
  7222.         srp_waitresp = 0;       /* we got a response */
  7223.  
  7224.         if (cnt < RESPONSE_LEN) {
  7225.             if (1)
  7226.               printf("Protocol error: malformed response\r\n");
  7227.             SendSRPAuthSB(SRP_REJECT,
  7228.                           (void *) "Protocol error: malformed response",
  7229.                           -1
  7230.                           );
  7231.             return(AUTH_FAILURE);
  7232.         }
  7233.  
  7234.         if(SRP_verify(s_srp, data, cnt) == SRP_SUCCESS) {
  7235.             resp = cstr_new();
  7236.             if(SRP_respond(s_srp, &resp) != SRP_SUCCESS) {
  7237.                 printf("Error computing response\r\n");
  7238.                 SendSRPAuthSB(SRP_REJECT,
  7239.                               (void *) "Error computing response", -1);
  7240.                 return(AUTH_FAILURE);
  7241.             }
  7242.             SendSRPAuthSB(SRP_ACCEPT, resp->data, resp->length);
  7243.             accept_complete = 1;
  7244.             cstr_free(resp);
  7245.  
  7246. #ifdef CK_ENCRYPTION
  7247. #ifdef CK_SSL
  7248.             if (!(ssl_active_flag || tls_active_flag))
  7249. #endif /* CK_SSL */
  7250.             {
  7251.                 skey.type = SK_GENERIC;
  7252.                 skey.length = s_key->length;
  7253.                 skey.data = s_key->data;
  7254.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  7255.                 cstr_clear_free(s_key);
  7256.                 s_key = NULL;
  7257.             }
  7258. #endif /* CK_ENCRYPTION */
  7259.             auth_finished(AUTH_VALID);
  7260.         }
  7261.         else {
  7262.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  7263.             auth_finished(AUTH_REJECT);
  7264.             return(AUTH_FAILURE);
  7265.         }
  7266.         return AUTH_SUCCESS;
  7267.  
  7268.     default:
  7269.         printf("Unknown SRP option %d\r\n", data[-1]);
  7270.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  7271.         return(AUTH_FAILURE);
  7272.     }
  7273. }
  7274. #endif /* PRE_SRP_1_7_3 */
  7275. #endif /* SRP */
  7276.  
  7277. #ifdef KRB5
  7278. #ifdef KINIT
  7279. /*
  7280.  * clients/kinit/kinit.c
  7281.  *
  7282.  * Copyright 1990 by the Massachusetts Institute of Technology.
  7283.  * All Rights Reserved.
  7284.  *
  7285.  * Export of this software from the United States of America may
  7286.  *   require a specific license from the United States Government.
  7287.  *   It is the responsibility of any person or organization contemplating
  7288.  *   export to obtain such a license before exporting.
  7289.  *
  7290.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  7291.  * distribute this software and its documentation for any purpose and
  7292.  * without fee is hereby granted, provided that the above copyright
  7293.  * notice appear in all copies and that both that copyright notice and
  7294.  * this permission notice appear in supporting documentation, and that
  7295.  * the name of M.I.T. not be used in advertising or publicity pertaining
  7296.  * to distribution of the software without specific, written prior
  7297.  * permission.  M.I.T. makes no representations about the suitability of
  7298.  * this software for any purpose.  It is provided "as is" without express
  7299.  * or implied warranty.
  7300.  *
  7301.  *
  7302.  * Initialize a credentials cache.
  7303.  */
  7304.  
  7305. #define KRB5_DEFAULT_OPTIONS 0
  7306. #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
  7307.  
  7308. static krb5_data tgtname = {
  7309. #ifndef HEIMDAL
  7310.     0,
  7311. #endif /* HEIMDAL */
  7312.     KRB5_TGS_NAME_SIZE,
  7313.     KRB5_TGS_NAME
  7314. };
  7315.  
  7316. /* Internal prototypes */
  7317. _PROTOTYP(static krb5_error_code krb5_validate_tgt,
  7318.         (krb5_context, krb5_ccache,krb5_principal, krb5_data *));
  7319. _PROTOTYP(static krb5_error_code krb5_renew_tgt,
  7320.         (krb5_context, krb5_ccache,
  7321.                         krb5_principal, krb5_data *));
  7322. _PROTOTYP(static krb5_error_code krb5_tgt_gen,
  7323.         (krb5_context, krb5_ccache,
  7324.                         krb5_principal, krb5_data *, int opt));
  7325.  
  7326. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7327. static krb5_error_code KRB5_CALLCONV
  7328. ck_krb5_prompter( krb5_context context,
  7329.                   void *data,
  7330.                   const char *name,
  7331.                   const char *banner,
  7332.                   int num_prompts,
  7333.                   krb5_prompt prompts[])
  7334. {
  7335.     krb5_error_code     errcode = 0;
  7336.     int                 i;
  7337. #ifdef KUI
  7338.     struct txtbox * tb = NULL;
  7339. #else /* KUI */
  7340.     char * prompt = NULL;
  7341. #endif /* KUI */
  7342.     int    len = 0, blen=0, nlen=0;
  7343.  
  7344.     debug(F110,"ck_krb5_prompter name",name,0);
  7345.     debug(F110,"ck_krb5_prompter banner",banner,0);
  7346.     debug(F101,"ck_krb5_prompter num_prompts","",num_prompts);
  7347.  
  7348.     if (name)
  7349.         nlen = strlen(name)+2;
  7350.  
  7351.     if (banner)
  7352.         blen = strlen(banner)+2;
  7353.  
  7354. #ifdef KUI
  7355.     tb = (struct txtbox *) malloc(sizeof(struct txtbox) * num_prompts);
  7356.     if ( tb != NULL ) {
  7357.         int ok;
  7358.         memset(tb,0,sizeof(struct txtbox) * num_prompts);
  7359.         for ( i=0; i < num_prompts; i++ ) {
  7360.             tb[i].t_buf = prompts[i].reply->data;
  7361.             tb[i].t_len = prompts[i].reply->length;
  7362.             tb[i].t_lbl = prompts[i].prompt;
  7363.             tb[i].t_dflt = NULL;
  7364.             tb[i].t_echo = (prompts[i].hidden ? 2 : 1);
  7365.         }   
  7366.  
  7367.         ok = uq_mtxt((char *)banner,NULL,num_prompts,tb);
  7368.         if ( ok ) {
  7369.             for ( i=0; i < num_prompts; i++ )
  7370.                 prompts[i].reply->length = strlen(prompts[i].reply->data);
  7371.         } else
  7372.             errcode = -2;
  7373.     }
  7374. #else /* KUI */
  7375.     for (i = 0; i < num_prompts; i++) {
  7376.         debug(F111,"ck_krb5_prompter prompt",prompts[i].prompt,i);
  7377.  
  7378.         if ( prompt && len < (nlen + blen + strlen(prompts[i].prompt)+2) ) {
  7379.             free(prompt);
  7380.             prompt = NULL;
  7381.         }
  7382.         if ( !prompt )
  7383.             prompt = (char *)malloc(nlen + blen + strlen(prompts[i].prompt)+2);
  7384.         if ( !prompt ) {
  7385.             errcode = KRB5_RC_MALLOC;
  7386.             goto cleanup;
  7387.         }
  7388.         len = nlen + blen + strlen(prompts[i].prompt)+2;
  7389.         ckmakxmsg(prompt,len,
  7390.                  (char *) (name?name:""),
  7391.                  name?"\r\n":"",
  7392.                  (char *) (banner?banner:""),
  7393.                  banner?"\r\n":"",
  7394.                  (char *)prompts[i].prompt,
  7395.                  ": ",NULL,NULL,NULL,NULL,NULL,NULL);
  7396.  
  7397.         memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7398.         if (prompts[i].hidden) {
  7399.             readpass(prompt, prompts[i].reply->data,
  7400.                       prompts[i].reply->length);
  7401.         } else {
  7402.             readtext(prompt, prompts[i].reply->data,
  7403.                       prompts[i].reply->length);
  7404.         }
  7405.         prompts[i].reply->length = strlen(prompts[i].reply->data);
  7406.     }
  7407. #endif /* KUI */
  7408.  
  7409.   cleanup:
  7410. #ifdef KUI
  7411.     if ( tb )
  7412.         free(tb);
  7413. #else /* KUI */
  7414.     if ( prompt )
  7415.         free(prompt);
  7416. #endif /* KUI */
  7417.     if (errcode) {
  7418.         for (i = 0; i < num_prompts; i++) {
  7419.             memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7420.         }
  7421.     }
  7422.     return errcode;
  7423. }
  7424.  
  7425. /*
  7426.  *      I'm not really sure what to do with this.  The NRL DLLs use a
  7427.  *      different interface for the krb5_prompter callback.  It has
  7428.  *      one less parameter.   This is going to be ugly.
  7429.  */
  7430. static krb5_error_code KRB5_CALLCONV
  7431. ck_NRL_krb5_prompter( krb5_context context,
  7432.                       const char *name,
  7433.                       const char *banner,
  7434.                       int num_prompts,
  7435.                       krb5_prompt prompts[])
  7436. {
  7437.     return(ck_krb5_prompter(context,NULL,name,banner,num_prompts,prompts));
  7438. }
  7439. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7440.  
  7441. #ifdef KRB524_CONV
  7442. long
  7443. try_convert524(krb5_context ctx, krb5_principal me, krb5_ccache cc)
  7444. {
  7445.     char * progname = "convert524";
  7446.     krb5_error_code code = 0;
  7447.     int icode = 0;
  7448.     krb5_principal kpcserver = 0;
  7449.     krb5_creds *v5creds = 0;
  7450.     krb5_creds increds;
  7451. #ifdef OS2
  7452.     LEASH_CREDENTIALS v4creds;
  7453. #else /* OS2 */
  7454.     CREDENTIALS v4creds;
  7455. #endif /* OS2 */
  7456.  
  7457.     memset((char *) &increds, 0, sizeof(increds));
  7458.     /*
  7459.       From this point on, we can goto cleanup because increds is
  7460.       initialized.
  7461.     */
  7462.  
  7463.     if ((code = krb5_build_principal(ctx,
  7464.                                      &kpcserver,
  7465.                                      krb5_princ_realm(ctx, me)->length,
  7466.                                      krb5_princ_realm(ctx, me)->data,
  7467.                                      "krbtgt",
  7468.                                      krb5_princ_realm(ctx, me)->data,
  7469.                                      NULL))) {
  7470.         com_err(progname, code,
  7471.                 "while creating service principal name");
  7472.         goto cleanup;
  7473.     }
  7474.  
  7475.     memset((char*) &increds, 0, sizeof(increds));
  7476.     increds.client = me;
  7477.     increds.server = kpcserver;
  7478.     /* Prevent duplicate free calls.  */
  7479.     kpcserver = 0;
  7480.  
  7481.     increds.times.endtime = 0;
  7482.     increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
  7483.     if ((code = krb5_get_credentials(ctx, 0,
  7484.                                      cc,
  7485.                                      &increds,
  7486.                                      &v5creds))) {
  7487.         com_err(progname, code,
  7488.                 "getting V5 credentials");
  7489.         goto cleanup;
  7490.     }
  7491.     if ((icode = krb524_convert_creds_kdc(ctx,
  7492.                                           v5creds,
  7493.                                           &v4creds))) {
  7494.         com_err(progname, icode,
  7495.                 "converting to V4 credentials");
  7496.         goto cleanup;
  7497.     }
  7498.     /* this is stolen from the v4 kinit */
  7499.     /* initialize ticket cache */
  7500.     if ((icode = krb_in_tkt(v4creds.pname, v4creds.pinst, v4creds.realm)
  7501.          != KSUCCESS)) {
  7502.         com_err(progname, icode,
  7503.                 "trying to create the V4 ticket file");
  7504.         goto cleanup;
  7505.     }
  7506.     /* stash ticket, session key, etc. for future use */
  7507.     if ((icode = krb_save_credentials(v4creds.service,
  7508.                                       v4creds.instance,
  7509.                                       v4creds.realm,
  7510.                                       v4creds.session,
  7511.                                       v4creds.lifetime,
  7512.                                       v4creds.kvno,
  7513.                                       &(v4creds.ticket_st),
  7514.                                       v4creds.issue_date))) {
  7515.         com_err(progname, icode,
  7516.                 "trying to save the V4 ticket");
  7517.         goto cleanup;
  7518.     }
  7519.  
  7520.  cleanup:
  7521.     memset(&v4creds, 0, sizeof(v4creds));
  7522.     if (v5creds)
  7523.         krb5_free_creds(ctx, v5creds);
  7524.     increds.client = 0;
  7525.     krb5_free_cred_contents(ctx, &increds);
  7526.     if (kpcserver)
  7527.         krb5_free_principal(ctx, kpcserver);
  7528.     return !(code || icode);
  7529. }
  7530. #endif /* KRB524_CONV */
  7531.  
  7532. #define NO_KEYTAB
  7533.  
  7534. int
  7535. #ifdef CK_ANSIC
  7536. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  7537.                  struct krb4_init_data * k4_init)
  7538. #else
  7539. ck_krb5_initTGT(op,init,k4_init)
  7540.     krb_op_data * op; struct krb5_init_data * init;
  7541.     struct krb4_init_data * k4_init;
  7542. #endif /* CK_ANSIC*/
  7543. {
  7544.     krb5_context kcontext;
  7545.     krb5_ccache ccache = NULL;
  7546.     krb5_deltat lifetime = KRB5_DEFAULT_LIFE;   /* -l option */
  7547.     krb5_timestamp starttime = 0;
  7548.     krb5_deltat rlife = 0;
  7549.     int options = KRB5_DEFAULT_OPTIONS;
  7550.     int option;
  7551.     int errflg = 0;
  7552.     krb5_error_code code;
  7553.     krb5_principal me=NULL;
  7554.     krb5_principal server=NULL;
  7555.     krb5_creds my_creds;
  7556.     krb5_timestamp now;
  7557. #ifndef HEIMDAL
  7558.     krb5_address **addrs = (krb5_address **)0;
  7559. #endif /* HEIMDAL */
  7560.     int addr_count=0;
  7561.     int i,j;
  7562. #ifndef NO_KEYTAB
  7563.     int use_keytab = 0;                 /* -k option */
  7564.     krb5_keytab keytab = NULL;
  7565. #endif /* NO_KEYTAB */
  7566.     struct passwd *pw = 0;
  7567.     int pwsize;
  7568.     char *client_name=NULL, principal[256]="", realm[256]="", numstr[40]="";
  7569.     char *password=NULL, passwd[80]="";
  7570. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7571.     krb5_get_init_creds_opt opts;
  7572. #endif
  7573.     char * name;
  7574.     int len;
  7575.  
  7576.     if ( !ck_krb5_is_installed() )
  7577.         return(-1);
  7578.  
  7579. #ifdef COMMENT
  7580.     printf("Kerberos V initialization\r\n");
  7581. #endif /* COMMENT */
  7582.  
  7583.     code = krb5_init_context(&kcontext);
  7584.     if (code) {
  7585.         com_err("krb5_kinit",code,"while init_context");
  7586.         krb5_errno = code;
  7587.         makestr(&krb5_errmsg,error_message(krb5_errno));
  7588.         return(-1);
  7589.     }
  7590.  
  7591.     debug(F110,"krb5_init","krb5_init_context",0);
  7592.  
  7593.     if ((code = krb5_timeofday(kcontext, &now))) {
  7594.         com_err("krb5_kinit",code,"while getting time of day");
  7595.         goto exit_k5_init;
  7596.     }
  7597.  
  7598. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7599.     memset(&opts, 0, sizeof(opts));
  7600.     krb5_get_init_creds_opt_init(&opts);
  7601.     debug(F110,"krb5_init","krb5_get_init_creds_opt_init",0);
  7602. #endif
  7603.  
  7604.     if ( init->renewable ) {
  7605.         options |= KDC_OPT_RENEWABLE;
  7606.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->renewable),"m",NULL,NULL);
  7607. #ifdef HEIMDAL
  7608.         code = -1;
  7609. #else /* HEIMDAL */
  7610.         code = krb5_string_to_deltat(numstr, &rlife);
  7611. #endif /* HEIMDAL */
  7612.         if (code != 0 || rlife == 0) {
  7613.             printf("Bad renewable time value %s\r\n", numstr);
  7614.             errflg++;
  7615.         }
  7616. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7617.         krb5_get_init_creds_opt_set_renew_life(&opts, rlife);
  7618. #endif
  7619.     }
  7620.     if ( init->renew ) {
  7621.         /* renew the ticket */
  7622.         options |= KDC_OPT_RENEW;
  7623.     }
  7624.  
  7625.     if ( init->validate ) {
  7626.         /* validate the ticket */
  7627.         options |= KDC_OPT_VALIDATE;
  7628.     }
  7629.     if ( init->proxiable ) {
  7630.         options |= KDC_OPT_PROXIABLE;
  7631. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7632.         krb5_get_init_creds_opt_set_proxiable(&opts, 1);
  7633. #endif
  7634.     }
  7635.     if ( init->forwardable ) {
  7636.         options |= KDC_OPT_FORWARDABLE;
  7637. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7638.         krb5_get_init_creds_opt_set_forwardable(&opts, 1);
  7639. #endif
  7640.     }
  7641. #ifndef NO_KEYTAB
  7642.     if (  ) {
  7643.         use_keytab = 1;
  7644.     }
  7645.     if (  ) {
  7646.         if (keytab == NULL && keytab_name != NULL) {
  7647.             code = krb5_kt_resolve(kcontext, keytab_name, &keytab);
  7648.             if (code != 0) {
  7649.                 debug(F111,"krb5_init resolving keytab",
  7650.                          keytab_name,code);
  7651.                 errflg++;
  7652.             }
  7653.         }
  7654.     }
  7655. #endif /* NO_KEYTAB */
  7656.     if ( init->lifetime ) {
  7657.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->lifetime),"m",NULL,NULL);
  7658. #ifdef HEIMDAL
  7659.         code = -1;
  7660. #else /* HEIMDAL */
  7661.         code = krb5_string_to_deltat(numstr, &lifetime);
  7662. #endif /* HEIMDAL */
  7663.         if (code != 0 || lifetime == 0) {
  7664.             printf("Bad lifetime value %s\r\n", numstr);
  7665.             errflg++;
  7666.         }
  7667. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7668.         krb5_get_init_creds_opt_set_tkt_life(&opts, lifetime);
  7669. #endif
  7670.     }
  7671.     if ( init->postdate ) {
  7672.         /* Convert cmdate() to a time_t value */
  7673.         struct tm * time_tm;
  7674.         struct tm * cmdate2tm(char *,int);
  7675.         time_tm = cmdate2tm(init->postdate,0);
  7676.         if ( time_tm )
  7677.             starttime = (krb5_timestamp) mktime(time_tm);
  7678.  
  7679.         if (code != 0 || starttime == 0 || starttime == -1) {
  7680.             krb5_deltat ktmp;
  7681. #ifdef HEIMDAL
  7682.             code = -1;
  7683. #else /* HEIMDAL */
  7684.             code = krb5_string_to_deltat(init->postdate, &ktmp);
  7685. #endif /* HEIMDAL */
  7686.             if (code == 0 && ktmp != 0) {
  7687.                 starttime = now + ktmp;
  7688.                 options |= KDC_OPT_POSTDATED;
  7689.             } else {
  7690.                 printf("Bad postdate start time value %s\r\n",
  7691.                         init->postdate);
  7692.                 errflg++;
  7693.             }
  7694.         } else {
  7695.             options |= KDC_OPT_POSTDATED;
  7696.         }
  7697.     }
  7698.  
  7699.     debug(F110,"krb5_init searching for ccache",op->cache,0);
  7700.  
  7701.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  7702.     if (code != 0) {
  7703.         com_err("krb5_kinit",code,"while getting default ccache");
  7704.         goto exit_k5_init;
  7705.     }
  7706.  
  7707.     /* This is our realm unless it is changed */
  7708.     ckstrncpy(realm,init->realm ? init->realm : krb5_d_realm, 256);
  7709.  
  7710. #ifdef BETATEST
  7711.     /* This code is going to take the realm and attempt to correct */
  7712.     /* the case.                                                   */
  7713.     {
  7714.         profile_t profile;
  7715.  
  7716.         code = krb5_get_profile(kcontext, &profile);
  7717.         if ( !code ) {
  7718.             const char  *names[4];
  7719.             char ** realms;
  7720.             int found = 0;
  7721.  
  7722.             names[0] = "realms";
  7723.             names[1] = NULL;
  7724.  
  7725.             code = profile_get_subsection_names(profile,names,&realms);
  7726.             if ( code == 0 ) {
  7727.                 int i=0;
  7728.                 while ( realms[i] ) {
  7729.                     if (ckstrcmp(realm,realms[i],-1,0) == 0) {
  7730.                         strcpy(realm,realms[i]);
  7731.                         found = 1;
  7732.                         break;
  7733.                     }
  7734.                     i++;
  7735.                 }
  7736.             }
  7737.  
  7738. #ifdef CK_DNS_SRV
  7739.             if ( !found ) {
  7740.                 char * dns_realm = NULL;
  7741.  
  7742.                 /* We did not find the realm in the profile so let's try DNS */
  7743.                 locate_txt_rr("_kerberos",realm,&dns_realm);
  7744.                 if ( dns_realm &&
  7745.                      ckstrcmp(realm,dns_realm,-1,0) == 0 &&
  7746.                      ckstrcmp(realm,dns_realm,-1,1) != 0
  7747.                      ) {
  7748.                     ckstrncpy(realm,dns_realm,256);
  7749.                     free(dns_realm);
  7750.                 }
  7751.             }
  7752. #endif /* CK_DNS_SRV */
  7753.         }
  7754.  
  7755.         if (init->realm &&
  7756.              ckstrcmp(realm,init->realm,-1,0) == 0 &&
  7757.              ckstrcmp(realm,init->realm,-1,1) != 0)
  7758.             strcpy(init->realm,realm);
  7759.         if (ckstrcmp(realm,krb5_d_realm,-1,0) == 0 &&
  7760.              ckstrcmp(realm,krb5_d_realm,-1,1) != 0)
  7761.             strcpy(krb5_d_realm,realm);
  7762.     }
  7763. #endif /* BETATEST */
  7764.  
  7765.     if (init->principal == NULL) {       /* No principal name specified */
  7766. #ifndef NO_KEYTAB
  7767.         if (use_keytab) {
  7768.             /* Use the default host/service name */
  7769.             code = krb5_sname_to_principal(kcontext, NULL, NULL,
  7770.                                             KRB5_NT_SRV_HST, &me);
  7771.             if (code == 0 &&
  7772.                 krb5_princ_realm(kcontext, me)->length < sizeof(realm))
  7773.             {
  7774.                 /* Save the realm */
  7775.                 memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  7776.                         krb5_princ_realm(kcontext, me)->length); /* safe */
  7777.                 realm[krb5_princ_realm(kcontext, me)->length]='\0';
  7778.             } else {
  7779.                 com_err("krb5_kinit",
  7780.                         code,
  7781.                         "when creating default server principal name");
  7782.                 goto exit_k5_init;
  7783.             }
  7784.         } else
  7785. #endif /* NO_KEYTAB */
  7786.         {
  7787.             int len;
  7788.             char * name;
  7789.  
  7790.             /* Get default principal from cache if one exists */
  7791.             code = krb5_cc_get_principal(kcontext, ccache, &me);
  7792. #ifdef HEIMDAL
  7793.             name = me->realm;
  7794.             len = strlen(name);
  7795. #else /* HEIMDAL */
  7796.             len = krb5_princ_realm(kcontext, me)->length;
  7797.             name = krb5_princ_realm(kcontext, me)->data;
  7798. #endif /* HEIMDAL */
  7799.             if (code == 0 && len < sizeof(realm))
  7800.             {
  7801.                 /* Save the realm */
  7802.                 memcpy(realm,name,len); /* safe */
  7803.                 realm[len]='\0';
  7804.             } else {
  7805. #ifdef HAVE_PWD_H
  7806.                 /* Else search passwd file for client */
  7807.  
  7808.                 pw = getpwuid((int) getuid());
  7809.                 if (pw) {
  7810.                     char princ_realm[256];
  7811.                     if ( (strlen(pw->pw_name) + strlen(realm) + 1) > 255 )
  7812.                         goto exit_k5_init;
  7813.  
  7814.                     ckstrncpy(principal,pw->pw_name,256);
  7815.                     ckstrncpy(princ_realm,pw->pw_name,256);
  7816.                     ckstrncat(princ_realm,"@",256);
  7817.                     ckstrncat(princ_realm,realm,256);
  7818.  
  7819.                     if ((code = krb5_parse_name(kcontext,princ_realm,&me))) {
  7820.                         krb5_errno = code;
  7821.                         com_err("krb5_kinit",code,"when parsing name",
  7822.                                   princ_realm);
  7823.                         goto exit_k5_init;
  7824.                     }
  7825.                 } else {
  7826.                     printf(
  7827.                         "Unable to identify user from password file\r\n");
  7828.                     goto exit_k5_init;
  7829.                 }
  7830. #else /* HAVE_PWD_H */
  7831.                 printf("Unable to identify user\r\n");
  7832.                 goto exit_k5_init;
  7833. #endif /* HAVE_PWD_H */
  7834.             }
  7835.         }
  7836.  
  7837. #ifdef HEIMDAL
  7838.         len = me->name.name_string.len;
  7839.         name = *me->name.name_string.val;
  7840. #else /* HEIMDAL */
  7841.         len = krb5_princ_name(kcontext, me)->length;
  7842.         name = krb5_princ_name(kcontext, me)->data;
  7843. #endif /* HEIMDAL */
  7844.         if ( len < sizeof(principal) ) {
  7845.             memcpy(principal,name,len);     /* safe */
  7846.             principal[len]='\0';
  7847.         }
  7848.     } /* Use specified name */
  7849.     else {
  7850.         char princ_realm[256];
  7851.         if ( (strlen(init->principal) +
  7852.               (init->instance ? strlen(init->instance)+1 : 0) +
  7853.               strlen(realm)
  7854.               + 2) > 255 )
  7855.              goto exit_k5_init;
  7856.  
  7857.         ckstrncpy(principal,init->principal,256);
  7858.         ckstrncpy(princ_realm,init->principal,256);
  7859.         if (init->instance) {
  7860.             ckstrncat(princ_realm,"/",256);
  7861.             ckstrncat(princ_realm,init->instance,256);
  7862.         }
  7863.         if (realm[0]) {
  7864.           ckstrncat(princ_realm,"@",256);
  7865.           ckstrncat(princ_realm,realm,256);
  7866.         }
  7867.         if ((code = krb5_parse_name (kcontext, princ_realm, &me))) {
  7868.             com_err("krb5_kinit",code,"when parsing name",princ_realm);
  7869.             goto exit_k5_init;
  7870.         }
  7871.     }
  7872.  
  7873.     if ((code = krb5_unparse_name(kcontext, me, &client_name))) {
  7874.         com_err("krb5_kinit",code,"when unparsing name");
  7875.         goto exit_k5_init;
  7876.     }
  7877.     debug(F110,"krb5_init client_name",client_name,0);
  7878.  
  7879.  
  7880.     memset((char *)&my_creds, 0, sizeof(my_creds));
  7881.     my_creds.client = me;
  7882.  
  7883.     if (init->service == NULL) {
  7884.         if ((code =
  7885.              krb5_build_principal_ext(kcontext,
  7886.                                       &server,
  7887.                                       strlen(realm),realm,
  7888.                                       tgtname.length, tgtname.data,
  7889.                                       strlen(realm),realm,
  7890.                                       0))) {
  7891.             com_err("krb5_kinit",code,"while building server name");
  7892.             goto exit_k5_init;
  7893.         }
  7894.     } else {
  7895.         if (code = krb5_parse_name(kcontext, init->service, &server)) {
  7896.             com_err("krb5_kinit",code,"while parsing service name",
  7897.                     init->service);
  7898.             goto exit_k5_init;
  7899.         }
  7900.     }
  7901.  
  7902.     my_creds.server = server;
  7903.  
  7904.     if (options & KDC_OPT_POSTDATED) {
  7905.         my_creds.times.starttime = starttime;
  7906.         my_creds.times.endtime = starttime + lifetime;
  7907.     } else {
  7908.         my_creds.times.starttime = 0;   /* start timer when request
  7909.                                            gets to KDC */
  7910.         my_creds.times.endtime = now + lifetime;
  7911.     }
  7912.     if (options & KDC_OPT_RENEWABLE) {
  7913.         my_creds.times.renew_till = now + rlife;
  7914.     } else
  7915.         my_creds.times.renew_till = 0;
  7916.  
  7917.     if (options & KDC_OPT_VALIDATE) {
  7918.         krb5_data outbuf;
  7919.  
  7920. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7921.         code = krb5_get_validated_creds(kcontext,
  7922.                                         &my_creds, me, ccache, init->service);
  7923.         if ( code == -1 )
  7924. #endif
  7925.         {
  7926. #ifdef HEIMDAL
  7927.             printf("?validate not implemented\r\n");
  7928.             code = -1;
  7929.             goto exit_k5_init;
  7930. #else /* HEIMDAL */
  7931.             code = krb5_validate_tgt(kcontext, ccache, server, &outbuf);
  7932. #endif /* HEIMDAL */
  7933.         }
  7934.         if (code) {
  7935.             com_err("krb5_kinit",code,"validating tgt");
  7936.             goto exit_k5_init;
  7937.         }
  7938.         /* should be done... */
  7939.         goto exit_k5_init;
  7940.     }
  7941.  
  7942.     if (options & KDC_OPT_RENEW) {
  7943.         krb5_data outbuf;
  7944.  
  7945. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7946.         code = krb5_get_renewed_creds(kcontext,
  7947.                                       &my_creds, me, ccache, init->service);
  7948.         if ( code == -1 )
  7949. #endif
  7950.         {
  7951. #ifdef HEIMDAL
  7952.             printf("?renew not implemented\r\n");
  7953.             code = -1;
  7954.             goto exit_k5_init;
  7955. #else /* HEIMDAL */
  7956.             code = krb5_renew_tgt(kcontext, ccache, server, &outbuf);
  7957. #endif /* HEIMDAL */
  7958.         }
  7959.         if (code) {
  7960.             com_err("krb5_kinit",code,"while renewing tgt");
  7961.             goto exit_k5_init;
  7962.         }
  7963.         /* should be done... */
  7964.         goto store_cred;
  7965.     }
  7966.  
  7967. #ifndef HEIMDAL
  7968.     if ( init->addrs && !init->no_addresses ) {
  7969.         /* construct an array of krb5_address structs to pass to get_in_tkt */
  7970.         /* include both the local ip addresses as well as any other that    */
  7971.         /* are specified.                                                   */
  7972.         unsigned long ipaddr;
  7973.  
  7974.         for ( addr_count=0;addr_count<KRB5_NUM_OF_ADDRS;addr_count++ )
  7975.             if ( init->addrs[addr_count] == NULL )
  7976.                 break;
  7977.  
  7978.         if (addr_count > 0) {
  7979.             krb5_address ** local_addrs=NULL;
  7980.             krb5_os_localaddr(kcontext, &local_addrs);
  7981.             i = 0;
  7982.             while ( local_addrs[i] )
  7983.                 i++;
  7984.             addr_count += i;
  7985.  
  7986.             addrs = (krb5_address **)
  7987.               malloc((addr_count+1) * sizeof(krb5_address *));
  7988.             if ( !addrs ) {
  7989.                 krb5_free_addresses(kcontext, local_addrs);
  7990.                 goto exit_k5_init;
  7991.             }
  7992.             memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
  7993.             i = 0;
  7994.             while ( local_addrs[i] ) {
  7995.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7996.                 if (addrs[i] == NULL) {
  7997.                     krb5_free_addresses(kcontext, local_addrs);
  7998.                     goto exit_k5_init;
  7999.                 }
  8000.  
  8001.                 addrs[i]->magic = local_addrs[i]->magic;
  8002.                 addrs[i]->addrtype = local_addrs[i]->addrtype;
  8003.                 addrs[i]->length = local_addrs[i]->length;
  8004.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  8005.                 if (!addrs[i]->contents) {
  8006.                     krb5_free_addresses(kcontext, local_addrs);
  8007.                     goto exit_k5_init;
  8008.                 }
  8009.  
  8010.                 memcpy(addrs[i]->contents,local_addrs[i]->contents,
  8011.                         local_addrs[i]->length);        /* safe */
  8012.                 i++;
  8013.             }
  8014.             krb5_free_addresses(kcontext, local_addrs);
  8015.  
  8016.             for ( j=0;i<addr_count;i++,j++ ) {
  8017.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  8018.                 if (addrs[i] == NULL)
  8019.                     goto exit_k5_init;
  8020.  
  8021.                 addrs[i]->magic = KV5M_ADDRESS;
  8022.                 addrs[i]->addrtype = AF_INET;
  8023.                 addrs[i]->length = 4;
  8024.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  8025.                 if (!addrs[i]->contents)
  8026.                     goto exit_k5_init;
  8027.  
  8028.                 ipaddr = inet_addr(init->addrs[j]);
  8029.                 memcpy(addrs[i]->contents,&ipaddr,4);   /* safe */
  8030.             }
  8031. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8032.             krb5_get_init_creds_opt_set_address_list(&opts,addrs);
  8033. #endif
  8034.         }
  8035.      }
  8036. #endif /* !HEIMDAL */
  8037. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8038.     if ( init->no_addresses )
  8039.         krb5_get_init_creds_opt_set_address_list(&opts,NULL);
  8040. #endif
  8041.  
  8042. #ifndef NO_KEYTAB
  8043.     if (!use_keytab)
  8044. #endif
  8045.     {
  8046.         if ( init->password ) {
  8047.             pwsize = strlen(init->password);
  8048.             if ( pwsize )
  8049.                 password = init->password;
  8050.         } else if (init->getk4 && k4_init) {
  8051.             /* When we are requesting that K4 tickets be automatically */
  8052.             /* acquired when K5 tickets are acquired, we must get the  */
  8053.             /* password up front.                                      */
  8054.             char prmpt[256];
  8055.             extern char * k5prprompt;
  8056.             extern char * k5pwprompt;
  8057.             int ok = 0;
  8058.  
  8059.             if ( k5pwprompt && k5pwprompt[0] &&
  8060.                  (strlen(k5pwprompt) + strlen(principal) +
  8061.                   strlen(realm) - 4) < sizeof(prmpt)) {
  8062.                 sprintf(prmpt,k5pwprompt,principal,realm);
  8063.             } else
  8064.                 ckmakxmsg(prmpt,sizeof(prmpt),
  8065.                            k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  8066.                            "Kerberos 5 Password for ",
  8067.                            principal,"@",realm,": ",
  8068.                            NULL,NULL,NULL,NULL,NULL,NULL,NULL
  8069.                            );
  8070.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  8071.             if ( ok )
  8072.                 password = passwd;
  8073.  
  8074.             if ( k4_init->password == NULL )
  8075.                 makestr(&k4_init->password,passwd);
  8076.         }
  8077. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8078.         debug(F100,"krb5_init calling krb5_get_init_creds_password()","",0);
  8079. #ifdef OS2
  8080.         if ( is_NRL_KRB5() )
  8081.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  8082.                                                  password,
  8083.                                                  (void *)ck_NRL_krb5_prompter,
  8084.                                                  NULL,
  8085.                                                  starttime, init->service,
  8086.                                                  &opts);
  8087.         else
  8088. #endif /* OS2 */
  8089.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  8090.                                                  password,
  8091.                                                  ck_krb5_prompter,
  8092.                                                  NULL,
  8093.                                                  starttime, init->service,
  8094.                                                  &opts);
  8095.         debug(F111,"krb5_init","krb5_get_init_creds_password()",code);
  8096.  
  8097.         if ( code == -1 )
  8098.         {
  8099.             if (!password) {
  8100.                 char prmpt[256];
  8101.                 int ok;
  8102.  
  8103.                 ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  8104.                           client_name,": ",NULL);
  8105.                 ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,
  8106.                 DEFAULT_UQ_TIMEOUT);
  8107.                 if ( ok )
  8108.                     password = passwd;
  8109.                 else {
  8110.                     code = -2;
  8111.                     goto exit_k5_init;
  8112.                 }
  8113.             }
  8114.  
  8115.             if ( !password ) 
  8116.                 password = "";
  8117.             code = krb5_get_in_tkt_with_password(kcontext, options,
  8118. #ifdef HEIMDAL
  8119.                                                   NULL,
  8120. #else /* HEIMDAL */
  8121.                                             init->no_addresses ? NULL :addrs,
  8122. #endif /* HEIMDAL */
  8123.                                                   NULL, NULL,
  8124.                                                   password,
  8125.                                                   NULL, &my_creds, NULL);
  8126.             if ( code )
  8127.                 debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  8128.         }
  8129. #else /* KRB5_HAVE_GET_INIT_CREDS */
  8130.         if (!password) {
  8131.             char prmpt[256];
  8132.             int ok;
  8133.  
  8134.             ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  8135.                       client_name,": ",NULL);
  8136.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  8137.             if ( ok )
  8138.                 password = passwd;
  8139.             else {
  8140.                 code = -2;
  8141.                 goto exit_k5_init;
  8142.             }
  8143.         }
  8144.         if ( !password ) 
  8145.             password = "";
  8146.         code = krb5_get_in_tkt_with_password(kcontext, options,
  8147. #ifdef HEIMDAL
  8148.                                               NULL,
  8149. #else /* HEIMDAL */
  8150.                                             init->no_addresses ? NULL :addrs,
  8151. #endif /* HEIMDAL */
  8152.                                               NULL, NULL,
  8153.                                               password,
  8154.                                               NULL, &my_creds, NULL);
  8155.         if ( code )
  8156.             debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  8157. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  8158.  
  8159.         if ( init->password && pwsize > 0 )
  8160.             memset(init->password, 0, pwsize);
  8161.         memset(passwd,0,80);
  8162.     }
  8163. #ifndef NO_KEYTAB
  8164.     else {
  8165. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8166.         code = krb5_get_init_creds_keytab(kcontext, &my_creds, me, keytab,
  8167.                                            starttime, init->service,
  8168.                                            &opts);
  8169. #ifdef OS2
  8170.         if ( code == -1)
  8171.             code = krb5_get_in_tkt_with_keytab(kcontext, options,
  8172.                                            init->no_addresses ? NULL :addrs,
  8173.                                                 NULL, NULL, keytab, NULL,
  8174.                                                 &my_creds, 0);
  8175. #endif /* OS2 */
  8176. #else /* KRB5_HAVE_GET_INIT_CREDS */
  8177.         code = krb5_get_in_tkt_with_keytab(kcontext, options,
  8178. #ifdef HEIMDAL
  8179.                                                   NULL,
  8180. #else /* HEIMDAL */
  8181.                                             init->no_addresses ? NULL :addrs,
  8182. #endif /* HEIMDAL */
  8183.                                             NULL, NULL, keytab, NULL,
  8184.                                             &my_creds, 0);
  8185. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  8186.     }
  8187. #endif
  8188.  
  8189.     if (code) {
  8190.         switch (code) {
  8191.         case KRB5KRB_AP_ERR_BAD_INTEGRITY:
  8192.             printf("Password incorrect\r\n");
  8193.             goto exit_k5_init;
  8194.         case KRB5KRB_AP_ERR_V4_REPLY:
  8195.             if (init->getk4 && k4_init) {
  8196.                 printf("Kerberos 5 Tickets not support by server.  ");
  8197.                 printf("A version 4 Ticket will be requested.\r\n");
  8198.             }
  8199.             goto exit_k5_init;
  8200.         default:
  8201.             goto exit_k5_init;
  8202.         }
  8203.     }
  8204.  
  8205.   store_cred:
  8206.     debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8207.  
  8208.     code = krb5_cc_initialize (kcontext, ccache, me);
  8209.     if ( code == KRB5_CC_BADNAME ) {
  8210.         /* This is a really ugly hack that should not have to be here.
  8211.          * krb5_cc_initialize should not fail with an error if the
  8212.          * cache already exists.  The reason the problem is occuring
  8213.          * is that the krb5 library is no longer calling cc_destroy()
  8214.          * when cc_initialize() is called and the CCAPI implementation
  8215.          * on Windows has not yet been corrected to handle it.  To
  8216.          * ensure that K95 will continue to work with both we will call
  8217.          * cc_destroy() if the cc_initialize() call fails with a BADNAME
  8218.          * error.  If the cc_destroy() is successful, we will try again.
  8219.          */
  8220.  
  8221.         debug(F100,"krb5_init calling krb5_cc_destroy()","",0);
  8222.         code = krb5_cc_destroy (kcontext, ccache);
  8223.         if ( !code ) {
  8224.             debug(F100,"krb5_init calling k5_get_ccache()","",0);
  8225.             code = k5_get_ccache(kcontext,&ccache,op->cache);
  8226.             debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8227.             code = krb5_cc_initialize (kcontext, ccache, me);
  8228.         } else
  8229.             code = KRB5_CC_BADNAME;
  8230.     }
  8231.     if (code) {
  8232.         com_err("krb5_kinit",code,"when initializing cache",op->cache);
  8233.         goto exit_k5_init;
  8234.     }
  8235.  
  8236.     debug(F100,"krb5_init calling krb5_cc_store_cred()","",0);
  8237.     code = krb5_cc_store_cred(kcontext, ccache, &my_creds);
  8238.     if (code) {
  8239.         com_err("krb5_kinit",code,"while storing credentials");
  8240.         goto exit_k5_init;
  8241.     }
  8242.  
  8243.     if ( init->getk4 && 
  8244. #ifdef KRB524_CONV
  8245.          !try_convert524(kcontext,me,ccache) && 
  8246. #endif /* KRB524_CONV */
  8247.          k4_init ) {
  8248.         int k4rc = ck_krb4_initTGT(op,k4_init);
  8249.         if (k4rc < 0)
  8250.             code = -3;
  8251.     }
  8252.  
  8253. exit_k5_init:
  8254.     debug(F100,"krb5_init exit_k5_init","",0);
  8255.  
  8256. #ifndef HEIMDAL
  8257.     /* Free krb5_address structures if we created them */
  8258.     if ( addrs ) {
  8259.         for ( i=0;i<addr_count;i++ ) {
  8260.             if ( addrs[i] ) {
  8261.                 if ( addrs[i]->contents )
  8262.                     free(addrs[i]->contents);
  8263.                 free(addrs[i]);
  8264.             }
  8265.         }
  8266.     }
  8267. #endif /* HEIMDAL */
  8268.  
  8269.  
  8270.     krb5_errno = code;
  8271.     makestr(&krb5_errmsg,krb5_errno ? error_message(krb5_errno) : "OK");
  8272.  
  8273.     if (client_name)
  8274.         krb5_free_unparsed_name(kcontext, client_name);
  8275.  
  8276.     /* my_creds is pointing at server */
  8277.     debug(F100,"krb5_init calling krb5_free_principal()","",0);
  8278.     krb5_free_principal(kcontext, server);
  8279.     debug(F100,"krb5_init calling krb5_cc_close()","",0);
  8280.     krb5_cc_close(kcontext,ccache);
  8281.     debug(F100,"krb5_init calling krb5_free_context()","",0);
  8282.     krb5_free_context(kcontext);
  8283.  
  8284.     if (code != -2)
  8285.         printf("Result from realm %s: %s\r\n",realm,
  8286.                 code==-3?"Unable to retrieve Kerberos IV credentials":
  8287.                 code?error_message(code):"OK");
  8288.     return(code?-1:0);
  8289. }
  8290.  
  8291. #ifndef HEIMDAL
  8292. #define VALIDATE 0
  8293. #define RENEW 1
  8294.  
  8295. /* stripped down version of krb5_mk_req */
  8296. static krb5_error_code
  8297. #ifdef CK_ANSIC
  8298. krb5_validate_tgt( krb5_context context,
  8299.                    krb5_ccache ccache,
  8300.                    krb5_principal     server, /* tgtname */
  8301.                    krb5_data *outbuf )
  8302. #else
  8303. krb5_validate_tgt(context, ccache, server, outbuf)
  8304.      krb5_context context;
  8305.      krb5_ccache ccache;
  8306.      krb5_principal     server; /* tgtname */
  8307.      krb5_data *outbuf;
  8308. #endif
  8309. {
  8310.     return krb5_tgt_gen(context, ccache, server, outbuf, VALIDATE);
  8311. }
  8312.  
  8313. /* stripped down version of krb5_mk_req */
  8314. static krb5_error_code
  8315. #ifdef CK_ANSIC
  8316. krb5_renew_tgt(krb5_context context,
  8317.                 krb5_ccache ccache,
  8318.                 krb5_principal    server, /* tgtname */
  8319.                 krb5_data *outbuf)
  8320. #else
  8321. krb5_renew_tgt(context, ccache, server, outbuf)
  8322.      krb5_context context;
  8323.      krb5_ccache ccache;
  8324.      krb5_principal       server; /* tgtname */
  8325.      krb5_data *outbuf;
  8326. #endif
  8327. {
  8328.     return krb5_tgt_gen(context, ccache, server, outbuf, RENEW);
  8329. }
  8330.  
  8331.  
  8332. /* stripped down version of krb5_mk_req */
  8333. static krb5_error_code
  8334. #ifdef CK_ANSIC
  8335. krb5_tgt_gen(krb5_context context,
  8336.               krb5_ccache ccache,
  8337.               krb5_principal      server, /* tgtname */
  8338.               krb5_data *outbuf,
  8339.               int opt)
  8340. #else
  8341. krb5_tgt_gen(context, ccache, server, outbuf, opt)
  8342.      krb5_context context;
  8343.      krb5_ccache ccache;
  8344.      krb5_principal       server; /* tgtname */
  8345.      krb5_data *outbuf;
  8346.      int opt;
  8347. #endif
  8348. {
  8349.     krb5_error_code       retval;
  8350.     krb5_creds          * credsp;
  8351.     krb5_creds            creds;
  8352.  
  8353.     /* obtain ticket & session key */
  8354.     memset((char *)&creds, 0, sizeof(creds));
  8355.     if ((retval = krb5_copy_principal(context, server, &creds.server)))
  8356.         goto cleanup;
  8357.  
  8358.     if ((retval = krb5_cc_get_principal(context, ccache, &creds.client)))
  8359.         goto cleanup_creds;
  8360.  
  8361.     if (opt == VALIDATE) {
  8362.         if ((retval = krb5_get_credentials_validate(context, 0,
  8363.                                                     ccache, &creds, &credsp)))
  8364.           goto cleanup_creds;
  8365.     } else {
  8366.         if ((retval = krb5_get_credentials_renew(context, 0,
  8367.                                                  ccache, &creds, &credsp)))
  8368.           goto cleanup_creds;
  8369.     }
  8370.  
  8371.     /* we don't actually need to do the mk_req, just get the creds. */
  8372. cleanup_creds:
  8373.     krb5_free_cred_contents(context, &creds);
  8374.  
  8375. cleanup:
  8376.  
  8377.     return retval;
  8378. }
  8379. #endif /* HEIMDAL */
  8380. #endif /* KINIT */
  8381. #ifdef KDESTROY
  8382. int
  8383. #ifdef CK_ANSIC
  8384. ck_krb5_destroy(struct krb_op_data * op)
  8385. #else
  8386. ck_krb5_destroy(op) struct krb_op_data * op;
  8387. #endif
  8388. {
  8389.     krb5_context kcontext;
  8390.     krb5_error_code retval;
  8391.     int c;
  8392.     krb5_ccache ccache = NULL;
  8393.     char *cache_name = NULL;
  8394.     int code;
  8395.     int errflg=0;
  8396.     int quiet = 0;
  8397.  
  8398.     if ( !ck_krb5_is_installed() )
  8399.         return(-1);
  8400.  
  8401.     code = krb5_init_context(&kcontext);
  8402.     if (code) {
  8403.         debug(F101,"ck_krb5_destroy while initializing krb5","",code);
  8404.         krb5_errno = code;
  8405.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8406.         return(-1);
  8407.     }
  8408.  
  8409.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  8410.     if (code != 0) {
  8411.         debug(F101,"ck_krb5_destroy while getting ccache",
  8412.                "",code);
  8413.         krb5_free_context(kcontext);
  8414.         krb5_errno = code;
  8415.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8416.         return(-1);
  8417.     }
  8418.  
  8419.     code = krb5_cc_destroy (kcontext, ccache);
  8420.     if (code != 0) {
  8421.         debug(F101,"ck_krb5_destroy while destroying cache","",code);
  8422.         if ( code == KRB5_FCC_NOFILE )
  8423.             printf("No ticket cache to destroy.\r\n");
  8424.         else
  8425.             printf("Ticket cache NOT destroyed!\r\n");
  8426.         krb5_cc_close(kcontext,ccache);
  8427.         krb5_free_context(kcontext);
  8428.         krb5_errno = code;
  8429.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8430.         return(-1);
  8431.     }
  8432.  
  8433.     printf("Tickets destroyed.\r\n");
  8434.     /* Do not call krb5_cc_close() because cache has been destroyed */
  8435.     krb5_free_context(kcontext);
  8436.     krb5_errno = 0;
  8437.     makestr(&krb5_errmsg,"OK");
  8438.     return (0);
  8439. }
  8440. #else /* KDESTROY */
  8441. #ifdef KRB5
  8442. int
  8443. #ifdef CK_ANSIC
  8444. ck_krb5_destroy(struct krb_op_data * op)
  8445. #else
  8446. ck_krb5_destroy(op) struct krb_op_data * op;
  8447. #endif
  8448. {
  8449.     printf("?Not implemented.\r\n");
  8450.     return(-1);
  8451. }
  8452. #endif /* KRB5 */
  8453. #endif /* KDESTROY */
  8454. #ifndef KLIST
  8455. #ifdef KRB5
  8456. int
  8457. #ifdef CK_ANSIC
  8458. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8459. #else
  8460. ck_krb5_list_creds(op,lc)
  8461.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8462. #endif
  8463. {
  8464.     printf("?Not implemented.\r\n");
  8465.     return(-1);
  8466. }
  8467. #endif /* KRB5 */
  8468. #else /* KLIST */
  8469. static int show_flags = 0, show_time = 0, status_only = 0, show_keys = 0;
  8470. static int show_etype = 0, show_addr = 0;
  8471. static char *defname;
  8472. static char *progname;
  8473. static krb5_int32 now;
  8474. static int timestamp_width;
  8475.  
  8476. _PROTOTYP(static char * etype_string, (krb5_enctype ));
  8477. _PROTOTYP(static void show_credential,(krb5_context,krb5_creds *));
  8478. _PROTOTYP(static int do_ccache, (krb5_context,char *));
  8479. _PROTOTYP(static int do_keytab, (krb5_context,char *));
  8480. _PROTOTYP(static void printtime, (time_t));
  8481. _PROTOTYP(static void fillit, (int, int));
  8482.  
  8483. #define DEFAULT 0
  8484. #define CCACHE 1
  8485. #define KEYTAB 2
  8486.  
  8487. int
  8488. #ifdef CK_ANSIC
  8489. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8490. #else
  8491. ck_krb5_list_creds(op,lc)
  8492.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8493. #endif
  8494. {
  8495.     krb5_context kcontext;
  8496.     krb5_error_code retval;
  8497.     int code;
  8498.     char *name = op->cache;
  8499.     int mode;
  8500.  
  8501.     if ( !ck_krb5_is_installed() )
  8502.         return(-1);
  8503.  
  8504.     code = krb5_init_context(&kcontext);
  8505.     if (code) {
  8506.         debug(F101,"ck_krb5_list_creds while initializing krb5","",code);
  8507.         krb5_errno = code;
  8508.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8509.         return(-1);
  8510.     }
  8511.  
  8512.     name = op->cache;
  8513.     mode = DEFAULT;
  8514.     show_flags = 0;
  8515.     show_time = 0;
  8516.     status_only = 0;
  8517.     show_keys = 0;
  8518.     show_etype = 0;
  8519.     show_addr = 0;
  8520.  
  8521.     show_flags = lc->flags;
  8522.     show_etype = lc->encryption;
  8523.     show_addr  = lc->addr;
  8524.     show_time = 1;
  8525.     show_keys = 1;
  8526.     mode = CCACHE;
  8527.  
  8528.     if ((code = krb5_timeofday(kcontext, &now))) {
  8529.         if (!status_only)
  8530.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  8531.                    "",code);
  8532.         krb5_free_context(kcontext);
  8533.         krb5_errno = code;
  8534.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8535.         return(-1);
  8536.     }
  8537.     else {
  8538.         char tmp[BUFSIZ];
  8539.  
  8540.         if (!krb5_timestamp_to_sfstring(now, tmp, 20, (char *) NULL) ||
  8541.             !krb5_timestamp_to_sfstring(now, tmp, sizeof(tmp), (char *) NULL))
  8542.             timestamp_width = (int) strlen(tmp);
  8543.         else
  8544.             timestamp_width = 15;
  8545.     }
  8546.  
  8547.     if (mode == DEFAULT || mode == CCACHE)
  8548.          retval = do_ccache(kcontext,name);
  8549.     else
  8550.          retval = do_keytab(kcontext,name);
  8551.     krb5_free_context(kcontext);
  8552.     return(retval);
  8553. }
  8554.  
  8555. static int
  8556. #ifdef CK_ANSIC
  8557. do_keytab(krb5_context kcontext, char * name)
  8558. #else
  8559. do_keytab(kcontext,name) krb5_context kcontext; char * name;
  8560. #endif
  8561. {
  8562.     krb5_keytab kt;
  8563.     krb5_keytab_entry entry;
  8564.     krb5_kt_cursor cursor;
  8565.     char buf[BUFSIZ]; /* hopefully large enough for any type */
  8566.     char *pname;
  8567.     int code = 0;
  8568.  
  8569.     if (name == NULL) {
  8570.         if ((code = krb5_kt_default(kcontext, &kt))) {
  8571.             debug(F101,"ck_krb5_list_creds while getting default keytab",
  8572.                    "",code);
  8573.             krb5_errno = code;
  8574.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8575.             return(-1);
  8576.         }
  8577.     } else {
  8578.         if ((code = krb5_kt_resolve(kcontext, name, &kt))) {
  8579.             debug(F111,"ck_krb5_list_creds while resolving keytab",
  8580.                      name,code);
  8581.             krb5_errno = code;
  8582.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8583.             return(-1);
  8584.         }
  8585.     }
  8586.  
  8587.     if ((code = krb5_kt_get_name(kcontext, kt, buf, BUFSIZ))) {
  8588.         debug(F101,"ck_krb5_list_creds while getting keytab name",
  8589.                "",code);
  8590.         krb5_errno = code;
  8591.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8592.         return(-1);
  8593.     }
  8594.  
  8595.      printf("Keytab name: %s\r\n", buf);
  8596.  
  8597.      if ((code = krb5_kt_start_seq_get(kcontext, kt, &cursor))) {
  8598.          debug(F101,"ck_krb5_list_creds while starting keytab scan",
  8599.                 "",code);
  8600.          krb5_errno = code;
  8601.          makestr(&krb5_errmsg,error_message(krb5_errno));
  8602.          return(-1);
  8603.      }
  8604.  
  8605.      if (show_time) {
  8606.           printf("KVNO Timestamp");
  8607.           fillit(timestamp_width - sizeof("Timestamp") + 2, (int) ' ');
  8608.           printf("Principal\r\n");
  8609.           printf("---- ");
  8610.           fillit(timestamp_width, (int) '-');
  8611.           printf(" ");
  8612.           fillit(78 - timestamp_width - sizeof("KVNO"), (int) '-');
  8613.           printf("\r\n");
  8614.      } else {
  8615.           printf("KVNO Principal\r\n");
  8616.           printf(
  8617. "---- --------------------------------------------------------------------\
  8618. ------\r\n");
  8619.      }
  8620.  
  8621.     while ((code = krb5_kt_next_entry(kcontext, kt, &entry, &cursor)) == 0) {
  8622.         if ((code = krb5_unparse_name(kcontext, entry.principal, &pname))) {
  8623.             debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8624.                    "",code);
  8625.             krb5_errno = code;
  8626.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8627.             return(-1);
  8628.         }
  8629.         printf("%4d ", entry.vno);
  8630.         if (show_time) {
  8631.             printtime(entry.timestamp);
  8632.             printf(" ");
  8633.         }
  8634.         printf("%s", pname);
  8635.         if (show_etype)
  8636.             printf(" (%s) " ,
  8637. #ifdef HEIMDAL
  8638.                     etype_string(entry.key.keytype)
  8639. #else /* HEIMDAL */
  8640.                     etype_string(entry.key.enctype)
  8641. #endif /* HEIMDAL */
  8642.                     );
  8643.         if (show_keys) {
  8644.             printf(" (0x");
  8645.             {
  8646.                 int i;
  8647.                 for (i = 0; i < entry.key.length; i++)
  8648.                     printf("%02x",
  8649. #ifdef HEIMDAL
  8650.                             entry.key.keyvalue[i]
  8651. #else /* HEIMDAL */
  8652.                             entry.key.contents[i]
  8653. #endif /* HEIMDAL */
  8654.                             );
  8655.             }
  8656.             printf(")");
  8657.         }
  8658.         printf("\r\n");
  8659.         krb5_free_unparsed_name(kcontext,pname);
  8660.     }
  8661.     if (code && code != KRB5_KT_END) {
  8662.         debug(F101,"ck_krb5_list_creds while scanning keytab",
  8663.                "",code);
  8664.         krb5_errno = code;
  8665.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8666.         return(-1);
  8667.     }
  8668.     if ((code = krb5_kt_end_seq_get(kcontext, kt, &cursor))) {
  8669.         debug(F101,"ck_krb5_list_creds while ending keytab scan",
  8670.                "",code);
  8671.         krb5_errno = code;
  8672.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8673.         return(-1);
  8674.     }
  8675.     krb5_errno = 0;
  8676.     makestr(&krb5_errmsg,"OK");
  8677.     return(0);
  8678. }
  8679.  
  8680. static int
  8681. #ifdef CK_ANSIC
  8682. do_ccache(krb5_context kcontext, char * cc_name)
  8683. #else
  8684. do_ccache(kcontext,name) krb5_context kcontext; char * cc_name;
  8685. #endif
  8686. {
  8687.     krb5_ccache cache = NULL;
  8688.     krb5_cc_cursor cur;
  8689.     krb5_creds creds;
  8690.     krb5_principal princ=NULL;
  8691.     krb5_flags flags=0;
  8692.     krb5_error_code code = 0;
  8693.     int exit_status = 0;
  8694.  
  8695.     if (status_only)
  8696.         /* exit_status is set back to 0 if a valid tgt is found */
  8697.         exit_status = 1;
  8698.  
  8699.     code = k5_get_ccache(kcontext,&cache,cc_name);
  8700.     if (code != 0) {
  8701.         debug(F111,"do_ccache while getting ccache",
  8702.                error_message(code),code);
  8703.         krb5_errno = code;
  8704.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8705.         return(-1);
  8706.     }
  8707.  
  8708.     flags = 0;                          /* turns off OPENCLOSE mode */
  8709.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8710.         if (code == ENOENT) {
  8711.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  8712.                    krb5_cc_get_name(kcontext, cache),code);
  8713.         } else {
  8714.             debug(F111,
  8715.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  8716.                   krb5_cc_get_name(kcontext, cache),code);
  8717.         }
  8718.         printf("No ticket File.\r\n");
  8719.         krb5_errno = code;
  8720.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8721.         krb5_cc_close(kcontext,cache);
  8722.         return(-1);
  8723.     }
  8724.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  8725.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  8726.                "",code);
  8727.         krb5_errno = code;
  8728.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8729.         krb5_cc_close(kcontext,cache);
  8730.         return(-1);
  8731.     }
  8732.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  8733.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8734.                "",code);
  8735.         krb5_errno = code;
  8736.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8737.         krb5_cc_close(kcontext,cache);
  8738.         return(-1);
  8739.     }
  8740.     if (!status_only) {
  8741.         printf("Ticket cache:      %s:%s\r\nDefault principal: %s\r\n\r\n",
  8742.                 krb5_cc_get_type(kcontext, cache),
  8743.                 krb5_cc_get_name(kcontext, cache), defname);
  8744.         printf("Valid starting");
  8745.         fillit(timestamp_width - sizeof("Valid starting") + 3,
  8746.                (int) ' ');
  8747.         printf("Expires");
  8748.         fillit(timestamp_width - sizeof("Expires") + 3,
  8749.                (int) ' ');
  8750.         printf("Service principal\r\n");
  8751.     }
  8752.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  8753.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  8754.                "",code);
  8755.         krb5_errno = code;
  8756.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8757.         krb5_cc_close(kcontext,cache);
  8758.         return(-1);
  8759.     }
  8760.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  8761.         if (status_only) {
  8762.             if (exit_status && creds.server->length == 2 &&
  8763.                 strcmp(creds.server->realm.data, princ->realm.data) == 0 &&
  8764.                 strcmp((char *)creds.server->data[0].data, "krbtgt") == 0 &&
  8765.                 strcmp((char *)creds.server->data[1].data,
  8766.                        princ->realm.data) == 0 &&
  8767.                 creds.times.endtime > now)
  8768.                 exit_status = 0;
  8769.         } else {
  8770.             show_credential(kcontext, &creds);
  8771.         }
  8772.         krb5_free_cred_contents(kcontext, &creds);
  8773.     }
  8774.     printf("\r\n");
  8775.     if (code == KRB5_CC_END || code == KRB5_CC_NOTFOUND) {
  8776.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  8777.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  8778.                    "",code);
  8779.             krb5_errno = code;
  8780.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8781.             krb5_cc_close(kcontext,cache);
  8782.             return(-1);
  8783.         }
  8784.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  8785.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8786.             debug(F101,"ck_krb5_list_creds while closing ccache",
  8787.                    "",code);
  8788.             krb5_errno = code;
  8789.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8790.             krb5_cc_close(kcontext,cache);
  8791.             return(-1);
  8792.         }
  8793.         krb5_errno = 0;
  8794.         makestr(&krb5_errmsg,"OK");
  8795.         krb5_cc_close(kcontext,cache);
  8796.         return(0);
  8797.     } else {
  8798.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  8799.         krb5_errno = code;
  8800.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8801.         krb5_cc_close(kcontext,cache);
  8802.         return(-1);
  8803.     }
  8804.     krb5_errno = 0;
  8805.     makestr(&krb5_errmsg,"OK");
  8806.     krb5_cc_close(kcontext,cache);
  8807.     return(0);
  8808. }
  8809.  
  8810. static char *
  8811. #ifdef CK_ANSIC
  8812. #ifdef HEIMDAL
  8813. etype_string(krb5_keytype enctype)
  8814. #else /* HEIMDAL */
  8815. etype_string(krb5_enctype enctype)
  8816. #endif /* HEIMDAL */
  8817. #else
  8818. #ifdef HEIMDAL
  8819. etype_string(enctype) krb5_keytype enctype;
  8820. #else /* HEIMDAL */
  8821. etype_string(enctype) krb5_enctype enctype;
  8822. #endif /* HEIMDAL */
  8823. #endif
  8824. {
  8825.     static char buf[12];
  8826.  
  8827.     switch (enctype) {
  8828.     case ENCTYPE_NULL:
  8829.         return "NULL";
  8830.     case ENCTYPE_DES_CBC_CRC:
  8831.         return "DES-CBC-CRC";
  8832.     case ENCTYPE_DES_CBC_MD4:
  8833.         return "DES-CBC-MD4";
  8834.     case ENCTYPE_DES_CBC_MD5:
  8835.         return "DES-CBC-MD5";
  8836.     case ENCTYPE_DES_CBC_RAW:
  8837.         return "DES-CBC-RAW";
  8838.     case ENCTYPE_DES3_CBC_SHA:
  8839.         return "DES3-CBC-SHA";
  8840.     case ENCTYPE_DES3_CBC_RAW:
  8841.         return "DES3-CBC-RAW";
  8842.     case ENCTYPE_DES_HMAC_SHA1:
  8843.         return "DES-HMAC-SHA1";
  8844.     case ENCTYPE_DES3_CBC_SHA1:
  8845.         return "DES3-CBC-SHA1";
  8846.     case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  8847.         return "AES128_CTS-HMAC-SHA1_96";
  8848.     case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  8849.         return "AES256_CTS-HMAC-SHA1_96";
  8850.     case ENCTYPE_ARCFOUR_HMAC:
  8851.         return "RC4-HMAC-NT";
  8852.     case ENCTYPE_ARCFOUR_HMAC_EXP:
  8853.         return "RC4-HMAC-NT-EXP";
  8854.     case ENCTYPE_UNKNOWN:
  8855.         return "UNKNOWN";
  8856.     case ENCTYPE_LOCAL_DES3_HMAC_SHA1:
  8857.         return "LOCAL-DES3-HMAC-SHA1";
  8858.     case ENCTYPE_LOCAL_RC4_MD4:
  8859.         return "LOCAL-RC4-MD4";
  8860.     default:
  8861.         ckmakmsg(buf, sizeof(buf),"etype ", ckitoa(enctype),NULL,NULL);
  8862.         return buf;
  8863.         break;
  8864.     }
  8865. }
  8866.  
  8867. static char *
  8868. #ifdef CK_ANSIC
  8869. flags_string(register krb5_creds *cred)
  8870. #else
  8871. flags_string(cred) register krb5_creds *cred;
  8872. #endif
  8873. {
  8874.     static char buf[32];
  8875.     int i = 0;
  8876.  
  8877.     if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
  8878.         buf[i++] = 'F';
  8879.     if (cred->ticket_flags & TKT_FLG_FORWARDED)
  8880.         buf[i++] = 'f';
  8881.     if (cred->ticket_flags & TKT_FLG_PROXIABLE)
  8882.         buf[i++] = 'P';
  8883.     if (cred->ticket_flags & TKT_FLG_PROXY)
  8884.         buf[i++] = 'p';
  8885.     if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
  8886.         buf[i++] = 'D';
  8887.     if (cred->ticket_flags & TKT_FLG_POSTDATED)
  8888.         buf[i++] = 'd';
  8889.     if (cred->ticket_flags & TKT_FLG_INVALID)
  8890.         buf[i++] = 'i';
  8891.     if (cred->ticket_flags & TKT_FLG_RENEWABLE)
  8892.         buf[i++] = 'R';
  8893.     if (cred->ticket_flags & TKT_FLG_INITIAL)
  8894.         buf[i++] = 'I';
  8895.     if (cred->ticket_flags & TKT_FLG_HW_AUTH)
  8896.         buf[i++] = 'H';
  8897.     if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
  8898.         buf[i++] = 'A';
  8899.     buf[i] = '\0';
  8900.     return(buf);
  8901. }
  8902.  
  8903. static char   *
  8904. #ifdef CK_ANSIC
  8905. short_date(long   *dp)
  8906. #else
  8907. short_date(dp) long   *dp;
  8908. #endif
  8909. {
  8910.     register char *cp;
  8911. #ifndef ctime
  8912.     extern char *ctime();
  8913. #endif /* ctime */
  8914.     cp = ctime(dp) + 4;
  8915.     cp[15] = '\0';
  8916.     return (cp);
  8917. }
  8918.  
  8919.  
  8920. static VOID
  8921. #ifdef CK_ANSIC
  8922. printtime(time_t tv)
  8923. #else
  8924. printtime(tv) time_t tv;
  8925. #endif
  8926. {
  8927.     char timestring[BUFSIZ];
  8928.     char format[12];
  8929.     char fill;
  8930.  
  8931.     fill = ' ';
  8932.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  8933.     if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv,
  8934.                                      timestring,
  8935.                                      timestamp_width+1,
  8936.                                      &fill)) {
  8937.         printf(format,timestring);
  8938.     }
  8939.     else {
  8940.         printf(format,short_date(&tv));
  8941.     }
  8942.  
  8943. }
  8944.  
  8945. static void
  8946. #ifdef CK_ANSIC
  8947. one_addr(krb5_address *a)
  8948. #else
  8949. one_addr(a) krb5_address *a;
  8950. #endif
  8951. {
  8952.     struct hostent *h;
  8953.     extern tcp_rdns;
  8954.  
  8955.     if ((a->addrtype == ADDRTYPE_INET) &&
  8956.         (a->length == 4)) {
  8957.         if (tcp_rdns != SET_OFF) {
  8958.             h = gethostbyaddr(a->contents, 4, AF_INET);
  8959.             if (h) {
  8960. #ifdef HADDRLIST
  8961.                 h = ck_copyhostent(h);
  8962. #endif /* HADDRLIST */
  8963.                 printf("%s (%d.%d.%d.%d)", h->h_name,
  8964.                         a->contents[0], a->contents[1],
  8965.                         a->contents[2], a->contents[3]);
  8966.             }
  8967.         }
  8968.         if (tcp_rdns == SET_OFF || !h) {
  8969.             printf("%d.%d.%d.%d", a->contents[0], a->contents[1],
  8970.                    a->contents[2], a->contents[3]);
  8971.         }
  8972.     } else {
  8973.         printf("unknown addr type %d", a->addrtype);
  8974.     }
  8975. }
  8976.  
  8977. static VOID
  8978. #ifdef CK_ANSIC
  8979. show_credential(krb5_context kcontext, register krb5_creds * cred)
  8980. #else
  8981. show_credential(kcontext, cred)
  8982.     krb5_context          kcontext;
  8983.     register krb5_creds * cred;
  8984. #endif
  8985. {
  8986.     krb5_error_code retval=0;
  8987.     krb5_ticket *tkt=NULL;
  8988.     char *name=NULL, *sname=NULL, *flags=NULL;
  8989.     int extra_field = 0;
  8990.  
  8991.     retval = krb5_unparse_name(kcontext, cred->client, &name);
  8992.     if (retval) {
  8993.         debug(F101,"ck_krb5_list_creds while unparsing client name","",retval);
  8994.         krb5_errno = retval;
  8995.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8996.         return;
  8997.     }
  8998.     retval = krb5_unparse_name(kcontext, cred->server, &sname);
  8999.     if (retval) {
  9000.         debug(F101,"ck_krb5_list_creds while unparsing server name","",retval);
  9001.         free(name);
  9002.         krb5_errno = retval;
  9003.         makestr(&krb5_errmsg,error_message(krb5_errno));
  9004.         return;
  9005.     }
  9006.     if (!cred->times.starttime)
  9007.         cred->times.starttime = cred->times.authtime;
  9008.  
  9009.     printtime(cred->times.starttime);
  9010.     printf("  ");
  9011.  
  9012.     if ( time(0) < cred->times.endtime )
  9013.         printtime(cred->times.endtime);
  9014.     else
  9015.         printf("** expired ** ");
  9016.  
  9017.     printf("  %s\r\n", sname);
  9018.  
  9019.     if (strcmp(name, defname)) {
  9020.         printf("   for client %s", name);
  9021.         extra_field++;
  9022.     }
  9023.  
  9024.     if (cred->times.renew_till) {
  9025.         if (!extra_field)
  9026.             printf("   ");
  9027.         else
  9028.             printf(", ");
  9029.         printf("renew until ");
  9030.         printtime(cred->times.renew_till);
  9031.         extra_field += 2;
  9032.     }
  9033.  
  9034.     if (extra_field > 3) {
  9035.         printf("\r\n");
  9036.         extra_field = 0;
  9037.     }
  9038.  
  9039.     if (show_flags) {
  9040.         flags = flags_string(cred);
  9041.         if (flags && *flags) {
  9042.             if (!extra_field)
  9043.                 printf("   ");
  9044.             else
  9045.                 printf(", ");
  9046.             printf("Flags: %s", flags);
  9047.             extra_field++;
  9048.         }
  9049.     }
  9050.  
  9051.     if (extra_field > 2) {
  9052.         printf("\r\n");
  9053.         extra_field = 0;
  9054.     }
  9055.  
  9056.     if (show_etype) {
  9057.         retval = decode_krb5_ticket(&cred->ticket, &tkt);
  9058.         if (!extra_field)
  9059.             printf("   ");
  9060.         else
  9061.             printf(", ");
  9062. #ifdef HEIMDAL
  9063.         printf("Etype (skey, tkt): %s, %s ",
  9064.                etype_string(cred->session.keytype),
  9065.                etype_string(tkt->enc_part.keytype));
  9066. #else /* HEIMDAL */
  9067.         printf("Etype (skey, tkt): %s, %s ",
  9068.                etype_string(cred->keyblock.enctype),
  9069.                etype_string(tkt->enc_part.enctype));
  9070. #endif /* HEIMDAL */
  9071.         krb5_free_ticket(kcontext, tkt);
  9072.         extra_field++;
  9073.     }
  9074.  
  9075.     /* if any additional info was printed, extra_field is non-zero */
  9076.     if (extra_field)
  9077.         printf("\r\n");
  9078.  
  9079.     if ( show_addr ) {
  9080.         if (!cred->addresses || !cred->addresses[0]) {
  9081.             printf("\tAddresses: (none)\r\n");
  9082.         } else {
  9083.             int i;
  9084.             for (i=0; cred->addresses[i]; i++) {
  9085.                 if (i)
  9086.                     printf("              ");
  9087.                 else
  9088.                     printf("   Addresses: ");
  9089.                 one_addr(cred->addresses[i]);
  9090.                 printf("\r\n");
  9091.             }
  9092.         }
  9093.     }
  9094.  
  9095.     krb5_free_unparsed_name(kcontext,name);
  9096.     krb5_free_unparsed_name(kcontext,sname);
  9097.  
  9098.     krb5_errno = 0;
  9099.     makestr(&krb5_errmsg,"OK");
  9100. }
  9101.  
  9102. static VOID
  9103. #ifdef CK_ANSIC
  9104. fillit(int num, int c)
  9105. #else
  9106. fillit(num, c) int num; int c;
  9107. #endif
  9108. {
  9109.     int i;
  9110.  
  9111.     for (i=0; i<num; i++)
  9112.         printf("%c",c);
  9113. }
  9114. #endif /* KLIST */
  9115. #endif /* KRB5 */
  9116.  
  9117. #ifdef KRB4
  9118. #define KDEBUG 1
  9119. int k4debug = 0;                /* Kerberos 4 runtime debugging */
  9120.  
  9121. #ifdef KINIT
  9122. #define KRB_DEFAULT_LIFE 120 /* 10 hours in 5 minute intervals */
  9123.  
  9124. #ifdef SNK4
  9125. /* SNK4 is a hardware authentication system used to pre-authenticate    */
  9126. /* a ticket getting ticket.  We do not support this code at the present */
  9127. /* time in Kermit.                                                      */
  9128. void
  9129. get_input(s, size, stream)
  9130. char *s;
  9131. int size;
  9132. FILE *stream;
  9133. {
  9134.     char *p;
  9135.  
  9136.     if (fgets(s, size, stream) == NULL)
  9137.         exit(1);
  9138.     if ( (p = strchr(s, '\n')) != NULL)
  9139.         *p = '\0';
  9140. }
  9141. #endif /* SNK4 */
  9142.  
  9143. #ifdef COMMENT
  9144. static char
  9145. #ifdef CK_ANSIC
  9146. hex_scan_nybble(char c)
  9147. #else
  9148. hex_scan_nybble(c) char c;
  9149. #endif
  9150. {
  9151.     if (c >= '0' && c <= '9')
  9152.         return c - '0';
  9153.     if (c >= 'A' && c <= 'F')
  9154.         return c - 'A' + 10;
  9155.     if (c >= 'a' && c <= 'f')
  9156.         return c - 'a' + 10;
  9157.     return -1;
  9158. }
  9159.  
  9160. /* returns: NULL for ok, pointer to error string for bad input */
  9161. static char*
  9162. #ifdef CK_ANSIC
  9163. hex_scan_four_bytes(char *out, char *in)
  9164. #else
  9165. hex_scan_four_bytes(out, in) char *out; char *in;
  9166. #endif
  9167. {
  9168.     int i;
  9169.     int c;
  9170.     char c1;
  9171.     for (i=0; i<8; i++) {
  9172.         if(!in[i])
  9173.             return "not enough input";
  9174.         c = hex_scan_nybble(in[i]);
  9175.         if(c<0)
  9176.             return "invalid digit";
  9177.         c1 = c;
  9178.         i++;
  9179.         if(!in[i])
  9180.             return "not enough input";
  9181.         c = hex_scan_nybble(in[i]);
  9182.         if(c<0)
  9183.             return "invalid digit";
  9184.         *out++ = (c1 << 4) + c;
  9185.     }
  9186.     switch(in[i]) {
  9187.     case 0:
  9188.     case '\r':
  9189.     case '\n':
  9190.         return NULL;
  9191.     default:
  9192.         return "extra characters at end of input";
  9193.     }
  9194. }
  9195. #endif /* COMMENT */
  9196.  
  9197. /* ck_krb4_initTGT() returns 0 on success */
  9198. int
  9199. #ifdef CK_ANSIC
  9200. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9201. #else
  9202. ck_krb4_initTGT(op,init)
  9203.     struct krb_op_data * op, struct krb4_init_data * init
  9204. #endif
  9205. {
  9206.     char    aname[ANAME_SZ+1];
  9207.     char    inst[INST_SZ+1];
  9208.     char    realm[REALM_SZ+1];
  9209.     char    *password=NULL;
  9210.     char    passwd[80]="";
  9211.     char    *username = NULL;
  9212.     char    *usernameptr=NULL;
  9213.     int     iflag,      /* Instance */
  9214.             rflag,      /* Realm */
  9215.             vflag,      /* Verbose */
  9216.             lflag,      /* Lifetime */
  9217.             pflag,      /* Preauth */
  9218.             lifetime=KRB_DEFAULT_LIFE,   /* Life Time */
  9219.             k_errno;
  9220.     register char *cp;
  9221.     register i;
  9222.  
  9223.     if ( !ck_krb4_is_installed() )
  9224.         return(-1);
  9225.  
  9226.     *inst = *realm = '\0';
  9227.     iflag = rflag = vflag = lflag = pflag = 0;
  9228.  
  9229.     vflag = init->verbose;
  9230.     pflag = init->preauth;
  9231.  
  9232.     if ( init->lifetime ) {
  9233.         lifetime = init->lifetime<5?1:init->lifetime/5;
  9234.         if ( lifetime > 255 ) lifetime = 255;
  9235.     }
  9236.     else
  9237.         lifetime = KRB_DEFAULT_LIFE;
  9238.  
  9239.     username = init->principal;
  9240.  
  9241.     if (username && username[0] &&
  9242.         (k_errno = kname_parse(aname, inst, realm, username))
  9243.         != AUTH_SUCCESS) {
  9244.         krb4_errno = k_errno;
  9245.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9246.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9247.         iflag = rflag = 1;
  9248.         username = NULL;
  9249.     }
  9250.  
  9251.     if ( init->realm ) {
  9252.         ckstrncpy(realm,init->realm,REALM_SZ);
  9253.     }
  9254.  
  9255.     if ( init->instance ) {
  9256.         ckstrncpy(inst,init->instance, INST_SZ);
  9257.     }
  9258.  
  9259. #ifdef COMMENT
  9260.     if ( vflag )
  9261.         printf("Kerberos IV initialization\r\n");
  9262. #endif /* COMMENT */
  9263.  
  9264.     if (!username || !username[0]) {
  9265.         debug(F100,"ck_krb4_initTGT no username specified","",0);
  9266.         printf("?Invalid principal specified.\r\n");
  9267.         krb4_errno = -1;
  9268.         makestr(&krb4_errmsg,"No principal specified");
  9269.         return(-1);
  9270.     }
  9271.     if (!*realm) {
  9272.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ);
  9273.     }
  9274.  
  9275.     if ( init->password )
  9276.         password = init->password;
  9277.     else {
  9278.         char prmpt[80];
  9279.         int ok;
  9280.  
  9281.         ckmakxmsg(prmpt,sizeof(prmpt),
  9282.                   "Kerberos 4 Password for ",username,"@",realm,": ",
  9283.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9284.         ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  9285.         if ( ok )
  9286.             password = passwd;
  9287.     }
  9288.  
  9289.     if (pflag) {
  9290.         k_errno = krb_get_pw_in_tkt_preauth( aname, inst, realm,
  9291.                                              "krbtgt", realm,
  9292.                                              lifetime,
  9293.                                              password);
  9294.         if (k_errno == -1) {    /* preauth method not available */
  9295.             k_errno = krb_get_pw_in_tkt(aname,
  9296.                                          inst, realm,
  9297.                                          "krbtgt", realm,
  9298.                                          lifetime,
  9299.                                          password);
  9300.         }
  9301.     } else {
  9302.         k_errno = krb_get_pw_in_tkt(aname,
  9303.                                      inst, realm,
  9304.                                      "krbtgt", realm,
  9305.                                      lifetime,
  9306.                                      password);
  9307.     }
  9308.  
  9309.     memset(passwd,0,sizeof(passwd));
  9310.     if (k_errno) {
  9311.         printf("%s for principal %s%s%s@%s\r\n",
  9312.                 krb_get_err_text_entry(k_errno), aname,
  9313.                 inst[0]?".":"", inst, realm);
  9314.         krb4_errno = k_errno;
  9315.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9316.         return(-1);
  9317.     } else if (vflag) {
  9318.         printf("Result from realm %s: ", realm);
  9319.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9320.     }
  9321.     krb4_errno = k_errno;
  9322.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9323.     return(0);
  9324. }
  9325. #endif /* KINIT */
  9326. #ifdef KDESTROY
  9327. int
  9328. #ifdef CK_ANSIC
  9329. ck_krb4_destroy(struct krb_op_data * op)
  9330. #else
  9331. ck_krb4_destroy(op) struct krb_op_data * op;
  9332. #endif
  9333. {
  9334.     int k_errno=0;
  9335.  
  9336.     if ( !ck_krb4_is_installed() )
  9337.         return(-1);
  9338.  
  9339.     k_errno = dest_tkt();
  9340.  
  9341.     krb4_errno = k_errno;
  9342.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9343.  
  9344.     if (k_errno == 0)
  9345.         printf("Tickets destroyed.\r\n");
  9346.     else if (k_errno == RET_TKFIL)
  9347.         printf("No tickets to destroy.\r\n");
  9348.     else {
  9349.         printf("Tickets MAY NOT be destroyed.\r\n");
  9350.         return(-1);
  9351.     }
  9352.     return(0);
  9353. }
  9354. #endif /* KDESTROY */
  9355. #ifdef KLIST
  9356. _PROTOTYP(static int display_tktfile,(char *, int, int, int));
  9357.  
  9358. int
  9359. #ifdef CK_ANSIC
  9360. ck_krb4_list_creds(struct krb_op_data * op)
  9361. #else
  9362. ck_krb4_list_creds(op) struct krb_op_data * op;
  9363. #endif
  9364. {
  9365.     int     long_form = 1;
  9366.     int     tgt_test = 0;
  9367.     int     do_srvtab = 0;
  9368.     int     show_kvnos = 0;
  9369.     char   *tkt_file = NULL;
  9370.  
  9371.     if ( !ck_krb4_is_installed() )
  9372.         return(-1);
  9373.  
  9374.     if ( op->cache )
  9375.         tkt_file = op->cache;
  9376.  
  9377.     if ( k4debug ) {
  9378.         show_kvnos = 1;
  9379.     }
  9380.  
  9381.     if (do_srvtab)
  9382.         return(display_srvtab(tkt_file));
  9383.     else
  9384.         return(display_tktfile(tkt_file, tgt_test, long_form, show_kvnos));
  9385. }
  9386.  
  9387. #ifndef KRB5
  9388. static int timestamp_width=0;
  9389.  
  9390. static char   *
  9391. #ifdef CK_ANSIC
  9392. short_date(long   *dp)
  9393. #else
  9394. short_date(dp) long   *dp;
  9395. #endif
  9396. {
  9397.     register char *cp;
  9398.     extern char *ctime();
  9399.     cp = ctime(dp) + 4;
  9400.     cp[15] = '\0';
  9401.     return (cp);
  9402. }
  9403.  
  9404.  
  9405. static VOID
  9406. #ifdef CK_ANSIC
  9407. printtime(time_t tv)
  9408. #else
  9409. printtime(tv) time_t tv;
  9410. #endif
  9411. {
  9412.     char timestring[BUFSIZ];
  9413.     char format[12];
  9414.     char fill;
  9415.  
  9416.     fill = ' ';
  9417.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  9418.     printf(format,short_date(&tv));
  9419. }
  9420. #endif /* KRB5 */
  9421.  
  9422. static int
  9423. #ifdef CK_ANSIC
  9424. display_tktfile(char *file, int tgt_test, int long_form, int show_kvnos)
  9425. #else
  9426. display_tktfile(file,tgt_test,long_form,show_kvnos)
  9427.     char *file; int tgt_test; int long_form; int show_kvnos;
  9428. #endif
  9429. {
  9430.     char    pname[ANAME_SZ];
  9431.     char    pinst[INST_SZ];
  9432.     char    prealm[REALM_SZ];
  9433.     char    buf1[20], buf2[20];
  9434.     int     k_errno;
  9435. #ifdef OS2
  9436.     LEASH_CREDENTIALS creds;
  9437. #else /* OS2 */
  9438.     CREDENTIALS creds;
  9439. #endif /* OS2 */
  9440.     int     header = 1;
  9441.  
  9442.     file = tkt_string();
  9443.  
  9444.     if (long_form) {
  9445.         printf("Ticket cache:      %s\r\n", file);
  9446.     }
  9447.  
  9448.     /*
  9449.      * Since krb_get_tf_realm will return a ticket_file error,
  9450.      * we will call tf_init and tf_close first to filter out
  9451.      * things like no ticket file.  Otherwise, the error that
  9452.      * the user would see would be
  9453.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9454.      * instead of
  9455.      * klist: No ticket file (tf_util)
  9456.      */
  9457.  
  9458.     /* Open ticket file */
  9459.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9460.         if (!tgt_test)
  9461.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9462.         krb4_errno = k_errno;
  9463.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9464.         return(-1);
  9465.     }
  9466.  
  9467.  
  9468.     /* Close ticket file */
  9469.     (void) tf_close();
  9470.  
  9471.     /*
  9472.      * We must find the realm of the ticket file here before calling
  9473.      * tf_init because since the realm of the ticket file is not
  9474.      * really stored in the principal section of the file, the
  9475.      * routine we use must itself call tf_init and tf_close.
  9476.      */
  9477.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9478.         if (!tgt_test)
  9479.             printf("can't find realm of ticket file: %s\r\n",
  9480.                     krb_get_err_text_entry (k_errno));
  9481.         krb4_errno = k_errno;
  9482.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9483.         return(-1);
  9484.     }
  9485.  
  9486.     /* Open ticket file */
  9487.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9488.         if (!tgt_test)
  9489.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9490.         krb4_errno = k_errno;
  9491.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9492.         return(-1);
  9493.     }
  9494.     /* Get principal name and instance */
  9495.     if ((k_errno = tf_get_pname(pname)) ||
  9496.          (k_errno = tf_get_pinst(pinst))) {
  9497.         (void) tf_close();
  9498.         if (!tgt_test)
  9499.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9500.         krb4_errno = k_errno;
  9501.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9502.         return(-1);
  9503.     }
  9504.  
  9505.     /*
  9506.      * You may think that this is the obvious place to get the
  9507.      * realm of the ticket file, but it can't be done here as the
  9508.      * routine to do this must open the ticket file.  This is why
  9509.      * it was done before tf_init.
  9510.      */
  9511.  
  9512.     if (!tgt_test && long_form)
  9513.         printf("Default principal: %s%s%s%s%s\r\n\r\n", pname,
  9514.                (pinst[0] ? "." : ""), pinst,
  9515.                (prealm[0] ? "@" : ""), prealm);
  9516.  
  9517.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9518.         if (!tgt_test && long_form && header) {
  9519.             printf("%-17s  %-17s  %s\r\n",
  9520.                    "Valid starting", "Expires", "Service principal");
  9521.             header = 0;
  9522.         }
  9523.         if (tgt_test) {
  9524.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9525.             if (!strcmp(creds.service, "krbtgt") &&
  9526.                 !strcmp(creds.instance, prealm)) {
  9527.                 krb4_errno = k_errno;
  9528.                 makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9529.  
  9530.                 (void) tf_close();
  9531.                 if (time(0) < creds.issue_date) {
  9532.                     return(0);          /* tgt hasn't expired */
  9533.                 } else {
  9534.                     return(-1);         /* has expired */
  9535.                 }
  9536.             }
  9537.             continue;                   /* not a tgt */
  9538.         }
  9539.         if (long_form) {
  9540.             timestamp_width = 17;       /* for k5 display function */
  9541.                                         /* if available            */
  9542.             printtime(creds.issue_date);
  9543.             printf("  ");
  9544.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9545.             if ( time(0) < creds.issue_date )
  9546.                 printtime(creds.issue_date);
  9547.             else
  9548.                 printf("*** expired ***  ");
  9549.             printf("  ");
  9550.         }
  9551.         if (show_kvnos)
  9552.           printf("%s%s%s%s%s (%d)\r\n",
  9553.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9554.                  (creds.realm[0] ? "@" : ""), creds.realm, creds.kvno);
  9555.         else
  9556.           printf("%s%s%s%s%s\r\n",
  9557.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9558.                  (creds.realm[0] ? "@" : ""), creds.realm);
  9559.  
  9560. #ifdef OS2
  9561.         if ( creds.address[0] )
  9562.             printf("   Address: %s\r\n",creds.address);
  9563. #endif /* OS2 */
  9564.     }
  9565.  
  9566.     (void) tf_close();
  9567.  
  9568.     if (tgt_test) {
  9569.         return(-1);
  9570.     }/* no tgt found */
  9571.     if (header && long_form && k_errno == EOF) {
  9572.         printf("No tickets in file.\r\n");
  9573.     }
  9574.     krb4_errno = k_errno;
  9575.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9576.     return(0);
  9577. }
  9578.  
  9579. #ifdef COMMENT
  9580. /* Just so we remember what the command line interface looked like */
  9581. usage()
  9582. {
  9583.     printf(
  9584.         "Usage: [ -s | -t ] [ -file filename ] [ -srvtab ] [ -version ]\r\n");
  9585.     return(-1);
  9586. }
  9587. #endif /* COMMENT */
  9588.  
  9589. /* adapted from getst() in librkb */
  9590. /*
  9591.  * ok_getst() takes a file descriptor, a string and a count.  It reads
  9592.  * from the file until either it has read "count" characters, or until
  9593.  * it reads a null byte.  When finished, what has been read exists in
  9594.  * the given string "s".  If "count" characters were actually read, the
  9595.  * last is changed to a null, so the returned string is always null-
  9596.  * terminated.  ok_getst() returns the number of characters read, including
  9597.  * the null terminator.
  9598.  *
  9599.  * If there is a read error, it returns -1 (like the read(2) system call)
  9600.  */
  9601.  
  9602. static int
  9603. #ifdef CK_ANSIC
  9604. ok_getst(int fd, register char *s, int n)
  9605. #else
  9606. ok_getst(fd, s, n) int fd; register char *s; int n;
  9607. #endif
  9608. {
  9609.     register int count = n;
  9610.     int err;
  9611.     while ((err = read(fd, s, 1)) > 0 && --count)
  9612.         if (*s++ == '\0')
  9613.             return (n - count);
  9614.     if (err < 0)
  9615.         return(-1);
  9616.     *s = '\0';
  9617.     return (n - count);
  9618. }
  9619.  
  9620. int
  9621. #ifdef CK_ANSIC
  9622. display_srvtab(char *file)
  9623. #else
  9624. display_srvtab(file) char *file;
  9625. #endif
  9626. {
  9627.     int stab;
  9628.     char serv[SNAME_SZ];
  9629.     char inst[INST_SZ];
  9630.     char rlm[REALM_SZ];
  9631.     unsigned char key[8];
  9632.     unsigned char vno;
  9633.     int count;
  9634.  
  9635.     printf("Server key file:   %s\r\n", file);
  9636. #ifdef NT
  9637. #ifndef O_RDONLY
  9638. #define O_RDONLY _O_RDONLY
  9639. #endif /* O_RDONLY */
  9640. #endif /* NT */
  9641.  
  9642.     if ((stab = open(file, O_RDONLY, 0400)) < 0) {
  9643.         perror(file);
  9644.         return(-1);
  9645.     }
  9646.     printf("%-15s %-15s %-10s %s\r\n","Service","Instance","Realm",
  9647.            "Key Version");
  9648.     printf("------------------------------------------------------\r\n");
  9649.  
  9650.     /* argh. getst doesn't return error codes, it silently fails */
  9651.     while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
  9652.            && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
  9653.            && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
  9654.         if (((count = read(stab,(char *) &vno,1)) != 1) ||
  9655.              ((count = read(stab,(char *) key,8)) != 8)) {
  9656.             if (count < 0)
  9657.                 perror("reading from key file");
  9658.             else
  9659.                 printf("key file truncated\r\n");
  9660.             return(-1);
  9661.         }
  9662.         printf("%-15s %-15s %-15s %d\r\n",serv,inst,rlm,vno);
  9663.     }
  9664.     if (count < 0)
  9665.         perror(file);
  9666.     (void) close(stab);
  9667.     return(0);
  9668. }
  9669. #endif /* KLIST */
  9670. #else /* KRB4 */
  9671. int
  9672. ck_krb4_autoget_TGT(char * dummy)
  9673. {
  9674.     return(-1);
  9675. }
  9676. #ifdef CK_KERBEROS
  9677. int
  9678. #ifdef CK_ANSIC
  9679. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9680. #else
  9681. ck_krb4_initTGT(op,init)
  9682.     struct krb_op_data * op, struct krb4_init_data * init
  9683. #endif
  9684. {
  9685.     return(-1);
  9686. }
  9687.  
  9688. #ifdef CK_ANSIC
  9689. ck_krb4_destroy(struct krb_op_data * op)
  9690. #else
  9691. ck_krb4_destroy(op) struct krb_op_data * op;
  9692. #endif
  9693. {
  9694.     return(-1);
  9695. }
  9696. int
  9697. #ifdef CK_ANSIC
  9698. ck_krb4_list_creds(struct krb_op_data * op)
  9699. #else
  9700. ck_krb4_list_creds(op) struct krb_op_data * op;
  9701. #endif
  9702. {
  9703.     return(-1);
  9704. }
  9705. #else /* CK_KERBEROS */
  9706. int ck_krb4_initTGT(void * a, void *b)
  9707. {
  9708.     return(-1);
  9709. }
  9710. int ck_krb4_destroy(void *a)
  9711. {
  9712.     return(-1);
  9713. }
  9714. int ck_krb4_list_creds(void *a)
  9715. {
  9716.     return(-1);
  9717. }
  9718. #endif /* CK_KERBEROS */
  9719. #endif /* KRB4 */
  9720.  
  9721. /* The following functions are used to implement the Kermit Script Language */
  9722. /* functions                                                                */
  9723.  
  9724. struct tkt_list_item {
  9725.     char * name;
  9726.     struct tkt_list_item * next;
  9727. };
  9728.  
  9729. static struct tkt_list_item * k4_tkt_list = NULL;
  9730.  
  9731. int
  9732. #ifdef CK_ANSIC
  9733. ck_krb4_get_tkts(VOID)
  9734. #else
  9735. ck_krb4_get_tkts()
  9736. #endif
  9737. {
  9738. #ifdef KRB4
  9739.     char   *file=NULL;
  9740.     char    pname[ANAME_SZ];
  9741.     char    pinst[INST_SZ];
  9742.     char    prealm[REALM_SZ];
  9743.     char    buf1[20], buf2[20];
  9744.     int     k_errno;
  9745. #ifdef OS2
  9746.     LEASH_CREDENTIALS creds;
  9747. #else /* OS2 */
  9748.     CREDENTIALS creds;
  9749. #endif /* OS2 */
  9750.     int     tkt_count=0;
  9751.     struct  tkt_list_item ** list = &k4_tkt_list;
  9752.  
  9753.     while ( k4_tkt_list ) {
  9754.         struct tkt_list_item * next;
  9755.         next = k4_tkt_list->next;
  9756.         free(k4_tkt_list->name);
  9757.         free(k4_tkt_list);
  9758.         k4_tkt_list = next;
  9759.     }
  9760.  
  9761.     if ( !ck_krb4_is_installed() )
  9762.         return(-1);
  9763.  
  9764.     file = tkt_string();
  9765.  
  9766.     /*
  9767.      * Since krb_get_tf_realm will return a ticket_file error,
  9768.      * we will call tf_init and tf_close first to filter out
  9769.      * things like no ticket file.  Otherwise, the error that
  9770.      * the user would see would be
  9771.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9772.      * instead of
  9773.      * klist: No ticket file (tf_util)
  9774.      */
  9775.  
  9776.     /* Open ticket file */
  9777.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9778.         return(-1);
  9779.     }
  9780.  
  9781.     /* Close ticket file */
  9782.     (void) tf_close();
  9783.  
  9784.     /*
  9785.      * We must find the realm of the ticket file here before calling
  9786.      * tf_init because since the realm of the ticket file is not
  9787.      * really stored in the principal section of the file, the
  9788.      * routine we use must itself call tf_init and tf_close.
  9789.      */
  9790.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9791.         return(-1);
  9792.     }
  9793.  
  9794.     /* Open ticket file */
  9795.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9796.         return(-1);
  9797.     }
  9798.     /* Get principal name and instance */
  9799.     if ((k_errno = tf_get_pname(pname)) ||
  9800.          (k_errno = tf_get_pinst(pinst))) {
  9801.         return(-1);
  9802.     }
  9803.  
  9804.     /*
  9805.      * You may think that this is the obvious place to get the
  9806.      * realm of the ticket file, but it can't be done here as the
  9807.      * routine to do this must open the ticket file.  This is why
  9808.      * it was done before tf_init.
  9809.      */
  9810.  
  9811.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9812.         char tkt_buf[256];
  9813.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9814.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9815.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9816.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9817.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  9818.         (*list)->name = strdup(tkt_buf);
  9819.         (*list)->next = NULL;
  9820.         list = &((*list)->next);
  9821.         tkt_count++;
  9822.     }
  9823.  
  9824.     tf_close();
  9825.     return(tkt_count);
  9826. #else /* KRB4 */
  9827.     return(0);
  9828. #endif /* KRB4 */
  9829. }
  9830.  
  9831. char *
  9832. #ifdef CK_ANSIC
  9833. ck_krb4_get_next_tkt(VOID)
  9834. #else
  9835. ck_krb4_get_next_tkt()
  9836. #endif
  9837. {
  9838. #ifdef KRB4
  9839.     static char * s=NULL;
  9840.     struct tkt_list_item * next=NULL;
  9841.  
  9842.     if ( s ) {
  9843.         free(s);
  9844.         s = NULL;
  9845.     }
  9846.  
  9847.     if ( k4_tkt_list == NULL )
  9848.         return(NULL);
  9849.  
  9850.     next = k4_tkt_list->next;
  9851.     s = k4_tkt_list->name;
  9852.     free(k4_tkt_list);
  9853.     k4_tkt_list = next;
  9854.     return(s);
  9855. #else /* KRB4 */
  9856.     return(NULL);
  9857. #endif /* KRB4 */
  9858. }
  9859.  
  9860. int
  9861. #ifdef CK_ANSIC
  9862. ck_krb4_tkt_isvalid(char * tktname)
  9863. #else
  9864. ck_krb4_tkt_isvalid(tktname) char * tktname;
  9865. #endif
  9866. {
  9867. #ifdef KRB4
  9868.     char   *file=NULL;
  9869.     char    pname[ANAME_SZ];
  9870.     char    pinst[INST_SZ];
  9871.     char    prealm[REALM_SZ];
  9872.     char    buf1[20], buf2[20];
  9873.     int     k_errno;
  9874.     time_t  issue_t, expire_t, now_t;
  9875. #ifdef OS2
  9876.     LEASH_CREDENTIALS creds;
  9877. #else /* OS2 */
  9878.     CREDENTIALS creds;
  9879. #endif /* OS2 */
  9880.  
  9881.     if ( !ck_krb4_is_installed() )
  9882.         return(-1);
  9883.  
  9884.     debug(F110,"ck_krb4_tkt_isvalid","tkt_string",0);
  9885.     file = tkt_string();
  9886.  
  9887.     /*
  9888.      * Since krb_get_tf_realm will return a ticket_file error,
  9889.      * we will call tf_init and tf_close first to filter out
  9890.      * things like no ticket file.  Otherwise, the error that
  9891.      * the user would see would be
  9892.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9893.      * instead of
  9894.      * klist: No ticket file (tf_util)
  9895.      */
  9896.  
  9897.     /* Open ticket file */
  9898.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9899.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9900.         return(-1);
  9901.     }
  9902.  
  9903.     /* Close ticket file */
  9904.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9905.     (void) tf_close();
  9906.  
  9907.     /*
  9908.      * We must find the realm of the ticket file here before calling
  9909.      * tf_init because since the realm of the ticket file is not
  9910.      * really stored in the principal section of the file, the
  9911.      * routine we use must itself call tf_init and tf_close.
  9912.      */
  9913.     debug(F110,"ck_krb4_tkt_isvalid","krb_get_tf_realm",0);
  9914.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9915.         return(-1);
  9916.     }
  9917.  
  9918.     /* Open ticket file */
  9919.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9920.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9921.         return(-1);
  9922.     }
  9923.     /* Get principal name and instance */
  9924.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_name/tf_get_pinst",0);
  9925.     if ((k_errno = tf_get_pname(pname)) ||
  9926.          (k_errno = tf_get_pinst(pinst))) {
  9927.  
  9928.         /* Close ticket file */
  9929.         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9930.         (void) tf_close();
  9931.  
  9932.         return(-1);
  9933.     }
  9934.  
  9935.     /*
  9936.      * You may think that this is the obvious place to get the
  9937.      * realm of the ticket file, but it can't be done here as the
  9938.      * routine to do this must open the ticket file.  This is why
  9939.      * it was done before tf_init.
  9940.      */
  9941.  
  9942.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_cred",0);
  9943.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9944.         char tkt_buf[256];
  9945.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9946.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9947.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9948.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9949.         if ( !strcmp(tktname,tkt_buf) ) {
  9950.  
  9951.             /* we found the ticket we are looking for */
  9952.             issue_t = creds.issue_date;
  9953.             expire_t = creds.issue_date
  9954.                 + ((unsigned char) creds.lifetime) * 5 * 60;
  9955.             now_t = time(0);
  9956.  
  9957.             /* We add a 5 minutes fudge factor to compensate for potential */
  9958.             /* clock skew errors between the KDC and K95's host OS         */
  9959.  
  9960.             if ( now_t >= (issue_t-300) && now_t < expire_t) {
  9961. #ifdef OS2
  9962. #ifdef CHECKADDRS
  9963.                 if ( krb4_checkaddrs ) {
  9964.                     extern char myipaddr[20];       /* From ckcnet.c */
  9965.                     if ( !myipaddr[0] ) {
  9966.                         int i;
  9967.                         char buf[60];
  9968.                         for ( i=0;i<64;i++ ) {
  9969.                             if ( getlocalipaddrs(buf,60,i) < 0 )
  9970.                                 break;
  9971.  
  9972.                             if ( !strcmp(buf,creds.address) ) {
  9973.                                 /* Close ticket file */
  9974.                                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9975.                                 (void) tf_close();
  9976.                                 return(1); /* They're the same */
  9977.                             }
  9978.                         }
  9979.  
  9980.                         /* Close ticket file */
  9981.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9982.                         (void) tf_close();
  9983.                         return(0);                  /* They're different */
  9984.                     } else if ( strcmp(myipaddr,creds.address) ) {
  9985.                         /* Close ticket file */
  9986.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9987.                         (void) tf_close();
  9988.                         return(0);                  /* They're different */
  9989.                     }
  9990.                     else {
  9991.                         /* Close ticket file */
  9992.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9993.                         (void) tf_close();
  9994.                         return(1);                  /* They're the same */
  9995.                     }
  9996.                 } else {
  9997.                     /* Close ticket file */
  9998.                     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9999.                     (void) tf_close();
  10000.                     return(1);                  /* They're the same */
  10001.                 }
  10002. #else /* CHECKADDRS */
  10003.                 /* Close ticket file */
  10004.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10005.                 (void) tf_close();
  10006.                 return(1);      /* valid but no ip address check */
  10007. #endif /* CHECKADDRS */
  10008. #else /* OS2 */
  10009.                 /* Close ticket file */
  10010.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10011.                 (void) tf_close();
  10012.                 return(1);      /* Valid but no ip address check */
  10013. #endif /* OS2 */
  10014.             }
  10015.             else {
  10016.                 /* Close ticket file */
  10017.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10018.                 (void) tf_close();
  10019.                 return(0);      /* expired or otherwise invalid */
  10020.             }
  10021.         }
  10022.     }
  10023.     /* Close ticket file */
  10024.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10025.     (void) tf_close();
  10026.     return(0);                  /* could not find the desired ticket */
  10027. #else /* KRB4 */
  10028.     return(-1);
  10029. #endif /* KRB4 */
  10030. }
  10031.  
  10032. int
  10033. #ifdef CK_ANSIC
  10034. ck_krb4_is_tgt_valid(VOID)
  10035. #else
  10036. ck_krb4_is_tgt_valid()
  10037. #endif
  10038. {
  10039. #ifdef KRB4
  10040.     char tgt[256];
  10041.     char * s;
  10042.     int rc = 0;
  10043.  
  10044.     s = krb4_d_realm ? krb4_d_realm : ck_krb4_getrealm();
  10045.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",s,"@",s);
  10046.     rc = ck_krb4_tkt_isvalid(tgt);
  10047.     debug(F111,"ck_krb4_is_tgt_valid",tgt,rc);
  10048.     return(rc > 0);
  10049. #else /* KRB4 */
  10050.     return(0);
  10051. #endif /* KRB4 */
  10052. }
  10053.  
  10054. int
  10055. #ifdef CK_ANSIC
  10056. ck_krb4_tkt_time(char * tktname)
  10057. #else
  10058. ck_krb4_tkt_time(tktname) char * tktname;
  10059. #endif
  10060. {
  10061. #ifdef KRB4
  10062.     char   *file=NULL;
  10063.     char    pname[ANAME_SZ];
  10064.     char    pinst[INST_SZ];
  10065.     char    prealm[REALM_SZ];
  10066.     char    buf1[20], buf2[20];
  10067.     int     k_errno;
  10068. #ifdef OS2
  10069.     LEASH_CREDENTIALS creds;
  10070. #else /* OS2 */
  10071.     CREDENTIALS creds;
  10072. #endif /* OS2 */
  10073.  
  10074.     if ( !ck_krb4_is_installed() )
  10075.         return(-1);
  10076.  
  10077.     file = tkt_string();
  10078.  
  10079.     /*
  10080.      * Since krb_get_tf_realm will return a ticket_file error,
  10081.      * we will call tf_init and tf_close first to filter out
  10082.      * things like no ticket file.  Otherwise, the error that
  10083.      * the user would see would be
  10084.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  10085.      * instead of
  10086.      * klist: No ticket file (tf_util)
  10087.      */
  10088.  
  10089.     /* Open ticket file */
  10090.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  10091.         return(-1);
  10092.     }
  10093.  
  10094.     /* Close ticket file */
  10095.     (void) tf_close();
  10096.  
  10097.     /*
  10098.      * We must find the realm of the ticket file here before calling
  10099.      * tf_init because since the realm of the ticket file is not
  10100.      * really stored in the principal section of the file, the
  10101.      * routine we use must itself call tf_init and tf_close.
  10102.      */
  10103.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  10104.         return(-1);
  10105.     }
  10106.  
  10107.     /* Open ticket file */
  10108.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  10109.         return(-1);
  10110.     }
  10111.     /* Get principal name and instance */
  10112.     if ((k_errno = tf_get_pname(pname)) ||
  10113.          (k_errno = tf_get_pinst(pinst))) {
  10114.         tf_close();
  10115.         return(-1);
  10116.     }
  10117.  
  10118.     /*
  10119.      * You may think that this is the obvious place to get the
  10120.      * realm of the ticket file, but it can't be done here as the
  10121.      * routine to do this must open the ticket file.  This is why
  10122.      * it was done before tf_init.
  10123.      */
  10124.  
  10125.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  10126.         char tkt_buf[256];
  10127.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  10128.                  creds.service, (creds.instance[0] ? "." : ""),
  10129.                  creds.instance,
  10130.                  (creds.realm[0] ? "@" : ""), creds.realm,
  10131.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  10132.         if ( !strcmp(tktname,tkt_buf) ) {
  10133.             /* we found the ticket we are looking for */
  10134.             int n = (creds.issue_date
  10135.                       + (((unsigned char) creds.lifetime) * 5 * 60))
  10136.                 - time(0);
  10137.             tf_close();
  10138.             return(n <= 0 ? 0 : n);
  10139.         }
  10140.     }
  10141.     tf_close();
  10142.     return(0);                  /* could not find the desired ticket */
  10143. #else /* KRB4 */
  10144.     return(-1);
  10145. #endif /* KRB4 */
  10146. }
  10147.  
  10148. char *
  10149. #ifdef CK_ANSIC
  10150. ck_krb4_getrealm(void)
  10151. #else
  10152. ck_krb4_getrealm()
  10153. #endif
  10154. {
  10155. #ifdef KRB4
  10156.     char   *file=NULL;
  10157.     int     k_errno;
  10158.     static char realm[256]="";
  10159.     realm[0]='\0';
  10160.  
  10161.     if ( !ck_krb4_is_installed() )
  10162.         return(realm);
  10163.  
  10164.     /* Try to get realm from ticket file */
  10165.     /* If failure get the local realm    */
  10166.  
  10167.     /*
  10168.     * Since krb_get_tf_realm will return a ticket_file error,
  10169.     * we will call tf_init and tf_close first to filter out
  10170.     * things like no ticket file.
  10171.     */
  10172.  
  10173.     /* Open ticket file */
  10174.     file = tkt_string();
  10175.     if (file == NULL || !file[0])
  10176.         return(realm);
  10177.  
  10178.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  10179.         /* Close ticket file */
  10180.         (void) tf_close();
  10181.  
  10182.         k_errno = krb_get_tf_realm(file, realm);
  10183.     }
  10184.     if (k_errno != KSUCCESS) {
  10185.         k_errno = krb_get_lrealm(realm, 1);
  10186.     }
  10187.     return(realm);
  10188. #else /* KRB4 */
  10189.     return("");
  10190. #endif /* KRB4 */
  10191. }
  10192.  
  10193. char *
  10194. #ifdef CK_ANSIC
  10195. ck_krb4_getprincipal(void)
  10196. #else
  10197. ck_krb4_getprincipal()
  10198. #endif
  10199. {
  10200. #ifdef KRB4
  10201.     char   *file=NULL;
  10202.     int     k_errno;
  10203.     static char principal[256]="";
  10204.     char        instance[256]="";
  10205.     char        realm[256]="";
  10206.     principal[0]='\0';
  10207.  
  10208.     if ( !ck_krb4_is_installed() )
  10209.         return(principal);
  10210.  
  10211.     /* Try to get realm from ticket file */
  10212.     /* If failure get the local realm    */
  10213.  
  10214.     /*
  10215.     * Since krb_get_tf_realm will return a ticket_file error,
  10216.     * we will call tf_init and tf_close first to filter out
  10217.     * things like no ticket file.
  10218.     */
  10219.  
  10220.     /* Open ticket file */
  10221.     file = tkt_string();
  10222.     if (file == NULL || !file[0])
  10223.         return(principal);
  10224.  
  10225.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  10226.         /* Close ticket file */
  10227.         (void) tf_close();
  10228.  
  10229.         k_errno = krb_get_tf_fullname(file, principal, instance, realm);
  10230.     }
  10231.     return(principal);
  10232. #else /* KRB4 */
  10233.     return("");
  10234. #endif /* KRB4 */
  10235. }
  10236.  
  10237. static struct tkt_list_item * k5_tkt_list = NULL;
  10238.  
  10239. int
  10240. #ifdef CK_ANSIC
  10241. ck_krb5_get_tkts(char * cc_name)
  10242. #else
  10243. ck_krb5_get_tkts(cc_name) char * cc_name;
  10244. #endif
  10245. {
  10246. #ifdef KRB5
  10247. #ifndef HEIMDAL
  10248.     krb5_context kcontext;
  10249.     krb5_error_code retval;
  10250.     krb5_ccache cache = NULL;
  10251.     krb5_cc_cursor cur;
  10252.     krb5_creds creds;
  10253.     krb5_principal princ=NULL;
  10254.     krb5_flags flags=0;
  10255.     krb5_error_code code=0;
  10256.     int exit_status = 0;
  10257.  
  10258.     int     tkt_count=0;
  10259.     struct  tkt_list_item ** list = &k5_tkt_list;
  10260.  
  10261.     while ( k5_tkt_list ) {
  10262.         struct tkt_list_item * next;
  10263.         next = k5_tkt_list->next;
  10264.         free(k5_tkt_list->name);
  10265.         free(k5_tkt_list);
  10266.         k5_tkt_list = next;
  10267.     }
  10268.  
  10269.     if ( !ck_krb5_is_installed() )
  10270.         return(-1);
  10271.  
  10272.     retval = krb5_init_context(&kcontext);
  10273.     if (retval) {
  10274.         debug(F101,"ck_krb5_get_tkts while initializing krb5","",retval);
  10275.         return(-1);
  10276.     }
  10277.  
  10278.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10279.     if (code != 0) {
  10280.         debug(F111,"ck_krb5_get_tkts while getting ccache",
  10281.                error_message(code),code);
  10282.         tkt_count = -1;
  10283.         goto exit_k5_get_tkt;
  10284.     }
  10285.  
  10286.     flags = 0;                          /* turns off OPENCLOSE mode */
  10287.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10288.         if (code == ENOENT) {
  10289.             debug(F111,"ck_krb5_get_tkts (ticket cache)",
  10290.                    krb5_cc_get_name(kcontext, cache),code);
  10291.         } else {
  10292.             debug(F111,
  10293.                  "ck_krb5_get_tkts while setting cache flags (ticket cache)",
  10294.                   krb5_cc_get_name(kcontext, cache),code);
  10295.         }
  10296.         tkt_count = -1;
  10297.         goto exit_k5_get_tkt;
  10298.     }
  10299.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10300.         debug(F101,"ck_krb5_get_tkts while retrieving principal name",
  10301.                "",code);
  10302.         tkt_count = -1;
  10303.         goto exit_k5_get_tkt;
  10304.     }
  10305.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10306.         debug(F101,"ck_krb5_get_tkts while unparsing principal name",
  10307.                "",code);
  10308.         tkt_count = -1;
  10309.         goto exit_k5_get_tkt;
  10310.     }
  10311.  
  10312.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10313.         debug(F101,"ck_krb5_get_tkts while starting to retrieve tickets",
  10314.                "",code);
  10315.         tkt_count = -1;
  10316.         goto exit_k5_get_tkt;
  10317.     }
  10318.  
  10319.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10320.         char *sname=NULL;
  10321.  
  10322.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10323.         if (retval) {
  10324.             debug(F101,
  10325.                   "ck_krb5_get_tkts while unparsing server name","",retval);
  10326.             tkt_count = -1;
  10327.             goto exit_k5_get_tkt;
  10328.         }
  10329.  
  10330.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  10331.         (*list)->name = sname;
  10332.         (*list)->next = NULL;
  10333.         list = &((*list)->next);
  10334.  
  10335.         krb5_free_unparsed_name(kcontext,sname);
  10336.         krb5_free_cred_contents(kcontext, &creds);
  10337.         tkt_count++;
  10338.     }
  10339.  
  10340.     if (code == KRB5_CC_END) {
  10341.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10342.             debug(F101,"ck_krb5_get_tkts while finishing ticket retrieval",
  10343.                    "",code);
  10344.             tkt_count = -1;
  10345.             goto exit_k5_get_tkt;
  10346.         }
  10347.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10348.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10349.             debug(F101,"ck_krb5_get_tkts while closing ccache",
  10350.                    "",code);
  10351.             tkt_count = -1;
  10352.             goto exit_k5_get_tkt;
  10353.         }
  10354.     } else {
  10355.         debug(F101,"ck_krb5_get_tkts while retrieving a ticket","",code);
  10356.         tkt_count = -1;
  10357.         goto exit_k5_get_tkt;
  10358.     }
  10359.  
  10360.   exit_k5_get_tkt:
  10361.     krb5_free_principal(kcontext,princ);
  10362.     krb5_free_unparsed_name(kcontext,defname);
  10363.     krb5_cc_close(kcontext,cache);
  10364.     krb5_free_context(kcontext);
  10365.     return(tkt_count);
  10366. #else /* HEIMDAL */
  10367.     return(-1);
  10368. #endif /* HEIMDAL */
  10369. #else /* KRB5 */
  10370.     return(0);
  10371. #endif /* KRB5 */
  10372. }
  10373.  
  10374. char *
  10375. #ifdef CK_ANSIC
  10376. ck_krb5_get_next_tkt(VOID)
  10377. #else
  10378. ck_krb5_get_next_tkt()
  10379. #endif
  10380. {
  10381. #ifdef KRB5
  10382. #ifndef HEIMDAL
  10383.     static char * s=NULL;
  10384.     struct tkt_list_item * next=NULL;
  10385.  
  10386.     if ( s ) {
  10387.         free(s);
  10388.         s = NULL;
  10389.     }
  10390.  
  10391.     if ( k5_tkt_list == NULL )
  10392.         return(NULL);
  10393.  
  10394.     next = k5_tkt_list->next;
  10395.     s = k5_tkt_list->name;
  10396.     free(k5_tkt_list);
  10397.     k5_tkt_list = next;
  10398.     return(s);
  10399. #else /* HEIMDAL */
  10400.     return("Not implemented");
  10401. #endif /* HEIMDAL */
  10402. #else /* KRB5 */
  10403.     return(NULL);
  10404. #endif /* KRB5 */
  10405. }
  10406.  
  10407. char *
  10408. #ifdef CK_ANSIC
  10409. ck_krb5_tkt_flags(char * cc_name, char * tktname)
  10410. #else
  10411. ck_krb5_tkt_flags(cc_name,tktname) char * cc_name; char * tktname;
  10412. #endif
  10413. {
  10414. #ifdef KRB5
  10415. #ifndef HEIMDAL
  10416.     krb5_context kcontext;
  10417.     krb5_error_code retval;
  10418.     krb5_ccache cache = NULL;
  10419.     krb5_cc_cursor cur;
  10420.     krb5_creds creds;
  10421.     krb5_principal princ=NULL;
  10422.     krb5_flags flags=0;
  10423.     krb5_error_code code=0;
  10424.     char * flag_str = "";
  10425.  
  10426.     if ( !ck_krb5_is_installed() )
  10427.         return("");
  10428.  
  10429.     retval = krb5_init_context(&kcontext);
  10430.     if (retval) {
  10431.         debug(F101,"ck_krb5_tkt_flags while initializing krb5","",retval);
  10432.         return("");
  10433.     }
  10434.  
  10435.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10436.     if (code != 0) {
  10437.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10438.                error_message(code),code);
  10439.         goto exit_k5_get_tkt;
  10440.     }
  10441.  
  10442.     flags = 0;                          /* turns off OPENCLOSE mode */
  10443.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10444.         if (code == ENOENT) {
  10445.             debug(F111,"ck_krb5_tkt_flags (ticket cache)",
  10446.                    krb5_cc_get_name(kcontext, cache),code);
  10447.         } else {
  10448.             debug(F111,
  10449.                  "ck_krb5_tkt_flags while setting cache flags (ticket cache)",
  10450.                   krb5_cc_get_name(kcontext, cache),code);
  10451.         }
  10452.         retval = -1;
  10453.         goto exit_k5_get_tkt;
  10454.     }
  10455.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10456.         debug(F101,"ck_krb5_tkt_flags while retrieving principal name",
  10457.                "",code);
  10458.         retval = -1;
  10459.         goto exit_k5_get_tkt;
  10460.     }
  10461.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10462.         debug(F101,"ck_krb5_tkt_flags while unparsing principal name",
  10463.                "",code);
  10464.         retval = -1;
  10465.         goto exit_k5_get_tkt;
  10466.     }
  10467.  
  10468.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10469.         debug(F101,"ck_krb5_tkt_flags while starting to retrieve tickets",
  10470.                "",code);
  10471.         retval = -1;
  10472.         goto exit_k5_get_tkt;
  10473.     }
  10474.  
  10475.     if ((code = krb5_timeofday(kcontext, &now))) {
  10476.         if (!status_only)
  10477.             debug(F101,"ck_krb5_tkt_flags while getting time of day.",
  10478.                    "",code);
  10479.         retval = -1;
  10480.         goto exit_k5_get_tkt;
  10481.     }
  10482.  
  10483.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10484.         char *sname=NULL;
  10485.  
  10486.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10487.         if (retval) {
  10488.             debug(F101,
  10489.                   "ck_krb5_tkt_flags while unparsing server name","",retval);
  10490.             retval = -1;
  10491.             krb5_free_cred_contents(kcontext, &creds);
  10492.             goto exit_k5_get_tkt;
  10493.         }
  10494.  
  10495.         if ( !strcmp(sname,tktname) ) {
  10496.             /* we found the ticket we are looking for */
  10497.  
  10498.             flag_str = flags_string(&creds);
  10499.  
  10500.             krb5_free_unparsed_name(kcontext,sname);
  10501.             krb5_free_cred_contents(kcontext, &creds);
  10502.             code = KRB5_CC_END;
  10503.             break;
  10504.         }
  10505.         krb5_free_unparsed_name(kcontext,sname);
  10506.         krb5_free_cred_contents(kcontext, &creds);
  10507.     }
  10508.  
  10509.     if (code == KRB5_CC_END) {
  10510.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10511.             debug(F101,"ck_krb5_tkt_flags while finishing ticket retrieval",
  10512.                    "",code);
  10513.             goto exit_k5_get_tkt;
  10514.         }
  10515.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10516.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10517.             debug(F101,"ck_krb5_tkt_flags while closing ccache",
  10518.                    "",code);
  10519.             goto exit_k5_get_tkt;
  10520.         }
  10521.     } else {
  10522.         debug(F101,"ck_krb5_tkt_flags while retrieving a ticket","",code);
  10523.         goto exit_k5_get_tkt;
  10524.     }
  10525.  
  10526.   exit_k5_get_tkt:
  10527.     krb5_free_principal(kcontext,princ);
  10528.     krb5_free_unparsed_name(kcontext,defname);
  10529.     krb5_cc_close(kcontext,cache);
  10530.     krb5_free_context(kcontext);
  10531.     return(flag_str);
  10532. #else /* HEIMDAL */
  10533.     return("Not implemented");
  10534. #endif /* HEIMDAL */
  10535. #else /* KRB5 */
  10536.     return("");
  10537. #endif /* KRB5 */
  10538. }
  10539.  
  10540.  
  10541. int
  10542. #ifdef CK_ANSIC
  10543. ck_krb5_tkt_isvalid(char * cc_name, char * tktname)
  10544. #else
  10545. ck_krb5_tkt_isvalid(cc_name,tktname) char * cc_name; char * tktname;
  10546. #endif
  10547. {
  10548. #ifdef KRB5
  10549. #ifndef HEIMDAL
  10550.     krb5_context kcontext=NULL;
  10551.     krb5_error_code retval;
  10552.     krb5_ccache cache = NULL;
  10553.     krb5_cc_cursor cur;
  10554.     krb5_creds creds;
  10555.     krb5_principal princ=NULL;
  10556.     krb5_flags flags=0;
  10557.     krb5_error_code code=0;
  10558. #ifdef CHECKADDRS
  10559.     krb5_address **     myAddrs=NULL;
  10560.     krb5_address **     p=NULL;
  10561.     BOOL                Addrfound = FALSE;
  10562. #endif /*CHECKADDRS*/
  10563.  
  10564.     if ( !ck_krb5_is_installed() )
  10565.         return(-1);
  10566.  
  10567.     retval = krb5_init_context(&kcontext);
  10568.     if (retval) {
  10569.         debug(F101,"ck_krb5_tkt_isvalid while initializing krb5","",retval);
  10570.         return(-1);
  10571.     }
  10572.  
  10573.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10574.     if (code != 0) {
  10575.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10576.                error_message(code),code);
  10577.         goto exit_k5_get_tkt;
  10578.     }
  10579.  
  10580.     flags = 0;                          /* turns off OPENCLOSE mode */
  10581.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10582.         if (code == ENOENT) {
  10583.             debug(F111,"ck_krb5_tkt_isvalid (ticket cache)",
  10584.                    krb5_cc_get_name(kcontext, cache),code);
  10585.         } else {
  10586.             debug(F111,
  10587.                 "ck_krb5_tkt_isvalid while setting cache flags (ticket cache)",
  10588.                   krb5_cc_get_name(kcontext, cache),code);
  10589.         }
  10590.         retval = -1;
  10591.         goto exit_k5_get_tkt;
  10592.     }
  10593.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10594.         debug(F101,"ck_krb5_tkt_isvalid while retrieving principal name",
  10595.                "",code);
  10596.         retval = -1;
  10597.         goto exit_k5_get_tkt;
  10598.     }
  10599.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10600.         debug(F101,"ck_krb5_tkt_isvalid while unparsing principal name",
  10601.                "",code);
  10602.         retval = -1;
  10603.         goto exit_k5_get_tkt;
  10604.     }
  10605.  
  10606.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10607.         debug(F101,"ck_krb5_tkt_isvalid while starting to retrieve tickets",
  10608.                "",code);
  10609.         retval = -1;
  10610.         goto exit_k5_get_tkt;
  10611.     }
  10612.  
  10613.     if ((code = krb5_timeofday(kcontext, &now))) {
  10614.         if (!status_only)
  10615.             debug(F101,"ck_krb5_tkt_isvalid while getting time of day.",
  10616.                    "",code);
  10617.         retval = -1;
  10618.         goto exit_k5_get_tkt;
  10619.     }
  10620.  
  10621.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10622.         char *sname=NULL;
  10623.  
  10624.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10625.         if (retval) {
  10626.             debug(F101,
  10627.                   "ck_krb5_tkt_isvalid while unparsing server name","",retval);
  10628.             retval = -1;
  10629.             krb5_free_cred_contents(kcontext, &creds);
  10630.             goto exit_k5_get_tkt;
  10631.         }
  10632.  
  10633.         if ( !strcmp(sname,tktname) ) {
  10634.             /* we found the ticket we are looking for */
  10635.  
  10636.             /* We add a 5 minutes fudge factor to compensate for potential */
  10637.             /* clock skew errors between the KDC and K95's host OS         */
  10638.  
  10639.             retval = ((creds.times.starttime > 0) &&
  10640.                        now >= (creds.times.starttime - 300) &&
  10641.                        now < (creds.times.endtime + 300) &&
  10642.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10643.  
  10644. #ifdef CHECKADDRS
  10645.             if ( retval && krb5_checkaddrs &&
  10646.                                  creds.addresses && creds.addresses[0] ) {
  10647.                 /* if we think it is valid, then lets check the IP Addresses */
  10648.                 /* to make sure it is valid for our current connection.      */
  10649.                 /* Also make sure it's for the correct IP address */
  10650.                 retval = krb5_os_localaddr(kcontext, &myAddrs);
  10651.                 if (retval) {
  10652.                     com_err(NULL, retval, "retrieving my IP address");
  10653.                     krb5_free_unparsed_name(kcontext,sname);
  10654.                     krb5_free_cred_contents(kcontext, &creds);
  10655.                     code = KRB5_CC_END;
  10656.                     retval = -1;
  10657.                     break;
  10658.                 }
  10659.  
  10660.              /* See if any of our addresses match any in cached credentials */
  10661.  
  10662.                 for (Addrfound=FALSE, p=myAddrs;
  10663.                      (Addrfound==FALSE) && (*p);
  10664.                      p++
  10665.                      ) {
  10666.                     if (krb5_address_search(kcontext, *p, creds.addresses)) {
  10667.                         Addrfound = TRUE;
  10668.                     }
  10669.                 }
  10670.                 krb5_free_addresses(k5_context, myAddrs);
  10671.  
  10672.                 if (Addrfound) {
  10673.                     krb5_free_unparsed_name(kcontext,sname);
  10674.                     krb5_free_cred_contents(kcontext, &creds);
  10675.                     code = KRB5_CC_END;
  10676.                     retval = 1;
  10677.                     break;
  10678.                 } else {
  10679.                     krb5_free_unparsed_name(kcontext,sname);
  10680.                     krb5_free_cred_contents(kcontext, &creds);
  10681.                     code = KRB5_CC_END;
  10682.                     retval = 0;
  10683.                     break;
  10684.                 }
  10685.             }
  10686. #endif /* CHECKADDRS */
  10687.  
  10688.             krb5_free_unparsed_name(kcontext,sname);
  10689.             krb5_free_cred_contents(kcontext, &creds);
  10690.             code = KRB5_CC_END;
  10691.             break;
  10692.         }
  10693.         krb5_free_unparsed_name(kcontext,sname);
  10694.         krb5_free_cred_contents(kcontext, &creds);
  10695.     }
  10696.  
  10697.     if (code == KRB5_CC_END) {
  10698.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10699.             debug(F101,"ck_krb5_tkt_isvalid while finishing ticket retrieval",
  10700.                    "",code);
  10701.             retval = -1;
  10702.             goto exit_k5_get_tkt;
  10703.         }
  10704.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10705.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10706.             debug(F101,"ck_krb5_tkt_isvalid while closing ccache",
  10707.                    "",code);
  10708.             retval = -1;
  10709.             goto exit_k5_get_tkt;
  10710.         }
  10711.     } else {
  10712.         debug(F101,"ck_krb5_tkt_isvalid while retrieving a ticket","",code);
  10713.         retval = -1;
  10714.         goto exit_k5_get_tkt;
  10715.     }
  10716.  
  10717.   exit_k5_get_tkt:
  10718.     krb5_free_principal(kcontext,princ);
  10719.     krb5_free_unparsed_name(kcontext,defname);
  10720.     krb5_cc_close(kcontext,cache);
  10721.     krb5_free_context(kcontext);
  10722.     return(retval);
  10723. #else /* HEIMDAL */
  10724.     return(-1);
  10725. #endif /* HEIMDAL */
  10726. #else /* KRB5 */
  10727.     return(-1);
  10728. #endif /* KRB5 */
  10729. }
  10730.  
  10731. int
  10732. #ifdef CK_ANSIC
  10733. ck_krb5_is_tgt_valid(VOID)
  10734. #else
  10735. ck_krb5_is_tgt_valid()
  10736. #endif
  10737. {
  10738. #ifdef KRB5
  10739. #ifndef HEIMDAL
  10740.     char tgt[256];
  10741.     char * s;
  10742.     int rc = 0;
  10743.  
  10744.     s = ck_krb5_getrealm(krb5_d_cc);
  10745.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",s,"@",s);
  10746.     rc = ck_krb5_tkt_isvalid(krb5_d_cc,tgt);
  10747.     debug(F111,"ck_krb5_is_tgt_valid",tgt,rc);
  10748.     return(rc>0);
  10749. #else /* HEIMDAL */
  10750.     return(-1);
  10751. #endif /* HEIMDAL */
  10752. #else /* KRB5 */
  10753.     return(0);
  10754. #endif /* KRB5 */
  10755. }
  10756.  
  10757. int
  10758. #ifdef CK_ANSIC
  10759. ck_krb5_tkt_time(char * cc_name, char * tktname)
  10760. #else
  10761. ck_krb5_tkt_time(cc_name, tktname) char * cc_name; char * tktname;
  10762. #endif
  10763. {
  10764. #ifdef KRB5
  10765. #ifndef HEIMDAL
  10766.     krb5_context kcontext;
  10767.     krb5_error_code retval;
  10768.     krb5_ccache cache = NULL;
  10769.     krb5_cc_cursor cur;
  10770.     krb5_creds creds;
  10771.     krb5_principal princ=NULL;
  10772.     krb5_flags flags=0;
  10773.     krb5_error_code code=0;
  10774.  
  10775.     if ( !ck_krb5_is_installed() )
  10776.         return(-1);
  10777.  
  10778.     retval = krb5_init_context(&kcontext);
  10779.     if (retval) {
  10780.         debug(F101,"ck_krb5_list_creds while initializing krb5","",retval);
  10781.         return(-1);
  10782.     }
  10783.  
  10784.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10785.     if (code != 0) {
  10786.         debug(F111,"ck_krb5_tkt_time while getting ccache",
  10787.                error_message(code),code);
  10788.         retval = -1;
  10789.         goto exit_k5_get_tkt;
  10790.     }
  10791.  
  10792.     flags = 0;                          /* turns off OPENCLOSE mode */
  10793.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10794.         if (code == ENOENT) {
  10795.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  10796.                    krb5_cc_get_name(kcontext, cache),code);
  10797.         } else {
  10798.             debug(F111,
  10799.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  10800.                   krb5_cc_get_name(kcontext, cache),code);
  10801.         }
  10802.         retval = -1;
  10803.         goto exit_k5_get_tkt;
  10804.     }
  10805.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10806.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  10807.                "",code);
  10808.         retval = -1;
  10809.         goto exit_k5_get_tkt;
  10810.     }
  10811.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10812.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  10813.                "",code);
  10814.         retval = -1;
  10815.         goto exit_k5_get_tkt;
  10816.     }
  10817.  
  10818.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10819.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  10820.                "",code);
  10821.         retval = -1;
  10822.         goto exit_k5_get_tkt;
  10823.     }
  10824.  
  10825.     if ((code = krb5_timeofday(kcontext, &now))) {
  10826.         if (!status_only)
  10827.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  10828.                    "",code);
  10829.         krb5_free_context(kcontext);
  10830.         return(-1);
  10831.     }
  10832.  
  10833.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10834.         char *sname=NULL;
  10835.  
  10836.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10837.         if (retval) {
  10838.             debug(F101,
  10839.                   "ck_krb5_list_creds while unparsing server name","",retval);
  10840.             retval = -1;
  10841.             krb5_free_unparsed_name(kcontext,sname);
  10842.             krb5_free_cred_contents(kcontext, &creds);
  10843.             goto exit_k5_get_tkt;
  10844.         }
  10845.  
  10846.         if ( !strcmp(sname,tktname) ) {
  10847.             /* we found the ticket we are looking for */
  10848.             int valid = (creds.times.starttime &&
  10849.                        now > creds.times.starttime &&
  10850.                        now < creds.times.endtime &&
  10851.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10852.             if ( valid ) {
  10853.                 retval = creds.times.endtime - now;
  10854.             }
  10855.             else
  10856.                 retval = 0;
  10857.             krb5_free_unparsed_name(kcontext,sname);
  10858.             krb5_free_cred_contents(kcontext, &creds);
  10859.             code = KRB5_CC_END;
  10860.             break;
  10861.         }
  10862.         krb5_free_unparsed_name(kcontext,sname);
  10863.         krb5_free_cred_contents(kcontext, &creds);
  10864.     }
  10865.  
  10866.     if (code == KRB5_CC_END) {
  10867.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10868.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  10869.                    "",code);
  10870.             retval = -1;
  10871.             goto exit_k5_get_tkt;
  10872.         }
  10873.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10874.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10875.             debug(F101,"ck_krb5_list_creds while closing ccache",
  10876.                    "",code);
  10877.             retval = -1;
  10878.             goto exit_k5_get_tkt;
  10879.         }
  10880.     } else {
  10881.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  10882.         retval = -1;
  10883.         goto exit_k5_get_tkt;
  10884.     }
  10885.  
  10886.   exit_k5_get_tkt:
  10887.     krb5_free_principal(kcontext,princ);
  10888.     krb5_free_unparsed_name(kcontext,defname);
  10889.     krb5_cc_close(kcontext,cache);
  10890.     krb5_free_context(kcontext);
  10891.     return(retval);
  10892. #else /* HEIMDAL */
  10893.     return(-1);
  10894. #endif /* HEIMDAL */
  10895. #else /* KRB5 */
  10896.     return(-1);
  10897. #endif /* KRB5 */
  10898. }
  10899.  
  10900. char *
  10901. #ifdef CK_ANSIC
  10902. ck_krb5_get_cc_name(void)
  10903. #else
  10904. ck_krb5_get_cc_name()
  10905. #endif
  10906. {
  10907. #ifdef KRB5
  10908. #ifndef HEIMDAL
  10909.     static char cc_name[CKMAXPATH+1]="";
  10910.     krb5_context kcontext = NULL;
  10911.     krb5_ccache ccache = NULL;
  10912.     krb5_error_code code;
  10913.     char * p=NULL;
  10914.  
  10915.     cc_name[0] = '\0';
  10916.  
  10917.     if ( !ck_krb5_is_installed() )
  10918.         return(cc_name);
  10919.  
  10920.     p = getenv("KRB5CCNAME");
  10921.     if ( !p ) {
  10922.         code = krb5_init_context(&kcontext);
  10923.         if (code) {
  10924.             com_err("ck_krb5_get_cc_name",code,"while init_context");
  10925.             return(cc_name);
  10926.         }
  10927.         if ((code = krb5_cc_default(kcontext, &ccache))) {
  10928.             com_err("ck_krb5_get_cc_name",code,"while getting default ccache");
  10929.             goto exit_k5_get_cc;
  10930.         }
  10931.  
  10932.         ckmakmsg(cc_name,sizeof(cc_name),
  10933.                  (char *)krb5_cc_get_type(kcontext,ccache),":",
  10934.                  (char *)krb5_cc_get_name(kcontext,ccache),NULL);
  10935.     } else {
  10936.         ckstrncpy(cc_name,p,CKMAXPATH);
  10937.     }
  10938.  
  10939.     if ( !strncmp("FILE:",cc_name,5) ) {
  10940.         for ( p=cc_name; *p ; p++ )
  10941.             if ( *p == '\\' ) *p = '/';
  10942.     }
  10943.  
  10944.   exit_k5_get_cc:
  10945.     if ( ccache )
  10946.         krb5_cc_close(kcontext,ccache);
  10947.     if ( kcontext )
  10948.         krb5_free_context(kcontext);
  10949.     return(cc_name);
  10950. #else /* HEIMDAL */
  10951.     return("Not implemented");
  10952. #endif /* HEIMDAL */
  10953. #else /* KRB5 */
  10954.     return("");
  10955. #endif /* KRB5 */
  10956. }
  10957.  
  10958. char *
  10959. #ifdef CK_ANSIC
  10960. ck_krb5_getrealm(char * cc_name)
  10961. #else
  10962. ck_krb5_getrealm(cc_name) char * cc_name;
  10963. #endif
  10964. {
  10965. #ifdef KRB5
  10966. #ifndef HEIMDAL
  10967.     static char realm[256]="";
  10968.     krb5_context kcontext;
  10969.     krb5_ccache ccache = NULL;
  10970.     krb5_error_code code;
  10971.     krb5_principal me=NULL;
  10972.  
  10973.     realm[0] = '\0';
  10974.  
  10975.     if ( !ck_krb5_is_installed() )
  10976.         return(realm);
  10977.  
  10978.     code = krb5_init_context(&kcontext);
  10979.     if (code) {
  10980.         return(realm);
  10981.     }
  10982.  
  10983.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10984.     if (code != 0) {
  10985.         goto exit_k5_getrealm;
  10986.     }
  10987.  
  10988.     code = krb5_cc_get_principal(kcontext, ccache, &me);
  10989.     if (code)
  10990.         code = krb5_parse_name(kcontext, "foo", &me);
  10991.     if (code) {
  10992.         goto exit_k5_getrealm;
  10993.     }
  10994.     if ( krb5_princ_realm(kcontext, me)->length < sizeof(realm) ) {
  10995.         memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  10996.                 krb5_princ_realm(kcontext, me)->length);        /* safe */
  10997.        realm[krb5_princ_realm(kcontext, me)->length]='\0';
  10998.     }
  10999.   exit_k5_getrealm:
  11000.     if ( me )
  11001.         krb5_free_principal(kcontext,me);
  11002.     if ( ccache )
  11003.         krb5_cc_close(kcontext,ccache);
  11004.     if (kcontext)
  11005.         krb5_free_context(kcontext);
  11006.     return(realm);
  11007. #else /* HEIMDAL */
  11008.     return("Not implemented");
  11009. #endif /* HEIMDAL */
  11010. #else /* KRB5 */
  11011.     return("");
  11012. #endif /* KRB5 */
  11013. }
  11014.  
  11015. char *
  11016. #ifdef CK_ANSIC
  11017. ck_krb5_getprincipal(char * cc_name)
  11018. #else
  11019. ck_krb5_getprincipal(cc_name) char * cc_name;
  11020. #endif
  11021. {
  11022. #ifdef KRB5
  11023. #ifndef HEIMDAL
  11024.     static char principal[UIDBUFLEN+1]="";
  11025.     krb5_context kcontext;
  11026.     krb5_ccache ccache = NULL;
  11027.     krb5_error_code code;
  11028.     krb5_principal me;
  11029.     char * p=NULL;
  11030.     int i;
  11031.  
  11032.     principal[0] = '\0';
  11033.  
  11034.     if ( !ck_krb5_is_installed() )
  11035.         return(principal);
  11036.  
  11037.     code = krb5_init_context(&kcontext);
  11038.     if (code) {
  11039.         return(principal);
  11040.     }
  11041.  
  11042.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  11043.     if (code != 0) {
  11044.         goto exit_k5_getprincipal;
  11045.     }
  11046.  
  11047.     if ((code = krb5_cc_get_principal(kcontext, ccache, &me))) {
  11048.         goto exit_k5_getprincipal;
  11049.     }
  11050.  
  11051.     if ((code = krb5_unparse_name (kcontext, me, &p))) {
  11052.         krb5_free_principal(kcontext,me);
  11053.         goto exit_k5_getprincipal;
  11054.     }
  11055.  
  11056.     ckstrncpy(principal,p,UIDBUFLEN);
  11057.     i = ckindex("@",principal,0,0,0);
  11058.     if (i)
  11059.       principal[i-1] = '\0';
  11060.  
  11061.     krb5_free_unparsed_name(kcontext,p);
  11062.  
  11063.   exit_k5_getprincipal:
  11064.     if ( ccache )
  11065.         krb5_cc_close(kcontext,ccache);
  11066.     if (kcontext)
  11067.         krb5_free_context(kcontext);
  11068.     return(principal);
  11069. #else /* HEIMDAL */
  11070.     return("Not implemented");
  11071. #endif /* HEIMDAL */
  11072. #else /* KRB5 */
  11073.     return("");
  11074. #endif /* KRB5 */
  11075. }
  11076.  
  11077. #ifndef CRYPT_DLL
  11078. int
  11079. ck_get_crypt_table(struct keytab ** pTable, int * pN)
  11080. {
  11081. #ifdef CK_ENCRYPTION
  11082.     return(get_crypt_table(pTable, pN));
  11083. #else /* ENCRYPTION */
  11084.     int i=0;
  11085. #ifndef OS2
  11086.     char * tmpstring = NULL;
  11087. #endif /* OS2 */
  11088.  
  11089.     if ( *pTable )
  11090.     {
  11091.         for ( i=0 ; i < *pN ; i++ )
  11092.             free( (*pTable)[i].kwd ) ;
  11093.         free ( *pTable )  ;
  11094.     }
  11095.     *pTable = NULL;
  11096.     *pN = 0;
  11097.  
  11098.     *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  11099.     if ( !(*pTable) )
  11100.         return(0);
  11101.  
  11102. #ifdef OS2
  11103.     (*pTable)[0].kwd =strdup("automatic");
  11104. #else /* OS2 */
  11105.     makestr(&tmpstring,"automatic");
  11106.     (*pTable)[0].kwd = tmpstring;
  11107.     tmpstring = NULL;
  11108. #endif /* OS2 */
  11109.     (*pTable)[0].kwval = ENCTYPE_ANY;
  11110.     (*pTable)[0].flgs = 0;
  11111. #ifdef OS2
  11112.     (*pTable)[1].kwd =strdup("none");
  11113. #else /* OS2 */
  11114.     makestr(&tmpstring,"none");
  11115.     (*pTable)[1].kwd = tmpstring;
  11116.     tmpstring = NULL;
  11117. #endif /* OS2 */
  11118.     (*pTable)[1].kwval = 999;
  11119.     (*pTable)[1].flgs = 0;
  11120.     (*pN) = 2;
  11121.  
  11122.     return(2);
  11123. #endif /* ENCRYPTION */
  11124. }
  11125.  
  11126. VOID
  11127. ck_encrypt_send_support()
  11128. {
  11129. #ifdef CK_ENCRYPTION
  11130.     encrypt_send_support();
  11131. #endif /* ENCRYPTION */
  11132. }
  11133. #endif /* CRYPT_DLL */
  11134.  
  11135. /*
  11136.  *
  11137.  * Kstream
  11138.  *
  11139.  * Emulates the kstream package in Kerberos 4
  11140.  *
  11141.  */
  11142.  
  11143. int
  11144. kstream_destroy()
  11145. {
  11146.     if (g_kstream != NULL) {
  11147.         auth_destroy();                       /* Destroy authorizing */
  11148.         free(g_kstream);
  11149.         g_kstream=NULL;
  11150.     }
  11151.     return 0;
  11152. }
  11153.  
  11154. VOID
  11155. #ifdef CK_ANSIC
  11156. kstream_set_buffer_mode(int mode)
  11157. #else
  11158. kstream_set_buffer_mode(mode) int mode;
  11159. #endif
  11160. {
  11161. }
  11162.  
  11163.  
  11164. int
  11165. #ifdef CK_ANSIC
  11166. kstream_create_from_fd(int fd,
  11167.                        kstream_ptr data)
  11168. #else
  11169. kstream_create_from_fd(fd,data)
  11170.     int fd; kstream_ptr data;
  11171. #endif
  11172. {
  11173.     int n;
  11174.  
  11175.     g_kstream = malloc(sizeof(struct kstream_int));
  11176.     if (g_kstream == NULL)
  11177.         return 0;
  11178.  
  11179.     g_kstream->fd = fd;
  11180.  
  11181.     n = auth_init(g_kstream);                   /* Initialize authorizing */
  11182.     if (n) {
  11183.         free(g_kstream);
  11184.         g_kstream = NULL;
  11185.         return 0;
  11186.     }
  11187.  
  11188.     g_kstream->encrypt = NULL;
  11189.     g_kstream->decrypt = NULL;
  11190.     g_kstream->encrypt_type = ENCTYPE_ANY;
  11191.     g_kstream->decrypt_type = ENCTYPE_ANY;
  11192.     return 1;
  11193. }
  11194.  
  11195. #ifdef CK_KERBEROS
  11196. #ifdef RLOGCODE
  11197. static int do_lencheck, use_ivecs;
  11198. extern int rlog_inband;
  11199.  
  11200. #ifdef KRB5
  11201. void
  11202. rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
  11203.                            protonum)
  11204.      krb5_keyblock *in_keyblock;
  11205.      int encrypt_flag;
  11206.      int lencheck;
  11207.      int am_client;
  11208.      enum krb5_kcmd_proto protonum;
  11209. {
  11210.     krb5_error_code status;
  11211.     size_t blocksize;
  11212.  
  11213.     if (!encrypt_flag)
  11214.         return;
  11215.  
  11216.     desinbuf.data = des_inbuf;
  11217.     desoutbuf.data = des_outpkt+4;      /* Set up des buffers */
  11218.     k5_session_key = in_keyblock;
  11219.  
  11220.     do_lencheck = lencheck;
  11221.  
  11222.     if ( protonum == KCMD_OLD_PROTOCOL ) {
  11223.         use_ivecs = 0;
  11224.         return;
  11225.     }
  11226.  
  11227.     use_ivecs = 1;
  11228.  
  11229.     if (status = krb5_c_block_size(k5_context, k5_session_key->enctype,
  11230.                                    &blocksize)) {
  11231.         /* XXX what do I do? */
  11232.         printf("fatal kerberos 5 crypto library error\n");
  11233.         ttclos(0);
  11234.         return;
  11235.     }
  11236.  
  11237.     encivec_i[0].length = encivec_i[1].length = 
  11238.     encivec_o[0].length = encivec_o[1].length = blocksize;
  11239.  
  11240.     if ((encivec_i[0].data = malloc(encivec_i[0].length * 4)) == NULL) {
  11241.         /* XXX what do I do? */
  11242.         printf("fatal malloc failed\n");
  11243.         ttclos(0);
  11244.         return;
  11245.     }
  11246.  
  11247.     encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
  11248.     encivec_o[0].data = encivec_i[1].data + encivec_i[1].length;
  11249.     encivec_o[1].data = encivec_o[0].data + encivec_o[0].length;
  11250.  
  11251.     /* is there a better way to initialize this? */
  11252.     memset(encivec_i[0].data, am_client, blocksize);
  11253.     memset(encivec_o[0].data, 1 - am_client, blocksize);
  11254.     memset(encivec_i[1].data, 2 | am_client, blocksize);
  11255.     memset(encivec_o[1].data, 2 | (1 - am_client), blocksize);
  11256. }
  11257. #endif /* KRB5 */
  11258.  
  11259. int
  11260. #ifdef CK_ANSIC
  11261. ck_krb_rlogin(CHAR * hostname, int port,
  11262.                CHAR * localuser, CHAR * remoteuser, CHAR * term_speed,
  11263.                struct sockaddr_in * l_addr, struct sockaddr_in * r_addr,
  11264.                int kversion, int encrypt_flag)
  11265. #else /* CK_ANSIC */
  11266. ck_krb_rlogin(hostname, port,
  11267.                localuser, remoteuser, term_speed, l_addr, r_addr, encrypt_flag)
  11268.     CHAR * hostname; int port;
  11269.     CHAR * localuser; CHAR * remoteuser; CHAR * term_speed;
  11270.     struct sockaddr_in * l_addr; struct sockaddr_in * r_addr;
  11271.     int kversion; int encrypt_flag;
  11272. #endif /* CK_ANSIC */
  11273. {
  11274.     unsigned long status;
  11275.     char * realm=NULL;
  11276.     extern int ttyfd;
  11277.     int c;
  11278.     long msglen;
  11279.  
  11280.     debug(F111,"ck_krb_rlogin",hostname,port);
  11281.  
  11282.     if ( kversion == 4 && !ck_krb4_is_installed() ) {
  11283.         printf("?Kerberos 4 is not installed\r\n");
  11284.         return(-1);
  11285.     } else if ( kversion == 5 && !ck_krb5_is_installed() ) {
  11286.         printf("?Kerberos 5 is not installed\r\n");
  11287.         return(-1);
  11288.     }
  11289.  
  11290.     if ( encrypt_flag && !ck_crypt_is_installed() ) {
  11291.         printf("?Encryption is not installed\r\n");
  11292.         return(-1);
  11293.     }
  11294.  
  11295.     if ( kversion == 5 ) {
  11296. #ifdef KRB5
  11297.         krb5_flags authopts=0;
  11298.         krb5_ccache ccache=NULL;
  11299.         char *cksumbuf=NULL;
  11300.         char *service=NULL;
  11301.         char * kcmd_version=NULL;
  11302.         enum krb5_kcmd_proto use_proto;
  11303.         krb5_data cksumdat;
  11304.         krb5_creds *get_cred = 0;
  11305.         krb5_error_code status;
  11306.         krb5_error      *error = 0;
  11307.         krb5_ap_rep_enc_part *rep_ret = NULL;
  11308.         krb5_data outbuf;
  11309.         int rc;
  11310.         krb5_int32 seqno=0;
  11311.         krb5_int32 server_seqno=0;
  11312.         char ** realmlist=NULL;
  11313.         int buflen;
  11314.         char tgt[256];
  11315.  
  11316.         debug(F100,"ck_krb_rlogin version 5","",0);
  11317.  
  11318.         realm = ck_krb5_realmofhost((char *)hostname);
  11319.         if (!realm) {
  11320.             ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  11321.             ckstrncat(strTmp, (char *)hostname,AUTHTMPBL);
  11322.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11323.             printf("?Kerberos 5 error: %s\r\n",strTmp);
  11324.             krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  11325.             makestr(&krb5_errmsg,strTmp);
  11326.             return(0);
  11327.         }
  11328.  
  11329.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11330.         debug(F110,"ck_rlog_rlogin TGT",tgt,0);
  11331.         if ( krb5_autoget &&
  11332.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11333.                 (ck_krb5_is_tgt_valid() > 0)) )
  11334.             ck_krb5_autoget_TGT(realm);
  11335.  
  11336.         buflen = strlen((char *)term_speed) + strlen((char *)remoteuser) + 64;
  11337.         if ((cksumbuf = malloc(buflen)) == 0) {
  11338.             printf("Unable to allocate memory for checksum buffer.\r\n");
  11339.             return(-1);
  11340.         }
  11341.         ckmakmsg(cksumbuf,buflen,ckuitoa((unsigned short) ntohs(port)),":",
  11342.          (char *)term_speed,(char *)remoteuser);
  11343.         cksumdat.data = cksumbuf;
  11344.         cksumdat.length = strlen(cksumbuf);
  11345.  
  11346.         status = krb5_init_context(&k5_context);
  11347.         if (status) {
  11348.             debug(F110,"ck_krb_rlogin()","unable to init_context",0);
  11349.             return(-1);
  11350.         }
  11351.  
  11352.         desinbuf.data = des_inbuf;
  11353.         desoutbuf.data = des_outpkt+4;  /* Set up des buffers */
  11354.  
  11355.         rc = k5_get_ccache(k5_context,&ccache,NULL);
  11356.         if (rc != 0) {
  11357.             com_err(NULL, rc, "while getting ccache.");
  11358.             return(0);
  11359.         }
  11360.  
  11361.         service = krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME;
  11362.  
  11363.         if (!(get_cred = (krb5_creds *)calloc(1, sizeof(krb5_creds)))) {
  11364.             printf("ck_krb_rlogin: no memory\r\n");
  11365.             return(-1);
  11366.         }
  11367.         memset(get_cred,0,sizeof(krb5_creds));
  11368.         status = krb5_sname_to_principal(k5_context, (char *) hostname,
  11369.                      service, KRB5_NT_SRV_HST,
  11370.                      &get_cred->server);
  11371.         if (status) {
  11372.             printf("ck_krb_rlogin: krb5_sname_to_principal failed: %s\r\n",
  11373.                      error_message(status));
  11374.             return(-1);
  11375.         }
  11376.  
  11377.         ttoc(0);
  11378.  
  11379.         if (status = krb5_cc_get_principal(k5_context,
  11380.                                            ccache,
  11381.                                            &get_cred->client)
  11382.             ) {
  11383.             (void) krb5_cc_close(k5_context, ccache);
  11384.             krb5_free_creds(k5_context, get_cred);
  11385.             goto bad;
  11386.         }
  11387.  
  11388.         if (krb5_rlog_ver == KCMD_OLD_PROTOCOL)
  11389.             get_cred->keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  11390.  
  11391.         /* Get ticket from credentials cache or kdc */
  11392.         status = krb5_get_credentials(k5_context,
  11393.                                       0,
  11394.                                       ccache,
  11395.                                       get_cred,
  11396.                                       &ret_cred
  11397.                                       );
  11398.         krb5_free_creds(k5_context, get_cred);
  11399.         get_cred = NULL;
  11400.         (void) krb5_cc_close(k5_context, ccache);
  11401.  
  11402.         if (status) 
  11403.             goto bad;
  11404.  
  11405.         /* Reset internal flags; these should not be set. */
  11406.         authopts &= (~OPTS_FORWARD_CREDS);
  11407.         authopts &= (~OPTS_FORWARDABLE_CREDS);
  11408.  
  11409.         if (krb5_auth_con_init(k5_context, &auth_context))
  11410.             goto bad;
  11411.  
  11412.         if (krb5_auth_con_setflags(k5_context, auth_context,
  11413.                                     KRB5_AUTH_CONTEXT_RET_TIME))
  11414.             goto bad;
  11415.  
  11416.         /* Only need local address for mk_cred() to send to krlogind */
  11417.         if (!krb5_d_no_addresses)
  11418.             if (status = krb5_auth_con_genaddrs(k5_context,
  11419.                                                 auth_context,
  11420.                                                  ttyfd,
  11421.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
  11422.                                                  ))
  11423.                 goto bad;
  11424.  
  11425.         /* Here is where we start to handle the new protocol in earnest */
  11426.         if ( krb5_rlog_ver == KCMD_PROTOCOL_COMPAT_HACK ) {
  11427.             krb5_boolean is_des;
  11428.  
  11429.             if (status = krb5_c_enctype_compare( k5_context,
  11430.                                                  ENCTYPE_DES_CBC_CRC,
  11431. #ifdef HEIMDAL
  11432.                                                  ret_cred->session.keytype,
  11433. #else /* HEIMDAL */
  11434.                                                  ret_cred->keyblock.enctype,
  11435. #endif /* HEIMDAL */
  11436.                                                  &is_des)) {
  11437.                 krb5_free_creds(k5_context, ret_cred);
  11438.                 ret_cred = NULL;
  11439.                 goto bad;
  11440.             }
  11441.  
  11442.             if ( is_des ) {
  11443.                 kcmd_version = "KCMDV0.1";
  11444.                 use_proto = KCMD_OLD_PROTOCOL;
  11445.             } else {
  11446.                 authopts = AP_OPTS_USE_SUBKEY;
  11447.                 kcmd_version = "KCMDV0.2";
  11448.                 use_proto = KCMD_NEW_PROTOCOL;
  11449.             }
  11450.         } else {
  11451.             use_proto = krb5_rlog_ver;
  11452.             switch ( krb5_rlog_ver ) {
  11453.             case KCMD_NEW_PROTOCOL:
  11454.                 authopts = AP_OPTS_USE_SUBKEY;
  11455.                 kcmd_version = "KCMDV0.2";
  11456.                 break;
  11457.             case KCMD_OLD_PROTOCOL:
  11458.                 kcmd_version = "KCMDV0.1";
  11459.                 break;
  11460.             default:
  11461.                 goto bad;
  11462.             }
  11463.         }
  11464.  
  11465.         /* call Kerberos library routine to obtain an authenticator,
  11466.            pass it over the socket to the server, and obtain mutual
  11467.            authentication.
  11468.          */
  11469.         status = krb5_sendauth(k5_context,
  11470.                                &auth_context,
  11471.                                (krb5_pointer) &ttyfd,
  11472.                                kcmd_version,
  11473.                                ret_cred->client,
  11474.                                ret_cred->server,
  11475.                                 authopts,
  11476.                                &cksumdat,
  11477.                                ret_cred,
  11478.                                0,
  11479.                                &error,
  11480.                                &rep_ret,
  11481.                                NULL
  11482.                                );
  11483.         krb5_free_data_contents(k5_context,&cksumdat);
  11484.  
  11485.         if (status) {
  11486.             if ( !quiet )
  11487.                 printf("Couldn't authenticate to server: %s\r\n",
  11488.                         error_message(status));
  11489.             if (error) {
  11490.                 if ( !quiet ) {
  11491.                     printf("Server returned error code %d (%s)\r\n",
  11492.                         error->error,
  11493.                         error_message(ERROR_TABLE_BASE_krb5 + error->error));
  11494.                     if (error->text.length) {
  11495.                         printf("Error text sent from server: %s\r\n",
  11496.                                 error->text.data);
  11497.                     }
  11498.                 }
  11499.                 krb5_free_error(k5_context, error);
  11500.                 error = 0;
  11501.             }
  11502.             goto bad;
  11503.         }
  11504.  
  11505.         if (rep_ret) {
  11506.             server_seqno = rep_ret->seq_number;
  11507.             krb5_free_ap_rep_enc_part(k5_context, rep_ret);
  11508.         }
  11509.  
  11510.         (void) ttol(remoteuser, strlen((char *)remoteuser)+1);
  11511.         (void) ttol(term_speed, strlen((char *)term_speed)+1);
  11512.         (void) ttol(localuser, strlen((char *)localuser)+1);
  11513.  
  11514.         if (forward_flag) {   /* Forward credentials (global) */
  11515.             if (status = krb5_fwd_tgt_creds( k5_context,
  11516.                                              auth_context,
  11517.                                              (char *)hostname,
  11518.                                              ret_cred->client,
  11519.                                              ret_cred->server,
  11520.                                              0,
  11521.                                              (forwardable_flag ?
  11522.                                                OPTS_FORWARDABLE_CREDS :
  11523.                                                0),
  11524.                                              &outbuf
  11525.                                              )
  11526.                  )
  11527.             {
  11528.                 printf("Error forwarding credentials: %s\r\n",
  11529.                          error_message(status));
  11530.                 goto bad2;
  11531.             }
  11532.  
  11533.             /* Send forwarded credentials */
  11534.             status = krb5_write_message(k5_context,
  11535.                                          (krb5_pointer)&ttyfd,
  11536.                                          &outbuf
  11537.                                          );
  11538.         }
  11539.         else { /* Dummy write to signal no forwarding */
  11540.           bad2:
  11541.             outbuf.length = 0;
  11542.             status = krb5_write_message(k5_context,
  11543.                                          (krb5_pointer)&ttyfd,
  11544.                                          &outbuf);
  11545.         }       
  11546.  
  11547.         if ((c = ttinc(0)) < 0) {
  11548.             if (c==-1) {
  11549.                 perror((char *)hostname);
  11550.             } else {
  11551.                 printf("ck_krb_rlogin: bad connection with remote host\r\n");
  11552.             }
  11553.             status = -1;
  11554.             goto bad;
  11555.         }
  11556.         if (c != 0) {
  11557.             while ((c = ttinc(1)) >= 0) {
  11558.                 (void) printf("%c",c);
  11559.                 if (c == '\n')
  11560.                     break;
  11561.             }
  11562.             status = -1;
  11563.             goto bad;
  11564.         }
  11565.  
  11566.         if ( status == 0 ) {        /* success */
  11567.             krb5_keyblock * key = 0;
  11568.  
  11569.             if ( use_proto == KCMD_NEW_PROTOCOL ) {
  11570.                 int on = 1;
  11571.                 rlog_inband = 1;
  11572.                 setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,
  11573.                            (char *) &on, sizeof on);
  11574.  
  11575.                 status = krb5_auth_con_getlocalsubkey( k5_context,
  11576.                                                        auth_context,
  11577.                                                        &key);
  11578.                 if ((status || !key) && encrypt_flag )
  11579.                     goto bad2;
  11580.             }
  11581.             if ( key == 0 ) {
  11582. #ifdef HEIMDAL
  11583.                 key = &ret_cred->session;
  11584. #else /* HEIMDAL */
  11585.                 key = &ret_cred->keyblock;
  11586. #endif /* HEIMDAL */
  11587.             }
  11588.  
  11589.             rcmd_stream_init_krb5(key, encrypt_flag, 1, 1, use_proto);
  11590.             if ( encrypt_flag )
  11591.                 rlog_encrypt = 1;
  11592.         }
  11593.         return (0);     /* success */
  11594.  
  11595.       bad:
  11596.         if ( status && !quiet ) {
  11597.             printf("Kerberos authentication error: %s\r\n",
  11598.                     error_message(status));
  11599.         }
  11600.         if (ret_cred) {
  11601.             krb5_free_creds(k5_context, ret_cred);
  11602.             ret_cred = NULL;
  11603.         }
  11604.         return (status);
  11605. #else /* KRB5 */
  11606.         return(-1);
  11607. #endif /* KRB5 */
  11608.     } else if (kversion == 4) {
  11609. #ifdef KRB4
  11610.         char tgt[4*REALM_SZ+1];
  11611.         debug(F100,"ck_krb_rlogin version 4","",0);
  11612.  
  11613.         realm = (char *)krb_realmofhost(hostname);
  11614.         if (!realm) {
  11615.             strcpy(strTmp, "Can't find realm for host \"");
  11616.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11617.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11618.             printf("?Kerberos 4 error: %s\r\n",strTmp);
  11619.             krb4_errno = 0;
  11620.             makestr(&krb4_errmsg,strTmp);
  11621.             return(0);
  11622.         }
  11623.  
  11624.         ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  11625.         status = ck_krb4_tkt_isvalid(tgt);
  11626.  
  11627.         if ( status <= 0 && krb4_autoget )
  11628.             ck_krb4_autoget_TGT(realm);
  11629.  
  11630.         ttoc(0);        /* write a NUL */
  11631.  
  11632.         status = krb_sendauth(encrypt_flag?KOPT_DO_MUTUAL:0,
  11633.                                ttyfd,
  11634.                                &k4_auth,
  11635.                                krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  11636.                                hostname,
  11637.                                realm,
  11638.                                (unsigned long) getpid(),
  11639.                                &k4_msg_data,
  11640.                                &cred,
  11641. #ifdef CK_ENCRYPTION
  11642.                                &k4_sched,
  11643. #else /* ENCRYPTION */
  11644.                                NULL,
  11645. #endif /* ENCRYPTION */
  11646.                                l_addr,
  11647.                                r_addr,
  11648.                                "KCMDV0.1");
  11649.         debug(F111,"ck_krb_rlogin","krb_sendauth",status);
  11650.         if (status != KSUCCESS) {
  11651.             printf( "krb_sendauth failed: %s\r\n",
  11652.                     krb_get_err_text_entry(status)
  11653.                     );
  11654.             return(-1);
  11655.         }
  11656.         ttol(remoteuser,strlen(remoteuser)+1);
  11657.         ttol(term_speed,strlen(term_speed)+1);
  11658.  
  11659.       reread:
  11660.         if ((c = ttinc(0)) < 0) {
  11661.             printf("rcmd: bad connection with remote host\r\n");
  11662.             return(-1);
  11663.         }
  11664.         debug(F111,"ck_krb_rlogin","first byte",c);
  11665.  
  11666.         if (c != 0) {
  11667.             char *check = "ld.so: warning:";
  11668.             /* If rlogind was compiled on SunOS4, and it somehow
  11669.             got the shared library version numbers wrong, it
  11670.             may give an ld.so warning about an old version of a
  11671.             shared library.  Just ignore any such warning.
  11672.             Note that the warning is a characteristic of the
  11673.             server; we may not ourselves be running under
  11674.             SunOS4.  */
  11675.             if (c == 'l') {
  11676.                 char *p;
  11677.                 char cc;
  11678.  
  11679.                 p = &check[1];
  11680.                 while ((c = ttinc(0)) >= 0) {
  11681.                     if (*p == '\0') {
  11682.                         if (c == '\n')
  11683.                             break;
  11684.                     } else {
  11685.                         if (c != *p)
  11686.                             break;
  11687.                         ++p;
  11688.                     }
  11689.                 }
  11690.  
  11691.                 if (*p == '\0')
  11692.                     goto reread;
  11693.             }
  11694.  
  11695.             printf(check);
  11696.             while ((c = ttinc(1)) >= 0) {
  11697.                 printf("%c",c);
  11698.                 if (c == '\n')
  11699.                     break;
  11700.             }
  11701.             debug(F110,"ck_krb_rlogin","fatal error 1",0);
  11702.             return(-1);
  11703.         }
  11704.  
  11705. #ifdef CK_ENCRYPTION
  11706.         if ( encrypt_flag ) {
  11707.             /* if we are encrypting we need to setup the encryption */
  11708.             /* routines.                                            */
  11709.             des_key_sched(cred.session, k4_sched);
  11710.             rlog_encrypt = 1;
  11711.         }
  11712. #endif /* ENCRYPTION */
  11713. #else /* KRB4 */
  11714.         return(-1);
  11715. #endif /* KRB4 */
  11716.     }
  11717.     return(0); /* success */
  11718. }
  11719.  
  11720. #define SRAND   srand
  11721. #define RAND    rand
  11722. #define RAND_TYPE       int
  11723.  
  11724. static long
  11725. random_confounder(size, fillin)
  11726. size_t size;
  11727. char * fillin;
  11728. {
  11729.     static int seeded = 0;
  11730.     register unsigned char *real_fill;
  11731.     RAND_TYPE   rval;
  11732.  
  11733.     if (!seeded) {
  11734.         /* time() defined in 4.12.2.4, but returns a time_t, which is an
  11735.            "arithmetic type" (4.12.1) */
  11736.         rval = (RAND_TYPE) time(0);
  11737.         SRAND(rval);
  11738.         rval = RAND();
  11739.         rval ^= getpid();
  11740.         SRAND(rval);
  11741.         seeded = 1;
  11742.     }
  11743.  
  11744.     real_fill = (unsigned char *)fillin;
  11745.     while (size > 0) {
  11746.         rval = RAND();
  11747.         *real_fill = rval & 0xff;
  11748.         real_fill++;
  11749.         size--;
  11750.         if (size) {
  11751.             *real_fill = (rval >> 8) & 0xff;
  11752.             real_fill++;
  11753.             size--;
  11754.         }
  11755.     }
  11756.     return 0;
  11757. }
  11758.  
  11759. #ifdef KRB5
  11760. int
  11761. krb5_des_avail(fd)
  11762.     int fd;
  11763. {
  11764.     return(nstored);
  11765. }
  11766.  
  11767. int
  11768. krb5_des_read(fd, buf, len, secondary)
  11769.     int fd;
  11770.     register char *buf;
  11771.     int len;
  11772.     int secondary;
  11773. {
  11774.     int nreturned = 0;
  11775.     long net_len,rd_len;
  11776.     int cc;
  11777.     krb5_error_code status;
  11778.     unsigned char c;
  11779.     krb5_data plain;
  11780.     krb5_enc_data cipher;
  11781.  
  11782.     debug(F111,"krb5_des_read","len",len);
  11783.     debug(F111,"krb5_des_read","rlog_encrypt",rlog_encrypt);
  11784.     if ( !rlog_encrypt ) {
  11785.         cc = net_read(fd, buf, len);
  11786.         debug(F111,"krb5_des_read","chars read",cc);
  11787.         if ( cc < 0 )
  11788.             netclos();
  11789.         return(cc);
  11790.     }
  11791.  
  11792.     if (nstored >= len) {
  11793.         if ( buf ) {
  11794.             memcpy(buf, store_ptr, len);        /* safe */
  11795.             store_ptr += len;
  11796.             nstored -= len;
  11797.             return(len);
  11798.         } else
  11799.             return(0);
  11800.     } else if (nstored) {
  11801.         if ( buf ) {
  11802.             memcpy(buf, store_ptr, nstored);    /* safe */
  11803.             nreturned += nstored;
  11804.             buf += nstored;
  11805.             len -= nstored;
  11806.             nstored = 0;
  11807.         }
  11808.         else
  11809.             return(0);
  11810.     }
  11811.  
  11812.     /* See the comment in v4_des_read. */
  11813.     while (1) {
  11814.         cc = net_read(fd, &c, 1);
  11815.         /* we should check for non-blocking here, but we'd have
  11816.         to make it save partial reads as well. */
  11817.         if (cc <= 0) {
  11818.             return cc; /* read error */
  11819.         }
  11820.         if (cc == 1) {
  11821.             if (c == 0 || !do_lencheck) 
  11822.                 break;
  11823.         }
  11824.     }
  11825.  
  11826.     rd_len = c;
  11827.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11828.     rd_len = (rd_len << 8) | c;
  11829.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11830.     rd_len = (rd_len << 8) | c;
  11831.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11832.     rd_len = (rd_len << 8) | c;
  11833.  
  11834.     if (status = krb5_c_encrypt_length(k5_context, 
  11835.                                     k5_session_key->enctype,
  11836.                                     use_ivecs ? rd_len + 4 : rd_len,
  11837.                     (size_t *)&net_len)) {
  11838.         errno = status;
  11839.         return(-1);
  11840.     }
  11841.  
  11842.     if ((net_len <= 0) || (net_len > sizeof(des_inbuf))) {
  11843.         /* preposterous length; assume out-of-sync; only
  11844.            recourse is to close connection, so return 0 */
  11845.         printf("Read size problem.\r\n");
  11846.         return(0);
  11847.     }
  11848.     if ((cc = net_read(fd, desinbuf.data, net_len)) != net_len )
  11849.     {
  11850.         /* pipe must have closed, return 0 */
  11851.         printf( "Read error: length received %d != expected %d.\r\n",
  11852.                 cc,
  11853.                 net_len
  11854.                 );
  11855.         return(cc);
  11856.     }
  11857.  
  11858.  
  11859.     /* decrypt info */
  11860.     cipher.enctype = ENCTYPE_UNKNOWN;
  11861.     cipher.ciphertext.length = net_len;
  11862.     cipher.ciphertext.data = desinbuf.data;
  11863.     plain.length = sizeof(storage);
  11864.     plain.data = storage;
  11865.  
  11866.     if ( status = krb5_c_decrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11867.                                  use_ivecs ? encivec_i + secondary : 0,
  11868.                                   &cipher,&plain) ) {
  11869.         /* probably out of sync */
  11870.         printf("Cannot decrypt data from network: %s\r\n",
  11871.                  error_message(status));
  11872.         errno = EIO;
  11873.         return(-1);
  11874.     }
  11875.     
  11876.     store_ptr = storage;
  11877.     nstored = rd_len;
  11878.  
  11879.     if ( use_ivecs ) {
  11880.         int rd_len2;
  11881.         rd_len2 = storage[0] & 0xff;
  11882.         rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
  11883.         rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
  11884.         rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
  11885.         if (rd_len2 != rd_len) {
  11886.             /* cleartext length trashed? */
  11887.             errno = EIO;
  11888.             return -1;
  11889.         }
  11890.         store_ptr += 4;
  11891.     }
  11892.  
  11893.     if ( !buf )
  11894.         return(0);
  11895.  
  11896. #ifdef RLOGCODE                         /* blah */
  11897.     if (rlog_inband && (ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN))
  11898.     {
  11899.         int i, left, n;
  11900.  
  11901.         for (i = 0; i < nstored; i++) {
  11902.             if (store_ptr[i] == '\377' &&
  11903.                 store_ptr[i+1] == '\377') {
  11904.                 left = nstored - i;
  11905.                 n = rlog_ctrl(&store_ptr[i], left);
  11906.                 if (n < 0) {
  11907.                     left -= (-n);
  11908.                     nstored = left;
  11909.                     /* flush before, and (-n) bytes */
  11910.                     if (left > 0)
  11911.                         memmove(store_ptr, &store_ptr[i-n], left);
  11912.                 } else if (n) {
  11913.                     left -= n;
  11914.                     nstored -= n;
  11915.                     if (left > 0)
  11916.                         memmove(store_ptr, &store_ptr[n], left);
  11917.                 }
  11918.             }
  11919.         }
  11920.     }
  11921. #endif /* RLOGCODE */
  11922.  
  11923.     if (nstored > len) {
  11924.         memcpy(buf, store_ptr, len);            /* safe */
  11925.         nreturned += len;
  11926.         store_ptr += len;
  11927.         nstored -= len;
  11928.     } else {
  11929.         memcpy(buf, store_ptr, nstored);        /* safe */
  11930.         nreturned += nstored;
  11931.         nstored = 0;
  11932.     }
  11933.     return(nreturned);
  11934. }
  11935.  
  11936. int
  11937. krb5_des_write(fd, buf, len, secondary)
  11938.     int fd;
  11939.     char *buf;
  11940.     int len;
  11941.     int secondary;
  11942. {
  11943.     char tmpbuf[2*RLOG_BUFSIZ+8];
  11944.     unsigned char *len_buf = (unsigned char *) tmpbuf;
  11945.     krb5_error_code status;
  11946.     krb5_data plain;
  11947.     krb5_enc_data cipher;
  11948.  
  11949.     debug(F111,"krb5_des_write","rlog_encrypt",rlog_encrypt);
  11950.     if ( !rlog_encrypt ) {
  11951.         int cc = net_write(fd, buf, len);
  11952.         debug(F111,"net_write","chars written",cc);
  11953.         return(cc != len ? -1 : len);
  11954.     }
  11955.  
  11956.     if (use_ivecs) {
  11957.         unsigned char *lenbuf2 = (unsigned char *) tmpbuf;
  11958.         if (len + 4 > sizeof(tmpbuf))
  11959.             abort ();
  11960.         lenbuf2[0] = (len & 0xff000000) >> 24;
  11961.         lenbuf2[1] = (len & 0xff0000) >> 16;
  11962.         lenbuf2[2] = (len & 0xff00) >> 8;
  11963.         lenbuf2[3] = (len & 0xff);
  11964.         memcpy (tmpbuf + 4, buf, len);
  11965.  
  11966.         plain.data = tmpbuf;
  11967.         plain.length = len + 4;
  11968.     } else {
  11969.         plain.data = buf;
  11970.         plain.length = len;
  11971.     }
  11972.  
  11973.     cipher.ciphertext.length = sizeof(des_outpkt)-4;
  11974.     cipher.ciphertext.data = desoutbuf.data;
  11975.  
  11976.     if ( status = krb5_c_encrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11977.                          use_ivecs ? encivec_o + secondary : 0,
  11978.                          &plain, &cipher)) {
  11979.         printf("Write encrypt problem: %s.\r\n",
  11980.                  error_message(status));
  11981.         errno = EIO;
  11982.         return(-1);
  11983.     }
  11984.     desoutbuf.length = cipher.ciphertext.length;
  11985.  
  11986.     len_buf = (unsigned char *) des_outpkt;
  11987.     len_buf[0] = (len & 0xff000000) >> 24;
  11988.     len_buf[1] = (len & 0xff0000) >> 16;
  11989.     len_buf[2] = (len & 0xff00) >> 8;
  11990.     len_buf[3] = (len & 0xff);
  11991.  
  11992.     if (net_write(fd, des_outpkt,desoutbuf.length+4)
  11993.          != desoutbuf.length+4){
  11994.         printf("Could not write out all data\r\n");
  11995.         return(-1);
  11996.     }
  11997.     else return(len);
  11998. }
  11999. #endif /* KRB5 */
  12000.  
  12001. #ifdef KRB4
  12002. /*
  12003.  * Note that the encrypted rlogin packets take the form of a four-byte
  12004.  * length followed by encrypted data.  On writing the data out, a significant
  12005.  * performance penalty is suffered (at least one RTT per character, two if we
  12006.  * are waiting for a shell to echo) by writing the data separately from the
  12007.  * length.  So, unlike the input buffer, which just contains the output
  12008.  * data, the output buffer represents the entire packet.
  12009.  */
  12010.  
  12011. int
  12012. krb4_des_avail(fd)
  12013.     int fd;
  12014. {
  12015.     return(nstored);
  12016. }
  12017.  
  12018. int
  12019. krb4_des_read(fd, buf, len)
  12020. int fd;
  12021. register char *buf;
  12022. int len;
  12023. {
  12024.     int nreturned = 0;
  12025.     unsigned long net_len, rd_len;
  12026.     int cc;
  12027.     unsigned char c;
  12028.     int gotzero = 0;
  12029.  
  12030.     debug(F111,"krb4_des_read","rlog_encrypt",rlog_encrypt);
  12031.     debug(F111,"krb4_des_read","len",len);
  12032.     if ( !rlog_encrypt ) {
  12033.         cc = net_read(fd, buf, len);
  12034.         debug(F111,"krb4_des_read","chars read",cc);
  12035.         if ( cc < 0 )
  12036.             netclos();
  12037.         return(cc);
  12038.     }
  12039.  
  12040.     if (nstored >= len) {
  12041.         if ( buf ) {
  12042.             debug(F111,"krb4_des_read (nstored >= len)","nstored",nstored);
  12043.             memcpy(buf, store_ptr, len);        /* safe */
  12044.             store_ptr += len;
  12045.             nstored -= len;
  12046.             return(len);
  12047.         } else
  12048.             return(0);
  12049.     } else if (nstored) {
  12050.         if ( buf ) {
  12051.             debug(F111,"krb4_des_read (nstored)","nstored",nstored);
  12052.             memcpy(buf, store_ptr, nstored);    /* safe */
  12053.             nreturned += nstored;
  12054.             buf += nstored;
  12055.             len -= nstored;
  12056.             nstored = 0;
  12057.         } else
  12058.             return(0);
  12059.     }
  12060.  
  12061.     /* We're fetching the length which is MSB first, and the MSB
  12062.     has to be zero unless the client is sending more than 2^24
  12063.     (16M) bytes in a single write (which is why this code is in
  12064.     rlogin but not rcp or rsh.) The only reasons we'd get something
  12065.     other than zero are:
  12066.     -- corruption of the tcp stream (which will show up when
  12067.     everything else is out of sync too)
  12068.     -- un-caught Berkeley-style "pseudo out-of-band data" which
  12069.     happens any time the user hits ^C twice.
  12070.     The latter is *very* common, as shown by an 'rlogin -x -d'
  12071.     using the CNS V4 rlogin.         Mark EIchin 1/95
  12072.     */
  12073.     debug(F110,"krb4_des_read",
  12074.           "about to call net_read() this will block",
  12075.           0
  12076.           );
  12077.     do {
  12078.         cc = net_read(fd, &c, 1);
  12079.         debug(F111,"net_read","chars read",cc);
  12080.         if (cc <= 0) {
  12081.             netclos();
  12082.             return(-1);
  12083.         }
  12084.         if (cc != 1) return 0; /* read error */
  12085.         if (cc == 1) {
  12086.             if (c == 0) gotzero = 1;
  12087.         }
  12088.     } while (!gotzero);
  12089.  
  12090.     debug(F110,"krb4_des_read","gotzero",0);
  12091.     cc = net_read(fd, &c, 1);
  12092.     debug(F111,"net_read","chars read",cc);
  12093.     if (cc < 0) {
  12094.         netclos();
  12095.         return(-1);
  12096.     } else if ( cc != 1 )
  12097.         return(0);
  12098.     net_len = c;
  12099.     cc = net_read(fd, &c, 1);
  12100.     debug(F111,"net_read","chars read",cc);
  12101.     if (cc < 0) {
  12102.         netclos();
  12103.         return(-1);
  12104.     } else if ( cc != 1 )
  12105.         return(0);
  12106.     net_len = (net_len << 8) | c;
  12107.     debug(F111,"net_read","chars read",cc);
  12108.     cc = net_read(fd, &c, 1);
  12109.     if (cc < 0) {
  12110.         netclos();
  12111.         return(-1);
  12112.     } else if ( cc != 1 )
  12113.         return(0);
  12114.     net_len = (net_len << 8) | c;
  12115.     debug(F111,"krb4_des_read","net_len",net_len);
  12116.  
  12117.     /* Note: net_len is unsigned */
  12118.     if (net_len > sizeof(des_inbuf)) {
  12119.         /* XXX preposterous length, probably out of sync.
  12120.         act as if pipe closed */
  12121.         return(0);
  12122.     }
  12123.     /* the writer tells us how much real data we are getting, but
  12124.     we need to read the pad bytes (8-byte boundary) */
  12125. #ifndef roundup
  12126. #define roundup(x,y) ((((x)+(y)-1)/(y))*(y))
  12127. #endif /* roundup */
  12128.     rd_len = roundup(net_len, 8);
  12129.     debug(F111,"krb4_des_read","rd_len",rd_len);
  12130.     cc = net_read(fd, des_inbuf, rd_len);
  12131.     debug(F111,"net_read","chars read",cc);
  12132.     if (cc < 0) {
  12133.         netclos();
  12134.         return(-1);
  12135.     } else if ( cc != rd_len )
  12136.         return(0);
  12137.  
  12138.     hexdump("krb4_des_read des_inbuf",des_inbuf,8);
  12139. #ifdef CK_ENCRYPTION
  12140. #ifdef KRB524
  12141.     (void) des_pcbc_encrypt(des_inbuf,
  12142.                              storage,
  12143.                              (net_len < 8) ? 8 : net_len,
  12144.                              k4_sched,
  12145.                              cred.session,
  12146.                              DECRYPT);
  12147. #else /* KRB524 */
  12148.     (void) des_pcbc_encrypt((Block *)des_inbuf,
  12149.                              (Block *)storage,
  12150.                              (net_len < 8) ? 8 : net_len,
  12151.                              k4_sched,
  12152.                              &cred.session,
  12153.                              DECRYPT);
  12154. #endif /* KRB524 */
  12155. #endif /* ENCRYPTION */
  12156.     hexdump("krb4_des_read storage",storage,8);
  12157.  
  12158.     /*
  12159.     * when the cleartext block is < 8 bytes, it is "right-justified"
  12160.     * in the block, so we need to adjust the pointer to the data
  12161.     */
  12162.     if (net_len < 8)
  12163.         store_ptr = storage + 8 - net_len;
  12164.     else
  12165.         store_ptr = storage;
  12166.     nstored = net_len;
  12167.  
  12168.     if ( !buf )
  12169.         return(0);
  12170.  
  12171.     if (nstored > len) {
  12172.         memcpy(buf, store_ptr, len);            /* safe */
  12173.         nreturned += len;
  12174.         store_ptr += len;
  12175.         nstored -= len;
  12176.     } else {
  12177.         memcpy(buf, store_ptr, nstored);        /* safe */
  12178.         nreturned += nstored;
  12179.         nstored = 0;
  12180.     }
  12181.  
  12182.     debug(F111,"net_read","nreturned",nreturned);
  12183.     return(nreturned);
  12184. }
  12185.  
  12186. int
  12187. krb4_des_write(fd, buf, len)
  12188. int fd;
  12189. char *buf;
  12190. int len;
  12191. {
  12192.     static char garbage_buf[8];
  12193.     unsigned char *len_buf = (unsigned char *) des_outpkt;
  12194.     int cc;
  12195.  
  12196.     debug(F111,"krb4_des_write","rlog_encrypt",rlog_encrypt);
  12197.     if ( !rlog_encrypt ) {
  12198.         cc = net_write(fd, buf, len);
  12199.         debug(F111,"net_write","chars written",cc);
  12200.         return(cc);
  12201.     }
  12202.  
  12203.     /*
  12204.     * pcbc_encrypt outputs in 8-byte (64 bit) increments
  12205.     *
  12206.     * it zero-fills the cleartext to 8-byte padding,
  12207.     * so if we have cleartext of < 8 bytes, we want
  12208.     * to insert random garbage before it so that the ciphertext
  12209.     * differs for each transmission of the same cleartext.
  12210.     * if len < 8 - sizeof(long), sizeof(long) bytes of random
  12211.     * garbage should be sufficient; leave the rest as-is in the buffer.
  12212.     * if len > 8 - sizeof(long), just garbage fill the rest.
  12213.     */
  12214.     if (len < 8) {
  12215.         random_confounder(8 - len, garbage_buf);
  12216.         /* this "right-justifies" the data in the buffer */
  12217.         (void) memcpy(garbage_buf + 8 - len, buf, len); /* safe */
  12218.         hexdump("krb4_des_write garbage_buf",garbage_buf,8);
  12219.     } else
  12220.         hexdump("krb4_des_write buf",buf,8);
  12221. #ifdef CK_ENCRYPTION
  12222. #ifdef KRB524
  12223.     (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
  12224.                              des_outpkt+4,
  12225.                              (len < 8) ? 8 : len,
  12226.                              k4_sched,
  12227.                              cred.session,
  12228.                              ENCRYPT);
  12229. #else /* KRB524 */
  12230.     (void) des_pcbc_encrypt((Block *)((len < 8) ? garbage_buf : buf),
  12231.                              (Block *)(des_outpkt+4),
  12232.                              (len < 8) ? 8 : len,
  12233.                              k4_sched,
  12234.                              &cred.session,
  12235.                              ENCRYPT);
  12236. #endif /* KRB524 */
  12237. #endif /* ENCRYPTION */
  12238.     if ( len < 8 )
  12239.         hexdump("krb4_des_write (post pcbc) garbage_buf",garbage_buf,8);
  12240.     else
  12241.         hexdump("krb4_des_write (post pcbc) buf",buf,8);
  12242.     hexdump("krb4_des_write (des_outpkt+4)",(des_outpkt+4),8);
  12243.  
  12244.     /* tell the other end the real amount, but send an 8-byte padded
  12245.     packet */
  12246.     len_buf[0] = (len & 0xff000000) >> 24;
  12247.     len_buf[1] = (len & 0xff0000) >> 16;
  12248.     len_buf[2] = (len & 0xff00) >> 8;
  12249.     len_buf[3] = (len & 0xff);
  12250.     hexdump("krb4_des_write des_outpkt len",des_outpkt,12);
  12251.     cc = net_write(fd, des_outpkt, roundup(len,8)+4);
  12252.     debug(F111,"net_write","chars written",cc);
  12253.     return(len);
  12254. }
  12255. #endif /* KRB4 */
  12256. #endif /* RLOGCODE */
  12257.  
  12258. #ifdef KRB524
  12259. #ifndef OS2
  12260. /* The following functions are missing from the compatibility library */
  12261. const char *
  12262. krb_get_err_text_entry(r) int r;
  12263. {
  12264.     extern char krb_err_text[];
  12265.     return(krb_err_txt[r]);
  12266. }
  12267. #endif /* OS2 */
  12268. #endif /* KRB524 */
  12269. #endif /* CK_KERBEROS */
  12270.  
  12271. #ifdef CK_KERBEROS
  12272. #ifdef KRB5_U2U
  12273. /* Kerberos 5 User to User Client */
  12274. int
  12275. k5_user_to_user_client_auth()
  12276. {
  12277.     extern int ttyfd;
  12278.     register int retval, i;
  12279.     char **srealms;             /* realm(s) of server */
  12280.     char *princ;                /* principal in credentials cache */
  12281.     krb5_ccache cc;
  12282.     krb5_creds creds, *new_creds;
  12283.     krb5_data reply, msg, msgtext, princ_data;
  12284.     krb5_ticket * ticket = NULL;
  12285.  
  12286.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12287.     {
  12288.         com_err("uu-client", retval, "getting credentials cache");
  12289.         return(-1);
  12290.     }
  12291.  
  12292.     memset ((char*)&creds, 0, sizeof(creds));
  12293.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12294.     {
  12295.         com_err("uu-client", retval, "getting principal name");
  12296.         return(-1);
  12297.     }
  12298.  
  12299.     if (retval = krb5_get_host_realm(k5_context, szHostName, &srealms))
  12300.     {
  12301.         com_err("uu-client", retval, "getting realms for \"%s\"", szHostName);
  12302.         return(-1);
  12303.     }
  12304.  
  12305.     if (retval = krb5_build_principal_ext(k5_context, &creds.server,
  12306.                                           krb5_princ_realm(k5_context,
  12307.                                                          creds.client)->length,
  12308.                                           krb5_princ_realm(k5_context,
  12309.                                                          creds.client)->data,
  12310.                                           6, "krbtgt",
  12311.                                           krb5_princ_realm(k5_context,
  12312.                                                          creds.client)->length,
  12313.                                           krb5_princ_realm(k5_context,
  12314.                                                          creds.client)->data,
  12315.                                           0))
  12316.     {
  12317.         com_err("uu-client", retval, "setting up tgt server name");
  12318.         return(-1);
  12319.     }
  12320.  
  12321.     /* Get TGT from credentials cache */
  12322.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_CACHED, cc,
  12323.                                        &creds, &new_creds))
  12324.     {
  12325.         com_err("uu-client", retval, "getting TGT");
  12326.         return(-1);
  12327.     }
  12328.  
  12329.     if (retval = krb5_unparse_name(k5_context, creds.client, &princ)) {
  12330.         com_err("uu-client", retval, "printing principal name");
  12331.         return(-1);
  12332.     }
  12333.     i = strlen(princ) + 1;
  12334.     princ_data.data = princ;
  12335.     princ_data.length = i;              /* include null terminator for
  12336.                                            server's convenience */
  12337.     retval = krb5_write_message(k5_context,
  12338.                                 (krb5_pointer) &ttyfd, &princ_data);
  12339.     if (retval)
  12340.     {
  12341.         com_err("uu-client", retval, "sending principal name to server");
  12342.         return(-1);
  12343.     }
  12344.     krb5_free_unparsed_name(k5_context,princ);
  12345.  
  12346.     retval = krb5_write_message(k5_context,
  12347.                                 (krb5_pointer) &ttyfd, &new_creds->ticket);
  12348.     if (retval)
  12349.     {
  12350.         com_err("uu-client", retval, "sending ticket to server");
  12351.         return(-1);
  12352.     }
  12353.  
  12354.     retval = krb5_read_message(k5_context, (krb5_pointer) &ttyfd, &reply);
  12355.     if (retval)
  12356.     {
  12357.         com_err("uu-client", retval, "reading reply from server");
  12358.         return(-1);
  12359.     }
  12360.  
  12361.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12362.         com_err("uu-client", retval, "initializing the auth_context");
  12363.         return(-1);
  12364.     }
  12365.  
  12366.     if (!krb5_d_no_addresses) {
  12367.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12368.                         KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12369.                         KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12370.         com_err("uu-client", retval, "generating addrs for auth_context");
  12371.         return(-1);
  12372.       }
  12373.     }
  12374.  
  12375.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12376.                                         KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12377.         com_err("uu-client", retval, "initializing the auth_context flags");
  12378.         return(-1);
  12379.     }
  12380.  
  12381.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12382.                                               &new_creds->keyblock)) {
  12383.         com_err("uu-client", retval, "setting useruserkey for authcontext");
  12384.         return(-1);
  12385.     }
  12386.  
  12387.     /* read the ap_req to get the session key */
  12388.     retval = krb5_rd_req(k5_context, &auth_context, &reply,
  12389.                           NULL, NULL, NULL, &ticket);
  12390.     if (retval) {
  12391.         com_err("uu-client", retval, "reading AP_REQ from server");
  12392.         return(-1);
  12393.     }
  12394.  
  12395.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12396.         return(-1);
  12397.  
  12398.     if ( strcmp("Kermit implements Kerberos 5 User to User",msg.data) )
  12399.         return(-1);
  12400.     krb5_free_data_contents(k5_context,&msg);
  12401.  
  12402.     msgtext.data = "As do I! :-)";
  12403.     msgtext.length = strlen(msgtext.data)+1;
  12404.  
  12405.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12406.         return(-1);
  12407.  
  12408.     if (retval = krb5_unparse_name(k5_context,
  12409. #ifdef HEIMDAL
  12410.                                     ticket->client,
  12411. #else /* HEIMDAL */
  12412.                                     ticket->enc_part2->client,
  12413. #endif /* HEIMDAL */
  12414.                                     &princ))
  12415.         com_err("uu-client", retval, "while unparsing client name");
  12416.     else {
  12417.         ckstrncpy(szUserNameAuthenticated,princ,UIDBUFLEN);
  12418.         validUser = AUTH_VALID;
  12419.         authentication_version = AUTHTYPE_KERBEROS_V5;
  12420.         if ( !quiet )
  12421.             printf("Peer name is \"%s\"\n", princ);
  12422.         krb5_free_unparsed_name(k5_context,princ);
  12423.     }
  12424.     return 0;
  12425. }
  12426.  
  12427. /* Kerberos 5 User to User Server */
  12428.  
  12429. int
  12430. k5_user_to_user_server_auth()
  12431. {
  12432.     krb5_data pname_data, tkt_data;
  12433.     int retval;
  12434.     krb5_creds creds, *new_creds;
  12435.     krb5_ccache cc;
  12436.     krb5_data msg, msgtext;
  12437.     extern int ttyfd;
  12438.  
  12439.     if (retval = krb5_read_message(k5_context,
  12440.                                    (krb5_pointer) &ttyfd, &pname_data)) {
  12441.         com_err ("uu-server", retval, "reading pname");
  12442.         return(-1);
  12443.     }
  12444.     /* client sends it already null-terminated. */
  12445.     if ( !quiet )
  12446.         printf ("Peer name is \"%s\".\n", pname_data.data);
  12447.     ckstrncpy(szUserNameAuthenticated,pname_data.data,UIDBUFLEN);
  12448.     validUser = AUTH_VALID;
  12449.     authentication_version = AUTHTYPE_KERBEROS_V5;
  12450.  
  12451.     if (retval = krb5_read_message(k5_context,
  12452.                                    (krb5_pointer) &ttyfd, &tkt_data)) {
  12453.         com_err ("uu-server", retval, "reading ticket data");
  12454.         return(-1);
  12455.     }
  12456.  
  12457.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12458.     {
  12459.         com_err("uu-server", retval, "getting credentials cache");
  12460.         return(-1);
  12461.     }
  12462.  
  12463.     memset ((char*)&creds, 0, sizeof(creds));
  12464.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12465.     {
  12466.         com_err("uu-server", retval, "getting principal name");
  12467.         return(-1);
  12468.     }
  12469.  
  12470.     if (retval = krb5_parse_name(k5_context, pname_data.data, &creds.server))
  12471.     {
  12472.         com_err("uu-server", retval, "parsing client name");
  12473.         return(-1);
  12474.     }
  12475.     creds.second_ticket = tkt_data;
  12476.  
  12477.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_USER_USER,
  12478.                                        cc, &creds, &new_creds))
  12479.     {
  12480.         com_err("uu-server", retval, "getting user-user ticket");
  12481.         return(-1);
  12482.     }
  12483.  
  12484.     /* send a ticket/authenticator to the other side, so it can get the key
  12485.        we're using for the krb_safe below. */
  12486.  
  12487.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12488.         com_err("uu-server", retval, "making auth_context");
  12489.         return(-1);
  12490.     }
  12491.  
  12492.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12493.                                          KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12494.         com_err("uu-server", retval, "initializing the auth_context flags");
  12495.         return(-1);
  12496.     }
  12497.  
  12498.     if (!krb5_d_no_addresses) {
  12499.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12500.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12501.                                 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12502.         com_err("uu-server", retval, "generating addrs for auth_context");
  12503.         return(-1);
  12504.       }
  12505.     }
  12506.  
  12507.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12508.                                               &new_creds->keyblock)) {
  12509.         com_err("uu-server", retval, "setting useruserkey for authcontext");
  12510.         return(-1);
  12511.     }
  12512.  
  12513.     if (retval = krb5_mk_req_extended(k5_context, &auth_context,
  12514.                                       AP_OPTS_USE_SESSION_KEY |
  12515.                                       AP_OPTS_MUTUAL_REQUIRED,
  12516.                                       NULL, new_creds, &msg)) {
  12517.         com_err("uu-server", retval, "making AP_REQ");
  12518.         return(-1);
  12519.     }
  12520.     retval = krb5_write_message(k5_context, (krb5_pointer) &ttyfd, &msg);
  12521.     if (retval) {
  12522.         com_err("uu-server", retval, "writing message to client");
  12523.         return(-1);
  12524.     }
  12525.     krb5_free_data_contents(k5_context,&msg);
  12526.  
  12527.     msgtext.data = "Kermit implements Kerberos 5 User to User";
  12528.     msgtext.length = strlen(msgtext.data)+1;
  12529.  
  12530.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12531.         return(-1);
  12532.  
  12533.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12534.         return(-1);
  12535.  
  12536.     if ( strcmp("As do I! :-)",msg.data) )
  12537.         return(-1);
  12538.     krb5_free_data_contents(k5_context,&msg);
  12539.  
  12540.  
  12541.     return(0);
  12542. }
  12543.  
  12544. int
  12545. k5_u2u_read_msg(krb5_context context, int fd, krb5_data * msg)
  12546. {
  12547.     int retval;
  12548.     krb5_data reply;
  12549.  
  12550.     retval = krb5_read_message(context, (krb5_pointer) &fd, &reply);
  12551.     if (retval)
  12552.     {
  12553.         com_err("uu-client", retval, "reading reply");
  12554.         return(-1);
  12555.     }
  12556.  
  12557.     if (retval = krb5_rd_priv(context, auth_context, &reply, msg, NULL)) {
  12558.         com_err("uu-client", retval, "decoding reply");
  12559.         return(-1);
  12560.     }
  12561.     return(0);
  12562. }
  12563.  
  12564. int
  12565. k5_u2u_write_msg(krb5_context context, int fd, krb5_data * msgtext)
  12566. {
  12567.     int retval;
  12568.     krb5_data msg;
  12569.  
  12570.     if (retval = krb5_mk_priv(k5_context, auth_context, msgtext, &msg, NULL))
  12571.     {
  12572.         com_err("uu-server", retval, "encoding message");
  12573.         return(-1);
  12574.     }
  12575.  
  12576.     retval = krb5_write_message(k5_context, (krb5_pointer) &fd, &msg);
  12577.     krb5_free_data_contents(k5_context,&msg);
  12578.     if (retval)
  12579.     {
  12580.         com_err("uu-server", retval, "writing message");
  12581.         return(-1);
  12582.     }
  12583.     return(0);
  12584. }
  12585.  
  12586. int
  12587. krb5_u2u_avail(fd)
  12588.     int fd;
  12589. {
  12590.     return(nstored);
  12591. }
  12592.  
  12593. int
  12594. krb5_u2u_read(fd, buf, len)
  12595.      int fd;
  12596.      register char *buf;
  12597.      int len;
  12598. {
  12599.     int nreturned = 0;
  12600.     krb5_data msg;
  12601.  
  12602.     debug(F111,"krb5_u2u_read","len",len);
  12603.  
  12604.     if ( !buf )
  12605.         return(0);
  12606.  
  12607.     if (nstored >= len) {
  12608.         memcpy(buf, store_ptr, len);        /* safe */
  12609.         store_ptr += len;
  12610.         nstored -= len;
  12611.         return(len);
  12612.     } else if (nstored) {
  12613.         memcpy(buf, store_ptr, nstored);    /* safe */
  12614.         nreturned += nstored;
  12615.         buf += nstored;
  12616.         len -= nstored;
  12617.         nstored = 0;
  12618.     }
  12619.  
  12620.     if (k5_u2u_read_msg(k5_context, fd, &msg) < 0)
  12621.         return(-1);
  12622.  
  12623.     if ( msg.length <= len ) {
  12624.         memcpy(buf, msg.data, msg.length);
  12625.         nreturned += msg.length;
  12626.         nstored = 0;
  12627.     } else {
  12628.         memcpy(buf, msg.data, len);
  12629.         nreturned += len;
  12630.  
  12631.         if ( msg.length - len < sizeof(storage) ) {
  12632.             store_ptr = storage;
  12633.             nstored = msg.length - len;
  12634.             memcpy(storage,msg.data+len,nstored);
  12635.         } else {
  12636.             nstored = 0;
  12637.             return(-1);
  12638.         }
  12639.     }
  12640.     return(nreturned);
  12641. }
  12642.  
  12643. int
  12644. krb5_u2u_write(fd, buf, len)
  12645.      int fd;
  12646.      char *buf;
  12647.      int len;
  12648. {
  12649.     krb5_data msg;
  12650.  
  12651.     msg.length = len;
  12652.     msg.data = buf;
  12653.  
  12654.     if ( k5_u2u_write_msg(k5_context, fd, &msg) < 0 )
  12655.         return(-1);
  12656.     else
  12657.         return(len);
  12658. }
  12659.  
  12660. #endif /* KRB5_U2U */
  12661. #endif /* CK_KERBEROS */
  12662.  
  12663. #ifdef CK_FORWARD_X
  12664. /*
  12665.  
  12666. Copyright (c) 1988  X Consortium
  12667.  
  12668. Permission is hereby granted, free of charge, to any person obtaining a copy
  12669. of this software and associated documentation files (the "Software"), to deal
  12670. in the Software without restriction, including without limitation the rights
  12671. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12672. copies of the Software, and to permit persons to whom the Software is
  12673. furnished to do so, subject to the following conditions:
  12674.  
  12675. The above copyright notice and this permission notice shall be included in
  12676. all copies or substantial portions of the Software.
  12677.  
  12678. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12679. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12680. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  12681. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  12682. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  12683. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12684.  
  12685. Except as contained in this notice, the name of the X Consortium shall not be
  12686. used in advertising or otherwise to promote the sale, use or other dealings
  12687. in this Software without prior written authorization from the X Consortium.
  12688.  
  12689. */
  12690. /*  Modified for stand-alone compiling by
  12691.  *  Peter 'Luna' Runestig <peter@runestig.com>
  12692.  */
  12693.  
  12694. #include <stdlib.h>
  12695. #include <string.h>
  12696. #include <fcntl.h>
  12697. #include <sys/stat.h>
  12698. #include <time.h>
  12699. #define Time_t time_t
  12700.  
  12701. void
  12702. XauDisposeAuth (auth)
  12703. Xauth   *auth;
  12704. {
  12705.     if (auth) {
  12706.         if (auth->address) (void) free (auth->address);
  12707.         if (auth->number) (void) free (auth->number);
  12708.         if (auth->name) (void) free (auth->name);
  12709.         if (auth->data) {
  12710.             (void) bzero (auth->data, auth->data_length);
  12711.             (void) free (auth->data);
  12712.         }
  12713.         free ((char *) auth);
  12714.     }
  12715.     return;
  12716. }
  12717.  
  12718. char *
  12719. XauFileName ()
  12720. {
  12721.     char *slashDotXauthority = "/.Xauthority";
  12722.     char    *name;
  12723.     static char *buf=NULL;
  12724.     static int  bsize=0;
  12725.     int     size, namelen;
  12726.     extern char * tn_fwdx_xauthority;
  12727.  
  12728.     if ( tn_fwdx_xauthority )
  12729.         return(tn_fwdx_xauthority);
  12730.  
  12731.     if (name = getenv ("XAUTHORITY"))
  12732.         return(name);
  12733.     name = zhome();
  12734.     if ( !name )
  12735.         return(NULL);
  12736.     namelen = strlen (name);
  12737.     size = namelen + strlen(slashDotXauthority) + 1;
  12738.     if (size > bsize) {
  12739.         if (buf)
  12740.             free (buf);
  12741.         buf = malloc ((unsigned) size);
  12742.         if (!buf)
  12743.             return 0;
  12744.         bsize = size;
  12745.     }
  12746.     ckstrncpy (buf, name, bsize);
  12747.     if ( name[namelen-1] != '/'
  12748. #ifdef OS2
  12749.          && name[namelen-1] != '\\'
  12750. #endif /* OS2 */
  12751.          )
  12752.         ckstrncat (buf, slashDotXauthority, bsize);
  12753.     else
  12754.         ckstrncat (buf, &slashDotXauthority[1], bsize);
  12755.     return(buf);
  12756. }
  12757.  
  12758. static int
  12759. binaryEqual (a, b, len)
  12760. register char   *a, *b;
  12761. register int    len;
  12762. {
  12763.     while (len--)
  12764.         if (*a++ != *b++)
  12765.             return 0;
  12766.     return 1;
  12767. }
  12768.  
  12769. #ifndef R_OK
  12770. #define R_OK 04
  12771. #endif /* R_OK */
  12772.  
  12773. Xauth *
  12774. XauGetAuthByAddr (family, address_length, address,
  12775.                           number_length, number,
  12776.                           name_length, name)
  12777. unsigned int    family;
  12778. unsigned int    address_length;
  12779. const char      *address;
  12780. unsigned int    number_length;
  12781. const char      *number;
  12782. unsigned int    name_length;
  12783. const char      *name;
  12784. {
  12785.     FILE    *auth_file;
  12786.     char    *auth_name;
  12787.     Xauth   *entry;
  12788.  
  12789.     auth_name = XauFileName();
  12790.     if (!auth_name)
  12791.         return 0;
  12792.     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
  12793.         return 0;
  12794.     auth_file = fopen (auth_name, "rb");
  12795.     if (!auth_file)
  12796.         return 0;
  12797.     for (;;) {
  12798.         entry = XauReadAuth (auth_file);
  12799.         if (!entry)
  12800.             break;
  12801.         /*
  12802.          * Match when:
  12803.          *   either family or entry->family are FamilyWild or
  12804.          *    family and entry->family are the same
  12805.          *  and
  12806.          *   either address or entry->address are empty or
  12807.          *    address and entry->address are the same
  12808.          *  and
  12809.          *   either number or entry->number are empty or
  12810.          *    number and entry->number are the same
  12811.          *  and
  12812.          *   either name or entry->name are empty or
  12813.          *    name and entry->name are the same
  12814.          */
  12815.  
  12816. /*      if ((family == FamilyWild || entry->family == FamilyWild ||
  12817.              (entry->family == family &&
  12818.               address_length == entry->address_length &&
  12819.               binaryEqual (entry->address, address, (int)address_length))) &&
  12820.             (number_length == 0 || entry->number_length == 0 ||
  12821.              (number_length == entry->number_length &&
  12822.               binaryEqual (entry->number, number, (int)number_length))) &&
  12823.             (name_length == 0 || entry->name_length == 0 ||
  12824.              (entry->name_length == name_length &&
  12825.               binaryEqual (entry->name, name, (int)name_length)))) */
  12826.         /* the original matching code above doesn't seem to meet the matching
  12827.          * algorithm, it doesn't check if "address_length == 0 ||
  12828.          * entry->address_length == 0". / Luna 2000-02-09
  12829.          */
  12830.         if ((family == FamilyWild || entry->family == FamilyWild ||
  12831.               entry->family == family) &&
  12832.             (address_length == 0 || entry->address_length == 0 ||
  12833.               (address_length == entry->address_length &&
  12834.               binaryEqual (entry->address, address, (int)address_length))) &&
  12835.             (number_length == 0 || entry->number_length == 0 ||
  12836.              (number_length == entry->number_length &&
  12837.               binaryEqual (entry->number, number, (int)number_length))) &&
  12838.             (name_length == 0 || entry->name_length == 0 ||
  12839.              (entry->name_length == name_length &&
  12840.               binaryEqual (entry->name, name, (int)name_length))))
  12841.             break;
  12842.         XauDisposeAuth (entry);
  12843.     }
  12844.     (void) fclose (auth_file);
  12845.     return entry;
  12846. }
  12847.  
  12848. static int
  12849. read_short (shortp, file)
  12850. unsigned short  *shortp;
  12851. FILE            *file;
  12852. {
  12853.     unsigned char   file_short[2];
  12854.  
  12855.     if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12856.         return 0;
  12857.     *shortp = file_short[0] * 256 + file_short[1];
  12858.     return 1;
  12859. }
  12860.  
  12861. static int
  12862. read_counted_string (countp, stringp, file)
  12863. unsigned short  *countp;
  12864. char    **stringp;
  12865. FILE    *file;
  12866. {
  12867.     unsigned short  len;
  12868.     char            *data;
  12869.  
  12870.     if (read_short (&len, file) == 0)
  12871.         return 0;
  12872.     if (len == 0) {
  12873.         data = 0;
  12874.     } else {
  12875.         data = malloc ((unsigned) len);
  12876.         if (!data)
  12877.             return 0;
  12878.         if (fread (data, (int) sizeof (char), (int) len, file) != len) {
  12879.             bzero (data, len);
  12880.             free (data);
  12881.             return 0;
  12882.         }
  12883.     }
  12884.     *stringp = data;
  12885.     *countp = len;
  12886.     return 1;
  12887. }
  12888.  
  12889. Xauth *
  12890. XauReadAuth (auth_file)
  12891. FILE    *auth_file;
  12892. {
  12893.     Xauth   local;
  12894.     Xauth   *ret;
  12895.  
  12896.     if (read_short (&local.family, auth_file) == 0)
  12897.         return 0;
  12898.     if (read_counted_string (&local.address_length,
  12899.                              &local.address, auth_file) == 0)
  12900.         return 0;
  12901.     if (read_counted_string (&local.number_length,
  12902.                              &local.number, auth_file) == 0) {
  12903.         if (local.address) free (local.address);
  12904.         return 0;
  12905.     }
  12906.     if (read_counted_string (&local.name_length,
  12907.                              &local.name, auth_file) == 0) {
  12908.         if (local.address) free (local.address);
  12909.         if (local.number) free (local.number);
  12910.         return 0;
  12911.     }
  12912.     if (read_counted_string (&local.data_length,
  12913.                              &local.data, auth_file) == 0) {
  12914.         if (local.address) free (local.address);
  12915.         if (local.number) free (local.number);
  12916.         if (local.name) free (local.name);
  12917.         return 0;
  12918.     }
  12919.     ret = (Xauth *) malloc (sizeof (Xauth));
  12920.     if (!ret) {
  12921.         if (local.address) free (local.address);
  12922.         if (local.number) free (local.number);
  12923.         if (local.name) free (local.name);
  12924.         if (local.data) {
  12925.             bzero (local.data, local.data_length);
  12926.             free (local.data);
  12927.         }
  12928.         return 0;
  12929.     }
  12930.     *ret = local;
  12931.     return ret;
  12932. }
  12933.  
  12934. static int
  12935. write_short (s, file)
  12936. unsigned short  s;
  12937. FILE            *file;
  12938. {
  12939.     unsigned char   file_short[2];
  12940.  
  12941.     file_short[0] = (s & (unsigned)0xff00) >> 8;
  12942.     file_short[1] = s & 0xff;
  12943.     if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12944.         return 0;
  12945.     return 1;
  12946. }
  12947.  
  12948. static int
  12949. write_counted_string (count, string, file)
  12950. unsigned short  count;
  12951. char    *string;
  12952. FILE    *file;
  12953. {
  12954.     if (write_short (count, file) == 0)
  12955.         return 0;
  12956.     if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
  12957.         return 0;
  12958.     return 1;
  12959. }
  12960.  
  12961. int
  12962. XauWriteAuth (auth_file, auth)
  12963. FILE    *auth_file;
  12964. Xauth   *auth;
  12965. {
  12966.     if (write_short (auth->family, auth_file) == 0)
  12967.         return 0;
  12968.     if (write_counted_string (auth->address_length,
  12969.                               auth->address, auth_file) == 0)
  12970.         return 0;
  12971.     if (write_counted_string (auth->number_length,
  12972.                               auth->number, auth_file) == 0)
  12973.         return 0;
  12974.     if (write_counted_string (auth->name_length, auth->name, auth_file) == 0)
  12975.         return 0;
  12976.     if (write_counted_string (auth->data_length, auth->data, auth_file) == 0)
  12977.         return 0;
  12978.     return 1;
  12979. }
  12980.  
  12981. #ifdef KRB5
  12982. #ifdef K5_XAUTH
  12983. /*
  12984.  * functions to encode/decode Kerberos V5 principals
  12985.  * into something that can be reasonable spewed over
  12986.  * the wire
  12987.  *
  12988.  * Author: Tom Yu <tlyu@MIT.EDU>
  12989.  *
  12990.  * Still needs to be fixed up wrt signed/unsigned lengths, but we'll worry
  12991.  * about that later.
  12992.  */
  12993.  
  12994. /*
  12995.  * XauKrb5Encode
  12996.  *
  12997.  * this function encodes the principal passed to it in a format that can
  12998.  * easily be dealt with by stuffing it into an X packet.  Encoding is as
  12999.  * follows:
  13000.  *   length count of the realm name
  13001.  *   realm
  13002.  *   component count
  13003.  *   length of component
  13004.  *   actual principal component
  13005.  *   etc....
  13006.  *
  13007.  * Note that this function allocates a hunk of memory, which must be
  13008.  * freed to avoid nasty memory leak type things.  All counts are
  13009.  * byte-swapped if needed. (except for the total length returned)
  13010.  *
  13011.  * nevermind.... stuffing the encoded packet in net byte order just to
  13012.  * always do the right thing.  Don't have to frob with alignment that way.
  13013.  */
  13014. int
  13015. XauKrb5Encode(princ, outbuf)
  13016.     krb5_principal princ;       /* principal to encode */
  13017.     krb5_data *outbuf;          /* output buffer */
  13018. {
  13019.     CARD16 i, numparts, totlen = 0, plen, rlen;
  13020.     char *cp, *pdata;
  13021.  
  13022.     rlen = krb5_princ_realm(princ)->length;
  13023.     numparts = krb5_princ_size(princ);
  13024.     totlen = 2 + rlen + 2;      /* include room for realm length
  13025.                                    and component count */
  13026.     for (i = 0; i < numparts; i++)
  13027.         totlen += krb5_princ_component(princ, i)->length + 2;
  13028.     /* add 2 bytes each time for length */
  13029.     if ((outbuf->data = (char *)malloc(totlen)) == NULL)
  13030.         return -1;
  13031.     cp = outbuf->data;
  13032.     *cp++ = (char)((int)(0xff00 & rlen) >> 8);
  13033.     *cp++ = (char)(0x00ff & rlen);
  13034.     memcpy(cp, krb5_princ_realm(princ)->data, rlen);    /* safe */
  13035.     cp += rlen;
  13036.     *cp++ = (char)((int)(0xff00 & numparts) >> 8);
  13037.     *cp++ = (char)(0x00ff & numparts);
  13038.     for (i = 0; i < numparts; i++)
  13039.     {
  13040.         plen = krb5_princ_component(princ, i)->length;
  13041.         pdata = krb5_princ_component(princ, i)->data;
  13042.         *cp++ = (char)((int)(0xff00 & plen) >> 8);
  13043.         *cp++ = (char)(0x00ff & plen);
  13044.         memcpy(cp, pdata, plen);                        /* safe */
  13045.         cp += plen;
  13046.     }
  13047.     outbuf->length = totlen;
  13048.     return 0;
  13049. }
  13050.  
  13051. /*
  13052.  * XauKrb5Decode
  13053.  *
  13054.  * This function essentially reverses what XauKrb5Encode does.
  13055.  * return value: 0 if okay, -1 if malloc fails, -2 if inbuf format bad
  13056.  */
  13057. int
  13058. XauKrb5Decode(inbuf, princ)
  13059.     krb5_data inbuf;
  13060.     krb5_principal *princ;
  13061. {
  13062.     CARD16 i, numparts, plen, rlen;
  13063.     CARD8 *cp, *pdata;
  13064.  
  13065.     if (inbuf.length < 4)
  13066.     {
  13067.         return -2;
  13068.     }
  13069.     *princ = (krb5_principal)malloc(sizeof (krb5_principal_data));
  13070.     if (*princ == NULL)
  13071.         return -1;
  13072.     bzero(*princ, sizeof (krb5_principal_data));
  13073.     cp = (CARD8 *)inbuf.data;
  13074.     rlen = *cp++ << 8;
  13075.     rlen |= *cp++;
  13076.     if (inbuf.length < 4 + (int)rlen + 2)
  13077.     {
  13078.         krb5_free_principal(*princ);
  13079.         return -2;
  13080.     }
  13081.     krb5_princ_realm(*princ)->data = (char *)malloc(rlen);
  13082.     if (krb5_princ_realm(*princ)->data == NULL)
  13083.     {
  13084.         krb5_free_principal(*princ);
  13085.         return -1;
  13086.     }
  13087.     krb5_princ_realm(*princ)->length = rlen;
  13088.     memcpy(krb5_princ_realm(*princ)->data, cp, rlen);   /* safe */
  13089.     cp += rlen;
  13090.     numparts = *cp++ << 8;
  13091.     numparts |= *cp++;
  13092.     krb5_princ_name(*princ) =
  13093.         (krb5_data *)malloc(numparts * sizeof (krb5_data));
  13094.     if (krb5_princ_name(*princ) == NULL)
  13095.     {
  13096.         krb5_free_principal(*princ);
  13097.         return -1;
  13098.     }
  13099.     krb5_princ_size(*princ) = 0;
  13100.     for (i = 0; i < numparts; i++)
  13101.     {
  13102.         if (cp + 2 > (CARD8 *)inbuf.data + inbuf.length)
  13103.         {
  13104.             krb5_free_principal(*princ);
  13105.             return -2;
  13106.         }
  13107.         plen = *cp++ << 8;
  13108.         plen |= *cp++;
  13109.         if (cp + plen > (CARD8 *)inbuf.data + inbuf.length)
  13110.         {
  13111.             krb5_free_principal(*princ);
  13112.             return -2;
  13113.         }
  13114.         pdata = (CARD8 *)malloc(plen);
  13115.         if (pdata == NULL)
  13116.         {
  13117.             krb5_free_principal(*princ);
  13118.             return -1;
  13119.         }
  13120.         krb5_princ_component(*princ, i)->data = (char *)pdata;
  13121.         krb5_princ_component(*princ, i)->length = plen;
  13122.         memcpy(pdata, cp, plen);        /* safe */
  13123.         cp += plen;
  13124.         krb5_princ_size(*princ)++;
  13125.     }
  13126.     return 0;
  13127. }
  13128. #endif /* K5_XAUTH */
  13129. #endif /* KRB5 */
  13130. #endif /* CK_FORWARD_X */
  13131. #endif /* CK_AUTHENTICATION */
  13132.  
  13133. /* C K _ A U T H _ I N I T
  13134.  * Initialize the Kerberos system for a pending connection
  13135.  *   hostname - a reverse DNS lookup of the hostname when possible
  13136.  *   ipaddr   - the ip address of the host
  13137.  *   username - the name the user wants to connect under not necessarily
  13138.  *              the same as principal
  13139.  *   socket   - the socket handle (ttyfd in Kermit speak)
  13140.  *
  13141.  * Returns: 1 on success and 0 on failure
  13142.  */
  13143.  
  13144. int
  13145. #ifdef CK_ANSIC
  13146. ck_auth_init( char * hostname, char * ipaddr, char * username, int socket )
  13147. #else /* CK_ANSIC */
  13148. ck_auth_init( hostname, ipaddr, username, socket )
  13149.     char * hostname; char * ipaddr; char *username; int socket;
  13150. #endif /* CK_ANSIC */
  13151. {
  13152. #ifdef CK_AUTHENTICATION
  13153. #ifdef OS2
  13154.     if ( !ck_security_loaddll() ) {
  13155.         TELOPT_ME_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  13156.         TELOPT_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  13157.         return(0);
  13158.     }
  13159. #endif /* OS2 */
  13160. #endif /* CK_AUTHENTICAITON */
  13161. #ifdef CK_ENCRYPTION
  13162.     if ( !!ck_crypt_is_installed() ) {
  13163.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  13164.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  13165.     }
  13166. #endif /* CK_ENCRYPTION */
  13167.  
  13168.     if (!hostname) hostname = "";
  13169.     if (!ipaddr) ipaddr = "";
  13170.     if (!username) username = "";
  13171.  
  13172.     debug(F110,"ck_auth_init Username",username,0);
  13173.     debug(F110,"ck_auth_init Hostname",hostname,0);
  13174.     debug(F110,"ck_auth_init Ipaddr",ipaddr,0);
  13175.  
  13176.     ckstrncpy( szUserName, username, UIDBUFLEN );
  13177.     ckstrncpy( szHostName, hostname, UIDBUFLEN );
  13178.     ckstrncpy( szIP, ipaddr, 16 );
  13179.     szUserNameRequested[0] = '\0';
  13180.     szUserNameAuthenticated[0] = '\0';
  13181.     validUser = AUTH_REJECT;
  13182.     accept_complete = 0;
  13183.     authentication_version = AUTHTYPE_NULL;
  13184.  
  13185. #ifdef CK_AUTHENTICATION
  13186.     auth_how = 0;
  13187.     auth_crypt = 0;
  13188.     auth_fwd = 0;
  13189.     mutual_complete = 0;
  13190.     if ( sstelnet )
  13191.         str_data[3] = TELQUAL_REPLY;
  13192.     else
  13193.         str_data[3] = TELQUAL_IS;
  13194. #endif /* CK_AUTHENTICATION */
  13195.  
  13196. #ifdef CK_SRP
  13197.     srp_waitresp = 0;
  13198. #endif /* SRP */
  13199.  
  13200. #ifdef CK_KERBEROS
  13201. #ifdef KRB5
  13202.     /* free previous ret_cred  */
  13203.     if ( ret_cred ) {
  13204. #ifdef CK_ENCRYPTION
  13205. #ifdef HEIMDAL
  13206.         if ( k5_session_key == &ret_cred->session)
  13207.             k5_session_key = NULL;
  13208. #else /* HEIMDAL */
  13209.         if ( k5_session_key == &ret_cred->keyblock)
  13210.             k5_session_key = NULL;
  13211. #endif /* HEIMDAL */
  13212. #endif /* CK_ENCRYPTION */
  13213.         krb5_free_creds(k5_context, ret_cred);
  13214.         ret_cred = NULL;
  13215.     }
  13216.     if (k5_ticket) {
  13217.         krb5_free_ticket(k5_context, k5_ticket);
  13218.         k5_ticket = NULL;
  13219.     }
  13220.     /* and context */
  13221.     if ( k5_context ) {
  13222.         krb5_free_context(k5_context);
  13223.         k5_context = NULL;
  13224.     }
  13225.  
  13226.     /* create k5_context */
  13227.     krb5_init_context(&k5_context);
  13228. #ifndef MIT_CURRENT
  13229. #ifndef NO_KRB5_INIT_ETS
  13230. /* This routine is a no-op in Kerberos 1.4.x and later */
  13231. /* and in some installations it can't be found in which case */
  13232. /* define NO_KRB5_INIT_ETS */
  13233.     if (k5_context)
  13234.         krb5_init_ets(k5_context);
  13235. #endif    /* NO_KRB5_INIT_ETS */
  13236. #endif /* MIT_CURRENT */
  13237. #ifdef KRB524_CONV
  13238.     krb524_init_ets(k5_context);
  13239. #endif /* KRB524_CONV */
  13240.     memset(&k5_auth,0,sizeof(k5_auth));
  13241.     if (auth_context) {
  13242.         krb5_auth_con_free(k5_context, auth_context);
  13243.         auth_context = 0;
  13244.     }
  13245. #ifdef CK_ENCRYPTION
  13246.     if (k5_session_key) {
  13247.         krb5_free_keyblock(k5_context, k5_session_key);
  13248.         k5_session_key = 0;
  13249.     }
  13250. #endif /* ENCRYPTION */
  13251. #ifdef TLS_VERIFY
  13252.     krb5_tls_verified = 0;
  13253. #endif /* TLS_VERIFY */
  13254. #endif /* KRB5 */
  13255.  
  13256. #ifdef KRB4
  13257. #ifdef CK_ENCRYPTION
  13258.     /* Initialize buffers used for authentication */
  13259.     memset(&k4_session_key, 0, sizeof(k4_session_key));
  13260.     memset(&k4_challenge, 0, sizeof(k4_challenge));
  13261. #endif /* CK_ENCRYPTION */
  13262. #endif /* KRB4 */
  13263.  
  13264. #ifdef RLOGCODE
  13265.     rlog_encrypt = 0;
  13266. #endif /* RLOGCODE */
  13267.     nstored = 0;
  13268.     store_ptr = storage;
  13269.     memset(storage,0,sizeof(storage));
  13270. #endif /* CK_KERBEROS */
  13271.  
  13272. #ifdef CK_ENCRYPTION
  13273.     kstream_destroy();
  13274.     if (!kstream_create_from_fd(socket, NULL))
  13275.         return(0);
  13276. #endif /* CK_ENCRYPTION */
  13277.     return(1);
  13278. }
  13279.  
  13280. void
  13281. auth_finished(result) int result; {
  13282.     extern char uidbuf[];
  13283.     extern int sstelnet;
  13284.  
  13285.     validUser = result;
  13286.     switch (result) {
  13287.     case AUTH_REJECT:           /* Rejected */
  13288.         if (sstelnet)
  13289.             uidbuf[0] = '\0';
  13290.         authentication_version = AUTHTYPE_NULL;
  13291.         break;
  13292.     case AUTH_UNKNOWN:          /* We don't know who he is, but he's okay */
  13293.         if (sstelnet)
  13294.             strcpy(uidbuf,"(unknown)");
  13295.         break;
  13296.     case AUTH_OTHER:            /* We know him, but not his name */
  13297.         if (sstelnet)
  13298.             strcpy(uidbuf,"(other)");
  13299.         break;
  13300.     case AUTH_USER:             /* We know he name */
  13301.     case AUTH_VALID:            /* We know him, and he needs no password */
  13302.         if (sstelnet)
  13303.             strcpy(uidbuf,szUserNameRequested);
  13304.         break;
  13305.     }
  13306. }
  13307.  
  13308. #ifdef MACOSX
  13309. #ifdef KRB5
  13310.  
  13311. krb5_error_code
  13312. ck_krb5_write_message(krb5_context con, krb5_pointer ptr, krb5_data *data)
  13313. {
  13314.     int fd = *((int *)ptr);
  13315.     long msglen;
  13316.  
  13317.     msglen = htonl(data->length);
  13318.     if (net_write(fd,(CHAR *)&msglen,4) != 4) {
  13319.         return(-1);
  13320.     }
  13321.     if ( data->length ) {
  13322.         if (net_write(fd,data->data,data->length) != data->length) {
  13323.             return(-1);
  13324.         }
  13325.     }
  13326.     return(0);
  13327. }
  13328.  
  13329. krb5_error_code
  13330. ck_krb5_read_message( krb5_context context,
  13331.                       krb5_pointer ptr,
  13332.                       krb5_data * data)
  13333. {
  13334.     extern int ttyfd;
  13335.     int fd = *((int *)ptr);
  13336.     long msglen;
  13337.     char *p;
  13338.     int i, rc;
  13339.  
  13340.     if (net_read(fd,&msglen,4) < 0)
  13341.         return(-1);
  13342.  
  13343.     data->length = ntohl(msglen);
  13344.     if ( data->length ) {
  13345.         data->data = malloc(data->length);
  13346.  
  13347.         i = 0;
  13348.         while ( i < data->length ) {
  13349.             if ((rc = net_read(fd,&data->data[i],(data->length - i))) < 0)
  13350.                 return(-1);
  13351.             i += rc;
  13352.         }
  13353.     }
  13354.     return(0);
  13355. }
  13356. #endif /* KRB5 */
  13357. #endif /* MACOSX */
  13358. #endif /* CK_SECURITY */
  13359.  
  13360.  
  13361.