venus: Remove dedebug
[openafs.git] / src / aklog / klog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include <rx/xdr.h>
17 #include <lock.h>
18 #include <ubik.h>
19 #include <afs/com_err.h>
20 #include <afs/auth.h>
21 #include <afs/afsutil.h>
22 #include <afs/cellconfig.h>
23 #include <afs/ptclient.h>
24 #include <afs/cmd.h>
25 #include <afs/ptuser.h>
26
27 #define KERBEROS_APPLE_DEPRECATED(x)
28 #include <krb5.h>
29
30 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
31 #define USING_MIT 1
32 #endif
33 #ifdef HAVE_KRB5_CREDS_SESSION
34 #define USING_HEIMDAL 1
35 #endif
36
37 #include "skipwrap.h"
38
39 /* This code borrowed heavily from the previous version of log.  Here is the
40    intro comment for that program: */
41
42 /*
43         log -- tell the Andrew Cache Manager your password
44         5 June 1985
45         modified
46         February 1986
47
48         Further modified in August 1987 to understand cell IDs.
49
50         Further modified in October 2006 to understand kerberos 5.
51  */
52
53 /* Current Usage:
54      klog [principal [password]] [-t] [-c cellname] [-k <k5realm>]
55
56      where:
57        principal is of the form 'name' or 'name@cell' which provides the
58           cellname.  See the -c option below.
59        password is the user's password.  This form is NOT recommended for
60           interactive users.
61        -t advises klog to write a Kerberos style ticket file in /tmp.
62        -c identifies cellname as the cell in which authentication is to take
63           place.
64        -k identifies an alternate kerberos realm to use provide
65           authentication services for the cell.
66  */
67
68 #define KLOGEXIT(code) rx_Finalize(); \
69                        (exit(!!code))
70 static int CommandProc(struct cmd_syndesc *as, void *arock);
71
72 static int zero_argc;
73 static char **zero_argv;
74
75 static krb5_context k5context;
76 static struct afsconf_dir *tdir;
77 static int always_evil = 2;     /* gcc optimizes 0 into bss.  fools. */
78
79 int
80 main(int argc, char *argv[])
81 {
82     struct cmd_syndesc *ts;
83     afs_int32 code;
84 #ifdef  AFS_AIX32_ENV
85     /*
86      * The following signal action for AIX is necessary so that in case of a
87      * crash (i.e. core is generated) we can include the user's data section
88      * in the core dump. Unfortunately, by default, only a partial core is
89      * generated which, in many cases, isn't too useful.
90      */
91     struct sigaction nsa;
92
93     sigemptyset(&nsa.sa_mask);
94     nsa.sa_handler = SIG_DFL;
95     nsa.sa_flags = SA_FULLDUMP;
96     sigaction(SIGABRT, &nsa, NULL);
97     sigaction(SIGSEGV, &nsa, NULL);
98 #endif
99     zero_argc = argc;
100     zero_argv = argv;
101
102     ts = cmd_CreateSyntax(NULL, CommandProc, NULL, 0,
103                           "obtain Kerberos authentication");
104
105 #define aXFLAG 0
106 #define aPRINCIPAL 1
107 #define aPASSWORD 2
108 #define aCELL 3
109 #define aKRBREALM 4
110 #define aPIPE 5
111 #define aSILENT 6
112 #define aLIFETIME 7
113 #define aSETPAG 8
114 #define aTMP 9
115 #define aNOPRDB 10
116 #define aUNWRAP 11
117 #define aK5 12
118 #define aK4 13
119 #define aDES 14
120
121     cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "obsolete, noop");
122     cmd_Seek(ts, aPRINCIPAL);
123     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
124     cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
125     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
126     cmd_AddParm(ts, "-k", CMD_SINGLE, CMD_OPTIONAL, "krb5 realm");
127     cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL,
128                 "read password from stdin");
129     cmd_AddParm(ts, "-silent", CMD_FLAG, CMD_OPTIONAL, "silent operation");
130     /* Note: -lifetime is not implemented in this version of klog. */
131     cmd_AddParm(ts, "-lifetime", CMD_SINGLE, CMD_OPTIONAL,
132                 "ticket lifetime in hh[:mm[:ss]]");
133     cmd_AddParm(ts, "-setpag", CMD_FLAG, CMD_OPTIONAL,
134                 "Create a new setpag before authenticating");
135     cmd_AddParm(ts, "-tmp", CMD_FLAG, CMD_OPTIONAL,
136                 "write Kerberos-style ticket file in /tmp");
137     cmd_AddParm(ts, "-noprdb", CMD_FLAG, CMD_OPTIONAL, "don't consult pt");
138     cmd_AddParm(ts, "-unwrap", CMD_FLAG, CMD_OPTIONAL, "perform 524d conversion");
139 #ifdef AFS_RXK5
140     cmd_AddParm(ts, "-k5", CMD_FLAG, CMD_OPTIONAL, "get rxk5 credentials");
141     cmd_AddParm(ts, "-k4", CMD_FLAG, CMD_OPTIONAL, "get rxkad credentials");
142 #else
143     ++ts->nParms;       /* skip -k5 */
144     cmd_AddParm(ts, "-k4", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, 0);
145 #endif
146     cmd_AddParm(ts, "-insecure_des", CMD_FLAG, CMD_OPTIONAL,
147                 "enable insecure single-DES for krb5");
148
149     code = cmd_Dispatch(argc, argv);
150     KLOGEXIT(code);
151 }
152
153 static char *
154 getpipepass(void)
155 {
156     static char gpbuf[BUFSIZ];
157     /* read a password from stdin, stop on \n or eof */
158     int i, tc;
159     memset(gpbuf, 0, sizeof(gpbuf));
160     for (i = 0; i < (sizeof(gpbuf) - 1); i++) {
161         tc = fgetc(stdin);
162         if (tc == '\n' || tc == EOF)
163             break;
164         gpbuf[i] = tc;
165     }
166     return gpbuf;
167 }
168
169 void
170 silent_errors(const char *who,
171     afs_int32 code,
172     const char *fmt,
173     va_list ap)
174 {
175     /* ignore and don't print error */
176 }
177
178 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
179
180 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
181 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
182 #define num_comp(c, p) (krb5_princ_size(c, p))
183 #define realm_data(c, p) krb5_princ_realm(c, p)->data
184 #define realm_len(c, p) krb5_princ_realm(c, p)->length
185
186 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
187
188 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
189 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
190 #define num_comp(c, p) ((p)->name.name_string.len)
191 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
192 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
193
194 #else
195 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
196 #endif
197
198 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
199
200 #define get_cred_keydata(c) c->keyblock.contents
201 #define get_cred_keylen(c) c->keyblock.length
202 #define get_creds_enctype(c) c->keyblock.enctype
203
204 #elif defined(HAVE_KRB5_CREDS_SESSION)
205
206 #define get_cred_keydata(c) c->session.keyvalue.data
207 #define get_cred_keylen(c) c->session.keyvalue.length
208 #define get_creds_enctype(c) c->session.keytype
209
210 #else
211 #error "Must have either keyblock or session member of krb5_creds"
212 #endif
213
214 static int
215 whoami(struct ktc_token *atoken,
216     struct afsconf_cell *cellconfig,
217     struct ktc_principal *aclient,
218     int *vicep)
219 {
220     int code;
221     char tempname[2*PR_MAXNAMELEN];
222
223     code = pr_Initialize(0, AFSDIR_CLIENT_ETC_DIRPATH, cellconfig->name);
224     if (code)
225         goto Failed;
226
227     if (*aclient->instance)
228         snprintf (tempname, sizeof tempname, "%s.%s",
229             aclient->name, aclient->instance);
230     else
231         snprintf (tempname, sizeof tempname, "%s", aclient->name);
232     code = pr_SNameToId(tempname, vicep);
233 Failed:
234     return code;
235 }
236
237 static void
238 k5_to_k4_name(krb5_context k5context,
239     krb5_principal k5princ,
240     struct ktc_principal *ktcprinc)
241 {
242     int i;
243
244     switch(num_comp(k5context, k5princ)) {
245         default:
246         /* case 2: */
247             i = get_princ_len(k5context, k5princ, 1);
248             if (i > MAXKTCNAMELEN-1) i = MAXKTCNAMELEN-1;
249             memcpy(ktcprinc->instance, get_princ_str(k5context, k5princ, 1), i);
250             AFS_FALLTHROUGH;
251         case 1:
252             i = get_princ_len(k5context, k5princ, 0);
253             if (i > MAXKTCNAMELEN-1) i = MAXKTCNAMELEN-1;
254             memcpy(ktcprinc->name, get_princ_str(k5context, k5princ, 0), i);
255             AFS_FALLTHROUGH;
256         case 0:
257             break;
258         }
259 }
260
261 #if defined(USING_HEIMDAL) || defined(HAVE_KRB5_PROMPT_TYPE)
262 static int
263 klog_is_pass_prompt(int index, krb5_context context, krb5_prompt prompts[])
264 {
265     switch (prompts[index].type) {
266     case KRB5_PROMPT_TYPE_PASSWORD:
267     case KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN:
268         return 1;
269     default:
270         return 0;
271     }
272 }
273 #elif defined(HAVE_KRB5_GET_PROMPT_TYPES)
274 static int
275 klog_is_pass_prompt(int index, krb5_context context, krb5_prompt prompts[])
276 {
277     /* this isn't thread-safe or anything obviously; it just should be good
278      * enough to work with klog */
279     static krb5_prompt_type *types = NULL;
280     if (index == 0) {
281         types = NULL;
282     }
283     if (!types) {
284         types = krb5_get_prompt_types(context);
285     }
286     if (!types) {
287         return 0;
288     }
289     switch (types[index]) {
290     case KRB5_PROMPT_TYPE_PASSWORD:
291     case KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN:
292         return 1;
293     default:
294         return 0;
295     }
296 }
297 #else
298 static int
299 klog_is_pass_prompt(int index, krb5_context context, krb5_prompt prompts[])
300 {
301     /* AIX 5.3 doesn't have krb5_get_prompt_types. Neither does HP-UX, which
302      * also doesn't even define KRB5_PROMPT_TYPE_PASSWORD &co. We have no way
303      * of determining the the prompt type, so just assume it's a password */
304     return 1;
305 }
306 #endif
307
308 /* save and reuse password.  This is necessary to make
309  *  "direct to service" authentication work with most
310  *  flavors of kerberos, when the afs principal has no instance.
311  */
312 struct kp_arg {
313     char **pp, *pstore;
314     size_t allocated;
315 };
316 krb5_error_code
317 klog_prompter(krb5_context context,
318     void *a,
319     const char *name,
320     const char *banner,
321     int num_prompts,
322     krb5_prompt prompts[])
323 {
324     krb5_error_code code;
325     int i;
326     struct kp_arg *kparg = (struct kp_arg *) a;
327     size_t length;
328
329     code = krb5_prompter_posix(context, a, name, banner, num_prompts, prompts);
330     if (code) return code;
331     for (i = 0; i < num_prompts; ++i) {
332         if (klog_is_pass_prompt(i, context, prompts)) {
333             length = prompts[i].reply->length;
334             if (length > kparg->allocated - 1)
335                 length = kparg->allocated - 1;
336             memcpy(kparg->pstore, prompts[i].reply->data, length);
337             kparg->pstore[length] = 0;
338             *kparg->pp = kparg->pstore;
339         }
340     }
341     return 0;
342 }
343
344 static int
345 CommandProc(struct cmd_syndesc *as, void *arock)
346 {
347     krb5_principal princ = 0;
348     char *cell, *pname, **hrealms, *service;
349     char service_temp[MAXKTCREALMLEN + 20];
350     krb5_creds incred[1], mcred[1], *outcred = 0, *afscred;
351     krb5_ccache cc = 0;
352 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
353     krb5_get_init_creds_opt *gic_opts;
354 #else
355     krb5_get_init_creds_opt gic_opts[1];
356 #endif
357     char *tofree = NULL, *outname;
358     int code;
359     char *what;
360     int i, dosetpag, evil, noprdb, id;
361 #ifdef AFS_RXK5
362     int authtype;
363 #endif
364     krb5_data enc_part[1];
365     krb5_prompter_fct pf = NULL;
366     char *pass = 0;
367     void *pa = 0;
368     struct kp_arg klog_arg[1];
369
370     char passwd[BUFSIZ];
371     struct afsconf_cell cellconfig[1];
372
373     static char rn[] = "klog";  /*Routine name */
374     static int Pipe = 0;        /* reading from a pipe */
375     static int Silent = 0;      /* Don't want error messages */
376
377     int writeTicketFile = 0;    /* write ticket file to /tmp */
378
379     service = 0;
380     memset(incred, 0, sizeof *incred);
381     /* blow away command line arguments */
382     for (i = 1; i < zero_argc; i++)
383         memset(zero_argv[i], 0, strlen(zero_argv[i]));
384     zero_argc = 0;
385     memset(klog_arg, 0, sizeof *klog_arg);
386
387     /* first determine quiet flag based on -silent switch */
388     Silent = (as->parms[aSILENT].items ? 1 : 0);
389
390     if (Silent) {
391         afs_set_com_err_hook(silent_errors);
392     }
393
394     if ((code = krb5_init_context(&k5context))) {
395         afs_com_err(rn, code, "while initializing Kerberos 5 library");
396         KLOGEXIT(code);
397     }
398     if ((code = rx_Init(0))) {
399         afs_com_err(rn, code, "while initializing rx");
400         KLOGEXIT(code);
401     }
402     initialize_U_error_table();
403     /*initialize_krb5_error_table();*/
404     initialize_RXK_error_table();
405     initialize_KTC_error_table();
406     initialize_ACFG_error_table();
407     /* initialize_rx_error_table(); */
408     if (!(tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
409         afs_com_err(rn, 0, "can't get afs configuration (afsconf_Open(%s))",
410             AFSDIR_CLIENT_ETC_DIRPATH);
411         KLOGEXIT(1);
412     }
413
414     /*
415      * Enable DES enctypes, which are currently still required for AFS.
416      * krb5_allow_weak_crypto is MIT Kerberos 1.8.  krb5_enctype_enable is
417      * Heimdal.
418      */
419     if (as->parms[aDES].items) {
420 #if defined(HAVE_KRB5_ENCTYPE_ENABLE)
421         i = krb5_enctype_valid(k5context, ETYPE_DES_CBC_CRC);
422         if (i)
423             krb5_enctype_enable(k5context, ETYPE_DES_CBC_CRC);
424 #elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
425         krb5_allow_weak_crypto(k5context, 1);
426 #endif
427     }
428
429     /* Parse remaining arguments. */
430
431     dosetpag = !! as->parms[aSETPAG].items;
432     Pipe = !! as->parms[aPIPE].items;
433     writeTicketFile = !! as->parms[aTMP].items;
434     noprdb = !! as->parms[aNOPRDB].items;
435     evil = (always_evil&1) || !! as->parms[aUNWRAP].items;
436
437 #ifdef AFS_RXK5
438     authtype = 0;
439     if (as->parms[aK5].items)
440         authtype |= FORCE_RXK5;
441     if (as->parms[aK4].items)
442         authtype |= FORCE_RXKAD;
443     if (!authtype)
444         authtype |= env_afs_rxk5_default();
445 #endif
446
447     cell = as->parms[aCELL].items ? as->parms[aCELL].items->data : 0;
448     if ((code = afsconf_GetCellInfo(tdir, cell, "afsprot", cellconfig))) {
449         if (cell)
450             afs_com_err(rn, code, "Can't get cell information for '%s'", cell);
451         else
452             afs_com_err(rn, code, "Can't get determine local cell!");
453         KLOGEXIT(code);
454     }
455
456     if (as->parms[aKRBREALM].items) {
457         code = krb5_set_default_realm(k5context,
458                 as->parms[aKRBREALM].items->data);
459         if (code) {
460             afs_com_err(rn, code, "Can't make <%s> the default realm",
461                 as->parms[aKRBREALM].items->data);
462             KLOGEXIT(code);
463         }
464     }
465     else if ((code = krb5_get_host_realm(k5context, cellconfig->hostName[0], &hrealms))) {
466         afs_com_err(rn, code, "Can't get realm for host <%s> in cell <%s>\n",
467                 cellconfig->hostName[0], cellconfig->name);
468         KLOGEXIT(code);
469     } else {
470         if (hrealms && *hrealms) {
471             code = krb5_set_default_realm(k5context,
472                     *hrealms);
473             if (code) {
474                 afs_com_err(rn, code, "Can't make <%s> the default realm",
475                     *hrealms);
476                 KLOGEXIT(code);
477             }
478         }
479         if (hrealms) krb5_free_host_realm(k5context, hrealms);
480     }
481
482     id = getuid();
483     if (as->parms[aPRINCIPAL].items) {
484         pname = as->parms[aPRINCIPAL].items->data;
485     } else {
486         /* No explicit name provided: use Unix uid. */
487         struct passwd *pw;
488         pw = getpwuid(id);
489         if (pw == 0) {
490             afs_com_err(rn, 0,
491                 "Can't figure out your name from your user id (%d).", id);
492             if (!Silent)
493                 fprintf(stderr, "%s: Try providing the user name.\n", rn);
494             KLOGEXIT(1);
495         }
496         pname = pw->pw_name;
497     }
498     code = krb5_parse_name(k5context, pname, &princ);
499     if (code) {
500         afs_com_err(rn, code, "Can't parse principal <%s>", pname);
501         KLOGEXIT(code);
502     }
503
504     if (as->parms[aPASSWORD].items) {
505         /*
506          * Current argument is the desired password string.  Remember it in
507          * our local buffer, and zero out the argument string - anyone can
508          * see it there with ps!
509          */
510         strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
511         memset(as->parms[aPASSWORD].items->data, 0,
512                strlen(as->parms[aPASSWORD].items->data));
513         pass = passwd;
514     }
515
516     /* Get the password if it wasn't provided. */
517     if (!pass) {
518         if (Pipe) {
519             strncpy(passwd, getpipepass(), sizeof(passwd));
520             pass = passwd;
521         } else {
522             pf = klog_prompter;
523             pa = klog_arg;
524         }
525     }
526
527     service = 0;
528 #ifdef AFS_RXK5
529     if (authtype & FORCE_RXK5) {
530         tofree = get_afs_krb5_svc_princ(cellconfig);
531         snprintf(service_temp, sizeof service_temp, "%s", tofree);
532     } else
533 #endif
534     snprintf (service_temp, sizeof service_temp, "afs/%s", cellconfig->name);
535
536     klog_arg->pp = &pass;
537     klog_arg->pstore = passwd;
538     klog_arg->allocated = sizeof(passwd);
539     /* XXX should allow k5 to prompt in most cases -- what about expired pw?*/
540 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
541     code = krb5_get_init_creds_opt_alloc(k5context, &gic_opts);
542     if (code) {
543         afs_com_err(rn, code, "Can't allocate get_init_creds options");
544         KLOGEXIT(code);
545     }
546 #else
547     krb5_get_init_creds_opt_init(gic_opts);
548 #endif
549
550     for (;;) {
551         code = krb5_get_init_creds_password(k5context,
552             incred,
553             princ,
554             pass,
555             pf, /* prompter */
556             pa, /* data */
557             0,  /* start_time */
558             0,  /* in_tkt_service */
559             gic_opts);
560         if (code != KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN)
561             break;
562     }
563     memset(passwd, 0, sizeof(passwd));
564     if (code) {
565         char *r = 0;
566         if (krb5_get_default_realm(k5context, &r))
567             r = 0;
568         if (r)
569             afs_com_err(rn, code, "Unable to authenticate in realm %s", r);
570         else
571             afs_com_err(rn, code, "Unable to authenticate to use cell %s",
572                 cellconfig->name);
573         if (r) free(r);
574         KLOGEXIT(code);
575     }
576
577     for (;;writeTicketFile = 0) {
578         if (writeTicketFile) {
579             what = "getting default ccache";
580             code = krb5_cc_default(k5context, &cc);
581         } else {
582             what = "krb5_cc_resolve";
583             code = krb5_cc_resolve(k5context, "MEMORY:core", &cc);
584             if (code) goto Failed;
585         }
586         what = "initializing ccache";
587         code = krb5_cc_initialize(k5context, cc, princ);
588         if (code) goto Failed;
589         what = "writing Kerberos ticket file";
590         code = krb5_cc_store_cred(k5context, cc, incred);
591         if (code) goto Failed;
592         if (writeTicketFile)
593             fprintf(stderr,
594                     "Wrote ticket file to %s\n",
595                     krb5_cc_get_name(k5context, cc));
596         break;
597       Failed:
598         if (code)
599             afs_com_err(rn, code, "%s", what);
600         if (writeTicketFile) {
601             if (cc) {
602                 krb5_cc_close(k5context, cc);
603                 cc = 0;
604             }
605             continue;
606         }
607         KLOGEXIT(code);
608     }
609
610     for (service = service_temp;;service = "afs") {
611         memset(mcred, 0, sizeof *mcred);
612         mcred->client = princ;
613         code = krb5_parse_name(k5context, service, &mcred->server);
614         if (code) {
615             afs_com_err(rn, code, "Unable to parse service <%s>\n", service);
616             KLOGEXIT(code);
617         }
618         if (tofree) { free(tofree); tofree = 0; }
619         if (!(code = krb5_unparse_name(k5context, mcred->server, &outname)))
620             tofree = outname;
621         else outname = service;
622         code = krb5_get_credentials(k5context, 0, cc, mcred, &outcred);
623         krb5_free_principal(k5context, mcred->server);
624         if (code != KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || service != service_temp) break;
625 #ifdef AFS_RXK5
626         if (authtype & FORCE_RXK5)
627             break;
628 #endif
629     }
630     afscred = outcred;
631
632     if (code) {
633         afs_com_err(rn, code, "Unable to get credentials to use %s", outname);
634         KLOGEXIT(code);
635     }
636
637 #ifdef AFS_RXK5
638     if (authtype & FORCE_RXK5) {
639         struct ktc_principal aserver[1];
640         int viceid = 555;
641
642         memset(aserver, 0, sizeof *aserver);
643         strncpy(aserver->cell, cellconfig->name, MAXKTCREALMLEN-1);
644         code = ktc_SetK5Token(k5context, aserver, afscred, viceid, dosetpag);
645         if (code) {
646             afs_com_err(rn, code, "Unable to store tokens for cell %s\n",
647                 cellconfig->name);
648             KLOGEXIT(1);
649         }
650     } else
651 #endif
652     {
653         struct ktc_principal aserver[1], aclient[1];
654         struct ktc_token atoken[1];
655
656         memset(atoken, 0, sizeof *atoken);
657         if (evil) {
658             size_t elen = enc_part->length;
659             atoken->kvno = RXKAD_TKT_TYPE_KERBEROS_V5_ENCPART_ONLY;
660             if (afs_krb5_skip_ticket_wrapper(afscred->ticket.data,
661                         afscred->ticket.length, (char **) &enc_part->data,
662                         &elen)) {
663                 afs_com_err(rn, 0, "Can't unwrap %s AFS credential",
664                     cellconfig->name);
665                 KLOGEXIT(1);
666             }
667         } else {
668             atoken->kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
669             *enc_part = afscred->ticket;
670         }
671         atoken->startTime = afscred->times.starttime;
672         atoken->endTime = afscred->times.endtime;
673         if (tkt_DeriveDesKey(get_creds_enctype(afscred),
674                              get_cred_keydata(afscred),
675                              get_cred_keylen(afscred), &atoken->sessionKey)) {
676             afs_com_err(rn, 0,
677                         "Cannot derive DES key from enctype %i of length %u",
678                         get_creds_enctype(afscred),
679                         (unsigned)get_cred_keylen(afscred));
680             KLOGEXIT(1);
681         }
682         memcpy(atoken->ticket, enc_part->data,
683             atoken->ticketLen = enc_part->length);
684         memset(aserver, 0, sizeof *aserver);
685         strncpy(aserver->name, "afs", 4);
686         strncpy(aserver->cell, cellconfig->name, MAXKTCREALMLEN-1);
687         memset(aclient, 0, sizeof *aclient);
688         i = realm_len(k5context, afscred->client);
689         if (i > MAXKTCREALMLEN-1) i = MAXKTCREALMLEN-1;
690         memcpy(aclient->cell, realm_data(k5context, afscred->client), i);
691         if (!noprdb) {
692             int viceid = 0;
693             k5_to_k4_name(k5context, afscred->client, aclient);
694             code = whoami(atoken, cellconfig, aclient, &viceid);
695             if (code) {
696                 afs_com_err(rn, code, "Can't get your viceid for cell %s", cellconfig->name);
697                 *aclient->name = 0;
698             } else
699                 snprintf(aclient->name, MAXKTCNAMELEN-1, "AFS ID %d", viceid);
700         }
701         if (!*aclient->name)
702             k5_to_k4_name(k5context, afscred->client, aclient);
703         code = ktc_SetToken(aserver, atoken, aclient, dosetpag);
704         if (code) {
705             afs_com_err(rn, code, "Unable to store tokens for cell %s\n",
706                 cellconfig->name);
707             KLOGEXIT(1);
708         }
709     }
710
711     krb5_free_principal(k5context, princ);
712     krb5_free_cred_contents(k5context, incred);
713     if (outcred) krb5_free_creds(k5context, outcred);
714     if (cc)
715         krb5_cc_close(k5context, cc);
716     if (tofree) free(tofree);
717
718     return 0;
719 }