home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku206.zip / ckuath.c < prev    next >
C/C++ Source or Header  |  2002-06-25  |  413KB  |  13,051 lines

  1. char *ckathv = "Authentication, 8.0.214, 21 June 2002";
  2. /*
  3.   C K U A T H . C  --  Authentication for C-Kermit
  4.  
  5.   Copyright (C) 1999, 2002,
  6.     Trustees of Columbia University in the City of New York.
  7.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  8.     copyright text in the ckcmai.c module for disclaimer and permissions.
  9.  
  10.   Author:  Jeffrey E Altman (jaltman@columbia.edu)
  11. */
  12. /*
  13.  * Additional copyrights included with affected code.
  14.  */
  15.  
  16. #ifdef HEIMDAL
  17. /*
  18.   Turned off User to User support
  19.   Turned off KDESTROY support
  20.   Turned off KLIST support
  21.   Turned off krb5_prompter() support
  22.   Turned off ticket validation
  23.   Turned off ticket renewal
  24.   Turned off alternative cache support in k5_get_ccache()
  25.  
  26.   Remaining link problems:
  27.  
  28.   ckuath.o: In function `ck_krb5_initTGT':
  29.   ckuath.o(.text+0x50c2): undefined reference to `krb5_string_to_deltat'
  30.   ckuath.o(.text+0x516d): undefined reference to `krb5_string_to_deltat'
  31.   ckuath.o(.text+0x51ef): undefined reference to `krb5_string_to_deltat'
  32. */
  33. #endif /* HEIMDAL */
  34.  
  35. /*
  36.  * Implements Kerberos 4/5, SRP, SSL, NTLM authentication and START_TLS
  37.  */
  38.  
  39. #include "ckcsym.h"
  40. #include "ckcdeb.h"
  41.  
  42. #define CKUATH_C
  43. #include "ckcker.h"
  44. #include "ckuusr.h"
  45. #include "ckucmd.h"                             /* For struct keytab */
  46. #include "ckcnet.h"
  47. #include "ckctel.h"
  48.  
  49. #ifdef CK_SECURITY
  50. char szUserNameRequested[UIDBUFLEN+1];    /* for incoming connections */
  51. char szUserNameAuthenticated[UIDBUFLEN+1];/* for incoming connections */
  52. char szHostName[UIDBUFLEN+1];
  53. char szUserName[UIDBUFLEN+1];
  54. static char szIP[16];
  55. static int  validUser = AUTH_REJECT;    /* User starts out invalid */
  56. int authentication_version = AUTHTYPE_NULL;
  57. int accept_complete = 0;
  58. #endif /* CK_SECURITY */
  59.  
  60. #ifdef CK_AUTHENTICATION
  61. #ifdef CK_SSL
  62. #ifdef KRB5
  63. #define TLS_VERIFY
  64. #endif /* KRB5 */
  65. #endif /* CK_SSL */
  66.  
  67. #ifdef CK_DES
  68. #ifdef CK_SSL
  69. #ifndef LIBDES
  70. #define LIBDES
  71. #endif /* LIBDES */
  72. #endif /* CK_SSL */
  73. #endif /* CK_DES */
  74.  
  75. #ifdef CRYPT_DLL
  76. #ifndef LIBDES
  77. #define LIBDES
  78. #endif /* LIBDES */
  79. #ifdef OS2
  80. #ifdef NT
  81. #include <windows.h>
  82. #else /* NT */
  83. #define INCL_DOSMODULEMGR
  84. #include <os2.h>
  85. #endif /* NT */
  86. #endif /* OS2 */
  87. #endif /* CRYPT_DLL */
  88.  
  89. #ifdef NT
  90. #define KRB5_AUTOCONF__
  91. #define NTLM
  92. #endif /* NT */
  93.  
  94. #ifdef CK_KERBEROS
  95. #define KINIT
  96. #ifndef HEIMDAL
  97. #define KLIST
  98. #define KDESTROY
  99. #endif /* HEIMDAL */
  100. #define CHECKADDRS
  101. #else /* CK_KERBEROS */
  102. #ifdef KRB4
  103. #undef KRB4
  104. #endif /* KRB4 */
  105. #ifdef KRB5
  106. #undef KRB5
  107. #endif /* KRB5 */
  108. #ifdef KRB524
  109. #undef KRB524
  110. #endif /* KRB524 */
  111. #endif /* CK_KERBEROS */
  112.  
  113. #include <stdlib.h>
  114. #include <string.h>
  115. #include <stdio.h>
  116. #include <time.h>
  117. #include <fcntl.h>
  118. #ifndef FREEBSD4
  119. #ifndef OpenBSD
  120. #include <malloc.h>
  121. #endif /* OpenBSD */
  122. #endif /* FREEBSD4 */
  123. #ifdef OS2
  124. #include <io.h>
  125. #endif /* OS2 */
  126.  
  127. #ifdef KRB5
  128. #ifdef HEIMDAL
  129. #ifdef printf
  130. #define saveprintf printf
  131. #undef printf
  132. #endif /* printf */
  133. #include "krb5.h"
  134. #include "com_err.h"
  135. #ifdef saveprintf
  136. #define printf saveprintf
  137. #endif /* saveprintf */
  138. #else /* HEIMDAL */
  139. #include "krb5.h"
  140. #include "profile.h"
  141. #include "com_err.h"
  142. #ifdef KRB5_GET_INIT_CREDS_OPT_TKT_LIFE
  143. #define KRB5_HAVE_GET_INIT_CREDS
  144. #else
  145. #define krb5_free_unparsed_name(con,val) krb5_xfree((char *)(val))
  146. #endif
  147. #ifndef KRB5_HAVE_GET_INIT_CREDS
  148. #define krb5_free_data_contents(c,v) krb5_xfree((char *)(v)->data)
  149. #endif
  150. #endif /* HEIMDAL */
  151. #ifdef HAVE_PWD_H
  152. #include <pwd.h>
  153. #endif
  154. #endif /* KRB5 */
  155.  
  156. #ifdef KRB4
  157. #define  des_cblock Block
  158. #define  const_des_cblock const Block
  159. #define  des_key_schedule Schedule
  160. #ifdef KRB524
  161. #ifdef NT
  162. #define _WINDOWS
  163. #endif /* NT */
  164. #include "kerberosIV/krb.h"
  165. #ifndef OS2
  166. _PROTOTYP(const char * krb_get_err_text_entry, (int));
  167. #endif /* OS2 */
  168. #else /* KRB524 */
  169. #ifdef SOLARIS
  170. #ifndef sun
  171. /* for some reason the Makefile entries for the Solaris systems have -Usun */
  172. #define sun
  173. #endif /* sun */
  174. #endif /* SOLARIS */
  175. #include "krb.h"
  176. #define krb_get_err_text_entry krb_get_err_text
  177. #endif /* KRB524 */
  178. #else /* KRB4 */
  179. #ifdef CK_SSL
  180. #define  des_cblock Block
  181. #ifdef COMMENT
  182. #define  const_des_cblock const Block
  183. #endif /* COMMENT */
  184. #define  des_key_schedule Schedule
  185. #endif /* CK_SSL */
  186. #endif /* KRB4 */
  187.  
  188. #include "ckuath.h"
  189. #ifdef CK_KERBEROS
  190. #ifndef KRB5
  191. #define NOBLOCKDEF
  192. #else /* KRB5 */
  193. #ifdef KRB524
  194. #define NOBLOCKDEF
  195. #endif /* KRB524 */
  196. #endif /* KRB5 */
  197. #endif /* CK_KERBEROS */
  198. #include "ckuat2.h"
  199.  
  200. #ifdef CK_SSL
  201. #ifdef LIBDES
  202. #ifdef OPENSSL_097
  203. #define OPENSSL_ENABLE_OLD_DES_SUPPORT
  204. #include <openssl/des.h>
  205. #endif /* OPENSSL_097 */
  206. #ifndef HEADER_DES_H
  207. #define HEADER_DES_H
  208. #endif /* HEADER_DES_H */
  209. #endif /* LIBDES */
  210. #include "ck_ssl.h"
  211. extern int ssl_finished_messages;
  212. #endif /* SSL */
  213.  
  214. #define PWD_SZ 128
  215.  
  216. #ifndef LIBDES
  217. #ifdef UNIX
  218. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  219. #endif /* UNIX */
  220. #else /* LIBDES */
  221. #define des_fixup_key_parity des_set_odd_parity
  222. #endif /* LIBDES */
  223.  
  224. #ifdef OS2
  225. #define MAP_DES
  226. #ifdef KRB4
  227. #define MAP_KRB4
  228. #endif /* KRB4 */
  229. #ifdef SRPDLL
  230. #define MAP_SRP
  231. #endif /* SRPDLL */
  232. #ifdef KRB5
  233. #define MAP_KRB5
  234. #endif /* KRB5 */
  235. #ifdef CRYPT_DLL
  236. #define MAP_CRYPT
  237. #endif /* CRYPT_DLL */
  238. #define MAP_NTLM
  239. #include "ckoath.h"
  240. #include "ckosyn.h"
  241. #endif /* OS2 */
  242.  
  243. /*
  244.  * Globals
  245.  */
  246. int auth_type_user[AUTHTYPLSTSZ] = {AUTHTYPE_AUTO, AUTHTYPE_NULL};
  247. int auth_how=0;
  248. int auth_crypt=0;
  249. int auth_fwd=0;
  250.  
  251. /* These are state completion variables */
  252. static int mutual_complete = 0;
  253.  
  254. #ifdef KRB4
  255. #ifdef OS2
  256. static LEASH_CREDENTIALS cred;
  257. #else /* OS2 */
  258. static CREDENTIALS cred;
  259. #endif /* OS2 */
  260. static KTEXT_ST k4_auth;
  261. static char     k4_name[ANAME_SZ];
  262. static AUTH_DAT k4_adat  = { 0 };
  263. static MSG_DAT  k4_msg_data;
  264. #ifdef CK_ENCRYPTION
  265. static Block    k4_session_key     = { 0 };
  266. static Schedule k4_sched;
  267. static Block    k4_challenge       = { 0 };
  268. #ifdef MIT_CURRENT
  269. static krb5_keyblock k4_krbkey;
  270. #endif /* MIT_CURRENT */
  271. #endif /* ENCRYPTION */
  272. #define KRB4_SERVICE_NAME    "rcmd"
  273.  
  274. _PROTOTYP(static int k4_auth_send,(VOID));
  275. _PROTOTYP(static int k4_auth_reply,(unsigned char *, int));
  276. _PROTOTYP(static int k4_auth_is,(unsigned char *, int));
  277. #endif /* KRB4 */
  278.  
  279. #ifdef KRB5
  280. static krb5_data          k5_auth;
  281. static krb5_auth_context  auth_context;
  282. static krb5_keyblock     *k5_session_key = NULL;
  283. static krb5_ticket       *k5_ticket = NULL;
  284. #ifndef KRB5_SERVICE_NAME
  285. #define KRB5_SERVICE_NAME    "host"
  286. #endif
  287.  
  288. _PROTOTYP(static int k5_auth_send,(int,int,int));
  289. _PROTOTYP(static int k5_auth_reply,(int, unsigned char *, int));
  290. _PROTOTYP(static int k5_auth_is,(int,unsigned char *, int));
  291. _PROTOTYP(static int SendK5AuthSB,(int, void *, int));
  292. #ifdef TLS_VERIFY
  293. static int krb5_tls_verified = 0;
  294. #endif /* TLS_VERIFY */
  295. #endif /* KRB5 */
  296.  
  297. #ifdef GSSAPI_KRB5
  298. #include <gssapi/gssapi.h>
  299. #include <gssapi/gssapi_generic.h>
  300. #include <gssapi/gssapi_krb5.h>
  301.  
  302. static gss_ctx_id_t gcontext;
  303. #define GSS_BUFSIZ 4096
  304. static gss_buffer_desc gss_send_tok, gss_recv_tok, *gss_token_ptr;
  305. static char gss_stbuf[GSS_BUFSIZ];
  306. static gss_name_t gss_target_name;
  307. static struct gss_channel_bindings_struct gss_chan;
  308.  
  309. _PROTOTYP(static int gssk5_auth_send,(int,int,int));
  310. _PROTOTYP(static int gssk5_auth_reply,(int, unsigned char *, int));
  311. _PROTOTYP(static int gssk5_auth_is,(int,unsigned char *, int));
  312. _PROTOTYP(static int SendGSSK5AuthSB,(int, void *, int));
  313. #endif /* GSSAPI_KRB5 */
  314.  
  315. #ifdef CK_SRP
  316. #ifdef PRE_SRP_1_7_3
  317. _PROTOTYP(static int srp_reply,(int, unsigned char *, int));
  318. _PROTOTYP(static int srp_is,(int, unsigned char *, int));
  319. #else /* PRE_SRP_1_7_3 */
  320. _PROTOTYP(static int new_srp_reply,(int, unsigned char *, int));
  321. _PROTOTYP(static int new_srp_is,(int, unsigned char *, int));
  322. #endif /* PRE_SRP_1_7_3 */
  323. #endif /* SRP */
  324.  
  325. _PROTOTYP(void auth_finished, (int));
  326.  
  327. #ifdef CK_ENCRYPTION
  328. int encrypt_flag = 1;
  329. #endif
  330. #ifdef FORWARD
  331. int forward_flag = 0;              /* forward tickets? */
  332. int forwardable_flag = 1;          /* get forwardable tickets to forward? */
  333. int forwarded_tickets = 0;         /* were tickets forwarded? */
  334. #endif
  335.  
  336. static unsigned char str_data[4096] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
  337.                                         AUTHTYPE_KERBEROS_V5, };
  338. #define AUTHTMPBL 2048
  339. static char strTmp[AUTHTMPBL+1];
  340. static char szLocalHostName[UIDBUFLEN+1];
  341. static kstream g_kstream=NULL;
  342.  
  343. #ifdef KRB5
  344. krb5_context k5_context=NULL;
  345. static krb5_creds * ret_cred=NULL;
  346. static krb5_context telnet_context=NULL;
  347. static char * telnet_krb5_realm = NULL;
  348. static krb5_principal fwd_server = NULL;
  349. #endif /* KRB5 */
  350.  
  351. #ifdef CK_SRP
  352. #ifdef PRE_SRP_1_4_4
  353. #ifndef PRE_SRP_1_4_5
  354. #define PRE_SRP_1_4_5
  355. #endif /* PRE_SRP_1_4_5 */
  356. #endif /* PRE_SRP_1_4_5 */
  357. #ifdef PRE_SRP_1_4_5
  358. #ifndef PRE_SRP_1_7_3
  359. #define PRE_SRP_1_7_3
  360. #endif /* PRE_SRP_1_7_3 */
  361. #endif /* PRE_SRP_1_4_5 */
  362. #include <t_pwd.h>
  363. #include <t_client.h>
  364. #include <t_server.h>
  365. static struct t_server * ts = NULL;
  366. static struct t_client * tc = NULL;
  367. #ifdef PRE_SRP_1_4_4
  368. static struct t_pw * tpw = NULL;
  369. static struct t_conf * tconf = NULL;
  370. #endif /* PRE_SRP_1_4_4 */
  371. #ifndef PRE_SRP_1_7_3
  372. #ifndef STDC_HEADERS
  373. #define STDC_HEADERS 1
  374. #endif /* STDC_HEADERS */
  375. #include <srp.h>
  376. static SRP * s_srp = NULL;
  377. static cstr * s_key = NULL;
  378. static SRP * c_srp = NULL;
  379. static cstr * c_key = NULL;
  380. #endif /* PRE_SRP_1_7_3 */
  381. static int srp_waitresp = 0;    /* Flag to indicate readiness for response */
  382. static char srp_passwd[PWD_SZ];
  383. #endif /* CK_SRP */
  384.  
  385. #ifdef CK_KERBEROS
  386. #ifdef RLOGCODE
  387. #define OPTS_FORWARD_CREDS           0x00000020
  388. #define OPTS_FORWARDABLE_CREDS       0x00000010
  389. #define KCMD_KEYUSAGE                1026
  390.  
  391. #define RLOG_BUFSIZ 5120
  392. static int rlog_encrypt = 0;
  393. char des_inbuf[2*RLOG_BUFSIZ];       /* needs to be > largest read size */
  394. char des_outpkt[2*RLOG_BUFSIZ+4];    /* needs to be > largest write size */
  395. #ifdef KRB5
  396. krb5_data desinbuf,desoutbuf;
  397. krb5_encrypt_block eblock;             /* eblock for encrypt/decrypt */
  398. static krb5_data encivec_i[2], encivec_o[2];
  399.  
  400. enum krb5_kcmd_proto {
  401.   /* Old protocol: DES encryption only.  No subkeys.  No protection
  402.      for cleartext length.  No ivec supplied.  OOB hacks used for
  403.      rlogin.  Checksum may be omitted at connection startup.  */
  404.   KCMD_OLD_PROTOCOL = 1,
  405.   /* New protocol: Any encryption scheme.  Client-generated subkey
  406.      required.  Prepend cleartext-length to cleartext data (but don't
  407.      include it in count).  Starting ivec defined, chained.  In-band
  408.      signalling.  Checksum required.  */
  409.   KCMD_NEW_PROTOCOL,
  410.   /* Hack: Get credentials, and use the old protocol iff the session
  411.      key type is single-DES.  */
  412.   KCMD_PROTOCOL_COMPAT_HACK,
  413.   KCMD_UNKNOWN_PROTOCOL
  414. };
  415. enum krb5_kcmd_proto krb5_rlog_ver = KCMD_PROTOCOL_COMPAT_HACK;
  416. #endif /* KRB5 */
  417. #endif /* RLOGCODE */
  418. static char storage[65536];            /* storage for the decryption */
  419. static int nstored = 0;
  420. static char *store_ptr = storage;
  421.  
  422. extern char * krb5_d_principal;         /* Default principal */
  423. extern char * krb5_d_instance;          /* Default instance */
  424. extern char * krb5_d_realm;             /* Default realm */
  425. extern char * krb5_d_cc;                /* Default credentials cache */
  426. extern char * krb5_d_srv;               /* Default service name */
  427. extern int    krb5_d_lifetime;          /* Default lifetime */
  428. extern int    krb5_d_forwardable;
  429. extern int    krb5_d_proxiable;
  430. extern int    krb5_d_renewable;
  431. extern int    krb5_autoget;
  432. extern int    krb5_checkaddrs;
  433. extern int    krb5_d_getk4;
  434. extern int    krb5_d_no_addresses;
  435. extern char * k5_keytab;
  436.  
  437. extern int    krb5_errno;
  438. extern char * krb5_errmsg;
  439.  
  440. extern char * krb4_d_principal;         /* Default principal */
  441. extern char * krb4_d_realm;             /* Default realm */
  442. extern char * krb4_d_srv;               /* Default service name */
  443. extern int    krb4_d_lifetime;          /* Default lifetime */
  444. extern int    krb4_d_preauth;
  445. extern char * krb4_d_instance;
  446. extern int    krb4_autoget;
  447. extern int    krb4_checkaddrs;
  448. extern char * k4_keytab;
  449.  
  450. extern int    krb4_errno;
  451. extern char * krb4_errmsg;
  452. #endif /* CK_KERBEROS */
  453.  
  454. extern char tn_msg[], hexbuf[];         /* from ckcnet.c */
  455. extern char pwbuf[];
  456. extern int  pwflg, pwcrypt;
  457. extern int deblog, debses, tn_deb;
  458. extern int sstelnet, inserver;
  459. #ifdef CK_LOGIN
  460. extern int ckxanon;
  461. #endif /* CK_LOGIN */
  462. extern int tn_auth_how;
  463. extern int tn_auth_enc;
  464. #ifdef CK_ENCRYPTION
  465. extern int cx_type;
  466. #endif /* CK_ENCRYPTION */
  467. extern int quiet, ttyfd, ttnproto;
  468.  
  469. int
  470. ck_gssapi_is_installed()
  471. {
  472. #ifdef KRB5
  473. #ifdef OS2
  474.     return(hGSSAPI != NULL);
  475. #else /* OS2 */
  476.     return(1);
  477. #endif /* OS2 */
  478. #else /* KRB5 */
  479.     return(0);
  480. #endif /* KRB5 */
  481. }
  482.  
  483. int
  484. ck_krb5_is_installed()
  485. {
  486. #ifdef KRB5
  487. #ifdef OS2
  488.     return(hKRB5_32 != NULL);
  489. #else /* OS2 */
  490.     return(1);
  491. #endif /* OS2 */
  492. #else /* KRB5 */
  493.     return(0);
  494. #endif /* KRB5 */
  495. }
  496.  
  497.  
  498. int
  499. ck_krb5_is_installed_as_server()
  500. {
  501. #ifdef KRB5
  502. #ifdef HEIMDAL
  503.     krb5_error_code ret;
  504.     krb5_keytab kt;
  505.     krb5_kt_cursor cursor;
  506.  
  507.     ret = krb5_kt_default(k5_context, &kt);
  508.     if ( ret ) {
  509.         krb5_kt_close(k5_context, kt);
  510.         return(0);
  511.     } else {
  512.         krb5_kt_end_seq_get(k5_context, kt, &cursor);
  513.         krb5_kt_close(k5_context, kt);
  514.         return(1);
  515.     }
  516. #else /* HEIMDAL */
  517. #ifndef COMMENT
  518.     char ktname[CKMAXPATH]="";
  519.  
  520.     if ( k5_keytab ) {
  521.         ckstrncpy(ktname,k5_keytab,CKMAXPATH);
  522.     } else {
  523.         krb5_error_code code;
  524.  
  525.         if ( k5_context == NULL)
  526.             if (krb5_init_context(&k5_context))
  527.                 return(0);
  528.  
  529.         code = krb5_kt_default_name(k5_context,ktname,CKMAXPATH);
  530.         debug(F101,"krb5_kt_default_name","",code);
  531.         if ( code ) {
  532.             /* We can't check the existence of the file since we can't   */
  533.             /* determine the file name.  So we return TRUE and let       */
  534.             /* Krb5 be offered to the user even though it may fail later */
  535.             return(1);
  536.         }
  537.     }
  538.  
  539.     if ( !strncmp("FILE:",ktname,5) ) {
  540.         if ( zchki(&ktname[5]) > 0 )
  541.             return(1);
  542.         else
  543.             return(0);
  544.     } else {
  545.         if (ktname[0])
  546.             return(1);
  547.         else
  548.             return(0);
  549.     }
  550. #else /* COMMENT */
  551.     krb5_error_code             krb5rc = KRB5KRB_ERR_GENERIC;
  552.     krb5_context                krb5context = NULL;
  553.     krb5_ccache                 krb5ccdef = NULL;
  554.     krb5_creds                  krb5creds, *krb5credsp = NULL;
  555.     int                         rc = 0;
  556.  
  557.     if ( !ck_krb5_is_installed() )
  558.         return(0);
  559.  
  560.     memset((char *)&krb5creds, 0, sizeof(krb5creds));
  561.  
  562.     if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  563.         goto err;
  564.  
  565.     if ((krb5rc = krb5_sname_to_principal(krb5context,
  566.                                           szHostName,
  567.                                           krb5_d_srv ?
  568.                                           krb5_d_srv :
  569.                                           KRB5_SERVICE_NAME,
  570.                                           KRB5_NT_SRV_HST,
  571.                                           &krb5creds.server)) != 0)
  572.       goto err;
  573.  
  574.     if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  575.         goto err;
  576.  
  577.     if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  578.                                          &krb5creds.client)) != 0)
  579.         goto err;
  580.  
  581.     if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  582.                                         &krb5creds, &krb5credsp)) != 0)
  583.         goto err;
  584.     rc = 1;
  585.  
  586.   err:
  587.  
  588.     if (krb5creds.client)
  589.       krb5_free_principal(krb5context, krb5creds.client);
  590.     if (krb5creds.server)
  591.       krb5_free_principal(krb5context, krb5creds.server);
  592.     if (krb5context)
  593.       krb5_free_context(krb5context);
  594.     return(rc);
  595.  
  596. #endif /* COMMENT */
  597. #endif /* HEIMDAL */
  598. #else /* KRB5 */
  599.     return(0);
  600. #endif /* KRB5 */
  601. }
  602.  
  603. int
  604. ck_krb4_is_installed()
  605. {
  606. #ifdef KRB4
  607. #ifdef OS2
  608.     return(hKRB4_32 != NULL);
  609. #else /* OS2 */
  610.     return(1);
  611. #endif /* OS2 */
  612. #else /* KRB4 */
  613.     return(0);
  614. #endif /* KRB4 */
  615. }
  616.  
  617. int
  618. ck_krb4_is_installed_as_server()
  619. {
  620.     if ( !ck_krb4_is_installed() )
  621.         return(0);
  622.  
  623. #ifdef KRB4
  624.     if ( !k4_keytab ) {
  625. #ifdef NT
  626.         char name[CKMAXPATH]="";
  627.         DWORD len = CKMAXPATH;
  628.  
  629.         len = GetWindowsDirectory(name,len);
  630.         if ( len > 0 )
  631.             ckstrncat(name,"/srvtab",CKMAXPATH);
  632.         if ( name[0] )
  633.             makestr(&k4_keytab,name);
  634. #else /* NT */
  635.         makestr(&k4_keytab,"/etc/srvtab");
  636. #endif /* NT */
  637.     }
  638.  
  639.     if ( !k4_keytab )
  640.         return(0);
  641.  
  642.     if ( zchki(k4_keytab) > 0 )
  643.         return(1);
  644. #ifdef KRB524
  645.     else if (ck_krb5_is_installed_as_server())
  646.         return(1);
  647. #endif /* KRB524 */
  648.     else
  649.         return(0);
  650. #endif /* KRB4 */
  651. }
  652.  
  653. int
  654. ck_srp_is_installed_as_server()
  655. {
  656. #ifdef CK_SRP
  657. #ifdef SRPDLL
  658.     if ( hSRP == NULL )
  659.         return(0);
  660. #endif /* SRPDLL */
  661. #ifdef COMMENT
  662.     /* This is the new API as of 1.7.4.  However, all it does
  663.        is allocate a data structure.  It can never fail.
  664.      */
  665.     {
  666.         SRP * s_srp = SRP_new(SRP_RFC2945_server_method());
  667.         if ( s_srp ) {
  668.             SRP_free(s_srp);
  669.             s_srp = NULL;
  670.             return(1);
  671.         }
  672.         return(0);
  673.     }
  674. #else /* COMMENT */
  675.     {
  676.         struct t_pw * tpw = NULL;
  677.         struct t_conf * tconf = NULL;
  678.         if((tconf = t_openconf(NULL)) == NULL)
  679.             return(0);
  680.         if((tpw = t_openpw(NULL)) == NULL) {
  681.             t_closeconf(tconf);
  682.             return(0);
  683.         }
  684.         t_closeconf(tconf);
  685.         t_closepw(tpw);
  686.         return(1);
  687.     }
  688. #endif /* COMMENT */
  689. #else /* SRP */
  690.     return(0);
  691. #endif /* SRP */
  692. }
  693.  
  694. int
  695. ck_srp_is_installed()
  696. {
  697. #ifdef CK_SRP
  698. #ifdef SRPDLL
  699.     if ( hSRP == NULL )
  700.         return(0);
  701. #endif /* SRPDLL */
  702.     return(1);
  703. #else /* CK_SRP */
  704.     return(0);
  705. #endif /* CK_SRP */
  706. }
  707.  
  708. int
  709. ck_krypto_is_installed()
  710. {
  711. #ifdef CK_SRP
  712. #ifdef OS2
  713.     if ( hLIBKRYPTO == NULL )
  714.         return(0);
  715. #endif /* OS2 */
  716.     return(1);
  717. #else /* CK_SRP */
  718.     return(0);
  719. #endif /* CK_SRP */
  720. }
  721.  
  722. int
  723. ck_crypt_is_installed()
  724. {
  725. #ifdef CK_ENCRYPTION
  726. #ifdef CRYPT_DLL
  727.     return(hCRYPT != NULL);
  728. #else /* CRYPT_DLL */
  729.     return(1);
  730. #endif /* CRYPT_DLL */
  731. #else /* ENCRYPTION */
  732.     return(0);
  733. #endif /* ENCRYPTION */
  734. }
  735.  
  736. int
  737. ck_ntlm_is_installed()
  738. {
  739. #ifdef NT
  740.     return(hSSPI != NULL);
  741. #else /* NT */
  742.     return(0);
  743. #endif /* NT */
  744. }
  745.  
  746. int
  747. ck_tn_auth_valid()
  748. {
  749.     return(validUser);
  750. }
  751.  
  752. /* C K _ K R B _ A U T H _ I N _ P R O G R E S S
  753.  *
  754.  * Is an authentication negotiation still in progress?
  755.  *
  756.  */
  757.  
  758. int
  759. #ifdef CK_ANSIC
  760. ck_tn_auth_in_progress(void)
  761. #else
  762. ck_tn_auth_in_progress()
  763. #endif
  764. {
  765.     switch (authentication_version) {
  766.     case AUTHTYPE_AUTO:
  767.         return(1);
  768.     case AUTHTYPE_NULL:
  769.         return(0);
  770. #ifdef KRB4
  771.     case AUTHTYPE_KERBEROS_V4:
  772.         if (!accept_complete) {
  773.             debug(F100,"ck_auth_in_progress() Kerberos 4 !accept_complete",
  774.                    "",0);
  775.             return(1);
  776.         }
  777.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  778.             debug(F100,"ck_auth_in_progress() Kerberos 4 !mutual_complete",
  779.                    "",0);
  780.             return(1);
  781.         }
  782.         else
  783.             return(0);
  784. #endif /* KRB4 */
  785. #ifdef KRB5
  786.     case AUTHTYPE_KERBEROS_V5:
  787.         if (!accept_complete) {
  788.             debug(F100,"ck_auth_in_progress() Kerberos 5 !accept_complete",
  789.                    "",0);
  790.             return(1);
  791.         }
  792.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  793.             debug(F100,"ck_auth_in_progress() Kerberos 5 !mutual_complete",
  794.                    "",0);
  795.             return(1);
  796.         }
  797.         else
  798.             return(0);
  799. #ifdef GSSAPI_K5
  800.     case AUTHTYPE_GSSAPI_KRB5:
  801.         if (!accept_complete) {
  802.             debug(F100,"ck_auth_in_progress() GSSAPI Kerberos 5 !accept_complete",
  803.                    "",0);
  804.             return(1);
  805.         }
  806.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  807.             debug(F100,"ck_auth_in_progress() GSSAPI Kerberos 5 !mutual_complete",
  808.                    "",0);
  809.             return(1);
  810.         }
  811.         else
  812.             return(0);
  813.         break;
  814. #endif /* GSSAPI_K5 */
  815. #endif /* KRB5 */
  816. #ifdef CK_SRP
  817.     case AUTHTYPE_SRP:
  818.         if (!accept_complete || srp_waitresp)
  819.             return(1);
  820.         else
  821.             return(0);
  822. #endif /* CK_SRP */
  823. #ifdef NTLM
  824.     case AUTHTYPE_NTLM:
  825.         if (!accept_complete) {
  826.             debug(F100,"ck_auth_in_progress() NTLM !accept_complete",
  827.                    "",0);
  828.             return(1);
  829.         }
  830.         else
  831.             return(0);
  832. #endif /* NTLM */
  833.     case AUTHTYPE_SSL:
  834.         if (!accept_complete) {
  835.             debug(F100,"ck_auth_in_progress() SSL !accept_complete",
  836.                    "",0);
  837.             return(1);
  838.         }
  839.         else
  840.             return(0);
  841.     default:
  842.         return(0);
  843.     }
  844.     return(0);
  845. }
  846.  
  847.  
  848. /*  C K _ K R B _ T N _ A U T H _ R E Q U E S T
  849.  *
  850.  *  Builds a Telnet Authentication Send Negotiation providing the
  851.  *  list of supported authentication methods.  To be used only
  852.  *  when accepting incoming connections as only the server (DO) side of the
  853.  *  Telnet negotiation is allowed to send an AUTH SEND.
  854.  *
  855.  *  Returns: 0 on success and -1 on failure
  856.  */
  857.  
  858. static unsigned char str_request[64] = { IAC, SB,
  859.                                              TELOPT_AUTHENTICATION,
  860.                                              TELQUAL_SEND };
  861. #ifdef GSSAPI_K5
  862. static int
  863. ck_tn_auth_request_gsskrb5(int i)
  864. {
  865.     if (ck_gssapi_is_installed() && ck_krb5_is_installed_as_server()) {
  866.         if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  867.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  868.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  869.                tn_auth_enc == TN_AUTH_ENC_EXCH) ) {
  870.             str_request[i++] = AUTHTYPE_KERBEROS_V5;
  871.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  872.             str_request[i] |= AUTH_ENCRYPT_AFTER_EXCHANGE;
  873.  
  874.             if ( deblog || tn_deb || debses )
  875.                 ckstrncat(tn_msg,
  876.                 "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_AFTER_EXCHANGE ",
  877.                           TN_MSG_LEN);
  878.             i++;
  879.         }
  880.     }
  881. }
  882. #endif /* GSSAPI_K5 */
  883.  
  884. #ifdef KRB5
  885. static int
  886. ck_tn_auth_request_krb5(int i)
  887. {
  888.     if (ck_krb5_is_installed_as_server()) {
  889. #ifdef CK_SSL
  890.         if ( ck_ssleay_is_installed() &&
  891.              (tls_active_flag || ssl_active_flag) &&
  892.              ssl_finished_messages )
  893.         {
  894. #ifdef USE_INI_CRED_FWD
  895.             if ( forward_flag &&
  896.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  897.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  898.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  899.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  900.                  )
  901.             {
  902.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  903.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  904.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  905.                 str_request[i] |= INI_CRED_FWD_ON;
  906.  
  907.                 if ( deblog || tn_deb || debses )
  908.                     ckstrncat(tn_msg,
  909.  "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS|INI_CRED_FWD_ON ",
  910.                                TN_MSG_LEN);
  911.                 i++;
  912.             }
  913. #endif /* USE_INI_CRED_FWD */
  914.             if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  915.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  916.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  917.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  918.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  919.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  920.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  921.  
  922.                 if ( deblog || tn_deb || debses )
  923.                     ckstrncat(tn_msg,
  924.                       "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS ",
  925.                               TN_MSG_LEN);
  926.                 i++;
  927.             }
  928.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  929.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  930.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  931.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  932.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  933.  
  934.                 if ( deblog || tn_deb || debses )
  935.                     ckstrncat(tn_msg,
  936.                     "KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  937.                                TN_MSG_LEN);
  938.                 i++;
  939.             }
  940.         }
  941. #ifdef CK_ENCRYPTION
  942.         else
  943.         {
  944. #endif /* CK_ENCRYPTION */
  945. #endif /* CK_SSL */
  946. #ifdef CK_ENCRYPTION
  947. #ifdef USE_INI_CRED_FWD
  948.             if ( forward_flag &&
  949.                  TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  950.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  951.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  952.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  953.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  954.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  955.                  )
  956.             {
  957.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  958.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  959.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  960.                 str_request[i] |= INI_CRED_FWD_ON;
  961.  
  962.                 if ( deblog || tn_deb || debses )
  963.                     ckstrncat(tn_msg,
  964.   "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT|INI_CRED_FWD_ON ",
  965.                                TN_MSG_LEN);
  966.                 i++;
  967.             }
  968. #endif /* USE_INI_CRED_FWD */
  969.  
  970.             if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  971.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  972.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  973.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  974.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  975.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  976.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  977.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  978.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  979.  
  980.                 if ( deblog || tn_deb || debses )
  981.                     ckstrncat(tn_msg,
  982.               "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT ",
  983.                                TN_MSG_LEN);
  984.                 i++;
  985.             }
  986. #ifdef CK_SSL
  987.         }
  988. #endif /* CK_SSL */
  989. #endif /* CK_ENCRYPTION */
  990.  
  991.         if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  992.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  993.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  994.                tn_auth_enc == TN_AUTH_ENC_NONE)
  995. #ifdef CK_SSL
  996.              && !(ck_ssleay_is_installed() &&
  997.                    (tls_active_flag || ssl_active_flag) &&
  998.                    tls_is_anon(0))
  999. #endif /* CK_SSL */
  1000.              )
  1001.         {
  1002. #ifdef CK_ENCRYPTION
  1003.             /* Can't perform mutual authentication without encryption */
  1004.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1005.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1006.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1007.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1008.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1009.  
  1010.                 if ( deblog || tn_deb || debses )
  1011.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL ",
  1012.                                TN_MSG_LEN);
  1013.                 i++;
  1014.             }
  1015. #endif /* CK_ENCRYPTION */
  1016.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1017.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1018.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1019.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1020.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1021.  
  1022.                 if ( deblog || tn_deb || debses )
  1023.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY ",
  1024.                                TN_MSG_LEN);
  1025.                 i++;
  1026.             }
  1027.         }
  1028.     }
  1029.     return(i);
  1030. }
  1031. #endif /* KRB5 */
  1032. #ifdef KRB4
  1033. static int
  1034. ck_tn_auth_request_krb4(int i)
  1035. {
  1036.     if (ck_krb4_is_installed_as_server()) {
  1037. #ifdef CK_ENCRYPTION
  1038.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1039.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1040.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1041.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1042.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1043.                tn_auth_enc == TN_AUTH_ENC_TELOPT) )
  1044.         {
  1045.             str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1046.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1047.             str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1048.  
  1049.             if ( deblog || tn_deb || debses )
  1050.               ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL|ENCRYPT ",
  1051.                         TN_MSG_LEN);
  1052.             i++;
  1053.         }
  1054. #endif /* CK_ENCRYPTION */
  1055.  
  1056.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1057.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1058.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1059.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1060.         {
  1061. #ifdef CK_ENCRYPTION
  1062.             /* Can't perform mutual authentication without encryption */
  1063.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1064.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1065.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1066.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1067.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1068.  
  1069.                 if ( deblog || tn_deb || debses )
  1070.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL ",
  1071.                                TN_MSG_LEN);
  1072.                 i++;
  1073.             }
  1074. #endif /* CK_ENCRYPTION */
  1075.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1076.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1077.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1078.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1079.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1080.  
  1081.                 if ( deblog || tn_deb || debses )
  1082.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|ONE_WAY ",
  1083.                                TN_MSG_LEN);
  1084.                 i++;
  1085.             }
  1086.         }
  1087.     }
  1088.  
  1089.     return(i);
  1090. }
  1091. #endif /* KRB4 */
  1092.  
  1093. #ifdef CK_SRP
  1094. static int
  1095. ck_tn_auth_request_srp(int i)
  1096. {
  1097.     if (ck_srp_is_installed_as_server()) {
  1098. #ifndef PRE_SRP_1_4_5
  1099.         /* Dont' do this yet.  SRP when it uses the ENCRYPT_USING_TELOPT   */
  1100.         /* flag it must perform a checksum of the auth-type-pair but there */
  1101.         /* is no mechansim to do that yet.                                 */
  1102. #ifdef CK_SSL
  1103.         if ( ck_ssleay_is_installed() &&
  1104.              (tls_active_flag || ssl_active_flag) &&
  1105.              ssl_finished_messages &&
  1106.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1107.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1108.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1109.                    tn_auth_enc == TN_AUTH_ENC_TELOPT))
  1110.         {
  1111.             str_request[i++] = AUTHTYPE_SRP;
  1112.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1113.             str_request[i] |= AUTH_ENCRYPT_START_TLS;
  1114.  
  1115.             if ( deblog || tn_deb || debses )
  1116.                 ckstrncat(tn_msg,
  1117.                            "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  1118.                            TN_MSG_LEN);
  1119.             i++;
  1120.         }
  1121. #ifdef CK_ENCRYPTION
  1122.         else {
  1123. #endif /* CK_ENCRYPTION */
  1124. #endif /* CK_SSL */
  1125. #ifdef CK_ENCRYPTION
  1126.             if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1127.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1128.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1129.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1130.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1131.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  1132.                  ) {
  1133.                 str_request[i++] = AUTHTYPE_SRP;
  1134.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1135.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1136.  
  1137.                 if ( deblog || tn_deb || debses )
  1138.                     ckstrncat(tn_msg,
  1139.                     "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_USING_TELOPT ",
  1140.                                TN_MSG_LEN);
  1141.                 i++;
  1142.             }
  1143. #ifdef CK_SSL
  1144.         }
  1145. #endif /* CK_SSL */
  1146. #endif /* CK_ENCRYPTION */
  1147. #endif /* PRE_SRP_1_4_5 */
  1148.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1149.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1150.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1151.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1152.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1153.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1154. #ifdef CK_SSL
  1155.              && !(ck_ssleay_is_installed() &&
  1156.                    (tls_active_flag || ssl_active_flag) &&
  1157.                    tls_is_anon(0))
  1158. #endif /* CK_SSL */
  1159.              )
  1160.         {
  1161.             str_request[i++] = AUTHTYPE_SRP;
  1162.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1163.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1164.  
  1165.             if ( deblog || tn_deb || debses )
  1166.                 ckstrncat(tn_msg,"SRP CLIENT_TO_SERVER|ONE_WAY ",
  1167.                            TN_MSG_LEN);
  1168.             i++;
  1169.         }
  1170.     }
  1171.  
  1172.     return(i);
  1173. }
  1174. #endif /* CK_SRP */
  1175.  
  1176. #ifdef CK_SSL
  1177. static int
  1178. ck_tn_auth_request_ssl(int i)
  1179. {
  1180.     if (ck_ssleay_is_installed()
  1181.          && !tls_active_flag && !ssl_active_flag && ssl_initialized
  1182.          ) {
  1183.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1184.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1185.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1186.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1187.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1188.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1189.         {
  1190.             str_request[i++] = AUTHTYPE_SSL;
  1191.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1192.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1193.             if ( deblog || tn_deb || debses )
  1194.                 ckstrncat(tn_msg,"SSL CLIENT_TO_SERVER|ONE_WAY ",
  1195.                            TN_MSG_LEN);
  1196.             i++;
  1197.         }
  1198.     }
  1199.  
  1200.     return(i);
  1201. }
  1202. #endif /* CK_SSL */
  1203. #ifdef NTLM
  1204. static int
  1205. ck_tn_auth_request_ntlm(int i)
  1206. {
  1207.     /* Microsoft's Telnet client won't perform authentication if */
  1208.     /* NTLM is not first.                                        */
  1209.     if ( ck_ntlm_is_valid(1) ) {
  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_NTLM;
  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,"NTLM CLIENT_TO_SERVER|ONE_WAY ",
  1222.                            TN_MSG_LEN);
  1223.             i++;
  1224.         }
  1225.     }
  1226.  
  1227.     return(i);
  1228. }
  1229. #endif /* NTLM */
  1230. int
  1231. #ifdef CK_ANSIC
  1232. ck_tn_auth_request(void)
  1233. #else
  1234. ck_tn_auth_request()
  1235. #endif
  1236. {
  1237.     int i = 4, rc = -1;
  1238.  
  1239. #ifdef CK_SSL
  1240.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1241.         return(0);
  1242.     }
  1243. #endif /* CK_SSL */
  1244.  
  1245.     if ( deblog || tn_deb || debses )
  1246.         strcpy(tn_msg,"TELNET SENT SB AUTHENTICATION SEND ");
  1247.  
  1248.     /* Create a list of acceptable Authentication types to send to */
  1249.     /* the client and let it choose find one that we support       */
  1250.  
  1251.     /* For those authentication methods that support Encryption or */
  1252.     /* Credentials Forwarding we must send all of the appropriate  */
  1253.     /* combinations based upon the state of                        */
  1254.     /* TELOPT_x_MODE(TELOPT_ENCRYPTION) and forward_flag.          */
  1255.  
  1256.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  1257. #ifdef GSSAPI_K5
  1258.         i = ck_tn_auth_request_gsskrb5(i);
  1259. #endif /* GSSAPI_K5 */
  1260. #ifdef KRB5
  1261.         i = ck_tn_auth_request_krb5(i);
  1262. #endif /* KRB5 */
  1263. #ifdef KRB4
  1264.         i = ck_tn_auth_request_krb4(i);
  1265. #endif /* KRB4 */
  1266. #ifdef CK_SRP
  1267.         i = ck_tn_auth_request_srp(i);
  1268. #endif /* SRP */
  1269. #ifdef CK_SSL
  1270.         i = ck_tn_auth_request_ssl(i);
  1271. #endif /* CK_SSL */
  1272. #ifdef NTLM
  1273.         i = ck_tn_auth_request_ntlm(i);
  1274. #endif /* NTLM */
  1275.     } else {
  1276.         int j;
  1277.         for ( j=0;
  1278.               j<AUTHTYPLSTSZ && auth_type_user[j] != AUTHTYPE_NULL;
  1279.               j++) {
  1280. #ifdef NTLM
  1281.             if (auth_type_user[j] == AUTHTYPE_NTLM)
  1282.                 i = ck_tn_auth_request_ntlm(i);
  1283. #endif /* NTLM */
  1284. #ifdef CK_SSL
  1285.             if ( auth_type_user[j] == AUTHTYPE_SSL )
  1286.                 i = ck_tn_auth_request_ssl(i);
  1287. #endif /* CK_SSL */
  1288. #ifdef CK_SRP
  1289.             if ( auth_type_user[j] == AUTHTYPE_SRP )
  1290.                 i = ck_tn_auth_request_srp(i);
  1291. #endif /* SRP */
  1292. #ifdef GSSAPI_K5
  1293.             if ( auth_type_user[j] == AUTHTYPE_GSSAPI_KRB5 )
  1294.                 i = ck_tn_auth_request_gsskrb5(i);
  1295. #endif /* GSSAPI_K5 */
  1296. #ifdef KRB5
  1297.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V5 )
  1298.                 i = ck_tn_auth_request_krb5(i);
  1299. #endif /* KRB5 */
  1300. #ifdef KRB4
  1301.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V4 )
  1302.                 i = ck_tn_auth_request_krb4(i);
  1303. #endif /* KRB4 */
  1304.         }
  1305.     }
  1306.  
  1307.     str_request[i++] = IAC;
  1308.     str_request[i++] = SE;
  1309.     if ( deblog || tn_deb || debses ) {
  1310.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1311.         debug(F100,tn_msg,"",0);
  1312.         if (tn_deb || debses) tn_debug(tn_msg);
  1313.     }
  1314.  
  1315.     /* Send data */
  1316. #ifdef OS2
  1317.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1318. #endif
  1319.     rc = ttol((CHAR *)str_request, i);
  1320. #ifdef OS2
  1321.     ReleaseTelnetMutex();
  1322. #endif
  1323.     if ( rc == i )
  1324.         return(0);
  1325.     else
  1326.         return(-1);
  1327. }
  1328.  
  1329. #ifdef CK_ENCRYPTION
  1330. VOID
  1331. ck_tn_enc_start()
  1332. {
  1333.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1334.         return;
  1335.     if (!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop &&
  1336.          (!encrypt_is_decrypting() || !encrypt_is_encrypting())) {
  1337.         debug(F110,"ck_tn_enc_start","nothing to do",0);
  1338.         return;
  1339.     }
  1340.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1341.     if (TELOPT_ME(TELOPT_ENCRYPTION) && !encrypt_is_encrypting()) {
  1342.         debug(F110,"ck_tn_enc_start","encrypt_request_start",0);
  1343.         encrypt_request_start();
  1344.     }
  1345.     if (TELOPT_U(TELOPT_ENCRYPTION) && !encrypt_is_decrypting()) {
  1346.         debug(F110,"ck_tn_enc_start","encrypt_send_request_start",0);
  1347.         encrypt_send_request_start();
  1348.     }
  1349.     tn_wait("encrypt start");
  1350.     tn_push();
  1351. }
  1352.  
  1353. VOID
  1354. ck_tn_enc_stop()
  1355. {
  1356.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1357.         return;
  1358.     if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop ||
  1359.          !(encrypt_is_decrypting() || encrypt_is_encrypting())) {
  1360.         debug(F110,"ck_tn_enc_stop","nothing to do",0);
  1361.       return;
  1362.     }
  1363.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1364.     if (TELOPT_U(TELOPT_ENCRYPTION) && encrypt_is_decrypting()) {
  1365.         debug(F110,"ck_tn_enc_stop","encrypt_send_request_end",0);
  1366.         encrypt_send_request_end();
  1367.     }
  1368.     if (TELOPT_ME(TELOPT_ENCRYPTION) && encrypt_is_encrypting()) {
  1369.         debug(F110,"ck_tn_enc_stop","encrypt_send_end",0);
  1370.         encrypt_send_end();
  1371.     }
  1372.     tn_wait("encrypt stop");
  1373.     tn_push();
  1374. }
  1375. #endif /* CK_ENCRYPTION */
  1376.  
  1377. /*  C K _ K R B _ T N _ S B _ A U T H
  1378.  *  An interface between the C-Kermit Telnet Command Parser and the Authent-
  1379.  *  ication option parser implemented in the Kerberos Telnet client.
  1380.  *
  1381.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1382.  *  len  - the length of the buffer
  1383.  *
  1384.  *  Returns: 0 on success and -1 on failure
  1385.  */
  1386.  
  1387. int
  1388. #ifdef CK_ANSIC
  1389. ck_tn_sb_auth(char * sb, int len)
  1390. #else /* CK_ANSIC */
  1391. ck_tn_sb_auth(sb,len) char * sb; int len;
  1392. #endif /* CK_ANSIC */
  1393. {
  1394.     /* auth_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1395.     /* and it wants the length to exclude the IAC SE bytes                  */
  1396.     char * buf;
  1397.     int rc = -1;
  1398.  
  1399.     buf = malloc(len-1);
  1400.     if ( !buf ) return(-1);
  1401.  
  1402.     buf[0] = SB;
  1403.     memcpy( &buf[1], sb, len-2 );
  1404.     rc = auth_parse(buf,len-1);
  1405.     free(buf);
  1406.     debug(F111,"ck_tn_sb_auth","rc",rc);
  1407.     if (rc == AUTH_FAILURE) {
  1408.         authentication_version = AUTHTYPE_NULL;
  1409. #ifndef NOLOCAL
  1410. #ifdef OS2
  1411.         ipadl25();
  1412. #endif /* OS2 */
  1413. #endif /* NOLOCAL */
  1414.         return(-1);
  1415.     }
  1416. #ifndef NOLOCAL
  1417. #ifdef OS2
  1418.     ipadl25();
  1419. #endif /* OS2 */
  1420. #endif /* NOLOCAL */
  1421.     return(0);
  1422. }
  1423.  
  1424. /*  C K _ K R B _ T N _ S B _ E N C R Y P T
  1425.  *  An interface between the C-Kermit Telnet Command Parser and the Encryption
  1426.  *  option parser implemented in the Kerberos Telnet client.
  1427.  *
  1428.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1429.  *  len  - the length of the buffer
  1430.  *
  1431.  *  Returns: Always returns 0 for success since encrypt_parse is void
  1432.  */
  1433.  
  1434.  
  1435. int
  1436. #ifdef CK_ANSIC
  1437. ck_tn_sb_encrypt(char * sb, int len)
  1438. #else
  1439. ck_tn_sb_encrypt(sb,len) char * sb; int len;
  1440. #endif /* CK_ANSIC */
  1441. {
  1442.     /* encrypt_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1443.     /* and it wants the length to exclude the IAC SE bytes                  */
  1444. #ifdef CK_ENCRYPTION
  1445.     char * buf;
  1446.     int rc = -1;
  1447.  
  1448.     buf = malloc(len-1);
  1449.     if ( !buf ) return(-1);
  1450.  
  1451.     buf[0] = SB;
  1452.     memcpy( &buf[1], sb, len-2 );
  1453.     rc = encrypt_parse(buf,len-1);
  1454.  
  1455.     if (rc < 0) {
  1456.         free(buf);
  1457.         return(-1);
  1458.     }
  1459.  
  1460.     /* This is a hack.  It does not belong here but should really be in */
  1461.     /* encrypt_parse() but in K95 the encrypt_parse() routine does not  */
  1462.     /* have access to the telopt_states array.                          */
  1463.     if ( buf[1] == ENCRYPT_REQEND )
  1464.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1465.     else if ( buf[1] == ENCRYPT_REQSTART )
  1466.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1467. #ifndef NOLOCAL
  1468. #ifdef OS2
  1469.     ipadl25();
  1470. #endif /* OS2 */
  1471. #endif /* NOLOCAL */
  1472.     free(buf);
  1473. #endif /* ENCRYPTION */
  1474.     return(0);
  1475. }
  1476.  
  1477.  
  1478. /*  C K _ K R B _ E N C R Y P T I N G
  1479.  *  Returns 1 if we are encrypting and 0 if we are not
  1480.  */
  1481.  
  1482. int
  1483. #ifdef CK_ANSIC
  1484. ck_tn_encrypting(VOID)
  1485. #else /* CK_ANSIC */
  1486. ck_tn_encrypting()
  1487. #endif /* CK_ANSIC */
  1488. {
  1489. #ifdef CK_ENCRYPTION
  1490.     if ( g_kstream == NULL )
  1491.         return(0);
  1492.     if ( g_kstream->encrypt && encrypt_is_encrypting()) {
  1493.         debug(F111,"ck_tn_encrypting","encrypting",
  1494.                g_kstream->encrypt_type);
  1495.         return(g_kstream->encrypt_type);
  1496.     }
  1497. #endif /* CK_ENCRYPTION */
  1498.     debug(F110,"ck_tn_encrypting","not encrypting",0);
  1499.     return(0);
  1500. }
  1501.  
  1502. /*  C K _ K R B _ D E C R Y P T I N G
  1503.  *  Returns 1 if we are decrypting and 0 if we are not
  1504.  */
  1505.  
  1506. int
  1507. #ifdef CK_ANSIC
  1508. ck_tn_decrypting(VOID)
  1509. #else
  1510. ck_tn_decrypting()
  1511. #endif /* CK_ANSIC */
  1512. {
  1513. #ifdef CK_ENCRYPTION
  1514.     if ( g_kstream == NULL )
  1515.         return(0);
  1516.     if ( g_kstream->decrypt && encrypt_is_decrypting()) {
  1517.         debug(F111,"ck_tn_decrypting","decrypting",
  1518.                g_kstream->decrypt_type);
  1519.         return(g_kstream->decrypt_type);
  1520.     }
  1521. #endif /* CK_ENCRYPTION */
  1522.     debug(F110,"ck_tn_decrypting","not decrypting",0);
  1523.     return(0);
  1524. }
  1525.  
  1526. /*  C K _ K R B _ A U T H E N T I C A T E D
  1527.  *  Returns the authentication type: AUTHTYPE_NULL, AUTHTYPE_KERBEROS4,
  1528.  *  or AUTHTYPE_KERBEROS5, AUTHTYPE_SRP, ... (see ckctel.h)
  1529.  */
  1530.  
  1531. int
  1532. #ifdef CK_ANSIC
  1533. ck_tn_authenticated(VOID)
  1534. #else
  1535. ck_tn_authenticated()
  1536. #endif
  1537. {
  1538.     return(authentication_version);
  1539. }
  1540.  
  1541. /*  C K _ K R B _ E N C R Y P T
  1542.  *  encrypts n characters in s if we are encrypting
  1543.  */
  1544.  
  1545. VOID
  1546. #ifdef CK_ANSIC
  1547. ck_tn_encrypt( char * s, int n )
  1548. #else
  1549. ck_tn_encrypt( s,n ) char * s; int n;
  1550. #endif
  1551. {
  1552. #ifdef CK_ENCRYPTION
  1553.     struct kstream_data_block i;
  1554.  
  1555.     if (g_kstream->encrypt && encrypt_is_encrypting()) {
  1556. #ifdef DEBUG
  1557.       hexdump("from plaintext", s, n);
  1558. #endif
  1559.         i.ptr = s;
  1560.         i.length = n;
  1561.         g_kstream->encrypt(&i, NULL);
  1562. #ifdef DEBUG
  1563.         hexdump("to cyphertext", s, n);
  1564. #endif
  1565.     }
  1566.     else debug(F101,"ck_tn_encrypt not encrypting","",n);
  1567. #endif /* ENCRYPTION */
  1568. }
  1569.  
  1570. /*  C K _ K R B _ D E C R Y P T
  1571.  *  decrypts n characters in s if we are decrypting
  1572.  */
  1573.  
  1574. VOID
  1575. #ifdef CK_ANSIC
  1576. ck_tn_decrypt( char * s, int n )
  1577. #else
  1578. ck_tn_decrypt( s,n ) char * s; int n;
  1579. #endif
  1580. {
  1581. #ifdef CK_ENCRYPTION
  1582.     struct kstream_data_block i;
  1583.  
  1584.     if (g_kstream->decrypt && encrypt_is_decrypting()) {
  1585.  
  1586. #ifdef DEBUG
  1587.         hexdump("from cyphertext", s, n);
  1588. #endif
  1589.  
  1590.         i.ptr = s;
  1591.         i.length = n;
  1592.         g_kstream->decrypt(&i, NULL);
  1593. #ifdef DEBUG
  1594.         hexdump("to plaintext", s, n);
  1595. #endif
  1596.     }
  1597.     else debug(F101,"ck_tn_decrypt not decrypting","",n);
  1598. #endif /* ENCRYPTION */
  1599. }
  1600.  
  1601. /*  S E N D K 5 A U T H S B
  1602.  *  Send a Kerberos 5 Authentication Subnegotiation to host and
  1603.  *  output appropriate Telnet Debug messages
  1604.  *
  1605.  *  type - Sub Negotiation type
  1606.  *  data - ptr to buffer containing data
  1607.  *  len  - len of buffer if not NUL terminated
  1608.  *
  1609.  *  returns number of characters sent or error value
  1610.  */
  1611.  
  1612. static int
  1613. #ifdef CK_ANSIC
  1614. SendK5AuthSB(int type, void *data, int len)
  1615. #else
  1616. SendK5AuthSB(type,data,len) int type; void *data; int len;
  1617. #endif
  1618. {
  1619.     int rc;
  1620.     unsigned char *p = str_data + 3;
  1621.     unsigned char *cd = (unsigned char *)data;
  1622.     extern int sstelnet;
  1623.  
  1624. #ifdef CK_SSL
  1625.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1626.         if (ttchk() < 0)
  1627.           return(0);
  1628.         else
  1629.           return(1);
  1630.     }
  1631. #endif /* CK_SSL */
  1632.  
  1633.     if ( type < 0 || type > 7 )         /* Check for invalid values */
  1634.         return(0);
  1635.  
  1636.     if (!cd) {
  1637.         cd = (unsigned char *)"";
  1638.         len = 0;
  1639.     }
  1640.  
  1641.     if (len == -1)                        /* Use strlen() for len */
  1642.         len = strlen((char *)cd);
  1643.  
  1644.     /* Construct Message */
  1645.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1646.     *p++ = AUTHTYPE_KERBEROS_V5;
  1647.     *p = AUTH_CLIENT_TO_SERVER;
  1648.     *p |= auth_how;
  1649. #ifdef CK_ENCRYPTION
  1650.     *p |= auth_crypt;
  1651. #endif
  1652. #ifdef USE_INI_CRED_FWD
  1653.     if (auth_fwd)
  1654.         *p |= INI_CRED_FWD_ON;
  1655. #endif /* USE_INI_CRED_FWD */
  1656.     p++;
  1657.     *p++ = type;
  1658.     while (len-- > 0) {
  1659.         if ((*p++ = *cd++) == IAC)
  1660.             *p++ = IAC;
  1661.     }
  1662.     *p++ = IAC;
  1663.     *p++ = SE;
  1664.  
  1665.     /* Handle Telnet Debugging Messages */
  1666.     if (deblog || tn_deb || debses) {
  1667.         int i;
  1668.         int deblen=p-str_data-2;
  1669.         char *s=NULL;
  1670.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1671.             auth_crypt
  1672. #ifdef USE_INI_CRED_FWD
  1673.               | (auth_fwd?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  1674. #endif /* USE_INI_CRED_FWD */
  1675.                     ;
  1676.  
  1677.         switch (type) {
  1678.         case 0:
  1679.             s = "AUTH";
  1680.             break;
  1681.         case 1:
  1682.             s = "REJECT";
  1683.             break;
  1684.         case 2:
  1685.             s = "ACCEPT";
  1686.             break;
  1687.         case 3:
  1688.             s = "RESPONSE";
  1689.             break;
  1690.         case 4:
  1691.             s = "FORWARD";
  1692.             break;
  1693.         case 5:
  1694.             s = "FORWARD_ACCEPT";
  1695.             break;
  1696.         case 6:
  1697.             s = "FORWARD_REJECT";
  1698.             break;
  1699.         case 7:
  1700.             s = "TLS_VERIFY";
  1701.                 break;
  1702.         }
  1703.  
  1704.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1705.                   "TELNET SENT SB ",
  1706.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1707.                  str_data[3] == TELQUAL_IS ? "IS" :
  1708.                  str_data[3] == TELQUAL_REPLY ? "REPLY" : "???"," ",
  1709.                  AUTHTYPE_NAME(authentication_version)," ",
  1710.                  AUTHMODE_NAME(mode)," ",
  1711.                  s," ",NULL);
  1712.         tn_hex(tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1713.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1714.         debug(F100,tn_msg,"",0);
  1715.         if (tn_deb || debses) tn_debug(tn_msg);
  1716.     }
  1717.  
  1718.     /* Send data */
  1719. #ifdef OS2
  1720.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1721. #endif
  1722.     rc = ttol((CHAR *)str_data, p - str_data);
  1723. #ifdef OS2
  1724.     ReleaseTelnetMutex();
  1725. #endif
  1726.     debug(F111,"SendK5AuthSB","ttol()",rc);
  1727.     return(rc);
  1728. }
  1729.  
  1730. /*  S E N D K 4 A U T H S B
  1731.  *  Send a Kerberos 4 Authentication Subnegotiation to host and
  1732.  *  output appropriate Telnet Debug messages
  1733.  *
  1734.  *  type - Sub Negotiation type
  1735.  *  data - ptr to buffer containing data
  1736.  *  len  - len of buffer if not NUL terminated
  1737.  *
  1738.  *  returns number of characters sent or error value
  1739.  */
  1740.  
  1741. static int
  1742. #ifdef CK_ANSIC
  1743. SendK4AuthSB(int type, void *data, int len)
  1744. #else
  1745. SendK4AuthSB(type,data,len) int type; void *data; int len;
  1746. #endif
  1747. {
  1748.     int rc;
  1749.     unsigned char *p = str_data + 3;
  1750.     unsigned char *cd = (unsigned char *)data;
  1751.     extern int sstelnet;
  1752.     int mode = (auth_how & AUTH_HOW_MASK) |
  1753.         auth_crypt;
  1754.  
  1755.     if ( type < 0 || type > 4 )         /* Check for invalid values */
  1756.         return(0);
  1757.  
  1758. #ifdef CK_SSL
  1759.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1760.         if (ttchk() < 0)
  1761.           return(0);
  1762.         else
  1763.           return(1);
  1764.     }
  1765. #endif /* CK_SSL */
  1766.  
  1767.     if (!cd) {
  1768.         cd = (unsigned char *)"";
  1769.         len = 0;
  1770.     }
  1771.  
  1772.     if (len == -1)                        /* Use strlen() for len */
  1773.         len = strlen((char *)cd);
  1774.  
  1775.  
  1776.     /* Construct Message */
  1777.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1778.     *p++ = AUTHTYPE_KERBEROS_V4;
  1779.     *p = AUTH_CLIENT_TO_SERVER;
  1780.     *p |= mode;
  1781.     p++;
  1782.     *p++ = type;
  1783.     while (len-- > 0) {
  1784.         if ((*p++ = *cd++) == IAC)
  1785.             *p++ = IAC;
  1786.         }
  1787.     *p++ = IAC;
  1788.     *p++ = SE;
  1789.  
  1790.     /* Handle Telnet Debugging Messages */
  1791.     if (deblog || tn_deb || debses) {
  1792.         int i;
  1793.         int deblen=p-str_data-2;
  1794.         char *s=NULL;
  1795.  
  1796.         switch (type) {
  1797.         case 0:
  1798.             s = "AUTH";
  1799.             break;
  1800.         case 1:
  1801.             s = "REJECT";
  1802.             break;
  1803.         case 2:
  1804.             s = "ACCEPT";
  1805.             break;
  1806.         case 3:
  1807.             s = "CHALLENGE";
  1808.             break;
  1809.         case 4:
  1810.             s = "RESPONSE";
  1811.             break;
  1812.         }
  1813.  
  1814.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  1815.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1816.                  str_data[3] == TELQUAL_IS ? "IS" :
  1817.                  (str_data[3] == TELQUAL_REPLY ? "REPLY" : "???")," ",
  1818.                  AUTHTYPE_NAME(authentication_version)," ",
  1819.                  AUTHMODE_NAME(mode)," ",
  1820.                  s," ",NULL);
  1821.         tn_hex(tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1822.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1823.         debug(F100,tn_msg,"",0);
  1824.         if (tn_deb || debses) tn_debug(tn_msg);
  1825.     }
  1826.  
  1827.     /* Send data */
  1828. #ifdef OS2
  1829.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1830. #endif
  1831.     rc = ttol((CHAR *)str_data, p - str_data);
  1832. #ifdef OS2
  1833.     ReleaseTelnetMutex();
  1834. #endif
  1835.     debug(F111,"SendK4AuthSB","ttol()",rc);
  1836.     return(rc);
  1837. }
  1838.  
  1839. /*  S E N D S R P A U T H S B
  1840.  *  Send a SRP Authentication Subnegotiation to host and
  1841.  *  output appropriate Telnet Debug messages
  1842.  *
  1843.  *  type - Sub Negotiation type
  1844.  *  data - ptr to buffer containing data
  1845.  *  len  - len of buffer if not NUL terminated
  1846.  *
  1847.  *  returns number of characters sent or error value
  1848.  */
  1849.  
  1850. static int
  1851. #ifdef CK_ANSIC
  1852. SendSRPAuthSB(int type, void *data, int len)
  1853. #else
  1854. SendSRPAuthSB(type,data,len) int type; void *data; int len;
  1855. #endif
  1856. {
  1857.     int rc;
  1858.     unsigned char *p = str_data + 3;
  1859.     unsigned char *cd = (unsigned char *)data;
  1860.     extern int sstelnet;
  1861.  
  1862.     /* Check for invalid values */
  1863.     if ( type != SRP_EXP && type != SRP_RESPONSE &&
  1864.          type != SRP_REJECT && type != SRP_ACCEPT &&
  1865.          type != SRP_CHALLENGE && type != SRP_PARAMS &&
  1866.          type != SRP_AUTH)
  1867.         return(0);
  1868.  
  1869.     if (len == -1)                        /* Use strlen() for len */
  1870.         len = strlen((char *)cd);
  1871.  
  1872.     /* Construct Message */
  1873.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1874.     *p++ = AUTHTYPE_SRP;
  1875.     *p = AUTH_CLIENT_TO_SERVER;
  1876.     *p |= auth_how;
  1877. #ifdef CK_ENCRYPTION
  1878.     *p |= auth_crypt;
  1879. #endif
  1880.     p++;
  1881.     *p++ = type;
  1882.     while (len-- > 0) {
  1883.         if ((*p++ = *cd++) == IAC)
  1884.             *p++ = IAC;
  1885.         }
  1886.     *p++ = IAC;
  1887.     *p++ = SE;
  1888.  
  1889.     /* Handle Telnet Debugging Messages */
  1890.     if (deblog || tn_deb || debses) {
  1891.         int i;
  1892.         int deblen=p-str_data-2;
  1893.         char *s=NULL;
  1894.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1895.             auth_crypt;
  1896.  
  1897.         switch (type) {
  1898.         case 0:
  1899.             s = "AUTH";
  1900.             break;
  1901.         case 1:
  1902.             s = "REJECT";
  1903.             break;
  1904.         case 2:
  1905.             s = "ACCEPT";
  1906.             break;
  1907.         case 3:
  1908.             s = "CHALLENGE";
  1909.             break;
  1910.         case 4:
  1911.             s = "RESPONSE";
  1912.             break;
  1913.         case 5:
  1914.             s = "FORWARD";
  1915.             break;
  1916.         case 6:
  1917.             s = "FORWARD_ACCEPT";
  1918.             break;
  1919.         case 7:
  1920.             s = "FORWARD_REJECT";
  1921.             break;
  1922.         case 8:
  1923.             s = "EXP";
  1924.             break;
  1925.         case 9:
  1926.             s = "PARAMS";
  1927.             break;
  1928.         }
  1929.  
  1930.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1931.                   "TELNET SENT SB ",
  1932.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1933.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  1934.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  1935.                  AUTHTYPE_NAME(authentication_version)," ",
  1936.                  AUTHMODE_NAME(mode)," ",
  1937.                  s," ",NULL);
  1938.         tn_hex(tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1939.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1940.         debug(F100,tn_msg,"",0);
  1941.         if (tn_deb || debses) tn_debug(tn_msg);
  1942.     }
  1943.  
  1944.     /* Send data */
  1945. #ifdef OS2
  1946.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1947. #endif
  1948.     rc = ttol((CHAR *)str_data, p - str_data);
  1949. #ifdef OS2
  1950.     ReleaseTelnetMutex();
  1951. #endif
  1952.     return(rc);
  1953. }
  1954.  
  1955. #ifdef CK_ENCRYPTION
  1956. /*
  1957.  * Function: Enable or disable the encryption process.
  1958.  *
  1959.  * Parameters:
  1960.  *      enable - TRUE to enable, FALSE to disable.
  1961.  */
  1962. static VOID
  1963. #ifdef CK_ANSIC
  1964. auth_encrypt_enable(BOOL enable)
  1965. #else
  1966. auth_encrypt_enable(enable) BOOL enable;
  1967. #endif
  1968. {
  1969.   encrypt_flag = enable;
  1970. }
  1971. #endif
  1972.  
  1973. /*
  1974.  * Function: Abort the authentication process
  1975.  *
  1976.  * Parameters:
  1977.  */
  1978. static VOID
  1979. #ifdef CK_ANSIC
  1980. auth_abort(char *errmsg, long r)
  1981. #else
  1982. auth_abort(errmsg,r) char *errmsg; long r;
  1983. #endif
  1984. {
  1985.     char buf[9];
  1986.     extern int sstelnet;
  1987.  
  1988. #ifdef CK_SSL
  1989.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1990.         return;
  1991.     }
  1992. #endif /* CK_SSL */
  1993.     debug(F111,"auth_abort",errmsg,r);
  1994.  
  1995.     /* Construct Telnet Debugging messages */
  1996.     if (deblog || tn_deb || debses) {
  1997.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1998.                   "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  1999.                   " IS ",AUTHTYPE_NAME(AUTHTYPE_NULL)," ",
  2000.                    AUTHTYPE_NAME(AUTHTYPE_NULL)," IAC SE",
  2001.                    NULL,NULL,NULL,NULL,NULL
  2002.                  );
  2003.         debug(F100,tn_msg,"",0);
  2004.         if (tn_deb || debses) tn_debug(tn_msg);
  2005.     }
  2006.  
  2007.     /* Construct the Abort message to send to the host   */
  2008.     /* Basicly we change the authentication type to NULL */
  2009.     sprintf(buf, "%c%c%c%c%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2010.              sstelnet ? TELQUAL_REPLY : TELQUAL_IS, AUTHTYPE_NULL,
  2011.              AUTHTYPE_NULL, IAC, SE);   /* safe */
  2012. #ifdef OS2
  2013.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2014. #endif
  2015.     ttol((CHAR *)buf, 8);
  2016. #ifdef OS2
  2017.     ReleaseTelnetMutex();
  2018. #endif
  2019.  
  2020.     /* If there is an error message, and error number construct */
  2021.     /* an explanation to display to the user                    */
  2022.     if (errmsg != NULL) {
  2023.         ckstrncpy(strTmp, errmsg, AUTHTMPBL);
  2024.     } else
  2025.         strTmp[0] = '\0';
  2026.  
  2027.  
  2028.     if (r != AUTH_SUCCESS) {
  2029.         ckstrncat(strTmp, "\r\n",AUTHTMPBL);
  2030. #ifdef KRB4
  2031.         if ( authentication_version == AUTHTYPE_KERBEROS_V4 ) {
  2032.             ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),
  2033.                        AUTHTMPBL);
  2034.             debug(F111,"auth_abort",(char *)krb_get_err_text_entry(r),r);
  2035.         }
  2036. #endif
  2037. #ifdef KRB5
  2038.         if ( authentication_version == AUTHTYPE_KERBEROS_V5 ) {
  2039.             ckstrncat(strTmp, error_message(r),AUTHTMPBL);
  2040.             debug(F111,"auth_abort",error_message(r),r);
  2041.         }
  2042. #endif
  2043.     }
  2044.     printf("Authentication failed: %s\r\n",strTmp);
  2045. #ifdef CKSYSLOG
  2046.     if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  2047.         cksyslog(SYSLG_LI, 0, "Telnet authentication failure",
  2048.                   (char *) szUserNameRequested,
  2049.                   strTmp);
  2050.     }
  2051. #endif /* CKSYSLOG */
  2052.     authentication_version = AUTHTYPE_NULL;
  2053. }
  2054.  
  2055.  
  2056. /*
  2057.  * Function: Copy data to buffer, doubling IAC character if present.
  2058.  *
  2059.  */
  2060. int
  2061. #ifdef CK_ANSIC
  2062. copy_for_net(unsigned char *to, unsigned char *from, int c)
  2063. #else
  2064. copy_for_net(to,from,c) unsigned char *to; unsigned char *from; int c;
  2065. #endif
  2066. {
  2067.     int n;
  2068.  
  2069.     n = c;
  2070.     debug(F111,"copy_for_net","before",n);
  2071.     while (c-- > 0) {
  2072.         if ((*to++ = *from++) == IAC) {
  2073.             n++;
  2074.             *to++ = IAC;
  2075.         }
  2076.     }
  2077.     debug(F111,"copy_for_net","after",n);
  2078.     return n;
  2079. }
  2080.  
  2081. #ifdef CK_SSL
  2082. /*  S E N D S S L A U T H S B
  2083.  *  Send a SSL Authentication Subnegotiation to host and
  2084.  *  output appropriate Telnet Debug messages
  2085.  *
  2086.  *  type - Sub Negotiation type
  2087.  *  data - ptr to buffer containing data
  2088.  *  len  - len of buffer if not NUL terminated
  2089.  *
  2090.  *  returns number of characters sent or error value
  2091.  */
  2092.  
  2093. int
  2094. #ifdef CK_ANSIC
  2095. SendSSLAuthSB(int type, void *data, int len)
  2096. #else
  2097. SendSSLAuthSB(type,data,len) int type; void *data; int len;
  2098. #endif
  2099. {
  2100.     int rc;
  2101.     unsigned char *p = str_data + 3;
  2102.     unsigned char *cd = (unsigned char *)data;
  2103.     extern int sstelnet;
  2104.  
  2105.     /* Check for invalid values */
  2106.     if ( type != SSL_START && type != SSL_ACCEPT &&
  2107.          type != SSL_REJECT)
  2108.         return(0);
  2109.  
  2110.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2111.         if (ttchk() < 0)
  2112.           return(0);
  2113.         else
  2114.           return(1);
  2115.     }
  2116.  
  2117.     if (len == -1)                        /* Use strlen() for len */
  2118.         len = strlen((char *)cd);
  2119.  
  2120.     /* Construct Message */
  2121.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  2122.     *p++ = AUTHTYPE_SSL;
  2123.     *p = AUTH_CLIENT_TO_SERVER;
  2124.     *p |= auth_how;
  2125. #ifdef CK_ENCRYPTION
  2126.     *p |= auth_crypt;
  2127. #endif
  2128.     p++;
  2129.     *p++ = type;
  2130.     while (len-- > 0) {
  2131.         if ((*p++ = *cd++) == IAC)
  2132.             *p++ = IAC;
  2133.         }
  2134.     *p++ = IAC;
  2135.     *p++ = SE;
  2136.  
  2137.     /* Handle Telnet Debugging Messages */
  2138.     if (deblog || tn_deb || debses) {
  2139.         int i;
  2140.         int deblen=p-str_data-2;
  2141.         char *s=NULL;
  2142.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  2143.             (auth_crypt?AUTH_ENCRYPT_USING_TELOPT:AUTH_ENCRYPT_OFF);
  2144.  
  2145.         switch (type) {
  2146.         case SSL_START:
  2147.             s = "START";
  2148.             break;
  2149.         case SSL_ACCEPT:
  2150.             s = "ACCEPT";
  2151.             break;
  2152.         case SSL_REJECT:
  2153.             s = "REJECT";
  2154.             break;
  2155.         }
  2156.  
  2157.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2158.                   "TELNET SENT SB ",
  2159.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  2160.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  2161.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  2162.                  AUTHTYPE_NAME(authentication_version)," ",
  2163.                  AUTHMODE_NAME(mode)," ",
  2164.                  s," ",NULL);
  2165.         tn_hex(tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  2166.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2167.         debug(F100,tn_msg,"",0);
  2168.         if (tn_deb || debses) tn_debug(tn_msg);
  2169.     }
  2170.  
  2171.     /* Send data */
  2172. #ifdef OS2
  2173.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2174. #endif
  2175.     rc = ttol((CHAR *)str_data, p - str_data);
  2176. #ifdef OS2
  2177.     ReleaseTelnetMutex();
  2178. #endif
  2179.     return(rc);
  2180. }
  2181. #endif  /* CK_SSL */
  2182.  
  2183. int
  2184. tn_how_ok(int how)
  2185. {
  2186.     switch ( tn_auth_how ) {
  2187.     case TN_AUTH_HOW_ANY:
  2188.         return(1);
  2189.     case TN_AUTH_HOW_ONE_WAY:
  2190.         return((how & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY);
  2191.     case TN_AUTH_HOW_MUTUAL:
  2192.         return((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL);
  2193.     default:
  2194.         return(0);
  2195.     }
  2196. }
  2197.  
  2198. int
  2199. tn_enc_ok(int enc)
  2200. {
  2201.     switch ( tn_auth_enc ) {
  2202.     case TN_AUTH_ENC_ANY:
  2203.         if ((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  2204.             (!ck_ssleay_is_installed
  2205. #ifdef CK_SSL
  2206.              || !ssl_finished_messages ||
  2207.              !(tls_active_flag || ssl_active_flag)
  2208. #endif /* CK_SSL */
  2209.              )) {
  2210. #ifdef CK_SSL
  2211.             if (!ssl_finished_messages)
  2212.                 debug(F100,"tn_enc_ok !ssl_finished_messages","",0);
  2213. #endif /* CK_SSL */
  2214.             return(0);
  2215.         }
  2216.         return(1);
  2217.     case TN_AUTH_ENC_NONE:
  2218.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_OFF);
  2219.     case TN_AUTH_ENC_TELOPT:
  2220.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT);
  2221.     case TN_AUTH_ENC_EXCH:
  2222.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_AFTER_EXCHANGE);
  2223.     case TN_AUTH_ENC_TLS:
  2224.         return(((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) &&
  2225.                ck_ssleay_is_installed()
  2226. #ifdef CK_SSL
  2227.                && ssl_finished_messages &&
  2228.                (tls_active_flag || ssl_active_flag)
  2229. #endif /* CK_SSL */
  2230.            );
  2231.     default:
  2232.         return(0);
  2233.     }
  2234. }
  2235.  
  2236. static int
  2237. atok(int at) {
  2238.     int i;
  2239.     if ( auth_type_user[0] == AUTHTYPE_AUTO )
  2240.         return(1);
  2241.     if ( auth_type_user[0] == AUTHTYPE_NULL )
  2242.         return(0);
  2243.  
  2244.     for ( i=0;
  2245.           i<AUTHTYPLSTSZ && auth_type_user[i] != AUTHTYPE_NULL;
  2246.           i++ ) {
  2247.         if ( auth_type_user[i] == at )
  2248.             return(1);
  2249.     }
  2250.  
  2251.     return(0);
  2252. }
  2253.  
  2254.  
  2255. /*
  2256.  * Function: Parse authentication send command
  2257.  *
  2258.  * Parameters:
  2259.  *  parsedat - the sub-command data.
  2260.  *
  2261.  *      end_sub - index of the character in the 'parsedat' array which
  2262.  *              is the last byte in a sub-negotiation
  2263.  *
  2264.  * Returns: Kerberos error code.
  2265.  */
  2266.  
  2267. static unsigned char send_list[512];
  2268. static int  send_len = 0;
  2269.  
  2270. _PROTOTYP(static int auth_send, (unsigned char *parsedat, int end_sub));
  2271.  
  2272. static int
  2273. #ifdef CK_ANSIC
  2274. auth_resend(int type)
  2275. #else
  2276. auth_resend(type) int type;
  2277. #endif /* CK_ANSIC */
  2278. {
  2279.     int i=2;
  2280.     while (i+1 <= send_len) {
  2281.         if (send_list[i] == type) {
  2282.             int j;
  2283.             send_len -= 2;
  2284.             for (j = i; j < send_len; j++)
  2285.                 send_list[j] = send_list[j+2];
  2286.         } else {
  2287.             i += 2;
  2288.         }
  2289.     }
  2290.     return(auth_send(send_list,send_len));
  2291. }
  2292.  
  2293. static int
  2294. #ifdef CK_ANSIC
  2295. auth_send(unsigned char *parsedat, int end_sub)
  2296. #else
  2297. auth_send(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  2298. #endif
  2299. {
  2300.     static unsigned char buf[4096];
  2301.     unsigned char *pname;
  2302.     int plen;
  2303.     int r;
  2304.     int i;
  2305.     int mode;
  2306. #ifdef MIT_CURRENT
  2307. #ifdef CK_ENCRYPTION
  2308.     krb5_data data;
  2309.     krb5_enc_data encdata;
  2310.     krb5_error_code code;
  2311.     krb5_keyblock random_key;
  2312. #endif /* ENCRYPTION */
  2313. #endif /* MIT_CURRENT */
  2314. #ifdef KRB5
  2315.     int krb5_msg = 0;
  2316. #endif /* KRB5 */
  2317. #ifdef KRB4
  2318.     int krb4_msg = 0;
  2319. #endif /* KRB4 */
  2320. #ifdef GSSAPI_KRB5
  2321.     int gssk5_msg = 0;
  2322. #endif /* GSSAPI_KRB5 */
  2323.     int iaccnt=0;
  2324.  
  2325. #ifdef CK_SSL
  2326.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2327.         return(AUTH_SUCCESS);
  2328. #endif /* CK_SSL */
  2329.  
  2330.     auth_how = -1;              /* We have not found an auth method  */
  2331.     auth_crypt = 0;             /* We are not using encryption (yet) */
  2332.     send_len = end_sub > 512 ? 512 : end_sub;
  2333.     memcpy(send_list,parsedat,send_len);
  2334.  
  2335.     /* Search the list of acceptable Authentication types sent from */
  2336.     /* the host and find one that we support                        */
  2337.  
  2338.     /* For Kerberos authentications, try to determine if we have a  */
  2339.     /* valid TGT, if not skip over the authentication type because  */
  2340.     /* we wouldn't be able to successfully login anyway.  Perhaps   */
  2341.     /* there is another supported authentication which we could use */
  2342.  
  2343. #ifdef NO_FTP_AUTH
  2344.     /* If the userid is "ftp" or "anonymous" refuse to perform AUTH */
  2345.     /* for Kerberos or SRP.                                         */
  2346. #endif /* NO_FTP_AUTH */
  2347.  
  2348.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  2349.     for (i = 2; i+1 <= end_sub; i += 2) {
  2350. #ifdef NTLM
  2351.         if (parsedat[i] == AUTHTYPE_NTLM &&
  2352.              ck_ntlm_is_valid(1) &&
  2353.              ntlm_auth_send() == 0) {
  2354.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2355.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2356. #ifdef CK_ENCRYPTION
  2357.                 /* NTLM does not support Telnet Encryption */
  2358.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2359.                     continue;
  2360.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2361. #endif /* CK_ENCRYPTION */
  2362.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2363.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2364.                 authentication_version = AUTHTYPE_NTLM;
  2365.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2366.                 break;
  2367.             }
  2368.         }
  2369. #endif /* NTLM */
  2370. #ifdef CK_SSL
  2371.         if ( parsedat[i] == AUTHTYPE_SSL && ssl_initialized &&
  2372. #ifdef SSLDLL
  2373.              ck_ssleay_is_installed() &&
  2374. #endif /* SSLDLL */
  2375.              !tls_active_flag && !ssl_active_flag
  2376. #ifndef USE_CERT_CB
  2377.              && tls_load_certs(ssl_ctx,ssl_con,0)
  2378. #endif /* USE_CERT_CB */
  2379.              ) {
  2380.  
  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.                 /* SSL 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_SSL;
  2392.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2393.                 break;
  2394.             }
  2395.         }
  2396. #endif /* SSL */
  2397. #ifdef CK_SRP
  2398.         if ( parsedat[i] == AUTHTYPE_SRP
  2399. #ifdef SRPDLL
  2400.              && hSRP
  2401. #endif /* SRPDLL */
  2402. #ifdef NO_FTP_AUTH
  2403.              && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2404. #endif /* NO_FTP_AUTH */
  2405.              ) {
  2406.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2407.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2408. #ifdef PRE_SRP_1_4_5
  2409.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2410.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2411.                     continue;
  2412. #endif /* PRE_SRP_1_4_5 */
  2413.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2414.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2415.                     (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2416.                      TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2417.                     continue;
  2418.  
  2419.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2420. #ifdef CK_ENCRYPTION
  2421.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2422.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2423.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2424.                 }
  2425. #endif /* CK_ENCRYPTION */
  2426. #ifdef CK_SSL
  2427.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2428.                      ck_ssleay_is_installed() &&
  2429.                      (tls_active_flag || ssl_active_flag) ) {
  2430.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2431.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2432.                 }
  2433. #endif /* CK_SSL */
  2434.                 authentication_version = AUTHTYPE_SRP;
  2435.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2436.                 break;
  2437.             }
  2438.         }
  2439. #endif /* SRP */
  2440. #ifdef GSSAPI_KRB5
  2441.         if (parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2442.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2443.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2444. #ifdef OS2
  2445.             hGSSAPI &&
  2446. #endif /* OS2 */
  2447. #ifdef NO_FTP_AUTH
  2448.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2449. #endif /* NO_FTP_AUTH */
  2450.              ck_gssapi_is_installed() && !gssk5_msg)
  2451.         {
  2452.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2453.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2454.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2455.             {
  2456.                 /* If we are auto-getting TGTs, try */
  2457.                 if ( !ck_krb5_is_tgt_valid() ) {
  2458.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2459.                 }
  2460.                 gssk5_msg = 1;
  2461.             }
  2462.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2463.                       AUTH_CLIENT_TO_SERVER &&
  2464.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2465.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2466. #ifdef CK_ENCRYPTION
  2467.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2468.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2469.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2470.                 }
  2471. #endif /* CK_ENCRYPTION */
  2472.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2473.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2474.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2475.                 break;
  2476.             }
  2477.         }
  2478. #endif /* GSSAPI_KRB5 */
  2479. #ifdef KRB5
  2480.         if (parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2481. #ifdef OS2
  2482.              hKRB5_32 &&
  2483. #endif /* OS2 */
  2484. #ifdef NO_FTP_AUTH
  2485.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2486. #endif /* NO_FTP_AUTH */
  2487.              ck_krb5_is_installed() && !krb5_msg) {
  2488.  
  2489.             /* Without encryption we can't perform mutual authentication */
  2490.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2491.                  !ck_crypt_is_installed())
  2492.                 continue;
  2493.  
  2494.             /* Skip over entries that request credential forwarding */
  2495.             /* if we are not forwarding.                            */
  2496.             if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2497.                 (forward_flag &&
  2498.                   ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2499.                 continue;
  2500.  
  2501.             if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2502.                                 parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2503.                                 parsedat[i+1] & INI_CRED_FWD_MASK) )
  2504.             {
  2505.                 /* If we are auto-getting TGTs, try */
  2506.                 if ( !ck_krb5_is_tgt_valid() ) {
  2507.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2508.                 }
  2509.                 krb5_msg = 1;
  2510.             }
  2511.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2512.                       AUTH_CLIENT_TO_SERVER &&
  2513.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2514.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2515.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2516.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2517.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2518.                     continue;
  2519.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2520.                      AUTH_ENCRYPT_START_TLS) &&
  2521.                      (!ck_ssleay_is_installed()
  2522. #ifdef CK_SSL
  2523.                        || !(tls_active_flag || ssl_active_flag)
  2524. #endif /* CK_SSL */
  2525.                        ))
  2526.                     continue;
  2527.  
  2528.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2529. #ifdef CK_ENCRYPTION
  2530.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2531.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2532.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2533.                 }
  2534. #endif /* CK_ENCRYPTION */
  2535. #ifdef CK_SSL
  2536.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2537.                      ck_ssleay_is_installed() &&
  2538.                      (tls_active_flag || ssl_active_flag) ) {
  2539.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2540.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2541.                 }
  2542. #endif /* CK_SSL */
  2543.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2544.                 authentication_version = AUTHTYPE_KERBEROS_V5;
  2545.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2546.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2547.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2548.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2549.                 }
  2550.                 break;
  2551.             }
  2552.         }
  2553. #endif /* KRB5 */
  2554. #ifdef KRB4
  2555.         if (parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2556. #ifdef OS2
  2557.              hKRB4_32 &&
  2558. #endif /* OS2 */
  2559. #ifdef NO_FTP_AUTH
  2560.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2561. #endif /* NO_FTP_AUTH */
  2562.              ck_krb4_is_installed() && !krb4_msg) {
  2563.             int rc = 0;
  2564.  
  2565.             /* Without encryption we can't perform mutual authentication */
  2566.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2567.                  !ck_crypt_is_installed() )
  2568.                 continue;
  2569.  
  2570.             if ( !k4_auth_send() )
  2571.             {
  2572.                 /* If we are auto-getting TGTs, try */
  2573.                 if ( !ck_krb4_is_tgt_valid() ) {
  2574.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2575.                 }
  2576.                 krb4_msg = 1;
  2577.             }
  2578.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2579.                       AUTH_CLIENT_TO_SERVER &&
  2580.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2581. #ifdef CK_ENCRYPTION
  2582.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2583.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2584.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2585.                     continue;
  2586.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2587.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2588.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2589.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2590.                 }
  2591. #endif /* CK_ENCRYPTION */
  2592.                 authentication_version = AUTHTYPE_KERBEROS_V4;
  2593.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2594.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2595.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2596.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2597.                 }
  2598.                 break;
  2599.             }
  2600.         }
  2601. #endif /* KRB4 */
  2602.     }
  2603.     } else {
  2604.         for (i = 2; i+1 <= end_sub; i += 2) {
  2605. #ifdef CK_SSL
  2606.             if ( atok(AUTHTYPE_SSL) && parsedat[i] == AUTHTYPE_SSL &&
  2607. #ifdef SSLDLL
  2608.                  ck_ssleay_is_installed() &&
  2609. #endif /* SSLDLL */
  2610.                  !tls_active_flag && !ssl_active_flag && ssl_initialized
  2611. #ifndef USE_CERT_CB
  2612.                  && tls_load_certs(ssl_ctx,ssl_con,0)
  2613. #endif /* USE_CERT_CB */
  2614.                  )
  2615.             {
  2616.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2617.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2618. #ifdef CK_ENCRYPTION
  2619.                     /* SSL does not support Telnet Encryption */
  2620.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2621.                         continue;
  2622.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2623. #endif /* CK_ENCRYPTION */
  2624.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2625.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2626.                     authentication_version = AUTHTYPE_SSL;
  2627.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2628.                     break;
  2629.                 }
  2630.             }
  2631. #endif /* SSL */
  2632. #ifdef CK_SRP
  2633.             if ( atok(AUTHTYPE_SRP) &&
  2634.                  parsedat[i] == AUTHTYPE_SRP
  2635. #ifdef SRPDLL
  2636.                  && hSRP
  2637. #endif /* SRPDLL */
  2638. #ifdef NO_FTP_AUTH
  2639.                  && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2640. #endif /* NO_FTP_AUTH */
  2641.                  ) {
  2642.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2643.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2644. #ifdef PRE_SRP_1_4_5
  2645.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2646.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2647.                     continue;
  2648. #endif /* PRE_SRP_1_4_5 */
  2649.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2650.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2651.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2652.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2653.                     continue;
  2654.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2655.                      AUTH_ENCRYPT_START_TLS) &&
  2656.                      (!ck_ssleay_is_installed()
  2657. #ifdef CK_SSL
  2658.                        || !(tls_active_flag || ssl_active_flag)
  2659. #endif /* CK_SSL */
  2660.                        ))
  2661.                     continue;
  2662.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2663. #ifdef CK_ENCRYPTION
  2664.                     if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2665.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2666.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2667.                     }
  2668. #endif /* CK_ENCRYPTION */
  2669. #ifdef CK_SSL
  2670.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2671.                      ck_ssleay_is_installed() &&
  2672.                      (tls_active_flag || ssl_active_flag) ) {
  2673.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2674.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2675.                 }
  2676. #endif /* CK_SSL */
  2677.                     authentication_version = AUTHTYPE_SRP;
  2678.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2679.                     break;
  2680.                 }
  2681.             }
  2682. #endif /* SRP */
  2683. #ifdef GSSAPI_KRB5
  2684.         if (atok(AUTHTYPE_GSSAPI_KRB5) &&
  2685.             parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2686.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2687.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2688. #ifdef OS2
  2689.             hGSSAPI &&
  2690. #endif /* OS2 */
  2691. #ifdef NO_FTP_AUTH
  2692.             strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2693. #endif /* NO_FTP_AUTH */
  2694.             ck_gssapi_is_installed() && !gssk5_msg)
  2695.         {
  2696.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2697.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2698.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2699.             {
  2700.                 /* If we are auto-getting TGTs, try */
  2701.                 if ( !ck_krb5_is_tgt_valid() ) {
  2702.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2703.                 }
  2704.                 gssk5_msg = 1;
  2705.             }
  2706.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2707.                       AUTH_CLIENT_TO_SERVER &&
  2708.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2709.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2710. #ifdef CK_ENCRYPTION
  2711.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2712.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2713.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2714.                 }
  2715. #endif /* CK_ENCRYPTION */
  2716.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2717.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2718.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2719.                 break;
  2720.             }
  2721.         }
  2722. #endif /* GSSAPI_KRB5 */
  2723. #ifdef KRB5
  2724.             if ( atok(AUTHTYPE_KERBEROS_V5) &&
  2725.                  parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2726. #ifdef OS2
  2727.                  hKRB5_32 &&
  2728. #endif /* OS2 */
  2729. #ifdef NO_FTP_AUTH
  2730.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2731. #endif /* NO_FTP_AUTH */
  2732.                  ck_krb5_is_installed() && !krb5_msg) {
  2733.  
  2734.                 /* Without encryption we can't perform mutual authentication */
  2735.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2736.                      !ck_crypt_is_installed())
  2737.                     continue;
  2738.  
  2739.                 /* Skip over entries that request credential forwarding */
  2740.                 /* if we are not forwarding.                            */
  2741.                 if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2742.                      (forward_flag &&
  2743.                        ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2744.                     continue;
  2745.  
  2746.                 if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2747.                                     parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2748.                                     parsedat[i+1] & INI_CRED_FWD_MASK) )
  2749.                 {
  2750.                     /* If we are auto-getting TGTs, try */
  2751.                     if ( !ck_krb5_is_tgt_valid() ) {
  2752.                         printf(
  2753.                            "Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2754.                     }
  2755.                     krb5_msg = 1;
  2756.                 }
  2757.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2758.                           AUTH_CLIENT_TO_SERVER &&
  2759.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2760.                 {
  2761.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2762.                          AUTH_ENCRYPT_USING_TELOPT) &&
  2763.                          (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2764.                            TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2765.                         continue;
  2766.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2767.                          AUTH_ENCRYPT_START_TLS) &&
  2768.                          (!ck_ssleay_is_installed()
  2769. #ifdef CK_SSL
  2770.                            || !(tls_active_flag || ssl_active_flag)
  2771. #endif /* CK_SSL */
  2772.                            ))
  2773.                         continue;
  2774.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2775. #ifdef CK_ENCRYPTION
  2776.                     if (auth_crypt) {
  2777.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2778.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2779.                     }
  2780. #endif /* CK_ENCRYPTION */
  2781. #ifdef CK_SSL
  2782.                     if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2783.                          ck_ssleay_is_installed() &&
  2784.                          (tls_active_flag || ssl_active_flag) ) {
  2785.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2786.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2787.                     }
  2788. #endif /* CK_SSL */
  2789.                     authentication_version = AUTHTYPE_KERBEROS_V5;
  2790.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2791.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2792.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2793.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2794.                     }
  2795.                     break;
  2796.                 }
  2797.             }
  2798. #endif /* KRB5 */
  2799. #ifdef KRB4
  2800.             if ( atok(AUTHTYPE_KERBEROS_V4) &&
  2801.                  parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2802. #ifdef OS2
  2803.                  hKRB4_32 &&
  2804. #endif /* OS2 */
  2805. #ifdef NO_FTP_AUTH
  2806.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2807. #endif /* NO_FTP_AUTH */
  2808.                  ck_krb4_is_installed() && !krb4_msg) {
  2809.                 int rc = 0;
  2810.  
  2811.                 /* Without encryption we can't perform mutual authentication */
  2812.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2813.                      !ck_crypt_is_installed())
  2814.                     continue;
  2815.  
  2816.                 if ( !k4_auth_send() )
  2817.                 {
  2818.                     /* If we are auto-getting TGTs, try */
  2819.                     if ( !ck_krb4_is_tgt_valid() ) {
  2820.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2821.                     }
  2822.                     krb4_msg = 1;
  2823.                 }
  2824.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2825.                           AUTH_CLIENT_TO_SERVER &&
  2826.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2827.                 {
  2828. #ifdef CK_ENCRYPTION
  2829.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2830.                         (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2831.                          TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2832.                       continue;
  2833.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2834.                     if (auth_crypt) {
  2835.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2836.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2837.                     }
  2838. #endif /* CK_ENCRYPTION */
  2839.                     authentication_version = AUTHTYPE_KERBEROS_V4;
  2840.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2841.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2842.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2843.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2844.                     }
  2845.                     break;
  2846.                 }
  2847.             }
  2848. #endif /* KRB4 */
  2849. #ifdef NTLM
  2850.         if ( atok(AUTHTYPE_NTLM) &&
  2851.              parsedat[i] == AUTHTYPE_NTLM &&
  2852.              ck_ntlm_is_valid(1) &&
  2853.              ntlm_auth_send() == 0) {
  2854.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2855.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2856. #ifdef CK_ENCRYPTION
  2857.                 /* NTLM does not support Telnet Encryption */
  2858.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2859.                     continue;
  2860.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2861. #endif /* CK_ENCRYPTION */
  2862.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2863.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2864.                 authentication_version = AUTHTYPE_NTLM;
  2865.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2866.                 break;
  2867.             }
  2868.         }
  2869. #endif /* NTLM */
  2870.         }
  2871.     }
  2872.  
  2873.     if (auth_how == -1) {               /* Did we find one? */
  2874.         switch ( auth_type_user[0] ) {  /* If not, abort the negotiation */
  2875.         case AUTHTYPE_NULL:
  2876.             auth_abort("User refused to accept any authentication method",0);
  2877.             break;
  2878.         case AUTHTYPE_AUTO:
  2879.             auth_abort("No authentication method available", 0);
  2880.             break;
  2881.         default: {
  2882.             char msg[80];
  2883.             ckmakmsg(msg,80,AUTHTYPE_NAME(auth_type_user[0]),
  2884.                       " could not be negotiated",NULL,NULL
  2885.                      );
  2886.             auth_abort(msg, 0);
  2887.         }
  2888.         }
  2889.         auth_finished(AUTH_REJECT);
  2890.         return AUTH_FAILURE;
  2891.     }
  2892.  
  2893.     printf("Authenticating with %s\r\n",
  2894.             AUTHTYPE_NAME(authentication_version));
  2895.  
  2896.     /* Send Telnet Auth Name message (if necessary) */
  2897.     switch ( authentication_version ) {
  2898.     case AUTHTYPE_SRP:
  2899.     case AUTHTYPE_KERBEROS_V4:
  2900.     case AUTHTYPE_KERBEROS_V5:
  2901.     case AUTHTYPE_GSSAPI_KRB5:
  2902.         /* if we do not have a name to login with get one now. */
  2903.         while ( szUserName[0] == '\0' ) {
  2904.             extern char * tn_pr_uid;
  2905.             int ok = uq_txt(NULL,
  2906.                      tn_pr_uid && tn_pr_uid[0] ? tn_pr_uid : "Host Userid: ",
  2907.                             1, NULL, szUserName, 63, NULL);
  2908.             if ( !ok )
  2909.                 return AUTH_FAILURE;
  2910.         }
  2911.         plen = strlen(szUserName);
  2912.         pname = (unsigned char *) szUserName;
  2913.  
  2914.         /* Construct Telnet Debugging Message */
  2915.         if (deblog || tn_deb || debses) {
  2916.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2917.                        "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2918.                        " NAME ",pname," IAC SE",NULL,
  2919.                        NULL,NULL,NULL,NULL,NULL,NULL
  2920.                      );
  2921.             debug(F100,tn_msg,"",0);
  2922.             if (tn_deb || debses) tn_debug(tn_msg);
  2923.         }
  2924.  
  2925.         /* Construct and send Authentication Name subnegotiation */
  2926.         if ( plen < sizeof(buf) - 6 ) {
  2927.             sprintf(buf, "%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2928.                      TELQUAL_NAME);
  2929.             memcpy(&buf[4], pname, plen);               /* safe */
  2930.             sprintf(&buf[plen + 4], "%c%c", IAC, SE);   /* safe */
  2931. #ifdef OS2
  2932.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2933. #endif
  2934.             ttol((CHAR *)buf, plen+6);
  2935. #ifdef OS2
  2936.             ReleaseTelnetMutex();
  2937. #endif
  2938.         } else {
  2939.             sprintf(buf, "%c%c%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2940.                      TELQUAL_NAME, IAC, SE);    /* safe */
  2941. #ifdef OS2
  2942.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2943. #endif
  2944.             ttol((CHAR *)buf, 6);
  2945. #ifdef OS2
  2946.             ReleaseTelnetMutex();
  2947. #endif
  2948.         }
  2949.     }
  2950.  
  2951.     /* Construct Authentication Mode subnegotiation message (if necessary) */
  2952.     switch ( authentication_version ) {
  2953.     case AUTHTYPE_SRP:
  2954.     case AUTHTYPE_KERBEROS_V4:
  2955.     case AUTHTYPE_KERBEROS_V5:
  2956.     case AUTHTYPE_GSSAPI_KRB5:
  2957.     case AUTHTYPE_NTLM:
  2958.         mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) | auth_crypt
  2959. #ifdef USE_INI_CRED_FWD
  2960.                | (((authentication_version == AUTHTYPE_KERBEROS_V5) &&
  2961.                   auth_fwd)?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  2962. #endif /* USE_INI_CRED_FWD */
  2963.                ;
  2964.         sprintf(buf, "%c%c%c%c%c%c%c",
  2965.                  IAC, SB, TELOPT_AUTHENTICATION,
  2966.                  TELQUAL_IS,
  2967.                  authentication_version,
  2968.                  mode,
  2969.                  KRB_AUTH);     /* safe */
  2970.         break;
  2971.     }
  2972.  
  2973.     /* Send initial authentication data */
  2974.     switch ( authentication_version ) {
  2975. #ifdef CK_SSL
  2976.     case AUTHTYPE_SSL:
  2977.         SendSSLAuthSB(SSL_START,NULL,0);
  2978.         break;
  2979. #endif /* SSL */
  2980. #ifdef CK_SRP
  2981.     case AUTHTYPE_SRP:
  2982.         sprintf(&buf[7], "%c%c", IAC, SE);      /* safe */
  2983.         if (deblog || tn_deb || debses) {
  2984.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2985.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2986.                       " IS ",AUTHTYPE_NAME(authentication_version),
  2987.                       " AUTH ",AUTHMODE_NAME(mode)," IAC SE",
  2988.                       NULL,NULL,NULL,NULL,NULL
  2989.                      );
  2990.             debug(F100,tn_msg,"",0);
  2991.             if (tn_deb || debses) tn_debug(tn_msg);
  2992.         }
  2993. #ifdef OS2
  2994.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2995. #endif
  2996.         ttol((CHAR *)buf, 9);
  2997. #ifdef OS2
  2998.         ReleaseTelnetMutex();
  2999. #endif
  3000.         break;
  3001. #endif /* SRP */
  3002. #ifdef NTLM
  3003.     case AUTHTYPE_NTLM: {
  3004.         int length = 0;
  3005.  
  3006.         for ( i=0 ; i<NTLMSecBuf[0].cbBuffer ; i++ ) {
  3007.             if ( ((char *)NTLMSecBuf[0].pvBuffer)[i] == IAC )
  3008.                 iaccnt++;
  3009.         }
  3010.  
  3011.         if ( ( 2*sizeof(ULONG) + NTLMSecBuf[0].cbBuffer + iaccnt + 10)  <
  3012.              sizeof(buf) ) {
  3013.             length = copy_for_net(&buf[7],(char *)&NTLMSecBuf[0],
  3014.                                    2*sizeof(ULONG));
  3015.             length += copy_for_net(&buf[7+length], NTLMSecBuf[0].pvBuffer,
  3016.                                   NTLMSecBuf[0].cbBuffer);
  3017.         }
  3018.         sprintf(&buf[7+length], "%c%c", IAC, SE);
  3019.  
  3020.         if (deblog || tn_deb || debses) {
  3021.             int i;
  3022.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3023.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3024.                       " IS ",AUTHTYPE_NAME(authentication_version)," ",
  3025.                       AUTHMODE_NAME(mode)," NTLM_AUTH ",
  3026.                        NULL,NULL,NULL,NULL,NULL
  3027.                       );
  3028.             tn_hex(tn_msg,TN_MSG_LEN,&buf[7],length);
  3029.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3030.             debug(F100,tn_msg,"",0);
  3031.             if (tn_deb || debses) tn_debug(tn_msg);
  3032.         }
  3033. #ifdef OS2
  3034.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3035. #endif
  3036.         ttol((CHAR *)buf, length+9);
  3037. #ifdef OS2
  3038.         ReleaseTelnetMutex();
  3039. #endif
  3040.         break;
  3041.     }
  3042. #endif /* NTLM */
  3043. #ifdef KRB4
  3044.     case AUTHTYPE_KERBEROS_V4:
  3045.         for ( i=0 ; i<k4_auth.length ; i++ ) {
  3046.             if ( k4_auth.dat[i] == IAC )
  3047.                 iaccnt++;
  3048.         }
  3049.  
  3050.         if ( k4_auth.length + iaccnt + 10 < sizeof(buf) )
  3051.           k4_auth.length = copy_for_net(&buf[7], k4_auth.dat, k4_auth.length);
  3052.         else
  3053.             k4_auth.length = 0;
  3054.         sprintf(&buf[k4_auth.length+7], "%c%c", IAC, SE);
  3055.  
  3056.         if (deblog || tn_deb || debses) {
  3057.             int i;
  3058.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3059.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3060.                       AUTHTYPE_NAME(authentication_version)," ",
  3061.                       AUTHMODE_NAME(mode)," AUTH ",
  3062.                       NULL,NULL,NULL,NULL,NULL
  3063.                      );
  3064.             tn_hex(tn_msg,TN_MSG_LEN,&buf[7],k4_auth.length);
  3065.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3066.             debug(F100,tn_msg,"",0);
  3067.             if (tn_deb || debses) tn_debug(tn_msg);
  3068.         }
  3069. #ifdef OS2
  3070.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3071. #endif
  3072.         ttol((CHAR *)buf, k4_auth.length+9);
  3073. #ifdef OS2
  3074.         ReleaseTelnetMutex();
  3075. #endif
  3076.  
  3077. #ifndef REMOVE_FOR_EXPORT
  3078. #ifdef CK_ENCRYPTION
  3079.         /*
  3080.          * If we are doing mutual authentication, get set up to send
  3081.          * the challenge, and verify it when the response comes back.
  3082.          */
  3083.         if ((auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  3084.             register int i;
  3085.             int rc = 0;
  3086.  
  3087. #ifdef MIT_CURRENT
  3088.             data.data = cred.session;
  3089.             data.length = 8; /* sizeof(cred.session) */;
  3090.  
  3091.             if (code = krb5_c_random_seed(k5_context, &data)) {
  3092.                 com_err("libtelnet", code,
  3093.                          "while seeding random number generator");
  3094.                 return(0);
  3095.             }
  3096.  
  3097.             if (code = krb5_c_make_random_key(k5_context,
  3098.                                                ENCTYPE_DES_CBC_RAW,
  3099.                                                &random_key)) {
  3100.                 com_err("libtelnet", code,
  3101.                          "while creating random session key");
  3102.                 return(0);
  3103.             }
  3104.  
  3105.             /* the krb4 code uses ecb mode, but on a single block
  3106.             with a zero ivec, ecb and cbc are the same */
  3107.             k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  3108.             k4_krbkey.length = 8;
  3109.             k4_krbkey.contents = cred.session;
  3110.  
  3111.             encdata.ciphertext.data = random_key.contents;
  3112.             encdata.ciphertext.length = random_key.length;
  3113.             encdata.enctype = ENCTYPE_UNKNOWN;
  3114.  
  3115.             data.data = k4_session_key;
  3116.             data.length = 8;
  3117.  
  3118.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3119.                                    &encdata, &data);
  3120.  
  3121.             krb5_free_keyblock_contents(k5_context, &random_key);
  3122.  
  3123.             if (code) {
  3124.                 com_err("libtelnet", code, "while encrypting random key");
  3125.                 return(0);
  3126.             }
  3127.  
  3128.             encdata.ciphertext.data = k4_session_key;
  3129.             encdata.ciphertext.length = 8;
  3130.             encdata.enctype = ENCTYPE_UNKNOWN;
  3131.  
  3132.             data.data = k4_challenge;
  3133.             data.length = 8;
  3134.  
  3135.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3136.                                    &encdata, &data);
  3137. #else /* MIT_CURRENT */
  3138.             memset(k4_sched,0,sizeof(Schedule));
  3139.             hexdump("auth_send",cred.session,8);
  3140.             rc = des_key_sched(cred.session, k4_sched);
  3141.             if ( rc == -1 ) {
  3142.                 printf("?Invalid DES key specified in credentials\r\n");
  3143.                 debug(F110,"auth_send",
  3144.                       "invalid DES Key specified in credentials",0);
  3145.             } else if ( rc == -2 ) {
  3146.                 printf("?Weak DES key specified in credentials\r\n");
  3147.                 debug(F110,"auth_send",
  3148.                       "weak DES Key specified in credentials",0);
  3149.             } else if ( rc != 0 ) {
  3150.                 printf("?DES Key Schedule not set by credentials\r\n");
  3151.                 debug(F110,"auth_send",
  3152.                       "DES Key Schedule not set by credentials",0);
  3153.             }
  3154.             hexdump("auth_send schedule",k4_sched,8*16);
  3155.  
  3156.             des_set_random_generator_seed(cred.session);
  3157.  
  3158.             do {
  3159.                 des_new_random_key(k4_session_key);
  3160.                 des_fixup_key_parity(k4_session_key);
  3161.             } while ( ck_des_is_weak_key(k4_session_key) );
  3162.  
  3163.             hexdump("auth_send des_new_random_key(k4_session_key)",
  3164.                      k4_session_key,8);
  3165.  
  3166.             /* Decrypt the session key so that we can send it to the */
  3167.             /* host as a challenge                                   */
  3168. #ifdef NT
  3169.             des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 0);
  3170. #else /* NT */
  3171.             des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 0);
  3172. #endif /* NT */
  3173.             hexdump(
  3174.                 "auth_send des_ecb_encrypt(k4_session_key,k4_session_key,0)",
  3175.                 k4_session_key,8
  3176.                     );
  3177.             /* Prepare the result of the challenge */
  3178.             /* Decrypt the session_key, add 1, and then encrypt it */
  3179.             /* The result stored in k4_challenge should match the  */
  3180.             /* KRB4_RESPONSE value from the host.                  */
  3181. #ifdef NT
  3182.             des_ecb_encrypt(k4_session_key, k4_challenge, k4_sched, 0);
  3183. #else /* NT */
  3184.             des_ecb_encrypt(&k4_session_key, &k4_challenge, k4_sched, 0);
  3185. #endif /* NT */
  3186.  
  3187.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,0)",
  3188.                      k4_challenge,8);
  3189. #endif /* MIT_CURRENT */
  3190.             /*
  3191.             * Increment the challenge by 1, and encrypt it for
  3192.             * later comparison.
  3193.             */
  3194.             for (i = 7; i >= 0; --i) {
  3195.                 register int x;
  3196.                 x = (unsigned int)k4_challenge[i] + 1;
  3197.                 k4_challenge[i] = x;    /* ignore overflow */
  3198.                 if (x < 256)            /* if no overflow, all done */
  3199.                     break;
  3200.             }
  3201.             hexdump("auth_send k4_challenge+1",k4_challenge,8);
  3202. #ifdef MIT_CURRENT
  3203.             data.data = k4_challenge;
  3204.             data.length = 8;
  3205.  
  3206.             encdata.ciphertext.data = k4_challenge;
  3207.             encdata.ciphertext.length = 8;
  3208.             encdata.enctype = ENCTYPE_UNKNOWN;
  3209.  
  3210.             if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0, &data,
  3211.                                        &encdata)) {
  3212.                 com_err("libtelnet", code, "while encrypting random key");
  3213.                 return(0);
  3214.             }
  3215. #else /* MIT_CURRENT */
  3216. #ifdef NT
  3217.             des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  3218. #else /* NT */
  3219.             des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  3220. #endif /* NT */
  3221.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,1)",
  3222.                      k4_challenge,8);
  3223. #endif /* MIT_CURRENT */
  3224.         }
  3225. #endif  /* ENCRYPTION */
  3226. #endif /* REMOVE_FOR_EXPORT */
  3227.         break;
  3228. #endif /* KRB4 */
  3229. #ifdef GSSAPI_KRB5
  3230.     case AUTHTYPE_GSSAPI_KRB5:
  3231.         for ( i=0 ; i<gss_send_tok.length ; i++ ) {
  3232.             if ( ((char *)gss_send_tok.value)[i] == IAC )
  3233.                 iaccnt++;
  3234.         }
  3235.  
  3236.         if ( gss_send_tok.length + iaccnt + 10 < sizeof(buf) )
  3237.             gss_send_tok.length = copy_for_net(&buf[7], gss_send_tok.value,
  3238.                                                gss_send_tok.length);
  3239.         else
  3240.             gss_send_tok.length = 0;
  3241.         sprintf(&buf[gss_send_tok.length+7], "%c%c", IAC, SE);       /* safe */
  3242.         if (deblog || tn_deb || debses) {
  3243.             int i;
  3244.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3245.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3246.                       AUTHTYPE_NAME(authentication_version)," ",
  3247.                       AUTHMODE_NAME(mode)," AUTH ",
  3248.                       NULL,NULL,NULL,NULL,NULL
  3249.                      );
  3250.             tn_hex(tn_msg,TN_MSG_LEN,&buf[7],gss_send_tok.length);
  3251.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3252.             debug(F100,tn_msg,"",0);
  3253.             if (tn_deb || debses) tn_debug(tn_msg);
  3254.         }
  3255. #ifdef OS2
  3256.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3257. #endif
  3258.         ttol((CHAR *)buf, gss_send_tok.length+9);
  3259. #ifdef OS2
  3260.         ReleaseTelnetMutex();
  3261. #endif
  3262.         break;
  3263. #endif /* GSSAPI_KRB5 */
  3264. #ifdef KRB5
  3265.     case AUTHTYPE_KERBEROS_V5:
  3266.         debug(F111,"auth_send KRB5","k5_auth.length",k5_auth.length);
  3267.         for ( i=0 ; i<k5_auth.length ; i++ ) {
  3268.             if ( ((char *)k5_auth.data)[i] == IAC )
  3269.                 iaccnt++;
  3270.         }
  3271.  
  3272.         if ( k5_auth.length + iaccnt + 10 < sizeof(buf) )
  3273.           k5_auth.length = copy_for_net(&buf[7], k5_auth.data, k5_auth.length);
  3274.         else {
  3275.           debug(F100,"auth_send() KRB5 auth data too large for buffer","",0);
  3276.           k5_auth.length = 0;
  3277.         }
  3278.  
  3279.         sprintf(&buf[k5_auth.length+7], "%c%c", IAC, SE);       /* safe */
  3280.         if (deblog || tn_deb || debses) {
  3281.             int i;
  3282.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3283.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3284.                       AUTHTYPE_NAME(authentication_version)," ",
  3285.                       AUTHMODE_NAME(mode)," AUTH ",
  3286.                       NULL,NULL,NULL,NULL,NULL
  3287.                      );
  3288.             tn_hex(tn_msg,TN_MSG_LEN,&buf[7],k5_auth.length);
  3289.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3290.             debug(F100,tn_msg,"",0);
  3291.             if (tn_deb || debses) tn_debug(tn_msg);
  3292.         }
  3293. #ifdef OS2
  3294.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3295. #endif
  3296.         ttol((CHAR *)buf, k5_auth.length+9);
  3297. #ifdef OS2
  3298.         ReleaseTelnetMutex();
  3299. #endif
  3300. #ifdef HEIMDAL
  3301.         krb5_data_free(&k5_auth);
  3302. #else /* HEIMDAL */
  3303.         krb5_free_data_contents(k5_context,&k5_auth);
  3304.         memset(&k5_auth,0,sizeof(krb5_data));
  3305. #endif /* HEIMDAL */
  3306.         break;
  3307. #endif /* KRB5 */
  3308.     }
  3309.     return AUTH_SUCCESS;
  3310. }
  3311.  
  3312. /*
  3313.  * Function: Parse authentication REPLY command
  3314.  *
  3315.  * Parameters:
  3316.  *  parsedat - the sub-command data.
  3317.  *
  3318.  *      end_sub - index of the character in the 'parsedat' array which
  3319.  *              is the last byte in a sub-negotiation
  3320.  *
  3321.  * Returns: Kerberos error code.
  3322.  */
  3323. static int
  3324. #ifdef CK_ANSIC
  3325. auth_reply(unsigned char *parsedat, int end_sub)
  3326. #else
  3327. auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3328. #endif
  3329. {
  3330.     int n = AUTH_FAILURE;
  3331.  
  3332.     if ( parsedat[2] != authentication_version ) {
  3333.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3334.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3335.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3336.         auth_finished(AUTH_REJECT);
  3337.         return(AUTH_FAILURE);
  3338.     }
  3339.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3340.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3341.                 AUTHMODE_NAME(parsedat[3]),
  3342.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3343.         auth_finished(AUTH_REJECT);
  3344.         return(AUTH_FAILURE);
  3345.     }
  3346.  
  3347. #ifdef KRB4
  3348.     if (authentication_version == AUTHTYPE_KERBEROS_V4)
  3349.         n = k4_auth_reply(parsedat, end_sub);
  3350. #endif
  3351. #ifdef KRB5
  3352.     if (authentication_version == AUTHTYPE_KERBEROS_V5)
  3353.         n = k5_auth_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3354. #endif
  3355. #ifdef CK_SRP
  3356.     if (authentication_version == AUTHTYPE_SRP) {
  3357. #ifndef PRE_SRP_1_7_3
  3358.         n = new_srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3359. #else /* PRE_SRP_1_7_3 */
  3360.         n = srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3361. #endif /* PRE_SRP_1_7_3 */
  3362.     }
  3363. #endif /* SRP */
  3364. #ifdef CK_SSL
  3365.     if (authentication_version == AUTHTYPE_SSL)
  3366.         n = ssl_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3367. #endif /* SSL */
  3368. #ifdef NTLM
  3369.     if (authentication_version == AUTHTYPE_NTLM)
  3370.         n = ntlm_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3371. #endif /* NTLM */
  3372.     return n;
  3373. }
  3374.  
  3375.  
  3376. /*
  3377.  * Function: Parse authentication IS command
  3378.  *
  3379.  * Parameters:
  3380.  *  parsedat - the sub-command data.
  3381.  *
  3382.  *      end_sub - index of the character in the 'parsedat' array which
  3383.  *              is the last byte in a sub-negotiation
  3384.  *
  3385.  * Returns: Kerberos error code.
  3386.  */
  3387. static int
  3388. #ifdef CK_ANSIC
  3389. auth_is(unsigned char *parsedat, int end_sub)
  3390. #else
  3391. auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3392. #endif
  3393. {
  3394.     int n = AUTH_FAILURE;
  3395.  
  3396.     if ( parsedat[2] == AUTHTYPE_NULL ) {
  3397.         auth_finished(AUTH_REJECT);
  3398.         return(AUTH_FAILURE);
  3399.     }
  3400.  
  3401.     /*
  3402.      *  If CLIENT_CHOOSE_ONCE is selected the server will not allow the
  3403.      *  client to switch to an alternate authentication method if the one
  3404.      *  it originally selected fails.  (ie, if the host's SRP parameters
  3405.      *  are invalid.)  However, I think this is a bit of a security risk
  3406.      *  since allowing that functionality means that it is impossible to
  3407.      *  detect if an attack is being carried out on
  3408.      */
  3409. #define CLIENT_CHOOSE_ONCE
  3410. #ifdef CLIENT_CHOOSE_ONCE
  3411.     if ( authentication_version == AUTHTYPE_AUTO )
  3412. #endif /* CLIENT_CHOOSE_ONCE */
  3413.     {
  3414.         /* this block of code needs to check the initial parameters */
  3415.         /* to ensure that those returned match one of the sets that */
  3416.         /* were sent to the client in the first place.              */
  3417.  
  3418.         int i=0;
  3419.         for ( i=4; str_request[i] != IAC ; i+=2) {
  3420.             if (str_request[i] == parsedat[2] &&
  3421.                  str_request[i+1] == parsedat[3])
  3422.                 break;
  3423.         }
  3424.  
  3425.         if ( str_request[i] == IAC ) {
  3426.             printf("Invalid authentication type pair (%s,%s)\r\n",
  3427.                     AUTHTYPE_NAME(parsedat[2]),
  3428.                     AUTHMODE_NAME(parsedat[3]));
  3429.             auth_finished(AUTH_REJECT);
  3430.             return(AUTH_FAILURE);
  3431.         }
  3432.  
  3433.         if (authentication_version != parsedat[2]) {
  3434.             authentication_version = parsedat[2];
  3435.             auth_how = (parsedat[3] & AUTH_HOW_MASK);
  3436.             auth_crypt = (parsedat[3] & AUTH_ENCRYPT_MASK);
  3437.             auth_fwd = (parsedat[3] & INI_CRED_FWD_MASK);
  3438.             debug(F111,"auth_is","authentication_version",
  3439.                   authentication_version);
  3440.             debug(F111,"auth_is","auth_how",auth_how);
  3441.             debug(F111,"auth_is","auth_crypt",auth_crypt);
  3442.             debug(F111,"auth_is","auth_fwd",auth_fwd);
  3443.         }
  3444.     }
  3445.  
  3446. #ifdef CLIENT_CHOOSE_ONCE
  3447.     if ( parsedat[2] != authentication_version ) {
  3448.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3449.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3450.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3451.         auth_finished(AUTH_REJECT);
  3452.         return(AUTH_FAILURE);
  3453.     }
  3454.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3455.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3456.                 AUTHMODE_NAME(parsedat[3]),
  3457.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3458.         auth_finished(AUTH_REJECT);
  3459.         return(AUTH_FAILURE);
  3460.     }
  3461. #endif /* CLIENT_CHOOSE_ONCE */
  3462.  
  3463.     switch (authentication_version) {
  3464. #ifdef KRB4
  3465.     case AUTHTYPE_KERBEROS_V4:
  3466.         n = k4_auth_is(parsedat, end_sub);
  3467.         break;
  3468. #endif
  3469. #ifdef KRB5
  3470.     case AUTHTYPE_KERBEROS_V5:
  3471.         n = k5_auth_is(parsedat[3],parsedat, end_sub);
  3472.         break;
  3473. #endif
  3474. #ifdef CK_SRP
  3475.     case AUTHTYPE_SRP:
  3476. #ifndef PRE_SRP_1_7_3
  3477.         n = new_srp_is(parsedat[3], parsedat, end_sub);
  3478. #else /* PRE_SRP_1_7_3 */
  3479.         n = srp_is(parsedat[3], parsedat, end_sub);
  3480. #endif /* PRE_SRP_1_7_3 */
  3481.         break;
  3482. #endif /* SRP */
  3483. #ifdef CK_SSL
  3484.     case AUTHTYPE_SSL:
  3485.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3486.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3487.         n = ssl_is(parsedat, end_sub);
  3488.         break;
  3489. #endif /* SSL */
  3490. #ifdef NTLM
  3491.     case AUTHTYPE_NTLM:
  3492.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3493.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3494.         n = ntlm_is(parsedat, end_sub);
  3495.         break;
  3496. #endif /* NTLM */
  3497.     case AUTHTYPE_NULL:
  3498.     default:
  3499.         n = AUTH_FAILURE;
  3500.     }
  3501.     debug(F111,"auth_is","n",n);
  3502.     return n;
  3503. }
  3504.  
  3505. /*
  3506.  * Function: Parse authentication NAME command
  3507.  *
  3508.  * Parameters:
  3509.  *  parsedat - the sub-command data.
  3510.  *
  3511.  *      end_sub - index of the character in the 'parsedat' array which
  3512.  *              is the last byte in a sub-negotiation
  3513.  *
  3514.  * Returns: Kerberos error code.
  3515.  */
  3516. static int
  3517. #ifdef CK_ANSIC
  3518. auth_name(unsigned char *parsedat, int end_sub)
  3519. #else
  3520. auth_name(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3521. #endif
  3522. {
  3523.     int len = (end_sub-2) > 63 ? 63 : (end_sub-2);
  3524.     if ( len > 0 && (len + 1) < sizeof(szUserNameRequested)) {
  3525.         memcpy(szUserNameRequested,&parsedat[2],len);           /* safe */
  3526.         szUserNameRequested[len] = '\0';
  3527.     } else
  3528.       szUserNameRequested[0] = '\0';
  3529.     debug(F111,"auth_name szUserNameRequested",szUserNameRequested,len);
  3530.     return(AUTH_SUCCESS);
  3531. }
  3532.  
  3533. /*
  3534.  * Function: Parse the athorization sub-options and reply.
  3535.  *
  3536.  * Parameters:
  3537.  *      parsedat - sub-option string to parse.
  3538.  *
  3539.  *      end_sub - last charcter position in parsedat.
  3540.  */
  3541. int
  3542. auth_parse(unsigned char *parsedat, int end_sub)
  3543. {
  3544.     int rc = AUTH_FAILURE;
  3545.     switch (parsedat[1]) {
  3546.     case TELQUAL_SEND:
  3547.         rc = auth_send(parsedat, end_sub);
  3548.         break;
  3549.     case TELQUAL_REPLY:
  3550.         rc= auth_reply(parsedat, end_sub);
  3551.         break;
  3552.     case TELQUAL_IS:
  3553.         rc = auth_is(parsedat, end_sub);
  3554.         break;
  3555.     case TELQUAL_NAME:
  3556.         rc = auth_name(parsedat, end_sub);
  3557.         break;
  3558.     }
  3559.     debug(F111,"auth_parse","rc",rc);
  3560.     return(rc);
  3561. }
  3562.  
  3563.  
  3564. /*
  3565.  * Function: Initialization routine called kstream encryption system.
  3566.  *
  3567.  * Parameters:
  3568.  *  data - user data.
  3569.  */
  3570. int
  3571. #ifdef CK_ANSIC
  3572. auth_init(kstream ks)
  3573. #else
  3574. auth_init(ks) kstream_ptr ks;
  3575. #endif
  3576. {
  3577. #ifdef FORWARD
  3578.     forwarded_tickets = 0;  /* were tickets forwarded? */
  3579. #endif /* FORWARD */
  3580. #ifdef CK_ENCRYPTION
  3581.     encrypt_init(ks,cx_type);
  3582. #endif
  3583.     return 0;
  3584. }
  3585.  
  3586.  
  3587. /*
  3588.  * Function: Destroy routine called kstream encryption system.
  3589.  *
  3590.  * Parameters:
  3591.  *  data - user data.
  3592.  */
  3593. VOID
  3594. #ifdef CK_ANSIC
  3595. auth_destroy(void)
  3596. #else
  3597. auth_destroy()
  3598. #endif
  3599. {
  3600. }
  3601.  
  3602.  
  3603. /*
  3604.  * Function: Callback to encrypt a block of characters
  3605.  *
  3606.  * Parameters:
  3607.  *  out - return as pointer to converted buffer.
  3608.  *
  3609.  *  in - the buffer to convert
  3610.  *
  3611.  * Returns: number of characters converted.
  3612.  */
  3613. int
  3614. #ifdef CK_ANSIC
  3615. auth_encrypt(struct kstream_data_block *out,
  3616.              struct kstream_data_block *in)
  3617. #else
  3618. auth_encrypt(out,in)
  3619.     struct kstream_data_block *out; struct kstream_data_block *in;
  3620. #endif
  3621. {
  3622.     out->ptr = in->ptr;
  3623.  
  3624.     out->length = in->length;
  3625.  
  3626.     return(out->length);
  3627. }
  3628.  
  3629.  
  3630. /*
  3631.  * Function: Callback to decrypt a block of characters
  3632.  *
  3633.  * Parameters:
  3634.  *  out - return as pointer to converted buffer.
  3635.  *
  3636.  *  in - the buffer to convert
  3637.  *
  3638.  * Returns: number of characters converted.
  3639.  */
  3640. int
  3641. #ifdef CK_ANSIC
  3642. auth_decrypt(struct kstream_data_block *out,
  3643.              struct kstream_data_block *in)
  3644. #else
  3645. auth_decrypt(out,in)
  3646.     struct kstream_data_block *out; struct kstream_data_block *in;
  3647. #endif
  3648. {
  3649.     out->ptr = in->ptr;
  3650.  
  3651.     out->length = in->length;
  3652.  
  3653.     return(out->length);
  3654. }
  3655.  
  3656. #ifdef KRB4
  3657. #ifdef NT
  3658. void
  3659. ck_krb4_debug(int x)
  3660. {
  3661.     set_krb_debug(x);
  3662.     set_krb_ap_req_debug(x);
  3663. }
  3664. #endif /* NT */
  3665. int
  3666. ck_krb4_autoget_TGT(char * realm)
  3667. {
  3668.     extern struct krb_op_data krb_op;
  3669.     extern struct krb4_init_data krb4_init;
  3670.     char passwd[PWD_SZ];
  3671.     char prompt[256];
  3672.     char * saverealm=NULL;
  3673.     int  rc = -1;
  3674.     extern char * k4prprompt;
  3675.     extern char * k4pwprompt;
  3676.  
  3677.     ini_kerb();         /* Place defaults in above structs */
  3678.     passwd[0] = '\0';
  3679.  
  3680.     if ( krb4_init.principal == NULL ||
  3681.          krb4_init.principal[0] == '\0') {
  3682.         int ok = uq_txt(NULL, 
  3683.                  k4prprompt && k4prprompt[0] ?
  3684.                  k4prprompt :
  3685.                  "Kerberos 4 Principal: ",
  3686.                  2, NULL, passwd,PWD_SZ-1, NULL);
  3687.         if ( ok && passwd[0] )
  3688.             makestr(&krb4_init.principal,passwd);
  3689.         else
  3690.             return(0);
  3691.     }
  3692.  
  3693.     /* Save realm in init structure so it can be restored */
  3694.     if ( realm ) {
  3695.         saverealm = krb4_init.realm;
  3696.         krb4_init.realm = realm;
  3697.     }
  3698.  
  3699.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  3700.         int ok;
  3701.         if ( k4pwprompt && k4pwprompt[0] &&
  3702.              (strlen(k4pwprompt) + strlen(krb4_init.principal) +
  3703.                strlen(krb4_init.realm) - 4) < sizeof(prompt)) {
  3704.             sprintf(prompt,k4pwprompt,krb4_init.principal,krb4_init.realm);
  3705.         } else
  3706.             ckmakxmsg(prompt,sizeof(prompt),
  3707.                   "Kerberos 4 Password for ",krb4_init.principal,"@",
  3708.                   krb4_init.realm,": ",
  3709.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3710.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL);
  3711.         if ( !ok )
  3712.             passwd[0] = '\0';
  3713.     } else {
  3714.         ckstrncpy(passwd,pwbuf,sizeof(passwd));
  3715. #ifdef OS2
  3716.         if ( pwcrypt )
  3717.             ck_encrypt((char *)passwd);
  3718. #endif /* OS2 */
  3719.     }
  3720.  
  3721.     if ( passwd[0] ) {
  3722.         makestr(&krb4_init.password,passwd);
  3723.         rc = ck_krb4_initTGT(&krb_op, &krb4_init);
  3724.         free(krb4_init.password);
  3725.         krb4_init.password = NULL;
  3726.     }
  3727.  
  3728.     krb4_init.password = NULL;
  3729.     memset(passwd,0,PWD_SZ);
  3730.  
  3731.     /* restore realm to init structure if needed */
  3732.     if ( saverealm )
  3733.         krb4_init.realm = saverealm;
  3734.     return(rc == 0);
  3735. }
  3736.  
  3737. char *
  3738. ck_krb4_realmofhost(char *host)
  3739. {
  3740.     return (char *)krb_realmofhost(host);
  3741. }
  3742.  
  3743. /*
  3744.  *
  3745.  * K4_auth_send - gets authentication bits we need to send to KDC.
  3746.  *
  3747.  * Result is left in auth
  3748.  *
  3749.  * Returns: 0 on failure, 1 on success
  3750.  */
  3751. static int
  3752. #ifdef CK_ANSIC
  3753. k4_auth_send(void)
  3754. #else
  3755. k4_auth_send()
  3756. #endif
  3757. {
  3758.     int r=0;                                    /* Return value */
  3759.     char instance[INST_SZ+1]="";
  3760.     char *realm=NULL;
  3761.     char tgt[4*REALM_SZ+1];
  3762.  
  3763.     memset(instance, 0, sizeof(instance));
  3764.  
  3765. #ifdef COMMENT
  3766.     /* we only need to call krb_get_phost if the hostname */
  3767.     /* is not fully qualified.  But we have already done  */
  3768.     /* this in netopen() call.  This will save a round of */
  3769.     /* DNS queries.                                       */
  3770.     debug(F110,"k4_auth_send","krb_get_phost",0);
  3771.     if (realm = (char *)krb_get_phost(szHostName)) {
  3772.         ckstrncpy(instance, realm, INST_SZ);
  3773.     }
  3774. #else /* COMMENT */
  3775.     {
  3776.         char *p;
  3777.         ckstrncpy(instance, szHostName, INST_SZ);
  3778.         for ( p=instance; *p && *p != '.' ; p++ );
  3779.         *p = '\0';
  3780.     }
  3781. #endif /* COMMENT */
  3782.  
  3783.     debug(F110,"k4_auth_send","krb_get_realmofhost",0);
  3784.     realm = (char *)krb_realmofhost(szHostName);
  3785.  
  3786.     if (!realm) {
  3787.         strcpy(strTmp, "Can't find realm for host \"");
  3788.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  3789.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  3790.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3791.         krb4_errno = r;
  3792.         makestr(&krb4_errmsg,strTmp);
  3793.         return(0);
  3794.     }
  3795.  
  3796.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  3797.     r = ck_krb4_tkt_isvalid(tgt);
  3798.  
  3799.     if ( r <= 0 && krb4_autoget )
  3800.         ck_krb4_autoget_TGT(realm);
  3801.  
  3802.     debug(F110,"k4_auth_send","krb_mk_req",0);
  3803.     r = krb_mk_req(&k4_auth, krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3804.                     instance, realm, 0);
  3805.  
  3806.     if (r == 0) {
  3807.         debug(F110,"k4_auth_send","krb_get_cred",0);
  3808.         r = krb_get_cred(krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3809.                           instance, realm, (CREDENTIALS *)&cred);
  3810.         if (r)
  3811.             debug(F111,"k4_auth_send","krb_get_cred() failed",r);
  3812.     }
  3813.     else
  3814.         debug(F111,"k4_auth_send","krb_mk_req() failed",r);
  3815.  
  3816.     if (r) {
  3817.         strcpy(strTmp, "Can't get \"");
  3818.         ckstrncat(strTmp,
  3819.                   krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,AUTHTMPBL);
  3820.         if (instance[0] != 0) {
  3821.             ckstrncat(strTmp, ".",AUTHTMPBL);
  3822.             ckstrncat(strTmp, instance,AUTHTMPBL);
  3823.         }
  3824.         ckstrncat(strTmp, "@",AUTHTMPBL);
  3825.         ckstrncat(strTmp, realm,AUTHTMPBL);
  3826.         ckstrncat(strTmp, "\" ticket\r\n  ",AUTHTMPBL);
  3827.         ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),AUTHTMPBL);
  3828.         debug(F111,"k4_auth_send",(char *)krb_get_err_text_entry(r),r);
  3829.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3830.         krb4_errno = r;
  3831.         makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3832.         return(0);
  3833.     }
  3834.  
  3835. #ifdef OS2
  3836.     if ( !szUserName[0] || !stricmp(szUserName,cred.pname) ) {
  3837.         ckstrncpy(szUserName, cred.pname, UIDBUFLEN);
  3838.     }
  3839. #endif /* OS2 */
  3840.     krb4_errno = r;
  3841.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3842.     debug(F110,"k4_auth_send",krb4_errmsg,0);
  3843.     return(1);
  3844. }
  3845.  
  3846. /*
  3847.  * Function: K4 parse authentication reply command
  3848.  *
  3849.  * Parameters:
  3850.  *  parsedat - the sub-command data.
  3851.  *
  3852.  *  end_sub - index of the character in the 'parsedat' array which
  3853.  *              is the last byte in a sub-negotiation
  3854.  *
  3855.  * Returns: Kerberos error code.
  3856.  */
  3857. static int
  3858. #ifdef CK_ANSIC
  3859. k4_auth_reply(unsigned char *parsedat, int end_sub)
  3860. #else
  3861. k4_auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3862. #endif
  3863. {
  3864. #ifdef CK_ENCRYPTION
  3865.     Session_Key skey;
  3866. #ifdef MIT_CURRENT
  3867.     krb5_data kdata;
  3868.     krb5_enc_data encdata;
  3869.     krb5_error_code code;
  3870. #endif /* MIT_CURRENT */
  3871. #endif
  3872.     time_t t;
  3873.     int x;
  3874.     int i;
  3875.  
  3876.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  3877.         auth_finished(AUTH_REJECT);
  3878.         return AUTH_FAILURE;
  3879.     }
  3880.  
  3881.     if (parsedat[4] == KRB_REJECT) {
  3882.         strTmp[0] = 0;
  3883.  
  3884.         for (i = 5; i <= end_sub; i++) {
  3885.             if (parsedat[i] == IAC)
  3886.                 break;
  3887.             strTmp[i-5] = parsedat[i];
  3888.             strTmp[i-4] = 0;
  3889.         }
  3890.  
  3891.         if (!strTmp[0])
  3892.             strcpy(strTmp, "Authentication rejected by remote machine!");
  3893.         printf("Kerberos V4 authentication failed!\r\n%s\r\n",strTmp);
  3894.         krb4_errno = -1;
  3895.         makestr(&krb4_errmsg,strTmp);
  3896.         auth_finished(AUTH_REJECT);
  3897.         return AUTH_FAILURE;
  3898.     }
  3899.  
  3900.     if (parsedat[4] == KRB_ACCEPT) {
  3901.         int net_len;
  3902.         if ((parsedat[3] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
  3903.             ckmakmsg(strTmp,sizeof(strTmp),"Kerberos V4 accepts you as ",
  3904.                       szUserName,NULL,NULL);
  3905.             printf("%s\r\n",strTmp);
  3906.             accept_complete = 1;
  3907.             krb4_errno = 0;
  3908.             makestr(&krb4_errmsg,strTmp);
  3909.             auth_finished(AUTH_USER);
  3910.             return AUTH_SUCCESS;
  3911.         }
  3912.  
  3913.         if ((parsedat[3] & AUTH_HOW_MASK) != AUTH_HOW_MUTUAL) {
  3914.             printf("Kerberos V4 authentication failed!\r\n");
  3915.             ckstrncpy(strTmp,
  3916.         "Kerberos V4 accepted you, but didn't provide mutual authentication",
  3917.                        sizeof(strTmp));
  3918.             printf("%s\r\n",strTmp);
  3919.             krb4_errno = -1;
  3920.             makestr(&krb4_errmsg,strTmp);
  3921.             auth_finished(AUTH_REJECT);
  3922.             return AUTH_FAILURE;
  3923.         }
  3924.  
  3925. #ifndef REMOVE_FOR_EXPORT
  3926. #ifdef CK_ENCRYPTION
  3927.         SendK4AuthSB(KRB4_CHALLENGE,k4_session_key,sizeof(k4_session_key));
  3928.  
  3929.         /* We have sent the decrypted session key to the host as a challenge */
  3930.         /* now encrypt it to restore it to its original valid DES key value */
  3931. #ifdef MIT_CURRENT
  3932.         kdata.data = k4_session_key;
  3933.         kdata.length = 8;
  3934.  
  3935.         encdata.ciphertext.data = k4_session_key;
  3936.         encdata.ciphertext.length = 8;
  3937.         encdata.enctype = ENCTYPE_UNKNOWN;
  3938.  
  3939.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey,
  3940.                                    0, 0, &kdata, &encdata)) {
  3941.             com_err("k4_auth_reply", code,
  3942.                      "while encrypting session_key");
  3943.             auth_finished(AUTH_REJECT);
  3944.             return AUTH_FAILURE;
  3945.         }
  3946. #else /* MIT_CURRENT */
  3947. #ifdef NT
  3948.         des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 1);
  3949. #else /* NT */
  3950.         des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 1);
  3951. #endif /* NT */
  3952.         hexdump(
  3953.             "k4_auth_reply des_ecb_encrypt(k4_session_key,k4_session_key,1)",
  3954.              k4_session_key,
  3955.              8
  3956.                 );
  3957. #endif /* MIT_CURRENT */
  3958.  
  3959. #ifdef CK_SSL
  3960.         if (!(ssl_active_flag || tls_active_flag))
  3961. #endif /* CK_SSL */
  3962.         {
  3963.         /* And then use it to configure the encryption state machine. */
  3964.             skey.type = SK_DES;
  3965.             skey.length = 8;
  3966.             skey.data = k4_session_key;
  3967.             encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  3968.         }
  3969. #endif /* ENCRYPTION */
  3970. #endif /* REMOVE_FOR_EXPORT */
  3971.         accept_complete = 1;
  3972.         ckmakmsg(strTmp,sizeof(strTmp),
  3973.                  "Kerberos V4 accepts you as ",szUserName,NULL,NULL);
  3974.         printf("%s\r\n",strTmp);
  3975.         krb4_errno = 0;
  3976.         makestr(&krb4_errmsg,strTmp);
  3977.         auth_finished(AUTH_USER);
  3978.         return AUTH_SUCCESS;
  3979.     }
  3980.  
  3981.     if (parsedat[4] == KRB4_RESPONSE) {
  3982.         if (end_sub < 12) {
  3983.             auth_finished(AUTH_REJECT);
  3984.             return AUTH_FAILURE;
  3985.         }
  3986.  
  3987.         hexdump("KRB4_RESPONSE &parsedat[5]",&parsedat[5],8);
  3988. #ifdef CK_ENCRYPTION
  3989.         hexdump("KRB4_RESPONSE k4_challenge",k4_challenge,8);
  3990.  
  3991.         /* The datablock returned from the host should match the value */
  3992.         /* we stored in k4_challenge.                                  */
  3993.         if (memcmp(&parsedat[5], k4_challenge, sizeof(k4_challenge)) != 0) {
  3994.             printf("Kerberos V4 authentication failed!\r\n%s\r\n",
  3995.             "Remote machine is being impersonated!");
  3996.             krb4_errno = -1;
  3997.             makestr(&krb4_errmsg,"Remote machine is being impersonated!");
  3998.             auth_finished(AUTH_REJECT);
  3999.             return AUTH_FAILURE;
  4000.         }
  4001. #else /* ENCRYPTION */
  4002.         makestr(&krb4_errmsg,"Kermit built without support for encryption.");
  4003.         return AUTH_FAILURE;
  4004. #endif /* ENCRYPTION */
  4005.         mutual_complete = 1;
  4006.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  4007.                    sizeof(strTmp));
  4008.         printf("%s\r\n",strTmp);
  4009.         krb4_errno = 0;
  4010.         makestr(&krb4_errmsg,strTmp);
  4011.         auth_finished(AUTH_USER);
  4012.         return AUTH_SUCCESS;
  4013.     }
  4014.     auth_finished(AUTH_REJECT);
  4015.     return AUTH_FAILURE;
  4016. }
  4017.  
  4018. /*
  4019.  * Function: K4 parse authentication IS command
  4020.  *
  4021.  * Parameters:
  4022.  *  parsedat - the sub-command data.
  4023.  *
  4024.  *  end_sub - index of the character in the 'parsedat' array which
  4025.  *            is the last byte in a sub-negotiation
  4026.  *
  4027.  * Returns: Kerberos error code.
  4028.  */
  4029.  
  4030. static int
  4031. #ifdef CK_ANSIC
  4032. k4_auth_is(unsigned char *parsedat, int end_sub)
  4033. #else
  4034. k4_auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  4035. #endif
  4036. {
  4037. #ifdef CK_ENCRYPTION
  4038.     Session_Key skey;
  4039. #ifdef MIT_CURRENT
  4040.     Block datablock, tmpkey;
  4041.     krb5_data kdata;
  4042.     krb5_enc_data encdata;
  4043.     krb5_error_code code;
  4044. #else /* MIT_CURRENT */
  4045.     Block datablock;
  4046. #endif /* MIT_CURRENT */
  4047. #endif  /* ENCRYPTION */
  4048.     char realm[REALM_SZ+1];
  4049.     char instance[INST_SZ];
  4050.     int r = 0;
  4051.     char * data = &parsedat[5];
  4052.     int    cnt = end_sub - 5;
  4053.     extern char myipaddr[];
  4054.     struct hostent *host;
  4055.     struct in_addr inaddr;
  4056.     int i;
  4057.  
  4058.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  4059.         debug(F110,"k4_auth_is","Not kerberos v4",0);
  4060.         auth_finished(AUTH_REJECT);
  4061.         return AUTH_FAILURE;
  4062.     }
  4063.  
  4064.     switch (parsedat[4]) {
  4065.     case KRB_AUTH:
  4066.         debug(F110,"k4_auth_is","KRB_AUTH",0);
  4067.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ+1);
  4068.         if (realm[0] == '\0') {
  4069.             SendK4AuthSB(KRB_REJECT, (void *)"No local V4 Realm.", -1);
  4070.             printf("\r\n? Kerberos 4 - No Local Realm\r\n");
  4071.             debug(F110,"k4_auth_is","No local realm",0);
  4072.             krb4_errno = -1;
  4073.             makestr(&krb4_errmsg,"No local realm");
  4074.             auth_finished(AUTH_REJECT);
  4075.             return AUTH_FAILURE;
  4076.         }
  4077.         debug(F110,"k4_auth_is",realm,0);
  4078.         if ( cnt < sizeof(k4_auth.dat) ) {
  4079.             k4_auth.length = cnt;
  4080.             memcpy((void *)k4_auth.dat, (void *)data, k4_auth.length);
  4081.         } else
  4082.             k4_auth.length = 0;
  4083.         hexdump("k4_auth.dat",k4_auth.dat, k4_auth.length);
  4084.  
  4085.         /* Get Instance */
  4086.         inaddr.s_addr = inet_addr(myipaddr);
  4087.         host = gethostbyaddr((unsigned char *)&inaddr,4,PF_INET);
  4088.         if ( host ) {
  4089. #ifdef HADDRLIST
  4090.             host = ck_copyhostent(host);
  4091. #endif /* HADDRLIST */
  4092.             ckstrncpy(instance,host->h_name,INST_SZ);
  4093.             for ( i=0;i<INST_SZ;i++ ) {
  4094.                 if ( instance[i] == '.' )
  4095.                     instance[i] = '\0';
  4096.                 else
  4097.                     instance[i] = tolower(instance[i]);
  4098.             }
  4099.         } else {
  4100.             instance[0] = '*';
  4101.             instance[1] = 0;
  4102.         }
  4103.  
  4104.         if (r = krb_rd_req(&k4_auth,
  4105.                             krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  4106.                             instance, 0, &k4_adat, k4_keytab)) {
  4107.  
  4108.             hexdump("k4_adat", &k4_adat, sizeof(AUTH_DAT));
  4109.             krb_kntoln(&k4_adat, k4_name);
  4110.             ckmakmsg(strTmp,sizeof(strTmp),
  4111.                      "Kerberos failed him as ", k4_name,NULL,NULL);
  4112.             printf("%s\r\n",strTmp);
  4113.             krb4_errno = r;
  4114.             makestr(&krb4_errmsg,strTmp);
  4115.             SendK4AuthSB(KRB_REJECT, (void *)krb_get_err_text_entry(r), -1);
  4116.             auth_finished(AUTH_REJECT);
  4117.             return AUTH_FAILURE;
  4118.         }
  4119.  
  4120. #ifdef CK_ENCRYPTION
  4121.         memcpy((void *)k4_session_key, (void *)k4_adat.session,
  4122.                 sizeof(Block));                 /* safe */
  4123.         hexdump("k4_auth_is k4_session_key",k4_session_key,sizeof(Block));
  4124. #endif /* ENCRYPTION */
  4125.         krb_kntoln(&k4_adat, k4_name);
  4126.  
  4127.         ckstrncpy(szUserNameAuthenticated,k4_name,UIDBUFLEN);
  4128.         if (szUserNameRequested && !kuserok(&k4_adat, k4_name)) {
  4129.             SendK4AuthSB(KRB_ACCEPT, (void *)0, 0);
  4130.             if ( !strcmp(k4_name,szUserNameRequested) )
  4131.                 auth_finished(AUTH_VALID);
  4132.             else
  4133.                 auth_finished(AUTH_USER);
  4134.             accept_complete = 1;
  4135.         }
  4136.         else {
  4137.             SendK4AuthSB(KRB_REJECT,
  4138.                   (void *)"user is not authorized", -1);
  4139.             auth_finished(AUTH_REJECT);
  4140.             krb4_errno = r;
  4141.             makestr(&krb4_errmsg,"user is not authorized");
  4142.             return(AUTH_FAILURE);
  4143.         }
  4144.         break;
  4145.  
  4146.     case KRB4_CHALLENGE:
  4147.         debug(F110,"k4_auth_is","KRB_CHALLENGE",0);
  4148. #ifndef CK_ENCRYPTION
  4149.         SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4150. #else   /* ENCRYPTION */
  4151.         if (!VALIDKEY(k4_session_key)) {
  4152.             /*
  4153.             * We don't have a valid session key, so just
  4154.             * send back a response with an empty session
  4155.             * key.
  4156.             */
  4157.             SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4158.             mutual_complete = 1;
  4159.             break;
  4160.         }
  4161.  
  4162.         /*
  4163.         * Initialize the random number generator since it's
  4164.         * used later on by the encryption routine.
  4165.         */
  4166. #ifdef MIT_CURRENT
  4167.         kdata.data = k4_session_key;
  4168.         kdata.length = 8;
  4169.  
  4170.         if (code = krb5_c_random_seed(k5_context, &kdata)) {
  4171.             com_err("k4_auth_is", code,
  4172.                      "while seeding random number generator");
  4173.             auth_finished(AUTH_REJECT);
  4174.             return AUTH_FAILURE;
  4175.         }
  4176.  
  4177.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4178.         /*
  4179.         * Take the received encrypted challenge, and encrypt
  4180.         * it again to get a unique session_key for the
  4181.         * ENCRYPT option.
  4182.         */
  4183.         k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  4184.         k4_krbkey.length = 8;
  4185.         k4_krbkey.contents = k4_session_key;
  4186.  
  4187.         kdata.data = datablock;
  4188.         kdata.length = 8;
  4189.  
  4190.         encdata.ciphertext.data = tmpkey;
  4191.         encdata.ciphertext.length = 8;
  4192.         encdata.enctype = ENCTYPE_UNKNOWN;
  4193.  
  4194.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4195.                                    &kdata, &encdata)) {
  4196.             com_err("k4_auth_is", code, "while encrypting random key");
  4197.             auth_finished(AUTH_REJECT);
  4198.             return AUTH_FAILURE;
  4199.         }
  4200.  
  4201. #ifdef CK_SSL
  4202.         if (!(ssl_active_flag || tls_active_flag))
  4203. #endif /* CK_SSL */
  4204.         {
  4205.             skey.type = SK_DES;
  4206.             skey.length = 8;
  4207.             skey.data = tmpkey;
  4208.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4209.         }
  4210.         /*
  4211.         * Now decrypt the received encrypted challenge,
  4212.         * increment by one, re-encrypt it and send it back.
  4213.         */
  4214.         encdata.ciphertext.data = datablock;
  4215.         encdata.ciphertext.length = 8;
  4216.         encdata.enctype = ENCTYPE_UNKNOWN;
  4217.  
  4218.         kdata.data = k4_challenge;
  4219.         kdata.length = 8;
  4220.  
  4221.         if (code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  4222.                                    &encdata, &kdata)) {
  4223.             com_err("k4_auth_is", code, "while decrypting challenge");
  4224.             auth_finished(AUTH_REJECT);
  4225.             return AUTH_FAILURE;
  4226.         }
  4227. #else /* MIT_CURRENT */
  4228.         des_set_random_generator_seed(k4_session_key);
  4229.         r = des_key_sched(k4_session_key, k4_sched);
  4230.         if ( r == -1 ) {
  4231.             printf("?Invalid DES key specified in credentials\r\n");
  4232.             debug(F110,"auth_is CHALLENGE",
  4233.                    "invalid DES Key specified in credentials",0);
  4234.         } else if ( r == -2 ) {
  4235.             printf("?Weak DES key specified in credentials\r\n");
  4236.             debug(F110,"auth_is CHALLENGE",
  4237.                    "weak DES Key specified in credentials",0);
  4238.         } else if ( r != 0 ) {
  4239.             printf("?DES Key Schedule not set by credentials\r\n");
  4240.             debug(F110,"auth_is CHALLENGE",
  4241.                    "DES Key Schedule not set by credentials",0);
  4242.         }
  4243.         hexdump("auth_is schedule",k4_sched,8*16);
  4244.  
  4245.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4246.         hexdump("auth_is challege",datablock,sizeof(Block));
  4247.  
  4248.         /*
  4249.         * Take the received encrypted challenge, and encrypt
  4250.         * it again to get a unique k4_session_key for the
  4251.         * ENCRYPT option.
  4252.         */
  4253. #ifdef NT
  4254.         des_ecb_encrypt(datablock, k4_session_key, k4_sched, 1);
  4255. #else /* NT */
  4256.         des_ecb_encrypt(&datablock, &k4_session_key, k4_sched, 1);
  4257. #endif /* NT */
  4258.         hexdump("auth_is des_ecb_encrypt(datablock,k4_session_key,1)",
  4259.                  k4_session_key,8);
  4260.  
  4261. #ifdef CK_SSL
  4262.         if (!(ssl_active_flag || tls_active_flag))
  4263. #endif /* CK_SSL */
  4264.         {
  4265.             skey.type = SK_DES;
  4266.             skey.length = 8;
  4267.             skey.data = k4_session_key;
  4268.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4269.         }
  4270.         /*
  4271.         * Now decrypt the received encrypted challenge,
  4272.         * increment by one, re-encrypt it and send it back.
  4273.         */
  4274. #ifdef NT
  4275.         des_ecb_encrypt(datablock, k4_challenge, k4_sched, 0);
  4276. #else /* NT */
  4277.         des_ecb_encrypt(&datablock, &k4_challenge, k4_sched, 0);
  4278. #endif /* NT */
  4279.         hexdump("auth_is des_ecb_encrypt(datablock,k4_challenge,0)",
  4280.                  k4_session_key,8);
  4281. #endif /* MIT_CURRENT */
  4282.         for (r = 7; r >= 0; r--) {
  4283.             register int t;
  4284.             t = (unsigned int)k4_challenge[r] + 1;
  4285.             k4_challenge[r] = t;        /* ignore overflow */
  4286.             if (t < 256)                /* if no overflow, all done */
  4287.                 break;
  4288.         }
  4289.         hexdump("auth_is k4_challenge+1",k4_challenge,8);
  4290.  
  4291. #ifdef MIT_CURRENT
  4292.         kdata.data = k4_challenge;
  4293.         kdata.length = 8;
  4294.  
  4295.         encdata.ciphertext.data = k4_challenge;
  4296.         encdata.ciphertext.length = 8;
  4297.         encdata.enctype = ENCTYPE_UNKNOWN;
  4298.  
  4299.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4300.                                    &kdata, &encdata)) {
  4301.             com_err("k4_auth_is", code, "while decrypting challenge");
  4302.             auth_finished(AUTH_REJECT);
  4303.             return AUTH_FAILURE;
  4304.         }
  4305. #else /* MIT_CURRENT */
  4306. #ifdef NT
  4307.         des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  4308. #else /* NT */
  4309.         des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  4310. #endif /* NT */
  4311.         hexdump("auth_is des_ecb_encrypt(k4_challenge_key,k4_challenge,1)",
  4312.                  k4_challenge,8);
  4313.  
  4314. #endif /* MIT_CURRENT */
  4315.         SendK4AuthSB(KRB4_RESPONSE,(void *)k4_challenge,sizeof(k4_challenge));
  4316. #endif  /* ENCRYPTION */
  4317.         mutual_complete = 1;
  4318.         break;
  4319.  
  4320.     default:
  4321.         if (1)
  4322.             printf("Unknown Kerberos option %d\r\n", data[-1]);
  4323.         SendK4AuthSB(KRB_REJECT, 0, 0);
  4324.         return(AUTH_FAILURE);
  4325.     }
  4326.     krb4_errno = r;
  4327.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  4328.     return(AUTH_SUCCESS);
  4329. }
  4330. #endif /* KRB4 */
  4331.  
  4332. #ifdef KRB5
  4333. int
  4334. ck_krb5_autoget_TGT(char * realm)
  4335. {
  4336.     extern struct krb_op_data krb_op;
  4337.     extern struct krb5_init_data krb5_init;
  4338.     char passwd[PWD_SZ];
  4339.     char prompt[64];
  4340.     char * saverealm=NULL;
  4341.     int  rc = -1;
  4342.     extern char * k5prprompt;
  4343.     extern char * k5pwprompt;
  4344.  
  4345.     ini_kerb();         /* Place defaults in above structs */
  4346.     passwd[0] = '\0';
  4347.  
  4348.     if ( krb5_init.principal == NULL ||
  4349.          krb5_init.principal[0] == '\0') {
  4350.         int ok = uq_txt(NULL,k5prprompt && k5prprompt[0] ? k5prprompt :
  4351.                   "Kerberos 5 Principal: ",2,NULL,passwd,PWD_SZ-1,NULL);
  4352.         if ( ok && passwd[0] )
  4353.             makestr(&krb5_init.principal,passwd);
  4354.         else
  4355.             return(0);
  4356.     }
  4357.  
  4358.     /* Save realm in init structure so it can be restored */
  4359.     if ( realm ) {
  4360.         saverealm = krb5_init.realm;
  4361.         krb5_init.realm = realm;
  4362.     }
  4363.  
  4364.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  4365.         int ok;
  4366.         if ( k5pwprompt && k5pwprompt[0] &&
  4367.              (strlen(k5pwprompt) + strlen(krb5_init.principal) +
  4368.               strlen(krb5_init.realm) - 4) < sizeof(prompt)) {
  4369.             sprintf(prompt,k5pwprompt,krb5_init.principal,krb5_init.realm);
  4370.         } else
  4371.         ckmakxmsg(prompt,sizeof(prompt),
  4372.                   k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  4373.                   "Kerberos 5 Password for ",
  4374.                   krb5_init.principal,"@",krb5_init.realm,": ",
  4375.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL
  4376.                  );
  4377.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL);
  4378.         if ( !ok )
  4379.             passwd[0] = '\0';
  4380.     } else {
  4381.         ckstrncpy(passwd,pwbuf,sizeof(passwd));
  4382. #ifdef OS2
  4383.         if ( pwcrypt )
  4384.             ck_encrypt((char *)passwd);
  4385. #endif /* OS2 */
  4386.     }
  4387.  
  4388.     if ( passwd[0] ) {
  4389.         extern struct krb4_init_data krb4_init;
  4390.         char * savek4realm=NULL;
  4391.  
  4392.         makestr(&krb5_init.password,passwd);
  4393.  
  4394.         if ( krb5_d_getk4 ) {
  4395.             krb5_init.getk4 = 1;
  4396.             makestr(&krb4_init.principal,krb5_init.principal);
  4397.             makestr(&krb4_init.password,passwd);
  4398.             if ( realm ) {
  4399.                 savek4realm = krb4_init.realm;
  4400.                 krb4_init.realm = realm;
  4401.             }
  4402.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,&krb4_init);
  4403.  
  4404.             if ( savek4realm )
  4405.                 krb4_init.realm = savek4realm;
  4406.             free(krb4_init.password);
  4407.             krb4_init.password = NULL;
  4408.         } else {
  4409.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,NULL);
  4410.         }
  4411.  
  4412.         free(krb5_init.password);
  4413.         krb5_init.password = NULL;
  4414.  
  4415.         memset(passwd,0,PWD_SZ);
  4416.     }
  4417.  
  4418.     /* restore realm to init structure if needed */
  4419.     if ( saverealm )
  4420.         krb5_init.realm = saverealm;
  4421.     return(rc == 0);
  4422. }
  4423.  
  4424. static krb5_error_code
  4425. #ifdef CK_ANSIC
  4426. k5_get_ccache( krb5_context k5_context, krb5_ccache * p_ccache,
  4427.                char * cc_name )
  4428. #else  /* CK_ANSIC */
  4429. k5_get_ccache(k5_context, p_ccache, cc_name)
  4430.     krb5_context k5_context;
  4431.     krb5_ccache * p_ccache;
  4432.     char * cc_name;
  4433. #endif /* CK_ANSIC */
  4434. {
  4435.     krb5_error_code r=0;
  4436.     char cc_tmp[CKMAXPATH+1];
  4437.     const char * def_name = NULL;
  4438.  
  4439. #ifndef HEIMDAL
  4440.     if ( cc_name ) {
  4441.         if ( strncmp("FILE:",cc_name,5) &&
  4442.              strncmp("MEMORY:",cc_name,7) &&
  4443.              strncmp("API:",cc_name,4) &&
  4444.              strncmp("STDIO:",cc_name,6))
  4445.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",cc_name,NULL,NULL);
  4446.         else {
  4447.             ckstrncpy(cc_tmp,cc_name,CKMAXPATH);
  4448.         }
  4449.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4450.         if (r != 0) {
  4451.             com_err("k5_get_ccache resolving ccache",r,
  4452.                      cc_tmp);
  4453.         }
  4454.     } else if ( krb5_d_cc ) {
  4455.         if ( strncmp("FILE:",krb5_d_cc,5) &&
  4456.              strncmp("MEMORY:",krb5_d_cc,7) &&
  4457.              strncmp("API:",krb5_d_cc,4) &&
  4458.              strncmp("STDIO:",krb5_d_cc,6))
  4459.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",krb5_d_cc,NULL,NULL);
  4460.         else {
  4461.             ckstrncpy(cc_tmp,krb5_d_cc,CKMAXPATH);
  4462.         }
  4463.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4464.         if (r != 0) {
  4465.             com_err("k5_get_ccache resolving ccache",r,
  4466.                      krb5_d_cc);
  4467.         }
  4468.     } else
  4469. #endif /* HEIMDAL */
  4470.     {
  4471.         if ((r = krb5_cc_default(k5_context, p_ccache))) {
  4472.             com_err("k5_get_ccache",r,"while getting default ccache");
  4473.         }
  4474.     }
  4475.         /* do not set krb5_errno/krb5_errmsg here since the value returned */
  4476.         /* is being passed internally within the krb5 functions.           */
  4477.     return(r);
  4478. }
  4479.  
  4480.  
  4481. char *
  4482. ck_krb5_realmofhost(char *host)
  4483. {
  4484.     char ** realmlist=NULL;
  4485.     krb5_context private_context=NULL;
  4486.     static char * realm = NULL;
  4487.  
  4488.     if ( !host )
  4489.         return NULL;
  4490.  
  4491.     if ( realm ) {
  4492.         free(realm);
  4493.         realm = NULL;
  4494.     }
  4495.  
  4496.     /* create private_context */
  4497.     if (krb5_init_context(&private_context)) {
  4498.         debug(F110,"ck_krb5_realmofhost()","unable to init_context",0);
  4499.         return(NULL);
  4500.     }
  4501.  
  4502.     krb5_get_host_realm(private_context,host,&realmlist);
  4503.     if (realmlist && realmlist[0]) {
  4504.         makestr(&realm,realmlist[0]);
  4505.         krb5_free_host_realm(private_context,realmlist);
  4506.         realmlist = NULL;
  4507.     }
  4508.  
  4509.     if ( private_context ) {
  4510.         krb5_free_context(private_context);
  4511.         private_context = NULL;
  4512.     }
  4513.  
  4514.     if (ckstrchr(realm,'.') == NULL) {
  4515.         int n = 0;
  4516.         char * p = host;
  4517.         while ( (p = ckstrchr(p,'.')) != NULL ) {
  4518.             n++;
  4519.             p++;
  4520.         }
  4521.         if (n == 1) {
  4522.             makestr(&realm,host);
  4523.             ckupper(realm);
  4524.         } else {
  4525.             free(realm);
  4526.             realm = NULL;
  4527.         }
  4528.     }
  4529.     return(realm);
  4530. }
  4531.  
  4532. /*
  4533.  *
  4534.  * K5_auth_send - gets authentication bits we need to send to KDC.
  4535.  *
  4536.  * Code lifted from telnet sample code in the appl directory.
  4537.  *
  4538.  * Result is left in k5_auth
  4539.  *
  4540.  * Returns: 0 on failure, 1 on success
  4541.  *
  4542.  */
  4543.  
  4544. static int
  4545. #ifdef CK_ANSIC
  4546. k5_auth_send(int how, int encrypt, int forward)
  4547. #else
  4548. k5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  4549. #endif
  4550. {
  4551.     krb5_error_code r=0;
  4552.     krb5_ccache ccache=NULL;
  4553. #ifndef HEIMDAL
  4554.     krb5_creds creds;
  4555. #endif /* HEIMDAL */
  4556.     krb5_creds * new_creds=NULL;
  4557. #ifdef CK_ENCRYPTION
  4558.     krb5_keyblock *newkey = 0;
  4559. #endif /* CK_ENCRYPTION */
  4560.     krb5_flags ap_opts, auth_flags;
  4561.     char type_check[32];
  4562.     krb5_data checksum;
  4563.     int len=0;
  4564.     char * realm = NULL;
  4565.     char tgt[256];
  4566.  
  4567.     realm = ck_krb5_realmofhost(szHostName);
  4568.     if (!realm) {
  4569.         ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  4570.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  4571.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  4572.         printf("?Kerberos 5 error: %s\r\n",strTmp);
  4573.         krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  4574.         makestr(&krb5_errmsg,strTmp);
  4575.         return(0);
  4576.     }
  4577.  
  4578.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  4579.     debug(F110,"k5_auth_send TGT",tgt,0);
  4580.     if ( krb5_autoget &&
  4581.          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  4582.           (ck_krb5_is_tgt_valid() > 0)) )
  4583.         ck_krb5_autoget_TGT(realm);
  4584.  
  4585.     r = k5_get_ccache(k5_context,&ccache,NULL);
  4586.     if ( r ) {
  4587.         com_err(NULL, r, "while authorizing (0).");
  4588.         krb5_errno = r;
  4589.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4590.         return(0);
  4591.     }
  4592.  
  4593. #ifndef HEIMDAL
  4594.     memset((char *)&creds, 0, sizeof(creds));
  4595.     if (r = krb5_sname_to_principal(k5_context, szHostName,
  4596.                                 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4597.                                 KRB5_NT_SRV_HST, &creds.server)) {
  4598.         com_err(NULL, r, "while authorizing (1).");
  4599.         krb5_errno = r;
  4600.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4601.         return(0);
  4602.     }
  4603.  
  4604.     if (forward_flag) {
  4605.         if (fwd_server) {
  4606.             krb5_free_principal(k5_context,fwd_server);
  4607.             fwd_server = NULL;
  4608.         }
  4609.         krb5_copy_principal(k5_context,creds.server,&fwd_server);
  4610.     }
  4611.  
  4612.     if (r = krb5_cc_get_principal(k5_context, ccache, &creds.client)) {
  4613.         com_err(NULL, r, "while authorizing (2).");
  4614.         krb5_free_cred_contents(k5_context, &creds);
  4615.         krb5_errno = r;
  4616.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4617.         return(0);
  4618.     }
  4619.  
  4620.     if (szUserName[0] == '\0') {                /* Get user name now */
  4621.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4622.         if ( len < sizeof(szUserName) ) {
  4623.             memcpy(szUserName,
  4624.                     krb5_princ_component(k5_context, creds.client, 0)->data,
  4625.                     len);                       /* safe */
  4626.         } else
  4627.             len = 0;
  4628.         szUserName[len] = '\0';
  4629.     } else {
  4630.         char * name = NULL;
  4631.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4632.         if ( len == strlen(szUserName) ) {
  4633.             name = krb5_princ_component(k5_context, creds.client, 0)->data;
  4634. #ifdef OS2
  4635.             if ( !strnicmp(szUserName,name,len) )
  4636.                 memcpy(szUserName,name,len);    /* safe */
  4637. #endif /* OS2 */
  4638.         }
  4639.     }
  4640.  
  4641.     /* Not sure if this is necessary anymore.  What impact does it have
  4642.      * on Win2000 TGTs that use DES_CBC_MD5 or RC4_HMAC?
  4643.      *
  4644.      * This prevents using 3DES Service Tickets.
  4645.      */
  4646.     creds.keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  4647.     if (r = krb5_get_credentials(k5_context, 0,
  4648.                                   ccache, &creds, &new_creds)) {
  4649.         com_err(NULL, r, "while authorizing (3).");
  4650.         krb5_free_cred_contents(k5_context, &creds);
  4651.         krb5_errno = r;
  4652.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4653.         return(0);
  4654.     }
  4655. #endif /* HEIMDAL */
  4656.  
  4657.     if (auth_context) {
  4658.         krb5_auth_con_free(k5_context, auth_context);
  4659.         auth_context = 0;
  4660.     }
  4661.     if (r = krb5_auth_con_init(k5_context, &auth_context)) {
  4662.         com_err(NULL, r, "while initializing auth context");
  4663.         krb5_errno = r;
  4664.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4665.         return(0);
  4666.     }
  4667.  
  4668.     /* UPDATE for START_TLS.  AUTH_ENCRYPT_START_TLS and inclusion of */
  4669.     /* client and then server finished messages.                      */
  4670.  
  4671.     type_check[0] = AUTHTYPE_KERBEROS_V5;
  4672.     type_check[1] = AUTH_CLIENT_TO_SERVER |
  4673.         (how ? AUTH_HOW_MUTUAL : AUTH_HOW_ONE_WAY) |
  4674.         (encrypt) |
  4675.         (forward ? INI_CRED_FWD_ON : INI_CRED_FWD_OFF);
  4676. #ifdef CK_SSL
  4677.     if (encrypt == AUTH_ENCRYPT_START_TLS) {
  4678.         ssl_get_client_finished(&type_check[2],12);
  4679.         ssl_get_server_finished(&type_check[14],12);
  4680.     }
  4681. #endif /* CK_SSL */
  4682.  
  4683. #ifndef HEIMDAL
  4684.     checksum.magic = KV5M_DATA;
  4685. #endif /* HEIMDAL */
  4686.     checksum.length =
  4687. #ifdef CK_SSL
  4688.         (encrypt == AUTH_ENCRYPT_START_TLS) ? 26 :
  4689. #endif /* CK_SSL */
  4690.         2;
  4691.     checksum.data = (char *)&type_check;
  4692.  
  4693.     ap_opts = 0;
  4694.     if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
  4695.         ap_opts |= AP_OPTS_MUTUAL_REQUIRED;
  4696.  
  4697. #ifdef HEIMDAL
  4698.     r = krb5_auth_setkeytype(k5_context, auth_context, KEYTYPE_DES);
  4699.     if (r)
  4700.         com_err(NULL, r, "while setting auth keytype");
  4701.     r = krb5_auth_con_setaddrs_from_fd(k5_context,auth_context, &ttyfd);
  4702.     if (r)
  4703.         com_err(NULL, r, "while setting auth addrs");
  4704.     r = krb5_mk_req(k5_context, &auth_context, ap_opts,
  4705.                     krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4706.                     szHostName, &checksum, ccache, &k5_auth);
  4707.     if (r)
  4708.         com_err(NULL, r, "while making request");
  4709. #else /* HEIMDAL */
  4710.     auth_flags = KRB5_AUTH_CONTEXT_RET_TIME;
  4711. #ifdef CK_ENCRYPTION
  4712.     ap_opts |= AP_OPTS_USE_SUBKEY;
  4713. #endif /* CK_ENCRYPTION */
  4714. #ifdef TLS_VERIFY
  4715.     if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  4716.         auth_flags |= KRB5_AUTH_CONTEXT_DO_SEQUENCE;
  4717.         if (!krb5_d_no_addresses)
  4718.             r = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  4719.                                  KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  4720.     }
  4721. #endif /* CK_SSL */
  4722.     krb5_auth_con_setflags(k5_context, auth_context, auth_flags);
  4723.     r = krb5_mk_req_extended(k5_context, &auth_context, ap_opts,
  4724.                               &checksum, new_creds, &k5_auth);
  4725. #endif /* HEIMDAL */
  4726.  
  4727. #ifdef CK_ENCRYPTION
  4728.     if (!r) {
  4729.         r = krb5_auth_con_getlocalsubkey(k5_context, auth_context, &newkey);
  4730.         if (r)
  4731.             r = krb5_auth_con_getkey(k5_context, auth_context, &newkey);
  4732.  
  4733.         if (k5_session_key) {
  4734.             krb5_free_keyblock(k5_context, k5_session_key);
  4735.             k5_session_key = 0;
  4736.         }
  4737.     }
  4738.     if (newkey) {
  4739.         /*
  4740.         * keep the key in our private storage, but don't use it
  4741.         * yet---see kerberos5_reply() below
  4742.         */
  4743. #ifdef HEIMDAL
  4744.         if ((newkey->keytype == ETYPE_DES_CBC_CRC) ||
  4745.              (newkey->keytype == ETYPE_DES_CBC_MD5) ||
  4746.              (newkey->keytype == ETYPE_DES_CBC_MD4))
  4747.         {
  4748.             debug(F111,"k5_auth_send()","newkey->keytype",newkey->keytype);
  4749.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4750.         }
  4751. #else /* HEIMDAL */
  4752.         /* look for all possible DES keys first - just for compatibility */
  4753.         /* other key types are much less likely to be available          */
  4754.         if ((newkey->enctype == ENCTYPE_DES_CBC_CRC) ||
  4755.              (newkey->enctype == ENCTYPE_DES_CBC_MD5) ||
  4756.              (newkey->enctype == ENCTYPE_DES_CBC_MD4))
  4757.         {
  4758.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4759.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4760.         }
  4761.         else if ((new_creds->keyblock.enctype == ENCTYPE_DES_CBC_CRC) ||
  4762.                  (new_creds->keyblock.enctype == ENCTYPE_DES_CBC_MD5))
  4763.         {
  4764.             /* use the session key in credentials instead */
  4765.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4766.                    new_creds->keyblock.enctype);
  4767.             krb5_copy_keyblock(k5_context,
  4768.                                 &new_creds->keyblock, &k5_session_key);
  4769.         }
  4770.         else if (newkey->enctype != 0)
  4771.         {
  4772.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4773.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4774.         }
  4775.         else if (new_creds->keyblock.enctype != 0)
  4776.         {
  4777.             /* use the session key in credentials instead */
  4778.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4779.                    new_creds->keyblock.enctype);
  4780.             krb5_copy_keyblock(k5_context,
  4781.                                 &new_creds->keyblock, &k5_session_key);
  4782.         }
  4783.         else {
  4784.             debug(F110,"k5_auth_send()","NO KEY in newkey",0);
  4785.         }
  4786. #endif /* HEIMDAL */
  4787.         krb5_free_keyblock(k5_context, newkey);
  4788.     }
  4789. #endif /* CK_ENCRYPTION */
  4790. #ifndef HEIMDAL
  4791.     krb5_free_cred_contents(k5_context, &creds);
  4792.     krb5_free_creds(k5_context, new_creds);
  4793. #endif /* HEIMDAL */
  4794.     krb5_cc_close(k5_context,ccache);
  4795.  
  4796.     if (r) {
  4797.         com_err(NULL, r, "while authorizing (4).");
  4798.         krb5_errno = r;
  4799.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4800.         return(0);
  4801.     }
  4802.     krb5_errno = 0;
  4803.     makestr(&krb5_errmsg,"OK");
  4804.     return(1);
  4805. }
  4806.  
  4807. /*
  4808.  * K5_auth_reply -- checks the reply for mutual authentication.
  4809.  */
  4810. static int
  4811. #ifdef CK_ANSIC
  4812. k5_auth_reply(int how, unsigned char *data, int cnt)
  4813. #else
  4814. k5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  4815. #endif
  4816. {
  4817. #ifdef CK_ENCRYPTION
  4818.     Session_Key skey;
  4819. #endif /* CK_ENCRYPTION */
  4820.  
  4821.     data += 4;                                  /* Point to status byte */
  4822.     cnt -=5;
  4823.  
  4824.     switch (*data++) {
  4825.     case KRB_REJECT:
  4826.         if (cnt > 0) {
  4827.             char *s;
  4828.             int len;
  4829.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication because\r\n",
  4830.                       sizeof(strTmp));
  4831.             len = strlen(strTmp);
  4832.             if ( len + cnt < sizeof(strTmp) ) {
  4833.                 s = strTmp + strlen(strTmp);
  4834.                 memcpy(s, data, cnt);           /* safe */
  4835.                 s[cnt] = 0;
  4836.             }
  4837.         } else
  4838.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication",
  4839.                       sizeof(strTmp));
  4840.         krb5_errno = -1;
  4841.         makestr(&krb5_errmsg,strTmp);
  4842.         printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4843.         auth_finished(AUTH_REJECT);
  4844.         return AUTH_FAILURE;
  4845.  
  4846.     case KRB_ACCEPT:
  4847.         if (!mutual_complete) {
  4848.             if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL && !mutual_complete) {
  4849.                 ckstrncpy(strTmp,
  4850.                           "Kerberos V5 accepted you, but didn't provide"
  4851.                           " mutual authentication",sizeof(strTmp));
  4852.                 printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4853.                 krb5_errno = -1;
  4854.                 makestr(&krb5_errmsg,strTmp);
  4855.                 auth_finished(AUTH_REJECT);
  4856.                 return AUTH_FAILURE;
  4857.             }
  4858.  
  4859. #ifdef CK_ENCRYPTION
  4860.             if (k5_session_key) {
  4861.                 /* Even if the session key is 3DES, we must lie because otherwise */
  4862.                 /* we can't negotiate the proper keys for DES encryption if the   */
  4863.                 /* since the host may be requiring SK_DES key choice.             */
  4864.                 skey.type = SK_DES;
  4865.                 skey.length = 8;
  4866. #ifdef HEIMDAL
  4867.                 skey.data = k5_session_key->keyvalue.data;
  4868. #else /* HEIMDAL */
  4869.                 skey.data = k5_session_key->contents;
  4870. #endif /* HEIMDAL */
  4871.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  4872.             }
  4873. #endif /* CK_ENCRYPTION */
  4874.         }
  4875.         if ( cnt > 0 ) {
  4876.             char *s;
  4877.             int len;
  4878.             ckstrncpy(strTmp,"Kerberos V5 accepts you as ",sizeof(strTmp));
  4879.             len = strlen(strTmp);
  4880.             if ( len + cnt < sizeof(strTmp) ) {
  4881.                 s = strTmp + strlen(strTmp);
  4882.                 memcpy(s,data,cnt);
  4883.                 s[cnt] = 0;
  4884.             }
  4885.         }
  4886.         accept_complete = 1;
  4887.         printf("%s\r\n",strTmp);
  4888.  
  4889. #ifdef FORWARD
  4890.         if (forward_flag
  4891. #ifdef COMMENT
  4892.              /* Marc Horowitz <marc@mit.edu> has successfully argued
  4893.                 that it is indeed safe to send Forwarded credentials
  4894.                 to an untrusted host.
  4895.               */
  4896.              && (auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL
  4897. #endif /* COMMENT */
  4898.              )
  4899.             kerberos5_forward();
  4900. #endif /* FORWARD */
  4901.         krb5_errno = 0;
  4902.         makestr(&krb5_errmsg,strTmp);
  4903.         auth_finished(AUTH_USER);
  4904.         return AUTH_SUCCESS;
  4905.  
  4906.     case KRB5_RESPONSE:
  4907. #ifdef TLS_VERIFY
  4908.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  4909.             !krb5_tls_verified) {
  4910.             printf(
  4911.     "Man in the middle attack detected.  Session terminated.\r\n");
  4912. #ifndef BETATEST
  4913.             netclos();
  4914. #endif /* BETATEST */
  4915.             krb5_errno = -1;
  4916.             makestr(&krb5_errmsg,"TLS not verified");
  4917.             auth_finished(AUTH_REJECT);
  4918.             return AUTH_FAILURE;
  4919.         }
  4920.         if((ssl_active_flag || tls_active_flag) &&
  4921.             (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  4922.             printf("TLS session parameters verified by Kerberos 5\r\n");
  4923.         }
  4924. #endif /* TLS_VERIFY */
  4925.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  4926.             /* the rest of the reply should contain a krb_ap_rep */
  4927.             krb5_ap_rep_enc_part *reply;
  4928.             krb5_data inbuf;
  4929.             krb5_error_code r;
  4930.  
  4931.             inbuf.length = cnt;
  4932.             inbuf.data = (char *)data;
  4933.  
  4934.             if (r = krb5_rd_rep(k5_context, auth_context, &inbuf, &reply)) {
  4935.                 com_err(NULL, r, "while authorizing. (5)");
  4936.                 krb5_errno = r;
  4937.                 makestr(&krb5_errmsg,error_message(krb5_errno));
  4938.                 auth_finished(AUTH_REJECT);
  4939.                 return AUTH_FAILURE;
  4940.             }
  4941.             krb5_free_ap_rep_enc_part(k5_context, reply);
  4942.  
  4943. #ifdef CK_ENCRYPTION
  4944.             if (encrypt_flag && k5_session_key) {
  4945.                 /* Even if the session key is 3DES, we must lie because otherwise */
  4946.                 /* we can't negotiate the proper keys for DES encryption if the   */
  4947.                 /* since the host may be requiring SK_DES key choice.             */
  4948.                 skey.type = SK_DES;
  4949.                 skey.length = 8;
  4950. #ifdef HEIMDAL
  4951.                 skey.data = k5_session_key->keyvalue.data;
  4952. #else /* HEIMDAL */
  4953.                 skey.data = k5_session_key->contents;
  4954. #endif /* HEIMDAL */
  4955.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  4956.             }
  4957. #endif /* ENCRYPTION */
  4958.             mutual_complete = 1;
  4959.         }
  4960.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  4961.                   sizeof(strTmp));
  4962.         krb5_errno = 0;
  4963.         makestr(&krb5_errmsg,strTmp);
  4964.         printf("%s\r\n",strTmp);
  4965.         auth_finished(AUTH_USER);
  4966.         return AUTH_SUCCESS;
  4967.  
  4968. #ifdef FORWARD
  4969.     case KRB5_FORWARD_ACCEPT:
  4970.         forwarded_tickets = 1;
  4971.         ckstrncpy(strTmp,"Remote machine has accepted forwarded credentials",
  4972.                   sizeof(strTmp));
  4973.         krb5_errno = 0;
  4974.         makestr(&krb5_errmsg,strTmp);
  4975.         printf("%s\r\n",strTmp);
  4976.         return AUTH_SUCCESS;
  4977.  
  4978.     case KRB5_FORWARD_REJECT:
  4979.         forwarded_tickets = 0;
  4980.         if (cnt > 0) {
  4981.             char *s;
  4982.             int len;
  4983.             len = ckstrncpy(strTmp,
  4984.                       "Kerberos V5 refuses forwarded credentials because ",
  4985.                        sizeof(strTmp));
  4986.             if ( len + cnt < sizeof(strTmp) ) {
  4987.                 s = strTmp + strlen(strTmp);
  4988.                 memcpy(s, data, cnt);
  4989.                 s[cnt] = 0;
  4990.             }
  4991.         } else
  4992.             ckstrncpy(strTmp, "Kerberos V5 refuses forwarded credentials",
  4993.                       sizeof(strTmp));
  4994.  
  4995.         printf("%s\r\n",strTmp);
  4996.         krb5_errno = -1;
  4997.         makestr(&krb5_errmsg,strTmp);
  4998.         return AUTH_SUCCESS;
  4999. #endif  /* FORWARD */
  5000.  
  5001. #ifdef TLS_VERIFY
  5002.     case KRB5_TLS_VERIFY:
  5003.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5004.             krb5_data reply, msg;
  5005.             char tls_verify[24];
  5006.             krb5_replay_data repdata;
  5007.             krb5_error_code r;
  5008.  
  5009.             ssl_get_server_finished(&tls_verify[0],12);
  5010.             ssl_get_client_finished(&tls_verify[12],12);
  5011.  
  5012.             reply.data = data;
  5013.             reply.length = cnt;
  5014.  
  5015.             if (!krb5_d_no_addresses)
  5016.                 krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5017.                                   KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  5018.  
  5019.             if (r = krb5_rd_safe(k5_context,auth_context,&reply,&msg,&repdata))
  5020.               {
  5021.                 com_err("", r, "decoding tls verifier");
  5022.                 krb5_errno = r;
  5023.                 makestr(&krb5_errmsg,"TLS verify failure");
  5024.                 auth_finished(AUTH_REJECT);
  5025.                 return(AUTH_FAILURE);
  5026.             }
  5027.             if ( msg.length == 24 && !memcmp(msg.data,tls_verify,24) )
  5028.                  krb5_tls_verified = 1;
  5029.             krb5_free_data_contents(k5_context,&msg);
  5030.             if (krb5_tls_verified)
  5031.                 return(AUTH_SUCCESS);
  5032.         }
  5033.         printf("Man in the middle attack detected.  Session terminated.\r\n");
  5034.         netclos();
  5035.         krb5_errno = -1;
  5036.         makestr(&krb5_errmsg,"TLS verify failure");
  5037.         auth_finished(AUTH_REJECT);
  5038.         return(AUTH_FAILURE);
  5039. #endif /* CK_SSL */
  5040.  
  5041.     default:
  5042.         krb5_errno = -1;
  5043.         makestr(&krb5_errmsg,"Unknown reply type");
  5044.         auth_finished(AUTH_REJECT);
  5045.         return AUTH_FAILURE;                        /* Unknown reply type */
  5046.     }
  5047. }
  5048.  
  5049. #ifdef FORWARD
  5050. /* Decode, decrypt and store the forwarded creds in the local ccache. */
  5051. /* Needed for KRB5_FORWARD                                            */
  5052. static krb5_error_code
  5053. rd_and_store_for_creds(context, auth_context, inbuf, client)
  5054.     krb5_context context;
  5055.     krb5_auth_context auth_context;
  5056.     krb5_data *inbuf;
  5057.     krb5_const_principal client;
  5058. {
  5059.     krb5_creds ** creds=NULL;
  5060.     krb5_error_code retval;
  5061.     krb5_ccache ccache=NULL;
  5062.  
  5063. #ifdef HEIMDAL
  5064.     /*
  5065.     Heimdal Telnetd creates the cache file at this point and sets
  5066.     the KRB5CCNAME environment variable.
  5067.  
  5068.     struct passwd *pwd;
  5069.     char ccname[1024];
  5070.  
  5071.     pwd = getpwnam(szUserNameRequested);
  5072.     if (pwd == NULL)
  5073.         break;
  5074.     snprintf(ccname, sizeof(ccname)-1, "FILE:/tmp/krb5cc_%u",pwd->pw_uid);
  5075.     retval = krb5_cc_resolve(context,ccname,&ccache);
  5076.  
  5077.     chown(ccname + 5, pwd->pw_uid, -1);
  5078.     */
  5079. #endif /* HEIMDAL */
  5080.  
  5081.     if (retval = k5_get_ccache(context,&ccache,NULL))
  5082.         return(retval);
  5083.  
  5084. #ifdef HEIMDAL
  5085.     if ((retval = krb5_cc_initialize(context, ccache, client)))
  5086.         return(retval);
  5087.  
  5088.     if ((retval = krb5_rd_cred(context, auth_context, ccache, inbuf)))
  5089.         return(retval);
  5090. #else /* HEIMDAL */
  5091.     if ((retval = krb5_rd_cred(context, auth_context, inbuf, &creds, NULL)))
  5092.         return(retval);
  5093.  
  5094.     if ((retval = krb5_cc_initialize(context, ccache, creds[0]->client)))
  5095.         goto cleanup;
  5096.  
  5097.     if ((retval = krb5_cc_store_cred(context, ccache, creds[0])))
  5098.         goto cleanup;
  5099.  
  5100.     if ((retval = krb5_cc_close(context, ccache)))
  5101.         goto cleanup;
  5102.  
  5103.   cleanup:
  5104.     krb5_free_tgt_creds(context, creds);
  5105. #endif /* HEIMDAL */
  5106.     return retval;
  5107. }
  5108. #endif /* FORWARD */
  5109.  
  5110. /*
  5111.  *
  5112.  * K5_auth_is.
  5113.  *
  5114.  */
  5115.  
  5116. static int
  5117. #ifdef CK_ANSIC
  5118. k5_auth_is(int how, unsigned char *data, int cnt)
  5119. #else
  5120. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  5121. #endif
  5122. {
  5123.     int r = 0;
  5124.     krb5_principal server;
  5125.     krb5_keyblock *newkey = NULL;
  5126.     krb5_data outbuf;
  5127.     char errbuf[128]="";
  5128.     char *getenv();
  5129. #ifndef HEIMDAL
  5130.     krb5_authenticator *authenticator;
  5131.     krb5_keytab keytabid = 0;
  5132. #endif /* HEIMDAL */
  5133.     krb5_data inbuf;
  5134. #ifdef CK_ENCRYPTION
  5135.     Session_Key skey;
  5136. #endif /* CK_ENCRYPTION */
  5137.     char princ[256]="";
  5138.     int len;
  5139.  
  5140.     data += 4;                                  /* Point to status byte */
  5141.     cnt -= 4;
  5142.  
  5143.     hexdump("k5_auth_is data",data,cnt);
  5144.     debug(F111,"k5_auth_is","how",how);
  5145.  
  5146.     if (cnt-- < 1) {
  5147.         auth_finished(AUTH_REJECT);
  5148.         return AUTH_FAILURE;
  5149.     }
  5150.     switch (*data++) {
  5151.     case KRB_AUTH:
  5152.         k5_auth.data = (char *)data;
  5153.         k5_auth.length = cnt;
  5154.  
  5155.         debug(F110,"k5_auth_is","KRB_AUTH",0);
  5156.         debug(F111,"k5_auth_is","auth_context",auth_context);
  5157.  
  5158.         if (!r && !auth_context) {
  5159.             r = krb5_auth_con_init(k5_context, &auth_context);
  5160.             debug(F111,"k5_auth_is","krb5_auth_con_init",r);
  5161.         }
  5162.  
  5163. #ifdef HEIMDAL
  5164.         if (!r)
  5165.             r = krb5_auth_con_setaddrs_from_fd(k5_context,auth_context,&ttyfd);
  5166.  
  5167.         if (!r)
  5168.             r = krb5_sock_to_principal(k5_context,0,"host",
  5169.                                        KRB5_NT_SRV_HST,&server);
  5170.  
  5171.         if (!r)
  5172. #else /* HEIMDAL */
  5173.         if (!r) {
  5174.             krb5_rcache rcache = NULL;
  5175.  
  5176.             r = krb5_auth_con_getrcache(k5_context, auth_context,
  5177.                                          &rcache);
  5178.             debug(F111,"k5_auth_is","krb5_auth_con_getrcache",r);
  5179.  
  5180.             if (!r && !rcache) {
  5181.                 /* Do not resolve server's principal name, we will check */
  5182.                 /* for validity after the krb5_rd_req() call.            */
  5183.                 r = krb5_sname_to_principal(k5_context, 0, 0,
  5184.                                              KRB5_NT_SRV_HST, &server);
  5185.                 debug(F111,"k5_auth_is","krb5_sname_to_principal",r);
  5186.  
  5187.                 if (!r) {
  5188.                     r = krb5_get_server_rcache(k5_context,
  5189.                         krb5_princ_component(k5_context, server, 0),
  5190.                                                 &rcache);
  5191.                     debug(F111,"k5_auth_is","krb5_get_server_rcache",r);
  5192.                     krb5_free_principal(k5_context, server);
  5193.                 }
  5194.             }
  5195.             if (!r) {
  5196.                 r = krb5_auth_con_setrcache(k5_context,
  5197.                                              auth_context, rcache);
  5198.                 debug(F111,"k5_auth_is","krb5_auth_con_setrcache",r);
  5199.             }
  5200.         }
  5201.         if (!r && k5_keytab) {
  5202.             r = krb5_kt_resolve(k5_context,
  5203.                                  k5_keytab, &keytabid);
  5204.             debug(F111,"k5_auth_is","krb5_kt_resolve",r);
  5205.         }
  5206. #endif /* HEIMDAL */
  5207.         if (!r) {
  5208.             r = krb5_rd_req(k5_context, &auth_context, &k5_auth,
  5209. #ifdef HEIMDAL
  5210.                              server, NULL, NULL,
  5211. #else /* HEIMDAL */
  5212.                              NULL, keytabid, NULL,
  5213. #endif /* HEIMDAL */
  5214.                              &k5_ticket);
  5215.             debug(F111,"k5_auth_is","krb5_rd_req",r);
  5216.         }
  5217.         if (r) {
  5218.             (void) ckstrncpy(errbuf, "krb5_rd_req failed: ",sizeof(errbuf));
  5219.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5220.             goto errout;
  5221.         }
  5222. #ifdef HEIMDAL
  5223.         krb5_free_principal(k5_context, server);
  5224.  
  5225.         {
  5226.             char type_check[26];
  5227.  
  5228.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5229.             /* client and then server finished messages. */
  5230.  
  5231.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5232.             type_check[1] = how;        /* not broken into parts */
  5233. #ifdef CK_SSL
  5234.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5235.                 ssl_get_client_finished(&type_check[2],12);
  5236.                 ssl_get_server_finished(&type_check[14],12);
  5237.                 hexdump("k5_auth_is type_check",type_check,26);
  5238.             }
  5239. #endif /* CK_SSL */
  5240.  
  5241.             r = krb5_verify_authenticator_checksum(k5_context,
  5242.                                                     auth_context,
  5243.                                                     type_check,
  5244. #ifdef CK_SSL
  5245.                 ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5246. #endif /* CK_SSL */
  5247.                                                     2);
  5248.         }
  5249. #else /* HEIMDAL */
  5250.         len = krb5_princ_component(k5_context,k5_ticket->server,0)->length;
  5251.         if (len < 256)
  5252.         {
  5253.             memcpy(princ,
  5254.                    krb5_princ_component(k5_context,k5_ticket->server,0)->data,
  5255.                    len);
  5256.             princ[len] = '\0';
  5257.         }
  5258.         if ( strcmp((krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME), princ) )
  5259.         {
  5260.             debug(F110,"k5_auth_is incorrect service name",princ,0);
  5261.             ckstrncpy(errbuf,"incorrect service name: ",sizeof(errbuf));
  5262.             ckstrncat(errbuf,krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5263.                      sizeof(errbuf));
  5264.             ckstrncat(errbuf," != ",sizeof(errbuf));
  5265.             ckstrncat(errbuf,princ,sizeof(errbuf));
  5266.             goto errout;
  5267.         }
  5268.  
  5269.         r = krb5_auth_con_getauthenticator(k5_context,
  5270.                                             auth_context,
  5271.                                             &authenticator);
  5272.         debug(F111,"k5_auth_is","krb5_auth_con_getauthenticator",r);
  5273.         if (r) {
  5274.             (void) ckstrncpy(errbuf,
  5275.                              "krb5_auth_con_getauthenticator failed: ",
  5276.                              sizeof(errbuf)
  5277.                              );
  5278.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5279.             goto errout;
  5280.         }
  5281.  
  5282.         if (authenticator->checksum) {
  5283.             char type_check[26];
  5284.             krb5_checksum *cksum = authenticator->checksum;
  5285.             krb5_keyblock *key;
  5286.  
  5287.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5288.             /* client and then server finished messages. */
  5289.  
  5290.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5291.             type_check[1] = how;        /* not broken into parts */
  5292. #ifdef CK_SSL
  5293.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5294.                 ssl_get_client_finished(&type_check[2],12);
  5295.                 ssl_get_server_finished(&type_check[14],12);
  5296.                 hexdump("k5_auth_is type_check",type_check,26);
  5297.             }
  5298. #endif /* CK_SSL */
  5299.  
  5300.             r = krb5_auth_con_getkey(k5_context, auth_context,
  5301.                                       &key);
  5302.             debug(F111,"k5_auth_is","krb5_auth_con_getkey",r);
  5303.             if (r) {
  5304.                 (void) ckstrncpy(errbuf, "krb5_auth_con_getkey failed: ",
  5305.                                   sizeof(errbuf));
  5306.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5307.                 goto errout;
  5308.             }
  5309.  
  5310.             r = krb5_verify_checksum(k5_context,
  5311.                                       cksum->checksum_type,
  5312.                                       cksum,
  5313.                                       &type_check,
  5314.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5315.                                       2,
  5316.                                       key->contents,
  5317.                                       key->length
  5318.                                       );
  5319.             debug(F111,"k5_auth_is","krb5_verify_checksum",r);
  5320.             if (r) {
  5321.                 (void) ckstrncpy(errbuf,
  5322.                                  "checksum verification failed: ",
  5323.                                  sizeof(errbuf)
  5324.                                  );
  5325.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5326.                 goto errout;
  5327.             }
  5328.             krb5_free_keyblock(k5_context, key);
  5329.         } else {
  5330.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT) {
  5331.                 (void) strcpy(errbuf,
  5332.                                "authenticator is missing required checksum");
  5333.                 goto errout;
  5334.             }
  5335.         }
  5336.  
  5337.         krb5_free_authenticator(k5_context, authenticator);
  5338. #endif /* HEIMDAL */
  5339.  
  5340. #ifdef TLS_VERIFY
  5341.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5342.             krb5_data in, msg;
  5343.             char tls_verify[24];
  5344.             krb5_replay_data repdata;
  5345.  
  5346.             ssl_get_server_finished(&tls_verify[0],12);
  5347.             ssl_get_client_finished(&tls_verify[12],12);
  5348.  
  5349.             in.data = tls_verify;
  5350.             in.length = 24;
  5351.  
  5352.             if (!krb5_d_no_addresses)
  5353.                 krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5354.                                    KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
  5355.             if (r = krb5_mk_safe(k5_context,auth_context,&in,&msg,&repdata)) {
  5356.                 com_err("", r, "encoding tls verifier");
  5357.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5358.                 goto errout;
  5359.             }
  5360.             SendK5AuthSB(KRB5_TLS_VERIFY, msg.data, msg.length);
  5361.             krb5_free_data_contents(k5_context,&msg);
  5362.         }
  5363. #endif /* CK_SSL */
  5364.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  5365.             /* do ap_rep stuff here */
  5366.             if ((r = krb5_mk_rep(k5_context,
  5367. #ifdef HEIMDAL
  5368.                                   &auth_context,
  5369. #else /* HEIMDAL */
  5370.                                   auth_context,
  5371. #endif /* HEIMDAL */
  5372.                                   &outbuf))) {
  5373.                 debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5374.                 (void) ckstrncpy(errbuf, "Make reply failed: ",sizeof(errbuf));
  5375.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5376.                 goto errout;
  5377.             }
  5378.             debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5379.  
  5380.             SendK5AuthSB(KRB5_RESPONSE, outbuf.data, outbuf.length);
  5381.             mutual_complete = 1;
  5382.         }
  5383.  
  5384. #ifdef HEIMDAL
  5385.         {
  5386.             char * name = NULL;
  5387.             if (krb5_unparse_name(k5_context, k5_ticket->client,
  5388.                                    &name))
  5389.             {
  5390.                 szUserNameAuthenticated[0] = '\0';
  5391.             } else {
  5392.                 ckstrncpy(szUserNameAuthenticated,UIDBUFLEN,name);
  5393.                 free(name);
  5394.             }
  5395.         }
  5396. #else /* HEIMDAL */
  5397.         if ( krb5_aname_to_localname(k5_context,
  5398.                                       k5_ticket->enc_part2->client,
  5399.                                       UIDBUFLEN,szUserNameAuthenticated) )
  5400.             szUserNameAuthenticated[0] = '\0';
  5401. #endif /* HEIMDAL */
  5402.  
  5403.         SendK5AuthSB(KRB_ACCEPT, szUserNameAuthenticated,
  5404.                       szUserNameAuthenticated[0] ? -1 : 0);
  5405.         accept_complete = 1;
  5406.         ckmakmsg(strTmp,sizeof(strTmp),
  5407.                  "Kerberos5 identifies him as ``",
  5408.                  szUserNameAuthenticated,"''",NULL);
  5409.         printf("%s\r\n",strTmp);
  5410.  
  5411.         if (szUserNameRequested[0] &&
  5412.             krb5_kuserok(k5_context,
  5413. #ifdef HEIMDAL
  5414.                           k5_ticket->client,
  5415. #else /* HEIMDAL */
  5416.                           k5_ticket->enc_part2->client,
  5417. #endif /* HEIMDAL */
  5418.                           szUserNameRequested))
  5419.             auth_finished(AUTH_VALID);
  5420.         else
  5421.             auth_finished(AUTH_USER);
  5422.  
  5423.         krb5_auth_con_getremotesubkey(k5_context, auth_context,
  5424.                                        &newkey);
  5425.         if (k5_session_key) {
  5426.             krb5_free_keyblock(k5_context, k5_session_key);
  5427.             k5_session_key = 0;
  5428.         }
  5429.         if (newkey) {
  5430.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  5431.             krb5_free_keyblock(k5_context, newkey);
  5432.         } else {
  5433.             krb5_copy_keyblock(k5_context,
  5434. #ifdef HEIMDAL
  5435.                                 &k5_ticket->ticket.key,
  5436. #else /* HEIMDAL */
  5437.                                 k5_ticket->enc_part2->session,
  5438. #endif /* HEIMDAL */
  5439.                                 &k5_session_key);
  5440.         }
  5441.  
  5442. #ifdef CK_ENCRYPTION
  5443. #ifdef HEIMDAL
  5444.         skey.type = k5_session_key->keyvalue.length == 8 ? SK_DES : SK_GENERIC;
  5445.         skey.length = k5_session_key->keyvalue.length;
  5446.         skey.data = k5_session_key->keyvalue.data;
  5447. #else /* HEIMDAL */
  5448.         skey.type = k5_session_key->length == 8 ? SK_DES : SK_GENERIC;
  5449.         skey.length = k5_session_key->length;
  5450.         skey.data = k5_session_key->contents;
  5451. #endif /* HEIMDAL */
  5452.         encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  5453. #endif /* CK_ENCRYPTION */
  5454.         debug(F100,"k5_auth_is AUTH_SUCCESS","",0);
  5455.         krb5_errno = r;
  5456.         if ( krb5_errno )
  5457.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5458.         else
  5459.             makestr(&krb5_errmsg,strTmp);
  5460.         return AUTH_SUCCESS;
  5461.  
  5462. #ifdef FORWARD
  5463.     case KRB5_FORWARD:
  5464.         if ( !forward_flag ) {
  5465.             SendK5AuthSB(KRB5_FORWARD_REJECT,
  5466.                           "forwarded credentials are being refused.",
  5467.                           -1);
  5468.             return(AUTH_SUCCESS);
  5469.         }
  5470.  
  5471.         inbuf.length = cnt;
  5472.         inbuf.data = (char *)data;
  5473.         if (
  5474. #ifndef HEIMDAL
  5475.             (!krb5_d_no_addresses &&
  5476.             (r = krb5_auth_con_genaddrs(k5_context,auth_context,g_kstream->fd,
  5477.                               KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))) ||
  5478. #endif /* HEIMDAL */
  5479.             (r = rd_and_store_for_creds(k5_context, auth_context,&inbuf,
  5480. #ifdef HEIMDAL
  5481.                                          k5_ticket->client
  5482. #else /* HEIMDAL */
  5483.                                          k5_ticket->enc_part2->client
  5484. #endif /* HEIMDAL */
  5485.                                          ))) {
  5486.             (void) ckstrncpy(errbuf, "Read forwarded creds failed: ",
  5487.                               sizeof(errbuf));
  5488.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5489.             SendK5AuthSB(KRB5_FORWARD_REJECT, errbuf, -1);
  5490.             printf("Could not read forwarded credentials\r\n");
  5491.             krb5_errno = r;
  5492.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5493.         }
  5494.         else {
  5495.             SendK5AuthSB(KRB5_FORWARD_ACCEPT, 0, 0);
  5496.             ckstrncpy(strTmp,"Forwarded credentials obtained",sizeof(strTmp));
  5497.             printf("%s\r\n",strTmp);
  5498.             krb5_errno = 0;
  5499.             makestr(&krb5_errmsg,strTmp);
  5500.         }
  5501.         /* A failure to accept forwarded credentials is not an */
  5502.         /* authentication failure.                             */
  5503.         return AUTH_SUCCESS;
  5504. #endif  /* FORWARD */
  5505.     default:
  5506.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  5507.         SendK5AuthSB(KRB_REJECT, 0, 0);
  5508.         break;
  5509.     }
  5510.     auth_finished(AUTH_REJECT);
  5511.     return AUTH_FAILURE;
  5512.  
  5513.   errout:
  5514.     SendK5AuthSB(KRB_REJECT, errbuf, -1);
  5515.     krb5_errno = r;
  5516.     makestr(&krb5_errmsg,errbuf);
  5517.     printf("%s\r\n", errbuf);
  5518.     if (auth_context) {
  5519.         krb5_auth_con_free(k5_context, auth_context);
  5520.         auth_context = 0;
  5521.     }
  5522.     auth_finished(AUTH_REJECT);
  5523.     return AUTH_FAILURE;
  5524. }
  5525.  
  5526. #ifdef FORWARD
  5527. int
  5528. #ifdef CK_ANSIC
  5529. kerberos5_forward(void)
  5530. #else
  5531. kerberos5_forward()
  5532. #endif
  5533. {
  5534.     krb5_error_code r;
  5535.     krb5_ccache ccache=NULL;
  5536.     krb5_principal client = 0;
  5537.     krb5_principal server = 0;
  5538.     krb5_data forw_creds;
  5539. #ifdef HEIMDAL
  5540.     krb5_creds      creds;
  5541. #endif /* HEIMDAL */
  5542.  
  5543.     forw_creds.data = 0;
  5544.  
  5545.     r = k5_get_ccache(k5_context,&ccache,NULL);
  5546.     if ( r ) {
  5547.         com_err(NULL, r, "Kerberos V5: could not get default ccache");
  5548.         krb5_errno = r;
  5549.         makestr(&krb5_errmsg,error_message(krb5_errno));
  5550.         return(AUTH_FAILURE);
  5551.     }
  5552.  
  5553.     if ((r = krb5_cc_get_principal(k5_context, ccache, &client))) {
  5554.         com_err(NULL, r, "Kerberos V5: could not get default principal");
  5555.         goto cleanup;
  5556.     }
  5557.  
  5558. #ifdef HEIMDAL
  5559.     memset(&creds, 0, sizeof(creds));
  5560.     creds.client = client;
  5561.  
  5562.     if (r = krb5_build_principal(k5_context,
  5563.                              &creds.server,
  5564.                              strlen(client->realm),
  5565.                               client->realm,
  5566.                               "krbtgt",
  5567.                               client->realm,
  5568.                                   NULL)) {
  5569.         com_err(NULL, r, "Kerberos V5: could not get principal");
  5570.         goto cleanup;
  5571.     }
  5572.  
  5573.     creds.times.endtime = 0;
  5574.  
  5575.     if (r = krb5_get_forwarded_creds(k5_context,
  5576.                                       auth_context,
  5577.                                       ccache,
  5578.                                       0,
  5579.                                       szHostName,
  5580.                                       &creds,
  5581.                                       &forw_creds)) {
  5582.         com_err(NULL, r, "Kerberos V5: error getting forwarded creds");
  5583.         goto cleanup;
  5584.     }
  5585. #else /* HEIMDAL */
  5586.     /* we should not need to make this call since we are storing the */
  5587.     /* server's principal in fwd_server from our call to             */
  5588.     /* krb5_sname_to_principal() in k5_auth_send()                   */
  5589.     if (fwd_server == NULL) {
  5590.         if ((r = krb5_sname_to_principal(k5_context, szHostName,
  5591.                                  krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5592.                                           KRB5_NT_SRV_HST, &server))) {
  5593.             com_err(NULL, r, "Kerberos V5: could not make server principal");
  5594.             goto cleanup;
  5595.         }
  5596.     }
  5597.  
  5598.     if (!krb5_d_no_addresses &&
  5599.         (r = krb5_auth_con_genaddrs(k5_context, auth_context, g_kstream->fd,
  5600.                              KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
  5601.     {
  5602.         com_err(NULL, r, "Kerberos V5: could not gen local full address");
  5603.         goto cleanup;
  5604.     }
  5605.  
  5606.     if (r = krb5_fwd_tgt_creds(k5_context, auth_context, 0, client,
  5607.                                 fwd_server ? fwd_server : server,
  5608.                                 ccache, forwardable_flag, &forw_creds)) {
  5609.         com_err(NULL, r, "Kerberos V5: error getting forwardable credentials");
  5610.         goto cleanup;
  5611.     }
  5612. #endif /* HEIMDAL */
  5613.  
  5614.     /* Send forwarded credentials */
  5615.     if (!SendK5AuthSB(KRB5_FORWARD, forw_creds.data, forw_creds.length)) {
  5616.         printf("Kerberos V5 forwarding error!\r\n%s\r\n",
  5617.                     "Not enough room for authentication data");
  5618.     }
  5619.  
  5620. cleanup:
  5621.     if (client)
  5622.         krb5_free_principal(k5_context, client);
  5623.     if (server)
  5624.         krb5_free_principal(k5_context, server);
  5625. #ifdef HEIMDAL
  5626.     krb5_data_free(&forw_creds);
  5627. #else /* HEIMDAL */
  5628.     krb5_free_data_contents(k5_context,&forw_creds);
  5629. #endif /* HEIMDAL */
  5630.     krb5_cc_close(k5_context, ccache);
  5631.  
  5632.     krb5_errno = r;
  5633.     makestr(&krb5_errmsg,krb5_errno?error_message(krb5_errno):"OK");
  5634.     return(r?AUTH_FAILURE:AUTH_SUCCESS);
  5635. }
  5636. #endif /* FORWARD */
  5637. #else /* KRB5 */
  5638. int
  5639. ck_krb5_autoget_TGT(char * dummy)
  5640. {
  5641.     return(0);
  5642. }
  5643. #ifdef CK_KERBEROS
  5644. int
  5645. #ifdef CK_ANSIC
  5646. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  5647.                  struct krb4_init_data * k4_init)
  5648. #else
  5649. ck_krb5_initTGT(op,init,k4_init)
  5650.     krb_op_data * op; struct krb5_init_data * init;
  5651.     struct krb4_init_data * k4_init;
  5652. #endif /* CK_ANSIC*/
  5653. {
  5654.     return(-1);
  5655. }
  5656.  
  5657. int
  5658. #ifdef CK_ANSIC
  5659. ck_krb5_destroy(struct krb_op_data * op)
  5660. #else
  5661. ck_krb5_destroy(op) struct krb_op_data * op;
  5662. #endif
  5663. {
  5664.     return(-1);
  5665. }
  5666.  
  5667. int
  5668. #ifdef CK_ANSIC
  5669. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  5670. #else
  5671. ck_krb5_list_creds(op,lc)
  5672.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  5673. #endif
  5674. {
  5675.     return(-1);
  5676. }
  5677. #else /* CK_KERBEROS */
  5678. int
  5679. #ifdef CK_ANSIC
  5680. ck_krb5_initTGT(void * op, void * init, void * k4_init )
  5681. #else
  5682. ck_krb5_initTGT(op,init,k4_init)
  5683.     void * op; void * init; void * k4_init;
  5684. #endif /* CK_ANSIC*/
  5685. {
  5686.     return(-1);
  5687. }
  5688.  
  5689. int
  5690. #ifdef CK_ANSIC
  5691. ck_krb5_destroy(void * op)
  5692. #else
  5693. ck_krb5_destroy(op) void * op;
  5694. #endif
  5695. {
  5696.     return(-1);
  5697. }
  5698.  
  5699. int
  5700. #ifdef CK_ANSIC
  5701. ck_krb5_list_creds(void * op, void * lc)
  5702. #else
  5703. ck_krb5_list_creds(op,lc)
  5704.     void * op; void * lc;
  5705. #endif
  5706. {
  5707.     return(-1);
  5708. }
  5709. #endif /* CK_KERBEROS */
  5710. #endif /* KRB5 */
  5711.  
  5712. #ifdef GSSAPI_KRB5
  5713. /*
  5714.  *
  5715.  * gssk5_auth_send - gets authentication bits we need to send to KDC.
  5716.  *
  5717.  * Result is left in k5_auth
  5718.  *
  5719.  * Returns: 0 on failure, 1 on success
  5720.  *
  5721.  */
  5722.  
  5723. static int
  5724. #ifdef CK_ANSIC
  5725. gssk5_auth_send(int how, int encrypt, int forward)
  5726. #else
  5727. gssk5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  5728. #endif
  5729. {
  5730.     OM_uint32 maj_stat, min_stat;
  5731. #ifdef KRB5
  5732.     char * realm = NULL;
  5733.     char tgt[256];
  5734. #endif /* KRB5 */
  5735.  
  5736.     gss_chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  5737.     gss_chan.initiator_address.length = 4;
  5738.     gss_chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  5739.     gss_chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  5740.     gss_chan.acceptor_address.length = 4;
  5741.     gss_chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  5742.     gss_chan.application_data.length = 0;
  5743.     gss_chan.application_data.value = 0;
  5744.  
  5745. #ifdef KRB5
  5746.     realm = ck_krb5_realmofhost(ftp_host);
  5747.     if (realm) {
  5748.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  5749.         debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  5750.         if ( krb5_autoget &&
  5751.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  5752.                 (ck_krb5_is_tgt_valid() > 0)) )
  5753.             ck_krb5_autoget_TGT(realm);
  5754.     }
  5755. #endif /* KRB5 */
  5756.  
  5757.     /* Blob from gss-client */
  5758.     /* host@hostname */
  5759.     /* the V5 GSSAPI binding canonicalizes this for us... */
  5760.     ckmakmsg(gss_stbuf,GSS_BUFSIZ,
  5761.              krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5762.              "@",
  5763.              szHostName,
  5764.               NULL
  5765.               );
  5766.     fprintf(stderr, "Authenticating to <%s>...\n", gss_stbuf);
  5767.     gss_send_tok.value = gss_stbuf;
  5768.     gss_send_tok.length = strlen(gss_stbuf);
  5769.     maj_stat = gss_import_name(&min_stat, &gss_send_tok,
  5770.                                 gss_nt_service_name,
  5771.                                 &gss_target_name
  5772.                                 );
  5773.     if (maj_stat != GSS_S_COMPLETE) {
  5774.         user_gss_error(maj_stat, min_stat, "parsing name");
  5775.         secure_error("name parsed <%s>\n", gss_stbuf);
  5776.         return(0);
  5777.     }
  5778.     token_ptr = GSS_C_NO_BUFFER;
  5779.     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  5780.  
  5781.     fprintf(stderr, "calling gss_init_sec_context\n");
  5782.     maj_stat =
  5783.         gss_init_sec_context(&min_stat,
  5784.                               GSS_C_NO_CREDENTIAL,
  5785.                               &gcontext,
  5786.                               gss_target_name,
  5787.                               gss_mech_krb5,
  5788.                               GSS_C_MUTUAL_FLAG |
  5789.                               GSS_C_REPLAY_FLAG |
  5790.                               ((forward && forward_flag) ?
  5791.                                 GSS_C_DELEG_FLAG : 0),
  5792.                               0,
  5793.                               (krb5_d_no_addresses ? /* channel bindings */
  5794.                                 GSS_C_NO_CHANNEL_BINDINGS :
  5795.                                 &gss_chan),
  5796.                               gss_token_ptr,
  5797.                               NULL,     /* ignore mech type */
  5798.                               &gss_send_tok,
  5799.                               NULL,     /* ignore ret_flags */
  5800.                               NULL
  5801.                               );        /* ignore time_rec */
  5802.  
  5803.  
  5804.         if (maj_stat != GSS_S_COMPLETE &&
  5805.              maj_stat != GSS_S_CONTINUE_NEEDED) {
  5806.             user_gss_error(maj_stat,
  5807.                             min_stat,
  5808.                             "initializing context"
  5809.                             );
  5810.             gss_release_name(&min_stat, &gss_target_name);
  5811.             return(0);
  5812.         }
  5813.         return(1);
  5814. }
  5815.  
  5816. /*
  5817.  * gssk5_auth_reply -- checks the reply for mutual authentication.
  5818.  */
  5819. static int
  5820. #ifdef CK_ANSIC
  5821. gssk5_auth_reply(int how, unsigned char *data, int cnt)
  5822. #else
  5823. gssk5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  5824. #endif
  5825. {
  5826.     data += 4;                                  /* Point to status byte */
  5827.     cnt -=5;
  5828.  
  5829.     switch (*data++) {
  5830.     case GSS_REJECT:
  5831.         if (cnt > 0) {
  5832.             char *s;
  5833.             int len;
  5834.             ckstrncpy(strTmp,"GSSAPI refuses authentication because\r\n",
  5835.                       sizeof(strTmp));
  5836.             len = strlen(strTmp);
  5837.             if ( len + cnt < sizeof(strTmp) ) {
  5838.                 s = strTmp + strlen(strTmp);
  5839.                 memcpy(s, data, cnt);           /* safe */
  5840.                 s[cnt] = 0;
  5841.             }
  5842.         } else
  5843.             ckstrncpy(strTmp,"GSSAPI refuses authentication",
  5844.                       sizeof(strTmp));
  5845.         printf("GSSAPI authentication failed!\r\n%s\r\n",strTmp);
  5846.         auth_finished(AUTH_REJECT);
  5847.         return AUTH_FAILURE;
  5848.  
  5849.     case GSS_ACCEPT:
  5850.         if ( cnt > 0 ) {
  5851.             char *s;
  5852.             int len;
  5853.             ckstrncpy(strTmp,"GSSAPI accepts you as ",sizeof(strTmp));
  5854.             len = strlen(strTmp);
  5855.             if ( len + cnt < sizeof(strTmp) ) {
  5856.                 s = strTmp + strlen(strTmp);
  5857.                 memcpy(s,data,cnt);
  5858.                 s[cnt] = 0;
  5859.             }
  5860.         }
  5861.         accept_complete = 1;
  5862.         printf("%s\r\n",strTmp);
  5863.         auth_finished(AUTH_USER);
  5864.         return AUTH_SUCCESS;
  5865.  
  5866.     case GSS_RESPONSE:
  5867.         gss_token_ptr = &gss_recv_tok;
  5868.         gss_recv_tok.value = data;
  5869.         gss_recv_tok.length = cnt;
  5870.  
  5871.         maj_stat =
  5872.             gss_init_sec_context(&min_stat,
  5873.                                   GSS_C_NO_CREDENTIAL,
  5874.                                   &gcontext,
  5875.                                   gss_target_name,
  5876.                                   gss_krb5_mech,
  5877.                                   GSS_C_MUTUAL_FLAG |
  5878.                                   GSS_C_REPLAY_FLAG |
  5879.                                   (forward_flag ?
  5880.                                     GSS_C_DELEG_FLAG : 0),
  5881.                                   0,
  5882.                                   (krb5_d_no_addresses ? /* channel bindings */
  5883.                                     GSS_C_NO_CHANNEL_BINDINGS :
  5884.                                     &gss_chan),
  5885.                                   gss_token_ptr,
  5886.                                   NULL, /* ignore mech type */
  5887.                                   &gss_send_tok,
  5888.                                   NULL, /* ignore ret_flags */
  5889.                                   NULL
  5890.                                   );    /* ignore time_rec */
  5891.  
  5892.         if ( maj_stat == GSS_S_COMPLETE )
  5893.         {
  5894.  
  5895.         } else if ( maj_stat == CSS_S_CONTINUE_NEEDED ) {
  5896.         } else {
  5897.         }
  5898.  
  5899.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  5900.                   sizeof(strTmp));
  5901.         printf("%s\r\n",strTmp);
  5902.         auth_finished(AUTH_USER);
  5903.         return AUTH_SUCCESS;
  5904.  
  5905.     default:
  5906.         auth_finished(AUTH_REJECT);
  5907.         return AUTH_FAILURE;                        /* Unknown reply type */
  5908.     }
  5909. }
  5910.  
  5911. /*
  5912.  *
  5913.  * gssk5_auth_is.
  5914.  *
  5915.  */
  5916.  
  5917. static int
  5918. #ifdef CK_ANSIC
  5919. k5_auth_is(int how, unsigned char *data, int cnt)
  5920. #else
  5921. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  5922. #endif
  5923. {
  5924.     int replied = 0;
  5925.     gss_cred_id_t server_creds, deleg_creds;
  5926.     gss_name_t client;
  5927.     int ret_flags;
  5928.     gss_buffer_desc name_buf;
  5929.     gss_name_t server_name;
  5930.     OM_uint32 acquire_maj,
  5931.       acquire_min,
  5932.       accept_maj,
  5933.       accept_min,
  5934.       stat_maj,
  5935.       stat_min;
  5936.     gss_OID mechid;
  5937.     gss_buffer_desc tok, out_tok;
  5938.     char gbuf[GSS_BUFSIZ];
  5939.     u_char gout_buf[GSS_BUFSIZ];
  5940.     char localname[MAXHOSTNAMELEN];
  5941.     char service_name[MAXHOSTNAMELEN+10];
  5942.     char **service;
  5943.     struct hostent *hp;
  5944.  
  5945.     data += 4;                                  /* Point to status byte */
  5946.     cnt -= 4;
  5947.  
  5948.     hexdump("gssk5_auth_is data",data,cnt);
  5949.     debug(F111,"gssk5_auth_is","how",how);
  5950.  
  5951.     if (cnt-- < 1) {
  5952.         auth_finished(AUTH_REJECT);
  5953.         return AUTH_FAILURE;
  5954.     }
  5955.     switch (*data++) {
  5956.     case GSS_AUTH:
  5957.         gss_chan.initiator_addrtype = GSS_C_AF_INET;
  5958.         gss_chan.initiator_address.length = 4;
  5959.         gss_chan.initiator_address.value = &his_addr.sin_addr.s_addr;
  5960.         gss_chan.acceptor_addrtype = GSS_C_AF_INET;
  5961.         gss_chan.acceptor_address.length = 4;
  5962.         gss_chan.acceptor_address.value = &ctrl_addr.sin_addr.s_addr;
  5963.         gss_chan.application_data.length = 0;
  5964.         gss_chan.application_data.value = 0;
  5965.  
  5966.         tok.value = data;
  5967.         tok.length = cnt;
  5968.  
  5969.         if (gethostname(localname, MAXHOSTNAMELEN)) {
  5970.             auth_finished(AUTH_REJECT);
  5971.             return AUTH_FAILURE;
  5972.         }
  5973.         if (!(hp = gethostbyname(localname))) {
  5974.             auth_finished(AUTH_REJECT);
  5975.             return AUTH_FAILURE;
  5976.         }
  5977. #ifdef HADDRLIST
  5978.         hp = ck_copyhostent(hp);
  5979. #endif /* HADDRLIST */
  5980.         strncpy(localname, hp->h_name, sizeof(localname) - 1);
  5981.         localname[sizeof(localname) - 1] = '\0';
  5982.  
  5983.         sprintf(service_name, "%s@%s", *service, localname);
  5984.         name_buf.value = service_name;
  5985.         name_buf.length = strlen(name_buf.value) + 1;
  5986.         stat_maj = gss_import_name(&stat_min, &name_buf,
  5987.                                     gss_nt_service_name,
  5988.                                     &server_name);
  5989.         if (stat_maj != GSS_S_COMPLETE) {
  5990.             auth_finished(AUTH_REJECT);
  5991.             return AUTH_FAILURE;
  5992.         }
  5993.  
  5994.         acquire_maj = gss_acquire_cred(&acquire_min, server_name, 0,
  5995.                                         GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
  5996.                                         &server_creds, NULL, NULL);
  5997.         (void) gss_release_name(&stat_min, &server_name);
  5998.  
  5999.         if (acquire_maj != GSS_S_COMPLETE) {
  6000.             reply_gss_error(535, accept_maj, accept_min,
  6001.                                  "accepting context");
  6002.             syslog(LOG_ERR, "failed accepting context");
  6003.             (void) gss_release_cred(&stat_min, &server_creds);
  6004.             if (ret_flags & GSS_C_DELEG_FLAG)
  6005.                 (void) gss_release_cred(&stat_min,
  6006.                                          &deleg_creds);
  6007.             return 0;
  6008.         }
  6009.  
  6010.         gcontext = GSS_C_NO_CONTEXT;
  6011.         accept_maj = gss_accept_sec_context(&accept_min,
  6012.                                             &gcontext, /* context_handle */
  6013.                                             /* verifier_cred_handle */
  6014.                                             server_creds,
  6015.                                             &tok, /* input_token */
  6016.                                              (krb5_d_no_addresses ? /* channel bindings */
  6017.                                                GSS_C_NO_CHANNEL_BINDINGS :
  6018.                                                &gss_chan),
  6019.                                              &client, /* src_name */
  6020.                                             &mechid, /* mech_type */
  6021.                                             &out_tok, /* output_token */
  6022.                                             &ret_flags,
  6023.                                             NULL,       /* ignore time_rec */
  6024.                                             /* forwarded credentials */
  6025.                                             &deleg_creds
  6026.                                             );
  6027.  
  6028.         if (accept_maj!=GSS_S_COMPLETE && accept_maj!=GSS_S_CONTINUE_NEEDED) {
  6029.             reply_gss_error(535, accept_maj, accept_min,
  6030.                              "accepting context");
  6031.             syslog(LOG_ERR, "failed accepting context");
  6032.             (void) gss_release_cred(&stat_min, &server_creds);
  6033.             if (ret_flags & GSS_C_DELEG_FLAG)
  6034.                 (void) gss_release_cred(&stat_min,
  6035.                                          &deleg_creds);
  6036.             return 0;
  6037.         }
  6038.  
  6039.         if (out_tok.length) {
  6040.             if (kerror = radix_encode(out_tok.value,gbuf,&out_tok.length, 0)) {
  6041.                 secure_error("Couldn't encode ADAT reply (%s)",
  6042.                              radix_error(kerror));
  6043.                 syslog(LOG_ERR, "couldn't encode ADAT reply");
  6044.                 (void) gss_release_cred(&stat_min, &server_creds);
  6045.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6046.                         (void) gss_release_cred(&stat_min,
  6047.                                                 &deleg_creds);
  6048.                 return(0);
  6049.             }
  6050.             if (stat_maj == GSS_S_COMPLETE) {
  6051.                 reply(235, "ADAT=%s", gbuf);
  6052.                 replied = 1;
  6053.             } else {
  6054.                 /* If the server accepts the security data, and
  6055.                    requires additional data, it should respond
  6056.                    with reply code 335. */
  6057.                 reply(335, "ADAT=%s", gbuf);
  6058.             }
  6059.             (void) gss_release_buffer(&stat_min, &out_tok);
  6060.         }
  6061.  
  6062.         if (stat_maj == GSS_S_COMPLETE) {
  6063.             /* GSSAPI authentication succeeded */
  6064.             stat_maj = gss_display_name(&stat_min, client,
  6065.                                          &client_name, &mechid);
  6066.             if (stat_maj != GSS_S_COMPLETE) {
  6067.                 /* "If the server rejects the security data (if
  6068.                    a checksum fails, for instance), it should
  6069.                    respond with reply code 535." */
  6070.                 reply_gss_error(535, stat_maj, stat_min,
  6071.                                 "extracting GSSAPI identity name");
  6072.                 syslog(LOG_ERR, "gssapi error extracting identity");
  6073.                 (void) gss_release_cred(&stat_min, &server_creds);
  6074.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6075.                         (void) gss_release_cred(&stat_min,
  6076.                                                 &deleg_creds);
  6077.                 return 0;
  6078.             }
  6079.             auth_type = temp_auth_type;
  6080.             temp_auth_type = NULL;
  6081.  
  6082.             (void) gss_release_cred(&stat_min, &server_creds);
  6083.             if (ret_flags & GSS_C_DELEG_FLAG) {
  6084.                 if (want_creds)
  6085.                     ftpd_gss_convert_creds(client_name.value,
  6086.                                             deleg_creds);
  6087.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6088.             }
  6089.  
  6090.             /* If the server accepts the security data, but does
  6091.                not require any additional data (i.e., the security
  6092.                data exchange has completed successfully), it must
  6093.                respond with reply code 235. */
  6094.             if (!replied)
  6095.             {
  6096.                 if (ret_flags & GSS_C_DELEG_FLAG && !have_creds)
  6097.                   reply(235,
  6098.  "GSSAPI Authentication succeeded, but could not accept forwarded credentials"
  6099.                         );
  6100.                 else
  6101.                   reply(235, "GSSAPI Authentication succeeded");
  6102.             }
  6103.             return(1);
  6104.         } else if (stat_maj == GSS_S_CONTINUE_NEEDED) {
  6105.             /* If the server accepts the security data, and
  6106.             requires additional data, it should respond with
  6107.             reply code 335. */
  6108.             reply(335, "more data needed");
  6109.             (void) gss_release_cred(&stat_min, &server_creds);
  6110.             if (ret_flags & GSS_C_DELEG_FLAG)
  6111.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6112.             return(0);
  6113.         } else {
  6114.             /* "If the server rejects the security data (if
  6115.             a checksum fails, for instance), it should
  6116.             respond with reply code 535." */
  6117.             reply_gss_error(535, stat_maj, stat_min,
  6118.                              "GSSAPI failed processing ADAT");
  6119.             syslog(LOG_ERR, "GSSAPI failed processing ADAT");
  6120.             (void) gss_release_cred(&stat_min, &server_creds);
  6121.             if (ret_flags & GSS_C_DELEG_FLAG)
  6122.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6123.             return(0);
  6124.         }
  6125.  
  6126.         debug(F100,"gssk5_auth_is AUTH_SUCCESS","",0);
  6127.         krb5_errno = r;
  6128.         if ( krb5_errno )
  6129.             makestr(&krb5_errmsg,error_message(krb5_errno));
  6130.         else
  6131.             makestr(&krb5_errmsg,strTmp);
  6132.         return AUTH_SUCCESS;
  6133.  
  6134.     default:
  6135.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  6136.         SendGSSK5AuthSB(GSS_REJECT, 0, 0);
  6137.         break;
  6138.     }
  6139.     auth_finished(AUTH_REJECT);
  6140.     return AUTH_FAILURE;
  6141. }
  6142. #endif /* GSSAPI_KRB5 */
  6143.  
  6144. #ifdef CK_SRP
  6145. /*
  6146.  * Copyright (c) 1997 Stanford University
  6147.  *
  6148.  * The use of this software for revenue-generating purposes may require a
  6149.  * license from the owners of the underlying intellectual property.
  6150.  * Specifically, the SRP-3 protocol may not be used for revenue-generating
  6151.  * purposes without a license.
  6152.  *
  6153.  * NOTE: Columbia University has a license.
  6154.  *
  6155.  * Within that constraint, permission to use, copy, modify, and distribute
  6156.  * this software and its documentation for any purpose is hereby granted
  6157.  * without fee, provided that the above copyright notices and this permission
  6158.  * notice appear in all copies of the software and related documentation.
  6159.  *
  6160.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  6161.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  6162.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  6163.  *
  6164.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  6165.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  6166.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  6167.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  6168.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  6169.  */
  6170.  
  6171. static void
  6172. srp_encode_length(data, num)
  6173.     unsigned char * data;
  6174.     int num;
  6175. {
  6176.     *data = (num >> 8) & 0xff;
  6177.     *++data = num & 0xff;
  6178. }
  6179.  
  6180. static int
  6181. srp_decode_length(data)
  6182.     unsigned char * data;
  6183. {
  6184.     return (((int) *data & 0xff) << 8) | (*(data + 1) & 0xff);
  6185. }
  6186.  
  6187. #ifdef PRE_SRP_1_7_3
  6188. static int
  6189. #ifdef CK_ANSIC
  6190. srp_reply(int how, unsigned char *data, int cnt)
  6191. #else
  6192. srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6193. #endif
  6194. {
  6195.     struct t_num n;
  6196.     struct t_num g;
  6197.     struct t_num s;
  6198.     struct t_num B;
  6199.     struct t_num * A;
  6200.     char hexbuf[MAXHEXPARAMLEN];
  6201.     char type_check[26];
  6202.     int pflag;
  6203.  
  6204. #ifdef CK_ENCRYPTION
  6205.     Session_Key skey;
  6206. #endif /* ENCRYPTION */
  6207.  
  6208.     char * str=NULL;
  6209.  
  6210.     data += 4;                          /* Point to status byte */
  6211.     cnt  -= 4;
  6212.  
  6213.     if(cnt-- < 1) {
  6214.         auth_finished(AUTH_REJECT);
  6215.         return AUTH_FAILURE;
  6216.     }
  6217.  
  6218.     switch(*data++) {
  6219.     case SRP_REJECT:
  6220.         ckmakmsg(strTmp,sizeof(strTmp),
  6221.                   "SRP refuses authentication for '",szUserName,
  6222.                   "'\r\n",NULL);
  6223.         if (cnt > 0) {
  6224.             int len = strlen(strTmp);
  6225.             if ( len + cnt < sizeof(strTmp) ) {
  6226.                 str = strTmp + strlen(strTmp);
  6227.                 memcpy(str,data,cnt);
  6228.                 str[cnt] = 0;
  6229.             }
  6230.         }
  6231.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6232.         if (tc != NULL) {
  6233.             t_clientclose(tc);
  6234.             tc = NULL;
  6235.         }
  6236.         auth_finished(AUTH_REJECT);
  6237.         return AUTH_FAILURE;
  6238.  
  6239.     case SRP_ACCEPT:
  6240.         if(cnt < RESPONSE_LEN || !srp_waitresp ||
  6241.             tc == NULL
  6242.             ) {
  6243.             printf("SRP Protocol error\r\n");
  6244.             return(auth_resend(AUTHTYPE_SRP));
  6245.         }
  6246.         srp_waitresp = 0;
  6247.  
  6248.         if(t_clientverify(tc, data) == 0) {
  6249.             printf("SRP accepts you as %s\r\n",szUserName);
  6250. #ifdef CK_SSL
  6251.             if((ssl_active_flag || tls_active_flag) &&
  6252.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6253.                 printf("TLS session parameters verified by SRP\r\n");
  6254.             } else
  6255. #endif /* CK_SSL */
  6256.  
  6257. #ifdef CK_ENCRYPTION
  6258.             {
  6259.                 skey.type = SK_GENERIC;
  6260.                 skey.length = SESSION_KEY_LEN;
  6261.                 skey.data = tc->session_key;
  6262.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6263.             }
  6264. #endif /* ENCRYPTION */
  6265.             t_clientclose(tc);
  6266.             tc = NULL;
  6267.             accept_complete = 1;
  6268.             auth_finished(AUTH_VALID);
  6269.             return AUTH_SUCCESS;
  6270.         } else {
  6271.             printf("SRP server authentication failed!\r\n");
  6272.             t_clientclose(tc);
  6273.             tc = NULL;
  6274.             return(auth_resend(AUTHTYPE_SRP));
  6275.         }
  6276.         break;
  6277.  
  6278.     case SRP_PARAMS:
  6279.         if(!szUserName) {
  6280.             printf("No username available\r\n");
  6281.             return(auth_resend(AUTHTYPE_SRP));
  6282.         }
  6283.  
  6284.         n.len = srp_decode_length(data);
  6285.         data += 2;
  6286.         cnt -= 2;
  6287.         if(n.len > cnt) {
  6288.             printf("n too long\r\n");
  6289.             return(auth_resend(AUTHTYPE_SRP));
  6290.         }
  6291.         n.data = data;
  6292.         data += n.len;
  6293.         cnt -= n.len;
  6294.  
  6295.         g.len = srp_decode_length(data);
  6296.         data += 2;
  6297.         cnt -= 2;
  6298.         if(g.len > cnt) {
  6299.             printf("g too long\r\n");
  6300.             return(auth_resend(AUTHTYPE_SRP));
  6301.         }
  6302.         g.data = data;
  6303.         data += g.len;
  6304.         cnt -= g.len;
  6305.  
  6306.         s.len = srp_decode_length(data);
  6307.         data += 2;
  6308.         cnt -= 2;
  6309.         if(s.len > cnt) {
  6310.             printf("salt too long\r\n");
  6311.             return(auth_resend(AUTHTYPE_SRP));
  6312.         }
  6313.         s.data = data;
  6314.         data += s.len;
  6315.         cnt -= s.len;
  6316.  
  6317.         /* If the parameters provided by the server cannot be
  6318.          * validated the following function will fail.
  6319.          */
  6320.         tc = t_clientopen(szUserName, &n, &g, &s);
  6321.         if (tc == NULL) {
  6322.             printf("SRP parameter initialization error\r\n");
  6323.             return(auth_resend(AUTHTYPE_SRP));
  6324.         }
  6325.         A = t_clientgenexp(tc);
  6326.         if(A == NULL) {
  6327.             printf("SRP protocol error\r\n");
  6328.             return(auth_resend(AUTHTYPE_SRP));
  6329.         }
  6330.         SendSRPAuthSB(SRP_EXP, A->data, A->len);
  6331.  
  6332.         if ( pwbuf[0] && pwflg ) {
  6333.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6334.                    8 * n.len,
  6335.                    szUserName
  6336.                    );
  6337.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6338. #ifdef OS2
  6339.             if ( pwcrypt )
  6340.                 ck_encrypt((char *)srp_passwd);
  6341. #endif /* OS2 */
  6342.         } else {
  6343.             extern char * srppwprompt;
  6344.             char preface[128];
  6345.             int ok;
  6346.  
  6347.             if (srppwprompt && srppwprompt[0] &&
  6348.                (strlen(srppwprompt) + strlen(szUserName) - 2) < sizeof(preface))
  6349.             {
  6350.                 sprintf(preface,srppwprompt,szUserName);
  6351.             } else {
  6352.                 ckmakxmsg( preface,sizeof(preface),
  6353.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6354.                           szUserName, "'\r\n", NULL, NULL, NULL, NULL, NULL,
  6355.                           NULL, NULL);
  6356.             }
  6357.             ok = uq_txt( preface,"Password: ",2,NULL,
  6358.                          srp_passwd,sizeof(srp_passwd)-1,NULL);
  6359.             if ( !ok )
  6360.                 srp_passwd[0] = '\0';
  6361.         }
  6362.  
  6363.         t_clientpasswd(tc, srp_passwd);
  6364.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6365.         return AUTH_SUCCESS;
  6366.  
  6367.     case SRP_CHALLENGE:
  6368.         if(tc == NULL) {
  6369.             printf("SRP protocol error\r\n");
  6370.             return(auth_resend(AUTHTYPE_SRP));
  6371.         }
  6372.  
  6373. #ifndef PRE_SRP_1_4_5
  6374.         /*
  6375.          * The original SRP AUTH implementation did not protect against
  6376.          * tampering of the auth-type-pairs.  Therefore, when the
  6377.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6378.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6379.          * is set we also insert the SSL/TLS client and server finished
  6380.          * messages to ensure that there is no man in the middle attack
  6381.          * underway on the SSL/TLS connection.
  6382.          */
  6383.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6384.             type_check[0] = AUTHTYPE_SRP;
  6385.             type_check[1] = how;
  6386. #ifdef CK_SSL
  6387.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6388.                 ssl_get_client_finished(&type_check[2],12);
  6389.                 ssl_get_server_finished(&type_check[14],12);
  6390.                 t_clientaddexdata(tc,type_check,26);
  6391.             } else
  6392. #endif /* CK_SSL */
  6393.                 t_clientaddexdata(tc,type_check,2);
  6394.         }
  6395. #endif /* PRE_SRP_1_4_5 */
  6396.  
  6397.         B.data = data;
  6398.         B.len = cnt;
  6399.         t_clientgetkey(tc, &B);
  6400.  
  6401.         SendSRPAuthSB(SRP_RESPONSE, t_clientresponse(tc), RESPONSE_LEN);
  6402.         srp_waitresp = 1;
  6403.         return AUTH_SUCCESS;
  6404.  
  6405.     default:
  6406.         return(auth_resend(AUTHTYPE_SRP));
  6407.     }
  6408.     return AUTH_FAILURE;
  6409. }
  6410.  
  6411. static int
  6412. #ifdef CK_ANSIC
  6413. srp_is(int how, unsigned char *data, int cnt)
  6414. #else
  6415. srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6416. #endif
  6417. {
  6418.     char pbuf[2 * MAXPARAMLEN + 5];
  6419.     char * ptr;
  6420.     char hexbuf[MAXHEXPARAMLEN];
  6421. #ifdef CK_ENCRYPTION
  6422.     Session_Key skey;
  6423. #endif
  6424.     struct t_num A;
  6425.     struct t_pw * tpw = NULL;
  6426.     struct t_conf * tconf = NULL;
  6427.     struct passwd * pass;
  6428.     static struct t_num * B = NULL;     /* Holder for B */
  6429. #ifdef CK_SSL
  6430.     char type_check[26];
  6431. #else
  6432.     char type_check[2];
  6433. #endif /* CK_SSL */
  6434.  
  6435.     if ((cnt -= 4) < 1) {
  6436.         auth_finished(AUTH_REJECT);
  6437.         return AUTH_FAILURE;
  6438.     }
  6439.  
  6440.     data += 4;
  6441.     cnt  -= 1;
  6442.     switch(*data++) {
  6443.     case SRP_AUTH:
  6444.         /* Send parameters back to client */
  6445.         if(ts != NULL) {
  6446.             t_serverclose(ts);
  6447.             ts = NULL;
  6448.         }
  6449.         if(!szUserNameRequested[0]) {
  6450.             if (1)
  6451.                 printf("No username available\r\n");
  6452.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6453.             auth_finished(AUTH_REJECT);
  6454.             return(AUTH_FAILURE);
  6455.         }
  6456. #ifdef IKSD
  6457. #ifdef CK_LOGIN
  6458.         if (inserver && ckxanon &&
  6459.              !strcmp(szUserNameRequested,"anonymous")) {
  6460.             SendSRPAuthSB(SRP_REJECT, (void *)
  6461.             "anonymous login cannot be performed with Secure Remote Password",
  6462.             -1);
  6463.             auth_finished(AUTH_REJECT);
  6464.             return(AUTH_FAILURE);
  6465.         }
  6466. #endif /* CK_LOGIN */
  6467. #endif /* IKSD */
  6468. #ifndef PRE_SRP_1_4_4
  6469.         if(tpw == NULL) {
  6470.             if((tpw = t_openpw(NULL)) == NULL) {
  6471.                 if (1)
  6472.                     printf("Unable to open password file\r\n");
  6473.                 SendSRPAuthSB(SRP_REJECT, (void *) "No password file", -1);
  6474.                 return(AUTH_FAILURE);
  6475.             }
  6476.         }
  6477.         if(tconf == NULL) {
  6478.             if((tconf = t_openconf(NULL)) == NULL) {
  6479.                 if (1)
  6480.                   printf("Unable to open configuration file\r\n");
  6481.                 SendSRPAuthSB(SRP_REJECT, (void *)"No configuration file", -1);
  6482.                 return(AUTH_FAILURE);
  6483.             }
  6484.         }
  6485.         ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6486.         t_closepw(tpw);
  6487.         tpw = NULL;
  6488.         t_closeconf(tconf);
  6489.         tconf = NULL;
  6490. #else /* PRE_SRP_1_4_4 */
  6491. #ifdef COMMENT
  6492.         /* the code in this block should no longer be necessary on OS/2
  6493.            or Windows because I have added functionality to libsrp.lib
  6494.            to find the srp files.   4/22/2000
  6495.         */
  6496.  
  6497.         /* On Windows and OS/2 there is no well defined place for the */
  6498.         /* ETC directory.  So we look for either an SRP_ETC or ETC    */
  6499.         /* environment variable in that order.  If we find one we     */
  6500.         /* attempt to open the files manually.                        */
  6501.         /* We will reuse the pbuf[] for the file names. */
  6502.         ptr = getenv("SRP_ETC");
  6503.         if ( !ptr )
  6504.             ptr = getenv("ETC");
  6505. #ifdef NT
  6506.         if ( !ptr ) {
  6507.             DWORD len;
  6508.             len = (2 * MAXPARAMLEN + 5);
  6509.  
  6510.             len = GetWindowsDirectory(pbuf,len);
  6511.             if ( len > 0 && len < (2 * MAXPARAMLEN + 5)) {
  6512.                 if ( !isWin95() ) {
  6513.                     if ( len == 1 )
  6514.                         ckstrncat(pbuf,"SYSTEM32/DRIVERS/ETC",sizeof(pbuf));
  6515.                     else
  6516.                         ckstrncat(pbuf,"/SYSTEM32/DRIVERS/ETC",sizeof(pbuf));
  6517.                 }
  6518.             }
  6519.             ptr = pbuf;
  6520.         }
  6521. #endif /* NT */
  6522.         if ( ptr ) {
  6523.             int len = strlen(ptr);
  6524.             int i;
  6525.             strcpy(pbuf,ptr);
  6526.             for ( i=0;i<len;i++ ) {
  6527.                 if ( pbuf[i] == '\\' )
  6528.                     pbuf[i] = '/';
  6529.             }
  6530.             if ( pbuf[len-1] != '/' )
  6531.                 ckstrncat(pbuf,"/tpasswd",sizeof(pbuf));
  6532.             else
  6533.                 ckstrncat(pbuf,"tpasswd",sizeof(pbuf));
  6534.             tpw = t_openpwbyname(pbuf);
  6535.  
  6536.             ckstrncat(pbuf,".conf",sizeof(pbuf));
  6537.             tconf = t_openconfbyname(pbuf);
  6538.         }
  6539.  
  6540.         if ( tpw && tconf )
  6541.             ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6542.         else
  6543.             ts = t_serveropen(szUserNameRequested);
  6544.         if ( tpw ) {
  6545.             t_closepw(tpw);
  6546.             tpw = NULL;
  6547.         }
  6548.         if ( tconf ) {
  6549.             t_closeconf(tconf);
  6550.             tconf = NULL;
  6551.         }
  6552. #else /* COMMENT */
  6553.         ts = t_serveropen(szUserNameRequested);
  6554. #endif /* COMMENT */
  6555. #endif /* PRE_SRP_1_4_4 */
  6556.  
  6557.         if( ts == NULL ) {
  6558.             printf("User %s not found\r\n", szUserNameRequested);
  6559.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  6560.             return(AUTH_FAILURE);
  6561.         }
  6562.  
  6563.         if ( ts->n.len + ts->g.len + ts->s.len + 6 > sizeof(pbuf) ) {
  6564.             printf("message length error\r\n");
  6565.             SendSRPAuthSB(SRP_REJECT, (void *) "message length error", -1);
  6566.             return(AUTH_FAILURE);
  6567.         }
  6568.         ptr = pbuf;
  6569.  
  6570.         srp_encode_length(ptr, ts->n.len);
  6571.         ptr += 2;
  6572.         memcpy(ptr, ts->n.data, ts->n.len);     /* safe */
  6573.         ptr += ts->n.len;
  6574.  
  6575.         srp_encode_length(ptr, ts->g.len);
  6576.         ptr += 2;
  6577.         memcpy(ptr, ts->g.data, ts->g.len);     /* safe */
  6578.         ptr += ts->g.len;
  6579.  
  6580.         srp_encode_length(ptr, ts->s.len);
  6581.         ptr += 2;
  6582.         memcpy(ptr, ts->s.data, ts->s.len);     /* safe */
  6583.         ptr += ts->s.len;
  6584.  
  6585.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  6586.  
  6587.         B = t_servergenexp(ts);
  6588.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  6589.         return AUTH_SUCCESS;
  6590.  
  6591.     case SRP_EXP:
  6592.         /* Client is sending A to us, compute challenge & expected response. */
  6593.         if (ts == NULL || B == NULL) {
  6594.             printf("Protocol error: SRP_EXP unexpected\r\n");
  6595.             SendSRPAuthSB(SRP_REJECT,
  6596.                           (void *) "Protocol error: unexpected EXP",
  6597.                           -1
  6598.                           );
  6599.             return(AUTH_FAILURE);
  6600.         }
  6601.  
  6602.         /* Wait until now to send B, since it contains the key to "u" */
  6603.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->len);
  6604.         B = NULL;
  6605.  
  6606. #ifndef PRE_SRP_1_4_5
  6607.         /*
  6608.          * The original SRP AUTH implementation did not protect against
  6609.          * tampering of the auth-type-pairs.  Therefore, when the
  6610.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6611.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6612.          * is set we also insert the SSL/TLS client and server finished
  6613.          * messages to ensure that there is no man in the middle attack
  6614.          * underway on the SSL/TLS connection.
  6615.          */
  6616.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  6617.             type_check[0] = AUTHTYPE_SRP;
  6618.             type_check[1] = how;
  6619. #ifdef CK_SSL
  6620.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6621.                 ssl_get_client_finished(&type_check[2],12);
  6622.                 ssl_get_server_finished(&type_check[14],12);
  6623.             }
  6624. #endif /* CK_SSL */
  6625.             t_serveraddexdata(ts,type_check,
  6626. #ifdef CK_SSL
  6627.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  6628. #endif /* CK_SSL */
  6629.                                2);
  6630.         }
  6631. #endif /* PRE_SRP_1_4_5 */
  6632.  
  6633.         A.data = data;
  6634.         A.len = cnt;
  6635.         ptr = t_servergetkey(ts, &A);
  6636.  
  6637.         if(ptr == NULL) {
  6638.             if (1)
  6639.               printf("Security alert: Trivial session key attempted\r\n");
  6640.             SendSRPAuthSB(SRP_REJECT,
  6641.                           (void *) "Trivial session key detected",
  6642.                           -1
  6643.                           );
  6644.             return(AUTH_FAILURE);
  6645.         }
  6646.         srp_waitresp = 1;
  6647.         return AUTH_SUCCESS;
  6648.  
  6649.     case SRP_RESPONSE:
  6650.         /* Got the response; see if it's correct */
  6651.         if (!srp_waitresp ||
  6652.              ts == NULL
  6653.              ) {
  6654.             if (1)
  6655.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  6656.             SendSRPAuthSB(SRP_REJECT,
  6657.                           (void *) "Protocol error: unexpected RESPONSE",
  6658.                           -1
  6659.                           );
  6660.             return(AUTH_FAILURE);
  6661.         }
  6662.         srp_waitresp = 0;       /* we got a response */
  6663.  
  6664.         if (cnt < RESPONSE_LEN) {
  6665.             if (1)
  6666.               printf("Protocol error: malformed response\r\n");
  6667.             SendSRPAuthSB(SRP_REJECT,
  6668.                           (void *) "Protocol error: malformed response",
  6669.                           -1
  6670.                           );
  6671.             return(AUTH_FAILURE);
  6672.         }
  6673.  
  6674.         if (t_serververify(ts, data) == 0) {
  6675.             SendSRPAuthSB(SRP_ACCEPT, t_serverresponse(ts), RESPONSE_LEN);
  6676.             accept_complete = 1;
  6677. #ifdef CK_ENCRYPTION
  6678. #ifdef CK_SSL
  6679.             if (!(ssl_active_flag || tls_active_flag))
  6680. #endif /* CK_SSL */
  6681.             {
  6682.                 hexdump("SRP_RESPONSE ts",ts,sizeof(ts));
  6683.                 hexdump("SRP_RESPONSE session_key",
  6684.                          ts->session_key,
  6685.                          SESSION_KEY_LEN
  6686.                          );
  6687.                 skey.type = SK_GENERIC;
  6688.                 skey.length = SESSION_KEY_LEN;
  6689.                 skey.data = ts->session_key;
  6690.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  6691.             }
  6692. #endif /* CK_ENCRYPTION */
  6693.             auth_finished(AUTH_VALID);
  6694.         }
  6695.         else {
  6696.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  6697.             auth_finished(AUTH_REJECT);
  6698.             return(AUTH_FAILURE);
  6699.         }
  6700.         return AUTH_SUCCESS;
  6701.  
  6702.     default:
  6703.         printf("Unknown SRP option %d\r\n", data[-1]);
  6704.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  6705.         return(AUTH_FAILURE);
  6706.     }
  6707. }
  6708. #else /* PRE_SRP_1_7_3 */
  6709. static int
  6710. #ifdef CK_ANSIC
  6711. new_srp_reply(int how, unsigned char *data, int cnt)
  6712. #else
  6713. new_srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6714. #endif
  6715. {
  6716.     data += 4;                          /* Point to status byte */
  6717.     cnt  -= 4;
  6718.  
  6719.     if(cnt-- < 1) {                     /* Matches with data++ */
  6720.         auth_finished(AUTH_REJECT);
  6721.         return AUTH_FAILURE;
  6722.     }
  6723.  
  6724.     switch(*data++) {
  6725.     case SRP_PARAMS: {
  6726.         struct t_num n;
  6727.         struct t_num g;
  6728.         struct t_num s;
  6729.         cstr * A;
  6730.  
  6731.         if(!szUserName) {
  6732.             printf("No username available\r\n");
  6733.             return(auth_resend(AUTHTYPE_SRP));
  6734.         }
  6735.  
  6736.         n.len = srp_decode_length(data);
  6737.         data += 2;
  6738.         cnt -= 2;
  6739.         if(n.len > cnt) {
  6740.             printf("n too long\r\n");
  6741.             return(auth_resend(AUTHTYPE_SRP));
  6742.         }
  6743.         n.data = data;
  6744.         data += n.len;
  6745.         cnt -= n.len;
  6746.  
  6747.         g.len = srp_decode_length(data);
  6748.         data += 2;
  6749.         cnt -= 2;
  6750.         if(g.len > cnt) {
  6751.             printf("g too long\r\n");
  6752.             return(auth_resend(AUTHTYPE_SRP));
  6753.         }
  6754.         g.data = data;
  6755.         data += g.len;
  6756.         cnt -= g.len;
  6757.  
  6758.         s.len = srp_decode_length(data);
  6759.         data += 2;
  6760.         cnt -= 2;
  6761.         if(s.len != cnt) {
  6762.             printf("invalid salt\r\n");
  6763.             return(auth_resend(AUTHTYPE_SRP));
  6764.         }
  6765.         s.data = data;
  6766.         data += s.len;
  6767.         cnt -= s.len;
  6768.  
  6769.         /* If the parameters provided by the server cannot be
  6770.          * validated the following function will fail.
  6771.          */
  6772.         c_srp = SRP_new(SRP_RFC2945_client_method());
  6773.         if(c_srp == NULL ||
  6774.             SRP_set_username(c_srp, szUserName) != SRP_SUCCESS ||
  6775.             SRP_set_params(c_srp, n.data, n.len, g.data, g.len, s.data, s.len) !=
  6776.             SRP_SUCCESS) {
  6777.             printf("SRP Parameter initialization error\r\n");
  6778.             return(auth_resend(AUTHTYPE_SRP));
  6779.         }
  6780.  
  6781.         A = cstr_new();
  6782.         if(SRP_gen_pub(c_srp, &A) != SRP_SUCCESS) {
  6783.             printf("SRP Error generating key exchange\r\n");
  6784.             return(auth_resend(AUTHTYPE_SRP));
  6785.         }
  6786.  
  6787.         SendSRPAuthSB(SRP_EXP, A->data, A->length);
  6788.         cstr_free(A);
  6789.  
  6790.         if ( pwbuf[0] && pwflg ) {
  6791.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6792.                    8 * n.len,
  6793.                    szUserName
  6794.                    );
  6795.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6796. #ifdef OS2
  6797.             if ( pwcrypt )
  6798.                 ck_encrypt((char *)srp_passwd);
  6799. #endif /* OS2 */
  6800.         } else {
  6801.             extern char * srppwprompt;
  6802.             char preface[128];
  6803.             int ok;
  6804.  
  6805.             if (srppwprompt && srppwprompt[0] &&
  6806.                (strlen(srppwprompt) + strlen(szUserName) - 2) < sizeof(preface))
  6807.             {
  6808.                 sprintf(preface,srppwprompt,szUserName);
  6809.             } else {
  6810.                 ckmakxmsg( preface,sizeof(preface),
  6811.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6812.                           szUserName, "'\r\n", NULL, NULL, NULL, NULL, NULL,
  6813.                           NULL, NULL);
  6814.             }
  6815.             ok = uq_txt(preface,"Password: ",2,NULL,
  6816.                         srp_passwd,sizeof(srp_passwd)-1,NULL);
  6817.             if ( !ok )
  6818.                 srp_passwd[0] = '\0';
  6819.         }
  6820.  
  6821.         if(SRP_set_auth_password(c_srp, srp_passwd) != SRP_SUCCESS) {
  6822.             memset(srp_passwd, 0, sizeof(srp_passwd));
  6823.             printf("SRP Error setting client password\r\n");
  6824.             return(auth_resend(AUTHTYPE_SRP));
  6825.         }
  6826.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6827.         return AUTH_SUCCESS;
  6828.     }
  6829.     case SRP_CHALLENGE: {
  6830.         char type_check[26];
  6831.         cstr * resp = NULL;
  6832.  
  6833.         if(c_srp == NULL) {
  6834.             printf("SRP protocol error\r\n");
  6835.             return(auth_resend(AUTHTYPE_SRP));
  6836.         }
  6837.  
  6838.         /*
  6839.          * The original SRP AUTH implementation did not protect against
  6840.          * tampering of the auth-type-pairs.  Therefore, when the
  6841.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6842.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6843.          * is set we also insert the SSL/TLS client and server finished
  6844.          * messages to ensure that there is no man in the middle attack
  6845.          * underway on the SSL/TLS connection.
  6846.          */
  6847.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6848.             type_check[0] = AUTHTYPE_SRP;
  6849.             type_check[1] = how;
  6850. #ifdef CK_SSL
  6851.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6852.                 ssl_get_client_finished(&type_check[2],12);
  6853.                 ssl_get_server_finished(&type_check[14],12);
  6854.                 SRP_add_ex_data(c_srp, type_check, 26);
  6855.             } else
  6856. #endif /* CK_SSL */
  6857.                 SRP_add_ex_data(c_srp, type_check, 2);
  6858.         }
  6859.  
  6860.         if(SRP_compute_key(c_srp, &c_key, data, cnt) != SRP_SUCCESS) {
  6861.             printf("SRP ERROR: unable to compute client key\r\n");
  6862.             return(auth_resend(AUTHTYPE_SRP));
  6863.         }
  6864.  
  6865.         resp = cstr_new();
  6866.         if(SRP_respond(c_srp, &resp) != SRP_SUCCESS) {
  6867.             printf("SRP ERROR: unable to compute client response\r\n");
  6868.             return(auth_resend(AUTHTYPE_SRP));
  6869.         }
  6870.         SendSRPAuthSB(SRP_RESPONSE, resp->data, resp->length);
  6871.         cstr_free(resp);
  6872.         srp_waitresp = 1;
  6873.         return AUTH_SUCCESS;
  6874.     }
  6875.     case SRP_ACCEPT: {
  6876. #ifdef CK_ENCRYPTION
  6877.         Session_Key skey;
  6878. #endif /* ENCRYPTION */
  6879.  
  6880.         if(cnt < RESPONSE_LEN || !srp_waitresp || c_srp == NULL) {
  6881.             printf("SRP Protocol error\r\n");
  6882.             return(auth_resend(AUTHTYPE_SRP));
  6883.         }
  6884.         srp_waitresp = 0;
  6885.  
  6886.         if(SRP_verify(c_srp, data, cnt) == SRP_SUCCESS) {
  6887.             printf("SRP accepts you as %s\r\n",szUserName);
  6888.  
  6889. #ifdef CK_SSL
  6890.             if((ssl_active_flag || tls_active_flag) &&
  6891.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6892.                 printf("TLS session parameters verified by SRP\r\n");
  6893.             } else
  6894. #endif /* CK_SSL */
  6895. #ifdef CK_ENCRYPTION
  6896.             {
  6897.                 skey.type = SK_GENERIC;
  6898.                 skey.length = c_key->length;
  6899.                 skey.data = c_key->data;
  6900.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6901.                 cstr_clear_free(c_key);
  6902.                 c_key = NULL;
  6903.             }
  6904. #endif /* CK_ENCRYPTION */
  6905.             accept_complete = 1;
  6906.             auth_finished(AUTH_VALID);
  6907.             SRP_free(c_srp);
  6908.             c_srp = NULL;
  6909.             return AUTH_SUCCESS;
  6910.         }
  6911.         else {
  6912.             printf("[ Error: SRP server authentication failed ]\r\n");
  6913.             return(auth_resend(AUTHTYPE_SRP));
  6914.         }
  6915.     }
  6916.     case SRP_REJECT: {
  6917.         char * str=NULL;
  6918.  
  6919.         ckmakmsg(strTmp,sizeof(strTmp),
  6920.                   "SRP refuses authentication for '",szUserName,
  6921.                   "'\r\n",NULL);
  6922.         if (cnt > 0) {
  6923.             int len = strlen(strTmp);
  6924.             if ( len + cnt < sizeof(strTmp) ) {
  6925.                 str = strTmp + strlen(strTmp);
  6926.                 memcpy(str,data,cnt);
  6927.                 str[cnt] = 0;
  6928.             }
  6929.         }
  6930.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6931.         auth_finished(AUTH_REJECT);
  6932.         return AUTH_FAILURE;
  6933.     }
  6934.     default:
  6935.         printf("Unknown SRP option %d\r\n", data[-1]);
  6936.         return(auth_resend(AUTHTYPE_SRP));
  6937.     }
  6938.     /* NEVER REACHED */
  6939. }
  6940.  
  6941. static int
  6942. #ifdef CK_ANSIC
  6943. new_srp_is(int how, unsigned char *data, int cnt)
  6944. #else
  6945. new_srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6946. #endif
  6947. {
  6948.     char pbuf[2 * MAXPARAMLEN + 5];
  6949.     char * ptr;
  6950. #ifdef CK_ENCRYPTION
  6951.     Session_Key skey;
  6952. #endif
  6953.     static cstr * B = NULL;             /* Holder for B */
  6954.     struct t_passwd * pass;
  6955.     cstr * resp;
  6956.     char type_check[26];
  6957.  
  6958.     if ((cnt -= 4) < 1) {
  6959.         auth_finished(AUTH_REJECT);
  6960.         return AUTH_FAILURE;
  6961.     }
  6962.  
  6963.     data += 4;
  6964.     cnt  -= 1;
  6965.     switch(*data++) {
  6966.     case SRP_AUTH:
  6967.         /* Send parameters back to client */
  6968.         if(s_srp != NULL) {
  6969.             SRP_free(s_srp);
  6970.             s_srp = NULL;
  6971.         }
  6972.         if (B != NULL) {
  6973.             cstr_free(B);
  6974.             B = NULL;
  6975.         }
  6976.         if(!szUserNameRequested[0]) {
  6977.             if (1)
  6978.                 printf("No username available\r\n");
  6979.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6980.             auth_finished(AUTH_REJECT);
  6981.             return(AUTH_FAILURE);
  6982.         }
  6983. #ifdef IKSD
  6984. #ifdef CK_LOGIN
  6985.         if (inserver && ckxanon &&
  6986.              !strcmp(szUserNameRequested,"anonymous")) {
  6987.             SendSRPAuthSB(SRP_REJECT, (void *)
  6988.             "anonymous login cannot be performed with Secure Remote Password",
  6989.             -1);
  6990.             auth_finished(AUTH_REJECT);
  6991.             return(AUTH_FAILURE);
  6992.         }
  6993. #endif /* CK_LOGIN */
  6994. #endif /* IKSD */
  6995.         s_srp = SRP_new(SRP_RFC2945_server_method());
  6996.         if(s_srp == NULL) {
  6997.             printf("Error initializing SRP server\r\n");
  6998.             SendSRPAuthSB(SRP_REJECT,
  6999.                           (void *) "SRP server init failed",
  7000.                           -1
  7001.                           );
  7002.             return(AUTH_FAILURE);
  7003.         }
  7004.         pass = gettpnam(szUserNameRequested);
  7005.         if(pass == NULL) {
  7006.             printf("User %s not found\r\n", szUserNameRequested);
  7007.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  7008.             return(AUTH_FAILURE);
  7009.         }
  7010.         if(SRP_set_username(s_srp, szUserNameRequested) != SRP_SUCCESS ||
  7011.             SRP_set_params(s_srp, pass->tc.modulus.data, pass->tc.modulus.len,
  7012.                             pass->tc.generator.data, pass->tc.generator.len,
  7013.                             pass->tp.salt.data, pass->tp.salt.len) != SRP_SUCCESS ||
  7014.             SRP_set_authenticator(s_srp, pass->tp.password.data,
  7015.                                    pass->tp.password.len) != SRP_SUCCESS) {
  7016.             printf("Error initializing SRP parameters\r\n");
  7017.             SendSRPAuthSB(SRP_REJECT, (void *) "SRP parameter init failed", -1);
  7018.             return(AUTH_FAILURE);
  7019.         }
  7020.  
  7021.         ptr = pbuf;
  7022.  
  7023.         srp_encode_length(ptr, pass->tc.modulus.len);
  7024.         ptr += 2;
  7025.         memcpy(ptr, pass->tc.modulus.data, pass->tc.modulus.len);
  7026.         ptr += pass->tc.modulus.len;
  7027.  
  7028.         srp_encode_length(ptr, pass->tc.generator.len);
  7029.         ptr += 2;
  7030.         memcpy(ptr, pass->tc.generator.data, pass->tc.generator.len);
  7031.         ptr += pass->tc.generator.len;
  7032.  
  7033.         srp_encode_length(ptr, pass->tp.salt.len);
  7034.         ptr += 2;
  7035.         memcpy(ptr, pass->tp.salt.data, pass->tp.salt.len);
  7036.         ptr += pass->tp.salt.len;
  7037.  
  7038.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  7039.  
  7040.         if(SRP_gen_pub(s_srp, &B) != SRP_SUCCESS) {
  7041.             printf("Error generating SRP public value\r\n");
  7042.             SendSRPAuthSB(SRP_REJECT, (void *) "SRP_gen_pub failed", -1);
  7043.             return(AUTH_FAILURE);
  7044.         }
  7045.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  7046.         return AUTH_SUCCESS;
  7047.  
  7048.     case SRP_EXP:
  7049.         /* Client is sending A to us.  Compute challenge and expected response. */
  7050.         if(s_srp == NULL || B == NULL) {
  7051.             printf("Protocol error: SRP_EXP unexpected\r\n");
  7052.             SendSRPAuthSB(SRP_REJECT, (void *) "Protocol error: unexpected EXP", -1);
  7053.             return(AUTH_FAILURE);
  7054.         }
  7055.  
  7056.         /* Wait until now to send B, since it contains the key to "u" */
  7057.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->length);
  7058.         cstr_free(B);
  7059.         B = NULL;
  7060.  
  7061.         /*
  7062.          * The original SRP AUTH implementation did not protect against
  7063.          * tampering of the auth-type-pairs.  Therefore, when the
  7064.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  7065.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  7066.          * is set we also insert the SSL/TLS client and server finished
  7067.          * messages to ensure that there is no man in the middle attack
  7068.          * underway on the SSL/TLS connection.
  7069.          */
  7070.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  7071.             type_check[0] = AUTHTYPE_SRP;
  7072.             type_check[1] = how;
  7073. #ifdef CK_SSL
  7074.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7075.                 ssl_get_client_finished(&type_check[2],12);
  7076.                 ssl_get_server_finished(&type_check[14],12);
  7077.                 SRP_add_ex_data(s_srp, type_check, 26);
  7078.             } else
  7079. #endif /* CK_SSL */
  7080.                 SRP_add_ex_data(s_srp, type_check, 2);
  7081.         }
  7082.  
  7083.         if(SRP_compute_key(s_srp, &s_key, data, cnt) != SRP_SUCCESS) {
  7084.             printf("Security alert: Trivial session key attempted\r\n");
  7085.             SendSRPAuthSB(SRP_REJECT, (void *) "Trivial session key detected", -1);
  7086.             return(AUTH_FAILURE);
  7087.         }
  7088.         srp_waitresp = 1;
  7089.         return AUTH_SUCCESS;
  7090.  
  7091.     case SRP_RESPONSE:
  7092.         /* Got the response; see if it's correct */
  7093.         if (!srp_waitresp || s_srp == NULL) {
  7094.             if (1)
  7095.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  7096.             SendSRPAuthSB(SRP_REJECT,
  7097.                           (void *) "Protocol error: unexpected RESPONSE",
  7098.                           -1
  7099.                           );
  7100.             return(AUTH_FAILURE);
  7101.         }
  7102.         srp_waitresp = 0;       /* we got a response */
  7103.  
  7104.         if (cnt < RESPONSE_LEN) {
  7105.             if (1)
  7106.               printf("Protocol error: malformed response\r\n");
  7107.             SendSRPAuthSB(SRP_REJECT,
  7108.                           (void *) "Protocol error: malformed response",
  7109.                           -1
  7110.                           );
  7111.             return(AUTH_FAILURE);
  7112.         }
  7113.  
  7114.         if(SRP_verify(s_srp, data, cnt) == SRP_SUCCESS) {
  7115.             resp = cstr_new();
  7116.             if(SRP_respond(s_srp, &resp) != SRP_SUCCESS) {
  7117.                 printf("Error computing response\r\n");
  7118.                 SendSRPAuthSB(SRP_REJECT,
  7119.                               (void *) "Error computing response", -1);
  7120.                 return(AUTH_FAILURE);
  7121.             }
  7122.             SendSRPAuthSB(SRP_ACCEPT, resp->data, resp->length);
  7123.             accept_complete = 1;
  7124.             cstr_free(resp);
  7125.  
  7126. #ifdef CK_ENCRYPTION
  7127. #ifdef CK_SSL
  7128.             if (!(ssl_active_flag || tls_active_flag))
  7129. #endif /* CK_SSL */
  7130.             {
  7131.                 skey.type = SK_GENERIC;
  7132.                 skey.length = s_key->length;
  7133.                 skey.data = s_key->data;
  7134.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  7135.                 cstr_clear_free(s_key);
  7136.                 s_key = NULL;
  7137.             }
  7138. #endif /* CK_ENCRYPTION */
  7139.             auth_finished(AUTH_VALID);
  7140.         }
  7141.         else {
  7142.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  7143.             auth_finished(AUTH_REJECT);
  7144.             return(AUTH_FAILURE);
  7145.         }
  7146.         return AUTH_SUCCESS;
  7147.  
  7148.     default:
  7149.         printf("Unknown SRP option %d\r\n", data[-1]);
  7150.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  7151.         return(AUTH_FAILURE);
  7152.     }
  7153. }
  7154. #endif /* PRE_SRP_1_7_3 */
  7155. #endif /* SRP */
  7156.  
  7157. #ifdef KRB5
  7158. #ifdef KINIT
  7159. /*
  7160.  * clients/kinit/kinit.c
  7161.  *
  7162.  * Copyright 1990 by the Massachusetts Institute of Technology.
  7163.  * All Rights Reserved.
  7164.  *
  7165.  * Export of this software from the United States of America may
  7166.  *   require a specific license from the United States Government.
  7167.  *   It is the responsibility of any person or organization contemplating
  7168.  *   export to obtain such a license before exporting.
  7169.  *
  7170.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  7171.  * distribute this software and its documentation for any purpose and
  7172.  * without fee is hereby granted, provided that the above copyright
  7173.  * notice appear in all copies and that both that copyright notice and
  7174.  * this permission notice appear in supporting documentation, and that
  7175.  * the name of M.I.T. not be used in advertising or publicity pertaining
  7176.  * to distribution of the software without specific, written prior
  7177.  * permission.  M.I.T. makes no representations about the suitability of
  7178.  * this software for any purpose.  It is provided "as is" without express
  7179.  * or implied warranty.
  7180.  *
  7181.  *
  7182.  * Initialize a credentials cache.
  7183.  */
  7184.  
  7185. #define KRB5_DEFAULT_OPTIONS 0
  7186. #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
  7187.  
  7188. static krb5_data tgtname = {
  7189. #ifndef HEIMDAL
  7190.     0,
  7191. #endif /* HEIMDAL */
  7192.     KRB5_TGS_NAME_SIZE,
  7193.     KRB5_TGS_NAME
  7194. };
  7195.  
  7196. /* Internal prototypes */
  7197. _PROTOTYP(static krb5_error_code krb5_validate_tgt,
  7198.         (krb5_context, krb5_ccache,krb5_principal, krb5_data *));
  7199. _PROTOTYP(static krb5_error_code krb5_renew_tgt,
  7200.         (krb5_context, krb5_ccache,
  7201.                         krb5_principal, krb5_data *));
  7202. _PROTOTYP(static krb5_error_code krb5_tgt_gen,
  7203.         (krb5_context, krb5_ccache,
  7204.                         krb5_principal, krb5_data *, int opt));
  7205.  
  7206. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7207. static krb5_error_code KRB5_CALLCONV
  7208. ck_krb5_prompter( krb5_context context,
  7209.                   void *data,
  7210.                   const char *name,
  7211.                   const char *banner,
  7212.                   int num_prompts,
  7213.                   krb5_prompt prompts[])
  7214. {
  7215.     krb5_error_code     errcode = 0;
  7216.     int                 i;
  7217. #ifdef KUI
  7218.     struct txtbox * tb = NULL;
  7219. #else /* KUI */
  7220.     char * prompt = NULL;
  7221. #endif /* KUI */
  7222.     int    len = 0, blen=0, nlen=0;
  7223.  
  7224.     debug(F110,"ck_krb5_prompter name",name,0);
  7225.     debug(F110,"ck_krb5_prompter banner",banner,0);
  7226.     debug(F101,"ck_krb5_prompter num_prompts","",num_prompts);
  7227.  
  7228.     if (name)
  7229.         nlen = strlen(name)+2;
  7230.  
  7231.     if (banner)
  7232.         blen = strlen(banner)+2;
  7233.  
  7234. #ifdef KUI
  7235.     tb = (struct txtbox *) malloc(sizeof(struct txtbox) * num_prompts);
  7236.     if ( tb != NULL ) {
  7237.         int ok;
  7238.         memset(tb,0,sizeof(struct txtbox) * num_prompts);
  7239.         for ( i=0; i < num_prompts; i++ ) {
  7240.             tb[i].t_buf = prompts[i].reply->data;
  7241.             tb[i].t_len = prompts[i].reply->length;
  7242.             tb[i].t_lbl = prompts[i].prompt;
  7243.             tb[i].t_dflt = NULL;
  7244.             tb[i].t_echo = (prompts[i].hidden ? 2 : 1);
  7245.         }   
  7246.  
  7247.         ok = uq_mtxt((char *)banner,NULL,num_prompts,tb);
  7248.         if ( ok ) {
  7249.             for ( i=0; i < num_prompts; i++ )
  7250.                 prompts[i].reply->length = strlen(prompts[i].reply->data);
  7251.         } else
  7252.             errcode = -1;
  7253.     }
  7254. #else /* KUI */
  7255.     for (i = 0; i < num_prompts; i++) {
  7256.         debug(F111,"ck_krb5_prompter prompt",prompts[i].prompt,i);
  7257.  
  7258.         if ( prompt && len < (nlen + blen + strlen(prompts[i].prompt)+2) ) {
  7259.             free(prompt);
  7260.             prompt = NULL;
  7261.         }
  7262.         if ( !prompt )
  7263.             prompt = (char *)malloc(nlen + blen + strlen(prompts[i].prompt)+2);
  7264.         if ( !prompt ) {
  7265.             errcode = KRB5_RC_MALLOC;
  7266.             goto cleanup;
  7267.         }
  7268.         len = nlen + blen + strlen(prompts[i].prompt)+2;
  7269.         ckmakxmsg(prompt,len,
  7270.                  (char *) (name?name:""),
  7271.                  name?"\r\n":"",
  7272.                  (char *) (banner?banner:""),
  7273.                  banner?"\r\n":"",
  7274.                  (char *)prompts[i].prompt,
  7275.                  ": ",NULL,NULL,NULL,NULL,NULL,NULL);
  7276.  
  7277.         memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7278.         if (prompts[i].hidden) {
  7279.             readpass(prompt, prompts[i].reply->data,
  7280.                       prompts[i].reply->length);
  7281.         } else {
  7282.             readtext(prompt, prompts[i].reply->data,
  7283.                       prompts[i].reply->length);
  7284.         }
  7285.         prompts[i].reply->length = strlen(prompts[i].reply->data);
  7286.     }
  7287. #endif /* KUI */
  7288.  
  7289.   cleanup:
  7290. #ifdef KUI
  7291.     if ( tb )
  7292.         free(tb);
  7293. #else /* KUI */
  7294.     if ( prompt )
  7295.         free(prompt);
  7296. #endif /* KUI */
  7297.     if (errcode) {
  7298.         for (i = 0; i < num_prompts; i++) {
  7299.             memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7300.         }
  7301.     }
  7302.     return errcode;
  7303. }
  7304.  
  7305. /*
  7306.  *      I'm not really sure what to do with this.  The NRL DLLs use a
  7307.  *      different interface for the krb5_prompter callback.  It has
  7308.  *      one less parameter.   This is going to be ugly.
  7309.  */
  7310. static krb5_error_code KRB5_CALLCONV
  7311. ck_NRL_krb5_prompter( krb5_context context,
  7312.                       const char *name,
  7313.                       const char *banner,
  7314.                       int num_prompts,
  7315.                       krb5_prompt prompts[])
  7316. {
  7317.     return(ck_krb5_prompter(context,NULL,name,banner,num_prompts,prompts));
  7318. }
  7319. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7320.  
  7321. #define NO_KEYTAB
  7322.  
  7323. int
  7324. #ifdef CK_ANSIC
  7325. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  7326.                  struct krb4_init_data * k4_init)
  7327. #else
  7328. ck_krb5_initTGT(op,init,k4_init)
  7329.     krb_op_data * op; struct krb5_init_data * init;
  7330.     struct krb4_init_data * k4_init;
  7331. #endif /* CK_ANSIC*/
  7332. {
  7333.     krb5_context kcontext;
  7334.     krb5_ccache ccache = NULL;
  7335.     krb5_deltat lifetime = KRB5_DEFAULT_LIFE;   /* -l option */
  7336.     krb5_timestamp starttime = 0;
  7337.     krb5_deltat rlife = 0;
  7338.     int options = KRB5_DEFAULT_OPTIONS;
  7339.     int option;
  7340.     int errflg = 0;
  7341.     krb5_error_code code;
  7342.     krb5_principal me=NULL;
  7343.     krb5_principal server=NULL;
  7344.     krb5_creds my_creds;
  7345.     krb5_timestamp now;
  7346. #ifndef HEIMDAL
  7347.     krb5_address **addrs = (krb5_address **)0;
  7348. #endif /* HEIMDAL */
  7349.     int addr_count=0;
  7350.     int i,j;
  7351. #ifndef NO_KEYTAB
  7352.     int use_keytab = 0;                 /* -k option */
  7353.     krb5_keytab keytab = NULL;
  7354. #endif /* NO_KEYTAB */
  7355.     struct passwd *pw = 0;
  7356.     int pwsize;
  7357.     char *client_name=NULL, principal[256]="", realm[256]="", numstr[40]="";
  7358.     char *password=NULL, passwd[80]="";
  7359. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7360.     krb5_get_init_creds_opt opts;
  7361. #endif
  7362.     char * name;
  7363.     int len;
  7364.  
  7365.     if ( !ck_krb5_is_installed() )
  7366.         return(-1);
  7367.  
  7368. #ifdef COMMENT
  7369.     printf("Kerberos V initialization\r\n");
  7370. #endif /* COMMENT */
  7371.  
  7372.     code = krb5_init_context(&kcontext);
  7373.     if (code) {
  7374.         com_err("krb5_kinit",code,"while init_context");
  7375.         krb5_errno = code;
  7376.         makestr(&krb5_errmsg,error_message(krb5_errno));
  7377.         return(-1);
  7378.     }
  7379.  
  7380.     debug(F110,"krb5_init","krb5_init_context",0);
  7381.  
  7382.     if ((code = krb5_timeofday(kcontext, &now))) {
  7383.         com_err("krb5_kinit",code,"while getting time of day");
  7384.         goto exit_k5_init;
  7385.     }
  7386.  
  7387. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7388.     memset(&opts, 0, sizeof(opts));
  7389.     krb5_get_init_creds_opt_init(&opts);
  7390.     debug(F110,"krb5_init","krb5_get_init_creds_opt_init",0);
  7391. #endif
  7392.  
  7393.     if ( init->renewable ) {
  7394.         options |= KDC_OPT_RENEWABLE;
  7395.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->renewable),"m",NULL,NULL);
  7396. #ifdef HEIMDAL
  7397.         code = -1;
  7398. #else /* HEIMDAL */
  7399.         code = krb5_string_to_deltat(numstr, &rlife);
  7400. #endif /* HEIMDAL */
  7401.         if (code != 0 || rlife == 0) {
  7402.             printf("Bad renewable time value %s\r\n", numstr);
  7403.             errflg++;
  7404.         }
  7405. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7406.         krb5_get_init_creds_opt_set_renew_life(&opts, rlife);
  7407. #endif
  7408.     }
  7409.     if ( init->renew ) {
  7410.         /* renew the ticket */
  7411.         options |= KDC_OPT_RENEW;
  7412.     }
  7413.  
  7414.     if ( init->validate ) {
  7415.         /* validate the ticket */
  7416.         options |= KDC_OPT_VALIDATE;
  7417.     }
  7418.     if ( init->proxiable ) {
  7419.         options |= KDC_OPT_PROXIABLE;
  7420. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7421.         krb5_get_init_creds_opt_set_proxiable(&opts, 1);
  7422. #endif
  7423.     }
  7424.     if ( init->forwardable ) {
  7425.         options |= KDC_OPT_FORWARDABLE;
  7426. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7427.         krb5_get_init_creds_opt_set_forwardable(&opts, 1);
  7428. #endif
  7429.     }
  7430. #ifndef NO_KEYTAB
  7431.     if (  ) {
  7432.         use_keytab = 1;
  7433.     }
  7434.     if (  ) {
  7435.         if (keytab == NULL && keytab_name != NULL) {
  7436.             code = krb5_kt_resolve(kcontext, keytab_name, &keytab);
  7437.             if (code != 0) {
  7438.                 debug(F111,"krb5_init resolving keytab",
  7439.                          keytab_name,code);
  7440.                 errflg++;
  7441.             }
  7442.         }
  7443.     }
  7444. #endif /* NO_KEYTAB */
  7445.     if ( init->lifetime ) {
  7446.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->lifetime),"m",NULL,NULL);
  7447. #ifdef HEIMDAL
  7448.         code = -1;
  7449. #else /* HEIMDAL */
  7450.         code = krb5_string_to_deltat(numstr, &lifetime);
  7451. #endif /* HEIMDAL */
  7452.         if (code != 0 || lifetime == 0) {
  7453.             printf("Bad lifetime value %s\r\n", numstr);
  7454.             errflg++;
  7455.         }
  7456. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7457.         krb5_get_init_creds_opt_set_tkt_life(&opts, lifetime);
  7458. #endif
  7459.     }
  7460.     if ( init->postdate ) {
  7461.         /* Convert cmdate() to a time_t value */
  7462.         struct tm * time_tm;
  7463.         struct tm * cmdate2tm(char *,int);
  7464.         time_tm = cmdate2tm(init->postdate,0);
  7465.         if ( time_tm )
  7466.             starttime = (krb5_timestamp) mktime(time_tm);
  7467.  
  7468.         if (code != 0 || starttime == 0 || starttime == -1) {
  7469.             krb5_deltat ktmp;
  7470. #ifdef HEIMDAL
  7471.             code = -1;
  7472. #else /* HEIMDAL */
  7473.             code = krb5_string_to_deltat(init->postdate, &ktmp);
  7474. #endif /* HEIMDAL */
  7475.             if (code == 0 && ktmp != 0) {
  7476.                 starttime = now + ktmp;
  7477.                 options |= KDC_OPT_POSTDATED;
  7478.             } else {
  7479.                 printf("Bad postdate start time value %s\r\n",
  7480.                         init->postdate);
  7481.                 errflg++;
  7482.             }
  7483.         } else {
  7484.             options |= KDC_OPT_POSTDATED;
  7485.         }
  7486.     }
  7487.  
  7488.     debug(F110,"krb5_init searching for ccache",op->cache,0);
  7489.  
  7490.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  7491.     if (code != 0) {
  7492.         com_err("krb5_kinit",code,"while getting default ccache");
  7493.         goto exit_k5_init;
  7494.     }
  7495.  
  7496.     /* This is our realm unless it is changed */
  7497.     ckstrncpy(realm,init->realm ? init->realm : krb5_d_realm, 256);
  7498.  
  7499. #ifdef BETATEST
  7500.     /* This code is going to take the realm and attempt to correct */
  7501.     /* the case.                                                   */
  7502.     {
  7503.         profile_t profile;
  7504.  
  7505.         code = krb5_get_profile(kcontext, &profile);
  7506.         if ( !code ) {
  7507.             const char  *names[4];
  7508.             char ** realms;
  7509.             int found = 0;
  7510.  
  7511.             names[0] = "realms";
  7512.             names[1] = NULL;
  7513.  
  7514.             code = profile_get_subsection_names(profile,names,&realms);
  7515.             if ( code == 0 ) {
  7516.                 int i=0;
  7517.                 while ( realms[i] ) {
  7518.                     if (ckstrcmp(realm,realms[i],-1,0) == 0) {
  7519.                         strcpy(realm,realms[i]);
  7520.                         found = 1;
  7521.                         break;
  7522.                     }
  7523.                     i++;
  7524.                 }
  7525.             }
  7526.  
  7527. #ifdef CK_DNS_SRV
  7528.             if ( !found ) {
  7529.                 char * dns_realm = NULL;
  7530.  
  7531.                 /* We did not find the realm in the profile so let's try DNS */
  7532.                 locate_txt_rr("_kerberos",realm,&dns_realm);
  7533.                 if ( dns_realm &&
  7534.                      ckstrcmp(realm,dns_realm,-1,0) == 0 &&
  7535.                      ckstrcmp(realm,dns_realm,-1,1) != 0
  7536.                      ) {
  7537.                     ckstrncpy(realm,dns_realm,256);
  7538.                     free(dns_realm);
  7539.                 }
  7540.             }
  7541. #endif /* CK_DNS_SRV */
  7542.         }
  7543.  
  7544.         if (init->realm &&
  7545.              ckstrcmp(realm,init->realm,-1,0) == 0 &&
  7546.              ckstrcmp(realm,init->realm,-1,1) != 0)
  7547.             strcpy(init->realm,realm);
  7548.         if (ckstrcmp(realm,krb5_d_realm,-1,0) == 0 &&
  7549.              ckstrcmp(realm,krb5_d_realm,-1,1) != 0)
  7550.             strcpy(krb5_d_realm,realm);
  7551.     }
  7552. #endif /* BETATEST */
  7553.  
  7554.     if (init->principal == NULL) {       /* No principal name specified */
  7555. #ifndef NO_KEYTAB
  7556.         if (use_keytab) {
  7557.             /* Use the default host/service name */
  7558.             code = krb5_sname_to_principal(kcontext, NULL, NULL,
  7559.                                             KRB5_NT_SRV_HST, &me);
  7560.             if (code == 0 &&
  7561.                 krb5_princ_realm(kcontext, me)->length < sizeof(realm))
  7562.             {
  7563.                 /* Save the realm */
  7564.                 memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  7565.                         krb5_princ_realm(kcontext, me)->length); /* safe */
  7566.                 realm[krb5_princ_realm(kcontext, me)->length]='\0';
  7567.             } else {
  7568.                 com_err("krb5_kinit",
  7569.                         code,
  7570.                         "when creating default server principal name");
  7571.                 goto exit_k5_init;
  7572.             }
  7573.         } else
  7574. #endif /* NO_KEYTAB */
  7575.         {
  7576.             int len;
  7577.             char * name;
  7578.  
  7579.             /* Get default principal from cache if one exists */
  7580.             code = krb5_cc_get_principal(kcontext, ccache, &me);
  7581. #ifdef HEIMDAL
  7582.             name = me->realm;
  7583.             len = strlen(name);
  7584. #else /* HEIMDAL */
  7585.             len = krb5_princ_realm(kcontext, me)->length;
  7586.             name = krb5_princ_realm(kcontext, me)->data;
  7587. #endif /* HEIMDAL */
  7588.             if (code == 0 && len < sizeof(realm))
  7589.             {
  7590.                 /* Save the realm */
  7591.                 memcpy(realm,name,len); /* safe */
  7592.                 realm[len]='\0';
  7593.             } else {
  7594. #ifdef HAVE_PWD_H
  7595.                 /* Else search passwd file for client */
  7596.  
  7597.                 pw = getpwuid((int) getuid());
  7598.                 if (pw) {
  7599.                     char princ_realm[256];
  7600.                     if ( (strlen(pw->pw_name) + strlen(realm) + 1) > 255 )
  7601.                         goto exit_k5_init;
  7602.  
  7603.                     ckstrncpy(principal,pw->pw_name,256);
  7604.                     ckstrncpy(princ_realm,pw->pw_name,256);
  7605.                     ckstrncat(princ_realm,"@",256);
  7606.                     ckstrncat(princ_realm,realm,256);
  7607.  
  7608.                     if ((code = krb5_parse_name(kcontext,princ_realm,&me))) {
  7609.                         krb5_errno = code;
  7610.                         com_err("krb5_kinit",code,"when parsing name",
  7611.                                   princ_realm);
  7612.                         goto exit_k5_init;
  7613.                     }
  7614.                 } else {
  7615.                     printf(
  7616.                         "Unable to identify user from password file\r\n");
  7617.                     goto exit_k5_init;
  7618.                 }
  7619. #else /* HAVE_PWD_H */
  7620.                 printf("Unable to identify user\r\n");
  7621.                 goto exit_k5_init;
  7622. #endif /* HAVE_PWD_H */
  7623.             }
  7624.         }
  7625.  
  7626. #ifdef HEIMDAL
  7627.         len = me->name.name_string.len;
  7628.         name = *me->name.name_string.val;
  7629. #else /* HEIMDAL */
  7630.         len = krb5_princ_name(kcontext, me)->length;
  7631.         name = krb5_princ_name(kcontext, me)->data;
  7632. #endif /* HEIMDAL */
  7633.         if ( len < sizeof(principal) ) {
  7634.             memcpy(principal,name,len);     /* safe */
  7635.             principal[len]='\0';
  7636.         }
  7637.     } /* Use specified name */
  7638.     else {
  7639.         char princ_realm[256];
  7640.         if ( (strlen(init->principal) +
  7641.               (init->instance ? strlen(init->instance)+1 : 0) +
  7642.               strlen(realm)
  7643.               + 2) > 255 )
  7644.              goto exit_k5_init;
  7645.  
  7646.         ckstrncpy(principal,init->principal,256);
  7647.         ckstrncpy(princ_realm,init->principal,256);
  7648.         if (init->instance) {
  7649.             ckstrncat(princ_realm,"/",256);
  7650.             ckstrncat(princ_realm,init->instance,256);
  7651.         }
  7652.         if (realm[0]) {
  7653.           ckstrncat(princ_realm,"@",256);
  7654.           ckstrncat(princ_realm,realm,256);
  7655.         }
  7656.         if ((code = krb5_parse_name (kcontext, princ_realm, &me))) {
  7657.             com_err("krb5_kinit",code,"when parsing name",princ_realm);
  7658.             goto exit_k5_init;
  7659.         }
  7660.     }
  7661.  
  7662.     if ((code = krb5_unparse_name(kcontext, me, &client_name))) {
  7663.         com_err("krb5_kinit",code,"when unparsing name");
  7664.         goto exit_k5_init;
  7665.     }
  7666.     debug(F110,"krb5_init client_name",client_name,0);
  7667.  
  7668.  
  7669.     memset((char *)&my_creds, 0, sizeof(my_creds));
  7670.     my_creds.client = me;
  7671.  
  7672.     if (init->service == NULL) {
  7673.         if ((code =
  7674.              krb5_build_principal_ext(kcontext,
  7675.                                       &server,
  7676.                                       strlen(realm),realm,
  7677.                                       tgtname.length, tgtname.data,
  7678.                                       strlen(realm),realm,
  7679.                                       0))) {
  7680.             com_err("krb5_kinit",code,"while building server name");
  7681.             goto exit_k5_init;
  7682.         }
  7683.     } else {
  7684.         if (code = krb5_parse_name(kcontext, init->service, &server)) {
  7685.             com_err("krb5_kinit",code,"while parsing service name",
  7686.                     init->service);
  7687.             goto exit_k5_init;
  7688.         }
  7689.     }
  7690.  
  7691.     my_creds.server = server;
  7692.  
  7693.     if (options & KDC_OPT_POSTDATED) {
  7694.         my_creds.times.starttime = starttime;
  7695.         my_creds.times.endtime = starttime + lifetime;
  7696.     } else {
  7697.         my_creds.times.starttime = 0;   /* start timer when request
  7698.                                            gets to KDC */
  7699.         my_creds.times.endtime = now + lifetime;
  7700.     }
  7701.     if (options & KDC_OPT_RENEWABLE) {
  7702.         my_creds.times.renew_till = now + rlife;
  7703.     } else
  7704.         my_creds.times.renew_till = 0;
  7705.  
  7706.     if (options & KDC_OPT_VALIDATE) {
  7707.         krb5_data outbuf;
  7708.  
  7709. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7710.         code = krb5_get_validated_creds(kcontext,
  7711.                                         &my_creds, me, ccache, init->service);
  7712.         if ( code == -1 )
  7713. #endif
  7714.         {
  7715. #ifdef HEIMDAL
  7716.             printf("?validate not implemented\r\n");
  7717.             code = -1;
  7718.             goto exit_k5_init;
  7719. #else /* HEIMDAL */
  7720.             code = krb5_validate_tgt(kcontext, ccache, server, &outbuf);
  7721. #endif /* HEIMDAL */
  7722.         }
  7723.         if (code) {
  7724.             com_err("krb5_kinit",code,"validating tgt");
  7725.             goto exit_k5_init;
  7726.         }
  7727.         /* should be done... */
  7728.         goto exit_k5_init;
  7729.     }
  7730.  
  7731.     if (options & KDC_OPT_RENEW) {
  7732.         krb5_data outbuf;
  7733.  
  7734. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7735.         code = krb5_get_renewed_creds(kcontext,
  7736.                                       &my_creds, me, ccache, init->service);
  7737.         if ( code == -1 )
  7738. #endif
  7739.         {
  7740. #ifdef HEIMDAL
  7741.             printf("?renew not implemented\r\n");
  7742.             code = -1;
  7743.             goto exit_k5_init;
  7744. #else /* HEIMDAL */
  7745.             code = krb5_renew_tgt(kcontext, ccache, server, &outbuf);
  7746. #endif /* HEIMDAL */
  7747.         }
  7748.         if (code) {
  7749.             com_err("krb5_kinit",code,"while renewing tgt");
  7750.             goto exit_k5_init;
  7751.         }
  7752.         /* should be done... */
  7753.         goto exit_k5_init;
  7754.     }
  7755.  
  7756. #ifndef HEIMDAL
  7757.     if ( init->addrs && !init->no_addresses ) {
  7758.         /* construct an array of krb5_address structs to pass to get_in_tkt */
  7759.         /* include both the local ip addresses as well as any other that    */
  7760.         /* are specified.                                                   */
  7761.         unsigned long ipaddr;
  7762.  
  7763.         for ( addr_count=0;addr_count<KRB5_NUM_OF_ADDRS;addr_count++ )
  7764.             if ( init->addrs[addr_count] == NULL )
  7765.                 break;
  7766.  
  7767.         if (addr_count > 0) {
  7768.             krb5_address ** local_addrs=NULL;
  7769.             krb5_os_localaddr(kcontext, &local_addrs);
  7770.             i = 0;
  7771.             while ( local_addrs[i] )
  7772.                 i++;
  7773.             addr_count += i;
  7774.  
  7775.             addrs = (krb5_address **)
  7776.               malloc((addr_count+1) * sizeof(krb5_address));
  7777.             if ( !addrs ) {
  7778.                 krb5_free_addresses(kcontext, local_addrs);
  7779.                 goto exit_k5_init;
  7780.             }
  7781.             memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
  7782.             i = 0;
  7783.             while ( local_addrs[i] ) {
  7784.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7785.                 if (addrs[i] == NULL) {
  7786.                     krb5_free_addresses(kcontext, local_addrs);
  7787.                     goto exit_k5_init;
  7788.                 }
  7789.  
  7790.                 addrs[i]->magic = local_addrs[i]->magic;
  7791.                 addrs[i]->addrtype = local_addrs[i]->addrtype;
  7792.                 addrs[i]->length = local_addrs[i]->length;
  7793.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  7794.                 if (!addrs[i]->contents) {
  7795.                     krb5_free_addresses(kcontext, local_addrs);
  7796.                     goto exit_k5_init;
  7797.                 }
  7798.  
  7799.                 memcpy(addrs[i]->contents,local_addrs[i]->contents,
  7800.                         local_addrs[i]->length);        /* safe */
  7801.                 i++;
  7802.             }
  7803.             krb5_free_addresses(kcontext, local_addrs);
  7804.  
  7805.             for ( j=0;i<addr_count;i++,j++ ) {
  7806.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7807.                 if (addrs[i] == NULL)
  7808.                     goto exit_k5_init;
  7809.  
  7810.                 addrs[i]->magic = KV5M_ADDRESS;
  7811.                 addrs[i]->addrtype = AF_INET;
  7812.                 addrs[i]->length = 4;
  7813.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  7814.                 if (!addrs[i]->contents)
  7815.                     goto exit_k5_init;
  7816.  
  7817.                 ipaddr = inet_addr(init->addrs[j]);
  7818.                 memcpy(addrs[i]->contents,&ipaddr,4);   /* safe */
  7819.             }
  7820. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7821.             krb5_get_init_creds_opt_set_address_list(&opts,addrs);
  7822. #endif
  7823.         }
  7824.      }
  7825. #endif /* !HEIMDAL */
  7826. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7827.     if ( init->no_addresses )
  7828.         krb5_get_init_creds_opt_set_address_list(&opts,NULL);
  7829. #endif
  7830.  
  7831. #ifndef NO_KEYTAB
  7832.     if (!use_keytab)
  7833. #endif
  7834.     {
  7835.         if ( init->password ) {
  7836.             pwsize = strlen(init->password);
  7837.             if ( pwsize )
  7838.                 password = init->password;
  7839.         } else if (init->getk4 && k4_init) {
  7840.             /* When we are requesting that K4 tickets be automatically */
  7841.             /* acquired when K5 tickets are acquired, we must get the  */
  7842.             /* password up front.                                      */
  7843.             char prmpt[256];
  7844.             extern char * k5prprompt;
  7845.             extern char * k5pwprompt;
  7846.             int ok = 0;
  7847.  
  7848.             if ( k5pwprompt && k5pwprompt[0] &&
  7849.                  (strlen(k5pwprompt) + strlen(principal) +
  7850.                   strlen(realm) - 4) < sizeof(prmpt)) {
  7851.                 sprintf(prmpt,k5pwprompt,principal,realm);
  7852.             } else
  7853.                 ckmakxmsg(prmpt,sizeof(prmpt),
  7854.                            k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  7855.                            "Kerberos 5 Password for ",
  7856.                            principal,"@",realm,": ",
  7857.                            NULL,NULL,NULL,NULL,NULL,NULL,NULL
  7858.                            );
  7859.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7860.             if ( ok )
  7861.                 password = passwd;
  7862.  
  7863.             if ( k4_init->password == NULL )
  7864.                 makestr(&k4_init->password,passwd);
  7865.         }
  7866. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7867.         debug(F100,"krb5_init calling krb5_get_init_creds_password()","",0);
  7868. #ifdef OS2
  7869.         if ( is_NRL_KRB5() )
  7870.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  7871.                                                  password,
  7872.                                                  (void *)ck_NRL_krb5_prompter,
  7873.                                                  NULL,
  7874.                                                  starttime, init->service,
  7875.                                                  &opts);
  7876.         else
  7877. #endif /* OS2 */
  7878.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  7879.                                                  password,
  7880.                                                  ck_krb5_prompter,
  7881.                                                  NULL,
  7882.                                                  starttime, init->service,
  7883.                                                  &opts);
  7884.         debug(F111,"krb5_init","krb5_get_init_creds_password()",code);
  7885.  
  7886.         if ( code == -1 )
  7887.         {
  7888.             if (!password) {
  7889.                 char prmpt[256];
  7890.                 int ok;
  7891.  
  7892.                 ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  7893.                           client_name,": ",NULL);
  7894.                 ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7895.                 if ( ok )
  7896.                     password = passwd;
  7897.             }
  7898.  
  7899.             if ( !password[0] ) {
  7900.                 debug(F111,"krb5_init","no password specified",0);
  7901.                 printf("A password must be specified for %s.\r\n",client_name);
  7902.                 goto exit_k5_init;
  7903.             }
  7904.             code = krb5_get_in_tkt_with_password(kcontext, options,
  7905. #ifdef HEIMDAL
  7906.                                                   NULL,
  7907. #else /* HEIMDAL */
  7908.                                             init->no_addresses ? NULL :addrs,
  7909. #endif /* HEIMDAL */
  7910.                                                   NULL, NULL,
  7911.                                                   password,
  7912.                                                   NULL, &my_creds, NULL);
  7913.             if ( code )
  7914.                 debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  7915.         }
  7916. #else /* KRB5_HAVE_GET_INIT_CREDS */
  7917.         if (!password) {
  7918.             char prmpt[256];
  7919.             int ok;
  7920.  
  7921.             ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  7922.                       client_name,": ",NULL);
  7923.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7924.             if ( ok )
  7925.                 password = passwd;
  7926.         }
  7927.  
  7928.         if ( !password[0] ) {
  7929.             debug(F111,"krb5_init","no password specified",0);
  7930.             printf("A password must be specified for %s.\r\n",client_name);
  7931.             goto exit_k5_init;
  7932.         }
  7933.         code = krb5_get_in_tkt_with_password(kcontext, options,
  7934. #ifdef HEIMDAL
  7935.                                               NULL,
  7936. #else /* HEIMDAL */
  7937.                                             init->no_addresses ? NULL :addrs,
  7938. #endif /* HEIMDAL */
  7939.                                               NULL, NULL,
  7940.                                               password,
  7941.                                               NULL, &my_creds, NULL);
  7942.         if ( code )
  7943.             debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  7944. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7945.  
  7946.         if ( init->password && pwsize > 0 )
  7947.             memset(init->password, 0, pwsize);
  7948.         memset(passwd,0,80);
  7949.     }
  7950. #ifndef NO_KEYTAB
  7951.     else {
  7952. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7953.         code = krb5_get_init_creds_keytab(kcontext, &my_creds, me, keytab,
  7954.                                            starttime, init->service,
  7955.                                            &opts);
  7956. #ifdef OS2
  7957.         if ( code == -1)
  7958.             code = krb5_get_in_tkt_with_keytab(kcontext, options,
  7959.                                            init->no_addresses ? NULL :addrs,
  7960.                                                 NULL, NULL, keytab, NULL,
  7961.                                                 &my_creds, 0);
  7962. #endif /* OS2 */
  7963. #else /* KRB5_HAVE_GET_INIT_CREDS */
  7964.         code = krb5_get_in_tkt_with_keytab(kcontext, options,
  7965. #ifdef HEIMDAL
  7966.                                                   NULL,
  7967. #else /* HEIMDAL */
  7968.                                             init->no_addresses ? NULL :addrs,
  7969. #endif /* HEIMDAL */
  7970.                                             NULL, NULL, keytab, NULL,
  7971.                                             &my_creds, 0);
  7972. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7973.     }
  7974. #endif
  7975.  
  7976.     if (code) {
  7977.         switch (code) {
  7978.         case KRB5KRB_AP_ERR_BAD_INTEGRITY:
  7979.             printf("Password incorrect\r\n");
  7980.             goto exit_k5_init;
  7981.         case KRB5KRB_AP_ERR_V4_REPLY:
  7982.             if (init->getk4 && k4_init) {
  7983.                 printf("Kerberos 5 Tickets not support by server.  ");
  7984.                 printf("A version 4 Ticket will be requested.\r\n");
  7985.             }
  7986.             goto exit_k5_init;
  7987.         default:
  7988.             goto exit_k5_init;
  7989.         }
  7990.     }
  7991.  
  7992.     debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  7993.  
  7994.     code = krb5_cc_initialize (kcontext, ccache, me);
  7995.     if ( code == KRB5_CC_BADNAME ) {
  7996.         /* This is a really ugly hack that should not have to be here.
  7997.          * krb5_cc_initialize should not fail with an error if the
  7998.          * cache already exists.  The reason the problem is occuring
  7999.          * is that the krb5 library is no longer calling cc_destroy()
  8000.          * when cc_initialize() is called and the CCAPI implementation
  8001.          * on Windows has not yet been corrected to handle it.  To
  8002.          * ensure that K95 will continue to work with both we will call
  8003.          * cc_destroy() if the cc_initialize() call fails with a BADNAME
  8004.          * error.  If the cc_destroy() is successful, we will try again.
  8005.          */
  8006.  
  8007.         debug(F100,"krb5_init calling krb5_cc_destroy()","",0);
  8008.         code = krb5_cc_destroy (kcontext, ccache);
  8009.         if ( !code ) {
  8010.             debug(F100,"krb5_init calling k5_get_ccache()","",0);
  8011.             code = k5_get_ccache(kcontext,&ccache,op->cache);
  8012.             debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8013.             code = krb5_cc_initialize (kcontext, ccache, me);
  8014.         } else
  8015.             code = KRB5_CC_BADNAME;
  8016.     }
  8017.     if (code) {
  8018.         com_err("krb5_kinit",code,"when initializing cache",op->cache);
  8019.         goto exit_k5_init;
  8020.     }
  8021.  
  8022.     debug(F100,"krb5_init calling krb5_cc_store_cred()","",0);
  8023.     code = krb5_cc_store_cred(kcontext, ccache, &my_creds);
  8024.     if (code) {
  8025.         com_err("krb5_kinit",code,"while storing credentials");
  8026.         goto exit_k5_init;
  8027.     }
  8028.  
  8029. exit_k5_init:
  8030.     debug(F100,"krb5_init exit_k5_init","",0);
  8031.  
  8032. #ifndef HEIMDAL
  8033.     /* Free krb5_address structures if we created them */
  8034.     if ( addrs ) {
  8035.         for ( i=0;i<addr_count;i++ ) {
  8036.             if ( addrs[i] ) {
  8037.                 if ( addrs[i]->contents )
  8038.                     free(addrs[i]->contents);
  8039.                 free(addrs[i]);
  8040.             }
  8041.         }
  8042.     }
  8043. #endif /* HEIMDAL */
  8044.  
  8045.     /* my_creds is pointing at server */
  8046.     debug(F100,"krb5_init calling krb5_free_principal()","",0);
  8047.     krb5_free_principal(kcontext, server);
  8048.     debug(F100,"krb5_init calling krb5_cc_close()","",0);
  8049.     krb5_cc_close(kcontext,ccache);
  8050.     debug(F100,"krb5_init calling krb5_free_context()","",0);
  8051.     krb5_free_context(kcontext);
  8052.  
  8053.     krb5_errno = code;
  8054.     makestr(&krb5_errmsg,krb5_errno ? error_message(krb5_errno) : "OK");
  8055.  
  8056.     if ( init->getk4 && k4_init ) {
  8057.         int k4rc = ck_krb4_initTGT(op,k4_init);
  8058.         return (k4rc < 0 ? -1 : 0);
  8059.     }
  8060.  
  8061.     printf("Result from realm %s: %s\r\n",realm,
  8062.             code?error_message(code):"OK");
  8063.     return(code?-1:0);
  8064. }
  8065.  
  8066. #ifndef HEIMDAL
  8067. #define VALIDATE 0
  8068. #define RENEW 1
  8069.  
  8070. /* stripped down version of krb5_mk_req */
  8071. static krb5_error_code
  8072. #ifdef CK_ANSIC
  8073. krb5_validate_tgt( krb5_context context,
  8074.                    krb5_ccache ccache,
  8075.                    krb5_principal     server, /* tgtname */
  8076.                    krb5_data *outbuf )
  8077. #else
  8078. krb5_validate_tgt(context, ccache, server, outbuf)
  8079.      krb5_context context;
  8080.      krb5_ccache ccache;
  8081.      krb5_principal     server; /* tgtname */
  8082.      krb5_data *outbuf;
  8083. #endif
  8084. {
  8085.     return krb5_tgt_gen(context, ccache, server, outbuf, VALIDATE);
  8086. }
  8087.  
  8088. /* stripped down version of krb5_mk_req */
  8089. static krb5_error_code
  8090. #ifdef CK_ANSIC
  8091. krb5_renew_tgt(krb5_context context,
  8092.                 krb5_ccache ccache,
  8093.                 krb5_principal    server, /* tgtname */
  8094.                 krb5_data *outbuf)
  8095. #else
  8096. krb5_renew_tgt(context, ccache, server, outbuf)
  8097.      krb5_context context;
  8098.      krb5_ccache ccache;
  8099.      krb5_principal       server; /* tgtname */
  8100.      krb5_data *outbuf;
  8101. #endif
  8102. {
  8103.     return krb5_tgt_gen(context, ccache, server, outbuf, RENEW);
  8104. }
  8105.  
  8106.  
  8107. /* stripped down version of krb5_mk_req */
  8108. static krb5_error_code
  8109. #ifdef CK_ANSIC
  8110. krb5_tgt_gen(krb5_context context,
  8111.               krb5_ccache ccache,
  8112.               krb5_principal      server, /* tgtname */
  8113.               krb5_data *outbuf,
  8114.               int opt)
  8115. #else
  8116. krb5_tgt_gen(context, ccache, server, outbuf, opt)
  8117.      krb5_context context;
  8118.      krb5_ccache ccache;
  8119.      krb5_principal       server; /* tgtname */
  8120.      krb5_data *outbuf;
  8121.      int opt;
  8122. #endif
  8123. {
  8124.     krb5_error_code       retval;
  8125.     krb5_creds          * credsp;
  8126.     krb5_creds            creds;
  8127.  
  8128.     /* obtain ticket & session key */
  8129.     memset((char *)&creds, 0, sizeof(creds));
  8130.     if ((retval = krb5_copy_principal(context, server, &creds.server)))
  8131.         goto cleanup;
  8132.  
  8133.     if ((retval = krb5_cc_get_principal(context, ccache, &creds.client)))
  8134.         goto cleanup_creds;
  8135.  
  8136.     if (opt == VALIDATE) {
  8137.         if ((retval = krb5_get_credentials_validate(context, 0,
  8138.                                                     ccache, &creds, &credsp)))
  8139.           goto cleanup_creds;
  8140.     } else {
  8141.         if ((retval = krb5_get_credentials_renew(context, 0,
  8142.                                                  ccache, &creds, &credsp)))
  8143.           goto cleanup_creds;
  8144.     }
  8145.  
  8146.     /* we don't actually need to do the mk_req, just get the creds. */
  8147. cleanup_creds:
  8148.     krb5_free_cred_contents(context, &creds);
  8149.  
  8150. cleanup:
  8151.  
  8152.     return retval;
  8153. }
  8154. #endif /* HEIMDAL */
  8155. #endif /* KINIT */
  8156. #ifdef KDESTROY
  8157. int
  8158. #ifdef CK_ANSIC
  8159. ck_krb5_destroy(struct krb_op_data * op)
  8160. #else
  8161. ck_krb5_destroy(op) struct krb_op_data * op;
  8162. #endif
  8163. {
  8164.     krb5_context kcontext;
  8165.     krb5_error_code retval;
  8166.     int c;
  8167.     krb5_ccache ccache = NULL;
  8168.     char *cache_name = NULL;
  8169.     int code;
  8170.     int errflg=0;
  8171.     int quiet = 0;
  8172.  
  8173.     if ( !ck_krb5_is_installed() )
  8174.         return(-1);
  8175.  
  8176.     code = krb5_init_context(&kcontext);
  8177.     if (code) {
  8178.         debug(F101,"ck_krb5_destroy while initializing krb5","",code);
  8179.         krb5_errno = code;
  8180.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8181.         return(-1);
  8182.     }
  8183.  
  8184.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  8185.     if (code != 0) {
  8186.         debug(F101,"ck_krb5_destroy while getting ccache",
  8187.                "",code);
  8188.         krb5_free_context(kcontext);
  8189.         krb5_errno = code;
  8190.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8191.         return(-1);
  8192.     }
  8193.  
  8194.     code = krb5_cc_destroy (kcontext, ccache);
  8195.     if (code != 0) {
  8196.         debug(F101,"ck_krb5_destroy while destroying cache","",code);
  8197.         if ( code == KRB5_FCC_NOFILE )
  8198.             printf("No ticket cache to destroy.\r\n");
  8199.         else
  8200.             printf("Ticket cache NOT destroyed!\r\n");
  8201.         krb5_cc_close(kcontext,ccache);
  8202.         krb5_free_context(kcontext);
  8203.         krb5_errno = code;
  8204.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8205.         return(-1);
  8206.     }
  8207.  
  8208.     printf("Tickets destroyed.\r\n");
  8209.     /* Do not call krb5_cc_close() because cache has been destroyed */
  8210.     krb5_free_context(kcontext);
  8211.     krb5_errno = 0;
  8212.     makestr(&krb5_errmsg,"OK");
  8213.     return (0);
  8214. }
  8215. #else /* KDESTROY */
  8216. #ifdef KRB5
  8217. int
  8218. #ifdef CK_ANSIC
  8219. ck_krb5_destroy(struct krb_op_data * op)
  8220. #else
  8221. ck_krb5_destroy(op) struct krb_op_data * op;
  8222. #endif
  8223. {
  8224.     printf("?Not implemented.\r\n");
  8225.     return(-1);
  8226. }
  8227. #endif /* KRB5 */
  8228. #endif /* KDESTROY */
  8229. #ifndef KLIST
  8230. #ifdef KRB5
  8231. int
  8232. #ifdef CK_ANSIC
  8233. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8234. #else
  8235. ck_krb5_list_creds(op,lc)
  8236.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8237. #endif
  8238. {
  8239.     printf("?Not implemented.\r\n");
  8240.     return(-1);
  8241. }
  8242. #endif /* KRB5 */
  8243. #else /* KLIST */
  8244. static int show_flags = 0, show_time = 0, status_only = 0, show_keys = 0;
  8245. static int show_etype = 0, show_addr = 0;
  8246. static char *defname;
  8247. static char *progname;
  8248. static krb5_int32 now;
  8249. static int timestamp_width;
  8250.  
  8251. _PROTOTYP(static char * etype_string, (krb5_enctype ));
  8252. _PROTOTYP(static void show_credential,(krb5_context,krb5_creds *));
  8253. _PROTOTYP(static int do_ccache, (krb5_context,char *));
  8254. _PROTOTYP(static int do_keytab, (krb5_context,char *));
  8255. _PROTOTYP(static void printtime, (time_t));
  8256. _PROTOTYP(static void fillit, (int, int));
  8257.  
  8258. #define DEFAULT 0
  8259. #define CCACHE 1
  8260. #define KEYTAB 2
  8261.  
  8262. int
  8263. #ifdef CK_ANSIC
  8264. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8265. #else
  8266. ck_krb5_list_creds(op,lc)
  8267.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8268. #endif
  8269. {
  8270.     krb5_context kcontext;
  8271.     krb5_error_code retval;
  8272.     int code;
  8273.     char *name = op->cache;
  8274.     int mode;
  8275.  
  8276.     if ( !ck_krb5_is_installed() )
  8277.         return(-1);
  8278.  
  8279.     code = krb5_init_context(&kcontext);
  8280.     if (code) {
  8281.         debug(F101,"ck_krb5_list_creds while initializing krb5","",code);
  8282.         krb5_errno = code;
  8283.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8284.         return(-1);
  8285.     }
  8286.  
  8287.     name = op->cache;
  8288.     mode = DEFAULT;
  8289.     show_flags = 0;
  8290.     show_time = 0;
  8291.     status_only = 0;
  8292.     show_keys = 0;
  8293.     show_etype = 0;
  8294.     show_addr = 0;
  8295.  
  8296.     show_flags = lc->flags;
  8297.     show_etype = lc->encryption;
  8298.     show_addr  = lc->addr;
  8299.     show_time = 1;
  8300.     show_keys = 1;
  8301.     mode = CCACHE;
  8302.  
  8303.     if ((code = krb5_timeofday(kcontext, &now))) {
  8304.         if (!status_only)
  8305.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  8306.                    "",code);
  8307.         krb5_free_context(kcontext);
  8308.         krb5_errno = code;
  8309.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8310.         return(-1);
  8311.     }
  8312.     else {
  8313.         char tmp[BUFSIZ];
  8314.  
  8315.         if (!krb5_timestamp_to_sfstring(now, tmp, 20, (char *) NULL) ||
  8316.             !krb5_timestamp_to_sfstring(now, tmp, sizeof(tmp), (char *) NULL))
  8317.             timestamp_width = (int) strlen(tmp);
  8318.         else
  8319.             timestamp_width = 15;
  8320.     }
  8321.  
  8322.     if (mode == DEFAULT || mode == CCACHE)
  8323.          retval = do_ccache(kcontext,name);
  8324.     else
  8325.          retval = do_keytab(kcontext,name);
  8326.     krb5_free_context(kcontext);
  8327.     return(retval);
  8328. }
  8329.  
  8330. static int
  8331. #ifdef CK_ANSIC
  8332. do_keytab(krb5_context kcontext, char * name)
  8333. #else
  8334. do_keytab(kcontext,name) krb5_context kcontext; char * name;
  8335. #endif
  8336. {
  8337.     krb5_keytab kt;
  8338.     krb5_keytab_entry entry;
  8339.     krb5_kt_cursor cursor;
  8340.     char buf[BUFSIZ]; /* hopefully large enough for any type */
  8341.     char *pname;
  8342.     int code = 0;
  8343.  
  8344.     if (name == NULL) {
  8345.         if ((code = krb5_kt_default(kcontext, &kt))) {
  8346.             debug(F101,"ck_krb5_list_creds while getting default keytab",
  8347.                    "",code);
  8348.             krb5_errno = code;
  8349.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8350.             return(-1);
  8351.         }
  8352.     } else {
  8353.         if ((code = krb5_kt_resolve(kcontext, name, &kt))) {
  8354.             debug(F111,"ck_krb5_list_creds while resolving keytab",
  8355.                      name,code);
  8356.             krb5_errno = code;
  8357.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8358.             return(-1);
  8359.         }
  8360.     }
  8361.  
  8362.     if ((code = krb5_kt_get_name(kcontext, kt, buf, BUFSIZ))) {
  8363.         debug(F101,"ck_krb5_list_creds while getting keytab name",
  8364.                "",code);
  8365.         krb5_errno = code;
  8366.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8367.         return(-1);
  8368.     }
  8369.  
  8370.      printf("Keytab name: %s\r\n", buf);
  8371.  
  8372.      if ((code = krb5_kt_start_seq_get(kcontext, kt, &cursor))) {
  8373.          debug(F101,"ck_krb5_list_creds while starting keytab scan",
  8374.                 "",code);
  8375.          krb5_errno = code;
  8376.          makestr(&krb5_errmsg,error_message(krb5_errno));
  8377.          return(-1);
  8378.      }
  8379.  
  8380.      if (show_time) {
  8381.           printf("KVNO Timestamp");
  8382.           fillit(timestamp_width - sizeof("Timestamp") + 2, (int) ' ');
  8383.           printf("Principal\r\n");
  8384.           printf("---- ");
  8385.           fillit(timestamp_width, (int) '-');
  8386.           printf(" ");
  8387.           fillit(78 - timestamp_width - sizeof("KVNO"), (int) '-');
  8388.           printf("\r\n");
  8389.      } else {
  8390.           printf("KVNO Principal\r\n");
  8391.           printf(
  8392. "---- --------------------------------------------------------------------\
  8393. ------\r\n");
  8394.      }
  8395.  
  8396.     while ((code = krb5_kt_next_entry(kcontext, kt, &entry, &cursor)) == 0) {
  8397.         if ((code = krb5_unparse_name(kcontext, entry.principal, &pname))) {
  8398.             debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8399.                    "",code);
  8400.             krb5_errno = code;
  8401.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8402.             return(-1);
  8403.         }
  8404.         printf("%4d ", entry.vno);
  8405.         if (show_time) {
  8406.             printtime(entry.timestamp);
  8407.             printf(" ");
  8408.         }
  8409.         printf("%s", pname);
  8410.         if (show_etype)
  8411.             printf(" (%s) " ,
  8412. #ifdef HEIMDAL
  8413.                     etype_string(entry.key.keytype)
  8414. #else /* HEIMDAL */
  8415.                     etype_string(entry.key.enctype)
  8416. #endif /* HEIMDAL */
  8417.                     );
  8418.         if (show_keys) {
  8419.             printf(" (0x");
  8420.             {
  8421.                 int i;
  8422.                 for (i = 0; i < entry.key.length; i++)
  8423.                     printf("%02x",
  8424. #ifdef HEIMDAL
  8425.                             entry.key.keyvalue[i]
  8426. #else /* HEIMDAL */
  8427.                             entry.key.contents[i]
  8428. #endif /* HEIMDAL */
  8429.                             );
  8430.             }
  8431.             printf(")");
  8432.         }
  8433.         printf("\r\n");
  8434.         krb5_free_unparsed_name(kcontext,pname);
  8435.     }
  8436.     if (code && code != KRB5_KT_END) {
  8437.         debug(F101,"ck_krb5_list_creds while scanning keytab",
  8438.                "",code);
  8439.         krb5_errno = code;
  8440.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8441.         return(-1);
  8442.     }
  8443.     if ((code = krb5_kt_end_seq_get(kcontext, kt, &cursor))) {
  8444.         debug(F101,"ck_krb5_list_creds while ending keytab scan",
  8445.                "",code);
  8446.         krb5_errno = code;
  8447.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8448.         return(-1);
  8449.     }
  8450.     krb5_errno = 0;
  8451.     makestr(&krb5_errmsg,"OK");
  8452.     return(0);
  8453. }
  8454.  
  8455. static int
  8456. #ifdef CK_ANSIC
  8457. do_ccache(krb5_context kcontext, char * cc_name)
  8458. #else
  8459. do_ccache(kcontext,name) krb5_context kcontext; char * cc_name;
  8460. #endif
  8461. {
  8462.     krb5_ccache cache = NULL;
  8463.     krb5_cc_cursor cur;
  8464.     krb5_creds creds;
  8465.     krb5_principal princ=NULL;
  8466.     krb5_flags flags=0;
  8467.     krb5_error_code code = 0;
  8468.     int exit_status = 0;
  8469.  
  8470.     if (status_only)
  8471.         /* exit_status is set back to 0 if a valid tgt is found */
  8472.         exit_status = 1;
  8473.  
  8474.     code = k5_get_ccache(kcontext,&cache,cc_name);
  8475.     if (code != 0) {
  8476.         debug(F111,"do_ccache while getting ccache",
  8477.                error_message(code),code);
  8478.         krb5_errno = code;
  8479.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8480.         return(-1);
  8481.     }
  8482.  
  8483.     flags = 0;                          /* turns off OPENCLOSE mode */
  8484.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8485.         if (code == ENOENT) {
  8486.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  8487.                    krb5_cc_get_name(kcontext, cache),code);
  8488.         } else {
  8489.             debug(F111,
  8490.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  8491.                   krb5_cc_get_name(kcontext, cache),code);
  8492.         }
  8493.         printf("No ticket File.\r\n");
  8494.         krb5_errno = code;
  8495.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8496.         krb5_cc_close(kcontext,cache);
  8497.         return(-1);
  8498.     }
  8499.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  8500.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  8501.                "",code);
  8502.         krb5_errno = code;
  8503.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8504.         krb5_cc_close(kcontext,cache);
  8505.         return(-1);
  8506.     }
  8507.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  8508.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8509.                "",code);
  8510.         krb5_errno = code;
  8511.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8512.         krb5_cc_close(kcontext,cache);
  8513.         return(-1);
  8514.     }
  8515.     if (!status_only) {
  8516.         printf("Ticket cache:      %s:%s\r\nDefault principal: %s\r\n\r\n",
  8517.                 krb5_cc_get_type(kcontext, cache),
  8518.                 krb5_cc_get_name(kcontext, cache), defname);
  8519.         printf("Valid starting");
  8520.         fillit(timestamp_width - sizeof("Valid starting") + 3,
  8521.                (int) ' ');
  8522.         printf("Expires");
  8523.         fillit(timestamp_width - sizeof("Expires") + 3,
  8524.                (int) ' ');
  8525.         printf("Service principal\r\n");
  8526.     }
  8527.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  8528.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  8529.                "",code);
  8530.         krb5_errno = code;
  8531.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8532.         krb5_cc_close(kcontext,cache);
  8533.         return(-1);
  8534.     }
  8535.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  8536.         if (status_only) {
  8537.             if (exit_status && creds.server->length == 2 &&
  8538.                 strcmp(creds.server->realm.data, princ->realm.data) == 0 &&
  8539.                 strcmp((char *)creds.server->data[0].data, "krbtgt") == 0 &&
  8540.                 strcmp((char *)creds.server->data[1].data,
  8541.                        princ->realm.data) == 0 &&
  8542.                 creds.times.endtime > now)
  8543.                 exit_status = 0;
  8544.         } else {
  8545.             show_credential(kcontext, &creds);
  8546.         }
  8547.         krb5_free_cred_contents(kcontext, &creds);
  8548.     }
  8549.     printf("\r\n");
  8550.     if (code == KRB5_CC_END || code == KRB5_CC_NOTFOUND) {
  8551.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  8552.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  8553.                    "",code);
  8554.             krb5_errno = code;
  8555.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8556.             krb5_cc_close(kcontext,cache);
  8557.             return(-1);
  8558.         }
  8559.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  8560.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8561.             debug(F101,"ck_krb5_list_creds while closing ccache",
  8562.                    "",code);
  8563.             krb5_errno = code;
  8564.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8565.             krb5_cc_close(kcontext,cache);
  8566.             return(-1);
  8567.         }
  8568.         krb5_errno = 0;
  8569.         makestr(&krb5_errmsg,"OK");
  8570.         krb5_cc_close(kcontext,cache);
  8571.         return(0);
  8572.     } else {
  8573.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  8574.         krb5_errno = code;
  8575.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8576.         krb5_cc_close(kcontext,cache);
  8577.         return(-1);
  8578.     }
  8579.     krb5_errno = 0;
  8580.     makestr(&krb5_errmsg,"OK");
  8581.     krb5_cc_close(kcontext,cache);
  8582.     return(0);
  8583. }
  8584.  
  8585. static char *
  8586. #ifdef CK_ANSIC
  8587. #ifdef HEIMDAL
  8588. etype_string(krb5_keytype enctype)
  8589. #else /* HEIMDAL */
  8590. etype_string(krb5_enctype enctype)
  8591. #endif /* HEIMDAL */
  8592. #else
  8593. #ifdef HEIMDAL
  8594. etype_string(enctype) krb5_keytype enctype;
  8595. #else /* HEIMDAL */
  8596. etype_string(enctype) krb5_enctype enctype;
  8597. #endif /* HEIMDAL */
  8598. #endif
  8599. {
  8600.     static char buf[12];
  8601.  
  8602.     switch (enctype) {
  8603.     case ENCTYPE_NULL:
  8604.         return "NULL";
  8605.     case ENCTYPE_DES_CBC_CRC:
  8606.         return "DES-CBC-CRC";
  8607.     case ENCTYPE_DES_CBC_MD4:
  8608.         return "DES-CBC-MD4";
  8609.     case ENCTYPE_DES_CBC_MD5:
  8610.         return "DES-CBC-MD5";
  8611.     case ENCTYPE_DES_CBC_RAW:
  8612.         return "DES-CBC-RAW";
  8613.     case ENCTYPE_DES3_CBC_SHA:
  8614.         return "DES3-CBC-SHA";
  8615.     case ENCTYPE_DES3_CBC_RAW:
  8616.         return "DES3-CBC-RAW";
  8617.     case ENCTYPE_DES_HMAC_SHA1:
  8618.         return "DES-HMAC-SHA1";
  8619.     case ENCTYPE_DES3_CBC_SHA1:
  8620.         return "DES3-CBC-SHA1";
  8621.     case ENCTYPE_UNKNOWN:
  8622.         return "UNKNOWN";
  8623.     case ENCTYPE_LOCAL_DES3_HMAC_SHA1:
  8624.         return "LOCAL-DES3-HMAC-SHA1";
  8625.     default:
  8626.         ckmakmsg(buf, sizeof(buf),"etype ", ckitoa(enctype),NULL,NULL);
  8627.         return buf;
  8628.         break;
  8629.     }
  8630. }
  8631.  
  8632. static char *
  8633. #ifdef CK_ANSIC
  8634. flags_string(register krb5_creds *cred)
  8635. #else
  8636. flags_string(cred) register krb5_creds *cred;
  8637. #endif
  8638. {
  8639.     static char buf[32];
  8640.     int i = 0;
  8641.  
  8642.     if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
  8643.         buf[i++] = 'F';
  8644.     if (cred->ticket_flags & TKT_FLG_FORWARDED)
  8645.         buf[i++] = 'f';
  8646.     if (cred->ticket_flags & TKT_FLG_PROXIABLE)
  8647.         buf[i++] = 'P';
  8648.     if (cred->ticket_flags & TKT_FLG_PROXY)
  8649.         buf[i++] = 'p';
  8650.     if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
  8651.         buf[i++] = 'D';
  8652.     if (cred->ticket_flags & TKT_FLG_POSTDATED)
  8653.         buf[i++] = 'd';
  8654.     if (cred->ticket_flags & TKT_FLG_INVALID)
  8655.         buf[i++] = 'i';
  8656.     if (cred->ticket_flags & TKT_FLG_RENEWABLE)
  8657.         buf[i++] = 'R';
  8658.     if (cred->ticket_flags & TKT_FLG_INITIAL)
  8659.         buf[i++] = 'I';
  8660.     if (cred->ticket_flags & TKT_FLG_HW_AUTH)
  8661.         buf[i++] = 'H';
  8662.     if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
  8663.         buf[i++] = 'A';
  8664.     buf[i] = '\0';
  8665.     return(buf);
  8666. }
  8667.  
  8668. static char   *
  8669. #ifdef CK_ANSIC
  8670. short_date(long   *dp)
  8671. #else
  8672. short_date(dp) long   *dp;
  8673. #endif
  8674. {
  8675.     register char *cp;
  8676. #ifndef ctime
  8677.     extern char *ctime();
  8678. #endif /* ctime */
  8679.     cp = ctime(dp) + 4;
  8680.     cp[15] = '\0';
  8681.     return (cp);
  8682. }
  8683.  
  8684.  
  8685. static VOID
  8686. #ifdef CK_ANSIC
  8687. printtime(time_t tv)
  8688. #else
  8689. printtime(tv) time_t tv;
  8690. #endif
  8691. {
  8692.     char timestring[BUFSIZ];
  8693.     char format[12];
  8694.     char fill;
  8695.  
  8696.     fill = ' ';
  8697.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  8698.     if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv,
  8699.                                      timestring,
  8700.                                      timestamp_width+1,
  8701.                                      &fill)) {
  8702.         printf(format,timestring);
  8703.     }
  8704.     else {
  8705.         printf(format,short_date(&tv));
  8706.     }
  8707.  
  8708. }
  8709.  
  8710. static void
  8711. #ifdef CK_ANSIC
  8712. one_addr(krb5_address *a)
  8713. #else
  8714. one_addr(a) krb5_address *a;
  8715. #endif
  8716. {
  8717.     struct hostent *h;
  8718.     extern tcp_rdns;
  8719.  
  8720.     if ((a->addrtype == ADDRTYPE_INET) &&
  8721.         (a->length == 4)) {
  8722.         if (tcp_rdns != SET_OFF) {
  8723.             h = gethostbyaddr(a->contents, 4, AF_INET);
  8724.             if (h) {
  8725. #ifdef HADDRLIST
  8726.                 h = ck_copyhostent(h);
  8727. #endif /* HADDRLIST */
  8728.                 printf("%s (%d.%d.%d.%d)", h->h_name,
  8729.                         a->contents[0], a->contents[1],
  8730.                         a->contents[2], a->contents[3]);
  8731.             }
  8732.         }
  8733.         if (tcp_rdns == SET_OFF || !h) {
  8734.             printf("%d.%d.%d.%d", a->contents[0], a->contents[1],
  8735.                    a->contents[2], a->contents[3]);
  8736.         }
  8737.     } else {
  8738.         printf("unknown addr type %d", a->addrtype);
  8739.     }
  8740. }
  8741.  
  8742. static VOID
  8743. #ifdef CK_ANSIC
  8744. show_credential(krb5_context kcontext, register krb5_creds * cred)
  8745. #else
  8746. show_credential(kcontext, cred)
  8747.     krb5_context          kcontext;
  8748.     register krb5_creds * cred;
  8749. #endif
  8750. {
  8751.     krb5_error_code retval=0;
  8752.     krb5_ticket *tkt=NULL;
  8753.     char *name=NULL, *sname=NULL, *flags=NULL;
  8754.     int extra_field = 0;
  8755.  
  8756.     retval = krb5_unparse_name(kcontext, cred->client, &name);
  8757.     if (retval) {
  8758.         debug(F101,"ck_krb5_list_creds while unparsing client name","",retval);
  8759.         krb5_errno = retval;
  8760.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8761.         return;
  8762.     }
  8763.     retval = krb5_unparse_name(kcontext, cred->server, &sname);
  8764.     if (retval) {
  8765.         debug(F101,"ck_krb5_list_creds while unparsing server name","",retval);
  8766.         free(name);
  8767.         krb5_errno = retval;
  8768.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8769.         return;
  8770.     }
  8771.     if (!cred->times.starttime)
  8772.         cred->times.starttime = cred->times.authtime;
  8773.  
  8774.     printtime(cred->times.starttime);
  8775.     printf("  ");
  8776.  
  8777.     if ( time(0) < cred->times.endtime )
  8778.         printtime(cred->times.endtime);
  8779.     else
  8780.         printf("** expired ** ");
  8781.  
  8782.     printf("  %s\r\n", sname);
  8783.  
  8784.     if (strcmp(name, defname)) {
  8785.         printf("   for client %s", name);
  8786.         extra_field++;
  8787.     }
  8788.  
  8789.     if (cred->times.renew_till) {
  8790.         if (!extra_field)
  8791.             printf("   ");
  8792.         else
  8793.             printf(", ");
  8794.         printf("renew until ");
  8795.         printtime(cred->times.renew_till);
  8796.         extra_field += 2;
  8797.     }
  8798.  
  8799.     if (extra_field > 3) {
  8800.         printf("\r\n");
  8801.         extra_field = 0;
  8802.     }
  8803.  
  8804.     if (show_flags) {
  8805.         flags = flags_string(cred);
  8806.         if (flags && *flags) {
  8807.             if (!extra_field)
  8808.                 printf("   ");
  8809.             else
  8810.                 printf(", ");
  8811.             printf("Flags: %s", flags);
  8812.             extra_field++;
  8813.         }
  8814.     }
  8815.  
  8816.     if (extra_field > 2) {
  8817.         printf("\r\n");
  8818.         extra_field = 0;
  8819.     }
  8820.  
  8821.     if (show_etype) {
  8822.         retval = decode_krb5_ticket(&cred->ticket, &tkt);
  8823.         if (!extra_field)
  8824.             printf("   ");
  8825.         else
  8826.             printf(", ");
  8827. #ifdef HEIMDAL
  8828.         printf("Etype (skey, tkt): %s, %s ",
  8829.                etype_string(cred->session.keytype),
  8830.                etype_string(tkt->enc_part.keytype));
  8831. #else /* HEIMDAL */
  8832.         printf("Etype (skey, tkt): %s, %s ",
  8833.                etype_string(cred->keyblock.enctype),
  8834.                etype_string(tkt->enc_part.enctype));
  8835. #endif /* HEIMDAL */
  8836.         krb5_free_ticket(kcontext, tkt);
  8837.         extra_field++;
  8838.     }
  8839.  
  8840.     /* if any additional info was printed, extra_field is non-zero */
  8841.     if (extra_field)
  8842.         printf("\r\n");
  8843.  
  8844.     if ( show_addr ) {
  8845.         if (!cred->addresses || !cred->addresses[0]) {
  8846.             printf("\tAddresses: (none)\r\n");
  8847.         } else {
  8848.             int i;
  8849.             for (i=0; cred->addresses[i]; i++) {
  8850.                 if (i)
  8851.                     printf("              ");
  8852.                 else
  8853.                     printf("   Addresses: ");
  8854.                 one_addr(cred->addresses[i]);
  8855.                 printf("\r\n");
  8856.             }
  8857.         }
  8858.     }
  8859.  
  8860.     krb5_free_unparsed_name(kcontext,name);
  8861.     krb5_free_unparsed_name(kcontext,sname);
  8862.  
  8863.     krb5_errno = 0;
  8864.     makestr(&krb5_errmsg,"OK");
  8865. }
  8866.  
  8867. static VOID
  8868. #ifdef CK_ANSIC
  8869. fillit(int num, int c)
  8870. #else
  8871. fillit(num, c) int num; int c;
  8872. #endif
  8873. {
  8874.     int i;
  8875.  
  8876.     for (i=0; i<num; i++)
  8877.         printf("%c",c);
  8878. }
  8879. #endif /* KLIST */
  8880. #endif /* KRB5 */
  8881.  
  8882. #ifdef KRB4
  8883. #define KDEBUG 1
  8884. int k4debug = 0;                /* Kerberos 4 runtime debugging */
  8885.  
  8886. #ifdef KINIT
  8887. #define KRB_DEFAULT_LIFE 120 /* 10 hours in 5 minute intervals */
  8888.  
  8889. #ifdef SNK4
  8890. /* SNK4 is a hardware authentication system used to pre-authenticate    */
  8891. /* a ticket getting ticket.  We do not support this code at the present */
  8892. /* time in Kermit.                                                      */
  8893. void
  8894. get_input(s, size, stream)
  8895. char *s;
  8896. int size;
  8897. FILE *stream;
  8898. {
  8899.     char *p;
  8900.  
  8901.     if (fgets(s, size, stream) == NULL)
  8902.         exit(1);
  8903.     if ( (p = strchr(s, '\n')) != NULL)
  8904.         *p = '\0';
  8905. }
  8906. #endif /* SNK4 */
  8907.  
  8908. #ifdef COMMENT
  8909. static char
  8910. #ifdef CK_ANSIC
  8911. hex_scan_nybble(char c)
  8912. #else
  8913. hex_scan_nybble(c) char c;
  8914. #endif
  8915. {
  8916.     if (c >= '0' && c <= '9')
  8917.         return c - '0';
  8918.     if (c >= 'A' && c <= 'F')
  8919.         return c - 'A' + 10;
  8920.     if (c >= 'a' && c <= 'f')
  8921.         return c - 'a' + 10;
  8922.     return -1;
  8923. }
  8924.  
  8925. /* returns: NULL for ok, pointer to error string for bad input */
  8926. static char*
  8927. #ifdef CK_ANSIC
  8928. hex_scan_four_bytes(char *out, char *in)
  8929. #else
  8930. hex_scan_four_bytes(out, in) char *out; char *in;
  8931. #endif
  8932. {
  8933.     int i;
  8934.     int c;
  8935.     char c1;
  8936.     for (i=0; i<8; i++) {
  8937.         if(!in[i])
  8938.             return "not enough input";
  8939.         c = hex_scan_nybble(in[i]);
  8940.         if(c<0)
  8941.             return "invalid digit";
  8942.         c1 = c;
  8943.         i++;
  8944.         if(!in[i])
  8945.             return "not enough input";
  8946.         c = hex_scan_nybble(in[i]);
  8947.         if(c<0)
  8948.             return "invalid digit";
  8949.         *out++ = (c1 << 4) + c;
  8950.     }
  8951.     switch(in[i]) {
  8952.     case 0:
  8953.     case '\r':
  8954.     case '\n':
  8955.         return NULL;
  8956.     default:
  8957.         return "extra characters at end of input";
  8958.     }
  8959. }
  8960. #endif /* COMMENT */
  8961.  
  8962. /* ck_krb4_initTGT() returns 0 on success */
  8963. int
  8964. #ifdef CK_ANSIC
  8965. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  8966. #else
  8967. ck_krb4_initTGT(op,init)
  8968.     struct krb_op_data * op, struct krb4_init_data * init
  8969. #endif
  8970. {
  8971.     char    aname[ANAME_SZ+1];
  8972.     char    inst[INST_SZ+1];
  8973.     char    realm[REALM_SZ+1];
  8974.     char    *password=NULL;
  8975.     char    passwd[80]="";
  8976.     char    *username = NULL;
  8977.     char    *usernameptr=NULL;
  8978.     int     iflag,      /* Instance */
  8979.             rflag,      /* Realm */
  8980.             vflag,      /* Verbose */
  8981.             lflag,      /* Lifetime */
  8982.             pflag,      /* Preauth */
  8983.             lifetime=KRB_DEFAULT_LIFE,   /* Life Time */
  8984.             k_errno;
  8985.     register char *cp;
  8986.     register i;
  8987.  
  8988.     if ( !ck_krb4_is_installed() )
  8989.         return(-1);
  8990.  
  8991.     *inst = *realm = '\0';
  8992.     iflag = rflag = vflag = lflag = pflag = 0;
  8993.  
  8994.     vflag = init->verbose;
  8995.     pflag = init->preauth;
  8996.  
  8997.     if ( init->lifetime ) {
  8998.         lifetime = init->lifetime<5?1:init->lifetime/5;
  8999.         if ( lifetime > 255 ) lifetime = 255;
  9000.     }
  9001.     else
  9002.         lifetime = KRB_DEFAULT_LIFE;
  9003.  
  9004.     username = init->principal;
  9005.  
  9006.     if (username && username[0] &&
  9007.         (k_errno = kname_parse(aname, inst, realm, username))
  9008.         != AUTH_SUCCESS) {
  9009.         krb4_errno = k_errno;
  9010.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9011.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9012.         iflag = rflag = 1;
  9013.         username = NULL;
  9014.     }
  9015.  
  9016.     if ( init->realm ) {
  9017.         ckstrncpy(realm,init->realm,REALM_SZ);
  9018.     }
  9019.  
  9020.     if ( init->instance ) {
  9021.         ckstrncpy(inst,init->instance, INST_SZ);
  9022.     }
  9023.  
  9024. #ifdef COMMENT
  9025.     if ( vflag )
  9026.         printf("Kerberos IV initialization\r\n");
  9027. #endif /* COMMENT */
  9028.  
  9029.     if (!username || !username[0]) {
  9030.         debug(F100,"ck_krb4_initTGT no username specified","",0);
  9031.         printf("?Invalid principal specified.\r\n");
  9032.         krb4_errno = -1;
  9033.         makestr(&krb4_errmsg,"No principal specified");
  9034.         return(-1);
  9035.     }
  9036.     if (!*realm) {
  9037.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ);
  9038.     }
  9039.  
  9040.     if ( init->password )
  9041.         password = init->password;
  9042.     else {
  9043.         char prmpt[80];
  9044.         int ok;
  9045.  
  9046.         ckmakxmsg(prmpt,sizeof(prmpt),
  9047.                   "Kerberos 4 Password for ",username,"@",realm,": ",
  9048.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9049.         ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  9050.         if ( ok )
  9051.             password = passwd;
  9052.     }
  9053.  
  9054.     if (pflag) {
  9055.         k_errno = krb_get_pw_in_tkt_preauth( aname, inst, realm,
  9056.                                              "krbtgt", realm,
  9057.                                              lifetime,
  9058.                                              password);
  9059.         if (k_errno == -1) {    /* preauth method not available */
  9060.             k_errno = krb_get_pw_in_tkt(aname,
  9061.                                          inst, realm,
  9062.                                          "krbtgt", realm,
  9063.                                          lifetime,
  9064.                                          password);
  9065.         }
  9066.     } else {
  9067.         k_errno = krb_get_pw_in_tkt(aname,
  9068.                                      inst, realm,
  9069.                                      "krbtgt", realm,
  9070.                                      lifetime,
  9071.                                      password);
  9072.     }
  9073.  
  9074.     memset(passwd,0,sizeof(passwd));
  9075.     if (k_errno) {
  9076.         printf("%s for principal %s%s%s@%s\r\n",
  9077.                 krb_get_err_text_entry(k_errno), aname,
  9078.                 inst[0]?".":"", inst, realm);
  9079.         krb4_errno = k_errno;
  9080.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9081.         return(-1);
  9082.     } else if (vflag) {
  9083.         printf("Result from realm %s: ", realm);
  9084.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9085.     }
  9086.     krb4_errno = k_errno;
  9087.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9088.     return(0);
  9089. }
  9090. #endif /* KINIT */
  9091. #ifdef KDESTROY
  9092. int
  9093. #ifdef CK_ANSIC
  9094. ck_krb4_destroy(struct krb_op_data * op)
  9095. #else
  9096. ck_krb4_destroy(op) struct krb_op_data * op;
  9097. #endif
  9098. {
  9099.     int k_errno=0;
  9100.  
  9101.     if ( !ck_krb4_is_installed() )
  9102.         return(-1);
  9103.  
  9104.     k_errno = dest_tkt();
  9105.  
  9106.     krb4_errno = k_errno;
  9107.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9108.  
  9109.     if (k_errno == 0)
  9110.         printf("Tickets destroyed.\r\n");
  9111.     else if (k_errno == RET_TKFIL)
  9112.         printf("No tickets to destroy.\r\n");
  9113.     else {
  9114.         printf("Tickets MAY NOT be destroyed.\r\n");
  9115.         return(-1);
  9116.     }
  9117.     return(0);
  9118. }
  9119. #endif /* KDESTROY */
  9120. #ifdef KLIST
  9121. _PROTOTYP(static int display_tktfile,(char *, int, int, int));
  9122.  
  9123. int
  9124. #ifdef CK_ANSIC
  9125. ck_krb4_list_creds(struct krb_op_data * op)
  9126. #else
  9127. ck_krb4_list_creds(op) struct krb_op_data * op;
  9128. #endif
  9129. {
  9130.     int     long_form = 1;
  9131.     int     tgt_test = 0;
  9132.     int     do_srvtab = 0;
  9133.     int     show_kvnos = 0;
  9134.     char   *tkt_file = NULL;
  9135.  
  9136.     if ( !ck_krb4_is_installed() )
  9137.         return(-1);
  9138.  
  9139.     if ( op->cache )
  9140.         tkt_file = op->cache;
  9141.  
  9142.     if ( k4debug ) {
  9143.         show_kvnos = 1;
  9144.     }
  9145.  
  9146.     if (do_srvtab)
  9147.         return(display_srvtab(tkt_file));
  9148.     else
  9149.         return(display_tktfile(tkt_file, tgt_test, long_form, show_kvnos));
  9150. }
  9151.  
  9152. #ifndef KRB5
  9153. static int timestamp_width=0;
  9154.  
  9155. static char   *
  9156. #ifdef CK_ANSIC
  9157. short_date(long   *dp)
  9158. #else
  9159. short_date(dp) long   *dp;
  9160. #endif
  9161. {
  9162.     register char *cp;
  9163.     extern char *ctime();
  9164.     cp = ctime(dp) + 4;
  9165.     cp[15] = '\0';
  9166.     return (cp);
  9167. }
  9168.  
  9169.  
  9170. static VOID
  9171. #ifdef CK_ANSIC
  9172. printtime(time_t tv)
  9173. #else
  9174. printtime(tv) time_t tv;
  9175. #endif
  9176. {
  9177.     char timestring[BUFSIZ];
  9178.     char format[12];
  9179.     char fill;
  9180.  
  9181.     fill = ' ';
  9182.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  9183.     printf(format,short_date(&tv));
  9184. }
  9185. #endif /* KRB5 */
  9186.  
  9187. static int
  9188. #ifdef CK_ANSIC
  9189. display_tktfile(char *file, int tgt_test, int long_form, int show_kvnos)
  9190. #else
  9191. display_tktfile(file,tgt_test,long_form,show_kvnos)
  9192.     char *file; int tgt_test; int long_form; int show_kvnos;
  9193. #endif
  9194. {
  9195.     char    pname[ANAME_SZ];
  9196.     char    pinst[INST_SZ];
  9197.     char    prealm[REALM_SZ];
  9198.     char    buf1[20], buf2[20];
  9199.     int     k_errno;
  9200. #ifdef OS2
  9201.     LEASH_CREDENTIALS creds;
  9202. #else /* OS2 */
  9203.     CREDENTIALS creds;
  9204. #endif /* OS2 */
  9205.     int     header = 1;
  9206.  
  9207.     file = tkt_string();
  9208.  
  9209.     if (long_form) {
  9210.         printf("Ticket cache:      %s\r\n", file);
  9211.     }
  9212.  
  9213.     /*
  9214.      * Since krb_get_tf_realm will return a ticket_file error,
  9215.      * we will call tf_init and tf_close first to filter out
  9216.      * things like no ticket file.  Otherwise, the error that
  9217.      * the user would see would be
  9218.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9219.      * instead of
  9220.      * klist: No ticket file (tf_util)
  9221.      */
  9222.  
  9223.     /* Open ticket file */
  9224.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9225.         if (!tgt_test)
  9226.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9227.         krb4_errno = k_errno;
  9228.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9229.         return(-1);
  9230.     }
  9231.  
  9232.  
  9233.     /* Close ticket file */
  9234.     (void) tf_close();
  9235.  
  9236.     /*
  9237.      * We must find the realm of the ticket file here before calling
  9238.      * tf_init because since the realm of the ticket file is not
  9239.      * really stored in the principal section of the file, the
  9240.      * routine we use must itself call tf_init and tf_close.
  9241.      */
  9242.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9243.         if (!tgt_test)
  9244.             printf("can't find realm of ticket file: %s\r\n",
  9245.                     krb_get_err_text_entry (k_errno));
  9246.         krb4_errno = k_errno;
  9247.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9248.         return(-1);
  9249.     }
  9250.  
  9251.     /* Open ticket file */
  9252.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9253.         if (!tgt_test)
  9254.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9255.         krb4_errno = k_errno;
  9256.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9257.         return(-1);
  9258.     }
  9259.     /* Get principal name and instance */
  9260.     if ((k_errno = tf_get_pname(pname)) ||
  9261.          (k_errno = tf_get_pinst(pinst))) {
  9262.         (void) tf_close();
  9263.         if (!tgt_test)
  9264.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9265.         krb4_errno = k_errno;
  9266.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9267.         return(-1);
  9268.     }
  9269.  
  9270.     /*
  9271.      * You may think that this is the obvious place to get the
  9272.      * realm of the ticket file, but it can't be done here as the
  9273.      * routine to do this must open the ticket file.  This is why
  9274.      * it was done before tf_init.
  9275.      */
  9276.  
  9277.     if (!tgt_test && long_form)
  9278.         printf("Default principal: %s%s%s%s%s\r\n\r\n", pname,
  9279.                (pinst[0] ? "." : ""), pinst,
  9280.                (prealm[0] ? "@" : ""), prealm);
  9281.  
  9282.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9283.         if (!tgt_test && long_form && header) {
  9284.             printf("%-17s  %-17s  %s\r\n",
  9285.                    "Valid starting", "Expires", "Service principal");
  9286.             header = 0;
  9287.         }
  9288.         if (tgt_test) {
  9289.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9290.             if (!strcmp(creds.service, "krbtgt") &&
  9291.                 !strcmp(creds.instance, prealm)) {
  9292.                 krb4_errno = k_errno;
  9293.                 makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9294.  
  9295.                 (void) tf_close();
  9296.                 if (time(0) < creds.issue_date) {
  9297.                     return(0);          /* tgt hasn't expired */
  9298.                 } else {
  9299.                     return(-1);         /* has expired */
  9300.                 }
  9301.             }
  9302.             continue;                   /* not a tgt */
  9303.         }
  9304.         if (long_form) {
  9305.             timestamp_width = 17;       /* for k5 display function */
  9306.                                         /* if available            */
  9307.             printtime(creds.issue_date);
  9308.             printf("  ");
  9309.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9310.             if ( time(0) < creds.issue_date )
  9311.                 printtime(creds.issue_date);
  9312.             else
  9313.                 printf("*** expired ***  ");
  9314.             printf("  ");
  9315.         }
  9316.         if (show_kvnos)
  9317.           printf("%s%s%s%s%s (%d)\r\n",
  9318.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9319.                  (creds.realm[0] ? "@" : ""), creds.realm, creds.kvno);
  9320.         else
  9321.           printf("%s%s%s%s%s\r\n",
  9322.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9323.                  (creds.realm[0] ? "@" : ""), creds.realm);
  9324.  
  9325. #ifdef OS2
  9326.         if ( creds.address[0] )
  9327.             printf("   Address: %s\r\n",creds.address);
  9328. #endif /* OS2 */
  9329.     }
  9330.  
  9331.     (void) tf_close();
  9332.  
  9333.     if (tgt_test) {
  9334.         return(-1);
  9335.     }/* no tgt found */
  9336.     if (header && long_form && k_errno == EOF) {
  9337.         printf("No tickets in file.\r\n");
  9338.     }
  9339.     krb4_errno = k_errno;
  9340.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9341.     return(0);
  9342. }
  9343.  
  9344. #ifdef COMMENT
  9345. /* Just so we remember what the command line interface looked like */
  9346. usage()
  9347. {
  9348.     printf(
  9349.         "Usage: [ -s | -t ] [ -file filename ] [ -srvtab ] [ -version ]\r\n");
  9350.     return(-1);
  9351. }
  9352. #endif /* COMMENT */
  9353.  
  9354. /* adapted from getst() in librkb */
  9355. /*
  9356.  * ok_getst() takes a file descriptor, a string and a count.  It reads
  9357.  * from the file until either it has read "count" characters, or until
  9358.  * it reads a null byte.  When finished, what has been read exists in
  9359.  * the given string "s".  If "count" characters were actually read, the
  9360.  * last is changed to a null, so the returned string is always null-
  9361.  * terminated.  ok_getst() returns the number of characters read, including
  9362.  * the null terminator.
  9363.  *
  9364.  * If there is a read error, it returns -1 (like the read(2) system call)
  9365.  */
  9366.  
  9367. static int
  9368. #ifdef CK_ANSIC
  9369. ok_getst(int fd, register char *s, int n)
  9370. #else
  9371. ok_getst(fd, s, n) int fd; register char *s; int n;
  9372. #endif
  9373. {
  9374.     register int count = n;
  9375.     int err;
  9376.     while ((err = read(fd, s, 1)) > 0 && --count)
  9377.         if (*s++ == '\0')
  9378.             return (n - count);
  9379.     if (err < 0)
  9380.         return(-1);
  9381.     *s = '\0';
  9382.     return (n - count);
  9383. }
  9384.  
  9385. int
  9386. #ifdef CK_ANSIC
  9387. display_srvtab(char *file)
  9388. #else
  9389. display_srvtab(file) char *file;
  9390. #endif
  9391. {
  9392.     int stab;
  9393.     char serv[SNAME_SZ];
  9394.     char inst[INST_SZ];
  9395.     char rlm[REALM_SZ];
  9396.     unsigned char key[8];
  9397.     unsigned char vno;
  9398.     int count;
  9399.  
  9400.     printf("Server key file:   %s\r\n", file);
  9401. #ifdef NT
  9402. #ifndef O_RDONLY
  9403. #define O_RDONLY _O_RDONLY
  9404. #endif /* O_RDONLY */
  9405. #endif /* NT */
  9406.  
  9407.     if ((stab = open(file, O_RDONLY, 0400)) < 0) {
  9408.         perror(file);
  9409.         return(-1);
  9410.     }
  9411.     printf("%-15s %-15s %-10s %s\r\n","Service","Instance","Realm",
  9412.            "Key Version");
  9413.     printf("------------------------------------------------------\r\n");
  9414.  
  9415.     /* argh. getst doesn't return error codes, it silently fails */
  9416.     while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
  9417.            && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
  9418.            && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
  9419.         if (((count = read(stab,(char *) &vno,1)) != 1) ||
  9420.              ((count = read(stab,(char *) key,8)) != 8)) {
  9421.             if (count < 0)
  9422.                 perror("reading from key file");
  9423.             else
  9424.                 printf("key file truncated\r\n");
  9425.             return(-1);
  9426.         }
  9427.         printf("%-15s %-15s %-15s %d\r\n",serv,inst,rlm,vno);
  9428.     }
  9429.     if (count < 0)
  9430.         perror(file);
  9431.     (void) close(stab);
  9432.     return(0);
  9433. }
  9434. #endif /* KLIST */
  9435. #else /* KRB4 */
  9436. int
  9437. ck_krb4_autoget_TGT(char * dummy)
  9438. {
  9439.     return(-1);
  9440. }
  9441. #ifdef CK_KERBEROS
  9442. int
  9443. #ifdef CK_ANSIC
  9444. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9445. #else
  9446. ck_krb4_initTGT(op,init)
  9447.     struct krb_op_data * op, struct krb4_init_data * init
  9448. #endif
  9449. {
  9450.     return(-1);
  9451. }
  9452.  
  9453. #ifdef CK_ANSIC
  9454. ck_krb4_destroy(struct krb_op_data * op)
  9455. #else
  9456. ck_krb4_destroy(op) struct krb_op_data * op;
  9457. #endif
  9458. {
  9459.     return(-1);
  9460. }
  9461. int
  9462. #ifdef CK_ANSIC
  9463. ck_krb4_list_creds(struct krb_op_data * op)
  9464. #else
  9465. ck_krb4_list_creds(op) struct krb_op_data * op;
  9466. #endif
  9467. {
  9468.     return(-1);
  9469. }
  9470. #else /* CK_KERBEROS */
  9471. int ck_krb4_initTGT(void * a, void *b)
  9472. {
  9473.     return(-1);
  9474. }
  9475. int ck_krb4_destroy(void *a)
  9476. {
  9477.     return(-1);
  9478. }
  9479. int ck_krb4_list_creds(void *a)
  9480. {
  9481.     return(-1);
  9482. }
  9483. #endif /* CK_KERBEROS */
  9484. #endif /* KRB4 */
  9485.  
  9486. /* The following functions are used to implement the Kermit Script Language */
  9487. /* functions                                                                */
  9488.  
  9489. struct tkt_list_item {
  9490.     char * name;
  9491.     struct tkt_list_item * next;
  9492. };
  9493.  
  9494. static struct tkt_list_item * k4_tkt_list = NULL;
  9495.  
  9496. int
  9497. #ifdef CK_ANSIC
  9498. ck_krb4_get_tkts(VOID)
  9499. #else
  9500. ck_krb4_get_tkts()
  9501. #endif
  9502. {
  9503. #ifdef KRB4
  9504.     char   *file=NULL;
  9505.     char    pname[ANAME_SZ];
  9506.     char    pinst[INST_SZ];
  9507.     char    prealm[REALM_SZ];
  9508.     char    buf1[20], buf2[20];
  9509.     int     k_errno;
  9510. #ifdef OS2
  9511.     LEASH_CREDENTIALS creds;
  9512. #else /* OS2 */
  9513.     CREDENTIALS creds;
  9514. #endif /* OS2 */
  9515.     int     tkt_count=0;
  9516.     struct  tkt_list_item ** list = &k4_tkt_list;
  9517.  
  9518.     while ( k4_tkt_list ) {
  9519.         struct tkt_list_item * next;
  9520.         next = k4_tkt_list->next;
  9521.         free(k4_tkt_list->name);
  9522.         free(k4_tkt_list);
  9523.         k4_tkt_list = next;
  9524.     }
  9525.  
  9526.     if ( !ck_krb4_is_installed() )
  9527.         return(-1);
  9528.  
  9529.     file = tkt_string();
  9530.  
  9531.     /*
  9532.      * Since krb_get_tf_realm will return a ticket_file error,
  9533.      * we will call tf_init and tf_close first to filter out
  9534.      * things like no ticket file.  Otherwise, the error that
  9535.      * the user would see would be
  9536.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9537.      * instead of
  9538.      * klist: No ticket file (tf_util)
  9539.      */
  9540.  
  9541.     /* Open ticket file */
  9542.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9543.         return(-1);
  9544.     }
  9545.  
  9546.     /* Close ticket file */
  9547.     (void) tf_close();
  9548.  
  9549.     /*
  9550.      * We must find the realm of the ticket file here before calling
  9551.      * tf_init because since the realm of the ticket file is not
  9552.      * really stored in the principal section of the file, the
  9553.      * routine we use must itself call tf_init and tf_close.
  9554.      */
  9555.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9556.         return(-1);
  9557.     }
  9558.  
  9559.     /* Open ticket file */
  9560.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9561.         return(-1);
  9562.     }
  9563.     /* Get principal name and instance */
  9564.     if ((k_errno = tf_get_pname(pname)) ||
  9565.          (k_errno = tf_get_pinst(pinst))) {
  9566.         return(-1);
  9567.     }
  9568.  
  9569.     /*
  9570.      * You may think that this is the obvious place to get the
  9571.      * realm of the ticket file, but it can't be done here as the
  9572.      * routine to do this must open the ticket file.  This is why
  9573.      * it was done before tf_init.
  9574.      */
  9575.  
  9576.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9577.         char tkt_buf[256];
  9578.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9579.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9580.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9581.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9582.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  9583.         (*list)->name = strdup(tkt_buf);
  9584.         (*list)->next = NULL;
  9585.         list = &((*list)->next);
  9586.         tkt_count++;
  9587.     }
  9588.  
  9589.     tf_close();
  9590.     return(tkt_count);
  9591. #else /* KRB4 */
  9592.     return(0);
  9593. #endif /* KRB4 */
  9594. }
  9595.  
  9596. char *
  9597. #ifdef CK_ANSIC
  9598. ck_krb4_get_next_tkt(VOID)
  9599. #else
  9600. ck_krb4_get_next_tkt()
  9601. #endif
  9602. {
  9603. #ifdef KRB4
  9604.     static char * s=NULL;
  9605.     struct tkt_list_item * next=NULL;
  9606.  
  9607.     if ( s ) {
  9608.         free(s);
  9609.         s = NULL;
  9610.     }
  9611.  
  9612.     if ( k4_tkt_list == NULL )
  9613.         return(NULL);
  9614.  
  9615.     next = k4_tkt_list->next;
  9616.     s = k4_tkt_list->name;
  9617.     free(k4_tkt_list);
  9618.     k4_tkt_list = next;
  9619.     return(s);
  9620. #else /* KRB4 */
  9621.     return(NULL);
  9622. #endif /* KRB4 */
  9623. }
  9624.  
  9625. int
  9626. #ifdef CK_ANSIC
  9627. ck_krb4_tkt_isvalid(char * tktname)
  9628. #else
  9629. ck_krb4_tkt_isvalid(tktname) char * tktname;
  9630. #endif
  9631. {
  9632. #ifdef KRB4
  9633.     char   *file=NULL;
  9634.     char    pname[ANAME_SZ];
  9635.     char    pinst[INST_SZ];
  9636.     char    prealm[REALM_SZ];
  9637.     char    buf1[20], buf2[20];
  9638.     int     k_errno;
  9639.     time_t  issue_t, expire_t, now_t;
  9640. #ifdef OS2
  9641.     LEASH_CREDENTIALS creds;
  9642. #else /* OS2 */
  9643.     CREDENTIALS creds;
  9644. #endif /* OS2 */
  9645.  
  9646.     if ( !ck_krb4_is_installed() )
  9647.         return(-1);
  9648.  
  9649.     debug(F110,"ck_krb4_tkt_isvalid","tkt_string",0);
  9650.     file = tkt_string();
  9651.  
  9652.     /*
  9653.      * Since krb_get_tf_realm will return a ticket_file error,
  9654.      * we will call tf_init and tf_close first to filter out
  9655.      * things like no ticket file.  Otherwise, the error that
  9656.      * the user would see would be
  9657.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9658.      * instead of
  9659.      * klist: No ticket file (tf_util)
  9660.      */
  9661.  
  9662.     /* Open ticket file */
  9663.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9664.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9665.         return(-1);
  9666.     }
  9667.  
  9668.     /* Close ticket file */
  9669.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9670.     (void) tf_close();
  9671.  
  9672.     /*
  9673.      * We must find the realm of the ticket file here before calling
  9674.      * tf_init because since the realm of the ticket file is not
  9675.      * really stored in the principal section of the file, the
  9676.      * routine we use must itself call tf_init and tf_close.
  9677.      */
  9678.     debug(F110,"ck_krb4_tkt_isvalid","krb_get_tf_realm",0);
  9679.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9680.         return(-1);
  9681.     }
  9682.  
  9683.     /* Open ticket file */
  9684.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9685.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9686.         return(-1);
  9687.     }
  9688.     /* Get principal name and instance */
  9689.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_name/tf_get_pinst",0);
  9690.     if ((k_errno = tf_get_pname(pname)) ||
  9691.          (k_errno = tf_get_pinst(pinst))) {
  9692.  
  9693.         /* Close ticket file */
  9694.         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9695.         (void) tf_close();
  9696.  
  9697.         return(-1);
  9698.     }
  9699.  
  9700.     /*
  9701.      * You may think that this is the obvious place to get the
  9702.      * realm of the ticket file, but it can't be done here as the
  9703.      * routine to do this must open the ticket file.  This is why
  9704.      * it was done before tf_init.
  9705.      */
  9706.  
  9707.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_cred",0);
  9708.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9709.         char tkt_buf[256];
  9710.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9711.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9712.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9713.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9714.         if ( !strcmp(tktname,tkt_buf) ) {
  9715.  
  9716.             /* we found the ticket we are looking for */
  9717.             issue_t = creds.issue_date;
  9718.             expire_t = creds.issue_date
  9719.                 + ((unsigned char) creds.lifetime) * 5 * 60;
  9720.             now_t = time(0);
  9721.  
  9722.             /* We add a 5 minutes fudge factor to compensate for potential */
  9723.             /* clock skew errors between the KDC and K95's host OS         */
  9724.  
  9725.             if ( now_t >= (issue_t-300) && now_t < expire_t) {
  9726. #ifdef OS2
  9727. #ifdef CHECKADDRS
  9728.                 if ( krb4_checkaddrs ) {
  9729.                     extern char myipaddr[20];       /* From ckcnet.c */
  9730.                     if ( !myipaddr[0] ) {
  9731.                         int i;
  9732.                         char buf[60];
  9733.                         for ( i=0;i<64;i++ ) {
  9734.                             if ( getlocalipaddrs(buf,60,i) < 0 )
  9735.                                 break;
  9736.  
  9737.                             if ( !strcmp(buf,creds.address) ) {
  9738.                                 /* Close ticket file */
  9739.                                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9740.                                 (void) tf_close();
  9741.                                 return(1); /* They're the same */
  9742.                             }
  9743.                         }
  9744.  
  9745.                         /* Close ticket file */
  9746.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9747.                         (void) tf_close();
  9748.                         return(0);                  /* They're different */
  9749.                     } else if ( strcmp(myipaddr,creds.address) ) {
  9750.                         /* Close ticket file */
  9751.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9752.                         (void) tf_close();
  9753.                         return(0);                  /* They're different */
  9754.                     }
  9755.                     else {
  9756.                         /* Close ticket file */
  9757.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9758.                         (void) tf_close();
  9759.                         return(1);                  /* They're the same */
  9760.                     }
  9761.                 } else {
  9762.                     /* Close ticket file */
  9763.                     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9764.                     (void) tf_close();
  9765.                     return(1);                  /* They're the same */
  9766.                 }
  9767. #else /* CHECKADDRS */
  9768.                 /* Close ticket file */
  9769.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9770.                 (void) tf_close();
  9771.                 return(1);      /* valid but no ip address check */
  9772. #endif /* CHECKADDRS */
  9773. #else /* OS2 */
  9774.                 /* Close ticket file */
  9775.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9776.                 (void) tf_close();
  9777.                 return(1);      /* Valid but no ip address check */
  9778. #endif /* OS2 */
  9779.             }
  9780.             else {
  9781.                 /* Close ticket file */
  9782.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9783.                 (void) tf_close();
  9784.                 return(0);      /* expired or otherwise invalid */
  9785.             }
  9786.         }
  9787.     }
  9788.     /* Close ticket file */
  9789.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9790.     (void) tf_close();
  9791.     return(0);                  /* could not find the desired ticket */
  9792. #else /* KRB4 */
  9793.     return(-1);
  9794. #endif /* KRB4 */
  9795. }
  9796.  
  9797. int
  9798. #ifdef CK_ANSIC
  9799. ck_krb4_is_tgt_valid(VOID)
  9800. #else
  9801. ck_krb4_is_tgt_valid()
  9802. #endif
  9803. {
  9804. #ifdef KRB4
  9805.     char tgt[256];
  9806.     char * s;
  9807.     int rc = 0;
  9808.  
  9809.     s = krb4_d_realm ? krb4_d_realm : ck_krb4_getrealm();
  9810.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",s,"@",s);
  9811.     rc = ck_krb4_tkt_isvalid(tgt);
  9812.     debug(F111,"ck_krb4_is_tgt_valid",tgt,rc);
  9813.     return(rc > 0);
  9814. #else /* KRB4 */
  9815.     return(0);
  9816. #endif /* KRB4 */
  9817. }
  9818.  
  9819. int
  9820. #ifdef CK_ANSIC
  9821. ck_krb4_tkt_time(char * tktname)
  9822. #else
  9823. ck_krb4_tkt_time(tktname) char * tktname;
  9824. #endif
  9825. {
  9826. #ifdef KRB4
  9827.     char   *file=NULL;
  9828.     char    pname[ANAME_SZ];
  9829.     char    pinst[INST_SZ];
  9830.     char    prealm[REALM_SZ];
  9831.     char    buf1[20], buf2[20];
  9832.     int     k_errno;
  9833. #ifdef OS2
  9834.     LEASH_CREDENTIALS creds;
  9835. #else /* OS2 */
  9836.     CREDENTIALS creds;
  9837. #endif /* OS2 */
  9838.  
  9839.     if ( !ck_krb4_is_installed() )
  9840.         return(-1);
  9841.  
  9842.     file = tkt_string();
  9843.  
  9844.     /*
  9845.      * Since krb_get_tf_realm will return a ticket_file error,
  9846.      * we will call tf_init and tf_close first to filter out
  9847.      * things like no ticket file.  Otherwise, the error that
  9848.      * the user would see would be
  9849.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9850.      * instead of
  9851.      * klist: No ticket file (tf_util)
  9852.      */
  9853.  
  9854.     /* Open ticket file */
  9855.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9856.         return(-1);
  9857.     }
  9858.  
  9859.     /* Close ticket file */
  9860.     (void) tf_close();
  9861.  
  9862.     /*
  9863.      * We must find the realm of the ticket file here before calling
  9864.      * tf_init because since the realm of the ticket file is not
  9865.      * really stored in the principal section of the file, the
  9866.      * routine we use must itself call tf_init and tf_close.
  9867.      */
  9868.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9869.         return(-1);
  9870.     }
  9871.  
  9872.     /* Open ticket file */
  9873.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9874.         return(-1);
  9875.     }
  9876.     /* Get principal name and instance */
  9877.     if ((k_errno = tf_get_pname(pname)) ||
  9878.          (k_errno = tf_get_pinst(pinst))) {
  9879.         tf_close();
  9880.         return(-1);
  9881.     }
  9882.  
  9883.     /*
  9884.      * You may think that this is the obvious place to get the
  9885.      * realm of the ticket file, but it can't be done here as the
  9886.      * routine to do this must open the ticket file.  This is why
  9887.      * it was done before tf_init.
  9888.      */
  9889.  
  9890.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9891.         char tkt_buf[256];
  9892.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9893.                  creds.service, (creds.instance[0] ? "." : ""),
  9894.                  creds.instance,
  9895.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9896.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9897.         if ( !strcmp(tktname,tkt_buf) ) {
  9898.             /* we found the ticket we are looking for */
  9899.             int n = (creds.issue_date
  9900.                       + (((unsigned char) creds.lifetime) * 5 * 60))
  9901.                 - time(0);
  9902.             tf_close();
  9903.             return(n <= 0 ? 0 : n);
  9904.         }
  9905.     }
  9906.     tf_close();
  9907.     return(0);                  /* could not find the desired ticket */
  9908. #else /* KRB4 */
  9909.     return(-1);
  9910. #endif /* KRB4 */
  9911. }
  9912.  
  9913. char *
  9914. #ifdef CK_ANSIC
  9915. ck_krb4_getrealm(void)
  9916. #else
  9917. ck_krb4_getrealm()
  9918. #endif
  9919. {
  9920. #ifdef KRB4
  9921.     char   *file=NULL;
  9922.     int     k_errno;
  9923.     static char realm[256]="";
  9924.     realm[0]='\0';
  9925.  
  9926.     if ( !ck_krb4_is_installed() )
  9927.         return(realm);
  9928.  
  9929.     /* Try to get realm from ticket file */
  9930.     /* If failure get the local realm    */
  9931.  
  9932.     /*
  9933.     * Since krb_get_tf_realm will return a ticket_file error,
  9934.     * we will call tf_init and tf_close first to filter out
  9935.     * things like no ticket file.
  9936.     */
  9937.  
  9938.     /* Open ticket file */
  9939.     file = tkt_string();
  9940.     if (file == NULL || !file[0])
  9941.         return(realm);
  9942.  
  9943.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  9944.         /* Close ticket file */
  9945.         (void) tf_close();
  9946.  
  9947.         k_errno = krb_get_tf_realm(file, realm);
  9948.     }
  9949.     if (k_errno != KSUCCESS) {
  9950.         k_errno = krb_get_lrealm(realm, 1);
  9951.     }
  9952.     return(realm);
  9953. #else /* KRB4 */
  9954.     return("");
  9955. #endif /* KRB4 */
  9956. }
  9957.  
  9958. char *
  9959. #ifdef CK_ANSIC
  9960. ck_krb4_getprincipal(void)
  9961. #else
  9962. ck_krb4_getprincipal()
  9963. #endif
  9964. {
  9965. #ifdef KRB4
  9966.     char   *file=NULL;
  9967.     int     k_errno;
  9968.     static char principal[256]="";
  9969.     char        instance[256]="";
  9970.     char        realm[256]="";
  9971.     principal[0]='\0';
  9972.  
  9973.     if ( !ck_krb4_is_installed() )
  9974.         return(principal);
  9975.  
  9976.     /* Try to get realm from ticket file */
  9977.     /* If failure get the local realm    */
  9978.  
  9979.     /*
  9980.     * Since krb_get_tf_realm will return a ticket_file error,
  9981.     * we will call tf_init and tf_close first to filter out
  9982.     * things like no ticket file.
  9983.     */
  9984.  
  9985.     /* Open ticket file */
  9986.     file = tkt_string();
  9987.     if (file == NULL || !file[0])
  9988.         return(principal);
  9989.  
  9990.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  9991.         /* Close ticket file */
  9992.         (void) tf_close();
  9993.  
  9994.         k_errno = krb_get_tf_fullname(file, principal, instance, realm);
  9995.     }
  9996.     return(principal);
  9997. #else /* KRB4 */
  9998.     return("");
  9999. #endif /* KRB4 */
  10000. }
  10001.  
  10002. static struct tkt_list_item * k5_tkt_list = NULL;
  10003.  
  10004. int
  10005. #ifdef CK_ANSIC
  10006. ck_krb5_get_tkts(char * cc_name)
  10007. #else
  10008. ck_krb5_get_tkts(cc_name) char * cc_name;
  10009. #endif
  10010. {
  10011. #ifdef KRB5
  10012. #ifndef HEIMDAL
  10013.     krb5_context kcontext;
  10014.     krb5_error_code retval;
  10015.     krb5_ccache cache = NULL;
  10016.     krb5_cc_cursor cur;
  10017.     krb5_creds creds;
  10018.     krb5_principal princ=NULL;
  10019.     krb5_flags flags=0;
  10020.     krb5_error_code code=0;
  10021.     int exit_status = 0;
  10022.  
  10023.     int     tkt_count=0;
  10024.     struct  tkt_list_item ** list = &k5_tkt_list;
  10025.  
  10026.     while ( k5_tkt_list ) {
  10027.         struct tkt_list_item * next;
  10028.         next = k5_tkt_list->next;
  10029.         free(k5_tkt_list->name);
  10030.         free(k5_tkt_list);
  10031.         k5_tkt_list = next;
  10032.     }
  10033.  
  10034.     if ( !ck_krb5_is_installed() )
  10035.         return(-1);
  10036.  
  10037.     retval = krb5_init_context(&kcontext);
  10038.     if (retval) {
  10039.         debug(F101,"ck_krb5_get_tkts while initializing krb5","",retval);
  10040.         return(-1);
  10041.     }
  10042.  
  10043.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10044.     if (code != 0) {
  10045.         debug(F111,"ck_krb5_get_tkts while getting ccache",
  10046.                error_message(code),code);
  10047.         tkt_count = -1;
  10048.         goto exit_k5_get_tkt;
  10049.     }
  10050.  
  10051.     flags = 0;                          /* turns off OPENCLOSE mode */
  10052.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10053.         if (code == ENOENT) {
  10054.             debug(F111,"ck_krb5_get_tkts (ticket cache)",
  10055.                    krb5_cc_get_name(kcontext, cache),code);
  10056.         } else {
  10057.             debug(F111,
  10058.                  "ck_krb5_get_tkts while setting cache flags (ticket cache)",
  10059.                   krb5_cc_get_name(kcontext, cache),code);
  10060.         }
  10061.         tkt_count = -1;
  10062.         goto exit_k5_get_tkt;
  10063.     }
  10064.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10065.         debug(F101,"ck_krb5_get_tkts while retrieving principal name",
  10066.                "",code);
  10067.         tkt_count = -1;
  10068.         goto exit_k5_get_tkt;
  10069.     }
  10070.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10071.         debug(F101,"ck_krb5_get_tkts while unparsing principal name",
  10072.                "",code);
  10073.         tkt_count = -1;
  10074.         goto exit_k5_get_tkt;
  10075.     }
  10076.  
  10077.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10078.         debug(F101,"ck_krb5_get_tkts while starting to retrieve tickets",
  10079.                "",code);
  10080.         tkt_count = -1;
  10081.         goto exit_k5_get_tkt;
  10082.     }
  10083.  
  10084.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10085.         char *sname=NULL;
  10086.  
  10087.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10088.         if (retval) {
  10089.             debug(F101,
  10090.                   "ck_krb5_get_tkts while unparsing server name","",retval);
  10091.             tkt_count = -1;
  10092.             goto exit_k5_get_tkt;
  10093.         }
  10094.  
  10095.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  10096.         (*list)->name = sname;
  10097.         (*list)->next = NULL;
  10098.         list = &((*list)->next);
  10099.  
  10100.         krb5_free_cred_contents(kcontext, &creds);
  10101.         tkt_count++;
  10102.     }
  10103.  
  10104.     if (code == KRB5_CC_END) {
  10105.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10106.             debug(F101,"ck_krb5_get_tkts while finishing ticket retrieval",
  10107.                    "",code);
  10108.             tkt_count = -1;
  10109.             goto exit_k5_get_tkt;
  10110.         }
  10111.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10112.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10113.             debug(F101,"ck_krb5_get_tkts while closing ccache",
  10114.                    "",code);
  10115.             tkt_count = -1;
  10116.             goto exit_k5_get_tkt;
  10117.         }
  10118.     } else {
  10119.         debug(F101,"ck_krb5_get_tkts while retrieving a ticket","",code);
  10120.         tkt_count = -1;
  10121.         goto exit_k5_get_tkt;
  10122.     }
  10123.  
  10124.   exit_k5_get_tkt:
  10125.     krb5_free_principal(kcontext,princ);
  10126.     krb5_free_unparsed_name(kcontext,defname);
  10127.     krb5_cc_close(kcontext,cache);
  10128.     krb5_free_context(kcontext);
  10129.     return(tkt_count);
  10130. #else /* HEIMDAL */
  10131.     return(-1);
  10132. #endif /* HEIMDAL */
  10133. #else /* KRB5 */
  10134.     return(0);
  10135. #endif /* KRB5 */
  10136. }
  10137.  
  10138. char *
  10139. #ifdef CK_ANSIC
  10140. ck_krb5_get_next_tkt(VOID)
  10141. #else
  10142. ck_krb5_get_next_tkt()
  10143. #endif
  10144. {
  10145. #ifdef KRB5
  10146. #ifndef HEIMDAL
  10147.     static char * s=NULL;
  10148.     struct tkt_list_item * next=NULL;
  10149.  
  10150.     if ( s ) {
  10151.         free(s);
  10152.         s = NULL;
  10153.     }
  10154.  
  10155.     if ( k5_tkt_list == NULL )
  10156.         return(NULL);
  10157.  
  10158.     next = k5_tkt_list->next;
  10159.     s = k5_tkt_list->name;
  10160.     free(k5_tkt_list);
  10161.     k5_tkt_list = next;
  10162.     return(s);
  10163. #else /* HEIMDAL */
  10164.     return("Not implemented");
  10165. #endif /* HEIMDAL */
  10166. #else /* KRB5 */
  10167.     return(NULL);
  10168. #endif /* KRB5 */
  10169. }
  10170.  
  10171. char *
  10172. #ifdef CK_ANSIC
  10173. ck_krb5_tkt_flags(char * cc_name, char * tktname)
  10174. #else
  10175. ck_krb5_tkt_flags(cc_name,tktname) char * cc_name; char * tktname;
  10176. #endif
  10177. {
  10178. #ifdef KRB5
  10179. #ifndef HEIMDAL
  10180.     krb5_context kcontext;
  10181.     krb5_error_code retval;
  10182.     krb5_ccache cache = NULL;
  10183.     krb5_cc_cursor cur;
  10184.     krb5_creds creds;
  10185.     krb5_principal princ=NULL;
  10186.     krb5_flags flags=0;
  10187.     krb5_error_code code=0;
  10188.     char * flag_str = "";
  10189.  
  10190.     if ( !ck_krb5_is_installed() )
  10191.         return("");
  10192.  
  10193.     retval = krb5_init_context(&kcontext);
  10194.     if (retval) {
  10195.         debug(F101,"ck_krb5_tkt_flags while initializing krb5","",retval);
  10196.         return("");
  10197.     }
  10198.  
  10199.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10200.     if (code != 0) {
  10201.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10202.                error_message(code),code);
  10203.         goto exit_k5_get_tkt;
  10204.     }
  10205.  
  10206.     flags = 0;                          /* turns off OPENCLOSE mode */
  10207.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10208.         if (code == ENOENT) {
  10209.             debug(F111,"ck_krb5_tkt_flags (ticket cache)",
  10210.                    krb5_cc_get_name(kcontext, cache),code);
  10211.         } else {
  10212.             debug(F111,
  10213.                  "ck_krb5_tkt_flags while setting cache flags (ticket cache)",
  10214.                   krb5_cc_get_name(kcontext, cache),code);
  10215.         }
  10216.         retval = -1;
  10217.         goto exit_k5_get_tkt;
  10218.     }
  10219.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10220.         debug(F101,"ck_krb5_tkt_flags while retrieving principal name",
  10221.                "",code);
  10222.         retval = -1;
  10223.         goto exit_k5_get_tkt;
  10224.     }
  10225.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10226.         debug(F101,"ck_krb5_tkt_flags while unparsing principal name",
  10227.                "",code);
  10228.         retval = -1;
  10229.         goto exit_k5_get_tkt;
  10230.     }
  10231.  
  10232.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10233.         debug(F101,"ck_krb5_tkt_flags while starting to retrieve tickets",
  10234.                "",code);
  10235.         retval = -1;
  10236.         goto exit_k5_get_tkt;
  10237.     }
  10238.  
  10239.     if ((code = krb5_timeofday(kcontext, &now))) {
  10240.         if (!status_only)
  10241.             debug(F101,"ck_krb5_tkt_flags while getting time of day.",
  10242.                    "",code);
  10243.         retval = -1;
  10244.         goto exit_k5_get_tkt;
  10245.     }
  10246.  
  10247.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10248.         char *sname=NULL;
  10249.  
  10250.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10251.         if (retval) {
  10252.             debug(F101,
  10253.                   "ck_krb5_tkt_flags while unparsing server name","",retval);
  10254.             retval = -1;
  10255.             krb5_free_cred_contents(kcontext, &creds);
  10256.             goto exit_k5_get_tkt;
  10257.         }
  10258.  
  10259.         if ( !strcmp(sname,tktname) ) {
  10260.             /* we found the ticket we are looking for */
  10261.  
  10262.             flag_str = flags_string(&creds);
  10263.  
  10264.             krb5_free_cred_contents(kcontext, &creds);
  10265.             code = KRB5_CC_END;
  10266.             break;
  10267.         }
  10268.         krb5_free_cred_contents(kcontext, &creds);
  10269.     }
  10270.  
  10271.     if (code == KRB5_CC_END) {
  10272.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10273.             debug(F101,"ck_krb5_tkt_flags while finishing ticket retrieval",
  10274.                    "",code);
  10275.             goto exit_k5_get_tkt;
  10276.         }
  10277.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10278.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10279.             debug(F101,"ck_krb5_tkt_flags while closing ccache",
  10280.                    "",code);
  10281.             goto exit_k5_get_tkt;
  10282.         }
  10283.     } else {
  10284.         debug(F101,"ck_krb5_tkt_flags while retrieving a ticket","",code);
  10285.         goto exit_k5_get_tkt;
  10286.     }
  10287.  
  10288.   exit_k5_get_tkt:
  10289.     krb5_free_principal(kcontext,princ);
  10290.     krb5_free_unparsed_name(kcontext,defname);
  10291.     krb5_cc_close(kcontext,cache);
  10292.     krb5_free_context(kcontext);
  10293.     return(flag_str);
  10294. #else /* HEIMDAL */
  10295.     return("Not implemented");
  10296. #endif /* HEIMDAL */
  10297. #else /* KRB5 */
  10298.     return("");
  10299. #endif /* KRB5 */
  10300. }
  10301.  
  10302.  
  10303. int
  10304. #ifdef CK_ANSIC
  10305. ck_krb5_tkt_isvalid(char * cc_name, char * tktname)
  10306. #else
  10307. ck_krb5_tkt_isvalid(cc_name,tktname) char * cc_name; char * tktname;
  10308. #endif
  10309. {
  10310. #ifdef KRB5
  10311. #ifndef HEIMDAL
  10312.     krb5_context kcontext=NULL;
  10313.     krb5_error_code retval;
  10314.     krb5_ccache cache = NULL;
  10315.     krb5_cc_cursor cur;
  10316.     krb5_creds creds;
  10317.     krb5_principal princ=NULL;
  10318.     krb5_flags flags=0;
  10319.     krb5_error_code code=0;
  10320. #ifdef CHECKADDRS
  10321.     krb5_address **     myAddrs=NULL;
  10322.     krb5_address **     p=NULL;
  10323.     BOOL                Addrfound = FALSE;
  10324. #endif /*CHECKADDRS*/
  10325.  
  10326.     if ( !ck_krb5_is_installed() )
  10327.         return(-1);
  10328.  
  10329.     retval = krb5_init_context(&kcontext);
  10330.     if (retval) {
  10331.         debug(F101,"ck_krb5_tkt_isvalid while initializing krb5","",retval);
  10332.         return(-1);
  10333.     }
  10334.  
  10335.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10336.     if (code != 0) {
  10337.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10338.                error_message(code),code);
  10339.         goto exit_k5_get_tkt;
  10340.     }
  10341.  
  10342.     flags = 0;                          /* turns off OPENCLOSE mode */
  10343.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10344.         if (code == ENOENT) {
  10345.             debug(F111,"ck_krb5_tkt_isvalid (ticket cache)",
  10346.                    krb5_cc_get_name(kcontext, cache),code);
  10347.         } else {
  10348.             debug(F111,
  10349.                 "ck_krb5_tkt_isvalid while setting cache flags (ticket cache)",
  10350.                   krb5_cc_get_name(kcontext, cache),code);
  10351.         }
  10352.         retval = -1;
  10353.         goto exit_k5_get_tkt;
  10354.     }
  10355.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10356.         debug(F101,"ck_krb5_tkt_isvalid while retrieving principal name",
  10357.                "",code);
  10358.         retval = -1;
  10359.         goto exit_k5_get_tkt;
  10360.     }
  10361.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10362.         debug(F101,"ck_krb5_tkt_isvalid while unparsing principal name",
  10363.                "",code);
  10364.         retval = -1;
  10365.         goto exit_k5_get_tkt;
  10366.     }
  10367.  
  10368.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10369.         debug(F101,"ck_krb5_tkt_isvalid while starting to retrieve tickets",
  10370.                "",code);
  10371.         retval = -1;
  10372.         goto exit_k5_get_tkt;
  10373.     }
  10374.  
  10375.     if ((code = krb5_timeofday(kcontext, &now))) {
  10376.         if (!status_only)
  10377.             debug(F101,"ck_krb5_tkt_isvalid while getting time of day.",
  10378.                    "",code);
  10379.         retval = -1;
  10380.         goto exit_k5_get_tkt;
  10381.     }
  10382.  
  10383.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10384.         char *sname=NULL;
  10385.  
  10386.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10387.         if (retval) {
  10388.             debug(F101,
  10389.                   "ck_krb5_tkt_isvalid while unparsing server name","",retval);
  10390.             retval = -1;
  10391.             krb5_free_cred_contents(kcontext, &creds);
  10392.             goto exit_k5_get_tkt;
  10393.         }
  10394.  
  10395.         if ( !strcmp(sname,tktname) ) {
  10396.             /* we found the ticket we are looking for */
  10397.  
  10398.             /* We add a 5 minutes fudge factor to compensate for potential */
  10399.             /* clock skew errors between the KDC and K95's host OS         */
  10400.  
  10401.             retval = ((creds.times.starttime > 0) &&
  10402.                        now >= (creds.times.starttime - 300) &&
  10403.                        now < (creds.times.endtime + 300) &&
  10404.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10405.  
  10406. #ifdef CHECKADDRS
  10407.             if ( retval && krb5_checkaddrs &&
  10408.                                  creds.addresses && creds.addresses[0] ) {
  10409.                 /* if we think it is valid, then lets check the IP Addresses */
  10410.                 /* to make sure it is valid for our current connection.      */
  10411.                 /* Also make sure it's for the correct IP address */
  10412.                 retval = krb5_os_localaddr(kcontext, &myAddrs);
  10413.                 if (retval) {
  10414.                     com_err(NULL, retval, "retrieving my IP address");
  10415.                     krb5_free_cred_contents(kcontext, &creds);
  10416.                     code = KRB5_CC_END;
  10417.                     retval = -1;
  10418.                     break;
  10419.                 }
  10420.  
  10421.              /* See if any of our addresses match any in cached credentials */
  10422.  
  10423.                 for (Addrfound=FALSE, p=myAddrs;
  10424.                      (Addrfound==FALSE) && (*p);
  10425.                      p++
  10426.                      ) {
  10427.                     if (krb5_address_search(kcontext, *p, creds.addresses)) {
  10428.                         Addrfound = TRUE;
  10429.                     }
  10430.                 }
  10431.                 krb5_free_addresses(k5_context, myAddrs);
  10432.  
  10433.                 if (Addrfound) {
  10434.                     krb5_free_cred_contents(kcontext, &creds);
  10435.                     code = KRB5_CC_END;
  10436.                     retval = 1;
  10437.                     break;
  10438.                 } else {
  10439.                     krb5_free_cred_contents(kcontext, &creds);
  10440.                     code = KRB5_CC_END;
  10441.                     retval = 0;
  10442.                     break;
  10443.                 }
  10444.             }
  10445. #endif /* CHECKADDRS */
  10446.  
  10447.             krb5_free_cred_contents(kcontext, &creds);
  10448.             code = KRB5_CC_END;
  10449.             break;
  10450.         }
  10451.         krb5_free_cred_contents(kcontext, &creds);
  10452.     }
  10453.  
  10454.     if (code == KRB5_CC_END) {
  10455.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10456.             debug(F101,"ck_krb5_tkt_isvalid while finishing ticket retrieval",
  10457.                    "",code);
  10458.             retval = -1;
  10459.             goto exit_k5_get_tkt;
  10460.         }
  10461.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10462.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10463.             debug(F101,"ck_krb5_tkt_isvalid while closing ccache",
  10464.                    "",code);
  10465.             retval = -1;
  10466.             goto exit_k5_get_tkt;
  10467.         }
  10468.     } else {
  10469.         debug(F101,"ck_krb5_tkt_isvalid while retrieving a ticket","",code);
  10470.         retval = -1;
  10471.         goto exit_k5_get_tkt;
  10472.     }
  10473.  
  10474.   exit_k5_get_tkt:
  10475.     krb5_free_principal(kcontext,princ);
  10476.     krb5_free_unparsed_name(kcontext,defname);
  10477.     krb5_cc_close(kcontext,cache);
  10478.     krb5_free_context(kcontext);
  10479.     return(retval);
  10480. #else /* HEIMDAL */
  10481.     return(-1);
  10482. #endif /* HEIMDAL */
  10483. #else /* KRB5 */
  10484.     return(-1);
  10485. #endif /* KRB5 */
  10486. }
  10487.  
  10488. int
  10489. #ifdef CK_ANSIC
  10490. ck_krb5_is_tgt_valid(VOID)
  10491. #else
  10492. ck_krb5_is_tgt_valid()
  10493. #endif
  10494. {
  10495. #ifdef KRB5
  10496. #ifndef HEIMDAL
  10497.     char tgt[256];
  10498.     char * s;
  10499.     int rc = 0;
  10500.  
  10501.     s = krb5_d_realm ? krb5_d_realm : ck_krb5_getrealm(krb5_d_cc);
  10502.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",s,"@",s);
  10503.     rc = ck_krb5_tkt_isvalid(krb5_d_cc,tgt);
  10504.     debug(F111,"ck_krb5_is_tgt_valid",tgt,rc);
  10505.     return(rc>0);
  10506. #else /* HEIMDAL */
  10507.     return(-1);
  10508. #endif /* HEIMDAL */
  10509. #else /* KRB5 */
  10510.     return(0);
  10511. #endif /* KRB5 */
  10512. }
  10513.  
  10514. int
  10515. #ifdef CK_ANSIC
  10516. ck_krb5_tkt_time(char * cc_name, char * tktname)
  10517. #else
  10518. ck_krb5_tkt_time(cc_name, tktname) char * cc_name; char * tktname;
  10519. #endif
  10520. {
  10521. #ifdef KRB5
  10522. #ifndef HEIMDAL
  10523.     krb5_context kcontext;
  10524.     krb5_error_code retval;
  10525.     krb5_ccache cache = NULL;
  10526.     krb5_cc_cursor cur;
  10527.     krb5_creds creds;
  10528.     krb5_principal princ=NULL;
  10529.     krb5_flags flags=0;
  10530.     krb5_error_code code=0;
  10531.  
  10532.     if ( !ck_krb5_is_installed() )
  10533.         return(-1);
  10534.  
  10535.     retval = krb5_init_context(&kcontext);
  10536.     if (retval) {
  10537.         debug(F101,"ck_krb5_list_creds while initializing krb5","",retval);
  10538.         return(-1);
  10539.     }
  10540.  
  10541.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10542.     if (code != 0) {
  10543.         debug(F111,"ck_krb5_tkt_time while getting ccache",
  10544.                error_message(code),code);
  10545.         retval = -1;
  10546.         goto exit_k5_get_tkt;
  10547.     }
  10548.  
  10549.     flags = 0;                          /* turns off OPENCLOSE mode */
  10550.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10551.         if (code == ENOENT) {
  10552.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  10553.                    krb5_cc_get_name(kcontext, cache),code);
  10554.         } else {
  10555.             debug(F111,
  10556.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  10557.                   krb5_cc_get_name(kcontext, cache),code);
  10558.         }
  10559.         retval = -1;
  10560.         goto exit_k5_get_tkt;
  10561.     }
  10562.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10563.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  10564.                "",code);
  10565.         retval = -1;
  10566.         goto exit_k5_get_tkt;
  10567.     }
  10568.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10569.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  10570.                "",code);
  10571.         retval = -1;
  10572.         goto exit_k5_get_tkt;
  10573.     }
  10574.  
  10575.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10576.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  10577.                "",code);
  10578.         retval = -1;
  10579.         goto exit_k5_get_tkt;
  10580.     }
  10581.  
  10582.     if ((code = krb5_timeofday(kcontext, &now))) {
  10583.         if (!status_only)
  10584.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  10585.                    "",code);
  10586.         krb5_free_context(kcontext);
  10587.         return(-1);
  10588.     }
  10589.  
  10590.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10591.         char *sname=NULL;
  10592.  
  10593.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10594.         if (retval) {
  10595.             debug(F101,
  10596.                   "ck_krb5_list_creds while unparsing server name","",retval);
  10597.             retval = -1;
  10598.             krb5_free_cred_contents(kcontext, &creds);
  10599.             goto exit_k5_get_tkt;
  10600.         }
  10601.  
  10602.         if ( !strcmp(sname,tktname) ) {
  10603.             /* we found the ticket we are looking for */
  10604.             int valid = (creds.times.starttime &&
  10605.                        now > creds.times.starttime &&
  10606.                        now < creds.times.endtime &&
  10607.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10608.             if ( valid ) {
  10609.                 retval = creds.times.endtime - now;
  10610.             }
  10611.             else
  10612.                 retval = 0;
  10613.             krb5_free_cred_contents(kcontext, &creds);
  10614.             code = KRB5_CC_END;
  10615.             break;
  10616.         }
  10617.         krb5_free_cred_contents(kcontext, &creds);
  10618.     }
  10619.  
  10620.     if (code == KRB5_CC_END) {
  10621.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10622.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  10623.                    "",code);
  10624.             retval = -1;
  10625.             goto exit_k5_get_tkt;
  10626.         }
  10627.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10628.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10629.             debug(F101,"ck_krb5_list_creds while closing ccache",
  10630.                    "",code);
  10631.             retval = -1;
  10632.             goto exit_k5_get_tkt;
  10633.         }
  10634.     } else {
  10635.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  10636.         retval = -1;
  10637.         goto exit_k5_get_tkt;
  10638.     }
  10639.  
  10640.   exit_k5_get_tkt:
  10641.     krb5_free_principal(kcontext,princ);
  10642.     krb5_free_unparsed_name(kcontext,defname);
  10643.     krb5_cc_close(kcontext,cache);
  10644.     krb5_free_context(kcontext);
  10645.     return(retval);
  10646. #else /* HEIMDAL */
  10647.     return(-1);
  10648. #endif /* HEIMDAL */
  10649. #else /* KRB5 */
  10650.     return(-1);
  10651. #endif /* KRB5 */
  10652. }
  10653.  
  10654. char *
  10655. #ifdef CK_ANSIC
  10656. ck_krb5_get_cc_name(void)
  10657. #else
  10658. ck_krb5_get_cc_name()
  10659. #endif
  10660. {
  10661. #ifdef KRB5
  10662. #ifndef HEIMDAL
  10663.     static char cc_name[CKMAXPATH+1]="";
  10664.     krb5_context kcontext = NULL;
  10665.     krb5_ccache ccache = NULL;
  10666.     krb5_error_code code;
  10667.     char * p=NULL;
  10668.  
  10669.     cc_name[0] = '\0';
  10670.  
  10671.     if ( !ck_krb5_is_installed() )
  10672.         return(cc_name);
  10673.  
  10674.     p = getenv("KRB5CCNAME");
  10675.     if ( !p ) {
  10676.         code = krb5_init_context(&kcontext);
  10677.         if (code) {
  10678.             com_err("ck_krb5_get_cc_name",code,"while init_context");
  10679.             return(cc_name);
  10680.         }
  10681.         if ((code = krb5_cc_default(kcontext, &ccache))) {
  10682.             com_err("ck_krb5_get_cc_name",code,"while getting default ccache");
  10683.             goto exit_k5_get_cc;
  10684.         }
  10685.  
  10686.         ckmakmsg(cc_name,sizeof(cc_name),
  10687.                  (char *)krb5_cc_get_type(kcontext,ccache),":",
  10688.                  (char *)krb5_cc_get_name(kcontext,ccache),NULL);
  10689.     } else {
  10690.         ckstrncpy(cc_name,p,CKMAXPATH);
  10691.     }
  10692.  
  10693.     if ( !strncmp("FILE:",cc_name,5) ) {
  10694.         for ( p=cc_name; *p ; p++ )
  10695.             if ( *p == '\\' ) *p = '/';
  10696.     }
  10697.  
  10698.   exit_k5_get_cc:
  10699.     if ( ccache )
  10700.         krb5_cc_close(kcontext,ccache);
  10701.     if ( kcontext )
  10702.         krb5_free_context(kcontext);
  10703.     return(cc_name);
  10704. #else /* HEIMDAL */
  10705.     return("Not implemented");
  10706. #endif /* HEIMDAL */
  10707. #else /* KRB5 */
  10708.     return("");
  10709. #endif /* KRB5 */
  10710. }
  10711.  
  10712. char *
  10713. #ifdef CK_ANSIC
  10714. ck_krb5_getrealm(char * cc_name)
  10715. #else
  10716. ck_krb5_getrealm(cc_name) char * cc_name;
  10717. #endif
  10718. {
  10719. #ifdef KRB5
  10720. #ifndef HEIMDAL
  10721.     static char realm[256]="";
  10722.     krb5_context kcontext;
  10723.     krb5_ccache ccache = NULL;
  10724.     krb5_error_code code;
  10725.     krb5_principal me;
  10726.  
  10727.     realm[0] = '\0';
  10728.  
  10729.     if ( !ck_krb5_is_installed() )
  10730.         return(realm);
  10731.  
  10732.     code = krb5_init_context(&kcontext);
  10733.     if (code) {
  10734.         return(realm);
  10735.     }
  10736.  
  10737.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10738.     if (code != 0) {
  10739.         goto exit_k5_getrealm;
  10740.     }
  10741.  
  10742.     if ((code = krb5_parse_name(kcontext, "foo", &me))) {
  10743.         goto exit_k5_getrealm;
  10744.     }
  10745.     if ( krb5_princ_realm(kcontext, me)->length < sizeof(realm) ) {
  10746.         memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  10747.                 krb5_princ_realm(kcontext, me)->length);        /* safe */
  10748.        realm[krb5_princ_realm(kcontext, me)->length]='\0';
  10749.     }
  10750.   exit_k5_getrealm:
  10751.     if ( ccache )
  10752.         krb5_cc_close(kcontext,ccache);
  10753.     if (kcontext)
  10754.         krb5_free_context(kcontext);
  10755.     return(realm);
  10756. #else /* HEIMDAL */
  10757.     return("Not implemented");
  10758. #endif /* HEIMDAL */
  10759. #else /* KRB5 */
  10760.     return("");
  10761. #endif /* KRB5 */
  10762. }
  10763.  
  10764. char *
  10765. #ifdef CK_ANSIC
  10766. ck_krb5_getprincipal(char * cc_name)
  10767. #else
  10768. ck_krb5_getprincipal(cc_name) char * cc_name;
  10769. #endif
  10770. {
  10771. #ifdef KRB5
  10772. #ifndef HEIMDAL
  10773.     static char principal[UIDBUFLEN+1]="";
  10774.     krb5_context kcontext;
  10775.     krb5_ccache ccache = NULL;
  10776.     krb5_error_code code;
  10777.     krb5_principal me;
  10778.     char * p=NULL;
  10779.     int i;
  10780.  
  10781.     principal[0] = '\0';
  10782.  
  10783.     if ( !ck_krb5_is_installed() )
  10784.         return(principal);
  10785.  
  10786.     code = krb5_init_context(&kcontext);
  10787.     if (code) {
  10788.         return(principal);
  10789.     }
  10790.  
  10791.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10792.     if (code != 0) {
  10793.         goto exit_k5_getprincipal;
  10794.     }
  10795.  
  10796.     if ((code = krb5_cc_get_principal(kcontext, ccache, &me))) {
  10797.         goto exit_k5_getprincipal;
  10798.     }
  10799.  
  10800.     if ((code = krb5_unparse_name (kcontext, me, &p))) {
  10801.         krb5_free_principal(kcontext,me);
  10802.         goto exit_k5_getprincipal;
  10803.     }
  10804.  
  10805.     ckstrncpy(principal,p,UIDBUFLEN);
  10806.     i = ckindex("@",principal,0,0,0);
  10807.     if (i)
  10808.       principal[i-1] = '\0';
  10809.  
  10810.     krb5_free_unparsed_name(kcontext,p);
  10811.  
  10812.   exit_k5_getprincipal:
  10813.     if ( ccache )
  10814.         krb5_cc_close(kcontext,ccache);
  10815.     if (kcontext)
  10816.         krb5_free_context(kcontext);
  10817.     return(principal);
  10818. #else /* HEIMDAL */
  10819.     return("Not implemented");
  10820. #endif /* HEIMDAL */
  10821. #else /* KRB5 */
  10822.     return("");
  10823. #endif /* KRB5 */
  10824. }
  10825.  
  10826. #ifndef CRYPT_DLL
  10827. int
  10828. ck_get_crypt_table(struct keytab ** pTable, int * pN)
  10829. {
  10830. #ifdef CK_ENCRYPTION
  10831.     return(get_crypt_table(pTable, pN));
  10832. #else /* ENCRYPTION */
  10833.     int i=0;
  10834. #ifndef OS2
  10835.     char * tmpstring = NULL;
  10836. #endif /* OS2 */
  10837.  
  10838.     if ( *pTable )
  10839.     {
  10840.         for ( i=0 ; i < *pN ; i++ )
  10841.             free( (*pTable)[i].kwd ) ;
  10842.         free ( *pTable )  ;
  10843.     }
  10844.     *pTable = NULL;
  10845.     *pN = 0;
  10846.  
  10847.     *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  10848.     if ( !(*pTable) )
  10849.         return(0);
  10850.  
  10851. #ifdef OS2
  10852.     (*pTable)[0].kwd =strdup("automatic");
  10853. #else /* OS2 */
  10854.     makestr(&tmpstring,"automatic");
  10855.     (*pTable)[0].kwd = tmpstring;
  10856.     tmpstring = NULL;
  10857. #endif /* OS2 */
  10858.     (*pTable)[0].kwval = ENCTYPE_ANY;
  10859.     (*pTable)[0].flgs = 0;
  10860. #ifdef OS2
  10861.     (*pTable)[1].kwd =strdup("none");
  10862. #else /* OS2 */
  10863.     makestr(&tmpstring,"none");
  10864.     (*pTable)[1].kwd = tmpstring;
  10865.     tmpstring = NULL;
  10866. #endif /* OS2 */
  10867.     (*pTable)[1].kwval = 999;
  10868.     (*pTable)[1].flgs = 0;
  10869.     (*pN) = 2;
  10870.  
  10871.     return(2);
  10872. #endif /* ENCRYPTION */
  10873. }
  10874.  
  10875. VOID
  10876. ck_encrypt_send_support()
  10877. {
  10878. #ifdef CK_ENCRYPTION
  10879.     encrypt_send_support();
  10880. #endif /* ENCRYPTION */
  10881. }
  10882. #endif /* CRYPT_DLL */
  10883.  
  10884. /*
  10885.  *
  10886.  * Kstream
  10887.  *
  10888.  * Emulates the kstream package in Kerberos 4
  10889.  *
  10890.  */
  10891.  
  10892. int
  10893. kstream_destroy()
  10894. {
  10895.     if (g_kstream != NULL) {
  10896.         auth_destroy();                       /* Destroy authorizing */
  10897.         free(g_kstream);
  10898.         g_kstream=NULL;
  10899.     }
  10900.     return 0;
  10901. }
  10902.  
  10903. VOID
  10904. #ifdef CK_ANSIC
  10905. kstream_set_buffer_mode(int mode)
  10906. #else
  10907. kstream_set_buffer_mode(mode) int mode;
  10908. #endif
  10909. {
  10910. }
  10911.  
  10912.  
  10913. int
  10914. #ifdef CK_ANSIC
  10915. kstream_create_from_fd(int fd,
  10916.                        kstream_ptr data)
  10917. #else
  10918. kstream_create_from_fd(fd,data)
  10919.     int fd; kstream_ptr data;
  10920. #endif
  10921. {
  10922.     int n;
  10923.  
  10924.     g_kstream = malloc(sizeof(struct kstream_int));
  10925.     if (g_kstream == NULL)
  10926.         return 0;
  10927.  
  10928.     g_kstream->fd = fd;
  10929.  
  10930.     n = auth_init(g_kstream);                   /* Initialize authorizing */
  10931.     if (n) {
  10932.         free(g_kstream);
  10933.         g_kstream = NULL;
  10934.         return 0;
  10935.     }
  10936.  
  10937.     g_kstream->encrypt = NULL;
  10938.     g_kstream->decrypt = NULL;
  10939.     g_kstream->encrypt_type = ENCTYPE_ANY;
  10940.     g_kstream->decrypt_type = ENCTYPE_ANY;
  10941.     return 1;
  10942. }
  10943.  
  10944. #ifdef CK_KERBEROS
  10945. #ifdef RLOGCODE
  10946. static int do_lencheck, use_ivecs;
  10947. extern int rlog_inband;
  10948.  
  10949. #ifdef KRB5
  10950. void
  10951. rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
  10952.                            protonum)
  10953.      krb5_keyblock *in_keyblock;
  10954.      int encrypt_flag;
  10955.      int lencheck;
  10956.      int am_client;
  10957.      enum krb5_kcmd_proto protonum;
  10958. {
  10959.     krb5_error_code status;
  10960.     size_t blocksize;
  10961.  
  10962.     if (!encrypt_flag)
  10963.         return;
  10964.  
  10965.     desinbuf.data = des_inbuf;
  10966.     desoutbuf.data = des_outpkt+4;      /* Set up des buffers */
  10967.     k5_session_key = in_keyblock;
  10968.  
  10969.     do_lencheck = lencheck;
  10970.  
  10971.     if ( protonum == KCMD_OLD_PROTOCOL ) {
  10972.         use_ivecs = 0;
  10973.         return;
  10974.     }
  10975.  
  10976.     use_ivecs = 1;
  10977.  
  10978.     if (status = krb5_c_block_size(k5_context, k5_session_key->enctype,
  10979.                                    &blocksize)) {
  10980.         /* XXX what do I do? */
  10981.         printf("fatal kerberos 5 crypto library error\n");
  10982.         ttclos(0);
  10983.         return;
  10984.     }
  10985.  
  10986.     encivec_i[0].length = encivec_i[1].length = 
  10987.     encivec_o[0].length = encivec_o[1].length = blocksize;
  10988.  
  10989.     if ((encivec_i[0].data = malloc(encivec_i[0].length * 4)) == NULL) {
  10990.         /* XXX what do I do? */
  10991.         printf("fatal malloc failed\n");
  10992.         ttclos(0);
  10993.         return;
  10994.     }
  10995.  
  10996.     encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
  10997.     encivec_o[0].data = encivec_i[1].data + encivec_i[1].length;
  10998.     encivec_o[1].data = encivec_o[0].data + encivec_o[0].length;
  10999.  
  11000.     /* is there a better way to initialize this? */
  11001.     memset(encivec_i[0].data, am_client, blocksize);
  11002.     memset(encivec_o[0].data, 1 - am_client, blocksize);
  11003.     memset(encivec_i[1].data, 2 | am_client, blocksize);
  11004.     memset(encivec_o[1].data, 2 | (1 - am_client), blocksize);
  11005. }
  11006. #endif /* KRB5 */
  11007.  
  11008. int
  11009. #ifdef CK_ANSIC
  11010. ck_krb_rlogin(CHAR * hostname, int port,
  11011.                CHAR * localuser, CHAR * remoteuser, CHAR * term_speed,
  11012.                struct sockaddr_in * l_addr, struct sockaddr_in * r_addr,
  11013.                int kversion, int encrypt_flag)
  11014. #else /* CK_ANSIC */
  11015. ck_krb_rlogin(hostname, port,
  11016.                localuser, remoteuser, term_speed, l_addr, r_addr, encrypt_flag)
  11017.     CHAR * hostname; int port;
  11018.     CHAR * localuser; CHAR * remoteuser; CHAR * term_speed;
  11019.     struct sockaddr_in * l_addr; struct sockaddr_in * r_addr;
  11020.     int kversion; int encrypt_flag;
  11021. #endif /* CK_ANSIC */
  11022. {
  11023.     unsigned long status;
  11024.     char * realm=NULL;
  11025.     extern int ttyfd;
  11026.     int c;
  11027.     long msglen;
  11028.  
  11029.     debug(F111,"ck_krb_rlogin",hostname,port);
  11030.  
  11031.     if ( kversion == 4 && !ck_krb4_is_installed() ) {
  11032.         printf("?Kerberos 4 is not installed\r\n");
  11033.         return(-1);
  11034.     } else if ( kversion == 5 && !ck_krb5_is_installed() ) {
  11035.         printf("?Kerberos 5 is not installed\r\n");
  11036.         return(-1);
  11037.     }
  11038.  
  11039.     if ( encrypt_flag && !ck_crypt_is_installed() ) {
  11040.         printf("?Encryption is not installed\r\n");
  11041.         return(-1);
  11042.     }
  11043.  
  11044.     if ( kversion == 5 ) {
  11045. #ifdef KRB5
  11046.         krb5_flags authopts=0;
  11047.         krb5_ccache ccache=NULL;
  11048.         char *cksumbuf=NULL;
  11049.         char *service=NULL;
  11050.         char * kcmd_version=NULL;
  11051.         enum krb5_kcmd_proto use_proto;
  11052.         krb5_data cksumdat;
  11053.         krb5_creds *get_cred = 0;
  11054.         krb5_error_code status;
  11055.         krb5_error      *error = 0;
  11056.         krb5_ap_rep_enc_part *rep_ret = NULL;
  11057.         krb5_data outbuf;
  11058.         int rc;
  11059.         krb5_int32 seqno=0;
  11060.         krb5_int32 server_seqno=0;
  11061.         char ** realmlist=NULL;
  11062.         int buflen;
  11063.         char tgt[256];
  11064.  
  11065.         debug(F100,"ck_krb_rlogin version 5","",0);
  11066.  
  11067.         realm = ck_krb5_realmofhost(hostname);
  11068.         if (!realm) {
  11069.             ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  11070.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11071.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11072.             printf("?Kerberos 5 error: %s\r\n",strTmp);
  11073.             krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  11074.             makestr(&krb5_errmsg,strTmp);
  11075.             return(0);
  11076.         }
  11077.  
  11078.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11079.         debug(F110,"ck_rlog_rlogin TGT",tgt,0);
  11080.         if ( krb5_autoget &&
  11081.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11082.                 (ck_krb5_is_tgt_valid() > 0)) )
  11083.             ck_krb5_autoget_TGT(realm);
  11084.  
  11085.         buflen = strlen(term_speed)+strlen(remoteuser)+64;
  11086.         if ((cksumbuf = malloc(buflen)) == 0)
  11087.         {
  11088.             printf("Unable to allocate memory for checksum buffer.\r\n");
  11089.             return(-1);
  11090.         }
  11091.  
  11092.         ckmakmsg(cksumbuf,buflen,ckuitoa((unsigned short) ntohs(port)),":",
  11093.                   term_speed,remoteuser);
  11094.         cksumdat.data = cksumbuf;
  11095.         cksumdat.length = strlen(cksumbuf);
  11096.  
  11097.         status = krb5_init_context(&k5_context);
  11098.         if (status) {
  11099.             debug(F110,"ck_krb_rlogin()","unable to init_context",0);
  11100.             return(-1);
  11101.         }
  11102.  
  11103.         desinbuf.data = des_inbuf;
  11104.         desoutbuf.data = des_outpkt+4;  /* Set up des buffers */
  11105.  
  11106.         rc = k5_get_ccache(k5_context,&ccache,NULL);
  11107.         if (rc != 0) {
  11108.             com_err(NULL, rc, "while getting ccache.");
  11109.             return(0);
  11110.         }
  11111.  
  11112.         service = krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME;
  11113.  
  11114.         if (!(get_cred = (krb5_creds *)calloc(1, sizeof(krb5_creds)))) {
  11115.             printf("ck_krb_rlogin: no memory\r\n");
  11116.             return(-1);
  11117.         }
  11118.         memset(get_cred,0,sizeof(krb5_creds));
  11119.         status = krb5_sname_to_principal(k5_context, hostname, service,
  11120.                                           KRB5_NT_SRV_HST, &get_cred->server);
  11121.         if (status) {
  11122.             printf("ck_krb_rlogin: krb5_sname_to_principal failed: %s\r\n",
  11123.                      error_message(status));
  11124.             return(-1);
  11125.         }
  11126.  
  11127.         ttoc(0);
  11128.  
  11129.         if (status = krb5_cc_get_principal(k5_context,
  11130.                                            ccache,
  11131.                                            &get_cred->client)
  11132.             ) {
  11133.             (void) krb5_cc_close(k5_context, ccache);
  11134.             krb5_free_creds(k5_context, get_cred);
  11135.             goto bad;
  11136.         }
  11137.  
  11138.         if (krb5_rlog_ver == KCMD_OLD_PROTOCOL)
  11139.             get_cred->keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  11140.  
  11141.         /* Get ticket from credentials cache or kdc */
  11142.         status = krb5_get_credentials(k5_context,
  11143.                                       0,
  11144.                                       ccache,
  11145.                                       get_cred,
  11146.                                       &ret_cred
  11147.                                       );
  11148.         krb5_free_creds(k5_context, get_cred);
  11149.         get_cred = NULL;
  11150.         (void) krb5_cc_close(k5_context, ccache);
  11151.  
  11152.         if (status) 
  11153.             goto bad;
  11154.  
  11155.         /* Reset internal flags; these should not be set. */
  11156.         authopts &= (~OPTS_FORWARD_CREDS);
  11157.         authopts &= (~OPTS_FORWARDABLE_CREDS);
  11158.  
  11159.         if (krb5_auth_con_init(k5_context, &auth_context))
  11160.             goto bad;
  11161.  
  11162.         if (krb5_auth_con_setflags(k5_context, auth_context,
  11163.                                     KRB5_AUTH_CONTEXT_RET_TIME))
  11164.             goto bad;
  11165.  
  11166.         /* Only need local address for mk_cred() to send to krlogind */
  11167.         if (!krb5_d_no_addresses)
  11168.             if (status = krb5_auth_con_genaddrs(k5_context,
  11169.                                                 auth_context,
  11170.                                                  ttyfd,
  11171.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
  11172.                                                  ))
  11173.                 goto bad;
  11174.  
  11175.         /* Here is where we start to handle the new protocol in earnest */
  11176.         if ( krb5_rlog_ver == KCMD_PROTOCOL_COMPAT_HACK ) {
  11177.             krb5_boolean is_des;
  11178.  
  11179.             if (status = krb5_c_enctype_compare( k5_context,
  11180.                                                  ENCTYPE_DES_CBC_CRC,
  11181. #ifdef HEIMDAL
  11182.                                                  ret_cred->session.keytype,
  11183. #else /* HEIMDAL */
  11184.                                                  ret_cred->keyblock.enctype,
  11185. #endif /* HEIMDAL */
  11186.                                                  &is_des)) {
  11187.                 krb5_free_creds(k5_context, ret_cred);
  11188.                 ret_cred = NULL;
  11189.                 goto bad;
  11190.             }
  11191.  
  11192.             if ( is_des ) {
  11193.                 kcmd_version = "KCMDV0.1";
  11194.                 use_proto = KCMD_OLD_PROTOCOL;
  11195.             } else {
  11196.                 authopts = AP_OPTS_USE_SUBKEY;
  11197.                 kcmd_version = "KCMDV0.2";
  11198.                 use_proto = KCMD_NEW_PROTOCOL;
  11199.             }
  11200.         } else {
  11201.             use_proto = krb5_rlog_ver;
  11202.             switch ( krb5_rlog_ver ) {
  11203.             case KCMD_NEW_PROTOCOL:
  11204.                 authopts = AP_OPTS_USE_SUBKEY;
  11205.                 kcmd_version = "KCMDV0.2";
  11206.                 break;
  11207.             case KCMD_OLD_PROTOCOL:
  11208.                 kcmd_version = "KCMDV0.1";
  11209.                 break;
  11210.             default:
  11211.                 goto bad;
  11212.             }
  11213.         }
  11214.  
  11215.         /* call Kerberos library routine to obtain an authenticator,
  11216.            pass it over the socket to the server, and obtain mutual
  11217.            authentication.
  11218.          */
  11219.         status = krb5_sendauth(k5_context,
  11220.                                &auth_context,
  11221.                                (krb5_pointer) &ttyfd,
  11222.                                kcmd_version,
  11223.                                ret_cred->client,
  11224.                                ret_cred->server,
  11225.                                 authopts,
  11226.                                &cksumdat,
  11227.                                ret_cred,
  11228.                                0,
  11229.                                &error,
  11230.                                &rep_ret,
  11231.                                NULL
  11232.                                );
  11233.         krb5_free_data_contents(k5_context,&cksumdat);
  11234.  
  11235.         if (status) {
  11236.             if ( !quiet )
  11237.                 printf("Couldn't authenticate to server: %s\r\n",
  11238.                         error_message(status));
  11239.             if (error) {
  11240.                 if ( !quiet ) {
  11241.                     printf("Server returned error code %d (%s)\r\n",
  11242.                         error->error,
  11243.                         error_message(ERROR_TABLE_BASE_krb5 + error->error));
  11244.                     if (error->text.length) {
  11245.                         printf("Error text sent from server: %s\r\n",
  11246.                                 error->text.data);
  11247.                     }
  11248.                 }
  11249.                 krb5_free_error(k5_context, error);
  11250.                 error = 0;
  11251.             }
  11252.             goto bad;
  11253.         }
  11254.  
  11255.         if (rep_ret) {
  11256.             server_seqno = rep_ret->seq_number;
  11257.             krb5_free_ap_rep_enc_part(k5_context, rep_ret);
  11258.         }
  11259.  
  11260.         (void) ttol(remoteuser, strlen(remoteuser)+1);
  11261.         (void) ttol(term_speed, strlen(term_speed)+1);
  11262.         (void) ttol(localuser, strlen(localuser)+1);
  11263.  
  11264.         if (forward_flag) {   /* Forward credentials (global) */
  11265.             if (status = krb5_fwd_tgt_creds( k5_context,
  11266.                                              auth_context,
  11267.                                              hostname,
  11268.                                              ret_cred->client,
  11269.                                              ret_cred->server,
  11270.                                              0,
  11271.                                              (forwardable_flag ?
  11272.                                                OPTS_FORWARDABLE_CREDS :
  11273.                                                0),
  11274.                                              &outbuf
  11275.                                              )
  11276.                  )
  11277.             {
  11278.                 printf("Error forwarding credentials: %s\r\n",
  11279.                          error_message(status));
  11280.                 goto bad2;
  11281.             }
  11282.  
  11283.             /* Send forwarded credentials */
  11284.             status = krb5_write_message(k5_context,
  11285.                                          (krb5_pointer)&ttyfd,
  11286.                                          &outbuf
  11287.                                          );
  11288.         }
  11289.         else { /* Dummy write to signal no forwarding */
  11290.           bad2:
  11291.             outbuf.length = 0;
  11292.             status = krb5_write_message(k5_context,
  11293.                                          (krb5_pointer)&ttyfd,
  11294.                                          &outbuf);
  11295.         }       
  11296.  
  11297.         if ((c = ttinc(0)) < 0) {
  11298.             if (c==-1) {
  11299.                 perror(hostname);
  11300.             } else {
  11301.                 printf("ck_krb_rlogin: bad connection with remote host\r\n");
  11302.             }
  11303.             status = -1;
  11304.             goto bad;
  11305.         }
  11306.         if (c != 0) {
  11307.             while ((c = ttinc(1)) >= 0) {
  11308.                 (void) printf("%c",c);
  11309.                 if (c == '\n')
  11310.                     break;
  11311.             }
  11312.             status = -1;
  11313.             goto bad;
  11314.         }
  11315.  
  11316.         if ( status == 0 ) {        /* success */
  11317.             krb5_keyblock * key = 0;
  11318.  
  11319.             if ( use_proto == KCMD_NEW_PROTOCOL ) {
  11320.                 int on = 1;
  11321.                 rlog_inband = 1;
  11322.                 setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,
  11323.                            (char *) &on, sizeof on);
  11324.  
  11325.                 status = krb5_auth_con_getlocalsubkey( k5_context,
  11326.                                                        auth_context,
  11327.                                                        &key);
  11328.                 if ((status || !key) && encrypt_flag )
  11329.                     goto bad2;
  11330.             }
  11331.             if ( key == 0 ) {
  11332. #ifdef HEIMDAL
  11333.                 key = &ret_cred->session;
  11334. #else /* HEIMDAL */
  11335.                 key = &ret_cred->keyblock;
  11336. #endif /* HEIMDAL */
  11337.             }
  11338.  
  11339.             rcmd_stream_init_krb5(key, encrypt_flag, 1, 1, use_proto);
  11340.             if ( encrypt_flag )
  11341.                 rlog_encrypt = 1;
  11342.         }
  11343.         return (0);     /* success */
  11344.  
  11345.       bad:
  11346.         if ( status && !quiet ) {
  11347.             printf("Kerberos authentication error: %s\r\n",
  11348.                     error_message(status));
  11349.         }
  11350.         if (ret_cred) {
  11351.             krb5_free_creds(k5_context, ret_cred);
  11352.             ret_cred = NULL;
  11353.         }
  11354.         return (status);
  11355. #else /* KRB5 */
  11356.         return(-1);
  11357. #endif /* KRB5 */
  11358.     } else if (kversion == 4) {
  11359. #ifdef KRB4
  11360.         char tgt[4*REALM_SZ+1];
  11361.         debug(F100,"ck_krb_rlogin version 4","",0);
  11362.  
  11363.         realm = (char *)krb_realmofhost(hostname);
  11364.         if (!realm) {
  11365.             strcpy(strTmp, "Can't find realm for host \"");
  11366.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11367.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11368.             printf("?Kerberos 4 error: %s\r\n",strTmp);
  11369.             krb4_errno = 0;
  11370.             makestr(&krb4_errmsg,strTmp);
  11371.             return(0);
  11372.         }
  11373.  
  11374.         ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  11375.         status = ck_krb4_tkt_isvalid(tgt);
  11376.  
  11377.         if ( status <= 0 && krb4_autoget )
  11378.             ck_krb4_autoget_TGT(realm);
  11379.  
  11380.         ttoc(0);        /* write a NUL */
  11381.  
  11382.         status = krb_sendauth(encrypt_flag?KOPT_DO_MUTUAL:0,
  11383.                                ttyfd,
  11384.                                &k4_auth,
  11385.                                krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  11386.                                hostname,
  11387.                                realm,
  11388.                                (unsigned long) getpid(),
  11389.                                &k4_msg_data,
  11390.                                (CREDENTIALS *)&cred,
  11391. #ifdef CK_ENCRYPTION
  11392.                                &k4_sched,
  11393. #else /* ENCRYPTION */
  11394.                                NULL,
  11395. #endif /* ENCRYPTION */
  11396.                                l_addr,
  11397.                                r_addr,
  11398.                                "KCMDV0.1");
  11399.         debug(F111,"ck_krb_rlogin","krb_sendauth",status);
  11400.         if (status != KSUCCESS) {
  11401.             printf( "krb_sendauth failed: %s\r\n",
  11402.                     krb_get_err_text_entry(status)
  11403.                     );
  11404.             return(-1);
  11405.         }
  11406.         ttol(remoteuser,strlen(remoteuser)+1);
  11407.         ttol(term_speed,strlen(term_speed)+1);
  11408.  
  11409.       reread:
  11410.         if ((c = ttinc(0)) < 0) {
  11411.             printf("rcmd: bad connection with remote host\r\n");
  11412.             return(-1);
  11413.         }
  11414.         debug(F111,"ck_krb_rlogin","first byte",c);
  11415.  
  11416.         if (c != 0) {
  11417.             char *check = "ld.so: warning:";
  11418.             /* If rlogind was compiled on SunOS4, and it somehow
  11419.             got the shared library version numbers wrong, it
  11420.             may give an ld.so warning about an old version of a
  11421.             shared library.  Just ignore any such warning.
  11422.             Note that the warning is a characteristic of the
  11423.             server; we may not ourselves be running under
  11424.             SunOS4.  */
  11425.             if (c == 'l') {
  11426.                 char *p;
  11427.                 char cc;
  11428.  
  11429.                 p = &check[1];
  11430.                 while ((c = ttinc(0)) >= 0) {
  11431.                     if (*p == '\0') {
  11432.                         if (c == '\n')
  11433.                             break;
  11434.                     } else {
  11435.                         if (c != *p)
  11436.                             break;
  11437.                         ++p;
  11438.                     }
  11439.                 }
  11440.  
  11441.                 if (*p == '\0')
  11442.                     goto reread;
  11443.             }
  11444.  
  11445.             printf(check);
  11446.             while ((c = ttinc(1)) >= 0) {
  11447.                 printf("%c",c);
  11448.                 if (c == '\n')
  11449.                     break;
  11450.             }
  11451.             debug(F110,"ck_krb_rlogin","fatal error 1",0);
  11452.             return(-1);
  11453.         }
  11454.  
  11455. #ifdef CK_ENCRYPTION
  11456.         if ( encrypt_flag ) {
  11457.             /* if we are encrypting we need to setup the encryption */
  11458.             /* routines.                                            */
  11459.             des_key_sched(cred.session, k4_sched);
  11460.             rlog_encrypt = 1;
  11461.         }
  11462. #endif /* ENCRYPTION */
  11463. #else /* KRB4 */
  11464.         return(-1);
  11465. #endif /* KRB4 */
  11466.     }
  11467.     return(0); /* success */
  11468. }
  11469.  
  11470. #define SRAND   srand
  11471. #define RAND    rand
  11472. #define RAND_TYPE       int
  11473.  
  11474. static long
  11475. random_confounder(size, fillin)
  11476. size_t size;
  11477. char * fillin;
  11478. {
  11479.     static int seeded = 0;
  11480.     register unsigned char *real_fill;
  11481.     RAND_TYPE   rval;
  11482.  
  11483.     if (!seeded) {
  11484.         /* time() defined in 4.12.2.4, but returns a time_t, which is an
  11485.            "arithmetic type" (4.12.1) */
  11486.         rval = (RAND_TYPE) time(0);
  11487.         SRAND(rval);
  11488.         rval = RAND();
  11489.         rval ^= getpid();
  11490.         SRAND(rval);
  11491.         seeded = 1;
  11492.     }
  11493.  
  11494.     real_fill = (unsigned char *)fillin;
  11495.     while (size > 0) {
  11496.         rval = RAND();
  11497.         *real_fill = rval & 0xff;
  11498.         real_fill++;
  11499.         size--;
  11500.         if (size) {
  11501.             *real_fill = (rval >> 8) & 0xff;
  11502.             real_fill++;
  11503.             size--;
  11504.         }
  11505.     }
  11506.     return 0;
  11507. }
  11508.  
  11509. #ifdef KRB5
  11510. int
  11511. krb5_des_avail(fd)
  11512.     int fd;
  11513. {
  11514.     return(nstored);
  11515. }
  11516.  
  11517. int
  11518. krb5_des_read(fd, buf, len, secondary)
  11519.     int fd;
  11520.     register char *buf;
  11521.     int len;
  11522.     int secondary;
  11523. {
  11524.     int nreturned = 0;
  11525.     long net_len,rd_len;
  11526.     int cc;
  11527.     krb5_error_code status;
  11528.     unsigned char c;
  11529.     krb5_data plain;
  11530.     krb5_enc_data cipher;
  11531.  
  11532.     debug(F111,"krb5_des_read","len",len);
  11533.     debug(F111,"krb5_des_read","rlog_encrypt",rlog_encrypt);
  11534.     if ( !rlog_encrypt ) {
  11535.         cc = net_read(fd, buf, len);
  11536.         debug(F111,"krb5_des_read","chars read",cc);
  11537.         if ( cc < 0 )
  11538.             netclos();
  11539.         return(cc);
  11540.     }
  11541.  
  11542.     if (nstored >= len) {
  11543.         if ( buf ) {
  11544.             memcpy(buf, store_ptr, len);        /* safe */
  11545.             store_ptr += len;
  11546.             nstored -= len;
  11547.             return(len);
  11548.         } else
  11549.             return(0);
  11550.     } else if (nstored) {
  11551.         if ( buf ) {
  11552.             memcpy(buf, store_ptr, nstored);    /* safe */
  11553.             nreturned += nstored;
  11554.             buf += nstored;
  11555.             len -= nstored;
  11556.             nstored = 0;
  11557.         }
  11558.         else
  11559.             return(0);
  11560.     }
  11561.  
  11562.     /* See the comment in v4_des_read. */
  11563.     while (1) {
  11564.         cc = net_read(fd, &c, 1);
  11565.         /* we should check for non-blocking here, but we'd have
  11566.         to make it save partial reads as well. */
  11567.         if (cc <= 0) {
  11568.             return cc; /* read error */
  11569.         }
  11570.         if (cc == 1) {
  11571.             if (c == 0 || !do_lencheck) 
  11572.                 break;
  11573.         }
  11574.     }
  11575.  
  11576.     rd_len = c;
  11577.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11578.     rd_len = (rd_len << 8) | c;
  11579.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11580.     rd_len = (rd_len << 8) | c;
  11581.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11582.     rd_len = (rd_len << 8) | c;
  11583.  
  11584.     if (status = krb5_c_encrypt_length(k5_context, 
  11585.                                     k5_session_key->enctype,
  11586.                                     use_ivecs ? rd_len + 4 : rd_len,
  11587.                                     &net_len)) {
  11588.         errno = status;
  11589.         return(-1);
  11590.     }
  11591.  
  11592.     if ((net_len <= 0) || (net_len > sizeof(des_inbuf))) {
  11593.         /* preposterous length; assume out-of-sync; only
  11594.            recourse is to close connection, so return 0 */
  11595.         printf("Read size problem.\r\n");
  11596.         return(0);
  11597.     }
  11598.     if ((cc = net_read(fd, desinbuf.data, net_len)) != net_len )
  11599.     {
  11600.         /* pipe must have closed, return 0 */
  11601.         printf( "Read error: length received %d != expected %d.\r\n",
  11602.                 cc,
  11603.                 net_len
  11604.                 );
  11605.         return(cc);
  11606.     }
  11607.  
  11608.  
  11609.     /* decrypt info */
  11610.     cipher.enctype = ENCTYPE_UNKNOWN;
  11611.     cipher.ciphertext.length = net_len;
  11612.     cipher.ciphertext.data = desinbuf.data;
  11613.     plain.length = sizeof(storage);
  11614.     plain.data = storage;
  11615.  
  11616.     if ( status = krb5_c_decrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11617.                                  use_ivecs ? encivec_i + secondary : 0,
  11618.                                   &cipher,&plain) ) {
  11619.         /* probably out of sync */
  11620.         printf("Cannot decrypt data from network: %s\r\n",
  11621.                  error_message(status));
  11622.         errno = EIO;
  11623.         return(-1);
  11624.     }
  11625.     
  11626.     store_ptr = storage;
  11627.     nstored = rd_len;
  11628.  
  11629.     if ( use_ivecs ) {
  11630.         int rd_len2;
  11631.         rd_len2 = storage[0] & 0xff;
  11632.         rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
  11633.         rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
  11634.         rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
  11635.         if (rd_len2 != rd_len) {
  11636.             /* cleartext length trashed? */
  11637.             errno = EIO;
  11638.             return -1;
  11639.         }
  11640.         store_ptr += 4;
  11641.     }
  11642.  
  11643.     if ( !buf )
  11644.         return(0);
  11645.  
  11646. #ifdef RLOGCODE                         /* blah */
  11647.     if (rlog_inband && (ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN))
  11648.     {
  11649.         int i, left, n;
  11650.  
  11651.         for (i = 0; i < nstored; i++) {
  11652.             if (store_ptr[i] == '\377' &&
  11653.                 store_ptr[i+1] == '\377') {
  11654.                 left = nstored - i;
  11655.                 n = rlog_ctrl(&store_ptr[i], left);
  11656.                 if (n < 0) {
  11657.                     left -= (-n);
  11658.                     nstored = left;
  11659.                     /* flush before, and (-n) bytes */
  11660.                     if (left > 0)
  11661.                         memmove(store_ptr, &store_ptr[i-n], left);
  11662.                 } else if (n) {
  11663.                     left -= n;
  11664.                     nstored -= n;
  11665.                     if (left > 0)
  11666.                         memmove(store_ptr, &store_ptr[n], left);
  11667.                 }
  11668.             }
  11669.         }
  11670.     }
  11671. #endif /* RLOGCODE */
  11672.  
  11673.     if (nstored > len) {
  11674.         memcpy(buf, store_ptr, len);            /* safe */
  11675.         nreturned += len;
  11676.         store_ptr += len;
  11677.         nstored -= len;
  11678.     } else {
  11679.         memcpy(buf, store_ptr, nstored);        /* safe */
  11680.         nreturned += nstored;
  11681.         nstored = 0;
  11682.     }
  11683.     return(nreturned);
  11684. }
  11685.  
  11686. int
  11687. krb5_des_write(fd, buf, len, secondary)
  11688.     int fd;
  11689.     char *buf;
  11690.     int len;
  11691.     int secondary;
  11692. {
  11693.     char tmpbuf[2*RLOG_BUFSIZ+8];
  11694.     unsigned char *len_buf = (unsigned char *) tmpbuf;
  11695.     krb5_error_code status;
  11696.     krb5_data plain;
  11697.     krb5_enc_data cipher;
  11698.  
  11699.     debug(F111,"krb5_des_write","rlog_encrypt",rlog_encrypt);
  11700.     if ( !rlog_encrypt ) {
  11701.         int cc = net_write(fd, buf, len);
  11702.         debug(F111,"net_write","chars written",cc);
  11703.         return(cc != len ? -1 : len);
  11704.     }
  11705.  
  11706.     if (use_ivecs) {
  11707.         unsigned char *lenbuf2 = (unsigned char *) tmpbuf;
  11708.         if (len + 4 > sizeof(tmpbuf))
  11709.             abort ();
  11710.         lenbuf2[0] = (len & 0xff000000) >> 24;
  11711.         lenbuf2[1] = (len & 0xff0000) >> 16;
  11712.         lenbuf2[2] = (len & 0xff00) >> 8;
  11713.         lenbuf2[3] = (len & 0xff);
  11714.         memcpy (tmpbuf + 4, buf, len);
  11715.  
  11716.         plain.data = tmpbuf;
  11717.         plain.length = len + 4;
  11718.     } else {
  11719.         plain.data = buf;
  11720.         plain.length = len;
  11721.     }
  11722.  
  11723.     cipher.ciphertext.length = sizeof(des_outpkt)-4;
  11724.     cipher.ciphertext.data = desoutbuf.data;
  11725.  
  11726.     if ( status = krb5_c_encrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11727.                          use_ivecs ? encivec_o + secondary : 0,
  11728.                          &plain, &cipher)) {
  11729.         printf("Write encrypt problem: %s.\r\n",
  11730.                  error_message(status));
  11731.         errno = EIO;
  11732.         return(-1);
  11733.     }
  11734.     desoutbuf.length = cipher.ciphertext.length;
  11735.  
  11736.     len_buf = (unsigned char *) des_outpkt;
  11737.     len_buf[0] = (len & 0xff000000) >> 24;
  11738.     len_buf[1] = (len & 0xff0000) >> 16;
  11739.     len_buf[2] = (len & 0xff00) >> 8;
  11740.     len_buf[3] = (len & 0xff);
  11741.  
  11742.     if (net_write(fd, des_outpkt,desoutbuf.length+4)
  11743.          != desoutbuf.length+4){
  11744.         printf("Could not write out all data\r\n");
  11745.         return(-1);
  11746.     }
  11747.     else return(len);
  11748. }
  11749. #endif /* KRB5 */
  11750.  
  11751. #ifdef KRB4
  11752. /*
  11753.  * Note that the encrypted rlogin packets take the form of a four-byte
  11754.  * length followed by encrypted data.  On writing the data out, a significant
  11755.  * performance penalty is suffered (at least one RTT per character, two if we
  11756.  * are waiting for a shell to echo) by writing the data separately from the
  11757.  * length.  So, unlike the input buffer, which just contains the output
  11758.  * data, the output buffer represents the entire packet.
  11759.  */
  11760.  
  11761. int
  11762. krb4_des_avail(fd)
  11763.     int fd;
  11764. {
  11765.     return(nstored);
  11766. }
  11767.  
  11768. int
  11769. krb4_des_read(fd, buf, len)
  11770. int fd;
  11771. register char *buf;
  11772. int len;
  11773. {
  11774.     int nreturned = 0;
  11775.     unsigned long net_len, rd_len;
  11776.     int cc;
  11777.     unsigned char c;
  11778.     int gotzero = 0;
  11779.  
  11780.     debug(F111,"krb4_des_read","rlog_encrypt",rlog_encrypt);
  11781.     debug(F111,"krb4_des_read","len",len);
  11782.     if ( !rlog_encrypt ) {
  11783.         cc = net_read(fd, buf, len);
  11784.         debug(F111,"krb4_des_read","chars read",cc);
  11785.         if ( cc < 0 )
  11786.             netclos();
  11787.         return(cc);
  11788.     }
  11789.  
  11790.     if (nstored >= len) {
  11791.         if ( buf ) {
  11792.             debug(F111,"krb4_des_read (nstored >= len)","nstored",nstored);
  11793.             memcpy(buf, store_ptr, len);        /* safe */
  11794.             store_ptr += len;
  11795.             nstored -= len;
  11796.             return(len);
  11797.         } else
  11798.             return(0);
  11799.     } else if (nstored) {
  11800.         if ( buf ) {
  11801.             debug(F111,"krb4_des_read (nstored)","nstored",nstored);
  11802.             memcpy(buf, store_ptr, nstored);    /* safe */
  11803.             nreturned += nstored;
  11804.             buf += nstored;
  11805.             len -= nstored;
  11806.             nstored = 0;
  11807.         } else
  11808.             return(0);
  11809.     }
  11810.  
  11811.     /* We're fetching the length which is MSB first, and the MSB
  11812.     has to be zero unless the client is sending more than 2^24
  11813.     (16M) bytes in a single write (which is why this code is in
  11814.     rlogin but not rcp or rsh.) The only reasons we'd get something
  11815.     other than zero are:
  11816.     -- corruption of the tcp stream (which will show up when
  11817.     everything else is out of sync too)
  11818.     -- un-caught Berkeley-style "pseudo out-of-band data" which
  11819.     happens any time the user hits ^C twice.
  11820.     The latter is *very* common, as shown by an 'rlogin -x -d'
  11821.     using the CNS V4 rlogin.         Mark EIchin 1/95
  11822.     */
  11823.     debug(F110,"krb4_des_read",
  11824.           "about to call net_read() this will block",
  11825.           0
  11826.           );
  11827.     do {
  11828.         cc = net_read(fd, &c, 1);
  11829.         debug(F111,"net_read","chars read",cc);
  11830.         if (cc <= 0) {
  11831.             netclos();
  11832.             return(-1);
  11833.         }
  11834.         if (cc != 1) return 0; /* read error */
  11835.         if (cc == 1) {
  11836.             if (c == 0) gotzero = 1;
  11837.         }
  11838.     } while (!gotzero);
  11839.  
  11840.     debug(F110,"krb4_des_read","gotzero",0);
  11841.     cc = net_read(fd, &c, 1);
  11842.     debug(F111,"net_read","chars read",cc);
  11843.     if (cc < 0) {
  11844.         netclos();
  11845.         return(-1);
  11846.     } else if ( cc != 1 )
  11847.         return(0);
  11848.     net_len = c;
  11849.     cc = net_read(fd, &c, 1);
  11850.     debug(F111,"net_read","chars read",cc);
  11851.     if (cc < 0) {
  11852.         netclos();
  11853.         return(-1);
  11854.     } else if ( cc != 1 )
  11855.         return(0);
  11856.     net_len = (net_len << 8) | c;
  11857.     debug(F111,"net_read","chars read",cc);
  11858.     cc = net_read(fd, &c, 1);
  11859.     if (cc < 0) {
  11860.         netclos();
  11861.         return(-1);
  11862.     } else if ( cc != 1 )
  11863.         return(0);
  11864.     net_len = (net_len << 8) | c;
  11865.     debug(F111,"krb4_des_read","net_len",net_len);
  11866.  
  11867.     /* Note: net_len is unsigned */
  11868.     if (net_len > sizeof(des_inbuf)) {
  11869.         /* XXX preposterous length, probably out of sync.
  11870.         act as if pipe closed */
  11871.         return(0);
  11872.     }
  11873.     /* the writer tells us how much real data we are getting, but
  11874.     we need to read the pad bytes (8-byte boundary) */
  11875. #ifndef roundup
  11876. #define roundup(x,y) ((((x)+(y)-1)/(y))*(y))
  11877. #endif /* roundup */
  11878.     rd_len = roundup(net_len, 8);
  11879.     debug(F111,"krb4_des_read","rd_len",rd_len);
  11880.     cc = net_read(fd, des_inbuf, rd_len);
  11881.     debug(F111,"net_read","chars read",cc);
  11882.     if (cc < 0) {
  11883.         netclos();
  11884.         return(-1);
  11885.     } else if ( cc != rd_len )
  11886.         return(0);
  11887.  
  11888.     hexdump("krb4_des_read des_inbuf",des_inbuf,8);
  11889. #ifdef CK_ENCRYPTION
  11890. #ifdef KRB524
  11891.     (void) des_pcbc_encrypt(des_inbuf,
  11892.                              storage,
  11893.                              (net_len < 8) ? 8 : net_len,
  11894.                              k4_sched,
  11895.                              cred.session,
  11896.                              DECRYPT);
  11897. #else /* KRB524 */
  11898.     (void) des_pcbc_encrypt((Block *)des_inbuf,
  11899.                              (Block *)storage,
  11900.                              (net_len < 8) ? 8 : net_len,
  11901.                              k4_sched,
  11902.                              &cred.session,
  11903.                              DECRYPT);
  11904. #endif /* KRB524 */
  11905. #endif /* ENCRYPTION */
  11906.     hexdump("krb4_des_read storage",storage,8);
  11907.  
  11908.     /*
  11909.     * when the cleartext block is < 8 bytes, it is "right-justified"
  11910.     * in the block, so we need to adjust the pointer to the data
  11911.     */
  11912.     if (net_len < 8)
  11913.         store_ptr = storage + 8 - net_len;
  11914.     else
  11915.         store_ptr = storage;
  11916.     nstored = net_len;
  11917.  
  11918.     if ( !buf )
  11919.         return(0);
  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.  
  11932.     debug(F111,"net_read","nreturned",nreturned);
  11933.     return(nreturned);
  11934. }
  11935.  
  11936. int
  11937. krb4_des_write(fd, buf, len)
  11938. int fd;
  11939. char *buf;
  11940. int len;
  11941. {
  11942.     static char garbage_buf[8];
  11943.     unsigned char *len_buf = (unsigned char *) des_outpkt;
  11944.     int cc;
  11945.  
  11946.     debug(F111,"krb4_des_write","rlog_encrypt",rlog_encrypt);
  11947.     if ( !rlog_encrypt ) {
  11948.         cc = net_write(fd, buf, len);
  11949.         debug(F111,"net_write","chars written",cc);
  11950.         return(cc);
  11951.     }
  11952.  
  11953.     /*
  11954.     * pcbc_encrypt outputs in 8-byte (64 bit) increments
  11955.     *
  11956.     * it zero-fills the cleartext to 8-byte padding,
  11957.     * so if we have cleartext of < 8 bytes, we want
  11958.     * to insert random garbage before it so that the ciphertext
  11959.     * differs for each transmission of the same cleartext.
  11960.     * if len < 8 - sizeof(long), sizeof(long) bytes of random
  11961.     * garbage should be sufficient; leave the rest as-is in the buffer.
  11962.     * if len > 8 - sizeof(long), just garbage fill the rest.
  11963.     */
  11964.     if (len < 8) {
  11965.         random_confounder(8 - len, garbage_buf);
  11966.         /* this "right-justifies" the data in the buffer */
  11967.         (void) memcpy(garbage_buf + 8 - len, buf, len); /* safe */
  11968.         hexdump("krb4_des_write garbage_buf",garbage_buf,8);
  11969.     } else
  11970.         hexdump("krb4_des_write buf",buf,8);
  11971. #ifdef CK_ENCRYPTION
  11972. #ifdef KRB524
  11973.     (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
  11974.                              des_outpkt+4,
  11975.                              (len < 8) ? 8 : len,
  11976.                              k4_sched,
  11977.                              cred.session,
  11978.                              ENCRYPT);
  11979. #else /* KRB524 */
  11980.     (void) des_pcbc_encrypt((Block *)((len < 8) ? garbage_buf : buf),
  11981.                              (Block *)(des_outpkt+4),
  11982.                              (len < 8) ? 8 : len,
  11983.                              k4_sched,
  11984.                              &cred.session,
  11985.                              ENCRYPT);
  11986. #endif /* KRB524 */
  11987. #endif /* ENCRYPTION */
  11988.     if ( len < 8 )
  11989.         hexdump("krb4_des_write (post pcbc) garbage_buf",garbage_buf,8);
  11990.     else
  11991.         hexdump("krb4_des_write (post pcbc) buf",buf,8);
  11992.     hexdump("krb4_des_write (des_outpkt+4)",(des_outpkt+4),8);
  11993.  
  11994.     /* tell the other end the real amount, but send an 8-byte padded
  11995.     packet */
  11996.     len_buf[0] = (len & 0xff000000) >> 24;
  11997.     len_buf[1] = (len & 0xff0000) >> 16;
  11998.     len_buf[2] = (len & 0xff00) >> 8;
  11999.     len_buf[3] = (len & 0xff);
  12000.     hexdump("krb4_des_write des_outpkt len",des_outpkt,12);
  12001.     cc = net_write(fd, des_outpkt, roundup(len,8)+4);
  12002.     debug(F111,"net_write","chars written",cc);
  12003.     return(len);
  12004. }
  12005. #endif /* KRB4 */
  12006. #endif /* RLOGCODE */
  12007.  
  12008. #ifdef KRB524
  12009. #ifndef OS2
  12010. /* The following functions are missing from the compatibility library */
  12011. const char *
  12012. krb_get_err_text_entry(r) int r;
  12013. {
  12014.     extern char krb_err_text[];
  12015.     return(krb_err_txt[r]);
  12016. }
  12017. #endif /* OS2 */
  12018. #endif /* KRB524 */
  12019. #endif /* CK_KERBEROS */
  12020.  
  12021. #ifdef CK_KERBEROS
  12022. #ifdef KRB5_U2U
  12023. /* Kerberos 5 User to User Client */
  12024. int
  12025. k5_user_to_user_client_auth()
  12026. {
  12027.     extern int ttyfd;
  12028.     register int retval, i;
  12029.     char **srealms;             /* realm(s) of server */
  12030.     char *princ;                /* principal in credentials cache */
  12031.     krb5_ccache cc;
  12032.     krb5_creds creds, *new_creds;
  12033.     krb5_data reply, msg, msgtext, princ_data;
  12034.     krb5_ticket * ticket = NULL;
  12035.  
  12036.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12037.     {
  12038.         com_err("uu-client", retval, "getting credentials cache");
  12039.         return(-1);
  12040.     }
  12041.  
  12042.     memset ((char*)&creds, 0, sizeof(creds));
  12043.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12044.     {
  12045.         com_err("uu-client", retval, "getting principal name");
  12046.         return(-1);
  12047.     }
  12048.  
  12049.     if (retval = krb5_get_host_realm(k5_context, szHostName, &srealms))
  12050.     {
  12051.         com_err("uu-client", retval, "getting realms for \"%s\"", szHostName);
  12052.         return(-1);
  12053.     }
  12054.  
  12055.     if (retval = krb5_build_principal_ext(k5_context, &creds.server,
  12056.                                           krb5_princ_realm(k5_context,
  12057.                                                          creds.client)->length,
  12058.                                           krb5_princ_realm(k5_context,
  12059.                                                          creds.client)->data,
  12060.                                           6, "krbtgt",
  12061.                                           krb5_princ_realm(k5_context,
  12062.                                                          creds.client)->length,
  12063.                                           krb5_princ_realm(k5_context,
  12064.                                                          creds.client)->data,
  12065.                                           0))
  12066.     {
  12067.         com_err("uu-client", retval, "setting up tgt server name");
  12068.         return(-1);
  12069.     }
  12070.  
  12071.     /* Get TGT from credentials cache */
  12072.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_CACHED, cc,
  12073.                                        &creds, &new_creds))
  12074.     {
  12075.         com_err("uu-client", retval, "getting TGT");
  12076.         return(-1);
  12077.     }
  12078.  
  12079.     if (retval = krb5_unparse_name(k5_context, creds.client, &princ)) {
  12080.         com_err("uu-client", retval, "printing principal name");
  12081.         return(-1);
  12082.     }
  12083.     i = strlen(princ) + 1;
  12084.     princ_data.data = princ;
  12085.     princ_data.length = i;              /* include null terminator for
  12086.                                            server's convenience */
  12087.     retval = krb5_write_message(k5_context,
  12088.                                 (krb5_pointer) &ttyfd, &princ_data);
  12089.     if (retval)
  12090.     {
  12091.         com_err("uu-client", retval, "sending principal name to server");
  12092.         return(-1);
  12093.     }
  12094.     krb5_free_unparsed_name(k5_context,princ);
  12095.  
  12096.     retval = krb5_write_message(k5_context,
  12097.                                 (krb5_pointer) &ttyfd, &new_creds->ticket);
  12098.     if (retval)
  12099.     {
  12100.         com_err("uu-client", retval, "sending ticket to server");
  12101.         return(-1);
  12102.     }
  12103.  
  12104.     retval = krb5_read_message(k5_context, (krb5_pointer) &ttyfd, &reply);
  12105.     if (retval)
  12106.     {
  12107.         com_err("uu-client", retval, "reading reply from server");
  12108.         return(-1);
  12109.     }
  12110.  
  12111.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12112.         com_err("uu-client", retval, "initializing the auth_context");
  12113.         return(-1);
  12114.     }
  12115.  
  12116.     if (!krb5_d_no_addresses) {
  12117.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12118.                         KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12119.                         KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12120.         com_err("uu-client", retval, "generating addrs for auth_context");
  12121.         return(-1);
  12122.       }
  12123.     }
  12124.  
  12125.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12126.                                         KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12127.         com_err("uu-client", retval, "initializing the auth_context flags");
  12128.         return(-1);
  12129.     }
  12130.  
  12131.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12132.                                               &new_creds->keyblock)) {
  12133.         com_err("uu-client", retval, "setting useruserkey for authcontext");
  12134.         return(-1);
  12135.     }
  12136.  
  12137.     /* read the ap_req to get the session key */
  12138.     retval = krb5_rd_req(k5_context, &auth_context, &reply,
  12139.                           NULL, NULL, NULL, &ticket);
  12140.     if (retval) {
  12141.         com_err("uu-client", retval, "reading AP_REQ from server");
  12142.         return(-1);
  12143.     }
  12144.  
  12145.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12146.         return(-1);
  12147.  
  12148.     if ( strcmp("Kermit implements Kerberos 5 User to User",msg.data) )
  12149.         return(-1);
  12150.     krb5_free_data_contents(k5_context,&msg);
  12151.  
  12152.     msgtext.data = "As do I! :-)";
  12153.     msgtext.length = strlen(msgtext.data)+1;
  12154.  
  12155.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12156.         return(-1);
  12157.  
  12158.     if (retval = krb5_unparse_name(k5_context,
  12159. #ifdef HEIMDAL
  12160.                                     ticket->client,
  12161. #else /* HEIMDAL */
  12162.                                     ticket->enc_part2->client,
  12163. #endif /* HEIMDAL */
  12164.                                     &princ))
  12165.         com_err("uu-client", retval, "while unparsing client name");
  12166.     else {
  12167.         ckstrncpy(szUserNameAuthenticated,princ,UIDBUFLEN);
  12168.         validUser = AUTH_VALID;
  12169.         authentication_version = AUTHTYPE_KERBEROS_V5;
  12170.         if ( !quiet )
  12171.             printf("Peer name is \"%s\"\n", princ);
  12172.         krb5_free_unparsed_name(k5_context,princ);
  12173.     }
  12174.     return 0;
  12175. }
  12176.  
  12177. /* Kerberos 5 User to User Server */
  12178.  
  12179. int
  12180. k5_user_to_user_server_auth()
  12181. {
  12182.     krb5_data pname_data, tkt_data;
  12183.     int retval;
  12184.     krb5_creds creds, *new_creds;
  12185.     krb5_ccache cc;
  12186.     krb5_data msg, msgtext;
  12187.     extern int ttyfd;
  12188.  
  12189.     if (retval = krb5_read_message(k5_context,
  12190.                                    (krb5_pointer) &ttyfd, &pname_data)) {
  12191.         com_err ("uu-server", retval, "reading pname");
  12192.         return(-1);
  12193.     }
  12194.     /* client sends it already null-terminated. */
  12195.     if ( !quiet )
  12196.         printf ("Peer name is \"%s\".\n", pname_data.data);
  12197.     ckstrncpy(szUserNameAuthenticated,pname_data.data,UIDBUFLEN);
  12198.     validUser = AUTH_VALID;
  12199.     authentication_version = AUTHTYPE_KERBEROS_V5;
  12200.  
  12201.     if (retval = krb5_read_message(k5_context,
  12202.                                    (krb5_pointer) &ttyfd, &tkt_data)) {
  12203.         com_err ("uu-server", retval, "reading ticket data");
  12204.         return(-1);
  12205.     }
  12206.  
  12207.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12208.     {
  12209.         com_err("uu-server", retval, "getting credentials cache");
  12210.         return(-1);
  12211.     }
  12212.  
  12213.     memset ((char*)&creds, 0, sizeof(creds));
  12214.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12215.     {
  12216.         com_err("uu-server", retval, "getting principal name");
  12217.         return(-1);
  12218.     }
  12219.  
  12220.     if (retval = krb5_parse_name(k5_context, pname_data.data, &creds.server))
  12221.     {
  12222.         com_err("uu-server", retval, "parsing client name");
  12223.         return(-1);
  12224.     }
  12225.     creds.second_ticket = tkt_data;
  12226.  
  12227.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_USER_USER,
  12228.                                        cc, &creds, &new_creds))
  12229.     {
  12230.         com_err("uu-server", retval, "getting user-user ticket");
  12231.         return(-1);
  12232.     }
  12233.  
  12234.     /* send a ticket/authenticator to the other side, so it can get the key
  12235.        we're using for the krb_safe below. */
  12236.  
  12237.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12238.         com_err("uu-server", retval, "making auth_context");
  12239.         return(-1);
  12240.     }
  12241.  
  12242.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12243.                                          KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12244.         com_err("uu-server", retval, "initializing the auth_context flags");
  12245.         return(-1);
  12246.     }
  12247.  
  12248.     if (!krb5_d_no_addresses) {
  12249.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12250.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12251.                                 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12252.         com_err("uu-server", retval, "generating addrs for auth_context");
  12253.         return(-1);
  12254.       }
  12255.     }
  12256.  
  12257.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12258.                                               &new_creds->keyblock)) {
  12259.         com_err("uu-server", retval, "setting useruserkey for authcontext");
  12260.         return(-1);
  12261.     }
  12262.  
  12263.     if (retval = krb5_mk_req_extended(k5_context, &auth_context,
  12264.                                       AP_OPTS_USE_SESSION_KEY |
  12265.                                       AP_OPTS_MUTUAL_REQUIRED,
  12266.                                       NULL, new_creds, &msg)) {
  12267.         com_err("uu-server", retval, "making AP_REQ");
  12268.         return(-1);
  12269.     }
  12270.     retval = krb5_write_message(k5_context, (krb5_pointer) &ttyfd, &msg);
  12271.     if (retval) {
  12272.         com_err("uu-server", retval, "writing message to client");
  12273.         return(-1);
  12274.     }
  12275.     krb5_free_data_contents(k5_context,&msg);
  12276.  
  12277.     msgtext.data = "Kermit implements Kerberos 5 User to User";
  12278.     msgtext.length = strlen(msgtext.data)+1;
  12279.  
  12280.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12281.         return(-1);
  12282.  
  12283.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12284.         return(-1);
  12285.  
  12286.     if ( strcmp("As do I! :-)",msg.data) )
  12287.         return(-1);
  12288.     krb5_free_data_contents(k5_context,&msg);
  12289.  
  12290.  
  12291.     return(0);
  12292. }
  12293.  
  12294. int
  12295. k5_u2u_read_msg(krb5_context context, int fd, krb5_data * msg)
  12296. {
  12297.     int retval;
  12298.     krb5_data reply;
  12299.  
  12300.     retval = krb5_read_message(context, (krb5_pointer) &fd, &reply);
  12301.     if (retval)
  12302.     {
  12303.         com_err("uu-client", retval, "reading reply");
  12304.         return(-1);
  12305.     }
  12306.  
  12307.     if (retval = krb5_rd_priv(context, auth_context, &reply, msg, NULL)) {
  12308.         com_err("uu-client", retval, "decoding reply");
  12309.         return(-1);
  12310.     }
  12311.     return(0);
  12312. }
  12313.  
  12314. int
  12315. k5_u2u_write_msg(krb5_context context, int fd, krb5_data * msgtext)
  12316. {
  12317.     int retval;
  12318.     krb5_data msg;
  12319.  
  12320.     if (retval = krb5_mk_priv(k5_context, auth_context, msgtext, &msg, NULL))
  12321.     {
  12322.         com_err("uu-server", retval, "encoding message");
  12323.         return(-1);
  12324.     }
  12325.  
  12326.     retval = krb5_write_message(k5_context, (krb5_pointer) &fd, &msg);
  12327.     krb5_free_data_contents(k5_context,&msg);
  12328.     if (retval)
  12329.     {
  12330.         com_err("uu-server", retval, "writing message");
  12331.         return(-1);
  12332.     }
  12333.     return(0);
  12334. }
  12335.  
  12336. int
  12337. krb5_u2u_avail(fd)
  12338.     int fd;
  12339. {
  12340.     return(nstored);
  12341. }
  12342.  
  12343. int
  12344. krb5_u2u_read(fd, buf, len)
  12345.      int fd;
  12346.      register char *buf;
  12347.      int len;
  12348. {
  12349.     int nreturned = 0;
  12350.     krb5_data msg;
  12351.  
  12352.     debug(F111,"krb5_u2u_read","len",len);
  12353.  
  12354.     if ( !buf )
  12355.         return(0);
  12356.  
  12357.     if (nstored >= len) {
  12358.         memcpy(buf, store_ptr, len);        /* safe */
  12359.         store_ptr += len;
  12360.         nstored -= len;
  12361.         return(len);
  12362.     } else if (nstored) {
  12363.         memcpy(buf, store_ptr, nstored);    /* safe */
  12364.         nreturned += nstored;
  12365.         buf += nstored;
  12366.         len -= nstored;
  12367.         nstored = 0;
  12368.     }
  12369.  
  12370.     if (k5_u2u_read_msg(k5_context, fd, &msg) < 0)
  12371.         return(-1);
  12372.  
  12373.     if ( msg.length <= len ) {
  12374.         memcpy(buf, msg.data, msg.length);
  12375.         nreturned += msg.length;
  12376.         nstored = 0;
  12377.     } else {
  12378.         memcpy(buf, msg.data, len);
  12379.         nreturned += len;
  12380.  
  12381.         if ( msg.length - len < sizeof(storage) ) {
  12382.             store_ptr = storage;
  12383.             nstored = msg.length - len;
  12384.             memcpy(storage,msg.data+len,nstored);
  12385.         } else {
  12386.             nstored = 0;
  12387.             return(-1);
  12388.         }
  12389.     }
  12390.     return(nreturned);
  12391. }
  12392.  
  12393. int
  12394. krb5_u2u_write(fd, buf, len)
  12395.      int fd;
  12396.      char *buf;
  12397.      int len;
  12398. {
  12399.     krb5_data msg;
  12400.  
  12401.     msg.length = len;
  12402.     msg.data = buf;
  12403.  
  12404.     if ( k5_u2u_write_msg(k5_context, fd, &msg) < 0 )
  12405.         return(-1);
  12406.     else
  12407.         return(len);
  12408. }
  12409.  
  12410. #endif /* KRB5_U2U */
  12411. #endif /* CK_KERBEROS */
  12412.  
  12413. #ifdef CK_FORWARD_X
  12414. /*
  12415.  
  12416. Copyright (c) 1988  X Consortium
  12417.  
  12418. Permission is hereby granted, free of charge, to any person obtaining a copy
  12419. of this software and associated documentation files (the "Software"), to deal
  12420. in the Software without restriction, including without limitation the rights
  12421. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12422. copies of the Software, and to permit persons to whom the Software is
  12423. furnished to do so, subject to the following conditions:
  12424.  
  12425. The above copyright notice and this permission notice shall be included in
  12426. all copies or substantial portions of the Software.
  12427.  
  12428. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12429. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12430. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  12431. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  12432. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  12433. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12434.  
  12435. Except as contained in this notice, the name of the X Consortium shall not be
  12436. used in advertising or otherwise to promote the sale, use or other dealings
  12437. in this Software without prior written authorization from the X Consortium.
  12438.  
  12439. */
  12440. /*  Modified for stand-alone compiling by
  12441.  *  Peter 'Luna' Runestig <peter@runestig.com>
  12442.  */
  12443.  
  12444. #include <stdlib.h>
  12445. #include <string.h>
  12446. #include <fcntl.h>
  12447. #include <sys/stat.h>
  12448. #include <errno.h>
  12449. #include <time.h>
  12450. #define Time_t time_t
  12451.  
  12452. void
  12453. XauDisposeAuth (auth)
  12454. Xauth   *auth;
  12455. {
  12456.     if (auth) {
  12457.         if (auth->address) (void) free (auth->address);
  12458.         if (auth->number) (void) free (auth->number);
  12459.         if (auth->name) (void) free (auth->name);
  12460.         if (auth->data) {
  12461.             (void) bzero (auth->data, auth->data_length);
  12462.             (void) free (auth->data);
  12463.         }
  12464.         free ((char *) auth);
  12465.     }
  12466.     return;
  12467. }
  12468.  
  12469. char *
  12470. XauFileName ()
  12471. {
  12472.     char *slashDotXauthority = "/.Xauthority";
  12473.     char    *name;
  12474.     static char *buf=NULL;
  12475.     static int  bsize=0;
  12476.     int     size, namelen;
  12477.     extern char * tn_fwdx_xauthority;
  12478.  
  12479.     if ( tn_fwdx_xauthority )
  12480.         return(tn_fwdx_xauthority);
  12481.  
  12482.     if (name = getenv ("XAUTHORITY"))
  12483.         return(name);
  12484.     name = zhome();
  12485.     if ( !name )
  12486.         return(NULL);
  12487.     namelen = strlen (name);
  12488.     size = namelen + strlen(slashDotXauthority) + 1;
  12489.     if (size > bsize) {
  12490.         if (buf)
  12491.             free (buf);
  12492.         buf = malloc ((unsigned) size);
  12493.         if (!buf)
  12494.             return 0;
  12495.         bsize = size;
  12496.     }
  12497.     ckstrncpy (buf, name, bsize);
  12498.     if ( name[namelen-1] != '/'
  12499. #ifdef OS2
  12500.          && name[namelen-1] != '\\'
  12501. #endif /* OS2 */
  12502.          )
  12503.         ckstrncat (buf, slashDotXauthority, bsize);
  12504.     else
  12505.         ckstrncat (buf, &slashDotXauthority[1], bsize);
  12506.     return(buf);
  12507. }
  12508.  
  12509. static int
  12510. binaryEqual (a, b, len)
  12511. register char   *a, *b;
  12512. register int    len;
  12513. {
  12514.     while (len--)
  12515.         if (*a++ != *b++)
  12516.             return 0;
  12517.     return 1;
  12518. }
  12519.  
  12520. #ifndef R_OK
  12521. #define R_OK 04
  12522. #endif /* R_OK */
  12523.  
  12524. Xauth *
  12525. XauGetAuthByAddr (family, address_length, address,
  12526.                           number_length, number,
  12527.                           name_length, name)
  12528. unsigned int    family;
  12529. unsigned int    address_length;
  12530. const char      *address;
  12531. unsigned int    number_length;
  12532. const char      *number;
  12533. unsigned int    name_length;
  12534. const char      *name;
  12535. {
  12536.     FILE    *auth_file;
  12537.     char    *auth_name;
  12538.     Xauth   *entry;
  12539.  
  12540.     auth_name = XauFileName();
  12541.     if (!auth_name)
  12542.         return 0;
  12543.     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
  12544.         return 0;
  12545.     auth_file = fopen (auth_name, "rb");
  12546.     if (!auth_file)
  12547.         return 0;
  12548.     for (;;) {
  12549.         entry = XauReadAuth (auth_file);
  12550.         if (!entry)
  12551.             break;
  12552.         /*
  12553.          * Match when:
  12554.          *   either family or entry->family are FamilyWild or
  12555.          *    family and entry->family are the same
  12556.          *  and
  12557.          *   either address or entry->address are empty or
  12558.          *    address and entry->address are the same
  12559.          *  and
  12560.          *   either number or entry->number are empty or
  12561.          *    number and entry->number are the same
  12562.          *  and
  12563.          *   either name or entry->name are empty or
  12564.          *    name and entry->name are the same
  12565.          */
  12566.  
  12567. /*      if ((family == FamilyWild || entry->family == FamilyWild ||
  12568.              (entry->family == family &&
  12569.               address_length == entry->address_length &&
  12570.               binaryEqual (entry->address, address, (int)address_length))) &&
  12571.             (number_length == 0 || entry->number_length == 0 ||
  12572.              (number_length == entry->number_length &&
  12573.               binaryEqual (entry->number, number, (int)number_length))) &&
  12574.             (name_length == 0 || entry->name_length == 0 ||
  12575.              (entry->name_length == name_length &&
  12576.               binaryEqual (entry->name, name, (int)name_length)))) */
  12577.         /* the original matching code above doesn't seem to meet the matching
  12578.          * algorithm, it doesn't check if "address_length == 0 ||
  12579.          * entry->address_length == 0". / Luna 2000-02-09
  12580.          */
  12581.         if ((family == FamilyWild || entry->family == FamilyWild ||
  12582.               entry->family == family) &&
  12583.             (address_length == 0 || entry->address_length == 0 ||
  12584.               (address_length == entry->address_length &&
  12585.               binaryEqual (entry->address, address, (int)address_length))) &&
  12586.             (number_length == 0 || entry->number_length == 0 ||
  12587.              (number_length == entry->number_length &&
  12588.               binaryEqual (entry->number, number, (int)number_length))) &&
  12589.             (name_length == 0 || entry->name_length == 0 ||
  12590.              (entry->name_length == name_length &&
  12591.               binaryEqual (entry->name, name, (int)name_length))))
  12592.             break;
  12593.         XauDisposeAuth (entry);
  12594.     }
  12595.     (void) fclose (auth_file);
  12596.     return entry;
  12597. }
  12598.  
  12599. static int
  12600. read_short (shortp, file)
  12601. unsigned short  *shortp;
  12602. FILE            *file;
  12603. {
  12604.     unsigned char   file_short[2];
  12605.  
  12606.     if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12607.         return 0;
  12608.     *shortp = file_short[0] * 256 + file_short[1];
  12609.     return 1;
  12610. }
  12611.  
  12612. static int
  12613. read_counted_string (countp, stringp, file)
  12614. unsigned short  *countp;
  12615. char    **stringp;
  12616. FILE    *file;
  12617. {
  12618.     unsigned short  len;
  12619.     char            *data;
  12620.  
  12621.     if (read_short (&len, file) == 0)
  12622.         return 0;
  12623.     if (len == 0) {
  12624.         data = 0;
  12625.     } else {
  12626.         data = malloc ((unsigned) len);
  12627.         if (!data)
  12628.             return 0;
  12629.         if (fread (data, (int) sizeof (char), (int) len, file) != len) {
  12630.             bzero (data, len);
  12631.             free (data);
  12632.             return 0;
  12633.         }
  12634.     }
  12635.     *stringp = data;
  12636.     *countp = len;
  12637.     return 1;
  12638. }
  12639.  
  12640. Xauth *
  12641. XauReadAuth (auth_file)
  12642. FILE    *auth_file;
  12643. {
  12644.     Xauth   local;
  12645.     Xauth   *ret;
  12646.  
  12647.     if (read_short (&local.family, auth_file) == 0)
  12648.         return 0;
  12649.     if (read_counted_string (&local.address_length,
  12650.                              &local.address, auth_file) == 0)
  12651.         return 0;
  12652.     if (read_counted_string (&local.number_length,
  12653.                              &local.number, auth_file) == 0) {
  12654.         if (local.address) free (local.address);
  12655.         return 0;
  12656.     }
  12657.     if (read_counted_string (&local.name_length,
  12658.                              &local.name, auth_file) == 0) {
  12659.         if (local.address) free (local.address);
  12660.         if (local.number) free (local.number);
  12661.         return 0;
  12662.     }
  12663.     if (read_counted_string (&local.data_length,
  12664.                              &local.data, auth_file) == 0) {
  12665.         if (local.address) free (local.address);
  12666.         if (local.number) free (local.number);
  12667.         if (local.name) free (local.name);
  12668.         return 0;
  12669.     }
  12670.     ret = (Xauth *) malloc (sizeof (Xauth));
  12671.     if (!ret) {
  12672.         if (local.address) free (local.address);
  12673.         if (local.number) free (local.number);
  12674.         if (local.name) free (local.name);
  12675.         if (local.data) {
  12676.             bzero (local.data, local.data_length);
  12677.             free (local.data);
  12678.         }
  12679.         return 0;
  12680.     }
  12681.     *ret = local;
  12682.     return ret;
  12683. }
  12684.  
  12685. static int
  12686. write_short (s, file)
  12687. unsigned short  s;
  12688. FILE            *file;
  12689. {
  12690.     unsigned char   file_short[2];
  12691.  
  12692.     file_short[0] = (s & (unsigned)0xff00) >> 8;
  12693.     file_short[1] = s & 0xff;
  12694.     if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12695.         return 0;
  12696.     return 1;
  12697. }
  12698.  
  12699. static int
  12700. write_counted_string (count, string, file)
  12701. unsigned short  count;
  12702. char    *string;
  12703. FILE    *file;
  12704. {
  12705.     if (write_short (count, file) == 0)
  12706.         return 0;
  12707.     if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
  12708.         return 0;
  12709.     return 1;
  12710. }
  12711.  
  12712. int
  12713. XauWriteAuth (auth_file, auth)
  12714. FILE    *auth_file;
  12715. Xauth   *auth;
  12716. {
  12717.     if (write_short (auth->family, auth_file) == 0)
  12718.         return 0;
  12719.     if (write_counted_string (auth->address_length,
  12720.                               auth->address, auth_file) == 0)
  12721.         return 0;
  12722.     if (write_counted_string (auth->number_length,
  12723.                               auth->number, auth_file) == 0)
  12724.         return 0;
  12725.     if (write_counted_string (auth->name_length, auth->name, auth_file) == 0)
  12726.         return 0;
  12727.     if (write_counted_string (auth->data_length, auth->data, auth_file) == 0)
  12728.         return 0;
  12729.     return 1;
  12730. }
  12731.  
  12732. #ifdef KRB5
  12733. #ifdef K5_XAUTH
  12734. /*
  12735.  * functions to encode/decode Kerberos V5 principals
  12736.  * into something that can be reasonable spewed over
  12737.  * the wire
  12738.  *
  12739.  * Author: Tom Yu <tlyu@MIT.EDU>
  12740.  *
  12741.  * Still needs to be fixed up wrt signed/unsigned lengths, but we'll worry
  12742.  * about that later.
  12743.  */
  12744.  
  12745. /*
  12746.  * XauKrb5Encode
  12747.  *
  12748.  * this function encodes the principal passed to it in a format that can
  12749.  * easily be dealt with by stuffing it into an X packet.  Encoding is as
  12750.  * follows:
  12751.  *   length count of the realm name
  12752.  *   realm
  12753.  *   component count
  12754.  *   length of component
  12755.  *   actual principal component
  12756.  *   etc....
  12757.  *
  12758.  * Note that this function allocates a hunk of memory, which must be
  12759.  * freed to avoid nasty memory leak type things.  All counts are
  12760.  * byte-swapped if needed. (except for the total length returned)
  12761.  *
  12762.  * nevermind.... stuffing the encoded packet in net byte order just to
  12763.  * always do the right thing.  Don't have to frob with alignment that way.
  12764.  */
  12765. int
  12766. XauKrb5Encode(princ, outbuf)
  12767.     krb5_principal princ;       /* principal to encode */
  12768.     krb5_data *outbuf;          /* output buffer */
  12769. {
  12770.     CARD16 i, numparts, totlen = 0, plen, rlen;
  12771.     char *cp, *pdata;
  12772.  
  12773.     rlen = krb5_princ_realm(princ)->length;
  12774.     numparts = krb5_princ_size(princ);
  12775.     totlen = 2 + rlen + 2;      /* include room for realm length
  12776.                                    and component count */
  12777.     for (i = 0; i < numparts; i++)
  12778.         totlen += krb5_princ_component(princ, i)->length + 2;
  12779.     /* add 2 bytes each time for length */
  12780.     if ((outbuf->data = (char *)malloc(totlen)) == NULL)
  12781.         return -1;
  12782.     cp = outbuf->data;
  12783.     *cp++ = (char)((int)(0xff00 & rlen) >> 8);
  12784.     *cp++ = (char)(0x00ff & rlen);
  12785.     memcpy(cp, krb5_princ_realm(princ)->data, rlen);    /* safe */
  12786.     cp += rlen;
  12787.     *cp++ = (char)((int)(0xff00 & numparts) >> 8);
  12788.     *cp++ = (char)(0x00ff & numparts);
  12789.     for (i = 0; i < numparts; i++)
  12790.     {
  12791.         plen = krb5_princ_component(princ, i)->length;
  12792.         pdata = krb5_princ_component(princ, i)->data;
  12793.         *cp++ = (char)((int)(0xff00 & plen) >> 8);
  12794.         *cp++ = (char)(0x00ff & plen);
  12795.         memcpy(cp, pdata, plen);                        /* safe */
  12796.         cp += plen;
  12797.     }
  12798.     outbuf->length = totlen;
  12799.     return 0;
  12800. }
  12801.  
  12802. /*
  12803.  * XauKrb5Decode
  12804.  *
  12805.  * This function essentially reverses what XauKrb5Encode does.
  12806.  * return value: 0 if okay, -1 if malloc fails, -2 if inbuf format bad
  12807.  */
  12808. int
  12809. XauKrb5Decode(inbuf, princ)
  12810.     krb5_data inbuf;
  12811.     krb5_principal *princ;
  12812. {
  12813.     CARD16 i, numparts, plen, rlen;
  12814.     CARD8 *cp, *pdata;
  12815.  
  12816.     if (inbuf.length < 4)
  12817.     {
  12818.         return -2;
  12819.     }
  12820.     *princ = (krb5_principal)malloc(sizeof (krb5_principal_data));
  12821.     if (*princ == NULL)
  12822.         return -1;
  12823.     bzero(*princ, sizeof (krb5_principal_data));
  12824.     cp = (CARD8 *)inbuf.data;
  12825.     rlen = *cp++ << 8;
  12826.     rlen |= *cp++;
  12827.     if (inbuf.length < 4 + (int)rlen + 2)
  12828.     {
  12829.         krb5_free_principal(*princ);
  12830.         return -2;
  12831.     }
  12832.     krb5_princ_realm(*princ)->data = (char *)malloc(rlen);
  12833.     if (krb5_princ_realm(*princ)->data == NULL)
  12834.     {
  12835.         krb5_free_principal(*princ);
  12836.         return -1;
  12837.     }
  12838.     krb5_princ_realm(*princ)->length = rlen;
  12839.     memcpy(krb5_princ_realm(*princ)->data, cp, rlen);   /* safe */
  12840.     cp += rlen;
  12841.     numparts = *cp++ << 8;
  12842.     numparts |= *cp++;
  12843.     krb5_princ_name(*princ) =
  12844.         (krb5_data *)malloc(numparts * sizeof (krb5_data));
  12845.     if (krb5_princ_name(*princ) == NULL)
  12846.     {
  12847.         krb5_free_principal(*princ);
  12848.         return -1;
  12849.     }
  12850.     krb5_princ_size(*princ) = 0;
  12851.     for (i = 0; i < numparts; i++)
  12852.     {
  12853.         if (cp + 2 > (CARD8 *)inbuf.data + inbuf.length)
  12854.         {
  12855.             krb5_free_principal(*princ);
  12856.             return -2;
  12857.         }
  12858.         plen = *cp++ << 8;
  12859.         plen |= *cp++;
  12860.         if (cp + plen > (CARD8 *)inbuf.data + inbuf.length)
  12861.         {
  12862.             krb5_free_principal(*princ);
  12863.             return -2;
  12864.         }
  12865.         pdata = (CARD8 *)malloc(plen);
  12866.         if (pdata == NULL)
  12867.         {
  12868.             krb5_free_principal(*princ);
  12869.             return -1;
  12870.         }
  12871.         krb5_princ_component(*princ, i)->data = (char *)pdata;
  12872.         krb5_princ_component(*princ, i)->length = plen;
  12873.         memcpy(pdata, cp, plen);        /* safe */
  12874.         cp += plen;
  12875.         krb5_princ_size(*princ)++;
  12876.     }
  12877.     return 0;
  12878. }
  12879. #endif /* K5_XAUTH */
  12880. #endif /* KRB5 */
  12881. #endif /* CK_FORWARD_X */
  12882. #endif /* CK_AUTHENTICATION */
  12883.  
  12884. #ifdef CK_SECURITY
  12885. /* C K _ A U T H _ I N I T
  12886.  * Initialize the Kerberos system for a pending connection
  12887.  *   hostname - a reverse DNS lookup of the hostname when possible
  12888.  *   ipaddr   - the ip address of the host
  12889.  *   username - the name the user wants to connect under not necessarily
  12890.  *              the same as principal
  12891.  *   socket   - the socket handle (ttyfd in Kermit speak)
  12892.  *
  12893.  * Returns: 1 on success and 0 on failure
  12894.  */
  12895.  
  12896. int
  12897. #ifdef CK_ANSIC
  12898. ck_auth_init( char * hostname, char * ipaddr, char * username, int socket )
  12899. #else /* CK_ANSIC */
  12900. ck_auth_init( hostname, ipaddr, username, socket )
  12901.     char * hostname; char * ipaddr; char *username; int socket;
  12902. #endif /* CK_ANSIC */
  12903. {
  12904. #ifdef CK_AUTHENTICATION
  12905. #ifdef OS2
  12906.     if ( !ck_security_loaddll() ) {
  12907.         TELOPT_ME_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  12908.         TELOPT_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  12909.         return(0);
  12910.     }
  12911. #endif /* OS2 */
  12912. #endif /* CK_AUTHENTICAITON */
  12913. #ifdef CK_ENCRYPTION
  12914.     if ( !!ck_crypt_is_installed() ) {
  12915.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  12916.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  12917.     }
  12918. #endif /* CK_ENCRYPTION */
  12919.  
  12920.     if (!hostname) hostname = "";
  12921.     if (!ipaddr) ipaddr = "";
  12922.     if (!username) username = "";
  12923.  
  12924.     debug(F110,"ck_auth_init Username",username,0);
  12925.     debug(F110,"ck_auth_init Hostname",hostname,0);
  12926.     debug(F110,"ck_auth_init Ipaddr",ipaddr,0);
  12927.  
  12928.     ckstrncpy( szUserName, username, UIDBUFLEN );
  12929.     ckstrncpy( szHostName, hostname, UIDBUFLEN );
  12930.     ckstrncpy( szIP, ipaddr, 16 );
  12931.     szUserNameRequested[0] = '\0';
  12932.     szUserNameAuthenticated[0] = '\0';
  12933.     validUser = AUTH_REJECT;
  12934.     accept_complete = 0;
  12935.     authentication_version = AUTHTYPE_NULL;
  12936.  
  12937. #ifdef CK_AUTHENTICATION
  12938.     auth_how = 0;
  12939.     auth_crypt = 0;
  12940.     auth_fwd = 0;
  12941.     mutual_complete = 0;
  12942.     if ( sstelnet )
  12943.         str_data[3] = TELQUAL_REPLY;
  12944.     else
  12945.         str_data[3] = TELQUAL_IS;
  12946. #endif /* CK_AUTHENTICATION */
  12947.  
  12948. #ifdef CK_SRP
  12949.     srp_waitresp = 0;
  12950. #endif /* SRP */
  12951.  
  12952. #ifdef CK_KERBEROS
  12953. #ifdef KRB5
  12954.     /* free previous ret_cred  */
  12955.     if ( ret_cred ) {
  12956. #ifdef CK_ENCRYPTION
  12957. #ifdef HEIMDAL
  12958.         if ( k5_session_key == &ret_cred->session)
  12959.             k5_session_key = NULL;
  12960. #else /* HEIMDAL */
  12961.         if ( k5_session_key == &ret_cred->keyblock)
  12962.             k5_session_key = NULL;
  12963. #endif /* HEIMDAL */
  12964. #endif /* CK_ENCRYPTION */
  12965.         krb5_free_creds(k5_context, ret_cred);
  12966.         ret_cred = NULL;
  12967.     }
  12968.     if (k5_ticket) {
  12969.         krb5_free_ticket(k5_context, k5_ticket);
  12970.         k5_ticket = NULL;
  12971.     }
  12972.     /* and context */
  12973.     if ( k5_context ) {
  12974.         krb5_free_context(k5_context);
  12975.         k5_context = NULL;
  12976.     }
  12977.  
  12978.     /* create k5_context */
  12979.     krb5_init_context(&k5_context);
  12980. #ifndef MIT_CURRENT
  12981.     if (k5_context)
  12982.         krb5_init_ets(k5_context);
  12983. #endif /* MIT_CURRENT */
  12984.     memset(&k5_auth,0,sizeof(k5_auth));
  12985.     if (auth_context) {
  12986.         krb5_auth_con_free(k5_context, auth_context);
  12987.         auth_context = 0;
  12988.     }
  12989. #ifdef CK_ENCRYPTION
  12990.     if (k5_session_key) {
  12991.         krb5_free_keyblock(k5_context, k5_session_key);
  12992.         k5_session_key = 0;
  12993.     }
  12994. #endif /* ENCRYPTION */
  12995. #ifdef TLS_VERIFY
  12996.     krb5_tls_verified = 0;
  12997. #endif /* TLS_VERIFY */
  12998. #endif /* KRB5 */
  12999.  
  13000. #ifdef KRB4
  13001. #ifdef CK_ENCRYPTION
  13002.     /* Initialize buffers used for authentication */
  13003.     memset(&k4_session_key, 0, sizeof(k4_session_key));
  13004.     memset(&k4_challenge, 0, sizeof(k4_challenge));
  13005. #endif /* ENCRYPTION */
  13006. #endif /* KRB4 */
  13007.  
  13008.     kstream_destroy();
  13009. #ifdef RLOGCODE
  13010.     rlog_encrypt = 0;
  13011. #endif /* RLOGCODE */
  13012.     nstored = 0;
  13013.     store_ptr = storage;
  13014.     memset(storage,0,sizeof(storage));
  13015.  
  13016.     if (!kstream_create_from_fd(socket, NULL))
  13017.         return(0);
  13018. #endif /* CK_KERBEROS */
  13019.  
  13020.     return(1);
  13021. }
  13022.  
  13023. void
  13024. auth_finished(result) int result; {
  13025.     extern char uidbuf[];
  13026.     extern int sstelnet;
  13027.  
  13028.     validUser = result;
  13029.     switch (result) {
  13030.     case AUTH_REJECT:           /* Rejected */
  13031.         if (sstelnet)
  13032.             uidbuf[0] = '\0';
  13033.         authentication_version = AUTHTYPE_NULL;
  13034.         break;
  13035.     case AUTH_UNKNOWN:          /* We don't know who he is, but he's okay */
  13036.         if (sstelnet)
  13037.             strcpy(uidbuf,"(unknown)");
  13038.         break;
  13039.     case AUTH_OTHER:            /* We know him, but not his name */
  13040.         if (sstelnet)
  13041.             strcpy(uidbuf,"(other)");
  13042.         break;
  13043.     case AUTH_USER:             /* We know he name */
  13044.     case AUTH_VALID:            /* We know him, and he needs no password */
  13045.         if (sstelnet)
  13046.             strcpy(uidbuf,szUserNameRequested);
  13047.         break;
  13048.     }
  13049. }
  13050. #endif /* CK_SECURITY */
  13051.