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 #include <afs/token.h>
70 #if defined(HAVE_ET_COM_ERR_H)
71 #include <et/com_err.h>
76 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
77 #include <afs/com_err.h>
80 #include <afs/param.h>
82 #include <sys/ioccom.h>
86 #include <afs/cellconfig.h>
88 #include <afs/venus.h>
89 #include <afs/ptserver.h>
90 #include <afs/ptuser.h>
91 #include <afs/pterror.h>
92 #include <afs/dirpath.h>
93 #include <afs/afsutil.h>
96 #include "linked_list.h"
98 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
101 #ifdef HAVE_KRB5_CREDS_SESSION
102 #define USING_HEIMDAL 1
108 #ifndef AFS_TRY_FULL_PRINC
109 #define AFS_TRY_FULL_PRINC 1
110 #endif /* AFS_TRY_FULL_PRINC */
112 #define AKLOG_TRYAGAIN -1
113 #define AKLOG_SUCCESS 0
114 #define AKLOG_USAGE 1
115 #define AKLOG_SOMETHINGSWRONG 2
117 #define AKLOG_KERBEROS 4
118 #define AKLOG_TOKEN 5
119 #define AKLOG_BADPATH 6
135 /* RedHat 4.x doesn't seem to define this */
136 #define MAXSYMLINKS 5
139 #define DIR '/' /* Character that divides directories */
140 #define DIRSTRING "/" /* String form of above */
141 #define VOLMARKER ':' /* Character separating cellname from mntpt */
142 #define VOLMARKERSTRING ":" /* String form of above */
146 char realm[REALM_SZ];
149 static krb5_ccache _krb425_ccache = NULL;
152 * Why doesn't AFS provide these prototypes?
155 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
161 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *, int);
162 static int isdir(char *, unsigned char *);
163 static krb5_error_code get_credv5(krb5_context context, char *, char *,
164 char *, krb5_creds **);
165 static int get_user_realm(krb5_context, char **);
167 #define TRYAGAIN(x) (x == AKLOG_TRYAGAIN || \
168 x == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || \
169 x == KRB5KRB_ERR_GENERIC)
171 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
173 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
174 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
175 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
176 #define realm_data(c, p) krb5_princ_realm(c, p)->data
177 #define realm_len(c, p) krb5_princ_realm(c, p)->length
179 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
181 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
182 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
183 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
184 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
185 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
188 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
191 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
192 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
196 krb5_encrypt_tkt_part(krb5_context context,
197 const krb5_keyblock *key,
204 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
206 if ((code = krb5_c_encrypt_length(context, key->enctype,
207 data->length, &enclen)))
209 ticket->enc_part.ciphertext.length = enclen;
210 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
214 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
215 0, data, &ticket->enc_part))) {
216 free(ticket->enc_part.ciphertext.data);
217 ticket->enc_part.ciphertext.data = 0;
229 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
231 #define get_cred_keydata(c) c->keyblock.contents
232 #define get_cred_keylen(c) c->keyblock.length
233 #define get_creds_enctype(c) c->keyblock.enctype
235 #elif defined(HAVE_KRB5_CREDS_SESSION)
237 #define get_cred_keydata(c) c->session.keyvalue.data
238 #define get_cred_keylen(c) c->session.keyvalue.length
239 #define get_creds_enctype(c) c->session.keytype
242 #error "Must have either keyblock or session member of krb5_creds"
245 #if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
246 #define krb5_524_convert_creds krb524_convert_creds_kdc
247 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
248 #define HAVE_NO_KRB5_524
252 #define deref_keyblock_enctype(kb) \
255 #define deref_entry_keyblock(entry) \
258 #define deref_session_key(creds) \
261 #define deref_enc_tkt_addrs(tkt) \
264 #define deref_enc_length(enc) \
265 ((enc)->cipher.length)
267 #define deref_enc_data(enc) \
270 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
273 #define deref_keyblock_enctype(kb) \
276 #define deref_entry_keyblock(entry) \
279 #define deref_session_key(creds) \
282 #define deref_enc_tkt_addrs(tkt) \
285 #define deref_enc_length(enc) \
286 ((enc)->ciphertext.length)
288 #define deref_enc_data(enc) \
289 ((enc)->ciphertext.data)
293 #define deref_entry_enctype(entry) \
294 deref_keyblock_enctype(&deref_entry_keyblock(entry))
297 * Provide a replacement for strerror if we don't have it
300 #ifndef HAVE_STRERROR
301 extern char *sys_errlist[];
302 #define strerror(x) sys_errlist[x]
303 #endif /* HAVE_STRERROR */
305 static char *progname = NULL; /* Name of this program */
306 static int dflag = FALSE; /* Give debugging information */
307 static int noauth = FALSE; /* If true, don't try to get tokens */
308 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
309 static int hosts = FALSE; /* Are we keeping track of hosts? */
310 static int noprdb = FALSE; /* Skip resolving name to id? */
311 static int linked = FALSE; /* try for both AFS nodes */
312 static int afssetpag = FALSE; /* setpag for AFS */
313 static int force = FALSE; /* Bash identical tokens? */
314 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
315 static char *keytab = NULL; /* keytab for akimpersonate */
316 static char *client = NULL; /* client principal for akimpersonate */
317 static linked_list zsublist; /* List of zephyr subscriptions */
318 static linked_list hostlist; /* List of host addresses */
319 static linked_list authedcells; /* List of cells already logged to */
321 /* A com_error bodge. The idea here is that this routine lets us lookup
322 * things in the system com_err, if the AFS one just tells us the error
327 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
334 const char *str = afs_error_message(code);
335 if (strncmp(str, "unknown", strlen("unknown")) == 0) {
336 #ifdef HAVE_KRB5_SVC_GET_MSG
337 krb5_svc_get_msg(code,&str);
338 #elif defined(HAVE_ERROR_MESSAGE)
339 str = error_message(code);
341 ; /* IRIX apparently has neither: use the string we have */
346 #ifdef HAVE_KRB5_SVC_GET_MSG
347 krb5_free_string(str);
351 vfprintf(stderr, fmt, ap);
358 afs_dprintf(char *fmt, ...) {
368 copy_cellinfo(cellinfo_t *cellinfo)
370 cellinfo_t *new_cellinfo;
372 if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
373 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
375 return ((char *)new_cellinfo);
380 get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
382 int status = AKLOG_SUCCESS;
383 struct afsconf_dir *configdir;
385 memset(cellconfig, 0, sizeof(*cellconfig));
387 *local_cell = malloc(MAXCELLCHARS);
388 if (*local_cell == NULL) {
389 fprintf(stderr, "%s: can't allocate memory for local cell name\n",
394 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
396 "%s: can't get afs configuration (afsconf_Open(%s))\n",
397 progname, AFSDIR_CLIENT_ETC_DIRPATH);
401 if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
402 fprintf(stderr, "%s: can't determine local cell.\n", progname);
406 if ((cell == NULL) || (cell[0] == 0))
409 /* XXX - This function modifies 'cell' by passing it through lcstring */
410 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
411 fprintf(stderr, "%s: Can't get information about cell %s.\n",
416 afsconf_Close(configdir);
422 extract_realm(krb5_context context, krb5_principal princ) {
426 len = realm_len(context, princ);
427 if (len > REALM_SZ-1)
430 realm = malloc(sizeof(char) * (len+1));
434 strncpy(realm, realm_data(context, princ), len);
441 get_realm_from_cred(krb5_context context, krb5_creds *v5cred, char **realm) {
442 #if !defined(HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
443 krb5_error_code code;
448 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
452 *realm = extract_realm(context, ticket->server);
456 krb5_free_ticket(context, ticket);
466 * Get a Kerberos service ticket to use as the base of an rxkad token for
470 * An initialized Kerberos v5 context
472 * The realm to look in for the service principal. If NULL, then the
473 * realm is determined from the cell name or the user's credentials
474 * (see below for the heuristics used)
476 * The cell information for the cell to obtain a ticket for
478 * A Kerberos credentials structure containing the ticket acquired
479 * for the cell. This is a dynamically allocated structure, which
480 * should be freed by using the appropriate Kerberos API function.
481 * @param[out] realmUsed
482 * The realm in which the cell's service principal was located. If
483 * unset, then the principal was located in the same realm as the
484 * current user. This is a malloc'd string which should be freed
488 * 0 on success, an error value upon failure
491 * This code tries principals in the following, much debated,
494 * If the realm is specified on the command line we do
495 * - afs/cell@COMMAND-LINE-REALM
496 * - afs@COMMAND-LINE-REALM
499 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
500 * - afs/cell@krb5_get_host_realm(db-server)
501 * Then, if krb5_get_host_realm(db-server) is non-empty
502 * - afs@ krb5_get_host_realm(db-server)
504 * - afs/cell@ upper-case-domain-of-db-server
505 * - afs@ upper-case-domain-of-db-server
507 * In all cases, the 'afs@' variant is only tried where the
508 * cell and the realm match case-insensitively.
512 rxkad_get_ticket(krb5_context context, char *realm,
513 struct afsconf_cell *cell,
514 krb5_creds **v5cred, char **realmUsed) {
515 char *realm_of_cell = NULL;
516 char *realm_of_user = NULL;
517 char *realm_from_princ = NULL;
523 if ((status = get_user_realm(context, &realm_of_user))) {
524 fprintf(stderr, "%s: Couldn't determine realm of user:", progname);
525 afs_com_err(progname, status, " while getting realm");
526 status = AKLOG_KERBEROS;
533 /* Cell on command line - use that one */
534 if (realm && realm[0]) {
535 realm_of_cell = realm;
536 status = AKLOG_TRYAGAIN;
537 afs_dprintf("We were told to authenticate to realm %s.\n", realm);
539 /* Initially, try using afs/cell@USERREALM */
540 afs_dprintf("Trying to authenticate to user's realm %s.\n",
542 realm_of_cell = realm_of_user;
543 status = get_credv5(context, AFSKEY, cell->name, realm_of_cell,
546 /* If that failed, try to determine the realm from the name of
547 * one of the DB servers */
548 if (TRYAGAIN(status)) {
549 realm_of_cell = afs_realm_of_cell(context, cell, FALSE);
550 if (!realm_of_cell) {
551 fprintf(stderr, "%s: Couldn't figure out realm for cell "
552 "%s.\n", progname, cell->name);
556 if (realm_of_cell[0])
557 afs_dprintf("We've deduced that we need to authenticate"
558 " to realm %s.\n", realm_of_cell);
560 afs_dprintf("We've deduced that we need to authenticate "
561 "using referrals.\n");
565 if (TRYAGAIN(status)) {
566 /* If we've got the full-princ-first option, or we're in a
567 * different realm from the cell - use the cell name as the
569 if (AFS_TRY_FULL_PRINC ||
570 strcasecmp(cell->name, realm_of_cell)!=0) {
571 status = get_credv5(context, AFSKEY, cell->name,
572 realm_of_cell, v5cred);
574 /* If we failed & we've got an empty realm, then try
575 * calling afs_realm_for_cell again. */
576 if (TRYAGAIN(status) && !realm_of_cell[0]) {
577 /* This time, get the realm by taking the domain
578 * component of the db server and make it upper case */
579 realm_of_cell = afs_realm_of_cell(context, cell, TRUE);
580 if (!realm_of_cell) {
582 "%s: Couldn't figure out realm for cell %s.\n",
583 progname, cell->name);
586 afs_dprintf("We've deduced that we need to authenticate"
587 " to realm %s.\n", realm_of_cell);
589 status = get_credv5(context, AFSKEY, cell->name,
590 realm_of_cell, v5cred);
593 /* If the realm and cell name match, then try without an
594 * instance, but only if realm is non-empty */
596 if (TRYAGAIN(status) &&
597 strcasecmp(cell->name, realm_of_cell) == 0) {
598 status = get_credv5(context, AFSKEY, NULL, realm_of_cell,
600 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
601 status = get_credv5(context, AFSKEY, cell->name,
602 realm_of_cell, v5cred);
607 /* Try to find a service principal for this cell.
608 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
609 * the first attempt, so we try twice to be sure */
611 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
618 afs_dprintf("Kerberos error code returned by get_cred : %d\n", status);
619 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
620 progname, cell->name);
621 afs_com_err(progname, status, "while getting AFS tickets");
622 #ifdef KRB5_CC_NOT_KTYPE
623 if (status == KRB5_CC_NOT_KTYPE) {
624 fprintf(stderr, "allow_weak_enctypes may be required in the Kerberos configuration\n");
627 status = AKLOG_KERBEROS;
631 /* If we've got a valid ticket, and we still don't know the realm name
632 * try to figure it out from the contents of the ticket
634 if (strcmp(realm_of_cell, "") == 0) {
635 status = get_realm_from_cred(context, *v5cred, &realm_from_princ);
638 "%s: Couldn't decode ticket to determine realm for "
640 progname, cell->name);
642 if (realm_from_princ)
643 realm_of_cell = realm_from_princ;
647 /* If the realm of the user and cell differ, then we need to use the
648 * realm when we later construct the user's principal */
649 if (realm_of_cell != NULL && strcmp(realm_of_user, realm_of_cell) != 0)
650 *realmUsed = realm_of_user;
653 if (realm_from_princ)
654 free(realm_from_princ);
655 if (realm_of_user && *realmUsed == NULL)
662 * Build an rxkad token from a Kerberos ticket, using only local tools (that
663 * is, without using a 524 conversion service)
666 * An initialised Kerberos 5 context
668 * A Kerberos credentials structure containing a suitable service ticket
669 * @param[out] tokenPtr
670 * An AFS token structure containing an rxkad token. This is a malloc'd
671 * structure which should be freed by the caller.
672 * @param[out[ userPtr
673 * A string containing the principal of the user to whom the token was
674 * issued. This is a malloc'd block which should be freed by the caller.
677 * 0 on success, an error value upon failure
680 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
681 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
682 char username[BUFSIZ];
683 struct ktc_tokenUnion *token;
684 struct token_rxkad *rxkadToken;
685 #ifdef HAVE_NO_KRB5_524
690 char k4name[ANAME_SZ];
691 char k4inst[INST_SZ];
692 char k4realm[REALM_SZ];
695 afs_dprintf("Using Kerberos V5 ticket natively\n");
700 #ifndef HAVE_NO_KRB5_524
701 status = krb5_524_conv_principal (context, v5cred->client,
706 afs_com_err(progname, status, "while converting principal "
707 "to Kerberos V4 format");
708 return 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 token = malloc(sizeof(struct ktc_tokenUnion));
736 memset(token, 0, sizeof(struct ktc_tokenUnion));
738 token->at_type = AFSTOKEN_UNION_KAD;
739 rxkadToken = &token->ktc_tokenUnion_u.at_kad;
741 rxkadToken->rk_kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
742 rxkadToken->rk_begintime = v5cred->times.starttime;;
743 rxkadToken->rk_endtime = v5cred->times.endtime;
744 memcpy(&rxkadToken->rk_key, get_cred_keydata(v5cred),
745 get_cred_keylen(v5cred));
746 rxkadToken->rk_ticket.rk_ticket_len = v5cred->ticket.length;
747 rxkadToken->rk_ticket.rk_ticket_val = malloc(v5cred->ticket.length);
748 if (rxkadToken->rk_ticket.rk_ticket_val == NULL) {
752 memcpy(rxkadToken->rk_ticket.rk_ticket_val, v5cred->ticket.data,
753 rxkadToken->rk_ticket.rk_ticket_len);
756 *userPtr = strdup(username);
762 * Convert a Keberos ticket to an rxkad token, using information obtained
763 * from an external Kerberos 5->4 conversion service. If the code is built
764 * with HAVE_NO_KRB5_524 then this is a stub function which will always
765 * return success without a token.
768 * An initialised Kerberos 5 context
770 * A Kerberos credentials structure containing a suitable service ticket
771 * @param[out] tokenPtr
772 * An AFS token structure containing an rxkad token. This is a malloc'd
773 * structure which should be freed by the caller.
774 * @param[out[ userPtr
775 * A string containing the principal of the user to whom the token was
776 * issued. This is a malloc'd block which should be freed by the caller.
779 * 0 on success, an error value upon failure
782 #ifdef HAVE_NO_KRB5_524
784 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
785 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
793 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
794 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
796 char username[BUFSIZ];
797 struct ktc_tokenUnion *token;
798 struct token_rxkad *rxkadToken;
804 afs_dprintf("Using Kerberos 524 translator service\n");
806 status = krb5_524_convert_creds(context, v5cred, &cred);
809 afs_com_err(progname, status, "while converting tickets "
810 "to Kerberos V4 format");
811 return AKLOG_KERBEROS;
814 strcpy (username, cred.pname);
816 strcat (username, ".");
817 strcat (username, cred.pinst);
820 token = malloc(sizeof(struct ktc_tokenUnion));
823 memset(token, 0, sizeof(struct ktc_tokenUnion));
825 token->at_type = AFSTOKEN_UNION_KAD;
827 rxkadToken = &token->ktc_tokenUnion_u.at_kad;
828 rxkadToken->rk_kvno = cred.kvno;
829 rxkadToken->rk_begintime = cred.issue_date;
831 * It seems silly to go through a bunch of contortions to
832 * extract the expiration time, when the v5 credentials already
833 * has the exact time! Let's use that instead.
835 * Note that this isn't a security hole, as the expiration time
836 * is also contained in the encrypted token
838 rxkadToken->rk_endtime = v5cred->times.endtime;
839 memcpy(&rxkadToken->rk_key, cred.session, 8);
840 rxkadToken->rk_ticket.rk_ticket_len = cred.ticket_st.length;
841 rxkadToken->rk_ticket.rk_ticket_val = malloc(cred.ticket_st.length);
842 if (rxkadToken->rk_ticket.rk_ticket_val == NULL) {
846 memcpy(rxkadToken->rk_ticket.rk_ticket_val, cred.ticket_st.dat,
847 rxkadToken->rk_ticket.rk_ticket_len);
850 *userPtr = strdup(username);
857 * This function gets an rxkad token for a given cell.
860 * An initialized Kerberos v5 context
862 * The cell information for the cell which we're obtaining a token for
864 * The realm to look in for the service principal. If NULL, then the
865 * realm is determined from the cell name or the user's credentials
866 * (see the documentation for rxkad_get_ticket)
868 * The rxkad token produced. This is a malloc'd structure which should
869 * be freed by the caller.
870 * @parma[out] authuser
871 * A string containing the principal of the user to whom the token was
872 * issued. This is a malloc'd block which should be freed by the caller.
873 * @param[out] foreign
874 * Whether the user is considered as 'foreign' to the realm of the cell.
877 * 0 on success, an error value upon failuer
880 rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
881 struct ktc_tokenUnion **token, char **authuser, int *foreign) {
883 char *realmUsed = NULL;
884 char *username = NULL;
892 status = rxkad_get_ticket(context, realm, cell, &v5cred, &realmUsed);
897 status = rxkad_get_converted_token(context, v5cred, token, &username);
899 status = rxkad_build_native_token(context, v5cred, token, &username);
904 /* We now have the username, plus the realm name, so stitch them together
905 * to give us the name that the ptserver will know the user by */
906 if (realmUsed == NULL) {
907 *authuser = username;
911 len = strlen(username)+strlen(realmUsed)+2;
912 *authuser = malloc(len);
913 afs_snprintf(*authuser, len, "%s@%s", username, realmUsed);
927 * Log to a cell. If the cell has already been logged to, return without
928 * doing anything. Otherwise, log to it and mark that it has been logged
932 auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
934 int status = AKLOG_SUCCESS;
936 char *username = NULL; /* To hold client username structure */
937 afs_int32 viceId; /* AFS uid of user */
939 char *local_cell = NULL;
940 struct ktc_tokenUnion *rxkadToken = NULL;
941 struct ktc_setTokenData *token;
942 struct ktc_setTokenData *btoken = NULL;
943 struct afsconf_cell cellconf;
945 /* NULL or empty cell returns information on local cell */
946 if ((status = get_cellconfig(cell, &cellconf, &local_cell)))
949 if (linkedcell != NULL) {
950 if (cellconf.linkedCell != NULL) {
951 *linkedcell = strdup(cellconf.linkedCell);
952 if (*linkedcell == NULL) {
961 if (ll_string(&authedcells, ll_s_check, cellconf.name)) {
962 afs_dprintf("Already authenticated to %s (or tried to)\n", cellconf.name);
963 status = AKLOG_SUCCESS;
968 * Record that we have attempted to log to this cell. We do this
969 * before we try rather than after so that we will not try
970 * and fail repeatedly for one cell.
972 ll_string(&authedcells, ll_s_add, cellconf.name);
975 * Record this cell in the list of zephyr subscriptions. We may
976 * want zephyr subscriptions even if authentication fails.
977 * If this is done after we attempt to get tokens, aklog -zsubs
978 * can return something different depending on whether or not we
979 * are in -noauth mode.
981 if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
983 "%s: failure adding cell %s to zephyr subscriptions list.\n",
984 progname, cellconf.name);
987 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
989 "%s: failure adding cell %s to zephyr subscriptions list.\n",
990 progname, local_cell);
995 afs_dprintf("Authenticating to cell %s (server %s).\n", cellconf.name,
996 cellconf.hostName[0]);
998 token = token_buildTokenJar(cellconf.name);
1004 status = rxkad_get_token(context, &cellconf, realm, &rxkadToken,
1005 &username, &isForeign);
1009 /* We need to keep the token structure around so that we can stick
1010 * the viceId into it (once we know it) */
1011 status = token_addToken(token, rxkadToken);
1013 afs_dprintf("Add Token failed with %d", status);
1018 ktc_GetTokenEx(cellconf.name, &btoken) == 0 &&
1019 token_SetsEquivalent(token, btoken)) {
1021 token_FreeSet(&btoken);
1022 afs_dprintf("Identical tokens already exist; skipping.\n");
1023 status = AKLOG_SUCCESS;
1028 token_FreeSet(&btoken);
1035 afs_dprintf("Not resolving name %s to id (-noprdb set)\n", username);
1038 afs_dprintf("About to resolve name %s to id in cell %s.\n", username,
1041 if (!pr_Initialize (0, AFSDIR_CLIENT_ETC_DIRPATH, cellconf.name))
1042 status = pr_SNameToId (username, &viceId);
1045 afs_dprintf("Error %d\n", status);
1047 afs_dprintf("Id %d\n", (int) viceId);
1051 * This code is taken from cklog -- it lets people
1052 * automatically register with the ptserver in foreign cells
1055 #ifdef ALLOW_REGISTER
1056 if ((status == 0) && (viceId == ANONYMOUSID) && isForeign) {
1057 afs_dprintf("doing first-time registration of %s at %s\n",
1058 username, cellconf.name);
1061 status = ktc_SetTokenEx(token);
1063 afs_com_err(progname, status,
1064 "while obtaining tokens for cell %s",
1066 status = AKLOG_TOKEN;
1070 * In case you're wondering, we don't need to change the
1071 * filename here because we're still connecting to the
1072 * same cell -- we're just using a different authenticat ion
1076 if ((status = pr_Initialize(1L, AFSDIR_CLIENT_ETC_DIRPATH,
1078 printf("Error %d\n", status);
1081 if ((status = pr_CreateUser(username, &viceId))) {
1082 fprintf(stderr, "%s: %s so unable to create remote PTS "
1083 "user %s in cell %s (status: %d).\n", progname,
1084 afs_error_message(status), username, cellconf.name,
1086 viceId = ANONYMOUSID;
1088 printf("created cross-cell entry for %s (Id %d) at %s\n",
1089 username, viceId, cellconf.name);
1092 #endif /* ALLOW_REGISTER */
1094 if ((status == 0) && (viceId != ANONYMOUSID)) {
1095 rxkadToken->ktc_tokenUnion_u.at_kad.rk_viceid = viceId;
1096 token_replaceToken(token, rxkadToken);
1100 afs_dprintf("Setting tokens. %s @ %s \n", username, cellconf.name);
1102 #ifndef AFS_AIX51_ENV
1103 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
1104 * this routine, it will not add the token. It is not clear what
1105 * is going on here! So we will do the following operation.
1106 * On AIX 5, it causes the parent program to die, so we won't.
1108 write(2,"",0); /* dummy write */
1110 token_setPag(token, afssetpag);
1111 status = ktc_SetTokenEx(token);
1113 afs_com_err(progname, status, "while setting tokens for cell %s",
1115 status = AKLOG_TOKEN;
1119 afs_dprintf("Noauth mode; not authenticating.\n");
1123 free(rxkadToken->ktc_tokenUnion_u.at_kad.rk_ticket.rk_ticket_val);
1136 get_afs_mountpoint(char *file, char *mountpoint, int size)
1139 char V ='V'; /* AFS has problem on Sun with pioctl */
1141 char our_file[MAXPATHLEN + 1];
1143 char *last_component;
1144 struct ViceIoctl vio;
1145 char cellname[BUFSIZ];
1147 memset(our_file, 0, sizeof(our_file));
1148 strcpy(our_file, file);
1150 if ((last_component = strrchr(our_file, DIR))) {
1151 *last_component++ = 0;
1152 parent_dir = our_file;
1155 last_component = our_file;
1159 memset(cellname, 0, sizeof(cellname));
1161 vio.in = last_component;
1162 vio.in_size = strlen(last_component)+1;
1163 vio.out_size = size;
1164 vio.out = mountpoint;
1166 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
1167 if (strchr(mountpoint, VOLMARKER) == NULL) {
1169 vio.in_size = strlen(file) + 1;
1170 vio.out_size = sizeof(cellname);
1173 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
1174 strcat(cellname, VOLMARKERSTRING);
1175 strcat(cellname, mountpoint + 1);
1176 memset(mountpoint + 1, 0, size - 1);
1177 strcpy(mountpoint + 1, cellname);
1187 * This routine each time it is called returns the next directory
1188 * down a pathname. It resolves all symbolic links. The first time
1189 * it is called, it should be called with the name of the path
1190 * to be descended. After that, it should be called with the arguemnt
1194 next_path(char *origpath)
1196 static char path[MAXPATHLEN + 1];
1197 static char pathtocheck[MAXPATHLEN + 1];
1199 int link = FALSE; /* Is this a symbolic link? */
1200 char linkbuf[MAXPATHLEN + 1];
1201 char tmpbuf[MAXPATHLEN + 1];
1203 static char *last_comp; /* last component of directory name */
1204 static char *elast_comp; /* End of last component */
1208 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1210 /* If we are given something for origpath, we are initializing only. */
1212 memset(path, 0, sizeof(path));
1213 memset(pathtocheck, 0, sizeof(pathtocheck));
1214 strcpy(path, origpath);
1220 /* We were not given origpath; find then next path to check */
1222 /* If we've gotten all the way through already, return NULL */
1223 if (last_comp == NULL)
1227 while (*last_comp == DIR)
1228 strncat(pathtocheck, last_comp++, 1);
1229 len = (elast_comp = strchr(last_comp, DIR))
1230 ? elast_comp - last_comp : strlen(last_comp);
1231 strncat(pathtocheck, last_comp, len);
1232 memset(linkbuf, 0, sizeof(linkbuf));
1233 if ((link = (readlink(pathtocheck, linkbuf,
1234 sizeof(linkbuf)) > 0))) {
1235 if (++symlinkcount > MAXSYMLINKS) {
1236 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1237 exit(AKLOG_BADPATH);
1239 memset(tmpbuf, 0, sizeof(tmpbuf));
1241 strcpy(tmpbuf, elast_comp);
1242 if (linkbuf[0] == DIR) {
1244 * If this is a symbolic link to an absolute path,
1245 * replace what we have by the absolute path.
1247 memset(path, 0, strlen(path));
1248 memcpy(path, linkbuf, sizeof(linkbuf));
1249 strcat(path, tmpbuf);
1252 memset(pathtocheck, 0, sizeof(pathtocheck));
1256 * If this is a symbolic link to a relative path,
1257 * replace only the last component with the link name.
1259 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1260 strcat(path, tmpbuf);
1262 if ((t = strrchr(pathtocheck, DIR))) {
1264 memset(t, 0, strlen(t));
1267 memset(pathtocheck, 0, sizeof(pathtocheck));
1271 last_comp = elast_comp;
1275 return(pathtocheck);
1279 add_hosts(char *file)
1282 char V = 'V'; /* AFS has problem on SunOS */
1284 struct ViceIoctl vio;
1285 char outbuf[BUFSIZ];
1291 memset(outbuf, 0, sizeof(outbuf));
1293 vio.out_size = sizeof(outbuf);
1297 afs_dprintf("Getting list of hosts for %s\n", file);
1299 /* Don't worry about errors. */
1300 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1301 phosts = (long *) outbuf;
1304 * Lists hosts that we care about. If ALLHOSTS is defined,
1305 * then all hosts that you ever may possible go through are
1306 * included in this list. If not, then only hosts that are
1307 * the only ones appear. That is, if a volume you must use
1308 * is replaced on only one server, that server is included.
1309 * If it is replicated on many servers, then none are included.
1310 * This is not perfect, but the result is that people don't
1311 * get subscribed to a lot of instances of FILSRV that they
1312 * probably won't need which reduces the instances of
1313 * people getting messages that don't apply to them.
1316 if (phosts[1] != '\0')
1319 for (i = 0; phosts[i]; i++) {
1321 in.s_addr = phosts[i];
1322 afs_dprintf("Got host %s\n", inet_ntoa(in));
1323 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1325 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1326 afs_dprintf("Got host %s\n", hp->h_name);
1327 ll_string(&zsublist, ll_s_add, hp->h_name);
1334 * This routine descends through a path to a directory, logging to
1335 * every cell it encounters along the way.
1338 auth_to_path(krb5_context context, char *path)
1340 int status = AKLOG_SUCCESS;
1341 int auth_status = AKLOG_SUCCESS;
1344 char pathtocheck[MAXPATHLEN + 1];
1345 char mountpoint[MAXPATHLEN + 1];
1354 strcpy(pathtocheck, path);
1356 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1357 fprintf(stderr, "Unable to find current working directory:\n");
1358 fprintf(stderr, "%s\n", pathtocheck);
1359 fprintf(stderr, "Try an absolute pathname.\n");
1360 exit(AKLOG_BADPATH);
1363 strcat(pathtocheck, DIRSTRING);
1364 strcat(pathtocheck, path);
1367 next_path(pathtocheck);
1369 /* Go on to the next level down the path */
1370 while ((nextpath = next_path(NULL))) {
1371 strcpy(pathtocheck, nextpath);
1372 afs_dprintf("Checking directory %s\n", pathtocheck);
1374 * If this is an afs mountpoint, determine what cell from
1375 * the mountpoint name which is of the form
1376 * #cellname:volumename or %cellname:volumename.
1378 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1379 /* skip over the '#' or '%' */
1380 cell = mountpoint + 1;
1381 /* Add this (cell:volumename) to the list of zsubs */
1383 ll_string(&zsublist, ll_s_add, cell);
1385 add_hosts(pathtocheck);
1386 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1388 if ((auth_status = auth_to_cell(context, cell, NULL, NULL))) {
1389 if (status == AKLOG_SUCCESS)
1390 status = auth_status;
1391 else if (status != auth_status)
1392 status = AKLOG_SOMETHINGSWRONG;
1397 if (isdir(pathtocheck, &isdirectory) < 0) {
1399 * If we've logged and still can't stat, there's
1402 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1403 pathtocheck, strerror(errno));
1404 return(AKLOG_BADPATH);
1406 else if (! isdirectory) {
1407 /* Allow only directories */
1408 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1410 return(AKLOG_BADPATH);
1420 /* Print usage message and exit */
1424 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1425 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1426 "[[-p | -path] pathname]\n",
1427 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1429 #ifndef HAVE_NO_KRB5_524
1433 fprintf(stderr, " -d gives debugging information.\n");
1434 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1435 fprintf(stderr, " pathname is the name of a directory to which ");
1436 fprintf(stderr, "you wish to authenticate.\n");
1437 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1438 fprintf(stderr, " -hosts gives host address information.\n");
1439 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1440 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1441 fprintf(stderr, " -force means replace identical tickets. \n");
1442 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1443 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1444 #ifndef HAVE_NO_KRB5_524
1445 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1447 fprintf(stderr, " No commandline arguments means ");
1448 fprintf(stderr, "authenticate to the local cell.\n");
1449 fprintf(stderr, "\n");
1454 main(int argc, char *argv[])
1456 krb5_context context;
1457 int status = AKLOG_SUCCESS;
1459 int somethingswrong = FALSE;
1461 cellinfo_t cellinfo;
1463 extern char *progname; /* Name of this program */
1465 int cmode = FALSE; /* Cellname mode */
1466 int pmode = FALSE; /* Path name mode */
1468 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1469 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1470 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1472 linked_list cells; /* List of cells to log to */
1473 linked_list paths; /* List of paths to log to */
1477 memset(&cellinfo, 0, sizeof(cellinfo));
1479 memset(realm, 0, sizeof(realm));
1480 memset(cell, 0, sizeof(cell));
1481 memset(path, 0, sizeof(path));
1489 /* Store the program name here for error messages */
1490 if ((progname = strrchr(argv[0], DIR)))
1495 krb5_init_context(&context);
1496 initialize_KTC_error_table ();
1497 initialize_U_error_table();
1498 initialize_RXK_error_table();
1499 initialize_ACFG_error_table();
1500 initialize_PT_error_table();
1501 afs_set_com_err_hook(redirect_errors);
1504 * Enable DES enctypes, which are currently still required for AFS.
1505 * krb5_allow_weak_crypto is MIT Kerberos 1.8. krb5_enctype_enable is
1508 #if defined(HAVE_KRB5_ENCTYPE_ENABLE)
1509 i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
1511 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1512 #elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
1513 krb5_allow_weak_crypto(context, 1);
1516 /* Initialize list of cells to which we have authenticated */
1517 ll_init(&authedcells);
1519 /* Parse commandline arguments and make list of what to do. */
1520 for (i = 1; i < argc; i++) {
1521 if (strcmp(argv[i], "-d") == 0)
1523 else if (strcmp(argv[i], "-noauth") == 0)
1525 else if (strcmp(argv[i], "-zsubs") == 0)
1527 else if (strcmp(argv[i], "-hosts") == 0)
1529 else if (strcmp(argv[i], "-noprdb") == 0)
1531 else if (strcmp(argv[i], "-linked") == 0)
1533 else if (strcmp(argv[i], "-force") == 0)
1535 #ifndef HAVE_NO_KRB5_524
1536 else if (strcmp(argv[i], "-524") == 0)
1539 else if (strcmp(argv[i], "-setpag") == 0)
1541 else if (((strcmp(argv[i], "-cell") == 0) ||
1542 (strcmp(argv[i], "-c") == 0)) && !pmode)
1545 strcpy(cell, argv[i]);
1549 else if ((strcmp(argv[i], "-keytab") == 0))
1555 else if ((strcmp(argv[i], "-principal") == 0))
1561 else if (((strcmp(argv[i], "-path") == 0) ||
1562 (strcmp(argv[i], "-p") == 0)) && !cmode)
1565 strcpy(path, argv[i]);
1570 else if (argv[i][0] == '-')
1572 else if (!pmode && !cmode) {
1573 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1574 (strcmp(argv[i], "..") == 0)) {
1576 strcpy(path, argv[i]);
1580 strcpy(cell, argv[i]);
1587 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1590 strcpy(realm, argv[i]);
1594 /* Add this cell to list of cells */
1595 strcpy(cellinfo.cell, cell);
1596 strcpy(cellinfo.realm, realm);
1597 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1599 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1600 ll_add_data(cur_node, new_cellinfo);
1603 "%s: failure copying cellinfo.\n", progname);
1608 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1612 memset(&cellinfo, 0, sizeof(cellinfo));
1614 memset(cell, 0, sizeof(cell));
1615 memset(realm, 0, sizeof(realm));
1618 /* Add this path to list of paths */
1619 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1621 if ((new_path = strdup(path)))
1622 ll_add_data(cur_node, new_path);
1624 fprintf(stderr, "%s: failure copying path name.\n",
1630 fprintf(stderr, "%s: failure adding path to paths list.\n",
1635 memset(path, 0, sizeof(path));
1639 /* If nothing was given, log to the local cell. */
1640 if ((cells.nelements + paths.nelements) == 0) {
1643 status = auth_to_cell(context, NULL, NULL, &linkedcell);
1645 /* If this cell is linked to a DCE cell, and user requested -linked,
1646 * get tokens for both. This is very useful when the AFS cell is
1647 * linked to a DFS cell and this system does not also have DFS.
1650 if (!status && linked && linkedcell != NULL) {
1651 afs_dprintf("Linked cell: %s\n", linkedcell);
1652 status = auth_to_cell(context, linkedcell, NULL, NULL);
1660 * Local hack - if the person has a file in their home
1661 * directory called ".xlog", read that for a list of
1662 * extra cells to authenticate to
1665 if ((pwd = getpwuid(getuid())) != NULL) {
1668 char fcell[100], xlog_path[512];
1670 strcpy(xlog_path, pwd->pw_dir);
1671 strcat(xlog_path, "/.xlog");
1673 if ((stat(xlog_path, &sbuf) == 0) &&
1674 ((f = fopen(xlog_path, "r")) != NULL)) {
1676 afs_dprintf("Reading %s for cells to authenticate to.\n",
1679 while (fgets(fcell, 100, f) != NULL) {
1682 fcell[strlen(fcell) - 1] = '\0';
1684 afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
1686 auth_status = auth_to_cell(context, fcell, NULL, NULL);
1687 if (status == AKLOG_SUCCESS)
1688 status = auth_status;
1690 status = AKLOG_SOMETHINGSWRONG;
1696 /* Log to all cells in the cells list first */
1697 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1698 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1699 if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm,
1703 if (linked && linkedcell != NULL) {
1704 afs_dprintf("Linked cell: %s\n", linkedcell);
1705 if ((status = auth_to_cell(context, linkedcell,
1706 cellinfo.realm, NULL)))
1709 if (linkedcell != NULL) {
1716 /* Then, log to all paths in the paths list */
1717 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1718 if ((status = auth_to_path(context, cur_node->data)))
1723 * If only one thing was logged to, we'll return the status
1724 * of the single call. Otherwise, we'll return a generic
1725 * something failed status.
1727 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1728 status = AKLOG_SOMETHINGSWRONG;
1731 /* If we are keeping track of zephyr subscriptions, print them. */
1733 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1734 printf("zsub: %s\n", cur_node->data);
1737 /* If we are keeping track of host information, print it. */
1739 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1740 printf("host: %s\n", cur_node->data);
1747 isdir(char *path, unsigned char *val)
1749 struct stat statbuf;
1751 if (lstat(path, &statbuf) < 0)
1754 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1762 static krb5_error_code
1763 get_credv5_akimpersonate(krb5_context context,
1765 krb5_principal service_principal,
1766 krb5_principal client_principal,
1769 int *allowed_enctypes,
1771 krb5_creds** out_creds /* out */ )
1773 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1774 krb5_error_code code;
1776 krb5_kt_cursor cursor[1];
1777 krb5_keytab_entry entry[1];
1779 krb5_creds *creds = 0;
1780 krb5_enctype enctype;
1782 krb5_keyblock session_key[1];
1784 Ticket ticket_reply[1];
1785 EncTicketPart enc_tkt_reply[1];
1786 krb5_address address[30];
1787 krb5_addresses faddr[1];
1789 time_t temp_time[2];
1791 krb5_ticket ticket_reply[1];
1792 krb5_enc_tkt_part enc_tkt_reply[1];
1793 krb5_address address[30], *faddr[30];
1797 static int any_enctype[] = {0};
1799 if (!(creds = malloc(sizeof *creds))) {
1803 if (!allowed_enctypes)
1804 allowed_enctypes = any_enctype;
1807 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1808 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1809 memset((char*)creds, 0, sizeof *creds);
1810 memset((char*)entry, 0, sizeof *entry);
1811 memset((char*)session_key, 0, sizeof *session_key);
1812 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1813 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1814 code = krb5_kt_resolve(context, keytab, &kt);
1817 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1819 afs_com_err(progname, code, "while resolving default keytab");
1823 if (service_principal) {
1824 for (i = 0; (enctype = allowed_enctypes[i]) || !i; ++i) {
1825 code = krb5_kt_get_entry(context,
1832 if (allowed_enctypes[i])
1833 deref_keyblock_enctype(session_key) = allowed_enctypes[i];
1838 afs_com_err(progname, code,"while scanning keytab entries");
1842 krb5_keytab_entry new[1];
1844 memset(new, 0, sizeof *new);
1845 if ((code == krb5_kt_start_seq_get(context, kt, cursor))) {
1846 afs_com_err(progname, code, "while starting keytab scan");
1849 while (!(code = krb5_kt_next_entry(context, kt, new, cursor))) {
1851 allowed_enctypes[i] && allowed_enctypes[i]
1852 != deref_entry_enctype(new); ++i)
1854 if ((!i || allowed_enctypes[i]) &&
1855 (best < 0 || best > i)) {
1856 krb5_free_keytab_entry_contents(context, entry);
1858 memset(new, 0, sizeof *new);
1859 } else krb5_free_keytab_entry_contents(context, new);
1861 if ((i = krb5_kt_end_seq_get(context, kt, cursor))) {
1862 afs_com_err(progname, i, "while ending keytab scan");
1867 afs_com_err(progname, code, "while scanning keytab");
1870 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1876 if ((code = krb5_generate_random_keyblock(context,
1877 deref_keyblock_enctype(session_key), session_key))) {
1878 afs_com_err(progname, code, "while making session key");
1881 enc_tkt_reply->flags.initial = 1;
1882 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1883 enc_tkt_reply->cname = client_principal->name;
1884 enc_tkt_reply->crealm = client_principal->realm;
1885 enc_tkt_reply->key = *session_key;
1887 static krb5_data empty_string;
1888 enc_tkt_reply->transited.contents = empty_string;
1890 enc_tkt_reply->authtime = starttime;
1891 enc_tkt_reply->starttime = temp_time;
1892 *enc_tkt_reply->starttime = starttime;
1894 enc_tkt_reply->renew_till = temp_time + 1;
1895 *enc_tkt_reply->renew_till = endtime;
1897 enc_tkt_reply->endtime = endtime;
1899 if ((code = krb5_c_make_random_key(context,
1900 deref_keyblock_enctype(session_key), session_key))) {
1901 afs_com_err(progname, code, "while making session key");
1904 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1905 #define DATACAST (unsigned char *)
1906 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1907 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1908 enc_tkt_reply->session = session_key;
1909 enc_tkt_reply->client = client_principal;
1911 static krb5_data empty_string;
1912 enc_tkt_reply->transited.tr_contents = empty_string;
1914 enc_tkt_reply->times.authtime = starttime;
1915 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1916 enc_tkt_reply->times.endtime = endtime;
1917 #endif /* USING_HEIMDAL */
1918 /* NB: We will discard address for now--ignoring caddr field
1919 in any case. MIT branch does what it always did. */
1921 if (paddress && *paddress) {
1922 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1925 faddr->val = address;
1927 for (i = 0; paddress[i]; ++i) {
1929 address[i].addr_type = KRB5_ADDRESS_INET;
1930 address[i].address.data = (void*)(paddress+i);
1931 address[i].address.length = sizeof(paddress[i]);
1934 address[i].magic = KV5M_ADDRESS;
1935 address[i].addrtype = ADDRTYPE_INET;
1937 address[i].addrtype = AF_INET;
1939 address[i].contents = (void*)(paddress+i);
1940 address[i].length = sizeof(int);
1941 faddr[i] = address+i;
1952 ticket_reply->sname = service_principal->name;
1953 ticket_reply->realm = service_principal->realm;
1955 { /* crypto block */
1956 krb5_crypto crypto = 0;
1957 unsigned char *buf = 0;
1958 size_t buf_size, buf_len;
1961 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1962 enc_tkt_reply, &buf_len, code);
1964 afs_com_err(progname, code, "while encoding ticket");
1968 if(buf_len != buf_size) {
1969 afs_com_err(progname, code,
1970 "%u != %u while encoding ticket (internal ASN.1 encoder error",
1971 (unsigned int)buf_len, (unsigned int)buf_size);
1974 what = "krb5_crypto_init";
1975 code = krb5_crypto_init(context,
1976 &deref_entry_keyblock(entry),
1977 deref_entry_enctype(entry),
1980 what = "krb5_encrypt";
1981 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
1982 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
1985 if (crypto) krb5_crypto_destroy(context, crypto);
1987 afs_com_err(progname, code, "while %s", what);
1990 } /* crypto block */
1991 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
1992 ticket_reply->enc_part.kvno = temp_vno;
1993 *ticket_reply->enc_part.kvno = entry->vno;
1994 ticket_reply->tkt_vno = 5;
1996 ticket_reply->server = service_principal;
1997 ticket_reply->enc_part2 = enc_tkt_reply;
1998 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
1999 afs_com_err(progname, code, "while making ticket");
2002 ticket_reply->enc_part.kvno = entry->vno;
2005 /* Construct Creds */
2007 if ((code = krb5_copy_principal(context, service_principal,
2009 afs_com_err(progname, code, "while copying service principal");
2012 if ((code = krb5_copy_principal(context, client_principal,
2014 afs_com_err(progname, code, "while copying client principal");
2017 if ((code = krb5_copy_keyblock_contents(context, session_key,
2018 &deref_session_key(creds)))) {
2019 afs_com_err(progname, code, "while copying session key");
2024 creds->times.authtime = enc_tkt_reply->authtime;
2025 creds->times.starttime = *(enc_tkt_reply->starttime);
2026 creds->times.endtime = enc_tkt_reply->endtime;
2027 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
2028 creds->flags.b = enc_tkt_reply->flags;
2030 creds->times = enc_tkt_reply->times;
2031 creds->ticket_flags = enc_tkt_reply->flags;
2033 if (!deref_enc_tkt_addrs(enc_tkt_reply))
2035 else if ((code = krb5_copy_addresses(context,
2036 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
2037 afs_com_err(progname, code, "while copying addresses");
2043 size_t creds_tkt_len;
2044 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
2045 ticket_reply, &creds_tkt_len, code);
2047 afs_com_err(progname, code, "while encoding ticket");
2052 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
2053 afs_com_err(progname, code, "while encoding ticket");
2056 creds->ticket = *temp;
2063 if (deref_enc_data(&ticket_reply->enc_part))
2064 free(deref_enc_data(&ticket_reply->enc_part));
2065 krb5_free_keytab_entry_contents(context, entry);
2066 if (client_principal)
2067 krb5_free_principal(context, client_principal);
2068 if (service_principal)
2069 krb5_free_principal(context, service_principal);
2071 krb5_cc_close(context, cc);
2073 krb5_kt_close(context, kt);
2074 if (creds) krb5_free_creds(context, creds);
2075 krb5_free_keyblock_contents(context, session_key);
2083 static krb5_error_code
2084 get_credv5(krb5_context context, char *name, char *inst, char *realm,
2089 static krb5_principal client_principal = 0;
2091 afs_dprintf("Getting tickets: %s%s%s@%s\n", name,
2092 (inst && inst[0]) ? "/" : "", inst ? inst : "", realm);
2094 memset(&increds, 0, sizeof(increds));
2095 /* ANL - instance may be ptr to a null string. Pass null then */
2096 if ((r = krb5_build_principal(context, &increds.server,
2097 strlen(realm), realm,
2099 (inst && strlen(inst)) ? inst : (void *) NULL,
2105 if (!_krb425_ccache) {
2106 r = krb5_cc_default(context, &_krb425_ccache);
2110 if (!client_principal) {
2112 r = krb5_parse_name(context, client, &client_principal);
2114 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2120 increds.client = client_principal;
2121 increds.times.endtime = 0;
2122 /* Ask for DES since that is what V4 understands */
2123 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
2126 int allowed_enctypes[] = {
2127 ENCTYPE_DES_CBC_CRC, 0
2130 r = get_credv5_akimpersonate(context,
2139 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
2146 get_user_realm(krb5_context context, char **realm)
2148 static krb5_principal client_principal = 0;
2149 krb5_error_code r = 0;
2153 if (!_krb425_ccache)
2154 krb5_cc_default(context, &_krb425_ccache);
2155 if (!client_principal) {
2157 r = krb5_parse_name(context, client, &client_principal);
2159 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2165 *realm = extract_realm(context, client_principal);