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>
38 #include <afs/param.h>
46 #include <afs/token.h>
48 #define KERBEROS_APPLE_DEPRECATED(x)
52 #elif HAVE_ET_COM_ERR_H
53 # include <et/com_err.h>
54 #elif HAVE_KRB5_COM_ERR_H
55 # include <krb5/com_err.h>
57 # error No com_err.h? We need some kind of com_err.h
60 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
61 #include <afs/com_err.h>
65 #include <sys/ioccom.h>
69 #include <afs/cellconfig.h>
71 #include <afs/venus.h>
72 #include <afs/ptserver.h>
73 #include <afs/ptuser.h>
74 #include <afs/pterror.h>
75 #include <afs/dirpath.h>
76 #include <afs/afsutil.h>
79 #include "linked_list.h"
81 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
84 #ifdef HAVE_KRB5_CREDS_SESSION
85 #define USING_HEIMDAL 1
91 #ifndef AFS_TRY_FULL_PRINC
92 #define AFS_TRY_FULL_PRINC 1
93 #endif /* AFS_TRY_FULL_PRINC */
95 #define AKLOG_TRYAGAIN -1
96 #define AKLOG_SUCCESS 0
98 #define AKLOG_SOMETHINGSWRONG 2
100 #define AKLOG_KERBEROS 4
101 #define AKLOG_TOKEN 5
102 #define AKLOG_BADPATH 6
114 /* RedHat 4.x doesn't seem to define this */
115 #define MAXSYMLINKS 5
118 #define DIR '/' /* Character that divides directories */
119 #define DIRSTRING "/" /* String form of above */
120 #define VOLMARKER ':' /* Character separating cellname from mntpt */
121 #define VOLMARKERSTRING ":" /* String form of above */
125 char realm[REALM_SZ];
128 static krb5_ccache _krb425_ccache = NULL;
131 * Why doesn't AFS provide these prototypes?
134 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
140 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *, int);
141 static int isdir(char *, unsigned char *);
142 static krb5_error_code get_credv5(krb5_context context, char *, char *,
143 char *, krb5_creds **);
144 static int get_user_realm(krb5_context, char **);
146 #define TRYAGAIN(x) (x == AKLOG_TRYAGAIN || \
147 x == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || \
148 x == KRB5KRB_ERR_GENERIC)
150 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
152 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
153 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
154 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
155 #define realm_data(c, p) krb5_princ_realm(c, p)->data
156 #define realm_len(c, p) krb5_princ_realm(c, p)->length
158 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
160 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
161 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
162 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
163 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
164 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
167 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
170 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
171 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
175 krb5_encrypt_tkt_part(krb5_context context,
176 const krb5_keyblock *key,
183 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
185 if ((code = krb5_c_encrypt_length(context, key->enctype,
186 data->length, &enclen)))
188 ticket->enc_part.ciphertext.length = enclen;
189 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
193 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
194 0, data, &ticket->enc_part))) {
195 free(ticket->enc_part.ciphertext.data);
196 ticket->enc_part.ciphertext.data = 0;
208 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
210 #define get_cred_keydata(c) c->keyblock.contents
211 #define get_cred_keylen(c) c->keyblock.length
212 #define get_creds_enctype(c) c->keyblock.enctype
214 #elif defined(HAVE_KRB5_CREDS_SESSION)
216 #define get_cred_keydata(c) c->session.keyvalue.data
217 #define get_cred_keylen(c) c->session.keyvalue.length
218 #define get_creds_enctype(c) c->session.keytype
221 #error "Must have either keyblock or session member of krb5_creds"
224 /* MITKerberosShim logs but returns success */
225 #if !defined(HAVE_KRB5_524_CONV_PRINCIPAL) || defined(AFS_DARWIN110_ENV) || (!defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC))
226 #define HAVE_NO_KRB5_524
227 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
228 #define krb5_524_convert_creds krb524_convert_creds_kdc
232 #define deref_keyblock_enctype(kb) \
235 #define deref_entry_keyblock(entry) \
238 #define deref_session_key(creds) \
241 #define deref_enc_tkt_addrs(tkt) \
244 #define deref_enc_length(enc) \
245 ((enc)->cipher.length)
247 #define deref_enc_data(enc) \
250 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
253 #define deref_keyblock_enctype(kb) \
256 #define deref_entry_keyblock(entry) \
259 #define deref_session_key(creds) \
262 #define deref_enc_tkt_addrs(tkt) \
265 #define deref_enc_length(enc) \
266 ((enc)->ciphertext.length)
268 #define deref_enc_data(enc) \
269 ((enc)->ciphertext.data)
273 #define deref_entry_enctype(entry) \
274 deref_keyblock_enctype(&deref_entry_keyblock(entry))
277 * Provide a replacement for strerror if we don't have it
280 #ifndef HAVE_STRERROR
281 extern char *sys_errlist[];
282 #define strerror(x) sys_errlist[x]
283 #endif /* HAVE_STRERROR */
285 static char *progname = NULL; /* Name of this program */
286 static int dflag = FALSE; /* Give debugging information */
287 static int noauth = FALSE; /* If true, don't try to get tokens */
288 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
289 static int hosts = FALSE; /* Are we keeping track of hosts? */
290 static int noprdb = FALSE; /* Skip resolving name to id? */
291 static int linked = FALSE; /* try for both AFS nodes */
292 static int afssetpag = FALSE; /* setpag for AFS */
293 static int force = FALSE; /* Bash identical tokens? */
294 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
295 static char *keytab = NULL; /* keytab for akimpersonate */
296 static char *client = NULL; /* client principal for akimpersonate */
297 static linked_list zsublist; /* List of zephyr subscriptions */
298 static linked_list hostlist; /* List of host addresses */
299 static linked_list authedcells; /* List of cells already logged to */
301 /* A com_error bodge. The idea here is that this routine lets us lookup
302 * things in the system com_err, if the AFS one just tells us the error
307 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
314 const char *str = afs_error_message(code);
315 if (strncmp(str, "unknown", strlen("unknown")) == 0) {
316 #ifdef HAVE_KRB5_SVC_GET_MSG
317 krb5_svc_get_msg(code,&str);
318 #elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
319 krb5_context context;
320 krb5_init_context(&context);
321 str = krb5_get_error_message(context, code);
322 krb5_free_context(context);
324 ; /* IRIX apparently has neither: use the string we have */
329 #ifdef HAVE_KRB5_SVC_GET_MSG
330 krb5_free_string(str);
334 vfprintf(stderr, fmt, ap);
341 afs_dprintf(char *fmt, ...) {
351 copy_cellinfo(cellinfo_t *cellinfo)
353 cellinfo_t *new_cellinfo;
355 if ((new_cellinfo = malloc(sizeof(cellinfo_t))))
356 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
358 return ((char *)new_cellinfo);
363 get_cellconfig(const char *config, char *cell,
364 struct afsconf_cell *cellconfig, char **local_cell)
366 int status = AKLOG_SUCCESS;
367 struct afsconf_dir *configdir;
369 memset(cellconfig, 0, sizeof(*cellconfig));
371 *local_cell = malloc(MAXCELLCHARS);
372 if (*local_cell == NULL) {
373 fprintf(stderr, "%s: can't allocate memory for local cell name\n",
378 if (!(configdir = afsconf_Open(config))) {
380 "%s: can't get afs configuration (afsconf_Open(%s))\n",
385 if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
386 fprintf(stderr, "%s: can't determine local cell.\n", progname);
390 if ((cell == NULL) || (cell[0] == 0))
393 /* XXX - This function modifies 'cell' by passing it through lcstring */
394 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
395 fprintf(stderr, "%s: Can't get information about cell %s.\n",
400 afsconf_Close(configdir);
406 extract_realm(krb5_context context, krb5_principal princ) {
410 len = realm_len(context, princ);
411 if (len > REALM_SZ-1)
414 realm = malloc(sizeof(char) * (len+1));
418 strncpy(realm, realm_data(context, princ), len);
425 get_realm_from_cred(krb5_context context, krb5_creds *v5cred, char **realm) {
426 #if !defined(HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
427 krb5_error_code code;
432 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
436 *realm = extract_realm(context, ticket->server);
440 krb5_free_ticket(context, ticket);
450 * Get a Kerberos service ticket to use as the base of an rxkad token for
454 * An initialized Kerberos v5 context
456 * The realm to look in for the service principal. If NULL, then the
457 * realm is determined from the cell name or the user's credentials
458 * (see below for the heuristics used)
460 * The cell information for the cell to obtain a ticket for
462 * A Kerberos credentials structure containing the ticket acquired
463 * for the cell. This is a dynamically allocated structure, which
464 * should be freed by using the appropriate Kerberos API function.
465 * @param[out] realmUsed
466 * The realm in which the cell's service principal was located. If
467 * unset, then the principal was located in the same realm as the
468 * current user. This is a malloc'd string which should be freed
472 * 0 on success, an error value upon failure
475 * This code tries principals in the following, much debated,
478 * If the realm is specified on the command line we do
479 * - afs/cell@COMMAND-LINE-REALM
480 * - afs@COMMAND-LINE-REALM
483 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
484 * - afs/cell@krb5_get_host_realm(db-server)
485 * Then, if krb5_get_host_realm(db-server) is non-empty
486 * - afs@ krb5_get_host_realm(db-server)
488 * - afs/cell@ upper-case-domain-of-db-server
489 * - afs@ upper-case-domain-of-db-server
491 * In all cases, the 'afs@' variant is only tried where the
492 * cell and the realm match case-insensitively.
496 rxkad_get_ticket(krb5_context context, char *realm,
497 struct afsconf_cell *cell,
498 krb5_creds **v5cred, char **realmUsed) {
499 char *realm_of_cell = NULL;
500 char *realm_of_user = NULL;
501 char *realm_from_princ = NULL;
507 if ((status = get_user_realm(context, &realm_of_user))) {
508 fprintf(stderr, "%s: Couldn't determine realm of user:", progname);
509 afs_com_err(progname, status, " while getting realm");
510 status = AKLOG_KERBEROS;
517 /* Cell on command line - use that one */
518 if (realm && realm[0]) {
519 realm_of_cell = realm;
520 status = AKLOG_TRYAGAIN;
521 afs_dprintf("We were told to authenticate to realm %s.\n", realm);
523 /* Initially, try using afs/cell@USERREALM */
524 afs_dprintf("Trying to authenticate to user's realm %s.\n",
526 realm_of_cell = realm_of_user;
527 status = get_credv5(context, AFSKEY, cell->name, realm_of_cell,
530 /* If that failed, try to determine the realm from the name of
531 * one of the DB servers */
532 if (TRYAGAIN(status)) {
533 realm_of_cell = afs_realm_of_cell(context, cell, FALSE);
534 if (!realm_of_cell) {
535 fprintf(stderr, "%s: Couldn't figure out realm for cell "
536 "%s.\n", progname, cell->name);
540 if (realm_of_cell[0])
541 afs_dprintf("We've deduced that we need to authenticate"
542 " to realm %s.\n", realm_of_cell);
544 afs_dprintf("We've deduced that we need to authenticate "
545 "using referrals.\n");
549 if (TRYAGAIN(status)) {
550 /* If we've got the full-princ-first option, or we're in a
551 * different realm from the cell - use the cell name as the
553 if (AFS_TRY_FULL_PRINC ||
554 strcasecmp(cell->name, realm_of_cell)!=0) {
555 status = get_credv5(context, AFSKEY, cell->name,
556 realm_of_cell, v5cred);
558 /* If we failed & we've got an empty realm, then try
559 * calling afs_realm_for_cell again. */
560 if (TRYAGAIN(status) && !realm_of_cell[0]) {
561 /* This time, get the realm by taking the domain
562 * component of the db server and make it upper case */
563 realm_of_cell = afs_realm_of_cell(context, cell, TRUE);
564 if (!realm_of_cell) {
566 "%s: Couldn't figure out realm for cell %s.\n",
567 progname, cell->name);
570 afs_dprintf("We've deduced that we need to authenticate"
571 " to realm %s.\n", realm_of_cell);
572 status = get_credv5(context, AFSKEY, cell->name,
573 realm_of_cell, v5cred);
577 /* If the realm and cell name match, then try without an
578 * instance, but only if realm is non-empty */
580 if (TRYAGAIN(status) &&
581 strcasecmp(cell->name, realm_of_cell) == 0) {
582 status = get_credv5(context, AFSKEY, NULL, realm_of_cell,
584 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
585 status = get_credv5(context, AFSKEY, cell->name,
586 realm_of_cell, v5cred);
591 /* Try to find a service principal for this cell.
592 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
593 * the first attempt, so we try twice to be sure */
595 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
602 afs_dprintf("Kerberos error code returned by get_cred : %d\n", status);
603 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
604 progname, cell->name);
605 afs_com_err(progname, status, "while getting AFS tickets");
606 #ifdef KRB5_CC_NOT_KTYPE
607 if (status == KRB5_CC_NOT_KTYPE) {
608 fprintf(stderr, "allow_weak_crypto may be required in the Kerberos configuration\n");
611 status = AKLOG_KERBEROS;
615 /* If we've got a valid ticket, and we still don't know the realm name
616 * try to figure it out from the contents of the ticket
618 if (strcmp(realm_of_cell, "") == 0) {
619 status = get_realm_from_cred(context, *v5cred, &realm_from_princ);
622 "%s: Couldn't decode ticket to determine realm for "
624 progname, cell->name);
626 if (realm_from_princ)
627 realm_of_cell = realm_from_princ;
631 /* If the realm of the user and cell differ, then we need to use the
632 * realm when we later construct the user's principal */
633 if (realm_of_cell != NULL && strcmp(realm_of_user, realm_of_cell) != 0)
634 *realmUsed = realm_of_user;
637 if (realm_from_princ)
638 free(realm_from_princ);
639 if (realm_of_user && *realmUsed == NULL)
646 * Build an rxkad token from a Kerberos ticket, using only local tools (that
647 * is, without using a 524 conversion service)
650 * An initialised Kerberos 5 context
652 * A Kerberos credentials structure containing a suitable service ticket
653 * @param[out] tokenPtr
654 * An AFS token structure containing an rxkad token. This is a malloc'd
655 * structure which should be freed by the caller.
656 * @param[out[ userPtr
657 * A string containing the principal of the user to whom the token was
658 * issued. This is a malloc'd block which should be freed by the caller,
662 * 0 on success, an error value upon failure
665 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
666 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
667 char username[BUFSIZ]="";
668 struct ktc_token token;
670 #ifdef HAVE_NO_KRB5_524
674 char k4name[ANAME_SZ];
675 char k4inst[INST_SZ];
676 char k4realm[REALM_SZ];
678 void *inkey = get_cred_keydata(v5cred);
679 size_t inkey_sz = get_cred_keylen(v5cred);
681 afs_dprintf("Using Kerberos V5 ticket natively\n");
686 #ifndef HAVE_NO_KRB5_524
687 status = krb5_524_conv_principal (context, v5cred->client,
693 afs_com_err(progname, status,
694 "while converting principal to Kerberos V4 format");
696 strcpy (username, k4name);
698 strcat (username, ".");
699 strcat (username, k4inst);
703 len = min(get_princ_len(context, v5cred->client, 0),
704 second_comp(context, v5cred->client) ?
705 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
706 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
707 username[len] = '\0';
709 if (second_comp(context, v5cred->client)) {
710 strcat(username, ".");
711 p = username + strlen(username);
712 len = min(get_princ_len(context, v5cred->client, 1),
713 MAXKTCNAMELEN - strlen(username) - 1);
714 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
719 memset(&token, 0, sizeof(struct ktc_token));
721 token.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
722 token.startTime = v5cred->times.starttime;;
723 token.endTime = v5cred->times.endtime;
724 if (tkt_DeriveDesKey(get_creds_enctype(v5cred), inkey, inkey_sz,
725 &token.sessionKey) != 0) {
728 token.ticketLen = v5cred->ticket.length;
729 memcpy(token.ticket, v5cred->ticket.data, token.ticketLen);
731 status = token_importRxkadViceId(tokenPtr, &token, 0);
736 if (username[0] != '\0')
737 *userPtr = strdup(username);
743 * Convert a Keberos ticket to an rxkad token, using information obtained
744 * from an external Kerberos 5->4 conversion service. If the code is built
745 * with HAVE_NO_KRB5_524 then this is a stub function which will always
746 * return success without a token.
749 * An initialised Kerberos 5 context
751 * A Kerberos credentials structure containing a suitable service ticket
752 * @param[out] tokenPtr
753 * An AFS token structure containing an rxkad token. This is a malloc'd
754 * structure which should be freed by the caller.
755 * @param[out[ userPtr
756 * A string containing the principal of the user to whom the token was
757 * issued. This is a malloc'd block which should be freed by the caller,
761 * 0 on success, an error value upon failure
764 #ifdef HAVE_NO_KRB5_524
766 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
767 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
775 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
776 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
778 char username[BUFSIZ];
779 struct ktc_token token;
785 afs_dprintf("Using Kerberos 524 translator service\n");
787 status = krb5_524_convert_creds(context, v5cred, &cred);
790 afs_com_err(progname, status, "while converting tickets "
791 "to Kerberos V4 format");
792 return AKLOG_KERBEROS;
795 strcpy (username, cred.pname);
797 strcat (username, ".");
798 strcat (username, cred.pinst);
801 memset(&token, 0, sizeof(struct ktc_token));
803 token.kvno = cred.kvno;
804 token.startTime = cred.issue_date;
806 * It seems silly to go through a bunch of contortions to
807 * extract the expiration time, when the v5 credentials already
808 * has the exact time! Let's use that instead.
810 * Note that this isn't a security hole, as the expiration time
811 * is also contained in the encrypted token
813 token.endTime = v5cred->times.endtime;
814 memcpy(&token.sessionKey, cred.session, 8);
815 token.ticketLen = cred.ticket_st.length;
816 memcpy(token.ticket, cred.ticket_st.dat, token.ticketLen);
818 status = token_importRxkadViceId(tokenPtr, &token, 0);
823 *userPtr = strdup(username);
830 * This function gets an rxkad token for a given cell.
833 * An initialized Kerberos v5 context
835 * The cell information for the cell which we're obtaining a token for
837 * The realm to look in for the service principal. If NULL, then the
838 * realm is determined from the cell name or the user's credentials
839 * (see the documentation for rxkad_get_ticket)
841 * The rxkad token produced. This is a malloc'd structure which should
842 * be freed by the caller.
843 * @parma[out] authuser
844 * A string containing the principal of the user to whom the token was
845 * issued. This is a malloc'd block which should be freed by the caller,
847 * @param[out] foreign
848 * Whether the user is considered as 'foreign' to the realm of the cell.
851 * 0 on success, an error value upon failuer
854 rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
855 struct ktc_tokenUnion **token, char **authuser, int *foreign) {
857 char *realmUsed = NULL;
858 char *username = NULL;
865 status = rxkad_get_ticket(context, realm, cell, &v5cred, &realmUsed);
870 status = rxkad_get_converted_token(context, v5cred, token, &username);
872 status = rxkad_build_native_token(context, v5cred, token, &username);
877 /* We now have the username, plus the realm name, so stitch them together
878 * to give us the name that the ptserver will know the user by */
879 if (realmUsed == NULL || username == NULL) {
880 *authuser = username;
884 asprintf(authuser, "%s@%s", username, realmUsed);
898 * Log to a cell. If the cell has already been logged to, return without
899 * doing anything. Otherwise, log to it and mark that it has been logged
903 auth_to_cell(krb5_context context, const char *config,
904 char *cell, char *realm, char **linkedcell)
906 int status = AKLOG_SUCCESS;
908 char *username = NULL; /* To hold client username structure */
909 afs_int32 viceId; /* AFS uid of user */
911 char *local_cell = NULL;
912 struct ktc_tokenUnion *rxkadToken = NULL;
913 struct ktc_setTokenData *token;
914 struct ktc_setTokenData *btoken = NULL;
915 struct afsconf_cell cellconf;
917 /* NULL or empty cell returns information on local cell */
918 if ((status = get_cellconfig(config, cell, &cellconf, &local_cell)))
921 if (linkedcell != NULL) {
922 if (cellconf.linkedCell != NULL) {
923 *linkedcell = strdup(cellconf.linkedCell);
924 if (*linkedcell == NULL) {
933 if (ll_string(&authedcells, ll_s_check, cellconf.name)) {
934 afs_dprintf("Already authenticated to %s (or tried to)\n", cellconf.name);
935 status = AKLOG_SUCCESS;
940 * Record that we have attempted to log to this cell. We do this
941 * before we try rather than after so that we will not try
942 * and fail repeatedly for one cell.
944 ll_string(&authedcells, ll_s_add, cellconf.name);
947 * Record this cell in the list of zephyr subscriptions. We may
948 * want zephyr subscriptions even if authentication fails.
949 * If this is done after we attempt to get tokens, aklog -zsubs
950 * can return something different depending on whether or not we
951 * are in -noauth mode.
953 if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
955 "%s: failure adding cell %s to zephyr subscriptions list.\n",
956 progname, cellconf.name);
959 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
961 "%s: failure adding cell %s to zephyr subscriptions list.\n",
962 progname, local_cell);
967 afs_dprintf("Authenticating to cell %s (server %s).\n", cellconf.name,
968 cellconf.hostName[0]);
970 token = token_buildTokenJar(cellconf.name);
976 status = rxkad_get_token(context, &cellconf, realm, &rxkadToken,
977 &username, &isForeign);
981 /* We need to keep the token structure around so that we can stick
982 * the viceId into it (once we know it) */
983 status = token_addToken(token, rxkadToken);
985 afs_dprintf("Add Token failed with %d", status);
990 ktc_GetTokenEx(cellconf.name, &btoken) == 0 &&
991 token_SetsEquivalent(token, btoken)) {
993 token_FreeSet(&btoken);
994 afs_dprintf("Identical tokens already exist; skipping.\n");
995 status = AKLOG_SUCCESS;
1000 token_FreeSet(&btoken);
1006 if (username == NULL) {
1007 afs_dprintf("Not resolving name to id\n");
1010 afs_dprintf("Not resolving name %s to id (-noprdb set)\n", username);
1013 afs_dprintf("About to resolve name %s to id in cell %s.\n", username,
1016 if (!pr_Initialize (0, AFSDIR_CLIENT_ETC_DIRPATH, cellconf.name))
1017 status = pr_SNameToId (username, &viceId);
1020 afs_dprintf("Error %d\n", status);
1022 afs_dprintf("Id %d\n", (int) viceId);
1026 * This code is taken from cklog -- it lets people
1027 * automatically register with the ptserver in foreign cells
1030 #ifdef ALLOW_REGISTER
1031 if ((status == 0) && (viceId == ANONYMOUSID) && isForeign) {
1032 afs_dprintf("doing first-time registration of %s at %s\n",
1033 username, cellconf.name);
1036 status = ktc_SetTokenEx(token);
1038 afs_com_err(progname, status,
1039 "while obtaining tokens for cell %s",
1041 status = AKLOG_TOKEN;
1045 * In case you're wondering, we don't need to change the
1046 * filename here because we're still connecting to the
1047 * same cell -- we're just using a different authenticat ion
1051 if ((status = pr_Initialize(1L, AFSDIR_CLIENT_ETC_DIRPATH,
1053 printf("Error %d\n", status);
1056 if ((status = pr_CreateUser(username, &viceId))) {
1057 fprintf(stderr, "%s: %s so unable to create remote PTS "
1058 "user %s in cell %s (status: %d).\n", progname,
1059 afs_error_message(status), username, cellconf.name,
1061 viceId = ANONYMOUSID;
1063 printf("created cross-cell entry for %s (Id %d) at %s\n",
1064 username, viceId, cellconf.name);
1067 #endif /* ALLOW_REGISTER */
1069 if ((status == 0) && (viceId != ANONYMOUSID)) {
1070 status = token_setRxkadViceId(rxkadToken, viceId);
1072 fprintf(stderr, "Error %d setting rxkad ViceId\n", status);
1073 status = AKLOG_SUCCESS;
1075 token_replaceToken(token, rxkadToken);
1081 afs_dprintf("Setting tokens. %s @ %s\n",
1082 username, cellconf.name);
1084 afs_dprintf("Setting tokens for cell %s\n", cellconf.name);
1087 #ifndef AFS_AIX51_ENV
1088 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
1089 * this routine, it will not add the token. It is not clear what
1090 * is going on here! So we will do the following operation.
1091 * On AIX 5, it causes the parent program to die, so we won't.
1093 write(2,"",0); /* dummy write */
1095 token_setPag(token, afssetpag);
1096 status = ktc_SetTokenEx(token);
1098 afs_com_err(progname, status, "while setting tokens for cell %s",
1100 status = AKLOG_TOKEN;
1104 afs_dprintf("Noauth mode; not authenticating.\n");
1108 token_freeToken(&rxkadToken);
1120 get_afs_mountpoint(char *file, char *mountpoint, int size)
1123 char V ='V'; /* AFS has problem on Sun with pioctl */
1125 char our_file[MAXPATHLEN + 1];
1127 char *last_component;
1128 struct ViceIoctl vio;
1129 char cellname[BUFSIZ];
1131 strlcpy(our_file, file, sizeof(our_file));
1133 if ((last_component = strrchr(our_file, DIR))) {
1134 *last_component++ = 0;
1135 parent_dir = our_file;
1138 last_component = our_file;
1142 memset(cellname, 0, sizeof(cellname));
1144 vio.in = last_component;
1145 vio.in_size = strlen(last_component)+1;
1146 vio.out_size = size;
1147 vio.out = mountpoint;
1149 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
1150 if (strchr(mountpoint, VOLMARKER) == NULL) {
1152 vio.in_size = strlen(file) + 1;
1153 vio.out_size = sizeof(cellname);
1156 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
1157 strlcat(cellname, VOLMARKERSTRING, sizeof(cellname));
1158 strlcat(cellname, mountpoint + 1, sizeof(cellname));
1159 memset(mountpoint + 1, 0, size - 1);
1160 strcpy(mountpoint + 1, cellname);
1170 * This routine each time it is called returns the next directory
1171 * down a pathname. It resolves all symbolic links. The first time
1172 * it is called, it should be called with the name of the path
1173 * to be descended. After that, it should be called with the arguemnt
1177 next_path(char *origpath)
1179 static char path[MAXPATHLEN + 1];
1180 static char pathtocheck[MAXPATHLEN + 1];
1182 ssize_t link; /* Return value from readlink */
1183 char linkbuf[MAXPATHLEN + 1];
1184 char tmpbuf[MAXPATHLEN + 1];
1186 static char *last_comp; /* last component of directory name */
1187 static char *elast_comp; /* End of last component */
1191 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1193 /* If we are given something for origpath, we are initializing only. */
1195 memset(path, 0, sizeof(path));
1196 memset(pathtocheck, 0, sizeof(pathtocheck));
1197 strlcpy(path, origpath, sizeof(path));
1203 /* We were not given origpath; find then next path to check */
1205 /* If we've gotten all the way through already, return NULL */
1206 if (last_comp == NULL)
1210 while (*last_comp == DIR)
1211 strncat(pathtocheck, last_comp++, 1);
1212 len = (elast_comp = strchr(last_comp, DIR))
1213 ? elast_comp - last_comp : strlen(last_comp);
1214 strncat(pathtocheck, last_comp, len);
1215 memset(linkbuf, 0, sizeof(linkbuf));
1216 link = readlink(pathtocheck, linkbuf, sizeof(linkbuf)-1);
1219 linkbuf[link] = '\0'; /* NUL terminate string */
1221 if (++symlinkcount > MAXSYMLINKS) {
1222 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1223 exit(AKLOG_BADPATH);
1226 memset(tmpbuf, 0, sizeof(tmpbuf));
1228 strlcpy(tmpbuf, elast_comp, sizeof(tmpbuf));
1229 if (linkbuf[0] == DIR) {
1231 * If this is a symbolic link to an absolute path,
1232 * replace what we have by the absolute path.
1234 memset(path, 0, strlen(path));
1235 memcpy(path, linkbuf, sizeof(linkbuf));
1236 strcat(path, tmpbuf);
1239 memset(pathtocheck, 0, sizeof(pathtocheck));
1243 * If this is a symbolic link to a relative path,
1244 * replace only the last component with the link name.
1246 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1247 strcat(path, tmpbuf);
1249 if ((t = strrchr(pathtocheck, DIR))) {
1251 memset(t, 0, strlen(t));
1254 memset(pathtocheck, 0, sizeof(pathtocheck));
1258 last_comp = elast_comp;
1262 return(pathtocheck);
1266 add_hosts(char *file)
1269 char V = 'V'; /* AFS has problem on SunOS */
1271 struct ViceIoctl vio;
1272 char outbuf[BUFSIZ];
1278 memset(outbuf, 0, sizeof(outbuf));
1280 vio.out_size = sizeof(outbuf);
1284 afs_dprintf("Getting list of hosts for %s\n", file);
1286 /* Don't worry about errors. */
1287 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1288 phosts = (long *) outbuf;
1291 * Lists hosts that we care about. If ALLHOSTS is defined,
1292 * then all hosts that you ever may possible go through are
1293 * included in this list. If not, then only hosts that are
1294 * the only ones appear. That is, if a volume you must use
1295 * is replaced on only one server, that server is included.
1296 * If it is replicated on many servers, then none are included.
1297 * This is not perfect, but the result is that people don't
1298 * get subscribed to a lot of instances of FILSRV that they
1299 * probably won't need which reduces the instances of
1300 * people getting messages that don't apply to them.
1303 if (phosts[1] != '\0')
1306 for (i = 0; phosts[i]; i++) {
1308 in.s_addr = phosts[i];
1309 afs_dprintf("Got host %s\n", inet_ntoa(in));
1310 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1312 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1313 afs_dprintf("Got host %s\n", hp->h_name);
1314 ll_string(&zsublist, ll_s_add, hp->h_name);
1321 * This routine descends through a path to a directory, logging to
1322 * every cell it encounters along the way.
1325 auth_to_path(krb5_context context, const char *config, char *path)
1327 int status = AKLOG_SUCCESS;
1328 int auth_status = AKLOG_SUCCESS;
1331 char pathtocheck[MAXPATHLEN + 1];
1332 char mountpoint[MAXPATHLEN + 1];
1341 strlcpy(pathtocheck, path, sizeof(pathtocheck));
1343 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1344 fprintf(stderr, "Unable to find current working directory:\n");
1345 fprintf(stderr, "%s\n", pathtocheck);
1346 fprintf(stderr, "Try an absolute pathname.\n");
1347 exit(AKLOG_BADPATH);
1350 strlcat(pathtocheck, DIRSTRING, sizeof(pathtocheck));
1351 strlcat(pathtocheck, path, sizeof(pathtocheck));
1354 next_path(pathtocheck);
1356 /* Go on to the next level down the path */
1357 while ((nextpath = next_path(NULL))) {
1358 strlcpy(pathtocheck, nextpath, sizeof(pathtocheck));
1359 afs_dprintf("Checking directory %s\n", pathtocheck);
1361 * If this is an afs mountpoint, determine what cell from
1362 * the mountpoint name which is of the form
1363 * #cellname:volumename or %cellname:volumename.
1365 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1366 /* skip over the '#' or '%' */
1367 cell = mountpoint + 1;
1368 /* Add this (cell:volumename) to the list of zsubs */
1370 ll_string(&zsublist, ll_s_add, cell);
1372 add_hosts(pathtocheck);
1373 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1375 auth_status = auth_to_cell(context, config, cell, NULL, NULL);
1377 if (status == AKLOG_SUCCESS)
1378 status = auth_status;
1379 else if (status != auth_status)
1380 status = AKLOG_SOMETHINGSWRONG;
1385 if (isdir(pathtocheck, &isdirectory) < 0) {
1387 * If we've logged and still can't stat, there's
1390 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1391 pathtocheck, strerror(errno));
1392 return(AKLOG_BADPATH);
1394 else if (! isdirectory) {
1395 /* Allow only directories */
1396 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1398 return(AKLOG_BADPATH);
1408 /* Print usage message and exit */
1412 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1413 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1414 "[[-p | -path] pathname]\n",
1415 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1417 #ifndef HAVE_NO_KRB5_524
1421 fprintf(stderr, " -d gives debugging information.\n");
1422 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1423 fprintf(stderr, " pathname is the name of a directory to which ");
1424 fprintf(stderr, "you wish to authenticate.\n");
1425 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1426 fprintf(stderr, " -hosts gives host address information.\n");
1427 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1428 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1429 fprintf(stderr, " -force means replace identical tickets. \n");
1430 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1431 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1432 #ifndef HAVE_NO_KRB5_524
1433 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1435 fprintf(stderr, " No commandline arguments means ");
1436 fprintf(stderr, "authenticate to the local cell.\n");
1437 fprintf(stderr, "\n");
1442 main(int argc, char *argv[])
1444 krb5_context context;
1445 int status = AKLOG_SUCCESS;
1447 int somethingswrong = FALSE;
1449 cellinfo_t cellinfo;
1451 extern char *progname; /* Name of this program */
1453 int cmode = FALSE; /* Cellname mode */
1454 int pmode = FALSE; /* Path name mode */
1456 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1457 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1458 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1460 linked_list cells; /* List of cells to log to */
1461 linked_list paths; /* List of paths to log to */
1464 const char *config = AFSDIR_CLIENT_ETC_DIRPATH;
1466 memset(&cellinfo, 0, sizeof(cellinfo));
1468 memset(realm, 0, sizeof(realm));
1469 memset(cell, 0, sizeof(cell));
1470 memset(path, 0, sizeof(path));
1478 /* Store the program name here for error messages */
1479 if ((progname = strrchr(argv[0], DIR)))
1484 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1486 char *filepath = NULL, *newpath = NULL;
1487 #ifndef AFS_DARWIN_ENV
1488 char *defaultpath = "/etc/krb5.conf:/etc/krb5/krb5.conf";
1490 char *defaultpath = "~/Library/Preferences/edu.mit.Kerberos:/Library/Preferences/edu.mit.Kerberos";
1492 filepath = getenv("KRB5_CONFIG");
1494 /* only fiddle with KRB5_CONFIG if krb5-weak.conf actually exists */
1495 asprintf(&newpath, "%s/krb5-weak.conf", AFSDIR_CLIENT_ETC_DIRPATH);
1496 if (access(newpath, R_OK) == 0) {
1499 asprintf(&newpath, "%s:%s/krb5-weak.conf",
1500 filepath ? filepath : defaultpath,
1501 AFSDIR_CLIENT_ETC_DIRPATH);
1502 setenv("KRB5_CONFIG", newpath, 1);
1505 krb5_init_context(&context);
1507 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1510 setenv("KRB5_CONFIG", filepath, 1);
1512 unsetenv("KRB5_CONFIG");
1515 initialize_KTC_error_table ();
1516 initialize_U_error_table();
1517 initialize_RXK_error_table();
1518 initialize_ACFG_error_table();
1519 initialize_PT_error_table();
1520 afs_set_com_err_hook(redirect_errors);
1523 * Enable DES enctypes, which are currently still required for AFS.
1524 * krb5_allow_weak_crypto is MIT Kerberos 1.8. krb5_enctype_enable is
1527 #if defined(HAVE_KRB5_ENCTYPE_ENABLE)
1528 i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
1530 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1531 #elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
1532 krb5_allow_weak_crypto(context, 1);
1535 /* Initialize list of cells to which we have authenticated */
1536 ll_init(&authedcells);
1538 /* Parse commandline arguments and make list of what to do. */
1539 for (i = 1; i < argc; i++) {
1540 if (strcmp(argv[i], "-d") == 0)
1542 else if (strcmp(argv[i], "-noauth") == 0)
1544 else if (strcmp(argv[i], "-zsubs") == 0)
1546 else if (strcmp(argv[i], "-hosts") == 0)
1548 else if (strcmp(argv[i], "-noprdb") == 0)
1550 else if (strcmp(argv[i], "-linked") == 0)
1552 else if (strcmp(argv[i], "-force") == 0)
1554 #ifndef HAVE_NO_KRB5_524
1555 else if (strcmp(argv[i], "-524") == 0)
1558 else if (strcmp(argv[i], "-setpag") == 0)
1560 else if (((strcmp(argv[i], "-cell") == 0) ||
1561 (strcmp(argv[i], "-c") == 0)) && !pmode)
1564 strlcpy(cell, argv[i], sizeof(cell));
1568 else if ((strcmp(argv[i], "-keytab") == 0))
1574 else if ((strcmp(argv[i], "-principal") == 0))
1580 else if (((strcmp(argv[i], "-path") == 0) ||
1581 (strcmp(argv[i], "-p") == 0)) && !cmode)
1584 strlcpy(path, argv[i], sizeof(path));
1588 else if (strcmp(argv[i], "-config") == 0)
1594 else if (argv[i][0] == '-')
1596 else if (!pmode && !cmode) {
1597 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1598 (strcmp(argv[i], "..") == 0)) {
1600 strlcpy(path, argv[i], sizeof(path));
1604 strlcpy(cell, argv[i], sizeof(path));
1611 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1614 strlcpy(realm, argv[i], sizeof(realm));
1618 /* Add this cell to list of cells */
1619 strcpy(cellinfo.cell, cell);
1620 strcpy(cellinfo.realm, realm);
1621 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1623 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1624 ll_add_data(cur_node, new_cellinfo);
1627 "%s: failure copying cellinfo.\n", progname);
1632 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1636 memset(&cellinfo, 0, sizeof(cellinfo));
1638 memset(cell, 0, sizeof(cell));
1639 memset(realm, 0, sizeof(realm));
1642 /* Add this path to list of paths */
1643 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1645 if ((new_path = strdup(path)))
1646 ll_add_data(cur_node, new_path);
1648 fprintf(stderr, "%s: failure copying path name.\n",
1654 fprintf(stderr, "%s: failure adding path to paths list.\n",
1659 memset(path, 0, sizeof(path));
1663 /* If nothing was given, log to the local cell. */
1664 if ((cells.nelements + paths.nelements) == 0) {
1667 status = auth_to_cell(context, config, NULL, NULL, &linkedcell);
1669 /* If this cell is linked to a DCE cell, and user requested -linked,
1670 * get tokens for both. This is very useful when the AFS cell is
1671 * linked to a DFS cell and this system does not also have DFS.
1674 if (!status && linked && linkedcell != NULL) {
1675 afs_dprintf("Linked cell: %s\n", linkedcell);
1676 status = auth_to_cell(context, config, linkedcell, NULL, NULL);
1684 * Local hack - if the person has a file in their home
1685 * directory called ".xlog", read that for a list of
1686 * extra cells to authenticate to
1689 if ((pwd = getpwuid(getuid())) != NULL) {
1692 char fcell[100], xlog_path[512];
1694 strlcpy(xlog_path, pwd->pw_dir, sizeof(xlog_path));
1695 strlcat(xlog_path, "/.xlog", sizeof(xlog_path));
1697 if ((stat(xlog_path, &sbuf) == 0) &&
1698 ((f = fopen(xlog_path, "r")) != NULL)) {
1700 afs_dprintf("Reading %s for cells to authenticate to.\n",
1703 while (fgets(fcell, 100, f) != NULL) {
1706 fcell[strlen(fcell) - 1] = '\0';
1708 afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
1710 auth_status = auth_to_cell(context, config, fcell, NULL, NULL);
1711 if (status == AKLOG_SUCCESS)
1712 status = auth_status;
1714 status = AKLOG_SOMETHINGSWRONG;
1720 /* Log to all cells in the cells list first */
1721 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1722 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1723 status = auth_to_cell(context, config, cellinfo.cell,
1724 cellinfo.realm, &linkedcell);
1728 if (linked && linkedcell != NULL) {
1729 afs_dprintf("Linked cell: %s\n", linkedcell);
1730 status = auth_to_cell(context, config, linkedcell,
1731 cellinfo.realm, NULL);
1735 if (linkedcell != NULL) {
1742 /* Then, log to all paths in the paths list */
1743 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1744 status = auth_to_path(context, config, cur_node->data);
1750 * If only one thing was logged to, we'll return the status
1751 * of the single call. Otherwise, we'll return a generic
1752 * something failed status.
1754 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1755 status = AKLOG_SOMETHINGSWRONG;
1758 /* If we are keeping track of zephyr subscriptions, print them. */
1760 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1761 printf("zsub: %s\n", cur_node->data);
1764 /* If we are keeping track of host information, print it. */
1766 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1767 printf("host: %s\n", cur_node->data);
1774 isdir(char *path, unsigned char *val)
1776 struct stat statbuf;
1778 if (lstat(path, &statbuf) < 0)
1781 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1789 static krb5_error_code
1790 get_credv5_akimpersonate(krb5_context context,
1792 krb5_principal service_principal,
1793 krb5_principal client_principal,
1796 int *allowed_enctypes,
1798 krb5_creds** out_creds /* out */ )
1800 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1801 krb5_error_code code;
1803 krb5_kt_cursor cursor[1];
1804 krb5_keytab_entry entry[1];
1806 krb5_creds *creds = 0;
1807 krb5_enctype enctype;
1809 krb5_keyblock session_key[1];
1811 Ticket ticket_reply[1];
1812 EncTicketPart enc_tkt_reply[1];
1813 krb5_address address[30];
1814 krb5_addresses faddr[1];
1815 unsigned int temp_vno[1];
1816 time_t temp_time[2];
1818 krb5_ticket ticket_reply[1];
1819 krb5_enc_tkt_part enc_tkt_reply[1];
1820 krb5_address address[30], *faddr[30];
1824 static int any_enctype[] = {0};
1826 if (!(creds = malloc(sizeof *creds))) {
1830 if (!allowed_enctypes)
1831 allowed_enctypes = any_enctype;
1834 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1835 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1836 memset((char*)creds, 0, sizeof *creds);
1837 memset((char*)entry, 0, sizeof *entry);
1838 memset((char*)session_key, 0, sizeof *session_key);
1839 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1840 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1841 code = krb5_kt_resolve(context, keytab, &kt);
1844 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1846 afs_com_err(progname, code, "while resolving default keytab");
1850 if (service_principal) {
1851 for (i = 0; (enctype = allowed_enctypes[i]) || !i; ++i) {
1852 code = krb5_kt_get_entry(context,
1859 if (allowed_enctypes[i])
1860 deref_keyblock_enctype(session_key) = allowed_enctypes[i];
1865 afs_com_err(progname, code,"while scanning keytab entries");
1869 krb5_keytab_entry new[1];
1871 memset(new, 0, sizeof *new);
1872 if ((code = krb5_kt_start_seq_get(context, kt, cursor))) {
1873 afs_com_err(progname, code, "while starting keytab scan");
1876 while (!(code = krb5_kt_next_entry(context, kt, new, cursor))) {
1878 allowed_enctypes[i] && allowed_enctypes[i]
1879 != deref_entry_enctype(new); ++i)
1881 if ((!i || allowed_enctypes[i]) &&
1882 (best < 0 || best > i)) {
1883 krb5_free_keytab_entry_contents(context, entry);
1885 memset(new, 0, sizeof *new);
1886 } else krb5_free_keytab_entry_contents(context, new);
1888 if ((i = krb5_kt_end_seq_get(context, kt, cursor))) {
1889 afs_com_err(progname, i, "while ending keytab scan");
1894 afs_com_err(progname, code, "while scanning keytab");
1897 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1903 if ((code = krb5_generate_random_keyblock(context,
1904 deref_keyblock_enctype(session_key), session_key))) {
1905 afs_com_err(progname, code, "while making session key");
1908 enc_tkt_reply->flags.initial = 1;
1909 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1910 enc_tkt_reply->cname = client_principal->name;
1911 enc_tkt_reply->crealm = client_principal->realm;
1912 enc_tkt_reply->key = *session_key;
1914 static krb5_data empty_string;
1915 enc_tkt_reply->transited.contents = empty_string;
1917 enc_tkt_reply->authtime = starttime;
1918 enc_tkt_reply->starttime = temp_time;
1919 *enc_tkt_reply->starttime = starttime;
1921 enc_tkt_reply->renew_till = temp_time + 1;
1922 *enc_tkt_reply->renew_till = endtime;
1924 enc_tkt_reply->endtime = endtime;
1926 if ((code = krb5_c_make_random_key(context,
1927 deref_keyblock_enctype(session_key), session_key))) {
1928 afs_com_err(progname, code, "while making session key");
1931 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1932 #define DATACAST (unsigned char *)
1933 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1934 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1935 enc_tkt_reply->session = session_key;
1936 enc_tkt_reply->client = client_principal;
1938 static krb5_data empty_string;
1939 enc_tkt_reply->transited.tr_contents = empty_string;
1941 enc_tkt_reply->times.authtime = starttime;
1942 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1943 enc_tkt_reply->times.endtime = endtime;
1944 #endif /* USING_HEIMDAL */
1945 /* NB: We will discard address for now--ignoring caddr field
1946 in any case. MIT branch does what it always did. */
1948 if (paddress && *paddress) {
1949 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1952 faddr->val = address;
1954 for (i = 0; paddress[i]; ++i) {
1956 address[i].addr_type = KRB5_ADDRESS_INET;
1957 address[i].address.data = (void*)(paddress+i);
1958 address[i].address.length = sizeof(paddress[i]);
1961 address[i].magic = KV5M_ADDRESS;
1962 address[i].addrtype = ADDRTYPE_INET;
1964 address[i].addrtype = AF_INET;
1966 address[i].contents = (void*)(paddress+i);
1967 address[i].length = sizeof(int);
1968 faddr[i] = address+i;
1979 ticket_reply->sname = service_principal->name;
1980 ticket_reply->realm = service_principal->realm;
1982 { /* crypto block */
1983 krb5_crypto crypto = 0;
1984 unsigned char *buf = 0;
1985 size_t buf_size, buf_len;
1988 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1989 enc_tkt_reply, &buf_len, code);
1991 afs_com_err(progname, code, "while encoding ticket");
1995 if(buf_len != buf_size) {
1996 afs_com_err(progname, code,
1997 "%u != %u while encoding ticket (internal ASN.1 encoder error",
1998 (unsigned int)buf_len, (unsigned int)buf_size);
2001 what = "krb5_crypto_init";
2002 code = krb5_crypto_init(context,
2003 &deref_entry_keyblock(entry),
2004 deref_entry_enctype(entry),
2007 what = "krb5_encrypt";
2008 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
2009 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
2012 if (crypto) krb5_crypto_destroy(context, crypto);
2014 afs_com_err(progname, code, "while %s", what);
2017 } /* crypto block */
2018 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
2019 ticket_reply->enc_part.kvno = (void *)temp_vno;
2020 *ticket_reply->enc_part.kvno = entry->vno;
2021 ticket_reply->tkt_vno = 5;
2023 ticket_reply->server = service_principal;
2024 ticket_reply->enc_part2 = enc_tkt_reply;
2025 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
2026 afs_com_err(progname, code, "while making ticket");
2029 ticket_reply->enc_part.kvno = entry->vno;
2032 /* Construct Creds */
2034 if ((code = krb5_copy_principal(context, service_principal,
2036 afs_com_err(progname, code, "while copying service principal");
2039 if ((code = krb5_copy_principal(context, client_principal,
2041 afs_com_err(progname, code, "while copying client principal");
2044 if ((code = krb5_copy_keyblock_contents(context, session_key,
2045 &deref_session_key(creds)))) {
2046 afs_com_err(progname, code, "while copying session key");
2051 creds->times.authtime = enc_tkt_reply->authtime;
2052 creds->times.starttime = *(enc_tkt_reply->starttime);
2053 creds->times.endtime = enc_tkt_reply->endtime;
2054 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
2055 creds->flags.b = enc_tkt_reply->flags;
2057 creds->times = enc_tkt_reply->times;
2058 creds->ticket_flags = enc_tkt_reply->flags;
2060 if (!deref_enc_tkt_addrs(enc_tkt_reply))
2062 else if ((code = krb5_copy_addresses(context,
2063 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
2064 afs_com_err(progname, code, "while copying addresses");
2070 size_t creds_tkt_len;
2071 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
2072 ticket_reply, &creds_tkt_len, code);
2074 afs_com_err(progname, code, "while encoding ticket");
2079 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
2080 afs_com_err(progname, code, "while encoding ticket");
2083 creds->ticket = *temp;
2090 if (deref_enc_data(&ticket_reply->enc_part))
2091 free(deref_enc_data(&ticket_reply->enc_part));
2092 krb5_free_keytab_entry_contents(context, entry);
2093 if (client_principal)
2094 krb5_free_principal(context, client_principal);
2095 if (service_principal)
2096 krb5_free_principal(context, service_principal);
2098 krb5_cc_close(context, cc);
2100 krb5_kt_close(context, kt);
2101 if (creds) krb5_free_creds(context, creds);
2102 krb5_free_keyblock_contents(context, session_key);
2110 static krb5_error_code
2111 get_credv5(krb5_context context, char *name, char *inst, char *realm,
2116 static krb5_principal client_principal = 0;
2118 afs_dprintf("Getting tickets: %s%s%s@%s\n", name,
2119 (inst && inst[0]) ? "/" : "", inst ? inst : "", realm);
2121 memset(&increds, 0, sizeof(increds));
2122 /* ANL - instance may be ptr to a null string. Pass null then */
2123 if ((r = krb5_build_principal(context, &increds.server,
2124 strlen(realm), realm,
2126 (inst && strlen(inst)) ? inst : NULL,
2132 if (!_krb425_ccache) {
2133 r = krb5_cc_default(context, &_krb425_ccache);
2137 if (!client_principal) {
2139 r = krb5_parse_name(context, client, &client_principal);
2141 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2147 increds.client = client_principal;
2148 increds.times.endtime = 0;
2150 /* Ask for DES since that is what V4 understands */
2151 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
2154 int allowed_enctypes[] = {
2155 ENCTYPE_DES_CBC_CRC, 0
2158 r = get_credv5_akimpersonate(context,
2167 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
2174 get_user_realm(krb5_context context, char **realm)
2176 static krb5_principal client_principal = 0;
2177 krb5_error_code r = 0;
2181 if (!_krb425_ccache) {
2182 r = krb5_cc_default(context, &_krb425_ccache);
2186 if (!client_principal) {
2188 r = krb5_parse_name(context, client, &client_principal);
2190 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2196 *realm = extract_realm(context, client_principal);