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>
42 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/errno.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
67 #if defined(HAVE_ET_COM_ERR_H)
68 #include <et/com_err.h>
73 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
74 #include <afs/com_err.h>
77 #include <afs/param.h>
79 #include <sys/ioccom.h>
82 /* Prevent inclusion of des.h to avoid conflicts with des types */
83 #define NO_DES_H_INCLUDE
86 #include <afs/cellconfig.h>
88 #include <afs/venus.h>
89 #include <afs/ptserver.h>
90 #include <afs/ptuser.h>
91 #include <afs/dirpath.h>
94 #include "linked_list.h"
96 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
99 #ifdef HAVE_KRB5_CREDS_SESSION
100 #define USING_HEIMDAL 1
106 #ifndef AFS_TRY_FULL_PRINC
107 #define AFS_TRY_FULL_PRINC 1
108 #endif /* AFS_TRY_FULL_PRINC */
110 #define AKLOG_TRYAGAIN -1
111 #define AKLOG_SUCCESS 0
112 #define AKLOG_USAGE 1
113 #define AKLOG_SOMETHINGSWRONG 2
115 #define AKLOG_KERBEROS 4
116 #define AKLOG_TOKEN 5
117 #define AKLOG_BADPATH 6
133 /* RedHat 4.x doesn't seem to define this */
134 #define MAXSYMLINKS 5
137 #define DIR '/' /* Character that divides directories */
138 #define DIRSTRING "/" /* String form of above */
139 #define VOLMARKER ':' /* Character separating cellname from mntpt */
140 #define VOLMARKERSTRING ":" /* String form of above */
144 char realm[REALM_SZ];
147 static krb5_ccache _krb425_ccache = NULL;
150 * Why doesn't AFS provide these prototypes?
153 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
159 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *, int);
160 static int isdir(char *, unsigned char *);
161 static krb5_error_code get_credv5(krb5_context context, char *, char *,
162 char *, krb5_creds **);
163 static int get_user_realm(krb5_context, char **);
165 #define TRYAGAIN(x) (x == AKLOG_TRYAGAIN || \
166 x == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || \
167 x == KRB5KRB_ERR_GENERIC)
169 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
171 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
172 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
173 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
174 #define realm_data(c, p) krb5_princ_realm(c, p)->data
175 #define realm_len(c, p) krb5_princ_realm(c, p)->length
177 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
179 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
180 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
181 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
182 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
183 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
186 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
189 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
190 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
194 krb5_encrypt_tkt_part(krb5_context context,
195 const krb5_keyblock *key,
202 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
204 if ((code = krb5_c_encrypt_length(context, key->enctype,
205 data->length, &enclen)))
207 ticket->enc_part.ciphertext.length = enclen;
208 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
212 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
213 0, data, &ticket->enc_part))) {
214 free(ticket->enc_part.ciphertext.data);
215 ticket->enc_part.ciphertext.data = 0;
227 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
229 #define get_cred_keydata(c) c->keyblock.contents
230 #define get_cred_keylen(c) c->keyblock.length
231 #define get_creds_enctype(c) c->keyblock.enctype
233 #elif defined(HAVE_KRB5_CREDS_SESSION)
235 #define get_cred_keydata(c) c->session.keyvalue.data
236 #define get_cred_keylen(c) c->session.keyvalue.length
237 #define get_creds_enctype(c) c->session.keytype
240 #error "Must have either keyblock or session member of krb5_creds"
243 #if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
244 #define krb5_524_convert_creds krb524_convert_creds_kdc
245 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
246 #define HAVE_NO_KRB5_524
250 #define deref_keyblock_enctype(kb) \
253 #define deref_entry_keyblock(entry) \
256 #define deref_session_key(creds) \
259 #define deref_enc_tkt_addrs(tkt) \
262 #define deref_enc_length(enc) \
263 ((enc)->cipher.length)
265 #define deref_enc_data(enc) \
268 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
271 #define deref_keyblock_enctype(kb) \
274 #define deref_entry_keyblock(entry) \
277 #define deref_session_key(creds) \
280 #define deref_enc_tkt_addrs(tkt) \
283 #define deref_enc_length(enc) \
284 ((enc)->ciphertext.length)
286 #define deref_enc_data(enc) \
287 ((enc)->ciphertext.data)
291 #define deref_entry_enctype(entry) \
292 deref_keyblock_enctype(&deref_entry_keyblock(entry))
295 * Provide a replacement for strerror if we don't have it
298 #ifndef HAVE_STRERROR
299 extern char *sys_errlist[];
300 #define strerror(x) sys_errlist[x]
301 #endif /* HAVE_STRERROR */
303 static char *progname = NULL; /* Name of this program */
304 static int dflag = FALSE; /* Give debugging information */
305 static int noauth = FALSE; /* If true, don't try to get tokens */
306 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
307 static int hosts = FALSE; /* Are we keeping track of hosts? */
308 static int noprdb = FALSE; /* Skip resolving name to id? */
309 static int linked = FALSE; /* try for both AFS nodes */
310 static int afssetpag = FALSE; /* setpag for AFS */
311 static int force = FALSE; /* Bash identical tokens? */
312 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
313 static char *keytab = NULL; /* keytab for akimpersonate */
314 static char *client = NULL; /* client principal for akimpersonate */
315 static linked_list zsublist; /* List of zephyr subscriptions */
316 static linked_list hostlist; /* List of host addresses */
317 static linked_list authedcells; /* List of cells already logged to */
319 /* A com_error bodge. The idea here is that this routine lets us lookup
320 * things in the system com_err, if the AFS one just tells us the error
325 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
332 const char *str = afs_error_message(code);
333 if (strncmp(str, "unknown", strlen("unknown")) == 0) {
334 str = error_message(code);
340 vfprintf(stderr, fmt, ap);
347 copy_cellinfo(cellinfo_t *cellinfo)
349 cellinfo_t *new_cellinfo;
351 if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
352 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
354 return ((char *)new_cellinfo);
359 get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
361 int status = AKLOG_SUCCESS;
362 struct afsconf_dir *configdir;
364 memset(cellconfig, 0, sizeof(*cellconfig));
366 *local_cell = malloc(MAXCELLCHARS);
367 if (*local_cell == NULL) {
368 fprintf(stderr, "%s: can't allocate memory for local cell name\n",
373 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
375 "%s: can't get afs configuration (afsconf_Open(%s))\n",
376 progname, AFSDIR_CLIENT_ETC_DIRPATH);
380 if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
381 fprintf(stderr, "%s: can't determine local cell.\n", progname);
385 if ((cell == NULL) || (cell[0] == 0))
388 /* XXX - This function modifies 'cell' by passing it through lcstring */
389 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
390 fprintf(stderr, "%s: Can't get information about cell %s.\n",
395 afsconf_Close(configdir);
401 extract_realm(krb5_context context, krb5_principal princ) {
405 len = realm_len(context, princ);
406 if (len > REALM_SZ-1)
409 realm = malloc(sizeof(char) * (len+1));
413 strncpy(realm, realm_data(context, princ), len);
420 get_realm_from_cred(krb5_context context, krb5_creds *v5cred, char **realm) {
421 #if !defined(HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
422 krb5_error_code code;
427 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
431 *realm = extract_realm(context, ticket->server);
435 krb5_free_ticket(context, ticket);
445 * Log to a cell. If the cell has already been logged to, return without
446 * doing anything. Otherwise, log to it and mark that it has been logged
450 auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
452 int status = AKLOG_SUCCESS;
453 char username[BUFSIZ]; /* To hold client username structure */
454 afs_int32 viceId; /* AFS uid of user */
456 char *realm_of_user = NULL; /* Kerberos realm of user */
457 char *realm_from_princ = NULL; /* Calculated realm data */
458 char *realm_of_cell = NULL; /* Pointer to realm we're using */
459 int retry; /* round, and round we go ... */
461 char *local_cell = NULL;
462 krb5_creds *v5cred = NULL;
463 struct ktc_principal aserver;
464 struct ktc_principal aclient;
465 struct ktc_token atoken, btoken;
466 struct afsconf_cell cellconf;
468 /* NULL or empty cell returns information on local cell */
469 if ((status = get_cellconfig(cell, &cellconf, &local_cell)))
472 if (linkedcell != NULL && cellconf.linkedCell != NULL) {
473 *linkedcell = strdup(cellconf.linkedCell);
474 if (*linkedcell == NULL) {
480 if (ll_string(&authedcells, ll_s_check, cellconf.name)) {
482 printf("Already authenticated to %s (or tried to)\n",
485 status = AKLOG_SUCCESS;
490 * Record that we have attempted to log to this cell. We do this
491 * before we try rather than after so that we will not try
492 * and fail repeatedly for one cell.
494 ll_string(&authedcells, ll_s_add, cellconf.name);
497 * Record this cell in the list of zephyr subscriptions. We may
498 * want zephyr subscriptions even if authentication fails.
499 * If this is done after we attempt to get tokens, aklog -zsubs
500 * can return something different depending on whether or not we
501 * are in -noauth mode.
503 if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
505 "%s: failure adding cell %s to zephyr subscriptions list.\n",
506 progname, cellconf.name);
509 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
511 "%s: failure adding cell %s to zephyr subscriptions list.\n",
512 progname, local_cell);
518 printf("Authenticating to cell %s (server %s).\n",
519 cellconf.name, cellconf.hostName[0]);
522 if ((status = get_user_realm(context, &realm_of_user))) {
523 fprintf(stderr, "%s: Couldn't determine realm of user:)",
525 afs_com_err(progname, status, " while getting realm");
526 status = AKLOG_KERBEROS;
534 /* This code tries principals in the following, much debated,
537 * If the realm is specified on the command line we do
538 * - afs/cell@COMMAND-LINE-REALM
539 * - afs@COMMAND-LINE-REALM
542 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
543 * - afs/cell@krb5_get_host_realm(db-server)
544 * Then, if krb5_get_host_realm(db-server) is non-empty
545 * - afs@ krb5_get_host_realm(db-server)
547 * - afs/cell@ upper-case-domain-of-db-server
548 * - afs@ upper-case-domain-of-db-server
550 * In all cases, the 'afs@' variant is only tried where the
551 * cell and the realm match case-insensitively.
554 /* Cell on command line - use that one */
555 if (realm && realm[0]) {
556 realm_of_cell = realm;
557 status = AKLOG_TRYAGAIN;
559 printf("We were told to authenticate to realm %s.\n",
563 /* Initially, try using afs/cell@USERREALM */
565 printf("Trying to authenticate to user's realm %s.\n",
569 realm_of_cell = realm_of_user;
570 status = get_credv5(context, AFSKEY, cellconf.name,
571 realm_of_cell, &v5cred);
573 /* If that failed, try to determine the realm from the name of
574 * one of the DB servers */
575 if (TRYAGAIN(status)) {
576 realm_of_cell = afs_realm_of_cell(context, &cellconf,
578 if (!realm_of_cell) {
580 "%s: Couldn't figure out realm for cell %s.\n",
581 progname, cellconf.name);
586 if (realm_of_cell[0])
587 printf("We've deduced that we need to authenticate"
588 " to realm %s.\n", realm_of_cell);
590 printf("We've deduced that we need to authenticate "
591 "using referrals.\n");
596 if (TRYAGAIN(status)) {
597 /* If we've got the full-princ-first option, or we're in a
598 * different realm from the cell - use the cell name as the
600 if (AFS_TRY_FULL_PRINC ||
601 strcasecmp(cellconf.name, realm_of_cell)!=0) {
602 status = get_credv5(context, AFSKEY, cellconf.name,
603 realm_of_cell, &v5cred);
605 /* If we failed & we've got an empty realm, then try
606 * calling afs_realm_for_cell again. */
607 if (TRYAGAIN(status) && !realm_of_cell[0]) {
608 /* This time, get the realm by taking the domain
609 * component of the db server and make it upper case */
610 realm_of_cell = afs_realm_of_cell(context,
612 if (!realm_of_cell) {
614 "%s: Couldn't figure out realm for cell "
615 "%s.\n", progname, cellconf.name);
619 printf("We've deduced that we need to authenticate"
620 " to realm %s.\n", realm_of_cell);
623 status = get_credv5(context, AFSKEY, cellconf.name,
624 realm_of_cell, &v5cred);
627 /* If the realm and cell name match, then try without an
628 * instance, but only if realm is non-empty */
630 if (TRYAGAIN(status) &&
631 strcasecmp(cellconf.name, realm_of_cell) == 0) {
632 status = get_credv5(context, AFSKEY, NULL,
633 realm_of_cell, &v5cred);
634 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
635 status = get_credv5(context, AFSKEY, cellconf.name,
636 realm_of_cell, &v5cred);
641 /* Try to find a service principal for this cell.
642 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
643 * the first attempt, so we try twice to be sure */
645 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
653 printf("Kerberos error code returned by get_cred : %d\n",
656 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
657 progname, cellconf.name);
658 afs_com_err(progname, status, "while getting AFS tickets");
659 status = AKLOG_KERBEROS;
663 /* If we've got a valid ticket, and we still don't know the realm name
664 * try to figure it out from the contents of the ticket
666 if (strcmp(realm_of_cell, "") == 0) {
667 status = get_realm_from_cred(context, v5cred, &realm_from_princ);
670 "%s: Couldn't decode ticket to determine realm for "
672 progname, cellconf.name);
674 if (realm_from_princ)
675 realm_of_cell = realm_from_princ;
679 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
680 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
681 strncpy(aserver.cell, cellconf.name, MAXKTCREALMLEN - 1);
684 * The default is to use rxkad2b, which means we put in a full
685 * V5 ticket. If the user specifies -524, we talk to the
686 * 524 ticket converter.
690 char k4name[ANAME_SZ], k4inst[INST_SZ], k4realm[REALM_SZ];
691 #ifdef HAVE_NO_KRB5_524
697 printf("Using Kerberos V5 ticket natively\n");
699 #ifndef HAVE_NO_KRB5_524
700 status = krb5_524_conv_principal (context, v5cred->client,
705 afs_com_err(progname, status, "while converting principal "
706 "to Kerberos V4 format");
707 status = AKLOG_KERBEROS;
710 strcpy (username, k4name);
712 strcat (username, ".");
713 strcat (username, k4inst);
716 len = min(get_princ_len(context, v5cred->client, 0),
717 second_comp(context, v5cred->client) ?
718 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
719 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
720 username[len] = '\0';
722 if (second_comp(context, v5cred->client)) {
723 strcat(username, ".");
724 p = username + strlen(username);
725 len = min(get_princ_len(context, v5cred->client, 1),
726 MAXKTCNAMELEN - strlen(username) - 1);
727 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
732 memset(&atoken, 0, sizeof(atoken));
733 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
734 atoken.startTime = v5cred->times.starttime;;
735 atoken.endTime = v5cred->times.endtime;
736 memcpy(&atoken.sessionKey, get_cred_keydata(v5cred),
737 get_cred_keylen(v5cred));
738 atoken.ticketLen = v5cred->ticket.length;
739 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
740 #ifndef HAVE_NO_KRB5_524
745 printf("Using Kerberos 524 translator service\n");
747 status = krb5_524_convert_creds(context, v5cred, &cred);
750 afs_com_err(progname, status, "while converting tickets "
751 "to Kerberos V4 format");
752 status = AKLOG_KERBEROS;
756 strcpy (username, cred.pname);
758 strcat (username, ".");
759 strcat (username, cred.pinst);
762 atoken.kvno = cred.kvno;
763 atoken.startTime = cred.issue_date;
765 * It seems silly to go through a bunch of contortions to
766 * extract the expiration time, when the v5 credentials already
767 * has the exact time! Let's use that instead.
769 * Note that this isn't a security hole, as the expiration time
770 * is also contained in the encrypted token
772 atoken.endTime = v5cred->times.endtime;
773 memcpy(&atoken.sessionKey, cred.session, 8);
774 atoken.ticketLen = cred.ticket_st.length;
775 memcpy(atoken.ticket, cred.ticket_st.dat, atoken.ticketLen);
776 #endif /* HAVE_NO_KRB5_524 */
780 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
781 atoken.kvno == btoken.kvno &&
782 atoken.ticketLen == btoken.ticketLen &&
783 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
784 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen)) {
787 printf("Identical tokens already exist; skipping.\n");
789 status = AKLOG_SUCCESS;
799 printf("Not resolving name %s to id (-noprdb set)\n",
804 if (strcmp(realm_of_user, realm_of_cell)) {
805 strcat(username, "@");
806 strcat(username, realm_of_user);
810 printf("About to resolve name %s to id in cell %s.\n",
811 username, aserver.cell);
814 if (!pr_Initialize (0, AFSDIR_CLIENT_ETC_DIRPATH, aserver.cell))
815 status = pr_SNameToId (username, &viceId);
819 printf("Error %d\n", status);
821 printf("Id %d\n", (int) viceId);
826 * This code is taken from cklog -- it lets people
827 * automatically register with the ptserver in foreign cells
830 #ifdef ALLOW_REGISTER
831 if ((status == 0) && (viceId == ANONYMOUSID) &&
832 (strcmp(realm_of_user, realm_of_cell) != 0)) {
834 printf("doing first-time registration of %s "
835 "at %s\n", username, cellconf.name);
838 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
839 strcpy(aclient.instance, "");
840 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
841 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
842 afs_com_err(progname, status,
843 "while obtaining tokens for cell %s",
845 status = AKLOG_TOKEN;
849 * In case you're wondering, we don't need to change the
850 * filename here because we're still connecting to the
851 * same cell -- we're just using a different authentication
855 if ((status = pr_Initialize(1L, AFSDIR_CLIENT_ETC_DIRPATH,
857 printf("Error %d\n", status);
860 if ((status = pr_CreateUser(username, &viceId))) {
861 fprintf(stderr, "%s: %s so unable to create remote PTS "
862 "user %s in cell %s (status: %d).\n", progname,
863 afs_error_message(status), username, cellconf.name,
865 viceId = ANONYMOUSID;
867 printf("created cross-cell entry for %s (Id %d) at %s\n",
868 username, viceId, cellconf.name);
871 #endif /* ALLOW_REGISTER */
874 * This is a crock, but it is Transarc's crock, so we have to play
875 * along in order to get the functionality. The way the afs id is
876 * stored is as a string in the username field of the token.
877 * Contrary to what you may think by looking at the code for
878 * tokens, this hack (AFS ID %d) will not work if you change %d
882 if ((status == 0) && (viceId != ANONYMOUSID)) {
883 sprintf(username, "AFS ID %d", (int) viceId);
888 fprintf(stdout, "Set username to %s\n", username);
891 /* Reset the "aclient" structure before we call ktc_SetToken.
892 * This structure was first set by the ktc_GetToken call when
893 * we were comparing whether identical tokens already existed.
895 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
896 strcpy(aclient.instance, "");
897 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
900 printf("Setting tokens. %s / %s @ %s \n",
901 aclient.name, aclient.instance, aclient.cell );
903 #ifndef AFS_AIX51_ENV
904 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
905 * this routine, it will not add the token. It is not clear what
906 * is going on here! So we will do the following operation.
907 * On AIX 5, it causes the parent program to die, so we won't.
909 write(2,"",0); /* dummy write */
911 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
912 afs_com_err(progname, status, "while obtaining tokens for cell %s",
914 status = AKLOG_TOKEN;
919 printf("Noauth mode; not authenticating.\n");
925 if (realm_from_princ)
926 free(realm_from_princ);
934 get_afs_mountpoint(char *file, char *mountpoint, int size)
937 char V ='V'; /* AFS has problem on Sun with pioctl */
939 char our_file[MAXPATHLEN + 1];
941 char *last_component;
942 struct ViceIoctl vio;
943 char cellname[BUFSIZ];
945 memset(our_file, 0, sizeof(our_file));
946 strcpy(our_file, file);
948 if ((last_component = strrchr(our_file, DIR))) {
949 *last_component++ = 0;
950 parent_dir = our_file;
953 last_component = our_file;
957 memset(cellname, 0, sizeof(cellname));
959 vio.in = last_component;
960 vio.in_size = strlen(last_component)+1;
962 vio.out = mountpoint;
964 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
965 if (strchr(mountpoint, VOLMARKER) == NULL) {
967 vio.in_size = strlen(file) + 1;
968 vio.out_size = sizeof(cellname);
971 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
972 strcat(cellname, VOLMARKERSTRING);
973 strcat(cellname, mountpoint + 1);
974 memset(mountpoint + 1, 0, size - 1);
975 strcpy(mountpoint + 1, cellname);
985 * This routine each time it is called returns the next directory
986 * down a pathname. It resolves all symbolic links. The first time
987 * it is called, it should be called with the name of the path
988 * to be descended. After that, it should be called with the arguemnt
992 next_path(char *origpath)
994 static char path[MAXPATHLEN + 1];
995 static char pathtocheck[MAXPATHLEN + 1];
997 int link = FALSE; /* Is this a symbolic link? */
998 char linkbuf[MAXPATHLEN + 1];
999 char tmpbuf[MAXPATHLEN + 1];
1001 static char *last_comp; /* last component of directory name */
1002 static char *elast_comp; /* End of last component */
1006 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1008 /* If we are given something for origpath, we are initializing only. */
1010 memset(path, 0, sizeof(path));
1011 memset(pathtocheck, 0, sizeof(pathtocheck));
1012 strcpy(path, origpath);
1018 /* We were not given origpath; find then next path to check */
1020 /* If we've gotten all the way through already, return NULL */
1021 if (last_comp == NULL)
1025 while (*last_comp == DIR)
1026 strncat(pathtocheck, last_comp++, 1);
1027 len = (elast_comp = strchr(last_comp, DIR))
1028 ? elast_comp - last_comp : strlen(last_comp);
1029 strncat(pathtocheck, last_comp, len);
1030 memset(linkbuf, 0, sizeof(linkbuf));
1031 if ((link = (readlink(pathtocheck, linkbuf,
1032 sizeof(linkbuf)) > 0))) {
1033 if (++symlinkcount > MAXSYMLINKS) {
1034 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1035 exit(AKLOG_BADPATH);
1037 memset(tmpbuf, 0, sizeof(tmpbuf));
1039 strcpy(tmpbuf, elast_comp);
1040 if (linkbuf[0] == DIR) {
1042 * If this is a symbolic link to an absolute path,
1043 * replace what we have by the absolute path.
1045 memset(path, 0, strlen(path));
1046 memcpy(path, linkbuf, sizeof(linkbuf));
1047 strcat(path, tmpbuf);
1050 memset(pathtocheck, 0, sizeof(pathtocheck));
1054 * If this is a symbolic link to a relative path,
1055 * replace only the last component with the link name.
1057 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1058 strcat(path, tmpbuf);
1060 if ((t = strrchr(pathtocheck, DIR))) {
1062 memset(t, 0, strlen(t));
1065 memset(pathtocheck, 0, sizeof(pathtocheck));
1069 last_comp = elast_comp;
1073 return(pathtocheck);
1077 add_hosts(char *file)
1080 char V = 'V'; /* AFS has problem on SunOS */
1082 struct ViceIoctl vio;
1083 char outbuf[BUFSIZ];
1089 memset(outbuf, 0, sizeof(outbuf));
1091 vio.out_size = sizeof(outbuf);
1096 printf("Getting list of hosts for %s\n", file);
1098 /* Don't worry about errors. */
1099 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1100 phosts = (long *) outbuf;
1103 * Lists hosts that we care about. If ALLHOSTS is defined,
1104 * then all hosts that you ever may possible go through are
1105 * included in this list. If not, then only hosts that are
1106 * the only ones appear. That is, if a volume you must use
1107 * is replaced on only one server, that server is included.
1108 * If it is replicated on many servers, then none are included.
1109 * This is not perfect, but the result is that people don't
1110 * get subscribed to a lot of instances of FILSRV that they
1111 * probably won't need which reduces the instances of
1112 * people getting messages that don't apply to them.
1115 if (phosts[1] != '\0')
1118 for (i = 0; phosts[i]; i++) {
1120 in.s_addr = phosts[i];
1122 printf("Got host %s\n", inet_ntoa(in));
1124 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1126 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1128 printf("Got host %s\n", hp->h_name);
1130 ll_string(&zsublist, ll_s_add, hp->h_name);
1137 * This routine descends through a path to a directory, logging to
1138 * every cell it encounters along the way.
1141 auth_to_path(krb5_context context, char *path)
1143 int status = AKLOG_SUCCESS;
1144 int auth_status = AKLOG_SUCCESS;
1147 char pathtocheck[MAXPATHLEN + 1];
1148 char mountpoint[MAXPATHLEN + 1];
1157 strcpy(pathtocheck, path);
1159 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1160 fprintf(stderr, "Unable to find current working directory:\n");
1161 fprintf(stderr, "%s\n", pathtocheck);
1162 fprintf(stderr, "Try an absolute pathname.\n");
1163 exit(AKLOG_BADPATH);
1166 strcat(pathtocheck, DIRSTRING);
1167 strcat(pathtocheck, path);
1170 next_path(pathtocheck);
1172 /* Go on to the next level down the path */
1173 while ((nextpath = next_path(NULL))) {
1174 strcpy(pathtocheck, nextpath);
1176 printf("Checking directory %s\n", pathtocheck);
1179 * If this is an afs mountpoint, determine what cell from
1180 * the mountpoint name which is of the form
1181 * #cellname:volumename or %cellname:volumename.
1183 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1184 /* skip over the '#' or '%' */
1185 cell = mountpoint + 1;
1186 /* Add this (cell:volumename) to the list of zsubs */
1188 ll_string(&zsublist, ll_s_add, cell);
1190 add_hosts(pathtocheck);
1191 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1193 if ((auth_status = auth_to_cell(context, cell, NULL, NULL))) {
1194 if (status == AKLOG_SUCCESS)
1195 status = auth_status;
1196 else if (status != auth_status)
1197 status = AKLOG_SOMETHINGSWRONG;
1202 if (isdir(pathtocheck, &isdirectory) < 0) {
1204 * If we've logged and still can't stat, there's
1207 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1208 pathtocheck, strerror(errno));
1209 return(AKLOG_BADPATH);
1211 else if (! isdirectory) {
1212 /* Allow only directories */
1213 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1215 return(AKLOG_BADPATH);
1225 /* Print usage message and exit */
1229 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1230 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1231 "[[-p | -path] pathname]\n",
1232 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1234 #ifndef HAVE_NO_KRB5_524
1238 fprintf(stderr, " -d gives debugging information.\n");
1239 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1240 fprintf(stderr, " pathname is the name of a directory to which ");
1241 fprintf(stderr, "you wish to authenticate.\n");
1242 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1243 fprintf(stderr, " -hosts gives host address information.\n");
1244 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1245 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1246 fprintf(stderr, " -force means replace identical tickets. \n");
1247 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1248 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1249 #ifndef HAVE_NO_KRB5_524
1250 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1252 fprintf(stderr, " No commandline arguments means ");
1253 fprintf(stderr, "authenticate to the local cell.\n");
1254 fprintf(stderr, "\n");
1259 main(int argc, char *argv[])
1261 krb5_context context;
1262 int status = AKLOG_SUCCESS;
1264 int somethingswrong = FALSE;
1266 cellinfo_t cellinfo;
1268 extern char *progname; /* Name of this program */
1270 extern int dflag; /* Debug mode */
1272 int cmode = FALSE; /* Cellname mode */
1273 int pmode = FALSE; /* Path name mode */
1275 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1276 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1277 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1279 linked_list cells; /* List of cells to log to */
1280 linked_list paths; /* List of paths to log to */
1284 memset(&cellinfo, 0, sizeof(cellinfo));
1286 memset(realm, 0, sizeof(realm));
1287 memset(cell, 0, sizeof(cell));
1288 memset(path, 0, sizeof(path));
1296 /* Store the program name here for error messages */
1297 if ((progname = strrchr(argv[0], DIR)))
1302 krb5_init_context(&context);
1303 initialize_ktc_error_table ();
1304 afs_set_com_err_hook(redirect_errors);
1307 * Enable DES enctypes, which are currently still required for AFS.
1308 * krb5_allow_weak_crypto is MIT Kerberos 1.8. krb5_enctype_enable is
1311 #if defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
1312 krb5_allow_weak_crypto(context, 1);
1313 #elif defined(HAVE_KRB5_ENCTYPE_ENABLE)
1314 i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
1316 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1319 /* Initialize list of cells to which we have authenticated */
1320 ll_init(&authedcells);
1322 /* Parse commandline arguments and make list of what to do. */
1323 for (i = 1; i < argc; i++) {
1324 if (strcmp(argv[i], "-d") == 0)
1326 else if (strcmp(argv[i], "-noauth") == 0)
1328 else if (strcmp(argv[i], "-zsubs") == 0)
1330 else if (strcmp(argv[i], "-hosts") == 0)
1332 else if (strcmp(argv[i], "-noprdb") == 0)
1334 else if (strcmp(argv[i], "-linked") == 0)
1336 else if (strcmp(argv[i], "-force") == 0)
1338 #ifndef HAVE_NO_KRB5_524
1339 else if (strcmp(argv[i], "-524") == 0)
1342 else if (strcmp(argv[i], "-setpag") == 0)
1344 else if (((strcmp(argv[i], "-cell") == 0) ||
1345 (strcmp(argv[i], "-c") == 0)) && !pmode)
1348 strcpy(cell, argv[i]);
1352 else if ((strcmp(argv[i], "-keytab") == 0))
1358 else if ((strcmp(argv[i], "-principal") == 0))
1364 else if (((strcmp(argv[i], "-path") == 0) ||
1365 (strcmp(argv[i], "-p") == 0)) && !cmode)
1368 strcpy(path, argv[i]);
1373 else if (argv[i][0] == '-')
1375 else if (!pmode && !cmode) {
1376 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1377 (strcmp(argv[i], "..") == 0)) {
1379 strcpy(path, argv[i]);
1383 strcpy(cell, argv[i]);
1390 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1393 strcpy(realm, argv[i]);
1397 /* Add this cell to list of cells */
1398 strcpy(cellinfo.cell, cell);
1399 strcpy(cellinfo.realm, realm);
1400 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1402 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1403 ll_add_data(cur_node, new_cellinfo);
1406 "%s: failure copying cellinfo.\n", progname);
1411 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1415 memset(&cellinfo, 0, sizeof(cellinfo));
1417 memset(cell, 0, sizeof(cell));
1418 memset(realm, 0, sizeof(realm));
1421 /* Add this path to list of paths */
1422 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1424 if ((new_path = strdup(path)))
1425 ll_add_data(cur_node, new_path);
1427 fprintf(stderr, "%s: failure copying path name.\n",
1433 fprintf(stderr, "%s: failure adding path to paths list.\n",
1438 memset(path, 0, sizeof(path));
1442 /* If nothing was given, log to the local cell. */
1443 if ((cells.nelements + paths.nelements) == 0) {
1446 status = auth_to_cell(context, NULL, NULL, &linkedcell);
1448 /* If this cell is linked to a DCE cell, and user requested -linked,
1449 * get tokens for both. This is very useful when the AFS cell is
1450 * linked to a DFS cell and this system does not also have DFS.
1453 if (!status && linked && linkedcell != NULL) {
1455 printf("Linked cell: %s\n", linkedcell);
1457 status = auth_to_cell(context, linkedcell, NULL, NULL);
1465 * Local hack - if the person has a file in their home
1466 * directory called ".xlog", read that for a list of
1467 * extra cells to authenticate to
1470 if ((pwd = getpwuid(getuid())) != NULL) {
1473 char fcell[100], xlog_path[512];
1475 strcpy(xlog_path, pwd->pw_dir);
1476 strcat(xlog_path, "/.xlog");
1478 if ((stat(xlog_path, &sbuf) == 0) &&
1479 ((f = fopen(xlog_path, "r")) != NULL)) {
1482 printf("Reading %s for cells to authenticate to.\n",
1486 while (fgets(fcell, 100, f) != NULL) {
1489 fcell[strlen(fcell) - 1] = '\0';
1492 printf("Found cell %s in %s.\n", fcell, xlog_path);
1495 auth_status = auth_to_cell(context, fcell, NULL, NULL);
1496 if (status == AKLOG_SUCCESS)
1497 status = auth_status;
1499 status = AKLOG_SOMETHINGSWRONG;
1505 /* Log to all cells in the cells list first */
1506 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1507 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1508 if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm,
1512 if (linked && linkedcell != NULL) {
1514 printf("Linked cell: %s\n", linkedcell);
1516 if ((status = auth_to_cell(context, linkedcell,
1517 cellinfo.realm, NULL)))
1520 if (linkedcell != NULL) {
1527 /* Then, log to all paths in the paths list */
1528 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1529 if ((status = auth_to_path(context, cur_node->data)))
1534 * If only one thing was logged to, we'll return the status
1535 * of the single call. Otherwise, we'll return a generic
1536 * something failed status.
1538 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1539 status = AKLOG_SOMETHINGSWRONG;
1542 /* If we are keeping track of zephyr subscriptions, print them. */
1544 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1545 printf("zsub: %s\n", cur_node->data);
1548 /* If we are keeping track of host information, print it. */
1550 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1551 printf("host: %s\n", cur_node->data);
1558 isdir(char *path, unsigned char *val)
1560 struct stat statbuf;
1562 if (lstat(path, &statbuf) < 0)
1565 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1573 static krb5_error_code
1574 get_credv5_akimpersonate(krb5_context context,
1576 krb5_principal service_principal,
1577 krb5_principal client_principal,
1580 int *allowed_enctypes,
1582 krb5_creds** out_creds /* out */ )
1584 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1585 krb5_error_code code;
1587 krb5_kt_cursor cursor[1];
1588 krb5_keytab_entry entry[1];
1590 krb5_creds *creds = 0;
1591 krb5_enctype enctype;
1593 krb5_keyblock session_key[1];
1595 Ticket ticket_reply[1];
1596 EncTicketPart enc_tkt_reply[1];
1597 krb5_address address[30];
1598 krb5_addresses faddr[1];
1600 time_t temp_time[2];
1602 krb5_ticket ticket_reply[1];
1603 krb5_enc_tkt_part enc_tkt_reply[1];
1604 krb5_address address[30], *faddr[30];
1608 static int any_enctype[] = {0};
1610 if (!(creds = malloc(sizeof *creds))) {
1614 if (!allowed_enctypes)
1615 allowed_enctypes = any_enctype;
1618 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1619 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1620 memset((char*)creds, 0, sizeof *creds);
1621 memset((char*)entry, 0, sizeof *entry);
1622 memset((char*)session_key, 0, sizeof *session_key);
1623 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1624 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1625 code = krb5_kt_resolve(context, keytab, &kt);
1628 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1630 afs_com_err(progname, code, "while resolving default keytab");
1634 if (service_principal) {
1635 for (i = 0; (enctype = allowed_enctypes[i]) || !i; ++i) {
1636 code = krb5_kt_get_entry(context,
1643 if (allowed_enctypes[i])
1644 deref_keyblock_enctype(session_key) = allowed_enctypes[i];
1649 afs_com_err(progname, code,"while scanning keytab entries");
1653 krb5_keytab_entry new[1];
1655 memset(new, 0, sizeof *new);
1656 if ((code == krb5_kt_start_seq_get(context, kt, cursor))) {
1657 afs_com_err(progname, code, "while starting keytab scan");
1660 while (!(code = krb5_kt_next_entry(context, kt, new, cursor))) {
1662 allowed_enctypes[i] && allowed_enctypes[i]
1663 != deref_entry_enctype(new); ++i)
1665 if ((!i || allowed_enctypes[i]) &&
1666 (best < 0 || best > i)) {
1667 krb5_free_keytab_entry_contents(context, entry);
1669 memset(new, 0, sizeof *new);
1670 } else krb5_free_keytab_entry_contents(context, new);
1672 if ((i = krb5_kt_end_seq_get(context, kt, cursor))) {
1673 afs_com_err(progname, i, "while ending keytab scan");
1678 afs_com_err(progname, code, "while scanning keytab");
1681 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1687 if ((code = krb5_generate_random_keyblock(context,
1688 deref_keyblock_enctype(session_key), session_key))) {
1689 afs_com_err(progname, code, "while making session key");
1692 enc_tkt_reply->flags.initial = 1;
1693 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1694 enc_tkt_reply->cname = client_principal->name;
1695 enc_tkt_reply->crealm = client_principal->realm;
1696 enc_tkt_reply->key = *session_key;
1698 static krb5_data empty_string;
1699 enc_tkt_reply->transited.contents = empty_string;
1701 enc_tkt_reply->authtime = starttime;
1702 enc_tkt_reply->starttime = temp_time;
1703 *enc_tkt_reply->starttime = starttime;
1705 enc_tkt_reply->renew_till = temp_time + 1;
1706 *enc_tkt_reply->renew_till = endtime;
1708 enc_tkt_reply->endtime = endtime;
1710 if ((code = krb5_c_make_random_key(context,
1711 deref_keyblock_enctype(session_key), session_key))) {
1712 afs_com_err(progname, code, "while making session key");
1715 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1716 #define DATACAST (unsigned char *)
1717 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1718 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1719 enc_tkt_reply->session = session_key;
1720 enc_tkt_reply->client = client_principal;
1722 static krb5_data empty_string;
1723 enc_tkt_reply->transited.tr_contents = empty_string;
1725 enc_tkt_reply->times.authtime = starttime;
1726 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1727 enc_tkt_reply->times.endtime = endtime;
1728 #endif /* USING_HEIMDAL */
1729 /* NB: We will discard address for now--ignoring caddr field
1730 in any case. MIT branch does what it always did. */
1732 if (paddress && *paddress) {
1733 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1736 faddr->val = address;
1738 for (i = 0; paddress[i]; ++i) {
1740 address[i].addr_type = KRB5_ADDRESS_INET;
1741 address[i].address.data = (void*)(paddress+i);
1742 address[i].address.length = sizeof(paddress[i]);
1745 address[i].magic = KV5M_ADDRESS;
1746 address[i].addrtype = ADDRTYPE_INET;
1748 address[i].addrtype = AF_INET;
1750 address[i].contents = (void*)(paddress+i);
1751 address[i].length = sizeof(int);
1752 faddr[i] = address+i;
1763 ticket_reply->sname = service_principal->name;
1764 ticket_reply->realm = service_principal->realm;
1766 { /* crypto block */
1767 krb5_crypto crypto = 0;
1768 unsigned char *buf = 0;
1769 size_t buf_size, buf_len;
1772 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1773 enc_tkt_reply, &buf_len, code);
1775 afs_com_err(progname, code, "while encoding ticket");
1779 if(buf_len != buf_size) {
1780 afs_com_err(progname, code,
1781 "%d != %d while encoding ticket (internal ASN.1 encoder error",
1785 what = "krb5_crypto_init";
1786 code = krb5_crypto_init(context,
1787 &deref_entry_keyblock(entry),
1788 deref_entry_enctype(entry),
1791 what = "krb5_encrypt";
1792 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
1793 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
1796 if (crypto) krb5_crypto_destroy(context, crypto);
1798 afs_com_err(progname, code, "while %s", what);
1801 } /* crypto block */
1802 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
1803 ticket_reply->enc_part.kvno = temp_vno;
1804 *ticket_reply->enc_part.kvno = entry->vno;
1805 ticket_reply->tkt_vno = 5;
1807 ticket_reply->server = service_principal;
1808 ticket_reply->enc_part2 = enc_tkt_reply;
1809 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
1810 afs_com_err(progname, code, "while making ticket");
1813 ticket_reply->enc_part.kvno = entry->vno;
1816 /* Construct Creds */
1818 if ((code = krb5_copy_principal(context, service_principal,
1820 afs_com_err(progname, code, "while copying service principal");
1823 if ((code = krb5_copy_principal(context, client_principal,
1825 afs_com_err(progname, code, "while copying client principal");
1828 if ((code = krb5_copy_keyblock_contents(context, session_key,
1829 &deref_session_key(creds)))) {
1830 afs_com_err(progname, code, "while copying session key");
1835 creds->times.authtime = enc_tkt_reply->authtime;
1836 creds->times.starttime = *(enc_tkt_reply->starttime);
1837 creds->times.endtime = enc_tkt_reply->endtime;
1838 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
1839 creds->flags.b = enc_tkt_reply->flags;
1841 creds->times = enc_tkt_reply->times;
1842 creds->ticket_flags = enc_tkt_reply->flags;
1844 if (!deref_enc_tkt_addrs(enc_tkt_reply))
1846 else if ((code = krb5_copy_addresses(context,
1847 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
1848 afs_com_err(progname, code, "while copying addresses");
1854 size_t creds_tkt_len;
1855 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
1856 ticket_reply, &creds_tkt_len, code);
1858 afs_com_err(progname, code, "while encoding ticket");
1863 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
1864 afs_com_err(progname, code, "while encoding ticket");
1867 creds->ticket = *temp;
1874 if (deref_enc_data(&ticket_reply->enc_part))
1875 free(deref_enc_data(&ticket_reply->enc_part));
1876 krb5_free_keytab_entry_contents(context, entry);
1877 if (client_principal)
1878 krb5_free_principal(context, client_principal);
1879 if (service_principal)
1880 krb5_free_principal(context, service_principal);
1882 krb5_cc_close(context, cc);
1884 krb5_kt_close(context, kt);
1885 if (creds) krb5_free_creds(context, creds);
1886 krb5_free_keyblock_contents(context, session_key);
1895 static krb5_error_code
1896 get_credv5(krb5_context context, char *name, char *inst, char *realm,
1901 static krb5_principal client_principal = 0;
1904 printf("Getting tickets: %s%s%s@%s\n", name, (inst && inst[0])
1905 ? "/" : "", inst ? inst : "", realm);
1908 memset(&increds, 0, sizeof(increds));
1909 /* ANL - instance may be ptr to a null string. Pass null then */
1910 if ((r = krb5_build_principal(context, &increds.server,
1911 strlen(realm), realm,
1913 (inst && strlen(inst)) ? inst : (void *) NULL,
1919 if (!_krb425_ccache) {
1920 r = krb5_cc_default(context, &_krb425_ccache);
1924 if (!client_principal) {
1926 r = krb5_parse_name(context, client, &client_principal);
1928 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1934 increds.client = client_principal;
1935 increds.times.endtime = 0;
1936 /* Ask for DES since that is what V4 understands */
1937 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
1940 int allowed_enctypes[] = {
1941 ENCTYPE_DES_CBC_CRC, 0
1944 r = get_credv5_akimpersonate(context,
1953 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
1960 get_user_realm(krb5_context context, char **realm)
1962 static krb5_principal client_principal = 0;
1963 krb5_error_code r = 0;
1967 if (!_krb425_ccache)
1968 krb5_cc_default(context, &_krb425_ccache);
1969 if (!client_principal) {
1971 r = krb5_parse_name(context, client, &client_principal);
1973 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1979 *realm = extract_realm(context, client_principal);