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 == KRB5_KT_NOTFOUND || \
149 x == KRB5KRB_ERR_GENERIC)
151 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
153 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
154 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
155 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
156 #define realm_data(c, p) krb5_princ_realm(c, p)->data
157 #define realm_len(c, p) krb5_princ_realm(c, p)->length
159 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
161 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
162 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
163 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
164 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
165 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
168 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
171 #if defined(HAVE_ENCODE_KRB5_TICKET)
172 extern krb5_error_code encode_krb5_ticket (const krb5_ticket *rep,
176 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
177 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
181 krb5_encrypt_tkt_part(krb5_context context,
182 const krb5_keyblock *key,
189 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
191 if ((code = krb5_c_encrypt_length(context, key->enctype,
192 data->length, &enclen)))
194 ticket->enc_part.ciphertext.length = enclen;
195 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
199 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
200 0, data, &ticket->enc_part))) {
201 free(ticket->enc_part.ciphertext.data);
202 ticket->enc_part.ciphertext.data = 0;
214 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
216 #define get_cred_keydata(c) c->keyblock.contents
217 #define get_cred_keylen(c) c->keyblock.length
218 #define get_creds_enctype(c) c->keyblock.enctype
220 #elif defined(HAVE_KRB5_CREDS_SESSION)
222 #define get_cred_keydata(c) c->session.keyvalue.data
223 #define get_cred_keylen(c) c->session.keyvalue.length
224 #define get_creds_enctype(c) c->session.keytype
227 #error "Must have either keyblock or session member of krb5_creds"
230 /* MITKerberosShim logs but returns success */
231 #if !defined(HAVE_KRB5_524_CONV_PRINCIPAL) || defined(AFS_DARWIN110_ENV) || (!defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC))
232 #define HAVE_NO_KRB5_524
233 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
234 #define krb5_524_convert_creds krb524_convert_creds_kdc
238 #define deref_keyblock_enctype(kb) \
241 #define deref_entry_keyblock(entry) \
244 #define deref_session_key(creds) \
247 #define deref_enc_tkt_addrs(tkt) \
250 #define deref_enc_length(enc) \
251 ((enc)->cipher.length)
253 #define deref_enc_data(enc) \
256 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
259 #define deref_keyblock_enctype(kb) \
262 #define deref_entry_keyblock(entry) \
265 #define deref_session_key(creds) \
268 #define deref_enc_tkt_addrs(tkt) \
271 #define deref_enc_length(enc) \
272 ((enc)->ciphertext.length)
274 #define deref_enc_data(enc) \
275 ((enc)->ciphertext.data)
279 #define deref_entry_enctype(entry) \
280 deref_keyblock_enctype(&deref_entry_keyblock(entry))
283 * Provide a replacement for strerror if we don't have it
286 #ifndef HAVE_STRERROR
287 extern char *sys_errlist[];
288 #define strerror(x) sys_errlist[x]
289 #endif /* HAVE_STRERROR */
291 static char *progname = NULL; /* Name of this program */
292 static int dflag = FALSE; /* Give debugging information */
293 static int noauth = FALSE; /* If true, don't try to get tokens */
294 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
295 static int hosts = FALSE; /* Are we keeping track of hosts? */
296 static int noprdb = FALSE; /* Skip resolving name to id? */
297 static int linked = FALSE; /* try for both AFS nodes */
298 static int afssetpag = FALSE; /* setpag for AFS */
299 static int force = FALSE; /* Bash identical tokens? */
300 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
301 static char *keytab = NULL; /* keytab for akimpersonate */
302 static char *client = NULL; /* client principal for akimpersonate */
303 static linked_list zsublist; /* List of zephyr subscriptions */
304 static linked_list hostlist; /* List of host addresses */
305 static linked_list authedcells; /* List of cells already logged to */
307 /* A com_error bodge. The idea here is that this routine lets us lookup
308 * things in the system com_err, if the AFS one just tells us the error
313 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
320 const char *str = afs_error_message(code);
321 if (strncmp(str, "unknown", strlen("unknown")) == 0) {
322 #ifdef HAVE_KRB5_SVC_GET_MSG
323 krb5_svc_get_msg(code,&str);
324 #elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
325 krb5_context context;
326 if (krb5_init_context(&context) == 0) {
327 str = krb5_get_error_message(context, code);
328 krb5_free_context(context);
331 ; /* IRIX apparently has neither: use the string we have */
336 #ifdef HAVE_KRB5_SVC_GET_MSG
337 krb5_free_string(str);
341 vfprintf(stderr, fmt, ap);
348 afs_dprintf(char *fmt, ...) {
358 copy_cellinfo(cellinfo_t *cellinfo)
360 cellinfo_t *new_cellinfo;
362 if ((new_cellinfo = malloc(sizeof(cellinfo_t))))
363 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
365 return ((char *)new_cellinfo);
370 get_cellconfig(const char *config, char *cell,
371 struct afsconf_cell *cellconfig, char **local_cell)
373 int status = AKLOG_SUCCESS;
374 struct afsconf_dir *configdir;
376 memset(cellconfig, 0, sizeof(*cellconfig));
378 *local_cell = malloc(MAXCELLCHARS);
379 if (*local_cell == NULL) {
380 fprintf(stderr, "%s: can't allocate memory for local cell name\n",
385 if (!(configdir = afsconf_Open(config))) {
387 "%s: can't get afs configuration (afsconf_Open(%s))\n",
392 if (cell != NULL && cell[0] == '\0') {
393 /* Use the local cell */
397 /* XXX - This function modifies 'cell' by passing it through lcstring */
398 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
400 fprintf(stderr, "%s: Can't get information about cell %s.\n",
403 fprintf(stderr, "%s: Can't get information about the local cell.\n",
407 } else if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
408 fprintf(stderr, "%s: can't determine local cell.\n", progname);
412 afsconf_Close(configdir);
418 extract_realm(krb5_context context, krb5_principal princ) {
422 len = realm_len(context, princ);
423 if (len > REALM_SZ-1)
426 realm = malloc(sizeof(char) * (len+1));
430 strncpy(realm, realm_data(context, princ), len);
437 get_realm_from_cred(krb5_context context, krb5_creds *v5cred, char **realm) {
438 #if !defined(HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
439 krb5_error_code code;
444 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
448 *realm = extract_realm(context, ticket->server);
452 krb5_free_ticket(context, ticket);
462 * Get a Kerberos service ticket to use as the base of an rxkad token for
466 * An initialized Kerberos v5 context
468 * The realm to look in for the service principal. If NULL, then the
469 * realm is determined from the cell name or the user's credentials
470 * (see below for the heuristics used)
472 * The cell information for the cell to obtain a ticket for
474 * A Kerberos credentials structure containing the ticket acquired
475 * for the cell. This is a dynamically allocated structure, which
476 * should be freed by using the appropriate Kerberos API function.
477 * @param[out] realmUsed
478 * The realm in which the cell's service principal was located. If
479 * unset, then the principal was located in the same realm as the
480 * current user. This is a malloc'd string which should be freed
484 * 0 on success, an error value upon failure
487 * This code tries principals in the following, much debated,
490 * If the realm is specified on the command line we do
491 * - afs/cell@COMMAND-LINE-REALM
492 * - afs@COMMAND-LINE-REALM
495 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
496 * - afs/cell@krb5_get_host_realm(db-server)
497 * Then, if krb5_get_host_realm(db-server) is non-empty
498 * - afs@ krb5_get_host_realm(db-server)
500 * - afs/cell@ upper-case-domain-of-db-server
501 * - afs@ upper-case-domain-of-db-server
503 * In all cases, the 'afs@' variant is only tried where the
504 * cell and the realm match case-insensitively.
508 rxkad_get_ticket(krb5_context context, char *realm,
509 struct afsconf_cell *cell,
510 krb5_creds **v5cred, char **realmUsed) {
511 char *realm_of_cell = NULL;
512 char *realm_of_user = NULL;
513 char *realm_from_princ = NULL;
519 if ((status = get_user_realm(context, &realm_of_user))) {
520 fprintf(stderr, "%s: Couldn't determine realm of user:", progname);
521 afs_com_err(progname, status, " while getting realm");
522 status = AKLOG_KERBEROS;
529 /* Cell on command line - use that one */
530 if (realm && realm[0]) {
531 realm_of_cell = realm;
532 status = AKLOG_TRYAGAIN;
533 afs_dprintf("We were told to authenticate to realm %s.\n", realm);
535 /* Initially, try using afs/cell@USERREALM */
536 afs_dprintf("Trying to authenticate to user's realm %s.\n",
538 realm_of_cell = realm_of_user;
539 status = get_credv5(context, AFSKEY, cell->name, realm_of_cell,
542 /* If that failed, try to determine the realm from the name of
543 * one of the DB servers */
544 if (TRYAGAIN(status)) {
545 realm_of_cell = afs_realm_of_cell(context, cell, FALSE);
546 if (!realm_of_cell) {
547 fprintf(stderr, "%s: Couldn't figure out realm for cell "
548 "%s.\n", progname, cell->name);
552 if (realm_of_cell[0])
553 afs_dprintf("We've deduced that we need to authenticate"
554 " to realm %s.\n", realm_of_cell);
556 afs_dprintf("We've deduced that we need to authenticate "
557 "using referrals.\n");
561 if (TRYAGAIN(status)) {
562 /* If we've got the full-princ-first option, or we're in a
563 * different realm from the cell - use the cell name as the
565 if (AFS_TRY_FULL_PRINC ||
566 strcasecmp(cell->name, realm_of_cell)!=0) {
567 status = get_credv5(context, AFSKEY, cell->name,
568 realm_of_cell, v5cred);
570 /* If we failed & we've got an empty realm, then try
571 * calling afs_realm_for_cell again. */
572 if (TRYAGAIN(status) && !realm_of_cell[0]) {
573 /* This time, get the realm by taking the domain
574 * component of the db server and make it upper case */
575 realm_of_cell = afs_realm_of_cell(context, cell, TRUE);
576 if (!realm_of_cell) {
578 "%s: Couldn't figure out realm for cell %s.\n",
579 progname, cell->name);
582 afs_dprintf("We've deduced that we need to authenticate"
583 " to realm %s.\n", realm_of_cell);
584 status = get_credv5(context, AFSKEY, cell->name,
585 realm_of_cell, v5cred);
589 /* If the realm and cell name match, then try without an
590 * instance, but only if realm is non-empty */
592 if (TRYAGAIN(status) &&
593 strcasecmp(cell->name, realm_of_cell) == 0) {
594 status = get_credv5(context, AFSKEY, NULL, realm_of_cell,
596 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
597 status = get_credv5(context, AFSKEY, cell->name,
598 realm_of_cell, v5cred);
603 /* Try to find a service principal for this cell.
604 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
605 * the first attempt, so we try twice to be sure */
607 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
614 afs_dprintf("Kerberos error code returned by get_cred : %d\n", status);
615 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
616 progname, cell->name);
617 afs_com_err(progname, status, "while getting AFS tickets");
618 #ifdef KRB5_CC_NOT_KTYPE
619 if (status == KRB5_CC_NOT_KTYPE) {
620 fprintf(stderr, "allow_weak_crypto may be required in the Kerberos configuration\n");
623 status = AKLOG_KERBEROS;
627 /* If we've got a valid ticket, and we still don't know the realm name
628 * try to figure it out from the contents of the ticket
630 if (strcmp(realm_of_cell, "") == 0) {
631 status = get_realm_from_cred(context, *v5cred, &realm_from_princ);
634 "%s: Couldn't decode ticket to determine realm for "
636 progname, cell->name);
638 if (realm_from_princ)
639 realm_of_cell = realm_from_princ;
643 /* If the realm of the user and cell differ, then we need to use the
644 * realm when we later construct the user's principal */
645 if (realm_of_cell != NULL && strcmp(realm_of_user, realm_of_cell) != 0)
646 *realmUsed = realm_of_user;
649 if (realm_from_princ)
650 free(realm_from_princ);
651 if (realm_of_user && *realmUsed == NULL)
658 * Build an rxkad token from a Kerberos ticket, using only local tools (that
659 * is, without using a 524 conversion service)
662 * An initialised Kerberos 5 context
664 * A Kerberos credentials structure containing a suitable service ticket
665 * @param[out] tokenPtr
666 * An AFS token structure containing an rxkad token. This is a malloc'd
667 * structure which should be freed by the caller.
668 * @param[out[ userPtr
669 * A string containing the principal of the user to whom the token was
670 * issued. This is a malloc'd block which should be freed by the caller,
674 * 0 on success, an error value upon failure
677 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
678 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
679 char username[BUFSIZ]="";
680 struct ktc_token token;
682 #ifdef HAVE_NO_KRB5_524
686 char k4name[ANAME_SZ];
687 char k4inst[INST_SZ];
688 char k4realm[REALM_SZ];
690 void *inkey = get_cred_keydata(v5cred);
691 size_t inkey_sz = get_cred_keylen(v5cred);
693 afs_dprintf("Using Kerberos V5 ticket natively\n");
698 #ifndef HAVE_NO_KRB5_524
699 status = krb5_524_conv_principal (context, v5cred->client,
705 afs_com_err(progname, status,
706 "while converting principal to Kerberos V4 format");
708 strcpy (username, k4name);
710 strcat (username, ".");
711 strcat (username, k4inst);
715 len = min(get_princ_len(context, v5cred->client, 0),
716 second_comp(context, v5cred->client) ?
717 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
718 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
719 username[len] = '\0';
721 if (second_comp(context, v5cred->client)) {
722 strcat(username, ".");
723 p = username + strlen(username);
724 len = min(get_princ_len(context, v5cred->client, 1),
725 MAXKTCNAMELEN - strlen(username) - 1);
726 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
731 memset(&token, 0, sizeof(struct ktc_token));
733 token.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
734 token.startTime = v5cred->times.starttime;;
735 token.endTime = v5cred->times.endtime;
736 if (tkt_DeriveDesKey(get_creds_enctype(v5cred), inkey, inkey_sz,
737 &token.sessionKey) != 0) {
740 token.ticketLen = v5cred->ticket.length;
741 memcpy(token.ticket, v5cred->ticket.data, token.ticketLen);
743 status = token_importRxkadViceId(tokenPtr, &token, 0);
748 if (username[0] != '\0')
749 *userPtr = strdup(username);
755 * Convert a Keberos ticket to an rxkad token, using information obtained
756 * from an external Kerberos 5->4 conversion service. If the code is built
757 * with HAVE_NO_KRB5_524 then this is a stub function which will always
758 * return success without a token.
761 * An initialised Kerberos 5 context
763 * A Kerberos credentials structure containing a suitable service ticket
764 * @param[out] tokenPtr
765 * An AFS token structure containing an rxkad token. This is a malloc'd
766 * structure which should be freed by the caller.
767 * @param[out[ userPtr
768 * A string containing the principal of the user to whom the token was
769 * issued. This is a malloc'd block which should be freed by the caller,
773 * 0 on success, an error value upon failure
776 #ifdef HAVE_NO_KRB5_524
778 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
779 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
787 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
788 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
790 char username[BUFSIZ];
791 struct ktc_token token;
797 afs_dprintf("Using Kerberos 524 translator service\n");
799 status = krb5_524_convert_creds(context, v5cred, &cred);
802 afs_com_err(progname, status, "while converting tickets "
803 "to Kerberos V4 format");
804 return AKLOG_KERBEROS;
807 strcpy (username, cred.pname);
809 strcat (username, ".");
810 strcat (username, cred.pinst);
813 memset(&token, 0, sizeof(struct ktc_token));
815 token.kvno = cred.kvno;
816 token.startTime = cred.issue_date;
818 * It seems silly to go through a bunch of contortions to
819 * extract the expiration time, when the v5 credentials already
820 * has the exact time! Let's use that instead.
822 * Note that this isn't a security hole, as the expiration time
823 * is also contained in the encrypted token
825 token.endTime = v5cred->times.endtime;
826 memcpy(&token.sessionKey, cred.session, 8);
827 token.ticketLen = cred.ticket_st.length;
828 memcpy(token.ticket, cred.ticket_st.dat, token.ticketLen);
830 status = token_importRxkadViceId(tokenPtr, &token, 0);
835 *userPtr = strdup(username);
842 * This function gets an rxkad token for a given cell.
845 * An initialized Kerberos v5 context
847 * The cell information for the cell which we're obtaining a token for
849 * The realm to look in for the service principal. If NULL, then the
850 * realm is determined from the cell name or the user's credentials
851 * (see the documentation for rxkad_get_ticket)
853 * The rxkad token produced. This is a malloc'd structure which should
854 * be freed by the caller.
855 * @parma[out] authuser
856 * A string containing the principal of the user to whom the token was
857 * issued. This is a malloc'd block which should be freed by the caller,
859 * @param[out] foreign
860 * Whether the user is considered as 'foreign' to the realm of the cell.
863 * 0 on success, an error value upon failuer
866 rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
867 struct ktc_tokenUnion **token, char **authuser, int *foreign) {
868 krb5_creds *v5cred = NULL;
869 char *realmUsed = NULL;
870 char *username = NULL;
877 status = rxkad_get_ticket(context, realm, cell, &v5cred, &realmUsed);
882 status = rxkad_get_converted_token(context, v5cred, token, &username);
884 status = rxkad_build_native_token(context, v5cred, token, &username);
889 /* We now have the username, plus the realm name, so stitch them together
890 * to give us the name that the ptserver will know the user by */
891 if (realmUsed == NULL || username == NULL) {
892 *authuser = username;
896 if (asprintf(authuser, "%s@%s", username, realmUsed) < 0) {
897 fprintf(stderr, "%s: Out of memory building PTS name\n", progname);
911 krb5_free_creds(context, v5cred);
917 * Log to a cell. If the cell has already been logged to, return without
918 * doing anything. Otherwise, log to it and mark that it has been logged
922 auth_to_cell(krb5_context context, const char *config,
923 char *cell, char *realm, char **linkedcell)
925 int status = AKLOG_SUCCESS;
927 char *username = NULL; /* To hold client username structure */
928 afs_int32 viceId; /* AFS uid of user */
930 char *local_cell = NULL;
931 struct ktc_tokenUnion *rxkadToken = NULL;
932 struct ktc_setTokenData *token;
933 struct ktc_setTokenData *btoken = NULL;
934 struct afsconf_cell cellconf;
936 /* NULL or empty cell returns information on local cell */
937 if ((status = get_cellconfig(config, cell, &cellconf, &local_cell)))
940 if (linkedcell != NULL) {
941 if (cellconf.linkedCell != NULL) {
942 *linkedcell = strdup(cellconf.linkedCell);
943 if (*linkedcell == NULL) {
952 if (ll_string(&authedcells, ll_s_check, cellconf.name)) {
953 afs_dprintf("Already authenticated to %s (or tried to)\n", cellconf.name);
954 status = AKLOG_SUCCESS;
959 * Record that we have attempted to log to this cell. We do this
960 * before we try rather than after so that we will not try
961 * and fail repeatedly for one cell.
963 ll_string(&authedcells, ll_s_add, cellconf.name);
966 * Record this cell in the list of zephyr subscriptions. We may
967 * want zephyr subscriptions even if authentication fails.
968 * If this is done after we attempt to get tokens, aklog -zsubs
969 * can return something different depending on whether or not we
970 * are in -noauth mode.
972 if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
974 "%s: failure adding cell %s to zephyr subscriptions list.\n",
975 progname, cellconf.name);
978 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
980 "%s: failure adding cell %s to zephyr subscriptions list.\n",
981 progname, local_cell);
986 afs_dprintf("Authenticating to cell %s (server %s).\n", cellconf.name,
987 cellconf.hostName[0]);
989 token = token_buildTokenJar(cellconf.name);
995 status = rxkad_get_token(context, &cellconf, realm, &rxkadToken,
996 &username, &isForeign);
1000 /* We need to keep the token structure around so that we can stick
1001 * the viceId into it (once we know it) */
1002 status = token_addToken(token, rxkadToken);
1004 afs_dprintf("Add Token failed with %d", status);
1009 ktc_GetTokenEx(cellconf.name, &btoken) == 0 &&
1010 token_SetsEquivalent(token, btoken)) {
1012 token_FreeSet(&btoken);
1013 afs_dprintf("Identical tokens already exist; skipping.\n");
1014 status = AKLOG_SUCCESS;
1019 token_FreeSet(&btoken);
1025 if (username == NULL) {
1026 afs_dprintf("Not resolving name to id\n");
1029 afs_dprintf("Not resolving name %s to id (-noprdb set)\n", username);
1032 afs_dprintf("About to resolve name %s to id in cell %s.\n", username,
1035 if (!pr_Initialize (0, AFSDIR_CLIENT_ETC_DIRPATH, cellconf.name))
1036 status = pr_SNameToId (username, &viceId);
1039 afs_dprintf("Error %d\n", status);
1041 afs_dprintf("Id %d\n", (int) viceId);
1045 * This code is taken from cklog -- it lets people
1046 * automatically register with the ptserver in foreign cells
1049 #ifdef ALLOW_REGISTER
1050 if ((status == 0) && (viceId == ANONYMOUSID) && isForeign) {
1051 afs_dprintf("doing first-time registration of %s at %s\n",
1052 username, cellconf.name);
1055 status = ktc_SetTokenEx(token);
1057 afs_com_err(progname, status,
1058 "while obtaining tokens for cell %s",
1060 status = AKLOG_TOKEN;
1064 * In case you're wondering, we don't need to change the
1065 * filename here because we're still connecting to the
1066 * same cell -- we're just using a different authenticat ion
1070 if ((status = pr_Initialize(1L, AFSDIR_CLIENT_ETC_DIRPATH,
1072 printf("Error %d\n", status);
1075 if ((status = pr_CreateUser(username, &viceId))) {
1076 fprintf(stderr, "%s: %s so unable to create remote PTS "
1077 "user %s in cell %s (status: %d).\n", progname,
1078 afs_error_message(status), username, cellconf.name,
1080 viceId = ANONYMOUSID;
1082 printf("created cross-cell entry for %s (Id %d) at %s\n",
1083 username, viceId, cellconf.name);
1086 #endif /* ALLOW_REGISTER */
1088 if ((status == 0) && (viceId != ANONYMOUSID)) {
1089 status = token_setRxkadViceId(rxkadToken, viceId);
1091 fprintf(stderr, "Error %d setting rxkad ViceId\n", status);
1092 status = AKLOG_SUCCESS;
1094 token_replaceToken(token, rxkadToken);
1100 afs_dprintf("Setting tokens. %s @ %s\n",
1101 username, cellconf.name);
1103 afs_dprintf("Setting tokens for cell %s\n", cellconf.name);
1106 #ifndef AFS_AIX51_ENV
1107 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
1108 * this routine, it will not add the token. It is not clear what
1109 * is going on here! So we will do the following operation.
1110 * On AIX 5, it causes the parent program to die, so we won't.
1111 * We don't care about the return value, but need to collect it
1112 * to avoid compiler warnings.
1114 if (write(2,"",0) < 0) {
1115 /* dummy write, don't care */
1118 token_setPag(token, afssetpag);
1119 status = ktc_SetTokenEx(token);
1121 afs_com_err(progname, status, "while setting tokens for cell %s",
1123 status = AKLOG_TOKEN;
1127 afs_dprintf("Noauth mode; not authenticating.\n");
1131 token_freeToken(&rxkadToken);
1143 get_afs_mountpoint(char *file, char *mountpoint, int size)
1146 char V ='V'; /* AFS has problem on Sun with pioctl */
1148 char our_file[MAXPATHLEN + 1];
1150 char *last_component;
1151 struct ViceIoctl vio;
1152 char cellname[BUFSIZ];
1154 strlcpy(our_file, file, sizeof(our_file));
1156 if ((last_component = strrchr(our_file, DIR))) {
1157 *last_component++ = 0;
1158 parent_dir = our_file;
1161 last_component = our_file;
1165 memset(cellname, 0, sizeof(cellname));
1167 vio.in = last_component;
1168 vio.in_size = strlen(last_component)+1;
1169 vio.out_size = size;
1170 vio.out = mountpoint;
1172 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
1173 if (strchr(mountpoint, VOLMARKER) == NULL) {
1175 vio.in_size = strlen(file) + 1;
1176 vio.out_size = sizeof(cellname);
1179 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
1180 strlcat(cellname, VOLMARKERSTRING, sizeof(cellname));
1181 strlcat(cellname, mountpoint + 1, sizeof(cellname));
1182 memset(mountpoint + 1, 0, size - 1);
1183 strcpy(mountpoint + 1, cellname);
1193 * This routine each time it is called returns the next directory
1194 * down a pathname. It resolves all symbolic links. The first time
1195 * it is called, it should be called with the name of the path
1196 * to be descended. After that, it should be called with the arguemnt
1200 next_path(char *origpath)
1202 static char path[MAXPATHLEN + 1];
1203 static char pathtocheck[MAXPATHLEN + 1];
1205 ssize_t link; /* Return value from readlink */
1206 char linkbuf[MAXPATHLEN + 1];
1207 char tmpbuf[MAXPATHLEN + 1];
1209 static char *last_comp; /* last component of directory name */
1210 static char *elast_comp; /* End of last component */
1214 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1216 /* If we are given something for origpath, we are initializing only. */
1218 memset(path, 0, sizeof(path));
1219 memset(pathtocheck, 0, sizeof(pathtocheck));
1220 strlcpy(path, origpath, sizeof(path));
1226 /* We were not given origpath; find then next path to check */
1228 /* If we've gotten all the way through already, return NULL */
1229 if (last_comp == NULL)
1233 while (*last_comp == DIR)
1234 strncat(pathtocheck, last_comp++, 1);
1235 len = (elast_comp = strchr(last_comp, DIR))
1236 ? elast_comp - last_comp : strlen(last_comp);
1237 strncat(pathtocheck, last_comp, len);
1238 memset(linkbuf, 0, sizeof(linkbuf));
1239 link = readlink(pathtocheck, linkbuf, sizeof(linkbuf)-1);
1242 linkbuf[link] = '\0'; /* NUL terminate string */
1244 if (++symlinkcount > MAXSYMLINKS) {
1245 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1246 exit(AKLOG_BADPATH);
1249 memset(tmpbuf, 0, sizeof(tmpbuf));
1251 strlcpy(tmpbuf, elast_comp, sizeof(tmpbuf));
1252 if (linkbuf[0] == DIR) {
1254 * If this is a symbolic link to an absolute path,
1255 * replace what we have by the absolute path.
1257 memset(path, 0, strlen(path));
1258 memcpy(path, linkbuf, sizeof(linkbuf));
1259 strcat(path, tmpbuf);
1262 memset(pathtocheck, 0, sizeof(pathtocheck));
1266 * If this is a symbolic link to a relative path,
1267 * replace only the last component with the link name.
1269 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1270 strcat(path, tmpbuf);
1272 if ((t = strrchr(pathtocheck, DIR))) {
1274 memset(t, 0, strlen(t));
1277 memset(pathtocheck, 0, sizeof(pathtocheck));
1281 last_comp = elast_comp;
1285 return(pathtocheck);
1289 add_hosts(char *file)
1292 char V = 'V'; /* AFS has problem on SunOS */
1294 struct ViceIoctl vio;
1295 char outbuf[BUFSIZ];
1301 memset(outbuf, 0, sizeof(outbuf));
1303 vio.out_size = sizeof(outbuf);
1307 afs_dprintf("Getting list of hosts for %s\n", file);
1309 /* Don't worry about errors. */
1310 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1311 phosts = (long *) outbuf;
1314 * Lists hosts that we care about. If ALLHOSTS is defined,
1315 * then all hosts that you ever may possible go through are
1316 * included in this list. If not, then only hosts that are
1317 * the only ones appear. That is, if a volume you must use
1318 * is replaced on only one server, that server is included.
1319 * If it is replicated on many servers, then none are included.
1320 * This is not perfect, but the result is that people don't
1321 * get subscribed to a lot of instances of FILSRV that they
1322 * probably won't need which reduces the instances of
1323 * people getting messages that don't apply to them.
1326 if (phosts[1] != '\0')
1329 for (i = 0; phosts[i]; i++) {
1331 in.s_addr = phosts[i];
1332 afs_dprintf("Got host %s\n", inet_ntoa(in));
1333 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1335 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1336 afs_dprintf("Got host %s\n", hp->h_name);
1337 ll_string(&zsublist, ll_s_add, hp->h_name);
1344 * This routine descends through a path to a directory, logging to
1345 * every cell it encounters along the way.
1348 auth_to_path(krb5_context context, const char *config, char *path)
1350 int status = AKLOG_SUCCESS;
1351 int auth_status = AKLOG_SUCCESS;
1354 char pathtocheck[MAXPATHLEN + 1];
1355 char mountpoint[MAXPATHLEN + 1];
1364 strlcpy(pathtocheck, path, sizeof(pathtocheck));
1366 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1367 fprintf(stderr, "Unable to find current working directory:\n");
1368 fprintf(stderr, "%s\n", pathtocheck);
1369 fprintf(stderr, "Try an absolute pathname.\n");
1370 exit(AKLOG_BADPATH);
1373 strlcat(pathtocheck, DIRSTRING, sizeof(pathtocheck));
1374 strlcat(pathtocheck, path, sizeof(pathtocheck));
1377 next_path(pathtocheck);
1379 /* Go on to the next level down the path */
1380 while ((nextpath = next_path(NULL))) {
1381 strlcpy(pathtocheck, nextpath, sizeof(pathtocheck));
1382 afs_dprintf("Checking directory %s\n", pathtocheck);
1384 * If this is an afs mountpoint, determine what cell from
1385 * the mountpoint name which is of the form
1386 * #cellname:volumename or %cellname:volumename.
1388 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1389 /* skip over the '#' or '%' */
1390 cell = mountpoint + 1;
1391 /* Add this (cell:volumename) to the list of zsubs */
1393 ll_string(&zsublist, ll_s_add, cell);
1395 add_hosts(pathtocheck);
1396 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1398 auth_status = auth_to_cell(context, config, cell, NULL, NULL);
1400 if (status == AKLOG_SUCCESS)
1401 status = auth_status;
1402 else if (status != auth_status)
1403 status = AKLOG_SOMETHINGSWRONG;
1408 if (isdir(pathtocheck, &isdirectory) < 0) {
1410 * If we've logged and still can't stat, there's
1413 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1414 pathtocheck, strerror(errno));
1415 return(AKLOG_BADPATH);
1417 else if (! isdirectory) {
1418 /* Allow only directories */
1419 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1421 return(AKLOG_BADPATH);
1431 /* Print usage message and exit */
1435 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1436 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1437 "[[-p | -path] pathname]\n",
1438 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1439 " [-linked] [-insecure_des]"
1440 #ifndef HAVE_NO_KRB5_524
1444 fprintf(stderr, " -d gives debugging information.\n");
1445 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1446 fprintf(stderr, " pathname is the name of a directory to which ");
1447 fprintf(stderr, "you wish to authenticate.\n");
1448 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1449 fprintf(stderr, " -hosts gives host address information.\n");
1450 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1451 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1452 fprintf(stderr, " -force means replace identical tickets. \n");
1453 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1454 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1455 #ifndef HAVE_NO_KRB5_524
1456 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1458 fprintf(stderr, " -insecure_des enables insecure single-DES for krb5.\n");
1459 fprintf(stderr, " No commandline arguments means ");
1460 fprintf(stderr, "authenticate to the local cell.\n");
1461 fprintf(stderr, "\n");
1466 main(int argc, char *argv[])
1468 krb5_context context;
1469 int status = AKLOG_SUCCESS;
1471 int somethingswrong = FALSE;
1472 int insecure_des = 0;
1474 cellinfo_t cellinfo;
1476 extern char *progname; /* Name of this program */
1478 int cmode = FALSE; /* Cellname mode */
1479 int pmode = FALSE; /* Path name mode */
1481 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1482 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1483 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1485 linked_list cells; /* List of cells to log to */
1486 linked_list paths; /* List of paths to log to */
1489 const char *config = AFSDIR_CLIENT_ETC_DIRPATH;
1491 memset(&cellinfo, 0, sizeof(cellinfo));
1493 memset(realm, 0, sizeof(realm));
1494 memset(cell, 0, sizeof(cell));
1495 memset(path, 0, sizeof(path));
1503 /* Store the program name here for error messages */
1504 if ((progname = strrchr(argv[0], DIR)))
1509 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1511 char *filepath = NULL, *newpath = NULL;
1512 #ifndef AFS_DARWIN_ENV
1513 char *defaultpath = "/etc/krb5.conf:/etc/krb5/krb5.conf";
1515 char *defaultpath = "~/Library/Preferences/edu.mit.Kerberos:/Library/Preferences/edu.mit.Kerberos";
1517 filepath = getenv("KRB5_CONFIG");
1519 /* only fiddle with KRB5_CONFIG if krb5-weak.conf actually exists */
1520 if (asprintf(&newpath, "%s/krb5-weak.conf",
1521 AFSDIR_CLIENT_ETC_DIRPATH) < 0)
1523 if (newpath != NULL && access(newpath, R_OK) == 0) {
1526 if (asprintf(&newpath, "%s:%s/krb5-weak.conf",
1527 filepath ? filepath : defaultpath,
1528 AFSDIR_CLIENT_ETC_DIRPATH) < 0)
1531 setenv("KRB5_CONFIG", newpath, 1);
1534 krb5_init_context(&context);
1536 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1540 setenv("KRB5_CONFIG", filepath, 1);
1542 unsetenv("KRB5_CONFIG");
1545 initialize_KTC_error_table ();
1546 initialize_U_error_table();
1547 initialize_RXK_error_table();
1548 initialize_ACFG_error_table();
1549 initialize_PT_error_table();
1550 afs_set_com_err_hook(redirect_errors);
1552 /* Initialize list of cells to which we have authenticated */
1553 ll_init(&authedcells);
1555 /* Parse commandline arguments and make list of what to do. */
1556 for (i = 1; i < argc; i++) {
1557 if (strcmp(argv[i], "-d") == 0)
1559 else if (strcmp(argv[i], "-noauth") == 0)
1561 else if (strcmp(argv[i], "-zsubs") == 0)
1563 else if (strcmp(argv[i], "-hosts") == 0)
1565 else if (strcmp(argv[i], "-noprdb") == 0)
1567 else if (strcmp(argv[i], "-linked") == 0)
1569 else if (strcmp(argv[i], "-force") == 0)
1571 #ifndef HAVE_NO_KRB5_524
1572 else if (strcmp(argv[i], "-524") == 0)
1575 else if (strcmp(argv[i], "-setpag") == 0)
1577 else if (((strcmp(argv[i], "-cell") == 0) ||
1578 (strcmp(argv[i], "-c") == 0)) && !pmode)
1581 strlcpy(cell, argv[i], sizeof(cell));
1585 else if ((strcmp(argv[i], "-keytab") == 0))
1591 else if ((strcmp(argv[i], "-principal") == 0))
1597 else if (((strcmp(argv[i], "-path") == 0) ||
1598 (strcmp(argv[i], "-p") == 0)) && !cmode)
1601 strlcpy(path, argv[i], sizeof(path));
1605 else if (strcmp(argv[i], "-config") == 0)
1611 else if (strcmp(argv[i], "-insecure_des") == 0)
1613 else if (argv[i][0] == '-')
1615 else if (!pmode && !cmode) {
1616 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1617 (strcmp(argv[i], "..") == 0)) {
1619 strlcpy(path, argv[i], sizeof(path));
1623 strlcpy(cell, argv[i], sizeof(cell));
1630 * Enable DES enctypes if requested. This is not required when rxkad-k5
1631 * is used, but some sites may not have updated.
1632 * krb5_allow_weak_crypto is MIT Kerberos 1.8. krb5_enctype_enable is
1636 #if defined(HAVE_KRB5_ENCTYPE_ENABLE)
1637 i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
1639 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1640 #elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
1641 krb5_allow_weak_crypto(context, 1);
1644 "%s: -insecure_des is not supported by this libkrb5\n", progname);
1650 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1653 strlcpy(realm, argv[i], sizeof(realm));
1657 /* Add this cell to list of cells */
1658 strcpy(cellinfo.cell, cell);
1659 strcpy(cellinfo.realm, realm);
1660 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1662 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1663 ll_add_data(cur_node, new_cellinfo);
1666 "%s: failure copying cellinfo.\n", progname);
1671 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1675 memset(&cellinfo, 0, sizeof(cellinfo));
1677 memset(cell, 0, sizeof(cell));
1678 memset(realm, 0, sizeof(realm));
1681 /* Add this path to list of paths */
1682 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1684 if ((new_path = strdup(path)))
1685 ll_add_data(cur_node, new_path);
1687 fprintf(stderr, "%s: failure copying path name.\n",
1693 fprintf(stderr, "%s: failure adding path to paths list.\n",
1698 memset(path, 0, sizeof(path));
1702 /* If nothing was given, log to the local cell. */
1703 if ((cells.nelements + paths.nelements) == 0) {
1706 status = auth_to_cell(context, config, NULL, NULL, &linkedcell);
1708 /* If this cell is linked to a DCE cell, and user requested -linked,
1709 * get tokens for both. This is very useful when the AFS cell is
1710 * linked to a DFS cell and this system does not also have DFS.
1713 if (!status && linked && linkedcell != NULL) {
1714 afs_dprintf("Linked cell: %s\n", linkedcell);
1715 status = auth_to_cell(context, config, linkedcell, NULL, NULL);
1723 * Local hack - if the person has a file in their home
1724 * directory called ".xlog", read that for a list of
1725 * extra cells to authenticate to
1728 if ((pwd = getpwuid(getuid())) != NULL) {
1731 char fcell[100], xlog_path[512];
1733 strlcpy(xlog_path, pwd->pw_dir, sizeof(xlog_path));
1734 strlcat(xlog_path, "/.xlog", sizeof(xlog_path));
1736 if ((stat(xlog_path, &sbuf) == 0) &&
1737 ((f = fopen(xlog_path, "r")) != NULL)) {
1739 afs_dprintf("Reading %s for cells to authenticate to.\n",
1742 while (fgets(fcell, 100, f) != NULL) {
1745 fcell[strlen(fcell) - 1] = '\0';
1747 afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
1749 auth_status = auth_to_cell(context, config, fcell, NULL, NULL);
1750 if (status == AKLOG_SUCCESS)
1751 status = auth_status;
1753 status = AKLOG_SOMETHINGSWRONG;
1759 /* Log to all cells in the cells list first */
1760 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1761 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1762 status = auth_to_cell(context, config, cellinfo.cell,
1763 cellinfo.realm, &linkedcell);
1767 if (linked && linkedcell != NULL) {
1768 afs_dprintf("Linked cell: %s\n", linkedcell);
1769 status = auth_to_cell(context, config, linkedcell,
1770 cellinfo.realm, NULL);
1774 if (linkedcell != NULL) {
1781 /* Then, log to all paths in the paths list */
1782 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1783 status = auth_to_path(context, config, cur_node->data);
1789 * If only one thing was logged to, we'll return the status
1790 * of the single call. Otherwise, we'll return a generic
1791 * something failed status.
1793 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1794 status = AKLOG_SOMETHINGSWRONG;
1797 /* If we are keeping track of zephyr subscriptions, print them. */
1799 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1800 printf("zsub: %s\n", cur_node->data);
1803 /* If we are keeping track of host information, print it. */
1805 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1806 printf("host: %s\n", cur_node->data);
1813 isdir(char *path, unsigned char *val)
1815 struct stat statbuf;
1817 if (lstat(path, &statbuf) < 0)
1820 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1828 static krb5_error_code
1829 get_credv5_akimpersonate(krb5_context context,
1831 krb5_principal service_principal,
1832 krb5_principal client_principal,
1836 krb5_creds** out_creds /* out */ )
1838 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1839 krb5_error_code code;
1841 krb5_keytab_entry entry[1];
1843 krb5_creds *creds = 0;
1844 krb5_enctype enctype;
1846 krb5_keyblock session_key[1];
1848 Ticket ticket_reply[1];
1849 EncTicketPart enc_tkt_reply[1];
1850 krb5_address address[30];
1851 krb5_addresses faddr[1];
1852 unsigned int temp_vno[1];
1853 time_t temp_time[2];
1855 krb5_ticket ticket_reply[1];
1856 krb5_enc_tkt_part enc_tkt_reply[1];
1857 krb5_address address[30], *faddr[30];
1862 if (!(creds = malloc(sizeof *creds))) {
1868 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1869 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1870 memset((char*)creds, 0, sizeof *creds);
1871 memset((char*)entry, 0, sizeof *entry);
1872 memset((char*)session_key, 0, sizeof *session_key);
1873 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1874 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1875 code = krb5_kt_resolve(context, keytab, &kt);
1878 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1880 afs_com_err(progname, code, "while resolving default keytab");
1884 code = krb5_kt_get_entry(context,
1891 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1893 afs_com_err(progname, code, "while scanning keytab entries");
1900 if ((code = krb5_generate_random_keyblock(context,
1901 deref_keyblock_enctype(session_key), session_key))) {
1902 afs_com_err(progname, code, "while making session key");
1905 enc_tkt_reply->flags.initial = 1;
1906 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1907 enc_tkt_reply->cname = client_principal->name;
1908 enc_tkt_reply->crealm = client_principal->realm;
1909 enc_tkt_reply->key = *session_key;
1911 static krb5_data empty_string;
1912 enc_tkt_reply->transited.contents = empty_string;
1914 enc_tkt_reply->authtime = starttime;
1915 enc_tkt_reply->starttime = temp_time;
1916 *enc_tkt_reply->starttime = starttime;
1917 enc_tkt_reply->endtime = endtime;
1919 if ((code = krb5_c_make_random_key(context,
1920 deref_keyblock_enctype(session_key), session_key))) {
1921 afs_com_err(progname, code, "while making session key");
1924 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1925 #define DATACAST (unsigned char *)
1926 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1927 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1928 enc_tkt_reply->session = session_key;
1929 enc_tkt_reply->client = client_principal;
1931 static krb5_data empty_string;
1932 enc_tkt_reply->transited.tr_contents = empty_string;
1934 enc_tkt_reply->times.authtime = starttime;
1935 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1936 enc_tkt_reply->times.endtime = endtime;
1937 #endif /* USING_HEIMDAL */
1938 /* NB: We will discard address for now--ignoring caddr field
1939 in any case. MIT branch does what it always did. */
1941 if (paddress && *paddress) {
1942 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1945 faddr->val = address;
1947 for (i = 0; paddress[i]; ++i) {
1949 address[i].addr_type = KRB5_ADDRESS_INET;
1950 address[i].address.data = (void*)(paddress+i);
1951 address[i].address.length = sizeof(paddress[i]);
1954 address[i].magic = KV5M_ADDRESS;
1955 address[i].addrtype = ADDRTYPE_INET;
1957 address[i].addrtype = AF_INET;
1959 address[i].contents = (void*)(paddress+i);
1960 address[i].length = sizeof(int);
1961 faddr[i] = address+i;
1972 ticket_reply->sname = service_principal->name;
1973 ticket_reply->realm = service_principal->realm;
1975 { /* crypto block */
1976 krb5_crypto crypto = 0;
1977 unsigned char *buf = 0;
1978 size_t buf_size, buf_len;
1981 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1982 enc_tkt_reply, &buf_len, code);
1984 afs_com_err(progname, code, "while encoding ticket");
1988 if(buf_len != buf_size) {
1989 afs_com_err(progname, code,
1990 "%u != %u while encoding ticket (internal ASN.1 encoder error",
1991 (unsigned int)buf_len, (unsigned int)buf_size);
1994 what = "krb5_crypto_init";
1995 code = krb5_crypto_init(context,
1996 &deref_entry_keyblock(entry),
1997 deref_entry_enctype(entry),
2000 what = "krb5_encrypt";
2001 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
2002 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
2005 if (crypto) krb5_crypto_destroy(context, crypto);
2007 afs_com_err(progname, code, "while %s", what);
2010 } /* crypto block */
2011 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
2012 ticket_reply->enc_part.kvno = (void *)temp_vno;
2013 *ticket_reply->enc_part.kvno = entry->vno;
2014 ticket_reply->tkt_vno = 5;
2016 ticket_reply->server = service_principal;
2017 ticket_reply->enc_part2 = enc_tkt_reply;
2018 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
2019 afs_com_err(progname, code, "while making ticket");
2022 ticket_reply->enc_part.kvno = entry->vno;
2025 /* Construct Creds */
2027 if ((code = krb5_copy_principal(context, service_principal,
2029 afs_com_err(progname, code, "while copying service principal");
2032 if ((code = krb5_copy_principal(context, client_principal,
2034 afs_com_err(progname, code, "while copying client principal");
2037 if ((code = krb5_copy_keyblock_contents(context, session_key,
2038 &deref_session_key(creds)))) {
2039 afs_com_err(progname, code, "while copying session key");
2044 creds->times.authtime = enc_tkt_reply->authtime;
2045 creds->times.starttime = *(enc_tkt_reply->starttime);
2046 creds->times.endtime = enc_tkt_reply->endtime;
2047 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
2048 creds->flags.b = enc_tkt_reply->flags;
2050 creds->times = enc_tkt_reply->times;
2051 creds->ticket_flags = enc_tkt_reply->flags;
2053 if (!deref_enc_tkt_addrs(enc_tkt_reply))
2055 else if ((code = krb5_copy_addresses(context,
2056 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
2057 afs_com_err(progname, code, "while copying addresses");
2063 size_t creds_tkt_len;
2064 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
2065 ticket_reply, &creds_tkt_len, code);
2067 afs_com_err(progname, code, "while encoding ticket");
2072 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
2073 afs_com_err(progname, code, "while encoding ticket");
2076 creds->ticket = *temp;
2083 if (deref_enc_data(&ticket_reply->enc_part))
2084 free(deref_enc_data(&ticket_reply->enc_part));
2085 krb5_free_keytab_entry_contents(context, entry);
2087 krb5_cc_close(context, cc);
2089 krb5_kt_close(context, kt);
2090 if (creds) krb5_free_creds(context, creds);
2091 krb5_free_keyblock_contents(context, session_key);
2099 static krb5_error_code
2100 get_credv5(krb5_context context, char *name, char *inst, char *realm,
2106 afs_dprintf("Getting tickets: %s%s%s@%s\n", name,
2107 (inst && inst[0]) ? "/" : "", inst ? inst : "", realm);
2109 memset(&increds, 0, sizeof(increds));
2110 /* ANL - instance may be ptr to a null string. Pass null then */
2111 if ((r = krb5_build_principal(context, &increds.server,
2112 strlen(realm), realm,
2114 (inst && strlen(inst)) ? inst : NULL,
2120 if (!_krb425_ccache) {
2121 r = krb5_cc_default(context, &_krb425_ccache);
2127 r = krb5_parse_name(context, client, &increds.client);
2129 r = krb5_cc_get_principal(context, _krb425_ccache, &increds.client);
2135 increds.times.endtime = 0;
2137 /* Ask for DES since that is what V4 understands */
2138 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
2142 r = get_credv5_akimpersonate(context,
2150 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
2155 if (increds.server) {
2156 krb5_free_principal(context, increds.server);
2159 if (increds.client) {
2160 krb5_free_principal(context, increds.client);
2167 get_user_realm(krb5_context context, char **realm)
2169 static krb5_principal client_principal = 0;
2170 krb5_error_code r = 0;
2174 if (!_krb425_ccache) {
2175 r = krb5_cc_default(context, &_krb425_ccache);
2179 if (!client_principal) {
2181 r = krb5_parse_name(context, client, &client_principal);
2183 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2189 *realm = extract_realm(context, client_principal);