4 * Copyright 1990,1991 by the Massachusetts Institute of Technology
5 * For distribution and copying rights, see the file "mit-copyright.h"
8 * Copyright (c) 2005, 2006
9 * The Linux Box Corporation
12 * Permission is granted to use, copy, create derivative works
13 * and redistribute this software and such derivative works
14 * for any purpose, so long as the name of the Linux Box
15 * Corporation is not used in any advertising or publicity
16 * pertaining to the use or distribution of this software
17 * without specific, written prior authorization. If the
18 * above copyright notice or any other identification of the
19 * Linux Box Corporation is included in any copy of any
20 * portion of this software, then the disclaimer below must
23 * This software is provided as is, without representation
24 * from the Linux Box Corporation as to its fitness for any
25 * purpose, and without warranty by the Linux Box Corporation
26 * of any kind, either express or implied, including
27 * without limitation the implied warranties of
28 * merchantability and fitness for a particular purpose. The
29 * regents of the Linux Box Corporation shall not be liable
30 * for any damages, including special, indirect, incidental, or
31 * consequential damages, with respect to any claim arising
32 * out of or in connection with the use of the software, even
33 * if it has been or is hereafter advised of the possibility of
37 #include <afsconfig.h>
44 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/errno.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
70 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
71 #include <afs/com_err.h>
74 #include <afs/param.h>
76 #include <sys/ioccom.h>
79 #include <afs/cellconfig.h>
81 #include <afs/venus.h>
82 #include <afs/ptserver.h>
83 #include <afs/ptuser.h>
84 #include <afs/dirpath.h>
87 #include "linked_list.h"
89 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
92 #ifdef HAVE_KRB5_CREDS_SESSION
93 #define USING_HEIMDAL 1
99 #ifndef AFS_TRY_FULL_PRINC
100 #define AFS_TRY_FULL_PRINC 1
101 #endif /* AFS_TRY_FULL_PRINC */
103 #define AKLOG_TRYAGAIN -1
104 #define AKLOG_SUCCESS 0
105 #define AKLOG_USAGE 1
106 #define AKLOG_SOMETHINGSWRONG 2
108 #define AKLOG_KERBEROS 4
109 #define AKLOG_TOKEN 5
110 #define AKLOG_BADPATH 6
126 /* RedHat 4.x doesn't seem to define this */
127 #define MAXSYMLINKS 5
130 #define DIR '/' /* Character that divides directories */
131 #define DIRSTRING "/" /* String form of above */
132 #define VOLMARKER ':' /* Character separating cellname from mntpt */
133 #define VOLMARKERSTRING ":" /* String form of above */
137 char realm[REALM_SZ];
140 struct afsconf_cell ak_cellconfig; /* General information about the cell */
141 static char linkedcell[MAXCELLCHARS+1];
142 static char linkedcell2[MAXCELLCHARS+1];
143 static krb5_ccache _krb425_ccache = NULL;
146 * Why doesn't AFS provide these prototypes?
149 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
155 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *, int);
156 static int isdir(char *, unsigned char *);
157 static krb5_error_code get_credv5(krb5_context context, char *, char *,
158 char *, krb5_creds **);
159 static int get_user_realm(krb5_context, char *);
161 #define TRYAGAIN(x) (x == AKLOG_TRYAGAIN || \
162 x == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || \
163 x == KRB5KRB_ERR_GENERIC)
165 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
167 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
168 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
169 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
170 #define realm_data(c, p) krb5_princ_realm(c, p)->data
171 #define realm_len(c, p) krb5_princ_realm(c, p)->length
173 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
175 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
176 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
177 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
178 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
179 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
182 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
185 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
187 krb5_encrypt_tkt_part(krb5_context context,
188 const krb5_keyblock *key,
195 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
197 if ((code = krb5_c_encrypt_length(context, key->enctype,
198 data->length, &enclen)))
200 ticket->enc_part.ciphertext.length = enclen;
201 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
205 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
206 0, data, &ticket->enc_part))) {
207 free(ticket->enc_part.ciphertext.data);
208 ticket->enc_part.ciphertext.data = 0;
220 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
222 #define get_cred_keydata(c) c->keyblock.contents
223 #define get_cred_keylen(c) c->keyblock.length
224 #define get_creds_enctype(c) c->keyblock.enctype
226 #elif defined(HAVE_KRB5_CREDS_SESSION)
228 #define get_cred_keydata(c) c->session.keyvalue.data
229 #define get_cred_keylen(c) c->session.keyvalue.length
230 #define get_creds_enctype(c) c->session.keytype
233 #error "Must have either keyblock or session member of krb5_creds"
236 #if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
237 #define krb5_524_convert_creds krb524_convert_creds_kdc
238 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
239 #define HAVE_NO_KRB5_524
243 #define deref_keyblock_enctype(kb) \
246 #define deref_entry_keyblock(entry) \
249 #define deref_session_key(creds) \
252 #define deref_enc_tkt_addrs(tkt) \
255 #define deref_enc_length(enc) \
256 ((enc)->cipher.length)
258 #define deref_enc_data(enc) \
261 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
264 #define deref_keyblock_enctype(kb) \
267 #define deref_entry_keyblock(entry) \
270 #define deref_session_key(creds) \
273 #define deref_enc_tkt_addrs(tkt) \
276 #define deref_enc_length(enc) \
277 ((enc)->ciphertext.length)
279 #define deref_enc_data(enc) \
280 ((enc)->ciphertext.data)
284 #define deref_entry_enctype(entry) \
285 deref_keyblock_enctype(&deref_entry_keyblock(entry))
288 * Provide a replacement for strerror if we don't have it
291 #ifndef HAVE_STRERROR
292 extern char *sys_errlist[];
293 #define strerror(x) sys_errlist[x]
294 #endif /* HAVE_STRERROR */
296 static char *progname = NULL; /* Name of this program */
297 static int dflag = FALSE; /* Give debugging information */
298 static int noauth = FALSE; /* If true, don't try to get tokens */
299 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
300 static int hosts = FALSE; /* Are we keeping track of hosts? */
301 static int noprdb = FALSE; /* Skip resolving name to id? */
302 static int linked = FALSE; /* try for both AFS nodes */
303 static int afssetpag = FALSE; /* setpag for AFS */
304 static int force = FALSE; /* Bash identical tokens? */
305 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
306 static char *keytab = NULL; /* keytab for akimpersonate */
307 static char *client = NULL; /* client principal for akimpersonate */
308 static linked_list zsublist; /* List of zephyr subscriptions */
309 static linked_list hostlist; /* List of host addresses */
310 static linked_list authedcells; /* List of cells already logged to */
312 /* ANL - CMU lifetime convert routine */
313 /* for K5.4.1 don't use this for now. Need to see if it is needed */
314 /* maybe needed in the krb524d module as well */
315 /* extern unsigned long krb_life_to_time(); */
317 static char *copy_cellinfo(cellinfo_t *cellinfo)
319 cellinfo_t *new_cellinfo;
321 if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
322 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
324 return ((char *)new_cellinfo);
328 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
330 int status = AKLOG_SUCCESS;
331 struct afsconf_dir *configdir;
333 memset(local_cell, 0, sizeof(local_cell));
334 memset((char *)cellconfig, 0, sizeof(*cellconfig));
336 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
338 "%s: can't get afs configuration (afsconf_Open(%s))\n",
339 progname, AFSDIR_CLIENT_ETC_DIRPATH);
343 if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
344 fprintf(stderr, "%s: can't determine local cell.\n", progname);
348 if ((cell == NULL) || (cell[0] == 0))
351 linkedcell[0] = '\0';
352 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
353 fprintf(stderr, "%s: Can't get information about cell %s.\n",
357 if (cellconfig->linkedCell)
358 strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
360 (void) afsconf_Close(configdir);
366 * Log to a cell. If the cell has already been logged to, return without
367 * doing anything. Otherwise, log to it and mark that it has been logged
370 static int auth_to_cell(krb5_context context, char *cell, char *realm)
372 int status = AKLOG_SUCCESS;
373 char username[BUFSIZ]; /* To hold client username structure */
374 afs_int32 viceId; /* AFS uid of user */
376 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
377 char *realm_from_princ = 0 ; /* Calculated realm data */
378 char *realm_of_cell = 0; /* Pointer to realm we're using */
379 int retry; /* round, and round we go ... */
381 char local_cell[MAXCELLCHARS+1];
382 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
383 static char lastcell[MAXCELLCHARS+1] = { 0 };
384 static char confname[512] = { 0 };
385 krb5_creds *v5cred = NULL;
386 struct ktc_principal aserver;
387 struct ktc_principal aclient;
388 struct ktc_token atoken, btoken;
390 memset(realm_of_user, 0, sizeof(realm_of_user));
392 if (confname[0] == '\0') {
393 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
394 confname[sizeof(confname) - 2] = '\0';
397 /* NULL or empty cell returns information on local cell */
398 if ((status = get_cellconfig(cell, &ak_cellconfig,
399 local_cell, linkedcell)))
402 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
403 cell_to_use[MAXCELLCHARS] = 0;
405 if (ll_string(&authedcells, ll_s_check, cell_to_use)) {
407 printf("Already authenticated to %s (or tried to)\n",
410 return(AKLOG_SUCCESS);
414 * Record that we have attempted to log to this cell. We do this
415 * before we try rather than after so that we will not try
416 * and fail repeatedly for one cell.
418 (void)ll_string(&authedcells, ll_s_add, cell_to_use);
421 * Record this cell in the list of zephyr subscriptions. We may
422 * want zephyr subscriptions even if authentication fails.
423 * If this is done after we attempt to get tokens, aklog -zsubs
424 * can return something different depending on whether or not we
425 * are in -noauth mode.
427 if (ll_string(&zsublist, ll_s_add, cell_to_use) == LL_FAILURE) {
429 "%s: failure adding cell %s to zephyr subscriptions list.\n",
430 progname, cell_to_use);
433 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
435 "%s: failure adding cell %s to zephyr subscriptions list.\n",
436 progname, local_cell);
442 printf("Authenticating to cell %s (server %s).\n",
443 cell_to_use, ak_cellconfig.hostName[0]);
446 if ((status = get_user_realm(context, realm_of_user))) {
447 fprintf(stderr, "%s: Couldn't determine realm of user:)",
449 afs_com_err(progname, status, " while getting realm");
450 return(AKLOG_KERBEROS);
457 /* This code tries principals in the following, much debated,
460 * If the realm is specified on the command line we do
461 * - afs/cell@COMMAND-LINE-REALM
462 * - afs@COMMAND-LINE-REALM
465 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
466 * - afs/cell@krb5_get_host_realm(db-server)
467 * Then, if krb5_get_host_realm(db-server) is non-empty
468 * - afs@ krb5_get_host_realm(db-server)
470 * - afs/cell@ upper-case-domain-of-db-server
471 * - afs@ upper-case-domain-of-db-server
473 * In all cases, the 'afs@' variant is only tried where the
474 * cell and the realm match case-insensitively.
477 /* Cell on command line - use that one */
478 if (realm && realm[0]) {
479 realm_of_cell = realm;
480 status = AKLOG_TRYAGAIN;
482 printf("We were told to authenticate to realm %s.\n",
486 /* Initially, try using afs/cell@USERREALM */
488 printf("Trying to authenticate to user's realm %s.\n",
492 realm_of_cell = realm_of_user;
493 status = get_credv5(context, AFSKEY, cell_to_use,
494 realm_of_cell, &v5cred);
496 /* If that failed, try to determine the realm from the name of
497 * one of the DB servers */
498 if (TRYAGAIN(status)) {
499 realm_of_cell = afs_realm_of_cell(context, &ak_cellconfig,
501 if (!realm_of_cell) {
503 "%s: Couldn't figure out realm for cell %s.\n",
504 progname, cell_to_use);
509 if (realm_of_cell[0])
510 printf("We've deduced that we need to authenticate"
511 " to realm %s.\n", realm_of_cell);
513 printf("We've deduced that we need to authenticate "
514 "using referrals.\n");
519 if (TRYAGAIN(status)) {
520 /* If we've got the full-princ-first option, or we're in a
521 * different realm from the cell - use the cell name as the
523 if (AFS_TRY_FULL_PRINC ||
524 strcasecmp(cell_to_use, realm_of_cell)!=0) {
525 status = get_credv5(context, AFSKEY, cell_to_use,
526 realm_of_cell, &v5cred);
528 /* If we failed & we've got an empty realm, then try
529 * calling afs_realm_for_cell again. */
530 if (TRYAGAIN(status) && !realm_of_cell[0]) {
531 /* This time, get the realm by taking the domain
532 * component of the db server and make it upper case */
533 realm_of_cell = afs_realm_of_cell(context,
534 &ak_cellconfig, TRUE);
535 if (!realm_of_cell) {
537 "%s: Couldn't figure out realm for cell "
538 "%s.\n", progname, cell_to_use);
542 printf("We've deduced that we need to authenticate"
543 " to realm %s.\n", realm_of_cell);
546 status = get_credv5(context, AFSKEY, cell_to_use,
547 realm_of_cell, &v5cred);
550 /* If the realm and cell name match, then try without an
551 * instance, but only if realm is non-empty */
553 if (TRYAGAIN(status) &&
554 strcasecmp(cell_to_use, realm_of_cell) == 0) {
555 status = get_credv5(context, AFSKEY, NULL,
556 realm_of_cell, &v5cred);
557 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
558 status = get_credv5(context, AFSKEY, cell_to_use,
559 realm_of_cell, &v5cred);
564 /* Try to find a service principal for this cell.
565 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
566 * the first attempt, so we try twice to be sure */
568 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
576 printf("Kerberos error code returned by get_cred : %d\n",
579 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
580 progname, cell_to_use);
581 afs_com_err(progname, status, "while getting AFS tickets");
582 return(AKLOG_KERBEROS);
585 /* If we've got a valid ticket, and we still don't know the realm name
586 * try to figure it out from the contents of the ticket
588 #if !defined(USING_HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
589 if (strcmp(realm_of_cell, "") == 0) {
590 krb5_error_code code;
593 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
597 "%s: Couldn't decode ticket to determine realm for "
599 progname, cell_to_use);
601 int len = realm_len(context, ticket->server);
602 /* This really shouldn't happen. */
603 if (len > REALM_SZ-1)
606 realm_from_princ = (char *) malloc(sizeof(char) * (len+1));
608 strncpy(realm_from_princ, realm_data(context, ticket->server),
610 realm_from_princ[len] = 0;
611 realm_of_cell = realm_from_princ;
613 krb5_free_ticket(context, ticket);
618 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
619 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
620 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
623 * The default is to use rxkad2b, which means we put in a full
624 * V5 ticket. If the user specifies -524, we talk to the
625 * 524 ticket converter.
633 printf("Using Kerberos V5 ticket natively\n");
635 status = krb5_524_conv_principal (context, v5cred->client, &k4name, &k4inst, &k4realm);
637 afs_com_err(progname, status, "while converting principal "
638 "to Kerberos V4 format");
639 return(AKLOG_KERBEROS);
641 strcpy (username, k4name);
643 strcat (username, ".");
644 strcat (username, k4inst);
647 memset(&atoken, 0, sizeof(atoken));
648 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
649 atoken.startTime = v5cred->times.starttime;;
650 atoken.endTime = v5cred->times.endtime;
651 memcpy(&atoken.sessionKey, get_cred_keydata(v5cred),
652 get_cred_keylen(v5cred));
653 atoken.ticketLen = v5cred->ticket.length;
654 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
655 #ifndef HAVE_NO_KRB5_524
660 printf("Using Kerberos 524 translator service\n");
662 status = krb5_524_convert_creds(context, v5cred, &cred);
665 afs_com_err(progname, status, "while converting tickets "
666 "to Kerberos V4 format");
667 return(AKLOG_KERBEROS);
670 strcpy (username, cred.pname);
672 strcat (username, ".");
673 strcat (username, cred.pinst);
676 atoken.kvno = cred.kvno;
677 atoken.startTime = cred.issue_date;
679 * It seems silly to go through a bunch of contortions to
680 * extract the expiration time, when the v5 credentials already
681 * has the exact time! Let's use that instead.
683 * Note that this isn't a security hole, as the expiration time
684 * is also contained in the encrypted token
686 atoken.endTime = v5cred->times.endtime;
687 memcpy(&atoken.sessionKey, cred.session, 8);
688 atoken.ticketLen = cred.ticket_st.length;
689 memcpy(atoken.ticket, cred.ticket_st.dat, atoken.ticketLen);
690 #endif /* HAVE_NO_KRB5_524 */
694 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
695 atoken.kvno == btoken.kvno &&
696 atoken.ticketLen == btoken.ticketLen &&
697 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
698 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen)) {
701 printf("Identical tokens already exist; skipping.\n");
712 printf("Not resolving name %s to id (-noprdb set)\n",
717 if (strcmp(realm_of_user, realm_of_cell)) {
718 strcat(username, "@");
719 strcat(username, realm_of_user);
723 printf("About to resolve name %s to id in cell %s.\n",
724 username, aserver.cell);
727 strcpy(lastcell, aserver.cell);
729 if (!pr_Initialize (0, confname, aserver.cell))
730 status = pr_SNameToId (username, &viceId);
734 printf("Error %d\n", status);
736 printf("Id %d\n", (int) viceId);
740 * This is a crock, but it is Transarc's crock, so
741 * we have to play along in order to get the
742 * functionality. The way the afs id is stored is
743 * as a string in the username field of the token.
744 * Contrary to what you may think by looking at
745 * the code for tokens, this hack (AFS ID %d) will
746 * not work if you change %d to something else.
750 * This code is taken from cklog -- it lets people
751 * automatically register with the ptserver in foreign cells
754 #ifdef ALLOW_REGISTER
755 if ((status == 0) && (viceId == ANONYMOUSID) &&
756 (strcmp(realm_of_user, realm_of_cell) != 0)) {
758 printf("doing first-time registration of %s "
759 "at %s\n", username, cell_to_use);
762 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
763 strcpy(aclient.instance, "");
764 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
765 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
766 fprintf(stderr, "%s: unable to obtain tokens for cell %s "
767 "(status: %d).\n", progname, cell_to_use, status);
768 status = AKLOG_TOKEN;
772 * In case you're wondering, we don't need to change the
773 * filename here because we're still connecting to the
774 * same cell -- we're just using a different authentication
778 if ((status = pr_Initialize(1L, confname, aserver.cell))) {
779 printf("Error %d\n", status);
782 if ((status = pr_CreateUser(username, &viceId))) {
783 fprintf(stderr, "%s: %s so unable to create remote PTS "
784 "user %s in cell %s (status: %d).\n", progname,
785 afs_error_message(status), username, cell_to_use,
787 viceId = ANONYMOUSID;
789 printf("created cross-cell entry for %s (Id %d) at %s\n",
790 username, viceId, cell_to_use);
793 #endif /* ALLOW_REGISTER */
795 if ((status == 0) && (viceId != ANONYMOUSID)) {
796 sprintf(username, "AFS ID %d", (int) viceId);
801 fprintf(stdout, "Set username to %s\n", username);
804 /* Reset the "aclient" structure before we call ktc_SetToken.
805 * This structure was first set by the ktc_GetToken call when
806 * we were comparing whether identical tokens already existed.
808 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
809 strcpy(aclient.instance, "");
810 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
813 printf("Setting tokens. %s / %s @ %s \n",
814 aclient.name, aclient.instance, aclient.cell );
816 #ifndef AFS_AIX51_ENV
817 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
818 * this routine, it will not add the token. It is not clear what
819 * is going on here! So we will do the following operation.
820 * On AIX 5, it causes the parent program to die, so we won't.
822 write(2,"",0); /* dummy write */
824 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
826 "%s: unable to obtain tokens for cell %s (status: %d).\n",
827 progname, cell_to_use, status);
828 status = AKLOG_TOKEN;
833 printf("Noauth mode; not authenticating.\n");
839 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
842 char V ='V'; /* AFS has problem on Sun with pioctl */
844 char our_file[MAXPATHLEN + 1];
846 char *last_component;
847 struct ViceIoctl vio;
848 char cellname[BUFSIZ];
850 memset(our_file, 0, sizeof(our_file));
851 strcpy(our_file, file);
853 if ((last_component = strrchr(our_file, DIR))) {
854 *last_component++ = 0;
855 parent_dir = our_file;
858 last_component = our_file;
862 memset(cellname, 0, sizeof(cellname));
864 vio.in = last_component;
865 vio.in_size = strlen(last_component)+1;
867 vio.out = mountpoint;
869 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
870 if (strchr(mountpoint, VOLMARKER) == NULL) {
872 vio.in_size = strlen(file) + 1;
873 vio.out_size = sizeof(cellname);
876 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
877 strcat(cellname, VOLMARKERSTRING);
878 strcat(cellname, mountpoint + 1);
879 memset(mountpoint + 1, 0, size - 1);
880 strcpy(mountpoint + 1, cellname);
890 * This routine each time it is called returns the next directory
891 * down a pathname. It resolves all symbolic links. The first time
892 * it is called, it should be called with the name of the path
893 * to be descended. After that, it should be called with the arguemnt
896 static char *next_path(char *origpath)
898 static char path[MAXPATHLEN + 1];
899 static char pathtocheck[MAXPATHLEN + 1];
901 int link = FALSE; /* Is this a symbolic link? */
902 char linkbuf[MAXPATHLEN + 1];
903 char tmpbuf[MAXPATHLEN + 1];
905 static char *last_comp; /* last component of directory name */
906 static char *elast_comp; /* End of last component */
910 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
912 /* If we are given something for origpath, we are initializing only. */
914 memset(path, 0, sizeof(path));
915 memset(pathtocheck, 0, sizeof(pathtocheck));
916 strcpy(path, origpath);
922 /* We were not given origpath; find then next path to check */
924 /* If we've gotten all the way through already, return NULL */
925 if (last_comp == NULL)
929 while (*last_comp == DIR)
930 strncat(pathtocheck, last_comp++, 1);
931 len = (elast_comp = strchr(last_comp, DIR))
932 ? elast_comp - last_comp : strlen(last_comp);
933 strncat(pathtocheck, last_comp, len);
934 memset(linkbuf, 0, sizeof(linkbuf));
935 if ((link = (readlink(pathtocheck, linkbuf,
936 sizeof(linkbuf)) > 0))) {
937 if (++symlinkcount > MAXSYMLINKS) {
938 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
941 memset(tmpbuf, 0, sizeof(tmpbuf));
943 strcpy(tmpbuf, elast_comp);
944 if (linkbuf[0] == DIR) {
946 * If this is a symbolic link to an absolute path,
947 * replace what we have by the absolute path.
949 memset(path, 0, strlen(path));
950 memcpy(path, linkbuf, sizeof(linkbuf));
951 strcat(path, tmpbuf);
954 memset(pathtocheck, 0, sizeof(pathtocheck));
958 * If this is a symbolic link to a relative path,
959 * replace only the last component with the link name.
961 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
962 strcat(path, tmpbuf);
964 if ((t = strrchr(pathtocheck, DIR))) {
966 memset(t, 0, strlen(t));
969 memset(pathtocheck, 0, sizeof(pathtocheck));
973 last_comp = elast_comp;
981 /*****************************************/
985 char V = 'V'; /* AFS has problem on SunOS */
987 struct ViceIoctl vio;
992 memset(outbuf, 0, sizeof(outbuf));
994 vio.out_size = sizeof(outbuf);
995 vio.in_size = sizeof(ind);
1000 fd = open("dee.tok",O_WRONLY);
1001 while(!pioctl(0,VIOCGETTOK,&vio,0)) {
1002 write(fd,&outbuf,sizeof(outbuf));
1007 /*****************************************/
1010 static void add_hosts(char *file)
1013 char V = 'V'; /* AFS has problem on SunOS */
1015 struct ViceIoctl vio;
1016 char outbuf[BUFSIZ];
1022 memset(outbuf, 0, sizeof(outbuf));
1024 vio.out_size = sizeof(outbuf);
1029 printf("Getting list of hosts for %s\n", file);
1031 /* Don't worry about errors. */
1032 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1033 phosts = (long *) outbuf;
1036 * Lists hosts that we care about. If ALLHOSTS is defined,
1037 * then all hosts that you ever may possible go through are
1038 * included in this list. If not, then only hosts that are
1039 * the only ones appear. That is, if a volume you must use
1040 * is replaced on only one server, that server is included.
1041 * If it is replicated on many servers, then none are included.
1042 * This is not perfect, but the result is that people don't
1043 * get subscribed to a lot of instances of FILSRV that they
1044 * probably won't need which reduces the instances of
1045 * people getting messages that don't apply to them.
1048 if (phosts[1] != '\0')
1051 for (i = 0; phosts[i]; i++) {
1053 in.s_addr = phosts[i];
1055 printf("Got host %s\n", inet_ntoa(in));
1057 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1059 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1061 printf("Got host %s\n", hp->h_name);
1063 ll_string(&zsublist, ll_s_add, hp->h_name);
1070 * This routine descends through a path to a directory, logging to
1071 * every cell it encounters along the way.
1073 static int auth_to_path(krb5_context context, char *path)
1075 int status = AKLOG_SUCCESS;
1076 int auth_to_cell_status = AKLOG_SUCCESS;
1079 char pathtocheck[MAXPATHLEN + 1];
1080 char mountpoint[MAXPATHLEN + 1];
1089 strcpy(pathtocheck, path);
1091 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1092 fprintf(stderr, "Unable to find current working directory:\n");
1093 fprintf(stderr, "%s\n", pathtocheck);
1094 fprintf(stderr, "Try an absolute pathname.\n");
1095 exit(AKLOG_BADPATH);
1098 strcat(pathtocheck, DIRSTRING);
1099 strcat(pathtocheck, path);
1102 next_path(pathtocheck);
1104 /* Go on to the next level down the path */
1105 while ((nextpath = next_path(NULL))) {
1106 strcpy(pathtocheck, nextpath);
1108 printf("Checking directory %s\n", pathtocheck);
1111 * If this is an afs mountpoint, determine what cell from
1112 * the mountpoint name which is of the form
1113 * #cellname:volumename or %cellname:volumename.
1115 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1116 /* skip over the '#' or '%' */
1117 cell = mountpoint + 1;
1118 /* Add this (cell:volumename) to the list of zsubs */
1120 ll_string(&zsublist, ll_s_add, cell);
1122 add_hosts(pathtocheck);
1123 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1125 if ((auth_to_cell_status = auth_to_cell(context, cell, NULL))) {
1126 if (status == AKLOG_SUCCESS)
1127 status = auth_to_cell_status;
1128 else if (status != auth_to_cell_status)
1129 status = AKLOG_SOMETHINGSWRONG;
1134 if (isdir(pathtocheck, &isdirectory) < 0) {
1136 * If we've logged and still can't stat, there's
1139 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1140 pathtocheck, strerror(errno));
1141 return(AKLOG_BADPATH);
1143 else if (! isdirectory) {
1144 /* Allow only directories */
1145 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1147 return(AKLOG_BADPATH);
1157 /* Print usage message and exit */
1158 static void usage(void)
1160 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1161 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1162 "[[-p | -path] pathname]\n",
1163 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1165 #ifndef HAVE_NO_KRB5_524
1169 fprintf(stderr, " -d gives debugging information.\n");
1170 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1171 fprintf(stderr, " pathname is the name of a directory to which ");
1172 fprintf(stderr, "you wish to authenticate.\n");
1173 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1174 fprintf(stderr, " -hosts gives host address information.\n");
1175 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1176 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1177 fprintf(stderr, " -force means replace identical tickets. \n");
1178 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1179 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1180 #ifndef HAVE_NO_KRB5_524
1181 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1183 fprintf(stderr, " No commandline arguments means ");
1184 fprintf(stderr, "authenticate to the local cell.\n");
1185 fprintf(stderr, "\n");
1189 void aklog(int argc, char *argv[])
1191 krb5_context context;
1192 int status = AKLOG_SUCCESS;
1194 int somethingswrong = FALSE;
1196 cellinfo_t cellinfo;
1198 extern char *progname; /* Name of this program */
1200 extern int dflag; /* Debug mode */
1202 int cmode = FALSE; /* Cellname mode */
1203 int pmode = FALSE; /* Path name mode */
1205 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1206 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1207 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1209 linked_list cells; /* List of cells to log to */
1210 linked_list paths; /* List of paths to log to */
1213 memset(&cellinfo, 0, sizeof(cellinfo));
1215 memset(realm, 0, sizeof(realm));
1216 memset(cell, 0, sizeof(cell));
1217 memset(path, 0, sizeof(path));
1225 /* Store the program name here for error messages */
1226 if ((progname = strrchr(argv[0], DIR)))
1231 krb5_init_context(&context);
1232 initialize_ktc_error_table ();
1234 /* Initialize list of cells to which we have authenticated */
1235 (void)ll_init(&authedcells);
1237 /* Parse commandline arguments and make list of what to do. */
1238 for (i = 1; i < argc; i++) {
1239 if (strcmp(argv[i], "-d") == 0)
1241 else if (strcmp(argv[i], "-noauth") == 0)
1243 else if (strcmp(argv[i], "-zsubs") == 0)
1245 else if (strcmp(argv[i], "-hosts") == 0)
1247 else if (strcmp(argv[i], "-noprdb") == 0)
1249 else if (strcmp(argv[i], "-linked") == 0)
1251 else if (strcmp(argv[i], "-force") == 0)
1253 #ifndef HAVE_NO_KRB5_524
1254 else if (strcmp(argv[i], "-524") == 0)
1257 else if (strcmp(argv[i], "-setpag") == 0)
1259 else if (((strcmp(argv[i], "-cell") == 0) ||
1260 (strcmp(argv[i], "-c") == 0)) && !pmode)
1263 strcpy(cell, argv[i]);
1267 else if ((strcmp(argv[i], "-keytab") == 0))
1273 else if ((strcmp(argv[i], "-principal") == 0))
1279 else if (((strcmp(argv[i], "-path") == 0) ||
1280 (strcmp(argv[i], "-p") == 0)) && !cmode)
1283 strcpy(path, argv[i]);
1288 else if (argv[i][0] == '-')
1290 else if (!pmode && !cmode) {
1291 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1292 (strcmp(argv[i], "..") == 0)) {
1294 strcpy(path, argv[i]);
1298 strcpy(cell, argv[i]);
1305 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1308 strcpy(realm, argv[i]);
1312 /* Add this cell to list of cells */
1313 strcpy(cellinfo.cell, cell);
1314 strcpy(cellinfo.realm, realm);
1315 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1317 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1318 ll_add_data(cur_node, new_cellinfo);
1321 "%s: failure copying cellinfo.\n", progname);
1326 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1330 memset(&cellinfo, 0, sizeof(cellinfo));
1332 memset(cell, 0, sizeof(cell));
1333 memset(realm, 0, sizeof(realm));
1336 /* Add this path to list of paths */
1337 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1339 if ((new_path = strdup(path)))
1340 ll_add_data(cur_node, new_path);
1342 fprintf(stderr, "%s: failure copying path name.\n",
1348 fprintf(stderr, "%s: failure adding path to paths list.\n",
1353 memset(path, 0, sizeof(path));
1358 * The code that _used_ to be here called setpag(). When you think
1359 * about this, doing this makes no sense! setpag() allocates a PAG
1360 * only for the current process, so the token installed would have
1361 * not be usable in the parent! Since ktc_SetToken() now takes a
1362 * 4th argument to control whether or not we're going to allocate
1363 * a PAG (and since when you do it _that_ way, it modifies the cred
1364 * structure of your parent)), why don't we use that instead?
1375 printf("setpag %d\n",status);
1376 j = getgroups(gidsetlen,gidset);
1377 printf("Groups(%d):",j);
1378 for (i = 0; i<j; i++) {
1379 printf("%d",gidset[i]);
1380 if((i+1)<j) printf(",");
1386 /* If nothing was given, log to the local cell. */
1387 if ((cells.nelements + paths.nelements) == 0) {
1390 status = auth_to_cell(context, NULL, NULL);
1392 /* If this cell is linked to a DCE cell, and user
1393 * requested -linked, get tokens for both
1394 * This is very usefull when the AFS cell is linked to a DFS
1395 * cell and this system does not also have DFS.
1398 if (!status && linked && linkedcell[0]) {
1399 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1401 printf("Linked cell: %s\n", linkedcell);
1403 status = auth_to_cell(context, linkedcell2, NULL);
1407 * Local hack - if the person has a file in their home
1408 * directory called ".xlog", read that for a list of
1409 * extra cells to authenticate to
1412 if ((pwd = getpwuid(getuid())) != NULL) {
1415 char fcell[100], xlog_path[512];
1417 strcpy(xlog_path, pwd->pw_dir);
1418 strcat(xlog_path, "/.xlog");
1420 if ((stat(xlog_path, &sbuf) == 0) &&
1421 ((f = fopen(xlog_path, "r")) != NULL)) {
1424 printf("Reading %s for cells to "
1425 "authenticate to.\n", xlog_path);
1428 while (fgets(fcell, 100, f) != NULL) {
1431 fcell[strlen(fcell) - 1] = '\0';
1434 printf("Found cell %s in %s.\n",
1438 auth_status = auth_to_cell(context, fcell, NULL);
1439 if (status == AKLOG_SUCCESS)
1440 status = auth_status;
1442 status = AKLOG_SOMETHINGSWRONG;
1448 /* Log to all cells in the cells list first */
1449 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1450 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1451 if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm)))
1454 if (linked && linkedcell[0]) {
1455 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1457 printf("Linked cell: %s\n",
1460 if ((status = auth_to_cell(context,linkedcell2,
1467 /* Then, log to all paths in the paths list */
1468 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1469 if ((status = auth_to_path(context, cur_node->data)))
1474 * If only one thing was logged to, we'll return the status
1475 * of the single call. Otherwise, we'll return a generic
1476 * something failed status.
1478 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1479 status = AKLOG_SOMETHINGSWRONG;
1482 /* If we are keeping track of zephyr subscriptions, print them. */
1484 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1485 printf("zsub: %s\n", cur_node->data);
1488 /* If we are keeping track of host information, print it. */
1490 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1491 printf("host: %s\n", cur_node->data);
1497 static int isdir(char *path, unsigned char *val)
1499 struct stat statbuf;
1501 if (lstat(path, &statbuf) < 0)
1504 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1512 static krb5_error_code get_credv5_akimpersonate(krb5_context context,
1514 krb5_principal service_principal,
1515 krb5_principal client_principal,
1518 int *allowed_enctypes,
1520 krb5_creds** out_creds /* out */ )
1522 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1523 krb5_error_code code;
1525 krb5_kt_cursor cursor[1];
1526 krb5_keytab_entry entry[1];
1528 krb5_creds *creds = 0;
1529 krb5_enctype enctype;
1531 krb5_keyblock session_key[1];
1533 Ticket ticket_reply[1];
1534 EncTicketPart enc_tkt_reply[1];
1535 krb5_address address[30];
1536 krb5_addresses faddr[1];
1538 time_t temp_time[2];
1540 krb5_ticket ticket_reply[1];
1541 krb5_enc_tkt_part enc_tkt_reply[1];
1542 krb5_address address[30], *faddr[30];
1546 static int any_enctype[] = {0};
1548 if (!(creds = malloc(sizeof *creds))) {
1552 if (!allowed_enctypes)
1553 allowed_enctypes = any_enctype;
1556 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1557 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1558 memset((char*)creds, 0, sizeof *creds);
1559 memset((char*)entry, 0, sizeof *entry);
1560 memset((char*)session_key, 0, sizeof *session_key);
1561 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1562 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1563 code = krb5_kt_resolve(context, keytab, &kt);
1566 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1568 afs_com_err(progname, code, "while resolving default keytab");
1572 if (service_principal) {
1573 for (i = 0; (enctype = allowed_enctypes[i]) || !i; ++i) {
1574 code = krb5_kt_get_entry(context,
1581 if (allowed_enctypes[i])
1582 deref_keyblock_enctype(session_key) = allowed_enctypes[i];
1587 afs_com_err(progname, code,"while scanning keytab entries");
1591 krb5_keytab_entry new[1];
1593 memset(new, 0, sizeof *new);
1594 if ((code == krb5_kt_start_seq_get(context, kt, cursor))) {
1595 afs_com_err(progname, code, "while starting keytab scan");
1598 while (!(code = krb5_kt_next_entry(context, kt, new, cursor))) {
1600 allowed_enctypes[i] && allowed_enctypes[i]
1601 != deref_entry_enctype(new); ++i)
1603 if ((!i || allowed_enctypes[i]) &&
1604 (best < 0 || best > i)) {
1605 krb5_free_keytab_entry_contents(context, entry);
1607 memset(new, 0, sizeof *new);
1608 } else krb5_free_keytab_entry_contents(context, new);
1610 if ((i = krb5_kt_end_seq_get(context, kt, cursor))) {
1611 afs_com_err(progname, i, "while ending keytab scan");
1616 afs_com_err(progname, code, "while scanning keytab");
1619 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1625 if ((code = krb5_generate_random_keyblock(context,
1626 deref_keyblock_enctype(session_key), session_key))) {
1627 afs_com_err(progname, code, "while making session key");
1630 enc_tkt_reply->flags.initial = 1;
1631 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1632 enc_tkt_reply->cname = client_principal->name;
1633 enc_tkt_reply->crealm = client_principal->realm;
1634 enc_tkt_reply->key = *session_key;
1636 static krb5_data empty_string;
1637 enc_tkt_reply->transited.contents = empty_string;
1639 enc_tkt_reply->authtime = starttime;
1640 enc_tkt_reply->starttime = temp_time;
1641 *enc_tkt_reply->starttime = starttime;
1643 enc_tkt_reply->renew_till = temp_time + 1;
1644 *enc_tkt_reply->renew_till = endtime;
1646 enc_tkt_reply->endtime = endtime;
1648 if ((code = krb5_c_make_random_key(context,
1649 deref_keyblock_enctype(session_key), session_key))) {
1650 afs_com_err(progname, code, "while making session key");
1653 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1654 #define DATACAST (unsigned char *)
1655 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1656 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1657 enc_tkt_reply->session = session_key;
1658 enc_tkt_reply->client = client_principal;
1660 static krb5_data empty_string;
1661 enc_tkt_reply->transited.tr_contents = empty_string;
1663 enc_tkt_reply->times.authtime = starttime;
1664 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1665 enc_tkt_reply->times.endtime = endtime;
1666 #endif /* USING_HEIMDAL */
1667 /* NB: We will discard address for now--ignoring caddr field
1668 in any case. MIT branch does what it always did. */
1670 if (paddress && *paddress) {
1671 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1674 faddr->val = address;
1676 for (i = 0; paddress[i]; ++i) {
1678 address[i].addr_type = KRB5_ADDRESS_INET;
1679 address[i].address.data = (void*)(paddress+i);
1680 address[i].address.length = sizeof(paddress[i]);
1683 address[i].magic = KV5M_ADDRESS;
1684 address[i].addrtype = ADDRTYPE_INET;
1686 address[i].addrtype = AF_INET;
1688 address[i].contents = (void*)(paddress+i);
1689 address[i].length = sizeof(int);
1690 faddr[i] = address+i;
1701 ticket_reply->sname = service_principal->name;
1702 ticket_reply->realm = service_principal->realm;
1704 { /* crypto block */
1705 krb5_crypto crypto = 0;
1706 unsigned char *buf = 0;
1707 size_t buf_size, buf_len;
1710 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1711 enc_tkt_reply, &buf_len, code);
1713 afs_com_err(progname, code, "while encoding ticket");
1717 if(buf_len != buf_size) {
1718 afs_com_err(progname, code,
1719 "%d != %d while encoding ticket (internal ASN.1 encoder error",
1723 what = "krb5_crypto_init";
1724 code = krb5_crypto_init(context,
1725 &deref_entry_keyblock(entry),
1726 deref_entry_enctype(entry),
1729 what = "krb5_encrypt";
1730 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
1731 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
1734 if (crypto) krb5_crypto_destroy(context, crypto);
1736 afs_com_err(progname, code, "while %s", what);
1739 } /* crypto block */
1740 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
1741 ticket_reply->enc_part.kvno = temp_vno;
1742 *ticket_reply->enc_part.kvno = entry->vno;
1743 ticket_reply->tkt_vno = 5;
1745 ticket_reply->server = service_principal;
1746 ticket_reply->enc_part2 = enc_tkt_reply;
1747 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
1748 afs_com_err(progname, code, "while making ticket");
1751 ticket_reply->enc_part.kvno = entry->vno;
1754 /* Construct Creds */
1756 if ((code = krb5_copy_principal(context, service_principal,
1758 afs_com_err(progname, code, "while copying service principal");
1761 if ((code = krb5_copy_principal(context, client_principal,
1763 afs_com_err(progname, code, "while copying client principal");
1766 if ((code = krb5_copy_keyblock_contents(context, session_key,
1767 &deref_session_key(creds)))) {
1768 afs_com_err(progname, code, "while copying session key");
1773 creds->times.authtime = enc_tkt_reply->authtime;
1774 creds->times.starttime = *(enc_tkt_reply->starttime);
1775 creds->times.endtime = enc_tkt_reply->endtime;
1776 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
1777 creds->flags.b = enc_tkt_reply->flags;
1779 creds->times = enc_tkt_reply->times;
1780 creds->ticket_flags = enc_tkt_reply->flags;
1782 if (!deref_enc_tkt_addrs(enc_tkt_reply))
1784 else if ((code = krb5_copy_addresses(context,
1785 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
1786 afs_com_err(progname, code, "while copying addresses");
1792 size_t creds_tkt_len;
1793 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
1794 ticket_reply, &creds_tkt_len, code);
1796 afs_com_err(progname, code, "while encoding ticket");
1801 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
1802 afs_com_err(progname, code, "while encoding ticket");
1805 creds->ticket = *temp;
1812 if (deref_enc_data(&ticket_reply->enc_part))
1813 free(deref_enc_data(&ticket_reply->enc_part));
1814 krb5_free_keytab_entry_contents(context, entry);
1815 if (client_principal)
1816 krb5_free_principal(context, client_principal);
1817 if (service_principal)
1818 krb5_free_principal(context, service_principal);
1820 krb5_cc_close(context, cc);
1822 krb5_kt_close(context, kt);
1823 if (creds) krb5_free_creds(context, creds);
1824 krb5_free_keyblock_contents(context, session_key);
1833 static krb5_error_code get_credv5(krb5_context context,
1834 char *name, char *inst, char *realm,
1839 static krb5_principal client_principal = 0;
1842 printf("Getting tickets: %s%s%s@%s\n", name, (inst && inst[0])
1843 ? "/" : "", inst ? inst : "", realm);
1846 memset((char *)&increds, 0, sizeof(increds));
1847 /* ANL - instance may be ptr to a null string. Pass null then */
1848 if ((r = krb5_build_principal(context, &increds.server,
1849 strlen(realm), realm,
1851 (inst && strlen(inst)) ? inst : (void *) NULL,
1857 if (!_krb425_ccache) {
1858 r = krb5_cc_default(context, &_krb425_ccache);
1862 if (!client_principal) {
1864 r = krb5_parse_name(context, client, &client_principal);
1866 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1872 increds.client = client_principal;
1873 increds.times.endtime = 0;
1874 /* Ask for DES since that is what V4 understands */
1875 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
1878 int allowed_enctypes[] = {
1879 ENCTYPE_DES_CBC_CRC, 0
1882 r = get_credv5_akimpersonate(context,
1891 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
1897 static int get_user_realm(krb5_context context, char *realm)
1899 static krb5_principal client_principal = 0;
1901 krb5_error_code r = 0;
1903 if (!_krb425_ccache)
1904 krb5_cc_default(context, &_krb425_ccache);
1905 if (!client_principal) {
1907 r = krb5_parse_name(context, client, &client_principal);
1909 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1915 i = realm_len(context, client_principal);
1916 if (i > REALM_SZ-1) i = REALM_SZ-1;
1917 strncpy(realm,realm_data(context, client_principal), i);