home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckuath.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  409KB  |  13,354 lines

  1. char *ckathv = "Authentication, 8.0.234, 11 Jan 2007";
  2. /*
  3.   C K U A T H . C  --  Authentication for C-Kermit
  4.  
  5.   Copyright (C) 1999, 2004,
  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.  
  11.   Copyright (C) 2006, 2007  Secure Endpoints Inc.
  12.  
  13.     Author:  Jeffrey E Altman (jaltman@secure-endpoints.com)
  14.                Secure Endpoints Inc., New York City
  15. */
  16. /*
  17.  * Additional copyrights included with affected code.
  18.  */
  19.  
  20. #ifdef HEIMDAL
  21. /*
  22.   Turned off User to User support
  23.   Turned off KDESTROY support
  24.   Turned off KLIST support
  25.   Turned off krb5_prompter() support
  26.   Turned off ticket validation
  27.   Turned off ticket renewal
  28.   Turned off alternative cache support in k5_get_ccache()
  29.  
  30.   Remaining link problems:
  31.  
  32.   ckuath.o: In function `ck_krb5_initTGT':
  33.   ckuath.o(.text+0x50c2): undefined reference to `krb5_string_to_deltat'
  34.   ckuath.o(.text+0x516d): undefined reference to `krb5_string_to_deltat'
  35.   ckuath.o(.text+0x51ef): undefined reference to `krb5_string_to_deltat'
  36. */
  37. #endif /* HEIMDAL */
  38.  
  39. /*
  40.  * Implements Kerberos 4/5, SRP, SSL, NTLM authentication and START_TLS
  41.  */
  42.  
  43. #include "ckcsym.h"
  44. #include "ckcdeb.h"
  45.  
  46. #ifdef CK_SECURITY
  47.  
  48. #define CKUATH_C
  49. #include "ckcker.h"
  50. #include "ckuusr.h"
  51. #include "ckucmd.h"                             /* For struct keytab */
  52. #include "ckcnet.h"
  53. #include "ckctel.h"
  54.  
  55. char szUserNameRequested[UIDBUFLEN+1];    /* for incoming connections */
  56. char szUserNameAuthenticated[UIDBUFLEN+1];/* for incoming connections */
  57. char szHostName[UIDBUFLEN+1];
  58. char szUserName[UIDBUFLEN+1];
  59. static char szIP[16];
  60. static int  validUser = AUTH_REJECT;    /* User starts out invalid */
  61. int authentication_version = AUTHTYPE_NULL;
  62. int accept_complete = 0;
  63.  
  64. #ifdef CK_AUTHENTICATION
  65. #ifdef CK_SSL
  66. #ifdef KRB5
  67. #define TLS_VERIFY
  68. #endif /* KRB5 */
  69. #endif /* CK_SSL */
  70.  
  71. #ifdef CK_DES
  72. #ifdef CK_SSL
  73. #ifndef LIBDES
  74. #define LIBDES
  75. #endif /* LIBDES */
  76. #endif /* CK_SSL */
  77. #endif /* CK_DES */
  78.  
  79. #ifdef CRYPT_DLL
  80. #ifndef LIBDES
  81. #define LIBDES
  82. #endif /* LIBDES */
  83. #ifdef OS2
  84. #ifdef NT
  85. #include <windows.h>
  86. #else /* NT */
  87. #define INCL_DOSMODULEMGR
  88. #include <os2.h>
  89. #endif /* NT */
  90. #endif /* OS2 */
  91. #endif /* CRYPT_DLL */
  92.  
  93. #ifdef NT
  94. #define KRB5_AUTOCONF__
  95. #define NTLM
  96. #endif /* NT */
  97.  
  98. #ifdef CK_KERBEROS
  99. #define KINIT
  100. #ifndef HEIMDAL
  101. #define KLIST
  102. #define KDESTROY
  103. #endif /* HEIMDAL */
  104. #define CHECKADDRS
  105. #else /* CK_KERBEROS */
  106. #ifdef KRB4
  107. #undef KRB4
  108. #endif /* KRB4 */
  109. #ifdef KRB5
  110. #undef KRB5
  111. #endif /* KRB5 */
  112. #ifdef KRB524
  113. #undef KRB524
  114. #endif /* KRB524 */
  115. #endif /* CK_KERBEROS */
  116.  
  117. #include <stdlib.h>
  118. #include <string.h>
  119. #include <stdio.h>
  120. #include <time.h>
  121. #include <fcntl.h>
  122. #include <errno.h>
  123. #ifndef malloc
  124. #ifndef VMS
  125. #ifndef FREEBSD4
  126. #ifndef OpenBSD
  127. #ifdef MACOSX
  128. #include <sys/malloc.h>
  129. #else /* MACOSX */
  130. #include <malloc.h>
  131. #endif /* MACOSX */
  132. #endif /* OpenBSD */
  133. #endif /* FREEBSD4 */
  134. #endif /* VMS */
  135. #endif /* malloc */
  136. #ifdef OS2
  137. #include <io.h>
  138. #endif /* OS2 */
  139.  
  140. #ifdef KRB5
  141. #ifdef HEIMDAL
  142. #ifdef printf
  143. #define saveprintf printf
  144. #undef printf
  145. #endif /* printf */
  146. #include "krb5.h"
  147. #include "com_err.h"
  148. #ifdef saveprintf
  149. #define printf saveprintf
  150. #endif /* saveprintf */
  151. #else /* HEIMDAL */
  152. #include "krb5.h"
  153. #include "profile.h"
  154. #include "com_err.h"
  155. #ifdef KRB5_GET_INIT_CREDS_OPT_TKT_LIFE
  156. #define KRB5_HAVE_GET_INIT_CREDS
  157. #else
  158. #define krb5_free_unparsed_name(con,val) krb5_xfree((char *)(val))
  159. #endif
  160. #ifndef KRB5_HAVE_GET_INIT_CREDS
  161. #define krb5_free_data_contents(c,v) krb5_xfree((char *)(v)->data)
  162. #endif
  163. #endif /* HEIMDAL */
  164. #ifdef HAVE_PWD_H
  165. #include <pwd.h>
  166. #endif
  167. #endif /* KRB5 */
  168.  
  169. #ifdef KRB4
  170. #define  des_cblock Block
  171. #define  const_des_cblock const Block
  172. #define  des_key_schedule Schedule
  173. #ifdef KRB524
  174. #ifdef NT
  175. #define _WINDOWS
  176. #endif /* NT */
  177. #include "kerberosIV/krb.h"
  178. #ifndef OS2
  179. #ifdef KRB524_CONV
  180. #include "krb524.h"
  181. #endif /* KRB524_CONV */
  182. _PROTOTYP(const char * krb_get_err_text_entry, (int));
  183. #endif /* OS2 */
  184. #else /* KRB524 */
  185. #ifdef SOLARIS
  186. #ifndef sun
  187. /* for some reason the Makefile entries for the Solaris systems have -Usun */
  188. #define sun
  189. #endif /* sun */
  190. #endif /* SOLARIS */
  191. #include "krb.h"
  192. #define krb_get_err_text_entry krb_get_err_text
  193. #endif /* KRB524 */
  194. #else /* KRB4 */
  195. #ifdef CK_SSL
  196. #define  des_cblock Block
  197. #ifdef COMMENT
  198. #define  const_des_cblock const Block
  199. #endif /* COMMENT */
  200. #define  des_key_schedule Schedule
  201. #endif /* CK_SSL */
  202. #endif /* KRB4 */
  203.  
  204. #include "ckuath.h"
  205. #ifdef CK_KERBEROS
  206. #ifndef KRB5
  207. #define NOBLOCKDEF
  208. #else /* KRB5 */
  209. #ifdef KRB524
  210. #define NOBLOCKDEF
  211. #endif /* KRB524 */
  212. #endif /* KRB5 */
  213. #endif /* CK_KERBEROS */
  214. #include "ckuat2.h"
  215.  
  216. #ifdef CK_SSL
  217. #ifdef LIBDES
  218. #ifdef OPENSSL_097
  219. #define OPENSSL_ENABLE_OLD_DES_SUPPORT
  220. #include <openssl/des.h>
  221. #endif /* OPENSSL_097 */
  222. #ifndef HEADER_DES_H
  223. #define HEADER_DES_H
  224. #endif /* HEADER_DES_H */
  225. #endif /* LIBDES */
  226. #include "ck_ssl.h"
  227. extern int ssl_finished_messages;
  228. #endif /* SSL */
  229.  
  230. #define PWD_SZ 128
  231.  
  232. #ifndef LIBDES
  233. #ifdef UNIX
  234. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  235. #endif /* UNIX */
  236. #else /* LIBDES */
  237. #define des_fixup_key_parity des_set_odd_parity
  238. #endif /* LIBDES */
  239.  
  240. #ifdef OS2
  241. #ifdef CK_ENCRYPTION
  242. #define MAP_DES
  243. #endif /* CK_ENCRYPTION */
  244. #ifdef KRB4
  245. #define MAP_KRB4
  246. #endif /* KRB4 */
  247. #ifdef SRPDLL
  248. #define MAP_SRP
  249. #endif /* SRPDLL */
  250. #ifdef KRB5
  251. #define MAP_KRB5
  252. #endif /* KRB5 */
  253. #ifdef CRYPT_DLL
  254. #define MAP_CRYPT
  255. #endif /* CRYPT_DLL */
  256. #define MAP_NTLM
  257. #include "ckoath.h"
  258. #include "ckosyn.h"
  259. #endif /* OS2 */
  260.  
  261. /*
  262.  * Globals
  263.  */
  264. int auth_type_user[AUTHTYPLSTSZ] = {AUTHTYPE_AUTO, AUTHTYPE_NULL};
  265. int auth_how=0;
  266. int auth_crypt=0;
  267. int auth_fwd=0;
  268.  
  269. /* These are state completion variables */
  270. static int mutual_complete = 0;
  271.  
  272. #ifdef KRB4
  273. #ifdef OS2
  274. static LEASH_CREDENTIALS cred;
  275. #else /* OS2 */
  276. static CREDENTIALS cred;
  277. #endif /* OS2 */
  278. static KTEXT_ST k4_auth;
  279. static char     k4_name[ANAME_SZ];
  280. static AUTH_DAT k4_adat  = { 0 };
  281. static MSG_DAT  k4_msg_data;
  282. #ifdef CK_ENCRYPTION
  283. static Block    k4_session_key     = { 0 };
  284. static Schedule k4_sched;
  285. static Block    k4_challenge       = { 0 };
  286. #ifdef MIT_CURRENT
  287. static krb5_keyblock k4_krbkey;
  288. #endif /* MIT_CURRENT */
  289. #endif /* ENCRYPTION */
  290. #define KRB4_SERVICE_NAME    "rcmd"
  291.  
  292. _PROTOTYP(static int k4_auth_send,(VOID));
  293. _PROTOTYP(static int k4_auth_reply,(unsigned char *, int));
  294. _PROTOTYP(static int k4_auth_is,(unsigned char *, int));
  295. #endif /* KRB4 */
  296.  
  297. #ifdef KRB5
  298. static krb5_data          k5_auth;
  299. static krb5_auth_context  auth_context;
  300. static krb5_keyblock     *k5_session_key = NULL;
  301. static krb5_ticket       *k5_ticket = NULL;
  302. #ifndef KRB5_SERVICE_NAME
  303. #define KRB5_SERVICE_NAME    "host"
  304. #ifdef MACOSX
  305. #define MIT_CURRENT 1
  306. #define decode_krb5_ticket  krb5_decode_ticket
  307. #define krb5_read_message   ck_krb5_read_message
  308. #define krb5_write_message  ck_krb5_write_message
  309. #endif /* MACOSX */
  310. #endif
  311.  
  312. _PROTOTYP(static int k5_auth_send,(int,int,int));
  313. _PROTOTYP(static int k5_auth_reply,(int, unsigned char *, int));
  314. _PROTOTYP(static int k5_auth_is,(int,unsigned char *, int));
  315. _PROTOTYP(static int SendK5AuthSB,(int, void *, int));
  316. #ifdef TLS_VERIFY
  317. static int krb5_tls_verified = 0;
  318. #endif /* TLS_VERIFY */
  319. #endif /* KRB5 */
  320.  
  321. #ifdef GSSAPI_KRB5
  322. #include <gssapi/gssapi.h>
  323. #include <gssapi/gssapi_generic.h>
  324. #include <gssapi/gssapi_krb5.h>
  325.  
  326. static gss_ctx_id_t gcontext;
  327. #define GSS_BUFSIZ 4096
  328. static gss_buffer_desc gss_send_tok, gss_recv_tok, *gss_token_ptr;
  329. static char gss_stbuf[GSS_BUFSIZ];
  330. static gss_name_t gss_target_name;
  331. static struct gss_channel_bindings_struct gss_chan;
  332.  
  333. _PROTOTYP(static int gssk5_auth_send,(int,int,int));
  334. _PROTOTYP(static int gssk5_auth_reply,(int, unsigned char *, int));
  335. _PROTOTYP(static int gssk5_auth_is,(int,unsigned char *, int));
  336. _PROTOTYP(static int SendGSSK5AuthSB,(int, void *, int));
  337. #endif /* GSSAPI_KRB5 */
  338.  
  339. #ifdef CK_SRP
  340. #ifdef PRE_SRP_1_7_3
  341. _PROTOTYP(static int srp_reply,(int, unsigned char *, int));
  342. _PROTOTYP(static int srp_is,(int, unsigned char *, int));
  343. #else /* PRE_SRP_1_7_3 */
  344. _PROTOTYP(static int new_srp_reply,(int, unsigned char *, int));
  345. _PROTOTYP(static int new_srp_is,(int, unsigned char *, int));
  346. #endif /* PRE_SRP_1_7_3 */
  347. #endif /* SRP */
  348.  
  349. #ifdef CK_ENCRYPTION
  350. int encrypt_flag = 1;
  351. #endif
  352. #ifdef FORWARD
  353. int forward_flag = 0;              /* forward tickets? */
  354. int forwardable_flag = 1;          /* get forwardable tickets to forward? */
  355. int forwarded_tickets = 0;         /* were tickets forwarded? */
  356. #endif
  357.  
  358. static unsigned char str_data[4096] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
  359.                                         AUTHTYPE_KERBEROS_V5, };
  360. #define AUTHTMPBL 2048
  361. static char strTmp[AUTHTMPBL+1];
  362. static char szLocalHostName[UIDBUFLEN+1];
  363. static kstream g_kstream=NULL;
  364.  
  365. #ifdef KRB5
  366. krb5_context k5_context=NULL;
  367. static krb5_creds * ret_cred=NULL;
  368. static krb5_context telnet_context=NULL;
  369. static char * telnet_krb5_realm = NULL;
  370. static krb5_principal fwd_server = NULL;
  371. #endif /* KRB5 */
  372.  
  373. #ifdef CK_SRP
  374. #ifdef PRE_SRP_1_4_4
  375. #ifndef PRE_SRP_1_4_5
  376. #define PRE_SRP_1_4_5
  377. #endif /* PRE_SRP_1_4_5 */
  378. #endif /* PRE_SRP_1_4_5 */
  379. #ifdef PRE_SRP_1_4_5
  380. #ifndef PRE_SRP_1_7_3
  381. #define PRE_SRP_1_7_3
  382. #endif /* PRE_SRP_1_7_3 */
  383. #endif /* PRE_SRP_1_4_5 */
  384. #include <t_pwd.h>
  385. #include <t_client.h>
  386. #include <t_server.h>
  387. static struct t_server * ts = NULL;
  388. static struct t_client * tc = NULL;
  389. #ifdef PRE_SRP_1_4_4
  390. static struct t_pw * tpw = NULL;
  391. static struct t_conf * tconf = NULL;
  392. #endif /* PRE_SRP_1_4_4 */
  393. #ifndef PRE_SRP_1_7_3
  394. #ifndef STDC_HEADERS
  395. #define STDC_HEADERS 1
  396. #endif /* STDC_HEADERS */
  397. #include <srp.h>
  398. static SRP * s_srp = NULL;
  399. static cstr * s_key = NULL;
  400. static SRP * c_srp = NULL;
  401. static cstr * c_key = NULL;
  402. #endif /* PRE_SRP_1_7_3 */
  403. static int srp_waitresp = 0;    /* Flag to indicate readiness for response */
  404. static char srp_passwd[PWD_SZ];
  405. #endif /* CK_SRP */
  406.  
  407. #ifdef CK_KERBEROS
  408. #ifdef RLOGCODE
  409. #define OPTS_FORWARD_CREDS           0x00000020
  410. #define OPTS_FORWARDABLE_CREDS       0x00000010
  411. #define KCMD_KEYUSAGE                1026
  412.  
  413. #define RLOG_BUFSIZ 5120
  414. static int rlog_encrypt = 0;
  415. char des_inbuf[2*RLOG_BUFSIZ];       /* needs to be > largest read size */
  416. char des_outpkt[2*RLOG_BUFSIZ+4];    /* needs to be > largest write size */
  417. #ifdef KRB5
  418. krb5_data desinbuf,desoutbuf;
  419. krb5_encrypt_block eblock;             /* eblock for encrypt/decrypt */
  420. static krb5_data encivec_i[2], encivec_o[2];
  421.  
  422. enum krb5_kcmd_proto {
  423.   /* Old protocol: DES encryption only.  No subkeys.  No protection
  424.      for cleartext length.  No ivec supplied.  OOB hacks used for
  425.      rlogin.  Checksum may be omitted at connection startup.  */
  426.   KCMD_OLD_PROTOCOL = 1,
  427.   /* New protocol: Any encryption scheme.  Client-generated subkey
  428.      required.  Prepend cleartext-length to cleartext data (but don't
  429.      include it in count).  Starting ivec defined, chained.  In-band
  430.      signalling.  Checksum required.  */
  431.   KCMD_NEW_PROTOCOL,
  432.   /* Hack: Get credentials, and use the old protocol iff the session
  433.      key type is single-DES.  */
  434.   KCMD_PROTOCOL_COMPAT_HACK,
  435.   KCMD_UNKNOWN_PROTOCOL
  436. };
  437. enum krb5_kcmd_proto krb5_rlog_ver = KCMD_PROTOCOL_COMPAT_HACK;
  438. #endif /* KRB5 */
  439. #endif /* RLOGCODE */
  440. static char storage[65536];            /* storage for the decryption */
  441. static int nstored = 0;
  442. static char *store_ptr = storage;
  443.  
  444. extern char * krb5_d_principal;         /* Default principal */
  445. extern char * krb5_d_instance;          /* Default instance */
  446. extern char * krb5_d_realm;             /* Default realm */
  447. extern char * krb5_d_cc;                /* Default credentials cache */
  448. extern char * krb5_d_srv;               /* Default service name */
  449. extern int    krb5_d_lifetime;          /* Default lifetime */
  450. extern int    krb5_d_forwardable;
  451. extern int    krb5_d_proxiable;
  452. extern int    krb5_d_renewable;
  453. extern int    krb5_autoget;
  454. extern int    krb5_checkaddrs;
  455. extern int    krb5_d_getk4;
  456. extern int    krb5_d_no_addresses;
  457. extern char * k5_keytab;
  458.  
  459. extern int    krb5_errno;
  460. extern char * krb5_errmsg;
  461.  
  462. extern char * krb4_d_principal;         /* Default principal */
  463. extern char * krb4_d_realm;             /* Default realm */
  464. extern char * krb4_d_srv;               /* Default service name */
  465. extern int    krb4_d_lifetime;          /* Default lifetime */
  466. extern int    krb4_d_preauth;
  467. extern char * krb4_d_instance;
  468. extern int    krb4_autoget;
  469. extern int    krb4_checkaddrs;
  470. extern char * k4_keytab;
  471.  
  472. extern int    krb4_errno;
  473. extern char * krb4_errmsg;
  474. #endif /* CK_KERBEROS */
  475.  
  476. extern char tn_msg[], hexbuf[];         /* from ckcnet.c */
  477. extern CHAR pwbuf[];
  478. extern int  pwflg, pwcrypt;
  479. extern int deblog, debses, tn_deb;
  480. extern int sstelnet, inserver;
  481. #ifdef CK_LOGIN
  482. extern int ckxanon;
  483. #endif /* CK_LOGIN */
  484. extern int tn_auth_how;
  485. extern int tn_auth_enc;
  486. #ifdef CK_ENCRYPTION
  487. extern int cx_type;
  488. #endif /* CK_ENCRYPTION */
  489. extern int quiet, ttyfd, ttnproto;
  490.  
  491. int
  492. ck_gssapi_is_installed()
  493. {
  494. #ifdef KRB5
  495. #ifdef OS2
  496.     return(hGSSAPI != NULL);
  497. #else /* OS2 */
  498.     return(1);
  499. #endif /* OS2 */
  500. #else /* KRB5 */
  501.     return(0);
  502. #endif /* KRB5 */
  503. }
  504.  
  505. int
  506. ck_krb5_is_installed()
  507. {
  508. #ifdef KRB5
  509. #ifdef OS2
  510.     return(hKRB5_32 != NULL);
  511. #else /* OS2 */
  512.     return(1);
  513. #endif /* OS2 */
  514. #else /* KRB5 */
  515.     return(0);
  516. #endif /* KRB5 */
  517. }
  518.  
  519.  
  520. int
  521. ck_krb5_is_installed_as_server()
  522. {
  523. #ifdef KRB5
  524. #ifdef HEIMDAL
  525.     krb5_error_code ret;
  526.     krb5_keytab kt;
  527.     krb5_kt_cursor cursor;
  528.  
  529.     ret = krb5_kt_default(k5_context, &kt);
  530.     if ( ret ) {
  531.         krb5_kt_close(k5_context, kt);
  532.         return(0);
  533.     } else {
  534.         krb5_kt_end_seq_get(k5_context, kt, &cursor);
  535.         krb5_kt_close(k5_context, kt);
  536.         return(1);
  537.     }
  538. #else /* HEIMDAL */
  539. #ifndef COMMENT
  540.     char ktname[CKMAXPATH]="";
  541.  
  542.     if ( k5_keytab ) {
  543.         ckstrncpy(ktname,k5_keytab,CKMAXPATH);
  544.     } else {
  545.         krb5_error_code code;
  546.  
  547.         if ( k5_context == NULL)
  548.             if (krb5_init_context(&k5_context))
  549.                 return(0);
  550.  
  551.         code = krb5_kt_default_name(k5_context,ktname,CKMAXPATH);
  552.         debug(F101,"krb5_kt_default_name","",code);
  553.         if ( code ) {
  554.             /* We can't check the existence of the file since we can't   */
  555.             /* determine the file name.  So we return TRUE and let       */
  556.             /* Krb5 be offered to the user even though it may fail later */
  557.             return(1);
  558.         }
  559.     }
  560.  
  561.     if ( !strncmp("FILE:",ktname,5) ) {
  562.         if ( zchki(&ktname[5]) > 0 )
  563.             return(1);
  564.         else
  565.             return(0);
  566.     } else {
  567.         if (ktname[0])
  568.             return(1);
  569.         else
  570.             return(0);
  571.     }
  572. #else /* COMMENT */
  573.     krb5_error_code             krb5rc = KRB5KRB_ERR_GENERIC;
  574.     krb5_context                krb5context = NULL;
  575.     krb5_ccache                 krb5ccdef = NULL;
  576.     krb5_creds                  krb5creds, *krb5credsp = NULL;
  577.     int                         rc = 0;
  578.  
  579.     if ( !ck_krb5_is_installed() )
  580.         return(0);
  581.  
  582.     memset((char *)&krb5creds, 0, sizeof(krb5creds));
  583.  
  584.     if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  585.         goto err;
  586.  
  587.     if ((krb5rc = krb5_sname_to_principal(krb5context,
  588.                                           szHostName,
  589.                                           krb5_d_srv ?
  590.                                           krb5_d_srv :
  591.                                           KRB5_SERVICE_NAME,
  592.                                           KRB5_NT_SRV_HST,
  593.                                           &krb5creds.server)) != 0)
  594.       goto err;
  595.  
  596.     if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  597.         goto err;
  598.  
  599.     if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  600.                                          &krb5creds.client)) != 0)
  601.         goto err;
  602.  
  603.     if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  604.                                         &krb5creds, &krb5credsp)) != 0)
  605.         goto err;
  606.     rc = 1;
  607.  
  608.   err:
  609.  
  610.     if (krb5creds.client)
  611.       krb5_free_principal(krb5context, krb5creds.client);
  612.     if (krb5creds.server)
  613.       krb5_free_principal(krb5context, krb5creds.server);
  614.     if (krb5context)
  615.       krb5_free_context(krb5context);
  616.     return(rc);
  617.  
  618. #endif /* COMMENT */
  619. #endif /* HEIMDAL */
  620. #else /* KRB5 */
  621.     return(0);
  622. #endif /* KRB5 */
  623. }
  624.  
  625. int
  626. ck_krb4_is_installed()
  627. {
  628. #ifdef KRB4
  629. #ifdef OS2
  630.     return(hKRB4_32 != NULL);
  631. #else /* OS2 */
  632.     return(1);
  633. #endif /* OS2 */
  634. #else /* KRB4 */
  635.     return(0);
  636. #endif /* KRB4 */
  637. }
  638.  
  639. int
  640. ck_krb4_is_installed_as_server()
  641. {
  642.     if ( !ck_krb4_is_installed() )
  643.         return(0);
  644.  
  645. #ifdef KRB4
  646.     if ( !k4_keytab ) {
  647. #ifdef NT
  648.         char name[CKMAXPATH]="";
  649.         DWORD len = CKMAXPATH;
  650.  
  651.         len = GetWindowsDirectory(name,len);
  652.         if ( len > 0 )
  653.             ckstrncat(name,"/srvtab",CKMAXPATH);
  654.         if ( name[0] )
  655.             makestr(&k4_keytab,name);
  656. #else /* NT */
  657.         makestr(&k4_keytab,"/etc/srvtab");
  658. #endif /* NT */
  659.     }
  660.  
  661.     if ( !k4_keytab )
  662.         return(0);
  663.  
  664.     if ( zchki(k4_keytab) > 0 )
  665.         return(1);
  666. #ifdef KRB524
  667.     else if (ck_krb5_is_installed_as_server())
  668.         return(1);
  669. #endif /* KRB524 */
  670.     else
  671.         return(0);
  672. #endif /* KRB4 */
  673. }
  674.  
  675. int
  676. ck_srp_is_installed_as_server()
  677. {
  678. #ifdef CK_SRP
  679. #ifdef SRPDLL
  680.     if ( hSRP == NULL )
  681.         return(0);
  682. #endif /* SRPDLL */
  683. #ifdef COMMENT
  684.     /* This is the new API as of 1.7.4.  However, all it does
  685.        is allocate a data structure.  It can never fail.
  686.      */
  687.     {
  688.         SRP * s_srp = SRP_new(SRP_RFC2945_server_method());
  689.         if ( s_srp ) {
  690.             SRP_free(s_srp);
  691.             s_srp = NULL;
  692.             return(1);
  693.         }
  694.         return(0);
  695.     }
  696. #else /* COMMENT */
  697.     {
  698.         struct t_pw * tpw = NULL;
  699.         struct t_conf * tconf = NULL;
  700.         if((tconf = t_openconf(NULL)) == NULL)
  701.             return(0);
  702.         if((tpw = t_openpw(NULL)) == NULL) {
  703.             t_closeconf(tconf);
  704.             return(0);
  705.         }
  706.         t_closeconf(tconf);
  707.         t_closepw(tpw);
  708.         return(1);
  709.     }
  710. #endif /* COMMENT */
  711. #else /* SRP */
  712.     return(0);
  713. #endif /* SRP */
  714. }
  715.  
  716. int
  717. ck_srp_is_installed()
  718. {
  719. #ifdef CK_SRP
  720. #ifdef SRPDLL
  721.     if ( hSRP == NULL )
  722.         return(0);
  723. #endif /* SRPDLL */
  724.     return(1);
  725. #else /* CK_SRP */
  726.     return(0);
  727. #endif /* CK_SRP */
  728. }
  729.  
  730. int
  731. ck_krypto_is_installed()
  732. {
  733. #ifdef CK_SRP
  734. #ifdef OS2
  735.     if ( hLIBKRYPTO == NULL )
  736.         return(0);
  737. #endif /* OS2 */
  738.     return(1);
  739. #else /* CK_SRP */
  740.     return(0);
  741. #endif /* CK_SRP */
  742. }
  743.  
  744. int
  745. ck_crypt_is_installed()
  746. {
  747. #ifdef CK_ENCRYPTION
  748. #ifdef CRYPT_DLL
  749.     return(hCRYPT != NULL);
  750. #else /* CRYPT_DLL */
  751.     return(1);
  752. #endif /* CRYPT_DLL */
  753. #else /* ENCRYPTION */
  754.     return(0);
  755. #endif /* ENCRYPTION */
  756. }
  757.  
  758. int
  759. ck_ntlm_is_installed()
  760. {
  761. #ifdef NT
  762.     return(hSSPI != NULL);
  763. #else /* NT */
  764.     return(0);
  765. #endif /* NT */
  766. }
  767.  
  768. int
  769. ck_tn_auth_valid()
  770. {
  771.     return(validUser);
  772. }
  773.  
  774. /* C K _ K R B _ A U T H _ I N _ P R O G R E S S
  775.  *
  776.  * Is an authentication negotiation still in progress?
  777.  *
  778.  */
  779.  
  780. int
  781. #ifdef CK_ANSIC
  782. ck_tn_auth_in_progress(void)
  783. #else
  784. ck_tn_auth_in_progress()
  785. #endif
  786. {
  787.     switch (authentication_version) {
  788.     case AUTHTYPE_AUTO:
  789.         return(1);
  790.     case AUTHTYPE_NULL:
  791.         return(0);
  792. #ifdef KRB4
  793.     case AUTHTYPE_KERBEROS_V4:
  794.         if (!accept_complete) {
  795.             debug(F100,"ck_auth_in_progress() Kerberos 4 !accept_complete",
  796.                    "",0);
  797.             return(1);
  798.         }
  799.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  800.             debug(F100,"ck_auth_in_progress() Kerberos 4 !mutual_complete",
  801.                    "",0);
  802.             return(1);
  803.         }
  804.         else
  805.             return(0);
  806. #endif /* KRB4 */
  807. #ifdef KRB5
  808.     case AUTHTYPE_KERBEROS_V5:
  809.         if (!accept_complete) {
  810.             debug(F100,"ck_auth_in_progress() Kerberos 5 !accept_complete",
  811.                    "",0);
  812.             return(1);
  813.         }
  814.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  815.             debug(F100,"ck_auth_in_progress() Kerberos 5 !mutual_complete",
  816.                    "",0);
  817.             return(1);
  818.         }
  819.         else
  820.             return(0);
  821. #ifdef GSSAPI_K5
  822.     case AUTHTYPE_GSSAPI_KRB5:
  823.         if (!accept_complete) {
  824.             debug(F100,
  825.           "ck_auth_in_progress() GSSAPI Kerberos 5 !accept_complete",
  826.           "",
  827.           0
  828.           );
  829.             return(1);
  830.         }
  831.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  832.             debug(F100,
  833.           "ck_auth_in_progress() GSSAPI Kerberos 5 !mutual_complete",
  834.           "",
  835.           0
  836.           );
  837.             return(1);
  838.         } else
  839.       return(0);
  840.         break;
  841. #endif /* GSSAPI_K5 */
  842. #endif /* KRB5 */
  843. #ifdef CK_SRP
  844.     case AUTHTYPE_SRP:
  845.         if (!accept_complete || srp_waitresp)
  846.             return(1);
  847.         else
  848.             return(0);
  849. #endif /* CK_SRP */
  850. #ifdef NTLM
  851.     case AUTHTYPE_NTLM:
  852.         if (!accept_complete) {
  853.             debug(F100,"ck_auth_in_progress() NTLM !accept_complete",
  854.                    "",0);
  855.             return(1);
  856.         }
  857.         else
  858.             return(0);
  859. #endif /* NTLM */
  860.     case AUTHTYPE_SSL:
  861.         if (!accept_complete) {
  862.             debug(F100,"ck_auth_in_progress() SSL !accept_complete",
  863.                    "",0);
  864.             return(1);
  865.         }
  866.         else
  867.             return(0);
  868.     default:
  869.         return(0);
  870.     }
  871.     return(0);
  872. }
  873.  
  874.  
  875. /*  C K _ K R B _ T N _ A U T H _ R E Q U E S T
  876.  *
  877.  *  Builds a Telnet Authentication Send Negotiation providing the
  878.  *  list of supported authentication methods.  To be used only
  879.  *  when accepting incoming connections as only the server (DO) side of the
  880.  *  Telnet negotiation is allowed to send an AUTH SEND.
  881.  *
  882.  *  Returns: 0 on success and -1 on failure
  883.  */
  884.  
  885. static unsigned char str_request[64] = { IAC, SB,
  886.                                              TELOPT_AUTHENTICATION,
  887.                                              TELQUAL_SEND };
  888. #ifdef GSSAPI_K5
  889. static int
  890. ck_tn_auth_request_gsskrb5(int i)
  891. {
  892.     if (ck_gssapi_is_installed() && ck_krb5_is_installed_as_server()) {
  893.         if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  894.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  895.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  896.                tn_auth_enc == TN_AUTH_ENC_EXCH) ) {
  897.             str_request[i++] = AUTHTYPE_KERBEROS_V5;
  898.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  899.             str_request[i] |= AUTH_ENCRYPT_AFTER_EXCHANGE;
  900.  
  901.             if ( deblog || tn_deb || debses )
  902.                 ckstrncat(tn_msg,
  903.                 "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_AFTER_EXCHANGE ",
  904.                           TN_MSG_LEN);
  905.             i++;
  906.         }
  907.     }
  908. }
  909. #endif /* GSSAPI_K5 */
  910.  
  911. #ifdef KRB5
  912. static int
  913. ck_tn_auth_request_krb5(int i)
  914. {
  915.     if (ck_krb5_is_installed_as_server()) {
  916. #ifdef CK_SSL
  917.         if ( ck_ssleay_is_installed() &&
  918.              (tls_active_flag || ssl_active_flag) &&
  919.              ssl_finished_messages )
  920.         {
  921. #ifdef USE_INI_CRED_FWD
  922.             if ( forward_flag &&
  923.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  924.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  925.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  926.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  927.                  )
  928.             {
  929.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  930.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  931.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  932.                 str_request[i] |= INI_CRED_FWD_ON;
  933.  
  934.                 if ( deblog || tn_deb || debses )
  935.                     ckstrncat(tn_msg,
  936.  "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS|INI_CRED_FWD_ON ",
  937.                                TN_MSG_LEN);
  938.                 i++;
  939.             }
  940. #endif /* USE_INI_CRED_FWD */
  941.             if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  942.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  943.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  944.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  945.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  946.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  947.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  948.  
  949.                 if ( deblog || tn_deb || debses )
  950.                     ckstrncat(tn_msg,
  951.                       "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS ",
  952.                               TN_MSG_LEN);
  953.                 i++;
  954.             }
  955.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  956.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  957.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  958.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  959.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  960.  
  961.                 if ( deblog || tn_deb || debses )
  962.                     ckstrncat(tn_msg,
  963.                     "KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  964.                                TN_MSG_LEN);
  965.                 i++;
  966.             }
  967.         }
  968. #ifdef CK_ENCRYPTION
  969.         else
  970.         {
  971. #endif /* CK_ENCRYPTION */
  972. #endif /* CK_SSL */
  973. #ifdef CK_ENCRYPTION
  974. #ifdef USE_INI_CRED_FWD
  975.             if ( forward_flag &&
  976.                  TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  977.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  978.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  979.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  980.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  981.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  982.                  )
  983.             {
  984.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  985.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  986.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  987.                 str_request[i] |= INI_CRED_FWD_ON;
  988.  
  989.                 if ( deblog || tn_deb || debses )
  990.                     ckstrncat(tn_msg,
  991.   "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT|INI_CRED_FWD_ON ",
  992.                                TN_MSG_LEN);
  993.                 i++;
  994.             }
  995. #endif /* USE_INI_CRED_FWD */
  996.  
  997.             if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  998.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  999.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1000.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1001.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1002.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  1003.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1004.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1005.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1006.  
  1007.                 if ( deblog || tn_deb || debses )
  1008.                     ckstrncat(tn_msg,
  1009.               "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT ",
  1010.                                TN_MSG_LEN);
  1011.                 i++;
  1012.             }
  1013. #ifdef CK_SSL
  1014.         }
  1015. #endif /* CK_SSL */
  1016. #endif /* CK_ENCRYPTION */
  1017.  
  1018.         if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1019.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1020.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1021.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1022. #ifdef CK_SSL
  1023.              && !(ck_ssleay_is_installed() &&
  1024.                    (tls_active_flag || ssl_active_flag) &&
  1025.                    tls_is_anon(0))
  1026. #endif /* CK_SSL */
  1027.              )
  1028.         {
  1029. #ifdef CK_ENCRYPTION
  1030.             /* Can't perform mutual authentication without encryption */
  1031.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1032.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1033.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1034.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1035.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1036.  
  1037.                 if ( deblog || tn_deb || debses )
  1038.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL ",
  1039.                                TN_MSG_LEN);
  1040.                 i++;
  1041.             }
  1042. #endif /* CK_ENCRYPTION */
  1043.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1044.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1045.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1046.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1047.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1048.  
  1049.                 if ( deblog || tn_deb || debses )
  1050.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY ",
  1051.                                TN_MSG_LEN);
  1052.                 i++;
  1053.             }
  1054.         }
  1055.     }
  1056.     return(i);
  1057. }
  1058. #endif /* KRB5 */
  1059. #ifdef KRB4
  1060. static int
  1061. ck_tn_auth_request_krb4(int i)
  1062. {
  1063.     if (ck_krb4_is_installed_as_server()) {
  1064. #ifdef CK_ENCRYPTION
  1065.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1066.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1067.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1068.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1069.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1070.                tn_auth_enc == TN_AUTH_ENC_TELOPT) )
  1071.         {
  1072.             str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1073.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1074.             str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1075.  
  1076.             if ( deblog || tn_deb || debses )
  1077.               ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL|ENCRYPT ",
  1078.                         TN_MSG_LEN);
  1079.             i++;
  1080.         }
  1081. #endif /* CK_ENCRYPTION */
  1082.  
  1083.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1084.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1085.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1086.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1087.         {
  1088. #ifdef CK_ENCRYPTION
  1089.             /* Can't perform mutual authentication without encryption */
  1090.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1091.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1092.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1093.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1094.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1095.  
  1096.                 if ( deblog || tn_deb || debses )
  1097.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL ",
  1098.                                TN_MSG_LEN);
  1099.                 i++;
  1100.             }
  1101. #endif /* CK_ENCRYPTION */
  1102.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1103.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1104.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1105.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1106.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1107.  
  1108.                 if ( deblog || tn_deb || debses )
  1109.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|ONE_WAY ",
  1110.                                TN_MSG_LEN);
  1111.                 i++;
  1112.             }
  1113.         }
  1114.     }
  1115.  
  1116.     return(i);
  1117. }
  1118. #endif /* KRB4 */
  1119.  
  1120. #ifdef CK_SRP
  1121. static int
  1122. ck_tn_auth_request_srp(int i)
  1123. {
  1124.     if (ck_srp_is_installed_as_server()) {
  1125. #ifndef PRE_SRP_1_4_5
  1126.         /* Dont' do this yet.  SRP when it uses the ENCRYPT_USING_TELOPT   */
  1127.         /* flag it must perform a checksum of the auth-type-pair but there */
  1128.         /* is no mechansim to do that yet.                                 */
  1129. #ifdef CK_SSL
  1130.         if ( ck_ssleay_is_installed() &&
  1131.              (tls_active_flag || ssl_active_flag) &&
  1132.              ssl_finished_messages &&
  1133.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1134.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1135.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1136.                    tn_auth_enc == TN_AUTH_ENC_TELOPT))
  1137.         {
  1138.             str_request[i++] = AUTHTYPE_SRP;
  1139.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1140.             str_request[i] |= AUTH_ENCRYPT_START_TLS;
  1141.  
  1142.             if ( deblog || tn_deb || debses )
  1143.                 ckstrncat(tn_msg,
  1144.                            "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  1145.                            TN_MSG_LEN);
  1146.             i++;
  1147.         }
  1148. #ifdef CK_ENCRYPTION
  1149.         else {
  1150. #endif /* CK_ENCRYPTION */
  1151. #endif /* CK_SSL */
  1152. #ifdef CK_ENCRYPTION
  1153.             if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1154.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1155.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1156.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1157.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1158.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  1159.                  ) {
  1160.                 str_request[i++] = AUTHTYPE_SRP;
  1161.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1162.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1163.  
  1164.                 if ( deblog || tn_deb || debses )
  1165.                     ckstrncat(tn_msg,
  1166.                     "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_USING_TELOPT ",
  1167.                                TN_MSG_LEN);
  1168.                 i++;
  1169.             }
  1170. #ifdef CK_SSL
  1171.         }
  1172. #endif /* CK_SSL */
  1173. #endif /* CK_ENCRYPTION */
  1174. #endif /* PRE_SRP_1_4_5 */
  1175.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1176.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1177.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1178.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1179.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1180.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1181. #ifdef CK_SSL
  1182.              && !(ck_ssleay_is_installed() &&
  1183.                    (tls_active_flag || ssl_active_flag) &&
  1184.                    tls_is_anon(0))
  1185. #endif /* CK_SSL */
  1186.              )
  1187.         {
  1188.             str_request[i++] = AUTHTYPE_SRP;
  1189.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1190.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1191.  
  1192.             if ( deblog || tn_deb || debses )
  1193.                 ckstrncat(tn_msg,"SRP CLIENT_TO_SERVER|ONE_WAY ",
  1194.                            TN_MSG_LEN);
  1195.             i++;
  1196.         }
  1197.     }
  1198.  
  1199.     return(i);
  1200. }
  1201. #endif /* CK_SRP */
  1202.  
  1203. #ifdef CK_SSL
  1204. static int
  1205. ck_tn_auth_request_ssl(int i)
  1206. {
  1207.     if (ck_ssleay_is_installed()
  1208.          && !tls_active_flag && !ssl_active_flag && ssl_initialized
  1209.          ) {
  1210.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1211.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1212.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1213.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1214.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1215.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1216.         {
  1217.             str_request[i++] = AUTHTYPE_SSL;
  1218.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1219.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1220.             if ( deblog || tn_deb || debses )
  1221.                 ckstrncat(tn_msg,"SSL CLIENT_TO_SERVER|ONE_WAY ",
  1222.                            TN_MSG_LEN);
  1223.             i++;
  1224.         }
  1225.     }
  1226.  
  1227.     return(i);
  1228. }
  1229. #endif /* CK_SSL */
  1230. #ifdef NTLM
  1231. static int
  1232. ck_tn_auth_request_ntlm(int i)
  1233. {
  1234.     /* Microsoft's Telnet client won't perform authentication if */
  1235.     /* NTLM is not first.                                        */
  1236.     if ( ck_ntlm_is_valid(1) ) {
  1237.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1238.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1239.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1240.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1241.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1242.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1243.         {
  1244.             str_request[i++] = AUTHTYPE_NTLM;
  1245.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1246.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1247.             if ( deblog || tn_deb || debses )
  1248.                 ckstrncat(tn_msg,"NTLM CLIENT_TO_SERVER|ONE_WAY ",
  1249.                            TN_MSG_LEN);
  1250.             i++;
  1251.         }
  1252.     }
  1253.  
  1254.     return(i);
  1255. }
  1256. #endif /* NTLM */
  1257. int
  1258. #ifdef CK_ANSIC
  1259. ck_tn_auth_request(void)
  1260. #else
  1261. ck_tn_auth_request()
  1262. #endif
  1263. {
  1264.     int i = 4, rc = -1;
  1265.  
  1266. #ifdef CK_SSL
  1267.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1268.         return(0);
  1269.     }
  1270. #endif /* CK_SSL */
  1271.  
  1272.     if ( deblog || tn_deb || debses )
  1273.         strcpy(tn_msg,"TELNET SENT SB AUTHENTICATION SEND ");
  1274.  
  1275.     /* Create a list of acceptable Authentication types to send to */
  1276.     /* the client and let it choose find one that we support       */
  1277.  
  1278.     /* For those authentication methods that support Encryption or */
  1279.     /* Credentials Forwarding we must send all of the appropriate  */
  1280.     /* combinations based upon the state of                        */
  1281.     /* TELOPT_x_MODE(TELOPT_ENCRYPTION) and forward_flag.          */
  1282.  
  1283.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  1284. #ifdef GSSAPI_K5
  1285.         i = ck_tn_auth_request_gsskrb5(i);
  1286. #endif /* GSSAPI_K5 */
  1287. #ifdef KRB5
  1288.         i = ck_tn_auth_request_krb5(i);
  1289. #endif /* KRB5 */
  1290. #ifdef KRB4
  1291.         i = ck_tn_auth_request_krb4(i);
  1292. #endif /* KRB4 */
  1293. #ifdef CK_SRP
  1294.         i = ck_tn_auth_request_srp(i);
  1295. #endif /* SRP */
  1296. #ifdef CK_SSL
  1297.         i = ck_tn_auth_request_ssl(i);
  1298. #endif /* CK_SSL */
  1299. #ifdef NTLM
  1300.         i = ck_tn_auth_request_ntlm(i);
  1301. #endif /* NTLM */
  1302.     } else {
  1303.         int j;
  1304.         for ( j=0;
  1305.               j<AUTHTYPLSTSZ && auth_type_user[j] != AUTHTYPE_NULL;
  1306.               j++) {
  1307. #ifdef NTLM
  1308.             if (auth_type_user[j] == AUTHTYPE_NTLM)
  1309.                 i = ck_tn_auth_request_ntlm(i);
  1310. #endif /* NTLM */
  1311. #ifdef CK_SSL
  1312.             if ( auth_type_user[j] == AUTHTYPE_SSL )
  1313.                 i = ck_tn_auth_request_ssl(i);
  1314. #endif /* CK_SSL */
  1315. #ifdef CK_SRP
  1316.             if ( auth_type_user[j] == AUTHTYPE_SRP )
  1317.                 i = ck_tn_auth_request_srp(i);
  1318. #endif /* SRP */
  1319. #ifdef GSSAPI_K5
  1320.             if ( auth_type_user[j] == AUTHTYPE_GSSAPI_KRB5 )
  1321.                 i = ck_tn_auth_request_gsskrb5(i);
  1322. #endif /* GSSAPI_K5 */
  1323. #ifdef KRB5
  1324.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V5 )
  1325.                 i = ck_tn_auth_request_krb5(i);
  1326. #endif /* KRB5 */
  1327. #ifdef KRB4
  1328.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V4 )
  1329.                 i = ck_tn_auth_request_krb4(i);
  1330. #endif /* KRB4 */
  1331.         }
  1332.     }
  1333.  
  1334.     str_request[i++] = IAC;
  1335.     str_request[i++] = SE;
  1336.     if ( deblog || tn_deb || debses ) {
  1337.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1338.         debug(F100,tn_msg,"",0);
  1339.         if (tn_deb || debses) tn_debug(tn_msg);
  1340.     }
  1341.  
  1342.     /* Send data */
  1343. #ifdef OS2
  1344.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1345. #endif
  1346.     rc = ttol((CHAR *)str_request, i);
  1347. #ifdef OS2
  1348.     ReleaseTelnetMutex();
  1349. #endif
  1350.     if ( rc == i )
  1351.         return(0);
  1352.     else
  1353.         return(-1);
  1354. }
  1355.  
  1356. #ifdef CK_ENCRYPTION
  1357. VOID
  1358. ck_tn_enc_start()
  1359. {
  1360.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1361.         return;
  1362.     if (!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop &&
  1363.          (!encrypt_is_decrypting() || !encrypt_is_encrypting())) {
  1364.         debug(F110,"ck_tn_enc_start","nothing to do",0);
  1365.         return;
  1366.     }
  1367.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1368.     if (TELOPT_ME(TELOPT_ENCRYPTION) && !encrypt_is_encrypting()) {
  1369.         debug(F110,"ck_tn_enc_start","encrypt_request_start",0);
  1370.         encrypt_request_start();
  1371.     }
  1372.     if (TELOPT_U(TELOPT_ENCRYPTION) && !encrypt_is_decrypting()) {
  1373.         debug(F110,"ck_tn_enc_start","encrypt_send_request_start",0);
  1374.         encrypt_send_request_start();
  1375.     }
  1376.     tn_wait("encrypt start");
  1377.     tn_push();
  1378. }
  1379.  
  1380. VOID
  1381. ck_tn_enc_stop()
  1382. {
  1383.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1384.         return;
  1385.     if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop ||
  1386.          !(encrypt_is_decrypting() || encrypt_is_encrypting())) {
  1387.         debug(F110,"ck_tn_enc_stop","nothing to do",0);
  1388.       return;
  1389.     }
  1390.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1391.     if (TELOPT_U(TELOPT_ENCRYPTION) && encrypt_is_decrypting()) {
  1392.         debug(F110,"ck_tn_enc_stop","encrypt_send_request_end",0);
  1393.         encrypt_send_request_end();
  1394.     }
  1395.     if (TELOPT_ME(TELOPT_ENCRYPTION) && encrypt_is_encrypting()) {
  1396.         debug(F110,"ck_tn_enc_stop","encrypt_send_end",0);
  1397.         encrypt_send_end();
  1398.     }
  1399.     tn_wait("encrypt stop");
  1400.     tn_push();
  1401. }
  1402. #endif /* CK_ENCRYPTION */
  1403.  
  1404. /*  C K _ K R B _ T N _ S B _ A U T H
  1405.  *  An interface between the C-Kermit Telnet Command Parser and the Authent-
  1406.  *  ication option parser implemented in the Kerberos Telnet client.
  1407.  *
  1408.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1409.  *  len  - the length of the buffer
  1410.  *
  1411.  *  Returns: 0 on success and -1 on failure
  1412.  */
  1413.  
  1414. int
  1415. #ifdef CK_ANSIC
  1416. ck_tn_sb_auth(char * sb, int len)
  1417. #else /* CK_ANSIC */
  1418. ck_tn_sb_auth(sb,len) char * sb; int len;
  1419. #endif /* CK_ANSIC */
  1420. {
  1421.     /* auth_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1422.     /* and it wants the length to exclude the IAC SE bytes               */
  1423.     CHAR * buf;
  1424.     int rc = -1;
  1425.  
  1426.     buf = malloc(len-1);
  1427.     if ( !buf ) return(-1);
  1428.  
  1429.     buf[0] = SB;
  1430.     memcpy( &buf[1], sb, len-2 );
  1431.     rc = auth_parse(buf,len-1);
  1432.     free(buf);
  1433.     debug(F111,"ck_tn_sb_auth","rc",rc);
  1434.     if (rc == AUTH_FAILURE) {
  1435.         authentication_version = AUTHTYPE_NULL;
  1436. #ifndef NOLOCAL
  1437. #ifdef OS2
  1438.         ipadl25();
  1439. #endif /* OS2 */
  1440. #endif /* NOLOCAL */
  1441.         return(-1);
  1442.     }
  1443. #ifndef NOLOCAL
  1444. #ifdef OS2
  1445.     ipadl25();
  1446. #endif /* OS2 */
  1447. #endif /* NOLOCAL */
  1448.     return(0);
  1449. }
  1450.  
  1451. /*  C K _ K R B _ T N _ S B _ E N C R Y P T
  1452.  *  An interface between the C-Kermit Telnet Command Parser and the Encryption
  1453.  *  option parser implemented in the Kerberos Telnet client.
  1454.  *
  1455.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1456.  *  len  - the length of the buffer
  1457.  *
  1458.  *  Returns: Always returns 0 for success since encrypt_parse is void
  1459.  */
  1460.  
  1461.  
  1462. int
  1463. #ifdef CK_ANSIC
  1464. ck_tn_sb_encrypt(char * sb, int len)
  1465. #else
  1466. ck_tn_sb_encrypt(sb,len) char * sb; int len;
  1467. #endif /* CK_ANSIC */
  1468. {
  1469.     /* encrypt_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1470.     /* and it wants the length to exclude the IAC SE bytes                  */
  1471. #ifdef CK_ENCRYPTION
  1472.     char * buf;
  1473.     int rc = -1;
  1474.  
  1475.     buf = malloc(len-1);
  1476.     if ( !buf ) return(-1);
  1477.  
  1478.     buf[0] = SB;
  1479.     memcpy( &buf[1], sb, len-2 );
  1480.     rc = encrypt_parse(buf,len-1);
  1481.  
  1482.     if (rc < 0) {
  1483.         free(buf);
  1484.         return(-1);
  1485.     }
  1486.  
  1487.     /* This is a hack.  It does not belong here but should really be in */
  1488.     /* encrypt_parse() but in K95 the encrypt_parse() routine does not  */
  1489.     /* have access to the telopt_states array.                          */
  1490.     if ( buf[1] == ENCRYPT_REQEND )
  1491.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1492.     else if ( buf[1] == ENCRYPT_REQSTART )
  1493.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1494. #ifndef NOLOCAL
  1495. #ifdef OS2
  1496.     ipadl25();
  1497. #endif /* OS2 */
  1498. #endif /* NOLOCAL */
  1499.     free(buf);
  1500. #endif /* ENCRYPTION */
  1501.     return(0);
  1502. }
  1503.  
  1504.  
  1505. /*  C K _ K R B _ E N C R Y P T I N G
  1506.  *  Returns 1 if we are encrypting and 0 if we are not
  1507.  */
  1508.  
  1509. int
  1510. #ifdef CK_ANSIC
  1511. ck_tn_encrypting(VOID)
  1512. #else /* CK_ANSIC */
  1513. ck_tn_encrypting()
  1514. #endif /* CK_ANSIC */
  1515. {
  1516. #ifdef CK_ENCRYPTION
  1517.     if ( g_kstream == NULL )
  1518.         return(0);
  1519.     if ( g_kstream->encrypt && encrypt_is_encrypting()) {
  1520.         debug(F111,"ck_tn_encrypting","encrypting",
  1521.                g_kstream->encrypt_type);
  1522.         return(g_kstream->encrypt_type);
  1523.     }
  1524. #endif /* CK_ENCRYPTION */
  1525.     debug(F110,"ck_tn_encrypting","not encrypting",0);
  1526.     return(0);
  1527. }
  1528.  
  1529. /*  C K _ K R B _ D E C R Y P T I N G
  1530.  *  Returns 1 if we are decrypting and 0 if we are not
  1531.  */
  1532.  
  1533. int
  1534. #ifdef CK_ANSIC
  1535. ck_tn_decrypting(VOID)
  1536. #else
  1537. ck_tn_decrypting()
  1538. #endif /* CK_ANSIC */
  1539. {
  1540. #ifdef CK_ENCRYPTION
  1541.     if ( g_kstream == NULL )
  1542.         return(0);
  1543.     if ( g_kstream->decrypt && encrypt_is_decrypting()) {
  1544.         debug(F111,"ck_tn_decrypting","decrypting",
  1545.                g_kstream->decrypt_type);
  1546.         return(g_kstream->decrypt_type);
  1547.     }
  1548. #endif /* CK_ENCRYPTION */
  1549.     debug(F110,"ck_tn_decrypting","not decrypting",0);
  1550.     return(0);
  1551. }
  1552.  
  1553. /*  C K _ K R B _ A U T H E N T I C A T E D
  1554.  *  Returns the authentication type: AUTHTYPE_NULL, AUTHTYPE_KERBEROS4,
  1555.  *  or AUTHTYPE_KERBEROS5, AUTHTYPE_SRP, ... (see ckctel.h)
  1556.  */
  1557.  
  1558. int
  1559. #ifdef CK_ANSIC
  1560. ck_tn_authenticated(VOID)
  1561. #else
  1562. ck_tn_authenticated()
  1563. #endif
  1564. {
  1565.     return(authentication_version);
  1566. }
  1567.  
  1568. /*  C K _ K R B _ E N C R Y P T
  1569.  *  encrypts n characters in s if we are encrypting
  1570.  */
  1571.  
  1572. VOID
  1573. #ifdef CK_ANSIC
  1574. ck_tn_encrypt( char * s, int n )
  1575. #else
  1576. ck_tn_encrypt( s,n ) char * s; int n;
  1577. #endif
  1578. {
  1579. #ifdef CK_ENCRYPTION
  1580.     struct kstream_data_block i;
  1581.  
  1582.     if (g_kstream->encrypt && encrypt_is_encrypting()) {
  1583. #ifdef DEBUG
  1584.       hexdump("from plaintext", s, n);
  1585. #endif
  1586.         i.ptr = s;
  1587.         i.length = n;
  1588.         g_kstream->encrypt(&i, NULL);
  1589. #ifdef DEBUG
  1590.         hexdump("to cyphertext", s, n);
  1591. #endif
  1592.     }
  1593.     else debug(F101,"ck_tn_encrypt not encrypting","",n);
  1594. #endif /* ENCRYPTION */
  1595. }
  1596.  
  1597. /*  C K _ K R B _ D E C R Y P T
  1598.  *  decrypts n characters in s if we are decrypting
  1599.  */
  1600.  
  1601. VOID
  1602. #ifdef CK_ANSIC
  1603. ck_tn_decrypt( char * s, int n )
  1604. #else
  1605. ck_tn_decrypt( s,n ) char * s; int n;
  1606. #endif
  1607. {
  1608. #ifdef CK_ENCRYPTION
  1609.     struct kstream_data_block i;
  1610.  
  1611.     if (g_kstream->decrypt && encrypt_is_decrypting()) {
  1612.  
  1613. #ifdef DEBUG
  1614.         hexdump("from cyphertext", s, n);
  1615. #endif
  1616.  
  1617.         i.ptr = s;
  1618.         i.length = n;
  1619.         g_kstream->decrypt(&i, NULL);
  1620. #ifdef DEBUG
  1621.         hexdump("to plaintext", s, n);
  1622. #endif
  1623.     }
  1624.     else debug(F101,"ck_tn_decrypt not decrypting","",n);
  1625. #endif /* ENCRYPTION */
  1626. }
  1627.  
  1628. /*  S E N D K 5 A U T H S B
  1629.  *  Send a Kerberos 5 Authentication Subnegotiation to host and
  1630.  *  output appropriate Telnet Debug messages
  1631.  *
  1632.  *  type - Sub Negotiation type
  1633.  *  data - ptr to buffer containing data
  1634.  *  len  - len of buffer if not NUL terminated
  1635.  *
  1636.  *  returns number of characters sent or error value
  1637.  */
  1638.  
  1639. static int
  1640. #ifdef CK_ANSIC
  1641. SendK5AuthSB(int type, void *data, int len)
  1642. #else
  1643. SendK5AuthSB(type,data,len) int type; void *data; int len;
  1644. #endif
  1645. {
  1646.     int rc;
  1647.     unsigned char *p = str_data + 3;
  1648.     unsigned char *cd = (unsigned char *)data;
  1649.     extern int sstelnet;
  1650.  
  1651. #ifdef CK_SSL
  1652.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1653.         if (ttchk() < 0)
  1654.           return(0);
  1655.         else
  1656.           return(1);
  1657.     }
  1658. #endif /* CK_SSL */
  1659.  
  1660.     if ( type < 0 || type > 7 )         /* Check for invalid values */
  1661.         return(0);
  1662.  
  1663.     if (!cd) {
  1664.         cd = (unsigned char *)"";
  1665.         len = 0;
  1666.     }
  1667.  
  1668.     if (len == -1)                        /* Use strlen() for len */
  1669.         len = strlen((char *)cd);
  1670.  
  1671.     /* Construct Message */
  1672.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1673.     *p++ = AUTHTYPE_KERBEROS_V5;
  1674.     *p = AUTH_CLIENT_TO_SERVER;
  1675.     *p |= auth_how;
  1676. #ifdef CK_ENCRYPTION
  1677.     *p |= auth_crypt;
  1678. #endif
  1679. #ifdef USE_INI_CRED_FWD
  1680.     if (auth_fwd)
  1681.         *p |= INI_CRED_FWD_ON;
  1682. #endif /* USE_INI_CRED_FWD */
  1683.     p++;
  1684.     *p++ = type;
  1685.     while (len-- > 0) {
  1686.         if ((*p++ = *cd++) == IAC)
  1687.             *p++ = IAC;
  1688.     }
  1689.     *p++ = IAC;
  1690.     *p++ = SE;
  1691.  
  1692.     /* Handle Telnet Debugging Messages */
  1693.     if (deblog || tn_deb || debses) {
  1694.         int i;
  1695.         int deblen=p-str_data-2;
  1696.         char *s=NULL;
  1697.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1698.             auth_crypt
  1699. #ifdef USE_INI_CRED_FWD
  1700.               | (auth_fwd?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  1701. #endif /* USE_INI_CRED_FWD */
  1702.                     ;
  1703.  
  1704.         switch (type) {
  1705.         case 0:
  1706.             s = "AUTH";
  1707.             break;
  1708.         case 1:
  1709.             s = "REJECT";
  1710.             break;
  1711.         case 2:
  1712.             s = "ACCEPT";
  1713.             break;
  1714.         case 3:
  1715.             s = "RESPONSE";
  1716.             break;
  1717.         case 4:
  1718.             s = "FORWARD";
  1719.             break;
  1720.         case 5:
  1721.             s = "FORWARD_ACCEPT";
  1722.             break;
  1723.         case 6:
  1724.             s = "FORWARD_REJECT";
  1725.             break;
  1726.         case 7:
  1727.             s = "TLS_VERIFY";
  1728.                 break;
  1729.         }
  1730.  
  1731.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1732.                   "TELNET SENT SB ",
  1733.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1734.                  str_data[3] == TELQUAL_IS ? "IS" :
  1735.                  str_data[3] == TELQUAL_REPLY ? "REPLY" : "???"," ",
  1736.                  AUTHTYPE_NAME(authentication_version)," ",
  1737.                  AUTHMODE_NAME(mode)," ",
  1738.                  s," ",NULL);
  1739.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1740.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1741.         debug(F100,tn_msg,"",0);
  1742.         if (tn_deb || debses) tn_debug(tn_msg);
  1743.     }
  1744.  
  1745.     /* Send data */
  1746. #ifdef OS2
  1747.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1748. #endif
  1749.     rc = ttol((CHAR *)str_data, p - str_data);
  1750. #ifdef OS2
  1751.     ReleaseTelnetMutex();
  1752. #endif
  1753.     debug(F111,"SendK5AuthSB","ttol()",rc);
  1754.     return(rc);
  1755. }
  1756.  
  1757. /*  S E N D K 4 A U T H S B
  1758.  *  Send a Kerberos 4 Authentication Subnegotiation to host and
  1759.  *  output appropriate Telnet Debug messages
  1760.  *
  1761.  *  type - Sub Negotiation type
  1762.  *  data - ptr to buffer containing data
  1763.  *  len  - len of buffer if not NUL terminated
  1764.  *
  1765.  *  returns number of characters sent or error value
  1766.  */
  1767.  
  1768. static int
  1769. #ifdef CK_ANSIC
  1770. SendK4AuthSB(int type, void *data, int len)
  1771. #else
  1772. SendK4AuthSB(type,data,len) int type; void *data; int len;
  1773. #endif
  1774. {
  1775.     int rc;
  1776.     unsigned char *p = str_data + 3;
  1777.     unsigned char *cd = (unsigned char *)data;
  1778.     extern int sstelnet;
  1779.     int mode = (auth_how & AUTH_HOW_MASK) |
  1780.         auth_crypt;
  1781.  
  1782.     if ( type < 0 || type > 4 )         /* Check for invalid values */
  1783.         return(0);
  1784.  
  1785. #ifdef CK_SSL
  1786.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1787.         if (ttchk() < 0)
  1788.           return(0);
  1789.         else
  1790.           return(1);
  1791.     }
  1792. #endif /* CK_SSL */
  1793.  
  1794.     if (!cd) {
  1795.         cd = (unsigned char *)"";
  1796.         len = 0;
  1797.     }
  1798.  
  1799.     if (len == -1)                        /* Use strlen() for len */
  1800.         len = strlen((char *)cd);
  1801.  
  1802.  
  1803.     /* Construct Message */
  1804.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1805.     *p++ = AUTHTYPE_KERBEROS_V4;
  1806.     *p = AUTH_CLIENT_TO_SERVER;
  1807.     *p |= mode;
  1808.     p++;
  1809.     *p++ = type;
  1810.     while (len-- > 0) {
  1811.         if ((*p++ = *cd++) == IAC)
  1812.             *p++ = IAC;
  1813.         }
  1814.     *p++ = IAC;
  1815.     *p++ = SE;
  1816.  
  1817.     /* Handle Telnet Debugging Messages */
  1818.     if (deblog || tn_deb || debses) {
  1819.         int i;
  1820.         int deblen=p-str_data-2;
  1821.         char *s=NULL;
  1822.  
  1823.         switch (type) {
  1824.         case 0:
  1825.             s = "AUTH";
  1826.             break;
  1827.         case 1:
  1828.             s = "REJECT";
  1829.             break;
  1830.         case 2:
  1831.             s = "ACCEPT";
  1832.             break;
  1833.         case 3:
  1834.             s = "CHALLENGE";
  1835.             break;
  1836.         case 4:
  1837.             s = "RESPONSE";
  1838.             break;
  1839.         }
  1840.  
  1841.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  1842.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1843.                  str_data[3] == TELQUAL_IS ? "IS" :
  1844.                  (str_data[3] == TELQUAL_REPLY ? "REPLY" : "???")," ",
  1845.                  AUTHTYPE_NAME(authentication_version)," ",
  1846.                  AUTHMODE_NAME(mode)," ",
  1847.                  s," ",NULL);
  1848.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1849.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1850.         debug(F100,tn_msg,"",0);
  1851.         if (tn_deb || debses) tn_debug(tn_msg);
  1852.     }
  1853.  
  1854.     /* Send data */
  1855. #ifdef OS2
  1856.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1857. #endif
  1858.     rc = ttol((CHAR *)str_data, p - str_data);
  1859. #ifdef OS2
  1860.     ReleaseTelnetMutex();
  1861. #endif
  1862.     debug(F111,"SendK4AuthSB","ttol()",rc);
  1863.     return(rc);
  1864. }
  1865.  
  1866. /*  S E N D S R P A U T H S B
  1867.  *  Send a SRP Authentication Subnegotiation to host and
  1868.  *  output appropriate Telnet Debug messages
  1869.  *
  1870.  *  type - Sub Negotiation type
  1871.  *  data - ptr to buffer containing data
  1872.  *  len  - len of buffer if not NUL terminated
  1873.  *
  1874.  *  returns number of characters sent or error value
  1875.  */
  1876.  
  1877. static int
  1878. #ifdef CK_ANSIC
  1879. SendSRPAuthSB(int type, void *data, int len)
  1880. #else
  1881. SendSRPAuthSB(type,data,len) int type; void *data; int len;
  1882. #endif
  1883. {
  1884.     int rc;
  1885.     unsigned char *p = str_data + 3;
  1886.     unsigned char *cd = (unsigned char *)data;
  1887.     extern int sstelnet;
  1888.  
  1889.     /* Check for invalid values */
  1890.     if ( type != SRP_EXP && type != SRP_RESPONSE &&
  1891.          type != SRP_REJECT && type != SRP_ACCEPT &&
  1892.          type != SRP_CHALLENGE && type != SRP_PARAMS &&
  1893.          type != SRP_AUTH)
  1894.         return(0);
  1895.  
  1896.     if (len == -1)                        /* Use strlen() for len */
  1897.         len = strlen((char *)cd);
  1898.  
  1899.     /* Construct Message */
  1900.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1901.     *p++ = AUTHTYPE_SRP;
  1902.     *p = AUTH_CLIENT_TO_SERVER;
  1903.     *p |= auth_how;
  1904. #ifdef CK_ENCRYPTION
  1905.     *p |= auth_crypt;
  1906. #endif
  1907.     p++;
  1908.     *p++ = type;
  1909.     while (len-- > 0) {
  1910.         if ((*p++ = *cd++) == IAC)
  1911.             *p++ = IAC;
  1912.         }
  1913.     *p++ = IAC;
  1914.     *p++ = SE;
  1915.  
  1916.     /* Handle Telnet Debugging Messages */
  1917.     if (deblog || tn_deb || debses) {
  1918.         int i;
  1919.         int deblen=p-str_data-2;
  1920.         char *s=NULL;
  1921.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1922.             auth_crypt;
  1923.  
  1924.         switch (type) {
  1925.         case 0:
  1926.             s = "AUTH";
  1927.             break;
  1928.         case 1:
  1929.             s = "REJECT";
  1930.             break;
  1931.         case 2:
  1932.             s = "ACCEPT";
  1933.             break;
  1934.         case 3:
  1935.             s = "CHALLENGE";
  1936.             break;
  1937.         case 4:
  1938.             s = "RESPONSE";
  1939.             break;
  1940.         case 5:
  1941.             s = "FORWARD";
  1942.             break;
  1943.         case 6:
  1944.             s = "FORWARD_ACCEPT";
  1945.             break;
  1946.         case 7:
  1947.             s = "FORWARD_REJECT";
  1948.             break;
  1949.         case 8:
  1950.             s = "EXP";
  1951.             break;
  1952.         case 9:
  1953.             s = "PARAMS";
  1954.             break;
  1955.         }
  1956.  
  1957.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1958.                   "TELNET SENT SB ",
  1959.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1960.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  1961.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  1962.                  AUTHTYPE_NAME(authentication_version)," ",
  1963.                  AUTHMODE_NAME(mode)," ",
  1964.                  s," ",NULL);
  1965.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1966.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1967.         debug(F100,tn_msg,"",0);
  1968.         if (tn_deb || debses) tn_debug(tn_msg);
  1969.     }
  1970.  
  1971.     /* Send data */
  1972. #ifdef OS2
  1973.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1974. #endif
  1975.     rc = ttol((CHAR *)str_data, p - str_data);
  1976. #ifdef OS2
  1977.     ReleaseTelnetMutex();
  1978. #endif
  1979.     return(rc);
  1980. }
  1981.  
  1982. #ifdef CK_ENCRYPTION
  1983. /*
  1984.  * Function: Enable or disable the encryption process.
  1985.  *
  1986.  * Parameters:
  1987.  *      enable - TRUE to enable, FALSE to disable.
  1988.  */
  1989. static VOID
  1990. #ifdef CK_ANSIC
  1991. auth_encrypt_enable(BOOL enable)
  1992. #else
  1993. auth_encrypt_enable(enable) BOOL enable;
  1994. #endif
  1995. {
  1996.   encrypt_flag = enable;
  1997. }
  1998. #endif
  1999.  
  2000. /*
  2001.  * Function: Abort the authentication process
  2002.  *
  2003.  * Parameters:
  2004.  */
  2005. static VOID
  2006. #ifdef CK_ANSIC
  2007. auth_abort(char *errmsg, long r)
  2008. #else
  2009. auth_abort(errmsg,r) char *errmsg; long r;
  2010. #endif
  2011. {
  2012.     char buf[9];
  2013.     extern int sstelnet;
  2014.  
  2015. #ifdef CK_SSL
  2016.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2017.         return;
  2018.     }
  2019. #endif /* CK_SSL */
  2020.     debug(F111,"auth_abort",errmsg,r);
  2021.  
  2022.     /* Construct Telnet Debugging messages */
  2023.     if (deblog || tn_deb || debses) {
  2024.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2025.                   "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2026.                   " IS ",AUTHTYPE_NAME(AUTHTYPE_NULL)," ",
  2027.                    AUTHTYPE_NAME(AUTHTYPE_NULL)," IAC SE",
  2028.                    NULL,NULL,NULL,NULL,NULL
  2029.                  );
  2030.         debug(F100,tn_msg,"",0);
  2031.         if (tn_deb || debses) tn_debug(tn_msg);
  2032.     }
  2033.  
  2034.     /* Construct the Abort message to send to the host   */
  2035.     /* Basicly we change the authentication type to NULL */
  2036.     sprintf(buf, "%c%c%c%c%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2037.              sstelnet ? TELQUAL_REPLY : TELQUAL_IS, AUTHTYPE_NULL,
  2038.              AUTHTYPE_NULL, IAC, SE);   /* safe */
  2039. #ifdef OS2
  2040.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2041. #endif
  2042.     ttol((CHAR *)buf, 8);
  2043. #ifdef OS2
  2044.     ReleaseTelnetMutex();
  2045. #endif
  2046.  
  2047.     /* If there is an error message, and error number construct */
  2048.     /* an explanation to display to the user                    */
  2049.     if (errmsg != NULL) {
  2050.         ckstrncpy(strTmp, errmsg, AUTHTMPBL);
  2051.     } else
  2052.         strTmp[0] = '\0';
  2053.  
  2054.  
  2055.     if (r != AUTH_SUCCESS) {
  2056.         ckstrncat(strTmp, "\r\n",AUTHTMPBL);
  2057. #ifdef KRB4
  2058.         if ( authentication_version == AUTHTYPE_KERBEROS_V4 ) {
  2059.             ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),
  2060.                        AUTHTMPBL);
  2061.             debug(F111,"auth_abort",(char *)krb_get_err_text_entry(r),r);
  2062.         }
  2063. #endif
  2064. #ifdef KRB5
  2065.         if ( authentication_version == AUTHTYPE_KERBEROS_V5 ) {
  2066.             ckstrncat(strTmp, error_message(r),AUTHTMPBL);
  2067.             debug(F111,"auth_abort",error_message(r),r);
  2068.         }
  2069. #endif
  2070.     }
  2071.     printf("Authentication failed: %s\r\n",strTmp);
  2072. #ifdef CKSYSLOG
  2073.     if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  2074.         cksyslog(SYSLG_LI, 0, "Telnet authentication failure",
  2075.                   (char *) szUserNameRequested,
  2076.                   strTmp);
  2077.     }
  2078. #endif /* CKSYSLOG */
  2079.     authentication_version = AUTHTYPE_NULL;
  2080. }
  2081.  
  2082.  
  2083. /*
  2084.  * Function: Copy data to buffer, doubling IAC character if present.
  2085.  *
  2086.  */
  2087. int
  2088. #ifdef CK_ANSIC
  2089. copy_for_net(unsigned char *to, unsigned char *from, int c)
  2090. #else
  2091. copy_for_net(to,from,c) unsigned char *to; unsigned char *from; int c;
  2092. #endif
  2093. {
  2094.     int n;
  2095.  
  2096.     n = c;
  2097.     debug(F111,"copy_for_net","before",n);
  2098.     while (c-- > 0) {
  2099.         if ((*to++ = *from++) == IAC) {
  2100.             n++;
  2101.             *to++ = IAC;
  2102.         }
  2103.     }
  2104.     debug(F111,"copy_for_net","after",n);
  2105.     return n;
  2106. }
  2107.  
  2108. #ifdef CK_SSL
  2109. /*  S E N D S S L A U T H S B
  2110.  *  Send a SSL Authentication Subnegotiation to host and
  2111.  *  output appropriate Telnet Debug messages
  2112.  *
  2113.  *  type - Sub Negotiation type
  2114.  *  data - ptr to buffer containing data
  2115.  *  len  - len of buffer if not NUL terminated
  2116.  *
  2117.  *  returns number of characters sent or error value
  2118.  */
  2119.  
  2120. int
  2121. #ifdef CK_ANSIC
  2122. SendSSLAuthSB(int type, void *data, int len)
  2123. #else
  2124. SendSSLAuthSB(type,data,len) int type; void *data; int len;
  2125. #endif
  2126. {
  2127.     int rc;
  2128.     unsigned char *p = str_data + 3;
  2129.     unsigned char *cd = (unsigned char *)data;
  2130.     extern int sstelnet;
  2131.  
  2132.     /* Check for invalid values */
  2133.     if ( type != SSL_START && type != SSL_ACCEPT &&
  2134.          type != SSL_REJECT)
  2135.         return(0);
  2136.  
  2137.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2138.         if (ttchk() < 0)
  2139.           return(0);
  2140.         else
  2141.           return(1);
  2142.     }
  2143.  
  2144.     if (len == -1)                        /* Use strlen() for len */
  2145.         len = strlen((char *)cd);
  2146.  
  2147.     /* Construct Message */
  2148.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  2149.     *p++ = AUTHTYPE_SSL;
  2150.     *p = AUTH_CLIENT_TO_SERVER;
  2151.     *p |= auth_how;
  2152. #ifdef CK_ENCRYPTION
  2153.     *p |= auth_crypt;
  2154. #endif
  2155.     p++;
  2156.     *p++ = type;
  2157.     while (len-- > 0) {
  2158.         if ((*p++ = *cd++) == IAC)
  2159.             *p++ = IAC;
  2160.         }
  2161.     *p++ = IAC;
  2162.     *p++ = SE;
  2163.  
  2164.     /* Handle Telnet Debugging Messages */
  2165.     if (deblog || tn_deb || debses) {
  2166.         int i;
  2167.         int deblen=p-str_data-2;
  2168.         char *s=NULL;
  2169.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  2170.             (auth_crypt?AUTH_ENCRYPT_USING_TELOPT:AUTH_ENCRYPT_OFF);
  2171.  
  2172.         switch (type) {
  2173.         case SSL_START:
  2174.             s = "START";
  2175.             break;
  2176.         case SSL_ACCEPT:
  2177.             s = "ACCEPT";
  2178.             break;
  2179.         case SSL_REJECT:
  2180.             s = "REJECT";
  2181.             break;
  2182.         }
  2183.  
  2184.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2185.                   "TELNET SENT SB ",
  2186.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  2187.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  2188.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  2189.                  AUTHTYPE_NAME(authentication_version)," ",
  2190.                  AUTHMODE_NAME(mode)," ",
  2191.                  s," ",NULL);
  2192.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  2193.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2194.         debug(F100,tn_msg,"",0);
  2195.         if (tn_deb || debses) tn_debug(tn_msg);
  2196.     }
  2197.  
  2198.     /* Send data */
  2199. #ifdef OS2
  2200.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2201. #endif
  2202.     rc = ttol((CHAR *)str_data, p - str_data);
  2203. #ifdef OS2
  2204.     ReleaseTelnetMutex();
  2205. #endif
  2206.     return(rc);
  2207. }
  2208. #endif  /* CK_SSL */
  2209.  
  2210. int
  2211. tn_how_ok(int how)
  2212. {
  2213.     switch ( tn_auth_how ) {
  2214.     case TN_AUTH_HOW_ANY:
  2215.         return(1);
  2216.     case TN_AUTH_HOW_ONE_WAY:
  2217.         return((how & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY);
  2218.     case TN_AUTH_HOW_MUTUAL:
  2219.         return((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL);
  2220.     default:
  2221.         return(0);
  2222.     }
  2223. }
  2224.  
  2225. int
  2226. tn_enc_ok(int enc)
  2227. {
  2228.     switch ( tn_auth_enc ) {
  2229.     case TN_AUTH_ENC_ANY:
  2230.         if ((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  2231.             (!ck_ssleay_is_installed()
  2232. #ifdef CK_SSL
  2233.              || !ssl_finished_messages ||
  2234.              !(tls_active_flag || ssl_active_flag)
  2235. #endif /* CK_SSL */
  2236.              )) {
  2237. #ifdef CK_SSL
  2238.             if (!ssl_finished_messages)
  2239.                 debug(F100,"tn_enc_ok !ssl_finished_messages","",0);
  2240. #endif /* CK_SSL */
  2241.             return(0);
  2242.         }
  2243.         return(1);
  2244.     case TN_AUTH_ENC_NONE:
  2245.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_OFF);
  2246.     case TN_AUTH_ENC_TELOPT:
  2247.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT);
  2248.     case TN_AUTH_ENC_EXCH:
  2249.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_AFTER_EXCHANGE);
  2250.     case TN_AUTH_ENC_TLS:
  2251.         return(((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) &&
  2252.                ck_ssleay_is_installed()
  2253. #ifdef CK_SSL
  2254.                && ssl_finished_messages &&
  2255.                (tls_active_flag || ssl_active_flag)
  2256. #endif /* CK_SSL */
  2257.            );
  2258.     default:
  2259.         return(0);
  2260.     }
  2261. }
  2262.  
  2263. static int
  2264. atok(int at) {
  2265.     int i;
  2266.     if ( auth_type_user[0] == AUTHTYPE_AUTO )
  2267.         return(1);
  2268.     if ( auth_type_user[0] == AUTHTYPE_NULL )
  2269.         return(0);
  2270.  
  2271.     for ( i=0;
  2272.           i<AUTHTYPLSTSZ && auth_type_user[i] != AUTHTYPE_NULL;
  2273.           i++ ) {
  2274.         if ( auth_type_user[i] == at )
  2275.             return(1);
  2276.     }
  2277.  
  2278.     return(0);
  2279. }
  2280.  
  2281.  
  2282. /*
  2283.  * Function: Parse authentication send command
  2284.  *
  2285.  * Parameters:
  2286.  *  parsedat - the sub-command data.
  2287.  *
  2288.  *      end_sub - index of the character in the 'parsedat' array which
  2289.  *              is the last byte in a sub-negotiation
  2290.  *
  2291.  * Returns: Kerberos error code.
  2292.  */
  2293.  
  2294. static unsigned char send_list[512];
  2295. static int  send_len = 0;
  2296.  
  2297. _PROTOTYP(static int auth_send, (unsigned char *parsedat, int end_sub));
  2298.  
  2299. static int
  2300. #ifdef CK_ANSIC
  2301. auth_resend(int type)
  2302. #else
  2303. auth_resend(type) int type;
  2304. #endif /* CK_ANSIC */
  2305. {
  2306.     int i=2;
  2307.     while (i+1 <= send_len) {
  2308.         if (send_list[i] == type) {
  2309.             int j;
  2310.             send_len -= 2;
  2311.             for (j = i; j < send_len; j++)
  2312.                 send_list[j] = send_list[j+2];
  2313.         } else {
  2314.             i += 2;
  2315.         }
  2316.     }
  2317.     return(auth_send(send_list,send_len));
  2318. }
  2319.  
  2320. static int
  2321. #ifdef CK_ANSIC
  2322. auth_send(unsigned char *parsedat, int end_sub)
  2323. #else
  2324. auth_send(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  2325. #endif
  2326. {
  2327.     static unsigned char buf[4096];
  2328.     unsigned char *pname;
  2329.     int plen;
  2330.     int r;
  2331.     int i;
  2332.     int mode;
  2333. #ifdef MIT_CURRENT
  2334. #ifdef CK_ENCRYPTION
  2335.     krb5_data data;
  2336.     krb5_enc_data encdata;
  2337.     krb5_error_code code;
  2338.     krb5_keyblock random_key;
  2339. #endif /* ENCRYPTION */
  2340. #endif /* MIT_CURRENT */
  2341. #ifdef KRB5
  2342.     int krb5_msg = 0;
  2343. #endif /* KRB5 */
  2344. #ifdef KRB4
  2345.     int krb4_msg = 0;
  2346. #endif /* KRB4 */
  2347. #ifdef GSSAPI_KRB5
  2348.     int gssk5_msg = 0;
  2349. #endif /* GSSAPI_KRB5 */
  2350.     int iaccnt=0;
  2351.  
  2352. #ifdef CK_SSL
  2353.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2354.         return(AUTH_SUCCESS);
  2355. #endif /* CK_SSL */
  2356.  
  2357.     auth_how = -1;              /* We have not found an auth method  */
  2358.     auth_crypt = 0;             /* We are not using encryption (yet) */
  2359.     send_len = end_sub > 512 ? 512 : end_sub;
  2360.     memcpy(send_list,parsedat,send_len);
  2361.  
  2362.     /* Search the list of acceptable Authentication types sent from */
  2363.     /* the host and find one that we support                        */
  2364.  
  2365.     /* For Kerberos authentications, try to determine if we have a  */
  2366.     /* valid TGT, if not skip over the authentication type because  */
  2367.     /* we wouldn't be able to successfully login anyway.  Perhaps   */
  2368.     /* there is another supported authentication which we could use */
  2369.  
  2370. #ifdef NO_FTP_AUTH
  2371.     /* If the userid is "ftp" or "anonymous" refuse to perform AUTH */
  2372.     /* for Kerberos or SRP.                                         */
  2373. #endif /* NO_FTP_AUTH */
  2374.  
  2375.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  2376.     for (i = 2; i+1 <= end_sub; i += 2) {
  2377. #ifdef NTLM
  2378.         if (parsedat[i] == AUTHTYPE_NTLM &&
  2379.              ck_ntlm_is_valid(1) &&
  2380.              ntlm_auth_send() == 0) {
  2381.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2382.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2383. #ifdef CK_ENCRYPTION
  2384.                 /* NTLM does not support Telnet Encryption */
  2385.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2386.                     continue;
  2387.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2388. #endif /* CK_ENCRYPTION */
  2389.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2390.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2391.                 authentication_version = AUTHTYPE_NTLM;
  2392.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2393.                 break;
  2394.             }
  2395.         }
  2396. #endif /* NTLM */
  2397. #ifdef CK_SSL
  2398.         if ( parsedat[i] == AUTHTYPE_SSL && ssl_initialized &&
  2399. #ifdef SSLDLL
  2400.              ck_ssleay_is_installed() &&
  2401. #endif /* SSLDLL */
  2402.              !tls_active_flag && !ssl_active_flag
  2403. #ifndef USE_CERT_CB
  2404.              && tls_load_certs(ssl_ctx,ssl_con,0)
  2405. #endif /* USE_CERT_CB */
  2406.              ) {
  2407.  
  2408.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2409.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2410. #ifdef CK_ENCRYPTION
  2411.                 /* SSL does not support Telnet Encryption */
  2412.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2413.                     continue;
  2414.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2415. #endif /* CK_ENCRYPTION */
  2416.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2417.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2418.                 authentication_version = AUTHTYPE_SSL;
  2419.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2420.                 break;
  2421.             }
  2422.         }
  2423. #endif /* SSL */
  2424. #ifdef CK_SRP
  2425.         if ( parsedat[i] == AUTHTYPE_SRP
  2426. #ifdef SRPDLL
  2427.              && hSRP
  2428. #endif /* SRPDLL */
  2429. #ifdef NO_FTP_AUTH
  2430.              && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2431. #endif /* NO_FTP_AUTH */
  2432.              ) {
  2433.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2434.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2435. #ifdef PRE_SRP_1_4_5
  2436.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2437.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2438.                     continue;
  2439. #endif /* PRE_SRP_1_4_5 */
  2440.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2441.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2442.                     (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2443.                      TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2444.                     continue;
  2445.  
  2446.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2447. #ifdef CK_ENCRYPTION
  2448.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2449.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2450.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2451.                 }
  2452. #endif /* CK_ENCRYPTION */
  2453. #ifdef CK_SSL
  2454.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2455.                      ck_ssleay_is_installed() &&
  2456.                      (tls_active_flag || ssl_active_flag) ) {
  2457.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2458.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2459.                 }
  2460. #endif /* CK_SSL */
  2461.                 authentication_version = AUTHTYPE_SRP;
  2462.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2463.                 break;
  2464.             }
  2465.         }
  2466. #endif /* SRP */
  2467. #ifdef GSSAPI_KRB5
  2468.         if (parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2469.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2470.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2471. #ifdef OS2
  2472.             hGSSAPI &&
  2473. #endif /* OS2 */
  2474. #ifdef NO_FTP_AUTH
  2475.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2476. #endif /* NO_FTP_AUTH */
  2477.              ck_gssapi_is_installed() && !gssk5_msg)
  2478.         {
  2479.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2480.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2481.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2482.             {
  2483.                 /* If we are auto-getting TGTs, try */
  2484.                 if ( !ck_krb5_is_tgt_valid() ) {
  2485.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2486.                 }
  2487.                 gssk5_msg = 1;
  2488.             }
  2489.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2490.                       AUTH_CLIENT_TO_SERVER &&
  2491.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2492.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2493. #ifdef CK_ENCRYPTION
  2494.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2495.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2496.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2497.                 }
  2498. #endif /* CK_ENCRYPTION */
  2499.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2500.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2501.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2502.                 break;
  2503.             }
  2504.         }
  2505. #endif /* GSSAPI_KRB5 */
  2506. #ifdef KRB5
  2507.         if (parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2508. #ifdef OS2
  2509.              hKRB5_32 &&
  2510. #endif /* OS2 */
  2511. #ifdef NO_FTP_AUTH
  2512.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2513. #endif /* NO_FTP_AUTH */
  2514.              ck_krb5_is_installed() && !krb5_msg) {
  2515.  
  2516.             /* Without encryption we can't perform mutual authentication */
  2517.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2518.                  !ck_crypt_is_installed())
  2519.                 continue;
  2520.  
  2521.             /* Skip over entries that request credential forwarding */
  2522.             /* if we are not forwarding.                            */
  2523.             if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2524.                 (forward_flag &&
  2525.                   ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2526.                 continue;
  2527.  
  2528.             if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2529.                                 parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2530.                                 parsedat[i+1] & INI_CRED_FWD_MASK) )
  2531.             {
  2532.                 /* If we are auto-getting TGTs, try */
  2533.                 if ( !ck_krb5_is_tgt_valid() ) {
  2534.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2535.                 }
  2536.                 krb5_msg = 1;
  2537.             }
  2538.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2539.                       AUTH_CLIENT_TO_SERVER &&
  2540.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2541.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2542.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2543.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2544.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2545.                     continue;
  2546.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2547.                      AUTH_ENCRYPT_START_TLS) &&
  2548.                      (!ck_ssleay_is_installed()
  2549. #ifdef CK_SSL
  2550.                        || !(tls_active_flag || ssl_active_flag)
  2551. #endif /* CK_SSL */
  2552.                        ))
  2553.                     continue;
  2554.  
  2555.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2556. #ifdef CK_ENCRYPTION
  2557.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2558.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2559.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2560.                 }
  2561. #endif /* CK_ENCRYPTION */
  2562. #ifdef CK_SSL
  2563.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2564.                      ck_ssleay_is_installed() &&
  2565.                      (tls_active_flag || ssl_active_flag) ) {
  2566.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2567.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2568.                 }
  2569. #endif /* CK_SSL */
  2570.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2571.                 authentication_version = AUTHTYPE_KERBEROS_V5;
  2572.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2573.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2574.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2575.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2576.                 }
  2577.                 break;
  2578.             }
  2579.         }
  2580. #endif /* KRB5 */
  2581. #ifdef KRB4
  2582.         if (parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2583. #ifdef OS2
  2584.              hKRB4_32 &&
  2585. #endif /* OS2 */
  2586. #ifdef NO_FTP_AUTH
  2587.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2588. #endif /* NO_FTP_AUTH */
  2589.              ck_krb4_is_installed() && !krb4_msg) {
  2590.             int rc = 0;
  2591.  
  2592.             /* Without encryption we can't perform mutual authentication */
  2593.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2594.                  !ck_crypt_is_installed() )
  2595.                 continue;
  2596.  
  2597.             if ( !k4_auth_send() )
  2598.             {
  2599.                 /* If we are auto-getting TGTs, try */
  2600.                 if ( !ck_krb4_is_tgt_valid() ) {
  2601.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2602.                 }
  2603.                 krb4_msg = 1;
  2604.             }
  2605.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2606.                       AUTH_CLIENT_TO_SERVER &&
  2607.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2608. #ifdef CK_ENCRYPTION
  2609.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2610.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2611.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2612.                     continue;
  2613.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2614.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2615.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2616.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2617.                 }
  2618. #endif /* CK_ENCRYPTION */
  2619.                 authentication_version = AUTHTYPE_KERBEROS_V4;
  2620.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2621.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2622.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2623.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2624.                 }
  2625.                 break;
  2626.             }
  2627.         }
  2628. #endif /* KRB4 */
  2629.     }
  2630.     } else {
  2631.         for (i = 2; i+1 <= end_sub; i += 2) {
  2632. #ifdef CK_SSL
  2633.             if ( atok(AUTHTYPE_SSL) && parsedat[i] == AUTHTYPE_SSL &&
  2634. #ifdef SSLDLL
  2635.                  ck_ssleay_is_installed() &&
  2636. #endif /* SSLDLL */
  2637.                  !tls_active_flag && !ssl_active_flag && ssl_initialized
  2638. #ifndef USE_CERT_CB
  2639.                  && tls_load_certs(ssl_ctx,ssl_con,0)
  2640. #endif /* USE_CERT_CB */
  2641.                  )
  2642.             {
  2643.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2644.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2645. #ifdef CK_ENCRYPTION
  2646.                     /* SSL does not support Telnet Encryption */
  2647.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2648.                         continue;
  2649.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2650. #endif /* CK_ENCRYPTION */
  2651.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2652.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2653.                     authentication_version = AUTHTYPE_SSL;
  2654.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2655.                     break;
  2656.                 }
  2657.             }
  2658. #endif /* SSL */
  2659. #ifdef CK_SRP
  2660.             if ( atok(AUTHTYPE_SRP) &&
  2661.                  parsedat[i] == AUTHTYPE_SRP
  2662. #ifdef SRPDLL
  2663.                  && hSRP
  2664. #endif /* SRPDLL */
  2665. #ifdef NO_FTP_AUTH
  2666.                  && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2667. #endif /* NO_FTP_AUTH */
  2668.                  ) {
  2669.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2670.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2671. #ifdef PRE_SRP_1_4_5
  2672.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2673.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2674.                     continue;
  2675. #endif /* PRE_SRP_1_4_5 */
  2676.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2677.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2678.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2679.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2680.                     continue;
  2681.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2682.                      AUTH_ENCRYPT_START_TLS) &&
  2683.                      (!ck_ssleay_is_installed()
  2684. #ifdef CK_SSL
  2685.                        || !(tls_active_flag || ssl_active_flag)
  2686. #endif /* CK_SSL */
  2687.                        ))
  2688.                     continue;
  2689.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2690. #ifdef CK_ENCRYPTION
  2691.                     if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2692.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2693.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2694.                     }
  2695. #endif /* CK_ENCRYPTION */
  2696. #ifdef CK_SSL
  2697.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2698.                      ck_ssleay_is_installed() &&
  2699.                      (tls_active_flag || ssl_active_flag) ) {
  2700.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2701.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2702.                 }
  2703. #endif /* CK_SSL */
  2704.                     authentication_version = AUTHTYPE_SRP;
  2705.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2706.                     break;
  2707.                 }
  2708.             }
  2709. #endif /* SRP */
  2710. #ifdef GSSAPI_KRB5
  2711.         if (atok(AUTHTYPE_GSSAPI_KRB5) &&
  2712.             parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2713.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2714.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2715. #ifdef OS2
  2716.             hGSSAPI &&
  2717. #endif /* OS2 */
  2718. #ifdef NO_FTP_AUTH
  2719.             strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2720. #endif /* NO_FTP_AUTH */
  2721.             ck_gssapi_is_installed() && !gssk5_msg)
  2722.         {
  2723.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2724.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2725.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2726.             {
  2727.                 /* If we are auto-getting TGTs, try */
  2728.                 if ( !ck_krb5_is_tgt_valid() ) {
  2729.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2730.                 }
  2731.                 gssk5_msg = 1;
  2732.             }
  2733.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2734.                       AUTH_CLIENT_TO_SERVER &&
  2735.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2736.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2737. #ifdef CK_ENCRYPTION
  2738.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2739.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2740.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2741.                 }
  2742. #endif /* CK_ENCRYPTION */
  2743.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2744.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2745.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2746.                 break;
  2747.             }
  2748.         }
  2749. #endif /* GSSAPI_KRB5 */
  2750. #ifdef KRB5
  2751.             if ( atok(AUTHTYPE_KERBEROS_V5) &&
  2752.                  parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2753. #ifdef OS2
  2754.                  hKRB5_32 &&
  2755. #endif /* OS2 */
  2756. #ifdef NO_FTP_AUTH
  2757.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2758. #endif /* NO_FTP_AUTH */
  2759.                  ck_krb5_is_installed() && !krb5_msg) {
  2760.  
  2761.                 /* Without encryption we can't perform mutual authentication */
  2762.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2763.                      !ck_crypt_is_installed())
  2764.                     continue;
  2765.  
  2766.                 /* Skip over entries that request credential forwarding */
  2767.                 /* if we are not forwarding.                            */
  2768.                 if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2769.                      (forward_flag &&
  2770.                        ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2771.                     continue;
  2772.  
  2773.                 if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2774.                                     parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2775.                                     parsedat[i+1] & INI_CRED_FWD_MASK) )
  2776.                 {
  2777.                     /* If we are auto-getting TGTs, try */
  2778.                     if ( !ck_krb5_is_tgt_valid() ) {
  2779.                         printf(
  2780.                            "Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2781.                     }
  2782.                     krb5_msg = 1;
  2783.                 }
  2784.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2785.                           AUTH_CLIENT_TO_SERVER &&
  2786.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2787.                 {
  2788.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2789.                          AUTH_ENCRYPT_USING_TELOPT) &&
  2790.                          (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2791.                            TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2792.                         continue;
  2793.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2794.                          AUTH_ENCRYPT_START_TLS) &&
  2795.                          (!ck_ssleay_is_installed()
  2796. #ifdef CK_SSL
  2797.                            || !(tls_active_flag || ssl_active_flag)
  2798. #endif /* CK_SSL */
  2799.                            ))
  2800.                         continue;
  2801.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2802. #ifdef CK_ENCRYPTION
  2803.                     if (auth_crypt) {
  2804.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2805.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2806.                     }
  2807. #endif /* CK_ENCRYPTION */
  2808. #ifdef CK_SSL
  2809.                     if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2810.                          ck_ssleay_is_installed() &&
  2811.                          (tls_active_flag || ssl_active_flag) ) {
  2812.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2813.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2814.                     }
  2815. #endif /* CK_SSL */
  2816.                     authentication_version = AUTHTYPE_KERBEROS_V5;
  2817.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2818.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2819.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2820.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2821.                     }
  2822.                     break;
  2823.                 }
  2824.             }
  2825. #endif /* KRB5 */
  2826. #ifdef KRB4
  2827.             if ( atok(AUTHTYPE_KERBEROS_V4) &&
  2828.                  parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2829. #ifdef OS2
  2830.                  hKRB4_32 &&
  2831. #endif /* OS2 */
  2832. #ifdef NO_FTP_AUTH
  2833.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2834. #endif /* NO_FTP_AUTH */
  2835.                  ck_krb4_is_installed() && !krb4_msg) {
  2836.                 int rc = 0;
  2837.  
  2838.                 /* Without encryption we can't perform mutual authentication */
  2839.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2840.                      !ck_crypt_is_installed())
  2841.                     continue;
  2842.  
  2843.                 if ( !k4_auth_send() )
  2844.                 {
  2845.                     /* If we are auto-getting TGTs, try */
  2846.                     if ( !ck_krb4_is_tgt_valid() ) {
  2847.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2848.                     }
  2849.                     krb4_msg = 1;
  2850.                 }
  2851.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2852.                           AUTH_CLIENT_TO_SERVER &&
  2853.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2854.                 {
  2855. #ifdef CK_ENCRYPTION
  2856.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2857.                         (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2858.                          TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2859.                       continue;
  2860.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2861.                     if (auth_crypt) {
  2862.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2863.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2864.                     }
  2865. #endif /* CK_ENCRYPTION */
  2866.                     authentication_version = AUTHTYPE_KERBEROS_V4;
  2867.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2868.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2869.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2870.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2871.                     }
  2872.                     break;
  2873.                 }
  2874.             }
  2875. #endif /* KRB4 */
  2876. #ifdef NTLM
  2877.         if ( atok(AUTHTYPE_NTLM) &&
  2878.              parsedat[i] == AUTHTYPE_NTLM &&
  2879.              ck_ntlm_is_valid(1) &&
  2880.              ntlm_auth_send() == 0) {
  2881.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2882.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2883. #ifdef CK_ENCRYPTION
  2884.                 /* NTLM does not support Telnet Encryption */
  2885.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2886.                     continue;
  2887.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2888. #endif /* CK_ENCRYPTION */
  2889.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2890.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2891.                 authentication_version = AUTHTYPE_NTLM;
  2892.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2893.                 break;
  2894.             }
  2895.         }
  2896. #endif /* NTLM */
  2897.         }
  2898.     }
  2899.  
  2900.     if (auth_how == -1) {               /* Did we find one? */
  2901.         switch ( auth_type_user[0] ) {  /* If not, abort the negotiation */
  2902.         case AUTHTYPE_NULL:
  2903.             auth_abort("User refused to accept any authentication method",0);
  2904.             break;
  2905.         case AUTHTYPE_AUTO:
  2906.             auth_abort("No authentication method available", 0);
  2907.             break;
  2908.         default: {
  2909.             char msg[80];
  2910.             ckmakmsg(msg,80,AUTHTYPE_NAME(auth_type_user[0]),
  2911.                       " could not be negotiated",NULL,NULL
  2912.                      );
  2913.             auth_abort(msg, 0);
  2914.         }
  2915.         }
  2916.         auth_finished(AUTH_REJECT);
  2917.         return AUTH_FAILURE;
  2918.     }
  2919.  
  2920.     printf("Authenticating with %s\r\n",
  2921.             AUTHTYPE_NAME(authentication_version));
  2922.  
  2923.     /* Send Telnet Auth Name message (if necessary) */
  2924.     switch ( authentication_version ) {
  2925.     case AUTHTYPE_SRP:
  2926.     case AUTHTYPE_KERBEROS_V4:
  2927.     case AUTHTYPE_KERBEROS_V5:
  2928.     case AUTHTYPE_GSSAPI_KRB5:
  2929.         /* if we do not have a name to login with get one now. */
  2930.         while ( szUserName[0] == '\0' ) {
  2931.             extern char * tn_pr_uid;
  2932.             int ok = uq_txt(NULL,
  2933.                      tn_pr_uid && tn_pr_uid[0] ? tn_pr_uid : "Host Userid: ",
  2934.                             1, NULL, szUserName, 63, NULL,DEFAULT_UQ_TIMEOUT);
  2935.             if ( !ok )
  2936.                 return AUTH_FAILURE;
  2937.         }
  2938.         plen = strlen(szUserName);
  2939.         pname = (unsigned char *) szUserName;
  2940.  
  2941.         /* Construct Telnet Debugging Message */
  2942.         if (deblog || tn_deb || debses) {
  2943.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2944.                        "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2945.                        " NAME ",(char *)pname," IAC SE",NULL,
  2946.                        NULL,NULL,NULL,NULL,NULL,NULL
  2947.                      );
  2948.             debug(F100,tn_msg,"",0);
  2949.             if (tn_deb || debses) tn_debug(tn_msg);
  2950.         }
  2951.  
  2952.         /* Construct and send Authentication Name subnegotiation */
  2953.         if ( plen < sizeof(buf) - 6 ) {
  2954.             sprintf((char *)buf, "%c%c%c%c", IAC, SB, 
  2955.                      TELOPT_AUTHENTICATION,
  2956.                      TELQUAL_NAME);
  2957.             memcpy(&buf[4], pname, plen);               /* safe */
  2958.             sprintf((char *)&buf[plen + 4], "%c%c", IAC, SE);   /* safe */
  2959. #ifdef OS2
  2960.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2961. #endif
  2962.             ttol((CHAR *)buf, plen+6);
  2963. #ifdef OS2
  2964.             ReleaseTelnetMutex();
  2965. #endif
  2966.         } else {
  2967.             sprintf((char *)buf, "%c%c%c%c%c%c", IAC, SB, 
  2968.                      TELOPT_AUTHENTICATION,
  2969.                      TELQUAL_NAME, IAC, SE);    /* safe */
  2970. #ifdef OS2
  2971.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2972. #endif
  2973.             ttol((CHAR *)buf, 6);
  2974. #ifdef OS2
  2975.             ReleaseTelnetMutex();
  2976. #endif
  2977.         }
  2978.     }
  2979.  
  2980.     /* Construct Authentication Mode subnegotiation message (if necessary) */
  2981.     switch ( authentication_version ) {
  2982.     case AUTHTYPE_SRP:
  2983.     case AUTHTYPE_KERBEROS_V4:
  2984.     case AUTHTYPE_KERBEROS_V5:
  2985.     case AUTHTYPE_GSSAPI_KRB5:
  2986.     case AUTHTYPE_NTLM:
  2987.         mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) | auth_crypt
  2988. #ifdef USE_INI_CRED_FWD
  2989.                | (((authentication_version == AUTHTYPE_KERBEROS_V5) &&
  2990.                   auth_fwd)?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  2991. #endif /* USE_INI_CRED_FWD */
  2992.                ;
  2993.         sprintf((char *)buf, "%c%c%c%c%c%c%c",
  2994.                  IAC, SB, TELOPT_AUTHENTICATION,
  2995.                  TELQUAL_IS,
  2996.                  authentication_version,
  2997.                  mode,
  2998.                  KRB_AUTH);     /* safe */
  2999.         break;
  3000.     }
  3001.  
  3002.     /* Send initial authentication data */
  3003.     switch ( authentication_version ) {
  3004. #ifdef CK_SSL
  3005.     case AUTHTYPE_SSL:
  3006.         SendSSLAuthSB(SSL_START,NULL,0);
  3007.         break;
  3008. #endif /* SSL */
  3009. #ifdef CK_SRP
  3010.     case AUTHTYPE_SRP:
  3011.         sprintf(&buf[7], "%c%c", IAC, SE);      /* safe */
  3012.         if (deblog || tn_deb || debses) {
  3013.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3014.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3015.                       " IS ",AUTHTYPE_NAME(authentication_version),
  3016.                       " AUTH ",AUTHMODE_NAME(mode)," IAC SE",
  3017.                       NULL,NULL,NULL,NULL,NULL
  3018.                      );
  3019.             debug(F100,tn_msg,"",0);
  3020.             if (tn_deb || debses) tn_debug(tn_msg);
  3021.         }
  3022. #ifdef OS2
  3023.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3024. #endif
  3025.         ttol((CHAR *)buf, 9);
  3026. #ifdef OS2
  3027.         ReleaseTelnetMutex();
  3028. #endif
  3029.         break;
  3030. #endif /* SRP */
  3031. #ifdef NTLM
  3032.     case AUTHTYPE_NTLM: {
  3033.         int length = 0;
  3034.  
  3035.         for ( i=0 ; i<NTLMSecBuf[0].cbBuffer ; i++ ) {
  3036.             if ( ((char *)NTLMSecBuf[0].pvBuffer)[i] == IAC )
  3037.                 iaccnt++;
  3038.         }
  3039.  
  3040.         if ( ( 2*sizeof(ULONG) + NTLMSecBuf[0].cbBuffer + iaccnt + 10)  <
  3041.              sizeof(buf) ) {
  3042.             length = copy_for_net(&buf[7],(char *)&NTLMSecBuf[0],
  3043.                                    2*sizeof(ULONG));
  3044.             length += copy_for_net(&buf[7+length], NTLMSecBuf[0].pvBuffer,
  3045.                                   NTLMSecBuf[0].cbBuffer);
  3046.         }
  3047.         sprintf(&buf[7+length], "%c%c", IAC, SE);
  3048.  
  3049.         if (deblog || tn_deb || debses) {
  3050.             int i;
  3051.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3052.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3053.                       " IS ",AUTHTYPE_NAME(authentication_version)," ",
  3054.                       AUTHMODE_NAME(mode)," NTLM_AUTH ",
  3055.                        NULL,NULL,NULL,NULL,NULL
  3056.                       );
  3057.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],length);
  3058.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3059.             debug(F100,tn_msg,"",0);
  3060.             if (tn_deb || debses) tn_debug(tn_msg);
  3061.         }
  3062. #ifdef OS2
  3063.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3064. #endif
  3065.         ttol((CHAR *)buf, length+9);
  3066. #ifdef OS2
  3067.         ReleaseTelnetMutex();
  3068. #endif
  3069.         break;
  3070.     }
  3071. #endif /* NTLM */
  3072. #ifdef KRB4
  3073.     case AUTHTYPE_KERBEROS_V4:
  3074.         for ( i=0 ; i<k4_auth.length ; i++ ) {
  3075.             if ( k4_auth.dat[i] == IAC )
  3076.                 iaccnt++;
  3077.         }
  3078.  
  3079.         if ( k4_auth.length + iaccnt + 10 < sizeof(buf) )
  3080.           k4_auth.length = copy_for_net(&buf[7], k4_auth.dat, k4_auth.length);
  3081.         else
  3082.             k4_auth.length = 0;
  3083.         sprintf(&buf[k4_auth.length+7], "%c%c", IAC, SE);
  3084.  
  3085.         if (deblog || tn_deb || debses) {
  3086.             int i;
  3087.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3088.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3089.                       AUTHTYPE_NAME(authentication_version)," ",
  3090.                       AUTHMODE_NAME(mode)," AUTH ",
  3091.                       NULL,NULL,NULL,NULL,NULL
  3092.                      );
  3093.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],k4_auth.length);
  3094.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3095.             debug(F100,tn_msg,"",0);
  3096.             if (tn_deb || debses) tn_debug(tn_msg);
  3097.         }
  3098. #ifdef OS2
  3099.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3100. #endif
  3101.         ttol((CHAR *)buf, k4_auth.length+9);
  3102. #ifdef OS2
  3103.         ReleaseTelnetMutex();
  3104. #endif
  3105.  
  3106. #ifndef REMOVE_FOR_EXPORT
  3107. #ifdef CK_ENCRYPTION
  3108.         /*
  3109.          * If we are doing mutual authentication, get set up to send
  3110.          * the challenge, and verify it when the response comes back.
  3111.          */
  3112.         if ((auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  3113.             register int i;
  3114.             int rc = 0;
  3115.  
  3116. #ifdef MIT_CURRENT
  3117.             data.data = cred.session;
  3118.             data.length = 8; /* sizeof(cred.session) */;
  3119.  
  3120.             if (code = krb5_c_random_seed(k5_context, &data)) {
  3121.                 com_err("libtelnet", code,
  3122.                          "while seeding random number generator");
  3123.                 return(0);
  3124.             }
  3125.  
  3126.             if (code = krb5_c_make_random_key(k5_context,
  3127.                                                ENCTYPE_DES_CBC_RAW,
  3128.                                                &random_key)) {
  3129.                 com_err("libtelnet", code,
  3130.                          "while creating random session key");
  3131.                 return(0);
  3132.             }
  3133.  
  3134.             /* the krb4 code uses ecb mode, but on a single block
  3135.             with a zero ivec, ecb and cbc are the same */
  3136.             k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  3137.             k4_krbkey.length = 8;
  3138.             k4_krbkey.contents = cred.session;
  3139.  
  3140.             encdata.ciphertext.data = random_key.contents;
  3141.             encdata.ciphertext.length = random_key.length;
  3142.             encdata.enctype = ENCTYPE_UNKNOWN;
  3143.  
  3144.             data.data = k4_session_key;
  3145.             data.length = 8;
  3146.  
  3147.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3148.                                    &encdata, &data);
  3149.  
  3150.             krb5_free_keyblock_contents(k5_context, &random_key);
  3151.  
  3152.             if (code) {
  3153.                 com_err("libtelnet", code, "while encrypting random key");
  3154.                 return(0);
  3155.             }
  3156.  
  3157.             encdata.ciphertext.data = k4_session_key;
  3158.             encdata.ciphertext.length = 8;
  3159.             encdata.enctype = ENCTYPE_UNKNOWN;
  3160.  
  3161.             data.data = k4_challenge;
  3162.             data.length = 8;
  3163.  
  3164.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3165.                                    &encdata, &data);
  3166. #else /* MIT_CURRENT */
  3167.             memset(k4_sched,0,sizeof(Schedule));
  3168.             hexdump("auth_send",cred.session,8);
  3169.             rc = des_key_sched(cred.session, k4_sched);
  3170.             if ( rc == -1 ) {
  3171.                 printf("?Invalid DES key specified in credentials\r\n");
  3172.                 debug(F110,"auth_send",
  3173.                       "invalid DES Key specified in credentials",0);
  3174.             } else if ( rc == -2 ) {
  3175.                 printf("?Weak DES key specified in credentials\r\n");
  3176.                 debug(F110,"auth_send",
  3177.                       "weak DES Key specified in credentials",0);
  3178.             } else if ( rc != 0 ) {
  3179.                 printf("?DES Key Schedule not set by credentials\r\n");
  3180.                 debug(F110,"auth_send",
  3181.                       "DES Key Schedule not set by credentials",0);
  3182.             }
  3183.             hexdump("auth_send schedule",k4_sched,8*16);
  3184.  
  3185.             des_set_random_generator_seed(cred.session);
  3186.  
  3187.             do {
  3188.                 des_new_random_key(k4_session_key);
  3189.                 des_fixup_key_parity(k4_session_key);
  3190.             } while ( ck_des_is_weak_key(k4_session_key) );
  3191.  
  3192.             hexdump("auth_send des_new_random_key(k4_session_key)",
  3193.                      k4_session_key,8);
  3194.  
  3195.             /* Decrypt the session key so that we can send it to the */
  3196.             /* host as a challenge                                   */
  3197. #ifdef NT
  3198.             des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 0);
  3199. #else /* NT */
  3200.             des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 0);
  3201. #endif /* NT */
  3202.             hexdump(
  3203.                 "auth_send des_ecb_encrypt(k4_session_key,k4_session_key,0)",
  3204.                 k4_session_key,8
  3205.                     );
  3206.             /* Prepare the result of the challenge */
  3207.             /* Decrypt the session_key, add 1, and then encrypt it */
  3208.             /* The result stored in k4_challenge should match the  */
  3209.             /* KRB4_RESPONSE value from the host.                  */
  3210. #ifdef NT
  3211.             des_ecb_encrypt(k4_session_key, k4_challenge, k4_sched, 0);
  3212. #else /* NT */
  3213.             des_ecb_encrypt(&k4_session_key, &k4_challenge, k4_sched, 0);
  3214. #endif /* NT */
  3215.  
  3216.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,0)",
  3217.                      k4_challenge,8);
  3218. #endif /* MIT_CURRENT */
  3219.             /*
  3220.             * Increment the challenge by 1, and encrypt it for
  3221.             * later comparison.
  3222.             */
  3223.             for (i = 7; i >= 0; --i) {
  3224.                 register int x;
  3225.                 x = (unsigned int)k4_challenge[i] + 1;
  3226.                 k4_challenge[i] = x;    /* ignore overflow */
  3227.                 if (x < 256)            /* if no overflow, all done */
  3228.                     break;
  3229.             }
  3230.             hexdump("auth_send k4_challenge+1",k4_challenge,8);
  3231. #ifdef MIT_CURRENT
  3232.             data.data = k4_challenge;
  3233.             data.length = 8;
  3234.  
  3235.             encdata.ciphertext.data = k4_challenge;
  3236.             encdata.ciphertext.length = 8;
  3237.             encdata.enctype = ENCTYPE_UNKNOWN;
  3238.  
  3239.             if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0, &data,
  3240.                                        &encdata)) {
  3241.                 com_err("libtelnet", code, "while encrypting random key");
  3242.                 return(0);
  3243.             }
  3244. #else /* MIT_CURRENT */
  3245. #ifdef NT
  3246.             des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  3247. #else /* NT */
  3248.             des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  3249. #endif /* NT */
  3250.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,1)",
  3251.                      k4_challenge,8);
  3252. #endif /* MIT_CURRENT */
  3253.         }
  3254. #endif  /* ENCRYPTION */
  3255. #endif /* REMOVE_FOR_EXPORT */
  3256.         break;
  3257. #endif /* KRB4 */
  3258. #ifdef GSSAPI_KRB5
  3259.     case AUTHTYPE_GSSAPI_KRB5:
  3260.         for ( i=0 ; i<gss_send_tok.length ; i++ ) {
  3261.             if ( ((char *)gss_send_tok.value)[i] == IAC )
  3262.                 iaccnt++;
  3263.         }
  3264.  
  3265.         if ( gss_send_tok.length + iaccnt + 10 < sizeof(buf) )
  3266.             gss_send_tok.length = copy_for_net(&buf[7], gss_send_tok.value,
  3267.                                                gss_send_tok.length);
  3268.         else
  3269.             gss_send_tok.length = 0;
  3270.         sprintf(&buf[gss_send_tok.length+7], "%c%c", IAC, SE);       /* safe */
  3271.         if (deblog || tn_deb || debses) {
  3272.             int i;
  3273.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3274.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3275.                       AUTHTYPE_NAME(authentication_version)," ",
  3276.                       AUTHMODE_NAME(mode)," AUTH ",
  3277.                       NULL,NULL,NULL,NULL,NULL
  3278.                      );
  3279.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],gss_send_tok.length);
  3280.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3281.             debug(F100,tn_msg,"",0);
  3282.             if (tn_deb || debses) tn_debug(tn_msg);
  3283.         }
  3284. #ifdef OS2
  3285.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3286. #endif
  3287.         ttol((CHAR *)buf, gss_send_tok.length+9);
  3288. #ifdef OS2
  3289.         ReleaseTelnetMutex();
  3290. #endif
  3291.         break;
  3292. #endif /* GSSAPI_KRB5 */
  3293. #ifdef KRB5
  3294.     case AUTHTYPE_KERBEROS_V5:
  3295.         debug(F111,"auth_send KRB5","k5_auth.length",k5_auth.length);
  3296.         for ( i=0 ; i<k5_auth.length ; i++ ) {
  3297.             if ( ((char *)k5_auth.data)[i] == IAC )
  3298.                 iaccnt++;
  3299.         }
  3300.  
  3301.         if ( k5_auth.length + iaccnt + 10 < sizeof(buf) )
  3302.           k5_auth.length = copy_for_net(&buf[7], k5_auth.data, 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(&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,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.     if (!krb5_get_host_realm(private_context,host,&realmlist)) {
  4555.     if (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 (realm == NULL || 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 = 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,auth_context,&ttyfd);
  5276.  
  5277.         if (!r)
  5278.             r = krb5_sock_to_principal(k5_context,0,"host",
  5279.                                        KRB5_NT_SRV_HST,&server);
  5280.  
  5281.         if (!r)
  5282. #else /* HEIMDAL */
  5283.         if (!r) {
  5284.             krb5_rcache rcache = NULL;
  5285.  
  5286.             r = krb5_auth_con_getrcache(k5_context, auth_context,
  5287.                                          &rcache);
  5288.             debug(F111,"k5_auth_is","krb5_auth_con_getrcache",r);
  5289.  
  5290.             if (!r && !rcache) {
  5291.                 /* Do not resolve server's principal name, we will check */
  5292.                 /* for validity after the krb5_rd_req() call.            */
  5293.                 r = krb5_sname_to_principal(k5_context, 0, 0,
  5294.                                              KRB5_NT_SRV_HST, &server);
  5295.                 debug(F111,"k5_auth_is","krb5_sname_to_principal",r);
  5296.  
  5297.                 if (!r) {
  5298.                     r = krb5_get_server_rcache(k5_context,
  5299.                         krb5_princ_component(k5_context, server, 0),
  5300.                                                 &rcache);
  5301.                     debug(F111,"k5_auth_is","krb5_get_server_rcache",r);
  5302.                     krb5_free_principal(k5_context, server);
  5303.                 }
  5304.             }
  5305.             if (!r) {
  5306.                 r = krb5_auth_con_setrcache(k5_context,
  5307.                                              auth_context, rcache);
  5308.                 debug(F111,"k5_auth_is","krb5_auth_con_setrcache",r);
  5309.             }
  5310.         }
  5311.         if (!r && k5_keytab) {
  5312.             r = krb5_kt_resolve(k5_context,
  5313.                                  k5_keytab, &keytabid);
  5314.             debug(F111,"k5_auth_is","krb5_kt_resolve",r);
  5315.         }
  5316. #endif /* HEIMDAL */
  5317.         if (!r) {
  5318.             r = krb5_rd_req(k5_context, &auth_context, &k5_auth,
  5319. #ifdef HEIMDAL
  5320.                              server, NULL, NULL,
  5321. #else /* HEIMDAL */
  5322.                              NULL, keytabid, NULL,
  5323. #endif /* HEIMDAL */
  5324.                              &k5_ticket);
  5325.             debug(F111,"k5_auth_is","krb5_rd_req",r);
  5326.         }
  5327.         if (r) {
  5328.             (void) ckstrncpy(errbuf, "krb5_rd_req failed: ",sizeof(errbuf));
  5329.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5330.             goto errout;
  5331.         }
  5332. #ifdef HEIMDAL
  5333.         krb5_free_principal(k5_context, server);
  5334.  
  5335.         {
  5336.             char type_check[26];
  5337.  
  5338.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5339.             /* client and then server finished messages. */
  5340.  
  5341.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5342.             type_check[1] = how;        /* not broken into parts */
  5343. #ifdef CK_SSL
  5344.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5345.                 ssl_get_client_finished(&type_check[2],12);
  5346.                 ssl_get_server_finished(&type_check[14],12);
  5347.                 hexdump("k5_auth_is type_check",type_check,26);
  5348.             }
  5349. #endif /* CK_SSL */
  5350.  
  5351.             r = krb5_verify_authenticator_checksum(k5_context,
  5352.                                                     auth_context,
  5353.                                                     type_check,
  5354. #ifdef CK_SSL
  5355.                 ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5356. #endif /* CK_SSL */
  5357.                                                     2);
  5358.         }
  5359. #else /* HEIMDAL */
  5360.         len = krb5_princ_component(k5_context,k5_ticket->server,0)->length;
  5361.         if (len < 256)
  5362.         {
  5363.             memcpy(princ,
  5364.                    krb5_princ_component(k5_context,k5_ticket->server,0)->data,
  5365.                    len);
  5366.             princ[len] = '\0';
  5367.         }
  5368.         if ( strcmp((krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME), princ) )
  5369.         {
  5370.             debug(F110,"k5_auth_is incorrect service name",princ,0);
  5371.             ckstrncpy(errbuf,"incorrect service name: ",sizeof(errbuf));
  5372.             ckstrncat(errbuf,krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5373.                      sizeof(errbuf));
  5374.             ckstrncat(errbuf," != ",sizeof(errbuf));
  5375.             ckstrncat(errbuf,princ,sizeof(errbuf));
  5376.             goto errout;
  5377.         }
  5378.  
  5379.         r = krb5_auth_con_getauthenticator(k5_context,
  5380.                                             auth_context,
  5381.                                             &authenticator);
  5382.         debug(F111,"k5_auth_is","krb5_auth_con_getauthenticator",r);
  5383.         if (r) {
  5384.             (void) ckstrncpy(errbuf,
  5385.                              "krb5_auth_con_getauthenticator failed: ",
  5386.                              sizeof(errbuf)
  5387.                              );
  5388.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5389.             goto errout;
  5390.         }
  5391.  
  5392.         if (authenticator->checksum) {
  5393.             char type_check[26];
  5394.             krb5_checksum *cksum = authenticator->checksum;
  5395.             krb5_keyblock *key;
  5396.  
  5397.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5398.             /* client and then server finished messages. */
  5399.  
  5400.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5401.             type_check[1] = how;        /* not broken into parts */
  5402. #ifdef CK_SSL
  5403.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5404.                 ssl_get_client_finished(&type_check[2],12);
  5405.                 ssl_get_server_finished(&type_check[14],12);
  5406.                 hexdump("k5_auth_is type_check",type_check,26);
  5407.             }
  5408. #endif /* CK_SSL */
  5409.  
  5410.             r = krb5_auth_con_getkey(k5_context, auth_context,
  5411.                                       &key);
  5412.             debug(F111,"k5_auth_is","krb5_auth_con_getkey",r);
  5413.             if (r) {
  5414.                 (void) ckstrncpy(errbuf, "krb5_auth_con_getkey failed: ",
  5415.                                   sizeof(errbuf));
  5416.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5417.                 goto errout;
  5418.             }
  5419.  
  5420.             r = krb5_verify_checksum(k5_context,
  5421.                                       cksum->checksum_type,
  5422.                                       cksum,
  5423.                                       &type_check,
  5424.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5425.                                       2,
  5426.                                       key->contents,
  5427.                                       key->length
  5428.                                       );
  5429.             debug(F111,"k5_auth_is","krb5_verify_checksum",r);
  5430.             if (r) {
  5431.                 (void) ckstrncpy(errbuf,
  5432.                                  "checksum verification failed: ",
  5433.                                  sizeof(errbuf)
  5434.                                  );
  5435.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5436.                 goto errout;
  5437.             }
  5438.             krb5_free_keyblock(k5_context, key);
  5439.         } else {
  5440.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT) {
  5441.                 (void) strcpy(errbuf,
  5442.                                "authenticator is missing required checksum");
  5443.                 goto errout;
  5444.             }
  5445.         }
  5446.  
  5447.         krb5_free_authenticator(k5_context, authenticator);
  5448. #endif /* HEIMDAL */
  5449.  
  5450. #ifdef TLS_VERIFY
  5451.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5452.             krb5_data in, msg;
  5453.             char tls_verify[24];
  5454.             krb5_replay_data repdata;
  5455.  
  5456.             ssl_get_server_finished(&tls_verify[0],12);
  5457.             ssl_get_client_finished(&tls_verify[12],12);
  5458.  
  5459.             in.data = tls_verify;
  5460.             in.length = 24;
  5461.  
  5462.             krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5463.                                    KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
  5464.             if (r = krb5_mk_safe(k5_context,auth_context,&in,&msg,&repdata)) {
  5465.                 com_err("", r, "encoding tls verifier");
  5466.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5467.                 goto errout;
  5468.             }
  5469.             SendK5AuthSB(KRB5_TLS_VERIFY, msg.data, msg.length);
  5470.             krb5_free_data_contents(k5_context,&msg);
  5471.         }
  5472. #endif /* CK_SSL */
  5473.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  5474.             /* do ap_rep stuff here */
  5475.             if ((r = krb5_mk_rep(k5_context,
  5476. #ifdef HEIMDAL
  5477.                                   &auth_context,
  5478. #else /* HEIMDAL */
  5479.                                   auth_context,
  5480. #endif /* HEIMDAL */
  5481.                                   &outbuf))) {
  5482.                 debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5483.                 (void) ckstrncpy(errbuf, "Make reply failed: ",sizeof(errbuf));
  5484.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5485.                 goto errout;
  5486.             }
  5487.             debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5488.  
  5489.             SendK5AuthSB(KRB5_RESPONSE, outbuf.data, outbuf.length);
  5490.             mutual_complete = 1;
  5491.         }
  5492.  
  5493. #ifdef HEIMDAL
  5494.         {
  5495.             char * name = NULL;
  5496.             if (krb5_unparse_name(k5_context, k5_ticket->client,
  5497.                                    &name))
  5498.             {
  5499.                 szUserNameAuthenticated[0] = '\0';
  5500.             } else {
  5501.                 ckstrncpy(szUserNameAuthenticated,UIDBUFLEN,name);
  5502.                 free(name);
  5503.             }
  5504.         }
  5505. #else /* HEIMDAL */
  5506.         if ( krb5_aname_to_localname(k5_context,
  5507.                                       k5_ticket->enc_part2->client,
  5508.                                       UIDBUFLEN,szUserNameAuthenticated) )
  5509.             szUserNameAuthenticated[0] = '\0';
  5510. #endif /* HEIMDAL */
  5511.  
  5512.         SendK5AuthSB(KRB_ACCEPT, szUserNameAuthenticated,
  5513.                       szUserNameAuthenticated[0] ? -1 : 0);
  5514.         accept_complete = 1;
  5515.         ckmakmsg(strTmp,sizeof(strTmp),
  5516.                  "Kerberos5 identifies him as ``",
  5517.                  szUserNameAuthenticated,"''",NULL);
  5518.         printf("%s\r\n",strTmp);
  5519.  
  5520.         if (szUserNameRequested[0] &&
  5521.             krb5_kuserok(k5_context,
  5522. #ifdef HEIMDAL
  5523.                           k5_ticket->client,
  5524. #else /* HEIMDAL */
  5525.                           k5_ticket->enc_part2->client,
  5526. #endif /* HEIMDAL */
  5527.                           szUserNameRequested))
  5528.             auth_finished(AUTH_VALID);
  5529.         else
  5530.             auth_finished(AUTH_USER);
  5531.  
  5532.         krb5_auth_con_getremotesubkey(k5_context, auth_context,
  5533.                                        &newkey);
  5534.         if (k5_session_key) {
  5535.             krb5_free_keyblock(k5_context, k5_session_key);
  5536.             k5_session_key = 0;
  5537.         }
  5538.         if (newkey) {
  5539.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  5540.             krb5_free_keyblock(k5_context, newkey);
  5541.         } else {
  5542.             krb5_copy_keyblock(k5_context,
  5543. #ifdef HEIMDAL
  5544.                                 &k5_ticket->ticket.key,
  5545. #else /* HEIMDAL */
  5546.                                 k5_ticket->enc_part2->session,
  5547. #endif /* HEIMDAL */
  5548.                                 &k5_session_key);
  5549.         }
  5550.  
  5551. #ifdef CK_ENCRYPTION
  5552. #ifdef HEIMDAL
  5553.         skey.type = k5_session_key->keyvalue.length == 8 ? SK_DES : SK_GENERIC;
  5554.         skey.length = k5_session_key->keyvalue.length;
  5555.         skey.data = k5_session_key->keyvalue.data;
  5556. #else /* HEIMDAL */
  5557.         skey.type = k5_session_key->length == 8 ? SK_DES : SK_GENERIC;
  5558.         skey.length = k5_session_key->length;
  5559.         skey.data = k5_session_key->contents;
  5560. #endif /* HEIMDAL */
  5561.         encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  5562. #endif /* CK_ENCRYPTION */
  5563.         debug(F100,"k5_auth_is AUTH_SUCCESS","",0);
  5564.         krb5_errno = r;
  5565.         if ( krb5_errno )
  5566.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5567.         else
  5568.             makestr(&krb5_errmsg,strTmp);
  5569.         return AUTH_SUCCESS;
  5570.  
  5571. #ifdef FORWARD
  5572.     case KRB5_FORWARD:
  5573.         if ( !forward_flag ) {
  5574.             SendK5AuthSB(KRB5_FORWARD_REJECT,
  5575.                           "forwarded credentials are being refused.",
  5576.                           -1);
  5577.             return(AUTH_SUCCESS);
  5578.         }
  5579.  
  5580.         inbuf.length = cnt;
  5581.         inbuf.data = (char *)data;
  5582.         if (
  5583. #ifndef HEIMDAL
  5584.             (!krb5_d_no_addresses &&
  5585.             (r = krb5_auth_con_genaddrs(k5_context,auth_context,g_kstream->fd,
  5586.                               KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))) ||
  5587. #endif /* HEIMDAL */
  5588.             (r = rd_and_store_for_creds(k5_context, auth_context,&inbuf,
  5589. #ifdef HEIMDAL
  5590.                                          k5_ticket->client
  5591. #else /* HEIMDAL */
  5592.                                          k5_ticket->enc_part2->client
  5593. #endif /* HEIMDAL */
  5594.                                          ))) {
  5595.             (void) ckstrncpy(errbuf, "Read forwarded creds failed: ",
  5596.                               sizeof(errbuf));
  5597.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5598.             SendK5AuthSB(KRB5_FORWARD_REJECT, errbuf, -1);
  5599.             printf("Could not read forwarded credentials\r\n");
  5600.             krb5_errno = r;
  5601.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5602.         }
  5603.         else {
  5604.             SendK5AuthSB(KRB5_FORWARD_ACCEPT, 0, 0);
  5605.             ckstrncpy(strTmp,"Forwarded credentials obtained",sizeof(strTmp));
  5606.             printf("%s\r\n",strTmp);
  5607.             krb5_errno = 0;
  5608.             makestr(&krb5_errmsg,strTmp);
  5609.         }
  5610.         /* A failure to accept forwarded credentials is not an */
  5611.         /* authentication failure.                             */
  5612.         return AUTH_SUCCESS;
  5613. #endif  /* FORWARD */
  5614.     default:
  5615.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  5616.         SendK5AuthSB(KRB_REJECT, 0, 0);
  5617.         break;
  5618.     }
  5619.     auth_finished(AUTH_REJECT);
  5620.     return AUTH_FAILURE;
  5621.  
  5622.   errout:
  5623.     SendK5AuthSB(KRB_REJECT, errbuf, -1);
  5624.     krb5_errno = r;
  5625.     makestr(&krb5_errmsg,errbuf);
  5626.     printf("%s\r\n", errbuf);
  5627.     if (auth_context) {
  5628.         krb5_auth_con_free(k5_context, auth_context);
  5629.         auth_context = 0;
  5630.     }
  5631.     auth_finished(AUTH_REJECT);
  5632.     return AUTH_FAILURE;
  5633. }
  5634.  
  5635. #ifdef FORWARD
  5636. int
  5637. #ifdef CK_ANSIC
  5638. kerberos5_forward(void)
  5639. #else
  5640. kerberos5_forward()
  5641. #endif
  5642. {
  5643.     krb5_error_code r;
  5644.     krb5_ccache ccache=NULL;
  5645.     krb5_principal client = 0;
  5646.     krb5_principal server = 0;
  5647.     krb5_data forw_creds;
  5648. #ifdef HEIMDAL
  5649.     krb5_creds      creds;
  5650. #endif /* HEIMDAL */
  5651.  
  5652.     forw_creds.data = 0;
  5653.  
  5654.     r = k5_get_ccache(k5_context,&ccache,NULL);
  5655.     if ( r ) {
  5656.         com_err(NULL, r, "Kerberos V5: could not get default ccache");
  5657.         krb5_errno = r;
  5658.         makestr(&krb5_errmsg,error_message(krb5_errno));
  5659.         return(AUTH_FAILURE);
  5660.     }
  5661.  
  5662.     if ((r = krb5_cc_get_principal(k5_context, ccache, &client))) {
  5663.         com_err(NULL, r, "Kerberos V5: could not get default principal");
  5664.         goto cleanup;
  5665.     }
  5666.  
  5667. #ifdef HEIMDAL
  5668.     memset(&creds, 0, sizeof(creds));
  5669.     creds.client = client;
  5670.  
  5671.     if (r = krb5_build_principal(k5_context,
  5672.                              &creds.server,
  5673.                              strlen(client->realm),
  5674.                               client->realm,
  5675.                               "krbtgt",
  5676.                               client->realm,
  5677.                                   NULL)) {
  5678.         com_err(NULL, r, "Kerberos V5: could not get principal");
  5679.         goto cleanup;
  5680.     }
  5681.  
  5682.     creds.times.endtime = 0;
  5683.  
  5684.     if (r = krb5_get_forwarded_creds(k5_context,
  5685.                                       auth_context,
  5686.                                       ccache,
  5687.                                       0,
  5688.                                       szHostName,
  5689.                                       &creds,
  5690.                                       &forw_creds)) {
  5691.         com_err(NULL, r, "Kerberos V5: error getting forwarded creds");
  5692.         goto cleanup;
  5693.     }
  5694. #else /* HEIMDAL */
  5695.     /* we should not need to make this call since we are storing the */
  5696.     /* server's principal in fwd_server from our call to             */
  5697.     /* krb5_sname_to_principal() in k5_auth_send()                   */
  5698.     if (fwd_server == NULL) {
  5699.         if ((r = krb5_sname_to_principal(k5_context, szHostName,
  5700.                                  krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5701.                                           KRB5_NT_SRV_HST, &server))) {
  5702.             com_err(NULL, r, "Kerberos V5: could not make server principal");
  5703.             goto cleanup;
  5704.         }
  5705.     }
  5706.  
  5707.     if (!krb5_d_no_addresses &&
  5708.         (r = krb5_auth_con_genaddrs(k5_context, auth_context, g_kstream->fd,
  5709.                              KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
  5710.     {
  5711.         com_err(NULL, r, "Kerberos V5: could not gen local full address");
  5712.         goto cleanup;
  5713.     }
  5714.  
  5715.     if (r = krb5_fwd_tgt_creds(k5_context, auth_context, 0, client,
  5716.                                 fwd_server ? fwd_server : server,
  5717.                                 ccache, forwardable_flag, &forw_creds)) {
  5718.         com_err(NULL, r, "Kerberos V5: error getting forwardable credentials");
  5719.         goto cleanup;
  5720.     }
  5721. #endif /* HEIMDAL */
  5722.  
  5723.     /* Send forwarded credentials */
  5724.     if (!SendK5AuthSB(KRB5_FORWARD, forw_creds.data, forw_creds.length)) {
  5725.         printf("Kerberos V5 forwarding error!\r\n%s\r\n",
  5726.                     "Not enough room for authentication data");
  5727.     }
  5728.  
  5729. cleanup:
  5730.     if (client)
  5731.         krb5_free_principal(k5_context, client);
  5732.     if (server)
  5733.         krb5_free_principal(k5_context, server);
  5734. #ifdef HEIMDAL
  5735.     krb5_data_free(&forw_creds);
  5736. #else /* HEIMDAL */
  5737.     krb5_free_data_contents(k5_context,&forw_creds);
  5738. #endif /* HEIMDAL */
  5739.     krb5_cc_close(k5_context, ccache);
  5740.  
  5741.     krb5_errno = r;
  5742.     makestr(&krb5_errmsg,krb5_errno?error_message(krb5_errno):"OK");
  5743.     return(r?AUTH_FAILURE:AUTH_SUCCESS);
  5744. }
  5745. #endif /* FORWARD */
  5746. #else /* KRB5 */
  5747. int
  5748. ck_krb5_autoget_TGT(char * dummy)
  5749. {
  5750.     return(0);
  5751. }
  5752. #ifdef CK_KERBEROS
  5753. int
  5754. #ifdef CK_ANSIC
  5755. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  5756.                  struct krb4_init_data * k4_init)
  5757. #else
  5758. ck_krb5_initTGT(op,init,k4_init)
  5759.     krb_op_data * op; struct krb5_init_data * init;
  5760.     struct krb4_init_data * k4_init;
  5761. #endif /* CK_ANSIC*/
  5762. {
  5763.     return(-1);
  5764. }
  5765.  
  5766. int
  5767. #ifdef CK_ANSIC
  5768. ck_krb5_destroy(struct krb_op_data * op)
  5769. #else
  5770. ck_krb5_destroy(op) struct krb_op_data * op;
  5771. #endif
  5772. {
  5773.     return(-1);
  5774. }
  5775.  
  5776. int
  5777. #ifdef CK_ANSIC
  5778. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  5779. #else
  5780. ck_krb5_list_creds(op,lc)
  5781.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  5782. #endif
  5783. {
  5784.     return(-1);
  5785. }
  5786. #else /* CK_KERBEROS */
  5787. int
  5788. #ifdef CK_ANSIC
  5789. ck_krb5_initTGT(void * op, void * init, void * k4_init )
  5790. #else
  5791. ck_krb5_initTGT(op,init,k4_init)
  5792.     void * op; void * init; void * k4_init;
  5793. #endif /* CK_ANSIC*/
  5794. {
  5795.     return(-1);
  5796. }
  5797.  
  5798. int
  5799. #ifdef CK_ANSIC
  5800. ck_krb5_destroy(void * op)
  5801. #else
  5802. ck_krb5_destroy(op) void * op;
  5803. #endif
  5804. {
  5805.     return(-1);
  5806. }
  5807.  
  5808. int
  5809. #ifdef CK_ANSIC
  5810. ck_krb5_list_creds(void * op, void * lc)
  5811. #else
  5812. ck_krb5_list_creds(op,lc)
  5813.     void * op; void * lc;
  5814. #endif
  5815. {
  5816.     return(-1);
  5817. }
  5818. #endif /* CK_KERBEROS */
  5819. #endif /* KRB5 */
  5820.  
  5821. #ifdef GSSAPI_KRB5
  5822. /*
  5823.  *
  5824.  * gssk5_auth_send - gets authentication bits we need to send to KDC.
  5825.  *
  5826.  * Result is left in k5_auth
  5827.  *
  5828.  * Returns: 0 on failure, 1 on success
  5829.  *
  5830.  */
  5831.  
  5832. static int
  5833. #ifdef CK_ANSIC
  5834. gssk5_auth_send(int how, int encrypt, int forward)
  5835. #else
  5836. gssk5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  5837. #endif
  5838. {
  5839.     OM_uint32 maj_stat, min_stat;
  5840. #ifdef KRB5
  5841.     char * realm = NULL;
  5842.     char tgt[256];
  5843. #endif /* KRB5 */
  5844.  
  5845.     gss_chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  5846.     gss_chan.initiator_address.length = 4;
  5847.     gss_chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  5848.     gss_chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  5849.     gss_chan.acceptor_address.length = 4;
  5850.     gss_chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  5851.     gss_chan.application_data.length = 0;
  5852.     gss_chan.application_data.value = 0;
  5853.  
  5854. #ifdef KRB5
  5855.     realm = ck_krb5_realmofhost(ftp_host);
  5856.     if (realm) {
  5857.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  5858.         debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  5859.         if ( krb5_autoget &&
  5860.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  5861.                 (ck_krb5_is_tgt_valid() > 0)) )
  5862.             ck_krb5_autoget_TGT(realm);
  5863.     }
  5864. #endif /* KRB5 */
  5865.  
  5866.     /* Blob from gss-client */
  5867.     /* host@hostname */
  5868.     /* the V5 GSSAPI binding canonicalizes this for us... */
  5869.     ckmakmsg(gss_stbuf,GSS_BUFSIZ,
  5870.              krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5871.              "@",
  5872.              szHostName,
  5873.               NULL
  5874.               );
  5875.     fprintf(stderr, "Authenticating to <%s>...\n", gss_stbuf);
  5876.     gss_send_tok.value = gss_stbuf;
  5877.     gss_send_tok.length = strlen(gss_stbuf);
  5878.     maj_stat = gss_import_name(&min_stat, &gss_send_tok,
  5879.                                 gss_nt_service_name,
  5880.                                 &gss_target_name
  5881.                                 );
  5882.     if (maj_stat != GSS_S_COMPLETE) {
  5883.         user_gss_error(maj_stat, min_stat, "parsing name");
  5884.         secure_error("name parsed <%s>\n", gss_stbuf);
  5885.         return(0);
  5886.     }
  5887.     token_ptr = GSS_C_NO_BUFFER;
  5888.     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  5889.  
  5890.     fprintf(stderr, "calling gss_init_sec_context\n");
  5891.     maj_stat =
  5892.         gss_init_sec_context(&min_stat,
  5893.                               GSS_C_NO_CREDENTIAL,
  5894.                               &gcontext,
  5895.                               gss_target_name,
  5896.                               gss_mech_krb5,
  5897.                               GSS_C_MUTUAL_FLAG |
  5898.                               GSS_C_REPLAY_FLAG |
  5899.                               ((forward && forward_flag) ?
  5900.                                 GSS_C_DELEG_FLAG : 0),
  5901.                               0,
  5902.                               (krb5_d_no_addresses ? /* channel bindings */
  5903.                                 GSS_C_NO_CHANNEL_BINDINGS :
  5904.                                 &gss_chan),
  5905.                               gss_token_ptr,
  5906.                               NULL,     /* ignore mech type */
  5907.                               &gss_send_tok,
  5908.                               NULL,     /* ignore ret_flags */
  5909.                               NULL
  5910.                               );        /* ignore time_rec */
  5911.  
  5912.  
  5913.         if (maj_stat != GSS_S_COMPLETE &&
  5914.              maj_stat != GSS_S_CONTINUE_NEEDED) {
  5915.             user_gss_error(maj_stat,
  5916.                             min_stat,
  5917.                             "initializing context"
  5918.                             );
  5919.             gss_release_name(&min_stat, &gss_target_name);
  5920.             return(0);
  5921.         }
  5922.         return(1);
  5923. }
  5924.  
  5925. /*
  5926.  * gssk5_auth_reply -- checks the reply for mutual authentication.
  5927.  */
  5928. static int
  5929. #ifdef CK_ANSIC
  5930. gssk5_auth_reply(int how, unsigned char *data, int cnt)
  5931. #else
  5932. gssk5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  5933. #endif
  5934. {
  5935.     data += 4;                                  /* Point to status byte */
  5936.     cnt -=5;
  5937.  
  5938.     switch (*data++) {
  5939.     case GSS_REJECT:
  5940.         if (cnt > 0) {
  5941.             char *s;
  5942.             int len;
  5943.             ckstrncpy(strTmp,"GSSAPI refuses authentication because\r\n",
  5944.                       sizeof(strTmp));
  5945.             len = strlen(strTmp);
  5946.             if ( len + cnt < sizeof(strTmp) ) {
  5947.                 s = strTmp + strlen(strTmp);
  5948.                 memcpy(s, data, cnt);           /* safe */
  5949.                 s[cnt] = 0;
  5950.             }
  5951.         } else
  5952.             ckstrncpy(strTmp,"GSSAPI refuses authentication",
  5953.                       sizeof(strTmp));
  5954.         printf("GSSAPI authentication failed!\r\n%s\r\n",strTmp);
  5955.         auth_finished(AUTH_REJECT);
  5956.         return AUTH_FAILURE;
  5957.  
  5958.     case GSS_ACCEPT:
  5959.         if ( cnt > 0 ) {
  5960.             char *s;
  5961.             int len;
  5962.             ckstrncpy(strTmp,"GSSAPI accepts you as ",sizeof(strTmp));
  5963.             len = strlen(strTmp);
  5964.             if ( len + cnt < sizeof(strTmp) ) {
  5965.                 s = strTmp + strlen(strTmp);
  5966.                 memcpy(s,data,cnt);
  5967.                 s[cnt] = 0;
  5968.             }
  5969.         }
  5970.         accept_complete = 1;
  5971.         printf("%s\r\n",strTmp);
  5972.         auth_finished(AUTH_USER);
  5973.         return AUTH_SUCCESS;
  5974.  
  5975.     case GSS_RESPONSE:
  5976.         gss_token_ptr = &gss_recv_tok;
  5977.         gss_recv_tok.value = data;
  5978.         gss_recv_tok.length = cnt;
  5979.  
  5980.         maj_stat =
  5981.             gss_init_sec_context(&min_stat,
  5982.                                   GSS_C_NO_CREDENTIAL,
  5983.                                   &gcontext,
  5984.                                   gss_target_name,
  5985.                                   gss_krb5_mech,
  5986.                                   GSS_C_MUTUAL_FLAG |
  5987.                                   GSS_C_REPLAY_FLAG |
  5988.                                   (forward_flag ?
  5989.                                     GSS_C_DELEG_FLAG : 0),
  5990.                                   0,
  5991.                                   (krb5_d_no_addresses ? /* channel bindings */
  5992.                                     GSS_C_NO_CHANNEL_BINDINGS :
  5993.                                     &gss_chan),
  5994.                                   gss_token_ptr,
  5995.                                   NULL, /* ignore mech type */
  5996.                                   &gss_send_tok,
  5997.                                   NULL, /* ignore ret_flags */
  5998.                                   NULL
  5999.                                   );    /* ignore time_rec */
  6000.  
  6001.         if ( maj_stat == GSS_S_COMPLETE )
  6002.         {
  6003.  
  6004.         } else if ( maj_stat == CSS_S_CONTINUE_NEEDED ) {
  6005.         } else {
  6006.         }
  6007.  
  6008.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  6009.                   sizeof(strTmp));
  6010.         printf("%s\r\n",strTmp);
  6011.         auth_finished(AUTH_USER);
  6012.         return AUTH_SUCCESS;
  6013.  
  6014.     default:
  6015.         auth_finished(AUTH_REJECT);
  6016.         return AUTH_FAILURE;                        /* Unknown reply type */
  6017.     }
  6018. }
  6019.  
  6020. /*
  6021.  *
  6022.  * gssk5_auth_is.
  6023.  *
  6024.  */
  6025.  
  6026. static int
  6027. #ifdef CK_ANSIC
  6028. k5_auth_is(int how, unsigned char *data, int cnt)
  6029. #else
  6030. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6031. #endif
  6032. {
  6033.     int replied = 0;
  6034.     gss_cred_id_t server_creds, deleg_creds;
  6035.     gss_name_t client;
  6036.     int ret_flags;
  6037.     gss_buffer_desc name_buf;
  6038.     gss_name_t server_name;
  6039.     OM_uint32 acquire_maj,
  6040.       acquire_min,
  6041.       accept_maj,
  6042.       accept_min,
  6043.       stat_maj,
  6044.       stat_min;
  6045.     gss_OID mechid;
  6046.     gss_buffer_desc tok, out_tok;
  6047.     char gbuf[GSS_BUFSIZ];
  6048.     u_char gout_buf[GSS_BUFSIZ];
  6049.     char localname[MAXHOSTNAMELEN];
  6050.     char service_name[MAXHOSTNAMELEN+10];
  6051.     char **service;
  6052.     struct hostent *hp;
  6053.  
  6054.     data += 4;                                  /* Point to status byte */
  6055.     cnt -= 4;
  6056.  
  6057.     hexdump("gssk5_auth_is data",data,cnt);
  6058.     debug(F111,"gssk5_auth_is","how",how);
  6059.  
  6060.     if (cnt-- < 1) {
  6061.         auth_finished(AUTH_REJECT);
  6062.         return AUTH_FAILURE;
  6063.     }
  6064.     switch (*data++) {
  6065.     case GSS_AUTH:
  6066.         gss_chan.initiator_addrtype = GSS_C_AF_INET;
  6067.         gss_chan.initiator_address.length = 4;
  6068.         gss_chan.initiator_address.value = &his_addr.sin_addr.s_addr;
  6069.         gss_chan.acceptor_addrtype = GSS_C_AF_INET;
  6070.         gss_chan.acceptor_address.length = 4;
  6071.         gss_chan.acceptor_address.value = &ctrl_addr.sin_addr.s_addr;
  6072.         gss_chan.application_data.length = 0;
  6073.         gss_chan.application_data.value = 0;
  6074.  
  6075.         tok.value = data;
  6076.         tok.length = cnt;
  6077.  
  6078.         if (gethostname(localname, MAXHOSTNAMELEN)) {
  6079.             auth_finished(AUTH_REJECT);
  6080.             return AUTH_FAILURE;
  6081.         }
  6082.         if (!(hp = gethostbyname(localname))) {
  6083.             auth_finished(AUTH_REJECT);
  6084.             return AUTH_FAILURE;
  6085.         }
  6086. #ifdef HADDRLIST
  6087.         hp = ck_copyhostent(hp);
  6088. #endif /* HADDRLIST */
  6089.         strncpy(localname, hp->h_name, sizeof(localname) - 1);
  6090.         localname[sizeof(localname) - 1] = '\0';
  6091.  
  6092.         sprintf(service_name, "%s@%s", *service, localname);
  6093.         name_buf.value = service_name;
  6094.         name_buf.length = strlen(name_buf.value) + 1;
  6095.         stat_maj = gss_import_name(&stat_min, &name_buf,
  6096.                                     gss_nt_service_name,
  6097.                                     &server_name);
  6098.         if (stat_maj != GSS_S_COMPLETE) {
  6099.             auth_finished(AUTH_REJECT);
  6100.             return AUTH_FAILURE;
  6101.         }
  6102.  
  6103.         acquire_maj = gss_acquire_cred(&acquire_min, server_name, 0,
  6104.                                         GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
  6105.                                         &server_creds, NULL, NULL);
  6106.         (void) gss_release_name(&stat_min, &server_name);
  6107.  
  6108.         if (acquire_maj != GSS_S_COMPLETE) {
  6109.             reply_gss_error(535, accept_maj, accept_min,
  6110.                                  "accepting context");
  6111.             syslog(LOG_ERR, "failed accepting context");
  6112.             (void) gss_release_cred(&stat_min, &server_creds);
  6113.             if (ret_flags & GSS_C_DELEG_FLAG)
  6114.                 (void) gss_release_cred(&stat_min,
  6115.                                          &deleg_creds);
  6116.             return 0;
  6117.         }
  6118.  
  6119.         gcontext = GSS_C_NO_CONTEXT;
  6120.         accept_maj = gss_accept_sec_context(&accept_min,
  6121.                                             &gcontext, /* context_handle */
  6122.                                             /* verifier_cred_handle */
  6123.                                             server_creds,
  6124.                                             &tok, /* input_token */
  6125.                         (krb5_d_no_addresses ?
  6126.                          /* channel bindings */
  6127.                                                GSS_C_NO_CHANNEL_BINDINGS :
  6128.                                                &gss_chan),
  6129.                                              &client, /* src_name */
  6130.                                             &mechid, /* mech_type */
  6131.                                             &out_tok, /* output_token */
  6132.                                             &ret_flags,
  6133.                                             NULL,       /* ignore time_rec */
  6134.                                             /* forwarded credentials */
  6135.                                             &deleg_creds
  6136.                                             );
  6137.  
  6138.         if (accept_maj!=GSS_S_COMPLETE && accept_maj!=GSS_S_CONTINUE_NEEDED) {
  6139.             reply_gss_error(535, accept_maj, accept_min,
  6140.                              "accepting context");
  6141.             syslog(LOG_ERR, "failed accepting context");
  6142.             (void) gss_release_cred(&stat_min, &server_creds);
  6143.             if (ret_flags & GSS_C_DELEG_FLAG)
  6144.                 (void) gss_release_cred(&stat_min,
  6145.                                          &deleg_creds);
  6146.             return 0;
  6147.         }
  6148.  
  6149.         if (out_tok.length) {
  6150.             if (kerror = radix_encode(out_tok.value,gbuf,&out_tok.length, 0)) {
  6151.                 secure_error("Couldn't encode ADAT reply (%s)",
  6152.                              radix_error(kerror));
  6153.                 syslog(LOG_ERR, "couldn't encode ADAT reply");
  6154.                 (void) gss_release_cred(&stat_min, &server_creds);
  6155.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6156.                         (void) gss_release_cred(&stat_min,
  6157.                                                 &deleg_creds);
  6158.                 return(0);
  6159.             }
  6160.             if (stat_maj == GSS_S_COMPLETE) {
  6161.                 reply(235, "ADAT=%s", gbuf);
  6162.                 replied = 1;
  6163.             } else {
  6164.                 /* If the server accepts the security data, and
  6165.                    requires additional data, it should respond
  6166.                    with reply code 335. */
  6167.                 reply(335, "ADAT=%s", gbuf);
  6168.             }
  6169.             (void) gss_release_buffer(&stat_min, &out_tok);
  6170.         }
  6171.  
  6172.         if (stat_maj == GSS_S_COMPLETE) {
  6173.             /* GSSAPI authentication succeeded */
  6174.             stat_maj = gss_display_name(&stat_min, client,
  6175.                                          &client_name, &mechid);
  6176.             if (stat_maj != GSS_S_COMPLETE) {
  6177.                 /* "If the server rejects the security data (if
  6178.                    a checksum fails, for instance), it should
  6179.                    respond with reply code 535." */
  6180.                 reply_gss_error(535, stat_maj, stat_min,
  6181.                                 "extracting GSSAPI identity name");
  6182.                 syslog(LOG_ERR, "gssapi error extracting identity");
  6183.                 (void) gss_release_cred(&stat_min, &server_creds);
  6184.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6185.                         (void) gss_release_cred(&stat_min,
  6186.                                                 &deleg_creds);
  6187.                 return 0;
  6188.             }
  6189.             auth_type = temp_auth_type;
  6190.             temp_auth_type = NULL;
  6191.  
  6192.             (void) gss_release_cred(&stat_min, &server_creds);
  6193.             if (ret_flags & GSS_C_DELEG_FLAG) {
  6194.                 if (want_creds)
  6195.                     ftpd_gss_convert_creds(client_name.value,
  6196.                                             deleg_creds);
  6197.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6198.             }
  6199.  
  6200.             /* If the server accepts the security data, but does
  6201.                not require any additional data (i.e., the security
  6202.                data exchange has completed successfully), it must
  6203.                respond with reply code 235. */
  6204.             if (!replied)
  6205.             {
  6206.                 if (ret_flags & GSS_C_DELEG_FLAG && !have_creds)
  6207.                   reply(235,
  6208.  "GSSAPI Authentication succeeded, but could not accept forwarded credentials"
  6209.                         );
  6210.                 else
  6211.                   reply(235, "GSSAPI Authentication succeeded");
  6212.             }
  6213.             return(1);
  6214.         } else if (stat_maj == GSS_S_CONTINUE_NEEDED) {
  6215.             /* If the server accepts the security data, and
  6216.             requires additional data, it should respond with
  6217.             reply code 335. */
  6218.             reply(335, "more data needed");
  6219.             (void) gss_release_cred(&stat_min, &server_creds);
  6220.             if (ret_flags & GSS_C_DELEG_FLAG)
  6221.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6222.             return(0);
  6223.         } else {
  6224.             /* "If the server rejects the security data (if
  6225.             a checksum fails, for instance), it should
  6226.             respond with reply code 535." */
  6227.             reply_gss_error(535, stat_maj, stat_min,
  6228.                              "GSSAPI failed processing ADAT");
  6229.             syslog(LOG_ERR, "GSSAPI failed processing ADAT");
  6230.             (void) gss_release_cred(&stat_min, &server_creds);
  6231.             if (ret_flags & GSS_C_DELEG_FLAG)
  6232.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6233.             return(0);
  6234.         }
  6235.  
  6236.         debug(F100,"gssk5_auth_is AUTH_SUCCESS","",0);
  6237.         krb5_errno = r;
  6238.         if ( krb5_errno )
  6239.             makestr(&krb5_errmsg,error_message(krb5_errno));
  6240.         else
  6241.             makestr(&krb5_errmsg,strTmp);
  6242.         return AUTH_SUCCESS;
  6243.  
  6244.     default:
  6245.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  6246.         SendGSSK5AuthSB(GSS_REJECT, 0, 0);
  6247.         break;
  6248.     }
  6249.     auth_finished(AUTH_REJECT);
  6250.     return AUTH_FAILURE;
  6251. }
  6252. #endif /* GSSAPI_KRB5 */
  6253.  
  6254. #ifdef CK_SRP
  6255. /*
  6256.  * Copyright (c) 1997 Stanford University
  6257.  *
  6258.  * The use of this software for revenue-generating purposes may require a
  6259.  * license from the owners of the underlying intellectual property.
  6260.  * Specifically, the SRP-3 protocol may not be used for revenue-generating
  6261.  * purposes without a license.
  6262.  *
  6263.  * NOTE: Columbia University has a license.
  6264.  *
  6265.  * Within that constraint, permission to use, copy, modify, and distribute
  6266.  * this software and its documentation for any purpose is hereby granted
  6267.  * without fee, provided that the above copyright notices and this permission
  6268.  * notice appear in all copies of the software and related documentation.
  6269.  *
  6270.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  6271.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  6272.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  6273.  *
  6274.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  6275.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  6276.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  6277.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  6278.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  6279.  */
  6280.  
  6281. static void
  6282. srp_encode_length(data, num)
  6283.     unsigned char * data;
  6284.     int num;
  6285. {
  6286.     *data = (num >> 8) & 0xff;
  6287.     *++data = num & 0xff;
  6288. }
  6289.  
  6290. static int
  6291. srp_decode_length(data)
  6292.     unsigned char * data;
  6293. {
  6294.     return (((int) *data & 0xff) << 8) | (*(data + 1) & 0xff);
  6295. }
  6296.  
  6297. #ifdef PRE_SRP_1_7_3
  6298. static int
  6299. #ifdef CK_ANSIC
  6300. srp_reply(int how, unsigned char *data, int cnt)
  6301. #else
  6302. srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6303. #endif
  6304. {
  6305.     struct t_num n;
  6306.     struct t_num g;
  6307.     struct t_num s;
  6308.     struct t_num B;
  6309.     struct t_num * A;
  6310.     char type_check[26];
  6311.     int pflag;
  6312.  
  6313. #ifdef CK_ENCRYPTION
  6314.     Session_Key skey;
  6315. #endif /* ENCRYPTION */
  6316.  
  6317.     char * str=NULL;
  6318.  
  6319.     data += 4;                          /* Point to status byte */
  6320.     cnt  -= 4;
  6321.  
  6322.     if(cnt-- < 1) {
  6323.         auth_finished(AUTH_REJECT);
  6324.         return AUTH_FAILURE;
  6325.     }
  6326.  
  6327.     switch(*data++) {
  6328.     case SRP_REJECT:
  6329.         ckmakmsg(strTmp,sizeof(strTmp),
  6330.                   "SRP refuses authentication for '",szUserName,
  6331.                   "'\r\n",NULL);
  6332.         if (cnt > 0) {
  6333.             int len = strlen(strTmp);
  6334.             if ( len + cnt < sizeof(strTmp) ) {
  6335.                 str = strTmp + strlen(strTmp);
  6336.                 memcpy(str,data,cnt);
  6337.                 str[cnt] = 0;
  6338.             }
  6339.         }
  6340.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6341.         if (tc != NULL) {
  6342.             t_clientclose(tc);
  6343.             tc = NULL;
  6344.         }
  6345.         auth_finished(AUTH_REJECT);
  6346.         return AUTH_FAILURE;
  6347.  
  6348.     case SRP_ACCEPT:
  6349.         if(cnt < RESPONSE_LEN || !srp_waitresp ||
  6350.             tc == NULL
  6351.             ) {
  6352.             printf("SRP Protocol error\r\n");
  6353.             return(auth_resend(AUTHTYPE_SRP));
  6354.         }
  6355.         srp_waitresp = 0;
  6356.  
  6357.         if(t_clientverify(tc, data) == 0) {
  6358.             printf("SRP accepts you as %s\r\n",szUserName);
  6359. #ifdef CK_SSL
  6360.             if((ssl_active_flag || tls_active_flag) &&
  6361.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6362.                 printf("TLS session parameters verified by SRP\r\n");
  6363.             } else
  6364. #endif /* CK_SSL */
  6365.  
  6366. #ifdef CK_ENCRYPTION
  6367.             {
  6368.                 skey.type = SK_GENERIC;
  6369.                 skey.length = SESSION_KEY_LEN;
  6370.                 skey.data = tc->session_key;
  6371.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6372.             }
  6373. #endif /* ENCRYPTION */
  6374.             t_clientclose(tc);
  6375.             tc = NULL;
  6376.             accept_complete = 1;
  6377.             auth_finished(AUTH_VALID);
  6378.             return AUTH_SUCCESS;
  6379.         } else {
  6380.             printf("SRP server authentication failed!\r\n");
  6381.             t_clientclose(tc);
  6382.             tc = NULL;
  6383.             return(auth_resend(AUTHTYPE_SRP));
  6384.         }
  6385.         break;
  6386.  
  6387.     case SRP_PARAMS:
  6388.         if(!szUserName) {
  6389.             printf("No username available\r\n");
  6390.             return(auth_resend(AUTHTYPE_SRP));
  6391.         }
  6392.  
  6393.         n.len = srp_decode_length(data);
  6394.         data += 2;
  6395.         cnt -= 2;
  6396.         if(n.len > cnt) {
  6397.             printf("n too long\r\n");
  6398.             return(auth_resend(AUTHTYPE_SRP));
  6399.         }
  6400.         n.data = data;
  6401.         data += n.len;
  6402.         cnt -= n.len;
  6403.  
  6404.         g.len = srp_decode_length(data);
  6405.         data += 2;
  6406.         cnt -= 2;
  6407.         if(g.len > cnt) {
  6408.             printf("g too long\r\n");
  6409.             return(auth_resend(AUTHTYPE_SRP));
  6410.         }
  6411.         g.data = data;
  6412.         data += g.len;
  6413.         cnt -= g.len;
  6414.  
  6415.         s.len = srp_decode_length(data);
  6416.         data += 2;
  6417.         cnt -= 2;
  6418.         if(s.len > cnt) {
  6419.             printf("salt too long\r\n");
  6420.             return(auth_resend(AUTHTYPE_SRP));
  6421.         }
  6422.         s.data = data;
  6423.         data += s.len;
  6424.         cnt -= s.len;
  6425.  
  6426.         /* If the parameters provided by the server cannot be
  6427.          * validated the following function will fail.
  6428.          */
  6429.         tc = t_clientopen(szUserName, &n, &g, &s);
  6430.         if (tc == NULL) {
  6431.             printf("SRP parameter initialization error\r\n");
  6432.             return(auth_resend(AUTHTYPE_SRP));
  6433.         }
  6434.         A = t_clientgenexp(tc);
  6435.         if(A == NULL) {
  6436.             printf("SRP protocol error\r\n");
  6437.             return(auth_resend(AUTHTYPE_SRP));
  6438.         }
  6439.         SendSRPAuthSB(SRP_EXP, A->data, A->len);
  6440.  
  6441.         if ( pwbuf[0] && pwflg ) {
  6442.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6443.                    8 * n.len,
  6444.                    szUserName
  6445.                    );
  6446.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6447. #ifdef OS2
  6448.             if ( pwcrypt )
  6449.                 ck_encrypt((char *)srp_passwd);
  6450. #endif /* OS2 */
  6451.         } else {
  6452.             extern char * srppwprompt;
  6453.             char preface[128];
  6454.             int ok;
  6455.  
  6456.             if (srppwprompt && srppwprompt[0] &&
  6457.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6458.         sizeof(preface)) {
  6459.                 sprintf(preface,srppwprompt,szUserName);
  6460.             } else {
  6461.                 ckmakxmsg( preface,sizeof(preface),
  6462.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6463.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6464.                           NULL, NULL);
  6465.             }
  6466.             ok = uq_txt( preface,"Password: ",2,NULL,
  6467.                          srp_passwd,sizeof(srp_passwd)-1,NULL,
  6468.              DEFAULT_UQ_TIMEOUT);
  6469.             if ( !ok )
  6470.                 srp_passwd[0] = '\0';
  6471.         }
  6472.  
  6473.         t_clientpasswd(tc, srp_passwd);
  6474.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6475.         return AUTH_SUCCESS;
  6476.  
  6477.     case SRP_CHALLENGE:
  6478.         if(tc == NULL) {
  6479.             printf("SRP protocol error\r\n");
  6480.             return(auth_resend(AUTHTYPE_SRP));
  6481.         }
  6482.  
  6483. #ifndef PRE_SRP_1_4_5
  6484.         /*
  6485.          * The original SRP AUTH implementation did not protect against
  6486.          * tampering of the auth-type-pairs.  Therefore, when the
  6487.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6488.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6489.          * is set we also insert the SSL/TLS client and server finished
  6490.          * messages to ensure that there is no man in the middle attack
  6491.          * underway on the SSL/TLS connection.
  6492.          */
  6493.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6494.             type_check[0] = AUTHTYPE_SRP;
  6495.             type_check[1] = how;
  6496. #ifdef CK_SSL
  6497.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6498.                 ssl_get_client_finished(&type_check[2],12);
  6499.                 ssl_get_server_finished(&type_check[14],12);
  6500.                 t_clientaddexdata(tc,type_check,26);
  6501.             } else
  6502. #endif /* CK_SSL */
  6503.                 t_clientaddexdata(tc,type_check,2);
  6504.         }
  6505. #endif /* PRE_SRP_1_4_5 */
  6506.  
  6507.         B.data = data;
  6508.         B.len = cnt;
  6509.         t_clientgetkey(tc, &B);
  6510.  
  6511.         SendSRPAuthSB(SRP_RESPONSE, t_clientresponse(tc), RESPONSE_LEN);
  6512.         srp_waitresp = 1;
  6513.         return AUTH_SUCCESS;
  6514.  
  6515.     default:
  6516.         return(auth_resend(AUTHTYPE_SRP));
  6517.     }
  6518.     return AUTH_FAILURE;
  6519. }
  6520.  
  6521. static int
  6522. #ifdef CK_ANSIC
  6523. srp_is(int how, unsigned char *data, int cnt)
  6524. #else
  6525. srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6526. #endif
  6527. {
  6528.     char * pbuf = NULL;
  6529.     char * ptr;
  6530. #ifdef CK_ENCRYPTION
  6531.     Session_Key skey;
  6532. #endif
  6533.     struct t_num A;
  6534.     struct t_pw * tpw = NULL;
  6535.     struct t_conf * tconf = NULL;
  6536.     struct passwd * pass;
  6537.     static struct t_num * B = NULL;     /* Holder for B */
  6538. #ifdef CK_SSL
  6539.     char type_check[26];
  6540. #else
  6541.     char type_check[2];
  6542. #endif /* CK_SSL */
  6543.  
  6544.     if ((cnt -= 4) < 1) {
  6545.         auth_finished(AUTH_REJECT);
  6546.         return AUTH_FAILURE;
  6547.     }
  6548.  
  6549.     data += 4;
  6550.     cnt  -= 1;
  6551.     switch(*data++) {
  6552.     case SRP_AUTH:
  6553.         /* Send parameters back to client */
  6554.         if(ts != NULL) {
  6555.             t_serverclose(ts);
  6556.             ts = NULL;
  6557.         }
  6558.         if(!szUserNameRequested[0]) {
  6559.             if (1)
  6560.                 printf("No username available\r\n");
  6561.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6562.             auth_finished(AUTH_REJECT);
  6563.             return(AUTH_FAILURE);
  6564.         }
  6565. #ifdef IKSD
  6566. #ifdef CK_LOGIN
  6567.         if (inserver && ckxanon &&
  6568.              !strcmp(szUserNameRequested,"anonymous")) {
  6569.             SendSRPAuthSB(SRP_REJECT, (void *)
  6570.             "anonymous login cannot be performed with Secure Remote Password",
  6571.             -1);
  6572.             auth_finished(AUTH_REJECT);
  6573.             return(AUTH_FAILURE);
  6574.         }
  6575. #endif /* CK_LOGIN */
  6576. #endif /* IKSD */
  6577. #ifndef PRE_SRP_1_4_4
  6578.         if(tpw == NULL) {
  6579.             if((tpw = t_openpw(NULL)) == NULL) {
  6580.                 if (1)
  6581.                     printf("Unable to open password file\r\n");
  6582.                 SendSRPAuthSB(SRP_REJECT, (void *) "No password file", -1);
  6583.                 return(AUTH_FAILURE);
  6584.             }
  6585.         }
  6586.         if(tconf == NULL) {
  6587.             if((tconf = t_openconf(NULL)) == NULL) {
  6588.                 if (1)
  6589.                   printf("Unable to open configuration file\r\n");
  6590.                 SendSRPAuthSB(SRP_REJECT, (void *)"No configuration file", -1);
  6591.                 return(AUTH_FAILURE);
  6592.             }
  6593.         }
  6594.         ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6595.         t_closepw(tpw);
  6596.         tpw = NULL;
  6597.         t_closeconf(tconf);
  6598.         tconf = NULL;
  6599. #else /* PRE_SRP_1_4_4 */
  6600. #ifdef COMMENT
  6601.         /* the code in this block should no longer be necessary on OS/2
  6602.            or Windows because I have added functionality to libsrp.lib
  6603.            to find the srp files.   4/22/2000
  6604.         */
  6605.  
  6606.         /* On Windows and OS/2 there is no well defined place for the */
  6607.         /* ETC directory.  So we look for either an SRP_ETC or ETC    */
  6608.         /* environment variable in that order.  If we find one we     */
  6609.         /* attempt to open the files manually.                        */
  6610.         /* We will reuse the strTmp[] for the file names. */
  6611.         ptr = getenv("SRP_ETC");
  6612.         if ( !ptr )
  6613.             ptr = getenv("ETC");
  6614. #ifdef NT
  6615.         if ( !ptr ) {
  6616.             DWORD len;
  6617.             len = AUTHTMPBL;
  6618.  
  6619.             len = GetWindowsDirectory(strTmp,len);
  6620.             if ( len > 0 && len < AUTHTMPBL) {
  6621.                 if ( !isWin95() ) {
  6622.                     if ( len == 1 )
  6623.               ckstrncat(strTmp,"SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6624.                     else
  6625.               ckstrncat(strTmp,"/SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6626.                 }
  6627.             }
  6628.             ptr = strTmp;
  6629.         }
  6630. #endif /* NT */
  6631.         if ( ptr ) {
  6632.             int len = strlen(ptr);
  6633.             int i;
  6634.         if (ptr != strTmp)
  6635.         strcpy(strTmp,ptr);
  6636.             for ( i=0;i<len;i++ ) {
  6637.                 if ( strTmp[i] == '\\' )
  6638.                     strTmp[i] = '/';
  6639.             }
  6640.             if ( strTmp[len-1] != '/' )
  6641.                 ckstrncat(strTmp,"/tpasswd",sizeof(strTmp));
  6642.             else
  6643.                 ckstrncat(strTmp,"tpasswd",sizeof(strTmp));
  6644.             tpw = t_openpwbyname(strTmp);
  6645.  
  6646.             ckstrncat(strTmp,".conf",sizeof(strTmp));
  6647.             tconf = t_openconfbyname(strTmp);
  6648.         }
  6649.  
  6650.         if ( tpw && tconf )
  6651.             ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6652.         else
  6653.             ts = t_serveropen(szUserNameRequested);
  6654.         if ( tpw ) {
  6655.             t_closepw(tpw);
  6656.             tpw = NULL;
  6657.         }
  6658.         if ( tconf ) {
  6659.             t_closeconf(tconf);
  6660.             tconf = NULL;
  6661.         }
  6662. #else /* COMMENT */
  6663.         ts = t_serveropen(szUserNameRequested);
  6664. #endif /* COMMENT */
  6665. #endif /* PRE_SRP_1_4_4 */
  6666.  
  6667.         if( ts == NULL ) {
  6668.             printf("User %s not found\r\n", szUserNameRequested);
  6669.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  6670.             return(AUTH_FAILURE);
  6671.         }
  6672.  
  6673.     pbuf = (char *)malloc(ts->n.len + ts->g.len + ts->s.len + 7);
  6674.     ptr = pbuf;
  6675.  
  6676.         srp_encode_length(ptr, ts->n.len);
  6677.         ptr += 2;
  6678.         memcpy(ptr, ts->n.data, ts->n.len);     /* safe */
  6679.         ptr += ts->n.len;
  6680.  
  6681.         srp_encode_length(ptr, ts->g.len);
  6682.         ptr += 2;
  6683.         memcpy(ptr, ts->g.data, ts->g.len);     /* safe */
  6684.         ptr += ts->g.len;
  6685.  
  6686.         srp_encode_length(ptr, ts->s.len);
  6687.         ptr += 2;
  6688.         memcpy(ptr, ts->s.data, ts->s.len);     /* safe */
  6689.         ptr += ts->s.len;
  6690.  
  6691.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  6692.     free(pbuf); pbuf = NULL;
  6693.  
  6694.         B = t_servergenexp(ts);
  6695.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  6696.         return AUTH_SUCCESS;
  6697.  
  6698.     case SRP_EXP:
  6699.         /* Client is sending A to us, compute challenge & expected response. */
  6700.         if (ts == NULL || B == NULL) {
  6701.             printf("Protocol error: SRP_EXP unexpected\r\n");
  6702.             SendSRPAuthSB(SRP_REJECT,
  6703.                           (void *) "Protocol error: unexpected EXP",
  6704.                           -1
  6705.                           );
  6706.             return(AUTH_FAILURE);
  6707.         }
  6708.  
  6709.         /* Wait until now to send B, since it contains the key to "u" */
  6710.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->len);
  6711.         B = NULL;
  6712.  
  6713. #ifndef PRE_SRP_1_4_5
  6714.         /*
  6715.          * The original SRP AUTH implementation did not protect against
  6716.          * tampering of the auth-type-pairs.  Therefore, when the
  6717.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6718.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6719.          * is set we also insert the SSL/TLS client and server finished
  6720.          * messages to ensure that there is no man in the middle attack
  6721.          * underway on the SSL/TLS connection.
  6722.          */
  6723.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  6724.             type_check[0] = AUTHTYPE_SRP;
  6725.             type_check[1] = how;
  6726. #ifdef CK_SSL
  6727.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6728.                 ssl_get_client_finished(&type_check[2],12);
  6729.                 ssl_get_server_finished(&type_check[14],12);
  6730.             }
  6731. #endif /* CK_SSL */
  6732.             t_serveraddexdata(ts,type_check,
  6733. #ifdef CK_SSL
  6734.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  6735. #endif /* CK_SSL */
  6736.                                2);
  6737.         }
  6738. #endif /* PRE_SRP_1_4_5 */
  6739.  
  6740.         A.data = data;
  6741.         A.len = cnt;
  6742.         ptr = t_servergetkey(ts, &A);
  6743.  
  6744.         if(ptr == NULL) {
  6745.             if (1)
  6746.               printf("Security alert: Trivial session key attempted\r\n");
  6747.             SendSRPAuthSB(SRP_REJECT,
  6748.                           (void *) "Trivial session key detected",
  6749.                           -1
  6750.                           );
  6751.             return(AUTH_FAILURE);
  6752.         }
  6753.         srp_waitresp = 1;
  6754.         return AUTH_SUCCESS;
  6755.  
  6756.     case SRP_RESPONSE:
  6757.         /* Got the response; see if it's correct */
  6758.         if (!srp_waitresp ||
  6759.              ts == NULL
  6760.              ) {
  6761.             if (1)
  6762.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  6763.             SendSRPAuthSB(SRP_REJECT,
  6764.                           (void *) "Protocol error: unexpected RESPONSE",
  6765.                           -1
  6766.                           );
  6767.             return(AUTH_FAILURE);
  6768.         }
  6769.         srp_waitresp = 0;       /* we got a response */
  6770.  
  6771.         if (cnt < RESPONSE_LEN) {
  6772.             if (1)
  6773.               printf("Protocol error: malformed response\r\n");
  6774.             SendSRPAuthSB(SRP_REJECT,
  6775.                           (void *) "Protocol error: malformed response",
  6776.                           -1
  6777.                           );
  6778.             return(AUTH_FAILURE);
  6779.         }
  6780.  
  6781.         if (t_serververify(ts, data) == 0) {
  6782.             SendSRPAuthSB(SRP_ACCEPT, t_serverresponse(ts), RESPONSE_LEN);
  6783.             accept_complete = 1;
  6784. #ifdef CK_ENCRYPTION
  6785. #ifdef CK_SSL
  6786.             if (!(ssl_active_flag || tls_active_flag))
  6787. #endif /* CK_SSL */
  6788.             {
  6789.                 hexdump("SRP_RESPONSE ts",ts,sizeof(ts));
  6790.                 hexdump("SRP_RESPONSE session_key",
  6791.                          ts->session_key,
  6792.                          SESSION_KEY_LEN
  6793.                          );
  6794.                 skey.type = SK_GENERIC;
  6795.                 skey.length = SESSION_KEY_LEN;
  6796.                 skey.data = ts->session_key;
  6797.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  6798.             }
  6799. #endif /* CK_ENCRYPTION */
  6800.             auth_finished(AUTH_VALID);
  6801.         }
  6802.         else {
  6803.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  6804.             auth_finished(AUTH_REJECT);
  6805.             return(AUTH_FAILURE);
  6806.         }
  6807.         return AUTH_SUCCESS;
  6808.  
  6809.     default:
  6810.         printf("Unknown SRP option %d\r\n", data[-1]);
  6811.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  6812.         return(AUTH_FAILURE);
  6813.     }
  6814. }
  6815. #else /* PRE_SRP_1_7_3 */
  6816. static int
  6817. #ifdef CK_ANSIC
  6818. new_srp_reply(int how, unsigned char *data, int cnt)
  6819. #else
  6820. new_srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6821. #endif
  6822. {
  6823.     data += 4;                          /* Point to status byte */
  6824.     cnt  -= 4;
  6825.  
  6826.     if(cnt-- < 1) {                     /* Matches with data++ */
  6827.         auth_finished(AUTH_REJECT);
  6828.         return AUTH_FAILURE;
  6829.     }
  6830.  
  6831.     switch(*data++) {
  6832.     case SRP_PARAMS: {
  6833.         struct t_num n;
  6834.         struct t_num g;
  6835.         struct t_num s;
  6836.         cstr * A;
  6837.  
  6838.         if(!szUserName) {
  6839.             printf("No username available\r\n");
  6840.             return(auth_resend(AUTHTYPE_SRP));
  6841.         }
  6842.  
  6843.         n.len = srp_decode_length(data);
  6844.         data += 2;
  6845.         cnt -= 2;
  6846.         if(n.len > cnt) {
  6847.             printf("n too long\r\n");
  6848.             return(auth_resend(AUTHTYPE_SRP));
  6849.         }
  6850.         n.data = data;
  6851.         data += n.len;
  6852.         cnt -= n.len;
  6853.  
  6854.         g.len = srp_decode_length(data);
  6855.         data += 2;
  6856.         cnt -= 2;
  6857.         if(g.len > cnt) {
  6858.             printf("g too long\r\n");
  6859.             return(auth_resend(AUTHTYPE_SRP));
  6860.         }
  6861.         g.data = data;
  6862.         data += g.len;
  6863.         cnt -= g.len;
  6864.  
  6865.         s.len = srp_decode_length(data);
  6866.         data += 2;
  6867.         cnt -= 2;
  6868.         if(s.len != cnt) {
  6869.             printf("invalid salt\r\n");
  6870.             return(auth_resend(AUTHTYPE_SRP));
  6871.         }
  6872.         s.data = data;
  6873.         data += s.len;
  6874.         cnt -= s.len;
  6875.  
  6876.         /* If the parameters provided by the server cannot be
  6877.          * validated the following function will fail.
  6878.          */
  6879.         c_srp = SRP_new(SRP_RFC2945_client_method());
  6880.         if (c_srp == NULL ||
  6881.         SRP_set_username(c_srp, szUserName) != SRP_SUCCESS ||
  6882.         SRP_set_params(c_srp,n.data,n.len,g.data,g.len,s.data,s.len) !=
  6883.         SRP_SUCCESS) {
  6884.             printf("SRP Parameter initialization error\r\n");
  6885.             return(auth_resend(AUTHTYPE_SRP));
  6886.         }
  6887.  
  6888.         A = cstr_new();
  6889.         if(SRP_gen_pub(c_srp, &A) != SRP_SUCCESS) {
  6890.             printf("SRP Error generating key exchange\r\n");
  6891.             return(auth_resend(AUTHTYPE_SRP));
  6892.         }
  6893.  
  6894.         SendSRPAuthSB(SRP_EXP, A->data, A->length);
  6895.         cstr_free(A);
  6896.  
  6897.         if ( pwbuf[0] && pwflg ) {
  6898.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6899.                    8 * n.len,
  6900.                    szUserName
  6901.                    );
  6902.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6903. #ifdef OS2
  6904.             if ( pwcrypt )
  6905.                 ck_encrypt((char *)srp_passwd);
  6906. #endif /* OS2 */
  6907.         } else {
  6908.             extern char * srppwprompt;
  6909.             char preface[128];
  6910.             int ok;
  6911.  
  6912.             if (srppwprompt && srppwprompt[0] &&
  6913.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6914.         sizeof(preface)) {
  6915.                 sprintf(preface,srppwprompt,szUserName);
  6916.             } else {
  6917.                 ckmakxmsg( preface,sizeof(preface),
  6918.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6919.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6920.                           NULL, NULL);
  6921.             }
  6922.             ok = uq_txt(preface,"Password: ",2,NULL,
  6923.                         srp_passwd,sizeof(srp_passwd)-1,NULL,
  6924.             DEFAULT_UQ_TIMEOUT);
  6925.             if ( !ok )
  6926.                 srp_passwd[0] = '\0';
  6927.         }
  6928.  
  6929.         if(SRP_set_auth_password(c_srp, srp_passwd) != SRP_SUCCESS) {
  6930.             memset(srp_passwd, 0, sizeof(srp_passwd));
  6931.             printf("SRP Error setting client password\r\n");
  6932.             return(auth_resend(AUTHTYPE_SRP));
  6933.         }
  6934.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6935.         return AUTH_SUCCESS;
  6936.     }
  6937.     case SRP_CHALLENGE: {
  6938.         char type_check[26];
  6939.         cstr * resp = NULL;
  6940.  
  6941.         if(c_srp == NULL) {
  6942.             printf("SRP protocol error\r\n");
  6943.             return(auth_resend(AUTHTYPE_SRP));
  6944.         }
  6945.  
  6946.         /*
  6947.          * The original SRP AUTH implementation did not protect against
  6948.          * tampering of the auth-type-pairs.  Therefore, when the
  6949.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6950.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6951.          * is set we also insert the SSL/TLS client and server finished
  6952.          * messages to ensure that there is no man in the middle attack
  6953.          * underway on the SSL/TLS connection.
  6954.          */
  6955.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6956.             type_check[0] = AUTHTYPE_SRP;
  6957.             type_check[1] = how;
  6958. #ifdef CK_SSL
  6959.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6960.                 ssl_get_client_finished(&type_check[2],12);
  6961.                 ssl_get_server_finished(&type_check[14],12);
  6962.                 SRP_add_ex_data(c_srp, type_check, 26);
  6963.             } else
  6964. #endif /* CK_SSL */
  6965.                 SRP_add_ex_data(c_srp, type_check, 2);
  6966.         }
  6967.  
  6968.         if(SRP_compute_key(c_srp, &c_key, data, cnt) != SRP_SUCCESS) {
  6969.             printf("SRP ERROR: unable to compute client key\r\n");
  6970.             return(auth_resend(AUTHTYPE_SRP));
  6971.         }
  6972.  
  6973.         resp = cstr_new();
  6974.         if(SRP_respond(c_srp, &resp) != SRP_SUCCESS) {
  6975.             printf("SRP ERROR: unable to compute client response\r\n");
  6976.             return(auth_resend(AUTHTYPE_SRP));
  6977.         }
  6978.         SendSRPAuthSB(SRP_RESPONSE, resp->data, resp->length);
  6979.         cstr_free(resp);
  6980.         srp_waitresp = 1;
  6981.         return AUTH_SUCCESS;
  6982.     }
  6983.     case SRP_ACCEPT: {
  6984. #ifdef CK_ENCRYPTION
  6985.         Session_Key skey;
  6986. #endif /* ENCRYPTION */
  6987.  
  6988.         if(cnt < RESPONSE_LEN || !srp_waitresp || c_srp == NULL) {
  6989.             printf("SRP Protocol error\r\n");
  6990.             return(auth_resend(AUTHTYPE_SRP));
  6991.         }
  6992.         srp_waitresp = 0;
  6993.  
  6994.         if(SRP_verify(c_srp, data, cnt) == SRP_SUCCESS) {
  6995.             printf("SRP accepts you as %s\r\n",szUserName);
  6996.  
  6997. #ifdef CK_SSL
  6998.             if((ssl_active_flag || tls_active_flag) &&
  6999.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7000.                 printf("TLS session parameters verified by SRP\r\n");
  7001.             } else
  7002. #endif /* CK_SSL */
  7003. #ifdef CK_ENCRYPTION
  7004.             {
  7005.                 skey.type = SK_GENERIC;
  7006.                 skey.length = c_key->length;
  7007.                 skey.data = c_key->data;
  7008.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  7009.                 cstr_clear_free(c_key);
  7010.                 c_key = NULL;
  7011.             }
  7012. #endif /* CK_ENCRYPTION */
  7013.             accept_complete = 1;
  7014.             auth_finished(AUTH_VALID);
  7015.             SRP_free(c_srp);
  7016.             c_srp = NULL;
  7017.             return AUTH_SUCCESS;
  7018.         }
  7019.         else {
  7020.             printf("[ Error: SRP server authentication failed ]\r\n");
  7021.             return(auth_resend(AUTHTYPE_SRP));
  7022.         }
  7023.     }
  7024.     case SRP_REJECT: {
  7025.         char * str=NULL;
  7026.  
  7027.         ckmakmsg(strTmp,sizeof(strTmp),
  7028.                   "SRP refuses authentication for '",szUserName,
  7029.                   "'\r\n",NULL);
  7030.         if (cnt > 0) {
  7031.             int len = strlen(strTmp);
  7032.             if ( len + cnt < sizeof(strTmp) ) {
  7033.                 str = strTmp + strlen(strTmp);
  7034.                 memcpy(str,data,cnt);
  7035.                 str[cnt] = 0;
  7036.             }
  7037.         }
  7038.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  7039.         auth_finished(AUTH_REJECT);
  7040.         return AUTH_FAILURE;
  7041.     }
  7042.     default:
  7043.         printf("Unknown SRP option %d\r\n", data[-1]);
  7044.         return(auth_resend(AUTHTYPE_SRP));
  7045.     }
  7046.     /* NEVER REACHED */
  7047. }
  7048.  
  7049. static int
  7050. #ifdef CK_ANSIC
  7051. new_srp_is(int how, unsigned char *data, int cnt)
  7052. #else
  7053. new_srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  7054. #endif
  7055. {
  7056.     char * pbuf = NULL;
  7057.     char * ptr;
  7058. #ifdef CK_ENCRYPTION
  7059.     Session_Key skey;
  7060. #endif
  7061.     static cstr * B = NULL;             /* Holder for B */
  7062.     struct t_passwd * pass;
  7063.     cstr * resp;
  7064.     char type_check[26];
  7065.  
  7066.     if ((cnt -= 4) < 1) {
  7067.         auth_finished(AUTH_REJECT);
  7068.         return AUTH_FAILURE;
  7069.     }
  7070.  
  7071.     data += 4;
  7072.     cnt  -= 1;
  7073.     switch(*data++) {
  7074.     case SRP_AUTH:
  7075.         /* Send parameters back to client */
  7076.         if(s_srp != NULL) {
  7077.             SRP_free(s_srp);
  7078.             s_srp = NULL;
  7079.         }
  7080.         if (B != NULL) {
  7081.             cstr_free(B);
  7082.             B = NULL;
  7083.         }
  7084.         if(!szUserNameRequested[0]) {
  7085.             if (1)
  7086.                 printf("No username available\r\n");
  7087.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  7088.             auth_finished(AUTH_REJECT);
  7089.             return(AUTH_FAILURE);
  7090.         }
  7091. #ifdef IKSD
  7092. #ifdef CK_LOGIN
  7093.         if (inserver && ckxanon &&
  7094.              !strcmp(szUserNameRequested,"anonymous")) {
  7095.             SendSRPAuthSB(SRP_REJECT, (void *)
  7096.             "anonymous login cannot be performed with Secure Remote Password",
  7097.             -1);
  7098.             auth_finished(AUTH_REJECT);
  7099.             return(AUTH_FAILURE);
  7100.         }
  7101. #endif /* CK_LOGIN */
  7102. #endif /* IKSD */
  7103.         s_srp = SRP_new(SRP_RFC2945_server_method());
  7104.         if(s_srp == NULL) {
  7105.             printf("Error initializing SRP server\r\n");
  7106.             SendSRPAuthSB(SRP_REJECT,
  7107.                           (void *) "SRP server init failed",
  7108.                           -1
  7109.                           );
  7110.             return(AUTH_FAILURE);
  7111.         }
  7112.         pass = gettpnam(szUserNameRequested);
  7113.         if(pass == NULL) {
  7114.             printf("User %s not found\r\n", szUserNameRequested);
  7115.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  7116.             return(AUTH_FAILURE);
  7117.         }
  7118.         if(SRP_set_username(s_srp, szUserNameRequested) != SRP_SUCCESS ||
  7119.        SRP_set_params(s_srp, pass->tc.modulus.data,
  7120.               pass->tc.modulus.len,
  7121.               pass->tc.generator.data,
  7122.               pass->tc.generator.len,
  7123.               pass->tp.salt.data,
  7124.               pass->tp.salt.len) != SRP_SUCCESS ||
  7125.        SRP_set_authenticator(s_srp,
  7126.                  pass->tp.password.data,
  7127.                  pass->tp.password.len) != SRP_SUCCESS) {
  7128.             printf("Error initializing SRP parameters\r\n");
  7129.             SendSRPAuthSB(SRP_REJECT,(void *)"SRP parameter init failed", -1);
  7130.             return(AUTH_FAILURE);
  7131.         }
  7132.  
  7133.     pbuf = (char *)malloc(pass->tc.modulus.len + pass->tc.generator.len +
  7134.                    pass->tp.salt.len + 7);
  7135.         ptr = pbuf;
  7136.  
  7137.         srp_encode_length(ptr, pass->tc.modulus.len);
  7138.         ptr += 2;
  7139.         memcpy(ptr, pass->tc.modulus.data, pass->tc.modulus.len);
  7140.         ptr += pass->tc.modulus.len;
  7141.  
  7142.         srp_encode_length(ptr, pass->tc.generator.len);
  7143.         ptr += 2;
  7144.         memcpy(ptr, pass->tc.generator.data, pass->tc.generator.len);
  7145.         ptr += pass->tc.generator.len;
  7146.  
  7147.         srp_encode_length(ptr, pass->tp.salt.len);
  7148.         ptr += 2;
  7149.         memcpy(ptr, pass->tp.salt.data, pass->tp.salt.len);
  7150.         ptr += pass->tp.salt.len;
  7151.  
  7152.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  7153.     free(pbuf);
  7154.     pbuf = NULL;
  7155.  
  7156.         if(SRP_gen_pub(s_srp, &B) != SRP_SUCCESS) {
  7157.             printf("Error generating SRP public value\r\n");
  7158.             SendSRPAuthSB(SRP_REJECT, (void *) "SRP_gen_pub failed", -1);
  7159.             return(AUTH_FAILURE);
  7160.         }
  7161.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  7162.         return AUTH_SUCCESS;
  7163.  
  7164.     case SRP_EXP:
  7165.       /* Client is sending A to us, compute challenge and expected response. */
  7166.         if (s_srp == NULL || B == NULL) {
  7167.         printf("Protocol error: SRP_EXP unexpected\r\n");
  7168.         SendSRPAuthSB(SRP_REJECT,
  7169.               (void *)"Protocol error: unexpected EXP", -1);
  7170.         return(AUTH_FAILURE);
  7171.     }
  7172.         /* Wait until now to send B, since it contains the key to "u" */
  7173.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->length);
  7174.         cstr_free(B);
  7175.         B = NULL;
  7176.  
  7177.         /*
  7178.          * The original SRP AUTH implementation did not protect against
  7179.          * tampering of the auth-type-pairs.  Therefore, when the
  7180.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  7181.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  7182.          * is set we also insert the SSL/TLS client and server finished
  7183.          * messages to ensure that there is no man in the middle attack
  7184.          * underway on the SSL/TLS connection.
  7185.          */
  7186.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  7187.             type_check[0] = AUTHTYPE_SRP;
  7188.             type_check[1] = how;
  7189. #ifdef CK_SSL
  7190.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7191.                 ssl_get_client_finished(&type_check[2],12);
  7192.                 ssl_get_server_finished(&type_check[14],12);
  7193.                 SRP_add_ex_data(s_srp, type_check, 26);
  7194.             } else
  7195. #endif /* CK_SSL */
  7196.                 SRP_add_ex_data(s_srp, type_check, 2);
  7197.         }
  7198.  
  7199.         if(SRP_compute_key(s_srp, &s_key, data, cnt) != SRP_SUCCESS) {
  7200.             printf("Security alert: Trivial session key attempted\r\n");
  7201.             SendSRPAuthSB(SRP_REJECT,
  7202.               (void *) "Trivial session key detected", -1);
  7203.             return(AUTH_FAILURE);
  7204.         }
  7205.         srp_waitresp = 1;
  7206.         return AUTH_SUCCESS;
  7207.  
  7208.     case SRP_RESPONSE:
  7209.         /* Got the response; see if it's correct */
  7210.         if (!srp_waitresp || s_srp == NULL) {
  7211.             if (1)
  7212.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  7213.             SendSRPAuthSB(SRP_REJECT,
  7214.                           (void *) "Protocol error: unexpected RESPONSE",
  7215.                           -1
  7216.                           );
  7217.             return(AUTH_FAILURE);
  7218.         }
  7219.         srp_waitresp = 0;       /* we got a response */
  7220.  
  7221.         if (cnt < RESPONSE_LEN) {
  7222.             if (1)
  7223.               printf("Protocol error: malformed response\r\n");
  7224.             SendSRPAuthSB(SRP_REJECT,
  7225.                           (void *) "Protocol error: malformed response",
  7226.                           -1
  7227.                           );
  7228.             return(AUTH_FAILURE);
  7229.         }
  7230.  
  7231.         if(SRP_verify(s_srp, data, cnt) == SRP_SUCCESS) {
  7232.             resp = cstr_new();
  7233.             if(SRP_respond(s_srp, &resp) != SRP_SUCCESS) {
  7234.                 printf("Error computing response\r\n");
  7235.                 SendSRPAuthSB(SRP_REJECT,
  7236.                               (void *) "Error computing response", -1);
  7237.                 return(AUTH_FAILURE);
  7238.             }
  7239.             SendSRPAuthSB(SRP_ACCEPT, resp->data, resp->length);
  7240.             accept_complete = 1;
  7241.             cstr_free(resp);
  7242.  
  7243. #ifdef CK_ENCRYPTION
  7244. #ifdef CK_SSL
  7245.             if (!(ssl_active_flag || tls_active_flag))
  7246. #endif /* CK_SSL */
  7247.             {
  7248.                 skey.type = SK_GENERIC;
  7249.                 skey.length = s_key->length;
  7250.                 skey.data = s_key->data;
  7251.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  7252.                 cstr_clear_free(s_key);
  7253.                 s_key = NULL;
  7254.             }
  7255. #endif /* CK_ENCRYPTION */
  7256.             auth_finished(AUTH_VALID);
  7257.         }
  7258.         else {
  7259.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  7260.             auth_finished(AUTH_REJECT);
  7261.             return(AUTH_FAILURE);
  7262.         }
  7263.         return AUTH_SUCCESS;
  7264.  
  7265.     default:
  7266.         printf("Unknown SRP option %d\r\n", data[-1]);
  7267.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  7268.         return(AUTH_FAILURE);
  7269.     }
  7270. }
  7271. #endif /* PRE_SRP_1_7_3 */
  7272. #endif /* SRP */
  7273.  
  7274. #ifdef KRB5
  7275. #ifdef KINIT
  7276. /*
  7277.  * clients/kinit/kinit.c
  7278.  *
  7279.  * Copyright 1990 by the Massachusetts Institute of Technology.
  7280.  * All Rights Reserved.
  7281.  *
  7282.  * Export of this software from the United States of America may
  7283.  *   require a specific license from the United States Government.
  7284.  *   It is the responsibility of any person or organization contemplating
  7285.  *   export to obtain such a license before exporting.
  7286.  *
  7287.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  7288.  * distribute this software and its documentation for any purpose and
  7289.  * without fee is hereby granted, provided that the above copyright
  7290.  * notice appear in all copies and that both that copyright notice and
  7291.  * this permission notice appear in supporting documentation, and that
  7292.  * the name of M.I.T. not be used in advertising or publicity pertaining
  7293.  * to distribution of the software without specific, written prior
  7294.  * permission.  M.I.T. makes no representations about the suitability of
  7295.  * this software for any purpose.  It is provided "as is" without express
  7296.  * or implied warranty.
  7297.  *
  7298.  *
  7299.  * Initialize a credentials cache.
  7300.  */
  7301.  
  7302. #define KRB5_DEFAULT_OPTIONS 0
  7303. #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
  7304.  
  7305. static krb5_data tgtname = {
  7306. #ifndef HEIMDAL
  7307.     0,
  7308. #endif /* HEIMDAL */
  7309.     KRB5_TGS_NAME_SIZE,
  7310.     KRB5_TGS_NAME
  7311. };
  7312.  
  7313. /* Internal prototypes */
  7314. _PROTOTYP(static krb5_error_code krb5_validate_tgt,
  7315.         (krb5_context, krb5_ccache,krb5_principal, krb5_data *));
  7316. _PROTOTYP(static krb5_error_code krb5_renew_tgt,
  7317.         (krb5_context, krb5_ccache,
  7318.                         krb5_principal, krb5_data *));
  7319. _PROTOTYP(static krb5_error_code krb5_tgt_gen,
  7320.         (krb5_context, krb5_ccache,
  7321.                         krb5_principal, krb5_data *, int opt));
  7322.  
  7323. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7324. static krb5_error_code KRB5_CALLCONV
  7325. ck_krb5_prompter( krb5_context context,
  7326.                   void *data,
  7327.                   const char *name,
  7328.                   const char *banner,
  7329.                   int num_prompts,
  7330.                   krb5_prompt prompts[])
  7331. {
  7332.     krb5_error_code     errcode = 0;
  7333.     int                 i;
  7334. #ifdef KUI
  7335.     struct txtbox * tb = NULL;
  7336. #else /* KUI */
  7337.     char * prompt = NULL;
  7338. #endif /* KUI */
  7339.     int    len = 0, blen=0, nlen=0;
  7340.  
  7341.     debug(F110,"ck_krb5_prompter name",name,0);
  7342.     debug(F110,"ck_krb5_prompter banner",banner,0);
  7343.     debug(F101,"ck_krb5_prompter num_prompts","",num_prompts);
  7344.  
  7345.     if (name)
  7346.         nlen = strlen(name)+2;
  7347.  
  7348.     if (banner)
  7349.         blen = strlen(banner)+2;
  7350.  
  7351. #ifdef KUI
  7352.     tb = (struct txtbox *) malloc(sizeof(struct txtbox) * num_prompts);
  7353.     if ( tb != NULL ) {
  7354.         int ok;
  7355.         memset(tb,0,sizeof(struct txtbox) * num_prompts);
  7356.         for ( i=0; i < num_prompts; i++ ) {
  7357.             tb[i].t_buf = prompts[i].reply->data;
  7358.             tb[i].t_len = prompts[i].reply->length;
  7359.             tb[i].t_lbl = prompts[i].prompt;
  7360.             tb[i].t_dflt = NULL;
  7361.             tb[i].t_echo = (prompts[i].hidden ? 2 : 1);
  7362.         }   
  7363.  
  7364.         ok = uq_mtxt((char *)banner,NULL,num_prompts,tb);
  7365.         if ( ok ) {
  7366.             for ( i=0; i < num_prompts; i++ )
  7367.                 prompts[i].reply->length = strlen(prompts[i].reply->data);
  7368.         } else
  7369.             errcode = -2;
  7370.     }
  7371. #else /* KUI */
  7372.     for (i = 0; i < num_prompts; i++) {
  7373.         debug(F111,"ck_krb5_prompter prompt",prompts[i].prompt,i);
  7374.  
  7375.         if ( prompt && len < (nlen + blen + strlen(prompts[i].prompt)+2) ) {
  7376.             free(prompt);
  7377.             prompt = NULL;
  7378.         }
  7379.         if ( !prompt )
  7380.             prompt = (char *)malloc(nlen + blen + strlen(prompts[i].prompt)+2);
  7381.         if ( !prompt ) {
  7382.             errcode = KRB5_RC_MALLOC;
  7383.             goto cleanup;
  7384.         }
  7385.         len = nlen + blen + strlen(prompts[i].prompt)+2;
  7386.         ckmakxmsg(prompt,len,
  7387.                  (char *) (name?name:""),
  7388.                  name?"\r\n":"",
  7389.                  (char *) (banner?banner:""),
  7390.                  banner?"\r\n":"",
  7391.                  (char *)prompts[i].prompt,
  7392.                  ": ",NULL,NULL,NULL,NULL,NULL,NULL);
  7393.  
  7394.         memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7395.         if (prompts[i].hidden) {
  7396.             readpass(prompt, prompts[i].reply->data,
  7397.                       prompts[i].reply->length);
  7398.         } else {
  7399.             readtext(prompt, prompts[i].reply->data,
  7400.                       prompts[i].reply->length);
  7401.         }
  7402.         prompts[i].reply->length = strlen(prompts[i].reply->data);
  7403.     }
  7404. #endif /* KUI */
  7405.  
  7406.   cleanup:
  7407. #ifdef KUI
  7408.     if ( tb )
  7409.         free(tb);
  7410. #else /* KUI */
  7411.     if ( prompt )
  7412.         free(prompt);
  7413. #endif /* KUI */
  7414.     if (errcode) {
  7415.         for (i = 0; i < num_prompts; i++) {
  7416.             memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7417.         }
  7418.     }
  7419.     return errcode;
  7420. }
  7421.  
  7422. /*
  7423.  *      I'm not really sure what to do with this.  The NRL DLLs use a
  7424.  *      different interface for the krb5_prompter callback.  It has
  7425.  *      one less parameter.   This is going to be ugly.
  7426.  */
  7427. static krb5_error_code KRB5_CALLCONV
  7428. ck_NRL_krb5_prompter( krb5_context context,
  7429.                       const char *name,
  7430.                       const char *banner,
  7431.                       int num_prompts,
  7432.                       krb5_prompt prompts[])
  7433. {
  7434.     return(ck_krb5_prompter(context,NULL,name,banner,num_prompts,prompts));
  7435. }
  7436. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7437.  
  7438. #ifdef KRB524_CONV
  7439. long
  7440. try_convert524(krb5_context ctx, krb5_principal me, krb5_ccache cc)
  7441. {
  7442.     char * progname = "convert524";
  7443.     krb5_error_code code = 0;
  7444.     int icode = 0;
  7445.     krb5_principal kpcserver = 0;
  7446.     krb5_creds *v5creds = 0;
  7447.     krb5_creds increds;
  7448. #ifdef OS2
  7449.     LEASH_CREDENTIALS v4creds;
  7450. #else /* OS2 */
  7451.     CREDENTIALS v4creds;
  7452. #endif /* OS2 */
  7453.  
  7454.     memset((char *) &increds, 0, sizeof(increds));
  7455.     /*
  7456.       From this point on, we can goto cleanup because increds is
  7457.       initialized.
  7458.     */
  7459.  
  7460.     if ((code = krb5_build_principal(ctx,
  7461.                                      &kpcserver,
  7462.                                      krb5_princ_realm(ctx, me)->length,
  7463.                                      krb5_princ_realm(ctx, me)->data,
  7464.                                      "krbtgt",
  7465.                                      krb5_princ_realm(ctx, me)->data,
  7466.                                      NULL))) {
  7467.         com_err(progname, code,
  7468.                 "while creating service principal name");
  7469.         goto cleanup;
  7470.     }
  7471.  
  7472.     memset((char*) &increds, 0, sizeof(increds));
  7473.     increds.client = me;
  7474.     increds.server = kpcserver;
  7475.     /* Prevent duplicate free calls.  */
  7476.     kpcserver = 0;
  7477.  
  7478.     increds.times.endtime = 0;
  7479.     increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
  7480.     if ((code = krb5_get_credentials(ctx, 0,
  7481.                                      cc,
  7482.                                      &increds,
  7483.                                      &v5creds))) {
  7484.         com_err(progname, code,
  7485.                 "getting V5 credentials");
  7486.         goto cleanup;
  7487.     }
  7488.     if ((icode = krb524_convert_creds_kdc(ctx,
  7489.                                           v5creds,
  7490.                                           &v4creds))) {
  7491.         com_err(progname, icode,
  7492.                 "converting to V4 credentials");
  7493.         goto cleanup;
  7494.     }
  7495.     /* this is stolen from the v4 kinit */
  7496.     /* initialize ticket cache */
  7497.     if ((icode = krb_in_tkt(v4creds.pname, v4creds.pinst, v4creds.realm)
  7498.          != KSUCCESS)) {
  7499.         com_err(progname, icode,
  7500.                 "trying to create the V4 ticket file");
  7501.         goto cleanup;
  7502.     }
  7503.     /* stash ticket, session key, etc. for future use */
  7504.     if ((icode = krb_save_credentials(v4creds.service,
  7505.                                       v4creds.instance,
  7506.                                       v4creds.realm,
  7507.                                       v4creds.session,
  7508.                                       v4creds.lifetime,
  7509.                                       v4creds.kvno,
  7510.                                       &(v4creds.ticket_st),
  7511.                                       v4creds.issue_date))) {
  7512.         com_err(progname, icode,
  7513.                 "trying to save the V4 ticket");
  7514.         goto cleanup;
  7515.     }
  7516.  
  7517.  cleanup:
  7518.     memset(&v4creds, 0, sizeof(v4creds));
  7519.     if (v5creds)
  7520.         krb5_free_creds(ctx, v5creds);
  7521.     increds.client = 0;
  7522.     krb5_free_cred_contents(ctx, &increds);
  7523.     if (kpcserver)
  7524.         krb5_free_principal(ctx, kpcserver);
  7525.     return !(code || icode);
  7526. }
  7527. #endif /* KRB524_CONV */
  7528.  
  7529. #define NO_KEYTAB
  7530.  
  7531. int
  7532. #ifdef CK_ANSIC
  7533. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  7534.                  struct krb4_init_data * k4_init)
  7535. #else
  7536. ck_krb5_initTGT(op,init,k4_init)
  7537.     krb_op_data * op; struct krb5_init_data * init;
  7538.     struct krb4_init_data * k4_init;
  7539. #endif /* CK_ANSIC*/
  7540. {
  7541.     krb5_context kcontext;
  7542.     krb5_ccache ccache = NULL;
  7543.     krb5_deltat lifetime = KRB5_DEFAULT_LIFE;   /* -l option */
  7544.     krb5_timestamp starttime = 0;
  7545.     krb5_deltat rlife = 0;
  7546.     int options = KRB5_DEFAULT_OPTIONS;
  7547.     int option;
  7548.     int errflg = 0;
  7549.     krb5_error_code code;
  7550.     krb5_principal me=NULL;
  7551.     krb5_principal server=NULL;
  7552.     krb5_creds my_creds;
  7553.     krb5_timestamp now;
  7554. #ifndef HEIMDAL
  7555.     krb5_address **addrs = (krb5_address **)0;
  7556. #endif /* HEIMDAL */
  7557.     int addr_count=0;
  7558.     int i,j;
  7559. #ifndef NO_KEYTAB
  7560.     int use_keytab = 0;                 /* -k option */
  7561.     krb5_keytab keytab = NULL;
  7562. #endif /* NO_KEYTAB */
  7563.     struct passwd *pw = 0;
  7564.     int pwsize;
  7565.     char *client_name=NULL, principal[256]="", realm[256]="", numstr[40]="";
  7566.     char *password=NULL, passwd[80]="";
  7567. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7568.     krb5_get_init_creds_opt opts;
  7569. #endif
  7570.     char * name;
  7571.     int len;
  7572.  
  7573.     if ( !ck_krb5_is_installed() )
  7574.         return(-1);
  7575.  
  7576. #ifdef COMMENT
  7577.     printf("Kerberos V initialization\r\n");
  7578. #endif /* COMMENT */
  7579.  
  7580.     code = krb5_init_context(&kcontext);
  7581.     if (code) {
  7582.         com_err("krb5_kinit",code,"while init_context");
  7583.         krb5_errno = code;
  7584.         makestr(&krb5_errmsg,error_message(krb5_errno));
  7585.         return(-1);
  7586.     }
  7587.  
  7588.     debug(F110,"krb5_init","krb5_init_context",0);
  7589.  
  7590.     if ((code = krb5_timeofday(kcontext, &now))) {
  7591.         com_err("krb5_kinit",code,"while getting time of day");
  7592.         goto exit_k5_init;
  7593.     }
  7594.  
  7595. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7596.     memset(&opts, 0, sizeof(opts));
  7597.     krb5_get_init_creds_opt_init(&opts);
  7598.     debug(F110,"krb5_init","krb5_get_init_creds_opt_init",0);
  7599. #endif
  7600.  
  7601.     if ( init->renewable ) {
  7602.         options |= KDC_OPT_RENEWABLE;
  7603.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->renewable),"m",NULL,NULL);
  7604. #ifdef HEIMDAL
  7605.         code = -1;
  7606. #else /* HEIMDAL */
  7607.         code = krb5_string_to_deltat(numstr, &rlife);
  7608. #endif /* HEIMDAL */
  7609.         if (code != 0 || rlife == 0) {
  7610.             printf("Bad renewable time value %s\r\n", numstr);
  7611.             errflg++;
  7612.         }
  7613. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7614.         krb5_get_init_creds_opt_set_renew_life(&opts, rlife);
  7615. #endif
  7616.     }
  7617.     if ( init->renew ) {
  7618.         /* renew the ticket */
  7619.         options |= KDC_OPT_RENEW;
  7620.     }
  7621.  
  7622.     if ( init->validate ) {
  7623.         /* validate the ticket */
  7624.         options |= KDC_OPT_VALIDATE;
  7625.     }
  7626.     if ( init->proxiable ) {
  7627.         options |= KDC_OPT_PROXIABLE;
  7628. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7629.         krb5_get_init_creds_opt_set_proxiable(&opts, 1);
  7630. #endif
  7631.     }
  7632.     if ( init->forwardable ) {
  7633.         options |= KDC_OPT_FORWARDABLE;
  7634. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7635.         krb5_get_init_creds_opt_set_forwardable(&opts, 1);
  7636. #endif
  7637.     }
  7638. #ifndef NO_KEYTAB
  7639.     if (  ) {
  7640.         use_keytab = 1;
  7641.     }
  7642.     if (  ) {
  7643.         if (keytab == NULL && keytab_name != NULL) {
  7644.             code = krb5_kt_resolve(kcontext, keytab_name, &keytab);
  7645.             if (code != 0) {
  7646.                 debug(F111,"krb5_init resolving keytab",
  7647.                          keytab_name,code);
  7648.                 errflg++;
  7649.             }
  7650.         }
  7651.     }
  7652. #endif /* NO_KEYTAB */
  7653.     if ( init->lifetime ) {
  7654.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->lifetime),"m",NULL,NULL);
  7655. #ifdef HEIMDAL
  7656.         code = -1;
  7657. #else /* HEIMDAL */
  7658.         code = krb5_string_to_deltat(numstr, &lifetime);
  7659. #endif /* HEIMDAL */
  7660.         if (code != 0 || lifetime == 0) {
  7661.             printf("Bad lifetime value %s\r\n", numstr);
  7662.             errflg++;
  7663.         }
  7664. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7665.         krb5_get_init_creds_opt_set_tkt_life(&opts, lifetime);
  7666. #endif
  7667.     }
  7668.     if ( init->postdate ) {
  7669.         /* Convert cmdate() to a time_t value */
  7670.         struct tm * time_tm;
  7671.         struct tm * cmdate2tm(char *,int);
  7672.         time_tm = cmdate2tm(init->postdate,0);
  7673.         if ( time_tm )
  7674.             starttime = (krb5_timestamp) mktime(time_tm);
  7675.  
  7676.         if (code != 0 || starttime == 0 || starttime == -1) {
  7677.             krb5_deltat ktmp;
  7678. #ifdef HEIMDAL
  7679.             code = -1;
  7680. #else /* HEIMDAL */
  7681.             code = krb5_string_to_deltat(init->postdate, &ktmp);
  7682. #endif /* HEIMDAL */
  7683.             if (code == 0 && ktmp != 0) {
  7684.                 starttime = now + ktmp;
  7685.                 options |= KDC_OPT_POSTDATED;
  7686.             } else {
  7687.                 printf("Bad postdate start time value %s\r\n",
  7688.                         init->postdate);
  7689.                 errflg++;
  7690.             }
  7691.         } else {
  7692.             options |= KDC_OPT_POSTDATED;
  7693.         }
  7694.     }
  7695.  
  7696.     debug(F110,"krb5_init searching for ccache",op->cache,0);
  7697.  
  7698.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  7699.     if (code != 0) {
  7700.         com_err("krb5_kinit",code,"while getting default ccache");
  7701.         goto exit_k5_init;
  7702.     }
  7703.  
  7704.     /* This is our realm unless it is changed */
  7705.     ckstrncpy(realm,init->realm ? init->realm : krb5_d_realm, 256);
  7706.  
  7707. #ifdef BETATEST
  7708.     /* This code is going to take the realm and attempt to correct */
  7709.     /* the case.                                                   */
  7710.     {
  7711.         profile_t profile;
  7712.  
  7713.         code = krb5_get_profile(kcontext, &profile);
  7714.         if ( !code ) {
  7715.             const char  *names[4];
  7716.             char ** realms;
  7717.             int found = 0;
  7718.  
  7719.             names[0] = "realms";
  7720.             names[1] = NULL;
  7721.  
  7722.             code = profile_get_subsection_names(profile,names,&realms);
  7723.             if ( code == 0 ) {
  7724.                 int i=0;
  7725.                 while ( realms[i] ) {
  7726.                     if (ckstrcmp(realm,realms[i],-1,0) == 0) {
  7727.                         strcpy(realm,realms[i]);
  7728.                         found = 1;
  7729.                         break;
  7730.                     }
  7731.                     i++;
  7732.                 }
  7733.             }
  7734.  
  7735. #ifdef CK_DNS_SRV
  7736.             if ( !found ) {
  7737.                 char * dns_realm = NULL;
  7738.  
  7739.                 /* We did not find the realm in the profile so let's try DNS */
  7740.                 locate_txt_rr("_kerberos",realm,&dns_realm);
  7741.                 if ( dns_realm &&
  7742.                      ckstrcmp(realm,dns_realm,-1,0) == 0 &&
  7743.                      ckstrcmp(realm,dns_realm,-1,1) != 0
  7744.                      ) {
  7745.                     ckstrncpy(realm,dns_realm,256);
  7746.                     free(dns_realm);
  7747.                 }
  7748.             }
  7749. #endif /* CK_DNS_SRV */
  7750.         }
  7751.  
  7752.         if (init->realm &&
  7753.              ckstrcmp(realm,init->realm,-1,0) == 0 &&
  7754.              ckstrcmp(realm,init->realm,-1,1) != 0)
  7755.             strcpy(init->realm,realm);
  7756.         if (ckstrcmp(realm,krb5_d_realm,-1,0) == 0 &&
  7757.              ckstrcmp(realm,krb5_d_realm,-1,1) != 0)
  7758.             strcpy(krb5_d_realm,realm);
  7759.     }
  7760. #endif /* BETATEST */
  7761.  
  7762.     if (init->principal == NULL) {       /* No principal name specified */
  7763. #ifndef NO_KEYTAB
  7764.         if (use_keytab) {
  7765.             /* Use the default host/service name */
  7766.             code = krb5_sname_to_principal(kcontext, NULL, NULL,
  7767.                                             KRB5_NT_SRV_HST, &me);
  7768.             if (code == 0 &&
  7769.                 krb5_princ_realm(kcontext, me)->length < sizeof(realm))
  7770.             {
  7771.                 /* Save the realm */
  7772.                 memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  7773.                         krb5_princ_realm(kcontext, me)->length); /* safe */
  7774.                 realm[krb5_princ_realm(kcontext, me)->length]='\0';
  7775.             } else {
  7776.                 com_err("krb5_kinit",
  7777.                         code,
  7778.                         "when creating default server principal name");
  7779.                 goto exit_k5_init;
  7780.             }
  7781.         } else
  7782. #endif /* NO_KEYTAB */
  7783.         {
  7784.             int len;
  7785.             char * name;
  7786.  
  7787.             /* Get default principal from cache if one exists */
  7788.             code = krb5_cc_get_principal(kcontext, ccache, &me);
  7789. #ifdef HEIMDAL
  7790.             name = me->realm;
  7791.             len = strlen(name);
  7792. #else /* HEIMDAL */
  7793.             len = krb5_princ_realm(kcontext, me)->length;
  7794.             name = krb5_princ_realm(kcontext, me)->data;
  7795. #endif /* HEIMDAL */
  7796.             if (code == 0 && len < sizeof(realm))
  7797.             {
  7798.                 /* Save the realm */
  7799.                 memcpy(realm,name,len); /* safe */
  7800.                 realm[len]='\0';
  7801.             } else {
  7802. #ifdef HAVE_PWD_H
  7803.                 /* Else search passwd file for client */
  7804.  
  7805.                 pw = getpwuid((int) getuid());
  7806.                 if (pw) {
  7807.                     char princ_realm[256];
  7808.                     if ( (strlen(pw->pw_name) + strlen(realm) + 1) > 255 )
  7809.                         goto exit_k5_init;
  7810.  
  7811.                     ckstrncpy(principal,pw->pw_name,256);
  7812.                     ckstrncpy(princ_realm,pw->pw_name,256);
  7813.                     ckstrncat(princ_realm,"@",256);
  7814.                     ckstrncat(princ_realm,realm,256);
  7815.  
  7816.                     if ((code = krb5_parse_name(kcontext,princ_realm,&me))) {
  7817.                         krb5_errno = code;
  7818.                         com_err("krb5_kinit",code,"when parsing name",
  7819.                                   princ_realm);
  7820.                         goto exit_k5_init;
  7821.                     }
  7822.                 } else {
  7823.                     printf(
  7824.                         "Unable to identify user from password file\r\n");
  7825.                     goto exit_k5_init;
  7826.                 }
  7827. #else /* HAVE_PWD_H */
  7828.                 printf("Unable to identify user\r\n");
  7829.                 goto exit_k5_init;
  7830. #endif /* HAVE_PWD_H */
  7831.             }
  7832.         }
  7833.  
  7834. #ifdef HEIMDAL
  7835.         len = me->name.name_string.len;
  7836.         name = *me->name.name_string.val;
  7837. #else /* HEIMDAL */
  7838.         len = krb5_princ_name(kcontext, me)->length;
  7839.         name = krb5_princ_name(kcontext, me)->data;
  7840. #endif /* HEIMDAL */
  7841.         if ( len < sizeof(principal) ) {
  7842.             memcpy(principal,name,len);     /* safe */
  7843.             principal[len]='\0';
  7844.         }
  7845.     } /* Use specified name */
  7846.     else {
  7847.         char princ_realm[256];
  7848.         if ( (strlen(init->principal) +
  7849.               (init->instance ? strlen(init->instance)+1 : 0) +
  7850.               strlen(realm)
  7851.               + 2) > 255 )
  7852.              goto exit_k5_init;
  7853.  
  7854.         ckstrncpy(principal,init->principal,256);
  7855.         ckstrncpy(princ_realm,init->principal,256);
  7856.         if (init->instance) {
  7857.             ckstrncat(princ_realm,"/",256);
  7858.             ckstrncat(princ_realm,init->instance,256);
  7859.         }
  7860.         if (realm[0]) {
  7861.           ckstrncat(princ_realm,"@",256);
  7862.           ckstrncat(princ_realm,realm,256);
  7863.         }
  7864.         if ((code = krb5_parse_name (kcontext, princ_realm, &me))) {
  7865.             com_err("krb5_kinit",code,"when parsing name",princ_realm);
  7866.             goto exit_k5_init;
  7867.         }
  7868.     }
  7869.  
  7870.     if ((code = krb5_unparse_name(kcontext, me, &client_name))) {
  7871.         com_err("krb5_kinit",code,"when unparsing name");
  7872.         goto exit_k5_init;
  7873.     }
  7874.     debug(F110,"krb5_init client_name",client_name,0);
  7875.  
  7876.  
  7877.     memset((char *)&my_creds, 0, sizeof(my_creds));
  7878.     my_creds.client = me;
  7879.  
  7880.     if (init->service == NULL) {
  7881.         if ((code =
  7882.              krb5_build_principal_ext(kcontext,
  7883.                                       &server,
  7884.                                       strlen(realm),realm,
  7885.                                       tgtname.length, tgtname.data,
  7886.                                       strlen(realm),realm,
  7887.                                       0))) {
  7888.             com_err("krb5_kinit",code,"while building server name");
  7889.             goto exit_k5_init;
  7890.         }
  7891.     } else {
  7892.         if (code = krb5_parse_name(kcontext, init->service, &server)) {
  7893.             com_err("krb5_kinit",code,"while parsing service name",
  7894.                     init->service);
  7895.             goto exit_k5_init;
  7896.         }
  7897.     }
  7898.  
  7899.     my_creds.server = server;
  7900.  
  7901.     if (options & KDC_OPT_POSTDATED) {
  7902.         my_creds.times.starttime = starttime;
  7903.         my_creds.times.endtime = starttime + lifetime;
  7904.     } else {
  7905.         my_creds.times.starttime = 0;   /* start timer when request
  7906.                                            gets to KDC */
  7907.         my_creds.times.endtime = now + lifetime;
  7908.     }
  7909.     if (options & KDC_OPT_RENEWABLE) {
  7910.         my_creds.times.renew_till = now + rlife;
  7911.     } else
  7912.         my_creds.times.renew_till = 0;
  7913.  
  7914.     if (options & KDC_OPT_VALIDATE) {
  7915.         krb5_data outbuf;
  7916.  
  7917. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7918.         code = krb5_get_validated_creds(kcontext,
  7919.                                         &my_creds, me, ccache, init->service);
  7920.         if ( code == -1 )
  7921. #endif
  7922.         {
  7923. #ifdef HEIMDAL
  7924.             printf("?validate not implemented\r\n");
  7925.             code = -1;
  7926.             goto exit_k5_init;
  7927. #else /* HEIMDAL */
  7928.             code = krb5_validate_tgt(kcontext, ccache, server, &outbuf);
  7929. #endif /* HEIMDAL */
  7930.         }
  7931.         if (code) {
  7932.             com_err("krb5_kinit",code,"validating tgt");
  7933.             goto exit_k5_init;
  7934.         }
  7935.         /* should be done... */
  7936.         goto exit_k5_init;
  7937.     }
  7938.  
  7939.     if (options & KDC_OPT_RENEW) {
  7940.         krb5_data outbuf;
  7941.  
  7942. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7943.         code = krb5_get_renewed_creds(kcontext,
  7944.                                       &my_creds, me, ccache, init->service);
  7945.         if ( code == -1 )
  7946. #endif
  7947.         {
  7948. #ifdef HEIMDAL
  7949.             printf("?renew not implemented\r\n");
  7950.             code = -1;
  7951.             goto exit_k5_init;
  7952. #else /* HEIMDAL */
  7953.             code = krb5_renew_tgt(kcontext, ccache, server, &outbuf);
  7954. #endif /* HEIMDAL */
  7955.         }
  7956.         if (code) {
  7957.             com_err("krb5_kinit",code,"while renewing tgt");
  7958.             goto exit_k5_init;
  7959.         }
  7960.         /* should be done... */
  7961.         goto store_cred;
  7962.     }
  7963.  
  7964. #ifndef HEIMDAL
  7965.     if ( init->addrs && !init->no_addresses ) {
  7966.         /* construct an array of krb5_address structs to pass to get_in_tkt */
  7967.         /* include both the local ip addresses as well as any other that    */
  7968.         /* are specified.                                                   */
  7969.         unsigned long ipaddr;
  7970.  
  7971.         for ( addr_count=0;addr_count<KRB5_NUM_OF_ADDRS;addr_count++ )
  7972.             if ( init->addrs[addr_count] == NULL )
  7973.                 break;
  7974.  
  7975.         if (addr_count > 0) {
  7976.             krb5_address ** local_addrs=NULL;
  7977.             krb5_os_localaddr(kcontext, &local_addrs);
  7978.             i = 0;
  7979.             while ( local_addrs[i] )
  7980.                 i++;
  7981.             addr_count += i;
  7982.  
  7983.             addrs = (krb5_address **)
  7984.               malloc((addr_count+1) * sizeof(krb5_address *));
  7985.             if ( !addrs ) {
  7986.                 krb5_free_addresses(kcontext, local_addrs);
  7987.                 goto exit_k5_init;
  7988.             }
  7989.             memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
  7990.             i = 0;
  7991.             while ( local_addrs[i] ) {
  7992.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7993.                 if (addrs[i] == NULL) {
  7994.                     krb5_free_addresses(kcontext, local_addrs);
  7995.                     goto exit_k5_init;
  7996.                 }
  7997.  
  7998.                 addrs[i]->magic = local_addrs[i]->magic;
  7999.                 addrs[i]->addrtype = local_addrs[i]->addrtype;
  8000.                 addrs[i]->length = local_addrs[i]->length;
  8001.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  8002.                 if (!addrs[i]->contents) {
  8003.                     krb5_free_addresses(kcontext, local_addrs);
  8004.                     goto exit_k5_init;
  8005.                 }
  8006.  
  8007.                 memcpy(addrs[i]->contents,local_addrs[i]->contents,
  8008.                         local_addrs[i]->length);        /* safe */
  8009.                 i++;
  8010.             }
  8011.             krb5_free_addresses(kcontext, local_addrs);
  8012.  
  8013.             for ( j=0;i<addr_count;i++,j++ ) {
  8014.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  8015.                 if (addrs[i] == NULL)
  8016.                     goto exit_k5_init;
  8017.  
  8018.                 addrs[i]->magic = KV5M_ADDRESS;
  8019.                 addrs[i]->addrtype = AF_INET;
  8020.                 addrs[i]->length = 4;
  8021.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  8022.                 if (!addrs[i]->contents)
  8023.                     goto exit_k5_init;
  8024.  
  8025.                 ipaddr = inet_addr(init->addrs[j]);
  8026.                 memcpy(addrs[i]->contents,&ipaddr,4);   /* safe */
  8027.             }
  8028. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8029.             krb5_get_init_creds_opt_set_address_list(&opts,addrs);
  8030. #endif
  8031.         }
  8032.      }
  8033. #endif /* !HEIMDAL */
  8034. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8035.     if ( init->no_addresses )
  8036.         krb5_get_init_creds_opt_set_address_list(&opts,NULL);
  8037. #endif
  8038.  
  8039. #ifndef NO_KEYTAB
  8040.     if (!use_keytab)
  8041. #endif
  8042.     {
  8043.         if ( init->password ) {
  8044.             pwsize = strlen(init->password);
  8045.             if ( pwsize )
  8046.                 password = init->password;
  8047.         } else if (init->getk4 && k4_init) {
  8048.             /* When we are requesting that K4 tickets be automatically */
  8049.             /* acquired when K5 tickets are acquired, we must get the  */
  8050.             /* password up front.                                      */
  8051.             char prmpt[256];
  8052.             extern char * k5prprompt;
  8053.             extern char * k5pwprompt;
  8054.             int ok = 0;
  8055.  
  8056.             if ( k5pwprompt && k5pwprompt[0] &&
  8057.                  (strlen(k5pwprompt) + strlen(principal) +
  8058.                   strlen(realm) - 4) < sizeof(prmpt)) {
  8059.                 sprintf(prmpt,k5pwprompt,principal,realm);
  8060.             } else
  8061.                 ckmakxmsg(prmpt,sizeof(prmpt),
  8062.                            k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  8063.                            "Kerberos 5 Password for ",
  8064.                            principal,"@",realm,": ",
  8065.                            NULL,NULL,NULL,NULL,NULL,NULL,NULL
  8066.                            );
  8067.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  8068.             if ( ok )
  8069.                 password = passwd;
  8070.  
  8071.             if ( k4_init->password == NULL )
  8072.                 makestr(&k4_init->password,passwd);
  8073.         }
  8074. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8075.         debug(F100,"krb5_init calling krb5_get_init_creds_password()","",0);
  8076. #ifdef OS2
  8077.         if ( is_NRL_KRB5() )
  8078.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  8079.                                                  password,
  8080.                                                  (void *)ck_NRL_krb5_prompter,
  8081.                                                  NULL,
  8082.                                                  starttime, init->service,
  8083.                                                  &opts);
  8084.         else
  8085. #endif /* OS2 */
  8086.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  8087.                                                  password,
  8088.                                                  ck_krb5_prompter,
  8089.                                                  NULL,
  8090.                                                  starttime, init->service,
  8091.                                                  &opts);
  8092.         debug(F111,"krb5_init","krb5_get_init_creds_password()",code);
  8093.  
  8094.         if ( code == -1 )
  8095.         {
  8096.             if (!password) {
  8097.                 char prmpt[256];
  8098.                 int ok;
  8099.  
  8100.                 ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  8101.                           client_name,": ",NULL);
  8102.                 ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,
  8103.                 DEFAULT_UQ_TIMEOUT);
  8104.                 if ( ok )
  8105.                     password = passwd;
  8106.                 else {
  8107.                     code = -2;
  8108.                     goto exit_k5_init;
  8109.                 }
  8110.             }
  8111.  
  8112.             if ( !password ) 
  8113.                 password = "";
  8114.             code = krb5_get_in_tkt_with_password(kcontext, options,
  8115. #ifdef HEIMDAL
  8116.                                                   NULL,
  8117. #else /* HEIMDAL */
  8118.                                             init->no_addresses ? NULL :addrs,
  8119. #endif /* HEIMDAL */
  8120.                                                   NULL, NULL,
  8121.                                                   password,
  8122.                                                   NULL, &my_creds, NULL);
  8123.             if ( code )
  8124.                 debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  8125.         }
  8126. #else /* KRB5_HAVE_GET_INIT_CREDS */
  8127.         if (!password) {
  8128.             char prmpt[256];
  8129.             int ok;
  8130.  
  8131.             ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  8132.                       client_name,": ",NULL);
  8133.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  8134.             if ( ok )
  8135.                 password = passwd;
  8136.             else {
  8137.                 code = -2;
  8138.                 goto exit_k5_init;
  8139.             }
  8140.         }
  8141.         if ( !password ) 
  8142.             password = "";
  8143.         code = krb5_get_in_tkt_with_password(kcontext, options,
  8144. #ifdef HEIMDAL
  8145.                                               NULL,
  8146. #else /* HEIMDAL */
  8147.                                             init->no_addresses ? NULL :addrs,
  8148. #endif /* HEIMDAL */
  8149.                                               NULL, NULL,
  8150.                                               password,
  8151.                                               NULL, &my_creds, NULL);
  8152.         if ( code )
  8153.             debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  8154. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  8155.  
  8156.         if ( init->password && pwsize > 0 )
  8157.             memset(init->password, 0, pwsize);
  8158.         memset(passwd,0,80);
  8159.     }
  8160. #ifndef NO_KEYTAB
  8161.     else {
  8162. #ifdef KRB5_HAVE_GET_INIT_CREDS
  8163.         code = krb5_get_init_creds_keytab(kcontext, &my_creds, me, keytab,
  8164.                                            starttime, init->service,
  8165.                                            &opts);
  8166. #ifdef OS2
  8167.         if ( code == -1)
  8168.             code = krb5_get_in_tkt_with_keytab(kcontext, options,
  8169.                                            init->no_addresses ? NULL :addrs,
  8170.                                                 NULL, NULL, keytab, NULL,
  8171.                                                 &my_creds, 0);
  8172. #endif /* OS2 */
  8173. #else /* KRB5_HAVE_GET_INIT_CREDS */
  8174.         code = krb5_get_in_tkt_with_keytab(kcontext, options,
  8175. #ifdef HEIMDAL
  8176.                                                   NULL,
  8177. #else /* HEIMDAL */
  8178.                                             init->no_addresses ? NULL :addrs,
  8179. #endif /* HEIMDAL */
  8180.                                             NULL, NULL, keytab, NULL,
  8181.                                             &my_creds, 0);
  8182. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  8183.     }
  8184. #endif
  8185.  
  8186.     if (code) {
  8187.         switch (code) {
  8188.         case KRB5KRB_AP_ERR_BAD_INTEGRITY:
  8189.             printf("Password incorrect\r\n");
  8190.             goto exit_k5_init;
  8191.         case KRB5KRB_AP_ERR_V4_REPLY:
  8192.             if (init->getk4 && k4_init) {
  8193.                 printf("Kerberos 5 Tickets not support by server.  ");
  8194.                 printf("A version 4 Ticket will be requested.\r\n");
  8195.             }
  8196.             goto exit_k5_init;
  8197.         default:
  8198.             goto exit_k5_init;
  8199.         }
  8200.     }
  8201.  
  8202.   store_cred:
  8203.     debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8204.  
  8205.     code = krb5_cc_initialize (kcontext, ccache, me);
  8206.     if ( code == KRB5_CC_BADNAME ) {
  8207.         /* This is a really ugly hack that should not have to be here.
  8208.          * krb5_cc_initialize should not fail with an error if the
  8209.          * cache already exists.  The reason the problem is occuring
  8210.          * is that the krb5 library is no longer calling cc_destroy()
  8211.          * when cc_initialize() is called and the CCAPI implementation
  8212.          * on Windows has not yet been corrected to handle it.  To
  8213.          * ensure that K95 will continue to work with both we will call
  8214.          * cc_destroy() if the cc_initialize() call fails with a BADNAME
  8215.          * error.  If the cc_destroy() is successful, we will try again.
  8216.          */
  8217.  
  8218.         debug(F100,"krb5_init calling krb5_cc_destroy()","",0);
  8219.         code = krb5_cc_destroy (kcontext, ccache);
  8220.         if ( !code ) {
  8221.             debug(F100,"krb5_init calling k5_get_ccache()","",0);
  8222.             code = k5_get_ccache(kcontext,&ccache,op->cache);
  8223.             debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8224.             code = krb5_cc_initialize (kcontext, ccache, me);
  8225.         } else
  8226.             code = KRB5_CC_BADNAME;
  8227.     }
  8228.     if (code) {
  8229.         com_err("krb5_kinit",code,"when initializing cache",op->cache);
  8230.         goto exit_k5_init;
  8231.     }
  8232.  
  8233.     debug(F100,"krb5_init calling krb5_cc_store_cred()","",0);
  8234.     code = krb5_cc_store_cred(kcontext, ccache, &my_creds);
  8235.     if (code) {
  8236.         com_err("krb5_kinit",code,"while storing credentials");
  8237.         goto exit_k5_init;
  8238.     }
  8239.  
  8240.     if ( init->getk4 && 
  8241. #ifdef KRB524_CONV
  8242.          !try_convert524(kcontext,me,ccache) && 
  8243. #endif /* KRB524_CONV */
  8244.          k4_init ) {
  8245.         int k4rc = ck_krb4_initTGT(op,k4_init);
  8246.         if (k4rc < 0)
  8247.             code = -3;
  8248.     }
  8249.  
  8250. exit_k5_init:
  8251.     debug(F100,"krb5_init exit_k5_init","",0);
  8252.  
  8253. #ifndef HEIMDAL
  8254.     /* Free krb5_address structures if we created them */
  8255.     if ( addrs ) {
  8256.         for ( i=0;i<addr_count;i++ ) {
  8257.             if ( addrs[i] ) {
  8258.                 if ( addrs[i]->contents )
  8259.                     free(addrs[i]->contents);
  8260.                 free(addrs[i]);
  8261.             }
  8262.         }
  8263.     }
  8264. #endif /* HEIMDAL */
  8265.  
  8266.  
  8267.     krb5_errno = code;
  8268.     makestr(&krb5_errmsg,krb5_errno ? error_message(krb5_errno) : "OK");
  8269.  
  8270.     if (client_name)
  8271.         krb5_free_unparsed_name(kcontext, client_name);
  8272.  
  8273.     /* my_creds is pointing at server */
  8274.     debug(F100,"krb5_init calling krb5_free_principal()","",0);
  8275.     krb5_free_principal(kcontext, server);
  8276.     debug(F100,"krb5_init calling krb5_cc_close()","",0);
  8277.     krb5_cc_close(kcontext,ccache);
  8278.     debug(F100,"krb5_init calling krb5_free_context()","",0);
  8279.     krb5_free_context(kcontext);
  8280.  
  8281.     if (code != -2)
  8282.         printf("Result from realm %s: %s\r\n",realm,
  8283.                 code==-3?"Unable to retrieve Kerberos IV credentials":
  8284.                 code?error_message(code):"OK");
  8285.     return(code?-1:0);
  8286. }
  8287.  
  8288. #ifndef HEIMDAL
  8289. #define VALIDATE 0
  8290. #define RENEW 1
  8291.  
  8292. /* stripped down version of krb5_mk_req */
  8293. static krb5_error_code
  8294. #ifdef CK_ANSIC
  8295. krb5_validate_tgt( krb5_context context,
  8296.                    krb5_ccache ccache,
  8297.                    krb5_principal     server, /* tgtname */
  8298.                    krb5_data *outbuf )
  8299. #else
  8300. krb5_validate_tgt(context, ccache, server, outbuf)
  8301.      krb5_context context;
  8302.      krb5_ccache ccache;
  8303.      krb5_principal     server; /* tgtname */
  8304.      krb5_data *outbuf;
  8305. #endif
  8306. {
  8307.     return krb5_tgt_gen(context, ccache, server, outbuf, VALIDATE);
  8308. }
  8309.  
  8310. /* stripped down version of krb5_mk_req */
  8311. static krb5_error_code
  8312. #ifdef CK_ANSIC
  8313. krb5_renew_tgt(krb5_context context,
  8314.                 krb5_ccache ccache,
  8315.                 krb5_principal    server, /* tgtname */
  8316.                 krb5_data *outbuf)
  8317. #else
  8318. krb5_renew_tgt(context, ccache, server, outbuf)
  8319.      krb5_context context;
  8320.      krb5_ccache ccache;
  8321.      krb5_principal       server; /* tgtname */
  8322.      krb5_data *outbuf;
  8323. #endif
  8324. {
  8325.     return krb5_tgt_gen(context, ccache, server, outbuf, RENEW);
  8326. }
  8327.  
  8328.  
  8329. /* stripped down version of krb5_mk_req */
  8330. static krb5_error_code
  8331. #ifdef CK_ANSIC
  8332. krb5_tgt_gen(krb5_context context,
  8333.               krb5_ccache ccache,
  8334.               krb5_principal      server, /* tgtname */
  8335.               krb5_data *outbuf,
  8336.               int opt)
  8337. #else
  8338. krb5_tgt_gen(context, ccache, server, outbuf, opt)
  8339.      krb5_context context;
  8340.      krb5_ccache ccache;
  8341.      krb5_principal       server; /* tgtname */
  8342.      krb5_data *outbuf;
  8343.      int opt;
  8344. #endif
  8345. {
  8346.     krb5_error_code       retval;
  8347.     krb5_creds          * credsp;
  8348.     krb5_creds            creds;
  8349.  
  8350.     /* obtain ticket & session key */
  8351.     memset((char *)&creds, 0, sizeof(creds));
  8352.     if ((retval = krb5_copy_principal(context, server, &creds.server)))
  8353.         goto cleanup;
  8354.  
  8355.     if ((retval = krb5_cc_get_principal(context, ccache, &creds.client)))
  8356.         goto cleanup_creds;
  8357.  
  8358.     if (opt == VALIDATE) {
  8359.         if ((retval = krb5_get_credentials_validate(context, 0,
  8360.                                                     ccache, &creds, &credsp)))
  8361.           goto cleanup_creds;
  8362.     } else {
  8363.         if ((retval = krb5_get_credentials_renew(context, 0,
  8364.                                                  ccache, &creds, &credsp)))
  8365.           goto cleanup_creds;
  8366.     }
  8367.  
  8368.     /* we don't actually need to do the mk_req, just get the creds. */
  8369. cleanup_creds:
  8370.     krb5_free_cred_contents(context, &creds);
  8371.  
  8372. cleanup:
  8373.  
  8374.     return retval;
  8375. }
  8376. #endif /* HEIMDAL */
  8377. #endif /* KINIT */
  8378. #ifdef KDESTROY
  8379. int
  8380. #ifdef CK_ANSIC
  8381. ck_krb5_destroy(struct krb_op_data * op)
  8382. #else
  8383. ck_krb5_destroy(op) struct krb_op_data * op;
  8384. #endif
  8385. {
  8386.     krb5_context kcontext;
  8387.     krb5_error_code retval;
  8388.     int c;
  8389.     krb5_ccache ccache = NULL;
  8390.     char *cache_name = NULL;
  8391.     int code;
  8392.     int errflg=0;
  8393.     int quiet = 0;
  8394.  
  8395.     if ( !ck_krb5_is_installed() )
  8396.         return(-1);
  8397.  
  8398.     code = krb5_init_context(&kcontext);
  8399.     if (code) {
  8400.         debug(F101,"ck_krb5_destroy while initializing krb5","",code);
  8401.         krb5_errno = code;
  8402.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8403.         return(-1);
  8404.     }
  8405.  
  8406.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  8407.     if (code != 0) {
  8408.         debug(F101,"ck_krb5_destroy while getting ccache",
  8409.                "",code);
  8410.         krb5_free_context(kcontext);
  8411.         krb5_errno = code;
  8412.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8413.         return(-1);
  8414.     }
  8415.  
  8416.     code = krb5_cc_destroy (kcontext, ccache);
  8417.     if (code != 0) {
  8418.         debug(F101,"ck_krb5_destroy while destroying cache","",code);
  8419.         if ( code == KRB5_FCC_NOFILE )
  8420.             printf("No ticket cache to destroy.\r\n");
  8421.         else
  8422.             printf("Ticket cache NOT destroyed!\r\n");
  8423.         krb5_cc_close(kcontext,ccache);
  8424.         krb5_free_context(kcontext);
  8425.         krb5_errno = code;
  8426.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8427.         return(-1);
  8428.     }
  8429.  
  8430.     printf("Tickets destroyed.\r\n");
  8431.     /* Do not call krb5_cc_close() because cache has been destroyed */
  8432.     krb5_free_context(kcontext);
  8433.     krb5_errno = 0;
  8434.     makestr(&krb5_errmsg,"OK");
  8435.     return (0);
  8436. }
  8437. #else /* KDESTROY */
  8438. #ifdef KRB5
  8439. int
  8440. #ifdef CK_ANSIC
  8441. ck_krb5_destroy(struct krb_op_data * op)
  8442. #else
  8443. ck_krb5_destroy(op) struct krb_op_data * op;
  8444. #endif
  8445. {
  8446.     printf("?Not implemented.\r\n");
  8447.     return(-1);
  8448. }
  8449. #endif /* KRB5 */
  8450. #endif /* KDESTROY */
  8451. #ifndef KLIST
  8452. #ifdef KRB5
  8453. int
  8454. #ifdef CK_ANSIC
  8455. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8456. #else
  8457. ck_krb5_list_creds(op,lc)
  8458.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8459. #endif
  8460. {
  8461.     printf("?Not implemented.\r\n");
  8462.     return(-1);
  8463. }
  8464. #endif /* KRB5 */
  8465. #else /* KLIST */
  8466. static int show_flags = 0, show_time = 0, status_only = 0, show_keys = 0;
  8467. static int show_etype = 0, show_addr = 0;
  8468. static char *defname;
  8469. static char *progname;
  8470. static krb5_int32 now;
  8471. static int timestamp_width;
  8472.  
  8473. _PROTOTYP(static char * etype_string, (krb5_enctype ));
  8474. _PROTOTYP(static void show_credential,(krb5_context,krb5_creds *));
  8475. _PROTOTYP(static int do_ccache, (krb5_context,char *));
  8476. _PROTOTYP(static int do_keytab, (krb5_context,char *));
  8477. _PROTOTYP(static void printtime, (time_t));
  8478. _PROTOTYP(static void fillit, (int, int));
  8479.  
  8480. #define DEFAULT 0
  8481. #define CCACHE 1
  8482. #define KEYTAB 2
  8483.  
  8484. int
  8485. #ifdef CK_ANSIC
  8486. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8487. #else
  8488. ck_krb5_list_creds(op,lc)
  8489.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8490. #endif
  8491. {
  8492.     krb5_context kcontext;
  8493.     krb5_error_code retval;
  8494.     int code;
  8495.     char *name = op->cache;
  8496.     int mode;
  8497.  
  8498.     if ( !ck_krb5_is_installed() )
  8499.         return(-1);
  8500.  
  8501.     code = krb5_init_context(&kcontext);
  8502.     if (code) {
  8503.         debug(F101,"ck_krb5_list_creds while initializing krb5","",code);
  8504.         krb5_errno = code;
  8505.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8506.         return(-1);
  8507.     }
  8508.  
  8509.     name = op->cache;
  8510.     mode = DEFAULT;
  8511.     show_flags = 0;
  8512.     show_time = 0;
  8513.     status_only = 0;
  8514.     show_keys = 0;
  8515.     show_etype = 0;
  8516.     show_addr = 0;
  8517.  
  8518.     show_flags = lc->flags;
  8519.     show_etype = lc->encryption;
  8520.     show_addr  = lc->addr;
  8521.     show_time = 1;
  8522.     show_keys = 1;
  8523.     mode = CCACHE;
  8524.  
  8525.     if ((code = krb5_timeofday(kcontext, &now))) {
  8526.         if (!status_only)
  8527.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  8528.                    "",code);
  8529.         krb5_free_context(kcontext);
  8530.         krb5_errno = code;
  8531.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8532.         return(-1);
  8533.     }
  8534.     else {
  8535.         char tmp[BUFSIZ];
  8536.  
  8537.         if (!krb5_timestamp_to_sfstring(now, tmp, 20, (char *) NULL) ||
  8538.             !krb5_timestamp_to_sfstring(now, tmp, sizeof(tmp), (char *) NULL))
  8539.             timestamp_width = (int) strlen(tmp);
  8540.         else
  8541.             timestamp_width = 15;
  8542.     }
  8543.  
  8544.     if (mode == DEFAULT || mode == CCACHE)
  8545.          retval = do_ccache(kcontext,name);
  8546.     else
  8547.          retval = do_keytab(kcontext,name);
  8548.     krb5_free_context(kcontext);
  8549.     return(retval);
  8550. }
  8551.  
  8552. static int
  8553. #ifdef CK_ANSIC
  8554. do_keytab(krb5_context kcontext, char * name)
  8555. #else
  8556. do_keytab(kcontext,name) krb5_context kcontext; char * name;
  8557. #endif
  8558. {
  8559.     krb5_keytab kt;
  8560.     krb5_keytab_entry entry;
  8561.     krb5_kt_cursor cursor;
  8562.     char buf[BUFSIZ]; /* hopefully large enough for any type */
  8563.     char *pname;
  8564.     int code = 0;
  8565.  
  8566.     if (name == NULL) {
  8567.         if ((code = krb5_kt_default(kcontext, &kt))) {
  8568.             debug(F101,"ck_krb5_list_creds while getting default keytab",
  8569.                    "",code);
  8570.             krb5_errno = code;
  8571.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8572.             return(-1);
  8573.         }
  8574.     } else {
  8575.         if ((code = krb5_kt_resolve(kcontext, name, &kt))) {
  8576.             debug(F111,"ck_krb5_list_creds while resolving keytab",
  8577.                      name,code);
  8578.             krb5_errno = code;
  8579.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8580.             return(-1);
  8581.         }
  8582.     }
  8583.  
  8584.     if ((code = krb5_kt_get_name(kcontext, kt, buf, BUFSIZ))) {
  8585.         debug(F101,"ck_krb5_list_creds while getting keytab name",
  8586.                "",code);
  8587.         krb5_errno = code;
  8588.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8589.         return(-1);
  8590.     }
  8591.  
  8592.      printf("Keytab name: %s\r\n", buf);
  8593.  
  8594.      if ((code = krb5_kt_start_seq_get(kcontext, kt, &cursor))) {
  8595.          debug(F101,"ck_krb5_list_creds while starting keytab scan",
  8596.                 "",code);
  8597.          krb5_errno = code;
  8598.          makestr(&krb5_errmsg,error_message(krb5_errno));
  8599.          return(-1);
  8600.      }
  8601.  
  8602.      if (show_time) {
  8603.           printf("KVNO Timestamp");
  8604.           fillit(timestamp_width - sizeof("Timestamp") + 2, (int) ' ');
  8605.           printf("Principal\r\n");
  8606.           printf("---- ");
  8607.           fillit(timestamp_width, (int) '-');
  8608.           printf(" ");
  8609.           fillit(78 - timestamp_width - sizeof("KVNO"), (int) '-');
  8610.           printf("\r\n");
  8611.      } else {
  8612.           printf("KVNO Principal\r\n");
  8613.           printf(
  8614. "---- --------------------------------------------------------------------\
  8615. ------\r\n");
  8616.      }
  8617.  
  8618.     while ((code = krb5_kt_next_entry(kcontext, kt, &entry, &cursor)) == 0) {
  8619.         if ((code = krb5_unparse_name(kcontext, entry.principal, &pname))) {
  8620.             debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8621.                    "",code);
  8622.             krb5_errno = code;
  8623.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8624.             return(-1);
  8625.         }
  8626.         printf("%4d ", entry.vno);
  8627.         if (show_time) {
  8628.             printtime(entry.timestamp);
  8629.             printf(" ");
  8630.         }
  8631.         printf("%s", pname);
  8632.         if (show_etype)
  8633.             printf(" (%s) " ,
  8634. #ifdef HEIMDAL
  8635.                     etype_string(entry.key.keytype)
  8636. #else /* HEIMDAL */
  8637.                     etype_string(entry.key.enctype)
  8638. #endif /* HEIMDAL */
  8639.                     );
  8640.         if (show_keys) {
  8641.             printf(" (0x");
  8642.             {
  8643.                 int i;
  8644.                 for (i = 0; i < entry.key.length; i++)
  8645.                     printf("%02x",
  8646. #ifdef HEIMDAL
  8647.                             entry.key.keyvalue[i]
  8648. #else /* HEIMDAL */
  8649.                             entry.key.contents[i]
  8650. #endif /* HEIMDAL */
  8651.                             );
  8652.             }
  8653.             printf(")");
  8654.         }
  8655.         printf("\r\n");
  8656.         krb5_free_unparsed_name(kcontext,pname);
  8657.     }
  8658.     if (code && code != KRB5_KT_END) {
  8659.         debug(F101,"ck_krb5_list_creds while scanning keytab",
  8660.                "",code);
  8661.         krb5_errno = code;
  8662.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8663.         return(-1);
  8664.     }
  8665.     if ((code = krb5_kt_end_seq_get(kcontext, kt, &cursor))) {
  8666.         debug(F101,"ck_krb5_list_creds while ending keytab scan",
  8667.                "",code);
  8668.         krb5_errno = code;
  8669.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8670.         return(-1);
  8671.     }
  8672.     krb5_errno = 0;
  8673.     makestr(&krb5_errmsg,"OK");
  8674.     return(0);
  8675. }
  8676.  
  8677. static int
  8678. #ifdef CK_ANSIC
  8679. do_ccache(krb5_context kcontext, char * cc_name)
  8680. #else
  8681. do_ccache(kcontext,name) krb5_context kcontext; char * cc_name;
  8682. #endif
  8683. {
  8684.     krb5_ccache cache = NULL;
  8685.     krb5_cc_cursor cur;
  8686.     krb5_creds creds;
  8687.     krb5_principal princ=NULL;
  8688.     krb5_flags flags=0;
  8689.     krb5_error_code code = 0;
  8690.     int exit_status = 0;
  8691.  
  8692.     if (status_only)
  8693.         /* exit_status is set back to 0 if a valid tgt is found */
  8694.         exit_status = 1;
  8695.  
  8696.     code = k5_get_ccache(kcontext,&cache,cc_name);
  8697.     if (code != 0) {
  8698.         debug(F111,"do_ccache while getting ccache",
  8699.                error_message(code),code);
  8700.         krb5_errno = code;
  8701.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8702.         return(-1);
  8703.     }
  8704.  
  8705.     flags = 0;                          /* turns off OPENCLOSE mode */
  8706.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8707.         if (code == ENOENT) {
  8708.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  8709.                    krb5_cc_get_name(kcontext, cache),code);
  8710.         } else {
  8711.             debug(F111,
  8712.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  8713.                   krb5_cc_get_name(kcontext, cache),code);
  8714.         }
  8715.         printf("No ticket File.\r\n");
  8716.         krb5_errno = code;
  8717.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8718.         krb5_cc_close(kcontext,cache);
  8719.         return(-1);
  8720.     }
  8721.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  8722.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  8723.                "",code);
  8724.         krb5_errno = code;
  8725.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8726.         krb5_cc_close(kcontext,cache);
  8727.         return(-1);
  8728.     }
  8729.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  8730.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8731.                "",code);
  8732.         krb5_errno = code;
  8733.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8734.         krb5_cc_close(kcontext,cache);
  8735.         return(-1);
  8736.     }
  8737.     if (!status_only) {
  8738.         printf("Ticket cache:      %s:%s\r\nDefault principal: %s\r\n\r\n",
  8739.                 krb5_cc_get_type(kcontext, cache),
  8740.                 krb5_cc_get_name(kcontext, cache), defname);
  8741.         printf("Valid starting");
  8742.         fillit(timestamp_width - sizeof("Valid starting") + 3,
  8743.                (int) ' ');
  8744.         printf("Expires");
  8745.         fillit(timestamp_width - sizeof("Expires") + 3,
  8746.                (int) ' ');
  8747.         printf("Service principal\r\n");
  8748.     }
  8749.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  8750.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  8751.                "",code);
  8752.         krb5_errno = code;
  8753.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8754.         krb5_cc_close(kcontext,cache);
  8755.         return(-1);
  8756.     }
  8757.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  8758.         if (status_only) {
  8759.             if (exit_status && creds.server->length == 2 &&
  8760.                 strcmp(creds.server->realm.data, princ->realm.data) == 0 &&
  8761.                 strcmp((char *)creds.server->data[0].data, "krbtgt") == 0 &&
  8762.                 strcmp((char *)creds.server->data[1].data,
  8763.                        princ->realm.data) == 0 &&
  8764.                 creds.times.endtime > now)
  8765.                 exit_status = 0;
  8766.         } else {
  8767.             show_credential(kcontext, &creds);
  8768.         }
  8769.         krb5_free_cred_contents(kcontext, &creds);
  8770.     }
  8771.     printf("\r\n");
  8772.     if (code == KRB5_CC_END || code == KRB5_CC_NOTFOUND) {
  8773.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  8774.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  8775.                    "",code);
  8776.             krb5_errno = code;
  8777.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8778.             krb5_cc_close(kcontext,cache);
  8779.             return(-1);
  8780.         }
  8781.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  8782.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8783.             debug(F101,"ck_krb5_list_creds while closing ccache",
  8784.                    "",code);
  8785.             krb5_errno = code;
  8786.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8787.             krb5_cc_close(kcontext,cache);
  8788.             return(-1);
  8789.         }
  8790.         krb5_errno = 0;
  8791.         makestr(&krb5_errmsg,"OK");
  8792.         krb5_cc_close(kcontext,cache);
  8793.         return(0);
  8794.     } else {
  8795.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  8796.         krb5_errno = code;
  8797.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8798.         krb5_cc_close(kcontext,cache);
  8799.         return(-1);
  8800.     }
  8801.     krb5_errno = 0;
  8802.     makestr(&krb5_errmsg,"OK");
  8803.     krb5_cc_close(kcontext,cache);
  8804.     return(0);
  8805. }
  8806.  
  8807. static char *
  8808. #ifdef CK_ANSIC
  8809. #ifdef HEIMDAL
  8810. etype_string(krb5_keytype enctype)
  8811. #else /* HEIMDAL */
  8812. etype_string(krb5_enctype enctype)
  8813. #endif /* HEIMDAL */
  8814. #else
  8815. #ifdef HEIMDAL
  8816. etype_string(enctype) krb5_keytype enctype;
  8817. #else /* HEIMDAL */
  8818. etype_string(enctype) krb5_enctype enctype;
  8819. #endif /* HEIMDAL */
  8820. #endif
  8821. {
  8822.     static char buf[12];
  8823.  
  8824.     switch (enctype) {
  8825.     case ENCTYPE_NULL:
  8826.         return "NULL";
  8827.     case ENCTYPE_DES_CBC_CRC:
  8828.         return "DES-CBC-CRC";
  8829.     case ENCTYPE_DES_CBC_MD4:
  8830.         return "DES-CBC-MD4";
  8831.     case ENCTYPE_DES_CBC_MD5:
  8832.         return "DES-CBC-MD5";
  8833.     case ENCTYPE_DES_CBC_RAW:
  8834.         return "DES-CBC-RAW";
  8835.     case ENCTYPE_DES3_CBC_SHA:
  8836.         return "DES3-CBC-SHA";
  8837.     case ENCTYPE_DES3_CBC_RAW:
  8838.         return "DES3-CBC-RAW";
  8839.     case ENCTYPE_DES_HMAC_SHA1:
  8840.         return "DES-HMAC-SHA1";
  8841.     case ENCTYPE_DES3_CBC_SHA1:
  8842.         return "DES3-CBC-SHA1";
  8843.     case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  8844.         return "AES128_CTS-HMAC-SHA1_96";
  8845.     case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  8846.         return "AES256_CTS-HMAC-SHA1_96";
  8847.     case ENCTYPE_ARCFOUR_HMAC:
  8848.         return "RC4-HMAC-NT";
  8849.     case ENCTYPE_ARCFOUR_HMAC_EXP:
  8850.         return "RC4-HMAC-NT-EXP";
  8851.     case ENCTYPE_UNKNOWN:
  8852.         return "UNKNOWN";
  8853.     case ENCTYPE_LOCAL_DES3_HMAC_SHA1:
  8854.         return "LOCAL-DES3-HMAC-SHA1";
  8855.     case ENCTYPE_LOCAL_RC4_MD4:
  8856.         return "LOCAL-RC4-MD4";
  8857.     default:
  8858.         ckmakmsg(buf, sizeof(buf),"etype ", ckitoa(enctype),NULL,NULL);
  8859.         return buf;
  8860.         break;
  8861.     }
  8862. }
  8863.  
  8864. static char *
  8865. #ifdef CK_ANSIC
  8866. flags_string(register krb5_creds *cred)
  8867. #else
  8868. flags_string(cred) register krb5_creds *cred;
  8869. #endif
  8870. {
  8871.     static char buf[32];
  8872.     int i = 0;
  8873.  
  8874.     if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
  8875.         buf[i++] = 'F';
  8876.     if (cred->ticket_flags & TKT_FLG_FORWARDED)
  8877.         buf[i++] = 'f';
  8878.     if (cred->ticket_flags & TKT_FLG_PROXIABLE)
  8879.         buf[i++] = 'P';
  8880.     if (cred->ticket_flags & TKT_FLG_PROXY)
  8881.         buf[i++] = 'p';
  8882.     if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
  8883.         buf[i++] = 'D';
  8884.     if (cred->ticket_flags & TKT_FLG_POSTDATED)
  8885.         buf[i++] = 'd';
  8886.     if (cred->ticket_flags & TKT_FLG_INVALID)
  8887.         buf[i++] = 'i';
  8888.     if (cred->ticket_flags & TKT_FLG_RENEWABLE)
  8889.         buf[i++] = 'R';
  8890.     if (cred->ticket_flags & TKT_FLG_INITIAL)
  8891.         buf[i++] = 'I';
  8892.     if (cred->ticket_flags & TKT_FLG_HW_AUTH)
  8893.         buf[i++] = 'H';
  8894.     if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
  8895.         buf[i++] = 'A';
  8896.     buf[i] = '\0';
  8897.     return(buf);
  8898. }
  8899.  
  8900. static char   *
  8901. #ifdef CK_ANSIC
  8902. short_date(long   *dp)
  8903. #else
  8904. short_date(dp) long   *dp;
  8905. #endif
  8906. {
  8907.     register char *cp;
  8908. #ifndef ctime
  8909.     extern char *ctime();
  8910. #endif /* ctime */
  8911.     cp = ctime(dp) + 4;
  8912.     cp[15] = '\0';
  8913.     return (cp);
  8914. }
  8915.  
  8916.  
  8917. static VOID
  8918. #ifdef CK_ANSIC
  8919. printtime(time_t tv)
  8920. #else
  8921. printtime(tv) time_t tv;
  8922. #endif
  8923. {
  8924.     char timestring[BUFSIZ];
  8925.     char format[12];
  8926.     char fill;
  8927.  
  8928.     fill = ' ';
  8929.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  8930.     if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv,
  8931.                                      timestring,
  8932.                                      timestamp_width+1,
  8933.                                      &fill)) {
  8934.         printf(format,timestring);
  8935.     }
  8936.     else {
  8937.         printf(format,short_date(&tv));
  8938.     }
  8939.  
  8940. }
  8941.  
  8942. static void
  8943. #ifdef CK_ANSIC
  8944. one_addr(krb5_address *a)
  8945. #else
  8946. one_addr(a) krb5_address *a;
  8947. #endif
  8948. {
  8949.     struct hostent *h;
  8950.     extern tcp_rdns;
  8951.  
  8952.     if ((a->addrtype == ADDRTYPE_INET) &&
  8953.         (a->length == 4)) {
  8954.         if (tcp_rdns != SET_OFF) {
  8955.             h = gethostbyaddr(a->contents, 4, AF_INET);
  8956.             if (h) {
  8957. #ifdef HADDRLIST
  8958.                 h = ck_copyhostent(h);
  8959. #endif /* HADDRLIST */
  8960.                 printf("%s (%d.%d.%d.%d)", h->h_name,
  8961.                         a->contents[0], a->contents[1],
  8962.                         a->contents[2], a->contents[3]);
  8963.             }
  8964.         }
  8965.         if (tcp_rdns == SET_OFF || !h) {
  8966.             printf("%d.%d.%d.%d", a->contents[0], a->contents[1],
  8967.                    a->contents[2], a->contents[3]);
  8968.         }
  8969.     } else {
  8970.         printf("unknown addr type %d", a->addrtype);
  8971.     }
  8972. }
  8973.  
  8974. static VOID
  8975. #ifdef CK_ANSIC
  8976. show_credential(krb5_context kcontext, register krb5_creds * cred)
  8977. #else
  8978. show_credential(kcontext, cred)
  8979.     krb5_context          kcontext;
  8980.     register krb5_creds * cred;
  8981. #endif
  8982. {
  8983.     krb5_error_code retval=0;
  8984.     krb5_ticket *tkt=NULL;
  8985.     char *name=NULL, *sname=NULL, *flags=NULL;
  8986.     int extra_field = 0;
  8987.  
  8988.     retval = krb5_unparse_name(kcontext, cred->client, &name);
  8989.     if (retval) {
  8990.         debug(F101,"ck_krb5_list_creds while unparsing client name","",retval);
  8991.         krb5_errno = retval;
  8992.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8993.         return;
  8994.     }
  8995.     retval = krb5_unparse_name(kcontext, cred->server, &sname);
  8996.     if (retval) {
  8997.         debug(F101,"ck_krb5_list_creds while unparsing server name","",retval);
  8998.         free(name);
  8999.         krb5_errno = retval;
  9000.         makestr(&krb5_errmsg,error_message(krb5_errno));
  9001.         return;
  9002.     }
  9003.     if (!cred->times.starttime)
  9004.         cred->times.starttime = cred->times.authtime;
  9005.  
  9006.     printtime(cred->times.starttime);
  9007.     printf("  ");
  9008.  
  9009.     if ( time(0) < cred->times.endtime )
  9010.         printtime(cred->times.endtime);
  9011.     else
  9012.         printf("** expired ** ");
  9013.  
  9014.     printf("  %s\r\n", sname);
  9015.  
  9016.     if (strcmp(name, defname)) {
  9017.         printf("   for client %s", name);
  9018.         extra_field++;
  9019.     }
  9020.  
  9021.     if (cred->times.renew_till) {
  9022.         if (!extra_field)
  9023.             printf("   ");
  9024.         else
  9025.             printf(", ");
  9026.         printf("renew until ");
  9027.         printtime(cred->times.renew_till);
  9028.         extra_field += 2;
  9029.     }
  9030.  
  9031.     if (extra_field > 3) {
  9032.         printf("\r\n");
  9033.         extra_field = 0;
  9034.     }
  9035.  
  9036.     if (show_flags) {
  9037.         flags = flags_string(cred);
  9038.         if (flags && *flags) {
  9039.             if (!extra_field)
  9040.                 printf("   ");
  9041.             else
  9042.                 printf(", ");
  9043.             printf("Flags: %s", flags);
  9044.             extra_field++;
  9045.         }
  9046.     }
  9047.  
  9048.     if (extra_field > 2) {
  9049.         printf("\r\n");
  9050.         extra_field = 0;
  9051.     }
  9052.  
  9053.     if (show_etype) {
  9054.         retval = decode_krb5_ticket(&cred->ticket, &tkt);
  9055.         if (!extra_field)
  9056.             printf("   ");
  9057.         else
  9058.             printf(", ");
  9059. #ifdef HEIMDAL
  9060.         printf("Etype (skey, tkt): %s, %s ",
  9061.                etype_string(cred->session.keytype),
  9062.                etype_string(tkt->enc_part.keytype));
  9063. #else /* HEIMDAL */
  9064.         printf("Etype (skey, tkt): %s, %s ",
  9065.                etype_string(cred->keyblock.enctype),
  9066.                etype_string(tkt->enc_part.enctype));
  9067. #endif /* HEIMDAL */
  9068.         krb5_free_ticket(kcontext, tkt);
  9069.         extra_field++;
  9070.     }
  9071.  
  9072.     /* if any additional info was printed, extra_field is non-zero */
  9073.     if (extra_field)
  9074.         printf("\r\n");
  9075.  
  9076.     if ( show_addr ) {
  9077.         if (!cred->addresses || !cred->addresses[0]) {
  9078.             printf("\tAddresses: (none)\r\n");
  9079.         } else {
  9080.             int i;
  9081.             for (i=0; cred->addresses[i]; i++) {
  9082.                 if (i)
  9083.                     printf("              ");
  9084.                 else
  9085.                     printf("   Addresses: ");
  9086.                 one_addr(cred->addresses[i]);
  9087.                 printf("\r\n");
  9088.             }
  9089.         }
  9090.     }
  9091.  
  9092.     krb5_free_unparsed_name(kcontext,name);
  9093.     krb5_free_unparsed_name(kcontext,sname);
  9094.  
  9095.     krb5_errno = 0;
  9096.     makestr(&krb5_errmsg,"OK");
  9097. }
  9098.  
  9099. static VOID
  9100. #ifdef CK_ANSIC
  9101. fillit(int num, int c)
  9102. #else
  9103. fillit(num, c) int num; int c;
  9104. #endif
  9105. {
  9106.     int i;
  9107.  
  9108.     for (i=0; i<num; i++)
  9109.         printf("%c",c);
  9110. }
  9111. #endif /* KLIST */
  9112. #endif /* KRB5 */
  9113.  
  9114. #ifdef KRB4
  9115. #define KDEBUG 1
  9116. int k4debug = 0;                /* Kerberos 4 runtime debugging */
  9117.  
  9118. #ifdef KINIT
  9119. #define KRB_DEFAULT_LIFE 120 /* 10 hours in 5 minute intervals */
  9120.  
  9121. #ifdef SNK4
  9122. /* SNK4 is a hardware authentication system used to pre-authenticate    */
  9123. /* a ticket getting ticket.  We do not support this code at the present */
  9124. /* time in Kermit.                                                      */
  9125. void
  9126. get_input(s, size, stream)
  9127. char *s;
  9128. int size;
  9129. FILE *stream;
  9130. {
  9131.     char *p;
  9132.  
  9133.     if (fgets(s, size, stream) == NULL)
  9134.         exit(1);
  9135.     if ( (p = strchr(s, '\n')) != NULL)
  9136.         *p = '\0';
  9137. }
  9138. #endif /* SNK4 */
  9139.  
  9140. #ifdef COMMENT
  9141. static char
  9142. #ifdef CK_ANSIC
  9143. hex_scan_nybble(char c)
  9144. #else
  9145. hex_scan_nybble(c) char c;
  9146. #endif
  9147. {
  9148.     if (c >= '0' && c <= '9')
  9149.         return c - '0';
  9150.     if (c >= 'A' && c <= 'F')
  9151.         return c - 'A' + 10;
  9152.     if (c >= 'a' && c <= 'f')
  9153.         return c - 'a' + 10;
  9154.     return -1;
  9155. }
  9156.  
  9157. /* returns: NULL for ok, pointer to error string for bad input */
  9158. static char*
  9159. #ifdef CK_ANSIC
  9160. hex_scan_four_bytes(char *out, char *in)
  9161. #else
  9162. hex_scan_four_bytes(out, in) char *out; char *in;
  9163. #endif
  9164. {
  9165.     int i;
  9166.     int c;
  9167.     char c1;
  9168.     for (i=0; i<8; i++) {
  9169.         if(!in[i])
  9170.             return "not enough input";
  9171.         c = hex_scan_nybble(in[i]);
  9172.         if(c<0)
  9173.             return "invalid digit";
  9174.         c1 = c;
  9175.         i++;
  9176.         if(!in[i])
  9177.             return "not enough input";
  9178.         c = hex_scan_nybble(in[i]);
  9179.         if(c<0)
  9180.             return "invalid digit";
  9181.         *out++ = (c1 << 4) + c;
  9182.     }
  9183.     switch(in[i]) {
  9184.     case 0:
  9185.     case '\r':
  9186.     case '\n':
  9187.         return NULL;
  9188.     default:
  9189.         return "extra characters at end of input";
  9190.     }
  9191. }
  9192. #endif /* COMMENT */
  9193.  
  9194. /* ck_krb4_initTGT() returns 0 on success */
  9195. int
  9196. #ifdef CK_ANSIC
  9197. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9198. #else
  9199. ck_krb4_initTGT(op,init)
  9200.     struct krb_op_data * op, struct krb4_init_data * init
  9201. #endif
  9202. {
  9203.     char    aname[ANAME_SZ+1];
  9204.     char    inst[INST_SZ+1];
  9205.     char    realm[REALM_SZ+1];
  9206.     char    *password=NULL;
  9207.     char    passwd[80]="";
  9208.     char    *username = NULL;
  9209.     char    *usernameptr=NULL;
  9210.     int     iflag,      /* Instance */
  9211.             rflag,      /* Realm */
  9212.             vflag,      /* Verbose */
  9213.             lflag,      /* Lifetime */
  9214.             pflag,      /* Preauth */
  9215.             lifetime=KRB_DEFAULT_LIFE,   /* Life Time */
  9216.             k_errno;
  9217.     register char *cp;
  9218.     register i;
  9219.  
  9220.     if ( !ck_krb4_is_installed() )
  9221.         return(-1);
  9222.  
  9223.     *inst = *realm = '\0';
  9224.     iflag = rflag = vflag = lflag = pflag = 0;
  9225.  
  9226.     vflag = init->verbose;
  9227.     pflag = init->preauth;
  9228.  
  9229.     if ( init->lifetime ) {
  9230.         lifetime = init->lifetime<5?1:init->lifetime/5;
  9231.         if ( lifetime > 255 ) lifetime = 255;
  9232.     }
  9233.     else
  9234.         lifetime = KRB_DEFAULT_LIFE;
  9235.  
  9236.     username = init->principal;
  9237.  
  9238.     if (username && username[0] &&
  9239.         (k_errno = kname_parse(aname, inst, realm, username))
  9240.         != AUTH_SUCCESS) {
  9241.         krb4_errno = k_errno;
  9242.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9243.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9244.         iflag = rflag = 1;
  9245.         username = NULL;
  9246.     }
  9247.  
  9248.     if ( init->realm ) {
  9249.         ckstrncpy(realm,init->realm,REALM_SZ);
  9250.     }
  9251.  
  9252.     if ( init->instance ) {
  9253.         ckstrncpy(inst,init->instance, INST_SZ);
  9254.     }
  9255.  
  9256. #ifdef COMMENT
  9257.     if ( vflag )
  9258.         printf("Kerberos IV initialization\r\n");
  9259. #endif /* COMMENT */
  9260.  
  9261.     if (!username || !username[0]) {
  9262.         debug(F100,"ck_krb4_initTGT no username specified","",0);
  9263.         printf("?Invalid principal specified.\r\n");
  9264.         krb4_errno = -1;
  9265.         makestr(&krb4_errmsg,"No principal specified");
  9266.         return(-1);
  9267.     }
  9268.     if (!*realm) {
  9269.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ);
  9270.     }
  9271.  
  9272.     if ( init->password )
  9273.         password = init->password;
  9274.     else {
  9275.         char prmpt[80];
  9276.         int ok;
  9277.  
  9278.         ckmakxmsg(prmpt,sizeof(prmpt),
  9279.                   "Kerberos 4 Password for ",username,"@",realm,": ",
  9280.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9281.         ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL,DEFAULT_UQ_TIMEOUT);
  9282.         if ( ok )
  9283.             password = passwd;
  9284.     }
  9285.  
  9286.     if (pflag) {
  9287.         k_errno = krb_get_pw_in_tkt_preauth( aname, inst, realm,
  9288.                                              "krbtgt", realm,
  9289.                                              lifetime,
  9290.                                              password);
  9291.         if (k_errno == -1) {    /* preauth method not available */
  9292.             k_errno = krb_get_pw_in_tkt(aname,
  9293.                                          inst, realm,
  9294.                                          "krbtgt", realm,
  9295.                                          lifetime,
  9296.                                          password);
  9297.         }
  9298.     } else {
  9299.         k_errno = krb_get_pw_in_tkt(aname,
  9300.                                      inst, realm,
  9301.                                      "krbtgt", realm,
  9302.                                      lifetime,
  9303.                                      password);
  9304.     }
  9305.  
  9306.     memset(passwd,0,sizeof(passwd));
  9307.     if (k_errno) {
  9308.         printf("%s for principal %s%s%s@%s\r\n",
  9309.                 krb_get_err_text_entry(k_errno), aname,
  9310.                 inst[0]?".":"", inst, realm);
  9311.         krb4_errno = k_errno;
  9312.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9313.         return(-1);
  9314.     } else if (vflag) {
  9315.         printf("Result from realm %s: ", realm);
  9316.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9317.     }
  9318.     krb4_errno = k_errno;
  9319.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9320.     return(0);
  9321. }
  9322. #endif /* KINIT */
  9323. #ifdef KDESTROY
  9324. int
  9325. #ifdef CK_ANSIC
  9326. ck_krb4_destroy(struct krb_op_data * op)
  9327. #else
  9328. ck_krb4_destroy(op) struct krb_op_data * op;
  9329. #endif
  9330. {
  9331.     int k_errno=0;
  9332.  
  9333.     if ( !ck_krb4_is_installed() )
  9334.         return(-1);
  9335.  
  9336.     k_errno = dest_tkt();
  9337.  
  9338.     krb4_errno = k_errno;
  9339.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9340.  
  9341.     if (k_errno == 0)
  9342.         printf("Tickets destroyed.\r\n");
  9343.     else if (k_errno == RET_TKFIL)
  9344.         printf("No tickets to destroy.\r\n");
  9345.     else {
  9346.         printf("Tickets MAY NOT be destroyed.\r\n");
  9347.         return(-1);
  9348.     }
  9349.     return(0);
  9350. }
  9351. #endif /* KDESTROY */
  9352. #ifdef KLIST
  9353. _PROTOTYP(static int display_tktfile,(char *, int, int, int));
  9354.  
  9355. int
  9356. #ifdef CK_ANSIC
  9357. ck_krb4_list_creds(struct krb_op_data * op)
  9358. #else
  9359. ck_krb4_list_creds(op) struct krb_op_data * op;
  9360. #endif
  9361. {
  9362.     int     long_form = 1;
  9363.     int     tgt_test = 0;
  9364.     int     do_srvtab = 0;
  9365.     int     show_kvnos = 0;
  9366.     char   *tkt_file = NULL;
  9367.  
  9368.     if ( !ck_krb4_is_installed() )
  9369.         return(-1);
  9370.  
  9371.     if ( op->cache )
  9372.         tkt_file = op->cache;
  9373.  
  9374.     if ( k4debug ) {
  9375.         show_kvnos = 1;
  9376.     }
  9377.  
  9378.     if (do_srvtab)
  9379.         return(display_srvtab(tkt_file));
  9380.     else
  9381.         return(display_tktfile(tkt_file, tgt_test, long_form, show_kvnos));
  9382. }
  9383.  
  9384. #ifndef KRB5
  9385. static int timestamp_width=0;
  9386.  
  9387. static char   *
  9388. #ifdef CK_ANSIC
  9389. short_date(long   *dp)
  9390. #else
  9391. short_date(dp) long   *dp;
  9392. #endif
  9393. {
  9394.     register char *cp;
  9395.     extern char *ctime();
  9396.     cp = ctime(dp) + 4;
  9397.     cp[15] = '\0';
  9398.     return (cp);
  9399. }
  9400.  
  9401.  
  9402. static VOID
  9403. #ifdef CK_ANSIC
  9404. printtime(time_t tv)
  9405. #else
  9406. printtime(tv) time_t tv;
  9407. #endif
  9408. {
  9409.     char timestring[BUFSIZ];
  9410.     char format[12];
  9411.     char fill;
  9412.  
  9413.     fill = ' ';
  9414.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  9415.     printf(format,short_date(&tv));
  9416. }
  9417. #endif /* KRB5 */
  9418.  
  9419. static int
  9420. #ifdef CK_ANSIC
  9421. display_tktfile(char *file, int tgt_test, int long_form, int show_kvnos)
  9422. #else
  9423. display_tktfile(file,tgt_test,long_form,show_kvnos)
  9424.     char *file; int tgt_test; int long_form; int show_kvnos;
  9425. #endif
  9426. {
  9427.     char    pname[ANAME_SZ];
  9428.     char    pinst[INST_SZ];
  9429.     char    prealm[REALM_SZ];
  9430.     char    buf1[20], buf2[20];
  9431.     int     k_errno;
  9432. #ifdef OS2
  9433.     LEASH_CREDENTIALS creds;
  9434. #else /* OS2 */
  9435.     CREDENTIALS creds;
  9436. #endif /* OS2 */
  9437.     int     header = 1;
  9438.  
  9439.     file = tkt_string();
  9440.  
  9441.     if (long_form) {
  9442.         printf("Ticket cache:      %s\r\n", file);
  9443.     }
  9444.  
  9445.     /*
  9446.      * Since krb_get_tf_realm will return a ticket_file error,
  9447.      * we will call tf_init and tf_close first to filter out
  9448.      * things like no ticket file.  Otherwise, the error that
  9449.      * the user would see would be
  9450.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9451.      * instead of
  9452.      * klist: No ticket file (tf_util)
  9453.      */
  9454.  
  9455.     /* Open ticket file */
  9456.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9457.         if (!tgt_test)
  9458.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9459.         krb4_errno = k_errno;
  9460.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9461.         return(-1);
  9462.     }
  9463.  
  9464.  
  9465.     /* Close ticket file */
  9466.     (void) tf_close();
  9467.  
  9468.     /*
  9469.      * We must find the realm of the ticket file here before calling
  9470.      * tf_init because since the realm of the ticket file is not
  9471.      * really stored in the principal section of the file, the
  9472.      * routine we use must itself call tf_init and tf_close.
  9473.      */
  9474.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9475.         if (!tgt_test)
  9476.             printf("can't find realm of ticket file: %s\r\n",
  9477.                     krb_get_err_text_entry (k_errno));
  9478.         krb4_errno = k_errno;
  9479.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9480.         return(-1);
  9481.     }
  9482.  
  9483.     /* Open ticket file */
  9484.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9485.         if (!tgt_test)
  9486.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9487.         krb4_errno = k_errno;
  9488.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9489.         return(-1);
  9490.     }
  9491.     /* Get principal name and instance */
  9492.     if ((k_errno = tf_get_pname(pname)) ||
  9493.          (k_errno = tf_get_pinst(pinst))) {
  9494.         (void) tf_close();
  9495.         if (!tgt_test)
  9496.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9497.         krb4_errno = k_errno;
  9498.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9499.         return(-1);
  9500.     }
  9501.  
  9502.     /*
  9503.      * You may think that this is the obvious place to get the
  9504.      * realm of the ticket file, but it can't be done here as the
  9505.      * routine to do this must open the ticket file.  This is why
  9506.      * it was done before tf_init.
  9507.      */
  9508.  
  9509.     if (!tgt_test && long_form)
  9510.         printf("Default principal: %s%s%s%s%s\r\n\r\n", pname,
  9511.                (pinst[0] ? "." : ""), pinst,
  9512.                (prealm[0] ? "@" : ""), prealm);
  9513.  
  9514.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9515.         if (!tgt_test && long_form && header) {
  9516.             printf("%-17s  %-17s  %s\r\n",
  9517.                    "Valid starting", "Expires", "Service principal");
  9518.             header = 0;
  9519.         }
  9520.         if (tgt_test) {
  9521.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9522.             if (!strcmp(creds.service, "krbtgt") &&
  9523.                 !strcmp(creds.instance, prealm)) {
  9524.                 krb4_errno = k_errno;
  9525.                 makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9526.  
  9527.                 (void) tf_close();
  9528.                 if (time(0) < creds.issue_date) {
  9529.                     return(0);          /* tgt hasn't expired */
  9530.                 } else {
  9531.                     return(-1);         /* has expired */
  9532.                 }
  9533.             }
  9534.             continue;                   /* not a tgt */
  9535.         }
  9536.         if (long_form) {
  9537.             timestamp_width = 17;       /* for k5 display function */
  9538.                                         /* if available            */
  9539.             printtime(creds.issue_date);
  9540.             printf("  ");
  9541.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9542.             if ( time(0) < creds.issue_date )
  9543.                 printtime(creds.issue_date);
  9544.             else
  9545.                 printf("*** expired ***  ");
  9546.             printf("  ");
  9547.         }
  9548.         if (show_kvnos)
  9549.           printf("%s%s%s%s%s (%d)\r\n",
  9550.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9551.                  (creds.realm[0] ? "@" : ""), creds.realm, creds.kvno);
  9552.         else
  9553.           printf("%s%s%s%s%s\r\n",
  9554.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9555.                  (creds.realm[0] ? "@" : ""), creds.realm);
  9556.  
  9557. #ifdef OS2
  9558.         if ( creds.address[0] )
  9559.             printf("   Address: %s\r\n",creds.address);
  9560. #endif /* OS2 */
  9561.     }
  9562.  
  9563.     (void) tf_close();
  9564.  
  9565.     if (tgt_test) {
  9566.         return(-1);
  9567.     }/* no tgt found */
  9568.     if (header && long_form && k_errno == EOF) {
  9569.         printf("No tickets in file.\r\n");
  9570.     }
  9571.     krb4_errno = k_errno;
  9572.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9573.     return(0);
  9574. }
  9575.  
  9576. #ifdef COMMENT
  9577. /* Just so we remember what the command line interface looked like */
  9578. usage()
  9579. {
  9580.     printf(
  9581.         "Usage: [ -s | -t ] [ -file filename ] [ -srvtab ] [ -version ]\r\n");
  9582.     return(-1);
  9583. }
  9584. #endif /* COMMENT */
  9585.  
  9586. /* adapted from getst() in librkb */
  9587. /*
  9588.  * ok_getst() takes a file descriptor, a string and a count.  It reads
  9589.  * from the file until either it has read "count" characters, or until
  9590.  * it reads a null byte.  When finished, what has been read exists in
  9591.  * the given string "s".  If "count" characters were actually read, the
  9592.  * last is changed to a null, so the returned string is always null-
  9593.  * terminated.  ok_getst() returns the number of characters read, including
  9594.  * the null terminator.
  9595.  *
  9596.  * If there is a read error, it returns -1 (like the read(2) system call)
  9597.  */
  9598.  
  9599. static int
  9600. #ifdef CK_ANSIC
  9601. ok_getst(int fd, register char *s, int n)
  9602. #else
  9603. ok_getst(fd, s, n) int fd; register char *s; int n;
  9604. #endif
  9605. {
  9606.     register int count = n;
  9607.     int err;
  9608.     while ((err = read(fd, s, 1)) > 0 && --count)
  9609.         if (*s++ == '\0')
  9610.             return (n - count);
  9611.     if (err < 0)
  9612.         return(-1);
  9613.     *s = '\0';
  9614.     return (n - count);
  9615. }
  9616.  
  9617. int
  9618. #ifdef CK_ANSIC
  9619. display_srvtab(char *file)
  9620. #else
  9621. display_srvtab(file) char *file;
  9622. #endif
  9623. {
  9624.     int stab;
  9625.     char serv[SNAME_SZ];
  9626.     char inst[INST_SZ];
  9627.     char rlm[REALM_SZ];
  9628.     unsigned char key[8];
  9629.     unsigned char vno;
  9630.     int count;
  9631.  
  9632.     printf("Server key file:   %s\r\n", file);
  9633. #ifdef NT
  9634. #ifndef O_RDONLY
  9635. #define O_RDONLY _O_RDONLY
  9636. #endif /* O_RDONLY */
  9637. #endif /* NT */
  9638.  
  9639.     if ((stab = open(file, O_RDONLY, 0400)) < 0) {
  9640.         perror(file);
  9641.         return(-1);
  9642.     }
  9643.     printf("%-15s %-15s %-10s %s\r\n","Service","Instance","Realm",
  9644.            "Key Version");
  9645.     printf("------------------------------------------------------\r\n");
  9646.  
  9647.     /* argh. getst doesn't return error codes, it silently fails */
  9648.     while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
  9649.            && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
  9650.            && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
  9651.         if (((count = read(stab,(char *) &vno,1)) != 1) ||
  9652.              ((count = read(stab,(char *) key,8)) != 8)) {
  9653.             if (count < 0)
  9654.                 perror("reading from key file");
  9655.             else
  9656.                 printf("key file truncated\r\n");
  9657.             return(-1);
  9658.         }
  9659.         printf("%-15s %-15s %-15s %d\r\n",serv,inst,rlm,vno);
  9660.     }
  9661.     if (count < 0)
  9662.         perror(file);
  9663.     (void) close(stab);
  9664.     return(0);
  9665. }
  9666. #endif /* KLIST */
  9667. #else /* KRB4 */
  9668. int
  9669. ck_krb4_autoget_TGT(char * dummy)
  9670. {
  9671.     return(-1);
  9672. }
  9673. #ifdef CK_KERBEROS
  9674. int
  9675. #ifdef CK_ANSIC
  9676. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9677. #else
  9678. ck_krb4_initTGT(op,init)
  9679.     struct krb_op_data * op, struct krb4_init_data * init
  9680. #endif
  9681. {
  9682.     return(-1);
  9683. }
  9684.  
  9685. #ifdef CK_ANSIC
  9686. ck_krb4_destroy(struct krb_op_data * op)
  9687. #else
  9688. ck_krb4_destroy(op) struct krb_op_data * op;
  9689. #endif
  9690. {
  9691.     return(-1);
  9692. }
  9693. int
  9694. #ifdef CK_ANSIC
  9695. ck_krb4_list_creds(struct krb_op_data * op)
  9696. #else
  9697. ck_krb4_list_creds(op) struct krb_op_data * op;
  9698. #endif
  9699. {
  9700.     return(-1);
  9701. }
  9702. #else /* CK_KERBEROS */
  9703. int ck_krb4_initTGT(void * a, void *b)
  9704. {
  9705.     return(-1);
  9706. }
  9707. int ck_krb4_destroy(void *a)
  9708. {
  9709.     return(-1);
  9710. }
  9711. int ck_krb4_list_creds(void *a)
  9712. {
  9713.     return(-1);
  9714. }
  9715. #endif /* CK_KERBEROS */
  9716. #endif /* KRB4 */
  9717.  
  9718. /* The following functions are used to implement the Kermit Script Language */
  9719. /* functions                                                                */
  9720.  
  9721. struct tkt_list_item {
  9722.     char * name;
  9723.     struct tkt_list_item * next;
  9724. };
  9725.  
  9726. static struct tkt_list_item * k4_tkt_list = NULL;
  9727.  
  9728. int
  9729. #ifdef CK_ANSIC
  9730. ck_krb4_get_tkts(VOID)
  9731. #else
  9732. ck_krb4_get_tkts()
  9733. #endif
  9734. {
  9735. #ifdef KRB4
  9736.     char   *file=NULL;
  9737.     char    pname[ANAME_SZ];
  9738.     char    pinst[INST_SZ];
  9739.     char    prealm[REALM_SZ];
  9740.     char    buf1[20], buf2[20];
  9741.     int     k_errno;
  9742. #ifdef OS2
  9743.     LEASH_CREDENTIALS creds;
  9744. #else /* OS2 */
  9745.     CREDENTIALS creds;
  9746. #endif /* OS2 */
  9747.     int     tkt_count=0;
  9748.     struct  tkt_list_item ** list = &k4_tkt_list;
  9749.  
  9750.     while ( k4_tkt_list ) {
  9751.         struct tkt_list_item * next;
  9752.         next = k4_tkt_list->next;
  9753.         free(k4_tkt_list->name);
  9754.         free(k4_tkt_list);
  9755.         k4_tkt_list = next;
  9756.     }
  9757.  
  9758.     if ( !ck_krb4_is_installed() )
  9759.         return(-1);
  9760.  
  9761.     file = tkt_string();
  9762.  
  9763.     /*
  9764.      * Since krb_get_tf_realm will return a ticket_file error,
  9765.      * we will call tf_init and tf_close first to filter out
  9766.      * things like no ticket file.  Otherwise, the error that
  9767.      * the user would see would be
  9768.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9769.      * instead of
  9770.      * klist: No ticket file (tf_util)
  9771.      */
  9772.  
  9773.     /* Open ticket file */
  9774.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9775.         return(-1);
  9776.     }
  9777.  
  9778.     /* Close ticket file */
  9779.     (void) tf_close();
  9780.  
  9781.     /*
  9782.      * We must find the realm of the ticket file here before calling
  9783.      * tf_init because since the realm of the ticket file is not
  9784.      * really stored in the principal section of the file, the
  9785.      * routine we use must itself call tf_init and tf_close.
  9786.      */
  9787.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9788.         return(-1);
  9789.     }
  9790.  
  9791.     /* Open ticket file */
  9792.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9793.         return(-1);
  9794.     }
  9795.     /* Get principal name and instance */
  9796.     if ((k_errno = tf_get_pname(pname)) ||
  9797.          (k_errno = tf_get_pinst(pinst))) {
  9798.         return(-1);
  9799.     }
  9800.  
  9801.     /*
  9802.      * You may think that this is the obvious place to get the
  9803.      * realm of the ticket file, but it can't be done here as the
  9804.      * routine to do this must open the ticket file.  This is why
  9805.      * it was done before tf_init.
  9806.      */
  9807.  
  9808.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9809.         char tkt_buf[256];
  9810.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9811.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9812.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9813.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9814.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  9815.         (*list)->name = strdup(tkt_buf);
  9816.         (*list)->next = NULL;
  9817.         list = &((*list)->next);
  9818.         tkt_count++;
  9819.     }
  9820.  
  9821.     tf_close();
  9822.     return(tkt_count);
  9823. #else /* KRB4 */
  9824.     return(0);
  9825. #endif /* KRB4 */
  9826. }
  9827.  
  9828. char *
  9829. #ifdef CK_ANSIC
  9830. ck_krb4_get_next_tkt(VOID)
  9831. #else
  9832. ck_krb4_get_next_tkt()
  9833. #endif
  9834. {
  9835. #ifdef KRB4
  9836.     static char * s=NULL;
  9837.     struct tkt_list_item * next=NULL;
  9838.  
  9839.     if ( s ) {
  9840.         free(s);
  9841.         s = NULL;
  9842.     }
  9843.  
  9844.     if ( k4_tkt_list == NULL )
  9845.         return(NULL);
  9846.  
  9847.     next = k4_tkt_list->next;
  9848.     s = k4_tkt_list->name;
  9849.     free(k4_tkt_list);
  9850.     k4_tkt_list = next;
  9851.     return(s);
  9852. #else /* KRB4 */
  9853.     return(NULL);
  9854. #endif /* KRB4 */
  9855. }
  9856.  
  9857. int
  9858. #ifdef CK_ANSIC
  9859. ck_krb4_tkt_isvalid(char * tktname)
  9860. #else
  9861. ck_krb4_tkt_isvalid(tktname) char * tktname;
  9862. #endif
  9863. {
  9864. #ifdef KRB4
  9865.     char   *file=NULL;
  9866.     char    pname[ANAME_SZ];
  9867.     char    pinst[INST_SZ];
  9868.     char    prealm[REALM_SZ];
  9869.     char    buf1[20], buf2[20];
  9870.     int     k_errno;
  9871.     time_t  issue_t, expire_t, now_t;
  9872. #ifdef OS2
  9873.     LEASH_CREDENTIALS creds;
  9874. #else /* OS2 */
  9875.     CREDENTIALS creds;
  9876. #endif /* OS2 */
  9877.  
  9878.     if ( !ck_krb4_is_installed() )
  9879.         return(-1);
  9880.  
  9881.     debug(F110,"ck_krb4_tkt_isvalid","tkt_string",0);
  9882.     file = tkt_string();
  9883.  
  9884.     /*
  9885.      * Since krb_get_tf_realm will return a ticket_file error,
  9886.      * we will call tf_init and tf_close first to filter out
  9887.      * things like no ticket file.  Otherwise, the error that
  9888.      * the user would see would be
  9889.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9890.      * instead of
  9891.      * klist: No ticket file (tf_util)
  9892.      */
  9893.  
  9894.     /* Open ticket file */
  9895.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9896.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9897.         return(-1);
  9898.     }
  9899.  
  9900.     /* Close ticket file */
  9901.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9902.     (void) tf_close();
  9903.  
  9904.     /*
  9905.      * We must find the realm of the ticket file here before calling
  9906.      * tf_init because since the realm of the ticket file is not
  9907.      * really stored in the principal section of the file, the
  9908.      * routine we use must itself call tf_init and tf_close.
  9909.      */
  9910.     debug(F110,"ck_krb4_tkt_isvalid","krb_get_tf_realm",0);
  9911.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9912.         return(-1);
  9913.     }
  9914.  
  9915.     /* Open ticket file */
  9916.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9917.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9918.         return(-1);
  9919.     }
  9920.     /* Get principal name and instance */
  9921.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_name/tf_get_pinst",0);
  9922.     if ((k_errno = tf_get_pname(pname)) ||
  9923.          (k_errno = tf_get_pinst(pinst))) {
  9924.  
  9925.         /* Close ticket file */
  9926.         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9927.         (void) tf_close();
  9928.  
  9929.         return(-1);
  9930.     }
  9931.  
  9932.     /*
  9933.      * You may think that this is the obvious place to get the
  9934.      * realm of the ticket file, but it can't be done here as the
  9935.      * routine to do this must open the ticket file.  This is why
  9936.      * it was done before tf_init.
  9937.      */
  9938.  
  9939.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_cred",0);
  9940.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  9941.         char tkt_buf[256];
  9942.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9943.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9944.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9945.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9946.         if ( !strcmp(tktname,tkt_buf) ) {
  9947.  
  9948.             /* we found the ticket we are looking for */
  9949.             issue_t = creds.issue_date;
  9950.             expire_t = creds.issue_date
  9951.                 + ((unsigned char) creds.lifetime) * 5 * 60;
  9952.             now_t = time(0);
  9953.  
  9954.             /* We add a 5 minutes fudge factor to compensate for potential */
  9955.             /* clock skew errors between the KDC and K95's host OS         */
  9956.  
  9957.             if ( now_t >= (issue_t-300) && now_t < expire_t) {
  9958. #ifdef OS2
  9959. #ifdef CHECKADDRS
  9960.                 if ( krb4_checkaddrs ) {
  9961.                     extern char myipaddr[20];       /* From ckcnet.c */
  9962.                     if ( !myipaddr[0] ) {
  9963.                         int i;
  9964.                         char buf[60];
  9965.                         for ( i=0;i<64;i++ ) {
  9966.                             if ( getlocalipaddrs(buf,60,i) < 0 )
  9967.                                 break;
  9968.  
  9969.                             if ( !strcmp(buf,creds.address) ) {
  9970.                                 /* Close ticket file */
  9971.                                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9972.                                 (void) tf_close();
  9973.                                 return(1); /* They're the same */
  9974.                             }
  9975.                         }
  9976.  
  9977.                         /* Close ticket file */
  9978.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9979.                         (void) tf_close();
  9980.                         return(0);                  /* They're different */
  9981.                     } else if ( strcmp(myipaddr,creds.address) ) {
  9982.                         /* Close ticket file */
  9983.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9984.                         (void) tf_close();
  9985.                         return(0);                  /* They're different */
  9986.                     }
  9987.                     else {
  9988.                         /* Close ticket file */
  9989.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9990.                         (void) tf_close();
  9991.                         return(1);                  /* They're the same */
  9992.                     }
  9993.                 } else {
  9994.                     /* Close ticket file */
  9995.                     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9996.                     (void) tf_close();
  9997.                     return(1);                  /* They're the same */
  9998.                 }
  9999. #else /* CHECKADDRS */
  10000.                 /* Close ticket file */
  10001.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10002.                 (void) tf_close();
  10003.                 return(1);      /* valid but no ip address check */
  10004. #endif /* CHECKADDRS */
  10005. #else /* OS2 */
  10006.                 /* Close ticket file */
  10007.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10008.                 (void) tf_close();
  10009.                 return(1);      /* Valid but no ip address check */
  10010. #endif /* OS2 */
  10011.             }
  10012.             else {
  10013.                 /* Close ticket file */
  10014.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10015.                 (void) tf_close();
  10016.                 return(0);      /* expired or otherwise invalid */
  10017.             }
  10018.         }
  10019.     }
  10020.     /* Close ticket file */
  10021.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  10022.     (void) tf_close();
  10023.     return(0);                  /* could not find the desired ticket */
  10024. #else /* KRB4 */
  10025.     return(-1);
  10026. #endif /* KRB4 */
  10027. }
  10028.  
  10029. int
  10030. #ifdef CK_ANSIC
  10031. ck_krb4_is_tgt_valid(VOID)
  10032. #else
  10033. ck_krb4_is_tgt_valid()
  10034. #endif
  10035. {
  10036. #ifdef KRB4
  10037.     char tgt[256];
  10038.     char * s;
  10039.     int rc = 0;
  10040.  
  10041.     s = krb4_d_realm ? krb4_d_realm : ck_krb4_getrealm();
  10042.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",s,"@",s);
  10043.     rc = ck_krb4_tkt_isvalid(tgt);
  10044.     debug(F111,"ck_krb4_is_tgt_valid",tgt,rc);
  10045.     return(rc > 0);
  10046. #else /* KRB4 */
  10047.     return(0);
  10048. #endif /* KRB4 */
  10049. }
  10050.  
  10051. int
  10052. #ifdef CK_ANSIC
  10053. ck_krb4_tkt_time(char * tktname)
  10054. #else
  10055. ck_krb4_tkt_time(tktname) char * tktname;
  10056. #endif
  10057. {
  10058. #ifdef KRB4
  10059.     char   *file=NULL;
  10060.     char    pname[ANAME_SZ];
  10061.     char    pinst[INST_SZ];
  10062.     char    prealm[REALM_SZ];
  10063.     char    buf1[20], buf2[20];
  10064.     int     k_errno;
  10065. #ifdef OS2
  10066.     LEASH_CREDENTIALS creds;
  10067. #else /* OS2 */
  10068.     CREDENTIALS creds;
  10069. #endif /* OS2 */
  10070.  
  10071.     if ( !ck_krb4_is_installed() )
  10072.         return(-1);
  10073.  
  10074.     file = tkt_string();
  10075.  
  10076.     /*
  10077.      * Since krb_get_tf_realm will return a ticket_file error,
  10078.      * we will call tf_init and tf_close first to filter out
  10079.      * things like no ticket file.  Otherwise, the error that
  10080.      * the user would see would be
  10081.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  10082.      * instead of
  10083.      * klist: No ticket file (tf_util)
  10084.      */
  10085.  
  10086.     /* Open ticket file */
  10087.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  10088.         return(-1);
  10089.     }
  10090.  
  10091.     /* Close ticket file */
  10092.     (void) tf_close();
  10093.  
  10094.     /*
  10095.      * We must find the realm of the ticket file here before calling
  10096.      * tf_init because since the realm of the ticket file is not
  10097.      * really stored in the principal section of the file, the
  10098.      * routine we use must itself call tf_init and tf_close.
  10099.      */
  10100.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  10101.         return(-1);
  10102.     }
  10103.  
  10104.     /* Open ticket file */
  10105.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  10106.         return(-1);
  10107.     }
  10108.     /* Get principal name and instance */
  10109.     if ((k_errno = tf_get_pname(pname)) ||
  10110.          (k_errno = tf_get_pinst(pinst))) {
  10111.         tf_close();
  10112.         return(-1);
  10113.     }
  10114.  
  10115.     /*
  10116.      * You may think that this is the obvious place to get the
  10117.      * realm of the ticket file, but it can't be done here as the
  10118.      * routine to do this must open the ticket file.  This is why
  10119.      * it was done before tf_init.
  10120.      */
  10121.  
  10122.     while ((k_errno = tf_get_cred(&creds)) == AUTH_SUCCESS) {
  10123.         char tkt_buf[256];
  10124.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  10125.                  creds.service, (creds.instance[0] ? "." : ""),
  10126.                  creds.instance,
  10127.                  (creds.realm[0] ? "@" : ""), creds.realm,
  10128.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  10129.         if ( !strcmp(tktname,tkt_buf) ) {
  10130.             /* we found the ticket we are looking for */
  10131.             int n = (creds.issue_date
  10132.                       + (((unsigned char) creds.lifetime) * 5 * 60))
  10133.                 - time(0);
  10134.             tf_close();
  10135.             return(n <= 0 ? 0 : n);
  10136.         }
  10137.     }
  10138.     tf_close();
  10139.     return(0);                  /* could not find the desired ticket */
  10140. #else /* KRB4 */
  10141.     return(-1);
  10142. #endif /* KRB4 */
  10143. }
  10144.  
  10145. char *
  10146. #ifdef CK_ANSIC
  10147. ck_krb4_getrealm(void)
  10148. #else
  10149. ck_krb4_getrealm()
  10150. #endif
  10151. {
  10152. #ifdef KRB4
  10153.     char   *file=NULL;
  10154.     int     k_errno;
  10155.     static char realm[256]="";
  10156.     realm[0]='\0';
  10157.  
  10158.     if ( !ck_krb4_is_installed() )
  10159.         return(realm);
  10160.  
  10161.     /* Try to get realm from ticket file */
  10162.     /* If failure get the local realm    */
  10163.  
  10164.     /*
  10165.     * Since krb_get_tf_realm will return a ticket_file error,
  10166.     * we will call tf_init and tf_close first to filter out
  10167.     * things like no ticket file.
  10168.     */
  10169.  
  10170.     /* Open ticket file */
  10171.     file = tkt_string();
  10172.     if (file == NULL || !file[0])
  10173.         return(realm);
  10174.  
  10175.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  10176.         /* Close ticket file */
  10177.         (void) tf_close();
  10178.  
  10179.         k_errno = krb_get_tf_realm(file, realm);
  10180.     }
  10181.     if (k_errno != KSUCCESS) {
  10182.         k_errno = krb_get_lrealm(realm, 1);
  10183.     }
  10184.     return(realm);
  10185. #else /* KRB4 */
  10186.     return("");
  10187. #endif /* KRB4 */
  10188. }
  10189.  
  10190. char *
  10191. #ifdef CK_ANSIC
  10192. ck_krb4_getprincipal(void)
  10193. #else
  10194. ck_krb4_getprincipal()
  10195. #endif
  10196. {
  10197. #ifdef KRB4
  10198.     char   *file=NULL;
  10199.     int     k_errno;
  10200.     static char principal[256]="";
  10201.     char        instance[256]="";
  10202.     char        realm[256]="";
  10203.     principal[0]='\0';
  10204.  
  10205.     if ( !ck_krb4_is_installed() )
  10206.         return(principal);
  10207.  
  10208.     /* Try to get realm from ticket file */
  10209.     /* If failure get the local realm    */
  10210.  
  10211.     /*
  10212.     * Since krb_get_tf_realm will return a ticket_file error,
  10213.     * we will call tf_init and tf_close first to filter out
  10214.     * things like no ticket file.
  10215.     */
  10216.  
  10217.     /* Open ticket file */
  10218.     file = tkt_string();
  10219.     if (file == NULL || !file[0])
  10220.         return(principal);
  10221.  
  10222.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  10223.         /* Close ticket file */
  10224.         (void) tf_close();
  10225.  
  10226.         k_errno = krb_get_tf_fullname(file, principal, instance, realm);
  10227.     }
  10228.     return(principal);
  10229. #else /* KRB4 */
  10230.     return("");
  10231. #endif /* KRB4 */
  10232. }
  10233.  
  10234. static struct tkt_list_item * k5_tkt_list = NULL;
  10235.  
  10236. int
  10237. #ifdef CK_ANSIC
  10238. ck_krb5_get_tkts(char * cc_name)
  10239. #else
  10240. ck_krb5_get_tkts(cc_name) char * cc_name;
  10241. #endif
  10242. {
  10243. #ifdef KRB5
  10244. #ifndef HEIMDAL
  10245.     krb5_context kcontext;
  10246.     krb5_error_code retval;
  10247.     krb5_ccache cache = NULL;
  10248.     krb5_cc_cursor cur;
  10249.     krb5_creds creds;
  10250.     krb5_principal princ=NULL;
  10251.     krb5_flags flags=0;
  10252.     krb5_error_code code=0;
  10253.     int exit_status = 0;
  10254.  
  10255.     int     tkt_count=0;
  10256.     struct  tkt_list_item ** list = &k5_tkt_list;
  10257.  
  10258.     while ( k5_tkt_list ) {
  10259.         struct tkt_list_item * next;
  10260.         next = k5_tkt_list->next;
  10261.         free(k5_tkt_list->name);
  10262.         free(k5_tkt_list);
  10263.         k5_tkt_list = next;
  10264.     }
  10265.  
  10266.     if ( !ck_krb5_is_installed() )
  10267.         return(-1);
  10268.  
  10269.     retval = krb5_init_context(&kcontext);
  10270.     if (retval) {
  10271.         debug(F101,"ck_krb5_get_tkts while initializing krb5","",retval);
  10272.         return(-1);
  10273.     }
  10274.  
  10275.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10276.     if (code != 0) {
  10277.         debug(F111,"ck_krb5_get_tkts while getting ccache",
  10278.                error_message(code),code);
  10279.         tkt_count = -1;
  10280.         goto exit_k5_get_tkt;
  10281.     }
  10282.  
  10283.     flags = 0;                          /* turns off OPENCLOSE mode */
  10284.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10285.         if (code == ENOENT) {
  10286.             debug(F111,"ck_krb5_get_tkts (ticket cache)",
  10287.                    krb5_cc_get_name(kcontext, cache),code);
  10288.         } else {
  10289.             debug(F111,
  10290.                  "ck_krb5_get_tkts while setting cache flags (ticket cache)",
  10291.                   krb5_cc_get_name(kcontext, cache),code);
  10292.         }
  10293.         tkt_count = -1;
  10294.         goto exit_k5_get_tkt;
  10295.     }
  10296.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10297.         debug(F101,"ck_krb5_get_tkts while retrieving principal name",
  10298.                "",code);
  10299.         tkt_count = -1;
  10300.         goto exit_k5_get_tkt;
  10301.     }
  10302.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10303.         debug(F101,"ck_krb5_get_tkts while unparsing principal name",
  10304.                "",code);
  10305.         tkt_count = -1;
  10306.         goto exit_k5_get_tkt;
  10307.     }
  10308.  
  10309.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10310.         debug(F101,"ck_krb5_get_tkts while starting to retrieve tickets",
  10311.                "",code);
  10312.         tkt_count = -1;
  10313.         goto exit_k5_get_tkt;
  10314.     }
  10315.  
  10316.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10317.         char *sname=NULL;
  10318.  
  10319.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10320.         if (retval) {
  10321.             debug(F101,
  10322.                   "ck_krb5_get_tkts while unparsing server name","",retval);
  10323.             tkt_count = -1;
  10324.             goto exit_k5_get_tkt;
  10325.         }
  10326.  
  10327.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  10328.         (*list)->name = sname;
  10329.         (*list)->next = NULL;
  10330.         list = &((*list)->next);
  10331.  
  10332.         krb5_free_unparsed_name(kcontext,sname);
  10333.         krb5_free_cred_contents(kcontext, &creds);
  10334.         tkt_count++;
  10335.     }
  10336.  
  10337.     if (code == KRB5_CC_END) {
  10338.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10339.             debug(F101,"ck_krb5_get_tkts while finishing ticket retrieval",
  10340.                    "",code);
  10341.             tkt_count = -1;
  10342.             goto exit_k5_get_tkt;
  10343.         }
  10344.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10345.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10346.             debug(F101,"ck_krb5_get_tkts while closing ccache",
  10347.                    "",code);
  10348.             tkt_count = -1;
  10349.             goto exit_k5_get_tkt;
  10350.         }
  10351.     } else {
  10352.         debug(F101,"ck_krb5_get_tkts while retrieving a ticket","",code);
  10353.         tkt_count = -1;
  10354.         goto exit_k5_get_tkt;
  10355.     }
  10356.  
  10357.   exit_k5_get_tkt:
  10358.     krb5_free_principal(kcontext,princ);
  10359.     krb5_free_unparsed_name(kcontext,defname);
  10360.     krb5_cc_close(kcontext,cache);
  10361.     krb5_free_context(kcontext);
  10362.     return(tkt_count);
  10363. #else /* HEIMDAL */
  10364.     return(-1);
  10365. #endif /* HEIMDAL */
  10366. #else /* KRB5 */
  10367.     return(0);
  10368. #endif /* KRB5 */
  10369. }
  10370.  
  10371. char *
  10372. #ifdef CK_ANSIC
  10373. ck_krb5_get_next_tkt(VOID)
  10374. #else
  10375. ck_krb5_get_next_tkt()
  10376. #endif
  10377. {
  10378. #ifdef KRB5
  10379. #ifndef HEIMDAL
  10380.     static char * s=NULL;
  10381.     struct tkt_list_item * next=NULL;
  10382.  
  10383.     if ( s ) {
  10384.         free(s);
  10385.         s = NULL;
  10386.     }
  10387.  
  10388.     if ( k5_tkt_list == NULL )
  10389.         return(NULL);
  10390.  
  10391.     next = k5_tkt_list->next;
  10392.     s = k5_tkt_list->name;
  10393.     free(k5_tkt_list);
  10394.     k5_tkt_list = next;
  10395.     return(s);
  10396. #else /* HEIMDAL */
  10397.     return("Not implemented");
  10398. #endif /* HEIMDAL */
  10399. #else /* KRB5 */
  10400.     return(NULL);
  10401. #endif /* KRB5 */
  10402. }
  10403.  
  10404. char *
  10405. #ifdef CK_ANSIC
  10406. ck_krb5_tkt_flags(char * cc_name, char * tktname)
  10407. #else
  10408. ck_krb5_tkt_flags(cc_name,tktname) char * cc_name; char * tktname;
  10409. #endif
  10410. {
  10411. #ifdef KRB5
  10412. #ifndef HEIMDAL
  10413.     krb5_context kcontext;
  10414.     krb5_error_code retval;
  10415.     krb5_ccache cache = NULL;
  10416.     krb5_cc_cursor cur;
  10417.     krb5_creds creds;
  10418.     krb5_principal princ=NULL;
  10419.     krb5_flags flags=0;
  10420.     krb5_error_code code=0;
  10421.     char * flag_str = "";
  10422.  
  10423.     if ( !ck_krb5_is_installed() )
  10424.         return("");
  10425.  
  10426.     retval = krb5_init_context(&kcontext);
  10427.     if (retval) {
  10428.         debug(F101,"ck_krb5_tkt_flags while initializing krb5","",retval);
  10429.         return("");
  10430.     }
  10431.  
  10432.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10433.     if (code != 0) {
  10434.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10435.                error_message(code),code);
  10436.         goto exit_k5_get_tkt;
  10437.     }
  10438.  
  10439.     flags = 0;                          /* turns off OPENCLOSE mode */
  10440.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10441.         if (code == ENOENT) {
  10442.             debug(F111,"ck_krb5_tkt_flags (ticket cache)",
  10443.                    krb5_cc_get_name(kcontext, cache),code);
  10444.         } else {
  10445.             debug(F111,
  10446.                  "ck_krb5_tkt_flags while setting cache flags (ticket cache)",
  10447.                   krb5_cc_get_name(kcontext, cache),code);
  10448.         }
  10449.         retval = -1;
  10450.         goto exit_k5_get_tkt;
  10451.     }
  10452.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10453.         debug(F101,"ck_krb5_tkt_flags while retrieving principal name",
  10454.                "",code);
  10455.         retval = -1;
  10456.         goto exit_k5_get_tkt;
  10457.     }
  10458.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10459.         debug(F101,"ck_krb5_tkt_flags while unparsing principal name",
  10460.                "",code);
  10461.         retval = -1;
  10462.         goto exit_k5_get_tkt;
  10463.     }
  10464.  
  10465.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10466.         debug(F101,"ck_krb5_tkt_flags while starting to retrieve tickets",
  10467.                "",code);
  10468.         retval = -1;
  10469.         goto exit_k5_get_tkt;
  10470.     }
  10471.  
  10472.     if ((code = krb5_timeofday(kcontext, &now))) {
  10473.         if (!status_only)
  10474.             debug(F101,"ck_krb5_tkt_flags while getting time of day.",
  10475.                    "",code);
  10476.         retval = -1;
  10477.         goto exit_k5_get_tkt;
  10478.     }
  10479.  
  10480.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10481.         char *sname=NULL;
  10482.  
  10483.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10484.         if (retval) {
  10485.             debug(F101,
  10486.                   "ck_krb5_tkt_flags while unparsing server name","",retval);
  10487.             retval = -1;
  10488.             krb5_free_cred_contents(kcontext, &creds);
  10489.             goto exit_k5_get_tkt;
  10490.         }
  10491.  
  10492.         if ( !strcmp(sname,tktname) ) {
  10493.             /* we found the ticket we are looking for */
  10494.  
  10495.             flag_str = flags_string(&creds);
  10496.  
  10497.             krb5_free_unparsed_name(kcontext,sname);
  10498.             krb5_free_cred_contents(kcontext, &creds);
  10499.             code = KRB5_CC_END;
  10500.             break;
  10501.         }
  10502.         krb5_free_unparsed_name(kcontext,sname);
  10503.         krb5_free_cred_contents(kcontext, &creds);
  10504.     }
  10505.  
  10506.     if (code == KRB5_CC_END) {
  10507.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10508.             debug(F101,"ck_krb5_tkt_flags while finishing ticket retrieval",
  10509.                    "",code);
  10510.             goto exit_k5_get_tkt;
  10511.         }
  10512.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10513.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10514.             debug(F101,"ck_krb5_tkt_flags while closing ccache",
  10515.                    "",code);
  10516.             goto exit_k5_get_tkt;
  10517.         }
  10518.     } else {
  10519.         debug(F101,"ck_krb5_tkt_flags while retrieving a ticket","",code);
  10520.         goto exit_k5_get_tkt;
  10521.     }
  10522.  
  10523.   exit_k5_get_tkt:
  10524.     krb5_free_principal(kcontext,princ);
  10525.     krb5_free_unparsed_name(kcontext,defname);
  10526.     krb5_cc_close(kcontext,cache);
  10527.     krb5_free_context(kcontext);
  10528.     return(flag_str);
  10529. #else /* HEIMDAL */
  10530.     return("Not implemented");
  10531. #endif /* HEIMDAL */
  10532. #else /* KRB5 */
  10533.     return("");
  10534. #endif /* KRB5 */
  10535. }
  10536.  
  10537.  
  10538. int
  10539. #ifdef CK_ANSIC
  10540. ck_krb5_tkt_isvalid(char * cc_name, char * tktname)
  10541. #else
  10542. ck_krb5_tkt_isvalid(cc_name,tktname) char * cc_name; char * tktname;
  10543. #endif
  10544. {
  10545. #ifdef KRB5
  10546. #ifndef HEIMDAL
  10547.     krb5_context kcontext=NULL;
  10548.     krb5_error_code retval;
  10549.     krb5_ccache cache = NULL;
  10550.     krb5_cc_cursor cur;
  10551.     krb5_creds creds;
  10552.     krb5_principal princ=NULL;
  10553.     krb5_flags flags=0;
  10554.     krb5_error_code code=0;
  10555. #ifdef CHECKADDRS
  10556.     krb5_address **     myAddrs=NULL;
  10557.     krb5_address **     p=NULL;
  10558.     BOOL                Addrfound = FALSE;
  10559. #endif /*CHECKADDRS*/
  10560.  
  10561.     if ( !ck_krb5_is_installed() )
  10562.         return(-1);
  10563.  
  10564.     retval = krb5_init_context(&kcontext);
  10565.     if (retval) {
  10566.         debug(F101,"ck_krb5_tkt_isvalid while initializing krb5","",retval);
  10567.         return(-1);
  10568.     }
  10569.  
  10570.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10571.     if (code != 0) {
  10572.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10573.                error_message(code),code);
  10574.         goto exit_k5_get_tkt;
  10575.     }
  10576.  
  10577.     flags = 0;                          /* turns off OPENCLOSE mode */
  10578.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10579.         if (code == ENOENT) {
  10580.             debug(F111,"ck_krb5_tkt_isvalid (ticket cache)",
  10581.                    krb5_cc_get_name(kcontext, cache),code);
  10582.         } else {
  10583.             debug(F111,
  10584.                 "ck_krb5_tkt_isvalid while setting cache flags (ticket cache)",
  10585.                   krb5_cc_get_name(kcontext, cache),code);
  10586.         }
  10587.         retval = -1;
  10588.         goto exit_k5_get_tkt;
  10589.     }
  10590.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10591.         debug(F101,"ck_krb5_tkt_isvalid while retrieving principal name",
  10592.                "",code);
  10593.         retval = -1;
  10594.         goto exit_k5_get_tkt;
  10595.     }
  10596.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10597.         debug(F101,"ck_krb5_tkt_isvalid while unparsing principal name",
  10598.                "",code);
  10599.         retval = -1;
  10600.         goto exit_k5_get_tkt;
  10601.     }
  10602.  
  10603.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10604.         debug(F101,"ck_krb5_tkt_isvalid while starting to retrieve tickets",
  10605.                "",code);
  10606.         retval = -1;
  10607.         goto exit_k5_get_tkt;
  10608.     }
  10609.  
  10610.     if ((code = krb5_timeofday(kcontext, &now))) {
  10611.         if (!status_only)
  10612.             debug(F101,"ck_krb5_tkt_isvalid while getting time of day.",
  10613.                    "",code);
  10614.         retval = -1;
  10615.         goto exit_k5_get_tkt;
  10616.     }
  10617.  
  10618.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10619.         char *sname=NULL;
  10620.  
  10621.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10622.         if (retval) {
  10623.             debug(F101,
  10624.                   "ck_krb5_tkt_isvalid while unparsing server name","",retval);
  10625.             retval = -1;
  10626.             krb5_free_cred_contents(kcontext, &creds);
  10627.             goto exit_k5_get_tkt;
  10628.         }
  10629.  
  10630.         if ( !strcmp(sname,tktname) ) {
  10631.             /* we found the ticket we are looking for */
  10632.  
  10633.             /* We add a 5 minutes fudge factor to compensate for potential */
  10634.             /* clock skew errors between the KDC and K95's host OS         */
  10635.  
  10636.             retval = ((creds.times.starttime > 0) &&
  10637.                        now >= (creds.times.starttime - 300) &&
  10638.                        now < (creds.times.endtime + 300) &&
  10639.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10640.  
  10641. #ifdef CHECKADDRS
  10642.             if ( retval && krb5_checkaddrs &&
  10643.                                  creds.addresses && creds.addresses[0] ) {
  10644.                 /* if we think it is valid, then lets check the IP Addresses */
  10645.                 /* to make sure it is valid for our current connection.      */
  10646.                 /* Also make sure it's for the correct IP address */
  10647.                 retval = krb5_os_localaddr(kcontext, &myAddrs);
  10648.                 if (retval) {
  10649.                     com_err(NULL, retval, "retrieving my IP address");
  10650.                     krb5_free_unparsed_name(kcontext,sname);
  10651.                     krb5_free_cred_contents(kcontext, &creds);
  10652.                     code = KRB5_CC_END;
  10653.                     retval = -1;
  10654.                     break;
  10655.                 }
  10656.  
  10657.              /* See if any of our addresses match any in cached credentials */
  10658.  
  10659.                 for (Addrfound=FALSE, p=myAddrs;
  10660.                      (Addrfound==FALSE) && (*p);
  10661.                      p++
  10662.                      ) {
  10663.                     if (krb5_address_search(kcontext, *p, creds.addresses)) {
  10664.                         Addrfound = TRUE;
  10665.                     }
  10666.                 }
  10667.                 krb5_free_addresses(k5_context, myAddrs);
  10668.  
  10669.                 if (Addrfound) {
  10670.                     krb5_free_unparsed_name(kcontext,sname);
  10671.                     krb5_free_cred_contents(kcontext, &creds);
  10672.                     code = KRB5_CC_END;
  10673.                     retval = 1;
  10674.                     break;
  10675.                 } else {
  10676.                     krb5_free_unparsed_name(kcontext,sname);
  10677.                     krb5_free_cred_contents(kcontext, &creds);
  10678.                     code = KRB5_CC_END;
  10679.                     retval = 0;
  10680.                     break;
  10681.                 }
  10682.             }
  10683. #endif /* CHECKADDRS */
  10684.  
  10685.             krb5_free_unparsed_name(kcontext,sname);
  10686.             krb5_free_cred_contents(kcontext, &creds);
  10687.             code = KRB5_CC_END;
  10688.             break;
  10689.         }
  10690.         krb5_free_unparsed_name(kcontext,sname);
  10691.         krb5_free_cred_contents(kcontext, &creds);
  10692.     }
  10693.  
  10694.     if (code == KRB5_CC_END) {
  10695.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10696.             debug(F101,"ck_krb5_tkt_isvalid while finishing ticket retrieval",
  10697.                    "",code);
  10698.             retval = -1;
  10699.             goto exit_k5_get_tkt;
  10700.         }
  10701.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10702.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10703.             debug(F101,"ck_krb5_tkt_isvalid while closing ccache",
  10704.                    "",code);
  10705.             retval = -1;
  10706.             goto exit_k5_get_tkt;
  10707.         }
  10708.     } else {
  10709.         debug(F101,"ck_krb5_tkt_isvalid while retrieving a ticket","",code);
  10710.         retval = -1;
  10711.         goto exit_k5_get_tkt;
  10712.     }
  10713.  
  10714.   exit_k5_get_tkt:
  10715.     krb5_free_principal(kcontext,princ);
  10716.     krb5_free_unparsed_name(kcontext,defname);
  10717.     krb5_cc_close(kcontext,cache);
  10718.     krb5_free_context(kcontext);
  10719.     return(retval);
  10720. #else /* HEIMDAL */
  10721.     return(-1);
  10722. #endif /* HEIMDAL */
  10723. #else /* KRB5 */
  10724.     return(-1);
  10725. #endif /* KRB5 */
  10726. }
  10727.  
  10728. int
  10729. #ifdef CK_ANSIC
  10730. ck_krb5_is_tgt_valid(VOID)
  10731. #else
  10732. ck_krb5_is_tgt_valid()
  10733. #endif
  10734. {
  10735. #ifdef KRB5
  10736. #ifndef HEIMDAL
  10737.     char tgt[256];
  10738.     char * s;
  10739.     int rc = 0;
  10740.  
  10741.     s = ck_krb5_getrealm(krb5_d_cc);
  10742.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",s,"@",s);
  10743.     rc = ck_krb5_tkt_isvalid(krb5_d_cc,tgt);
  10744.     debug(F111,"ck_krb5_is_tgt_valid",tgt,rc);
  10745.     return(rc>0);
  10746. #else /* HEIMDAL */
  10747.     return(-1);
  10748. #endif /* HEIMDAL */
  10749. #else /* KRB5 */
  10750.     return(0);
  10751. #endif /* KRB5 */
  10752. }
  10753.  
  10754. int
  10755. #ifdef CK_ANSIC
  10756. ck_krb5_tkt_time(char * cc_name, char * tktname)
  10757. #else
  10758. ck_krb5_tkt_time(cc_name, tktname) char * cc_name; char * tktname;
  10759. #endif
  10760. {
  10761. #ifdef KRB5
  10762. #ifndef HEIMDAL
  10763.     krb5_context kcontext;
  10764.     krb5_error_code retval;
  10765.     krb5_ccache cache = NULL;
  10766.     krb5_cc_cursor cur;
  10767.     krb5_creds creds;
  10768.     krb5_principal princ=NULL;
  10769.     krb5_flags flags=0;
  10770.     krb5_error_code code=0;
  10771.  
  10772.     if ( !ck_krb5_is_installed() )
  10773.         return(-1);
  10774.  
  10775.     retval = krb5_init_context(&kcontext);
  10776.     if (retval) {
  10777.         debug(F101,"ck_krb5_list_creds while initializing krb5","",retval);
  10778.         return(-1);
  10779.     }
  10780.  
  10781.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10782.     if (code != 0) {
  10783.         debug(F111,"ck_krb5_tkt_time while getting ccache",
  10784.                error_message(code),code);
  10785.         retval = -1;
  10786.         goto exit_k5_get_tkt;
  10787.     }
  10788.  
  10789.     flags = 0;                          /* turns off OPENCLOSE mode */
  10790.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10791.         if (code == ENOENT) {
  10792.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  10793.                    krb5_cc_get_name(kcontext, cache),code);
  10794.         } else {
  10795.             debug(F111,
  10796.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  10797.                   krb5_cc_get_name(kcontext, cache),code);
  10798.         }
  10799.         retval = -1;
  10800.         goto exit_k5_get_tkt;
  10801.     }
  10802.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10803.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  10804.                "",code);
  10805.         retval = -1;
  10806.         goto exit_k5_get_tkt;
  10807.     }
  10808.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10809.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  10810.                "",code);
  10811.         retval = -1;
  10812.         goto exit_k5_get_tkt;
  10813.     }
  10814.  
  10815.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10816.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  10817.                "",code);
  10818.         retval = -1;
  10819.         goto exit_k5_get_tkt;
  10820.     }
  10821.  
  10822.     if ((code = krb5_timeofday(kcontext, &now))) {
  10823.         if (!status_only)
  10824.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  10825.                    "",code);
  10826.         krb5_free_context(kcontext);
  10827.         return(-1);
  10828.     }
  10829.  
  10830.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10831.         char *sname=NULL;
  10832.  
  10833.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10834.         if (retval) {
  10835.             debug(F101,
  10836.                   "ck_krb5_list_creds while unparsing server name","",retval);
  10837.             retval = -1;
  10838.             krb5_free_unparsed_name(kcontext,sname);
  10839.             krb5_free_cred_contents(kcontext, &creds);
  10840.             goto exit_k5_get_tkt;
  10841.         }
  10842.  
  10843.         if ( !strcmp(sname,tktname) ) {
  10844.             /* we found the ticket we are looking for */
  10845.             int valid = (creds.times.starttime &&
  10846.                        now > creds.times.starttime &&
  10847.                        now < creds.times.endtime &&
  10848.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10849.             if ( valid ) {
  10850.                 retval = creds.times.endtime - now;
  10851.             }
  10852.             else
  10853.                 retval = 0;
  10854.             krb5_free_unparsed_name(kcontext,sname);
  10855.             krb5_free_cred_contents(kcontext, &creds);
  10856.             code = KRB5_CC_END;
  10857.             break;
  10858.         }
  10859.         krb5_free_unparsed_name(kcontext,sname);
  10860.         krb5_free_cred_contents(kcontext, &creds);
  10861.     }
  10862.  
  10863.     if (code == KRB5_CC_END) {
  10864.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10865.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  10866.                    "",code);
  10867.             retval = -1;
  10868.             goto exit_k5_get_tkt;
  10869.         }
  10870.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10871.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10872.             debug(F101,"ck_krb5_list_creds while closing ccache",
  10873.                    "",code);
  10874.             retval = -1;
  10875.             goto exit_k5_get_tkt;
  10876.         }
  10877.     } else {
  10878.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  10879.         retval = -1;
  10880.         goto exit_k5_get_tkt;
  10881.     }
  10882.  
  10883.   exit_k5_get_tkt:
  10884.     krb5_free_principal(kcontext,princ);
  10885.     krb5_free_unparsed_name(kcontext,defname);
  10886.     krb5_cc_close(kcontext,cache);
  10887.     krb5_free_context(kcontext);
  10888.     return(retval);
  10889. #else /* HEIMDAL */
  10890.     return(-1);
  10891. #endif /* HEIMDAL */
  10892. #else /* KRB5 */
  10893.     return(-1);
  10894. #endif /* KRB5 */
  10895. }
  10896.  
  10897. char *
  10898. #ifdef CK_ANSIC
  10899. ck_krb5_get_cc_name(void)
  10900. #else
  10901. ck_krb5_get_cc_name()
  10902. #endif
  10903. {
  10904. #ifdef KRB5
  10905. #ifndef HEIMDAL
  10906.     static char cc_name[CKMAXPATH+1]="";
  10907.     krb5_context kcontext = NULL;
  10908.     krb5_ccache ccache = NULL;
  10909.     krb5_error_code code;
  10910.     char * p=NULL;
  10911.  
  10912.     cc_name[0] = '\0';
  10913.  
  10914.     if ( !ck_krb5_is_installed() )
  10915.         return(cc_name);
  10916.  
  10917.     p = getenv("KRB5CCNAME");
  10918.     if ( !p ) {
  10919.         code = krb5_init_context(&kcontext);
  10920.         if (code) {
  10921.             com_err("ck_krb5_get_cc_name",code,"while init_context");
  10922.             return(cc_name);
  10923.         }
  10924.         if ((code = krb5_cc_default(kcontext, &ccache))) {
  10925.             com_err("ck_krb5_get_cc_name",code,"while getting default ccache");
  10926.             goto exit_k5_get_cc;
  10927.         }
  10928.  
  10929.         ckmakmsg(cc_name,sizeof(cc_name),
  10930.                  (char *)krb5_cc_get_type(kcontext,ccache),":",
  10931.                  (char *)krb5_cc_get_name(kcontext,ccache),NULL);
  10932.     } else {
  10933.         ckstrncpy(cc_name,p,CKMAXPATH);
  10934.     }
  10935.  
  10936.     if ( !strncmp("FILE:",cc_name,5) ) {
  10937.         for ( p=cc_name; *p ; p++ )
  10938.             if ( *p == '\\' ) *p = '/';
  10939.     }
  10940.  
  10941.   exit_k5_get_cc:
  10942.     if ( ccache )
  10943.         krb5_cc_close(kcontext,ccache);
  10944.     if ( kcontext )
  10945.         krb5_free_context(kcontext);
  10946.     return(cc_name);
  10947. #else /* HEIMDAL */
  10948.     return("Not implemented");
  10949. #endif /* HEIMDAL */
  10950. #else /* KRB5 */
  10951.     return("");
  10952. #endif /* KRB5 */
  10953. }
  10954.  
  10955. char *
  10956. #ifdef CK_ANSIC
  10957. ck_krb5_getrealm(char * cc_name)
  10958. #else
  10959. ck_krb5_getrealm(cc_name) char * cc_name;
  10960. #endif
  10961. {
  10962. #ifdef KRB5
  10963. #ifndef HEIMDAL
  10964.     static char realm[256]="";
  10965.     krb5_context kcontext;
  10966.     krb5_ccache ccache = NULL;
  10967.     krb5_error_code code;
  10968.     krb5_principal me=NULL;
  10969.  
  10970.     realm[0] = '\0';
  10971.  
  10972.     if ( !ck_krb5_is_installed() )
  10973.         return(realm);
  10974.  
  10975.     code = krb5_init_context(&kcontext);
  10976.     if (code) {
  10977.         return(realm);
  10978.     }
  10979.  
  10980.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10981.     if (code != 0) {
  10982.         goto exit_k5_getrealm;
  10983.     }
  10984.  
  10985.     code = krb5_cc_get_principal(kcontext, ccache, &me);
  10986.     if (code)
  10987.         code = krb5_parse_name(kcontext, "foo", &me);
  10988.     if (code) {
  10989.         goto exit_k5_getrealm;
  10990.     }
  10991.     if ( krb5_princ_realm(kcontext, me)->length < sizeof(realm) ) {
  10992.         memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  10993.                 krb5_princ_realm(kcontext, me)->length);        /* safe */
  10994.        realm[krb5_princ_realm(kcontext, me)->length]='\0';
  10995.     }
  10996.   exit_k5_getrealm:
  10997.     if ( me )
  10998.         krb5_free_principal(kcontext,me);
  10999.     if ( ccache )
  11000.         krb5_cc_close(kcontext,ccache);
  11001.     if (kcontext)
  11002.         krb5_free_context(kcontext);
  11003.     return(realm);
  11004. #else /* HEIMDAL */
  11005.     return("Not implemented");
  11006. #endif /* HEIMDAL */
  11007. #else /* KRB5 */
  11008.     return("");
  11009. #endif /* KRB5 */
  11010. }
  11011.  
  11012. char *
  11013. #ifdef CK_ANSIC
  11014. ck_krb5_getprincipal(char * cc_name)
  11015. #else
  11016. ck_krb5_getprincipal(cc_name) char * cc_name;
  11017. #endif
  11018. {
  11019. #ifdef KRB5
  11020. #ifndef HEIMDAL
  11021.     static char principal[UIDBUFLEN+1]="";
  11022.     krb5_context kcontext;
  11023.     krb5_ccache ccache = NULL;
  11024.     krb5_error_code code;
  11025.     krb5_principal me;
  11026.     char * p=NULL;
  11027.     int i;
  11028.  
  11029.     principal[0] = '\0';
  11030.  
  11031.     if ( !ck_krb5_is_installed() )
  11032.         return(principal);
  11033.  
  11034.     code = krb5_init_context(&kcontext);
  11035.     if (code) {
  11036.         return(principal);
  11037.     }
  11038.  
  11039.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  11040.     if (code != 0) {
  11041.         goto exit_k5_getprincipal;
  11042.     }
  11043.  
  11044.     if ((code = krb5_cc_get_principal(kcontext, ccache, &me))) {
  11045.         goto exit_k5_getprincipal;
  11046.     }
  11047.  
  11048.     if ((code = krb5_unparse_name (kcontext, me, &p))) {
  11049.         krb5_free_principal(kcontext,me);
  11050.         goto exit_k5_getprincipal;
  11051.     }
  11052.  
  11053.     ckstrncpy(principal,p,UIDBUFLEN);
  11054.     i = ckindex("@",principal,0,0,0);
  11055.     if (i)
  11056.       principal[i-1] = '\0';
  11057.  
  11058.     krb5_free_unparsed_name(kcontext,p);
  11059.  
  11060.   exit_k5_getprincipal:
  11061.     if ( ccache )
  11062.         krb5_cc_close(kcontext,ccache);
  11063.     if (kcontext)
  11064.         krb5_free_context(kcontext);
  11065.     return(principal);
  11066. #else /* HEIMDAL */
  11067.     return("Not implemented");
  11068. #endif /* HEIMDAL */
  11069. #else /* KRB5 */
  11070.     return("");
  11071. #endif /* KRB5 */
  11072. }
  11073.  
  11074. #ifndef CRYPT_DLL
  11075. int
  11076. ck_get_crypt_table(struct keytab ** pTable, int * pN)
  11077. {
  11078. #ifdef CK_ENCRYPTION
  11079.     return(get_crypt_table(pTable, pN));
  11080. #else /* ENCRYPTION */
  11081.     int i=0;
  11082. #ifndef OS2
  11083.     char * tmpstring = NULL;
  11084. #endif /* OS2 */
  11085.  
  11086.     if ( *pTable )
  11087.     {
  11088.         for ( i=0 ; i < *pN ; i++ )
  11089.             free( (*pTable)[i].kwd ) ;
  11090.         free ( *pTable )  ;
  11091.     }
  11092.     *pTable = NULL;
  11093.     *pN = 0;
  11094.  
  11095.     *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  11096.     if ( !(*pTable) )
  11097.         return(0);
  11098.  
  11099. #ifdef OS2
  11100.     (*pTable)[0].kwd =strdup("automatic");
  11101. #else /* OS2 */
  11102.     makestr(&tmpstring,"automatic");
  11103.     (*pTable)[0].kwd = tmpstring;
  11104.     tmpstring = NULL;
  11105. #endif /* OS2 */
  11106.     (*pTable)[0].kwval = ENCTYPE_ANY;
  11107.     (*pTable)[0].flgs = 0;
  11108. #ifdef OS2
  11109.     (*pTable)[1].kwd =strdup("none");
  11110. #else /* OS2 */
  11111.     makestr(&tmpstring,"none");
  11112.     (*pTable)[1].kwd = tmpstring;
  11113.     tmpstring = NULL;
  11114. #endif /* OS2 */
  11115.     (*pTable)[1].kwval = 999;
  11116.     (*pTable)[1].flgs = 0;
  11117.     (*pN) = 2;
  11118.  
  11119.     return(2);
  11120. #endif /* ENCRYPTION */
  11121. }
  11122.  
  11123. VOID
  11124. ck_encrypt_send_support()
  11125. {
  11126. #ifdef CK_ENCRYPTION
  11127.     encrypt_send_support();
  11128. #endif /* ENCRYPTION */
  11129. }
  11130. #endif /* CRYPT_DLL */
  11131.  
  11132. /*
  11133.  *
  11134.  * Kstream
  11135.  *
  11136.  * Emulates the kstream package in Kerberos 4
  11137.  *
  11138.  */
  11139.  
  11140. int
  11141. kstream_destroy()
  11142. {
  11143.     if (g_kstream != NULL) {
  11144.         auth_destroy();                       /* Destroy authorizing */
  11145.         free(g_kstream);
  11146.         g_kstream=NULL;
  11147.     }
  11148.     return 0;
  11149. }
  11150.  
  11151. VOID
  11152. #ifdef CK_ANSIC
  11153. kstream_set_buffer_mode(int mode)
  11154. #else
  11155. kstream_set_buffer_mode(mode) int mode;
  11156. #endif
  11157. {
  11158. }
  11159.  
  11160.  
  11161. int
  11162. #ifdef CK_ANSIC
  11163. kstream_create_from_fd(int fd,
  11164.                        kstream_ptr data)
  11165. #else
  11166. kstream_create_from_fd(fd,data)
  11167.     int fd; kstream_ptr data;
  11168. #endif
  11169. {
  11170.     int n;
  11171.  
  11172.     g_kstream = malloc(sizeof(struct kstream_int));
  11173.     if (g_kstream == NULL)
  11174.         return 0;
  11175.  
  11176.     g_kstream->fd = fd;
  11177.  
  11178.     n = auth_init(g_kstream);                   /* Initialize authorizing */
  11179.     if (n) {
  11180.         free(g_kstream);
  11181.         g_kstream = NULL;
  11182.         return 0;
  11183.     }
  11184.  
  11185.     g_kstream->encrypt = NULL;
  11186.     g_kstream->decrypt = NULL;
  11187.     g_kstream->encrypt_type = ENCTYPE_ANY;
  11188.     g_kstream->decrypt_type = ENCTYPE_ANY;
  11189.     return 1;
  11190. }
  11191.  
  11192. #ifdef CK_KERBEROS
  11193. #ifdef RLOGCODE
  11194. static int do_lencheck, use_ivecs;
  11195. extern int rlog_inband;
  11196.  
  11197. #ifdef KRB5
  11198. void
  11199. rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
  11200.                            protonum)
  11201.      krb5_keyblock *in_keyblock;
  11202.      int encrypt_flag;
  11203.      int lencheck;
  11204.      int am_client;
  11205.      enum krb5_kcmd_proto protonum;
  11206. {
  11207.     krb5_error_code status;
  11208.     size_t blocksize;
  11209.  
  11210.     if (!encrypt_flag)
  11211.         return;
  11212.  
  11213.     desinbuf.data = des_inbuf;
  11214.     desoutbuf.data = des_outpkt+4;      /* Set up des buffers */
  11215.     k5_session_key = in_keyblock;
  11216.  
  11217.     do_lencheck = lencheck;
  11218.  
  11219.     if ( protonum == KCMD_OLD_PROTOCOL ) {
  11220.         use_ivecs = 0;
  11221.         return;
  11222.     }
  11223.  
  11224.     use_ivecs = 1;
  11225.  
  11226.     if (status = krb5_c_block_size(k5_context, k5_session_key->enctype,
  11227.                                    &blocksize)) {
  11228.         /* XXX what do I do? */
  11229.         printf("fatal kerberos 5 crypto library error\n");
  11230.         ttclos(0);
  11231.         return;
  11232.     }
  11233.  
  11234.     encivec_i[0].length = encivec_i[1].length = 
  11235.     encivec_o[0].length = encivec_o[1].length = blocksize;
  11236.  
  11237.     if ((encivec_i[0].data = malloc(encivec_i[0].length * 4)) == NULL) {
  11238.         /* XXX what do I do? */
  11239.         printf("fatal malloc failed\n");
  11240.         ttclos(0);
  11241.         return;
  11242.     }
  11243.  
  11244.     encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
  11245.     encivec_o[0].data = encivec_i[1].data + encivec_i[1].length;
  11246.     encivec_o[1].data = encivec_o[0].data + encivec_o[0].length;
  11247.  
  11248.     /* is there a better way to initialize this? */
  11249.     memset(encivec_i[0].data, am_client, blocksize);
  11250.     memset(encivec_o[0].data, 1 - am_client, blocksize);
  11251.     memset(encivec_i[1].data, 2 | am_client, blocksize);
  11252.     memset(encivec_o[1].data, 2 | (1 - am_client), blocksize);
  11253. }
  11254. #endif /* KRB5 */
  11255.  
  11256. int
  11257. #ifdef CK_ANSIC
  11258. ck_krb_rlogin(CHAR * hostname, int port,
  11259.                CHAR * localuser, CHAR * remoteuser, CHAR * term_speed,
  11260.                struct sockaddr_in * l_addr, struct sockaddr_in * r_addr,
  11261.                int kversion, int encrypt_flag)
  11262. #else /* CK_ANSIC */
  11263. ck_krb_rlogin(hostname, port,
  11264.                localuser, remoteuser, term_speed, l_addr, r_addr, encrypt_flag)
  11265.     CHAR * hostname; int port;
  11266.     CHAR * localuser; CHAR * remoteuser; CHAR * term_speed;
  11267.     struct sockaddr_in * l_addr; struct sockaddr_in * r_addr;
  11268.     int kversion; int encrypt_flag;
  11269. #endif /* CK_ANSIC */
  11270. {
  11271.     unsigned long status;
  11272.     char * realm=NULL;
  11273.     extern int ttyfd;
  11274.     int c;
  11275.     long msglen;
  11276.  
  11277.     debug(F111,"ck_krb_rlogin",hostname,port);
  11278.  
  11279.     if ( kversion == 4 && !ck_krb4_is_installed() ) {
  11280.         printf("?Kerberos 4 is not installed\r\n");
  11281.         return(-1);
  11282.     } else if ( kversion == 5 && !ck_krb5_is_installed() ) {
  11283.         printf("?Kerberos 5 is not installed\r\n");
  11284.         return(-1);
  11285.     }
  11286.  
  11287.     if ( encrypt_flag && !ck_crypt_is_installed() ) {
  11288.         printf("?Encryption is not installed\r\n");
  11289.         return(-1);
  11290.     }
  11291.  
  11292.     if ( kversion == 5 ) {
  11293. #ifdef KRB5
  11294.         krb5_flags authopts=0;
  11295.         krb5_ccache ccache=NULL;
  11296.         char *cksumbuf=NULL;
  11297.         char *service=NULL;
  11298.         char * kcmd_version=NULL;
  11299.         enum krb5_kcmd_proto use_proto;
  11300.         krb5_data cksumdat;
  11301.         krb5_creds *get_cred = 0;
  11302.         krb5_error_code status;
  11303.         krb5_error      *error = 0;
  11304.         krb5_ap_rep_enc_part *rep_ret = NULL;
  11305.         krb5_data outbuf;
  11306.         int rc;
  11307.         krb5_int32 seqno=0;
  11308.         krb5_int32 server_seqno=0;
  11309.         char ** realmlist=NULL;
  11310.         int buflen;
  11311.         char tgt[256];
  11312.  
  11313.         debug(F100,"ck_krb_rlogin version 5","",0);
  11314.  
  11315.         realm = ck_krb5_realmofhost(hostname);
  11316.         if (!realm) {
  11317.             ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  11318.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11319.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11320.             printf("?Kerberos 5 error: %s\r\n",strTmp);
  11321.             krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  11322.             makestr(&krb5_errmsg,strTmp);
  11323.             return(-1);
  11324.         }
  11325.  
  11326.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11327.         debug(F110,"ck_rlog_rlogin TGT",tgt,0);
  11328.         if ( krb5_autoget &&
  11329.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11330.                 (ck_krb5_is_tgt_valid() > 0)) )
  11331.             ck_krb5_autoget_TGT(realm);
  11332.  
  11333.         buflen = strlen(term_speed)+strlen(remoteuser)+64;
  11334.         if ((cksumbuf = malloc(buflen)) == 0)
  11335.         {
  11336.             printf("Unable to allocate memory for checksum buffer.\r\n");
  11337.             return(-1);
  11338.         }
  11339.  
  11340.         ckmakmsg(cksumbuf,buflen,ckuitoa((unsigned short) ntohs(port)),":",
  11341.                   term_speed,remoteuser);
  11342.         cksumdat.data = cksumbuf;
  11343.         cksumdat.length = strlen(cksumbuf);
  11344.  
  11345.         status = krb5_init_context(&k5_context);
  11346.         if (status) {
  11347.             debug(F110,"ck_krb_rlogin()","unable to init_context",0);
  11348.             return(-1);
  11349.         }
  11350.  
  11351.         desinbuf.data = des_inbuf;
  11352.         desoutbuf.data = des_outpkt+4;  /* Set up des buffers */
  11353.  
  11354.         rc = k5_get_ccache(k5_context,&ccache,NULL);
  11355.         if (rc != 0) {
  11356.             com_err(NULL, rc, "while getting ccache.");
  11357.             return(0);
  11358.         }
  11359.  
  11360.         service = krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME;
  11361.  
  11362.         if (!(get_cred = (krb5_creds *)calloc(1, sizeof(krb5_creds)))) {
  11363.             printf("ck_krb_rlogin: no memory\r\n");
  11364.             return(-1);
  11365.         }
  11366.         memset(get_cred,0,sizeof(krb5_creds));
  11367.         status = krb5_sname_to_principal(k5_context, hostname, service,
  11368.                                           KRB5_NT_SRV_HST, &get_cred->server);
  11369.         if (status) {
  11370.             printf("ck_krb_rlogin: krb5_sname_to_principal failed: %s\r\n",
  11371.                      error_message(status));
  11372.             return(-1);
  11373.         }
  11374.  
  11375.         ttoc(0);
  11376.  
  11377.         if (status = krb5_cc_get_principal(k5_context,
  11378.                                            ccache,
  11379.                                            &get_cred->client)
  11380.             ) {
  11381.             (void) krb5_cc_close(k5_context, ccache);
  11382.             krb5_free_creds(k5_context, get_cred);
  11383.             goto bad;
  11384.         }
  11385.  
  11386.         if (krb5_rlog_ver == KCMD_OLD_PROTOCOL)
  11387.             get_cred->keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  11388.  
  11389.         /* Get ticket from credentials cache or kdc */
  11390.         status = krb5_get_credentials(k5_context,
  11391.                                       0,
  11392.                                       ccache,
  11393.                                       get_cred,
  11394.                                       &ret_cred
  11395.                                       );
  11396.         krb5_free_creds(k5_context, get_cred);
  11397.         get_cred = NULL;
  11398.         (void) krb5_cc_close(k5_context, ccache);
  11399.  
  11400.         if (status) 
  11401.             goto bad;
  11402.  
  11403.         /* Reset internal flags; these should not be set. */
  11404.         authopts &= (~OPTS_FORWARD_CREDS);
  11405.         authopts &= (~OPTS_FORWARDABLE_CREDS);
  11406.  
  11407.         if (krb5_auth_con_init(k5_context, &auth_context))
  11408.             goto bad;
  11409.  
  11410.         if (krb5_auth_con_setflags(k5_context, auth_context,
  11411.                                     KRB5_AUTH_CONTEXT_RET_TIME))
  11412.             goto bad;
  11413.  
  11414.         /* Only need local address for mk_cred() to send to krlogind */
  11415.         if (!krb5_d_no_addresses)
  11416.             if (status = krb5_auth_con_genaddrs(k5_context,
  11417.                                                 auth_context,
  11418.                                                  ttyfd,
  11419.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
  11420.                                                  ))
  11421.                 goto bad;
  11422.  
  11423.         /* Here is where we start to handle the new protocol in earnest */
  11424.         if ( krb5_rlog_ver == KCMD_PROTOCOL_COMPAT_HACK ) {
  11425.             krb5_boolean is_des;
  11426.  
  11427.             if (status = krb5_c_enctype_compare( k5_context,
  11428.                                                  ENCTYPE_DES_CBC_CRC,
  11429. #ifdef HEIMDAL
  11430.                                                  ret_cred->session.keytype,
  11431. #else /* HEIMDAL */
  11432.                                                  ret_cred->keyblock.enctype,
  11433. #endif /* HEIMDAL */
  11434.                                                  &is_des)) {
  11435.                 krb5_free_creds(k5_context, ret_cred);
  11436.                 ret_cred = NULL;
  11437.                 goto bad;
  11438.             }
  11439.  
  11440.             if ( is_des ) {
  11441.                 kcmd_version = "KCMDV0.1";
  11442.                 use_proto = KCMD_OLD_PROTOCOL;
  11443.             } else {
  11444.                 authopts = AP_OPTS_USE_SUBKEY;
  11445.                 kcmd_version = "KCMDV0.2";
  11446.                 use_proto = KCMD_NEW_PROTOCOL;
  11447.             }
  11448.         } else {
  11449.             use_proto = krb5_rlog_ver;
  11450.             switch ( krb5_rlog_ver ) {
  11451.             case KCMD_NEW_PROTOCOL:
  11452.                 authopts = AP_OPTS_USE_SUBKEY;
  11453.                 kcmd_version = "KCMDV0.2";
  11454.                 break;
  11455.             case KCMD_OLD_PROTOCOL:
  11456.                 kcmd_version = "KCMDV0.1";
  11457.                 break;
  11458.             default:
  11459.                 goto bad;
  11460.             }
  11461.         }
  11462.  
  11463.         /* call Kerberos library routine to obtain an authenticator,
  11464.            pass it over the socket to the server, and obtain mutual
  11465.            authentication.
  11466.          */
  11467.         status = krb5_sendauth(k5_context,
  11468.                                &auth_context,
  11469.                                (krb5_pointer) &ttyfd,
  11470.                                kcmd_version,
  11471.                                ret_cred->client,
  11472.                                ret_cred->server,
  11473.                                 authopts,
  11474.                                &cksumdat,
  11475.                                ret_cred,
  11476.                                0,
  11477.                                &error,
  11478.                                &rep_ret,
  11479.                                NULL
  11480.                                );
  11481.         krb5_free_data_contents(k5_context,&cksumdat);
  11482.  
  11483.         if (status) {
  11484.             if ( !quiet )
  11485.                 printf("Couldn't authenticate to server: %s\r\n",
  11486.                         error_message(status));
  11487.             if (error) {
  11488.                 if ( !quiet ) {
  11489.                     printf("Server returned error code %d (%s)\r\n",
  11490.                         error->error,
  11491.                         error_message(ERROR_TABLE_BASE_krb5 + error->error));
  11492.                     if (error->text.length) {
  11493.                         printf("Error text sent from server: %s\r\n",
  11494.                                 error->text.data);
  11495.                     }
  11496.                 }
  11497.                 krb5_free_error(k5_context, error);
  11498.                 error = 0;
  11499.             }
  11500.             goto bad;
  11501.         }
  11502.  
  11503.         if (rep_ret) {
  11504.             server_seqno = rep_ret->seq_number;
  11505.             krb5_free_ap_rep_enc_part(k5_context, rep_ret);
  11506.         }
  11507.  
  11508.         (void) ttol(remoteuser, strlen(remoteuser)+1);
  11509.         (void) ttol(term_speed, strlen(term_speed)+1);
  11510.         (void) ttol(localuser, strlen(localuser)+1);
  11511.  
  11512.         if (forward_flag) {   /* Forward credentials (global) */
  11513.             if (status = krb5_fwd_tgt_creds( k5_context,
  11514.                                              auth_context,
  11515.                                              hostname,
  11516.                                              ret_cred->client,
  11517.                                              ret_cred->server,
  11518.                                              0,
  11519.                                              (forwardable_flag ?
  11520.                                                OPTS_FORWARDABLE_CREDS :
  11521.                                                0),
  11522.                                              &outbuf
  11523.                                              )
  11524.                  )
  11525.             {
  11526.                 printf("Error forwarding credentials: %s\r\n",
  11527.                          error_message(status));
  11528.                 goto bad2;
  11529.             }
  11530.  
  11531.             /* Send forwarded credentials */
  11532.             status = krb5_write_message(k5_context,
  11533.                                          (krb5_pointer)&ttyfd,
  11534.                                          &outbuf
  11535.                                          );
  11536.         }
  11537.         else { /* Dummy write to signal no forwarding */
  11538.           bad2:
  11539.             outbuf.length = 0;
  11540.             status = krb5_write_message(k5_context,
  11541.                                          (krb5_pointer)&ttyfd,
  11542.                                          &outbuf);
  11543.         }       
  11544.  
  11545.         if ((c = ttinc(0)) < 0) {
  11546.             if (c==-1) {
  11547.                 perror(hostname);
  11548.             } else {
  11549.                 printf("ck_krb_rlogin: bad connection with remote host\r\n");
  11550.             }
  11551.             status = -1;
  11552.             goto bad;
  11553.         }
  11554.         if (c != 0) {
  11555.             while ((c = ttinc(1)) >= 0) {
  11556.                 (void) printf("%c",c);
  11557.                 if (c == '\n')
  11558.                     break;
  11559.             }
  11560.             status = -1;
  11561.             goto bad;
  11562.         }
  11563.  
  11564.         if ( status == 0 ) {        /* success */
  11565.             krb5_keyblock * key = 0;
  11566.  
  11567.             if ( use_proto == KCMD_NEW_PROTOCOL ) {
  11568.                 int on = 1;
  11569.                 rlog_inband = 1;
  11570.                 setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,
  11571.                            (char *) &on, sizeof on);
  11572.  
  11573.                 status = krb5_auth_con_getlocalsubkey( k5_context,
  11574.                                                        auth_context,
  11575.                                                        &key);
  11576.                 if ((status || !key) && encrypt_flag )
  11577.                     goto bad2;
  11578.             }
  11579.             if ( key == 0 ) {
  11580. #ifdef HEIMDAL
  11581.                 key = &ret_cred->session;
  11582. #else /* HEIMDAL */
  11583.                 key = &ret_cred->keyblock;
  11584. #endif /* HEIMDAL */
  11585.             }
  11586.  
  11587.             rcmd_stream_init_krb5(key, encrypt_flag, 1, 1, use_proto);
  11588.             if ( encrypt_flag )
  11589.                 rlog_encrypt = 1;
  11590.         }
  11591.         return (0);     /* success */
  11592.  
  11593.       bad:
  11594.         if ( status && !quiet ) {
  11595.             printf("Kerberos authentication error: %s\r\n",
  11596.                     error_message(status));
  11597.         }
  11598.         if (ret_cred) {
  11599.             krb5_free_creds(k5_context, ret_cred);
  11600.             ret_cred = NULL;
  11601.         }
  11602.         return (status);
  11603. #else /* KRB5 */
  11604.         return(-1);
  11605. #endif /* KRB5 */
  11606.     } else if (kversion == 4) {
  11607. #ifdef KRB4
  11608.         char tgt[4*REALM_SZ+1];
  11609.         debug(F100,"ck_krb_rlogin version 4","",0);
  11610.  
  11611.         realm = (char *)krb_realmofhost(hostname);
  11612.         if (!realm) {
  11613.             strcpy(strTmp, "Can't find realm for host \"");
  11614.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11615.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11616.             printf("?Kerberos 4 error: %s\r\n",strTmp);
  11617.             krb4_errno = 0;
  11618.             makestr(&krb4_errmsg,strTmp);
  11619.             return(0);
  11620.         }
  11621.  
  11622.         ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  11623.         status = ck_krb4_tkt_isvalid(tgt);
  11624.  
  11625.         if ( status <= 0 && krb4_autoget )
  11626.             ck_krb4_autoget_TGT(realm);
  11627.  
  11628.         ttoc(0);        /* write a NUL */
  11629.  
  11630.         status = krb_sendauth(encrypt_flag?KOPT_DO_MUTUAL:0,
  11631.                                ttyfd,
  11632.                                &k4_auth,
  11633.                                krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  11634.                                hostname,
  11635.                                realm,
  11636.                                (unsigned long) getpid(),
  11637.                                &k4_msg_data,
  11638.                                &cred,
  11639. #ifdef CK_ENCRYPTION
  11640.                                &k4_sched,
  11641. #else /* ENCRYPTION */
  11642.                                NULL,
  11643. #endif /* ENCRYPTION */
  11644.                                l_addr,
  11645.                                r_addr,
  11646.                                "KCMDV0.1");
  11647.         debug(F111,"ck_krb_rlogin","krb_sendauth",status);
  11648.         if (status != KSUCCESS) {
  11649.             printf( "krb_sendauth failed: %s\r\n",
  11650.                     krb_get_err_text_entry(status)
  11651.                     );
  11652.             return(-1);
  11653.         }
  11654.         ttol(remoteuser,strlen(remoteuser)+1);
  11655.         ttol(term_speed,strlen(term_speed)+1);
  11656.  
  11657.       reread:
  11658.         if ((c = ttinc(0)) < 0) {
  11659.             printf("rcmd: bad connection with remote host\r\n");
  11660.             return(-1);
  11661.         }
  11662.         debug(F111,"ck_krb_rlogin","first byte",c);
  11663.  
  11664.         if (c != 0) {
  11665.             char *check = "ld.so: warning:";
  11666.             /* If rlogind was compiled on SunOS4, and it somehow
  11667.             got the shared library version numbers wrong, it
  11668.             may give an ld.so warning about an old version of a
  11669.             shared library.  Just ignore any such warning.
  11670.             Note that the warning is a characteristic of the
  11671.             server; we may not ourselves be running under
  11672.             SunOS4.  */
  11673.             if (c == 'l') {
  11674.                 char *p;
  11675.                 char cc;
  11676.  
  11677.                 p = &check[1];
  11678.                 while ((c = ttinc(0)) >= 0) {
  11679.                     if (*p == '\0') {
  11680.                         if (c == '\n')
  11681.                             break;
  11682.                     } else {
  11683.                         if (c != *p)
  11684.                             break;
  11685.                         ++p;
  11686.                     }
  11687.                 }
  11688.  
  11689.                 if (*p == '\0')
  11690.                     goto reread;
  11691.             }
  11692.  
  11693.             printf(check);
  11694.             while ((c = ttinc(1)) >= 0) {
  11695.                 printf("%c",c);
  11696.                 if (c == '\n')
  11697.                     break;
  11698.             }
  11699.             debug(F110,"ck_krb_rlogin","fatal error 1",0);
  11700.             return(-1);
  11701.         }
  11702.  
  11703. #ifdef CK_ENCRYPTION
  11704.         if ( encrypt_flag ) {
  11705.             /* if we are encrypting we need to setup the encryption */
  11706.             /* routines.                                            */
  11707.             des_key_sched(cred.session, k4_sched);
  11708.             rlog_encrypt = 1;
  11709.         }
  11710. #endif /* ENCRYPTION */
  11711. #else /* KRB4 */
  11712.         return(-1);
  11713. #endif /* KRB4 */
  11714.     }
  11715.     return(0); /* success */
  11716. }
  11717.  
  11718. #define SRAND   srand
  11719. #define RAND    rand
  11720. #define RAND_TYPE       int
  11721.  
  11722. static long
  11723. random_confounder(size, fillin)
  11724. size_t size;
  11725. char * fillin;
  11726. {
  11727.     static int seeded = 0;
  11728.     register unsigned char *real_fill;
  11729.     RAND_TYPE   rval;
  11730.  
  11731.     if (!seeded) {
  11732.         /* time() defined in 4.12.2.4, but returns a time_t, which is an
  11733.            "arithmetic type" (4.12.1) */
  11734.         rval = (RAND_TYPE) time(0);
  11735.         SRAND(rval);
  11736.         rval = RAND();
  11737.         rval ^= getpid();
  11738.         SRAND(rval);
  11739.         seeded = 1;
  11740.     }
  11741.  
  11742.     real_fill = (unsigned char *)fillin;
  11743.     while (size > 0) {
  11744.         rval = RAND();
  11745.         *real_fill = rval & 0xff;
  11746.         real_fill++;
  11747.         size--;
  11748.         if (size) {
  11749.             *real_fill = (rval >> 8) & 0xff;
  11750.             real_fill++;
  11751.             size--;
  11752.         }
  11753.     }
  11754.     return 0;
  11755. }
  11756.  
  11757. #ifdef KRB5
  11758. int
  11759. krb5_des_avail(fd)
  11760.     int fd;
  11761. {
  11762.     return(nstored);
  11763. }
  11764.  
  11765. int
  11766. krb5_des_read(fd, buf, len, secondary)
  11767.     int fd;
  11768.     register char *buf;
  11769.     int len;
  11770.     int secondary;
  11771. {
  11772.     int nreturned = 0;
  11773.     long net_len,rd_len;
  11774.     int cc;
  11775.     krb5_error_code status;
  11776.     unsigned char c;
  11777.     krb5_data plain;
  11778.     krb5_enc_data cipher;
  11779.  
  11780.     debug(F111,"krb5_des_read","len",len);
  11781.     debug(F111,"krb5_des_read","rlog_encrypt",rlog_encrypt);
  11782.     if ( !rlog_encrypt ) {
  11783.         cc = net_read(fd, buf, len);
  11784.         debug(F111,"krb5_des_read","chars read",cc);
  11785.         if ( cc < 0 )
  11786.             netclos();
  11787.         return(cc);
  11788.     }
  11789.  
  11790.     if (nstored >= len) {
  11791.         if ( buf ) {
  11792.             memcpy(buf, store_ptr, len);        /* safe */
  11793.             store_ptr += len;
  11794.             nstored -= len;
  11795.             return(len);
  11796.         } else
  11797.             return(0);
  11798.     } else if (nstored) {
  11799.         if ( buf ) {
  11800.             memcpy(buf, store_ptr, nstored);    /* safe */
  11801.             nreturned += nstored;
  11802.             buf += nstored;
  11803.             len -= nstored;
  11804.             nstored = 0;
  11805.         }
  11806.         else
  11807.             return(0);
  11808.     }
  11809.  
  11810.     /* See the comment in v4_des_read. */
  11811.     while (1) {
  11812.         cc = net_read(fd, &c, 1);
  11813.         /* we should check for non-blocking here, but we'd have
  11814.         to make it save partial reads as well. */
  11815.         if (cc <= 0) {
  11816.             return cc; /* read error */
  11817.         }
  11818.         if (cc == 1) {
  11819.             if (c == 0 || !do_lencheck) 
  11820.                 break;
  11821.         }
  11822.     }
  11823.  
  11824.     rd_len = c;
  11825.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11826.     rd_len = (rd_len << 8) | 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.  
  11832.     if (status = krb5_c_encrypt_length(k5_context, 
  11833.                                     k5_session_key->enctype,
  11834.                                     use_ivecs ? rd_len + 4 : rd_len,
  11835.                                     &net_len)) {
  11836.         errno = status;
  11837.         return(-1);
  11838.     }
  11839.  
  11840.     if ((net_len <= 0) || (net_len > sizeof(des_inbuf))) {
  11841.         /* preposterous length; assume out-of-sync; only
  11842.            recourse is to close connection, so return 0 */
  11843.         printf("Read size problem.\r\n");
  11844.         return(0);
  11845.     }
  11846.     if ((cc = net_read(fd, desinbuf.data, net_len)) != net_len )
  11847.     {
  11848.         /* pipe must have closed, return 0 */
  11849.         printf( "Read error: length received %d != expected %d.\r\n",
  11850.                 cc,
  11851.                 net_len
  11852.                 );
  11853.         return(cc);
  11854.     }
  11855.  
  11856.  
  11857.     /* decrypt info */
  11858.     cipher.enctype = ENCTYPE_UNKNOWN;
  11859.     cipher.ciphertext.length = net_len;
  11860.     cipher.ciphertext.data = desinbuf.data;
  11861.     plain.length = sizeof(storage);
  11862.     plain.data = storage;
  11863.  
  11864.     if ( status = krb5_c_decrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11865.                                  use_ivecs ? encivec_i + secondary : 0,
  11866.                                   &cipher,&plain) ) {
  11867.         /* probably out of sync */
  11868.         printf("Cannot decrypt data from network: %s\r\n",
  11869.                  error_message(status));
  11870.         errno = EIO;
  11871.         return(-1);
  11872.     }
  11873.     
  11874.     store_ptr = storage;
  11875.     nstored = rd_len;
  11876.  
  11877.     if ( use_ivecs ) {
  11878.         int rd_len2;
  11879.         rd_len2 = storage[0] & 0xff;
  11880.         rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
  11881.         rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
  11882.         rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
  11883.         if (rd_len2 != rd_len) {
  11884.             /* cleartext length trashed? */
  11885.             errno = EIO;
  11886.             return -1;
  11887.         }
  11888.         store_ptr += 4;
  11889.     }
  11890.  
  11891.     if ( !buf )
  11892.         return(0);
  11893.  
  11894. #ifdef RLOGCODE                         /* blah */
  11895.     if (rlog_inband && (ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN))
  11896.     {
  11897.         int i, left, n;
  11898.  
  11899.         for (i = 0; i < nstored; i++) {
  11900.             if (store_ptr[i] == '\377' &&
  11901.                 store_ptr[i+1] == '\377') {
  11902.                 left = nstored - i;
  11903.                 n = rlog_ctrl(&store_ptr[i], left);
  11904.                 if (n < 0) {
  11905.                     left -= (-n);
  11906.                     nstored = left;
  11907.                     /* flush before, and (-n) bytes */
  11908.                     if (left > 0)
  11909.                         memmove(store_ptr, &store_ptr[i-n], left);
  11910.                 } else if (n) {
  11911.                     left -= n;
  11912.                     nstored -= n;
  11913.                     if (left > 0)
  11914.                         memmove(store_ptr, &store_ptr[n], left);
  11915.                 }
  11916.             }
  11917.         }
  11918.     }
  11919. #endif /* RLOGCODE */
  11920.  
  11921.     if (nstored > len) {
  11922.         memcpy(buf, store_ptr, len);            /* safe */
  11923.         nreturned += len;
  11924.         store_ptr += len;
  11925.         nstored -= len;
  11926.     } else {
  11927.         memcpy(buf, store_ptr, nstored);        /* safe */
  11928.         nreturned += nstored;
  11929.         nstored = 0;
  11930.     }
  11931.     return(nreturned);
  11932. }
  11933.  
  11934. int
  11935. krb5_des_write(fd, buf, len, secondary)
  11936.     int fd;
  11937.     char *buf;
  11938.     int len;
  11939.     int secondary;
  11940. {
  11941.     char tmpbuf[2*RLOG_BUFSIZ+8];
  11942.     unsigned char *len_buf = (unsigned char *) tmpbuf;
  11943.     krb5_error_code status;
  11944.     krb5_data plain;
  11945.     krb5_enc_data cipher;
  11946.  
  11947.     debug(F111,"krb5_des_write","rlog_encrypt",rlog_encrypt);
  11948.     if ( !rlog_encrypt ) {
  11949.         int cc = net_write(fd, buf, len);
  11950.         debug(F111,"net_write","chars written",cc);
  11951.         return(cc != len ? -1 : len);
  11952.     }
  11953.  
  11954.     if (use_ivecs) {
  11955.         unsigned char *lenbuf2 = (unsigned char *) tmpbuf;
  11956.         if (len + 4 > sizeof(tmpbuf))
  11957.             abort ();
  11958.         lenbuf2[0] = (len & 0xff000000) >> 24;
  11959.         lenbuf2[1] = (len & 0xff0000) >> 16;
  11960.         lenbuf2[2] = (len & 0xff00) >> 8;
  11961.         lenbuf2[3] = (len & 0xff);
  11962.         memcpy (tmpbuf + 4, buf, len);
  11963.  
  11964.         plain.data = tmpbuf;
  11965.         plain.length = len + 4;
  11966.     } else {
  11967.         plain.data = buf;
  11968.         plain.length = len;
  11969.     }
  11970.  
  11971.     cipher.ciphertext.length = sizeof(des_outpkt)-4;
  11972.     cipher.ciphertext.data = desoutbuf.data;
  11973.  
  11974.     if ( status = krb5_c_encrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11975.                          use_ivecs ? encivec_o + secondary : 0,
  11976.                          &plain, &cipher)) {
  11977.         printf("Write encrypt problem: %s.\r\n",
  11978.                  error_message(status));
  11979.         errno = EIO;
  11980.         return(-1);
  11981.     }
  11982.     desoutbuf.length = cipher.ciphertext.length;
  11983.  
  11984.     len_buf = (unsigned char *) des_outpkt;
  11985.     len_buf[0] = (len & 0xff000000) >> 24;
  11986.     len_buf[1] = (len & 0xff0000) >> 16;
  11987.     len_buf[2] = (len & 0xff00) >> 8;
  11988.     len_buf[3] = (len & 0xff);
  11989.  
  11990.     if (net_write(fd, des_outpkt,desoutbuf.length+4)
  11991.          != desoutbuf.length+4){
  11992.         printf("Could not write out all data\r\n");
  11993.         return(-1);
  11994.     }
  11995.     else return(len);
  11996. }
  11997. #endif /* KRB5 */
  11998.  
  11999. #ifdef KRB4
  12000. /*
  12001.  * Note that the encrypted rlogin packets take the form of a four-byte
  12002.  * length followed by encrypted data.  On writing the data out, a significant
  12003.  * performance penalty is suffered (at least one RTT per character, two if we
  12004.  * are waiting for a shell to echo) by writing the data separately from the
  12005.  * length.  So, unlike the input buffer, which just contains the output
  12006.  * data, the output buffer represents the entire packet.
  12007.  */
  12008.  
  12009. int
  12010. krb4_des_avail(fd)
  12011.     int fd;
  12012. {
  12013.     return(nstored);
  12014. }
  12015.  
  12016. int
  12017. krb4_des_read(fd, buf, len)
  12018. int fd;
  12019. register char *buf;
  12020. int len;
  12021. {
  12022.     int nreturned = 0;
  12023.     unsigned long net_len, rd_len;
  12024.     int cc;
  12025.     unsigned char c;
  12026.     int gotzero = 0;
  12027.  
  12028.     debug(F111,"krb4_des_read","rlog_encrypt",rlog_encrypt);
  12029.     debug(F111,"krb4_des_read","len",len);
  12030.     if ( !rlog_encrypt ) {
  12031.         cc = net_read(fd, buf, len);
  12032.         debug(F111,"krb4_des_read","chars read",cc);
  12033.         if ( cc < 0 )
  12034.             netclos();
  12035.         return(cc);
  12036.     }
  12037.  
  12038.     if (nstored >= len) {
  12039.         if ( buf ) {
  12040.             debug(F111,"krb4_des_read (nstored >= len)","nstored",nstored);
  12041.             memcpy(buf, store_ptr, len);        /* safe */
  12042.             store_ptr += len;
  12043.             nstored -= len;
  12044.             return(len);
  12045.         } else
  12046.             return(0);
  12047.     } else if (nstored) {
  12048.         if ( buf ) {
  12049.             debug(F111,"krb4_des_read (nstored)","nstored",nstored);
  12050.             memcpy(buf, store_ptr, nstored);    /* safe */
  12051.             nreturned += nstored;
  12052.             buf += nstored;
  12053.             len -= nstored;
  12054.             nstored = 0;
  12055.         } else
  12056.             return(0);
  12057.     }
  12058.  
  12059.     /* We're fetching the length which is MSB first, and the MSB
  12060.     has to be zero unless the client is sending more than 2^24
  12061.     (16M) bytes in a single write (which is why this code is in
  12062.     rlogin but not rcp or rsh.) The only reasons we'd get something
  12063.     other than zero are:
  12064.     -- corruption of the tcp stream (which will show up when
  12065.     everything else is out of sync too)
  12066.     -- un-caught Berkeley-style "pseudo out-of-band data" which
  12067.     happens any time the user hits ^C twice.
  12068.     The latter is *very* common, as shown by an 'rlogin -x -d'
  12069.     using the CNS V4 rlogin.         Mark EIchin 1/95
  12070.     */
  12071.     debug(F110,"krb4_des_read",
  12072.           "about to call net_read() this will block",
  12073.           0
  12074.           );
  12075.     do {
  12076.         cc = net_read(fd, &c, 1);
  12077.         debug(F111,"net_read","chars read",cc);
  12078.         if (cc <= 0) {
  12079.             netclos();
  12080.             return(-1);
  12081.         }
  12082.         if (cc != 1) return 0; /* read error */
  12083.         if (cc == 1) {
  12084.             if (c == 0) gotzero = 1;
  12085.         }
  12086.     } while (!gotzero);
  12087.  
  12088.     debug(F110,"krb4_des_read","gotzero",0);
  12089.     cc = net_read(fd, &c, 1);
  12090.     debug(F111,"net_read","chars read",cc);
  12091.     if (cc < 0) {
  12092.         netclos();
  12093.         return(-1);
  12094.     } else if ( cc != 1 )
  12095.         return(0);
  12096.     net_len = c;
  12097.     cc = net_read(fd, &c, 1);
  12098.     debug(F111,"net_read","chars read",cc);
  12099.     if (cc < 0) {
  12100.         netclos();
  12101.         return(-1);
  12102.     } else if ( cc != 1 )
  12103.         return(0);
  12104.     net_len = (net_len << 8) | c;
  12105.     debug(F111,"net_read","chars read",cc);
  12106.     cc = net_read(fd, &c, 1);
  12107.     if (cc < 0) {
  12108.         netclos();
  12109.         return(-1);
  12110.     } else if ( cc != 1 )
  12111.         return(0);
  12112.     net_len = (net_len << 8) | c;
  12113.     debug(F111,"krb4_des_read","net_len",net_len);
  12114.  
  12115.     /* Note: net_len is unsigned */
  12116.     if (net_len > sizeof(des_inbuf)) {
  12117.         /* XXX preposterous length, probably out of sync.
  12118.         act as if pipe closed */
  12119.         return(0);
  12120.     }
  12121.     /* the writer tells us how much real data we are getting, but
  12122.     we need to read the pad bytes (8-byte boundary) */
  12123. #ifndef roundup
  12124. #define roundup(x,y) ((((x)+(y)-1)/(y))*(y))
  12125. #endif /* roundup */
  12126.     rd_len = roundup(net_len, 8);
  12127.     debug(F111,"krb4_des_read","rd_len",rd_len);
  12128.     cc = net_read(fd, des_inbuf, rd_len);
  12129.     debug(F111,"net_read","chars read",cc);
  12130.     if (cc < 0) {
  12131.         netclos();
  12132.         return(-1);
  12133.     } else if ( cc != rd_len )
  12134.         return(0);
  12135.  
  12136.     hexdump("krb4_des_read des_inbuf",des_inbuf,8);
  12137. #ifdef CK_ENCRYPTION
  12138. #ifdef KRB524
  12139.     (void) des_pcbc_encrypt(des_inbuf,
  12140.                              storage,
  12141.                              (net_len < 8) ? 8 : net_len,
  12142.                              k4_sched,
  12143.                              cred.session,
  12144.                              DECRYPT);
  12145. #else /* KRB524 */
  12146.     (void) des_pcbc_encrypt((Block *)des_inbuf,
  12147.                              (Block *)storage,
  12148.                              (net_len < 8) ? 8 : net_len,
  12149.                              k4_sched,
  12150.                              &cred.session,
  12151.                              DECRYPT);
  12152. #endif /* KRB524 */
  12153. #endif /* ENCRYPTION */
  12154.     hexdump("krb4_des_read storage",storage,8);
  12155.  
  12156.     /*
  12157.     * when the cleartext block is < 8 bytes, it is "right-justified"
  12158.     * in the block, so we need to adjust the pointer to the data
  12159.     */
  12160.     if (net_len < 8)
  12161.         store_ptr = storage + 8 - net_len;
  12162.     else
  12163.         store_ptr = storage;
  12164.     nstored = net_len;
  12165.  
  12166.     if ( !buf )
  12167.         return(0);
  12168.  
  12169.     if (nstored > len) {
  12170.         memcpy(buf, store_ptr, len);            /* safe */
  12171.         nreturned += len;
  12172.         store_ptr += len;
  12173.         nstored -= len;
  12174.     } else {
  12175.         memcpy(buf, store_ptr, nstored);        /* safe */
  12176.         nreturned += nstored;
  12177.         nstored = 0;
  12178.     }
  12179.  
  12180.     debug(F111,"net_read","nreturned",nreturned);
  12181.     return(nreturned);
  12182. }
  12183.  
  12184. int
  12185. krb4_des_write(fd, buf, len)
  12186. int fd;
  12187. char *buf;
  12188. int len;
  12189. {
  12190.     static char garbage_buf[8];
  12191.     unsigned char *len_buf = (unsigned char *) des_outpkt;
  12192.     int cc;
  12193.  
  12194.     debug(F111,"krb4_des_write","rlog_encrypt",rlog_encrypt);
  12195.     if ( !rlog_encrypt ) {
  12196.         cc = net_write(fd, buf, len);
  12197.         debug(F111,"net_write","chars written",cc);
  12198.         return(cc);
  12199.     }
  12200.  
  12201.     /*
  12202.     * pcbc_encrypt outputs in 8-byte (64 bit) increments
  12203.     *
  12204.     * it zero-fills the cleartext to 8-byte padding,
  12205.     * so if we have cleartext of < 8 bytes, we want
  12206.     * to insert random garbage before it so that the ciphertext
  12207.     * differs for each transmission of the same cleartext.
  12208.     * if len < 8 - sizeof(long), sizeof(long) bytes of random
  12209.     * garbage should be sufficient; leave the rest as-is in the buffer.
  12210.     * if len > 8 - sizeof(long), just garbage fill the rest.
  12211.     */
  12212.     if (len < 8) {
  12213.         random_confounder(8 - len, garbage_buf);
  12214.         /* this "right-justifies" the data in the buffer */
  12215.         (void) memcpy(garbage_buf + 8 - len, buf, len); /* safe */
  12216.         hexdump("krb4_des_write garbage_buf",garbage_buf,8);
  12217.     } else
  12218.         hexdump("krb4_des_write buf",buf,8);
  12219. #ifdef CK_ENCRYPTION
  12220. #ifdef KRB524
  12221.     (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
  12222.                              des_outpkt+4,
  12223.                              (len < 8) ? 8 : len,
  12224.                              k4_sched,
  12225.                              cred.session,
  12226.                              ENCRYPT);
  12227. #else /* KRB524 */
  12228.     (void) des_pcbc_encrypt((Block *)((len < 8) ? garbage_buf : buf),
  12229.                              (Block *)(des_outpkt+4),
  12230.                              (len < 8) ? 8 : len,
  12231.                              k4_sched,
  12232.                              &cred.session,
  12233.                              ENCRYPT);
  12234. #endif /* KRB524 */
  12235. #endif /* ENCRYPTION */
  12236.     if ( len < 8 )
  12237.         hexdump("krb4_des_write (post pcbc) garbage_buf",garbage_buf,8);
  12238.     else
  12239.         hexdump("krb4_des_write (post pcbc) buf",buf,8);
  12240.     hexdump("krb4_des_write (des_outpkt+4)",(des_outpkt+4),8);
  12241.  
  12242.     /* tell the other end the real amount, but send an 8-byte padded
  12243.     packet */
  12244.     len_buf[0] = (len & 0xff000000) >> 24;
  12245.     len_buf[1] = (len & 0xff0000) >> 16;
  12246.     len_buf[2] = (len & 0xff00) >> 8;
  12247.     len_buf[3] = (len & 0xff);
  12248.     hexdump("krb4_des_write des_outpkt len",des_outpkt,12);
  12249.     cc = net_write(fd, des_outpkt, roundup(len,8)+4);
  12250.     debug(F111,"net_write","chars written",cc);
  12251.     return(len);
  12252. }
  12253. #endif /* KRB4 */
  12254. #endif /* RLOGCODE */
  12255.  
  12256. #ifdef KRB524
  12257. #ifndef OS2
  12258. /* The following functions are missing from the compatibility library */
  12259. const char *
  12260. krb_get_err_text_entry(r) int r;
  12261. {
  12262.     extern char krb_err_text[];
  12263.     return(krb_err_txt[r]);
  12264. }
  12265. #endif /* OS2 */
  12266. #endif /* KRB524 */
  12267. #endif /* CK_KERBEROS */
  12268.  
  12269. #ifdef CK_KERBEROS
  12270. #ifdef KRB5_U2U
  12271. /* Kerberos 5 User to User Client */
  12272. int
  12273. k5_user_to_user_client_auth()
  12274. {
  12275.     extern int ttyfd;
  12276.     register int retval, i;
  12277.     char **srealms;             /* realm(s) of server */
  12278.     char *princ;                /* principal in credentials cache */
  12279.     krb5_ccache cc;
  12280.     krb5_creds creds, *new_creds;
  12281.     krb5_data reply, msg, msgtext, princ_data;
  12282.     krb5_ticket * ticket = NULL;
  12283.  
  12284.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12285.     {
  12286.         com_err("uu-client", retval, "getting credentials cache");
  12287.         return(-1);
  12288.     }
  12289.  
  12290.     memset ((char*)&creds, 0, sizeof(creds));
  12291.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12292.     {
  12293.         com_err("uu-client", retval, "getting principal name");
  12294.         return(-1);
  12295.     }
  12296.  
  12297.     if (retval = krb5_get_host_realm(k5_context, szHostName, &srealms))
  12298.     {
  12299.         com_err("uu-client", retval, "getting realms for \"%s\"", szHostName);
  12300.         return(-1);
  12301.     }
  12302.  
  12303.     if (retval = krb5_build_principal_ext(k5_context, &creds.server,
  12304.                                           krb5_princ_realm(k5_context,
  12305.                                                          creds.client)->length,
  12306.                                           krb5_princ_realm(k5_context,
  12307.                                                          creds.client)->data,
  12308.                                           6, "krbtgt",
  12309.                                           krb5_princ_realm(k5_context,
  12310.                                                          creds.client)->length,
  12311.                                           krb5_princ_realm(k5_context,
  12312.                                                          creds.client)->data,
  12313.                                           0))
  12314.     {
  12315.         com_err("uu-client", retval, "setting up tgt server name");
  12316.         return(-1);
  12317.     }
  12318.  
  12319.     /* Get TGT from credentials cache */
  12320.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_CACHED, cc,
  12321.                                        &creds, &new_creds))
  12322.     {
  12323.         com_err("uu-client", retval, "getting TGT");
  12324.         return(-1);
  12325.     }
  12326.  
  12327.     if (retval = krb5_unparse_name(k5_context, creds.client, &princ)) {
  12328.         com_err("uu-client", retval, "printing principal name");
  12329.         return(-1);
  12330.     }
  12331.     i = strlen(princ) + 1;
  12332.     princ_data.data = princ;
  12333.     princ_data.length = i;              /* include null terminator for
  12334.                                            server's convenience */
  12335.     retval = krb5_write_message(k5_context,
  12336.                                 (krb5_pointer) &ttyfd, &princ_data);
  12337.     if (retval)
  12338.     {
  12339.         com_err("uu-client", retval, "sending principal name to server");
  12340.         return(-1);
  12341.     }
  12342.     krb5_free_unparsed_name(k5_context,princ);
  12343.  
  12344.     retval = krb5_write_message(k5_context,
  12345.                                 (krb5_pointer) &ttyfd, &new_creds->ticket);
  12346.     if (retval)
  12347.     {
  12348.         com_err("uu-client", retval, "sending ticket to server");
  12349.         return(-1);
  12350.     }
  12351.  
  12352.     retval = krb5_read_message(k5_context, (krb5_pointer) &ttyfd, &reply);
  12353.     if (retval)
  12354.     {
  12355.         com_err("uu-client", retval, "reading reply from server");
  12356.         return(-1);
  12357.     }
  12358.  
  12359.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12360.         com_err("uu-client", retval, "initializing the auth_context");
  12361.         return(-1);
  12362.     }
  12363.  
  12364.     if (!krb5_d_no_addresses) {
  12365.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12366.                         KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12367.                         KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12368.         com_err("uu-client", retval, "generating addrs for auth_context");
  12369.         return(-1);
  12370.       }
  12371.     }
  12372.  
  12373.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12374.                                         KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12375.         com_err("uu-client", retval, "initializing the auth_context flags");
  12376.         return(-1);
  12377.     }
  12378.  
  12379.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12380.                                               &new_creds->keyblock)) {
  12381.         com_err("uu-client", retval, "setting useruserkey for authcontext");
  12382.         return(-1);
  12383.     }
  12384.  
  12385.     /* read the ap_req to get the session key */
  12386.     retval = krb5_rd_req(k5_context, &auth_context, &reply,
  12387.                           NULL, NULL, NULL, &ticket);
  12388.     if (retval) {
  12389.         com_err("uu-client", retval, "reading AP_REQ from server");
  12390.         return(-1);
  12391.     }
  12392.  
  12393.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12394.         return(-1);
  12395.  
  12396.     if ( strcmp("Kermit implements Kerberos 5 User to User",msg.data) )
  12397.         return(-1);
  12398.     krb5_free_data_contents(k5_context,&msg);
  12399.  
  12400.     msgtext.data = "As do I! :-)";
  12401.     msgtext.length = strlen(msgtext.data)+1;
  12402.  
  12403.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12404.         return(-1);
  12405.  
  12406.     if (retval = krb5_unparse_name(k5_context,
  12407. #ifdef HEIMDAL
  12408.                                     ticket->client,
  12409. #else /* HEIMDAL */
  12410.                                     ticket->enc_part2->client,
  12411. #endif /* HEIMDAL */
  12412.                                     &princ))
  12413.         com_err("uu-client", retval, "while unparsing client name");
  12414.     else {
  12415.         ckstrncpy(szUserNameAuthenticated,princ,UIDBUFLEN);
  12416.         validUser = AUTH_VALID;
  12417.         authentication_version = AUTHTYPE_KERBEROS_V5;
  12418.         if ( !quiet )
  12419.             printf("Peer name is \"%s\"\n", princ);
  12420.         krb5_free_unparsed_name(k5_context,princ);
  12421.     }
  12422.     return 0;
  12423. }
  12424.  
  12425. /* Kerberos 5 User to User Server */
  12426.  
  12427. int
  12428. k5_user_to_user_server_auth()
  12429. {
  12430.     krb5_data pname_data, tkt_data;
  12431.     int retval;
  12432.     krb5_creds creds, *new_creds;
  12433.     krb5_ccache cc;
  12434.     krb5_data msg, msgtext;
  12435.     extern int ttyfd;
  12436.  
  12437.     if (retval = krb5_read_message(k5_context,
  12438.                                    (krb5_pointer) &ttyfd, &pname_data)) {
  12439.         com_err ("uu-server", retval, "reading pname");
  12440.         return(-1);
  12441.     }
  12442.     /* client sends it already null-terminated. */
  12443.     if ( !quiet )
  12444.         printf ("Peer name is \"%s\".\n", pname_data.data);
  12445.     ckstrncpy(szUserNameAuthenticated,pname_data.data,UIDBUFLEN);
  12446.     validUser = AUTH_VALID;
  12447.     authentication_version = AUTHTYPE_KERBEROS_V5;
  12448.  
  12449.     if (retval = krb5_read_message(k5_context,
  12450.                                    (krb5_pointer) &ttyfd, &tkt_data)) {
  12451.         com_err ("uu-server", retval, "reading ticket data");
  12452.         return(-1);
  12453.     }
  12454.  
  12455.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12456.     {
  12457.         com_err("uu-server", retval, "getting credentials cache");
  12458.         return(-1);
  12459.     }
  12460.  
  12461.     memset ((char*)&creds, 0, sizeof(creds));
  12462.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12463.     {
  12464.         com_err("uu-server", retval, "getting principal name");
  12465.         return(-1);
  12466.     }
  12467.  
  12468.     if (retval = krb5_parse_name(k5_context, pname_data.data, &creds.server))
  12469.     {
  12470.         com_err("uu-server", retval, "parsing client name");
  12471.         return(-1);
  12472.     }
  12473.     creds.second_ticket = tkt_data;
  12474.  
  12475.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_USER_USER,
  12476.                                        cc, &creds, &new_creds))
  12477.     {
  12478.         com_err("uu-server", retval, "getting user-user ticket");
  12479.         return(-1);
  12480.     }
  12481.  
  12482.     /* send a ticket/authenticator to the other side, so it can get the key
  12483.        we're using for the krb_safe below. */
  12484.  
  12485.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12486.         com_err("uu-server", retval, "making auth_context");
  12487.         return(-1);
  12488.     }
  12489.  
  12490.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12491.                                          KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12492.         com_err("uu-server", retval, "initializing the auth_context flags");
  12493.         return(-1);
  12494.     }
  12495.  
  12496.     if (!krb5_d_no_addresses) {
  12497.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12498.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12499.                                 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12500.         com_err("uu-server", retval, "generating addrs for auth_context");
  12501.         return(-1);
  12502.       }
  12503.     }
  12504.  
  12505.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12506.                                               &new_creds->keyblock)) {
  12507.         com_err("uu-server", retval, "setting useruserkey for authcontext");
  12508.         return(-1);
  12509.     }
  12510.  
  12511.     if (retval = krb5_mk_req_extended(k5_context, &auth_context,
  12512.                                       AP_OPTS_USE_SESSION_KEY |
  12513.                                       AP_OPTS_MUTUAL_REQUIRED,
  12514.                                       NULL, new_creds, &msg)) {
  12515.         com_err("uu-server", retval, "making AP_REQ");
  12516.         return(-1);
  12517.     }
  12518.     retval = krb5_write_message(k5_context, (krb5_pointer) &ttyfd, &msg);
  12519.     if (retval) {
  12520.         com_err("uu-server", retval, "writing message to client");
  12521.         return(-1);
  12522.     }
  12523.     krb5_free_data_contents(k5_context,&msg);
  12524.  
  12525.     msgtext.data = "Kermit implements Kerberos 5 User to User";
  12526.     msgtext.length = strlen(msgtext.data)+1;
  12527.  
  12528.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12529.         return(-1);
  12530.  
  12531.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12532.         return(-1);
  12533.  
  12534.     if ( strcmp("As do I! :-)",msg.data) )
  12535.         return(-1);
  12536.     krb5_free_data_contents(k5_context,&msg);
  12537.  
  12538.  
  12539.     return(0);
  12540. }
  12541.  
  12542. int
  12543. k5_u2u_read_msg(krb5_context context, int fd, krb5_data * msg)
  12544. {
  12545.     int retval;
  12546.     krb5_data reply;
  12547.  
  12548.     retval = krb5_read_message(context, (krb5_pointer) &fd, &reply);
  12549.     if (retval)
  12550.     {
  12551.         com_err("uu-client", retval, "reading reply");
  12552.         return(-1);
  12553.     }
  12554.  
  12555.     if (retval = krb5_rd_priv(context, auth_context, &reply, msg, NULL)) {
  12556.         com_err("uu-client", retval, "decoding reply");
  12557.         return(-1);
  12558.     }
  12559.     return(0);
  12560. }
  12561.  
  12562. int
  12563. k5_u2u_write_msg(krb5_context context, int fd, krb5_data * msgtext)
  12564. {
  12565.     int retval;
  12566.     krb5_data msg;
  12567.  
  12568.     if (retval = krb5_mk_priv(k5_context, auth_context, msgtext, &msg, NULL))
  12569.     {
  12570.         com_err("uu-server", retval, "encoding message");
  12571.         return(-1);
  12572.     }
  12573.  
  12574.     retval = krb5_write_message(k5_context, (krb5_pointer) &fd, &msg);
  12575.     krb5_free_data_contents(k5_context,&msg);
  12576.     if (retval)
  12577.     {
  12578.         com_err("uu-server", retval, "writing message");
  12579.         return(-1);
  12580.     }
  12581.     return(0);
  12582. }
  12583.  
  12584. int
  12585. krb5_u2u_avail(fd)
  12586.     int fd;
  12587. {
  12588.     return(nstored);
  12589. }
  12590.  
  12591. int
  12592. krb5_u2u_read(fd, buf, len)
  12593.      int fd;
  12594.      register char *buf;
  12595.      int len;
  12596. {
  12597.     int nreturned = 0;
  12598.     krb5_data msg;
  12599.  
  12600.     debug(F111,"krb5_u2u_read","len",len);
  12601.  
  12602.     if ( !buf )
  12603.         return(0);
  12604.  
  12605.     if (nstored >= len) {
  12606.         memcpy(buf, store_ptr, len);        /* safe */
  12607.         store_ptr += len;
  12608.         nstored -= len;
  12609.         return(len);
  12610.     } else if (nstored) {
  12611.         memcpy(buf, store_ptr, nstored);    /* safe */
  12612.         nreturned += nstored;
  12613.         buf += nstored;
  12614.         len -= nstored;
  12615.         nstored = 0;
  12616.     }
  12617.  
  12618.     if (k5_u2u_read_msg(k5_context, fd, &msg) < 0)
  12619.         return(-1);
  12620.  
  12621.     if ( msg.length <= len ) {
  12622.         memcpy(buf, msg.data, msg.length);
  12623.         nreturned += msg.length;
  12624.         nstored = 0;
  12625.     } else {
  12626.         memcpy(buf, msg.data, len);
  12627.         nreturned += len;
  12628.  
  12629.         if ( msg.length - len < sizeof(storage) ) {
  12630.             store_ptr = storage;
  12631.             nstored = msg.length - len;
  12632.             memcpy(storage,msg.data+len,nstored);
  12633.         } else {
  12634.             nstored = 0;
  12635.             return(-1);
  12636.         }
  12637.     }
  12638.     return(nreturned);
  12639. }
  12640.  
  12641. int
  12642. krb5_u2u_write(fd, buf, len)
  12643.      int fd;
  12644.      char *buf;
  12645.      int len;
  12646. {
  12647.     krb5_data msg;
  12648.  
  12649.     msg.length = len;
  12650.     msg.data = buf;
  12651.  
  12652.     if ( k5_u2u_write_msg(k5_context, fd, &msg) < 0 )
  12653.         return(-1);
  12654.     else
  12655.         return(len);
  12656. }
  12657.  
  12658. #endif /* KRB5_U2U */
  12659. #endif /* CK_KERBEROS */
  12660.  
  12661. #ifdef CK_FORWARD_X
  12662. /*
  12663.  
  12664. Copyright (c) 1988  X Consortium
  12665.  
  12666. Permission is hereby granted, free of charge, to any person obtaining a copy
  12667. of this software and associated documentation files (the "Software"), to deal
  12668. in the Software without restriction, including without limitation the rights
  12669. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12670. copies of the Software, and to permit persons to whom the Software is
  12671. furnished to do so, subject to the following conditions:
  12672.  
  12673. The above copyright notice and this permission notice shall be included in
  12674. all copies or substantial portions of the Software.
  12675.  
  12676. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12677. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12678. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  12679. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  12680. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  12681. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12682.  
  12683. Except as contained in this notice, the name of the X Consortium shall not be
  12684. used in advertising or otherwise to promote the sale, use or other dealings
  12685. in this Software without prior written authorization from the X Consortium.
  12686.  
  12687. */
  12688. /*  Modified for stand-alone compiling by
  12689.  *  Peter 'Luna' Runestig <peter@runestig.com>
  12690.  */
  12691.  
  12692. #include <stdlib.h>
  12693. #include <string.h>
  12694. #include <fcntl.h>
  12695. #include <sys/stat.h>
  12696. #include <time.h>
  12697. #define Time_t time_t
  12698.  
  12699. void
  12700. XauDisposeAuth (auth)
  12701. Xauth   *auth;
  12702. {
  12703.     if (auth) {
  12704.         if (auth->address) (void) free (auth->address);
  12705.         if (auth->number) (void) free (auth->number);
  12706.         if (auth->name) (void) free (auth->name);
  12707.         if (auth->data) {
  12708.             (void) bzero (auth->data, auth->data_length);
  12709.             (void) free (auth->data);
  12710.         }
  12711.         free ((char *) auth);
  12712.     }
  12713.     return;
  12714. }
  12715.  
  12716. char *
  12717. XauFileName ()
  12718. {
  12719.     char *slashDotXauthority = "/.Xauthority";
  12720.     char    *name;
  12721.     static char *buf=NULL;
  12722.     static int  bsize=0;
  12723.     int     size, namelen;
  12724.     extern char * tn_fwdx_xauthority;
  12725.  
  12726.     if ( tn_fwdx_xauthority )
  12727.         return(tn_fwdx_xauthority);
  12728.  
  12729.     if (name = getenv ("XAUTHORITY"))
  12730.         return(name);
  12731.     name = zhome();
  12732.     if ( !name )
  12733.         return(NULL);
  12734.     namelen = strlen (name);
  12735.     size = namelen + strlen(slashDotXauthority) + 1;
  12736.     if (size > bsize) {
  12737.         if (buf)
  12738.             free (buf);
  12739.         buf = malloc ((unsigned) size);
  12740.         if (!buf)
  12741.             return 0;
  12742.         bsize = size;
  12743.     }
  12744.     ckstrncpy (buf, name, bsize);
  12745.     if ( name[namelen-1] != '/'
  12746. #ifdef OS2
  12747.          && name[namelen-1] != '\\'
  12748. #endif /* OS2 */
  12749.          )
  12750.         ckstrncat (buf, slashDotXauthority, bsize);
  12751.     else
  12752.         ckstrncat (buf, &slashDotXauthority[1], bsize);
  12753.     return(buf);
  12754. }
  12755.  
  12756. static int
  12757. binaryEqual (a, b, len)
  12758. register char   *a, *b;
  12759. register int    len;
  12760. {
  12761.     while (len--)
  12762.         if (*a++ != *b++)
  12763.             return 0;
  12764.     return 1;
  12765. }
  12766.  
  12767. #ifndef R_OK
  12768. #define R_OK 04
  12769. #endif /* R_OK */
  12770.  
  12771. Xauth *
  12772. XauGetAuthByAddr (family, address_length, address,
  12773.                           number_length, number,
  12774.                           name_length, name)
  12775. unsigned int    family;
  12776. unsigned int    address_length;
  12777. const char      *address;
  12778. unsigned int    number_length;
  12779. const char      *number;
  12780. unsigned int    name_length;
  12781. const char      *name;
  12782. {
  12783.     FILE    *auth_file;
  12784.     char    *auth_name;
  12785.     Xauth   *entry;
  12786.  
  12787.     auth_name = XauFileName();
  12788.     if (!auth_name)
  12789.         return 0;
  12790.     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
  12791.         return 0;
  12792.     auth_file = fopen (auth_name, "rb");
  12793.     if (!auth_file)
  12794.         return 0;
  12795.     for (;;) {
  12796.         entry = XauReadAuth (auth_file);
  12797.         if (!entry)
  12798.             break;
  12799.         /*
  12800.          * Match when:
  12801.          *   either family or entry->family are FamilyWild or
  12802.          *    family and entry->family are the same
  12803.          *  and
  12804.          *   either address or entry->address are empty or
  12805.          *    address and entry->address are the same
  12806.          *  and
  12807.          *   either number or entry->number are empty or
  12808.          *    number and entry->number are the same
  12809.          *  and
  12810.          *   either name or entry->name are empty or
  12811.          *    name and entry->name are the same
  12812.          */
  12813.  
  12814. /*      if ((family == FamilyWild || entry->family == FamilyWild ||
  12815.              (entry->family == family &&
  12816.               address_length == entry->address_length &&
  12817.               binaryEqual (entry->address, address, (int)address_length))) &&
  12818.             (number_length == 0 || entry->number_length == 0 ||
  12819.              (number_length == entry->number_length &&
  12820.               binaryEqual (entry->number, number, (int)number_length))) &&
  12821.             (name_length == 0 || entry->name_length == 0 ||
  12822.              (entry->name_length == name_length &&
  12823.               binaryEqual (entry->name, name, (int)name_length)))) */
  12824.         /* the original matching code above doesn't seem to meet the matching
  12825.          * algorithm, it doesn't check if "address_length == 0 ||
  12826.          * entry->address_length == 0". / Luna 2000-02-09
  12827.          */
  12828.         if ((family == FamilyWild || entry->family == FamilyWild ||
  12829.               entry->family == family) &&
  12830.             (address_length == 0 || entry->address_length == 0 ||
  12831.               (address_length == entry->address_length &&
  12832.               binaryEqual (entry->address, address, (int)address_length))) &&
  12833.             (number_length == 0 || entry->number_length == 0 ||
  12834.              (number_length == entry->number_length &&
  12835.               binaryEqual (entry->number, number, (int)number_length))) &&
  12836.             (name_length == 0 || entry->name_length == 0 ||
  12837.              (entry->name_length == name_length &&
  12838.               binaryEqual (entry->name, name, (int)name_length))))
  12839.             break;
  12840.         XauDisposeAuth (entry);
  12841.     }
  12842.     (void) fclose (auth_file);
  12843.     return entry;
  12844. }
  12845.  
  12846. static int
  12847. read_short (shortp, file)
  12848. unsigned short  *shortp;
  12849. FILE            *file;
  12850. {
  12851.     unsigned char   file_short[2];
  12852.  
  12853.     if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12854.         return 0;
  12855.     *shortp = file_short[0] * 256 + file_short[1];
  12856.     return 1;
  12857. }
  12858.  
  12859. static int
  12860. read_counted_string (countp, stringp, file)
  12861. unsigned short  *countp;
  12862. char    **stringp;
  12863. FILE    *file;
  12864. {
  12865.     unsigned short  len;
  12866.     char            *data;
  12867.  
  12868.     if (read_short (&len, file) == 0)
  12869.         return 0;
  12870.     if (len == 0) {
  12871.         data = 0;
  12872.     } else {
  12873.         data = malloc ((unsigned) len);
  12874.         if (!data)
  12875.             return 0;
  12876.         if (fread (data, (int) sizeof (char), (int) len, file) != len) {
  12877.             bzero (data, len);
  12878.             free (data);
  12879.             return 0;
  12880.         }
  12881.     }
  12882.     *stringp = data;
  12883.     *countp = len;
  12884.     return 1;
  12885. }
  12886.  
  12887. Xauth *
  12888. XauReadAuth (auth_file)
  12889. FILE    *auth_file;
  12890. {
  12891.     Xauth   local;
  12892.     Xauth   *ret;
  12893.  
  12894.     if (read_short (&local.family, auth_file) == 0)
  12895.         return 0;
  12896.     if (read_counted_string (&local.address_length,
  12897.                              &local.address, auth_file) == 0)
  12898.         return 0;
  12899.     if (read_counted_string (&local.number_length,
  12900.                              &local.number, auth_file) == 0) {
  12901.         if (local.address) free (local.address);
  12902.         return 0;
  12903.     }
  12904.     if (read_counted_string (&local.name_length,
  12905.                              &local.name, auth_file) == 0) {
  12906.         if (local.address) free (local.address);
  12907.         if (local.number) free (local.number);
  12908.         return 0;
  12909.     }
  12910.     if (read_counted_string (&local.data_length,
  12911.                              &local.data, auth_file) == 0) {
  12912.         if (local.address) free (local.address);
  12913.         if (local.number) free (local.number);
  12914.         if (local.name) free (local.name);
  12915.         return 0;
  12916.     }
  12917.     ret = (Xauth *) malloc (sizeof (Xauth));
  12918.     if (!ret) {
  12919.         if (local.address) free (local.address);
  12920.         if (local.number) free (local.number);
  12921.         if (local.name) free (local.name);
  12922.         if (local.data) {
  12923.             bzero (local.data, local.data_length);
  12924.             free (local.data);
  12925.         }
  12926.         return 0;
  12927.     }
  12928.     *ret = local;
  12929.     return ret;
  12930. }
  12931.  
  12932. static int
  12933. write_short (s, file)
  12934. unsigned short  s;
  12935. FILE            *file;
  12936. {
  12937.     unsigned char   file_short[2];
  12938.  
  12939.     file_short[0] = (s & (unsigned)0xff00) >> 8;
  12940.     file_short[1] = s & 0xff;
  12941.     if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12942.         return 0;
  12943.     return 1;
  12944. }
  12945.  
  12946. static int
  12947. write_counted_string (count, string, file)
  12948. unsigned short  count;
  12949. char    *string;
  12950. FILE    *file;
  12951. {
  12952.     if (write_short (count, file) == 0)
  12953.         return 0;
  12954.     if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
  12955.         return 0;
  12956.     return 1;
  12957. }
  12958.  
  12959. int
  12960. XauWriteAuth (auth_file, auth)
  12961. FILE    *auth_file;
  12962. Xauth   *auth;
  12963. {
  12964.     if (write_short (auth->family, auth_file) == 0)
  12965.         return 0;
  12966.     if (write_counted_string (auth->address_length,
  12967.                               auth->address, auth_file) == 0)
  12968.         return 0;
  12969.     if (write_counted_string (auth->number_length,
  12970.                               auth->number, auth_file) == 0)
  12971.         return 0;
  12972.     if (write_counted_string (auth->name_length, auth->name, auth_file) == 0)
  12973.         return 0;
  12974.     if (write_counted_string (auth->data_length, auth->data, auth_file) == 0)
  12975.         return 0;
  12976.     return 1;
  12977. }
  12978.  
  12979. #ifdef KRB5
  12980. #ifdef K5_XAUTH
  12981. /*
  12982.  * functions to encode/decode Kerberos V5 principals
  12983.  * into something that can be reasonable spewed over
  12984.  * the wire
  12985.  *
  12986.  * Author: Tom Yu <tlyu@MIT.EDU>
  12987.  *
  12988.  * Still needs to be fixed up wrt signed/unsigned lengths, but we'll worry
  12989.  * about that later.
  12990.  */
  12991.  
  12992. /*
  12993.  * XauKrb5Encode
  12994.  *
  12995.  * this function encodes the principal passed to it in a format that can
  12996.  * easily be dealt with by stuffing it into an X packet.  Encoding is as
  12997.  * follows:
  12998.  *   length count of the realm name
  12999.  *   realm
  13000.  *   component count
  13001.  *   length of component
  13002.  *   actual principal component
  13003.  *   etc....
  13004.  *
  13005.  * Note that this function allocates a hunk of memory, which must be
  13006.  * freed to avoid nasty memory leak type things.  All counts are
  13007.  * byte-swapped if needed. (except for the total length returned)
  13008.  *
  13009.  * nevermind.... stuffing the encoded packet in net byte order just to
  13010.  * always do the right thing.  Don't have to frob with alignment that way.
  13011.  */
  13012. int
  13013. XauKrb5Encode(princ, outbuf)
  13014.     krb5_principal princ;       /* principal to encode */
  13015.     krb5_data *outbuf;          /* output buffer */
  13016. {
  13017.     CARD16 i, numparts, totlen = 0, plen, rlen;
  13018.     char *cp, *pdata;
  13019.  
  13020.     rlen = krb5_princ_realm(princ)->length;
  13021.     numparts = krb5_princ_size(princ);
  13022.     totlen = 2 + rlen + 2;      /* include room for realm length
  13023.                                    and component count */
  13024.     for (i = 0; i < numparts; i++)
  13025.         totlen += krb5_princ_component(princ, i)->length + 2;
  13026.     /* add 2 bytes each time for length */
  13027.     if ((outbuf->data = (char *)malloc(totlen)) == NULL)
  13028.         return -1;
  13029.     cp = outbuf->data;
  13030.     *cp++ = (char)((int)(0xff00 & rlen) >> 8);
  13031.     *cp++ = (char)(0x00ff & rlen);
  13032.     memcpy(cp, krb5_princ_realm(princ)->data, rlen);    /* safe */
  13033.     cp += rlen;
  13034.     *cp++ = (char)((int)(0xff00 & numparts) >> 8);
  13035.     *cp++ = (char)(0x00ff & numparts);
  13036.     for (i = 0; i < numparts; i++)
  13037.     {
  13038.         plen = krb5_princ_component(princ, i)->length;
  13039.         pdata = krb5_princ_component(princ, i)->data;
  13040.         *cp++ = (char)((int)(0xff00 & plen) >> 8);
  13041.         *cp++ = (char)(0x00ff & plen);
  13042.         memcpy(cp, pdata, plen);                        /* safe */
  13043.         cp += plen;
  13044.     }
  13045.     outbuf->length = totlen;
  13046.     return 0;
  13047. }
  13048.  
  13049. /*
  13050.  * XauKrb5Decode
  13051.  *
  13052.  * This function essentially reverses what XauKrb5Encode does.
  13053.  * return value: 0 if okay, -1 if malloc fails, -2 if inbuf format bad
  13054.  */
  13055. int
  13056. XauKrb5Decode(inbuf, princ)
  13057.     krb5_data inbuf;
  13058.     krb5_principal *princ;
  13059. {
  13060.     CARD16 i, numparts, plen, rlen;
  13061.     CARD8 *cp, *pdata;
  13062.  
  13063.     if (inbuf.length < 4)
  13064.     {
  13065.         return -2;
  13066.     }
  13067.     *princ = (krb5_principal)malloc(sizeof (krb5_principal_data));
  13068.     if (*princ == NULL)
  13069.         return -1;
  13070.     bzero(*princ, sizeof (krb5_principal_data));
  13071.     cp = (CARD8 *)inbuf.data;
  13072.     rlen = *cp++ << 8;
  13073.     rlen |= *cp++;
  13074.     if (inbuf.length < 4 + (int)rlen + 2)
  13075.     {
  13076.         krb5_free_principal(*princ);
  13077.         return -2;
  13078.     }
  13079.     krb5_princ_realm(*princ)->data = (char *)malloc(rlen);
  13080.     if (krb5_princ_realm(*princ)->data == NULL)
  13081.     {
  13082.         krb5_free_principal(*princ);
  13083.         return -1;
  13084.     }
  13085.     krb5_princ_realm(*princ)->length = rlen;
  13086.     memcpy(krb5_princ_realm(*princ)->data, cp, rlen);   /* safe */
  13087.     cp += rlen;
  13088.     numparts = *cp++ << 8;
  13089.     numparts |= *cp++;
  13090.     krb5_princ_name(*princ) =
  13091.         (krb5_data *)malloc(numparts * sizeof (krb5_data));
  13092.     if (krb5_princ_name(*princ) == NULL)
  13093.     {
  13094.         krb5_free_principal(*princ);
  13095.         return -1;
  13096.     }
  13097.     krb5_princ_size(*princ) = 0;
  13098.     for (i = 0; i < numparts; i++)
  13099.     {
  13100.         if (cp + 2 > (CARD8 *)inbuf.data + inbuf.length)
  13101.         {
  13102.             krb5_free_principal(*princ);
  13103.             return -2;
  13104.         }
  13105.         plen = *cp++ << 8;
  13106.         plen |= *cp++;
  13107.         if (cp + plen > (CARD8 *)inbuf.data + inbuf.length)
  13108.         {
  13109.             krb5_free_principal(*princ);
  13110.             return -2;
  13111.         }
  13112.         pdata = (CARD8 *)malloc(plen);
  13113.         if (pdata == NULL)
  13114.         {
  13115.             krb5_free_principal(*princ);
  13116.             return -1;
  13117.         }
  13118.         krb5_princ_component(*princ, i)->data = (char *)pdata;
  13119.         krb5_princ_component(*princ, i)->length = plen;
  13120.         memcpy(pdata, cp, plen);        /* safe */
  13121.         cp += plen;
  13122.         krb5_princ_size(*princ)++;
  13123.     }
  13124.     return 0;
  13125. }
  13126. #endif /* K5_XAUTH */
  13127. #endif /* KRB5 */
  13128. #endif /* CK_FORWARD_X */
  13129. #endif /* CK_AUTHENTICATION */
  13130.  
  13131. /* C K _ A U T H _ I N I T
  13132.  * Initialize the Kerberos system for a pending connection
  13133.  *   hostname - a reverse DNS lookup of the hostname when possible
  13134.  *   ipaddr   - the ip address of the host
  13135.  *   username - the name the user wants to connect under not necessarily
  13136.  *              the same as principal
  13137.  *   socket   - the socket handle (ttyfd in Kermit speak)
  13138.  *
  13139.  * Returns: 1 on success and 0 on failure
  13140.  */
  13141.  
  13142. int
  13143. #ifdef CK_ANSIC
  13144. ck_auth_init( char * hostname, char * ipaddr, char * username, int socket )
  13145. #else /* CK_ANSIC */
  13146. ck_auth_init( hostname, ipaddr, username, socket )
  13147.     char * hostname; char * ipaddr; char *username; int socket;
  13148. #endif /* CK_ANSIC */
  13149. {
  13150. #ifdef CK_AUTHENTICATION
  13151. #ifdef OS2
  13152.     if ( !ck_security_loaddll() ) {
  13153.         TELOPT_ME_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  13154.         TELOPT_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  13155.         return(0);
  13156.     }
  13157. #endif /* OS2 */
  13158. #endif /* CK_AUTHENTICAITON */
  13159. #ifdef CK_ENCRYPTION
  13160.     if ( !!ck_crypt_is_installed() ) {
  13161.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  13162.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  13163.     }
  13164. #endif /* CK_ENCRYPTION */
  13165.  
  13166.     if (!hostname) hostname = "";
  13167.     if (!ipaddr) ipaddr = "";
  13168.     if (!username) username = "";
  13169.  
  13170.     debug(F110,"ck_auth_init Username",username,0);
  13171.     debug(F110,"ck_auth_init Hostname",hostname,0);
  13172.     debug(F110,"ck_auth_init Ipaddr",ipaddr,0);
  13173.  
  13174.     ckstrncpy( szUserName, username, UIDBUFLEN );
  13175.     ckstrncpy( szHostName, hostname, UIDBUFLEN );
  13176.     ckstrncpy( szIP, ipaddr, 16 );
  13177.     szUserNameRequested[0] = '\0';
  13178.     szUserNameAuthenticated[0] = '\0';
  13179.     validUser = AUTH_REJECT;
  13180.     accept_complete = 0;
  13181.     authentication_version = AUTHTYPE_NULL;
  13182.  
  13183. #ifdef CK_AUTHENTICATION
  13184.     auth_how = 0;
  13185.     auth_crypt = 0;
  13186.     auth_fwd = 0;
  13187.     mutual_complete = 0;
  13188.     if ( sstelnet )
  13189.         str_data[3] = TELQUAL_REPLY;
  13190.     else
  13191.         str_data[3] = TELQUAL_IS;
  13192. #endif /* CK_AUTHENTICATION */
  13193.  
  13194. #ifdef CK_SRP
  13195.     srp_waitresp = 0;
  13196. #endif /* SRP */
  13197.  
  13198. #ifdef CK_KERBEROS
  13199. #ifdef KRB5
  13200.     /* free previous ret_cred  */
  13201.     if ( ret_cred ) {
  13202. #ifdef CK_ENCRYPTION
  13203. #ifdef HEIMDAL
  13204.         if ( k5_session_key == &ret_cred->session)
  13205.             k5_session_key = NULL;
  13206. #else /* HEIMDAL */
  13207.         if ( k5_session_key == &ret_cred->keyblock)
  13208.             k5_session_key = NULL;
  13209. #endif /* HEIMDAL */
  13210. #endif /* CK_ENCRYPTION */
  13211.         krb5_free_creds(k5_context, ret_cred);
  13212.         ret_cred = NULL;
  13213.     }
  13214.     if (k5_ticket) {
  13215.         krb5_free_ticket(k5_context, k5_ticket);
  13216.         k5_ticket = NULL;
  13217.     }
  13218.     /* and context */
  13219.     if ( k5_context ) {
  13220.         krb5_free_context(k5_context);
  13221.         k5_context = NULL;
  13222.     }
  13223.  
  13224.     /* create k5_context */
  13225.     krb5_init_context(&k5_context);
  13226. #ifndef MIT_CURRENT
  13227.     if (k5_context)
  13228.         krb5_init_ets(k5_context);
  13229. #endif /* MIT_CURRENT */
  13230. #ifdef KRB524_CONV
  13231.     krb524_init_ets(k5_context);
  13232. #endif /* KRB524_CONV */
  13233.     memset(&k5_auth,0,sizeof(k5_auth));
  13234.     if (auth_context) {
  13235.         krb5_auth_con_free(k5_context, auth_context);
  13236.         auth_context = 0;
  13237.     }
  13238. #ifdef CK_ENCRYPTION
  13239.     if (k5_session_key) {
  13240.         krb5_free_keyblock(k5_context, k5_session_key);
  13241.         k5_session_key = 0;
  13242.     }
  13243. #endif /* ENCRYPTION */
  13244. #ifdef TLS_VERIFY
  13245.     krb5_tls_verified = 0;
  13246. #endif /* TLS_VERIFY */
  13247. #endif /* KRB5 */
  13248.  
  13249. #ifdef KRB4
  13250. #ifdef CK_ENCRYPTION
  13251.     /* Initialize buffers used for authentication */
  13252.     memset(&k4_session_key, 0, sizeof(k4_session_key));
  13253.     memset(&k4_challenge, 0, sizeof(k4_challenge));
  13254. #endif /* CK_ENCRYPTION */
  13255. #endif /* KRB4 */
  13256.  
  13257. #ifdef RLOGCODE
  13258.     rlog_encrypt = 0;
  13259. #endif /* RLOGCODE */
  13260.     nstored = 0;
  13261.     store_ptr = storage;
  13262.     memset(storage,0,sizeof(storage));
  13263. #endif /* CK_KERBEROS */
  13264.  
  13265. #ifdef CK_ENCRYPTION
  13266.     kstream_destroy();
  13267.     if (!kstream_create_from_fd(socket, NULL))
  13268.         return(0);
  13269. #endif /* CK_ENCRYPTION */
  13270.     return(1);
  13271. }
  13272.  
  13273. void
  13274. auth_finished(result) int result; {
  13275.     extern char uidbuf[];
  13276.     extern int sstelnet;
  13277.  
  13278.     validUser = result;
  13279.     switch (result) {
  13280.     case AUTH_REJECT:           /* Rejected */
  13281.         if (sstelnet)
  13282.             uidbuf[0] = '\0';
  13283.         authentication_version = AUTHTYPE_NULL;
  13284.         break;
  13285.     case AUTH_UNKNOWN:          /* We don't know who he is, but he's okay */
  13286.         if (sstelnet)
  13287.             strcpy(uidbuf,"(unknown)");
  13288.         break;
  13289.     case AUTH_OTHER:            /* We know him, but not his name */
  13290.         if (sstelnet)
  13291.             strcpy(uidbuf,"(other)");
  13292.         break;
  13293.     case AUTH_USER:             /* We know he name */
  13294.     case AUTH_VALID:            /* We know him, and he needs no password */
  13295.         if (sstelnet)
  13296.             strcpy(uidbuf,szUserNameRequested);
  13297.         break;
  13298.     }
  13299. }
  13300.  
  13301. #ifdef MACOSX
  13302. #ifdef KRB5
  13303.  
  13304. krb5_error_code
  13305. ck_krb5_write_message(krb5_context con, krb5_pointer ptr, krb5_data *data)
  13306. {
  13307.     int fd = *((int *)ptr);
  13308.     long msglen;
  13309.  
  13310.     msglen = htonl(data->length);
  13311.     if (net_write(fd,(CHAR *)&msglen,4) != 4) {
  13312.         return(-1);
  13313.     }
  13314.     if ( data->length ) {
  13315.         if (net_write(fd,data->data,data->length) != data->length) {
  13316.             return(-1);
  13317.         }
  13318.     }
  13319.     return(0);
  13320. }
  13321.  
  13322. krb5_error_code
  13323. ck_krb5_read_message( krb5_context context,
  13324.                       krb5_pointer ptr,
  13325.                       krb5_data * data)
  13326. {
  13327.     extern int ttyfd;
  13328.     int fd = *((int *)ptr);
  13329.     long msglen;
  13330.     char *p;
  13331.     int i, rc;
  13332.  
  13333.     if (net_read(fd,&msglen,4) < 0)
  13334.         return(-1);
  13335.  
  13336.     data->length = ntohl(msglen);
  13337.     if ( data->length ) {
  13338.         data->data = malloc(data->length);
  13339.  
  13340.         i = 0;
  13341.         while ( i < data->length ) {
  13342.             if ((rc = net_read(fd,&data->data[i],(data->length - i))) < 0)
  13343.                 return(-1);
  13344.             i += rc;
  13345.         }
  13346.     }
  13347.     return(0);
  13348. }
  13349. #endif /* KRB5 */
  13350. #endif /* MACOSX */
  13351. #endif /* CK_SECURITY */
  13352.  
  13353.  
  13354.