home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv208.zip / ckuath.c < prev    next >
C/C++ Source or Header  |  2003-03-16  |  413KB  |  13,069 lines

  1. char *ckathv = "Authentication, 8.0.218, 9 Mar 2003";
  2. /*
  3.   C K U A T H . C  --  Authentication for C-Kermit
  4.  
  5.   Copyright (C) 1999, 2003,
  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. #ifdef CK_SECURITY
  43.  
  44. #define CKUATH_C
  45. #include "ckcker.h"
  46. #include "ckuusr.h"
  47. #include "ckucmd.h"                             /* For struct keytab */
  48. #include "ckcnet.h"
  49. #include "ckctel.h"
  50.  
  51. char szUserNameRequested[UIDBUFLEN+1];    /* for incoming connections */
  52. char szUserNameAuthenticated[UIDBUFLEN+1];/* for incoming connections */
  53. char szHostName[UIDBUFLEN+1];
  54. char szUserName[UIDBUFLEN+1];
  55. static char szIP[16];
  56. static int  validUser = AUTH_REJECT;    /* User starts out invalid */
  57. int authentication_version = AUTHTYPE_NULL;
  58. int accept_complete = 0;
  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 VMS
  119. #ifndef FREEBSD4
  120. #ifndef OpenBSD
  121. #include <malloc.h>
  122. #endif /* OpenBSD */
  123. #endif /* FREEBSD4 */
  124. #endif /* VMS */
  125. #ifdef OS2
  126. #include <io.h>
  127. #endif /* OS2 */
  128.  
  129. #ifdef KRB5
  130. #ifdef HEIMDAL
  131. #ifdef printf
  132. #define saveprintf printf
  133. #undef printf
  134. #endif /* printf */
  135. #include "krb5.h"
  136. #include "com_err.h"
  137. #ifdef saveprintf
  138. #define printf saveprintf
  139. #endif /* saveprintf */
  140. #else /* HEIMDAL */
  141. #include "krb5.h"
  142. #include "profile.h"
  143. #include "com_err.h"
  144. #ifdef KRB5_GET_INIT_CREDS_OPT_TKT_LIFE
  145. #define KRB5_HAVE_GET_INIT_CREDS
  146. #else
  147. #define krb5_free_unparsed_name(con,val) krb5_xfree((char *)(val))
  148. #endif
  149. #ifndef KRB5_HAVE_GET_INIT_CREDS
  150. #define krb5_free_data_contents(c,v) krb5_xfree((char *)(v)->data)
  151. #endif
  152. #endif /* HEIMDAL */
  153. #ifdef HAVE_PWD_H
  154. #include <pwd.h>
  155. #endif
  156. #endif /* KRB5 */
  157.  
  158. #ifdef KRB4
  159. #define  des_cblock Block
  160. #define  const_des_cblock const Block
  161. #define  des_key_schedule Schedule
  162. #ifdef KRB524
  163. #ifdef NT
  164. #define _WINDOWS
  165. #endif /* NT */
  166. #include "kerberosIV/krb.h"
  167. #ifndef OS2
  168. _PROTOTYP(const char * krb_get_err_text_entry, (int));
  169. #endif /* OS2 */
  170. #else /* KRB524 */
  171. #ifdef SOLARIS
  172. #ifndef sun
  173. /* for some reason the Makefile entries for the Solaris systems have -Usun */
  174. #define sun
  175. #endif /* sun */
  176. #endif /* SOLARIS */
  177. #include "krb.h"
  178. #define krb_get_err_text_entry krb_get_err_text
  179. #endif /* KRB524 */
  180. #else /* KRB4 */
  181. #ifdef CK_SSL
  182. #define  des_cblock Block
  183. #ifdef COMMENT
  184. #define  const_des_cblock const Block
  185. #endif /* COMMENT */
  186. #define  des_key_schedule Schedule
  187. #endif /* CK_SSL */
  188. #endif /* KRB4 */
  189.  
  190. #include "ckuath.h"
  191. #ifdef CK_KERBEROS
  192. #ifndef KRB5
  193. #define NOBLOCKDEF
  194. #else /* KRB5 */
  195. #ifdef KRB524
  196. #define NOBLOCKDEF
  197. #endif /* KRB524 */
  198. #endif /* KRB5 */
  199. #endif /* CK_KERBEROS */
  200. #include "ckuat2.h"
  201.  
  202. #ifdef CK_SSL
  203. #ifdef LIBDES
  204. #ifdef OPENSSL_097
  205. #define OPENSSL_ENABLE_OLD_DES_SUPPORT
  206. #include <openssl/des.h>
  207. #endif /* OPENSSL_097 */
  208. #ifndef HEADER_DES_H
  209. #define HEADER_DES_H
  210. #endif /* HEADER_DES_H */
  211. #endif /* LIBDES */
  212. #include "ck_ssl.h"
  213. extern int ssl_finished_messages;
  214. #endif /* SSL */
  215.  
  216. #define PWD_SZ 128
  217.  
  218. #ifndef LIBDES
  219. #ifdef UNIX
  220. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  221. #endif /* UNIX */
  222. #else /* LIBDES */
  223. #define des_fixup_key_parity des_set_odd_parity
  224. #endif /* LIBDES */
  225.  
  226. #ifdef OS2
  227. #define MAP_DES
  228. #ifdef KRB4
  229. #define MAP_KRB4
  230. #endif /* KRB4 */
  231. #ifdef SRPDLL
  232. #define MAP_SRP
  233. #endif /* SRPDLL */
  234. #ifdef KRB5
  235. #define MAP_KRB5
  236. #endif /* KRB5 */
  237. #ifdef CRYPT_DLL
  238. #define MAP_CRYPT
  239. #endif /* CRYPT_DLL */
  240. #define MAP_NTLM
  241. #include "ckoath.h"
  242. #include "ckosyn.h"
  243. #endif /* OS2 */
  244.  
  245. /*
  246.  * Globals
  247.  */
  248. int auth_type_user[AUTHTYPLSTSZ] = {AUTHTYPE_AUTO, AUTHTYPE_NULL};
  249. int auth_how=0;
  250. int auth_crypt=0;
  251. int auth_fwd=0;
  252.  
  253. /* These are state completion variables */
  254. static int mutual_complete = 0;
  255.  
  256. #ifdef KRB4
  257. #ifdef OS2
  258. static LEASH_CREDENTIALS cred;
  259. #else /* OS2 */
  260. static CREDENTIALS cred;
  261. #endif /* OS2 */
  262. static KTEXT_ST k4_auth;
  263. static char     k4_name[ANAME_SZ];
  264. static AUTH_DAT k4_adat  = { 0 };
  265. static MSG_DAT  k4_msg_data;
  266. #ifdef CK_ENCRYPTION
  267. static Block    k4_session_key     = { 0 };
  268. static Schedule k4_sched;
  269. static Block    k4_challenge       = { 0 };
  270. #ifdef MIT_CURRENT
  271. static krb5_keyblock k4_krbkey;
  272. #endif /* MIT_CURRENT */
  273. #endif /* ENCRYPTION */
  274. #define KRB4_SERVICE_NAME    "rcmd"
  275.  
  276. _PROTOTYP(static int k4_auth_send,(VOID));
  277. _PROTOTYP(static int k4_auth_reply,(unsigned char *, int));
  278. _PROTOTYP(static int k4_auth_is,(unsigned char *, int));
  279. #endif /* KRB4 */
  280.  
  281. #ifdef KRB5
  282. static krb5_data          k5_auth;
  283. static krb5_auth_context  auth_context;
  284. static krb5_keyblock     *k5_session_key = NULL;
  285. static krb5_ticket       *k5_ticket = NULL;
  286. #ifndef KRB5_SERVICE_NAME
  287. #define KRB5_SERVICE_NAME    "host"
  288. #endif
  289.  
  290. _PROTOTYP(static int k5_auth_send,(int,int,int));
  291. _PROTOTYP(static int k5_auth_reply,(int, unsigned char *, int));
  292. _PROTOTYP(static int k5_auth_is,(int,unsigned char *, int));
  293. _PROTOTYP(static int SendK5AuthSB,(int, void *, int));
  294. #ifdef TLS_VERIFY
  295. static int krb5_tls_verified = 0;
  296. #endif /* TLS_VERIFY */
  297. #endif /* KRB5 */
  298.  
  299. #ifdef GSSAPI_KRB5
  300. #include <gssapi/gssapi.h>
  301. #include <gssapi/gssapi_generic.h>
  302. #include <gssapi/gssapi_krb5.h>
  303.  
  304. static gss_ctx_id_t gcontext;
  305. #define GSS_BUFSIZ 4096
  306. static gss_buffer_desc gss_send_tok, gss_recv_tok, *gss_token_ptr;
  307. static char gss_stbuf[GSS_BUFSIZ];
  308. static gss_name_t gss_target_name;
  309. static struct gss_channel_bindings_struct gss_chan;
  310.  
  311. _PROTOTYP(static int gssk5_auth_send,(int,int,int));
  312. _PROTOTYP(static int gssk5_auth_reply,(int, unsigned char *, int));
  313. _PROTOTYP(static int gssk5_auth_is,(int,unsigned char *, int));
  314. _PROTOTYP(static int SendGSSK5AuthSB,(int, void *, int));
  315. #endif /* GSSAPI_KRB5 */
  316.  
  317. #ifdef CK_SRP
  318. #ifdef PRE_SRP_1_7_3
  319. _PROTOTYP(static int srp_reply,(int, unsigned char *, int));
  320. _PROTOTYP(static int srp_is,(int, unsigned char *, int));
  321. #else /* PRE_SRP_1_7_3 */
  322. _PROTOTYP(static int new_srp_reply,(int, unsigned char *, int));
  323. _PROTOTYP(static int new_srp_is,(int, unsigned char *, int));
  324. #endif /* PRE_SRP_1_7_3 */
  325. #endif /* SRP */
  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,
  803.           "ck_auth_in_progress() GSSAPI Kerberos 5 !accept_complete",
  804.           "",
  805.           0
  806.           );
  807.             return(1);
  808.         }
  809.         else if ((auth_how & AUTH_HOW_MASK) && !mutual_complete) {
  810.             debug(F100,
  811.           "ck_auth_in_progress() GSSAPI Kerberos 5 !mutual_complete",
  812.           "",
  813.           0
  814.           );
  815.             return(1);
  816.         } else
  817.       return(0);
  818.         break;
  819. #endif /* GSSAPI_K5 */
  820. #endif /* KRB5 */
  821. #ifdef CK_SRP
  822.     case AUTHTYPE_SRP:
  823.         if (!accept_complete || srp_waitresp)
  824.             return(1);
  825.         else
  826.             return(0);
  827. #endif /* CK_SRP */
  828. #ifdef NTLM
  829.     case AUTHTYPE_NTLM:
  830.         if (!accept_complete) {
  831.             debug(F100,"ck_auth_in_progress() NTLM !accept_complete",
  832.                    "",0);
  833.             return(1);
  834.         }
  835.         else
  836.             return(0);
  837. #endif /* NTLM */
  838.     case AUTHTYPE_SSL:
  839.         if (!accept_complete) {
  840.             debug(F100,"ck_auth_in_progress() SSL !accept_complete",
  841.                    "",0);
  842.             return(1);
  843.         }
  844.         else
  845.             return(0);
  846.     default:
  847.         return(0);
  848.     }
  849.     return(0);
  850. }
  851.  
  852.  
  853. /*  C K _ K R B _ T N _ A U T H _ R E Q U E S T
  854.  *
  855.  *  Builds a Telnet Authentication Send Negotiation providing the
  856.  *  list of supported authentication methods.  To be used only
  857.  *  when accepting incoming connections as only the server (DO) side of the
  858.  *  Telnet negotiation is allowed to send an AUTH SEND.
  859.  *
  860.  *  Returns: 0 on success and -1 on failure
  861.  */
  862.  
  863. static unsigned char str_request[64] = { IAC, SB,
  864.                                              TELOPT_AUTHENTICATION,
  865.                                              TELQUAL_SEND };
  866. #ifdef GSSAPI_K5
  867. static int
  868. ck_tn_auth_request_gsskrb5(int i)
  869. {
  870.     if (ck_gssapi_is_installed() && ck_krb5_is_installed_as_server()) {
  871.         if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  872.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  873.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  874.                tn_auth_enc == TN_AUTH_ENC_EXCH) ) {
  875.             str_request[i++] = AUTHTYPE_KERBEROS_V5;
  876.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  877.             str_request[i] |= AUTH_ENCRYPT_AFTER_EXCHANGE;
  878.  
  879.             if ( deblog || tn_deb || debses )
  880.                 ckstrncat(tn_msg,
  881.                 "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_AFTER_EXCHANGE ",
  882.                           TN_MSG_LEN);
  883.             i++;
  884.         }
  885.     }
  886. }
  887. #endif /* GSSAPI_K5 */
  888.  
  889. #ifdef KRB5
  890. static int
  891. ck_tn_auth_request_krb5(int i)
  892. {
  893.     if (ck_krb5_is_installed_as_server()) {
  894. #ifdef CK_SSL
  895.         if ( ck_ssleay_is_installed() &&
  896.              (tls_active_flag || ssl_active_flag) &&
  897.              ssl_finished_messages )
  898.         {
  899. #ifdef USE_INI_CRED_FWD
  900.             if ( forward_flag &&
  901.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  902.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  903.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  904.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  905.                  )
  906.             {
  907.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  908.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  909.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  910.                 str_request[i] |= INI_CRED_FWD_ON;
  911.  
  912.                 if ( deblog || tn_deb || debses )
  913.                     ckstrncat(tn_msg,
  914.  "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS|INI_CRED_FWD_ON ",
  915.                                TN_MSG_LEN);
  916.                 i++;
  917.             }
  918. #endif /* USE_INI_CRED_FWD */
  919.             if ( (tn_auth_how == TN_AUTH_HOW_ANY ||
  920.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  921.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  922.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  923.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  924.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  925.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  926.  
  927.                 if ( deblog || tn_deb || debses )
  928.                     ckstrncat(tn_msg,
  929.                       "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_START_TLS ",
  930.                               TN_MSG_LEN);
  931.                 i++;
  932.             }
  933.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  934.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  935.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  936.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  937.                 str_request[i] |= AUTH_ENCRYPT_START_TLS;
  938.  
  939.                 if ( deblog || tn_deb || debses )
  940.                     ckstrncat(tn_msg,
  941.                     "KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  942.                                TN_MSG_LEN);
  943.                 i++;
  944.             }
  945.         }
  946. #ifdef CK_ENCRYPTION
  947.         else
  948.         {
  949. #endif /* CK_ENCRYPTION */
  950. #endif /* CK_SSL */
  951. #ifdef CK_ENCRYPTION
  952. #ifdef USE_INI_CRED_FWD
  953.             if ( forward_flag &&
  954.                  TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  955.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  956.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  957.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  958.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  959.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  960.                  )
  961.             {
  962.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  963.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  964.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  965.                 str_request[i] |= INI_CRED_FWD_ON;
  966.  
  967.                 if ( deblog || tn_deb || debses )
  968.                     ckstrncat(tn_msg,
  969.   "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT|INI_CRED_FWD_ON ",
  970.                                TN_MSG_LEN);
  971.                 i++;
  972.             }
  973. #endif /* USE_INI_CRED_FWD */
  974.  
  975.             if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  976.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  977.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  978.                    tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  979.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  980.                    tn_auth_enc == TN_AUTH_ENC_TELOPT) ) {
  981.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  982.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  983.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  984.  
  985.                 if ( deblog || tn_deb || debses )
  986.                     ckstrncat(tn_msg,
  987.               "KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL|ENCRYPT_USING_TELOPT ",
  988.                                TN_MSG_LEN);
  989.                 i++;
  990.             }
  991. #ifdef CK_SSL
  992.         }
  993. #endif /* CK_SSL */
  994. #endif /* CK_ENCRYPTION */
  995.  
  996.         if ( TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  997.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  998.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  999.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1000. #ifdef CK_SSL
  1001.              && !(ck_ssleay_is_installed() &&
  1002.                    (tls_active_flag || ssl_active_flag) &&
  1003.                    tls_is_anon(0))
  1004. #endif /* CK_SSL */
  1005.              )
  1006.         {
  1007. #ifdef CK_ENCRYPTION
  1008.             /* Can't perform mutual authentication without encryption */
  1009.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1010.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1011.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1012.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1013.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1014.  
  1015.                 if ( deblog || tn_deb || debses )
  1016.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|MUTUAL ",
  1017.                                TN_MSG_LEN);
  1018.                 i++;
  1019.             }
  1020. #endif /* CK_ENCRYPTION */
  1021.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1022.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1023.                 str_request[i++] = AUTHTYPE_KERBEROS_V5;
  1024.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1025.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1026.  
  1027.                 if ( deblog || tn_deb || debses )
  1028.                     ckstrncat(tn_msg,"KERBEROS_V5 CLIENT_TO_SERVER|ONE_WAY ",
  1029.                                TN_MSG_LEN);
  1030.                 i++;
  1031.             }
  1032.         }
  1033.     }
  1034.     return(i);
  1035. }
  1036. #endif /* KRB5 */
  1037. #ifdef KRB4
  1038. static int
  1039. ck_tn_auth_request_krb4(int i)
  1040. {
  1041.     if (ck_krb4_is_installed_as_server()) {
  1042. #ifdef CK_ENCRYPTION
  1043.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1044.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1045.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1046.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1047.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1048.                tn_auth_enc == TN_AUTH_ENC_TELOPT) )
  1049.         {
  1050.             str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1051.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1052.             str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1053.  
  1054.             if ( deblog || tn_deb || debses )
  1055.               ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL|ENCRYPT ",
  1056.                         TN_MSG_LEN);
  1057.             i++;
  1058.         }
  1059. #endif /* CK_ENCRYPTION */
  1060.  
  1061.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1062.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1063.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1064.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1065.         {
  1066. #ifdef CK_ENCRYPTION
  1067.             /* Can't perform mutual authentication without encryption */
  1068.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1069.                  tn_auth_how == TN_AUTH_HOW_MUTUAL ) {
  1070.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1071.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_MUTUAL;
  1072.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1073.  
  1074.                 if ( deblog || tn_deb || debses )
  1075.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|MUTUAL ",
  1076.                                TN_MSG_LEN);
  1077.                 i++;
  1078.             }
  1079. #endif /* CK_ENCRYPTION */
  1080.             if ( tn_auth_how == TN_AUTH_HOW_ANY ||
  1081.                  tn_auth_how == TN_AUTH_HOW_ONE_WAY ) {
  1082.                 str_request[i++] = AUTHTYPE_KERBEROS_V4;
  1083.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1084.                 str_request[i] |= AUTH_ENCRYPT_OFF;
  1085.  
  1086.                 if ( deblog || tn_deb || debses )
  1087.                     ckstrncat(tn_msg,"KERBEROS_V4 CLIENT_TO_SERVER|ONE_WAY ",
  1088.                                TN_MSG_LEN);
  1089.                 i++;
  1090.             }
  1091.         }
  1092.     }
  1093.  
  1094.     return(i);
  1095. }
  1096. #endif /* KRB4 */
  1097.  
  1098. #ifdef CK_SRP
  1099. static int
  1100. ck_tn_auth_request_srp(int i)
  1101. {
  1102.     if (ck_srp_is_installed_as_server()) {
  1103. #ifndef PRE_SRP_1_4_5
  1104.         /* Dont' do this yet.  SRP when it uses the ENCRYPT_USING_TELOPT   */
  1105.         /* flag it must perform a checksum of the auth-type-pair but there */
  1106.         /* is no mechansim to do that yet.                                 */
  1107. #ifdef CK_SSL
  1108.         if ( ck_ssleay_is_installed() &&
  1109.              (tls_active_flag || ssl_active_flag) &&
  1110.              ssl_finished_messages &&
  1111.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1112.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1113.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1114.                    tn_auth_enc == TN_AUTH_ENC_TELOPT))
  1115.         {
  1116.             str_request[i++] = AUTHTYPE_SRP;
  1117.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1118.             str_request[i] |= AUTH_ENCRYPT_START_TLS;
  1119.  
  1120.             if ( deblog || tn_deb || debses )
  1121.                 ckstrncat(tn_msg,
  1122.                            "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_START_TLS ",
  1123.                            TN_MSG_LEN);
  1124.             i++;
  1125.         }
  1126. #ifdef CK_ENCRYPTION
  1127.         else {
  1128. #endif /* CK_ENCRYPTION */
  1129. #endif /* CK_SSL */
  1130. #ifdef CK_ENCRYPTION
  1131.             if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1132.                  TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_RF &&
  1133.                  (tn_auth_how == TN_AUTH_HOW_ANY ||
  1134.                    tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1135.                  (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1136.                    tn_auth_enc == TN_AUTH_ENC_TELOPT)
  1137.                  ) {
  1138.                 str_request[i++] = AUTHTYPE_SRP;
  1139.                 str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1140.                 str_request[i] |= AUTH_ENCRYPT_USING_TELOPT;
  1141.  
  1142.                 if ( deblog || tn_deb || debses )
  1143.                     ckstrncat(tn_msg,
  1144.                     "SRP CLIENT_TO_SERVER|ONE_WAY|ENCRYPT_USING_TELOPT ",
  1145.                                TN_MSG_LEN);
  1146.                 i++;
  1147.             }
  1148. #ifdef CK_SSL
  1149.         }
  1150. #endif /* CK_SSL */
  1151. #endif /* CK_ENCRYPTION */
  1152. #endif /* PRE_SRP_1_4_5 */
  1153.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1154.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1155.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1156.                tn_auth_how == TN_AUTH_HOW_MUTUAL)  &&
  1157.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1158.                tn_auth_enc == TN_AUTH_ENC_NONE)
  1159. #ifdef CK_SSL
  1160.              && !(ck_ssleay_is_installed() &&
  1161.                    (tls_active_flag || ssl_active_flag) &&
  1162.                    tls_is_anon(0))
  1163. #endif /* CK_SSL */
  1164.              )
  1165.         {
  1166.             str_request[i++] = AUTHTYPE_SRP;
  1167.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1168.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1169.  
  1170.             if ( deblog || tn_deb || debses )
  1171.                 ckstrncat(tn_msg,"SRP CLIENT_TO_SERVER|ONE_WAY ",
  1172.                            TN_MSG_LEN);
  1173.             i++;
  1174.         }
  1175.     }
  1176.  
  1177.     return(i);
  1178. }
  1179. #endif /* CK_SRP */
  1180.  
  1181. #ifdef CK_SSL
  1182. static int
  1183. ck_tn_auth_request_ssl(int i)
  1184. {
  1185.     if (ck_ssleay_is_installed()
  1186.          && !tls_active_flag && !ssl_active_flag && ssl_initialized
  1187.          ) {
  1188.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1189.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1190.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1191.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1192.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1193.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1194.         {
  1195.             str_request[i++] = AUTHTYPE_SSL;
  1196.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1197.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1198.             if ( deblog || tn_deb || debses )
  1199.                 ckstrncat(tn_msg,"SSL CLIENT_TO_SERVER|ONE_WAY ",
  1200.                            TN_MSG_LEN);
  1201.             i++;
  1202.         }
  1203.     }
  1204.  
  1205.     return(i);
  1206. }
  1207. #endif /* CK_SSL */
  1208. #ifdef NTLM
  1209. static int
  1210. ck_tn_auth_request_ntlm(int i)
  1211. {
  1212.     /* Microsoft's Telnet client won't perform authentication if */
  1213.     /* NTLM is not first.                                        */
  1214.     if ( ck_ntlm_is_valid(1) ) {
  1215.         if (TELOPT_ME_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1216.              TELOPT_U_MODE(TELOPT_ENCRYPTION) != TN_NG_MU &&
  1217.              (tn_auth_how == TN_AUTH_HOW_ANY ||
  1218.                tn_auth_how == TN_AUTH_HOW_ONE_WAY)  &&
  1219.              (tn_auth_enc == TN_AUTH_ENC_ANY ||
  1220.                tn_auth_enc == TN_AUTH_ENC_NONE) )
  1221.         {
  1222.             str_request[i++] = AUTHTYPE_NTLM;
  1223.             str_request[i] = AUTH_CLIENT_TO_SERVER | AUTH_HOW_ONE_WAY;
  1224.             str_request[i] |= AUTH_ENCRYPT_OFF;
  1225.             if ( deblog || tn_deb || debses )
  1226.                 ckstrncat(tn_msg,"NTLM CLIENT_TO_SERVER|ONE_WAY ",
  1227.                            TN_MSG_LEN);
  1228.             i++;
  1229.         }
  1230.     }
  1231.  
  1232.     return(i);
  1233. }
  1234. #endif /* NTLM */
  1235. int
  1236. #ifdef CK_ANSIC
  1237. ck_tn_auth_request(void)
  1238. #else
  1239. ck_tn_auth_request()
  1240. #endif
  1241. {
  1242.     int i = 4, rc = -1;
  1243.  
  1244. #ifdef CK_SSL
  1245.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1246.         return(0);
  1247.     }
  1248. #endif /* CK_SSL */
  1249.  
  1250.     if ( deblog || tn_deb || debses )
  1251.         strcpy(tn_msg,"TELNET SENT SB AUTHENTICATION SEND ");
  1252.  
  1253.     /* Create a list of acceptable Authentication types to send to */
  1254.     /* the client and let it choose find one that we support       */
  1255.  
  1256.     /* For those authentication methods that support Encryption or */
  1257.     /* Credentials Forwarding we must send all of the appropriate  */
  1258.     /* combinations based upon the state of                        */
  1259.     /* TELOPT_x_MODE(TELOPT_ENCRYPTION) and forward_flag.          */
  1260.  
  1261.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  1262. #ifdef GSSAPI_K5
  1263.         i = ck_tn_auth_request_gsskrb5(i);
  1264. #endif /* GSSAPI_K5 */
  1265. #ifdef KRB5
  1266.         i = ck_tn_auth_request_krb5(i);
  1267. #endif /* KRB5 */
  1268. #ifdef KRB4
  1269.         i = ck_tn_auth_request_krb4(i);
  1270. #endif /* KRB4 */
  1271. #ifdef CK_SRP
  1272.         i = ck_tn_auth_request_srp(i);
  1273. #endif /* SRP */
  1274. #ifdef CK_SSL
  1275.         i = ck_tn_auth_request_ssl(i);
  1276. #endif /* CK_SSL */
  1277. #ifdef NTLM
  1278.         i = ck_tn_auth_request_ntlm(i);
  1279. #endif /* NTLM */
  1280.     } else {
  1281.         int j;
  1282.         for ( j=0;
  1283.               j<AUTHTYPLSTSZ && auth_type_user[j] != AUTHTYPE_NULL;
  1284.               j++) {
  1285. #ifdef NTLM
  1286.             if (auth_type_user[j] == AUTHTYPE_NTLM)
  1287.                 i = ck_tn_auth_request_ntlm(i);
  1288. #endif /* NTLM */
  1289. #ifdef CK_SSL
  1290.             if ( auth_type_user[j] == AUTHTYPE_SSL )
  1291.                 i = ck_tn_auth_request_ssl(i);
  1292. #endif /* CK_SSL */
  1293. #ifdef CK_SRP
  1294.             if ( auth_type_user[j] == AUTHTYPE_SRP )
  1295.                 i = ck_tn_auth_request_srp(i);
  1296. #endif /* SRP */
  1297. #ifdef GSSAPI_K5
  1298.             if ( auth_type_user[j] == AUTHTYPE_GSSAPI_KRB5 )
  1299.                 i = ck_tn_auth_request_gsskrb5(i);
  1300. #endif /* GSSAPI_K5 */
  1301. #ifdef KRB5
  1302.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V5 )
  1303.                 i = ck_tn_auth_request_krb5(i);
  1304. #endif /* KRB5 */
  1305. #ifdef KRB4
  1306.             if ( auth_type_user[j] == AUTHTYPE_KERBEROS_V4 )
  1307.                 i = ck_tn_auth_request_krb4(i);
  1308. #endif /* KRB4 */
  1309.         }
  1310.     }
  1311.  
  1312.     str_request[i++] = IAC;
  1313.     str_request[i++] = SE;
  1314.     if ( deblog || tn_deb || debses ) {
  1315.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1316.         debug(F100,tn_msg,"",0);
  1317.         if (tn_deb || debses) tn_debug(tn_msg);
  1318.     }
  1319.  
  1320.     /* Send data */
  1321. #ifdef OS2
  1322.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1323. #endif
  1324.     rc = ttol((CHAR *)str_request, i);
  1325. #ifdef OS2
  1326.     ReleaseTelnetMutex();
  1327. #endif
  1328.     if ( rc == i )
  1329.         return(0);
  1330.     else
  1331.         return(-1);
  1332. }
  1333.  
  1334. #ifdef CK_ENCRYPTION
  1335. VOID
  1336. ck_tn_enc_start()
  1337. {
  1338.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1339.         return;
  1340.     if (!TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop &&
  1341.          (!encrypt_is_decrypting() || !encrypt_is_encrypting())) {
  1342.         debug(F110,"ck_tn_enc_start","nothing to do",0);
  1343.         return;
  1344.     }
  1345.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1346.     if (TELOPT_ME(TELOPT_ENCRYPTION) && !encrypt_is_encrypting()) {
  1347.         debug(F110,"ck_tn_enc_start","encrypt_request_start",0);
  1348.         encrypt_request_start();
  1349.     }
  1350.     if (TELOPT_U(TELOPT_ENCRYPTION) && !encrypt_is_decrypting()) {
  1351.         debug(F110,"ck_tn_enc_start","encrypt_send_request_start",0);
  1352.         encrypt_send_request_start();
  1353.     }
  1354.     tn_wait("encrypt start");
  1355.     tn_push();
  1356. }
  1357.  
  1358. VOID
  1359. ck_tn_enc_stop()
  1360. {
  1361.     if (!TELOPT_ME(TELOPT_ENCRYPTION) && !TELOPT_U(TELOPT_ENCRYPTION))
  1362.         return;
  1363.     if (TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop ||
  1364.          !(encrypt_is_decrypting() || encrypt_is_encrypting())) {
  1365.         debug(F110,"ck_tn_enc_stop","nothing to do",0);
  1366.       return;
  1367.     }
  1368.     TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1369.     if (TELOPT_U(TELOPT_ENCRYPTION) && encrypt_is_decrypting()) {
  1370.         debug(F110,"ck_tn_enc_stop","encrypt_send_request_end",0);
  1371.         encrypt_send_request_end();
  1372.     }
  1373.     if (TELOPT_ME(TELOPT_ENCRYPTION) && encrypt_is_encrypting()) {
  1374.         debug(F110,"ck_tn_enc_stop","encrypt_send_end",0);
  1375.         encrypt_send_end();
  1376.     }
  1377.     tn_wait("encrypt stop");
  1378.     tn_push();
  1379. }
  1380. #endif /* CK_ENCRYPTION */
  1381.  
  1382. /*  C K _ K R B _ T N _ S B _ A U T H
  1383.  *  An interface between the C-Kermit Telnet Command Parser and the Authent-
  1384.  *  ication option parser implemented in the Kerberos Telnet client.
  1385.  *
  1386.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1387.  *  len  - the length of the buffer
  1388.  *
  1389.  *  Returns: 0 on success and -1 on failure
  1390.  */
  1391.  
  1392. int
  1393. #ifdef CK_ANSIC
  1394. ck_tn_sb_auth(char * sb, int len)
  1395. #else /* CK_ANSIC */
  1396. ck_tn_sb_auth(sb,len) char * sb; int len;
  1397. #endif /* CK_ANSIC */
  1398. {
  1399.     /* auth_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1400.     /* and it wants the length to exclude the IAC SE bytes               */
  1401.     CHAR * buf;
  1402.     int rc = -1;
  1403.  
  1404.     buf = malloc(len-1);
  1405.     if ( !buf ) return(-1);
  1406.  
  1407.     buf[0] = SB;
  1408.     memcpy( &buf[1], sb, len-2 );
  1409.     rc = auth_parse(buf,len-1);
  1410.     free(buf);
  1411.     debug(F111,"ck_tn_sb_auth","rc",rc);
  1412.     if (rc == AUTH_FAILURE) {
  1413.         authentication_version = AUTHTYPE_NULL;
  1414. #ifndef NOLOCAL
  1415. #ifdef OS2
  1416.         ipadl25();
  1417. #endif /* OS2 */
  1418. #endif /* NOLOCAL */
  1419.         return(-1);
  1420.     }
  1421. #ifndef NOLOCAL
  1422. #ifdef OS2
  1423.     ipadl25();
  1424. #endif /* OS2 */
  1425. #endif /* NOLOCAL */
  1426.     return(0);
  1427. }
  1428.  
  1429. /*  C K _ K R B _ T N _ S B _ E N C R Y P T
  1430.  *  An interface between the C-Kermit Telnet Command Parser and the Encryption
  1431.  *  option parser implemented in the Kerberos Telnet client.
  1432.  *
  1433.  *  sb   - the subnegotiation as calculated in ckcnet.c
  1434.  *  len  - the length of the buffer
  1435.  *
  1436.  *  Returns: Always returns 0 for success since encrypt_parse is void
  1437.  */
  1438.  
  1439.  
  1440. int
  1441. #ifdef CK_ANSIC
  1442. ck_tn_sb_encrypt(char * sb, int len)
  1443. #else
  1444. ck_tn_sb_encrypt(sb,len) char * sb; int len;
  1445. #endif /* CK_ANSIC */
  1446. {
  1447.     /* encrypt_parse() assumes that sb starts at pos 1 not 0 as in ckcnet.c */
  1448.     /* and it wants the length to exclude the IAC SE bytes                  */
  1449. #ifdef CK_ENCRYPTION
  1450.     char * buf;
  1451.     int rc = -1;
  1452.  
  1453.     buf = malloc(len-1);
  1454.     if ( !buf ) return(-1);
  1455.  
  1456.     buf[0] = SB;
  1457.     memcpy( &buf[1], sb, len-2 );
  1458.     rc = encrypt_parse(buf,len-1);
  1459.  
  1460.     if (rc < 0) {
  1461.         free(buf);
  1462.         return(-1);
  1463.     }
  1464.  
  1465.     /* This is a hack.  It does not belong here but should really be in */
  1466.     /* encrypt_parse() but in K95 the encrypt_parse() routine does not  */
  1467.     /* have access to the telopt_states array.                          */
  1468.     if ( buf[1] == ENCRYPT_REQEND )
  1469.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 1;
  1470.     else if ( buf[1] == ENCRYPT_REQSTART )
  1471.         TELOPT_SB(TELOPT_ENCRYPTION).encrypt.stop = 0;
  1472. #ifndef NOLOCAL
  1473. #ifdef OS2
  1474.     ipadl25();
  1475. #endif /* OS2 */
  1476. #endif /* NOLOCAL */
  1477.     free(buf);
  1478. #endif /* ENCRYPTION */
  1479.     return(0);
  1480. }
  1481.  
  1482.  
  1483. /*  C K _ K R B _ E N C R Y P T I N G
  1484.  *  Returns 1 if we are encrypting and 0 if we are not
  1485.  */
  1486.  
  1487. int
  1488. #ifdef CK_ANSIC
  1489. ck_tn_encrypting(VOID)
  1490. #else /* CK_ANSIC */
  1491. ck_tn_encrypting()
  1492. #endif /* CK_ANSIC */
  1493. {
  1494. #ifdef CK_ENCRYPTION
  1495.     if ( g_kstream == NULL )
  1496.         return(0);
  1497.     if ( g_kstream->encrypt && encrypt_is_encrypting()) {
  1498.         debug(F111,"ck_tn_encrypting","encrypting",
  1499.                g_kstream->encrypt_type);
  1500.         return(g_kstream->encrypt_type);
  1501.     }
  1502. #endif /* CK_ENCRYPTION */
  1503.     debug(F110,"ck_tn_encrypting","not encrypting",0);
  1504.     return(0);
  1505. }
  1506.  
  1507. /*  C K _ K R B _ D E C R Y P T I N G
  1508.  *  Returns 1 if we are decrypting and 0 if we are not
  1509.  */
  1510.  
  1511. int
  1512. #ifdef CK_ANSIC
  1513. ck_tn_decrypting(VOID)
  1514. #else
  1515. ck_tn_decrypting()
  1516. #endif /* CK_ANSIC */
  1517. {
  1518. #ifdef CK_ENCRYPTION
  1519.     if ( g_kstream == NULL )
  1520.         return(0);
  1521.     if ( g_kstream->decrypt && encrypt_is_decrypting()) {
  1522.         debug(F111,"ck_tn_decrypting","decrypting",
  1523.                g_kstream->decrypt_type);
  1524.         return(g_kstream->decrypt_type);
  1525.     }
  1526. #endif /* CK_ENCRYPTION */
  1527.     debug(F110,"ck_tn_decrypting","not decrypting",0);
  1528.     return(0);
  1529. }
  1530.  
  1531. /*  C K _ K R B _ A U T H E N T I C A T E D
  1532.  *  Returns the authentication type: AUTHTYPE_NULL, AUTHTYPE_KERBEROS4,
  1533.  *  or AUTHTYPE_KERBEROS5, AUTHTYPE_SRP, ... (see ckctel.h)
  1534.  */
  1535.  
  1536. int
  1537. #ifdef CK_ANSIC
  1538. ck_tn_authenticated(VOID)
  1539. #else
  1540. ck_tn_authenticated()
  1541. #endif
  1542. {
  1543.     return(authentication_version);
  1544. }
  1545.  
  1546. /*  C K _ K R B _ E N C R Y P T
  1547.  *  encrypts n characters in s if we are encrypting
  1548.  */
  1549.  
  1550. VOID
  1551. #ifdef CK_ANSIC
  1552. ck_tn_encrypt( char * s, int n )
  1553. #else
  1554. ck_tn_encrypt( s,n ) char * s; int n;
  1555. #endif
  1556. {
  1557. #ifdef CK_ENCRYPTION
  1558.     struct kstream_data_block i;
  1559.  
  1560.     if (g_kstream->encrypt && encrypt_is_encrypting()) {
  1561. #ifdef DEBUG
  1562.       hexdump("from plaintext", s, n);
  1563. #endif
  1564.         i.ptr = s;
  1565.         i.length = n;
  1566.         g_kstream->encrypt(&i, NULL);
  1567. #ifdef DEBUG
  1568.         hexdump("to cyphertext", s, n);
  1569. #endif
  1570.     }
  1571.     else debug(F101,"ck_tn_encrypt not encrypting","",n);
  1572. #endif /* ENCRYPTION */
  1573. }
  1574.  
  1575. /*  C K _ K R B _ D E C R Y P T
  1576.  *  decrypts n characters in s if we are decrypting
  1577.  */
  1578.  
  1579. VOID
  1580. #ifdef CK_ANSIC
  1581. ck_tn_decrypt( char * s, int n )
  1582. #else
  1583. ck_tn_decrypt( s,n ) char * s; int n;
  1584. #endif
  1585. {
  1586. #ifdef CK_ENCRYPTION
  1587.     struct kstream_data_block i;
  1588.  
  1589.     if (g_kstream->decrypt && encrypt_is_decrypting()) {
  1590.  
  1591. #ifdef DEBUG
  1592.         hexdump("from cyphertext", s, n);
  1593. #endif
  1594.  
  1595.         i.ptr = s;
  1596.         i.length = n;
  1597.         g_kstream->decrypt(&i, NULL);
  1598. #ifdef DEBUG
  1599.         hexdump("to plaintext", s, n);
  1600. #endif
  1601.     }
  1602.     else debug(F101,"ck_tn_decrypt not decrypting","",n);
  1603. #endif /* ENCRYPTION */
  1604. }
  1605.  
  1606. /*  S E N D K 5 A U T H S B
  1607.  *  Send a Kerberos 5 Authentication Subnegotiation to host and
  1608.  *  output appropriate Telnet Debug messages
  1609.  *
  1610.  *  type - Sub Negotiation type
  1611.  *  data - ptr to buffer containing data
  1612.  *  len  - len of buffer if not NUL terminated
  1613.  *
  1614.  *  returns number of characters sent or error value
  1615.  */
  1616.  
  1617. static int
  1618. #ifdef CK_ANSIC
  1619. SendK5AuthSB(int type, void *data, int len)
  1620. #else
  1621. SendK5AuthSB(type,data,len) int type; void *data; int len;
  1622. #endif
  1623. {
  1624.     int rc;
  1625.     unsigned char *p = str_data + 3;
  1626.     unsigned char *cd = (unsigned char *)data;
  1627.     extern int sstelnet;
  1628.  
  1629. #ifdef CK_SSL
  1630.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1631.         if (ttchk() < 0)
  1632.           return(0);
  1633.         else
  1634.           return(1);
  1635.     }
  1636. #endif /* CK_SSL */
  1637.  
  1638.     if ( type < 0 || type > 7 )         /* Check for invalid values */
  1639.         return(0);
  1640.  
  1641.     if (!cd) {
  1642.         cd = (unsigned char *)"";
  1643.         len = 0;
  1644.     }
  1645.  
  1646.     if (len == -1)                        /* Use strlen() for len */
  1647.         len = strlen((char *)cd);
  1648.  
  1649.     /* Construct Message */
  1650.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1651.     *p++ = AUTHTYPE_KERBEROS_V5;
  1652.     *p = AUTH_CLIENT_TO_SERVER;
  1653.     *p |= auth_how;
  1654. #ifdef CK_ENCRYPTION
  1655.     *p |= auth_crypt;
  1656. #endif
  1657. #ifdef USE_INI_CRED_FWD
  1658.     if (auth_fwd)
  1659.         *p |= INI_CRED_FWD_ON;
  1660. #endif /* USE_INI_CRED_FWD */
  1661.     p++;
  1662.     *p++ = type;
  1663.     while (len-- > 0) {
  1664.         if ((*p++ = *cd++) == IAC)
  1665.             *p++ = IAC;
  1666.     }
  1667.     *p++ = IAC;
  1668.     *p++ = SE;
  1669.  
  1670.     /* Handle Telnet Debugging Messages */
  1671.     if (deblog || tn_deb || debses) {
  1672.         int i;
  1673.         int deblen=p-str_data-2;
  1674.         char *s=NULL;
  1675.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1676.             auth_crypt
  1677. #ifdef USE_INI_CRED_FWD
  1678.               | (auth_fwd?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  1679. #endif /* USE_INI_CRED_FWD */
  1680.                     ;
  1681.  
  1682.         switch (type) {
  1683.         case 0:
  1684.             s = "AUTH";
  1685.             break;
  1686.         case 1:
  1687.             s = "REJECT";
  1688.             break;
  1689.         case 2:
  1690.             s = "ACCEPT";
  1691.             break;
  1692.         case 3:
  1693.             s = "RESPONSE";
  1694.             break;
  1695.         case 4:
  1696.             s = "FORWARD";
  1697.             break;
  1698.         case 5:
  1699.             s = "FORWARD_ACCEPT";
  1700.             break;
  1701.         case 6:
  1702.             s = "FORWARD_REJECT";
  1703.             break;
  1704.         case 7:
  1705.             s = "TLS_VERIFY";
  1706.                 break;
  1707.         }
  1708.  
  1709.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1710.                   "TELNET SENT SB ",
  1711.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1712.                  str_data[3] == TELQUAL_IS ? "IS" :
  1713.                  str_data[3] == TELQUAL_REPLY ? "REPLY" : "???"," ",
  1714.                  AUTHTYPE_NAME(authentication_version)," ",
  1715.                  AUTHMODE_NAME(mode)," ",
  1716.                  s," ",NULL);
  1717.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1718.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1719.         debug(F100,tn_msg,"",0);
  1720.         if (tn_deb || debses) tn_debug(tn_msg);
  1721.     }
  1722.  
  1723.     /* Send data */
  1724. #ifdef OS2
  1725.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1726. #endif
  1727.     rc = ttol((CHAR *)str_data, p - str_data);
  1728. #ifdef OS2
  1729.     ReleaseTelnetMutex();
  1730. #endif
  1731.     debug(F111,"SendK5AuthSB","ttol()",rc);
  1732.     return(rc);
  1733. }
  1734.  
  1735. /*  S E N D K 4 A U T H S B
  1736.  *  Send a Kerberos 4 Authentication Subnegotiation to host and
  1737.  *  output appropriate Telnet Debug messages
  1738.  *
  1739.  *  type - Sub Negotiation type
  1740.  *  data - ptr to buffer containing data
  1741.  *  len  - len of buffer if not NUL terminated
  1742.  *
  1743.  *  returns number of characters sent or error value
  1744.  */
  1745.  
  1746. static int
  1747. #ifdef CK_ANSIC
  1748. SendK4AuthSB(int type, void *data, int len)
  1749. #else
  1750. SendK4AuthSB(type,data,len) int type; void *data; int len;
  1751. #endif
  1752. {
  1753.     int rc;
  1754.     unsigned char *p = str_data + 3;
  1755.     unsigned char *cd = (unsigned char *)data;
  1756.     extern int sstelnet;
  1757.     int mode = (auth_how & AUTH_HOW_MASK) |
  1758.         auth_crypt;
  1759.  
  1760.     if ( type < 0 || type > 4 )         /* Check for invalid values */
  1761.         return(0);
  1762.  
  1763. #ifdef CK_SSL
  1764.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1765.         if (ttchk() < 0)
  1766.           return(0);
  1767.         else
  1768.           return(1);
  1769.     }
  1770. #endif /* CK_SSL */
  1771.  
  1772.     if (!cd) {
  1773.         cd = (unsigned char *)"";
  1774.         len = 0;
  1775.     }
  1776.  
  1777.     if (len == -1)                        /* Use strlen() for len */
  1778.         len = strlen((char *)cd);
  1779.  
  1780.  
  1781.     /* Construct Message */
  1782.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1783.     *p++ = AUTHTYPE_KERBEROS_V4;
  1784.     *p = AUTH_CLIENT_TO_SERVER;
  1785.     *p |= mode;
  1786.     p++;
  1787.     *p++ = type;
  1788.     while (len-- > 0) {
  1789.         if ((*p++ = *cd++) == IAC)
  1790.             *p++ = IAC;
  1791.         }
  1792.     *p++ = IAC;
  1793.     *p++ = SE;
  1794.  
  1795.     /* Handle Telnet Debugging Messages */
  1796.     if (deblog || tn_deb || debses) {
  1797.         int i;
  1798.         int deblen=p-str_data-2;
  1799.         char *s=NULL;
  1800.  
  1801.         switch (type) {
  1802.         case 0:
  1803.             s = "AUTH";
  1804.             break;
  1805.         case 1:
  1806.             s = "REJECT";
  1807.             break;
  1808.         case 2:
  1809.             s = "ACCEPT";
  1810.             break;
  1811.         case 3:
  1812.             s = "CHALLENGE";
  1813.             break;
  1814.         case 4:
  1815.             s = "RESPONSE";
  1816.             break;
  1817.         }
  1818.  
  1819.         ckmakxmsg(tn_msg,TN_MSG_LEN,"TELNET SENT SB ",
  1820.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1821.                  str_data[3] == TELQUAL_IS ? "IS" :
  1822.                  (str_data[3] == TELQUAL_REPLY ? "REPLY" : "???")," ",
  1823.                  AUTHTYPE_NAME(authentication_version)," ",
  1824.                  AUTHMODE_NAME(mode)," ",
  1825.                  s," ",NULL);
  1826.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1827.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1828.         debug(F100,tn_msg,"",0);
  1829.         if (tn_deb || debses) tn_debug(tn_msg);
  1830.     }
  1831.  
  1832.     /* Send data */
  1833. #ifdef OS2
  1834.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1835. #endif
  1836.     rc = ttol((CHAR *)str_data, p - str_data);
  1837. #ifdef OS2
  1838.     ReleaseTelnetMutex();
  1839. #endif
  1840.     debug(F111,"SendK4AuthSB","ttol()",rc);
  1841.     return(rc);
  1842. }
  1843.  
  1844. /*  S E N D S R P A U T H S B
  1845.  *  Send a SRP Authentication Subnegotiation to host and
  1846.  *  output appropriate Telnet Debug messages
  1847.  *
  1848.  *  type - Sub Negotiation type
  1849.  *  data - ptr to buffer containing data
  1850.  *  len  - len of buffer if not NUL terminated
  1851.  *
  1852.  *  returns number of characters sent or error value
  1853.  */
  1854.  
  1855. static int
  1856. #ifdef CK_ANSIC
  1857. SendSRPAuthSB(int type, void *data, int len)
  1858. #else
  1859. SendSRPAuthSB(type,data,len) int type; void *data; int len;
  1860. #endif
  1861. {
  1862.     int rc;
  1863.     unsigned char *p = str_data + 3;
  1864.     unsigned char *cd = (unsigned char *)data;
  1865.     extern int sstelnet;
  1866.  
  1867.     /* Check for invalid values */
  1868.     if ( type != SRP_EXP && type != SRP_RESPONSE &&
  1869.          type != SRP_REJECT && type != SRP_ACCEPT &&
  1870.          type != SRP_CHALLENGE && type != SRP_PARAMS &&
  1871.          type != SRP_AUTH)
  1872.         return(0);
  1873.  
  1874.     if (len == -1)                        /* Use strlen() for len */
  1875.         len = strlen((char *)cd);
  1876.  
  1877.     /* Construct Message */
  1878.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  1879.     *p++ = AUTHTYPE_SRP;
  1880.     *p = AUTH_CLIENT_TO_SERVER;
  1881.     *p |= auth_how;
  1882. #ifdef CK_ENCRYPTION
  1883.     *p |= auth_crypt;
  1884. #endif
  1885.     p++;
  1886.     *p++ = type;
  1887.     while (len-- > 0) {
  1888.         if ((*p++ = *cd++) == IAC)
  1889.             *p++ = IAC;
  1890.         }
  1891.     *p++ = IAC;
  1892.     *p++ = SE;
  1893.  
  1894.     /* Handle Telnet Debugging Messages */
  1895.     if (deblog || tn_deb || debses) {
  1896.         int i;
  1897.         int deblen=p-str_data-2;
  1898.         char *s=NULL;
  1899.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  1900.             auth_crypt;
  1901.  
  1902.         switch (type) {
  1903.         case 0:
  1904.             s = "AUTH";
  1905.             break;
  1906.         case 1:
  1907.             s = "REJECT";
  1908.             break;
  1909.         case 2:
  1910.             s = "ACCEPT";
  1911.             break;
  1912.         case 3:
  1913.             s = "CHALLENGE";
  1914.             break;
  1915.         case 4:
  1916.             s = "RESPONSE";
  1917.             break;
  1918.         case 5:
  1919.             s = "FORWARD";
  1920.             break;
  1921.         case 6:
  1922.             s = "FORWARD_ACCEPT";
  1923.             break;
  1924.         case 7:
  1925.             s = "FORWARD_REJECT";
  1926.             break;
  1927.         case 8:
  1928.             s = "EXP";
  1929.             break;
  1930.         case 9:
  1931.             s = "PARAMS";
  1932.             break;
  1933.         }
  1934.  
  1935.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  1936.                   "TELNET SENT SB ",
  1937.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  1938.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  1939.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  1940.                  AUTHTYPE_NAME(authentication_version)," ",
  1941.                  AUTHMODE_NAME(mode)," ",
  1942.                  s," ",NULL);
  1943.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  1944.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1945.         debug(F100,tn_msg,"",0);
  1946.         if (tn_deb || debses) tn_debug(tn_msg);
  1947.     }
  1948.  
  1949.     /* Send data */
  1950. #ifdef OS2
  1951.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1952. #endif
  1953.     rc = ttol((CHAR *)str_data, p - str_data);
  1954. #ifdef OS2
  1955.     ReleaseTelnetMutex();
  1956. #endif
  1957.     return(rc);
  1958. }
  1959.  
  1960. #ifdef CK_ENCRYPTION
  1961. /*
  1962.  * Function: Enable or disable the encryption process.
  1963.  *
  1964.  * Parameters:
  1965.  *      enable - TRUE to enable, FALSE to disable.
  1966.  */
  1967. static VOID
  1968. #ifdef CK_ANSIC
  1969. auth_encrypt_enable(BOOL enable)
  1970. #else
  1971. auth_encrypt_enable(enable) BOOL enable;
  1972. #endif
  1973. {
  1974.   encrypt_flag = enable;
  1975. }
  1976. #endif
  1977.  
  1978. /*
  1979.  * Function: Abort the authentication process
  1980.  *
  1981.  * Parameters:
  1982.  */
  1983. static VOID
  1984. #ifdef CK_ANSIC
  1985. auth_abort(char *errmsg, long r)
  1986. #else
  1987. auth_abort(errmsg,r) char *errmsg; long r;
  1988. #endif
  1989. {
  1990.     char buf[9];
  1991.     extern int sstelnet;
  1992.  
  1993. #ifdef CK_SSL
  1994.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  1995.         return;
  1996.     }
  1997. #endif /* CK_SSL */
  1998.     debug(F111,"auth_abort",errmsg,r);
  1999.  
  2000.     /* Construct Telnet Debugging messages */
  2001.     if (deblog || tn_deb || debses) {
  2002.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2003.                   "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2004.                   " IS ",AUTHTYPE_NAME(AUTHTYPE_NULL)," ",
  2005.                    AUTHTYPE_NAME(AUTHTYPE_NULL)," IAC SE",
  2006.                    NULL,NULL,NULL,NULL,NULL
  2007.                  );
  2008.         debug(F100,tn_msg,"",0);
  2009.         if (tn_deb || debses) tn_debug(tn_msg);
  2010.     }
  2011.  
  2012.     /* Construct the Abort message to send to the host   */
  2013.     /* Basicly we change the authentication type to NULL */
  2014.     sprintf(buf, "%c%c%c%c%c%c%c%c", IAC, SB, TELOPT_AUTHENTICATION,
  2015.              sstelnet ? TELQUAL_REPLY : TELQUAL_IS, AUTHTYPE_NULL,
  2016.              AUTHTYPE_NULL, IAC, SE);   /* safe */
  2017. #ifdef OS2
  2018.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2019. #endif
  2020.     ttol((CHAR *)buf, 8);
  2021. #ifdef OS2
  2022.     ReleaseTelnetMutex();
  2023. #endif
  2024.  
  2025.     /* If there is an error message, and error number construct */
  2026.     /* an explanation to display to the user                    */
  2027.     if (errmsg != NULL) {
  2028.         ckstrncpy(strTmp, errmsg, AUTHTMPBL);
  2029.     } else
  2030.         strTmp[0] = '\0';
  2031.  
  2032.  
  2033.     if (r != AUTH_SUCCESS) {
  2034.         ckstrncat(strTmp, "\r\n",AUTHTMPBL);
  2035. #ifdef KRB4
  2036.         if ( authentication_version == AUTHTYPE_KERBEROS_V4 ) {
  2037.             ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),
  2038.                        AUTHTMPBL);
  2039.             debug(F111,"auth_abort",(char *)krb_get_err_text_entry(r),r);
  2040.         }
  2041. #endif
  2042. #ifdef KRB5
  2043.         if ( authentication_version == AUTHTYPE_KERBEROS_V5 ) {
  2044.             ckstrncat(strTmp, error_message(r),AUTHTMPBL);
  2045.             debug(F111,"auth_abort",error_message(r),r);
  2046.         }
  2047. #endif
  2048.     }
  2049.     printf("Authentication failed: %s\r\n",strTmp);
  2050. #ifdef CKSYSLOG
  2051.     if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  2052.         cksyslog(SYSLG_LI, 0, "Telnet authentication failure",
  2053.                   (char *) szUserNameRequested,
  2054.                   strTmp);
  2055.     }
  2056. #endif /* CKSYSLOG */
  2057.     authentication_version = AUTHTYPE_NULL;
  2058. }
  2059.  
  2060.  
  2061. /*
  2062.  * Function: Copy data to buffer, doubling IAC character if present.
  2063.  *
  2064.  */
  2065. int
  2066. #ifdef CK_ANSIC
  2067. copy_for_net(unsigned char *to, unsigned char *from, int c)
  2068. #else
  2069. copy_for_net(to,from,c) unsigned char *to; unsigned char *from; int c;
  2070. #endif
  2071. {
  2072.     int n;
  2073.  
  2074.     n = c;
  2075.     debug(F111,"copy_for_net","before",n);
  2076.     while (c-- > 0) {
  2077.         if ((*to++ = *from++) == IAC) {
  2078.             n++;
  2079.             *to++ = IAC;
  2080.         }
  2081.     }
  2082.     debug(F111,"copy_for_net","after",n);
  2083.     return n;
  2084. }
  2085.  
  2086. #ifdef CK_SSL
  2087. /*  S E N D S S L A U T H S B
  2088.  *  Send a SSL Authentication Subnegotiation to host and
  2089.  *  output appropriate Telnet Debug messages
  2090.  *
  2091.  *  type - Sub Negotiation type
  2092.  *  data - ptr to buffer containing data
  2093.  *  len  - len of buffer if not NUL terminated
  2094.  *
  2095.  *  returns number of characters sent or error value
  2096.  */
  2097.  
  2098. int
  2099. #ifdef CK_ANSIC
  2100. SendSSLAuthSB(int type, void *data, int len)
  2101. #else
  2102. SendSSLAuthSB(type,data,len) int type; void *data; int len;
  2103. #endif
  2104. {
  2105.     int rc;
  2106.     unsigned char *p = str_data + 3;
  2107.     unsigned char *cd = (unsigned char *)data;
  2108.     extern int sstelnet;
  2109.  
  2110.     /* Check for invalid values */
  2111.     if ( type != SSL_START && type != SSL_ACCEPT &&
  2112.          type != SSL_REJECT)
  2113.         return(0);
  2114.  
  2115.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) {
  2116.         if (ttchk() < 0)
  2117.           return(0);
  2118.         else
  2119.           return(1);
  2120.     }
  2121.  
  2122.     if (len == -1)                        /* Use strlen() for len */
  2123.         len = strlen((char *)cd);
  2124.  
  2125.     /* Construct Message */
  2126.     *p++ = sstelnet ? TELQUAL_REPLY : TELQUAL_IS;
  2127.     *p++ = AUTHTYPE_SSL;
  2128.     *p = AUTH_CLIENT_TO_SERVER;
  2129.     *p |= auth_how;
  2130. #ifdef CK_ENCRYPTION
  2131.     *p |= auth_crypt;
  2132. #endif
  2133.     p++;
  2134.     *p++ = type;
  2135.     while (len-- > 0) {
  2136.         if ((*p++ = *cd++) == IAC)
  2137.             *p++ = IAC;
  2138.         }
  2139.     *p++ = IAC;
  2140.     *p++ = SE;
  2141.  
  2142.     /* Handle Telnet Debugging Messages */
  2143.     if (deblog || tn_deb || debses) {
  2144.         int i;
  2145.         int deblen=p-str_data-2;
  2146.         char *s=NULL;
  2147.         int mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) |
  2148.             (auth_crypt?AUTH_ENCRYPT_USING_TELOPT:AUTH_ENCRYPT_OFF);
  2149.  
  2150.         switch (type) {
  2151.         case SSL_START:
  2152.             s = "START";
  2153.             break;
  2154.         case SSL_ACCEPT:
  2155.             s = "ACCEPT";
  2156.             break;
  2157.         case SSL_REJECT:
  2158.             s = "REJECT";
  2159.             break;
  2160.         }
  2161.  
  2162.         ckmakxmsg(tn_msg,TN_MSG_LEN,
  2163.                   "TELNET SENT SB ",
  2164.                  TELOPT(TELOPT_AUTHENTICATION)," ",
  2165.                  str_data[3] == TELQUAL_REPLY ? "REPLY" :
  2166.                  str_data[3] == TELQUAL_IS ? "IS" : "???"," ",
  2167.                  AUTHTYPE_NAME(authentication_version)," ",
  2168.                  AUTHMODE_NAME(mode)," ",
  2169.                  s," ",NULL);
  2170.         tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_data[7],deblen-7);
  2171.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2172.         debug(F100,tn_msg,"",0);
  2173.         if (tn_deb || debses) tn_debug(tn_msg);
  2174.     }
  2175.  
  2176.     /* Send data */
  2177. #ifdef OS2
  2178.     RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2179. #endif
  2180.     rc = ttol((CHAR *)str_data, p - str_data);
  2181. #ifdef OS2
  2182.     ReleaseTelnetMutex();
  2183. #endif
  2184.     return(rc);
  2185. }
  2186. #endif  /* CK_SSL */
  2187.  
  2188. int
  2189. tn_how_ok(int how)
  2190. {
  2191.     switch ( tn_auth_how ) {
  2192.     case TN_AUTH_HOW_ANY:
  2193.         return(1);
  2194.     case TN_AUTH_HOW_ONE_WAY:
  2195.         return((how & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY);
  2196.     case TN_AUTH_HOW_MUTUAL:
  2197.         return((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL);
  2198.     default:
  2199.         return(0);
  2200.     }
  2201. }
  2202.  
  2203. int
  2204. tn_enc_ok(int enc)
  2205. {
  2206.     switch ( tn_auth_enc ) {
  2207.     case TN_AUTH_ENC_ANY:
  2208.         if ((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  2209.             (!ck_ssleay_is_installed
  2210. #ifdef CK_SSL
  2211.              || !ssl_finished_messages ||
  2212.              !(tls_active_flag || ssl_active_flag)
  2213. #endif /* CK_SSL */
  2214.              )) {
  2215. #ifdef CK_SSL
  2216.             if (!ssl_finished_messages)
  2217.                 debug(F100,"tn_enc_ok !ssl_finished_messages","",0);
  2218. #endif /* CK_SSL */
  2219.             return(0);
  2220.         }
  2221.         return(1);
  2222.     case TN_AUTH_ENC_NONE:
  2223.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_OFF);
  2224.     case TN_AUTH_ENC_TELOPT:
  2225.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT);
  2226.     case TN_AUTH_ENC_EXCH:
  2227.         return((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_AFTER_EXCHANGE);
  2228.     case TN_AUTH_ENC_TLS:
  2229.         return(((enc & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) &&
  2230.                ck_ssleay_is_installed()
  2231. #ifdef CK_SSL
  2232.                && ssl_finished_messages &&
  2233.                (tls_active_flag || ssl_active_flag)
  2234. #endif /* CK_SSL */
  2235.            );
  2236.     default:
  2237.         return(0);
  2238.     }
  2239. }
  2240.  
  2241. static int
  2242. atok(int at) {
  2243.     int i;
  2244.     if ( auth_type_user[0] == AUTHTYPE_AUTO )
  2245.         return(1);
  2246.     if ( auth_type_user[0] == AUTHTYPE_NULL )
  2247.         return(0);
  2248.  
  2249.     for ( i=0;
  2250.           i<AUTHTYPLSTSZ && auth_type_user[i] != AUTHTYPE_NULL;
  2251.           i++ ) {
  2252.         if ( auth_type_user[i] == at )
  2253.             return(1);
  2254.     }
  2255.  
  2256.     return(0);
  2257. }
  2258.  
  2259.  
  2260. /*
  2261.  * Function: Parse authentication send command
  2262.  *
  2263.  * Parameters:
  2264.  *  parsedat - the sub-command data.
  2265.  *
  2266.  *      end_sub - index of the character in the 'parsedat' array which
  2267.  *              is the last byte in a sub-negotiation
  2268.  *
  2269.  * Returns: Kerberos error code.
  2270.  */
  2271.  
  2272. static unsigned char send_list[512];
  2273. static int  send_len = 0;
  2274.  
  2275. _PROTOTYP(static int auth_send, (unsigned char *parsedat, int end_sub));
  2276.  
  2277. static int
  2278. #ifdef CK_ANSIC
  2279. auth_resend(int type)
  2280. #else
  2281. auth_resend(type) int type;
  2282. #endif /* CK_ANSIC */
  2283. {
  2284.     int i=2;
  2285.     while (i+1 <= send_len) {
  2286.         if (send_list[i] == type) {
  2287.             int j;
  2288.             send_len -= 2;
  2289.             for (j = i; j < send_len; j++)
  2290.                 send_list[j] = send_list[j+2];
  2291.         } else {
  2292.             i += 2;
  2293.         }
  2294.     }
  2295.     return(auth_send(send_list,send_len));
  2296. }
  2297.  
  2298. static int
  2299. #ifdef CK_ANSIC
  2300. auth_send(unsigned char *parsedat, int end_sub)
  2301. #else
  2302. auth_send(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  2303. #endif
  2304. {
  2305.     static unsigned char buf[4096];
  2306.     unsigned char *pname;
  2307.     int plen;
  2308.     int r;
  2309.     int i;
  2310.     int mode;
  2311. #ifdef MIT_CURRENT
  2312. #ifdef CK_ENCRYPTION
  2313.     krb5_data data;
  2314.     krb5_enc_data encdata;
  2315.     krb5_error_code code;
  2316.     krb5_keyblock random_key;
  2317. #endif /* ENCRYPTION */
  2318. #endif /* MIT_CURRENT */
  2319. #ifdef KRB5
  2320.     int krb5_msg = 0;
  2321. #endif /* KRB5 */
  2322. #ifdef KRB4
  2323.     int krb4_msg = 0;
  2324. #endif /* KRB4 */
  2325. #ifdef GSSAPI_KRB5
  2326.     int gssk5_msg = 0;
  2327. #endif /* GSSAPI_KRB5 */
  2328.     int iaccnt=0;
  2329.  
  2330. #ifdef CK_SSL
  2331.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2332.         return(AUTH_SUCCESS);
  2333. #endif /* CK_SSL */
  2334.  
  2335.     auth_how = -1;              /* We have not found an auth method  */
  2336.     auth_crypt = 0;             /* We are not using encryption (yet) */
  2337.     send_len = end_sub > 512 ? 512 : end_sub;
  2338.     memcpy(send_list,parsedat,send_len);
  2339.  
  2340.     /* Search the list of acceptable Authentication types sent from */
  2341.     /* the host and find one that we support                        */
  2342.  
  2343.     /* For Kerberos authentications, try to determine if we have a  */
  2344.     /* valid TGT, if not skip over the authentication type because  */
  2345.     /* we wouldn't be able to successfully login anyway.  Perhaps   */
  2346.     /* there is another supported authentication which we could use */
  2347.  
  2348. #ifdef NO_FTP_AUTH
  2349.     /* If the userid is "ftp" or "anonymous" refuse to perform AUTH */
  2350.     /* for Kerberos or SRP.                                         */
  2351. #endif /* NO_FTP_AUTH */
  2352.  
  2353.     if ( auth_type_user[0] == AUTHTYPE_AUTO ) {
  2354.     for (i = 2; i+1 <= end_sub; i += 2) {
  2355. #ifdef NTLM
  2356.         if (parsedat[i] == AUTHTYPE_NTLM &&
  2357.              ck_ntlm_is_valid(1) &&
  2358.              ntlm_auth_send() == 0) {
  2359.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2360.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2361. #ifdef CK_ENCRYPTION
  2362.                 /* NTLM does not support Telnet Encryption */
  2363.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2364.                     continue;
  2365.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2366. #endif /* CK_ENCRYPTION */
  2367.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2368.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2369.                 authentication_version = AUTHTYPE_NTLM;
  2370.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2371.                 break;
  2372.             }
  2373.         }
  2374. #endif /* NTLM */
  2375. #ifdef CK_SSL
  2376.         if ( parsedat[i] == AUTHTYPE_SSL && ssl_initialized &&
  2377. #ifdef SSLDLL
  2378.              ck_ssleay_is_installed() &&
  2379. #endif /* SSLDLL */
  2380.              !tls_active_flag && !ssl_active_flag
  2381. #ifndef USE_CERT_CB
  2382.              && tls_load_certs(ssl_ctx,ssl_con,0)
  2383. #endif /* USE_CERT_CB */
  2384.              ) {
  2385.  
  2386.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2387.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2388. #ifdef CK_ENCRYPTION
  2389.                 /* SSL does not support Telnet Encryption */
  2390.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2391.                     continue;
  2392.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2393. #endif /* CK_ENCRYPTION */
  2394.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2395.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2396.                 authentication_version = AUTHTYPE_SSL;
  2397.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2398.                 break;
  2399.             }
  2400.         }
  2401. #endif /* SSL */
  2402. #ifdef CK_SRP
  2403.         if ( parsedat[i] == AUTHTYPE_SRP
  2404. #ifdef SRPDLL
  2405.              && hSRP
  2406. #endif /* SRPDLL */
  2407. #ifdef NO_FTP_AUTH
  2408.              && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2409. #endif /* NO_FTP_AUTH */
  2410.              ) {
  2411.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2412.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2413. #ifdef PRE_SRP_1_4_5
  2414.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2415.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2416.                     continue;
  2417. #endif /* PRE_SRP_1_4_5 */
  2418.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2419.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2420.                     (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2421.                      TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2422.                     continue;
  2423.  
  2424.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2425. #ifdef CK_ENCRYPTION
  2426.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2427.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2428.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2429.                 }
  2430. #endif /* CK_ENCRYPTION */
  2431. #ifdef CK_SSL
  2432.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2433.                      ck_ssleay_is_installed() &&
  2434.                      (tls_active_flag || ssl_active_flag) ) {
  2435.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2436.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2437.                 }
  2438. #endif /* CK_SSL */
  2439.                 authentication_version = AUTHTYPE_SRP;
  2440.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2441.                 break;
  2442.             }
  2443.         }
  2444. #endif /* SRP */
  2445. #ifdef GSSAPI_KRB5
  2446.         if (parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2447.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2448.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2449. #ifdef OS2
  2450.             hGSSAPI &&
  2451. #endif /* OS2 */
  2452. #ifdef NO_FTP_AUTH
  2453.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2454. #endif /* NO_FTP_AUTH */
  2455.              ck_gssapi_is_installed() && !gssk5_msg)
  2456.         {
  2457.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2458.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2459.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2460.             {
  2461.                 /* If we are auto-getting TGTs, try */
  2462.                 if ( !ck_krb5_is_tgt_valid() ) {
  2463.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2464.                 }
  2465.                 gssk5_msg = 1;
  2466.             }
  2467.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2468.                       AUTH_CLIENT_TO_SERVER &&
  2469.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2470.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2471. #ifdef CK_ENCRYPTION
  2472.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2473.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2474.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2475.                 }
  2476. #endif /* CK_ENCRYPTION */
  2477.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2478.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2479.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2480.                 break;
  2481.             }
  2482.         }
  2483. #endif /* GSSAPI_KRB5 */
  2484. #ifdef KRB5
  2485.         if (parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2486. #ifdef OS2
  2487.              hKRB5_32 &&
  2488. #endif /* OS2 */
  2489. #ifdef NO_FTP_AUTH
  2490.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2491. #endif /* NO_FTP_AUTH */
  2492.              ck_krb5_is_installed() && !krb5_msg) {
  2493.  
  2494.             /* Without encryption we can't perform mutual authentication */
  2495.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2496.                  !ck_crypt_is_installed())
  2497.                 continue;
  2498.  
  2499.             /* Skip over entries that request credential forwarding */
  2500.             /* if we are not forwarding.                            */
  2501.             if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2502.                 (forward_flag &&
  2503.                   ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2504.                 continue;
  2505.  
  2506.             if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2507.                                 parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2508.                                 parsedat[i+1] & INI_CRED_FWD_MASK) )
  2509.             {
  2510.                 /* If we are auto-getting TGTs, try */
  2511.                 if ( !ck_krb5_is_tgt_valid() ) {
  2512.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2513.                 }
  2514.                 krb5_msg = 1;
  2515.             }
  2516.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2517.                       AUTH_CLIENT_TO_SERVER &&
  2518.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2519.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2520.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2521.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2522.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2523.                     continue;
  2524.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2525.                      AUTH_ENCRYPT_START_TLS) &&
  2526.                      (!ck_ssleay_is_installed()
  2527. #ifdef CK_SSL
  2528.                        || !(tls_active_flag || ssl_active_flag)
  2529. #endif /* CK_SSL */
  2530.                        ))
  2531.                     continue;
  2532.  
  2533.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2534. #ifdef CK_ENCRYPTION
  2535.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2536.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2537.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2538.                 }
  2539. #endif /* CK_ENCRYPTION */
  2540. #ifdef CK_SSL
  2541.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2542.                      ck_ssleay_is_installed() &&
  2543.                      (tls_active_flag || ssl_active_flag) ) {
  2544.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2545.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2546.                 }
  2547. #endif /* CK_SSL */
  2548.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2549.                 authentication_version = AUTHTYPE_KERBEROS_V5;
  2550.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2551.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2552.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2553.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2554.                 }
  2555.                 break;
  2556.             }
  2557.         }
  2558. #endif /* KRB5 */
  2559. #ifdef KRB4
  2560.         if (parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2561. #ifdef OS2
  2562.              hKRB4_32 &&
  2563. #endif /* OS2 */
  2564. #ifdef NO_FTP_AUTH
  2565.              strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2566. #endif /* NO_FTP_AUTH */
  2567.              ck_krb4_is_installed() && !krb4_msg) {
  2568.             int rc = 0;
  2569.  
  2570.             /* Without encryption we can't perform mutual authentication */
  2571.             if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2572.                  !ck_crypt_is_installed() )
  2573.                 continue;
  2574.  
  2575.             if ( !k4_auth_send() )
  2576.             {
  2577.                 /* If we are auto-getting TGTs, try */
  2578.                 if ( !ck_krb4_is_tgt_valid() ) {
  2579.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2580.                 }
  2581.                 krb4_msg = 1;
  2582.             }
  2583.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2584.                       AUTH_CLIENT_TO_SERVER &&
  2585.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2586. #ifdef CK_ENCRYPTION
  2587.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2588.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2589.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2590.                     continue;
  2591.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2592.                 if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2593.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2594.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2595.                 }
  2596. #endif /* CK_ENCRYPTION */
  2597.                 authentication_version = AUTHTYPE_KERBEROS_V4;
  2598.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2599.                 if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2600.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2601.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2602.                 }
  2603.                 break;
  2604.             }
  2605.         }
  2606. #endif /* KRB4 */
  2607.     }
  2608.     } else {
  2609.         for (i = 2; i+1 <= end_sub; i += 2) {
  2610. #ifdef CK_SSL
  2611.             if ( atok(AUTHTYPE_SSL) && parsedat[i] == AUTHTYPE_SSL &&
  2612. #ifdef SSLDLL
  2613.                  ck_ssleay_is_installed() &&
  2614. #endif /* SSLDLL */
  2615.                  !tls_active_flag && !ssl_active_flag && ssl_initialized
  2616. #ifndef USE_CERT_CB
  2617.                  && tls_load_certs(ssl_ctx,ssl_con,0)
  2618. #endif /* USE_CERT_CB */
  2619.                  )
  2620.             {
  2621.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2622.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2623. #ifdef CK_ENCRYPTION
  2624.                     /* SSL does not support Telnet Encryption */
  2625.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2626.                         continue;
  2627.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2628. #endif /* CK_ENCRYPTION */
  2629.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2630.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2631.                     authentication_version = AUTHTYPE_SSL;
  2632.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2633.                     break;
  2634.                 }
  2635.             }
  2636. #endif /* SSL */
  2637. #ifdef CK_SRP
  2638.             if ( atok(AUTHTYPE_SRP) &&
  2639.                  parsedat[i] == AUTHTYPE_SRP
  2640. #ifdef SRPDLL
  2641.                  && hSRP
  2642. #endif /* SRPDLL */
  2643. #ifdef NO_FTP_AUTH
  2644.                  && strcmp("ftp",szUserName) && strcmp("anonymous",szUserName)
  2645. #endif /* NO_FTP_AUTH */
  2646.                  ) {
  2647.                 if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2648.                      tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2649. #ifdef PRE_SRP_1_4_5
  2650.                 if (parsedat[i+1] & AUTH_ENCRYPT_MASK)
  2651.                      /* Do not support ENCRYPT_USING_TELOPT yet. */
  2652.                     continue;
  2653. #endif /* PRE_SRP_1_4_5 */
  2654.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2655.                      AUTH_ENCRYPT_USING_TELOPT) &&
  2656.                      (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2657.                        TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2658.                     continue;
  2659.                 if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2660.                      AUTH_ENCRYPT_START_TLS) &&
  2661.                      (!ck_ssleay_is_installed()
  2662. #ifdef CK_SSL
  2663.                        || !(tls_active_flag || ssl_active_flag)
  2664. #endif /* CK_SSL */
  2665.                        ))
  2666.                     continue;
  2667.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2668. #ifdef CK_ENCRYPTION
  2669.                     if ( auth_crypt == AUTH_ENCRYPT_USING_TELOPT ) {
  2670.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2671.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2672.                     }
  2673. #endif /* CK_ENCRYPTION */
  2674. #ifdef CK_SSL
  2675.                 if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2676.                      ck_ssleay_is_installed() &&
  2677.                      (tls_active_flag || ssl_active_flag) ) {
  2678.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2679.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2680.                 }
  2681. #endif /* CK_SSL */
  2682.                     authentication_version = AUTHTYPE_SRP;
  2683.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2684.                     break;
  2685.                 }
  2686.             }
  2687. #endif /* SRP */
  2688. #ifdef GSSAPI_KRB5
  2689.         if (atok(AUTHTYPE_GSSAPI_KRB5) &&
  2690.             parsedat[i] == AUTHTYPE_GSSAPI_KRB5 &&
  2691.             (parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2692.             AUTH_ENCRYPT_AFTER_EXCHANGE &&
  2693. #ifdef OS2
  2694.             hGSSAPI &&
  2695. #endif /* OS2 */
  2696. #ifdef NO_FTP_AUTH
  2697.             strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2698. #endif /* NO_FTP_AUTH */
  2699.             ck_gssapi_is_installed() && !gssk5_msg)
  2700.         {
  2701.             if ( !gssk5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2702.                                   parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2703.                                   parsedat[i+1] & INI_CRED_FWD_MASK) )
  2704.             {
  2705.                 /* If we are auto-getting TGTs, try */
  2706.                 if ( !ck_krb5_is_tgt_valid() ) {
  2707.                 printf("Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2708.                 }
  2709.                 gssk5_msg = 1;
  2710.             }
  2711.             else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2712.                       AUTH_CLIENT_TO_SERVER &&
  2713.                       tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2714.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2715. #ifdef CK_ENCRYPTION
  2716.                 if ( auth_crypt == AUTH_ENCRYPT_AFTER_EXCHANGE ) {
  2717.                     TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2718.                     TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2719.                 }
  2720. #endif /* CK_ENCRYPTION */
  2721.                 auth_fwd = parsedat[i+1] & INI_CRED_FWD_MASK;
  2722.                 authentication_version = AUTHTYPE_GSSAPI_KRB5;
  2723.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2724.                 break;
  2725.             }
  2726.         }
  2727. #endif /* GSSAPI_KRB5 */
  2728. #ifdef KRB5
  2729.             if ( atok(AUTHTYPE_KERBEROS_V5) &&
  2730.                  parsedat[i] == AUTHTYPE_KERBEROS_V5 &&
  2731. #ifdef OS2
  2732.                  hKRB5_32 &&
  2733. #endif /* OS2 */
  2734. #ifdef NO_FTP_AUTH
  2735.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2736. #endif /* NO_FTP_AUTH */
  2737.                  ck_krb5_is_installed() && !krb5_msg) {
  2738.  
  2739.                 /* Without encryption we can't perform mutual authentication */
  2740.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2741.                      !ck_crypt_is_installed())
  2742.                     continue;
  2743.  
  2744.                 /* Skip over entries that request credential forwarding */
  2745.                 /* if we are not forwarding.                            */
  2746.                 if ((!forward_flag && (parsedat[i+1] & INI_CRED_FWD_MASK)) ||
  2747.                      (forward_flag &&
  2748.                        ((parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY)))
  2749.                     continue;
  2750.  
  2751.                 if ( !k5_auth_send(parsedat[i+1] & AUTH_HOW_MASK,
  2752.                                     parsedat[i+1] & AUTH_ENCRYPT_MASK,
  2753.                                     parsedat[i+1] & INI_CRED_FWD_MASK) )
  2754.                 {
  2755.                     /* If we are auto-getting TGTs, try */
  2756.                     if ( !ck_krb5_is_tgt_valid() ) {
  2757.                         printf(
  2758.                            "Kerberos 5: Ticket Getting Ticket not valid.\r\n");
  2759.                     }
  2760.                     krb5_msg = 1;
  2761.                 }
  2762.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2763.                           AUTH_CLIENT_TO_SERVER &&
  2764.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2765.                 {
  2766.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2767.                          AUTH_ENCRYPT_USING_TELOPT) &&
  2768.                          (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2769.                            TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2770.                         continue;
  2771.                     if (((parsedat[i+1] & AUTH_ENCRYPT_MASK) ==
  2772.                          AUTH_ENCRYPT_START_TLS) &&
  2773.                          (!ck_ssleay_is_installed()
  2774. #ifdef CK_SSL
  2775.                            || !(tls_active_flag || ssl_active_flag)
  2776. #endif /* CK_SSL */
  2777.                            ))
  2778.                         continue;
  2779.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2780. #ifdef CK_ENCRYPTION
  2781.                     if (auth_crypt) {
  2782.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2783.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2784.                     }
  2785. #endif /* CK_ENCRYPTION */
  2786. #ifdef CK_SSL
  2787.                     if ( auth_crypt == AUTH_ENCRYPT_START_TLS &&
  2788.                          ck_ssleay_is_installed() &&
  2789.                          (tls_active_flag || ssl_active_flag) ) {
  2790.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2791.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2792.                     }
  2793. #endif /* CK_SSL */
  2794.                     authentication_version = AUTHTYPE_KERBEROS_V5;
  2795.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2796.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2797.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2798.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2799.                     }
  2800.                     break;
  2801.                 }
  2802.             }
  2803. #endif /* KRB5 */
  2804. #ifdef KRB4
  2805.             if ( atok(AUTHTYPE_KERBEROS_V4) &&
  2806.                  parsedat[i] == AUTHTYPE_KERBEROS_V4 &&
  2807. #ifdef OS2
  2808.                  hKRB4_32 &&
  2809. #endif /* OS2 */
  2810. #ifdef NO_FTP_AUTH
  2811.                  strcmp("ftp",szUserName) && strcmp("anonymous",szUserName) &&
  2812. #endif /* NO_FTP_AUTH */
  2813.                  ck_krb4_is_installed() && !krb4_msg) {
  2814.                 int rc = 0;
  2815.  
  2816.                 /* Without encryption we can't perform mutual authentication */
  2817.                 if ( (parsedat[i+1] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
  2818.                      !ck_crypt_is_installed())
  2819.                     continue;
  2820.  
  2821.                 if ( !k4_auth_send() )
  2822.                 {
  2823.                     /* If we are auto-getting TGTs, try */
  2824.                     if ( !ck_krb4_is_tgt_valid() ) {
  2825.                     printf("Kerberos 4: Ticket Getting Ticket not valid.\r\n");
  2826.                     }
  2827.                     krb4_msg = 1;
  2828.                 }
  2829.                 else if ((parsedat[i+1] & AUTH_WHO_MASK) ==
  2830.                           AUTH_CLIENT_TO_SERVER &&
  2831.                           tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1]))
  2832.                 {
  2833. #ifdef CK_ENCRYPTION
  2834.                     if ((parsedat[i+1] & AUTH_ENCRYPT_MASK) &&
  2835.                         (TELOPT_ME_MODE(TELOPT_ENCRYPTION) == TN_NG_RF ||
  2836.                          TELOPT_U_MODE(TELOPT_ENCRYPTION) == TN_NG_RF))
  2837.                       continue;
  2838.                     auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2839.                     if (auth_crypt) {
  2840.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2841.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_MU;
  2842.                     }
  2843. #endif /* CK_ENCRYPTION */
  2844.                     authentication_version = AUTHTYPE_KERBEROS_V4;
  2845.                     auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2846.                     if ( auth_how == AUTH_HOW_ONE_WAY ) {
  2847.                         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2848.                         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2849.                     }
  2850.                     break;
  2851.                 }
  2852.             }
  2853. #endif /* KRB4 */
  2854. #ifdef NTLM
  2855.         if ( atok(AUTHTYPE_NTLM) &&
  2856.              parsedat[i] == AUTHTYPE_NTLM &&
  2857.              ck_ntlm_is_valid(1) &&
  2858.              ntlm_auth_send() == 0) {
  2859.             if ((parsedat[i+1] & AUTH_WHO_MASK) == AUTH_CLIENT_TO_SERVER &&
  2860.                  tn_how_ok(parsedat[i+1]) && tn_enc_ok(parsedat[i+1])) {
  2861. #ifdef CK_ENCRYPTION
  2862.                 /* NTLM does not support Telnet Encryption */
  2863.                 if ((parsedat[i+1] & AUTH_ENCRYPT_MASK))
  2864.                     continue;
  2865.                 auth_crypt = parsedat[i+1] & AUTH_ENCRYPT_MASK;
  2866. #endif /* CK_ENCRYPTION */
  2867.                 TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2868.                 TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  2869.                 authentication_version = AUTHTYPE_NTLM;
  2870.                 auth_how = parsedat[i+1] & AUTH_HOW_MASK;
  2871.                 break;
  2872.             }
  2873.         }
  2874. #endif /* NTLM */
  2875.         }
  2876.     }
  2877.  
  2878.     if (auth_how == -1) {               /* Did we find one? */
  2879.         switch ( auth_type_user[0] ) {  /* If not, abort the negotiation */
  2880.         case AUTHTYPE_NULL:
  2881.             auth_abort("User refused to accept any authentication method",0);
  2882.             break;
  2883.         case AUTHTYPE_AUTO:
  2884.             auth_abort("No authentication method available", 0);
  2885.             break;
  2886.         default: {
  2887.             char msg[80];
  2888.             ckmakmsg(msg,80,AUTHTYPE_NAME(auth_type_user[0]),
  2889.                       " could not be negotiated",NULL,NULL
  2890.                      );
  2891.             auth_abort(msg, 0);
  2892.         }
  2893.         }
  2894.         auth_finished(AUTH_REJECT);
  2895.         return AUTH_FAILURE;
  2896.     }
  2897.  
  2898.     printf("Authenticating with %s\r\n",
  2899.             AUTHTYPE_NAME(authentication_version));
  2900.  
  2901.     /* Send Telnet Auth Name message (if necessary) */
  2902.     switch ( authentication_version ) {
  2903.     case AUTHTYPE_SRP:
  2904.     case AUTHTYPE_KERBEROS_V4:
  2905.     case AUTHTYPE_KERBEROS_V5:
  2906.     case AUTHTYPE_GSSAPI_KRB5:
  2907.         /* if we do not have a name to login with get one now. */
  2908.         while ( szUserName[0] == '\0' ) {
  2909.             extern char * tn_pr_uid;
  2910.             int ok = uq_txt(NULL,
  2911.                      tn_pr_uid && tn_pr_uid[0] ? tn_pr_uid : "Host Userid: ",
  2912.                             1, NULL, szUserName, 63, NULL);
  2913.             if ( !ok )
  2914.                 return AUTH_FAILURE;
  2915.         }
  2916.         plen = strlen(szUserName);
  2917.         pname = (unsigned char *) szUserName;
  2918.  
  2919.         /* Construct Telnet Debugging Message */
  2920.         if (deblog || tn_deb || debses) {
  2921.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2922.                        "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2923.                        " NAME ",(char *)pname," IAC SE",NULL,
  2924.                        NULL,NULL,NULL,NULL,NULL,NULL
  2925.                      );
  2926.             debug(F100,tn_msg,"",0);
  2927.             if (tn_deb || debses) tn_debug(tn_msg);
  2928.         }
  2929.  
  2930.         /* Construct and send Authentication Name subnegotiation */
  2931.         if ( plen < sizeof(buf) - 6 ) {
  2932.             sprintf((char *)buf, "%c%c%c%c", IAC, SB, 
  2933.                      TELOPT_AUTHENTICATION,
  2934.                      TELQUAL_NAME);
  2935.             memcpy(&buf[4], pname, plen);               /* safe */
  2936.             sprintf((char *)&buf[plen + 4], "%c%c", IAC, SE);   /* safe */
  2937. #ifdef OS2
  2938.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2939. #endif
  2940.             ttol((CHAR *)buf, plen+6);
  2941. #ifdef OS2
  2942.             ReleaseTelnetMutex();
  2943. #endif
  2944.         } else {
  2945.             sprintf((char *)buf, "%c%c%c%c%c%c", IAC, SB, 
  2946.                      TELOPT_AUTHENTICATION,
  2947.                      TELQUAL_NAME, IAC, SE);    /* safe */
  2948. #ifdef OS2
  2949.             RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2950. #endif
  2951.             ttol((CHAR *)buf, 6);
  2952. #ifdef OS2
  2953.             ReleaseTelnetMutex();
  2954. #endif
  2955.         }
  2956.     }
  2957.  
  2958.     /* Construct Authentication Mode subnegotiation message (if necessary) */
  2959.     switch ( authentication_version ) {
  2960.     case AUTHTYPE_SRP:
  2961.     case AUTHTYPE_KERBEROS_V4:
  2962.     case AUTHTYPE_KERBEROS_V5:
  2963.     case AUTHTYPE_GSSAPI_KRB5:
  2964.     case AUTHTYPE_NTLM:
  2965.         mode = AUTH_CLIENT_TO_SERVER | (auth_how & AUTH_HOW_MASK) | auth_crypt
  2966. #ifdef USE_INI_CRED_FWD
  2967.                | (((authentication_version == AUTHTYPE_KERBEROS_V5) &&
  2968.                   auth_fwd)?INI_CRED_FWD_ON:INI_CRED_FWD_OFF)
  2969. #endif /* USE_INI_CRED_FWD */
  2970.                ;
  2971.         sprintf((char *)buf, "%c%c%c%c%c%c%c",
  2972.                  IAC, SB, TELOPT_AUTHENTICATION,
  2973.                  TELQUAL_IS,
  2974.                  authentication_version,
  2975.                  mode,
  2976.                  KRB_AUTH);     /* safe */
  2977.         break;
  2978.     }
  2979.  
  2980.     /* Send initial authentication data */
  2981.     switch ( authentication_version ) {
  2982. #ifdef CK_SSL
  2983.     case AUTHTYPE_SSL:
  2984.         SendSSLAuthSB(SSL_START,NULL,0);
  2985.         break;
  2986. #endif /* SSL */
  2987. #ifdef CK_SRP
  2988.     case AUTHTYPE_SRP:
  2989.         sprintf(&buf[7], "%c%c", IAC, SE);      /* safe */
  2990.         if (deblog || tn_deb || debses) {
  2991.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  2992.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  2993.                       " IS ",AUTHTYPE_NAME(authentication_version),
  2994.                       " AUTH ",AUTHMODE_NAME(mode)," IAC SE",
  2995.                       NULL,NULL,NULL,NULL,NULL
  2996.                      );
  2997.             debug(F100,tn_msg,"",0);
  2998.             if (tn_deb || debses) tn_debug(tn_msg);
  2999.         }
  3000. #ifdef OS2
  3001.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3002. #endif
  3003.         ttol((CHAR *)buf, 9);
  3004. #ifdef OS2
  3005.         ReleaseTelnetMutex();
  3006. #endif
  3007.         break;
  3008. #endif /* SRP */
  3009. #ifdef NTLM
  3010.     case AUTHTYPE_NTLM: {
  3011.         int length = 0;
  3012.  
  3013.         for ( i=0 ; i<NTLMSecBuf[0].cbBuffer ; i++ ) {
  3014.             if ( ((char *)NTLMSecBuf[0].pvBuffer)[i] == IAC )
  3015.                 iaccnt++;
  3016.         }
  3017.  
  3018.         if ( ( 2*sizeof(ULONG) + NTLMSecBuf[0].cbBuffer + iaccnt + 10)  <
  3019.              sizeof(buf) ) {
  3020.             length = copy_for_net(&buf[7],(char *)&NTLMSecBuf[0],
  3021.                                    2*sizeof(ULONG));
  3022.             length += copy_for_net(&buf[7+length], NTLMSecBuf[0].pvBuffer,
  3023.                                   NTLMSecBuf[0].cbBuffer);
  3024.         }
  3025.         sprintf(&buf[7+length], "%c%c", IAC, SE);
  3026.  
  3027.         if (deblog || tn_deb || debses) {
  3028.             int i;
  3029.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3030.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION),
  3031.                       " IS ",AUTHTYPE_NAME(authentication_version)," ",
  3032.                       AUTHMODE_NAME(mode)," NTLM_AUTH ",
  3033.                        NULL,NULL,NULL,NULL,NULL
  3034.                       );
  3035.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],length);
  3036.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3037.             debug(F100,tn_msg,"",0);
  3038.             if (tn_deb || debses) tn_debug(tn_msg);
  3039.         }
  3040. #ifdef OS2
  3041.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3042. #endif
  3043.         ttol((CHAR *)buf, length+9);
  3044. #ifdef OS2
  3045.         ReleaseTelnetMutex();
  3046. #endif
  3047.         break;
  3048.     }
  3049. #endif /* NTLM */
  3050. #ifdef KRB4
  3051.     case AUTHTYPE_KERBEROS_V4:
  3052.         for ( i=0 ; i<k4_auth.length ; i++ ) {
  3053.             if ( k4_auth.dat[i] == IAC )
  3054.                 iaccnt++;
  3055.         }
  3056.  
  3057.         if ( k4_auth.length + iaccnt + 10 < sizeof(buf) )
  3058.           k4_auth.length = copy_for_net(&buf[7], k4_auth.dat, k4_auth.length);
  3059.         else
  3060.             k4_auth.length = 0;
  3061.         sprintf(&buf[k4_auth.length+7], "%c%c", IAC, SE);
  3062.  
  3063.         if (deblog || tn_deb || debses) {
  3064.             int i;
  3065.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3066.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3067.                       AUTHTYPE_NAME(authentication_version)," ",
  3068.                       AUTHMODE_NAME(mode)," AUTH ",
  3069.                       NULL,NULL,NULL,NULL,NULL
  3070.                      );
  3071.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],k4_auth.length);
  3072.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3073.             debug(F100,tn_msg,"",0);
  3074.             if (tn_deb || debses) tn_debug(tn_msg);
  3075.         }
  3076. #ifdef OS2
  3077.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3078. #endif
  3079.         ttol((CHAR *)buf, k4_auth.length+9);
  3080. #ifdef OS2
  3081.         ReleaseTelnetMutex();
  3082. #endif
  3083.  
  3084. #ifndef REMOVE_FOR_EXPORT
  3085. #ifdef CK_ENCRYPTION
  3086.         /*
  3087.          * If we are doing mutual authentication, get set up to send
  3088.          * the challenge, and verify it when the response comes back.
  3089.          */
  3090.         if ((auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  3091.             register int i;
  3092.             int rc = 0;
  3093.  
  3094. #ifdef MIT_CURRENT
  3095.             data.data = cred.session;
  3096.             data.length = 8; /* sizeof(cred.session) */;
  3097.  
  3098.             if (code = krb5_c_random_seed(k5_context, &data)) {
  3099.                 com_err("libtelnet", code,
  3100.                          "while seeding random number generator");
  3101.                 return(0);
  3102.             }
  3103.  
  3104.             if (code = krb5_c_make_random_key(k5_context,
  3105.                                                ENCTYPE_DES_CBC_RAW,
  3106.                                                &random_key)) {
  3107.                 com_err("libtelnet", code,
  3108.                          "while creating random session key");
  3109.                 return(0);
  3110.             }
  3111.  
  3112.             /* the krb4 code uses ecb mode, but on a single block
  3113.             with a zero ivec, ecb and cbc are the same */
  3114.             k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  3115.             k4_krbkey.length = 8;
  3116.             k4_krbkey.contents = cred.session;
  3117.  
  3118.             encdata.ciphertext.data = random_key.contents;
  3119.             encdata.ciphertext.length = random_key.length;
  3120.             encdata.enctype = ENCTYPE_UNKNOWN;
  3121.  
  3122.             data.data = k4_session_key;
  3123.             data.length = 8;
  3124.  
  3125.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3126.                                    &encdata, &data);
  3127.  
  3128.             krb5_free_keyblock_contents(k5_context, &random_key);
  3129.  
  3130.             if (code) {
  3131.                 com_err("libtelnet", code, "while encrypting random key");
  3132.                 return(0);
  3133.             }
  3134.  
  3135.             encdata.ciphertext.data = k4_session_key;
  3136.             encdata.ciphertext.length = 8;
  3137.             encdata.enctype = ENCTYPE_UNKNOWN;
  3138.  
  3139.             data.data = k4_challenge;
  3140.             data.length = 8;
  3141.  
  3142.             code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  3143.                                    &encdata, &data);
  3144. #else /* MIT_CURRENT */
  3145.             memset(k4_sched,0,sizeof(Schedule));
  3146.             hexdump("auth_send",cred.session,8);
  3147.             rc = des_key_sched(cred.session, k4_sched);
  3148.             if ( rc == -1 ) {
  3149.                 printf("?Invalid DES key specified in credentials\r\n");
  3150.                 debug(F110,"auth_send",
  3151.                       "invalid DES Key specified in credentials",0);
  3152.             } else if ( rc == -2 ) {
  3153.                 printf("?Weak DES key specified in credentials\r\n");
  3154.                 debug(F110,"auth_send",
  3155.                       "weak DES Key specified in credentials",0);
  3156.             } else if ( rc != 0 ) {
  3157.                 printf("?DES Key Schedule not set by credentials\r\n");
  3158.                 debug(F110,"auth_send",
  3159.                       "DES Key Schedule not set by credentials",0);
  3160.             }
  3161.             hexdump("auth_send schedule",k4_sched,8*16);
  3162.  
  3163.             des_set_random_generator_seed(cred.session);
  3164.  
  3165.             do {
  3166.                 des_new_random_key(k4_session_key);
  3167.                 des_fixup_key_parity(k4_session_key);
  3168.             } while ( ck_des_is_weak_key(k4_session_key) );
  3169.  
  3170.             hexdump("auth_send des_new_random_key(k4_session_key)",
  3171.                      k4_session_key,8);
  3172.  
  3173.             /* Decrypt the session key so that we can send it to the */
  3174.             /* host as a challenge                                   */
  3175. #ifdef NT
  3176.             des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 0);
  3177. #else /* NT */
  3178.             des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 0);
  3179. #endif /* NT */
  3180.             hexdump(
  3181.                 "auth_send des_ecb_encrypt(k4_session_key,k4_session_key,0)",
  3182.                 k4_session_key,8
  3183.                     );
  3184.             /* Prepare the result of the challenge */
  3185.             /* Decrypt the session_key, add 1, and then encrypt it */
  3186.             /* The result stored in k4_challenge should match the  */
  3187.             /* KRB4_RESPONSE value from the host.                  */
  3188. #ifdef NT
  3189.             des_ecb_encrypt(k4_session_key, k4_challenge, k4_sched, 0);
  3190. #else /* NT */
  3191.             des_ecb_encrypt(&k4_session_key, &k4_challenge, k4_sched, 0);
  3192. #endif /* NT */
  3193.  
  3194.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,0)",
  3195.                      k4_challenge,8);
  3196. #endif /* MIT_CURRENT */
  3197.             /*
  3198.             * Increment the challenge by 1, and encrypt it for
  3199.             * later comparison.
  3200.             */
  3201.             for (i = 7; i >= 0; --i) {
  3202.                 register int x;
  3203.                 x = (unsigned int)k4_challenge[i] + 1;
  3204.                 k4_challenge[i] = x;    /* ignore overflow */
  3205.                 if (x < 256)            /* if no overflow, all done */
  3206.                     break;
  3207.             }
  3208.             hexdump("auth_send k4_challenge+1",k4_challenge,8);
  3209. #ifdef MIT_CURRENT
  3210.             data.data = k4_challenge;
  3211.             data.length = 8;
  3212.  
  3213.             encdata.ciphertext.data = k4_challenge;
  3214.             encdata.ciphertext.length = 8;
  3215.             encdata.enctype = ENCTYPE_UNKNOWN;
  3216.  
  3217.             if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0, &data,
  3218.                                        &encdata)) {
  3219.                 com_err("libtelnet", code, "while encrypting random key");
  3220.                 return(0);
  3221.             }
  3222. #else /* MIT_CURRENT */
  3223. #ifdef NT
  3224.             des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  3225. #else /* NT */
  3226.             des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  3227. #endif /* NT */
  3228.             hexdump("auth_send des_ecb_encrypt(k4_session_key,k4_challenge,1)",
  3229.                      k4_challenge,8);
  3230. #endif /* MIT_CURRENT */
  3231.         }
  3232. #endif  /* ENCRYPTION */
  3233. #endif /* REMOVE_FOR_EXPORT */
  3234.         break;
  3235. #endif /* KRB4 */
  3236. #ifdef GSSAPI_KRB5
  3237.     case AUTHTYPE_GSSAPI_KRB5:
  3238.         for ( i=0 ; i<gss_send_tok.length ; i++ ) {
  3239.             if ( ((char *)gss_send_tok.value)[i] == IAC )
  3240.                 iaccnt++;
  3241.         }
  3242.  
  3243.         if ( gss_send_tok.length + iaccnt + 10 < sizeof(buf) )
  3244.             gss_send_tok.length = copy_for_net(&buf[7], gss_send_tok.value,
  3245.                                                gss_send_tok.length);
  3246.         else
  3247.             gss_send_tok.length = 0;
  3248.         sprintf(&buf[gss_send_tok.length+7], "%c%c", IAC, SE);       /* safe */
  3249.         if (deblog || tn_deb || debses) {
  3250.             int i;
  3251.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3252.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3253.                       AUTHTYPE_NAME(authentication_version)," ",
  3254.                       AUTHMODE_NAME(mode)," AUTH ",
  3255.                       NULL,NULL,NULL,NULL,NULL
  3256.                      );
  3257.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],gss_send_tok.length);
  3258.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3259.             debug(F100,tn_msg,"",0);
  3260.             if (tn_deb || debses) tn_debug(tn_msg);
  3261.         }
  3262. #ifdef OS2
  3263.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3264. #endif
  3265.         ttol((CHAR *)buf, gss_send_tok.length+9);
  3266. #ifdef OS2
  3267.         ReleaseTelnetMutex();
  3268. #endif
  3269.         break;
  3270. #endif /* GSSAPI_KRB5 */
  3271. #ifdef KRB5
  3272.     case AUTHTYPE_KERBEROS_V5:
  3273.         debug(F111,"auth_send KRB5","k5_auth.length",k5_auth.length);
  3274.         for ( i=0 ; i<k5_auth.length ; i++ ) {
  3275.             if ( ((char *)k5_auth.data)[i] == IAC )
  3276.                 iaccnt++;
  3277.         }
  3278.  
  3279.         if ( k5_auth.length + iaccnt + 10 < sizeof(buf) )
  3280.           k5_auth.length = copy_for_net(&buf[7], k5_auth.data, k5_auth.length);
  3281.         else {
  3282.           debug(F100,"auth_send() KRB5 auth data too large for buffer","",0);
  3283.           k5_auth.length = 0;
  3284.         }
  3285.  
  3286.         sprintf(&buf[k5_auth.length+7], "%c%c", IAC, SE);       /* safe */
  3287.         if (deblog || tn_deb || debses) {
  3288.             int i;
  3289.             ckmakxmsg(tn_msg,TN_MSG_LEN,
  3290.                       "TELNET SENT SB ",TELOPT(TELOPT_AUTHENTICATION)," IS ",
  3291.                       AUTHTYPE_NAME(authentication_version)," ",
  3292.                       AUTHMODE_NAME(mode)," AUTH ",
  3293.                       NULL,NULL,NULL,NULL,NULL
  3294.                      );
  3295.             tn_hex((char *)tn_msg,TN_MSG_LEN,&buf[7],k5_auth.length);
  3296.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  3297.             debug(F100,tn_msg,"",0);
  3298.             if (tn_deb || debses) tn_debug(tn_msg);
  3299.         }
  3300. #ifdef OS2
  3301.         RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  3302. #endif
  3303.         ttol((CHAR *)buf, k5_auth.length+9);
  3304. #ifdef OS2
  3305.         ReleaseTelnetMutex();
  3306. #endif
  3307. #ifdef HEIMDAL
  3308.         krb5_data_free(&k5_auth);
  3309. #else /* HEIMDAL */
  3310.         krb5_free_data_contents(k5_context,&k5_auth);
  3311.         memset(&k5_auth,0,sizeof(krb5_data));
  3312. #endif /* HEIMDAL */
  3313.         break;
  3314. #endif /* KRB5 */
  3315.     }
  3316.     return AUTH_SUCCESS;
  3317. }
  3318.  
  3319. /*
  3320.  * Function: Parse authentication REPLY command
  3321.  *
  3322.  * Parameters:
  3323.  *  parsedat - the sub-command data.
  3324.  *
  3325.  *      end_sub - index of the character in the 'parsedat' array which
  3326.  *              is the last byte in a sub-negotiation
  3327.  *
  3328.  * Returns: Kerberos error code.
  3329.  */
  3330. static int
  3331. #ifdef CK_ANSIC
  3332. auth_reply(unsigned char *parsedat, int end_sub)
  3333. #else
  3334. auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3335. #endif
  3336. {
  3337.     int n = AUTH_FAILURE;
  3338.  
  3339.     if ( parsedat[2] != authentication_version ) {
  3340.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3341.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3342.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3343.         auth_finished(AUTH_REJECT);
  3344.         return(AUTH_FAILURE);
  3345.     }
  3346.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3347.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3348.                 AUTHMODE_NAME(parsedat[3]),
  3349.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3350.         auth_finished(AUTH_REJECT);
  3351.         return(AUTH_FAILURE);
  3352.     }
  3353.  
  3354. #ifdef KRB4
  3355.     if (authentication_version == AUTHTYPE_KERBEROS_V4)
  3356.         n = k4_auth_reply(parsedat, end_sub);
  3357. #endif
  3358. #ifdef KRB5
  3359.     if (authentication_version == AUTHTYPE_KERBEROS_V5)
  3360.         n = k5_auth_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3361. #endif
  3362. #ifdef CK_SRP
  3363.     if (authentication_version == AUTHTYPE_SRP) {
  3364. #ifndef PRE_SRP_1_7_3
  3365.         n = new_srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3366. #else /* PRE_SRP_1_7_3 */
  3367.         n = srp_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3368. #endif /* PRE_SRP_1_7_3 */
  3369.     }
  3370. #endif /* SRP */
  3371. #ifdef CK_SSL
  3372.     if (authentication_version == AUTHTYPE_SSL)
  3373.         n = ssl_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3374. #endif /* SSL */
  3375. #ifdef NTLM
  3376.     if (authentication_version == AUTHTYPE_NTLM)
  3377.         n = ntlm_reply(auth_how|auth_crypt|auth_fwd, parsedat, end_sub);
  3378. #endif /* NTLM */
  3379.     return n;
  3380. }
  3381.  
  3382.  
  3383. /*
  3384.  * Function: Parse authentication IS command
  3385.  *
  3386.  * Parameters:
  3387.  *  parsedat - the sub-command data.
  3388.  *
  3389.  *      end_sub - index of the character in the 'parsedat' array which
  3390.  *              is the last byte in a sub-negotiation
  3391.  *
  3392.  * Returns: Kerberos error code.
  3393.  */
  3394. static int
  3395. #ifdef CK_ANSIC
  3396. auth_is(unsigned char *parsedat, int end_sub)
  3397. #else
  3398. auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3399. #endif
  3400. {
  3401.     int n = AUTH_FAILURE;
  3402.  
  3403.     if ( parsedat[2] == AUTHTYPE_NULL ) {
  3404.         auth_finished(AUTH_REJECT);
  3405.         return(AUTH_FAILURE);
  3406.     }
  3407.  
  3408.     /*
  3409.      *  If CLIENT_CHOOSE_ONCE is selected the server will not allow the
  3410.      *  client to switch to an alternate authentication method if the one
  3411.      *  it originally selected fails.  (ie, if the host's SRP parameters
  3412.      *  are invalid.)  However, I think this is a bit of a security risk
  3413.      *  since allowing that functionality means that it is impossible to
  3414.      *  detect if an attack is being carried out on
  3415.      */
  3416. #define CLIENT_CHOOSE_ONCE
  3417. #ifdef CLIENT_CHOOSE_ONCE
  3418.     if ( authentication_version == AUTHTYPE_AUTO )
  3419. #endif /* CLIENT_CHOOSE_ONCE */
  3420.     {
  3421.         /* this block of code needs to check the initial parameters */
  3422.         /* to ensure that those returned match one of the sets that */
  3423.         /* were sent to the client in the first place.              */
  3424.  
  3425.         int i=0;
  3426.         for ( i=4; str_request[i] != IAC ; i+=2) {
  3427.             if (str_request[i] == parsedat[2] &&
  3428.                  str_request[i+1] == parsedat[3])
  3429.                 break;
  3430.         }
  3431.  
  3432.         if ( str_request[i] == IAC ) {
  3433.             printf("Invalid authentication type pair (%s,%s)\r\n",
  3434.                     AUTHTYPE_NAME(parsedat[2]),
  3435.                     AUTHMODE_NAME(parsedat[3]));
  3436.             auth_finished(AUTH_REJECT);
  3437.             return(AUTH_FAILURE);
  3438.         }
  3439.  
  3440.         if (authentication_version != parsedat[2]) {
  3441.             authentication_version = parsedat[2];
  3442.             auth_how = (parsedat[3] & AUTH_HOW_MASK);
  3443.             auth_crypt = (parsedat[3] & AUTH_ENCRYPT_MASK);
  3444.             auth_fwd = (parsedat[3] & INI_CRED_FWD_MASK);
  3445.             debug(F111,"auth_is","authentication_version",
  3446.                   authentication_version);
  3447.             debug(F111,"auth_is","auth_how",auth_how);
  3448.             debug(F111,"auth_is","auth_crypt",auth_crypt);
  3449.             debug(F111,"auth_is","auth_fwd",auth_fwd);
  3450.         }
  3451.     }
  3452.  
  3453. #ifdef CLIENT_CHOOSE_ONCE
  3454.     if ( parsedat[2] != authentication_version ) {
  3455.         printf("Authentication version mismatch (%s [%d] != %s [%d])\r\n",
  3456.                 AUTHTYPE_NAME(parsedat[2]),parsedat[2],
  3457.                 AUTHTYPE_NAME(authentication_version),authentication_version);
  3458.         auth_finished(AUTH_REJECT);
  3459.         return(AUTH_FAILURE);
  3460.     }
  3461.     if ( parsedat[3] != (auth_how|auth_crypt|auth_fwd) ) {
  3462.         printf("Authentication mode mismatch (%s != %s)\r\n",
  3463.                 AUTHMODE_NAME(parsedat[3]),
  3464.                 AUTHMODE_NAME(auth_how|auth_crypt|auth_fwd));
  3465.         auth_finished(AUTH_REJECT);
  3466.         return(AUTH_FAILURE);
  3467.     }
  3468. #endif /* CLIENT_CHOOSE_ONCE */
  3469.  
  3470.     switch (authentication_version) {
  3471. #ifdef KRB4
  3472.     case AUTHTYPE_KERBEROS_V4:
  3473.         n = k4_auth_is(parsedat, end_sub);
  3474.         break;
  3475. #endif
  3476. #ifdef KRB5
  3477.     case AUTHTYPE_KERBEROS_V5:
  3478.         n = k5_auth_is(parsedat[3],parsedat, end_sub);
  3479.         break;
  3480. #endif
  3481. #ifdef CK_SRP
  3482.     case AUTHTYPE_SRP:
  3483. #ifndef PRE_SRP_1_7_3
  3484.         n = new_srp_is(parsedat[3], parsedat, end_sub);
  3485. #else /* PRE_SRP_1_7_3 */
  3486.         n = srp_is(parsedat[3], parsedat, end_sub);
  3487. #endif /* PRE_SRP_1_7_3 */
  3488.         break;
  3489. #endif /* SRP */
  3490. #ifdef CK_SSL
  3491.     case AUTHTYPE_SSL:
  3492.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3493.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3494.         n = ssl_is(parsedat, end_sub);
  3495.         break;
  3496. #endif /* SSL */
  3497. #ifdef NTLM
  3498.     case AUTHTYPE_NTLM:
  3499.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3500.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  3501.         n = ntlm_is(parsedat, end_sub);
  3502.         break;
  3503. #endif /* NTLM */
  3504.     case AUTHTYPE_NULL:
  3505.     default:
  3506.         n = AUTH_FAILURE;
  3507.     }
  3508.     debug(F111,"auth_is","n",n);
  3509.     return n;
  3510. }
  3511.  
  3512. /*
  3513.  * Function: Parse authentication NAME command
  3514.  *
  3515.  * Parameters:
  3516.  *  parsedat - the sub-command data.
  3517.  *
  3518.  *      end_sub - index of the character in the 'parsedat' array which
  3519.  *              is the last byte in a sub-negotiation
  3520.  *
  3521.  * Returns: Kerberos error code.
  3522.  */
  3523. static int
  3524. #ifdef CK_ANSIC
  3525. auth_name(unsigned char *parsedat, int end_sub)
  3526. #else
  3527. auth_name(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3528. #endif
  3529. {
  3530.     int len = (end_sub-2) > 63 ? 63 : (end_sub-2);
  3531.     if ( len > 0 && (len + 1) < sizeof(szUserNameRequested)) {
  3532.         memcpy(szUserNameRequested,&parsedat[2],len);           /* safe */
  3533.         szUserNameRequested[len] = '\0';
  3534.     } else
  3535.       szUserNameRequested[0] = '\0';
  3536.     debug(F111,"auth_name szUserNameRequested",szUserNameRequested,len);
  3537.     return(AUTH_SUCCESS);
  3538. }
  3539.  
  3540. /*
  3541.  * Function: Parse the athorization sub-options and reply.
  3542.  *
  3543.  * Parameters:
  3544.  *      parsedat - sub-option string to parse.
  3545.  *
  3546.  *      end_sub - last charcter position in parsedat.
  3547.  */
  3548. int
  3549. auth_parse(unsigned char *parsedat, int end_sub)
  3550. {
  3551.     int rc = AUTH_FAILURE;
  3552.     switch (parsedat[1]) {
  3553.     case TELQUAL_SEND:
  3554.         rc = auth_send(parsedat, end_sub);
  3555.         break;
  3556.     case TELQUAL_REPLY:
  3557.         rc= auth_reply(parsedat, end_sub);
  3558.         break;
  3559.     case TELQUAL_IS:
  3560.         rc = auth_is(parsedat, end_sub);
  3561.         break;
  3562.     case TELQUAL_NAME:
  3563.         rc = auth_name(parsedat, end_sub);
  3564.         break;
  3565.     }
  3566.     debug(F111,"auth_parse","rc",rc);
  3567.     return(rc);
  3568. }
  3569.  
  3570.  
  3571. /*
  3572.  * Function: Initialization routine called kstream encryption system.
  3573.  *
  3574.  * Parameters:
  3575.  *  data - user data.
  3576.  */
  3577. int
  3578. #ifdef CK_ANSIC
  3579. auth_init(kstream ks)
  3580. #else
  3581. auth_init(ks) kstream_ptr ks;
  3582. #endif
  3583. {
  3584. #ifdef FORWARD
  3585.     forwarded_tickets = 0;  /* were tickets forwarded? */
  3586. #endif /* FORWARD */
  3587. #ifdef CK_ENCRYPTION
  3588.     encrypt_init(ks,cx_type);
  3589. #endif
  3590.     return 0;
  3591. }
  3592.  
  3593.  
  3594. /*
  3595.  * Function: Destroy routine called kstream encryption system.
  3596.  *
  3597.  * Parameters:
  3598.  *  data - user data.
  3599.  */
  3600. VOID
  3601. #ifdef CK_ANSIC
  3602. auth_destroy(void)
  3603. #else
  3604. auth_destroy()
  3605. #endif
  3606. {
  3607. }
  3608.  
  3609.  
  3610. /*
  3611.  * Function: Callback to encrypt a block of characters
  3612.  *
  3613.  * Parameters:
  3614.  *  out - return as pointer to converted buffer.
  3615.  *
  3616.  *  in - the buffer to convert
  3617.  *
  3618.  * Returns: number of characters converted.
  3619.  */
  3620. int
  3621. #ifdef CK_ANSIC
  3622. auth_encrypt(struct kstream_data_block *out,
  3623.              struct kstream_data_block *in)
  3624. #else
  3625. auth_encrypt(out,in)
  3626.     struct kstream_data_block *out; struct kstream_data_block *in;
  3627. #endif
  3628. {
  3629.     out->ptr = in->ptr;
  3630.  
  3631.     out->length = in->length;
  3632.  
  3633.     return(out->length);
  3634. }
  3635.  
  3636.  
  3637. /*
  3638.  * Function: Callback to decrypt a block of characters
  3639.  *
  3640.  * Parameters:
  3641.  *  out - return as pointer to converted buffer.
  3642.  *
  3643.  *  in - the buffer to convert
  3644.  *
  3645.  * Returns: number of characters converted.
  3646.  */
  3647. int
  3648. #ifdef CK_ANSIC
  3649. auth_decrypt(struct kstream_data_block *out,
  3650.              struct kstream_data_block *in)
  3651. #else
  3652. auth_decrypt(out,in)
  3653.     struct kstream_data_block *out; struct kstream_data_block *in;
  3654. #endif
  3655. {
  3656.     out->ptr = in->ptr;
  3657.  
  3658.     out->length = in->length;
  3659.  
  3660.     return(out->length);
  3661. }
  3662.  
  3663. #ifdef KRB4
  3664. #ifdef NT
  3665. void
  3666. ck_krb4_debug(int x)
  3667. {
  3668.     set_krb_debug(x);
  3669.     set_krb_ap_req_debug(x);
  3670. }
  3671. #endif /* NT */
  3672. int
  3673. ck_krb4_autoget_TGT(char * realm)
  3674. {
  3675.     extern struct krb_op_data krb_op;
  3676.     extern struct krb4_init_data krb4_init;
  3677.     char passwd[PWD_SZ];
  3678.     char prompt[256];
  3679.     char * saverealm=NULL;
  3680.     int  rc = -1;
  3681.     extern char * k4prprompt;
  3682.     extern char * k4pwprompt;
  3683.  
  3684.     ini_kerb();         /* Place defaults in above structs */
  3685.     passwd[0] = '\0';
  3686.  
  3687.     if ( krb4_init.principal == NULL ||
  3688.          krb4_init.principal[0] == '\0') {
  3689.         int ok = uq_txt(NULL, 
  3690.                  k4prprompt && k4prprompt[0] ?
  3691.                  k4prprompt :
  3692.                  "Kerberos 4 Principal: ",
  3693.                  2, NULL, passwd,PWD_SZ-1, NULL);
  3694.         if ( ok && passwd[0] )
  3695.             makestr(&krb4_init.principal,passwd);
  3696.         else
  3697.             return(0);
  3698.     }
  3699.  
  3700.     /* Save realm in init structure so it can be restored */
  3701.     if ( realm ) {
  3702.         saverealm = krb4_init.realm;
  3703.         krb4_init.realm = realm;
  3704.     }
  3705.  
  3706.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  3707.         int ok;
  3708.         if ( k4pwprompt && k4pwprompt[0] &&
  3709.              (strlen(k4pwprompt) + strlen(krb4_init.principal) +
  3710.                strlen(krb4_init.realm) - 4) < sizeof(prompt)) {
  3711.             sprintf(prompt,k4pwprompt,krb4_init.principal,krb4_init.realm);
  3712.         } else
  3713.             ckmakxmsg(prompt,sizeof(prompt),
  3714.                   "Kerberos 4 Password for ",krb4_init.principal,"@",
  3715.                   krb4_init.realm,": ",
  3716.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3717.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL);
  3718.         if ( !ok )
  3719.             passwd[0] = '\0';
  3720.     } else {
  3721.         ckstrncpy(passwd,pwbuf,sizeof(passwd));
  3722. #ifdef OS2
  3723.         if ( pwcrypt )
  3724.             ck_encrypt((char *)passwd);
  3725. #endif /* OS2 */
  3726.     }
  3727.  
  3728.     if ( passwd[0] ) {
  3729.         makestr(&krb4_init.password,passwd);
  3730.         rc = ck_krb4_initTGT(&krb_op, &krb4_init);
  3731.         free(krb4_init.password);
  3732.         krb4_init.password = NULL;
  3733.     }
  3734.  
  3735.     krb4_init.password = NULL;
  3736.     memset(passwd,0,PWD_SZ);
  3737.  
  3738.     /* restore realm to init structure if needed */
  3739.     if ( saverealm )
  3740.         krb4_init.realm = saverealm;
  3741.     return(rc == 0);
  3742. }
  3743.  
  3744. char *
  3745. ck_krb4_realmofhost(char *host)
  3746. {
  3747.     return (char *)krb_realmofhost(host);
  3748. }
  3749.  
  3750. /*
  3751.  *
  3752.  * K4_auth_send - gets authentication bits we need to send to KDC.
  3753.  *
  3754.  * Result is left in auth
  3755.  *
  3756.  * Returns: 0 on failure, 1 on success
  3757.  */
  3758. static int
  3759. #ifdef CK_ANSIC
  3760. k4_auth_send(void)
  3761. #else
  3762. k4_auth_send()
  3763. #endif
  3764. {
  3765.     int r=0;                                    /* Return value */
  3766.     char instance[INST_SZ+1]="";
  3767.     char *realm=NULL;
  3768.     char tgt[4*REALM_SZ+1];
  3769.  
  3770.     memset(instance, 0, sizeof(instance));
  3771.  
  3772. #ifdef COMMENT
  3773.     /* we only need to call krb_get_phost if the hostname */
  3774.     /* is not fully qualified.  But we have already done  */
  3775.     /* this in netopen() call.  This will save a round of */
  3776.     /* DNS queries.                                       */
  3777.     debug(F110,"k4_auth_send","krb_get_phost",0);
  3778.     if (realm = (char *)krb_get_phost(szHostName)) {
  3779.         ckstrncpy(instance, realm, INST_SZ);
  3780.     }
  3781. #else /* COMMENT */
  3782.     {
  3783.         char *p;
  3784.         ckstrncpy(instance, szHostName, INST_SZ);
  3785.         for ( p=instance; *p && *p != '.' ; p++ );
  3786.         *p = '\0';
  3787.     }
  3788. #endif /* COMMENT */
  3789.  
  3790.     debug(F110,"k4_auth_send","krb_get_realmofhost",0);
  3791.     realm = (char *)krb_realmofhost(szHostName);
  3792.  
  3793.     if (!realm) {
  3794.         strcpy(strTmp, "Can't find realm for host \"");
  3795.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  3796.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  3797.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3798.         krb4_errno = r;
  3799.         makestr(&krb4_errmsg,strTmp);
  3800.         return(0);
  3801.     }
  3802.  
  3803.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  3804.     r = ck_krb4_tkt_isvalid(tgt);
  3805.  
  3806.     if ( r <= 0 && krb4_autoget )
  3807.         ck_krb4_autoget_TGT(realm);
  3808.  
  3809.     debug(F110,"k4_auth_send","krb_mk_req",0);
  3810.     r = krb_mk_req(&k4_auth, krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3811.                     instance, realm, 0);
  3812.  
  3813.     if (r == 0) {
  3814.         debug(F110,"k4_auth_send","krb_get_cred",0);
  3815.         r = krb_get_cred(krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  3816.                           instance, realm, (CREDENTIALS *)&cred);
  3817.         if (r)
  3818.             debug(F111,"k4_auth_send","krb_get_cred() failed",r);
  3819.     }
  3820.     else
  3821.         debug(F111,"k4_auth_send","krb_mk_req() failed",r);
  3822.  
  3823.     if (r) {
  3824.         strcpy(strTmp, "Can't get \"");
  3825.         ckstrncat(strTmp,
  3826.                   krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,AUTHTMPBL);
  3827.         if (instance[0] != 0) {
  3828.             ckstrncat(strTmp, ".",AUTHTMPBL);
  3829.             ckstrncat(strTmp, instance,AUTHTMPBL);
  3830.         }
  3831.         ckstrncat(strTmp, "@",AUTHTMPBL);
  3832.         ckstrncat(strTmp, realm,AUTHTMPBL);
  3833.         ckstrncat(strTmp, "\" ticket\r\n  ",AUTHTMPBL);
  3834.         ckstrncat(strTmp, (char *)krb_get_err_text_entry(r),AUTHTMPBL);
  3835.         debug(F111,"k4_auth_send",(char *)krb_get_err_text_entry(r),r);
  3836.         printf("?Kerberos 4 error: %s\r\n",strTmp);
  3837.         krb4_errno = r;
  3838.         makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3839.         return(0);
  3840.     }
  3841.  
  3842. #ifdef OS2
  3843.     if ( !szUserName[0] || !stricmp(szUserName,cred.pname) ) {
  3844.         ckstrncpy(szUserName, cred.pname, UIDBUFLEN);
  3845.     }
  3846. #endif /* OS2 */
  3847.     krb4_errno = r;
  3848.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  3849.     debug(F110,"k4_auth_send",krb4_errmsg,0);
  3850.     return(1);
  3851. }
  3852.  
  3853. /*
  3854.  * Function: K4 parse authentication reply command
  3855.  *
  3856.  * Parameters:
  3857.  *  parsedat - the sub-command data.
  3858.  *
  3859.  *  end_sub - index of the character in the 'parsedat' array which
  3860.  *              is the last byte in a sub-negotiation
  3861.  *
  3862.  * Returns: Kerberos error code.
  3863.  */
  3864. static int
  3865. #ifdef CK_ANSIC
  3866. k4_auth_reply(unsigned char *parsedat, int end_sub)
  3867. #else
  3868. k4_auth_reply(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  3869. #endif
  3870. {
  3871. #ifdef CK_ENCRYPTION
  3872.     Session_Key skey;
  3873. #ifdef MIT_CURRENT
  3874.     krb5_data kdata;
  3875.     krb5_enc_data encdata;
  3876.     krb5_error_code code;
  3877. #endif /* MIT_CURRENT */
  3878. #endif
  3879.     time_t t;
  3880.     int x;
  3881.     int i;
  3882.  
  3883.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  3884.         auth_finished(AUTH_REJECT);
  3885.         return AUTH_FAILURE;
  3886.     }
  3887.  
  3888.     if (parsedat[4] == KRB_REJECT) {
  3889.         strTmp[0] = 0;
  3890.  
  3891.         for (i = 5; i <= end_sub; i++) {
  3892.             if (parsedat[i] == IAC)
  3893.                 break;
  3894.             strTmp[i-5] = parsedat[i];
  3895.             strTmp[i-4] = 0;
  3896.         }
  3897.  
  3898.         if (!strTmp[0])
  3899.             strcpy(strTmp, "Authentication rejected by remote machine!");
  3900.         printf("Kerberos V4 authentication failed!\r\n%s\r\n",strTmp);
  3901.         krb4_errno = -1;
  3902.         makestr(&krb4_errmsg,strTmp);
  3903.         auth_finished(AUTH_REJECT);
  3904.         return AUTH_FAILURE;
  3905.     }
  3906.  
  3907.     if (parsedat[4] == KRB_ACCEPT) {
  3908.         int net_len;
  3909.         if ((parsedat[3] & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
  3910.             ckmakmsg(strTmp,sizeof(strTmp),"Kerberos V4 accepts you as ",
  3911.                       szUserName,NULL,NULL);
  3912.             printf("%s\r\n",strTmp);
  3913.             accept_complete = 1;
  3914.             krb4_errno = 0;
  3915.             makestr(&krb4_errmsg,strTmp);
  3916.             auth_finished(AUTH_USER);
  3917.             return AUTH_SUCCESS;
  3918.         }
  3919.  
  3920.         if ((parsedat[3] & AUTH_HOW_MASK) != AUTH_HOW_MUTUAL) {
  3921.             printf("Kerberos V4 authentication failed!\r\n");
  3922.             ckstrncpy(strTmp,
  3923.         "Kerberos V4 accepted you, but didn't provide mutual authentication",
  3924.                        sizeof(strTmp));
  3925.             printf("%s\r\n",strTmp);
  3926.             krb4_errno = -1;
  3927.             makestr(&krb4_errmsg,strTmp);
  3928.             auth_finished(AUTH_REJECT);
  3929.             return AUTH_FAILURE;
  3930.         }
  3931.  
  3932. #ifndef REMOVE_FOR_EXPORT
  3933. #ifdef CK_ENCRYPTION
  3934.         SendK4AuthSB(KRB4_CHALLENGE,k4_session_key,sizeof(k4_session_key));
  3935.  
  3936.         /* We have sent the decrypted session key to the host as a challenge */
  3937.         /* now encrypt it to restore it to its original valid DES key value */
  3938. #ifdef MIT_CURRENT
  3939.         kdata.data = k4_session_key;
  3940.         kdata.length = 8;
  3941.  
  3942.         encdata.ciphertext.data = k4_session_key;
  3943.         encdata.ciphertext.length = 8;
  3944.         encdata.enctype = ENCTYPE_UNKNOWN;
  3945.  
  3946.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey,
  3947.                                    0, 0, &kdata, &encdata)) {
  3948.             com_err("k4_auth_reply", code,
  3949.                      "while encrypting session_key");
  3950.             auth_finished(AUTH_REJECT);
  3951.             return AUTH_FAILURE;
  3952.         }
  3953. #else /* MIT_CURRENT */
  3954. #ifdef NT
  3955.         des_ecb_encrypt(k4_session_key, k4_session_key, k4_sched, 1);
  3956. #else /* NT */
  3957.         des_ecb_encrypt(&k4_session_key, &k4_session_key, k4_sched, 1);
  3958. #endif /* NT */
  3959.         hexdump(
  3960.             "k4_auth_reply des_ecb_encrypt(k4_session_key,k4_session_key,1)",
  3961.              k4_session_key,
  3962.              8
  3963.                 );
  3964. #endif /* MIT_CURRENT */
  3965.  
  3966. #ifdef CK_SSL
  3967.         if (!(ssl_active_flag || tls_active_flag))
  3968. #endif /* CK_SSL */
  3969.         {
  3970.         /* And then use it to configure the encryption state machine. */
  3971.             skey.type = SK_DES;
  3972.             skey.length = 8;
  3973.             skey.data = k4_session_key;
  3974.             encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  3975.         }
  3976. #endif /* ENCRYPTION */
  3977. #endif /* REMOVE_FOR_EXPORT */
  3978.         accept_complete = 1;
  3979.         ckmakmsg(strTmp,sizeof(strTmp),
  3980.                  "Kerberos V4 accepts you as ",szUserName,NULL,NULL);
  3981.         printf("%s\r\n",strTmp);
  3982.         krb4_errno = 0;
  3983.         makestr(&krb4_errmsg,strTmp);
  3984.         auth_finished(AUTH_USER);
  3985.         return AUTH_SUCCESS;
  3986.     }
  3987.  
  3988.     if (parsedat[4] == KRB4_RESPONSE) {
  3989.         if (end_sub < 12) {
  3990.             auth_finished(AUTH_REJECT);
  3991.             return AUTH_FAILURE;
  3992.         }
  3993.  
  3994.         hexdump("KRB4_RESPONSE &parsedat[5]",&parsedat[5],8);
  3995. #ifdef CK_ENCRYPTION
  3996.         hexdump("KRB4_RESPONSE k4_challenge",k4_challenge,8);
  3997.  
  3998.         /* The datablock returned from the host should match the value */
  3999.         /* we stored in k4_challenge.                                  */
  4000.         if (memcmp(&parsedat[5], k4_challenge, sizeof(k4_challenge)) != 0) {
  4001.             printf("Kerberos V4 authentication failed!\r\n%s\r\n",
  4002.             "Remote machine is being impersonated!");
  4003.             krb4_errno = -1;
  4004.             makestr(&krb4_errmsg,"Remote machine is being impersonated!");
  4005.             auth_finished(AUTH_REJECT);
  4006.             return AUTH_FAILURE;
  4007.         }
  4008. #else /* ENCRYPTION */
  4009.         makestr(&krb4_errmsg,"Kermit built without support for encryption.");
  4010.         return AUTH_FAILURE;
  4011. #endif /* ENCRYPTION */
  4012.         mutual_complete = 1;
  4013.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  4014.                    sizeof(strTmp));
  4015.         printf("%s\r\n",strTmp);
  4016.         krb4_errno = 0;
  4017.         makestr(&krb4_errmsg,strTmp);
  4018.         auth_finished(AUTH_USER);
  4019.         return AUTH_SUCCESS;
  4020.     }
  4021.     auth_finished(AUTH_REJECT);
  4022.     return AUTH_FAILURE;
  4023. }
  4024.  
  4025. /*
  4026.  * Function: K4 parse authentication IS command
  4027.  *
  4028.  * Parameters:
  4029.  *  parsedat - the sub-command data.
  4030.  *
  4031.  *  end_sub - index of the character in the 'parsedat' array which
  4032.  *            is the last byte in a sub-negotiation
  4033.  *
  4034.  * Returns: Kerberos error code.
  4035.  */
  4036.  
  4037. static int
  4038. #ifdef CK_ANSIC
  4039. k4_auth_is(unsigned char *parsedat, int end_sub)
  4040. #else
  4041. k4_auth_is(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  4042. #endif
  4043. {
  4044. #ifdef CK_ENCRYPTION
  4045.     Session_Key skey;
  4046. #ifdef MIT_CURRENT
  4047.     Block datablock, tmpkey;
  4048.     krb5_data kdata;
  4049.     krb5_enc_data encdata;
  4050.     krb5_error_code code;
  4051. #else /* MIT_CURRENT */
  4052.     Block datablock;
  4053. #endif /* MIT_CURRENT */
  4054. #endif  /* ENCRYPTION */
  4055.     char realm[REALM_SZ+1];
  4056.     char instance[INST_SZ];
  4057.     int r = 0;
  4058.     char * data = &parsedat[5];
  4059.     int    cnt = end_sub - 5;
  4060.     extern char myipaddr[];
  4061.     struct hostent *host;
  4062.     struct in_addr inaddr;
  4063.     int i;
  4064.  
  4065.     if (end_sub < 4 || parsedat[2] != AUTHTYPE_KERBEROS_V4) {
  4066.         debug(F110,"k4_auth_is","Not kerberos v4",0);
  4067.         auth_finished(AUTH_REJECT);
  4068.         return AUTH_FAILURE;
  4069.     }
  4070.  
  4071.     switch (parsedat[4]) {
  4072.     case KRB_AUTH:
  4073.         debug(F110,"k4_auth_is","KRB_AUTH",0);
  4074.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ+1);
  4075.         if (realm[0] == '\0') {
  4076.             SendK4AuthSB(KRB_REJECT, (void *)"No local V4 Realm.", -1);
  4077.             printf("\r\n? Kerberos 4 - No Local Realm\r\n");
  4078.             debug(F110,"k4_auth_is","No local realm",0);
  4079.             krb4_errno = -1;
  4080.             makestr(&krb4_errmsg,"No local realm");
  4081.             auth_finished(AUTH_REJECT);
  4082.             return AUTH_FAILURE;
  4083.         }
  4084.         debug(F110,"k4_auth_is",realm,0);
  4085.         if ( cnt < sizeof(k4_auth.dat) ) {
  4086.             k4_auth.length = cnt;
  4087.             memcpy((void *)k4_auth.dat, (void *)data, k4_auth.length);
  4088.         } else
  4089.             k4_auth.length = 0;
  4090.         hexdump("k4_auth.dat",k4_auth.dat, k4_auth.length);
  4091.  
  4092.         /* Get Instance */
  4093.         inaddr.s_addr = inet_addr(myipaddr);
  4094.         host = gethostbyaddr((unsigned char *)&inaddr,4,PF_INET);
  4095.         if ( host ) {
  4096. #ifdef HADDRLIST
  4097.             host = ck_copyhostent(host);
  4098. #endif /* HADDRLIST */
  4099.             ckstrncpy(instance,host->h_name,INST_SZ);
  4100.             for ( i=0;i<INST_SZ;i++ ) {
  4101.                 if ( instance[i] == '.' )
  4102.                     instance[i] = '\0';
  4103.                 else
  4104.                     instance[i] = tolower(instance[i]);
  4105.             }
  4106.         } else {
  4107.             instance[0] = '*';
  4108.             instance[1] = 0;
  4109.         }
  4110.  
  4111.         if (r = krb_rd_req(&k4_auth,
  4112.                             krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  4113.                             instance, 0, &k4_adat, k4_keytab)) {
  4114.  
  4115.             hexdump("k4_adat", &k4_adat, sizeof(AUTH_DAT));
  4116.             krb_kntoln(&k4_adat, k4_name);
  4117.             ckmakmsg(strTmp,sizeof(strTmp),
  4118.                      "Kerberos failed him as ", k4_name,NULL,NULL);
  4119.             printf("%s\r\n",strTmp);
  4120.             krb4_errno = r;
  4121.             makestr(&krb4_errmsg,strTmp);
  4122.             SendK4AuthSB(KRB_REJECT, (void *)krb_get_err_text_entry(r), -1);
  4123.             auth_finished(AUTH_REJECT);
  4124.             return AUTH_FAILURE;
  4125.         }
  4126.  
  4127. #ifdef CK_ENCRYPTION
  4128.         memcpy((void *)k4_session_key, (void *)k4_adat.session,
  4129.                 sizeof(Block));                 /* safe */
  4130.         hexdump("k4_auth_is k4_session_key",k4_session_key,sizeof(Block));
  4131. #endif /* ENCRYPTION */
  4132.         krb_kntoln(&k4_adat, k4_name);
  4133.  
  4134.         ckstrncpy(szUserNameAuthenticated,k4_name,UIDBUFLEN);
  4135.         if (szUserNameRequested && !kuserok(&k4_adat, k4_name)) {
  4136.             SendK4AuthSB(KRB_ACCEPT, (void *)0, 0);
  4137.             if ( !strcmp(k4_name,szUserNameRequested) )
  4138.                 auth_finished(AUTH_VALID);
  4139.             else
  4140.                 auth_finished(AUTH_USER);
  4141.             accept_complete = 1;
  4142.         }
  4143.         else {
  4144.             SendK4AuthSB(KRB_REJECT,
  4145.                   (void *)"user is not authorized", -1);
  4146.             auth_finished(AUTH_REJECT);
  4147.             krb4_errno = r;
  4148.             makestr(&krb4_errmsg,"user is not authorized");
  4149.             return(AUTH_FAILURE);
  4150.         }
  4151.         break;
  4152.  
  4153.     case KRB4_CHALLENGE:
  4154.         debug(F110,"k4_auth_is","KRB_CHALLENGE",0);
  4155. #ifndef CK_ENCRYPTION
  4156.         SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4157. #else   /* ENCRYPTION */
  4158.         if (!VALIDKEY(k4_session_key)) {
  4159.             /*
  4160.             * We don't have a valid session key, so just
  4161.             * send back a response with an empty session
  4162.             * key.
  4163.             */
  4164.             SendK4AuthSB(KRB4_RESPONSE, (void *)0, 0);
  4165.             mutual_complete = 1;
  4166.             break;
  4167.         }
  4168.  
  4169.         /*
  4170.         * Initialize the random number generator since it's
  4171.         * used later on by the encryption routine.
  4172.         */
  4173. #ifdef MIT_CURRENT
  4174.         kdata.data = k4_session_key;
  4175.         kdata.length = 8;
  4176.  
  4177.         if (code = krb5_c_random_seed(k5_context, &kdata)) {
  4178.             com_err("k4_auth_is", code,
  4179.                      "while seeding random number generator");
  4180.             auth_finished(AUTH_REJECT);
  4181.             return AUTH_FAILURE;
  4182.         }
  4183.  
  4184.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4185.         /*
  4186.         * Take the received encrypted challenge, and encrypt
  4187.         * it again to get a unique session_key for the
  4188.         * ENCRYPT option.
  4189.         */
  4190.         k4_krbkey.enctype = ENCTYPE_DES_CBC_RAW;
  4191.         k4_krbkey.length = 8;
  4192.         k4_krbkey.contents = k4_session_key;
  4193.  
  4194.         kdata.data = datablock;
  4195.         kdata.length = 8;
  4196.  
  4197.         encdata.ciphertext.data = tmpkey;
  4198.         encdata.ciphertext.length = 8;
  4199.         encdata.enctype = ENCTYPE_UNKNOWN;
  4200.  
  4201.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4202.                                    &kdata, &encdata)) {
  4203.             com_err("k4_auth_is", code, "while encrypting random key");
  4204.             auth_finished(AUTH_REJECT);
  4205.             return AUTH_FAILURE;
  4206.         }
  4207.  
  4208. #ifdef CK_SSL
  4209.         if (!(ssl_active_flag || tls_active_flag))
  4210. #endif /* CK_SSL */
  4211.         {
  4212.             skey.type = SK_DES;
  4213.             skey.length = 8;
  4214.             skey.data = tmpkey;
  4215.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4216.         }
  4217.         /*
  4218.         * Now decrypt the received encrypted challenge,
  4219.         * increment by one, re-encrypt it and send it back.
  4220.         */
  4221.         encdata.ciphertext.data = datablock;
  4222.         encdata.ciphertext.length = 8;
  4223.         encdata.enctype = ENCTYPE_UNKNOWN;
  4224.  
  4225.         kdata.data = k4_challenge;
  4226.         kdata.length = 8;
  4227.  
  4228.         if (code = krb5_c_decrypt(k5_context, &k4_krbkey, 0, 0,
  4229.                                    &encdata, &kdata)) {
  4230.             com_err("k4_auth_is", code, "while decrypting challenge");
  4231.             auth_finished(AUTH_REJECT);
  4232.             return AUTH_FAILURE;
  4233.         }
  4234. #else /* MIT_CURRENT */
  4235.         des_set_random_generator_seed(k4_session_key);
  4236.         r = des_key_sched(k4_session_key, k4_sched);
  4237.         if ( r == -1 ) {
  4238.             printf("?Invalid DES key specified in credentials\r\n");
  4239.             debug(F110,"auth_is CHALLENGE",
  4240.                    "invalid DES Key specified in credentials",0);
  4241.         } else if ( r == -2 ) {
  4242.             printf("?Weak DES key specified in credentials\r\n");
  4243.             debug(F110,"auth_is CHALLENGE",
  4244.                    "weak DES Key specified in credentials",0);
  4245.         } else if ( r != 0 ) {
  4246.             printf("?DES Key Schedule not set by credentials\r\n");
  4247.             debug(F110,"auth_is CHALLENGE",
  4248.                    "DES Key Schedule not set by credentials",0);
  4249.         }
  4250.         hexdump("auth_is schedule",k4_sched,8*16);
  4251.  
  4252.         memcpy((void *)datablock, (void *)data, sizeof(Block)); /* safe */
  4253.         hexdump("auth_is challege",datablock,sizeof(Block));
  4254.  
  4255.         /*
  4256.         * Take the received encrypted challenge, and encrypt
  4257.         * it again to get a unique k4_session_key for the
  4258.         * ENCRYPT option.
  4259.         */
  4260. #ifdef NT
  4261.         des_ecb_encrypt(datablock, k4_session_key, k4_sched, 1);
  4262. #else /* NT */
  4263.         des_ecb_encrypt(&datablock, &k4_session_key, k4_sched, 1);
  4264. #endif /* NT */
  4265.         hexdump("auth_is des_ecb_encrypt(datablock,k4_session_key,1)",
  4266.                  k4_session_key,8);
  4267.  
  4268. #ifdef CK_SSL
  4269.         if (!(ssl_active_flag || tls_active_flag))
  4270. #endif /* CK_SSL */
  4271.         {
  4272.             skey.type = SK_DES;
  4273.             skey.length = 8;
  4274.             skey.data = k4_session_key;
  4275.             encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  4276.         }
  4277.         /*
  4278.         * Now decrypt the received encrypted challenge,
  4279.         * increment by one, re-encrypt it and send it back.
  4280.         */
  4281. #ifdef NT
  4282.         des_ecb_encrypt(datablock, k4_challenge, k4_sched, 0);
  4283. #else /* NT */
  4284.         des_ecb_encrypt(&datablock, &k4_challenge, k4_sched, 0);
  4285. #endif /* NT */
  4286.         hexdump("auth_is des_ecb_encrypt(datablock,k4_challenge,0)",
  4287.                  k4_session_key,8);
  4288. #endif /* MIT_CURRENT */
  4289.         for (r = 7; r >= 0; r--) {
  4290.             register int t;
  4291.             t = (unsigned int)k4_challenge[r] + 1;
  4292.             k4_challenge[r] = t;        /* ignore overflow */
  4293.             if (t < 256)                /* if no overflow, all done */
  4294.                 break;
  4295.         }
  4296.         hexdump("auth_is k4_challenge+1",k4_challenge,8);
  4297.  
  4298. #ifdef MIT_CURRENT
  4299.         kdata.data = k4_challenge;
  4300.         kdata.length = 8;
  4301.  
  4302.         encdata.ciphertext.data = k4_challenge;
  4303.         encdata.ciphertext.length = 8;
  4304.         encdata.enctype = ENCTYPE_UNKNOWN;
  4305.  
  4306.         if (code = krb5_c_encrypt(k5_context, &k4_krbkey, 0, 0,
  4307.                                    &kdata, &encdata)) {
  4308.             com_err("k4_auth_is", code, "while decrypting challenge");
  4309.             auth_finished(AUTH_REJECT);
  4310.             return AUTH_FAILURE;
  4311.         }
  4312. #else /* MIT_CURRENT */
  4313. #ifdef NT
  4314.         des_ecb_encrypt(k4_challenge, k4_challenge, k4_sched, 1);
  4315. #else /* NT */
  4316.         des_ecb_encrypt(&k4_challenge, &k4_challenge, k4_sched, 1);
  4317. #endif /* NT */
  4318.         hexdump("auth_is des_ecb_encrypt(k4_challenge_key,k4_challenge,1)",
  4319.                  k4_challenge,8);
  4320.  
  4321. #endif /* MIT_CURRENT */
  4322.         SendK4AuthSB(KRB4_RESPONSE,(void *)k4_challenge,sizeof(k4_challenge));
  4323. #endif  /* ENCRYPTION */
  4324.         mutual_complete = 1;
  4325.         break;
  4326.  
  4327.     default:
  4328.         if (1)
  4329.             printf("Unknown Kerberos option %d\r\n", data[-1]);
  4330.         SendK4AuthSB(KRB_REJECT, 0, 0);
  4331.         return(AUTH_FAILURE);
  4332.     }
  4333.     krb4_errno = r;
  4334.     makestr(&krb4_errmsg,krb_get_err_text_entry(krb4_errno));
  4335.     return(AUTH_SUCCESS);
  4336. }
  4337. #endif /* KRB4 */
  4338.  
  4339. #ifdef KRB5
  4340. int
  4341. ck_krb5_autoget_TGT(char * realm)
  4342. {
  4343.     extern struct krb_op_data krb_op;
  4344.     extern struct krb5_init_data krb5_init;
  4345.     char passwd[PWD_SZ];
  4346.     char prompt[64];
  4347.     char * saverealm=NULL;
  4348.     int  rc = -1;
  4349.     extern char * k5prprompt;
  4350.     extern char * k5pwprompt;
  4351.  
  4352.     ini_kerb();         /* Place defaults in above structs */
  4353.     passwd[0] = '\0';
  4354.  
  4355.     if ( krb5_init.principal == NULL ||
  4356.          krb5_init.principal[0] == '\0') {
  4357.         int ok = uq_txt(NULL,k5prprompt && k5prprompt[0] ? k5prprompt :
  4358.                   "Kerberos 5 Principal: ",2,NULL,passwd,PWD_SZ-1,NULL);
  4359.         if ( ok && passwd[0] )
  4360.             makestr(&krb5_init.principal,passwd);
  4361.         else
  4362.             return(0);
  4363.     }
  4364.  
  4365.     /* Save realm in init structure so it can be restored */
  4366.     if ( realm ) {
  4367.         saverealm = krb5_init.realm;
  4368.         krb5_init.realm = realm;
  4369.     }
  4370.  
  4371.     if ( passwd[0] || !(pwbuf[0] && pwflg) ) {
  4372.         int ok;
  4373.         if ( k5pwprompt && k5pwprompt[0] &&
  4374.              (strlen(k5pwprompt) + strlen(krb5_init.principal) +
  4375.               strlen(krb5_init.realm) - 4) < sizeof(prompt)) {
  4376.             sprintf(prompt,k5pwprompt,krb5_init.principal,krb5_init.realm);
  4377.         } else
  4378.         ckmakxmsg(prompt,sizeof(prompt),
  4379.                   k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  4380.                   "Kerberos 5 Password for ",
  4381.                   krb5_init.principal,"@",krb5_init.realm,": ",
  4382.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL
  4383.                  );
  4384.         ok = uq_txt(NULL,prompt,2,NULL,passwd,PWD_SZ-1,NULL);
  4385.         if ( !ok )
  4386.             passwd[0] = '\0';
  4387.     } else {
  4388.         ckstrncpy(passwd,pwbuf,sizeof(passwd));
  4389. #ifdef OS2
  4390.         if ( pwcrypt )
  4391.             ck_encrypt((char *)passwd);
  4392. #endif /* OS2 */
  4393.     }
  4394.  
  4395.     if ( passwd[0] ) {
  4396.         extern struct krb4_init_data krb4_init;
  4397.         char * savek4realm=NULL;
  4398.  
  4399.         makestr(&krb5_init.password,passwd);
  4400.  
  4401.         if ( krb5_d_getk4 ) {
  4402.             krb5_init.getk4 = 1;
  4403.             makestr(&krb4_init.principal,krb5_init.principal);
  4404.             makestr(&krb4_init.password,passwd);
  4405.             if ( realm ) {
  4406.                 savek4realm = krb4_init.realm;
  4407.                 krb4_init.realm = realm;
  4408.             }
  4409.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,&krb4_init);
  4410.  
  4411.             if ( savek4realm )
  4412.                 krb4_init.realm = savek4realm;
  4413.             free(krb4_init.password);
  4414.             krb4_init.password = NULL;
  4415.         } else {
  4416.             rc = ck_krb5_initTGT(&krb_op, &krb5_init,NULL);
  4417.         }
  4418.  
  4419.         free(krb5_init.password);
  4420.         krb5_init.password = NULL;
  4421.  
  4422.         memset(passwd,0,PWD_SZ);
  4423.     }
  4424.  
  4425.     /* restore realm to init structure if needed */
  4426.     if ( saverealm )
  4427.         krb5_init.realm = saverealm;
  4428.     return(rc == 0);
  4429. }
  4430.  
  4431. static krb5_error_code
  4432. #ifdef CK_ANSIC
  4433. k5_get_ccache( krb5_context k5_context, krb5_ccache * p_ccache,
  4434.                char * cc_name )
  4435. #else  /* CK_ANSIC */
  4436. k5_get_ccache(k5_context, p_ccache, cc_name)
  4437.     krb5_context k5_context;
  4438.     krb5_ccache * p_ccache;
  4439.     char * cc_name;
  4440. #endif /* CK_ANSIC */
  4441. {
  4442.     krb5_error_code r=0;
  4443.     char cc_tmp[CKMAXPATH+1];
  4444.     const char * def_name = NULL;
  4445.  
  4446. #ifndef HEIMDAL
  4447.     if ( cc_name ) {
  4448.         if ( strncmp("FILE:",cc_name,5) &&
  4449.              strncmp("MEMORY:",cc_name,7) &&
  4450.              strncmp("API:",cc_name,4) &&
  4451.              strncmp("STDIO:",cc_name,6))
  4452.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",cc_name,NULL,NULL);
  4453.         else {
  4454.             ckstrncpy(cc_tmp,cc_name,CKMAXPATH);
  4455.         }
  4456.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4457.         if (r != 0) {
  4458.             com_err("k5_get_ccache resolving ccache",r,
  4459.                      cc_tmp);
  4460.         }
  4461.     } else if ( krb5_d_cc ) {
  4462.         if ( strncmp("FILE:",krb5_d_cc,5) &&
  4463.              strncmp("MEMORY:",krb5_d_cc,7) &&
  4464.              strncmp("API:",krb5_d_cc,4) &&
  4465.              strncmp("STDIO:",krb5_d_cc,6))
  4466.             ckmakmsg(cc_tmp,CKMAXPATH,"FILE:",krb5_d_cc,NULL,NULL);
  4467.         else {
  4468.             ckstrncpy(cc_tmp,krb5_d_cc,CKMAXPATH);
  4469.         }
  4470.         r = krb5_cc_resolve (k5_context, cc_tmp, p_ccache);
  4471.         if (r != 0) {
  4472.             com_err("k5_get_ccache resolving ccache",r,
  4473.                      krb5_d_cc);
  4474.         }
  4475.     } else
  4476. #endif /* HEIMDAL */
  4477.     {
  4478.         if ((r = krb5_cc_default(k5_context, p_ccache))) {
  4479.             com_err("k5_get_ccache",r,"while getting default ccache");
  4480.         }
  4481.     }
  4482.         /* do not set krb5_errno/krb5_errmsg here since the value returned */
  4483.         /* is being passed internally within the krb5 functions.           */
  4484.     return(r);
  4485. }
  4486.  
  4487.  
  4488. char *
  4489. ck_krb5_realmofhost(char *host)
  4490. {
  4491.     char ** realmlist=NULL;
  4492.     krb5_context private_context=NULL;
  4493.     static char * realm = NULL;
  4494.  
  4495.     if ( !host )
  4496.         return NULL;
  4497.  
  4498.     if ( realm ) {
  4499.         free(realm);
  4500.         realm = NULL;
  4501.     }
  4502.  
  4503.     /* create private_context */
  4504.     if (krb5_init_context(&private_context)) {
  4505.         debug(F110,"ck_krb5_realmofhost()","unable to init_context",0);
  4506.         return(NULL);
  4507.     }
  4508.  
  4509.     krb5_get_host_realm(private_context,host,&realmlist);
  4510.     if (realmlist && realmlist[0]) {
  4511.         makestr(&realm,realmlist[0]);
  4512.         krb5_free_host_realm(private_context,realmlist);
  4513.         realmlist = NULL;
  4514.     }
  4515.  
  4516.     if ( private_context ) {
  4517.         krb5_free_context(private_context);
  4518.         private_context = NULL;
  4519.     }
  4520.  
  4521.     if (ckstrchr(realm,'.') == NULL) {
  4522.         int n = 0;
  4523.         char * p = host;
  4524.         while ( (p = ckstrchr(p,'.')) != NULL ) {
  4525.             n++;
  4526.             p++;
  4527.         }
  4528.         if (n == 1) {
  4529.             makestr(&realm,host);
  4530.             ckupper(realm);
  4531.         } else {
  4532.             free(realm);
  4533.             realm = NULL;
  4534.         }
  4535.     }
  4536.     return(realm);
  4537. }
  4538.  
  4539. /*
  4540.  *
  4541.  * K5_auth_send - gets authentication bits we need to send to KDC.
  4542.  *
  4543.  * Code lifted from telnet sample code in the appl directory.
  4544.  *
  4545.  * Result is left in k5_auth
  4546.  *
  4547.  * Returns: 0 on failure, 1 on success
  4548.  *
  4549.  */
  4550.  
  4551. static int
  4552. #ifdef CK_ANSIC
  4553. k5_auth_send(int how, int encrypt, int forward)
  4554. #else
  4555. k5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  4556. #endif
  4557. {
  4558.     krb5_error_code r=0;
  4559.     krb5_ccache ccache=NULL;
  4560. #ifndef HEIMDAL
  4561.     krb5_creds creds;
  4562. #endif /* HEIMDAL */
  4563.     krb5_creds * new_creds=NULL;
  4564. #ifdef CK_ENCRYPTION
  4565.     krb5_keyblock *newkey = 0;
  4566. #endif /* CK_ENCRYPTION */
  4567.     krb5_flags ap_opts, auth_flags;
  4568.     char type_check[32];
  4569.     krb5_data checksum;
  4570.     int len=0;
  4571.     char * realm = NULL;
  4572.     char tgt[256];
  4573.  
  4574.     realm = ck_krb5_realmofhost(szHostName);
  4575.     if (!realm) {
  4576.         ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  4577.         ckstrncat(strTmp, szHostName,AUTHTMPBL);
  4578.         ckstrncat(strTmp, "\"",AUTHTMPBL);
  4579.         printf("?Kerberos 5 error: %s\r\n",strTmp);
  4580.         krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  4581.         makestr(&krb5_errmsg,strTmp);
  4582.         return(0);
  4583.     }
  4584.  
  4585.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  4586.     debug(F110,"k5_auth_send TGT",tgt,0);
  4587.     if ( krb5_autoget &&
  4588.          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  4589.           (ck_krb5_is_tgt_valid() > 0)) )
  4590.         ck_krb5_autoget_TGT(realm);
  4591.  
  4592.     r = k5_get_ccache(k5_context,&ccache,NULL);
  4593.     if ( r ) {
  4594.         com_err(NULL, r, "while authorizing (0).");
  4595.         krb5_errno = r;
  4596.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4597.         return(0);
  4598.     }
  4599.  
  4600. #ifndef HEIMDAL
  4601.     memset((char *)&creds, 0, sizeof(creds));
  4602.     if (r = krb5_sname_to_principal(k5_context, szHostName,
  4603.                                 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4604.                                 KRB5_NT_SRV_HST, &creds.server)) {
  4605.         com_err(NULL, r, "while authorizing (1).");
  4606.         krb5_errno = r;
  4607.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4608.         return(0);
  4609.     }
  4610.  
  4611.     if (forward_flag) {
  4612.         if (fwd_server) {
  4613.             krb5_free_principal(k5_context,fwd_server);
  4614.             fwd_server = NULL;
  4615.         }
  4616.         krb5_copy_principal(k5_context,creds.server,&fwd_server);
  4617.     }
  4618.  
  4619.     if (r = krb5_cc_get_principal(k5_context, ccache, &creds.client)) {
  4620.         com_err(NULL, r, "while authorizing (2).");
  4621.         krb5_free_cred_contents(k5_context, &creds);
  4622.         krb5_errno = r;
  4623.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4624.         return(0);
  4625.     }
  4626.  
  4627.     if (szUserName[0] == '\0') {                /* Get user name now */
  4628.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4629.         if ( len < sizeof(szUserName) ) {
  4630.             memcpy(szUserName,
  4631.                     krb5_princ_component(k5_context, creds.client, 0)->data,
  4632.                     len);                       /* safe */
  4633.         } else
  4634.             len = 0;
  4635.         szUserName[len] = '\0';
  4636.     } else {
  4637.         char * name = NULL;
  4638.         len  = krb5_princ_component(k5_context, creds.client, 0)->length;
  4639.         if ( len == strlen(szUserName) ) {
  4640.             name = krb5_princ_component(k5_context, creds.client, 0)->data;
  4641. #ifdef OS2
  4642.             if ( !strnicmp(szUserName,name,len) )
  4643.                 memcpy(szUserName,name,len);    /* safe */
  4644. #endif /* OS2 */
  4645.         }
  4646.     }
  4647.  
  4648.     /* Not sure if this is necessary anymore.  What impact does it have
  4649.      * on Win2000 TGTs that use DES_CBC_MD5 or RC4_HMAC?
  4650.      *
  4651.      * This prevents using 3DES Service Tickets.
  4652.      */
  4653.     creds.keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  4654.     if (r = krb5_get_credentials(k5_context, 0,
  4655.                                   ccache, &creds, &new_creds)) {
  4656.         com_err(NULL, r, "while authorizing (3).");
  4657.         krb5_free_cred_contents(k5_context, &creds);
  4658.         krb5_errno = r;
  4659.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4660.         return(0);
  4661.     }
  4662. #endif /* HEIMDAL */
  4663.  
  4664.     if (auth_context) {
  4665.         krb5_auth_con_free(k5_context, auth_context);
  4666.         auth_context = 0;
  4667.     }
  4668.     if (r = krb5_auth_con_init(k5_context, &auth_context)) {
  4669.         com_err(NULL, r, "while initializing auth context");
  4670.         krb5_errno = r;
  4671.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4672.         return(0);
  4673.     }
  4674.  
  4675.     /* UPDATE for START_TLS.  AUTH_ENCRYPT_START_TLS and inclusion of */
  4676.     /* client and then server finished messages.                      */
  4677.  
  4678.     type_check[0] = AUTHTYPE_KERBEROS_V5;
  4679.     type_check[1] = AUTH_CLIENT_TO_SERVER |
  4680.         (how ? AUTH_HOW_MUTUAL : AUTH_HOW_ONE_WAY) |
  4681.         (encrypt) |
  4682.         (forward ? INI_CRED_FWD_ON : INI_CRED_FWD_OFF);
  4683. #ifdef CK_SSL
  4684.     if (encrypt == AUTH_ENCRYPT_START_TLS) {
  4685.         ssl_get_client_finished(&type_check[2],12);
  4686.         ssl_get_server_finished(&type_check[14],12);
  4687.     }
  4688. #endif /* CK_SSL */
  4689.  
  4690. #ifndef HEIMDAL
  4691.     checksum.magic = KV5M_DATA;
  4692. #endif /* HEIMDAL */
  4693.     checksum.length =
  4694. #ifdef CK_SSL
  4695.         (encrypt == AUTH_ENCRYPT_START_TLS) ? 26 :
  4696. #endif /* CK_SSL */
  4697.         2;
  4698.     checksum.data = (char *)&type_check;
  4699.  
  4700.     ap_opts = 0;
  4701.     if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
  4702.         ap_opts |= AP_OPTS_MUTUAL_REQUIRED;
  4703.  
  4704. #ifdef HEIMDAL
  4705.     r = krb5_auth_setkeytype(k5_context, auth_context, KEYTYPE_DES);
  4706.     if (r)
  4707.         com_err(NULL, r, "while setting auth keytype");
  4708.     r = krb5_auth_con_setaddrs_from_fd(k5_context,auth_context, &ttyfd);
  4709.     if (r)
  4710.         com_err(NULL, r, "while setting auth addrs");
  4711.     r = krb5_mk_req(k5_context, &auth_context, ap_opts,
  4712.                     krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  4713.                     szHostName, &checksum, ccache, &k5_auth);
  4714.     if (r)
  4715.         com_err(NULL, r, "while making request");
  4716. #else /* HEIMDAL */
  4717.     auth_flags = KRB5_AUTH_CONTEXT_RET_TIME;
  4718. #ifdef CK_ENCRYPTION
  4719.     ap_opts |= AP_OPTS_USE_SUBKEY;
  4720. #endif /* CK_ENCRYPTION */
  4721. #ifdef TLS_VERIFY
  4722.     if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  4723.         auth_flags |= KRB5_AUTH_CONTEXT_DO_SEQUENCE;
  4724.         if (!krb5_d_no_addresses)
  4725.             r = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  4726.                                  KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  4727.     }
  4728. #endif /* CK_SSL */
  4729.     krb5_auth_con_setflags(k5_context, auth_context, auth_flags);
  4730.     r = krb5_mk_req_extended(k5_context, &auth_context, ap_opts,
  4731.                               &checksum, new_creds, &k5_auth);
  4732. #endif /* HEIMDAL */
  4733.  
  4734. #ifdef CK_ENCRYPTION
  4735.     if (!r) {
  4736.         r = krb5_auth_con_getlocalsubkey(k5_context, auth_context, &newkey);
  4737.         if (r)
  4738.             r = krb5_auth_con_getkey(k5_context, auth_context, &newkey);
  4739.  
  4740.         if (k5_session_key) {
  4741.             krb5_free_keyblock(k5_context, k5_session_key);
  4742.             k5_session_key = 0;
  4743.         }
  4744.     }
  4745.     if (newkey) {
  4746.         /*
  4747.         * keep the key in our private storage, but don't use it
  4748.         * yet---see kerberos5_reply() below
  4749.         */
  4750. #ifdef HEIMDAL
  4751.         if ((newkey->keytype == ETYPE_DES_CBC_CRC) ||
  4752.              (newkey->keytype == ETYPE_DES_CBC_MD5) ||
  4753.              (newkey->keytype == ETYPE_DES_CBC_MD4))
  4754.         {
  4755.             debug(F111,"k5_auth_send()","newkey->keytype",newkey->keytype);
  4756.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4757.         }
  4758. #else /* HEIMDAL */
  4759.         /* look for all possible DES keys first - just for compatibility */
  4760.         /* other key types are much less likely to be available          */
  4761.         if ((newkey->enctype == ENCTYPE_DES_CBC_CRC) ||
  4762.              (newkey->enctype == ENCTYPE_DES_CBC_MD5) ||
  4763.              (newkey->enctype == ENCTYPE_DES_CBC_MD4))
  4764.         {
  4765.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4766.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4767.         }
  4768.         else if ((new_creds->keyblock.enctype == ENCTYPE_DES_CBC_CRC) ||
  4769.                  (new_creds->keyblock.enctype == ENCTYPE_DES_CBC_MD5))
  4770.         {
  4771.             /* use the session key in credentials instead */
  4772.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4773.                    new_creds->keyblock.enctype);
  4774.             krb5_copy_keyblock(k5_context,
  4775.                                 &new_creds->keyblock, &k5_session_key);
  4776.         }
  4777.         else if (newkey->enctype != 0)
  4778.         {
  4779.             debug(F111,"k5_auth_send()","newkey->enctype",newkey->enctype);
  4780.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  4781.         }
  4782.         else if (new_creds->keyblock.enctype != 0)
  4783.         {
  4784.             /* use the session key in credentials instead */
  4785.             debug(F111,"k5_auth_send()","new_creds->keyblock.enctype",
  4786.                    new_creds->keyblock.enctype);
  4787.             krb5_copy_keyblock(k5_context,
  4788.                                 &new_creds->keyblock, &k5_session_key);
  4789.         }
  4790.         else {
  4791.             debug(F110,"k5_auth_send()","NO KEY in newkey",0);
  4792.         }
  4793. #endif /* HEIMDAL */
  4794.         krb5_free_keyblock(k5_context, newkey);
  4795.     }
  4796. #endif /* CK_ENCRYPTION */
  4797. #ifndef HEIMDAL
  4798.     krb5_free_cred_contents(k5_context, &creds);
  4799.     krb5_free_creds(k5_context, new_creds);
  4800. #endif /* HEIMDAL */
  4801.     krb5_cc_close(k5_context,ccache);
  4802.  
  4803.     if (r) {
  4804.         com_err(NULL, r, "while authorizing (4).");
  4805.         krb5_errno = r;
  4806.         makestr(&krb5_errmsg,error_message(krb5_errno));
  4807.         return(0);
  4808.     }
  4809.     krb5_errno = 0;
  4810.     makestr(&krb5_errmsg,"OK");
  4811.     return(1);
  4812. }
  4813.  
  4814. /*
  4815.  * K5_auth_reply -- checks the reply for mutual authentication.
  4816.  */
  4817. static int
  4818. #ifdef CK_ANSIC
  4819. k5_auth_reply(int how, unsigned char *data, int cnt)
  4820. #else
  4821. k5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  4822. #endif
  4823. {
  4824. #ifdef CK_ENCRYPTION
  4825.     Session_Key skey;
  4826. #endif /* CK_ENCRYPTION */
  4827.  
  4828.     data += 4;                                  /* Point to status byte */
  4829.     cnt -=5;
  4830.  
  4831.     switch (*data++) {
  4832.     case KRB_REJECT:
  4833.         if (cnt > 0) {
  4834.             char *s;
  4835.             int len;
  4836.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication because\r\n",
  4837.                       sizeof(strTmp));
  4838.             len = strlen(strTmp);
  4839.             if ( len + cnt < sizeof(strTmp) ) {
  4840.                 s = strTmp + strlen(strTmp);
  4841.                 memcpy(s, data, cnt);           /* safe */
  4842.                 s[cnt] = 0;
  4843.             }
  4844.         } else
  4845.             ckstrncpy(strTmp,"Kerberos V5 refuses authentication",
  4846.                       sizeof(strTmp));
  4847.         krb5_errno = -1;
  4848.         makestr(&krb5_errmsg,strTmp);
  4849.         printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4850.         auth_finished(AUTH_REJECT);
  4851.         return AUTH_FAILURE;
  4852.  
  4853.     case KRB_ACCEPT:
  4854.         if (!mutual_complete) {
  4855.             if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL && !mutual_complete) {
  4856.                 ckstrncpy(strTmp,
  4857.                           "Kerberos V5 accepted you, but didn't provide"
  4858.                           " mutual authentication",sizeof(strTmp));
  4859.                 printf("Kerberos authentication failed!\r\n%s\r\n",strTmp);
  4860.                 krb5_errno = -1;
  4861.                 makestr(&krb5_errmsg,strTmp);
  4862.                 auth_finished(AUTH_REJECT);
  4863.                 return AUTH_FAILURE;
  4864.             }
  4865.  
  4866. #ifdef CK_ENCRYPTION
  4867.             if (k5_session_key) {
  4868.         /*
  4869.           Even if the session key is 3DES, we must lie because
  4870.           otherwise we can't negotiate the proper keys for DES
  4871.           encryption if the since the host may be requiring SK_DES
  4872.           key choice.
  4873.         */
  4874.                 skey.type = SK_DES;
  4875.                 skey.length = 8;
  4876. #ifdef HEIMDAL
  4877.                 skey.data = k5_session_key->keyvalue.data;
  4878. #else /* HEIMDAL */
  4879.                 skey.data = k5_session_key->contents;
  4880. #endif /* HEIMDAL */
  4881.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  4882.             }
  4883. #endif /* CK_ENCRYPTION */
  4884.         }
  4885.         if ( cnt > 0 ) {
  4886.             char *s;
  4887.             int len;
  4888.             ckstrncpy(strTmp,"Kerberos V5 accepts you as ",sizeof(strTmp));
  4889.             len = strlen(strTmp);
  4890.             if ( len + cnt < sizeof(strTmp) ) {
  4891.                 s = strTmp + strlen(strTmp);
  4892.                 memcpy(s,data,cnt);
  4893.                 s[cnt] = 0;
  4894.             }
  4895.         }
  4896.         accept_complete = 1;
  4897.         printf("%s\r\n",strTmp);
  4898.  
  4899. #ifdef FORWARD
  4900.         if (forward_flag
  4901. #ifdef COMMENT
  4902.              /* Marc Horowitz <marc@mit.edu> has successfully argued
  4903.                 that it is indeed safe to send Forwarded credentials
  4904.                 to an untrusted host.
  4905.               */
  4906.              && (auth_how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL
  4907. #endif /* COMMENT */
  4908.              )
  4909.             kerberos5_forward();
  4910. #endif /* FORWARD */
  4911.         krb5_errno = 0;
  4912.         makestr(&krb5_errmsg,strTmp);
  4913.         auth_finished(AUTH_USER);
  4914.         return AUTH_SUCCESS;
  4915.  
  4916.     case KRB5_RESPONSE:
  4917. #ifdef TLS_VERIFY
  4918.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS &&
  4919.             !krb5_tls_verified) {
  4920.             printf(
  4921.     "Man in the middle attack detected.  Session terminated.\r\n");
  4922. #ifndef BETATEST
  4923.             netclos();
  4924. #endif /* BETATEST */
  4925.             krb5_errno = -1;
  4926.             makestr(&krb5_errmsg,"TLS not verified");
  4927.             auth_finished(AUTH_REJECT);
  4928.             return AUTH_FAILURE;
  4929.         }
  4930.         if((ssl_active_flag || tls_active_flag) &&
  4931.             (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  4932.             printf("TLS session parameters verified by Kerberos 5\r\n");
  4933.         }
  4934. #endif /* TLS_VERIFY */
  4935.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  4936.             /* the rest of the reply should contain a krb_ap_rep */
  4937.             krb5_ap_rep_enc_part *reply;
  4938.             krb5_data inbuf;
  4939.             krb5_error_code r;
  4940.  
  4941.             inbuf.length = cnt;
  4942.             inbuf.data = (char *)data;
  4943.  
  4944.             if (r = krb5_rd_rep(k5_context, auth_context, &inbuf, &reply)) {
  4945.                 com_err(NULL, r, "while authorizing. (5)");
  4946.                 krb5_errno = r;
  4947.                 makestr(&krb5_errmsg,error_message(krb5_errno));
  4948.                 auth_finished(AUTH_REJECT);
  4949.                 return AUTH_FAILURE;
  4950.             }
  4951.             krb5_free_ap_rep_enc_part(k5_context, reply);
  4952.  
  4953. #ifdef CK_ENCRYPTION
  4954.             if (encrypt_flag && k5_session_key) {
  4955.         /*
  4956.           Even if the session key is 3DES, we must lie because
  4957.           otherwise we can't negotiate the proper keys for DES
  4958.           encryption if the since the host may be requiring SK_DES
  4959.           key choice.
  4960.         */
  4961.                 skey.type = SK_DES;
  4962.                 skey.length = 8;
  4963. #ifdef HEIMDAL
  4964.                 skey.data = k5_session_key->keyvalue.data;
  4965. #else /* HEIMDAL */
  4966.                 skey.data = k5_session_key->contents;
  4967. #endif /* HEIMDAL */
  4968.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  4969.             }
  4970. #endif /* ENCRYPTION */
  4971.             mutual_complete = 1;
  4972.         }
  4973.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  4974.                   sizeof(strTmp));
  4975.         krb5_errno = 0;
  4976.         makestr(&krb5_errmsg,strTmp);
  4977.         printf("%s\r\n",strTmp);
  4978.         auth_finished(AUTH_USER);
  4979.         return AUTH_SUCCESS;
  4980.  
  4981. #ifdef FORWARD
  4982.     case KRB5_FORWARD_ACCEPT:
  4983.         forwarded_tickets = 1;
  4984.         ckstrncpy(strTmp,"Remote machine has accepted forwarded credentials",
  4985.                   sizeof(strTmp));
  4986.         krb5_errno = 0;
  4987.         makestr(&krb5_errmsg,strTmp);
  4988.         printf("%s\r\n",strTmp);
  4989.         return AUTH_SUCCESS;
  4990.  
  4991.     case KRB5_FORWARD_REJECT:
  4992.         forwarded_tickets = 0;
  4993.         if (cnt > 0) {
  4994.             char *s;
  4995.             int len;
  4996.             len = ckstrncpy(strTmp,
  4997.                       "Kerberos V5 refuses forwarded credentials because ",
  4998.                        sizeof(strTmp));
  4999.             if ( len + cnt < sizeof(strTmp) ) {
  5000.                 s = strTmp + strlen(strTmp);
  5001.                 memcpy(s, data, cnt);
  5002.                 s[cnt] = 0;
  5003.             }
  5004.         } else
  5005.             ckstrncpy(strTmp, "Kerberos V5 refuses forwarded credentials",
  5006.                       sizeof(strTmp));
  5007.  
  5008.         printf("%s\r\n",strTmp);
  5009.         krb5_errno = -1;
  5010.         makestr(&krb5_errmsg,strTmp);
  5011.         return AUTH_SUCCESS;
  5012. #endif  /* FORWARD */
  5013.  
  5014. #ifdef TLS_VERIFY
  5015.     case KRB5_TLS_VERIFY:
  5016.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5017.             krb5_data reply, msg;
  5018.             char tls_verify[24];
  5019.             krb5_replay_data repdata;
  5020.             krb5_error_code r;
  5021.  
  5022.             ssl_get_server_finished(&tls_verify[0],12);
  5023.             ssl_get_client_finished(&tls_verify[12],12);
  5024.  
  5025.             reply.data = data;
  5026.             reply.length = cnt;
  5027.  
  5028.             if (!krb5_d_no_addresses)
  5029.                 krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5030.                                   KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
  5031.  
  5032.             if (r = krb5_rd_safe(k5_context,auth_context,&reply,&msg,&repdata))
  5033.               {
  5034.                 com_err("", r, "decoding tls verifier");
  5035.                 krb5_errno = r;
  5036.                 makestr(&krb5_errmsg,"TLS verify failure");
  5037.                 auth_finished(AUTH_REJECT);
  5038.                 return(AUTH_FAILURE);
  5039.             }
  5040.             if ( msg.length == 24 && !memcmp(msg.data,tls_verify,24) )
  5041.                  krb5_tls_verified = 1;
  5042.             krb5_free_data_contents(k5_context,&msg);
  5043.             if (krb5_tls_verified)
  5044.                 return(AUTH_SUCCESS);
  5045.         }
  5046.         printf("Man in the middle attack detected.  Session terminated.\r\n");
  5047.         netclos();
  5048.         krb5_errno = -1;
  5049.         makestr(&krb5_errmsg,"TLS verify failure");
  5050.         auth_finished(AUTH_REJECT);
  5051.         return(AUTH_FAILURE);
  5052. #endif /* CK_SSL */
  5053.  
  5054.     default:
  5055.         krb5_errno = -1;
  5056.         makestr(&krb5_errmsg,"Unknown reply type");
  5057.         auth_finished(AUTH_REJECT);
  5058.         return AUTH_FAILURE;                        /* Unknown reply type */
  5059.     }
  5060. }
  5061.  
  5062. #ifdef FORWARD
  5063. /* Decode, decrypt and store the forwarded creds in the local ccache. */
  5064. /* Needed for KRB5_FORWARD                                            */
  5065. static krb5_error_code
  5066. rd_and_store_for_creds(context, auth_context, inbuf, client)
  5067.     krb5_context context;
  5068.     krb5_auth_context auth_context;
  5069.     krb5_data *inbuf;
  5070.     krb5_const_principal client;
  5071. {
  5072.     krb5_creds ** creds=NULL;
  5073.     krb5_error_code retval;
  5074.     krb5_ccache ccache=NULL;
  5075.  
  5076. #ifdef HEIMDAL
  5077.     /*
  5078.     Heimdal Telnetd creates the cache file at this point and sets
  5079.     the KRB5CCNAME environment variable.
  5080.  
  5081.     struct passwd *pwd;
  5082.     char ccname[1024];
  5083.  
  5084.     pwd = getpwnam(szUserNameRequested);
  5085.     if (pwd == NULL)
  5086.         break;
  5087.     snprintf(ccname, sizeof(ccname)-1, "FILE:/tmp/krb5cc_%u",pwd->pw_uid);
  5088.     retval = krb5_cc_resolve(context,ccname,&ccache);
  5089.  
  5090.     chown(ccname + 5, pwd->pw_uid, -1);
  5091.     */
  5092. #endif /* HEIMDAL */
  5093.  
  5094.     if (retval = k5_get_ccache(context,&ccache,NULL))
  5095.         return(retval);
  5096.  
  5097. #ifdef HEIMDAL
  5098.     if ((retval = krb5_cc_initialize(context, ccache, client)))
  5099.         return(retval);
  5100.  
  5101.     if ((retval = krb5_rd_cred(context, auth_context, ccache, inbuf)))
  5102.         return(retval);
  5103. #else /* HEIMDAL */
  5104.     if ((retval = krb5_rd_cred(context, auth_context, inbuf, &creds, NULL)))
  5105.         return(retval);
  5106.  
  5107.     if ((retval = krb5_cc_initialize(context, ccache, creds[0]->client)))
  5108.         goto cleanup;
  5109.  
  5110.     if ((retval = krb5_cc_store_cred(context, ccache, creds[0])))
  5111.         goto cleanup;
  5112.  
  5113.     if ((retval = krb5_cc_close(context, ccache)))
  5114.         goto cleanup;
  5115.  
  5116.   cleanup:
  5117.     krb5_free_tgt_creds(context, creds);
  5118. #endif /* HEIMDAL */
  5119.     return retval;
  5120. }
  5121. #endif /* FORWARD */
  5122.  
  5123. /*
  5124.  *
  5125.  * K5_auth_is.
  5126.  *
  5127.  */
  5128.  
  5129. static int
  5130. #ifdef CK_ANSIC
  5131. k5_auth_is(int how, unsigned char *data, int cnt)
  5132. #else
  5133. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  5134. #endif
  5135. {
  5136.     int r = 0;
  5137.     krb5_principal server;
  5138.     krb5_keyblock *newkey = NULL;
  5139.     krb5_data outbuf;
  5140.     char errbuf[128]="";
  5141.     char *getenv();
  5142. #ifndef HEIMDAL
  5143.     krb5_authenticator *authenticator;
  5144.     krb5_keytab keytabid = 0;
  5145. #endif /* HEIMDAL */
  5146.     krb5_data inbuf;
  5147. #ifdef CK_ENCRYPTION
  5148.     Session_Key skey;
  5149. #endif /* CK_ENCRYPTION */
  5150.     char princ[256]="";
  5151.     int len;
  5152.  
  5153.     data += 4;                                  /* Point to status byte */
  5154.     cnt -= 4;
  5155.  
  5156.     hexdump("k5_auth_is data",data,cnt);
  5157.     debug(F111,"k5_auth_is","how",how);
  5158.  
  5159.     if (cnt-- < 1) {
  5160.         auth_finished(AUTH_REJECT);
  5161.         return AUTH_FAILURE;
  5162.     }
  5163.     switch (*data++) {
  5164.     case KRB_AUTH:
  5165.         k5_auth.data = (char *)data;
  5166.         k5_auth.length = cnt;
  5167.  
  5168.         debug(F110,"k5_auth_is","KRB_AUTH",0);
  5169.         debug(F111,"k5_auth_is","auth_context",auth_context);
  5170.  
  5171.         if (!r && !auth_context) {
  5172.             r = krb5_auth_con_init(k5_context, &auth_context);
  5173.             debug(F111,"k5_auth_is","krb5_auth_con_init",r);
  5174.         }
  5175.  
  5176. #ifdef HEIMDAL
  5177.         if (!r)
  5178.             r = krb5_auth_con_setaddrs_from_fd(k5_context,auth_context,&ttyfd);
  5179.  
  5180.         if (!r)
  5181.             r = krb5_sock_to_principal(k5_context,0,"host",
  5182.                                        KRB5_NT_SRV_HST,&server);
  5183.  
  5184.         if (!r)
  5185. #else /* HEIMDAL */
  5186.         if (!r) {
  5187.             krb5_rcache rcache = NULL;
  5188.  
  5189.             r = krb5_auth_con_getrcache(k5_context, auth_context,
  5190.                                          &rcache);
  5191.             debug(F111,"k5_auth_is","krb5_auth_con_getrcache",r);
  5192.  
  5193.             if (!r && !rcache) {
  5194.                 /* Do not resolve server's principal name, we will check */
  5195.                 /* for validity after the krb5_rd_req() call.            */
  5196.                 r = krb5_sname_to_principal(k5_context, 0, 0,
  5197.                                              KRB5_NT_SRV_HST, &server);
  5198.                 debug(F111,"k5_auth_is","krb5_sname_to_principal",r);
  5199.  
  5200.                 if (!r) {
  5201.                     r = krb5_get_server_rcache(k5_context,
  5202.                         krb5_princ_component(k5_context, server, 0),
  5203.                                                 &rcache);
  5204.                     debug(F111,"k5_auth_is","krb5_get_server_rcache",r);
  5205.                     krb5_free_principal(k5_context, server);
  5206.                 }
  5207.             }
  5208.             if (!r) {
  5209.                 r = krb5_auth_con_setrcache(k5_context,
  5210.                                              auth_context, rcache);
  5211.                 debug(F111,"k5_auth_is","krb5_auth_con_setrcache",r);
  5212.             }
  5213.         }
  5214.         if (!r && k5_keytab) {
  5215.             r = krb5_kt_resolve(k5_context,
  5216.                                  k5_keytab, &keytabid);
  5217.             debug(F111,"k5_auth_is","krb5_kt_resolve",r);
  5218.         }
  5219. #endif /* HEIMDAL */
  5220.         if (!r) {
  5221.             r = krb5_rd_req(k5_context, &auth_context, &k5_auth,
  5222. #ifdef HEIMDAL
  5223.                              server, NULL, NULL,
  5224. #else /* HEIMDAL */
  5225.                              NULL, keytabid, NULL,
  5226. #endif /* HEIMDAL */
  5227.                              &k5_ticket);
  5228.             debug(F111,"k5_auth_is","krb5_rd_req",r);
  5229.         }
  5230.         if (r) {
  5231.             (void) ckstrncpy(errbuf, "krb5_rd_req failed: ",sizeof(errbuf));
  5232.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5233.             goto errout;
  5234.         }
  5235. #ifdef HEIMDAL
  5236.         krb5_free_principal(k5_context, server);
  5237.  
  5238.         {
  5239.             char type_check[26];
  5240.  
  5241.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5242.             /* client and then server finished messages. */
  5243.  
  5244.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5245.             type_check[1] = how;        /* not broken into parts */
  5246. #ifdef CK_SSL
  5247.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5248.                 ssl_get_client_finished(&type_check[2],12);
  5249.                 ssl_get_server_finished(&type_check[14],12);
  5250.                 hexdump("k5_auth_is type_check",type_check,26);
  5251.             }
  5252. #endif /* CK_SSL */
  5253.  
  5254.             r = krb5_verify_authenticator_checksum(k5_context,
  5255.                                                     auth_context,
  5256.                                                     type_check,
  5257. #ifdef CK_SSL
  5258.                 ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5259. #endif /* CK_SSL */
  5260.                                                     2);
  5261.         }
  5262. #else /* HEIMDAL */
  5263.         len = krb5_princ_component(k5_context,k5_ticket->server,0)->length;
  5264.         if (len < 256)
  5265.         {
  5266.             memcpy(princ,
  5267.                    krb5_princ_component(k5_context,k5_ticket->server,0)->data,
  5268.                    len);
  5269.             princ[len] = '\0';
  5270.         }
  5271.         if ( strcmp((krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME), princ) )
  5272.         {
  5273.             debug(F110,"k5_auth_is incorrect service name",princ,0);
  5274.             ckstrncpy(errbuf,"incorrect service name: ",sizeof(errbuf));
  5275.             ckstrncat(errbuf,krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5276.                      sizeof(errbuf));
  5277.             ckstrncat(errbuf," != ",sizeof(errbuf));
  5278.             ckstrncat(errbuf,princ,sizeof(errbuf));
  5279.             goto errout;
  5280.         }
  5281.  
  5282.         r = krb5_auth_con_getauthenticator(k5_context,
  5283.                                             auth_context,
  5284.                                             &authenticator);
  5285.         debug(F111,"k5_auth_is","krb5_auth_con_getauthenticator",r);
  5286.         if (r) {
  5287.             (void) ckstrncpy(errbuf,
  5288.                              "krb5_auth_con_getauthenticator failed: ",
  5289.                              sizeof(errbuf)
  5290.                              );
  5291.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5292.             goto errout;
  5293.         }
  5294.  
  5295.         if (authenticator->checksum) {
  5296.             char type_check[26];
  5297.             krb5_checksum *cksum = authenticator->checksum;
  5298.             krb5_keyblock *key;
  5299.  
  5300.             /* UPDATE for START_TLS. AUTH_ENCRYPT_START_TLS and inclusion of */
  5301.             /* client and then server finished messages. */
  5302.  
  5303.             type_check[0] = AUTHTYPE_KERBEROS_V5;
  5304.             type_check[1] = how;        /* not broken into parts */
  5305. #ifdef CK_SSL
  5306.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5307.                 ssl_get_client_finished(&type_check[2],12);
  5308.                 ssl_get_server_finished(&type_check[14],12);
  5309.                 hexdump("k5_auth_is type_check",type_check,26);
  5310.             }
  5311. #endif /* CK_SSL */
  5312.  
  5313.             r = krb5_auth_con_getkey(k5_context, auth_context,
  5314.                                       &key);
  5315.             debug(F111,"k5_auth_is","krb5_auth_con_getkey",r);
  5316.             if (r) {
  5317.                 (void) ckstrncpy(errbuf, "krb5_auth_con_getkey failed: ",
  5318.                                   sizeof(errbuf));
  5319.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5320.                 goto errout;
  5321.             }
  5322.  
  5323.             r = krb5_verify_checksum(k5_context,
  5324.                                       cksum->checksum_type,
  5325.                                       cksum,
  5326.                                       &type_check,
  5327.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  5328.                                       2,
  5329.                                       key->contents,
  5330.                                       key->length
  5331.                                       );
  5332.             debug(F111,"k5_auth_is","krb5_verify_checksum",r);
  5333.             if (r) {
  5334.                 (void) ckstrncpy(errbuf,
  5335.                                  "checksum verification failed: ",
  5336.                                  sizeof(errbuf)
  5337.                                  );
  5338.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5339.                 goto errout;
  5340.             }
  5341.             krb5_free_keyblock(k5_context, key);
  5342.         } else {
  5343.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_USING_TELOPT) {
  5344.                 (void) strcpy(errbuf,
  5345.                                "authenticator is missing required checksum");
  5346.                 goto errout;
  5347.             }
  5348.         }
  5349.  
  5350.         krb5_free_authenticator(k5_context, authenticator);
  5351. #endif /* HEIMDAL */
  5352.  
  5353. #ifdef TLS_VERIFY
  5354.         if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  5355.             krb5_data in, msg;
  5356.             char tls_verify[24];
  5357.             krb5_replay_data repdata;
  5358.  
  5359.             ssl_get_server_finished(&tls_verify[0],12);
  5360.             ssl_get_client_finished(&tls_verify[12],12);
  5361.  
  5362.             in.data = tls_verify;
  5363.             in.length = 24;
  5364.  
  5365.             if (!krb5_d_no_addresses)
  5366.                 krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  5367.                                    KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
  5368.             if (r = krb5_mk_safe(k5_context,auth_context,&in,&msg,&repdata)) {
  5369.                 com_err("", r, "encoding tls verifier");
  5370.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5371.                 goto errout;
  5372.             }
  5373.             SendK5AuthSB(KRB5_TLS_VERIFY, msg.data, msg.length);
  5374.             krb5_free_data_contents(k5_context,&msg);
  5375.         }
  5376. #endif /* CK_SSL */
  5377.         if ((how & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
  5378.             /* do ap_rep stuff here */
  5379.             if ((r = krb5_mk_rep(k5_context,
  5380. #ifdef HEIMDAL
  5381.                                   &auth_context,
  5382. #else /* HEIMDAL */
  5383.                                   auth_context,
  5384. #endif /* HEIMDAL */
  5385.                                   &outbuf))) {
  5386.                 debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5387.                 (void) ckstrncpy(errbuf, "Make reply failed: ",sizeof(errbuf));
  5388.                 (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5389.                 goto errout;
  5390.             }
  5391.             debug(F111,"k5_auth_is","krb5_mk_rep",r);
  5392.  
  5393.             SendK5AuthSB(KRB5_RESPONSE, outbuf.data, outbuf.length);
  5394.             mutual_complete = 1;
  5395.         }
  5396.  
  5397. #ifdef HEIMDAL
  5398.         {
  5399.             char * name = NULL;
  5400.             if (krb5_unparse_name(k5_context, k5_ticket->client,
  5401.                                    &name))
  5402.             {
  5403.                 szUserNameAuthenticated[0] = '\0';
  5404.             } else {
  5405.                 ckstrncpy(szUserNameAuthenticated,UIDBUFLEN,name);
  5406.                 free(name);
  5407.             }
  5408.         }
  5409. #else /* HEIMDAL */
  5410.         if ( krb5_aname_to_localname(k5_context,
  5411.                                       k5_ticket->enc_part2->client,
  5412.                                       UIDBUFLEN,szUserNameAuthenticated) )
  5413.             szUserNameAuthenticated[0] = '\0';
  5414. #endif /* HEIMDAL */
  5415.  
  5416.         SendK5AuthSB(KRB_ACCEPT, szUserNameAuthenticated,
  5417.                       szUserNameAuthenticated[0] ? -1 : 0);
  5418.         accept_complete = 1;
  5419.         ckmakmsg(strTmp,sizeof(strTmp),
  5420.                  "Kerberos5 identifies him as ``",
  5421.                  szUserNameAuthenticated,"''",NULL);
  5422.         printf("%s\r\n",strTmp);
  5423.  
  5424.         if (szUserNameRequested[0] &&
  5425.             krb5_kuserok(k5_context,
  5426. #ifdef HEIMDAL
  5427.                           k5_ticket->client,
  5428. #else /* HEIMDAL */
  5429.                           k5_ticket->enc_part2->client,
  5430. #endif /* HEIMDAL */
  5431.                           szUserNameRequested))
  5432.             auth_finished(AUTH_VALID);
  5433.         else
  5434.             auth_finished(AUTH_USER);
  5435.  
  5436.         krb5_auth_con_getremotesubkey(k5_context, auth_context,
  5437.                                        &newkey);
  5438.         if (k5_session_key) {
  5439.             krb5_free_keyblock(k5_context, k5_session_key);
  5440.             k5_session_key = 0;
  5441.         }
  5442.         if (newkey) {
  5443.             krb5_copy_keyblock(k5_context, newkey, &k5_session_key);
  5444.             krb5_free_keyblock(k5_context, newkey);
  5445.         } else {
  5446.             krb5_copy_keyblock(k5_context,
  5447. #ifdef HEIMDAL
  5448.                                 &k5_ticket->ticket.key,
  5449. #else /* HEIMDAL */
  5450.                                 k5_ticket->enc_part2->session,
  5451. #endif /* HEIMDAL */
  5452.                                 &k5_session_key);
  5453.         }
  5454.  
  5455. #ifdef CK_ENCRYPTION
  5456. #ifdef HEIMDAL
  5457.         skey.type = k5_session_key->keyvalue.length == 8 ? SK_DES : SK_GENERIC;
  5458.         skey.length = k5_session_key->keyvalue.length;
  5459.         skey.data = k5_session_key->keyvalue.data;
  5460. #else /* HEIMDAL */
  5461.         skey.type = k5_session_key->length == 8 ? SK_DES : SK_GENERIC;
  5462.         skey.length = k5_session_key->length;
  5463.         skey.data = k5_session_key->contents;
  5464. #endif /* HEIMDAL */
  5465.         encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  5466. #endif /* CK_ENCRYPTION */
  5467.         debug(F100,"k5_auth_is AUTH_SUCCESS","",0);
  5468.         krb5_errno = r;
  5469.         if ( krb5_errno )
  5470.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5471.         else
  5472.             makestr(&krb5_errmsg,strTmp);
  5473.         return AUTH_SUCCESS;
  5474.  
  5475. #ifdef FORWARD
  5476.     case KRB5_FORWARD:
  5477.         if ( !forward_flag ) {
  5478.             SendK5AuthSB(KRB5_FORWARD_REJECT,
  5479.                           "forwarded credentials are being refused.",
  5480.                           -1);
  5481.             return(AUTH_SUCCESS);
  5482.         }
  5483.  
  5484.         inbuf.length = cnt;
  5485.         inbuf.data = (char *)data;
  5486.         if (
  5487. #ifndef HEIMDAL
  5488.             (!krb5_d_no_addresses &&
  5489.             (r = krb5_auth_con_genaddrs(k5_context,auth_context,g_kstream->fd,
  5490.                               KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))) ||
  5491. #endif /* HEIMDAL */
  5492.             (r = rd_and_store_for_creds(k5_context, auth_context,&inbuf,
  5493. #ifdef HEIMDAL
  5494.                                          k5_ticket->client
  5495. #else /* HEIMDAL */
  5496.                                          k5_ticket->enc_part2->client
  5497. #endif /* HEIMDAL */
  5498.                                          ))) {
  5499.             (void) ckstrncpy(errbuf, "Read forwarded creds failed: ",
  5500.                               sizeof(errbuf));
  5501.             (void) ckstrncat(errbuf, error_message(r),sizeof(errbuf));
  5502.             SendK5AuthSB(KRB5_FORWARD_REJECT, errbuf, -1);
  5503.             printf("Could not read forwarded credentials\r\n");
  5504.             krb5_errno = r;
  5505.             makestr(&krb5_errmsg,error_message(krb5_errno));
  5506.         }
  5507.         else {
  5508.             SendK5AuthSB(KRB5_FORWARD_ACCEPT, 0, 0);
  5509.             ckstrncpy(strTmp,"Forwarded credentials obtained",sizeof(strTmp));
  5510.             printf("%s\r\n",strTmp);
  5511.             krb5_errno = 0;
  5512.             makestr(&krb5_errmsg,strTmp);
  5513.         }
  5514.         /* A failure to accept forwarded credentials is not an */
  5515.         /* authentication failure.                             */
  5516.         return AUTH_SUCCESS;
  5517. #endif  /* FORWARD */
  5518.     default:
  5519.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  5520.         SendK5AuthSB(KRB_REJECT, 0, 0);
  5521.         break;
  5522.     }
  5523.     auth_finished(AUTH_REJECT);
  5524.     return AUTH_FAILURE;
  5525.  
  5526.   errout:
  5527.     SendK5AuthSB(KRB_REJECT, errbuf, -1);
  5528.     krb5_errno = r;
  5529.     makestr(&krb5_errmsg,errbuf);
  5530.     printf("%s\r\n", errbuf);
  5531.     if (auth_context) {
  5532.         krb5_auth_con_free(k5_context, auth_context);
  5533.         auth_context = 0;
  5534.     }
  5535.     auth_finished(AUTH_REJECT);
  5536.     return AUTH_FAILURE;
  5537. }
  5538.  
  5539. #ifdef FORWARD
  5540. int
  5541. #ifdef CK_ANSIC
  5542. kerberos5_forward(void)
  5543. #else
  5544. kerberos5_forward()
  5545. #endif
  5546. {
  5547.     krb5_error_code r;
  5548.     krb5_ccache ccache=NULL;
  5549.     krb5_principal client = 0;
  5550.     krb5_principal server = 0;
  5551.     krb5_data forw_creds;
  5552. #ifdef HEIMDAL
  5553.     krb5_creds      creds;
  5554. #endif /* HEIMDAL */
  5555.  
  5556.     forw_creds.data = 0;
  5557.  
  5558.     r = k5_get_ccache(k5_context,&ccache,NULL);
  5559.     if ( r ) {
  5560.         com_err(NULL, r, "Kerberos V5: could not get default ccache");
  5561.         krb5_errno = r;
  5562.         makestr(&krb5_errmsg,error_message(krb5_errno));
  5563.         return(AUTH_FAILURE);
  5564.     }
  5565.  
  5566.     if ((r = krb5_cc_get_principal(k5_context, ccache, &client))) {
  5567.         com_err(NULL, r, "Kerberos V5: could not get default principal");
  5568.         goto cleanup;
  5569.     }
  5570.  
  5571. #ifdef HEIMDAL
  5572.     memset(&creds, 0, sizeof(creds));
  5573.     creds.client = client;
  5574.  
  5575.     if (r = krb5_build_principal(k5_context,
  5576.                              &creds.server,
  5577.                              strlen(client->realm),
  5578.                               client->realm,
  5579.                               "krbtgt",
  5580.                               client->realm,
  5581.                                   NULL)) {
  5582.         com_err(NULL, r, "Kerberos V5: could not get principal");
  5583.         goto cleanup;
  5584.     }
  5585.  
  5586.     creds.times.endtime = 0;
  5587.  
  5588.     if (r = krb5_get_forwarded_creds(k5_context,
  5589.                                       auth_context,
  5590.                                       ccache,
  5591.                                       0,
  5592.                                       szHostName,
  5593.                                       &creds,
  5594.                                       &forw_creds)) {
  5595.         com_err(NULL, r, "Kerberos V5: error getting forwarded creds");
  5596.         goto cleanup;
  5597.     }
  5598. #else /* HEIMDAL */
  5599.     /* we should not need to make this call since we are storing the */
  5600.     /* server's principal in fwd_server from our call to             */
  5601.     /* krb5_sname_to_principal() in k5_auth_send()                   */
  5602.     if (fwd_server == NULL) {
  5603.         if ((r = krb5_sname_to_principal(k5_context, szHostName,
  5604.                                  krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5605.                                           KRB5_NT_SRV_HST, &server))) {
  5606.             com_err(NULL, r, "Kerberos V5: could not make server principal");
  5607.             goto cleanup;
  5608.         }
  5609.     }
  5610.  
  5611.     if (!krb5_d_no_addresses &&
  5612.         (r = krb5_auth_con_genaddrs(k5_context, auth_context, g_kstream->fd,
  5613.                              KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
  5614.     {
  5615.         com_err(NULL, r, "Kerberos V5: could not gen local full address");
  5616.         goto cleanup;
  5617.     }
  5618.  
  5619.     if (r = krb5_fwd_tgt_creds(k5_context, auth_context, 0, client,
  5620.                                 fwd_server ? fwd_server : server,
  5621.                                 ccache, forwardable_flag, &forw_creds)) {
  5622.         com_err(NULL, r, "Kerberos V5: error getting forwardable credentials");
  5623.         goto cleanup;
  5624.     }
  5625. #endif /* HEIMDAL */
  5626.  
  5627.     /* Send forwarded credentials */
  5628.     if (!SendK5AuthSB(KRB5_FORWARD, forw_creds.data, forw_creds.length)) {
  5629.         printf("Kerberos V5 forwarding error!\r\n%s\r\n",
  5630.                     "Not enough room for authentication data");
  5631.     }
  5632.  
  5633. cleanup:
  5634.     if (client)
  5635.         krb5_free_principal(k5_context, client);
  5636.     if (server)
  5637.         krb5_free_principal(k5_context, server);
  5638. #ifdef HEIMDAL
  5639.     krb5_data_free(&forw_creds);
  5640. #else /* HEIMDAL */
  5641.     krb5_free_data_contents(k5_context,&forw_creds);
  5642. #endif /* HEIMDAL */
  5643.     krb5_cc_close(k5_context, ccache);
  5644.  
  5645.     krb5_errno = r;
  5646.     makestr(&krb5_errmsg,krb5_errno?error_message(krb5_errno):"OK");
  5647.     return(r?AUTH_FAILURE:AUTH_SUCCESS);
  5648. }
  5649. #endif /* FORWARD */
  5650. #else /* KRB5 */
  5651. int
  5652. ck_krb5_autoget_TGT(char * dummy)
  5653. {
  5654.     return(0);
  5655. }
  5656. #ifdef CK_KERBEROS
  5657. int
  5658. #ifdef CK_ANSIC
  5659. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  5660.                  struct krb4_init_data * k4_init)
  5661. #else
  5662. ck_krb5_initTGT(op,init,k4_init)
  5663.     krb_op_data * op; struct krb5_init_data * init;
  5664.     struct krb4_init_data * k4_init;
  5665. #endif /* CK_ANSIC*/
  5666. {
  5667.     return(-1);
  5668. }
  5669.  
  5670. int
  5671. #ifdef CK_ANSIC
  5672. ck_krb5_destroy(struct krb_op_data * op)
  5673. #else
  5674. ck_krb5_destroy(op) struct krb_op_data * op;
  5675. #endif
  5676. {
  5677.     return(-1);
  5678. }
  5679.  
  5680. int
  5681. #ifdef CK_ANSIC
  5682. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  5683. #else
  5684. ck_krb5_list_creds(op,lc)
  5685.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  5686. #endif
  5687. {
  5688.     return(-1);
  5689. }
  5690. #else /* CK_KERBEROS */
  5691. int
  5692. #ifdef CK_ANSIC
  5693. ck_krb5_initTGT(void * op, void * init, void * k4_init )
  5694. #else
  5695. ck_krb5_initTGT(op,init,k4_init)
  5696.     void * op; void * init; void * k4_init;
  5697. #endif /* CK_ANSIC*/
  5698. {
  5699.     return(-1);
  5700. }
  5701.  
  5702. int
  5703. #ifdef CK_ANSIC
  5704. ck_krb5_destroy(void * op)
  5705. #else
  5706. ck_krb5_destroy(op) void * op;
  5707. #endif
  5708. {
  5709.     return(-1);
  5710. }
  5711.  
  5712. int
  5713. #ifdef CK_ANSIC
  5714. ck_krb5_list_creds(void * op, void * lc)
  5715. #else
  5716. ck_krb5_list_creds(op,lc)
  5717.     void * op; void * lc;
  5718. #endif
  5719. {
  5720.     return(-1);
  5721. }
  5722. #endif /* CK_KERBEROS */
  5723. #endif /* KRB5 */
  5724.  
  5725. #ifdef GSSAPI_KRB5
  5726. /*
  5727.  *
  5728.  * gssk5_auth_send - gets authentication bits we need to send to KDC.
  5729.  *
  5730.  * Result is left in k5_auth
  5731.  *
  5732.  * Returns: 0 on failure, 1 on success
  5733.  *
  5734.  */
  5735.  
  5736. static int
  5737. #ifdef CK_ANSIC
  5738. gssk5_auth_send(int how, int encrypt, int forward)
  5739. #else
  5740. gssk5_auth_send(how,encrypt,forward) int how; int encrypt; int forward;
  5741. #endif
  5742. {
  5743.     OM_uint32 maj_stat, min_stat;
  5744. #ifdef KRB5
  5745.     char * realm = NULL;
  5746.     char tgt[256];
  5747. #endif /* KRB5 */
  5748.  
  5749.     gss_chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  5750.     gss_chan.initiator_address.length = 4;
  5751.     gss_chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  5752.     gss_chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  5753.     gss_chan.acceptor_address.length = 4;
  5754.     gss_chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  5755.     gss_chan.application_data.length = 0;
  5756.     gss_chan.application_data.value = 0;
  5757.  
  5758. #ifdef KRB5
  5759.     realm = ck_krb5_realmofhost(ftp_host);
  5760.     if (realm) {
  5761.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  5762.         debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  5763.         if ( krb5_autoget &&
  5764.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  5765.                 (ck_krb5_is_tgt_valid() > 0)) )
  5766.             ck_krb5_autoget_TGT(realm);
  5767.     }
  5768. #endif /* KRB5 */
  5769.  
  5770.     /* Blob from gss-client */
  5771.     /* host@hostname */
  5772.     /* the V5 GSSAPI binding canonicalizes this for us... */
  5773.     ckmakmsg(gss_stbuf,GSS_BUFSIZ,
  5774.              krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME,
  5775.              "@",
  5776.              szHostName,
  5777.               NULL
  5778.               );
  5779.     fprintf(stderr, "Authenticating to <%s>...\n", gss_stbuf);
  5780.     gss_send_tok.value = gss_stbuf;
  5781.     gss_send_tok.length = strlen(gss_stbuf);
  5782.     maj_stat = gss_import_name(&min_stat, &gss_send_tok,
  5783.                                 gss_nt_service_name,
  5784.                                 &gss_target_name
  5785.                                 );
  5786.     if (maj_stat != GSS_S_COMPLETE) {
  5787.         user_gss_error(maj_stat, min_stat, "parsing name");
  5788.         secure_error("name parsed <%s>\n", gss_stbuf);
  5789.         return(0);
  5790.     }
  5791.     token_ptr = GSS_C_NO_BUFFER;
  5792.     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  5793.  
  5794.     fprintf(stderr, "calling gss_init_sec_context\n");
  5795.     maj_stat =
  5796.         gss_init_sec_context(&min_stat,
  5797.                               GSS_C_NO_CREDENTIAL,
  5798.                               &gcontext,
  5799.                               gss_target_name,
  5800.                               gss_mech_krb5,
  5801.                               GSS_C_MUTUAL_FLAG |
  5802.                               GSS_C_REPLAY_FLAG |
  5803.                               ((forward && forward_flag) ?
  5804.                                 GSS_C_DELEG_FLAG : 0),
  5805.                               0,
  5806.                               (krb5_d_no_addresses ? /* channel bindings */
  5807.                                 GSS_C_NO_CHANNEL_BINDINGS :
  5808.                                 &gss_chan),
  5809.                               gss_token_ptr,
  5810.                               NULL,     /* ignore mech type */
  5811.                               &gss_send_tok,
  5812.                               NULL,     /* ignore ret_flags */
  5813.                               NULL
  5814.                               );        /* ignore time_rec */
  5815.  
  5816.  
  5817.         if (maj_stat != GSS_S_COMPLETE &&
  5818.              maj_stat != GSS_S_CONTINUE_NEEDED) {
  5819.             user_gss_error(maj_stat,
  5820.                             min_stat,
  5821.                             "initializing context"
  5822.                             );
  5823.             gss_release_name(&min_stat, &gss_target_name);
  5824.             return(0);
  5825.         }
  5826.         return(1);
  5827. }
  5828.  
  5829. /*
  5830.  * gssk5_auth_reply -- checks the reply for mutual authentication.
  5831.  */
  5832. static int
  5833. #ifdef CK_ANSIC
  5834. gssk5_auth_reply(int how, unsigned char *data, int cnt)
  5835. #else
  5836. gssk5_auth_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  5837. #endif
  5838. {
  5839.     data += 4;                                  /* Point to status byte */
  5840.     cnt -=5;
  5841.  
  5842.     switch (*data++) {
  5843.     case GSS_REJECT:
  5844.         if (cnt > 0) {
  5845.             char *s;
  5846.             int len;
  5847.             ckstrncpy(strTmp,"GSSAPI refuses authentication because\r\n",
  5848.                       sizeof(strTmp));
  5849.             len = strlen(strTmp);
  5850.             if ( len + cnt < sizeof(strTmp) ) {
  5851.                 s = strTmp + strlen(strTmp);
  5852.                 memcpy(s, data, cnt);           /* safe */
  5853.                 s[cnt] = 0;
  5854.             }
  5855.         } else
  5856.             ckstrncpy(strTmp,"GSSAPI refuses authentication",
  5857.                       sizeof(strTmp));
  5858.         printf("GSSAPI authentication failed!\r\n%s\r\n",strTmp);
  5859.         auth_finished(AUTH_REJECT);
  5860.         return AUTH_FAILURE;
  5861.  
  5862.     case GSS_ACCEPT:
  5863.         if ( cnt > 0 ) {
  5864.             char *s;
  5865.             int len;
  5866.             ckstrncpy(strTmp,"GSSAPI accepts you as ",sizeof(strTmp));
  5867.             len = strlen(strTmp);
  5868.             if ( len + cnt < sizeof(strTmp) ) {
  5869.                 s = strTmp + strlen(strTmp);
  5870.                 memcpy(s,data,cnt);
  5871.                 s[cnt] = 0;
  5872.             }
  5873.         }
  5874.         accept_complete = 1;
  5875.         printf("%s\r\n",strTmp);
  5876.         auth_finished(AUTH_USER);
  5877.         return AUTH_SUCCESS;
  5878.  
  5879.     case GSS_RESPONSE:
  5880.         gss_token_ptr = &gss_recv_tok;
  5881.         gss_recv_tok.value = data;
  5882.         gss_recv_tok.length = cnt;
  5883.  
  5884.         maj_stat =
  5885.             gss_init_sec_context(&min_stat,
  5886.                                   GSS_C_NO_CREDENTIAL,
  5887.                                   &gcontext,
  5888.                                   gss_target_name,
  5889.                                   gss_krb5_mech,
  5890.                                   GSS_C_MUTUAL_FLAG |
  5891.                                   GSS_C_REPLAY_FLAG |
  5892.                                   (forward_flag ?
  5893.                                     GSS_C_DELEG_FLAG : 0),
  5894.                                   0,
  5895.                                   (krb5_d_no_addresses ? /* channel bindings */
  5896.                                     GSS_C_NO_CHANNEL_BINDINGS :
  5897.                                     &gss_chan),
  5898.                                   gss_token_ptr,
  5899.                                   NULL, /* ignore mech type */
  5900.                                   &gss_send_tok,
  5901.                                   NULL, /* ignore ret_flags */
  5902.                                   NULL
  5903.                                   );    /* ignore time_rec */
  5904.  
  5905.         if ( maj_stat == GSS_S_COMPLETE )
  5906.         {
  5907.  
  5908.         } else if ( maj_stat == CSS_S_CONTINUE_NEEDED ) {
  5909.         } else {
  5910.         }
  5911.  
  5912.         ckstrncpy(strTmp,"Remote machine has been mutually authenticated",
  5913.                   sizeof(strTmp));
  5914.         printf("%s\r\n",strTmp);
  5915.         auth_finished(AUTH_USER);
  5916.         return AUTH_SUCCESS;
  5917.  
  5918.     default:
  5919.         auth_finished(AUTH_REJECT);
  5920.         return AUTH_FAILURE;                        /* Unknown reply type */
  5921.     }
  5922. }
  5923.  
  5924. /*
  5925.  *
  5926.  * gssk5_auth_is.
  5927.  *
  5928.  */
  5929.  
  5930. static int
  5931. #ifdef CK_ANSIC
  5932. k5_auth_is(int how, unsigned char *data, int cnt)
  5933. #else
  5934. k5_auth_is(how,data,cnt) int how; unsigned char *data; int cnt;
  5935. #endif
  5936. {
  5937.     int replied = 0;
  5938.     gss_cred_id_t server_creds, deleg_creds;
  5939.     gss_name_t client;
  5940.     int ret_flags;
  5941.     gss_buffer_desc name_buf;
  5942.     gss_name_t server_name;
  5943.     OM_uint32 acquire_maj,
  5944.       acquire_min,
  5945.       accept_maj,
  5946.       accept_min,
  5947.       stat_maj,
  5948.       stat_min;
  5949.     gss_OID mechid;
  5950.     gss_buffer_desc tok, out_tok;
  5951.     char gbuf[GSS_BUFSIZ];
  5952.     u_char gout_buf[GSS_BUFSIZ];
  5953.     char localname[MAXHOSTNAMELEN];
  5954.     char service_name[MAXHOSTNAMELEN+10];
  5955.     char **service;
  5956.     struct hostent *hp;
  5957.  
  5958.     data += 4;                                  /* Point to status byte */
  5959.     cnt -= 4;
  5960.  
  5961.     hexdump("gssk5_auth_is data",data,cnt);
  5962.     debug(F111,"gssk5_auth_is","how",how);
  5963.  
  5964.     if (cnt-- < 1) {
  5965.         auth_finished(AUTH_REJECT);
  5966.         return AUTH_FAILURE;
  5967.     }
  5968.     switch (*data++) {
  5969.     case GSS_AUTH:
  5970.         gss_chan.initiator_addrtype = GSS_C_AF_INET;
  5971.         gss_chan.initiator_address.length = 4;
  5972.         gss_chan.initiator_address.value = &his_addr.sin_addr.s_addr;
  5973.         gss_chan.acceptor_addrtype = GSS_C_AF_INET;
  5974.         gss_chan.acceptor_address.length = 4;
  5975.         gss_chan.acceptor_address.value = &ctrl_addr.sin_addr.s_addr;
  5976.         gss_chan.application_data.length = 0;
  5977.         gss_chan.application_data.value = 0;
  5978.  
  5979.         tok.value = data;
  5980.         tok.length = cnt;
  5981.  
  5982.         if (gethostname(localname, MAXHOSTNAMELEN)) {
  5983.             auth_finished(AUTH_REJECT);
  5984.             return AUTH_FAILURE;
  5985.         }
  5986.         if (!(hp = gethostbyname(localname))) {
  5987.             auth_finished(AUTH_REJECT);
  5988.             return AUTH_FAILURE;
  5989.         }
  5990. #ifdef HADDRLIST
  5991.         hp = ck_copyhostent(hp);
  5992. #endif /* HADDRLIST */
  5993.         strncpy(localname, hp->h_name, sizeof(localname) - 1);
  5994.         localname[sizeof(localname) - 1] = '\0';
  5995.  
  5996.         sprintf(service_name, "%s@%s", *service, localname);
  5997.         name_buf.value = service_name;
  5998.         name_buf.length = strlen(name_buf.value) + 1;
  5999.         stat_maj = gss_import_name(&stat_min, &name_buf,
  6000.                                     gss_nt_service_name,
  6001.                                     &server_name);
  6002.         if (stat_maj != GSS_S_COMPLETE) {
  6003.             auth_finished(AUTH_REJECT);
  6004.             return AUTH_FAILURE;
  6005.         }
  6006.  
  6007.         acquire_maj = gss_acquire_cred(&acquire_min, server_name, 0,
  6008.                                         GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
  6009.                                         &server_creds, NULL, NULL);
  6010.         (void) gss_release_name(&stat_min, &server_name);
  6011.  
  6012.         if (acquire_maj != GSS_S_COMPLETE) {
  6013.             reply_gss_error(535, accept_maj, accept_min,
  6014.                                  "accepting context");
  6015.             syslog(LOG_ERR, "failed accepting context");
  6016.             (void) gss_release_cred(&stat_min, &server_creds);
  6017.             if (ret_flags & GSS_C_DELEG_FLAG)
  6018.                 (void) gss_release_cred(&stat_min,
  6019.                                          &deleg_creds);
  6020.             return 0;
  6021.         }
  6022.  
  6023.         gcontext = GSS_C_NO_CONTEXT;
  6024.         accept_maj = gss_accept_sec_context(&accept_min,
  6025.                                             &gcontext, /* context_handle */
  6026.                                             /* verifier_cred_handle */
  6027.                                             server_creds,
  6028.                                             &tok, /* input_token */
  6029.                         (krb5_d_no_addresses ?
  6030.                          /* channel bindings */
  6031.                                                GSS_C_NO_CHANNEL_BINDINGS :
  6032.                                                &gss_chan),
  6033.                                              &client, /* src_name */
  6034.                                             &mechid, /* mech_type */
  6035.                                             &out_tok, /* output_token */
  6036.                                             &ret_flags,
  6037.                                             NULL,       /* ignore time_rec */
  6038.                                             /* forwarded credentials */
  6039.                                             &deleg_creds
  6040.                                             );
  6041.  
  6042.         if (accept_maj!=GSS_S_COMPLETE && accept_maj!=GSS_S_CONTINUE_NEEDED) {
  6043.             reply_gss_error(535, accept_maj, accept_min,
  6044.                              "accepting context");
  6045.             syslog(LOG_ERR, "failed accepting context");
  6046.             (void) gss_release_cred(&stat_min, &server_creds);
  6047.             if (ret_flags & GSS_C_DELEG_FLAG)
  6048.                 (void) gss_release_cred(&stat_min,
  6049.                                          &deleg_creds);
  6050.             return 0;
  6051.         }
  6052.  
  6053.         if (out_tok.length) {
  6054.             if (kerror = radix_encode(out_tok.value,gbuf,&out_tok.length, 0)) {
  6055.                 secure_error("Couldn't encode ADAT reply (%s)",
  6056.                              radix_error(kerror));
  6057.                 syslog(LOG_ERR, "couldn't encode ADAT reply");
  6058.                 (void) gss_release_cred(&stat_min, &server_creds);
  6059.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6060.                         (void) gss_release_cred(&stat_min,
  6061.                                                 &deleg_creds);
  6062.                 return(0);
  6063.             }
  6064.             if (stat_maj == GSS_S_COMPLETE) {
  6065.                 reply(235, "ADAT=%s", gbuf);
  6066.                 replied = 1;
  6067.             } else {
  6068.                 /* If the server accepts the security data, and
  6069.                    requires additional data, it should respond
  6070.                    with reply code 335. */
  6071.                 reply(335, "ADAT=%s", gbuf);
  6072.             }
  6073.             (void) gss_release_buffer(&stat_min, &out_tok);
  6074.         }
  6075.  
  6076.         if (stat_maj == GSS_S_COMPLETE) {
  6077.             /* GSSAPI authentication succeeded */
  6078.             stat_maj = gss_display_name(&stat_min, client,
  6079.                                          &client_name, &mechid);
  6080.             if (stat_maj != GSS_S_COMPLETE) {
  6081.                 /* "If the server rejects the security data (if
  6082.                    a checksum fails, for instance), it should
  6083.                    respond with reply code 535." */
  6084.                 reply_gss_error(535, stat_maj, stat_min,
  6085.                                 "extracting GSSAPI identity name");
  6086.                 syslog(LOG_ERR, "gssapi error extracting identity");
  6087.                 (void) gss_release_cred(&stat_min, &server_creds);
  6088.                 if (ret_flags & GSS_C_DELEG_FLAG)
  6089.                         (void) gss_release_cred(&stat_min,
  6090.                                                 &deleg_creds);
  6091.                 return 0;
  6092.             }
  6093.             auth_type = temp_auth_type;
  6094.             temp_auth_type = NULL;
  6095.  
  6096.             (void) gss_release_cred(&stat_min, &server_creds);
  6097.             if (ret_flags & GSS_C_DELEG_FLAG) {
  6098.                 if (want_creds)
  6099.                     ftpd_gss_convert_creds(client_name.value,
  6100.                                             deleg_creds);
  6101.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6102.             }
  6103.  
  6104.             /* If the server accepts the security data, but does
  6105.                not require any additional data (i.e., the security
  6106.                data exchange has completed successfully), it must
  6107.                respond with reply code 235. */
  6108.             if (!replied)
  6109.             {
  6110.                 if (ret_flags & GSS_C_DELEG_FLAG && !have_creds)
  6111.                   reply(235,
  6112.  "GSSAPI Authentication succeeded, but could not accept forwarded credentials"
  6113.                         );
  6114.                 else
  6115.                   reply(235, "GSSAPI Authentication succeeded");
  6116.             }
  6117.             return(1);
  6118.         } else if (stat_maj == GSS_S_CONTINUE_NEEDED) {
  6119.             /* If the server accepts the security data, and
  6120.             requires additional data, it should respond with
  6121.             reply code 335. */
  6122.             reply(335, "more data needed");
  6123.             (void) gss_release_cred(&stat_min, &server_creds);
  6124.             if (ret_flags & GSS_C_DELEG_FLAG)
  6125.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6126.             return(0);
  6127.         } else {
  6128.             /* "If the server rejects the security data (if
  6129.             a checksum fails, for instance), it should
  6130.             respond with reply code 535." */
  6131.             reply_gss_error(535, stat_maj, stat_min,
  6132.                              "GSSAPI failed processing ADAT");
  6133.             syslog(LOG_ERR, "GSSAPI failed processing ADAT");
  6134.             (void) gss_release_cred(&stat_min, &server_creds);
  6135.             if (ret_flags & GSS_C_DELEG_FLAG)
  6136.                 (void) gss_release_cred(&stat_min, &deleg_creds);
  6137.             return(0);
  6138.         }
  6139.  
  6140.         debug(F100,"gssk5_auth_is AUTH_SUCCESS","",0);
  6141.         krb5_errno = r;
  6142.         if ( krb5_errno )
  6143.             makestr(&krb5_errmsg,error_message(krb5_errno));
  6144.         else
  6145.             makestr(&krb5_errmsg,strTmp);
  6146.         return AUTH_SUCCESS;
  6147.  
  6148.     default:
  6149.         printf("Unknown Kerberos option %d\r\n", data[-1]);
  6150.         SendGSSK5AuthSB(GSS_REJECT, 0, 0);
  6151.         break;
  6152.     }
  6153.     auth_finished(AUTH_REJECT);
  6154.     return AUTH_FAILURE;
  6155. }
  6156. #endif /* GSSAPI_KRB5 */
  6157.  
  6158. #ifdef CK_SRP
  6159. /*
  6160.  * Copyright (c) 1997 Stanford University
  6161.  *
  6162.  * The use of this software for revenue-generating purposes may require a
  6163.  * license from the owners of the underlying intellectual property.
  6164.  * Specifically, the SRP-3 protocol may not be used for revenue-generating
  6165.  * purposes without a license.
  6166.  *
  6167.  * NOTE: Columbia University has a license.
  6168.  *
  6169.  * Within that constraint, permission to use, copy, modify, and distribute
  6170.  * this software and its documentation for any purpose is hereby granted
  6171.  * without fee, provided that the above copyright notices and this permission
  6172.  * notice appear in all copies of the software and related documentation.
  6173.  *
  6174.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  6175.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  6176.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  6177.  *
  6178.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  6179.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  6180.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  6181.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  6182.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  6183.  */
  6184.  
  6185. static void
  6186. srp_encode_length(data, num)
  6187.     unsigned char * data;
  6188.     int num;
  6189. {
  6190.     *data = (num >> 8) & 0xff;
  6191.     *++data = num & 0xff;
  6192. }
  6193.  
  6194. static int
  6195. srp_decode_length(data)
  6196.     unsigned char * data;
  6197. {
  6198.     return (((int) *data & 0xff) << 8) | (*(data + 1) & 0xff);
  6199. }
  6200.  
  6201. #ifdef PRE_SRP_1_7_3
  6202. static int
  6203. #ifdef CK_ANSIC
  6204. srp_reply(int how, unsigned char *data, int cnt)
  6205. #else
  6206. srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6207. #endif
  6208. {
  6209.     struct t_num n;
  6210.     struct t_num g;
  6211.     struct t_num s;
  6212.     struct t_num B;
  6213.     struct t_num * A;
  6214.     char type_check[26];
  6215.     int pflag;
  6216.  
  6217. #ifdef CK_ENCRYPTION
  6218.     Session_Key skey;
  6219. #endif /* ENCRYPTION */
  6220.  
  6221.     char * str=NULL;
  6222.  
  6223.     data += 4;                          /* Point to status byte */
  6224.     cnt  -= 4;
  6225.  
  6226.     if(cnt-- < 1) {
  6227.         auth_finished(AUTH_REJECT);
  6228.         return AUTH_FAILURE;
  6229.     }
  6230.  
  6231.     switch(*data++) {
  6232.     case SRP_REJECT:
  6233.         ckmakmsg(strTmp,sizeof(strTmp),
  6234.                   "SRP refuses authentication for '",szUserName,
  6235.                   "'\r\n",NULL);
  6236.         if (cnt > 0) {
  6237.             int len = strlen(strTmp);
  6238.             if ( len + cnt < sizeof(strTmp) ) {
  6239.                 str = strTmp + strlen(strTmp);
  6240.                 memcpy(str,data,cnt);
  6241.                 str[cnt] = 0;
  6242.             }
  6243.         }
  6244.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6245.         if (tc != NULL) {
  6246.             t_clientclose(tc);
  6247.             tc = NULL;
  6248.         }
  6249.         auth_finished(AUTH_REJECT);
  6250.         return AUTH_FAILURE;
  6251.  
  6252.     case SRP_ACCEPT:
  6253.         if(cnt < RESPONSE_LEN || !srp_waitresp ||
  6254.             tc == NULL
  6255.             ) {
  6256.             printf("SRP Protocol error\r\n");
  6257.             return(auth_resend(AUTHTYPE_SRP));
  6258.         }
  6259.         srp_waitresp = 0;
  6260.  
  6261.         if(t_clientverify(tc, data) == 0) {
  6262.             printf("SRP accepts you as %s\r\n",szUserName);
  6263. #ifdef CK_SSL
  6264.             if((ssl_active_flag || tls_active_flag) &&
  6265.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6266.                 printf("TLS session parameters verified by SRP\r\n");
  6267.             } else
  6268. #endif /* CK_SSL */
  6269.  
  6270. #ifdef CK_ENCRYPTION
  6271.             {
  6272.                 skey.type = SK_GENERIC;
  6273.                 skey.length = SESSION_KEY_LEN;
  6274.                 skey.data = tc->session_key;
  6275.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6276.             }
  6277. #endif /* ENCRYPTION */
  6278.             t_clientclose(tc);
  6279.             tc = NULL;
  6280.             accept_complete = 1;
  6281.             auth_finished(AUTH_VALID);
  6282.             return AUTH_SUCCESS;
  6283.         } else {
  6284.             printf("SRP server authentication failed!\r\n");
  6285.             t_clientclose(tc);
  6286.             tc = NULL;
  6287.             return(auth_resend(AUTHTYPE_SRP));
  6288.         }
  6289.         break;
  6290.  
  6291.     case SRP_PARAMS:
  6292.         if(!szUserName) {
  6293.             printf("No username available\r\n");
  6294.             return(auth_resend(AUTHTYPE_SRP));
  6295.         }
  6296.  
  6297.         n.len = srp_decode_length(data);
  6298.         data += 2;
  6299.         cnt -= 2;
  6300.         if(n.len > cnt) {
  6301.             printf("n too long\r\n");
  6302.             return(auth_resend(AUTHTYPE_SRP));
  6303.         }
  6304.         n.data = data;
  6305.         data += n.len;
  6306.         cnt -= n.len;
  6307.  
  6308.         g.len = srp_decode_length(data);
  6309.         data += 2;
  6310.         cnt -= 2;
  6311.         if(g.len > cnt) {
  6312.             printf("g too long\r\n");
  6313.             return(auth_resend(AUTHTYPE_SRP));
  6314.         }
  6315.         g.data = data;
  6316.         data += g.len;
  6317.         cnt -= g.len;
  6318.  
  6319.         s.len = srp_decode_length(data);
  6320.         data += 2;
  6321.         cnt -= 2;
  6322.         if(s.len > cnt) {
  6323.             printf("salt too long\r\n");
  6324.             return(auth_resend(AUTHTYPE_SRP));
  6325.         }
  6326.         s.data = data;
  6327.         data += s.len;
  6328.         cnt -= s.len;
  6329.  
  6330.         /* If the parameters provided by the server cannot be
  6331.          * validated the following function will fail.
  6332.          */
  6333.         tc = t_clientopen(szUserName, &n, &g, &s);
  6334.         if (tc == NULL) {
  6335.             printf("SRP parameter initialization error\r\n");
  6336.             return(auth_resend(AUTHTYPE_SRP));
  6337.         }
  6338.         A = t_clientgenexp(tc);
  6339.         if(A == NULL) {
  6340.             printf("SRP protocol error\r\n");
  6341.             return(auth_resend(AUTHTYPE_SRP));
  6342.         }
  6343.         SendSRPAuthSB(SRP_EXP, A->data, A->len);
  6344.  
  6345.         if ( pwbuf[0] && pwflg ) {
  6346.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6347.                    8 * n.len,
  6348.                    szUserName
  6349.                    );
  6350.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6351. #ifdef OS2
  6352.             if ( pwcrypt )
  6353.                 ck_encrypt((char *)srp_passwd);
  6354. #endif /* OS2 */
  6355.         } else {
  6356.             extern char * srppwprompt;
  6357.             char preface[128];
  6358.             int ok;
  6359.  
  6360.             if (srppwprompt && srppwprompt[0] &&
  6361.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6362.         sizeof(preface)) {
  6363.                 sprintf(preface,srppwprompt,szUserName);
  6364.             } else {
  6365.                 ckmakxmsg( preface,sizeof(preface),
  6366.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6367.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6368.                           NULL, NULL);
  6369.             }
  6370.             ok = uq_txt( preface,"Password: ",2,NULL,
  6371.                          srp_passwd,sizeof(srp_passwd)-1,NULL);
  6372.             if ( !ok )
  6373.                 srp_passwd[0] = '\0';
  6374.         }
  6375.  
  6376.         t_clientpasswd(tc, srp_passwd);
  6377.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6378.         return AUTH_SUCCESS;
  6379.  
  6380.     case SRP_CHALLENGE:
  6381.         if(tc == NULL) {
  6382.             printf("SRP protocol error\r\n");
  6383.             return(auth_resend(AUTHTYPE_SRP));
  6384.         }
  6385.  
  6386. #ifndef PRE_SRP_1_4_5
  6387.         /*
  6388.          * The original SRP AUTH implementation did not protect against
  6389.          * tampering of the auth-type-pairs.  Therefore, when the
  6390.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6391.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6392.          * is set we also insert the SSL/TLS client and server finished
  6393.          * messages to ensure that there is no man in the middle attack
  6394.          * underway on the SSL/TLS connection.
  6395.          */
  6396.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6397.             type_check[0] = AUTHTYPE_SRP;
  6398.             type_check[1] = how;
  6399. #ifdef CK_SSL
  6400.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6401.                 ssl_get_client_finished(&type_check[2],12);
  6402.                 ssl_get_server_finished(&type_check[14],12);
  6403.                 t_clientaddexdata(tc,type_check,26);
  6404.             } else
  6405. #endif /* CK_SSL */
  6406.                 t_clientaddexdata(tc,type_check,2);
  6407.         }
  6408. #endif /* PRE_SRP_1_4_5 */
  6409.  
  6410.         B.data = data;
  6411.         B.len = cnt;
  6412.         t_clientgetkey(tc, &B);
  6413.  
  6414.         SendSRPAuthSB(SRP_RESPONSE, t_clientresponse(tc), RESPONSE_LEN);
  6415.         srp_waitresp = 1;
  6416.         return AUTH_SUCCESS;
  6417.  
  6418.     default:
  6419.         return(auth_resend(AUTHTYPE_SRP));
  6420.     }
  6421.     return AUTH_FAILURE;
  6422. }
  6423.  
  6424. static int
  6425. #ifdef CK_ANSIC
  6426. srp_is(int how, unsigned char *data, int cnt)
  6427. #else
  6428. srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6429. #endif
  6430. {
  6431.     char * pbuf = NULL;
  6432.     char * ptr;
  6433. #ifdef CK_ENCRYPTION
  6434.     Session_Key skey;
  6435. #endif
  6436.     struct t_num A;
  6437.     struct t_pw * tpw = NULL;
  6438.     struct t_conf * tconf = NULL;
  6439.     struct passwd * pass;
  6440.     static struct t_num * B = NULL;     /* Holder for B */
  6441. #ifdef CK_SSL
  6442.     char type_check[26];
  6443. #else
  6444.     char type_check[2];
  6445. #endif /* CK_SSL */
  6446.  
  6447.     if ((cnt -= 4) < 1) {
  6448.         auth_finished(AUTH_REJECT);
  6449.         return AUTH_FAILURE;
  6450.     }
  6451.  
  6452.     data += 4;
  6453.     cnt  -= 1;
  6454.     switch(*data++) {
  6455.     case SRP_AUTH:
  6456.         /* Send parameters back to client */
  6457.         if(ts != NULL) {
  6458.             t_serverclose(ts);
  6459.             ts = NULL;
  6460.         }
  6461.         if(!szUserNameRequested[0]) {
  6462.             if (1)
  6463.                 printf("No username available\r\n");
  6464.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6465.             auth_finished(AUTH_REJECT);
  6466.             return(AUTH_FAILURE);
  6467.         }
  6468. #ifdef IKSD
  6469. #ifdef CK_LOGIN
  6470.         if (inserver && ckxanon &&
  6471.              !strcmp(szUserNameRequested,"anonymous")) {
  6472.             SendSRPAuthSB(SRP_REJECT, (void *)
  6473.             "anonymous login cannot be performed with Secure Remote Password",
  6474.             -1);
  6475.             auth_finished(AUTH_REJECT);
  6476.             return(AUTH_FAILURE);
  6477.         }
  6478. #endif /* CK_LOGIN */
  6479. #endif /* IKSD */
  6480. #ifndef PRE_SRP_1_4_4
  6481.         if(tpw == NULL) {
  6482.             if((tpw = t_openpw(NULL)) == NULL) {
  6483.                 if (1)
  6484.                     printf("Unable to open password file\r\n");
  6485.                 SendSRPAuthSB(SRP_REJECT, (void *) "No password file", -1);
  6486.                 return(AUTH_FAILURE);
  6487.             }
  6488.         }
  6489.         if(tconf == NULL) {
  6490.             if((tconf = t_openconf(NULL)) == NULL) {
  6491.                 if (1)
  6492.                   printf("Unable to open configuration file\r\n");
  6493.                 SendSRPAuthSB(SRP_REJECT, (void *)"No configuration file", -1);
  6494.                 return(AUTH_FAILURE);
  6495.             }
  6496.         }
  6497.         ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6498.         t_closepw(tpw);
  6499.         tpw = NULL;
  6500.         t_closeconf(tconf);
  6501.         tconf = NULL;
  6502. #else /* PRE_SRP_1_4_4 */
  6503. #ifdef COMMENT
  6504.         /* the code in this block should no longer be necessary on OS/2
  6505.            or Windows because I have added functionality to libsrp.lib
  6506.            to find the srp files.   4/22/2000
  6507.         */
  6508.  
  6509.         /* On Windows and OS/2 there is no well defined place for the */
  6510.         /* ETC directory.  So we look for either an SRP_ETC or ETC    */
  6511.         /* environment variable in that order.  If we find one we     */
  6512.         /* attempt to open the files manually.                        */
  6513.         /* We will reuse the strTmp[] for the file names. */
  6514.         ptr = getenv("SRP_ETC");
  6515.         if ( !ptr )
  6516.             ptr = getenv("ETC");
  6517. #ifdef NT
  6518.         if ( !ptr ) {
  6519.             DWORD len;
  6520.             len = AUTHTMPBL;
  6521.  
  6522.             len = GetWindowsDirectory(strTmp,len);
  6523.             if ( len > 0 && len < AUTHTMPBL) {
  6524.                 if ( !isWin95() ) {
  6525.                     if ( len == 1 )
  6526.               ckstrncat(strTmp,"SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6527.                     else
  6528.               ckstrncat(strTmp,"/SYSTEM32/DRIVERS/ETC",sizeof(strTmp));
  6529.                 }
  6530.             }
  6531.             ptr = strTmp;
  6532.         }
  6533. #endif /* NT */
  6534.         if ( ptr ) {
  6535.             int len = strlen(ptr);
  6536.             int i;
  6537.         if (ptr != strTmp)
  6538.         strcpy(strTmp,ptr);
  6539.             for ( i=0;i<len;i++ ) {
  6540.                 if ( strTmp[i] == '\\' )
  6541.                     strTmp[i] = '/';
  6542.             }
  6543.             if ( strTmp[len-1] != '/' )
  6544.                 ckstrncat(strTmp,"/tpasswd",sizeof(strTmp));
  6545.             else
  6546.                 ckstrncat(strTmp,"tpasswd",sizeof(strTmp));
  6547.             tpw = t_openpwbyname(strTmp);
  6548.  
  6549.             ckstrncat(strTmp,".conf",sizeof(strTmp));
  6550.             tconf = t_openconfbyname(strTmp);
  6551.         }
  6552.  
  6553.         if ( tpw && tconf )
  6554.             ts = t_serveropenfromfiles(szUserNameRequested, tpw, tconf);
  6555.         else
  6556.             ts = t_serveropen(szUserNameRequested);
  6557.         if ( tpw ) {
  6558.             t_closepw(tpw);
  6559.             tpw = NULL;
  6560.         }
  6561.         if ( tconf ) {
  6562.             t_closeconf(tconf);
  6563.             tconf = NULL;
  6564.         }
  6565. #else /* COMMENT */
  6566.         ts = t_serveropen(szUserNameRequested);
  6567. #endif /* COMMENT */
  6568. #endif /* PRE_SRP_1_4_4 */
  6569.  
  6570.         if( ts == NULL ) {
  6571.             printf("User %s not found\r\n", szUserNameRequested);
  6572.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  6573.             return(AUTH_FAILURE);
  6574.         }
  6575.  
  6576.     pbuf = (char *)malloc(ts->n.len + ts->g.len + ts->s.len + 7);
  6577.     ptr = pbuf;
  6578.  
  6579.         srp_encode_length(ptr, ts->n.len);
  6580.         ptr += 2;
  6581.         memcpy(ptr, ts->n.data, ts->n.len);     /* safe */
  6582.         ptr += ts->n.len;
  6583.  
  6584.         srp_encode_length(ptr, ts->g.len);
  6585.         ptr += 2;
  6586.         memcpy(ptr, ts->g.data, ts->g.len);     /* safe */
  6587.         ptr += ts->g.len;
  6588.  
  6589.         srp_encode_length(ptr, ts->s.len);
  6590.         ptr += 2;
  6591.         memcpy(ptr, ts->s.data, ts->s.len);     /* safe */
  6592.         ptr += ts->s.len;
  6593.  
  6594.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  6595.     free(pbuf); pbuf = NULL;
  6596.  
  6597.         B = t_servergenexp(ts);
  6598.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  6599.         return AUTH_SUCCESS;
  6600.  
  6601.     case SRP_EXP:
  6602.         /* Client is sending A to us, compute challenge & expected response. */
  6603.         if (ts == NULL || B == NULL) {
  6604.             printf("Protocol error: SRP_EXP unexpected\r\n");
  6605.             SendSRPAuthSB(SRP_REJECT,
  6606.                           (void *) "Protocol error: unexpected EXP",
  6607.                           -1
  6608.                           );
  6609.             return(AUTH_FAILURE);
  6610.         }
  6611.  
  6612.         /* Wait until now to send B, since it contains the key to "u" */
  6613.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->len);
  6614.         B = NULL;
  6615.  
  6616. #ifndef PRE_SRP_1_4_5
  6617.         /*
  6618.          * The original SRP AUTH implementation did not protect against
  6619.          * tampering of the auth-type-pairs.  Therefore, when the
  6620.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6621.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6622.          * is set we also insert the SSL/TLS client and server finished
  6623.          * messages to ensure that there is no man in the middle attack
  6624.          * underway on the SSL/TLS connection.
  6625.          */
  6626.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  6627.             type_check[0] = AUTHTYPE_SRP;
  6628.             type_check[1] = how;
  6629. #ifdef CK_SSL
  6630.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6631.                 ssl_get_client_finished(&type_check[2],12);
  6632.                 ssl_get_server_finished(&type_check[14],12);
  6633.             }
  6634. #endif /* CK_SSL */
  6635.             t_serveraddexdata(ts,type_check,
  6636. #ifdef CK_SSL
  6637.                   ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) ? 26 :
  6638. #endif /* CK_SSL */
  6639.                                2);
  6640.         }
  6641. #endif /* PRE_SRP_1_4_5 */
  6642.  
  6643.         A.data = data;
  6644.         A.len = cnt;
  6645.         ptr = t_servergetkey(ts, &A);
  6646.  
  6647.         if(ptr == NULL) {
  6648.             if (1)
  6649.               printf("Security alert: Trivial session key attempted\r\n");
  6650.             SendSRPAuthSB(SRP_REJECT,
  6651.                           (void *) "Trivial session key detected",
  6652.                           -1
  6653.                           );
  6654.             return(AUTH_FAILURE);
  6655.         }
  6656.         srp_waitresp = 1;
  6657.         return AUTH_SUCCESS;
  6658.  
  6659.     case SRP_RESPONSE:
  6660.         /* Got the response; see if it's correct */
  6661.         if (!srp_waitresp ||
  6662.              ts == NULL
  6663.              ) {
  6664.             if (1)
  6665.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  6666.             SendSRPAuthSB(SRP_REJECT,
  6667.                           (void *) "Protocol error: unexpected RESPONSE",
  6668.                           -1
  6669.                           );
  6670.             return(AUTH_FAILURE);
  6671.         }
  6672.         srp_waitresp = 0;       /* we got a response */
  6673.  
  6674.         if (cnt < RESPONSE_LEN) {
  6675.             if (1)
  6676.               printf("Protocol error: malformed response\r\n");
  6677.             SendSRPAuthSB(SRP_REJECT,
  6678.                           (void *) "Protocol error: malformed response",
  6679.                           -1
  6680.                           );
  6681.             return(AUTH_FAILURE);
  6682.         }
  6683.  
  6684.         if (t_serververify(ts, data) == 0) {
  6685.             SendSRPAuthSB(SRP_ACCEPT, t_serverresponse(ts), RESPONSE_LEN);
  6686.             accept_complete = 1;
  6687. #ifdef CK_ENCRYPTION
  6688. #ifdef CK_SSL
  6689.             if (!(ssl_active_flag || tls_active_flag))
  6690. #endif /* CK_SSL */
  6691.             {
  6692.                 hexdump("SRP_RESPONSE ts",ts,sizeof(ts));
  6693.                 hexdump("SRP_RESPONSE session_key",
  6694.                          ts->session_key,
  6695.                          SESSION_KEY_LEN
  6696.                          );
  6697.                 skey.type = SK_GENERIC;
  6698.                 skey.length = SESSION_KEY_LEN;
  6699.                 skey.data = ts->session_key;
  6700.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  6701.             }
  6702. #endif /* CK_ENCRYPTION */
  6703.             auth_finished(AUTH_VALID);
  6704.         }
  6705.         else {
  6706.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  6707.             auth_finished(AUTH_REJECT);
  6708.             return(AUTH_FAILURE);
  6709.         }
  6710.         return AUTH_SUCCESS;
  6711.  
  6712.     default:
  6713.         printf("Unknown SRP option %d\r\n", data[-1]);
  6714.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  6715.         return(AUTH_FAILURE);
  6716.     }
  6717. }
  6718. #else /* PRE_SRP_1_7_3 */
  6719. static int
  6720. #ifdef CK_ANSIC
  6721. new_srp_reply(int how, unsigned char *data, int cnt)
  6722. #else
  6723. new_srp_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  6724. #endif
  6725. {
  6726.     data += 4;                          /* Point to status byte */
  6727.     cnt  -= 4;
  6728.  
  6729.     if(cnt-- < 1) {                     /* Matches with data++ */
  6730.         auth_finished(AUTH_REJECT);
  6731.         return AUTH_FAILURE;
  6732.     }
  6733.  
  6734.     switch(*data++) {
  6735.     case SRP_PARAMS: {
  6736.         struct t_num n;
  6737.         struct t_num g;
  6738.         struct t_num s;
  6739.         cstr * A;
  6740.  
  6741.         if(!szUserName) {
  6742.             printf("No username available\r\n");
  6743.             return(auth_resend(AUTHTYPE_SRP));
  6744.         }
  6745.  
  6746.         n.len = srp_decode_length(data);
  6747.         data += 2;
  6748.         cnt -= 2;
  6749.         if(n.len > cnt) {
  6750.             printf("n too long\r\n");
  6751.             return(auth_resend(AUTHTYPE_SRP));
  6752.         }
  6753.         n.data = data;
  6754.         data += n.len;
  6755.         cnt -= n.len;
  6756.  
  6757.         g.len = srp_decode_length(data);
  6758.         data += 2;
  6759.         cnt -= 2;
  6760.         if(g.len > cnt) {
  6761.             printf("g too long\r\n");
  6762.             return(auth_resend(AUTHTYPE_SRP));
  6763.         }
  6764.         g.data = data;
  6765.         data += g.len;
  6766.         cnt -= g.len;
  6767.  
  6768.         s.len = srp_decode_length(data);
  6769.         data += 2;
  6770.         cnt -= 2;
  6771.         if(s.len != cnt) {
  6772.             printf("invalid salt\r\n");
  6773.             return(auth_resend(AUTHTYPE_SRP));
  6774.         }
  6775.         s.data = data;
  6776.         data += s.len;
  6777.         cnt -= s.len;
  6778.  
  6779.         /* If the parameters provided by the server cannot be
  6780.          * validated the following function will fail.
  6781.          */
  6782.         c_srp = SRP_new(SRP_RFC2945_client_method());
  6783.         if (c_srp == NULL ||
  6784.         SRP_set_username(c_srp, szUserName) != SRP_SUCCESS ||
  6785.         SRP_set_params(c_srp,n.data,n.len,g.data,g.len,s.data,s.len) !=
  6786.         SRP_SUCCESS) {
  6787.             printf("SRP Parameter initialization error\r\n");
  6788.             return(auth_resend(AUTHTYPE_SRP));
  6789.         }
  6790.  
  6791.         A = cstr_new();
  6792.         if(SRP_gen_pub(c_srp, &A) != SRP_SUCCESS) {
  6793.             printf("SRP Error generating key exchange\r\n");
  6794.             return(auth_resend(AUTHTYPE_SRP));
  6795.         }
  6796.  
  6797.         SendSRPAuthSB(SRP_EXP, A->data, A->length);
  6798.         cstr_free(A);
  6799.  
  6800.         if ( pwbuf[0] && pwflg ) {
  6801.             printf("SRP using %d-bit modulus for '%s'\r\n",
  6802.                    8 * n.len,
  6803.                    szUserName
  6804.                    );
  6805.             ckstrncpy(srp_passwd,pwbuf,sizeof(srp_passwd));
  6806. #ifdef OS2
  6807.             if ( pwcrypt )
  6808.                 ck_encrypt((char *)srp_passwd);
  6809. #endif /* OS2 */
  6810.         } else {
  6811.             extern char * srppwprompt;
  6812.             char preface[128];
  6813.             int ok;
  6814.  
  6815.             if (srppwprompt && srppwprompt[0] &&
  6816.         (strlen(srppwprompt) + strlen(szUserName) - 2) <
  6817.         sizeof(preface)) {
  6818.                 sprintf(preface,srppwprompt,szUserName);
  6819.             } else {
  6820.                 ckmakxmsg( preface,sizeof(preface),
  6821.                           "SRP using ",ckitoa(8*n.len),"-bit modulus for '",
  6822.                           szUserName, "'", NULL, NULL, NULL, NULL, NULL,
  6823.                           NULL, NULL);
  6824.             }
  6825.             ok = uq_txt(preface,"Password: ",2,NULL,
  6826.                         srp_passwd,sizeof(srp_passwd)-1,NULL);
  6827.             if ( !ok )
  6828.                 srp_passwd[0] = '\0';
  6829.         }
  6830.  
  6831.         if(SRP_set_auth_password(c_srp, srp_passwd) != SRP_SUCCESS) {
  6832.             memset(srp_passwd, 0, sizeof(srp_passwd));
  6833.             printf("SRP Error setting client password\r\n");
  6834.             return(auth_resend(AUTHTYPE_SRP));
  6835.         }
  6836.         memset(srp_passwd, 0, sizeof(srp_passwd));
  6837.         return AUTH_SUCCESS;
  6838.     }
  6839.     case SRP_CHALLENGE: {
  6840.         char type_check[26];
  6841.         cstr * resp = NULL;
  6842.  
  6843.         if(c_srp == NULL) {
  6844.             printf("SRP protocol error\r\n");
  6845.             return(auth_resend(AUTHTYPE_SRP));
  6846.         }
  6847.  
  6848.         /*
  6849.          * The original SRP AUTH implementation did not protect against
  6850.          * tampering of the auth-type-pairs.  Therefore, when the
  6851.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  6852.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  6853.          * is set we also insert the SSL/TLS client and server finished
  6854.          * messages to ensure that there is no man in the middle attack
  6855.          * underway on the SSL/TLS connection.
  6856.          */
  6857.         if ((how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF) {
  6858.             type_check[0] = AUTHTYPE_SRP;
  6859.             type_check[1] = how;
  6860. #ifdef CK_SSL
  6861.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6862.                 ssl_get_client_finished(&type_check[2],12);
  6863.                 ssl_get_server_finished(&type_check[14],12);
  6864.                 SRP_add_ex_data(c_srp, type_check, 26);
  6865.             } else
  6866. #endif /* CK_SSL */
  6867.                 SRP_add_ex_data(c_srp, type_check, 2);
  6868.         }
  6869.  
  6870.         if(SRP_compute_key(c_srp, &c_key, data, cnt) != SRP_SUCCESS) {
  6871.             printf("SRP ERROR: unable to compute client key\r\n");
  6872.             return(auth_resend(AUTHTYPE_SRP));
  6873.         }
  6874.  
  6875.         resp = cstr_new();
  6876.         if(SRP_respond(c_srp, &resp) != SRP_SUCCESS) {
  6877.             printf("SRP ERROR: unable to compute client response\r\n");
  6878.             return(auth_resend(AUTHTYPE_SRP));
  6879.         }
  6880.         SendSRPAuthSB(SRP_RESPONSE, resp->data, resp->length);
  6881.         cstr_free(resp);
  6882.         srp_waitresp = 1;
  6883.         return AUTH_SUCCESS;
  6884.     }
  6885.     case SRP_ACCEPT: {
  6886. #ifdef CK_ENCRYPTION
  6887.         Session_Key skey;
  6888. #endif /* ENCRYPTION */
  6889.  
  6890.         if(cnt < RESPONSE_LEN || !srp_waitresp || c_srp == NULL) {
  6891.             printf("SRP Protocol error\r\n");
  6892.             return(auth_resend(AUTHTYPE_SRP));
  6893.         }
  6894.         srp_waitresp = 0;
  6895.  
  6896.         if(SRP_verify(c_srp, data, cnt) == SRP_SUCCESS) {
  6897.             printf("SRP accepts you as %s\r\n",szUserName);
  6898.  
  6899. #ifdef CK_SSL
  6900.             if((ssl_active_flag || tls_active_flag) &&
  6901.                 (how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  6902.                 printf("TLS session parameters verified by SRP\r\n");
  6903.             } else
  6904. #endif /* CK_SSL */
  6905. #ifdef CK_ENCRYPTION
  6906.             {
  6907.                 skey.type = SK_GENERIC;
  6908.                 skey.length = c_key->length;
  6909.                 skey.data = c_key->data;
  6910.                 encrypt_session_key(&skey, AUTH_CLIENT_TO_SERVER);
  6911.                 cstr_clear_free(c_key);
  6912.                 c_key = NULL;
  6913.             }
  6914. #endif /* CK_ENCRYPTION */
  6915.             accept_complete = 1;
  6916.             auth_finished(AUTH_VALID);
  6917.             SRP_free(c_srp);
  6918.             c_srp = NULL;
  6919.             return AUTH_SUCCESS;
  6920.         }
  6921.         else {
  6922.             printf("[ Error: SRP server authentication failed ]\r\n");
  6923.             return(auth_resend(AUTHTYPE_SRP));
  6924.         }
  6925.     }
  6926.     case SRP_REJECT: {
  6927.         char * str=NULL;
  6928.  
  6929.         ckmakmsg(strTmp,sizeof(strTmp),
  6930.                   "SRP refuses authentication for '",szUserName,
  6931.                   "'\r\n",NULL);
  6932.         if (cnt > 0) {
  6933.             int len = strlen(strTmp);
  6934.             if ( len + cnt < sizeof(strTmp) ) {
  6935.                 str = strTmp + strlen(strTmp);
  6936.                 memcpy(str,data,cnt);
  6937.                 str[cnt] = 0;
  6938.             }
  6939.         }
  6940.         printf("SRP authentication failed!\r\n%s\r\n",strTmp);
  6941.         auth_finished(AUTH_REJECT);
  6942.         return AUTH_FAILURE;
  6943.     }
  6944.     default:
  6945.         printf("Unknown SRP option %d\r\n", data[-1]);
  6946.         return(auth_resend(AUTHTYPE_SRP));
  6947.     }
  6948.     /* NEVER REACHED */
  6949. }
  6950.  
  6951. static int
  6952. #ifdef CK_ANSIC
  6953. new_srp_is(int how, unsigned char *data, int cnt)
  6954. #else
  6955. new_srp_is(how,data,cnt) int how; unsigned char *data; int cnt;
  6956. #endif
  6957. {
  6958.     char * pbuf = NULL;
  6959.     char * ptr;
  6960. #ifdef CK_ENCRYPTION
  6961.     Session_Key skey;
  6962. #endif
  6963.     static cstr * B = NULL;             /* Holder for B */
  6964.     struct t_passwd * pass;
  6965.     cstr * resp;
  6966.     char type_check[26];
  6967.  
  6968.     if ((cnt -= 4) < 1) {
  6969.         auth_finished(AUTH_REJECT);
  6970.         return AUTH_FAILURE;
  6971.     }
  6972.  
  6973.     data += 4;
  6974.     cnt  -= 1;
  6975.     switch(*data++) {
  6976.     case SRP_AUTH:
  6977.         /* Send parameters back to client */
  6978.         if(s_srp != NULL) {
  6979.             SRP_free(s_srp);
  6980.             s_srp = NULL;
  6981.         }
  6982.         if (B != NULL) {
  6983.             cstr_free(B);
  6984.             B = NULL;
  6985.         }
  6986.         if(!szUserNameRequested[0]) {
  6987.             if (1)
  6988.                 printf("No username available\r\n");
  6989.             SendSRPAuthSB(SRP_REJECT, (void *) "No username supplied", -1);
  6990.             auth_finished(AUTH_REJECT);
  6991.             return(AUTH_FAILURE);
  6992.         }
  6993. #ifdef IKSD
  6994. #ifdef CK_LOGIN
  6995.         if (inserver && ckxanon &&
  6996.              !strcmp(szUserNameRequested,"anonymous")) {
  6997.             SendSRPAuthSB(SRP_REJECT, (void *)
  6998.             "anonymous login cannot be performed with Secure Remote Password",
  6999.             -1);
  7000.             auth_finished(AUTH_REJECT);
  7001.             return(AUTH_FAILURE);
  7002.         }
  7003. #endif /* CK_LOGIN */
  7004. #endif /* IKSD */
  7005.         s_srp = SRP_new(SRP_RFC2945_server_method());
  7006.         if(s_srp == NULL) {
  7007.             printf("Error initializing SRP server\r\n");
  7008.             SendSRPAuthSB(SRP_REJECT,
  7009.                           (void *) "SRP server init failed",
  7010.                           -1
  7011.                           );
  7012.             return(AUTH_FAILURE);
  7013.         }
  7014.         pass = gettpnam(szUserNameRequested);
  7015.         if(pass == NULL) {
  7016.             printf("User %s not found\r\n", szUserNameRequested);
  7017.             SendSRPAuthSB(SRP_REJECT, (void *) "Password not set", -1);
  7018.             return(AUTH_FAILURE);
  7019.         }
  7020.         if(SRP_set_username(s_srp, szUserNameRequested) != SRP_SUCCESS ||
  7021.        SRP_set_params(s_srp, pass->tc.modulus.data,
  7022.               pass->tc.modulus.len,
  7023.               pass->tc.generator.data,
  7024.               pass->tc.generator.len,
  7025.               pass->tp.salt.data,
  7026.               pass->tp.salt.len) != SRP_SUCCESS ||
  7027.        SRP_set_authenticator(s_srp,
  7028.                  pass->tp.password.data,
  7029.                  pass->tp.password.len) != SRP_SUCCESS) {
  7030.             printf("Error initializing SRP parameters\r\n");
  7031.             SendSRPAuthSB(SRP_REJECT,(void *)"SRP parameter init failed", -1);
  7032.             return(AUTH_FAILURE);
  7033.         }
  7034.  
  7035.     pbuf = (char *)malloc(pass->tc.modulus.len + pass->tc.generator.len +
  7036.                    pass->tp.salt.len + 7);
  7037.         ptr = pbuf;
  7038.  
  7039.         srp_encode_length(ptr, pass->tc.modulus.len);
  7040.         ptr += 2;
  7041.         memcpy(ptr, pass->tc.modulus.data, pass->tc.modulus.len);
  7042.         ptr += pass->tc.modulus.len;
  7043.  
  7044.         srp_encode_length(ptr, pass->tc.generator.len);
  7045.         ptr += 2;
  7046.         memcpy(ptr, pass->tc.generator.data, pass->tc.generator.len);
  7047.         ptr += pass->tc.generator.len;
  7048.  
  7049.         srp_encode_length(ptr, pass->tp.salt.len);
  7050.         ptr += 2;
  7051.         memcpy(ptr, pass->tp.salt.data, pass->tp.salt.len);
  7052.         ptr += pass->tp.salt.len;
  7053.  
  7054.         SendSRPAuthSB(SRP_PARAMS, pbuf, ptr - pbuf);
  7055.     free(pbuf);
  7056.     pbuf = NULL;
  7057.  
  7058.         if(SRP_gen_pub(s_srp, &B) != SRP_SUCCESS) {
  7059.             printf("Error generating SRP public value\r\n");
  7060.             SendSRPAuthSB(SRP_REJECT, (void *) "SRP_gen_pub failed", -1);
  7061.             return(AUTH_FAILURE);
  7062.         }
  7063.         ckstrncpy(szUserNameAuthenticated,szUserNameRequested,UIDBUFLEN);
  7064.         return AUTH_SUCCESS;
  7065.  
  7066.     case SRP_EXP:
  7067.       /* Client is sending A to us, compute challenge and expected response. */
  7068.         if (s_srp == NULL || B == NULL) {
  7069.         printf("Protocol error: SRP_EXP unexpected\r\n");
  7070.         SendSRPAuthSB(SRP_REJECT,
  7071.               (void *)"Protocol error: unexpected EXP", -1);
  7072.         return(AUTH_FAILURE);
  7073.     }
  7074.         /* Wait until now to send B, since it contains the key to "u" */
  7075.         SendSRPAuthSB(SRP_CHALLENGE, B->data, B->length);
  7076.         cstr_free(B);
  7077.         B = NULL;
  7078.  
  7079.         /*
  7080.          * The original SRP AUTH implementation did not protect against
  7081.          * tampering of the auth-type-pairs.  Therefore, when the
  7082.          * AUTH_ENCRYPT_MASK bits are zero, no extra data is inserted
  7083.          * into the SRP hash computation.  When AUTH_ENCRYPT_START_TLS
  7084.          * is set we also insert the SSL/TLS client and server finished
  7085.          * messages to ensure that there is no man in the middle attack
  7086.          * underway on the SSL/TLS connection.
  7087.          */
  7088.         if ( (how & AUTH_ENCRYPT_MASK) != AUTH_ENCRYPT_OFF ) {
  7089.             type_check[0] = AUTHTYPE_SRP;
  7090.             type_check[1] = how;
  7091. #ifdef CK_SSL
  7092.             if ((how & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_START_TLS) {
  7093.                 ssl_get_client_finished(&type_check[2],12);
  7094.                 ssl_get_server_finished(&type_check[14],12);
  7095.                 SRP_add_ex_data(s_srp, type_check, 26);
  7096.             } else
  7097. #endif /* CK_SSL */
  7098.                 SRP_add_ex_data(s_srp, type_check, 2);
  7099.         }
  7100.  
  7101.         if(SRP_compute_key(s_srp, &s_key, data, cnt) != SRP_SUCCESS) {
  7102.             printf("Security alert: Trivial session key attempted\r\n");
  7103.             SendSRPAuthSB(SRP_REJECT,
  7104.               (void *) "Trivial session key detected", -1);
  7105.             return(AUTH_FAILURE);
  7106.         }
  7107.         srp_waitresp = 1;
  7108.         return AUTH_SUCCESS;
  7109.  
  7110.     case SRP_RESPONSE:
  7111.         /* Got the response; see if it's correct */
  7112.         if (!srp_waitresp || s_srp == NULL) {
  7113.             if (1)
  7114.               printf("Protocol error: SRP_RESPONSE unexpected\r\n");
  7115.             SendSRPAuthSB(SRP_REJECT,
  7116.                           (void *) "Protocol error: unexpected RESPONSE",
  7117.                           -1
  7118.                           );
  7119.             return(AUTH_FAILURE);
  7120.         }
  7121.         srp_waitresp = 0;       /* we got a response */
  7122.  
  7123.         if (cnt < RESPONSE_LEN) {
  7124.             if (1)
  7125.               printf("Protocol error: malformed response\r\n");
  7126.             SendSRPAuthSB(SRP_REJECT,
  7127.                           (void *) "Protocol error: malformed response",
  7128.                           -1
  7129.                           );
  7130.             return(AUTH_FAILURE);
  7131.         }
  7132.  
  7133.         if(SRP_verify(s_srp, data, cnt) == SRP_SUCCESS) {
  7134.             resp = cstr_new();
  7135.             if(SRP_respond(s_srp, &resp) != SRP_SUCCESS) {
  7136.                 printf("Error computing response\r\n");
  7137.                 SendSRPAuthSB(SRP_REJECT,
  7138.                               (void *) "Error computing response", -1);
  7139.                 return(AUTH_FAILURE);
  7140.             }
  7141.             SendSRPAuthSB(SRP_ACCEPT, resp->data, resp->length);
  7142.             accept_complete = 1;
  7143.             cstr_free(resp);
  7144.  
  7145. #ifdef CK_ENCRYPTION
  7146. #ifdef CK_SSL
  7147.             if (!(ssl_active_flag || tls_active_flag))
  7148. #endif /* CK_SSL */
  7149.             {
  7150.                 skey.type = SK_GENERIC;
  7151.                 skey.length = s_key->length;
  7152.                 skey.data = s_key->data;
  7153.                 encrypt_session_key(&skey, AUTH_SERVER_TO_CLIENT);
  7154.                 cstr_clear_free(s_key);
  7155.                 s_key = NULL;
  7156.             }
  7157. #endif /* CK_ENCRYPTION */
  7158.             auth_finished(AUTH_VALID);
  7159.         }
  7160.         else {
  7161.             SendSRPAuthSB(SRP_REJECT, (void *) "Login incorrect", -1);
  7162.             auth_finished(AUTH_REJECT);
  7163.             return(AUTH_FAILURE);
  7164.         }
  7165.         return AUTH_SUCCESS;
  7166.  
  7167.     default:
  7168.         printf("Unknown SRP option %d\r\n", data[-1]);
  7169.         SendSRPAuthSB(SRP_REJECT, (void *) "Unknown option received", -1);
  7170.         return(AUTH_FAILURE);
  7171.     }
  7172. }
  7173. #endif /* PRE_SRP_1_7_3 */
  7174. #endif /* SRP */
  7175.  
  7176. #ifdef KRB5
  7177. #ifdef KINIT
  7178. /*
  7179.  * clients/kinit/kinit.c
  7180.  *
  7181.  * Copyright 1990 by the Massachusetts Institute of Technology.
  7182.  * All Rights Reserved.
  7183.  *
  7184.  * Export of this software from the United States of America may
  7185.  *   require a specific license from the United States Government.
  7186.  *   It is the responsibility of any person or organization contemplating
  7187.  *   export to obtain such a license before exporting.
  7188.  *
  7189.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  7190.  * distribute this software and its documentation for any purpose and
  7191.  * without fee is hereby granted, provided that the above copyright
  7192.  * notice appear in all copies and that both that copyright notice and
  7193.  * this permission notice appear in supporting documentation, and that
  7194.  * the name of M.I.T. not be used in advertising or publicity pertaining
  7195.  * to distribution of the software without specific, written prior
  7196.  * permission.  M.I.T. makes no representations about the suitability of
  7197.  * this software for any purpose.  It is provided "as is" without express
  7198.  * or implied warranty.
  7199.  *
  7200.  *
  7201.  * Initialize a credentials cache.
  7202.  */
  7203.  
  7204. #define KRB5_DEFAULT_OPTIONS 0
  7205. #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
  7206.  
  7207. static krb5_data tgtname = {
  7208. #ifndef HEIMDAL
  7209.     0,
  7210. #endif /* HEIMDAL */
  7211.     KRB5_TGS_NAME_SIZE,
  7212.     KRB5_TGS_NAME
  7213. };
  7214.  
  7215. /* Internal prototypes */
  7216. _PROTOTYP(static krb5_error_code krb5_validate_tgt,
  7217.         (krb5_context, krb5_ccache,krb5_principal, krb5_data *));
  7218. _PROTOTYP(static krb5_error_code krb5_renew_tgt,
  7219.         (krb5_context, krb5_ccache,
  7220.                         krb5_principal, krb5_data *));
  7221. _PROTOTYP(static krb5_error_code krb5_tgt_gen,
  7222.         (krb5_context, krb5_ccache,
  7223.                         krb5_principal, krb5_data *, int opt));
  7224.  
  7225. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7226. static krb5_error_code KRB5_CALLCONV
  7227. ck_krb5_prompter( krb5_context context,
  7228.                   void *data,
  7229.                   const char *name,
  7230.                   const char *banner,
  7231.                   int num_prompts,
  7232.                   krb5_prompt prompts[])
  7233. {
  7234.     krb5_error_code     errcode = 0;
  7235.     int                 i;
  7236. #ifdef KUI
  7237.     struct txtbox * tb = NULL;
  7238. #else /* KUI */
  7239.     char * prompt = NULL;
  7240. #endif /* KUI */
  7241.     int    len = 0, blen=0, nlen=0;
  7242.  
  7243.     debug(F110,"ck_krb5_prompter name",name,0);
  7244.     debug(F110,"ck_krb5_prompter banner",banner,0);
  7245.     debug(F101,"ck_krb5_prompter num_prompts","",num_prompts);
  7246.  
  7247.     if (name)
  7248.         nlen = strlen(name)+2;
  7249.  
  7250.     if (banner)
  7251.         blen = strlen(banner)+2;
  7252.  
  7253. #ifdef KUI
  7254.     tb = (struct txtbox *) malloc(sizeof(struct txtbox) * num_prompts);
  7255.     if ( tb != NULL ) {
  7256.         int ok;
  7257.         memset(tb,0,sizeof(struct txtbox) * num_prompts);
  7258.         for ( i=0; i < num_prompts; i++ ) {
  7259.             tb[i].t_buf = prompts[i].reply->data;
  7260.             tb[i].t_len = prompts[i].reply->length;
  7261.             tb[i].t_lbl = prompts[i].prompt;
  7262.             tb[i].t_dflt = NULL;
  7263.             tb[i].t_echo = (prompts[i].hidden ? 2 : 1);
  7264.         }   
  7265.  
  7266.         ok = uq_mtxt((char *)banner,NULL,num_prompts,tb);
  7267.         if ( ok ) {
  7268.             for ( i=0; i < num_prompts; i++ )
  7269.                 prompts[i].reply->length = strlen(prompts[i].reply->data);
  7270.         } else
  7271.             errcode = -1;
  7272.     }
  7273. #else /* KUI */
  7274.     for (i = 0; i < num_prompts; i++) {
  7275.         debug(F111,"ck_krb5_prompter prompt",prompts[i].prompt,i);
  7276.  
  7277.         if ( prompt && len < (nlen + blen + strlen(prompts[i].prompt)+2) ) {
  7278.             free(prompt);
  7279.             prompt = NULL;
  7280.         }
  7281.         if ( !prompt )
  7282.             prompt = (char *)malloc(nlen + blen + strlen(prompts[i].prompt)+2);
  7283.         if ( !prompt ) {
  7284.             errcode = KRB5_RC_MALLOC;
  7285.             goto cleanup;
  7286.         }
  7287.         len = nlen + blen + strlen(prompts[i].prompt)+2;
  7288.         ckmakxmsg(prompt,len,
  7289.                  (char *) (name?name:""),
  7290.                  name?"\r\n":"",
  7291.                  (char *) (banner?banner:""),
  7292.                  banner?"\r\n":"",
  7293.                  (char *)prompts[i].prompt,
  7294.                  ": ",NULL,NULL,NULL,NULL,NULL,NULL);
  7295.  
  7296.         memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7297.         if (prompts[i].hidden) {
  7298.             readpass(prompt, prompts[i].reply->data,
  7299.                       prompts[i].reply->length);
  7300.         } else {
  7301.             readtext(prompt, prompts[i].reply->data,
  7302.                       prompts[i].reply->length);
  7303.         }
  7304.         prompts[i].reply->length = strlen(prompts[i].reply->data);
  7305.     }
  7306. #endif /* KUI */
  7307.  
  7308.   cleanup:
  7309. #ifdef KUI
  7310.     if ( tb )
  7311.         free(tb);
  7312. #else /* KUI */
  7313.     if ( prompt )
  7314.         free(prompt);
  7315. #endif /* KUI */
  7316.     if (errcode) {
  7317.         for (i = 0; i < num_prompts; i++) {
  7318.             memset(prompts[i].reply->data, 0, prompts[i].reply->length);
  7319.         }
  7320.     }
  7321.     return errcode;
  7322. }
  7323.  
  7324. /*
  7325.  *      I'm not really sure what to do with this.  The NRL DLLs use a
  7326.  *      different interface for the krb5_prompter callback.  It has
  7327.  *      one less parameter.   This is going to be ugly.
  7328.  */
  7329. static krb5_error_code KRB5_CALLCONV
  7330. ck_NRL_krb5_prompter( krb5_context context,
  7331.                       const char *name,
  7332.                       const char *banner,
  7333.                       int num_prompts,
  7334.                       krb5_prompt prompts[])
  7335. {
  7336.     return(ck_krb5_prompter(context,NULL,name,banner,num_prompts,prompts));
  7337. }
  7338. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7339.  
  7340. #define NO_KEYTAB
  7341.  
  7342. int
  7343. #ifdef CK_ANSIC
  7344. ck_krb5_initTGT( struct krb_op_data * op, struct krb5_init_data * init,
  7345.                  struct krb4_init_data * k4_init)
  7346. #else
  7347. ck_krb5_initTGT(op,init,k4_init)
  7348.     krb_op_data * op; struct krb5_init_data * init;
  7349.     struct krb4_init_data * k4_init;
  7350. #endif /* CK_ANSIC*/
  7351. {
  7352.     krb5_context kcontext;
  7353.     krb5_ccache ccache = NULL;
  7354.     krb5_deltat lifetime = KRB5_DEFAULT_LIFE;   /* -l option */
  7355.     krb5_timestamp starttime = 0;
  7356.     krb5_deltat rlife = 0;
  7357.     int options = KRB5_DEFAULT_OPTIONS;
  7358.     int option;
  7359.     int errflg = 0;
  7360.     krb5_error_code code;
  7361.     krb5_principal me=NULL;
  7362.     krb5_principal server=NULL;
  7363.     krb5_creds my_creds;
  7364.     krb5_timestamp now;
  7365. #ifndef HEIMDAL
  7366.     krb5_address **addrs = (krb5_address **)0;
  7367. #endif /* HEIMDAL */
  7368.     int addr_count=0;
  7369.     int i,j;
  7370. #ifndef NO_KEYTAB
  7371.     int use_keytab = 0;                 /* -k option */
  7372.     krb5_keytab keytab = NULL;
  7373. #endif /* NO_KEYTAB */
  7374.     struct passwd *pw = 0;
  7375.     int pwsize;
  7376.     char *client_name=NULL, principal[256]="", realm[256]="", numstr[40]="";
  7377.     char *password=NULL, passwd[80]="";
  7378. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7379.     krb5_get_init_creds_opt opts;
  7380. #endif
  7381.     char * name;
  7382.     int len;
  7383.  
  7384.     if ( !ck_krb5_is_installed() )
  7385.         return(-1);
  7386.  
  7387. #ifdef COMMENT
  7388.     printf("Kerberos V initialization\r\n");
  7389. #endif /* COMMENT */
  7390.  
  7391.     code = krb5_init_context(&kcontext);
  7392.     if (code) {
  7393.         com_err("krb5_kinit",code,"while init_context");
  7394.         krb5_errno = code;
  7395.         makestr(&krb5_errmsg,error_message(krb5_errno));
  7396.         return(-1);
  7397.     }
  7398.  
  7399.     debug(F110,"krb5_init","krb5_init_context",0);
  7400.  
  7401.     if ((code = krb5_timeofday(kcontext, &now))) {
  7402.         com_err("krb5_kinit",code,"while getting time of day");
  7403.         goto exit_k5_init;
  7404.     }
  7405.  
  7406. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7407.     memset(&opts, 0, sizeof(opts));
  7408.     krb5_get_init_creds_opt_init(&opts);
  7409.     debug(F110,"krb5_init","krb5_get_init_creds_opt_init",0);
  7410. #endif
  7411.  
  7412.     if ( init->renewable ) {
  7413.         options |= KDC_OPT_RENEWABLE;
  7414.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->renewable),"m",NULL,NULL);
  7415. #ifdef HEIMDAL
  7416.         code = -1;
  7417. #else /* HEIMDAL */
  7418.         code = krb5_string_to_deltat(numstr, &rlife);
  7419. #endif /* HEIMDAL */
  7420.         if (code != 0 || rlife == 0) {
  7421.             printf("Bad renewable time value %s\r\n", numstr);
  7422.             errflg++;
  7423.         }
  7424. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7425.         krb5_get_init_creds_opt_set_renew_life(&opts, rlife);
  7426. #endif
  7427.     }
  7428.     if ( init->renew ) {
  7429.         /* renew the ticket */
  7430.         options |= KDC_OPT_RENEW;
  7431.     }
  7432.  
  7433.     if ( init->validate ) {
  7434.         /* validate the ticket */
  7435.         options |= KDC_OPT_VALIDATE;
  7436.     }
  7437.     if ( init->proxiable ) {
  7438.         options |= KDC_OPT_PROXIABLE;
  7439. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7440.         krb5_get_init_creds_opt_set_proxiable(&opts, 1);
  7441. #endif
  7442.     }
  7443.     if ( init->forwardable ) {
  7444.         options |= KDC_OPT_FORWARDABLE;
  7445. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7446.         krb5_get_init_creds_opt_set_forwardable(&opts, 1);
  7447. #endif
  7448.     }
  7449. #ifndef NO_KEYTAB
  7450.     if (  ) {
  7451.         use_keytab = 1;
  7452.     }
  7453.     if (  ) {
  7454.         if (keytab == NULL && keytab_name != NULL) {
  7455.             code = krb5_kt_resolve(kcontext, keytab_name, &keytab);
  7456.             if (code != 0) {
  7457.                 debug(F111,"krb5_init resolving keytab",
  7458.                          keytab_name,code);
  7459.                 errflg++;
  7460.             }
  7461.         }
  7462.     }
  7463. #endif /* NO_KEYTAB */
  7464.     if ( init->lifetime ) {
  7465.         ckmakmsg(numstr,sizeof(numstr),ckitoa(init->lifetime),"m",NULL,NULL);
  7466. #ifdef HEIMDAL
  7467.         code = -1;
  7468. #else /* HEIMDAL */
  7469.         code = krb5_string_to_deltat(numstr, &lifetime);
  7470. #endif /* HEIMDAL */
  7471.         if (code != 0 || lifetime == 0) {
  7472.             printf("Bad lifetime value %s\r\n", numstr);
  7473.             errflg++;
  7474.         }
  7475. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7476.         krb5_get_init_creds_opt_set_tkt_life(&opts, lifetime);
  7477. #endif
  7478.     }
  7479.     if ( init->postdate ) {
  7480.         /* Convert cmdate() to a time_t value */
  7481.         struct tm * time_tm;
  7482.         struct tm * cmdate2tm(char *,int);
  7483.         time_tm = cmdate2tm(init->postdate,0);
  7484.         if ( time_tm )
  7485.             starttime = (krb5_timestamp) mktime(time_tm);
  7486.  
  7487.         if (code != 0 || starttime == 0 || starttime == -1) {
  7488.             krb5_deltat ktmp;
  7489. #ifdef HEIMDAL
  7490.             code = -1;
  7491. #else /* HEIMDAL */
  7492.             code = krb5_string_to_deltat(init->postdate, &ktmp);
  7493. #endif /* HEIMDAL */
  7494.             if (code == 0 && ktmp != 0) {
  7495.                 starttime = now + ktmp;
  7496.                 options |= KDC_OPT_POSTDATED;
  7497.             } else {
  7498.                 printf("Bad postdate start time value %s\r\n",
  7499.                         init->postdate);
  7500.                 errflg++;
  7501.             }
  7502.         } else {
  7503.             options |= KDC_OPT_POSTDATED;
  7504.         }
  7505.     }
  7506.  
  7507.     debug(F110,"krb5_init searching for ccache",op->cache,0);
  7508.  
  7509.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  7510.     if (code != 0) {
  7511.         com_err("krb5_kinit",code,"while getting default ccache");
  7512.         goto exit_k5_init;
  7513.     }
  7514.  
  7515.     /* This is our realm unless it is changed */
  7516.     ckstrncpy(realm,init->realm ? init->realm : krb5_d_realm, 256);
  7517.  
  7518. #ifdef BETATEST
  7519.     /* This code is going to take the realm and attempt to correct */
  7520.     /* the case.                                                   */
  7521.     {
  7522.         profile_t profile;
  7523.  
  7524.         code = krb5_get_profile(kcontext, &profile);
  7525.         if ( !code ) {
  7526.             const char  *names[4];
  7527.             char ** realms;
  7528.             int found = 0;
  7529.  
  7530.             names[0] = "realms";
  7531.             names[1] = NULL;
  7532.  
  7533.             code = profile_get_subsection_names(profile,names,&realms);
  7534.             if ( code == 0 ) {
  7535.                 int i=0;
  7536.                 while ( realms[i] ) {
  7537.                     if (ckstrcmp(realm,realms[i],-1,0) == 0) {
  7538.                         strcpy(realm,realms[i]);
  7539.                         found = 1;
  7540.                         break;
  7541.                     }
  7542.                     i++;
  7543.                 }
  7544.             }
  7545.  
  7546. #ifdef CK_DNS_SRV
  7547.             if ( !found ) {
  7548.                 char * dns_realm = NULL;
  7549.  
  7550.                 /* We did not find the realm in the profile so let's try DNS */
  7551.                 locate_txt_rr("_kerberos",realm,&dns_realm);
  7552.                 if ( dns_realm &&
  7553.                      ckstrcmp(realm,dns_realm,-1,0) == 0 &&
  7554.                      ckstrcmp(realm,dns_realm,-1,1) != 0
  7555.                      ) {
  7556.                     ckstrncpy(realm,dns_realm,256);
  7557.                     free(dns_realm);
  7558.                 }
  7559.             }
  7560. #endif /* CK_DNS_SRV */
  7561.         }
  7562.  
  7563.         if (init->realm &&
  7564.              ckstrcmp(realm,init->realm,-1,0) == 0 &&
  7565.              ckstrcmp(realm,init->realm,-1,1) != 0)
  7566.             strcpy(init->realm,realm);
  7567.         if (ckstrcmp(realm,krb5_d_realm,-1,0) == 0 &&
  7568.              ckstrcmp(realm,krb5_d_realm,-1,1) != 0)
  7569.             strcpy(krb5_d_realm,realm);
  7570.     }
  7571. #endif /* BETATEST */
  7572.  
  7573.     if (init->principal == NULL) {       /* No principal name specified */
  7574. #ifndef NO_KEYTAB
  7575.         if (use_keytab) {
  7576.             /* Use the default host/service name */
  7577.             code = krb5_sname_to_principal(kcontext, NULL, NULL,
  7578.                                             KRB5_NT_SRV_HST, &me);
  7579.             if (code == 0 &&
  7580.                 krb5_princ_realm(kcontext, me)->length < sizeof(realm))
  7581.             {
  7582.                 /* Save the realm */
  7583.                 memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  7584.                         krb5_princ_realm(kcontext, me)->length); /* safe */
  7585.                 realm[krb5_princ_realm(kcontext, me)->length]='\0';
  7586.             } else {
  7587.                 com_err("krb5_kinit",
  7588.                         code,
  7589.                         "when creating default server principal name");
  7590.                 goto exit_k5_init;
  7591.             }
  7592.         } else
  7593. #endif /* NO_KEYTAB */
  7594.         {
  7595.             int len;
  7596.             char * name;
  7597.  
  7598.             /* Get default principal from cache if one exists */
  7599.             code = krb5_cc_get_principal(kcontext, ccache, &me);
  7600. #ifdef HEIMDAL
  7601.             name = me->realm;
  7602.             len = strlen(name);
  7603. #else /* HEIMDAL */
  7604.             len = krb5_princ_realm(kcontext, me)->length;
  7605.             name = krb5_princ_realm(kcontext, me)->data;
  7606. #endif /* HEIMDAL */
  7607.             if (code == 0 && len < sizeof(realm))
  7608.             {
  7609.                 /* Save the realm */
  7610.                 memcpy(realm,name,len); /* safe */
  7611.                 realm[len]='\0';
  7612.             } else {
  7613. #ifdef HAVE_PWD_H
  7614.                 /* Else search passwd file for client */
  7615.  
  7616.                 pw = getpwuid((int) getuid());
  7617.                 if (pw) {
  7618.                     char princ_realm[256];
  7619.                     if ( (strlen(pw->pw_name) + strlen(realm) + 1) > 255 )
  7620.                         goto exit_k5_init;
  7621.  
  7622.                     ckstrncpy(principal,pw->pw_name,256);
  7623.                     ckstrncpy(princ_realm,pw->pw_name,256);
  7624.                     ckstrncat(princ_realm,"@",256);
  7625.                     ckstrncat(princ_realm,realm,256);
  7626.  
  7627.                     if ((code = krb5_parse_name(kcontext,princ_realm,&me))) {
  7628.                         krb5_errno = code;
  7629.                         com_err("krb5_kinit",code,"when parsing name",
  7630.                                   princ_realm);
  7631.                         goto exit_k5_init;
  7632.                     }
  7633.                 } else {
  7634.                     printf(
  7635.                         "Unable to identify user from password file\r\n");
  7636.                     goto exit_k5_init;
  7637.                 }
  7638. #else /* HAVE_PWD_H */
  7639.                 printf("Unable to identify user\r\n");
  7640.                 goto exit_k5_init;
  7641. #endif /* HAVE_PWD_H */
  7642.             }
  7643.         }
  7644.  
  7645. #ifdef HEIMDAL
  7646.         len = me->name.name_string.len;
  7647.         name = *me->name.name_string.val;
  7648. #else /* HEIMDAL */
  7649.         len = krb5_princ_name(kcontext, me)->length;
  7650.         name = krb5_princ_name(kcontext, me)->data;
  7651. #endif /* HEIMDAL */
  7652.         if ( len < sizeof(principal) ) {
  7653.             memcpy(principal,name,len);     /* safe */
  7654.             principal[len]='\0';
  7655.         }
  7656.     } /* Use specified name */
  7657.     else {
  7658.         char princ_realm[256];
  7659.         if ( (strlen(init->principal) +
  7660.               (init->instance ? strlen(init->instance)+1 : 0) +
  7661.               strlen(realm)
  7662.               + 2) > 255 )
  7663.              goto exit_k5_init;
  7664.  
  7665.         ckstrncpy(principal,init->principal,256);
  7666.         ckstrncpy(princ_realm,init->principal,256);
  7667.         if (init->instance) {
  7668.             ckstrncat(princ_realm,"/",256);
  7669.             ckstrncat(princ_realm,init->instance,256);
  7670.         }
  7671.         if (realm[0]) {
  7672.           ckstrncat(princ_realm,"@",256);
  7673.           ckstrncat(princ_realm,realm,256);
  7674.         }
  7675.         if ((code = krb5_parse_name (kcontext, princ_realm, &me))) {
  7676.             com_err("krb5_kinit",code,"when parsing name",princ_realm);
  7677.             goto exit_k5_init;
  7678.         }
  7679.     }
  7680.  
  7681.     if ((code = krb5_unparse_name(kcontext, me, &client_name))) {
  7682.         com_err("krb5_kinit",code,"when unparsing name");
  7683.         goto exit_k5_init;
  7684.     }
  7685.     debug(F110,"krb5_init client_name",client_name,0);
  7686.  
  7687.  
  7688.     memset((char *)&my_creds, 0, sizeof(my_creds));
  7689.     my_creds.client = me;
  7690.  
  7691.     if (init->service == NULL) {
  7692.         if ((code =
  7693.              krb5_build_principal_ext(kcontext,
  7694.                                       &server,
  7695.                                       strlen(realm),realm,
  7696.                                       tgtname.length, tgtname.data,
  7697.                                       strlen(realm),realm,
  7698.                                       0))) {
  7699.             com_err("krb5_kinit",code,"while building server name");
  7700.             goto exit_k5_init;
  7701.         }
  7702.     } else {
  7703.         if (code = krb5_parse_name(kcontext, init->service, &server)) {
  7704.             com_err("krb5_kinit",code,"while parsing service name",
  7705.                     init->service);
  7706.             goto exit_k5_init;
  7707.         }
  7708.     }
  7709.  
  7710.     my_creds.server = server;
  7711.  
  7712.     if (options & KDC_OPT_POSTDATED) {
  7713.         my_creds.times.starttime = starttime;
  7714.         my_creds.times.endtime = starttime + lifetime;
  7715.     } else {
  7716.         my_creds.times.starttime = 0;   /* start timer when request
  7717.                                            gets to KDC */
  7718.         my_creds.times.endtime = now + lifetime;
  7719.     }
  7720.     if (options & KDC_OPT_RENEWABLE) {
  7721.         my_creds.times.renew_till = now + rlife;
  7722.     } else
  7723.         my_creds.times.renew_till = 0;
  7724.  
  7725.     if (options & KDC_OPT_VALIDATE) {
  7726.         krb5_data outbuf;
  7727.  
  7728. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7729.         code = krb5_get_validated_creds(kcontext,
  7730.                                         &my_creds, me, ccache, init->service);
  7731.         if ( code == -1 )
  7732. #endif
  7733.         {
  7734. #ifdef HEIMDAL
  7735.             printf("?validate not implemented\r\n");
  7736.             code = -1;
  7737.             goto exit_k5_init;
  7738. #else /* HEIMDAL */
  7739.             code = krb5_validate_tgt(kcontext, ccache, server, &outbuf);
  7740. #endif /* HEIMDAL */
  7741.         }
  7742.         if (code) {
  7743.             com_err("krb5_kinit",code,"validating tgt");
  7744.             goto exit_k5_init;
  7745.         }
  7746.         /* should be done... */
  7747.         goto exit_k5_init;
  7748.     }
  7749.  
  7750.     if (options & KDC_OPT_RENEW) {
  7751.         krb5_data outbuf;
  7752.  
  7753. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7754.         code = krb5_get_renewed_creds(kcontext,
  7755.                                       &my_creds, me, ccache, init->service);
  7756.         if ( code == -1 )
  7757. #endif
  7758.         {
  7759. #ifdef HEIMDAL
  7760.             printf("?renew not implemented\r\n");
  7761.             code = -1;
  7762.             goto exit_k5_init;
  7763. #else /* HEIMDAL */
  7764.             code = krb5_renew_tgt(kcontext, ccache, server, &outbuf);
  7765. #endif /* HEIMDAL */
  7766.         }
  7767.         if (code) {
  7768.             com_err("krb5_kinit",code,"while renewing tgt");
  7769.             goto exit_k5_init;
  7770.         }
  7771.         /* should be done... */
  7772.         goto exit_k5_init;
  7773.     }
  7774.  
  7775. #ifndef HEIMDAL
  7776.     if ( init->addrs && !init->no_addresses ) {
  7777.         /* construct an array of krb5_address structs to pass to get_in_tkt */
  7778.         /* include both the local ip addresses as well as any other that    */
  7779.         /* are specified.                                                   */
  7780.         unsigned long ipaddr;
  7781.  
  7782.         for ( addr_count=0;addr_count<KRB5_NUM_OF_ADDRS;addr_count++ )
  7783.             if ( init->addrs[addr_count] == NULL )
  7784.                 break;
  7785.  
  7786.         if (addr_count > 0) {
  7787.             krb5_address ** local_addrs=NULL;
  7788.             krb5_os_localaddr(kcontext, &local_addrs);
  7789.             i = 0;
  7790.             while ( local_addrs[i] )
  7791.                 i++;
  7792.             addr_count += i;
  7793.  
  7794.             addrs = (krb5_address **)
  7795.               malloc((addr_count+1) * sizeof(krb5_address));
  7796.             if ( !addrs ) {
  7797.                 krb5_free_addresses(kcontext, local_addrs);
  7798.                 goto exit_k5_init;
  7799.             }
  7800.             memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
  7801.             i = 0;
  7802.             while ( local_addrs[i] ) {
  7803.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7804.                 if (addrs[i] == NULL) {
  7805.                     krb5_free_addresses(kcontext, local_addrs);
  7806.                     goto exit_k5_init;
  7807.                 }
  7808.  
  7809.                 addrs[i]->magic = local_addrs[i]->magic;
  7810.                 addrs[i]->addrtype = local_addrs[i]->addrtype;
  7811.                 addrs[i]->length = local_addrs[i]->length;
  7812.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  7813.                 if (!addrs[i]->contents) {
  7814.                     krb5_free_addresses(kcontext, local_addrs);
  7815.                     goto exit_k5_init;
  7816.                 }
  7817.  
  7818.                 memcpy(addrs[i]->contents,local_addrs[i]->contents,
  7819.                         local_addrs[i]->length);        /* safe */
  7820.                 i++;
  7821.             }
  7822.             krb5_free_addresses(kcontext, local_addrs);
  7823.  
  7824.             for ( j=0;i<addr_count;i++,j++ ) {
  7825.                 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
  7826.                 if (addrs[i] == NULL)
  7827.                     goto exit_k5_init;
  7828.  
  7829.                 addrs[i]->magic = KV5M_ADDRESS;
  7830.                 addrs[i]->addrtype = AF_INET;
  7831.                 addrs[i]->length = 4;
  7832.                 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
  7833.                 if (!addrs[i]->contents)
  7834.                     goto exit_k5_init;
  7835.  
  7836.                 ipaddr = inet_addr(init->addrs[j]);
  7837.                 memcpy(addrs[i]->contents,&ipaddr,4);   /* safe */
  7838.             }
  7839. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7840.             krb5_get_init_creds_opt_set_address_list(&opts,addrs);
  7841. #endif
  7842.         }
  7843.      }
  7844. #endif /* !HEIMDAL */
  7845. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7846.     if ( init->no_addresses )
  7847.         krb5_get_init_creds_opt_set_address_list(&opts,NULL);
  7848. #endif
  7849.  
  7850. #ifndef NO_KEYTAB
  7851.     if (!use_keytab)
  7852. #endif
  7853.     {
  7854.         if ( init->password ) {
  7855.             pwsize = strlen(init->password);
  7856.             if ( pwsize )
  7857.                 password = init->password;
  7858.         } else if (init->getk4 && k4_init) {
  7859.             /* When we are requesting that K4 tickets be automatically */
  7860.             /* acquired when K5 tickets are acquired, we must get the  */
  7861.             /* password up front.                                      */
  7862.             char prmpt[256];
  7863.             extern char * k5prprompt;
  7864.             extern char * k5pwprompt;
  7865.             int ok = 0;
  7866.  
  7867.             if ( k5pwprompt && k5pwprompt[0] &&
  7868.                  (strlen(k5pwprompt) + strlen(principal) +
  7869.                   strlen(realm) - 4) < sizeof(prmpt)) {
  7870.                 sprintf(prmpt,k5pwprompt,principal,realm);
  7871.             } else
  7872.                 ckmakxmsg(prmpt,sizeof(prmpt),
  7873.                            k5pwprompt && k5pwprompt[0] ? k5pwprompt :
  7874.                            "Kerberos 5 Password for ",
  7875.                            principal,"@",realm,": ",
  7876.                            NULL,NULL,NULL,NULL,NULL,NULL,NULL
  7877.                            );
  7878.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7879.             if ( ok )
  7880.                 password = passwd;
  7881.  
  7882.             if ( k4_init->password == NULL )
  7883.                 makestr(&k4_init->password,passwd);
  7884.         }
  7885. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7886.         debug(F100,"krb5_init calling krb5_get_init_creds_password()","",0);
  7887. #ifdef OS2
  7888.         if ( is_NRL_KRB5() )
  7889.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  7890.                                                  password,
  7891.                                                  (void *)ck_NRL_krb5_prompter,
  7892.                                                  NULL,
  7893.                                                  starttime, init->service,
  7894.                                                  &opts);
  7895.         else
  7896. #endif /* OS2 */
  7897.             code = krb5_get_init_creds_password(kcontext, &my_creds, me,
  7898.                                                  password,
  7899.                                                  ck_krb5_prompter,
  7900.                                                  NULL,
  7901.                                                  starttime, init->service,
  7902.                                                  &opts);
  7903.         debug(F111,"krb5_init","krb5_get_init_creds_password()",code);
  7904.  
  7905.         if ( code == -1 )
  7906.         {
  7907.             if (!password) {
  7908.                 char prmpt[256];
  7909.                 int ok;
  7910.  
  7911.                 ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  7912.                           client_name,": ",NULL);
  7913.                 ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7914.                 if ( ok )
  7915.                     password = passwd;
  7916.             }
  7917.  
  7918.             if ( !password[0] ) {
  7919.                 debug(F111,"krb5_init","no password specified",0);
  7920.                 printf("A password must be specified for %s.\r\n",client_name);
  7921.                 goto exit_k5_init;
  7922.             }
  7923.             code = krb5_get_in_tkt_with_password(kcontext, options,
  7924. #ifdef HEIMDAL
  7925.                                                   NULL,
  7926. #else /* HEIMDAL */
  7927.                                             init->no_addresses ? NULL :addrs,
  7928. #endif /* HEIMDAL */
  7929.                                                   NULL, NULL,
  7930.                                                   password,
  7931.                                                   NULL, &my_creds, NULL);
  7932.             if ( code )
  7933.                 debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  7934.         }
  7935. #else /* KRB5_HAVE_GET_INIT_CREDS */
  7936.         if (!password) {
  7937.             char prmpt[256];
  7938.             int ok;
  7939.  
  7940.             ckmakmsg(prmpt,sizeof(prmpt),"Kerberos 5 Password for ",
  7941.                       client_name,": ",NULL);
  7942.             ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  7943.             if ( ok )
  7944.                 password = passwd;
  7945.         }
  7946.  
  7947.         if ( !password[0] ) {
  7948.             debug(F111,"krb5_init","no password specified",0);
  7949.             printf("A password must be specified for %s.\r\n",client_name);
  7950.             goto exit_k5_init;
  7951.         }
  7952.         code = krb5_get_in_tkt_with_password(kcontext, options,
  7953. #ifdef HEIMDAL
  7954.                                               NULL,
  7955. #else /* HEIMDAL */
  7956.                                             init->no_addresses ? NULL :addrs,
  7957. #endif /* HEIMDAL */
  7958.                                               NULL, NULL,
  7959.                                               password,
  7960.                                               NULL, &my_creds, NULL);
  7961.         if ( code )
  7962.             debug(F111,"krb5_init","krb5_get_in_tkt_with_password()",code);
  7963. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7964.  
  7965.         if ( init->password && pwsize > 0 )
  7966.             memset(init->password, 0, pwsize);
  7967.         memset(passwd,0,80);
  7968.     }
  7969. #ifndef NO_KEYTAB
  7970.     else {
  7971. #ifdef KRB5_HAVE_GET_INIT_CREDS
  7972.         code = krb5_get_init_creds_keytab(kcontext, &my_creds, me, keytab,
  7973.                                            starttime, init->service,
  7974.                                            &opts);
  7975. #ifdef OS2
  7976.         if ( code == -1)
  7977.             code = krb5_get_in_tkt_with_keytab(kcontext, options,
  7978.                                            init->no_addresses ? NULL :addrs,
  7979.                                                 NULL, NULL, keytab, NULL,
  7980.                                                 &my_creds, 0);
  7981. #endif /* OS2 */
  7982. #else /* KRB5_HAVE_GET_INIT_CREDS */
  7983.         code = krb5_get_in_tkt_with_keytab(kcontext, options,
  7984. #ifdef HEIMDAL
  7985.                                                   NULL,
  7986. #else /* HEIMDAL */
  7987.                                             init->no_addresses ? NULL :addrs,
  7988. #endif /* HEIMDAL */
  7989.                                             NULL, NULL, keytab, NULL,
  7990.                                             &my_creds, 0);
  7991. #endif /* KRB5_HAVE_GET_INIT_CREDS */
  7992.     }
  7993. #endif
  7994.  
  7995.     if (code) {
  7996.         switch (code) {
  7997.         case KRB5KRB_AP_ERR_BAD_INTEGRITY:
  7998.             printf("Password incorrect\r\n");
  7999.             goto exit_k5_init;
  8000.         case KRB5KRB_AP_ERR_V4_REPLY:
  8001.             if (init->getk4 && k4_init) {
  8002.                 printf("Kerberos 5 Tickets not support by server.  ");
  8003.                 printf("A version 4 Ticket will be requested.\r\n");
  8004.             }
  8005.             goto exit_k5_init;
  8006.         default:
  8007.             goto exit_k5_init;
  8008.         }
  8009.     }
  8010.  
  8011.     debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8012.  
  8013.     code = krb5_cc_initialize (kcontext, ccache, me);
  8014.     if ( code == KRB5_CC_BADNAME ) {
  8015.         /* This is a really ugly hack that should not have to be here.
  8016.          * krb5_cc_initialize should not fail with an error if the
  8017.          * cache already exists.  The reason the problem is occuring
  8018.          * is that the krb5 library is no longer calling cc_destroy()
  8019.          * when cc_initialize() is called and the CCAPI implementation
  8020.          * on Windows has not yet been corrected to handle it.  To
  8021.          * ensure that K95 will continue to work with both we will call
  8022.          * cc_destroy() if the cc_initialize() call fails with a BADNAME
  8023.          * error.  If the cc_destroy() is successful, we will try again.
  8024.          */
  8025.  
  8026.         debug(F100,"krb5_init calling krb5_cc_destroy()","",0);
  8027.         code = krb5_cc_destroy (kcontext, ccache);
  8028.         if ( !code ) {
  8029.             debug(F100,"krb5_init calling k5_get_ccache()","",0);
  8030.             code = k5_get_ccache(kcontext,&ccache,op->cache);
  8031.             debug(F100,"krb5_init calling krb5_cc_initialize()","",0);
  8032.             code = krb5_cc_initialize (kcontext, ccache, me);
  8033.         } else
  8034.             code = KRB5_CC_BADNAME;
  8035.     }
  8036.     if (code) {
  8037.         com_err("krb5_kinit",code,"when initializing cache",op->cache);
  8038.         goto exit_k5_init;
  8039.     }
  8040.  
  8041.     debug(F100,"krb5_init calling krb5_cc_store_cred()","",0);
  8042.     code = krb5_cc_store_cred(kcontext, ccache, &my_creds);
  8043.     if (code) {
  8044.         com_err("krb5_kinit",code,"while storing credentials");
  8045.         goto exit_k5_init;
  8046.     }
  8047.  
  8048. exit_k5_init:
  8049.     debug(F100,"krb5_init exit_k5_init","",0);
  8050.  
  8051. #ifndef HEIMDAL
  8052.     /* Free krb5_address structures if we created them */
  8053.     if ( addrs ) {
  8054.         for ( i=0;i<addr_count;i++ ) {
  8055.             if ( addrs[i] ) {
  8056.                 if ( addrs[i]->contents )
  8057.                     free(addrs[i]->contents);
  8058.                 free(addrs[i]);
  8059.             }
  8060.         }
  8061.     }
  8062. #endif /* HEIMDAL */
  8063.  
  8064.     /* my_creds is pointing at server */
  8065.     debug(F100,"krb5_init calling krb5_free_principal()","",0);
  8066.     krb5_free_principal(kcontext, server);
  8067.     debug(F100,"krb5_init calling krb5_cc_close()","",0);
  8068.     krb5_cc_close(kcontext,ccache);
  8069.     debug(F100,"krb5_init calling krb5_free_context()","",0);
  8070.     krb5_free_context(kcontext);
  8071.  
  8072.     krb5_errno = code;
  8073.     makestr(&krb5_errmsg,krb5_errno ? error_message(krb5_errno) : "OK");
  8074.  
  8075.     if ( init->getk4 && k4_init ) {
  8076.         int k4rc = ck_krb4_initTGT(op,k4_init);
  8077.         return (k4rc < 0 ? -1 : 0);
  8078.     }
  8079.  
  8080.     printf("Result from realm %s: %s\r\n",realm,
  8081.             code?error_message(code):"OK");
  8082.     return(code?-1:0);
  8083. }
  8084.  
  8085. #ifndef HEIMDAL
  8086. #define VALIDATE 0
  8087. #define RENEW 1
  8088.  
  8089. /* stripped down version of krb5_mk_req */
  8090. static krb5_error_code
  8091. #ifdef CK_ANSIC
  8092. krb5_validate_tgt( krb5_context context,
  8093.                    krb5_ccache ccache,
  8094.                    krb5_principal     server, /* tgtname */
  8095.                    krb5_data *outbuf )
  8096. #else
  8097. krb5_validate_tgt(context, ccache, server, outbuf)
  8098.      krb5_context context;
  8099.      krb5_ccache ccache;
  8100.      krb5_principal     server; /* tgtname */
  8101.      krb5_data *outbuf;
  8102. #endif
  8103. {
  8104.     return krb5_tgt_gen(context, ccache, server, outbuf, VALIDATE);
  8105. }
  8106.  
  8107. /* stripped down version of krb5_mk_req */
  8108. static krb5_error_code
  8109. #ifdef CK_ANSIC
  8110. krb5_renew_tgt(krb5_context context,
  8111.                 krb5_ccache ccache,
  8112.                 krb5_principal    server, /* tgtname */
  8113.                 krb5_data *outbuf)
  8114. #else
  8115. krb5_renew_tgt(context, ccache, server, outbuf)
  8116.      krb5_context context;
  8117.      krb5_ccache ccache;
  8118.      krb5_principal       server; /* tgtname */
  8119.      krb5_data *outbuf;
  8120. #endif
  8121. {
  8122.     return krb5_tgt_gen(context, ccache, server, outbuf, RENEW);
  8123. }
  8124.  
  8125.  
  8126. /* stripped down version of krb5_mk_req */
  8127. static krb5_error_code
  8128. #ifdef CK_ANSIC
  8129. krb5_tgt_gen(krb5_context context,
  8130.               krb5_ccache ccache,
  8131.               krb5_principal      server, /* tgtname */
  8132.               krb5_data *outbuf,
  8133.               int opt)
  8134. #else
  8135. krb5_tgt_gen(context, ccache, server, outbuf, opt)
  8136.      krb5_context context;
  8137.      krb5_ccache ccache;
  8138.      krb5_principal       server; /* tgtname */
  8139.      krb5_data *outbuf;
  8140.      int opt;
  8141. #endif
  8142. {
  8143.     krb5_error_code       retval;
  8144.     krb5_creds          * credsp;
  8145.     krb5_creds            creds;
  8146.  
  8147.     /* obtain ticket & session key */
  8148.     memset((char *)&creds, 0, sizeof(creds));
  8149.     if ((retval = krb5_copy_principal(context, server, &creds.server)))
  8150.         goto cleanup;
  8151.  
  8152.     if ((retval = krb5_cc_get_principal(context, ccache, &creds.client)))
  8153.         goto cleanup_creds;
  8154.  
  8155.     if (opt == VALIDATE) {
  8156.         if ((retval = krb5_get_credentials_validate(context, 0,
  8157.                                                     ccache, &creds, &credsp)))
  8158.           goto cleanup_creds;
  8159.     } else {
  8160.         if ((retval = krb5_get_credentials_renew(context, 0,
  8161.                                                  ccache, &creds, &credsp)))
  8162.           goto cleanup_creds;
  8163.     }
  8164.  
  8165.     /* we don't actually need to do the mk_req, just get the creds. */
  8166. cleanup_creds:
  8167.     krb5_free_cred_contents(context, &creds);
  8168.  
  8169. cleanup:
  8170.  
  8171.     return retval;
  8172. }
  8173. #endif /* HEIMDAL */
  8174. #endif /* KINIT */
  8175. #ifdef KDESTROY
  8176. int
  8177. #ifdef CK_ANSIC
  8178. ck_krb5_destroy(struct krb_op_data * op)
  8179. #else
  8180. ck_krb5_destroy(op) struct krb_op_data * op;
  8181. #endif
  8182. {
  8183.     krb5_context kcontext;
  8184.     krb5_error_code retval;
  8185.     int c;
  8186.     krb5_ccache ccache = NULL;
  8187.     char *cache_name = NULL;
  8188.     int code;
  8189.     int errflg=0;
  8190.     int quiet = 0;
  8191.  
  8192.     if ( !ck_krb5_is_installed() )
  8193.         return(-1);
  8194.  
  8195.     code = krb5_init_context(&kcontext);
  8196.     if (code) {
  8197.         debug(F101,"ck_krb5_destroy while initializing krb5","",code);
  8198.         krb5_errno = code;
  8199.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8200.         return(-1);
  8201.     }
  8202.  
  8203.     code = k5_get_ccache(kcontext,&ccache,op->cache);
  8204.     if (code != 0) {
  8205.         debug(F101,"ck_krb5_destroy while getting ccache",
  8206.                "",code);
  8207.         krb5_free_context(kcontext);
  8208.         krb5_errno = code;
  8209.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8210.         return(-1);
  8211.     }
  8212.  
  8213.     code = krb5_cc_destroy (kcontext, ccache);
  8214.     if (code != 0) {
  8215.         debug(F101,"ck_krb5_destroy while destroying cache","",code);
  8216.         if ( code == KRB5_FCC_NOFILE )
  8217.             printf("No ticket cache to destroy.\r\n");
  8218.         else
  8219.             printf("Ticket cache NOT destroyed!\r\n");
  8220.         krb5_cc_close(kcontext,ccache);
  8221.         krb5_free_context(kcontext);
  8222.         krb5_errno = code;
  8223.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8224.         return(-1);
  8225.     }
  8226.  
  8227.     printf("Tickets destroyed.\r\n");
  8228.     /* Do not call krb5_cc_close() because cache has been destroyed */
  8229.     krb5_free_context(kcontext);
  8230.     krb5_errno = 0;
  8231.     makestr(&krb5_errmsg,"OK");
  8232.     return (0);
  8233. }
  8234. #else /* KDESTROY */
  8235. #ifdef KRB5
  8236. int
  8237. #ifdef CK_ANSIC
  8238. ck_krb5_destroy(struct krb_op_data * op)
  8239. #else
  8240. ck_krb5_destroy(op) struct krb_op_data * op;
  8241. #endif
  8242. {
  8243.     printf("?Not implemented.\r\n");
  8244.     return(-1);
  8245. }
  8246. #endif /* KRB5 */
  8247. #endif /* KDESTROY */
  8248. #ifndef KLIST
  8249. #ifdef KRB5
  8250. int
  8251. #ifdef CK_ANSIC
  8252. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8253. #else
  8254. ck_krb5_list_creds(op,lc)
  8255.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8256. #endif
  8257. {
  8258.     printf("?Not implemented.\r\n");
  8259.     return(-1);
  8260. }
  8261. #endif /* KRB5 */
  8262. #else /* KLIST */
  8263. static int show_flags = 0, show_time = 0, status_only = 0, show_keys = 0;
  8264. static int show_etype = 0, show_addr = 0;
  8265. static char *defname;
  8266. static char *progname;
  8267. static krb5_int32 now;
  8268. static int timestamp_width;
  8269.  
  8270. _PROTOTYP(static char * etype_string, (krb5_enctype ));
  8271. _PROTOTYP(static void show_credential,(krb5_context,krb5_creds *));
  8272. _PROTOTYP(static int do_ccache, (krb5_context,char *));
  8273. _PROTOTYP(static int do_keytab, (krb5_context,char *));
  8274. _PROTOTYP(static void printtime, (time_t));
  8275. _PROTOTYP(static void fillit, (int, int));
  8276.  
  8277. #define DEFAULT 0
  8278. #define CCACHE 1
  8279. #define KEYTAB 2
  8280.  
  8281. int
  8282. #ifdef CK_ANSIC
  8283. ck_krb5_list_creds(struct krb_op_data * op, struct krb5_list_cred_data * lc)
  8284. #else
  8285. ck_krb5_list_creds(op,lc)
  8286.     struct krb_op_data * op; struct krb5_list_cred_data * lc;
  8287. #endif
  8288. {
  8289.     krb5_context kcontext;
  8290.     krb5_error_code retval;
  8291.     int code;
  8292.     char *name = op->cache;
  8293.     int mode;
  8294.  
  8295.     if ( !ck_krb5_is_installed() )
  8296.         return(-1);
  8297.  
  8298.     code = krb5_init_context(&kcontext);
  8299.     if (code) {
  8300.         debug(F101,"ck_krb5_list_creds while initializing krb5","",code);
  8301.         krb5_errno = code;
  8302.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8303.         return(-1);
  8304.     }
  8305.  
  8306.     name = op->cache;
  8307.     mode = DEFAULT;
  8308.     show_flags = 0;
  8309.     show_time = 0;
  8310.     status_only = 0;
  8311.     show_keys = 0;
  8312.     show_etype = 0;
  8313.     show_addr = 0;
  8314.  
  8315.     show_flags = lc->flags;
  8316.     show_etype = lc->encryption;
  8317.     show_addr  = lc->addr;
  8318.     show_time = 1;
  8319.     show_keys = 1;
  8320.     mode = CCACHE;
  8321.  
  8322.     if ((code = krb5_timeofday(kcontext, &now))) {
  8323.         if (!status_only)
  8324.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  8325.                    "",code);
  8326.         krb5_free_context(kcontext);
  8327.         krb5_errno = code;
  8328.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8329.         return(-1);
  8330.     }
  8331.     else {
  8332.         char tmp[BUFSIZ];
  8333.  
  8334.         if (!krb5_timestamp_to_sfstring(now, tmp, 20, (char *) NULL) ||
  8335.             !krb5_timestamp_to_sfstring(now, tmp, sizeof(tmp), (char *) NULL))
  8336.             timestamp_width = (int) strlen(tmp);
  8337.         else
  8338.             timestamp_width = 15;
  8339.     }
  8340.  
  8341.     if (mode == DEFAULT || mode == CCACHE)
  8342.          retval = do_ccache(kcontext,name);
  8343.     else
  8344.          retval = do_keytab(kcontext,name);
  8345.     krb5_free_context(kcontext);
  8346.     return(retval);
  8347. }
  8348.  
  8349. static int
  8350. #ifdef CK_ANSIC
  8351. do_keytab(krb5_context kcontext, char * name)
  8352. #else
  8353. do_keytab(kcontext,name) krb5_context kcontext; char * name;
  8354. #endif
  8355. {
  8356.     krb5_keytab kt;
  8357.     krb5_keytab_entry entry;
  8358.     krb5_kt_cursor cursor;
  8359.     char buf[BUFSIZ]; /* hopefully large enough for any type */
  8360.     char *pname;
  8361.     int code = 0;
  8362.  
  8363.     if (name == NULL) {
  8364.         if ((code = krb5_kt_default(kcontext, &kt))) {
  8365.             debug(F101,"ck_krb5_list_creds while getting default keytab",
  8366.                    "",code);
  8367.             krb5_errno = code;
  8368.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8369.             return(-1);
  8370.         }
  8371.     } else {
  8372.         if ((code = krb5_kt_resolve(kcontext, name, &kt))) {
  8373.             debug(F111,"ck_krb5_list_creds while resolving keytab",
  8374.                      name,code);
  8375.             krb5_errno = code;
  8376.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8377.             return(-1);
  8378.         }
  8379.     }
  8380.  
  8381.     if ((code = krb5_kt_get_name(kcontext, kt, buf, BUFSIZ))) {
  8382.         debug(F101,"ck_krb5_list_creds while getting keytab name",
  8383.                "",code);
  8384.         krb5_errno = code;
  8385.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8386.         return(-1);
  8387.     }
  8388.  
  8389.      printf("Keytab name: %s\r\n", buf);
  8390.  
  8391.      if ((code = krb5_kt_start_seq_get(kcontext, kt, &cursor))) {
  8392.          debug(F101,"ck_krb5_list_creds while starting keytab scan",
  8393.                 "",code);
  8394.          krb5_errno = code;
  8395.          makestr(&krb5_errmsg,error_message(krb5_errno));
  8396.          return(-1);
  8397.      }
  8398.  
  8399.      if (show_time) {
  8400.           printf("KVNO Timestamp");
  8401.           fillit(timestamp_width - sizeof("Timestamp") + 2, (int) ' ');
  8402.           printf("Principal\r\n");
  8403.           printf("---- ");
  8404.           fillit(timestamp_width, (int) '-');
  8405.           printf(" ");
  8406.           fillit(78 - timestamp_width - sizeof("KVNO"), (int) '-');
  8407.           printf("\r\n");
  8408.      } else {
  8409.           printf("KVNO Principal\r\n");
  8410.           printf(
  8411. "---- --------------------------------------------------------------------\
  8412. ------\r\n");
  8413.      }
  8414.  
  8415.     while ((code = krb5_kt_next_entry(kcontext, kt, &entry, &cursor)) == 0) {
  8416.         if ((code = krb5_unparse_name(kcontext, entry.principal, &pname))) {
  8417.             debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8418.                    "",code);
  8419.             krb5_errno = code;
  8420.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8421.             return(-1);
  8422.         }
  8423.         printf("%4d ", entry.vno);
  8424.         if (show_time) {
  8425.             printtime(entry.timestamp);
  8426.             printf(" ");
  8427.         }
  8428.         printf("%s", pname);
  8429.         if (show_etype)
  8430.             printf(" (%s) " ,
  8431. #ifdef HEIMDAL
  8432.                     etype_string(entry.key.keytype)
  8433. #else /* HEIMDAL */
  8434.                     etype_string(entry.key.enctype)
  8435. #endif /* HEIMDAL */
  8436.                     );
  8437.         if (show_keys) {
  8438.             printf(" (0x");
  8439.             {
  8440.                 int i;
  8441.                 for (i = 0; i < entry.key.length; i++)
  8442.                     printf("%02x",
  8443. #ifdef HEIMDAL
  8444.                             entry.key.keyvalue[i]
  8445. #else /* HEIMDAL */
  8446.                             entry.key.contents[i]
  8447. #endif /* HEIMDAL */
  8448.                             );
  8449.             }
  8450.             printf(")");
  8451.         }
  8452.         printf("\r\n");
  8453.         krb5_free_unparsed_name(kcontext,pname);
  8454.     }
  8455.     if (code && code != KRB5_KT_END) {
  8456.         debug(F101,"ck_krb5_list_creds while scanning keytab",
  8457.                "",code);
  8458.         krb5_errno = code;
  8459.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8460.         return(-1);
  8461.     }
  8462.     if ((code = krb5_kt_end_seq_get(kcontext, kt, &cursor))) {
  8463.         debug(F101,"ck_krb5_list_creds while ending keytab scan",
  8464.                "",code);
  8465.         krb5_errno = code;
  8466.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8467.         return(-1);
  8468.     }
  8469.     krb5_errno = 0;
  8470.     makestr(&krb5_errmsg,"OK");
  8471.     return(0);
  8472. }
  8473.  
  8474. static int
  8475. #ifdef CK_ANSIC
  8476. do_ccache(krb5_context kcontext, char * cc_name)
  8477. #else
  8478. do_ccache(kcontext,name) krb5_context kcontext; char * cc_name;
  8479. #endif
  8480. {
  8481.     krb5_ccache cache = NULL;
  8482.     krb5_cc_cursor cur;
  8483.     krb5_creds creds;
  8484.     krb5_principal princ=NULL;
  8485.     krb5_flags flags=0;
  8486.     krb5_error_code code = 0;
  8487.     int exit_status = 0;
  8488.  
  8489.     if (status_only)
  8490.         /* exit_status is set back to 0 if a valid tgt is found */
  8491.         exit_status = 1;
  8492.  
  8493.     code = k5_get_ccache(kcontext,&cache,cc_name);
  8494.     if (code != 0) {
  8495.         debug(F111,"do_ccache while getting ccache",
  8496.                error_message(code),code);
  8497.         krb5_errno = code;
  8498.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8499.         return(-1);
  8500.     }
  8501.  
  8502.     flags = 0;                          /* turns off OPENCLOSE mode */
  8503.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8504.         if (code == ENOENT) {
  8505.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  8506.                    krb5_cc_get_name(kcontext, cache),code);
  8507.         } else {
  8508.             debug(F111,
  8509.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  8510.                   krb5_cc_get_name(kcontext, cache),code);
  8511.         }
  8512.         printf("No ticket File.\r\n");
  8513.         krb5_errno = code;
  8514.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8515.         krb5_cc_close(kcontext,cache);
  8516.         return(-1);
  8517.     }
  8518.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  8519.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  8520.                "",code);
  8521.         krb5_errno = code;
  8522.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8523.         krb5_cc_close(kcontext,cache);
  8524.         return(-1);
  8525.     }
  8526.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  8527.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  8528.                "",code);
  8529.         krb5_errno = code;
  8530.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8531.         krb5_cc_close(kcontext,cache);
  8532.         return(-1);
  8533.     }
  8534.     if (!status_only) {
  8535.         printf("Ticket cache:      %s:%s\r\nDefault principal: %s\r\n\r\n",
  8536.                 krb5_cc_get_type(kcontext, cache),
  8537.                 krb5_cc_get_name(kcontext, cache), defname);
  8538.         printf("Valid starting");
  8539.         fillit(timestamp_width - sizeof("Valid starting") + 3,
  8540.                (int) ' ');
  8541.         printf("Expires");
  8542.         fillit(timestamp_width - sizeof("Expires") + 3,
  8543.                (int) ' ');
  8544.         printf("Service principal\r\n");
  8545.     }
  8546.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  8547.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  8548.                "",code);
  8549.         krb5_errno = code;
  8550.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8551.         krb5_cc_close(kcontext,cache);
  8552.         return(-1);
  8553.     }
  8554.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  8555.         if (status_only) {
  8556.             if (exit_status && creds.server->length == 2 &&
  8557.                 strcmp(creds.server->realm.data, princ->realm.data) == 0 &&
  8558.                 strcmp((char *)creds.server->data[0].data, "krbtgt") == 0 &&
  8559.                 strcmp((char *)creds.server->data[1].data,
  8560.                        princ->realm.data) == 0 &&
  8561.                 creds.times.endtime > now)
  8562.                 exit_status = 0;
  8563.         } else {
  8564.             show_credential(kcontext, &creds);
  8565.         }
  8566.         krb5_free_cred_contents(kcontext, &creds);
  8567.     }
  8568.     printf("\r\n");
  8569.     if (code == KRB5_CC_END || code == KRB5_CC_NOTFOUND) {
  8570.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  8571.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  8572.                    "",code);
  8573.             krb5_errno = code;
  8574.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8575.             krb5_cc_close(kcontext,cache);
  8576.             return(-1);
  8577.         }
  8578.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  8579.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  8580.             debug(F101,"ck_krb5_list_creds while closing ccache",
  8581.                    "",code);
  8582.             krb5_errno = code;
  8583.             makestr(&krb5_errmsg,error_message(krb5_errno));
  8584.             krb5_cc_close(kcontext,cache);
  8585.             return(-1);
  8586.         }
  8587.         krb5_errno = 0;
  8588.         makestr(&krb5_errmsg,"OK");
  8589.         krb5_cc_close(kcontext,cache);
  8590.         return(0);
  8591.     } else {
  8592.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  8593.         krb5_errno = code;
  8594.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8595.         krb5_cc_close(kcontext,cache);
  8596.         return(-1);
  8597.     }
  8598.     krb5_errno = 0;
  8599.     makestr(&krb5_errmsg,"OK");
  8600.     krb5_cc_close(kcontext,cache);
  8601.     return(0);
  8602. }
  8603.  
  8604. static char *
  8605. #ifdef CK_ANSIC
  8606. #ifdef HEIMDAL
  8607. etype_string(krb5_keytype enctype)
  8608. #else /* HEIMDAL */
  8609. etype_string(krb5_enctype enctype)
  8610. #endif /* HEIMDAL */
  8611. #else
  8612. #ifdef HEIMDAL
  8613. etype_string(enctype) krb5_keytype enctype;
  8614. #else /* HEIMDAL */
  8615. etype_string(enctype) krb5_enctype enctype;
  8616. #endif /* HEIMDAL */
  8617. #endif
  8618. {
  8619.     static char buf[12];
  8620.  
  8621.     switch (enctype) {
  8622.     case ENCTYPE_NULL:
  8623.         return "NULL";
  8624.     case ENCTYPE_DES_CBC_CRC:
  8625.         return "DES-CBC-CRC";
  8626.     case ENCTYPE_DES_CBC_MD4:
  8627.         return "DES-CBC-MD4";
  8628.     case ENCTYPE_DES_CBC_MD5:
  8629.         return "DES-CBC-MD5";
  8630.     case ENCTYPE_DES_CBC_RAW:
  8631.         return "DES-CBC-RAW";
  8632.     case ENCTYPE_DES3_CBC_SHA:
  8633.         return "DES3-CBC-SHA";
  8634.     case ENCTYPE_DES3_CBC_RAW:
  8635.         return "DES3-CBC-RAW";
  8636.     case ENCTYPE_DES_HMAC_SHA1:
  8637.         return "DES-HMAC-SHA1";
  8638.     case ENCTYPE_DES3_CBC_SHA1:
  8639.         return "DES3-CBC-SHA1";
  8640.     case ENCTYPE_UNKNOWN:
  8641.         return "UNKNOWN";
  8642.     case ENCTYPE_LOCAL_DES3_HMAC_SHA1:
  8643.         return "LOCAL-DES3-HMAC-SHA1";
  8644.     default:
  8645.         ckmakmsg(buf, sizeof(buf),"etype ", ckitoa(enctype),NULL,NULL);
  8646.         return buf;
  8647.         break;
  8648.     }
  8649. }
  8650.  
  8651. static char *
  8652. #ifdef CK_ANSIC
  8653. flags_string(register krb5_creds *cred)
  8654. #else
  8655. flags_string(cred) register krb5_creds *cred;
  8656. #endif
  8657. {
  8658.     static char buf[32];
  8659.     int i = 0;
  8660.  
  8661.     if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
  8662.         buf[i++] = 'F';
  8663.     if (cred->ticket_flags & TKT_FLG_FORWARDED)
  8664.         buf[i++] = 'f';
  8665.     if (cred->ticket_flags & TKT_FLG_PROXIABLE)
  8666.         buf[i++] = 'P';
  8667.     if (cred->ticket_flags & TKT_FLG_PROXY)
  8668.         buf[i++] = 'p';
  8669.     if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
  8670.         buf[i++] = 'D';
  8671.     if (cred->ticket_flags & TKT_FLG_POSTDATED)
  8672.         buf[i++] = 'd';
  8673.     if (cred->ticket_flags & TKT_FLG_INVALID)
  8674.         buf[i++] = 'i';
  8675.     if (cred->ticket_flags & TKT_FLG_RENEWABLE)
  8676.         buf[i++] = 'R';
  8677.     if (cred->ticket_flags & TKT_FLG_INITIAL)
  8678.         buf[i++] = 'I';
  8679.     if (cred->ticket_flags & TKT_FLG_HW_AUTH)
  8680.         buf[i++] = 'H';
  8681.     if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
  8682.         buf[i++] = 'A';
  8683.     buf[i] = '\0';
  8684.     return(buf);
  8685. }
  8686.  
  8687. static char   *
  8688. #ifdef CK_ANSIC
  8689. short_date(long   *dp)
  8690. #else
  8691. short_date(dp) long   *dp;
  8692. #endif
  8693. {
  8694.     register char *cp;
  8695. #ifndef ctime
  8696.     extern char *ctime();
  8697. #endif /* ctime */
  8698.     cp = ctime(dp) + 4;
  8699.     cp[15] = '\0';
  8700.     return (cp);
  8701. }
  8702.  
  8703.  
  8704. static VOID
  8705. #ifdef CK_ANSIC
  8706. printtime(time_t tv)
  8707. #else
  8708. printtime(tv) time_t tv;
  8709. #endif
  8710. {
  8711.     char timestring[BUFSIZ];
  8712.     char format[12];
  8713.     char fill;
  8714.  
  8715.     fill = ' ';
  8716.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  8717.     if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv,
  8718.                                      timestring,
  8719.                                      timestamp_width+1,
  8720.                                      &fill)) {
  8721.         printf(format,timestring);
  8722.     }
  8723.     else {
  8724.         printf(format,short_date(&tv));
  8725.     }
  8726.  
  8727. }
  8728.  
  8729. static void
  8730. #ifdef CK_ANSIC
  8731. one_addr(krb5_address *a)
  8732. #else
  8733. one_addr(a) krb5_address *a;
  8734. #endif
  8735. {
  8736.     struct hostent *h;
  8737.     extern tcp_rdns;
  8738.  
  8739.     if ((a->addrtype == ADDRTYPE_INET) &&
  8740.         (a->length == 4)) {
  8741.         if (tcp_rdns != SET_OFF) {
  8742.             h = gethostbyaddr(a->contents, 4, AF_INET);
  8743.             if (h) {
  8744. #ifdef HADDRLIST
  8745.                 h = ck_copyhostent(h);
  8746. #endif /* HADDRLIST */
  8747.                 printf("%s (%d.%d.%d.%d)", h->h_name,
  8748.                         a->contents[0], a->contents[1],
  8749.                         a->contents[2], a->contents[3]);
  8750.             }
  8751.         }
  8752.         if (tcp_rdns == SET_OFF || !h) {
  8753.             printf("%d.%d.%d.%d", a->contents[0], a->contents[1],
  8754.                    a->contents[2], a->contents[3]);
  8755.         }
  8756.     } else {
  8757.         printf("unknown addr type %d", a->addrtype);
  8758.     }
  8759. }
  8760.  
  8761. static VOID
  8762. #ifdef CK_ANSIC
  8763. show_credential(krb5_context kcontext, register krb5_creds * cred)
  8764. #else
  8765. show_credential(kcontext, cred)
  8766.     krb5_context          kcontext;
  8767.     register krb5_creds * cred;
  8768. #endif
  8769. {
  8770.     krb5_error_code retval=0;
  8771.     krb5_ticket *tkt=NULL;
  8772.     char *name=NULL, *sname=NULL, *flags=NULL;
  8773.     int extra_field = 0;
  8774.  
  8775.     retval = krb5_unparse_name(kcontext, cred->client, &name);
  8776.     if (retval) {
  8777.         debug(F101,"ck_krb5_list_creds while unparsing client name","",retval);
  8778.         krb5_errno = retval;
  8779.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8780.         return;
  8781.     }
  8782.     retval = krb5_unparse_name(kcontext, cred->server, &sname);
  8783.     if (retval) {
  8784.         debug(F101,"ck_krb5_list_creds while unparsing server name","",retval);
  8785.         free(name);
  8786.         krb5_errno = retval;
  8787.         makestr(&krb5_errmsg,error_message(krb5_errno));
  8788.         return;
  8789.     }
  8790.     if (!cred->times.starttime)
  8791.         cred->times.starttime = cred->times.authtime;
  8792.  
  8793.     printtime(cred->times.starttime);
  8794.     printf("  ");
  8795.  
  8796.     if ( time(0) < cred->times.endtime )
  8797.         printtime(cred->times.endtime);
  8798.     else
  8799.         printf("** expired ** ");
  8800.  
  8801.     printf("  %s\r\n", sname);
  8802.  
  8803.     if (strcmp(name, defname)) {
  8804.         printf("   for client %s", name);
  8805.         extra_field++;
  8806.     }
  8807.  
  8808.     if (cred->times.renew_till) {
  8809.         if (!extra_field)
  8810.             printf("   ");
  8811.         else
  8812.             printf(", ");
  8813.         printf("renew until ");
  8814.         printtime(cred->times.renew_till);
  8815.         extra_field += 2;
  8816.     }
  8817.  
  8818.     if (extra_field > 3) {
  8819.         printf("\r\n");
  8820.         extra_field = 0;
  8821.     }
  8822.  
  8823.     if (show_flags) {
  8824.         flags = flags_string(cred);
  8825.         if (flags && *flags) {
  8826.             if (!extra_field)
  8827.                 printf("   ");
  8828.             else
  8829.                 printf(", ");
  8830.             printf("Flags: %s", flags);
  8831.             extra_field++;
  8832.         }
  8833.     }
  8834.  
  8835.     if (extra_field > 2) {
  8836.         printf("\r\n");
  8837.         extra_field = 0;
  8838.     }
  8839.  
  8840.     if (show_etype) {
  8841.         retval = decode_krb5_ticket(&cred->ticket, &tkt);
  8842.         if (!extra_field)
  8843.             printf("   ");
  8844.         else
  8845.             printf(", ");
  8846. #ifdef HEIMDAL
  8847.         printf("Etype (skey, tkt): %s, %s ",
  8848.                etype_string(cred->session.keytype),
  8849.                etype_string(tkt->enc_part.keytype));
  8850. #else /* HEIMDAL */
  8851.         printf("Etype (skey, tkt): %s, %s ",
  8852.                etype_string(cred->keyblock.enctype),
  8853.                etype_string(tkt->enc_part.enctype));
  8854. #endif /* HEIMDAL */
  8855.         krb5_free_ticket(kcontext, tkt);
  8856.         extra_field++;
  8857.     }
  8858.  
  8859.     /* if any additional info was printed, extra_field is non-zero */
  8860.     if (extra_field)
  8861.         printf("\r\n");
  8862.  
  8863.     if ( show_addr ) {
  8864.         if (!cred->addresses || !cred->addresses[0]) {
  8865.             printf("\tAddresses: (none)\r\n");
  8866.         } else {
  8867.             int i;
  8868.             for (i=0; cred->addresses[i]; i++) {
  8869.                 if (i)
  8870.                     printf("              ");
  8871.                 else
  8872.                     printf("   Addresses: ");
  8873.                 one_addr(cred->addresses[i]);
  8874.                 printf("\r\n");
  8875.             }
  8876.         }
  8877.     }
  8878.  
  8879.     krb5_free_unparsed_name(kcontext,name);
  8880.     krb5_free_unparsed_name(kcontext,sname);
  8881.  
  8882.     krb5_errno = 0;
  8883.     makestr(&krb5_errmsg,"OK");
  8884. }
  8885.  
  8886. static VOID
  8887. #ifdef CK_ANSIC
  8888. fillit(int num, int c)
  8889. #else
  8890. fillit(num, c) int num; int c;
  8891. #endif
  8892. {
  8893.     int i;
  8894.  
  8895.     for (i=0; i<num; i++)
  8896.         printf("%c",c);
  8897. }
  8898. #endif /* KLIST */
  8899. #endif /* KRB5 */
  8900.  
  8901. #ifdef KRB4
  8902. #define KDEBUG 1
  8903. int k4debug = 0;                /* Kerberos 4 runtime debugging */
  8904.  
  8905. #ifdef KINIT
  8906. #define KRB_DEFAULT_LIFE 120 /* 10 hours in 5 minute intervals */
  8907.  
  8908. #ifdef SNK4
  8909. /* SNK4 is a hardware authentication system used to pre-authenticate    */
  8910. /* a ticket getting ticket.  We do not support this code at the present */
  8911. /* time in Kermit.                                                      */
  8912. void
  8913. get_input(s, size, stream)
  8914. char *s;
  8915. int size;
  8916. FILE *stream;
  8917. {
  8918.     char *p;
  8919.  
  8920.     if (fgets(s, size, stream) == NULL)
  8921.         exit(1);
  8922.     if ( (p = strchr(s, '\n')) != NULL)
  8923.         *p = '\0';
  8924. }
  8925. #endif /* SNK4 */
  8926.  
  8927. #ifdef COMMENT
  8928. static char
  8929. #ifdef CK_ANSIC
  8930. hex_scan_nybble(char c)
  8931. #else
  8932. hex_scan_nybble(c) char c;
  8933. #endif
  8934. {
  8935.     if (c >= '0' && c <= '9')
  8936.         return c - '0';
  8937.     if (c >= 'A' && c <= 'F')
  8938.         return c - 'A' + 10;
  8939.     if (c >= 'a' && c <= 'f')
  8940.         return c - 'a' + 10;
  8941.     return -1;
  8942. }
  8943.  
  8944. /* returns: NULL for ok, pointer to error string for bad input */
  8945. static char*
  8946. #ifdef CK_ANSIC
  8947. hex_scan_four_bytes(char *out, char *in)
  8948. #else
  8949. hex_scan_four_bytes(out, in) char *out; char *in;
  8950. #endif
  8951. {
  8952.     int i;
  8953.     int c;
  8954.     char c1;
  8955.     for (i=0; i<8; i++) {
  8956.         if(!in[i])
  8957.             return "not enough input";
  8958.         c = hex_scan_nybble(in[i]);
  8959.         if(c<0)
  8960.             return "invalid digit";
  8961.         c1 = c;
  8962.         i++;
  8963.         if(!in[i])
  8964.             return "not enough input";
  8965.         c = hex_scan_nybble(in[i]);
  8966.         if(c<0)
  8967.             return "invalid digit";
  8968.         *out++ = (c1 << 4) + c;
  8969.     }
  8970.     switch(in[i]) {
  8971.     case 0:
  8972.     case '\r':
  8973.     case '\n':
  8974.         return NULL;
  8975.     default:
  8976.         return "extra characters at end of input";
  8977.     }
  8978. }
  8979. #endif /* COMMENT */
  8980.  
  8981. /* ck_krb4_initTGT() returns 0 on success */
  8982. int
  8983. #ifdef CK_ANSIC
  8984. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  8985. #else
  8986. ck_krb4_initTGT(op,init)
  8987.     struct krb_op_data * op, struct krb4_init_data * init
  8988. #endif
  8989. {
  8990.     char    aname[ANAME_SZ+1];
  8991.     char    inst[INST_SZ+1];
  8992.     char    realm[REALM_SZ+1];
  8993.     char    *password=NULL;
  8994.     char    passwd[80]="";
  8995.     char    *username = NULL;
  8996.     char    *usernameptr=NULL;
  8997.     int     iflag,      /* Instance */
  8998.             rflag,      /* Realm */
  8999.             vflag,      /* Verbose */
  9000.             lflag,      /* Lifetime */
  9001.             pflag,      /* Preauth */
  9002.             lifetime=KRB_DEFAULT_LIFE,   /* Life Time */
  9003.             k_errno;
  9004.     register char *cp;
  9005.     register i;
  9006.  
  9007.     if ( !ck_krb4_is_installed() )
  9008.         return(-1);
  9009.  
  9010.     *inst = *realm = '\0';
  9011.     iflag = rflag = vflag = lflag = pflag = 0;
  9012.  
  9013.     vflag = init->verbose;
  9014.     pflag = init->preauth;
  9015.  
  9016.     if ( init->lifetime ) {
  9017.         lifetime = init->lifetime<5?1:init->lifetime/5;
  9018.         if ( lifetime > 255 ) lifetime = 255;
  9019.     }
  9020.     else
  9021.         lifetime = KRB_DEFAULT_LIFE;
  9022.  
  9023.     username = init->principal;
  9024.  
  9025.     if (username && username[0] &&
  9026.         (k_errno = kname_parse(aname, inst, realm, username))
  9027.         != AUTH_SUCCESS) {
  9028.         krb4_errno = k_errno;
  9029.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9030.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9031.         iflag = rflag = 1;
  9032.         username = NULL;
  9033.     }
  9034.  
  9035.     if ( init->realm ) {
  9036.         ckstrncpy(realm,init->realm,REALM_SZ);
  9037.     }
  9038.  
  9039.     if ( init->instance ) {
  9040.         ckstrncpy(inst,init->instance, INST_SZ);
  9041.     }
  9042.  
  9043. #ifdef COMMENT
  9044.     if ( vflag )
  9045.         printf("Kerberos IV initialization\r\n");
  9046. #endif /* COMMENT */
  9047.  
  9048.     if (!username || !username[0]) {
  9049.         debug(F100,"ck_krb4_initTGT no username specified","",0);
  9050.         printf("?Invalid principal specified.\r\n");
  9051.         krb4_errno = -1;
  9052.         makestr(&krb4_errmsg,"No principal specified");
  9053.         return(-1);
  9054.     }
  9055.     if (!*realm) {
  9056.         ckstrncpy(realm,ck_krb4_getrealm(),REALM_SZ);
  9057.     }
  9058.  
  9059.     if ( init->password )
  9060.         password = init->password;
  9061.     else {
  9062.         char prmpt[80];
  9063.         int ok;
  9064.  
  9065.         ckmakxmsg(prmpt,sizeof(prmpt),
  9066.                   "Kerberos 4 Password for ",username,"@",realm,": ",
  9067.                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9068.         ok = uq_txt(NULL,prmpt,2,NULL,passwd,80,NULL);
  9069.         if ( ok )
  9070.             password = passwd;
  9071.     }
  9072.  
  9073.     if (pflag) {
  9074.         k_errno = krb_get_pw_in_tkt_preauth( aname, inst, realm,
  9075.                                              "krbtgt", realm,
  9076.                                              lifetime,
  9077.                                              password);
  9078.         if (k_errno == -1) {    /* preauth method not available */
  9079.             k_errno = krb_get_pw_in_tkt(aname,
  9080.                                          inst, realm,
  9081.                                          "krbtgt", realm,
  9082.                                          lifetime,
  9083.                                          password);
  9084.         }
  9085.     } else {
  9086.         k_errno = krb_get_pw_in_tkt(aname,
  9087.                                      inst, realm,
  9088.                                      "krbtgt", realm,
  9089.                                      lifetime,
  9090.                                      password);
  9091.     }
  9092.  
  9093.     memset(passwd,0,sizeof(passwd));
  9094.     if (k_errno) {
  9095.         printf("%s for principal %s%s%s@%s\r\n",
  9096.                 krb_get_err_text_entry(k_errno), aname,
  9097.                 inst[0]?".":"", inst, realm);
  9098.         krb4_errno = k_errno;
  9099.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9100.         return(-1);
  9101.     } else if (vflag) {
  9102.         printf("Result from realm %s: ", realm);
  9103.         printf("%s\r\n", krb_get_err_text_entry(k_errno));
  9104.     }
  9105.     krb4_errno = k_errno;
  9106.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9107.     return(0);
  9108. }
  9109. #endif /* KINIT */
  9110. #ifdef KDESTROY
  9111. int
  9112. #ifdef CK_ANSIC
  9113. ck_krb4_destroy(struct krb_op_data * op)
  9114. #else
  9115. ck_krb4_destroy(op) struct krb_op_data * op;
  9116. #endif
  9117. {
  9118.     int k_errno=0;
  9119.  
  9120.     if ( !ck_krb4_is_installed() )
  9121.         return(-1);
  9122.  
  9123.     k_errno = dest_tkt();
  9124.  
  9125.     krb4_errno = k_errno;
  9126.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9127.  
  9128.     if (k_errno == 0)
  9129.         printf("Tickets destroyed.\r\n");
  9130.     else if (k_errno == RET_TKFIL)
  9131.         printf("No tickets to destroy.\r\n");
  9132.     else {
  9133.         printf("Tickets MAY NOT be destroyed.\r\n");
  9134.         return(-1);
  9135.     }
  9136.     return(0);
  9137. }
  9138. #endif /* KDESTROY */
  9139. #ifdef KLIST
  9140. _PROTOTYP(static int display_tktfile,(char *, int, int, int));
  9141.  
  9142. int
  9143. #ifdef CK_ANSIC
  9144. ck_krb4_list_creds(struct krb_op_data * op)
  9145. #else
  9146. ck_krb4_list_creds(op) struct krb_op_data * op;
  9147. #endif
  9148. {
  9149.     int     long_form = 1;
  9150.     int     tgt_test = 0;
  9151.     int     do_srvtab = 0;
  9152.     int     show_kvnos = 0;
  9153.     char   *tkt_file = NULL;
  9154.  
  9155.     if ( !ck_krb4_is_installed() )
  9156.         return(-1);
  9157.  
  9158.     if ( op->cache )
  9159.         tkt_file = op->cache;
  9160.  
  9161.     if ( k4debug ) {
  9162.         show_kvnos = 1;
  9163.     }
  9164.  
  9165.     if (do_srvtab)
  9166.         return(display_srvtab(tkt_file));
  9167.     else
  9168.         return(display_tktfile(tkt_file, tgt_test, long_form, show_kvnos));
  9169. }
  9170.  
  9171. #ifndef KRB5
  9172. static int timestamp_width=0;
  9173.  
  9174. static char   *
  9175. #ifdef CK_ANSIC
  9176. short_date(long   *dp)
  9177. #else
  9178. short_date(dp) long   *dp;
  9179. #endif
  9180. {
  9181.     register char *cp;
  9182.     extern char *ctime();
  9183.     cp = ctime(dp) + 4;
  9184.     cp[15] = '\0';
  9185.     return (cp);
  9186. }
  9187.  
  9188.  
  9189. static VOID
  9190. #ifdef CK_ANSIC
  9191. printtime(time_t tv)
  9192. #else
  9193. printtime(tv) time_t tv;
  9194. #endif
  9195. {
  9196.     char timestring[BUFSIZ];
  9197.     char format[12];
  9198.     char fill;
  9199.  
  9200.     fill = ' ';
  9201.     sprintf(format,"%%-%ds",timestamp_width);   /* safe */
  9202.     printf(format,short_date(&tv));
  9203. }
  9204. #endif /* KRB5 */
  9205.  
  9206. static int
  9207. #ifdef CK_ANSIC
  9208. display_tktfile(char *file, int tgt_test, int long_form, int show_kvnos)
  9209. #else
  9210. display_tktfile(file,tgt_test,long_form,show_kvnos)
  9211.     char *file; int tgt_test; int long_form; int show_kvnos;
  9212. #endif
  9213. {
  9214.     char    pname[ANAME_SZ];
  9215.     char    pinst[INST_SZ];
  9216.     char    prealm[REALM_SZ];
  9217.     char    buf1[20], buf2[20];
  9218.     int     k_errno;
  9219. #ifdef OS2
  9220.     LEASH_CREDENTIALS creds;
  9221. #else /* OS2 */
  9222.     CREDENTIALS creds;
  9223. #endif /* OS2 */
  9224.     int     header = 1;
  9225.  
  9226.     file = tkt_string();
  9227.  
  9228.     if (long_form) {
  9229.         printf("Ticket cache:      %s\r\n", file);
  9230.     }
  9231.  
  9232.     /*
  9233.      * Since krb_get_tf_realm will return a ticket_file error,
  9234.      * we will call tf_init and tf_close first to filter out
  9235.      * things like no ticket file.  Otherwise, the error that
  9236.      * the user would see would be
  9237.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9238.      * instead of
  9239.      * klist: No ticket file (tf_util)
  9240.      */
  9241.  
  9242.     /* Open ticket file */
  9243.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9244.         if (!tgt_test)
  9245.             printf("%s\r\n", 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.  
  9252.     /* Close ticket file */
  9253.     (void) tf_close();
  9254.  
  9255.     /*
  9256.      * We must find the realm of the ticket file here before calling
  9257.      * tf_init because since the realm of the ticket file is not
  9258.      * really stored in the principal section of the file, the
  9259.      * routine we use must itself call tf_init and tf_close.
  9260.      */
  9261.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9262.         if (!tgt_test)
  9263.             printf("can't find realm of ticket file: %s\r\n",
  9264.                     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.     /* Open ticket file */
  9271.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9272.         if (!tgt_test)
  9273.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9274.         krb4_errno = k_errno;
  9275.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9276.         return(-1);
  9277.     }
  9278.     /* Get principal name and instance */
  9279.     if ((k_errno = tf_get_pname(pname)) ||
  9280.          (k_errno = tf_get_pinst(pinst))) {
  9281.         (void) tf_close();
  9282.         if (!tgt_test)
  9283.             printf("%s\r\n", krb_get_err_text_entry (k_errno));
  9284.         krb4_errno = k_errno;
  9285.         makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9286.         return(-1);
  9287.     }
  9288.  
  9289.     /*
  9290.      * You may think that this is the obvious place to get the
  9291.      * realm of the ticket file, but it can't be done here as the
  9292.      * routine to do this must open the ticket file.  This is why
  9293.      * it was done before tf_init.
  9294.      */
  9295.  
  9296.     if (!tgt_test && long_form)
  9297.         printf("Default principal: %s%s%s%s%s\r\n\r\n", pname,
  9298.                (pinst[0] ? "." : ""), pinst,
  9299.                (prealm[0] ? "@" : ""), prealm);
  9300.  
  9301.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9302.         if (!tgt_test && long_form && header) {
  9303.             printf("%-17s  %-17s  %s\r\n",
  9304.                    "Valid starting", "Expires", "Service principal");
  9305.             header = 0;
  9306.         }
  9307.         if (tgt_test) {
  9308.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9309.             if (!strcmp(creds.service, "krbtgt") &&
  9310.                 !strcmp(creds.instance, prealm)) {
  9311.                 krb4_errno = k_errno;
  9312.                 makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9313.  
  9314.                 (void) tf_close();
  9315.                 if (time(0) < creds.issue_date) {
  9316.                     return(0);          /* tgt hasn't expired */
  9317.                 } else {
  9318.                     return(-1);         /* has expired */
  9319.                 }
  9320.             }
  9321.             continue;                   /* not a tgt */
  9322.         }
  9323.         if (long_form) {
  9324.             timestamp_width = 17;       /* for k5 display function */
  9325.                                         /* if available            */
  9326.             printtime(creds.issue_date);
  9327.             printf("  ");
  9328.             creds.issue_date += ((unsigned char) creds.lifetime) * 5 * 60;
  9329.             if ( time(0) < creds.issue_date )
  9330.                 printtime(creds.issue_date);
  9331.             else
  9332.                 printf("*** expired ***  ");
  9333.             printf("  ");
  9334.         }
  9335.         if (show_kvnos)
  9336.           printf("%s%s%s%s%s (%d)\r\n",
  9337.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9338.                  (creds.realm[0] ? "@" : ""), creds.realm, creds.kvno);
  9339.         else
  9340.           printf("%s%s%s%s%s\r\n",
  9341.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9342.                  (creds.realm[0] ? "@" : ""), creds.realm);
  9343.  
  9344. #ifdef OS2
  9345.         if ( creds.address[0] )
  9346.             printf("   Address: %s\r\n",creds.address);
  9347. #endif /* OS2 */
  9348.     }
  9349.  
  9350.     (void) tf_close();
  9351.  
  9352.     if (tgt_test) {
  9353.         return(-1);
  9354.     }/* no tgt found */
  9355.     if (header && long_form && k_errno == EOF) {
  9356.         printf("No tickets in file.\r\n");
  9357.     }
  9358.     krb4_errno = k_errno;
  9359.     makestr(&krb4_errmsg,krb_get_err_text_entry(k_errno));
  9360.     return(0);
  9361. }
  9362.  
  9363. #ifdef COMMENT
  9364. /* Just so we remember what the command line interface looked like */
  9365. usage()
  9366. {
  9367.     printf(
  9368.         "Usage: [ -s | -t ] [ -file filename ] [ -srvtab ] [ -version ]\r\n");
  9369.     return(-1);
  9370. }
  9371. #endif /* COMMENT */
  9372.  
  9373. /* adapted from getst() in librkb */
  9374. /*
  9375.  * ok_getst() takes a file descriptor, a string and a count.  It reads
  9376.  * from the file until either it has read "count" characters, or until
  9377.  * it reads a null byte.  When finished, what has been read exists in
  9378.  * the given string "s".  If "count" characters were actually read, the
  9379.  * last is changed to a null, so the returned string is always null-
  9380.  * terminated.  ok_getst() returns the number of characters read, including
  9381.  * the null terminator.
  9382.  *
  9383.  * If there is a read error, it returns -1 (like the read(2) system call)
  9384.  */
  9385.  
  9386. static int
  9387. #ifdef CK_ANSIC
  9388. ok_getst(int fd, register char *s, int n)
  9389. #else
  9390. ok_getst(fd, s, n) int fd; register char *s; int n;
  9391. #endif
  9392. {
  9393.     register int count = n;
  9394.     int err;
  9395.     while ((err = read(fd, s, 1)) > 0 && --count)
  9396.         if (*s++ == '\0')
  9397.             return (n - count);
  9398.     if (err < 0)
  9399.         return(-1);
  9400.     *s = '\0';
  9401.     return (n - count);
  9402. }
  9403.  
  9404. int
  9405. #ifdef CK_ANSIC
  9406. display_srvtab(char *file)
  9407. #else
  9408. display_srvtab(file) char *file;
  9409. #endif
  9410. {
  9411.     int stab;
  9412.     char serv[SNAME_SZ];
  9413.     char inst[INST_SZ];
  9414.     char rlm[REALM_SZ];
  9415.     unsigned char key[8];
  9416.     unsigned char vno;
  9417.     int count;
  9418.  
  9419.     printf("Server key file:   %s\r\n", file);
  9420. #ifdef NT
  9421. #ifndef O_RDONLY
  9422. #define O_RDONLY _O_RDONLY
  9423. #endif /* O_RDONLY */
  9424. #endif /* NT */
  9425.  
  9426.     if ((stab = open(file, O_RDONLY, 0400)) < 0) {
  9427.         perror(file);
  9428.         return(-1);
  9429.     }
  9430.     printf("%-15s %-15s %-10s %s\r\n","Service","Instance","Realm",
  9431.            "Key Version");
  9432.     printf("------------------------------------------------------\r\n");
  9433.  
  9434.     /* argh. getst doesn't return error codes, it silently fails */
  9435.     while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
  9436.            && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
  9437.            && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
  9438.         if (((count = read(stab,(char *) &vno,1)) != 1) ||
  9439.              ((count = read(stab,(char *) key,8)) != 8)) {
  9440.             if (count < 0)
  9441.                 perror("reading from key file");
  9442.             else
  9443.                 printf("key file truncated\r\n");
  9444.             return(-1);
  9445.         }
  9446.         printf("%-15s %-15s %-15s %d\r\n",serv,inst,rlm,vno);
  9447.     }
  9448.     if (count < 0)
  9449.         perror(file);
  9450.     (void) close(stab);
  9451.     return(0);
  9452. }
  9453. #endif /* KLIST */
  9454. #else /* KRB4 */
  9455. int
  9456. ck_krb4_autoget_TGT(char * dummy)
  9457. {
  9458.     return(-1);
  9459. }
  9460. #ifdef CK_KERBEROS
  9461. int
  9462. #ifdef CK_ANSIC
  9463. ck_krb4_initTGT(struct krb_op_data * op, struct krb4_init_data * init)
  9464. #else
  9465. ck_krb4_initTGT(op,init)
  9466.     struct krb_op_data * op, struct krb4_init_data * init
  9467. #endif
  9468. {
  9469.     return(-1);
  9470. }
  9471.  
  9472. #ifdef CK_ANSIC
  9473. ck_krb4_destroy(struct krb_op_data * op)
  9474. #else
  9475. ck_krb4_destroy(op) struct krb_op_data * op;
  9476. #endif
  9477. {
  9478.     return(-1);
  9479. }
  9480. int
  9481. #ifdef CK_ANSIC
  9482. ck_krb4_list_creds(struct krb_op_data * op)
  9483. #else
  9484. ck_krb4_list_creds(op) struct krb_op_data * op;
  9485. #endif
  9486. {
  9487.     return(-1);
  9488. }
  9489. #else /* CK_KERBEROS */
  9490. int ck_krb4_initTGT(void * a, void *b)
  9491. {
  9492.     return(-1);
  9493. }
  9494. int ck_krb4_destroy(void *a)
  9495. {
  9496.     return(-1);
  9497. }
  9498. int ck_krb4_list_creds(void *a)
  9499. {
  9500.     return(-1);
  9501. }
  9502. #endif /* CK_KERBEROS */
  9503. #endif /* KRB4 */
  9504.  
  9505. /* The following functions are used to implement the Kermit Script Language */
  9506. /* functions                                                                */
  9507.  
  9508. struct tkt_list_item {
  9509.     char * name;
  9510.     struct tkt_list_item * next;
  9511. };
  9512.  
  9513. static struct tkt_list_item * k4_tkt_list = NULL;
  9514.  
  9515. int
  9516. #ifdef CK_ANSIC
  9517. ck_krb4_get_tkts(VOID)
  9518. #else
  9519. ck_krb4_get_tkts()
  9520. #endif
  9521. {
  9522. #ifdef KRB4
  9523.     char   *file=NULL;
  9524.     char    pname[ANAME_SZ];
  9525.     char    pinst[INST_SZ];
  9526.     char    prealm[REALM_SZ];
  9527.     char    buf1[20], buf2[20];
  9528.     int     k_errno;
  9529. #ifdef OS2
  9530.     LEASH_CREDENTIALS creds;
  9531. #else /* OS2 */
  9532.     CREDENTIALS creds;
  9533. #endif /* OS2 */
  9534.     int     tkt_count=0;
  9535.     struct  tkt_list_item ** list = &k4_tkt_list;
  9536.  
  9537.     while ( k4_tkt_list ) {
  9538.         struct tkt_list_item * next;
  9539.         next = k4_tkt_list->next;
  9540.         free(k4_tkt_list->name);
  9541.         free(k4_tkt_list);
  9542.         k4_tkt_list = next;
  9543.     }
  9544.  
  9545.     if ( !ck_krb4_is_installed() )
  9546.         return(-1);
  9547.  
  9548.     file = tkt_string();
  9549.  
  9550.     /*
  9551.      * Since krb_get_tf_realm will return a ticket_file error,
  9552.      * we will call tf_init and tf_close first to filter out
  9553.      * things like no ticket file.  Otherwise, the error that
  9554.      * the user would see would be
  9555.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9556.      * instead of
  9557.      * klist: No ticket file (tf_util)
  9558.      */
  9559.  
  9560.     /* Open ticket file */
  9561.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9562.         return(-1);
  9563.     }
  9564.  
  9565.     /* Close ticket file */
  9566.     (void) tf_close();
  9567.  
  9568.     /*
  9569.      * We must find the realm of the ticket file here before calling
  9570.      * tf_init because since the realm of the ticket file is not
  9571.      * really stored in the principal section of the file, the
  9572.      * routine we use must itself call tf_init and tf_close.
  9573.      */
  9574.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9575.         return(-1);
  9576.     }
  9577.  
  9578.     /* Open ticket file */
  9579.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9580.         return(-1);
  9581.     }
  9582.     /* Get principal name and instance */
  9583.     if ((k_errno = tf_get_pname(pname)) ||
  9584.          (k_errno = tf_get_pinst(pinst))) {
  9585.         return(-1);
  9586.     }
  9587.  
  9588.     /*
  9589.      * You may think that this is the obvious place to get the
  9590.      * realm of the ticket file, but it can't be done here as the
  9591.      * routine to do this must open the ticket file.  This is why
  9592.      * it was done before tf_init.
  9593.      */
  9594.  
  9595.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9596.         char tkt_buf[256];
  9597.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9598.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9599.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9600.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9601.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  9602.         (*list)->name = strdup(tkt_buf);
  9603.         (*list)->next = NULL;
  9604.         list = &((*list)->next);
  9605.         tkt_count++;
  9606.     }
  9607.  
  9608.     tf_close();
  9609.     return(tkt_count);
  9610. #else /* KRB4 */
  9611.     return(0);
  9612. #endif /* KRB4 */
  9613. }
  9614.  
  9615. char *
  9616. #ifdef CK_ANSIC
  9617. ck_krb4_get_next_tkt(VOID)
  9618. #else
  9619. ck_krb4_get_next_tkt()
  9620. #endif
  9621. {
  9622. #ifdef KRB4
  9623.     static char * s=NULL;
  9624.     struct tkt_list_item * next=NULL;
  9625.  
  9626.     if ( s ) {
  9627.         free(s);
  9628.         s = NULL;
  9629.     }
  9630.  
  9631.     if ( k4_tkt_list == NULL )
  9632.         return(NULL);
  9633.  
  9634.     next = k4_tkt_list->next;
  9635.     s = k4_tkt_list->name;
  9636.     free(k4_tkt_list);
  9637.     k4_tkt_list = next;
  9638.     return(s);
  9639. #else /* KRB4 */
  9640.     return(NULL);
  9641. #endif /* KRB4 */
  9642. }
  9643.  
  9644. int
  9645. #ifdef CK_ANSIC
  9646. ck_krb4_tkt_isvalid(char * tktname)
  9647. #else
  9648. ck_krb4_tkt_isvalid(tktname) char * tktname;
  9649. #endif
  9650. {
  9651. #ifdef KRB4
  9652.     char   *file=NULL;
  9653.     char    pname[ANAME_SZ];
  9654.     char    pinst[INST_SZ];
  9655.     char    prealm[REALM_SZ];
  9656.     char    buf1[20], buf2[20];
  9657.     int     k_errno;
  9658.     time_t  issue_t, expire_t, now_t;
  9659. #ifdef OS2
  9660.     LEASH_CREDENTIALS creds;
  9661. #else /* OS2 */
  9662.     CREDENTIALS creds;
  9663. #endif /* OS2 */
  9664.  
  9665.     if ( !ck_krb4_is_installed() )
  9666.         return(-1);
  9667.  
  9668.     debug(F110,"ck_krb4_tkt_isvalid","tkt_string",0);
  9669.     file = tkt_string();
  9670.  
  9671.     /*
  9672.      * Since krb_get_tf_realm will return a ticket_file error,
  9673.      * we will call tf_init and tf_close first to filter out
  9674.      * things like no ticket file.  Otherwise, the error that
  9675.      * the user would see would be
  9676.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9677.      * instead of
  9678.      * klist: No ticket file (tf_util)
  9679.      */
  9680.  
  9681.     /* Open ticket file */
  9682.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9683.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9684.         return(-1);
  9685.     }
  9686.  
  9687.     /* Close ticket file */
  9688.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9689.     (void) tf_close();
  9690.  
  9691.     /*
  9692.      * We must find the realm of the ticket file here before calling
  9693.      * tf_init because since the realm of the ticket file is not
  9694.      * really stored in the principal section of the file, the
  9695.      * routine we use must itself call tf_init and tf_close.
  9696.      */
  9697.     debug(F110,"ck_krb4_tkt_isvalid","krb_get_tf_realm",0);
  9698.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9699.         return(-1);
  9700.     }
  9701.  
  9702.     /* Open ticket file */
  9703.     debug(F110,"ck_krb4_tkt_isvalid","tf_init",0);
  9704.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9705.         return(-1);
  9706.     }
  9707.     /* Get principal name and instance */
  9708.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_name/tf_get_pinst",0);
  9709.     if ((k_errno = tf_get_pname(pname)) ||
  9710.          (k_errno = tf_get_pinst(pinst))) {
  9711.  
  9712.         /* Close ticket file */
  9713.         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9714.         (void) tf_close();
  9715.  
  9716.         return(-1);
  9717.     }
  9718.  
  9719.     /*
  9720.      * You may think that this is the obvious place to get the
  9721.      * realm of the ticket file, but it can't be done here as the
  9722.      * routine to do this must open the ticket file.  This is why
  9723.      * it was done before tf_init.
  9724.      */
  9725.  
  9726.     debug(F110,"ck_krb4_tkt_isvalid","tf_get_cred",0);
  9727.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9728.         char tkt_buf[256];
  9729.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9730.                  creds.service, (creds.instance[0] ? "." : ""), creds.instance,
  9731.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9732.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9733.         if ( !strcmp(tktname,tkt_buf) ) {
  9734.  
  9735.             /* we found the ticket we are looking for */
  9736.             issue_t = creds.issue_date;
  9737.             expire_t = creds.issue_date
  9738.                 + ((unsigned char) creds.lifetime) * 5 * 60;
  9739.             now_t = time(0);
  9740.  
  9741.             /* We add a 5 minutes fudge factor to compensate for potential */
  9742.             /* clock skew errors between the KDC and K95's host OS         */
  9743.  
  9744.             if ( now_t >= (issue_t-300) && now_t < expire_t) {
  9745. #ifdef OS2
  9746. #ifdef CHECKADDRS
  9747.                 if ( krb4_checkaddrs ) {
  9748.                     extern char myipaddr[20];       /* From ckcnet.c */
  9749.                     if ( !myipaddr[0] ) {
  9750.                         int i;
  9751.                         char buf[60];
  9752.                         for ( i=0;i<64;i++ ) {
  9753.                             if ( getlocalipaddrs(buf,60,i) < 0 )
  9754.                                 break;
  9755.  
  9756.                             if ( !strcmp(buf,creds.address) ) {
  9757.                                 /* Close ticket file */
  9758.                                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9759.                                 (void) tf_close();
  9760.                                 return(1); /* They're the same */
  9761.                             }
  9762.                         }
  9763.  
  9764.                         /* Close ticket file */
  9765.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9766.                         (void) tf_close();
  9767.                         return(0);                  /* They're different */
  9768.                     } else if ( strcmp(myipaddr,creds.address) ) {
  9769.                         /* Close ticket file */
  9770.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9771.                         (void) tf_close();
  9772.                         return(0);                  /* They're different */
  9773.                     }
  9774.                     else {
  9775.                         /* Close ticket file */
  9776.                         debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9777.                         (void) tf_close();
  9778.                         return(1);                  /* They're the same */
  9779.                     }
  9780.                 } else {
  9781.                     /* Close ticket file */
  9782.                     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9783.                     (void) tf_close();
  9784.                     return(1);                  /* They're the same */
  9785.                 }
  9786. #else /* CHECKADDRS */
  9787.                 /* Close ticket file */
  9788.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9789.                 (void) tf_close();
  9790.                 return(1);      /* valid but no ip address check */
  9791. #endif /* CHECKADDRS */
  9792. #else /* OS2 */
  9793.                 /* Close ticket file */
  9794.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9795.                 (void) tf_close();
  9796.                 return(1);      /* Valid but no ip address check */
  9797. #endif /* OS2 */
  9798.             }
  9799.             else {
  9800.                 /* Close ticket file */
  9801.                 debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9802.                 (void) tf_close();
  9803.                 return(0);      /* expired or otherwise invalid */
  9804.             }
  9805.         }
  9806.     }
  9807.     /* Close ticket file */
  9808.     debug(F110,"ck_krb4_tkt_isvalid","tf_close",0);
  9809.     (void) tf_close();
  9810.     return(0);                  /* could not find the desired ticket */
  9811. #else /* KRB4 */
  9812.     return(-1);
  9813. #endif /* KRB4 */
  9814. }
  9815.  
  9816. int
  9817. #ifdef CK_ANSIC
  9818. ck_krb4_is_tgt_valid(VOID)
  9819. #else
  9820. ck_krb4_is_tgt_valid()
  9821. #endif
  9822. {
  9823. #ifdef KRB4
  9824.     char tgt[256];
  9825.     char * s;
  9826.     int rc = 0;
  9827.  
  9828.     s = krb4_d_realm ? krb4_d_realm : ck_krb4_getrealm();
  9829.     ckmakmsg(tgt,sizeof(tgt),"krbtgt.",s,"@",s);
  9830.     rc = ck_krb4_tkt_isvalid(tgt);
  9831.     debug(F111,"ck_krb4_is_tgt_valid",tgt,rc);
  9832.     return(rc > 0);
  9833. #else /* KRB4 */
  9834.     return(0);
  9835. #endif /* KRB4 */
  9836. }
  9837.  
  9838. int
  9839. #ifdef CK_ANSIC
  9840. ck_krb4_tkt_time(char * tktname)
  9841. #else
  9842. ck_krb4_tkt_time(tktname) char * tktname;
  9843. #endif
  9844. {
  9845. #ifdef KRB4
  9846.     char   *file=NULL;
  9847.     char    pname[ANAME_SZ];
  9848.     char    pinst[INST_SZ];
  9849.     char    prealm[REALM_SZ];
  9850.     char    buf1[20], buf2[20];
  9851.     int     k_errno;
  9852. #ifdef OS2
  9853.     LEASH_CREDENTIALS creds;
  9854. #else /* OS2 */
  9855.     CREDENTIALS creds;
  9856. #endif /* OS2 */
  9857.  
  9858.     if ( !ck_krb4_is_installed() )
  9859.         return(-1);
  9860.  
  9861.     file = tkt_string();
  9862.  
  9863.     /*
  9864.      * Since krb_get_tf_realm will return a ticket_file error,
  9865.      * we will call tf_init and tf_close first to filter out
  9866.      * things like no ticket file.  Otherwise, the error that
  9867.      * the user would see would be
  9868.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  9869.      * instead of
  9870.      * klist: No ticket file (tf_util)
  9871.      */
  9872.  
  9873.     /* Open ticket file */
  9874.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9875.         return(-1);
  9876.     }
  9877.  
  9878.     /* Close ticket file */
  9879.     (void) tf_close();
  9880.  
  9881.     /*
  9882.      * We must find the realm of the ticket file here before calling
  9883.      * tf_init because since the realm of the ticket file is not
  9884.      * really stored in the principal section of the file, the
  9885.      * routine we use must itself call tf_init and tf_close.
  9886.      */
  9887.     if ((k_errno = krb_get_tf_realm(file, prealm)) != AUTH_SUCCESS) {
  9888.         return(-1);
  9889.     }
  9890.  
  9891.     /* Open ticket file */
  9892.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  9893.         return(-1);
  9894.     }
  9895.     /* Get principal name and instance */
  9896.     if ((k_errno = tf_get_pname(pname)) ||
  9897.          (k_errno = tf_get_pinst(pinst))) {
  9898.         tf_close();
  9899.         return(-1);
  9900.     }
  9901.  
  9902.     /*
  9903.      * You may think that this is the obvious place to get the
  9904.      * realm of the ticket file, but it can't be done here as the
  9905.      * routine to do this must open the ticket file.  This is why
  9906.      * it was done before tf_init.
  9907.      */
  9908.  
  9909.     while ((k_errno = tf_get_cred((CREDENTIALS *)&creds)) == AUTH_SUCCESS) {
  9910.         char tkt_buf[256];
  9911.         ckmakxmsg(tkt_buf,sizeof(tkt_buf),
  9912.                  creds.service, (creds.instance[0] ? "." : ""),
  9913.                  creds.instance,
  9914.                  (creds.realm[0] ? "@" : ""), creds.realm,
  9915.                  NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  9916.         if ( !strcmp(tktname,tkt_buf) ) {
  9917.             /* we found the ticket we are looking for */
  9918.             int n = (creds.issue_date
  9919.                       + (((unsigned char) creds.lifetime) * 5 * 60))
  9920.                 - time(0);
  9921.             tf_close();
  9922.             return(n <= 0 ? 0 : n);
  9923.         }
  9924.     }
  9925.     tf_close();
  9926.     return(0);                  /* could not find the desired ticket */
  9927. #else /* KRB4 */
  9928.     return(-1);
  9929. #endif /* KRB4 */
  9930. }
  9931.  
  9932. char *
  9933. #ifdef CK_ANSIC
  9934. ck_krb4_getrealm(void)
  9935. #else
  9936. ck_krb4_getrealm()
  9937. #endif
  9938. {
  9939. #ifdef KRB4
  9940.     char   *file=NULL;
  9941.     int     k_errno;
  9942.     static char realm[256]="";
  9943.     realm[0]='\0';
  9944.  
  9945.     if ( !ck_krb4_is_installed() )
  9946.         return(realm);
  9947.  
  9948.     /* Try to get realm from ticket file */
  9949.     /* If failure get the local realm    */
  9950.  
  9951.     /*
  9952.     * Since krb_get_tf_realm will return a ticket_file error,
  9953.     * we will call tf_init and tf_close first to filter out
  9954.     * things like no ticket file.
  9955.     */
  9956.  
  9957.     /* Open ticket file */
  9958.     file = tkt_string();
  9959.     if (file == NULL || !file[0])
  9960.         return(realm);
  9961.  
  9962.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  9963.         /* Close ticket file */
  9964.         (void) tf_close();
  9965.  
  9966.         k_errno = krb_get_tf_realm(file, realm);
  9967.     }
  9968.     if (k_errno != KSUCCESS) {
  9969.         k_errno = krb_get_lrealm(realm, 1);
  9970.     }
  9971.     return(realm);
  9972. #else /* KRB4 */
  9973.     return("");
  9974. #endif /* KRB4 */
  9975. }
  9976.  
  9977. char *
  9978. #ifdef CK_ANSIC
  9979. ck_krb4_getprincipal(void)
  9980. #else
  9981. ck_krb4_getprincipal()
  9982. #endif
  9983. {
  9984. #ifdef KRB4
  9985.     char   *file=NULL;
  9986.     int     k_errno;
  9987.     static char principal[256]="";
  9988.     char        instance[256]="";
  9989.     char        realm[256]="";
  9990.     principal[0]='\0';
  9991.  
  9992.     if ( !ck_krb4_is_installed() )
  9993.         return(principal);
  9994.  
  9995.     /* Try to get realm from ticket file */
  9996.     /* If failure get the local realm    */
  9997.  
  9998.     /*
  9999.     * Since krb_get_tf_realm will return a ticket_file error,
  10000.     * we will call tf_init and tf_close first to filter out
  10001.     * things like no ticket file.
  10002.     */
  10003.  
  10004.     /* Open ticket file */
  10005.     file = tkt_string();
  10006.     if (file == NULL || !file[0])
  10007.         return(principal);
  10008.  
  10009.     if ((k_errno = tf_init(file, R_TKT_FIL)) == KSUCCESS) {
  10010.         /* Close ticket file */
  10011.         (void) tf_close();
  10012.  
  10013.         k_errno = krb_get_tf_fullname(file, principal, instance, realm);
  10014.     }
  10015.     return(principal);
  10016. #else /* KRB4 */
  10017.     return("");
  10018. #endif /* KRB4 */
  10019. }
  10020.  
  10021. static struct tkt_list_item * k5_tkt_list = NULL;
  10022.  
  10023. int
  10024. #ifdef CK_ANSIC
  10025. ck_krb5_get_tkts(char * cc_name)
  10026. #else
  10027. ck_krb5_get_tkts(cc_name) char * cc_name;
  10028. #endif
  10029. {
  10030. #ifdef KRB5
  10031. #ifndef HEIMDAL
  10032.     krb5_context kcontext;
  10033.     krb5_error_code retval;
  10034.     krb5_ccache cache = NULL;
  10035.     krb5_cc_cursor cur;
  10036.     krb5_creds creds;
  10037.     krb5_principal princ=NULL;
  10038.     krb5_flags flags=0;
  10039.     krb5_error_code code=0;
  10040.     int exit_status = 0;
  10041.  
  10042.     int     tkt_count=0;
  10043.     struct  tkt_list_item ** list = &k5_tkt_list;
  10044.  
  10045.     while ( k5_tkt_list ) {
  10046.         struct tkt_list_item * next;
  10047.         next = k5_tkt_list->next;
  10048.         free(k5_tkt_list->name);
  10049.         free(k5_tkt_list);
  10050.         k5_tkt_list = next;
  10051.     }
  10052.  
  10053.     if ( !ck_krb5_is_installed() )
  10054.         return(-1);
  10055.  
  10056.     retval = krb5_init_context(&kcontext);
  10057.     if (retval) {
  10058.         debug(F101,"ck_krb5_get_tkts while initializing krb5","",retval);
  10059.         return(-1);
  10060.     }
  10061.  
  10062.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10063.     if (code != 0) {
  10064.         debug(F111,"ck_krb5_get_tkts while getting ccache",
  10065.                error_message(code),code);
  10066.         tkt_count = -1;
  10067.         goto exit_k5_get_tkt;
  10068.     }
  10069.  
  10070.     flags = 0;                          /* turns off OPENCLOSE mode */
  10071.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10072.         if (code == ENOENT) {
  10073.             debug(F111,"ck_krb5_get_tkts (ticket cache)",
  10074.                    krb5_cc_get_name(kcontext, cache),code);
  10075.         } else {
  10076.             debug(F111,
  10077.                  "ck_krb5_get_tkts while setting cache flags (ticket cache)",
  10078.                   krb5_cc_get_name(kcontext, cache),code);
  10079.         }
  10080.         tkt_count = -1;
  10081.         goto exit_k5_get_tkt;
  10082.     }
  10083.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10084.         debug(F101,"ck_krb5_get_tkts while retrieving principal name",
  10085.                "",code);
  10086.         tkt_count = -1;
  10087.         goto exit_k5_get_tkt;
  10088.     }
  10089.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10090.         debug(F101,"ck_krb5_get_tkts while unparsing principal name",
  10091.                "",code);
  10092.         tkt_count = -1;
  10093.         goto exit_k5_get_tkt;
  10094.     }
  10095.  
  10096.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10097.         debug(F101,"ck_krb5_get_tkts while starting to retrieve tickets",
  10098.                "",code);
  10099.         tkt_count = -1;
  10100.         goto exit_k5_get_tkt;
  10101.     }
  10102.  
  10103.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10104.         char *sname=NULL;
  10105.  
  10106.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10107.         if (retval) {
  10108.             debug(F101,
  10109.                   "ck_krb5_get_tkts while unparsing server name","",retval);
  10110.             tkt_count = -1;
  10111.             goto exit_k5_get_tkt;
  10112.         }
  10113.  
  10114.         *list = (struct tkt_list_item *) malloc(sizeof(struct tkt_list_item));
  10115.         (*list)->name = sname;
  10116.         (*list)->next = NULL;
  10117.         list = &((*list)->next);
  10118.  
  10119.         krb5_free_cred_contents(kcontext, &creds);
  10120.         tkt_count++;
  10121.     }
  10122.  
  10123.     if (code == KRB5_CC_END) {
  10124.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10125.             debug(F101,"ck_krb5_get_tkts while finishing ticket retrieval",
  10126.                    "",code);
  10127.             tkt_count = -1;
  10128.             goto exit_k5_get_tkt;
  10129.         }
  10130.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10131.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10132.             debug(F101,"ck_krb5_get_tkts while closing ccache",
  10133.                    "",code);
  10134.             tkt_count = -1;
  10135.             goto exit_k5_get_tkt;
  10136.         }
  10137.     } else {
  10138.         debug(F101,"ck_krb5_get_tkts while retrieving a ticket","",code);
  10139.         tkt_count = -1;
  10140.         goto exit_k5_get_tkt;
  10141.     }
  10142.  
  10143.   exit_k5_get_tkt:
  10144.     krb5_free_principal(kcontext,princ);
  10145.     krb5_free_unparsed_name(kcontext,defname);
  10146.     krb5_cc_close(kcontext,cache);
  10147.     krb5_free_context(kcontext);
  10148.     return(tkt_count);
  10149. #else /* HEIMDAL */
  10150.     return(-1);
  10151. #endif /* HEIMDAL */
  10152. #else /* KRB5 */
  10153.     return(0);
  10154. #endif /* KRB5 */
  10155. }
  10156.  
  10157. char *
  10158. #ifdef CK_ANSIC
  10159. ck_krb5_get_next_tkt(VOID)
  10160. #else
  10161. ck_krb5_get_next_tkt()
  10162. #endif
  10163. {
  10164. #ifdef KRB5
  10165. #ifndef HEIMDAL
  10166.     static char * s=NULL;
  10167.     struct tkt_list_item * next=NULL;
  10168.  
  10169.     if ( s ) {
  10170.         free(s);
  10171.         s = NULL;
  10172.     }
  10173.  
  10174.     if ( k5_tkt_list == NULL )
  10175.         return(NULL);
  10176.  
  10177.     next = k5_tkt_list->next;
  10178.     s = k5_tkt_list->name;
  10179.     free(k5_tkt_list);
  10180.     k5_tkt_list = next;
  10181.     return(s);
  10182. #else /* HEIMDAL */
  10183.     return("Not implemented");
  10184. #endif /* HEIMDAL */
  10185. #else /* KRB5 */
  10186.     return(NULL);
  10187. #endif /* KRB5 */
  10188. }
  10189.  
  10190. char *
  10191. #ifdef CK_ANSIC
  10192. ck_krb5_tkt_flags(char * cc_name, char * tktname)
  10193. #else
  10194. ck_krb5_tkt_flags(cc_name,tktname) char * cc_name; char * tktname;
  10195. #endif
  10196. {
  10197. #ifdef KRB5
  10198. #ifndef HEIMDAL
  10199.     krb5_context kcontext;
  10200.     krb5_error_code retval;
  10201.     krb5_ccache cache = NULL;
  10202.     krb5_cc_cursor cur;
  10203.     krb5_creds creds;
  10204.     krb5_principal princ=NULL;
  10205.     krb5_flags flags=0;
  10206.     krb5_error_code code=0;
  10207.     char * flag_str = "";
  10208.  
  10209.     if ( !ck_krb5_is_installed() )
  10210.         return("");
  10211.  
  10212.     retval = krb5_init_context(&kcontext);
  10213.     if (retval) {
  10214.         debug(F101,"ck_krb5_tkt_flags while initializing krb5","",retval);
  10215.         return("");
  10216.     }
  10217.  
  10218.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10219.     if (code != 0) {
  10220.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10221.                error_message(code),code);
  10222.         goto exit_k5_get_tkt;
  10223.     }
  10224.  
  10225.     flags = 0;                          /* turns off OPENCLOSE mode */
  10226.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10227.         if (code == ENOENT) {
  10228.             debug(F111,"ck_krb5_tkt_flags (ticket cache)",
  10229.                    krb5_cc_get_name(kcontext, cache),code);
  10230.         } else {
  10231.             debug(F111,
  10232.                  "ck_krb5_tkt_flags while setting cache flags (ticket cache)",
  10233.                   krb5_cc_get_name(kcontext, cache),code);
  10234.         }
  10235.         retval = -1;
  10236.         goto exit_k5_get_tkt;
  10237.     }
  10238.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10239.         debug(F101,"ck_krb5_tkt_flags while retrieving principal name",
  10240.                "",code);
  10241.         retval = -1;
  10242.         goto exit_k5_get_tkt;
  10243.     }
  10244.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10245.         debug(F101,"ck_krb5_tkt_flags while unparsing principal name",
  10246.                "",code);
  10247.         retval = -1;
  10248.         goto exit_k5_get_tkt;
  10249.     }
  10250.  
  10251.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10252.         debug(F101,"ck_krb5_tkt_flags while starting to retrieve tickets",
  10253.                "",code);
  10254.         retval = -1;
  10255.         goto exit_k5_get_tkt;
  10256.     }
  10257.  
  10258.     if ((code = krb5_timeofday(kcontext, &now))) {
  10259.         if (!status_only)
  10260.             debug(F101,"ck_krb5_tkt_flags while getting time of day.",
  10261.                    "",code);
  10262.         retval = -1;
  10263.         goto exit_k5_get_tkt;
  10264.     }
  10265.  
  10266.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10267.         char *sname=NULL;
  10268.  
  10269.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10270.         if (retval) {
  10271.             debug(F101,
  10272.                   "ck_krb5_tkt_flags while unparsing server name","",retval);
  10273.             retval = -1;
  10274.             krb5_free_cred_contents(kcontext, &creds);
  10275.             goto exit_k5_get_tkt;
  10276.         }
  10277.  
  10278.         if ( !strcmp(sname,tktname) ) {
  10279.             /* we found the ticket we are looking for */
  10280.  
  10281.             flag_str = flags_string(&creds);
  10282.  
  10283.             krb5_free_cred_contents(kcontext, &creds);
  10284.             code = KRB5_CC_END;
  10285.             break;
  10286.         }
  10287.         krb5_free_cred_contents(kcontext, &creds);
  10288.     }
  10289.  
  10290.     if (code == KRB5_CC_END) {
  10291.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10292.             debug(F101,"ck_krb5_tkt_flags while finishing ticket retrieval",
  10293.                    "",code);
  10294.             goto exit_k5_get_tkt;
  10295.         }
  10296.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10297.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10298.             debug(F101,"ck_krb5_tkt_flags while closing ccache",
  10299.                    "",code);
  10300.             goto exit_k5_get_tkt;
  10301.         }
  10302.     } else {
  10303.         debug(F101,"ck_krb5_tkt_flags while retrieving a ticket","",code);
  10304.         goto exit_k5_get_tkt;
  10305.     }
  10306.  
  10307.   exit_k5_get_tkt:
  10308.     krb5_free_principal(kcontext,princ);
  10309.     krb5_free_unparsed_name(kcontext,defname);
  10310.     krb5_cc_close(kcontext,cache);
  10311.     krb5_free_context(kcontext);
  10312.     return(flag_str);
  10313. #else /* HEIMDAL */
  10314.     return("Not implemented");
  10315. #endif /* HEIMDAL */
  10316. #else /* KRB5 */
  10317.     return("");
  10318. #endif /* KRB5 */
  10319. }
  10320.  
  10321.  
  10322. int
  10323. #ifdef CK_ANSIC
  10324. ck_krb5_tkt_isvalid(char * cc_name, char * tktname)
  10325. #else
  10326. ck_krb5_tkt_isvalid(cc_name,tktname) char * cc_name; char * tktname;
  10327. #endif
  10328. {
  10329. #ifdef KRB5
  10330. #ifndef HEIMDAL
  10331.     krb5_context kcontext=NULL;
  10332.     krb5_error_code retval;
  10333.     krb5_ccache cache = NULL;
  10334.     krb5_cc_cursor cur;
  10335.     krb5_creds creds;
  10336.     krb5_principal princ=NULL;
  10337.     krb5_flags flags=0;
  10338.     krb5_error_code code=0;
  10339. #ifdef CHECKADDRS
  10340.     krb5_address **     myAddrs=NULL;
  10341.     krb5_address **     p=NULL;
  10342.     BOOL                Addrfound = FALSE;
  10343. #endif /*CHECKADDRS*/
  10344.  
  10345.     if ( !ck_krb5_is_installed() )
  10346.         return(-1);
  10347.  
  10348.     retval = krb5_init_context(&kcontext);
  10349.     if (retval) {
  10350.         debug(F101,"ck_krb5_tkt_isvalid while initializing krb5","",retval);
  10351.         return(-1);
  10352.     }
  10353.  
  10354.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10355.     if (code != 0) {
  10356.         debug(F111,"ck_krb5_tkt_isvalid while getting ccache",
  10357.                error_message(code),code);
  10358.         goto exit_k5_get_tkt;
  10359.     }
  10360.  
  10361.     flags = 0;                          /* turns off OPENCLOSE mode */
  10362.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10363.         if (code == ENOENT) {
  10364.             debug(F111,"ck_krb5_tkt_isvalid (ticket cache)",
  10365.                    krb5_cc_get_name(kcontext, cache),code);
  10366.         } else {
  10367.             debug(F111,
  10368.                 "ck_krb5_tkt_isvalid while setting cache flags (ticket cache)",
  10369.                   krb5_cc_get_name(kcontext, cache),code);
  10370.         }
  10371.         retval = -1;
  10372.         goto exit_k5_get_tkt;
  10373.     }
  10374.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10375.         debug(F101,"ck_krb5_tkt_isvalid while retrieving principal name",
  10376.                "",code);
  10377.         retval = -1;
  10378.         goto exit_k5_get_tkt;
  10379.     }
  10380.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10381.         debug(F101,"ck_krb5_tkt_isvalid while unparsing principal name",
  10382.                "",code);
  10383.         retval = -1;
  10384.         goto exit_k5_get_tkt;
  10385.     }
  10386.  
  10387.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10388.         debug(F101,"ck_krb5_tkt_isvalid while starting to retrieve tickets",
  10389.                "",code);
  10390.         retval = -1;
  10391.         goto exit_k5_get_tkt;
  10392.     }
  10393.  
  10394.     if ((code = krb5_timeofday(kcontext, &now))) {
  10395.         if (!status_only)
  10396.             debug(F101,"ck_krb5_tkt_isvalid while getting time of day.",
  10397.                    "",code);
  10398.         retval = -1;
  10399.         goto exit_k5_get_tkt;
  10400.     }
  10401.  
  10402.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10403.         char *sname=NULL;
  10404.  
  10405.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10406.         if (retval) {
  10407.             debug(F101,
  10408.                   "ck_krb5_tkt_isvalid while unparsing server name","",retval);
  10409.             retval = -1;
  10410.             krb5_free_cred_contents(kcontext, &creds);
  10411.             goto exit_k5_get_tkt;
  10412.         }
  10413.  
  10414.         if ( !strcmp(sname,tktname) ) {
  10415.             /* we found the ticket we are looking for */
  10416.  
  10417.             /* We add a 5 minutes fudge factor to compensate for potential */
  10418.             /* clock skew errors between the KDC and K95's host OS         */
  10419.  
  10420.             retval = ((creds.times.starttime > 0) &&
  10421.                        now >= (creds.times.starttime - 300) &&
  10422.                        now < (creds.times.endtime + 300) &&
  10423.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10424.  
  10425. #ifdef CHECKADDRS
  10426.             if ( retval && krb5_checkaddrs &&
  10427.                                  creds.addresses && creds.addresses[0] ) {
  10428.                 /* if we think it is valid, then lets check the IP Addresses */
  10429.                 /* to make sure it is valid for our current connection.      */
  10430.                 /* Also make sure it's for the correct IP address */
  10431.                 retval = krb5_os_localaddr(kcontext, &myAddrs);
  10432.                 if (retval) {
  10433.                     com_err(NULL, retval, "retrieving my IP address");
  10434.                     krb5_free_cred_contents(kcontext, &creds);
  10435.                     code = KRB5_CC_END;
  10436.                     retval = -1;
  10437.                     break;
  10438.                 }
  10439.  
  10440.              /* See if any of our addresses match any in cached credentials */
  10441.  
  10442.                 for (Addrfound=FALSE, p=myAddrs;
  10443.                      (Addrfound==FALSE) && (*p);
  10444.                      p++
  10445.                      ) {
  10446.                     if (krb5_address_search(kcontext, *p, creds.addresses)) {
  10447.                         Addrfound = TRUE;
  10448.                     }
  10449.                 }
  10450.                 krb5_free_addresses(k5_context, myAddrs);
  10451.  
  10452.                 if (Addrfound) {
  10453.                     krb5_free_cred_contents(kcontext, &creds);
  10454.                     code = KRB5_CC_END;
  10455.                     retval = 1;
  10456.                     break;
  10457.                 } else {
  10458.                     krb5_free_cred_contents(kcontext, &creds);
  10459.                     code = KRB5_CC_END;
  10460.                     retval = 0;
  10461.                     break;
  10462.                 }
  10463.             }
  10464. #endif /* CHECKADDRS */
  10465.  
  10466.             krb5_free_cred_contents(kcontext, &creds);
  10467.             code = KRB5_CC_END;
  10468.             break;
  10469.         }
  10470.         krb5_free_cred_contents(kcontext, &creds);
  10471.     }
  10472.  
  10473.     if (code == KRB5_CC_END) {
  10474.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10475.             debug(F101,"ck_krb5_tkt_isvalid while finishing ticket retrieval",
  10476.                    "",code);
  10477.             retval = -1;
  10478.             goto exit_k5_get_tkt;
  10479.         }
  10480.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10481.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10482.             debug(F101,"ck_krb5_tkt_isvalid while closing ccache",
  10483.                    "",code);
  10484.             retval = -1;
  10485.             goto exit_k5_get_tkt;
  10486.         }
  10487.     } else {
  10488.         debug(F101,"ck_krb5_tkt_isvalid while retrieving a ticket","",code);
  10489.         retval = -1;
  10490.         goto exit_k5_get_tkt;
  10491.     }
  10492.  
  10493.   exit_k5_get_tkt:
  10494.     krb5_free_principal(kcontext,princ);
  10495.     krb5_free_unparsed_name(kcontext,defname);
  10496.     krb5_cc_close(kcontext,cache);
  10497.     krb5_free_context(kcontext);
  10498.     return(retval);
  10499. #else /* HEIMDAL */
  10500.     return(-1);
  10501. #endif /* HEIMDAL */
  10502. #else /* KRB5 */
  10503.     return(-1);
  10504. #endif /* KRB5 */
  10505. }
  10506.  
  10507. int
  10508. #ifdef CK_ANSIC
  10509. ck_krb5_is_tgt_valid(VOID)
  10510. #else
  10511. ck_krb5_is_tgt_valid()
  10512. #endif
  10513. {
  10514. #ifdef KRB5
  10515. #ifndef HEIMDAL
  10516.     char tgt[256];
  10517.     char * s;
  10518.     int rc = 0;
  10519.  
  10520.     s = krb5_d_realm ? krb5_d_realm : ck_krb5_getrealm(krb5_d_cc);
  10521.     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",s,"@",s);
  10522.     rc = ck_krb5_tkt_isvalid(krb5_d_cc,tgt);
  10523.     debug(F111,"ck_krb5_is_tgt_valid",tgt,rc);
  10524.     return(rc>0);
  10525. #else /* HEIMDAL */
  10526.     return(-1);
  10527. #endif /* HEIMDAL */
  10528. #else /* KRB5 */
  10529.     return(0);
  10530. #endif /* KRB5 */
  10531. }
  10532.  
  10533. int
  10534. #ifdef CK_ANSIC
  10535. ck_krb5_tkt_time(char * cc_name, char * tktname)
  10536. #else
  10537. ck_krb5_tkt_time(cc_name, tktname) char * cc_name; char * tktname;
  10538. #endif
  10539. {
  10540. #ifdef KRB5
  10541. #ifndef HEIMDAL
  10542.     krb5_context kcontext;
  10543.     krb5_error_code retval;
  10544.     krb5_ccache cache = NULL;
  10545.     krb5_cc_cursor cur;
  10546.     krb5_creds creds;
  10547.     krb5_principal princ=NULL;
  10548.     krb5_flags flags=0;
  10549.     krb5_error_code code=0;
  10550.  
  10551.     if ( !ck_krb5_is_installed() )
  10552.         return(-1);
  10553.  
  10554.     retval = krb5_init_context(&kcontext);
  10555.     if (retval) {
  10556.         debug(F101,"ck_krb5_list_creds while initializing krb5","",retval);
  10557.         return(-1);
  10558.     }
  10559.  
  10560.     code = k5_get_ccache(kcontext,&cache,cc_name);
  10561.     if (code != 0) {
  10562.         debug(F111,"ck_krb5_tkt_time while getting ccache",
  10563.                error_message(code),code);
  10564.         retval = -1;
  10565.         goto exit_k5_get_tkt;
  10566.     }
  10567.  
  10568.     flags = 0;                          /* turns off OPENCLOSE mode */
  10569.     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10570.         if (code == ENOENT) {
  10571.             debug(F111,"ck_krb5_list_creds (ticket cache)",
  10572.                    krb5_cc_get_name(kcontext, cache),code);
  10573.         } else {
  10574.             debug(F111,
  10575.                  "ck_krb5_list_creds while setting cache flags (ticket cache)",
  10576.                   krb5_cc_get_name(kcontext, cache),code);
  10577.         }
  10578.         retval = -1;
  10579.         goto exit_k5_get_tkt;
  10580.     }
  10581.     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
  10582.         debug(F101,"ck_krb5_list_creds while retrieving principal name",
  10583.                "",code);
  10584.         retval = -1;
  10585.         goto exit_k5_get_tkt;
  10586.     }
  10587.     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
  10588.         debug(F101,"ck_krb5_list_creds while unparsing principal name",
  10589.                "",code);
  10590.         retval = -1;
  10591.         goto exit_k5_get_tkt;
  10592.     }
  10593.  
  10594.     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
  10595.         debug(F101,"ck_krb5_list_creds while starting to retrieve tickets",
  10596.                "",code);
  10597.         retval = -1;
  10598.         goto exit_k5_get_tkt;
  10599.     }
  10600.  
  10601.     if ((code = krb5_timeofday(kcontext, &now))) {
  10602.         if (!status_only)
  10603.             debug(F101,"ck_krb5_list_creds while getting time of day.",
  10604.                    "",code);
  10605.         krb5_free_context(kcontext);
  10606.         return(-1);
  10607.     }
  10608.  
  10609.     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
  10610.         char *sname=NULL;
  10611.  
  10612.         retval = krb5_unparse_name(kcontext, creds.server, &sname);
  10613.         if (retval) {
  10614.             debug(F101,
  10615.                   "ck_krb5_list_creds while unparsing server name","",retval);
  10616.             retval = -1;
  10617.             krb5_free_cred_contents(kcontext, &creds);
  10618.             goto exit_k5_get_tkt;
  10619.         }
  10620.  
  10621.         if ( !strcmp(sname,tktname) ) {
  10622.             /* we found the ticket we are looking for */
  10623.             int valid = (creds.times.starttime &&
  10624.                        now > creds.times.starttime &&
  10625.                        now < creds.times.endtime &&
  10626.                        !(creds.ticket_flags & TKT_FLG_INVALID));
  10627.             if ( valid ) {
  10628.                 retval = creds.times.endtime - now;
  10629.             }
  10630.             else
  10631.                 retval = 0;
  10632.             krb5_free_cred_contents(kcontext, &creds);
  10633.             code = KRB5_CC_END;
  10634.             break;
  10635.         }
  10636.         krb5_free_cred_contents(kcontext, &creds);
  10637.     }
  10638.  
  10639.     if (code == KRB5_CC_END) {
  10640.         if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
  10641.             debug(F101,"ck_krb5_list_creds while finishing ticket retrieval",
  10642.                    "",code);
  10643.             retval = -1;
  10644.             goto exit_k5_get_tkt;
  10645.         }
  10646.         flags = KRB5_TC_OPENCLOSE;      /* turns on OPENCLOSE mode */
  10647.         if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
  10648.             debug(F101,"ck_krb5_list_creds while closing ccache",
  10649.                    "",code);
  10650.             retval = -1;
  10651.             goto exit_k5_get_tkt;
  10652.         }
  10653.     } else {
  10654.         debug(F101,"ck_krb5_list_creds while retrieving a ticket","",code);
  10655.         retval = -1;
  10656.         goto exit_k5_get_tkt;
  10657.     }
  10658.  
  10659.   exit_k5_get_tkt:
  10660.     krb5_free_principal(kcontext,princ);
  10661.     krb5_free_unparsed_name(kcontext,defname);
  10662.     krb5_cc_close(kcontext,cache);
  10663.     krb5_free_context(kcontext);
  10664.     return(retval);
  10665. #else /* HEIMDAL */
  10666.     return(-1);
  10667. #endif /* HEIMDAL */
  10668. #else /* KRB5 */
  10669.     return(-1);
  10670. #endif /* KRB5 */
  10671. }
  10672.  
  10673. char *
  10674. #ifdef CK_ANSIC
  10675. ck_krb5_get_cc_name(void)
  10676. #else
  10677. ck_krb5_get_cc_name()
  10678. #endif
  10679. {
  10680. #ifdef KRB5
  10681. #ifndef HEIMDAL
  10682.     static char cc_name[CKMAXPATH+1]="";
  10683.     krb5_context kcontext = NULL;
  10684.     krb5_ccache ccache = NULL;
  10685.     krb5_error_code code;
  10686.     char * p=NULL;
  10687.  
  10688.     cc_name[0] = '\0';
  10689.  
  10690.     if ( !ck_krb5_is_installed() )
  10691.         return(cc_name);
  10692.  
  10693.     p = getenv("KRB5CCNAME");
  10694.     if ( !p ) {
  10695.         code = krb5_init_context(&kcontext);
  10696.         if (code) {
  10697.             com_err("ck_krb5_get_cc_name",code,"while init_context");
  10698.             return(cc_name);
  10699.         }
  10700.         if ((code = krb5_cc_default(kcontext, &ccache))) {
  10701.             com_err("ck_krb5_get_cc_name",code,"while getting default ccache");
  10702.             goto exit_k5_get_cc;
  10703.         }
  10704.  
  10705.         ckmakmsg(cc_name,sizeof(cc_name),
  10706.                  (char *)krb5_cc_get_type(kcontext,ccache),":",
  10707.                  (char *)krb5_cc_get_name(kcontext,ccache),NULL);
  10708.     } else {
  10709.         ckstrncpy(cc_name,p,CKMAXPATH);
  10710.     }
  10711.  
  10712.     if ( !strncmp("FILE:",cc_name,5) ) {
  10713.         for ( p=cc_name; *p ; p++ )
  10714.             if ( *p == '\\' ) *p = '/';
  10715.     }
  10716.  
  10717.   exit_k5_get_cc:
  10718.     if ( ccache )
  10719.         krb5_cc_close(kcontext,ccache);
  10720.     if ( kcontext )
  10721.         krb5_free_context(kcontext);
  10722.     return(cc_name);
  10723. #else /* HEIMDAL */
  10724.     return("Not implemented");
  10725. #endif /* HEIMDAL */
  10726. #else /* KRB5 */
  10727.     return("");
  10728. #endif /* KRB5 */
  10729. }
  10730.  
  10731. char *
  10732. #ifdef CK_ANSIC
  10733. ck_krb5_getrealm(char * cc_name)
  10734. #else
  10735. ck_krb5_getrealm(cc_name) char * cc_name;
  10736. #endif
  10737. {
  10738. #ifdef KRB5
  10739. #ifndef HEIMDAL
  10740.     static char realm[256]="";
  10741.     krb5_context kcontext;
  10742.     krb5_ccache ccache = NULL;
  10743.     krb5_error_code code;
  10744.     krb5_principal me;
  10745.  
  10746.     realm[0] = '\0';
  10747.  
  10748.     if ( !ck_krb5_is_installed() )
  10749.         return(realm);
  10750.  
  10751.     code = krb5_init_context(&kcontext);
  10752.     if (code) {
  10753.         return(realm);
  10754.     }
  10755.  
  10756.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10757.     if (code != 0) {
  10758.         goto exit_k5_getrealm;
  10759.     }
  10760.  
  10761.     if ((code = krb5_parse_name(kcontext, "foo", &me))) {
  10762.         goto exit_k5_getrealm;
  10763.     }
  10764.     if ( krb5_princ_realm(kcontext, me)->length < sizeof(realm) ) {
  10765.         memcpy(realm,krb5_princ_realm(kcontext, me)->data,
  10766.                 krb5_princ_realm(kcontext, me)->length);        /* safe */
  10767.        realm[krb5_princ_realm(kcontext, me)->length]='\0';
  10768.     }
  10769.   exit_k5_getrealm:
  10770.     if ( ccache )
  10771.         krb5_cc_close(kcontext,ccache);
  10772.     if (kcontext)
  10773.         krb5_free_context(kcontext);
  10774.     return(realm);
  10775. #else /* HEIMDAL */
  10776.     return("Not implemented");
  10777. #endif /* HEIMDAL */
  10778. #else /* KRB5 */
  10779.     return("");
  10780. #endif /* KRB5 */
  10781. }
  10782.  
  10783. char *
  10784. #ifdef CK_ANSIC
  10785. ck_krb5_getprincipal(char * cc_name)
  10786. #else
  10787. ck_krb5_getprincipal(cc_name) char * cc_name;
  10788. #endif
  10789. {
  10790. #ifdef KRB5
  10791. #ifndef HEIMDAL
  10792.     static char principal[UIDBUFLEN+1]="";
  10793.     krb5_context kcontext;
  10794.     krb5_ccache ccache = NULL;
  10795.     krb5_error_code code;
  10796.     krb5_principal me;
  10797.     char * p=NULL;
  10798.     int i;
  10799.  
  10800.     principal[0] = '\0';
  10801.  
  10802.     if ( !ck_krb5_is_installed() )
  10803.         return(principal);
  10804.  
  10805.     code = krb5_init_context(&kcontext);
  10806.     if (code) {
  10807.         return(principal);
  10808.     }
  10809.  
  10810.     code = k5_get_ccache(kcontext,&ccache,cc_name);
  10811.     if (code != 0) {
  10812.         goto exit_k5_getprincipal;
  10813.     }
  10814.  
  10815.     if ((code = krb5_cc_get_principal(kcontext, ccache, &me))) {
  10816.         goto exit_k5_getprincipal;
  10817.     }
  10818.  
  10819.     if ((code = krb5_unparse_name (kcontext, me, &p))) {
  10820.         krb5_free_principal(kcontext,me);
  10821.         goto exit_k5_getprincipal;
  10822.     }
  10823.  
  10824.     ckstrncpy(principal,p,UIDBUFLEN);
  10825.     i = ckindex("@",principal,0,0,0);
  10826.     if (i)
  10827.       principal[i-1] = '\0';
  10828.  
  10829.     krb5_free_unparsed_name(kcontext,p);
  10830.  
  10831.   exit_k5_getprincipal:
  10832.     if ( ccache )
  10833.         krb5_cc_close(kcontext,ccache);
  10834.     if (kcontext)
  10835.         krb5_free_context(kcontext);
  10836.     return(principal);
  10837. #else /* HEIMDAL */
  10838.     return("Not implemented");
  10839. #endif /* HEIMDAL */
  10840. #else /* KRB5 */
  10841.     return("");
  10842. #endif /* KRB5 */
  10843. }
  10844.  
  10845. #ifndef CRYPT_DLL
  10846. int
  10847. ck_get_crypt_table(struct keytab ** pTable, int * pN)
  10848. {
  10849. #ifdef CK_ENCRYPTION
  10850.     return(get_crypt_table(pTable, pN));
  10851. #else /* ENCRYPTION */
  10852.     int i=0;
  10853. #ifndef OS2
  10854.     char * tmpstring = NULL;
  10855. #endif /* OS2 */
  10856.  
  10857.     if ( *pTable )
  10858.     {
  10859.         for ( i=0 ; i < *pN ; i++ )
  10860.             free( (*pTable)[i].kwd ) ;
  10861.         free ( *pTable )  ;
  10862.     }
  10863.     *pTable = NULL;
  10864.     *pN = 0;
  10865.  
  10866.     *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  10867.     if ( !(*pTable) )
  10868.         return(0);
  10869.  
  10870. #ifdef OS2
  10871.     (*pTable)[0].kwd =strdup("automatic");
  10872. #else /* OS2 */
  10873.     makestr(&tmpstring,"automatic");
  10874.     (*pTable)[0].kwd = tmpstring;
  10875.     tmpstring = NULL;
  10876. #endif /* OS2 */
  10877.     (*pTable)[0].kwval = ENCTYPE_ANY;
  10878.     (*pTable)[0].flgs = 0;
  10879. #ifdef OS2
  10880.     (*pTable)[1].kwd =strdup("none");
  10881. #else /* OS2 */
  10882.     makestr(&tmpstring,"none");
  10883.     (*pTable)[1].kwd = tmpstring;
  10884.     tmpstring = NULL;
  10885. #endif /* OS2 */
  10886.     (*pTable)[1].kwval = 999;
  10887.     (*pTable)[1].flgs = 0;
  10888.     (*pN) = 2;
  10889.  
  10890.     return(2);
  10891. #endif /* ENCRYPTION */
  10892. }
  10893.  
  10894. VOID
  10895. ck_encrypt_send_support()
  10896. {
  10897. #ifdef CK_ENCRYPTION
  10898.     encrypt_send_support();
  10899. #endif /* ENCRYPTION */
  10900. }
  10901. #endif /* CRYPT_DLL */
  10902.  
  10903. /*
  10904.  *
  10905.  * Kstream
  10906.  *
  10907.  * Emulates the kstream package in Kerberos 4
  10908.  *
  10909.  */
  10910.  
  10911. int
  10912. kstream_destroy()
  10913. {
  10914.     if (g_kstream != NULL) {
  10915.         auth_destroy();                       /* Destroy authorizing */
  10916.         free(g_kstream);
  10917.         g_kstream=NULL;
  10918.     }
  10919.     return 0;
  10920. }
  10921.  
  10922. VOID
  10923. #ifdef CK_ANSIC
  10924. kstream_set_buffer_mode(int mode)
  10925. #else
  10926. kstream_set_buffer_mode(mode) int mode;
  10927. #endif
  10928. {
  10929. }
  10930.  
  10931.  
  10932. int
  10933. #ifdef CK_ANSIC
  10934. kstream_create_from_fd(int fd,
  10935.                        kstream_ptr data)
  10936. #else
  10937. kstream_create_from_fd(fd,data)
  10938.     int fd; kstream_ptr data;
  10939. #endif
  10940. {
  10941.     int n;
  10942.  
  10943.     g_kstream = malloc(sizeof(struct kstream_int));
  10944.     if (g_kstream == NULL)
  10945.         return 0;
  10946.  
  10947.     g_kstream->fd = fd;
  10948.  
  10949.     n = auth_init(g_kstream);                   /* Initialize authorizing */
  10950.     if (n) {
  10951.         free(g_kstream);
  10952.         g_kstream = NULL;
  10953.         return 0;
  10954.     }
  10955.  
  10956.     g_kstream->encrypt = NULL;
  10957.     g_kstream->decrypt = NULL;
  10958.     g_kstream->encrypt_type = ENCTYPE_ANY;
  10959.     g_kstream->decrypt_type = ENCTYPE_ANY;
  10960.     return 1;
  10961. }
  10962.  
  10963. #ifdef CK_KERBEROS
  10964. #ifdef RLOGCODE
  10965. static int do_lencheck, use_ivecs;
  10966. extern int rlog_inband;
  10967.  
  10968. #ifdef KRB5
  10969. void
  10970. rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
  10971.                            protonum)
  10972.      krb5_keyblock *in_keyblock;
  10973.      int encrypt_flag;
  10974.      int lencheck;
  10975.      int am_client;
  10976.      enum krb5_kcmd_proto protonum;
  10977. {
  10978.     krb5_error_code status;
  10979.     size_t blocksize;
  10980.  
  10981.     if (!encrypt_flag)
  10982.         return;
  10983.  
  10984.     desinbuf.data = des_inbuf;
  10985.     desoutbuf.data = des_outpkt+4;      /* Set up des buffers */
  10986.     k5_session_key = in_keyblock;
  10987.  
  10988.     do_lencheck = lencheck;
  10989.  
  10990.     if ( protonum == KCMD_OLD_PROTOCOL ) {
  10991.         use_ivecs = 0;
  10992.         return;
  10993.     }
  10994.  
  10995.     use_ivecs = 1;
  10996.  
  10997.     if (status = krb5_c_block_size(k5_context, k5_session_key->enctype,
  10998.                                    &blocksize)) {
  10999.         /* XXX what do I do? */
  11000.         printf("fatal kerberos 5 crypto library error\n");
  11001.         ttclos(0);
  11002.         return;
  11003.     }
  11004.  
  11005.     encivec_i[0].length = encivec_i[1].length = 
  11006.     encivec_o[0].length = encivec_o[1].length = blocksize;
  11007.  
  11008.     if ((encivec_i[0].data = malloc(encivec_i[0].length * 4)) == NULL) {
  11009.         /* XXX what do I do? */
  11010.         printf("fatal malloc failed\n");
  11011.         ttclos(0);
  11012.         return;
  11013.     }
  11014.  
  11015.     encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
  11016.     encivec_o[0].data = encivec_i[1].data + encivec_i[1].length;
  11017.     encivec_o[1].data = encivec_o[0].data + encivec_o[0].length;
  11018.  
  11019.     /* is there a better way to initialize this? */
  11020.     memset(encivec_i[0].data, am_client, blocksize);
  11021.     memset(encivec_o[0].data, 1 - am_client, blocksize);
  11022.     memset(encivec_i[1].data, 2 | am_client, blocksize);
  11023.     memset(encivec_o[1].data, 2 | (1 - am_client), blocksize);
  11024. }
  11025. #endif /* KRB5 */
  11026.  
  11027. int
  11028. #ifdef CK_ANSIC
  11029. ck_krb_rlogin(CHAR * hostname, int port,
  11030.                CHAR * localuser, CHAR * remoteuser, CHAR * term_speed,
  11031.                struct sockaddr_in * l_addr, struct sockaddr_in * r_addr,
  11032.                int kversion, int encrypt_flag)
  11033. #else /* CK_ANSIC */
  11034. ck_krb_rlogin(hostname, port,
  11035.                localuser, remoteuser, term_speed, l_addr, r_addr, encrypt_flag)
  11036.     CHAR * hostname; int port;
  11037.     CHAR * localuser; CHAR * remoteuser; CHAR * term_speed;
  11038.     struct sockaddr_in * l_addr; struct sockaddr_in * r_addr;
  11039.     int kversion; int encrypt_flag;
  11040. #endif /* CK_ANSIC */
  11041. {
  11042.     unsigned long status;
  11043.     char * realm=NULL;
  11044.     extern int ttyfd;
  11045.     int c;
  11046.     long msglen;
  11047.  
  11048.     debug(F111,"ck_krb_rlogin",hostname,port);
  11049.  
  11050.     if ( kversion == 4 && !ck_krb4_is_installed() ) {
  11051.         printf("?Kerberos 4 is not installed\r\n");
  11052.         return(-1);
  11053.     } else if ( kversion == 5 && !ck_krb5_is_installed() ) {
  11054.         printf("?Kerberos 5 is not installed\r\n");
  11055.         return(-1);
  11056.     }
  11057.  
  11058.     if ( encrypt_flag && !ck_crypt_is_installed() ) {
  11059.         printf("?Encryption is not installed\r\n");
  11060.         return(-1);
  11061.     }
  11062.  
  11063.     if ( kversion == 5 ) {
  11064. #ifdef KRB5
  11065.         krb5_flags authopts=0;
  11066.         krb5_ccache ccache=NULL;
  11067.         char *cksumbuf=NULL;
  11068.         char *service=NULL;
  11069.         char * kcmd_version=NULL;
  11070.         enum krb5_kcmd_proto use_proto;
  11071.         krb5_data cksumdat;
  11072.         krb5_creds *get_cred = 0;
  11073.         krb5_error_code status;
  11074.         krb5_error      *error = 0;
  11075.         krb5_ap_rep_enc_part *rep_ret = NULL;
  11076.         krb5_data outbuf;
  11077.         int rc;
  11078.         krb5_int32 seqno=0;
  11079.         krb5_int32 server_seqno=0;
  11080.         char ** realmlist=NULL;
  11081.         int buflen;
  11082.         char tgt[256];
  11083.  
  11084.         debug(F100,"ck_krb_rlogin version 5","",0);
  11085.  
  11086.         realm = ck_krb5_realmofhost(hostname);
  11087.         if (!realm) {
  11088.             ckstrncpy(strTmp, "Can't find realm for host \"",AUTHTMPBL);
  11089.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11090.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11091.             printf("?Kerberos 5 error: %s\r\n",strTmp);
  11092.             krb5_errno = KRB5_ERR_HOST_REALM_UNKNOWN;
  11093.             makestr(&krb5_errmsg,strTmp);
  11094.             return(0);
  11095.         }
  11096.  
  11097.         ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11098.         debug(F110,"ck_rlog_rlogin TGT",tgt,0);
  11099.         if ( krb5_autoget &&
  11100.              !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11101.                 (ck_krb5_is_tgt_valid() > 0)) )
  11102.             ck_krb5_autoget_TGT(realm);
  11103.  
  11104.         buflen = strlen(term_speed)+strlen(remoteuser)+64;
  11105.         if ((cksumbuf = malloc(buflen)) == 0)
  11106.         {
  11107.             printf("Unable to allocate memory for checksum buffer.\r\n");
  11108.             return(-1);
  11109.         }
  11110.  
  11111.         ckmakmsg(cksumbuf,buflen,ckuitoa((unsigned short) ntohs(port)),":",
  11112.                   term_speed,remoteuser);
  11113.         cksumdat.data = cksumbuf;
  11114.         cksumdat.length = strlen(cksumbuf);
  11115.  
  11116.         status = krb5_init_context(&k5_context);
  11117.         if (status) {
  11118.             debug(F110,"ck_krb_rlogin()","unable to init_context",0);
  11119.             return(-1);
  11120.         }
  11121.  
  11122.         desinbuf.data = des_inbuf;
  11123.         desoutbuf.data = des_outpkt+4;  /* Set up des buffers */
  11124.  
  11125.         rc = k5_get_ccache(k5_context,&ccache,NULL);
  11126.         if (rc != 0) {
  11127.             com_err(NULL, rc, "while getting ccache.");
  11128.             return(0);
  11129.         }
  11130.  
  11131.         service = krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME;
  11132.  
  11133.         if (!(get_cred = (krb5_creds *)calloc(1, sizeof(krb5_creds)))) {
  11134.             printf("ck_krb_rlogin: no memory\r\n");
  11135.             return(-1);
  11136.         }
  11137.         memset(get_cred,0,sizeof(krb5_creds));
  11138.         status = krb5_sname_to_principal(k5_context, hostname, service,
  11139.                                           KRB5_NT_SRV_HST, &get_cred->server);
  11140.         if (status) {
  11141.             printf("ck_krb_rlogin: krb5_sname_to_principal failed: %s\r\n",
  11142.                      error_message(status));
  11143.             return(-1);
  11144.         }
  11145.  
  11146.         ttoc(0);
  11147.  
  11148.         if (status = krb5_cc_get_principal(k5_context,
  11149.                                            ccache,
  11150.                                            &get_cred->client)
  11151.             ) {
  11152.             (void) krb5_cc_close(k5_context, ccache);
  11153.             krb5_free_creds(k5_context, get_cred);
  11154.             goto bad;
  11155.         }
  11156.  
  11157.         if (krb5_rlog_ver == KCMD_OLD_PROTOCOL)
  11158.             get_cred->keyblock.enctype=ENCTYPE_DES_CBC_CRC;
  11159.  
  11160.         /* Get ticket from credentials cache or kdc */
  11161.         status = krb5_get_credentials(k5_context,
  11162.                                       0,
  11163.                                       ccache,
  11164.                                       get_cred,
  11165.                                       &ret_cred
  11166.                                       );
  11167.         krb5_free_creds(k5_context, get_cred);
  11168.         get_cred = NULL;
  11169.         (void) krb5_cc_close(k5_context, ccache);
  11170.  
  11171.         if (status) 
  11172.             goto bad;
  11173.  
  11174.         /* Reset internal flags; these should not be set. */
  11175.         authopts &= (~OPTS_FORWARD_CREDS);
  11176.         authopts &= (~OPTS_FORWARDABLE_CREDS);
  11177.  
  11178.         if (krb5_auth_con_init(k5_context, &auth_context))
  11179.             goto bad;
  11180.  
  11181.         if (krb5_auth_con_setflags(k5_context, auth_context,
  11182.                                     KRB5_AUTH_CONTEXT_RET_TIME))
  11183.             goto bad;
  11184.  
  11185.         /* Only need local address for mk_cred() to send to krlogind */
  11186.         if (!krb5_d_no_addresses)
  11187.             if (status = krb5_auth_con_genaddrs(k5_context,
  11188.                                                 auth_context,
  11189.                                                  ttyfd,
  11190.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
  11191.                                                  ))
  11192.                 goto bad;
  11193.  
  11194.         /* Here is where we start to handle the new protocol in earnest */
  11195.         if ( krb5_rlog_ver == KCMD_PROTOCOL_COMPAT_HACK ) {
  11196.             krb5_boolean is_des;
  11197.  
  11198.             if (status = krb5_c_enctype_compare( k5_context,
  11199.                                                  ENCTYPE_DES_CBC_CRC,
  11200. #ifdef HEIMDAL
  11201.                                                  ret_cred->session.keytype,
  11202. #else /* HEIMDAL */
  11203.                                                  ret_cred->keyblock.enctype,
  11204. #endif /* HEIMDAL */
  11205.                                                  &is_des)) {
  11206.                 krb5_free_creds(k5_context, ret_cred);
  11207.                 ret_cred = NULL;
  11208.                 goto bad;
  11209.             }
  11210.  
  11211.             if ( is_des ) {
  11212.                 kcmd_version = "KCMDV0.1";
  11213.                 use_proto = KCMD_OLD_PROTOCOL;
  11214.             } else {
  11215.                 authopts = AP_OPTS_USE_SUBKEY;
  11216.                 kcmd_version = "KCMDV0.2";
  11217.                 use_proto = KCMD_NEW_PROTOCOL;
  11218.             }
  11219.         } else {
  11220.             use_proto = krb5_rlog_ver;
  11221.             switch ( krb5_rlog_ver ) {
  11222.             case KCMD_NEW_PROTOCOL:
  11223.                 authopts = AP_OPTS_USE_SUBKEY;
  11224.                 kcmd_version = "KCMDV0.2";
  11225.                 break;
  11226.             case KCMD_OLD_PROTOCOL:
  11227.                 kcmd_version = "KCMDV0.1";
  11228.                 break;
  11229.             default:
  11230.                 goto bad;
  11231.             }
  11232.         }
  11233.  
  11234.         /* call Kerberos library routine to obtain an authenticator,
  11235.            pass it over the socket to the server, and obtain mutual
  11236.            authentication.
  11237.          */
  11238.         status = krb5_sendauth(k5_context,
  11239.                                &auth_context,
  11240.                                (krb5_pointer) &ttyfd,
  11241.                                kcmd_version,
  11242.                                ret_cred->client,
  11243.                                ret_cred->server,
  11244.                                 authopts,
  11245.                                &cksumdat,
  11246.                                ret_cred,
  11247.                                0,
  11248.                                &error,
  11249.                                &rep_ret,
  11250.                                NULL
  11251.                                );
  11252.         krb5_free_data_contents(k5_context,&cksumdat);
  11253.  
  11254.         if (status) {
  11255.             if ( !quiet )
  11256.                 printf("Couldn't authenticate to server: %s\r\n",
  11257.                         error_message(status));
  11258.             if (error) {
  11259.                 if ( !quiet ) {
  11260.                     printf("Server returned error code %d (%s)\r\n",
  11261.                         error->error,
  11262.                         error_message(ERROR_TABLE_BASE_krb5 + error->error));
  11263.                     if (error->text.length) {
  11264.                         printf("Error text sent from server: %s\r\n",
  11265.                                 error->text.data);
  11266.                     }
  11267.                 }
  11268.                 krb5_free_error(k5_context, error);
  11269.                 error = 0;
  11270.             }
  11271.             goto bad;
  11272.         }
  11273.  
  11274.         if (rep_ret) {
  11275.             server_seqno = rep_ret->seq_number;
  11276.             krb5_free_ap_rep_enc_part(k5_context, rep_ret);
  11277.         }
  11278.  
  11279.         (void) ttol(remoteuser, strlen(remoteuser)+1);
  11280.         (void) ttol(term_speed, strlen(term_speed)+1);
  11281.         (void) ttol(localuser, strlen(localuser)+1);
  11282.  
  11283.         if (forward_flag) {   /* Forward credentials (global) */
  11284.             if (status = krb5_fwd_tgt_creds( k5_context,
  11285.                                              auth_context,
  11286.                                              hostname,
  11287.                                              ret_cred->client,
  11288.                                              ret_cred->server,
  11289.                                              0,
  11290.                                              (forwardable_flag ?
  11291.                                                OPTS_FORWARDABLE_CREDS :
  11292.                                                0),
  11293.                                              &outbuf
  11294.                                              )
  11295.                  )
  11296.             {
  11297.                 printf("Error forwarding credentials: %s\r\n",
  11298.                          error_message(status));
  11299.                 goto bad2;
  11300.             }
  11301.  
  11302.             /* Send forwarded credentials */
  11303.             status = krb5_write_message(k5_context,
  11304.                                          (krb5_pointer)&ttyfd,
  11305.                                          &outbuf
  11306.                                          );
  11307.         }
  11308.         else { /* Dummy write to signal no forwarding */
  11309.           bad2:
  11310.             outbuf.length = 0;
  11311.             status = krb5_write_message(k5_context,
  11312.                                          (krb5_pointer)&ttyfd,
  11313.                                          &outbuf);
  11314.         }       
  11315.  
  11316.         if ((c = ttinc(0)) < 0) {
  11317.             if (c==-1) {
  11318.                 perror(hostname);
  11319.             } else {
  11320.                 printf("ck_krb_rlogin: bad connection with remote host\r\n");
  11321.             }
  11322.             status = -1;
  11323.             goto bad;
  11324.         }
  11325.         if (c != 0) {
  11326.             while ((c = ttinc(1)) >= 0) {
  11327.                 (void) printf("%c",c);
  11328.                 if (c == '\n')
  11329.                     break;
  11330.             }
  11331.             status = -1;
  11332.             goto bad;
  11333.         }
  11334.  
  11335.         if ( status == 0 ) {        /* success */
  11336.             krb5_keyblock * key = 0;
  11337.  
  11338.             if ( use_proto == KCMD_NEW_PROTOCOL ) {
  11339.                 int on = 1;
  11340.                 rlog_inband = 1;
  11341.                 setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,
  11342.                            (char *) &on, sizeof on);
  11343.  
  11344.                 status = krb5_auth_con_getlocalsubkey( k5_context,
  11345.                                                        auth_context,
  11346.                                                        &key);
  11347.                 if ((status || !key) && encrypt_flag )
  11348.                     goto bad2;
  11349.             }
  11350.             if ( key == 0 ) {
  11351. #ifdef HEIMDAL
  11352.                 key = &ret_cred->session;
  11353. #else /* HEIMDAL */
  11354.                 key = &ret_cred->keyblock;
  11355. #endif /* HEIMDAL */
  11356.             }
  11357.  
  11358.             rcmd_stream_init_krb5(key, encrypt_flag, 1, 1, use_proto);
  11359.             if ( encrypt_flag )
  11360.                 rlog_encrypt = 1;
  11361.         }
  11362.         return (0);     /* success */
  11363.  
  11364.       bad:
  11365.         if ( status && !quiet ) {
  11366.             printf("Kerberos authentication error: %s\r\n",
  11367.                     error_message(status));
  11368.         }
  11369.         if (ret_cred) {
  11370.             krb5_free_creds(k5_context, ret_cred);
  11371.             ret_cred = NULL;
  11372.         }
  11373.         return (status);
  11374. #else /* KRB5 */
  11375.         return(-1);
  11376. #endif /* KRB5 */
  11377.     } else if (kversion == 4) {
  11378. #ifdef KRB4
  11379.         char tgt[4*REALM_SZ+1];
  11380.         debug(F100,"ck_krb_rlogin version 4","",0);
  11381.  
  11382.         realm = (char *)krb_realmofhost(hostname);
  11383.         if (!realm) {
  11384.             strcpy(strTmp, "Can't find realm for host \"");
  11385.             ckstrncat(strTmp, hostname,AUTHTMPBL);
  11386.             ckstrncat(strTmp, "\"",AUTHTMPBL);
  11387.             printf("?Kerberos 4 error: %s\r\n",strTmp);
  11388.             krb4_errno = 0;
  11389.             makestr(&krb4_errmsg,strTmp);
  11390.             return(0);
  11391.         }
  11392.  
  11393.         ckmakmsg(tgt,sizeof(tgt),"krbtgt.",realm,"@",realm);
  11394.         status = ck_krb4_tkt_isvalid(tgt);
  11395.  
  11396.         if ( status <= 0 && krb4_autoget )
  11397.             ck_krb4_autoget_TGT(realm);
  11398.  
  11399.         ttoc(0);        /* write a NUL */
  11400.  
  11401.         status = krb_sendauth(encrypt_flag?KOPT_DO_MUTUAL:0,
  11402.                                ttyfd,
  11403.                                &k4_auth,
  11404.                                krb4_d_srv ? krb4_d_srv : KRB4_SERVICE_NAME,
  11405.                                hostname,
  11406.                                realm,
  11407.                                (unsigned long) getpid(),
  11408.                                &k4_msg_data,
  11409.                                (CREDENTIALS *)&cred,
  11410. #ifdef CK_ENCRYPTION
  11411.                                &k4_sched,
  11412. #else /* ENCRYPTION */
  11413.                                NULL,
  11414. #endif /* ENCRYPTION */
  11415.                                l_addr,
  11416.                                r_addr,
  11417.                                "KCMDV0.1");
  11418.         debug(F111,"ck_krb_rlogin","krb_sendauth",status);
  11419.         if (status != KSUCCESS) {
  11420.             printf( "krb_sendauth failed: %s\r\n",
  11421.                     krb_get_err_text_entry(status)
  11422.                     );
  11423.             return(-1);
  11424.         }
  11425.         ttol(remoteuser,strlen(remoteuser)+1);
  11426.         ttol(term_speed,strlen(term_speed)+1);
  11427.  
  11428.       reread:
  11429.         if ((c = ttinc(0)) < 0) {
  11430.             printf("rcmd: bad connection with remote host\r\n");
  11431.             return(-1);
  11432.         }
  11433.         debug(F111,"ck_krb_rlogin","first byte",c);
  11434.  
  11435.         if (c != 0) {
  11436.             char *check = "ld.so: warning:";
  11437.             /* If rlogind was compiled on SunOS4, and it somehow
  11438.             got the shared library version numbers wrong, it
  11439.             may give an ld.so warning about an old version of a
  11440.             shared library.  Just ignore any such warning.
  11441.             Note that the warning is a characteristic of the
  11442.             server; we may not ourselves be running under
  11443.             SunOS4.  */
  11444.             if (c == 'l') {
  11445.                 char *p;
  11446.                 char cc;
  11447.  
  11448.                 p = &check[1];
  11449.                 while ((c = ttinc(0)) >= 0) {
  11450.                     if (*p == '\0') {
  11451.                         if (c == '\n')
  11452.                             break;
  11453.                     } else {
  11454.                         if (c != *p)
  11455.                             break;
  11456.                         ++p;
  11457.                     }
  11458.                 }
  11459.  
  11460.                 if (*p == '\0')
  11461.                     goto reread;
  11462.             }
  11463.  
  11464.             printf(check);
  11465.             while ((c = ttinc(1)) >= 0) {
  11466.                 printf("%c",c);
  11467.                 if (c == '\n')
  11468.                     break;
  11469.             }
  11470.             debug(F110,"ck_krb_rlogin","fatal error 1",0);
  11471.             return(-1);
  11472.         }
  11473.  
  11474. #ifdef CK_ENCRYPTION
  11475.         if ( encrypt_flag ) {
  11476.             /* if we are encrypting we need to setup the encryption */
  11477.             /* routines.                                            */
  11478.             des_key_sched(cred.session, k4_sched);
  11479.             rlog_encrypt = 1;
  11480.         }
  11481. #endif /* ENCRYPTION */
  11482. #else /* KRB4 */
  11483.         return(-1);
  11484. #endif /* KRB4 */
  11485.     }
  11486.     return(0); /* success */
  11487. }
  11488.  
  11489. #define SRAND   srand
  11490. #define RAND    rand
  11491. #define RAND_TYPE       int
  11492.  
  11493. static long
  11494. random_confounder(size, fillin)
  11495. size_t size;
  11496. char * fillin;
  11497. {
  11498.     static int seeded = 0;
  11499.     register unsigned char *real_fill;
  11500.     RAND_TYPE   rval;
  11501.  
  11502.     if (!seeded) {
  11503.         /* time() defined in 4.12.2.4, but returns a time_t, which is an
  11504.            "arithmetic type" (4.12.1) */
  11505.         rval = (RAND_TYPE) time(0);
  11506.         SRAND(rval);
  11507.         rval = RAND();
  11508.         rval ^= getpid();
  11509.         SRAND(rval);
  11510.         seeded = 1;
  11511.     }
  11512.  
  11513.     real_fill = (unsigned char *)fillin;
  11514.     while (size > 0) {
  11515.         rval = RAND();
  11516.         *real_fill = rval & 0xff;
  11517.         real_fill++;
  11518.         size--;
  11519.         if (size) {
  11520.             *real_fill = (rval >> 8) & 0xff;
  11521.             real_fill++;
  11522.             size--;
  11523.         }
  11524.     }
  11525.     return 0;
  11526. }
  11527.  
  11528. #ifdef KRB5
  11529. int
  11530. krb5_des_avail(fd)
  11531.     int fd;
  11532. {
  11533.     return(nstored);
  11534. }
  11535.  
  11536. int
  11537. krb5_des_read(fd, buf, len, secondary)
  11538.     int fd;
  11539.     register char *buf;
  11540.     int len;
  11541.     int secondary;
  11542. {
  11543.     int nreturned = 0;
  11544.     long net_len,rd_len;
  11545.     int cc;
  11546.     krb5_error_code status;
  11547.     unsigned char c;
  11548.     krb5_data plain;
  11549.     krb5_enc_data cipher;
  11550.  
  11551.     debug(F111,"krb5_des_read","len",len);
  11552.     debug(F111,"krb5_des_read","rlog_encrypt",rlog_encrypt);
  11553.     if ( !rlog_encrypt ) {
  11554.         cc = net_read(fd, buf, len);
  11555.         debug(F111,"krb5_des_read","chars read",cc);
  11556.         if ( cc < 0 )
  11557.             netclos();
  11558.         return(cc);
  11559.     }
  11560.  
  11561.     if (nstored >= len) {
  11562.         if ( buf ) {
  11563.             memcpy(buf, store_ptr, len);        /* safe */
  11564.             store_ptr += len;
  11565.             nstored -= len;
  11566.             return(len);
  11567.         } else
  11568.             return(0);
  11569.     } else if (nstored) {
  11570.         if ( buf ) {
  11571.             memcpy(buf, store_ptr, nstored);    /* safe */
  11572.             nreturned += nstored;
  11573.             buf += nstored;
  11574.             len -= nstored;
  11575.             nstored = 0;
  11576.         }
  11577.         else
  11578.             return(0);
  11579.     }
  11580.  
  11581.     /* See the comment in v4_des_read. */
  11582.     while (1) {
  11583.         cc = net_read(fd, &c, 1);
  11584.         /* we should check for non-blocking here, but we'd have
  11585.         to make it save partial reads as well. */
  11586.         if (cc <= 0) {
  11587.             return cc; /* read error */
  11588.         }
  11589.         if (cc == 1) {
  11590.             if (c == 0 || !do_lencheck) 
  11591.                 break;
  11592.         }
  11593.     }
  11594.  
  11595.     rd_len = c;
  11596.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11597.     rd_len = (rd_len << 8) | c;
  11598.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11599.     rd_len = (rd_len << 8) | c;
  11600.     if ((cc = net_read(fd, &c, 1)) != 1) return 0;
  11601.     rd_len = (rd_len << 8) | c;
  11602.  
  11603.     if (status = krb5_c_encrypt_length(k5_context, 
  11604.                                     k5_session_key->enctype,
  11605.                                     use_ivecs ? rd_len + 4 : rd_len,
  11606.                                     &net_len)) {
  11607.         errno = status;
  11608.         return(-1);
  11609.     }
  11610.  
  11611.     if ((net_len <= 0) || (net_len > sizeof(des_inbuf))) {
  11612.         /* preposterous length; assume out-of-sync; only
  11613.            recourse is to close connection, so return 0 */
  11614.         printf("Read size problem.\r\n");
  11615.         return(0);
  11616.     }
  11617.     if ((cc = net_read(fd, desinbuf.data, net_len)) != net_len )
  11618.     {
  11619.         /* pipe must have closed, return 0 */
  11620.         printf( "Read error: length received %d != expected %d.\r\n",
  11621.                 cc,
  11622.                 net_len
  11623.                 );
  11624.         return(cc);
  11625.     }
  11626.  
  11627.  
  11628.     /* decrypt info */
  11629.     cipher.enctype = ENCTYPE_UNKNOWN;
  11630.     cipher.ciphertext.length = net_len;
  11631.     cipher.ciphertext.data = desinbuf.data;
  11632.     plain.length = sizeof(storage);
  11633.     plain.data = storage;
  11634.  
  11635.     if ( status = krb5_c_decrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11636.                                  use_ivecs ? encivec_i + secondary : 0,
  11637.                                   &cipher,&plain) ) {
  11638.         /* probably out of sync */
  11639.         printf("Cannot decrypt data from network: %s\r\n",
  11640.                  error_message(status));
  11641.         errno = EIO;
  11642.         return(-1);
  11643.     }
  11644.     
  11645.     store_ptr = storage;
  11646.     nstored = rd_len;
  11647.  
  11648.     if ( use_ivecs ) {
  11649.         int rd_len2;
  11650.         rd_len2 = storage[0] & 0xff;
  11651.         rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
  11652.         rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
  11653.         rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
  11654.         if (rd_len2 != rd_len) {
  11655.             /* cleartext length trashed? */
  11656.             errno = EIO;
  11657.             return -1;
  11658.         }
  11659.         store_ptr += 4;
  11660.     }
  11661.  
  11662.     if ( !buf )
  11663.         return(0);
  11664.  
  11665. #ifdef RLOGCODE                         /* blah */
  11666.     if (rlog_inband && (ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN))
  11667.     {
  11668.         int i, left, n;
  11669.  
  11670.         for (i = 0; i < nstored; i++) {
  11671.             if (store_ptr[i] == '\377' &&
  11672.                 store_ptr[i+1] == '\377') {
  11673.                 left = nstored - i;
  11674.                 n = rlog_ctrl(&store_ptr[i], left);
  11675.                 if (n < 0) {
  11676.                     left -= (-n);
  11677.                     nstored = left;
  11678.                     /* flush before, and (-n) bytes */
  11679.                     if (left > 0)
  11680.                         memmove(store_ptr, &store_ptr[i-n], left);
  11681.                 } else if (n) {
  11682.                     left -= n;
  11683.                     nstored -= n;
  11684.                     if (left > 0)
  11685.                         memmove(store_ptr, &store_ptr[n], left);
  11686.                 }
  11687.             }
  11688.         }
  11689.     }
  11690. #endif /* RLOGCODE */
  11691.  
  11692.     if (nstored > len) {
  11693.         memcpy(buf, store_ptr, len);            /* safe */
  11694.         nreturned += len;
  11695.         store_ptr += len;
  11696.         nstored -= len;
  11697.     } else {
  11698.         memcpy(buf, store_ptr, nstored);        /* safe */
  11699.         nreturned += nstored;
  11700.         nstored = 0;
  11701.     }
  11702.     return(nreturned);
  11703. }
  11704.  
  11705. int
  11706. krb5_des_write(fd, buf, len, secondary)
  11707.     int fd;
  11708.     char *buf;
  11709.     int len;
  11710.     int secondary;
  11711. {
  11712.     char tmpbuf[2*RLOG_BUFSIZ+8];
  11713.     unsigned char *len_buf = (unsigned char *) tmpbuf;
  11714.     krb5_error_code status;
  11715.     krb5_data plain;
  11716.     krb5_enc_data cipher;
  11717.  
  11718.     debug(F111,"krb5_des_write","rlog_encrypt",rlog_encrypt);
  11719.     if ( !rlog_encrypt ) {
  11720.         int cc = net_write(fd, buf, len);
  11721.         debug(F111,"net_write","chars written",cc);
  11722.         return(cc != len ? -1 : len);
  11723.     }
  11724.  
  11725.     if (use_ivecs) {
  11726.         unsigned char *lenbuf2 = (unsigned char *) tmpbuf;
  11727.         if (len + 4 > sizeof(tmpbuf))
  11728.             abort ();
  11729.         lenbuf2[0] = (len & 0xff000000) >> 24;
  11730.         lenbuf2[1] = (len & 0xff0000) >> 16;
  11731.         lenbuf2[2] = (len & 0xff00) >> 8;
  11732.         lenbuf2[3] = (len & 0xff);
  11733.         memcpy (tmpbuf + 4, buf, len);
  11734.  
  11735.         plain.data = tmpbuf;
  11736.         plain.length = len + 4;
  11737.     } else {
  11738.         plain.data = buf;
  11739.         plain.length = len;
  11740.     }
  11741.  
  11742.     cipher.ciphertext.length = sizeof(des_outpkt)-4;
  11743.     cipher.ciphertext.data = desoutbuf.data;
  11744.  
  11745.     if ( status = krb5_c_encrypt(k5_context, k5_session_key, KCMD_KEYUSAGE,
  11746.                          use_ivecs ? encivec_o + secondary : 0,
  11747.                          &plain, &cipher)) {
  11748.         printf("Write encrypt problem: %s.\r\n",
  11749.                  error_message(status));
  11750.         errno = EIO;
  11751.         return(-1);
  11752.     }
  11753.     desoutbuf.length = cipher.ciphertext.length;
  11754.  
  11755.     len_buf = (unsigned char *) des_outpkt;
  11756.     len_buf[0] = (len & 0xff000000) >> 24;
  11757.     len_buf[1] = (len & 0xff0000) >> 16;
  11758.     len_buf[2] = (len & 0xff00) >> 8;
  11759.     len_buf[3] = (len & 0xff);
  11760.  
  11761.     if (net_write(fd, des_outpkt,desoutbuf.length+4)
  11762.          != desoutbuf.length+4){
  11763.         printf("Could not write out all data\r\n");
  11764.         return(-1);
  11765.     }
  11766.     else return(len);
  11767. }
  11768. #endif /* KRB5 */
  11769.  
  11770. #ifdef KRB4
  11771. /*
  11772.  * Note that the encrypted rlogin packets take the form of a four-byte
  11773.  * length followed by encrypted data.  On writing the data out, a significant
  11774.  * performance penalty is suffered (at least one RTT per character, two if we
  11775.  * are waiting for a shell to echo) by writing the data separately from the
  11776.  * length.  So, unlike the input buffer, which just contains the output
  11777.  * data, the output buffer represents the entire packet.
  11778.  */
  11779.  
  11780. int
  11781. krb4_des_avail(fd)
  11782.     int fd;
  11783. {
  11784.     return(nstored);
  11785. }
  11786.  
  11787. int
  11788. krb4_des_read(fd, buf, len)
  11789. int fd;
  11790. register char *buf;
  11791. int len;
  11792. {
  11793.     int nreturned = 0;
  11794.     unsigned long net_len, rd_len;
  11795.     int cc;
  11796.     unsigned char c;
  11797.     int gotzero = 0;
  11798.  
  11799.     debug(F111,"krb4_des_read","rlog_encrypt",rlog_encrypt);
  11800.     debug(F111,"krb4_des_read","len",len);
  11801.     if ( !rlog_encrypt ) {
  11802.         cc = net_read(fd, buf, len);
  11803.         debug(F111,"krb4_des_read","chars read",cc);
  11804.         if ( cc < 0 )
  11805.             netclos();
  11806.         return(cc);
  11807.     }
  11808.  
  11809.     if (nstored >= len) {
  11810.         if ( buf ) {
  11811.             debug(F111,"krb4_des_read (nstored >= len)","nstored",nstored);
  11812.             memcpy(buf, store_ptr, len);        /* safe */
  11813.             store_ptr += len;
  11814.             nstored -= len;
  11815.             return(len);
  11816.         } else
  11817.             return(0);
  11818.     } else if (nstored) {
  11819.         if ( buf ) {
  11820.             debug(F111,"krb4_des_read (nstored)","nstored",nstored);
  11821.             memcpy(buf, store_ptr, nstored);    /* safe */
  11822.             nreturned += nstored;
  11823.             buf += nstored;
  11824.             len -= nstored;
  11825.             nstored = 0;
  11826.         } else
  11827.             return(0);
  11828.     }
  11829.  
  11830.     /* We're fetching the length which is MSB first, and the MSB
  11831.     has to be zero unless the client is sending more than 2^24
  11832.     (16M) bytes in a single write (which is why this code is in
  11833.     rlogin but not rcp or rsh.) The only reasons we'd get something
  11834.     other than zero are:
  11835.     -- corruption of the tcp stream (which will show up when
  11836.     everything else is out of sync too)
  11837.     -- un-caught Berkeley-style "pseudo out-of-band data" which
  11838.     happens any time the user hits ^C twice.
  11839.     The latter is *very* common, as shown by an 'rlogin -x -d'
  11840.     using the CNS V4 rlogin.         Mark EIchin 1/95
  11841.     */
  11842.     debug(F110,"krb4_des_read",
  11843.           "about to call net_read() this will block",
  11844.           0
  11845.           );
  11846.     do {
  11847.         cc = net_read(fd, &c, 1);
  11848.         debug(F111,"net_read","chars read",cc);
  11849.         if (cc <= 0) {
  11850.             netclos();
  11851.             return(-1);
  11852.         }
  11853.         if (cc != 1) return 0; /* read error */
  11854.         if (cc == 1) {
  11855.             if (c == 0) gotzero = 1;
  11856.         }
  11857.     } while (!gotzero);
  11858.  
  11859.     debug(F110,"krb4_des_read","gotzero",0);
  11860.     cc = net_read(fd, &c, 1);
  11861.     debug(F111,"net_read","chars read",cc);
  11862.     if (cc < 0) {
  11863.         netclos();
  11864.         return(-1);
  11865.     } else if ( cc != 1 )
  11866.         return(0);
  11867.     net_len = c;
  11868.     cc = net_read(fd, &c, 1);
  11869.     debug(F111,"net_read","chars read",cc);
  11870.     if (cc < 0) {
  11871.         netclos();
  11872.         return(-1);
  11873.     } else if ( cc != 1 )
  11874.         return(0);
  11875.     net_len = (net_len << 8) | c;
  11876.     debug(F111,"net_read","chars read",cc);
  11877.     cc = net_read(fd, &c, 1);
  11878.     if (cc < 0) {
  11879.         netclos();
  11880.         return(-1);
  11881.     } else if ( cc != 1 )
  11882.         return(0);
  11883.     net_len = (net_len << 8) | c;
  11884.     debug(F111,"krb4_des_read","net_len",net_len);
  11885.  
  11886.     /* Note: net_len is unsigned */
  11887.     if (net_len > sizeof(des_inbuf)) {
  11888.         /* XXX preposterous length, probably out of sync.
  11889.         act as if pipe closed */
  11890.         return(0);
  11891.     }
  11892.     /* the writer tells us how much real data we are getting, but
  11893.     we need to read the pad bytes (8-byte boundary) */
  11894. #ifndef roundup
  11895. #define roundup(x,y) ((((x)+(y)-1)/(y))*(y))
  11896. #endif /* roundup */
  11897.     rd_len = roundup(net_len, 8);
  11898.     debug(F111,"krb4_des_read","rd_len",rd_len);
  11899.     cc = net_read(fd, des_inbuf, rd_len);
  11900.     debug(F111,"net_read","chars read",cc);
  11901.     if (cc < 0) {
  11902.         netclos();
  11903.         return(-1);
  11904.     } else if ( cc != rd_len )
  11905.         return(0);
  11906.  
  11907.     hexdump("krb4_des_read des_inbuf",des_inbuf,8);
  11908. #ifdef CK_ENCRYPTION
  11909. #ifdef KRB524
  11910.     (void) des_pcbc_encrypt(des_inbuf,
  11911.                              storage,
  11912.                              (net_len < 8) ? 8 : net_len,
  11913.                              k4_sched,
  11914.                              cred.session,
  11915.                              DECRYPT);
  11916. #else /* KRB524 */
  11917.     (void) des_pcbc_encrypt((Block *)des_inbuf,
  11918.                              (Block *)storage,
  11919.                              (net_len < 8) ? 8 : net_len,
  11920.                              k4_sched,
  11921.                              &cred.session,
  11922.                              DECRYPT);
  11923. #endif /* KRB524 */
  11924. #endif /* ENCRYPTION */
  11925.     hexdump("krb4_des_read storage",storage,8);
  11926.  
  11927.     /*
  11928.     * when the cleartext block is < 8 bytes, it is "right-justified"
  11929.     * in the block, so we need to adjust the pointer to the data
  11930.     */
  11931.     if (net_len < 8)
  11932.         store_ptr = storage + 8 - net_len;
  11933.     else
  11934.         store_ptr = storage;
  11935.     nstored = net_len;
  11936.  
  11937.     if ( !buf )
  11938.         return(0);
  11939.  
  11940.     if (nstored > len) {
  11941.         memcpy(buf, store_ptr, len);            /* safe */
  11942.         nreturned += len;
  11943.         store_ptr += len;
  11944.         nstored -= len;
  11945.     } else {
  11946.         memcpy(buf, store_ptr, nstored);        /* safe */
  11947.         nreturned += nstored;
  11948.         nstored = 0;
  11949.     }
  11950.  
  11951.     debug(F111,"net_read","nreturned",nreturned);
  11952.     return(nreturned);
  11953. }
  11954.  
  11955. int
  11956. krb4_des_write(fd, buf, len)
  11957. int fd;
  11958. char *buf;
  11959. int len;
  11960. {
  11961.     static char garbage_buf[8];
  11962.     unsigned char *len_buf = (unsigned char *) des_outpkt;
  11963.     int cc;
  11964.  
  11965.     debug(F111,"krb4_des_write","rlog_encrypt",rlog_encrypt);
  11966.     if ( !rlog_encrypt ) {
  11967.         cc = net_write(fd, buf, len);
  11968.         debug(F111,"net_write","chars written",cc);
  11969.         return(cc);
  11970.     }
  11971.  
  11972.     /*
  11973.     * pcbc_encrypt outputs in 8-byte (64 bit) increments
  11974.     *
  11975.     * it zero-fills the cleartext to 8-byte padding,
  11976.     * so if we have cleartext of < 8 bytes, we want
  11977.     * to insert random garbage before it so that the ciphertext
  11978.     * differs for each transmission of the same cleartext.
  11979.     * if len < 8 - sizeof(long), sizeof(long) bytes of random
  11980.     * garbage should be sufficient; leave the rest as-is in the buffer.
  11981.     * if len > 8 - sizeof(long), just garbage fill the rest.
  11982.     */
  11983.     if (len < 8) {
  11984.         random_confounder(8 - len, garbage_buf);
  11985.         /* this "right-justifies" the data in the buffer */
  11986.         (void) memcpy(garbage_buf + 8 - len, buf, len); /* safe */
  11987.         hexdump("krb4_des_write garbage_buf",garbage_buf,8);
  11988.     } else
  11989.         hexdump("krb4_des_write buf",buf,8);
  11990. #ifdef CK_ENCRYPTION
  11991. #ifdef KRB524
  11992.     (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
  11993.                              des_outpkt+4,
  11994.                              (len < 8) ? 8 : len,
  11995.                              k4_sched,
  11996.                              cred.session,
  11997.                              ENCRYPT);
  11998. #else /* KRB524 */
  11999.     (void) des_pcbc_encrypt((Block *)((len < 8) ? garbage_buf : buf),
  12000.                              (Block *)(des_outpkt+4),
  12001.                              (len < 8) ? 8 : len,
  12002.                              k4_sched,
  12003.                              &cred.session,
  12004.                              ENCRYPT);
  12005. #endif /* KRB524 */
  12006. #endif /* ENCRYPTION */
  12007.     if ( len < 8 )
  12008.         hexdump("krb4_des_write (post pcbc) garbage_buf",garbage_buf,8);
  12009.     else
  12010.         hexdump("krb4_des_write (post pcbc) buf",buf,8);
  12011.     hexdump("krb4_des_write (des_outpkt+4)",(des_outpkt+4),8);
  12012.  
  12013.     /* tell the other end the real amount, but send an 8-byte padded
  12014.     packet */
  12015.     len_buf[0] = (len & 0xff000000) >> 24;
  12016.     len_buf[1] = (len & 0xff0000) >> 16;
  12017.     len_buf[2] = (len & 0xff00) >> 8;
  12018.     len_buf[3] = (len & 0xff);
  12019.     hexdump("krb4_des_write des_outpkt len",des_outpkt,12);
  12020.     cc = net_write(fd, des_outpkt, roundup(len,8)+4);
  12021.     debug(F111,"net_write","chars written",cc);
  12022.     return(len);
  12023. }
  12024. #endif /* KRB4 */
  12025. #endif /* RLOGCODE */
  12026.  
  12027. #ifdef KRB524
  12028. #ifndef OS2
  12029. /* The following functions are missing from the compatibility library */
  12030. const char *
  12031. krb_get_err_text_entry(r) int r;
  12032. {
  12033.     extern char krb_err_text[];
  12034.     return(krb_err_txt[r]);
  12035. }
  12036. #endif /* OS2 */
  12037. #endif /* KRB524 */
  12038. #endif /* CK_KERBEROS */
  12039.  
  12040. #ifdef CK_KERBEROS
  12041. #ifdef KRB5_U2U
  12042. /* Kerberos 5 User to User Client */
  12043. int
  12044. k5_user_to_user_client_auth()
  12045. {
  12046.     extern int ttyfd;
  12047.     register int retval, i;
  12048.     char **srealms;             /* realm(s) of server */
  12049.     char *princ;                /* principal in credentials cache */
  12050.     krb5_ccache cc;
  12051.     krb5_creds creds, *new_creds;
  12052.     krb5_data reply, msg, msgtext, princ_data;
  12053.     krb5_ticket * ticket = NULL;
  12054.  
  12055.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12056.     {
  12057.         com_err("uu-client", retval, "getting credentials cache");
  12058.         return(-1);
  12059.     }
  12060.  
  12061.     memset ((char*)&creds, 0, sizeof(creds));
  12062.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12063.     {
  12064.         com_err("uu-client", retval, "getting principal name");
  12065.         return(-1);
  12066.     }
  12067.  
  12068.     if (retval = krb5_get_host_realm(k5_context, szHostName, &srealms))
  12069.     {
  12070.         com_err("uu-client", retval, "getting realms for \"%s\"", szHostName);
  12071.         return(-1);
  12072.     }
  12073.  
  12074.     if (retval = krb5_build_principal_ext(k5_context, &creds.server,
  12075.                                           krb5_princ_realm(k5_context,
  12076.                                                          creds.client)->length,
  12077.                                           krb5_princ_realm(k5_context,
  12078.                                                          creds.client)->data,
  12079.                                           6, "krbtgt",
  12080.                                           krb5_princ_realm(k5_context,
  12081.                                                          creds.client)->length,
  12082.                                           krb5_princ_realm(k5_context,
  12083.                                                          creds.client)->data,
  12084.                                           0))
  12085.     {
  12086.         com_err("uu-client", retval, "setting up tgt server name");
  12087.         return(-1);
  12088.     }
  12089.  
  12090.     /* Get TGT from credentials cache */
  12091.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_CACHED, cc,
  12092.                                        &creds, &new_creds))
  12093.     {
  12094.         com_err("uu-client", retval, "getting TGT");
  12095.         return(-1);
  12096.     }
  12097.  
  12098.     if (retval = krb5_unparse_name(k5_context, creds.client, &princ)) {
  12099.         com_err("uu-client", retval, "printing principal name");
  12100.         return(-1);
  12101.     }
  12102.     i = strlen(princ) + 1;
  12103.     princ_data.data = princ;
  12104.     princ_data.length = i;              /* include null terminator for
  12105.                                            server's convenience */
  12106.     retval = krb5_write_message(k5_context,
  12107.                                 (krb5_pointer) &ttyfd, &princ_data);
  12108.     if (retval)
  12109.     {
  12110.         com_err("uu-client", retval, "sending principal name to server");
  12111.         return(-1);
  12112.     }
  12113.     krb5_free_unparsed_name(k5_context,princ);
  12114.  
  12115.     retval = krb5_write_message(k5_context,
  12116.                                 (krb5_pointer) &ttyfd, &new_creds->ticket);
  12117.     if (retval)
  12118.     {
  12119.         com_err("uu-client", retval, "sending ticket to server");
  12120.         return(-1);
  12121.     }
  12122.  
  12123.     retval = krb5_read_message(k5_context, (krb5_pointer) &ttyfd, &reply);
  12124.     if (retval)
  12125.     {
  12126.         com_err("uu-client", retval, "reading reply from server");
  12127.         return(-1);
  12128.     }
  12129.  
  12130.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12131.         com_err("uu-client", retval, "initializing the auth_context");
  12132.         return(-1);
  12133.     }
  12134.  
  12135.     if (!krb5_d_no_addresses) {
  12136.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12137.                         KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12138.                         KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12139.         com_err("uu-client", retval, "generating addrs for auth_context");
  12140.         return(-1);
  12141.       }
  12142.     }
  12143.  
  12144.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12145.                                         KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12146.         com_err("uu-client", retval, "initializing the auth_context flags");
  12147.         return(-1);
  12148.     }
  12149.  
  12150.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12151.                                               &new_creds->keyblock)) {
  12152.         com_err("uu-client", retval, "setting useruserkey for authcontext");
  12153.         return(-1);
  12154.     }
  12155.  
  12156.     /* read the ap_req to get the session key */
  12157.     retval = krb5_rd_req(k5_context, &auth_context, &reply,
  12158.                           NULL, NULL, NULL, &ticket);
  12159.     if (retval) {
  12160.         com_err("uu-client", retval, "reading AP_REQ from server");
  12161.         return(-1);
  12162.     }
  12163.  
  12164.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12165.         return(-1);
  12166.  
  12167.     if ( strcmp("Kermit implements Kerberos 5 User to User",msg.data) )
  12168.         return(-1);
  12169.     krb5_free_data_contents(k5_context,&msg);
  12170.  
  12171.     msgtext.data = "As do I! :-)";
  12172.     msgtext.length = strlen(msgtext.data)+1;
  12173.  
  12174.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12175.         return(-1);
  12176.  
  12177.     if (retval = krb5_unparse_name(k5_context,
  12178. #ifdef HEIMDAL
  12179.                                     ticket->client,
  12180. #else /* HEIMDAL */
  12181.                                     ticket->enc_part2->client,
  12182. #endif /* HEIMDAL */
  12183.                                     &princ))
  12184.         com_err("uu-client", retval, "while unparsing client name");
  12185.     else {
  12186.         ckstrncpy(szUserNameAuthenticated,princ,UIDBUFLEN);
  12187.         validUser = AUTH_VALID;
  12188.         authentication_version = AUTHTYPE_KERBEROS_V5;
  12189.         if ( !quiet )
  12190.             printf("Peer name is \"%s\"\n", princ);
  12191.         krb5_free_unparsed_name(k5_context,princ);
  12192.     }
  12193.     return 0;
  12194. }
  12195.  
  12196. /* Kerberos 5 User to User Server */
  12197.  
  12198. int
  12199. k5_user_to_user_server_auth()
  12200. {
  12201.     krb5_data pname_data, tkt_data;
  12202.     int retval;
  12203.     krb5_creds creds, *new_creds;
  12204.     krb5_ccache cc;
  12205.     krb5_data msg, msgtext;
  12206.     extern int ttyfd;
  12207.  
  12208.     if (retval = krb5_read_message(k5_context,
  12209.                                    (krb5_pointer) &ttyfd, &pname_data)) {
  12210.         com_err ("uu-server", retval, "reading pname");
  12211.         return(-1);
  12212.     }
  12213.     /* client sends it already null-terminated. */
  12214.     if ( !quiet )
  12215.         printf ("Peer name is \"%s\".\n", pname_data.data);
  12216.     ckstrncpy(szUserNameAuthenticated,pname_data.data,UIDBUFLEN);
  12217.     validUser = AUTH_VALID;
  12218.     authentication_version = AUTHTYPE_KERBEROS_V5;
  12219.  
  12220.     if (retval = krb5_read_message(k5_context,
  12221.                                    (krb5_pointer) &ttyfd, &tkt_data)) {
  12222.         com_err ("uu-server", retval, "reading ticket data");
  12223.         return(-1);
  12224.     }
  12225.  
  12226.     if (retval = k5_get_ccache(k5_context,&cc,NULL))
  12227.     {
  12228.         com_err("uu-server", retval, "getting credentials cache");
  12229.         return(-1);
  12230.     }
  12231.  
  12232.     memset ((char*)&creds, 0, sizeof(creds));
  12233.     if (retval = krb5_cc_get_principal(k5_context, cc, &creds.client))
  12234.     {
  12235.         com_err("uu-server", retval, "getting principal name");
  12236.         return(-1);
  12237.     }
  12238.  
  12239.     if (retval = krb5_parse_name(k5_context, pname_data.data, &creds.server))
  12240.     {
  12241.         com_err("uu-server", retval, "parsing client name");
  12242.         return(-1);
  12243.     }
  12244.     creds.second_ticket = tkt_data;
  12245.  
  12246.     if (retval = krb5_get_credentials(k5_context, KRB5_GC_USER_USER,
  12247.                                        cc, &creds, &new_creds))
  12248.     {
  12249.         com_err("uu-server", retval, "getting user-user ticket");
  12250.         return(-1);
  12251.     }
  12252.  
  12253.     /* send a ticket/authenticator to the other side, so it can get the key
  12254.        we're using for the krb_safe below. */
  12255.  
  12256.     if (retval = krb5_auth_con_init(k5_context, &auth_context)) {
  12257.         com_err("uu-server", retval, "making auth_context");
  12258.         return(-1);
  12259.     }
  12260.  
  12261.     if (retval = krb5_auth_con_setflags(k5_context, auth_context,
  12262.                                          KRB5_AUTH_CONTEXT_DO_SEQUENCE)) {
  12263.         com_err("uu-server", retval, "initializing the auth_context flags");
  12264.         return(-1);
  12265.     }
  12266.  
  12267.     if (!krb5_d_no_addresses) {
  12268.       if (retval = krb5_auth_con_genaddrs(k5_context, auth_context, ttyfd,
  12269.                                 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
  12270.                                 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) {
  12271.         com_err("uu-server", retval, "generating addrs for auth_context");
  12272.         return(-1);
  12273.       }
  12274.     }
  12275.  
  12276.     if (retval = krb5_auth_con_setuseruserkey(k5_context, auth_context,
  12277.                                               &new_creds->keyblock)) {
  12278.         com_err("uu-server", retval, "setting useruserkey for authcontext");
  12279.         return(-1);
  12280.     }
  12281.  
  12282.     if (retval = krb5_mk_req_extended(k5_context, &auth_context,
  12283.                                       AP_OPTS_USE_SESSION_KEY |
  12284.                                       AP_OPTS_MUTUAL_REQUIRED,
  12285.                                       NULL, new_creds, &msg)) {
  12286.         com_err("uu-server", retval, "making AP_REQ");
  12287.         return(-1);
  12288.     }
  12289.     retval = krb5_write_message(k5_context, (krb5_pointer) &ttyfd, &msg);
  12290.     if (retval) {
  12291.         com_err("uu-server", retval, "writing message to client");
  12292.         return(-1);
  12293.     }
  12294.     krb5_free_data_contents(k5_context,&msg);
  12295.  
  12296.     msgtext.data = "Kermit implements Kerberos 5 User to User";
  12297.     msgtext.length = strlen(msgtext.data)+1;
  12298.  
  12299.     if (k5_u2u_write_msg(k5_context,&msgtext) < 0)
  12300.         return(-1);
  12301.  
  12302.     if (k5_u2u_read_msg(k5_context,&msg) < 0)
  12303.         return(-1);
  12304.  
  12305.     if ( strcmp("As do I! :-)",msg.data) )
  12306.         return(-1);
  12307.     krb5_free_data_contents(k5_context,&msg);
  12308.  
  12309.  
  12310.     return(0);
  12311. }
  12312.  
  12313. int
  12314. k5_u2u_read_msg(krb5_context context, int fd, krb5_data * msg)
  12315. {
  12316.     int retval;
  12317.     krb5_data reply;
  12318.  
  12319.     retval = krb5_read_message(context, (krb5_pointer) &fd, &reply);
  12320.     if (retval)
  12321.     {
  12322.         com_err("uu-client", retval, "reading reply");
  12323.         return(-1);
  12324.     }
  12325.  
  12326.     if (retval = krb5_rd_priv(context, auth_context, &reply, msg, NULL)) {
  12327.         com_err("uu-client", retval, "decoding reply");
  12328.         return(-1);
  12329.     }
  12330.     return(0);
  12331. }
  12332.  
  12333. int
  12334. k5_u2u_write_msg(krb5_context context, int fd, krb5_data * msgtext)
  12335. {
  12336.     int retval;
  12337.     krb5_data msg;
  12338.  
  12339.     if (retval = krb5_mk_priv(k5_context, auth_context, msgtext, &msg, NULL))
  12340.     {
  12341.         com_err("uu-server", retval, "encoding message");
  12342.         return(-1);
  12343.     }
  12344.  
  12345.     retval = krb5_write_message(k5_context, (krb5_pointer) &fd, &msg);
  12346.     krb5_free_data_contents(k5_context,&msg);
  12347.     if (retval)
  12348.     {
  12349.         com_err("uu-server", retval, "writing message");
  12350.         return(-1);
  12351.     }
  12352.     return(0);
  12353. }
  12354.  
  12355. int
  12356. krb5_u2u_avail(fd)
  12357.     int fd;
  12358. {
  12359.     return(nstored);
  12360. }
  12361.  
  12362. int
  12363. krb5_u2u_read(fd, buf, len)
  12364.      int fd;
  12365.      register char *buf;
  12366.      int len;
  12367. {
  12368.     int nreturned = 0;
  12369.     krb5_data msg;
  12370.  
  12371.     debug(F111,"krb5_u2u_read","len",len);
  12372.  
  12373.     if ( !buf )
  12374.         return(0);
  12375.  
  12376.     if (nstored >= len) {
  12377.         memcpy(buf, store_ptr, len);        /* safe */
  12378.         store_ptr += len;
  12379.         nstored -= len;
  12380.         return(len);
  12381.     } else if (nstored) {
  12382.         memcpy(buf, store_ptr, nstored);    /* safe */
  12383.         nreturned += nstored;
  12384.         buf += nstored;
  12385.         len -= nstored;
  12386.         nstored = 0;
  12387.     }
  12388.  
  12389.     if (k5_u2u_read_msg(k5_context, fd, &msg) < 0)
  12390.         return(-1);
  12391.  
  12392.     if ( msg.length <= len ) {
  12393.         memcpy(buf, msg.data, msg.length);
  12394.         nreturned += msg.length;
  12395.         nstored = 0;
  12396.     } else {
  12397.         memcpy(buf, msg.data, len);
  12398.         nreturned += len;
  12399.  
  12400.         if ( msg.length - len < sizeof(storage) ) {
  12401.             store_ptr = storage;
  12402.             nstored = msg.length - len;
  12403.             memcpy(storage,msg.data+len,nstored);
  12404.         } else {
  12405.             nstored = 0;
  12406.             return(-1);
  12407.         }
  12408.     }
  12409.     return(nreturned);
  12410. }
  12411.  
  12412. int
  12413. krb5_u2u_write(fd, buf, len)
  12414.      int fd;
  12415.      char *buf;
  12416.      int len;
  12417. {
  12418.     krb5_data msg;
  12419.  
  12420.     msg.length = len;
  12421.     msg.data = buf;
  12422.  
  12423.     if ( k5_u2u_write_msg(k5_context, fd, &msg) < 0 )
  12424.         return(-1);
  12425.     else
  12426.         return(len);
  12427. }
  12428.  
  12429. #endif /* KRB5_U2U */
  12430. #endif /* CK_KERBEROS */
  12431.  
  12432. #ifdef CK_FORWARD_X
  12433. /*
  12434.  
  12435. Copyright (c) 1988  X Consortium
  12436.  
  12437. Permission is hereby granted, free of charge, to any person obtaining a copy
  12438. of this software and associated documentation files (the "Software"), to deal
  12439. in the Software without restriction, including without limitation the rights
  12440. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12441. copies of the Software, and to permit persons to whom the Software is
  12442. furnished to do so, subject to the following conditions:
  12443.  
  12444. The above copyright notice and this permission notice shall be included in
  12445. all copies or substantial portions of the Software.
  12446.  
  12447. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12448. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12449. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  12450. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  12451. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  12452. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12453.  
  12454. Except as contained in this notice, the name of the X Consortium shall not be
  12455. used in advertising or otherwise to promote the sale, use or other dealings
  12456. in this Software without prior written authorization from the X Consortium.
  12457.  
  12458. */
  12459. /*  Modified for stand-alone compiling by
  12460.  *  Peter 'Luna' Runestig <peter@runestig.com>
  12461.  */
  12462.  
  12463. #include <stdlib.h>
  12464. #include <string.h>
  12465. #include <fcntl.h>
  12466. #include <sys/stat.h>
  12467. #include <errno.h>
  12468. #include <time.h>
  12469. #define Time_t time_t
  12470.  
  12471. void
  12472. XauDisposeAuth (auth)
  12473. Xauth   *auth;
  12474. {
  12475.     if (auth) {
  12476.         if (auth->address) (void) free (auth->address);
  12477.         if (auth->number) (void) free (auth->number);
  12478.         if (auth->name) (void) free (auth->name);
  12479.         if (auth->data) {
  12480.             (void) bzero (auth->data, auth->data_length);
  12481.             (void) free (auth->data);
  12482.         }
  12483.         free ((char *) auth);
  12484.     }
  12485.     return;
  12486. }
  12487.  
  12488. char *
  12489. XauFileName ()
  12490. {
  12491.     char *slashDotXauthority = "/.Xauthority";
  12492.     char    *name;
  12493.     static char *buf=NULL;
  12494.     static int  bsize=0;
  12495.     int     size, namelen;
  12496.     extern char * tn_fwdx_xauthority;
  12497.  
  12498.     if ( tn_fwdx_xauthority )
  12499.         return(tn_fwdx_xauthority);
  12500.  
  12501.     if (name = getenv ("XAUTHORITY"))
  12502.         return(name);
  12503.     name = zhome();
  12504.     if ( !name )
  12505.         return(NULL);
  12506.     namelen = strlen (name);
  12507.     size = namelen + strlen(slashDotXauthority) + 1;
  12508.     if (size > bsize) {
  12509.         if (buf)
  12510.             free (buf);
  12511.         buf = malloc ((unsigned) size);
  12512.         if (!buf)
  12513.             return 0;
  12514.         bsize = size;
  12515.     }
  12516.     ckstrncpy (buf, name, bsize);
  12517.     if ( name[namelen-1] != '/'
  12518. #ifdef OS2
  12519.          && name[namelen-1] != '\\'
  12520. #endif /* OS2 */
  12521.          )
  12522.         ckstrncat (buf, slashDotXauthority, bsize);
  12523.     else
  12524.         ckstrncat (buf, &slashDotXauthority[1], bsize);
  12525.     return(buf);
  12526. }
  12527.  
  12528. static int
  12529. binaryEqual (a, b, len)
  12530. register char   *a, *b;
  12531. register int    len;
  12532. {
  12533.     while (len--)
  12534.         if (*a++ != *b++)
  12535.             return 0;
  12536.     return 1;
  12537. }
  12538.  
  12539. #ifndef R_OK
  12540. #define R_OK 04
  12541. #endif /* R_OK */
  12542.  
  12543. Xauth *
  12544. XauGetAuthByAddr (family, address_length, address,
  12545.                           number_length, number,
  12546.                           name_length, name)
  12547. unsigned int    family;
  12548. unsigned int    address_length;
  12549. const char      *address;
  12550. unsigned int    number_length;
  12551. const char      *number;
  12552. unsigned int    name_length;
  12553. const char      *name;
  12554. {
  12555.     FILE    *auth_file;
  12556.     char    *auth_name;
  12557.     Xauth   *entry;
  12558.  
  12559.     auth_name = XauFileName();
  12560.     if (!auth_name)
  12561.         return 0;
  12562.     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
  12563.         return 0;
  12564.     auth_file = fopen (auth_name, "rb");
  12565.     if (!auth_file)
  12566.         return 0;
  12567.     for (;;) {
  12568.         entry = XauReadAuth (auth_file);
  12569.         if (!entry)
  12570.             break;
  12571.         /*
  12572.          * Match when:
  12573.          *   either family or entry->family are FamilyWild or
  12574.          *    family and entry->family are the same
  12575.          *  and
  12576.          *   either address or entry->address are empty or
  12577.          *    address and entry->address are the same
  12578.          *  and
  12579.          *   either number or entry->number are empty or
  12580.          *    number and entry->number are the same
  12581.          *  and
  12582.          *   either name or entry->name are empty or
  12583.          *    name and entry->name are the same
  12584.          */
  12585.  
  12586. /*      if ((family == FamilyWild || entry->family == FamilyWild ||
  12587.              (entry->family == family &&
  12588.               address_length == entry->address_length &&
  12589.               binaryEqual (entry->address, address, (int)address_length))) &&
  12590.             (number_length == 0 || entry->number_length == 0 ||
  12591.              (number_length == entry->number_length &&
  12592.               binaryEqual (entry->number, number, (int)number_length))) &&
  12593.             (name_length == 0 || entry->name_length == 0 ||
  12594.              (entry->name_length == name_length &&
  12595.               binaryEqual (entry->name, name, (int)name_length)))) */
  12596.         /* the original matching code above doesn't seem to meet the matching
  12597.          * algorithm, it doesn't check if "address_length == 0 ||
  12598.          * entry->address_length == 0". / Luna 2000-02-09
  12599.          */
  12600.         if ((family == FamilyWild || entry->family == FamilyWild ||
  12601.               entry->family == family) &&
  12602.             (address_length == 0 || entry->address_length == 0 ||
  12603.               (address_length == entry->address_length &&
  12604.               binaryEqual (entry->address, address, (int)address_length))) &&
  12605.             (number_length == 0 || entry->number_length == 0 ||
  12606.              (number_length == entry->number_length &&
  12607.               binaryEqual (entry->number, number, (int)number_length))) &&
  12608.             (name_length == 0 || entry->name_length == 0 ||
  12609.              (entry->name_length == name_length &&
  12610.               binaryEqual (entry->name, name, (int)name_length))))
  12611.             break;
  12612.         XauDisposeAuth (entry);
  12613.     }
  12614.     (void) fclose (auth_file);
  12615.     return entry;
  12616. }
  12617.  
  12618. static int
  12619. read_short (shortp, file)
  12620. unsigned short  *shortp;
  12621. FILE            *file;
  12622. {
  12623.     unsigned char   file_short[2];
  12624.  
  12625.     if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12626.         return 0;
  12627.     *shortp = file_short[0] * 256 + file_short[1];
  12628.     return 1;
  12629. }
  12630.  
  12631. static int
  12632. read_counted_string (countp, stringp, file)
  12633. unsigned short  *countp;
  12634. char    **stringp;
  12635. FILE    *file;
  12636. {
  12637.     unsigned short  len;
  12638.     char            *data;
  12639.  
  12640.     if (read_short (&len, file) == 0)
  12641.         return 0;
  12642.     if (len == 0) {
  12643.         data = 0;
  12644.     } else {
  12645.         data = malloc ((unsigned) len);
  12646.         if (!data)
  12647.             return 0;
  12648.         if (fread (data, (int) sizeof (char), (int) len, file) != len) {
  12649.             bzero (data, len);
  12650.             free (data);
  12651.             return 0;
  12652.         }
  12653.     }
  12654.     *stringp = data;
  12655.     *countp = len;
  12656.     return 1;
  12657. }
  12658.  
  12659. Xauth *
  12660. XauReadAuth (auth_file)
  12661. FILE    *auth_file;
  12662. {
  12663.     Xauth   local;
  12664.     Xauth   *ret;
  12665.  
  12666.     if (read_short (&local.family, auth_file) == 0)
  12667.         return 0;
  12668.     if (read_counted_string (&local.address_length,
  12669.                              &local.address, auth_file) == 0)
  12670.         return 0;
  12671.     if (read_counted_string (&local.number_length,
  12672.                              &local.number, auth_file) == 0) {
  12673.         if (local.address) free (local.address);
  12674.         return 0;
  12675.     }
  12676.     if (read_counted_string (&local.name_length,
  12677.                              &local.name, auth_file) == 0) {
  12678.         if (local.address) free (local.address);
  12679.         if (local.number) free (local.number);
  12680.         return 0;
  12681.     }
  12682.     if (read_counted_string (&local.data_length,
  12683.                              &local.data, auth_file) == 0) {
  12684.         if (local.address) free (local.address);
  12685.         if (local.number) free (local.number);
  12686.         if (local.name) free (local.name);
  12687.         return 0;
  12688.     }
  12689.     ret = (Xauth *) malloc (sizeof (Xauth));
  12690.     if (!ret) {
  12691.         if (local.address) free (local.address);
  12692.         if (local.number) free (local.number);
  12693.         if (local.name) free (local.name);
  12694.         if (local.data) {
  12695.             bzero (local.data, local.data_length);
  12696.             free (local.data);
  12697.         }
  12698.         return 0;
  12699.     }
  12700.     *ret = local;
  12701.     return ret;
  12702. }
  12703.  
  12704. static int
  12705. write_short (s, file)
  12706. unsigned short  s;
  12707. FILE            *file;
  12708. {
  12709.     unsigned char   file_short[2];
  12710.  
  12711.     file_short[0] = (s & (unsigned)0xff00) >> 8;
  12712.     file_short[1] = s & 0xff;
  12713.     if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
  12714.         return 0;
  12715.     return 1;
  12716. }
  12717.  
  12718. static int
  12719. write_counted_string (count, string, file)
  12720. unsigned short  count;
  12721. char    *string;
  12722. FILE    *file;
  12723. {
  12724.     if (write_short (count, file) == 0)
  12725.         return 0;
  12726.     if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
  12727.         return 0;
  12728.     return 1;
  12729. }
  12730.  
  12731. int
  12732. XauWriteAuth (auth_file, auth)
  12733. FILE    *auth_file;
  12734. Xauth   *auth;
  12735. {
  12736.     if (write_short (auth->family, auth_file) == 0)
  12737.         return 0;
  12738.     if (write_counted_string (auth->address_length,
  12739.                               auth->address, auth_file) == 0)
  12740.         return 0;
  12741.     if (write_counted_string (auth->number_length,
  12742.                               auth->number, auth_file) == 0)
  12743.         return 0;
  12744.     if (write_counted_string (auth->name_length, auth->name, auth_file) == 0)
  12745.         return 0;
  12746.     if (write_counted_string (auth->data_length, auth->data, auth_file) == 0)
  12747.         return 0;
  12748.     return 1;
  12749. }
  12750.  
  12751. #ifdef KRB5
  12752. #ifdef K5_XAUTH
  12753. /*
  12754.  * functions to encode/decode Kerberos V5 principals
  12755.  * into something that can be reasonable spewed over
  12756.  * the wire
  12757.  *
  12758.  * Author: Tom Yu <tlyu@MIT.EDU>
  12759.  *
  12760.  * Still needs to be fixed up wrt signed/unsigned lengths, but we'll worry
  12761.  * about that later.
  12762.  */
  12763.  
  12764. /*
  12765.  * XauKrb5Encode
  12766.  *
  12767.  * this function encodes the principal passed to it in a format that can
  12768.  * easily be dealt with by stuffing it into an X packet.  Encoding is as
  12769.  * follows:
  12770.  *   length count of the realm name
  12771.  *   realm
  12772.  *   component count
  12773.  *   length of component
  12774.  *   actual principal component
  12775.  *   etc....
  12776.  *
  12777.  * Note that this function allocates a hunk of memory, which must be
  12778.  * freed to avoid nasty memory leak type things.  All counts are
  12779.  * byte-swapped if needed. (except for the total length returned)
  12780.  *
  12781.  * nevermind.... stuffing the encoded packet in net byte order just to
  12782.  * always do the right thing.  Don't have to frob with alignment that way.
  12783.  */
  12784. int
  12785. XauKrb5Encode(princ, outbuf)
  12786.     krb5_principal princ;       /* principal to encode */
  12787.     krb5_data *outbuf;          /* output buffer */
  12788. {
  12789.     CARD16 i, numparts, totlen = 0, plen, rlen;
  12790.     char *cp, *pdata;
  12791.  
  12792.     rlen = krb5_princ_realm(princ)->length;
  12793.     numparts = krb5_princ_size(princ);
  12794.     totlen = 2 + rlen + 2;      /* include room for realm length
  12795.                                    and component count */
  12796.     for (i = 0; i < numparts; i++)
  12797.         totlen += krb5_princ_component(princ, i)->length + 2;
  12798.     /* add 2 bytes each time for length */
  12799.     if ((outbuf->data = (char *)malloc(totlen)) == NULL)
  12800.         return -1;
  12801.     cp = outbuf->data;
  12802.     *cp++ = (char)((int)(0xff00 & rlen) >> 8);
  12803.     *cp++ = (char)(0x00ff & rlen);
  12804.     memcpy(cp, krb5_princ_realm(princ)->data, rlen);    /* safe */
  12805.     cp += rlen;
  12806.     *cp++ = (char)((int)(0xff00 & numparts) >> 8);
  12807.     *cp++ = (char)(0x00ff & numparts);
  12808.     for (i = 0; i < numparts; i++)
  12809.     {
  12810.         plen = krb5_princ_component(princ, i)->length;
  12811.         pdata = krb5_princ_component(princ, i)->data;
  12812.         *cp++ = (char)((int)(0xff00 & plen) >> 8);
  12813.         *cp++ = (char)(0x00ff & plen);
  12814.         memcpy(cp, pdata, plen);                        /* safe */
  12815.         cp += plen;
  12816.     }
  12817.     outbuf->length = totlen;
  12818.     return 0;
  12819. }
  12820.  
  12821. /*
  12822.  * XauKrb5Decode
  12823.  *
  12824.  * This function essentially reverses what XauKrb5Encode does.
  12825.  * return value: 0 if okay, -1 if malloc fails, -2 if inbuf format bad
  12826.  */
  12827. int
  12828. XauKrb5Decode(inbuf, princ)
  12829.     krb5_data inbuf;
  12830.     krb5_principal *princ;
  12831. {
  12832.     CARD16 i, numparts, plen, rlen;
  12833.     CARD8 *cp, *pdata;
  12834.  
  12835.     if (inbuf.length < 4)
  12836.     {
  12837.         return -2;
  12838.     }
  12839.     *princ = (krb5_principal)malloc(sizeof (krb5_principal_data));
  12840.     if (*princ == NULL)
  12841.         return -1;
  12842.     bzero(*princ, sizeof (krb5_principal_data));
  12843.     cp = (CARD8 *)inbuf.data;
  12844.     rlen = *cp++ << 8;
  12845.     rlen |= *cp++;
  12846.     if (inbuf.length < 4 + (int)rlen + 2)
  12847.     {
  12848.         krb5_free_principal(*princ);
  12849.         return -2;
  12850.     }
  12851.     krb5_princ_realm(*princ)->data = (char *)malloc(rlen);
  12852.     if (krb5_princ_realm(*princ)->data == NULL)
  12853.     {
  12854.         krb5_free_principal(*princ);
  12855.         return -1;
  12856.     }
  12857.     krb5_princ_realm(*princ)->length = rlen;
  12858.     memcpy(krb5_princ_realm(*princ)->data, cp, rlen);   /* safe */
  12859.     cp += rlen;
  12860.     numparts = *cp++ << 8;
  12861.     numparts |= *cp++;
  12862.     krb5_princ_name(*princ) =
  12863.         (krb5_data *)malloc(numparts * sizeof (krb5_data));
  12864.     if (krb5_princ_name(*princ) == NULL)
  12865.     {
  12866.         krb5_free_principal(*princ);
  12867.         return -1;
  12868.     }
  12869.     krb5_princ_size(*princ) = 0;
  12870.     for (i = 0; i < numparts; i++)
  12871.     {
  12872.         if (cp + 2 > (CARD8 *)inbuf.data + inbuf.length)
  12873.         {
  12874.             krb5_free_principal(*princ);
  12875.             return -2;
  12876.         }
  12877.         plen = *cp++ << 8;
  12878.         plen |= *cp++;
  12879.         if (cp + plen > (CARD8 *)inbuf.data + inbuf.length)
  12880.         {
  12881.             krb5_free_principal(*princ);
  12882.             return -2;
  12883.         }
  12884.         pdata = (CARD8 *)malloc(plen);
  12885.         if (pdata == NULL)
  12886.         {
  12887.             krb5_free_principal(*princ);
  12888.             return -1;
  12889.         }
  12890.         krb5_princ_component(*princ, i)->data = (char *)pdata;
  12891.         krb5_princ_component(*princ, i)->length = plen;
  12892.         memcpy(pdata, cp, plen);        /* safe */
  12893.         cp += plen;
  12894.         krb5_princ_size(*princ)++;
  12895.     }
  12896.     return 0;
  12897. }
  12898. #endif /* K5_XAUTH */
  12899. #endif /* KRB5 */
  12900. #endif /* CK_FORWARD_X */
  12901. #endif /* CK_AUTHENTICATION */
  12902.  
  12903. /* C K _ A U T H _ I N I T
  12904.  * Initialize the Kerberos system for a pending connection
  12905.  *   hostname - a reverse DNS lookup of the hostname when possible
  12906.  *   ipaddr   - the ip address of the host
  12907.  *   username - the name the user wants to connect under not necessarily
  12908.  *              the same as principal
  12909.  *   socket   - the socket handle (ttyfd in Kermit speak)
  12910.  *
  12911.  * Returns: 1 on success and 0 on failure
  12912.  */
  12913.  
  12914. int
  12915. #ifdef CK_ANSIC
  12916. ck_auth_init( char * hostname, char * ipaddr, char * username, int socket )
  12917. #else /* CK_ANSIC */
  12918. ck_auth_init( hostname, ipaddr, username, socket )
  12919.     char * hostname; char * ipaddr; char *username; int socket;
  12920. #endif /* CK_ANSIC */
  12921. {
  12922. #ifdef CK_AUTHENTICATION
  12923. #ifdef OS2
  12924.     if ( !ck_security_loaddll() ) {
  12925.         TELOPT_ME_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  12926.         TELOPT_U_MODE(TELOPT_AUTHENTICATION) = TN_NG_RF;
  12927.         return(0);
  12928.     }
  12929. #endif /* OS2 */
  12930. #endif /* CK_AUTHENTICAITON */
  12931. #ifdef CK_ENCRYPTION
  12932.     if ( !!ck_crypt_is_installed() ) {
  12933.         TELOPT_ME_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  12934.         TELOPT_U_MODE(TELOPT_ENCRYPTION) = TN_NG_RF;
  12935.     }
  12936. #endif /* CK_ENCRYPTION */
  12937.  
  12938.     if (!hostname) hostname = "";
  12939.     if (!ipaddr) ipaddr = "";
  12940.     if (!username) username = "";
  12941.  
  12942.     debug(F110,"ck_auth_init Username",username,0);
  12943.     debug(F110,"ck_auth_init Hostname",hostname,0);
  12944.     debug(F110,"ck_auth_init Ipaddr",ipaddr,0);
  12945.  
  12946.     ckstrncpy( szUserName, username, UIDBUFLEN );
  12947.     ckstrncpy( szHostName, hostname, UIDBUFLEN );
  12948.     ckstrncpy( szIP, ipaddr, 16 );
  12949.     szUserNameRequested[0] = '\0';
  12950.     szUserNameAuthenticated[0] = '\0';
  12951.     validUser = AUTH_REJECT;
  12952.     accept_complete = 0;
  12953.     authentication_version = AUTHTYPE_NULL;
  12954.  
  12955. #ifdef CK_AUTHENTICATION
  12956.     auth_how = 0;
  12957.     auth_crypt = 0;
  12958.     auth_fwd = 0;
  12959.     mutual_complete = 0;
  12960.     if ( sstelnet )
  12961.         str_data[3] = TELQUAL_REPLY;
  12962.     else
  12963.         str_data[3] = TELQUAL_IS;
  12964. #endif /* CK_AUTHENTICATION */
  12965.  
  12966. #ifdef CK_SRP
  12967.     srp_waitresp = 0;
  12968. #endif /* SRP */
  12969.  
  12970. #ifdef CK_KERBEROS
  12971. #ifdef KRB5
  12972.     /* free previous ret_cred  */
  12973.     if ( ret_cred ) {
  12974. #ifdef CK_ENCRYPTION
  12975. #ifdef HEIMDAL
  12976.         if ( k5_session_key == &ret_cred->session)
  12977.             k5_session_key = NULL;
  12978. #else /* HEIMDAL */
  12979.         if ( k5_session_key == &ret_cred->keyblock)
  12980.             k5_session_key = NULL;
  12981. #endif /* HEIMDAL */
  12982. #endif /* CK_ENCRYPTION */
  12983.         krb5_free_creds(k5_context, ret_cred);
  12984.         ret_cred = NULL;
  12985.     }
  12986.     if (k5_ticket) {
  12987.         krb5_free_ticket(k5_context, k5_ticket);
  12988.         k5_ticket = NULL;
  12989.     }
  12990.     /* and context */
  12991.     if ( k5_context ) {
  12992.         krb5_free_context(k5_context);
  12993.         k5_context = NULL;
  12994.     }
  12995.  
  12996.     /* create k5_context */
  12997.     krb5_init_context(&k5_context);
  12998. #ifndef MIT_CURRENT
  12999.     if (k5_context)
  13000.         krb5_init_ets(k5_context);
  13001. #endif /* MIT_CURRENT */
  13002.     memset(&k5_auth,0,sizeof(k5_auth));
  13003.     if (auth_context) {
  13004.         krb5_auth_con_free(k5_context, auth_context);
  13005.         auth_context = 0;
  13006.     }
  13007. #ifdef CK_ENCRYPTION
  13008.     if (k5_session_key) {
  13009.         krb5_free_keyblock(k5_context, k5_session_key);
  13010.         k5_session_key = 0;
  13011.     }
  13012. #endif /* ENCRYPTION */
  13013. #ifdef TLS_VERIFY
  13014.     krb5_tls_verified = 0;
  13015. #endif /* TLS_VERIFY */
  13016. #endif /* KRB5 */
  13017.  
  13018. #ifdef KRB4
  13019. #ifdef CK_ENCRYPTION
  13020.     /* Initialize buffers used for authentication */
  13021.     memset(&k4_session_key, 0, sizeof(k4_session_key));
  13022.     memset(&k4_challenge, 0, sizeof(k4_challenge));
  13023. #endif /* ENCRYPTION */
  13024. #endif /* KRB4 */
  13025.  
  13026.     kstream_destroy();
  13027. #ifdef RLOGCODE
  13028.     rlog_encrypt = 0;
  13029. #endif /* RLOGCODE */
  13030.     nstored = 0;
  13031.     store_ptr = storage;
  13032.     memset(storage,0,sizeof(storage));
  13033.  
  13034.     if (!kstream_create_from_fd(socket, NULL))
  13035.         return(0);
  13036. #endif /* CK_KERBEROS */
  13037.  
  13038.     return(1);
  13039. }
  13040.  
  13041. void
  13042. auth_finished(result) int result; {
  13043.     extern char uidbuf[];
  13044.     extern int sstelnet;
  13045.  
  13046.     validUser = result;
  13047.     switch (result) {
  13048.     case AUTH_REJECT:           /* Rejected */
  13049.         if (sstelnet)
  13050.             uidbuf[0] = '\0';
  13051.         authentication_version = AUTHTYPE_NULL;
  13052.         break;
  13053.     case AUTH_UNKNOWN:          /* We don't know who he is, but he's okay */
  13054.         if (sstelnet)
  13055.             strcpy(uidbuf,"(unknown)");
  13056.         break;
  13057.     case AUTH_OTHER:            /* We know him, but not his name */
  13058.         if (sstelnet)
  13059.             strcpy(uidbuf,"(other)");
  13060.         break;
  13061.     case AUTH_USER:             /* We know he name */
  13062.     case AUTH_VALID:            /* We know him, and he needs no password */
  13063.         if (sstelnet)
  13064.             strcpy(uidbuf,szUserNameRequested);
  13065.         break;
  13066.     }
  13067. }
  13068. #endif /* CK_SECURITY */
  13069.