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>
49 #if defined(HAVE_ET_COM_ERR_H)
50 #include <et/com_err.h>
55 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
56 #include <afs/com_err.h>
60 #include <sys/ioccom.h>
64 #include <afs/cellconfig.h>
66 #include <afs/venus.h>
67 #include <afs/ptserver.h>
68 #include <afs/ptuser.h>
69 #include <afs/pterror.h>
70 #include <afs/dirpath.h>
71 #include <afs/afsutil.h>
74 #include "linked_list.h"
76 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
79 #ifdef HAVE_KRB5_CREDS_SESSION
80 #define USING_HEIMDAL 1
86 #ifndef AFS_TRY_FULL_PRINC
87 #define AFS_TRY_FULL_PRINC 1
88 #endif /* AFS_TRY_FULL_PRINC */
90 #define AKLOG_TRYAGAIN -1
91 #define AKLOG_SUCCESS 0
93 #define AKLOG_SOMETHINGSWRONG 2
95 #define AKLOG_KERBEROS 4
97 #define AKLOG_BADPATH 6
109 /* RedHat 4.x doesn't seem to define this */
110 #define MAXSYMLINKS 5
113 #define DIR '/' /* Character that divides directories */
114 #define DIRSTRING "/" /* String form of above */
115 #define VOLMARKER ':' /* Character separating cellname from mntpt */
116 #define VOLMARKERSTRING ":" /* String form of above */
120 char realm[REALM_SZ];
123 static krb5_ccache _krb425_ccache = NULL;
126 * Why doesn't AFS provide these prototypes?
129 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
135 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *, int);
136 static int isdir(char *, unsigned char *);
137 static krb5_error_code get_credv5(krb5_context context, char *, char *,
138 char *, krb5_creds **);
139 static int get_user_realm(krb5_context, char **);
141 #define TRYAGAIN(x) (x == AKLOG_TRYAGAIN || \
142 x == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || \
143 x == KRB5KRB_ERR_GENERIC)
145 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
147 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
148 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
149 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
150 #define realm_data(c, p) krb5_princ_realm(c, p)->data
151 #define realm_len(c, p) krb5_princ_realm(c, p)->length
153 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
155 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
156 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
157 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
158 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
159 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
162 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
165 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
166 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
170 krb5_encrypt_tkt_part(krb5_context context,
171 const krb5_keyblock *key,
178 if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
180 if ((code = krb5_c_encrypt_length(context, key->enctype,
181 data->length, &enclen)))
183 ticket->enc_part.ciphertext.length = enclen;
184 if (!(ticket->enc_part.ciphertext.data = malloc(enclen))) {
188 if ((code = krb5_c_encrypt(context, key, KRB5_KEYUSAGE_KDC_REP_TICKET,
189 0, data, &ticket->enc_part))) {
190 free(ticket->enc_part.ciphertext.data);
191 ticket->enc_part.ciphertext.data = 0;
203 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
205 #define get_cred_keydata(c) c->keyblock.contents
206 #define get_cred_keylen(c) c->keyblock.length
207 #define get_creds_enctype(c) c->keyblock.enctype
209 #elif defined(HAVE_KRB5_CREDS_SESSION)
211 #define get_cred_keydata(c) c->session.keyvalue.data
212 #define get_cred_keylen(c) c->session.keyvalue.length
213 #define get_creds_enctype(c) c->session.keytype
216 #error "Must have either keyblock or session member of krb5_creds"
219 /* MITKerberosShim logs but returns success */
220 #if !defined(HAVE_KRB5_524_CONV_PRINCIPAL) || defined(AFS_DARWIN110_ENV) || (!defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC))
221 #define HAVE_NO_KRB5_524
222 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
223 #define krb5_524_convert_creds krb524_convert_creds_kdc
227 #define deref_keyblock_enctype(kb) \
230 #define deref_entry_keyblock(entry) \
233 #define deref_session_key(creds) \
236 #define deref_enc_tkt_addrs(tkt) \
239 #define deref_enc_length(enc) \
240 ((enc)->cipher.length)
242 #define deref_enc_data(enc) \
245 #define krb5_free_keytab_entry_contents krb5_kt_free_entry
248 #define deref_keyblock_enctype(kb) \
251 #define deref_entry_keyblock(entry) \
254 #define deref_session_key(creds) \
257 #define deref_enc_tkt_addrs(tkt) \
260 #define deref_enc_length(enc) \
261 ((enc)->ciphertext.length)
263 #define deref_enc_data(enc) \
264 ((enc)->ciphertext.data)
268 #define deref_entry_enctype(entry) \
269 deref_keyblock_enctype(&deref_entry_keyblock(entry))
272 * Provide a replacement for strerror if we don't have it
275 #ifndef HAVE_STRERROR
276 extern char *sys_errlist[];
277 #define strerror(x) sys_errlist[x]
278 #endif /* HAVE_STRERROR */
280 static char *progname = NULL; /* Name of this program */
281 static int dflag = FALSE; /* Give debugging information */
282 static int noauth = FALSE; /* If true, don't try to get tokens */
283 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
284 static int hosts = FALSE; /* Are we keeping track of hosts? */
285 static int noprdb = FALSE; /* Skip resolving name to id? */
286 static int linked = FALSE; /* try for both AFS nodes */
287 static int afssetpag = FALSE; /* setpag for AFS */
288 static int force = FALSE; /* Bash identical tokens? */
289 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
290 static char *keytab = NULL; /* keytab for akimpersonate */
291 static char *client = NULL; /* client principal for akimpersonate */
292 static linked_list zsublist; /* List of zephyr subscriptions */
293 static linked_list hostlist; /* List of host addresses */
294 static linked_list authedcells; /* List of cells already logged to */
296 /* A com_error bodge. The idea here is that this routine lets us lookup
297 * things in the system com_err, if the AFS one just tells us the error
302 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
309 const char *str = afs_error_message(code);
310 if (strncmp(str, "unknown", strlen("unknown")) == 0) {
311 #ifdef HAVE_KRB5_SVC_GET_MSG
312 krb5_svc_get_msg(code,&str);
313 #elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
314 krb5_context context;
315 krb5_init_context(&context);
316 str = krb5_get_error_message(context, code);
317 krb5_free_context(context);
319 ; /* IRIX apparently has neither: use the string we have */
324 #ifdef HAVE_KRB5_SVC_GET_MSG
325 krb5_free_string(str);
329 vfprintf(stderr, fmt, ap);
336 afs_dprintf(char *fmt, ...) {
346 copy_cellinfo(cellinfo_t *cellinfo)
348 cellinfo_t *new_cellinfo;
350 if ((new_cellinfo = malloc(sizeof(cellinfo_t))))
351 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
353 return ((char *)new_cellinfo);
358 get_cellconfig(const char *config, char *cell,
359 struct afsconf_cell *cellconfig, char **local_cell)
361 int status = AKLOG_SUCCESS;
362 struct afsconf_dir *configdir;
364 memset(cellconfig, 0, sizeof(*cellconfig));
366 *local_cell = malloc(MAXCELLCHARS);
367 if (*local_cell == NULL) {
368 fprintf(stderr, "%s: can't allocate memory for local cell name\n",
373 if (!(configdir = afsconf_Open(config))) {
375 "%s: can't get afs configuration (afsconf_Open(%s))\n",
380 if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
381 fprintf(stderr, "%s: can't determine local cell.\n", progname);
385 if ((cell == NULL) || (cell[0] == 0))
388 /* XXX - This function modifies 'cell' by passing it through lcstring */
389 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
390 fprintf(stderr, "%s: Can't get information about cell %s.\n",
395 afsconf_Close(configdir);
401 extract_realm(krb5_context context, krb5_principal princ) {
405 len = realm_len(context, princ);
406 if (len > REALM_SZ-1)
409 realm = malloc(sizeof(char) * (len+1));
413 strncpy(realm, realm_data(context, princ), len);
420 get_realm_from_cred(krb5_context context, krb5_creds *v5cred, char **realm) {
421 #if !defined(HEIMDAL) && defined(HAVE_KRB5_DECODE_TICKET)
422 krb5_error_code code;
427 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
431 *realm = extract_realm(context, ticket->server);
435 krb5_free_ticket(context, ticket);
445 * Get a Kerberos service ticket to use as the base of an rxkad token for
449 * An initialized Kerberos v5 context
451 * The realm to look in for the service principal. If NULL, then the
452 * realm is determined from the cell name or the user's credentials
453 * (see below for the heuristics used)
455 * The cell information for the cell to obtain a ticket for
457 * A Kerberos credentials structure containing the ticket acquired
458 * for the cell. This is a dynamically allocated structure, which
459 * should be freed by using the appropriate Kerberos API function.
460 * @param[out] realmUsed
461 * The realm in which the cell's service principal was located. If
462 * unset, then the principal was located in the same realm as the
463 * current user. This is a malloc'd string which should be freed
467 * 0 on success, an error value upon failure
470 * This code tries principals in the following, much debated,
473 * If the realm is specified on the command line we do
474 * - afs/cell@COMMAND-LINE-REALM
475 * - afs@COMMAND-LINE-REALM
478 * - afs/cell@REALM-FROM-USERS-PRINCIPAL
479 * - afs/cell@krb5_get_host_realm(db-server)
480 * Then, if krb5_get_host_realm(db-server) is non-empty
481 * - afs@ krb5_get_host_realm(db-server)
483 * - afs/cell@ upper-case-domain-of-db-server
484 * - afs@ upper-case-domain-of-db-server
486 * In all cases, the 'afs@' variant is only tried where the
487 * cell and the realm match case-insensitively.
491 rxkad_get_ticket(krb5_context context, char *realm,
492 struct afsconf_cell *cell,
493 krb5_creds **v5cred, char **realmUsed) {
494 char *realm_of_cell = NULL;
495 char *realm_of_user = NULL;
496 char *realm_from_princ = NULL;
502 if ((status = get_user_realm(context, &realm_of_user))) {
503 fprintf(stderr, "%s: Couldn't determine realm of user:", progname);
504 afs_com_err(progname, status, " while getting realm");
505 status = AKLOG_KERBEROS;
512 /* Cell on command line - use that one */
513 if (realm && realm[0]) {
514 realm_of_cell = realm;
515 status = AKLOG_TRYAGAIN;
516 afs_dprintf("We were told to authenticate to realm %s.\n", realm);
518 /* Initially, try using afs/cell@USERREALM */
519 afs_dprintf("Trying to authenticate to user's realm %s.\n",
521 realm_of_cell = realm_of_user;
522 status = get_credv5(context, AFSKEY, cell->name, realm_of_cell,
525 /* If that failed, try to determine the realm from the name of
526 * one of the DB servers */
527 if (TRYAGAIN(status)) {
528 realm_of_cell = afs_realm_of_cell(context, cell, FALSE);
529 if (!realm_of_cell) {
530 fprintf(stderr, "%s: Couldn't figure out realm for cell "
531 "%s.\n", progname, cell->name);
535 if (realm_of_cell[0])
536 afs_dprintf("We've deduced that we need to authenticate"
537 " to realm %s.\n", realm_of_cell);
539 afs_dprintf("We've deduced that we need to authenticate "
540 "using referrals.\n");
544 if (TRYAGAIN(status)) {
545 /* If we've got the full-princ-first option, or we're in a
546 * different realm from the cell - use the cell name as the
548 if (AFS_TRY_FULL_PRINC ||
549 strcasecmp(cell->name, realm_of_cell)!=0) {
550 status = get_credv5(context, AFSKEY, cell->name,
551 realm_of_cell, v5cred);
553 /* If we failed & we've got an empty realm, then try
554 * calling afs_realm_for_cell again. */
555 if (TRYAGAIN(status) && !realm_of_cell[0]) {
556 /* This time, get the realm by taking the domain
557 * component of the db server and make it upper case */
558 realm_of_cell = afs_realm_of_cell(context, cell, TRUE);
559 if (!realm_of_cell) {
561 "%s: Couldn't figure out realm for cell %s.\n",
562 progname, cell->name);
565 afs_dprintf("We've deduced that we need to authenticate"
566 " to realm %s.\n", realm_of_cell);
568 status = get_credv5(context, AFSKEY, cell->name,
569 realm_of_cell, v5cred);
572 /* If the realm and cell name match, then try without an
573 * instance, but only if realm is non-empty */
575 if (TRYAGAIN(status) &&
576 strcasecmp(cell->name, realm_of_cell) == 0) {
577 status = get_credv5(context, AFSKEY, NULL, realm_of_cell,
579 if (!AFS_TRY_FULL_PRINC && TRYAGAIN(status)) {
580 status = get_credv5(context, AFSKEY, cell->name,
581 realm_of_cell, v5cred);
586 /* Try to find a service principal for this cell.
587 * Some broken MIT libraries return KRB5KRB_AP_ERR_MSG_TYPE upon
588 * the first attempt, so we try twice to be sure */
590 if (status == KRB5KRB_AP_ERR_MSG_TYPE && retry == 1)
597 afs_dprintf("Kerberos error code returned by get_cred : %d\n", status);
598 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
599 progname, cell->name);
600 afs_com_err(progname, status, "while getting AFS tickets");
601 #ifdef KRB5_CC_NOT_KTYPE
602 if (status == KRB5_CC_NOT_KTYPE) {
603 fprintf(stderr, "allow_weak_crypto may be required in the Kerberos configuration\n");
606 status = AKLOG_KERBEROS;
610 /* If we've got a valid ticket, and we still don't know the realm name
611 * try to figure it out from the contents of the ticket
613 if (strcmp(realm_of_cell, "") == 0) {
614 status = get_realm_from_cred(context, *v5cred, &realm_from_princ);
617 "%s: Couldn't decode ticket to determine realm for "
619 progname, cell->name);
621 if (realm_from_princ)
622 realm_of_cell = realm_from_princ;
626 /* If the realm of the user and cell differ, then we need to use the
627 * realm when we later construct the user's principal */
628 if (realm_of_cell != NULL && strcmp(realm_of_user, realm_of_cell) != 0)
629 *realmUsed = realm_of_user;
632 if (realm_from_princ)
633 free(realm_from_princ);
634 if (realm_of_user && *realmUsed == NULL)
641 * Build an rxkad token from a Kerberos ticket, using only local tools (that
642 * is, without using a 524 conversion service)
645 * An initialised Kerberos 5 context
647 * A Kerberos credentials structure containing a suitable service ticket
648 * @param[out] tokenPtr
649 * An AFS token structure containing an rxkad token. This is a malloc'd
650 * structure which should be freed by the caller.
651 * @param[out[ userPtr
652 * A string containing the principal of the user to whom the token was
653 * issued. This is a malloc'd block which should be freed by the caller.
656 * 0 on success, an error value upon failure
659 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
660 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
661 char username[BUFSIZ];
662 struct ktc_token token;
664 #ifdef HAVE_NO_KRB5_524
668 char k4name[ANAME_SZ];
669 char k4inst[INST_SZ];
670 char k4realm[REALM_SZ];
673 afs_dprintf("Using Kerberos V5 ticket natively\n");
678 #ifndef HAVE_NO_KRB5_524
679 status = krb5_524_conv_principal (context, v5cred->client,
684 afs_com_err(progname, status, "while converting principal "
685 "to Kerberos V4 format");
686 return AKLOG_KERBEROS;
688 strcpy (username, k4name);
690 strcat (username, ".");
691 strcat (username, k4inst);
694 len = min(get_princ_len(context, v5cred->client, 0),
695 second_comp(context, v5cred->client) ?
696 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
697 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
698 username[len] = '\0';
700 if (second_comp(context, v5cred->client)) {
701 strcat(username, ".");
702 p = username + strlen(username);
703 len = min(get_princ_len(context, v5cred->client, 1),
704 MAXKTCNAMELEN - strlen(username) - 1);
705 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
710 memset(&token, 0, sizeof(struct ktc_token));
712 token.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
713 token.startTime = v5cred->times.starttime;;
714 token.endTime = v5cred->times.endtime;
715 memcpy(&token.sessionKey, get_cred_keydata(v5cred),
716 get_cred_keylen(v5cred));
717 token.ticketLen = v5cred->ticket.length;
718 memcpy(token.ticket, v5cred->ticket.data, token.ticketLen);
720 status = token_importRxkadViceId(tokenPtr, &token, 0);
725 *userPtr = strdup(username);
731 * Convert a Keberos ticket to an rxkad token, using information obtained
732 * from an external Kerberos 5->4 conversion service. If the code is built
733 * with HAVE_NO_KRB5_524 then this is a stub function which will always
734 * return success without a token.
737 * An initialised Kerberos 5 context
739 * A Kerberos credentials structure containing a suitable service ticket
740 * @param[out] tokenPtr
741 * An AFS token structure containing an rxkad token. This is a malloc'd
742 * structure which should be freed by the caller.
743 * @param[out[ userPtr
744 * A string containing the principal of the user to whom the token was
745 * issued. This is a malloc'd block which should be freed by the caller.
748 * 0 on success, an error value upon failure
751 #ifdef HAVE_NO_KRB5_524
753 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
754 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
762 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
763 struct ktc_tokenUnion **tokenPtr, char **userPtr) {
765 char username[BUFSIZ];
766 struct ktc_token token;
772 afs_dprintf("Using Kerberos 524 translator service\n");
774 status = krb5_524_convert_creds(context, v5cred, &cred);
777 afs_com_err(progname, status, "while converting tickets "
778 "to Kerberos V4 format");
779 return AKLOG_KERBEROS;
782 strcpy (username, cred.pname);
784 strcat (username, ".");
785 strcat (username, cred.pinst);
788 memset(&token, 0, sizeof(struct ktc_token));
790 token.kvno = cred.kvno;
791 token.startTime = cred.issue_date;
793 * It seems silly to go through a bunch of contortions to
794 * extract the expiration time, when the v5 credentials already
795 * has the exact time! Let's use that instead.
797 * Note that this isn't a security hole, as the expiration time
798 * is also contained in the encrypted token
800 token.endTime = v5cred->times.endtime;
801 memcpy(&token.sessionKey, cred.session, 8);
802 token.ticketLen = cred.ticket_st.length;
803 memcpy(token.ticket, cred.ticket_st.dat, token.ticketLen);
805 status = token_importRxkadViceId(tokenPtr, &token, 0);
810 *userPtr = strdup(username);
817 * This function gets an rxkad token for a given cell.
820 * An initialized Kerberos v5 context
822 * The cell information for the cell which we're obtaining a token for
824 * The realm to look in for the service principal. If NULL, then the
825 * realm is determined from the cell name or the user's credentials
826 * (see the documentation for rxkad_get_ticket)
828 * The rxkad token produced. This is a malloc'd structure which should
829 * be freed by the caller.
830 * @parma[out] authuser
831 * A string containing the principal of the user to whom the token was
832 * issued. This is a malloc'd block which should be freed by the caller.
833 * @param[out] foreign
834 * Whether the user is considered as 'foreign' to the realm of the cell.
837 * 0 on success, an error value upon failuer
840 rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
841 struct ktc_tokenUnion **token, char **authuser, int *foreign) {
843 char *realmUsed = NULL;
844 char *username = NULL;
851 status = rxkad_get_ticket(context, realm, cell, &v5cred, &realmUsed);
856 status = rxkad_get_converted_token(context, v5cred, token, &username);
858 status = rxkad_build_native_token(context, v5cred, token, &username);
863 /* We now have the username, plus the realm name, so stitch them together
864 * to give us the name that the ptserver will know the user by */
865 if (realmUsed == NULL) {
866 *authuser = username;
870 asprintf(authuser, "%s@%s", username, realmUsed);
884 * Log to a cell. If the cell has already been logged to, return without
885 * doing anything. Otherwise, log to it and mark that it has been logged
889 auth_to_cell(krb5_context context, const char *config,
890 char *cell, char *realm, char **linkedcell)
892 int status = AKLOG_SUCCESS;
894 char *username = NULL; /* To hold client username structure */
895 afs_int32 viceId; /* AFS uid of user */
897 char *local_cell = NULL;
898 struct ktc_tokenUnion *rxkadToken = NULL;
899 struct ktc_setTokenData *token;
900 struct ktc_setTokenData *btoken = NULL;
901 struct afsconf_cell cellconf;
903 /* NULL or empty cell returns information on local cell */
904 if ((status = get_cellconfig(config, cell, &cellconf, &local_cell)))
907 if (linkedcell != NULL) {
908 if (cellconf.linkedCell != NULL) {
909 *linkedcell = strdup(cellconf.linkedCell);
910 if (*linkedcell == NULL) {
919 if (ll_string(&authedcells, ll_s_check, cellconf.name)) {
920 afs_dprintf("Already authenticated to %s (or tried to)\n", cellconf.name);
921 status = AKLOG_SUCCESS;
926 * Record that we have attempted to log to this cell. We do this
927 * before we try rather than after so that we will not try
928 * and fail repeatedly for one cell.
930 ll_string(&authedcells, ll_s_add, cellconf.name);
933 * Record this cell in the list of zephyr subscriptions. We may
934 * want zephyr subscriptions even if authentication fails.
935 * If this is done after we attempt to get tokens, aklog -zsubs
936 * can return something different depending on whether or not we
937 * are in -noauth mode.
939 if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
941 "%s: failure adding cell %s to zephyr subscriptions list.\n",
942 progname, cellconf.name);
945 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
947 "%s: failure adding cell %s to zephyr subscriptions list.\n",
948 progname, local_cell);
953 afs_dprintf("Authenticating to cell %s (server %s).\n", cellconf.name,
954 cellconf.hostName[0]);
956 token = token_buildTokenJar(cellconf.name);
962 status = rxkad_get_token(context, &cellconf, realm, &rxkadToken,
963 &username, &isForeign);
967 /* We need to keep the token structure around so that we can stick
968 * the viceId into it (once we know it) */
969 status = token_addToken(token, rxkadToken);
971 afs_dprintf("Add Token failed with %d", status);
976 ktc_GetTokenEx(cellconf.name, &btoken) == 0 &&
977 token_SetsEquivalent(token, btoken)) {
979 token_FreeSet(&btoken);
980 afs_dprintf("Identical tokens already exist; skipping.\n");
981 status = AKLOG_SUCCESS;
986 token_FreeSet(&btoken);
993 afs_dprintf("Not resolving name %s to id (-noprdb set)\n", username);
996 afs_dprintf("About to resolve name %s to id in cell %s.\n", username,
999 if (!pr_Initialize (0, AFSDIR_CLIENT_ETC_DIRPATH, cellconf.name))
1000 status = pr_SNameToId (username, &viceId);
1003 afs_dprintf("Error %d\n", status);
1005 afs_dprintf("Id %d\n", (int) viceId);
1009 * This code is taken from cklog -- it lets people
1010 * automatically register with the ptserver in foreign cells
1013 #ifdef ALLOW_REGISTER
1014 if ((status == 0) && (viceId == ANONYMOUSID) && isForeign) {
1015 afs_dprintf("doing first-time registration of %s at %s\n",
1016 username, cellconf.name);
1019 status = ktc_SetTokenEx(token);
1021 afs_com_err(progname, status,
1022 "while obtaining tokens for cell %s",
1024 status = AKLOG_TOKEN;
1028 * In case you're wondering, we don't need to change the
1029 * filename here because we're still connecting to the
1030 * same cell -- we're just using a different authenticat ion
1034 if ((status = pr_Initialize(1L, AFSDIR_CLIENT_ETC_DIRPATH,
1036 printf("Error %d\n", status);
1039 if ((status = pr_CreateUser(username, &viceId))) {
1040 fprintf(stderr, "%s: %s so unable to create remote PTS "
1041 "user %s in cell %s (status: %d).\n", progname,
1042 afs_error_message(status), username, cellconf.name,
1044 viceId = ANONYMOUSID;
1046 printf("created cross-cell entry for %s (Id %d) at %s\n",
1047 username, viceId, cellconf.name);
1050 #endif /* ALLOW_REGISTER */
1052 if ((status == 0) && (viceId != ANONYMOUSID)) {
1053 status = token_setRxkadViceId(rxkadToken, viceId);
1055 fprintf(stderr, "Error %d setting rxkad ViceId\n", status);
1056 status = AKLOG_SUCCESS;
1058 token_replaceToken(token, rxkadToken);
1063 afs_dprintf("Setting tokens. %s @ %s \n", username, cellconf.name);
1065 #ifndef AFS_AIX51_ENV
1066 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
1067 * this routine, it will not add the token. It is not clear what
1068 * is going on here! So we will do the following operation.
1069 * On AIX 5, it causes the parent program to die, so we won't.
1071 write(2,"",0); /* dummy write */
1073 token_setPag(token, afssetpag);
1074 status = ktc_SetTokenEx(token);
1076 afs_com_err(progname, status, "while setting tokens for cell %s",
1078 status = AKLOG_TOKEN;
1082 afs_dprintf("Noauth mode; not authenticating.\n");
1086 token_freeToken(&rxkadToken);
1098 get_afs_mountpoint(char *file, char *mountpoint, int size)
1101 char V ='V'; /* AFS has problem on Sun with pioctl */
1103 char our_file[MAXPATHLEN + 1];
1105 char *last_component;
1106 struct ViceIoctl vio;
1107 char cellname[BUFSIZ];
1109 strlcpy(our_file, file, sizeof(our_file));
1111 if ((last_component = strrchr(our_file, DIR))) {
1112 *last_component++ = 0;
1113 parent_dir = our_file;
1116 last_component = our_file;
1120 memset(cellname, 0, sizeof(cellname));
1122 vio.in = last_component;
1123 vio.in_size = strlen(last_component)+1;
1124 vio.out_size = size;
1125 vio.out = mountpoint;
1127 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
1128 if (strchr(mountpoint, VOLMARKER) == NULL) {
1130 vio.in_size = strlen(file) + 1;
1131 vio.out_size = sizeof(cellname);
1134 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
1135 strlcat(cellname, VOLMARKERSTRING, sizeof(cellname));
1136 strlcat(cellname, mountpoint + 1, sizeof(cellname));
1137 memset(mountpoint + 1, 0, size - 1);
1138 strcpy(mountpoint + 1, cellname);
1148 * This routine each time it is called returns the next directory
1149 * down a pathname. It resolves all symbolic links. The first time
1150 * it is called, it should be called with the name of the path
1151 * to be descended. After that, it should be called with the arguemnt
1155 next_path(char *origpath)
1157 static char path[MAXPATHLEN + 1];
1158 static char pathtocheck[MAXPATHLEN + 1];
1160 int link = FALSE; /* Is this a symbolic link? */
1161 char linkbuf[MAXPATHLEN + 1];
1162 char tmpbuf[MAXPATHLEN + 1];
1164 static char *last_comp; /* last component of directory name */
1165 static char *elast_comp; /* End of last component */
1169 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1171 /* If we are given something for origpath, we are initializing only. */
1173 memset(path, 0, sizeof(path));
1174 memset(pathtocheck, 0, sizeof(pathtocheck));
1175 strlcpy(path, origpath, sizeof(path));
1181 /* We were not given origpath; find then next path to check */
1183 /* If we've gotten all the way through already, return NULL */
1184 if (last_comp == NULL)
1188 while (*last_comp == DIR)
1189 strncat(pathtocheck, last_comp++, 1);
1190 len = (elast_comp = strchr(last_comp, DIR))
1191 ? elast_comp - last_comp : strlen(last_comp);
1192 strncat(pathtocheck, last_comp, len);
1193 memset(linkbuf, 0, sizeof(linkbuf));
1194 link = readlink(pathtocheck, linkbuf, sizeof(linkbuf)-1);
1197 linkbuf[link] = '\0'; /* NUL terminate string */
1199 if (++symlinkcount > MAXSYMLINKS) {
1200 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1201 exit(AKLOG_BADPATH);
1204 memset(tmpbuf, 0, sizeof(tmpbuf));
1206 strlcpy(tmpbuf, elast_comp, sizeof(tmpbuf));
1207 if (linkbuf[0] == DIR) {
1209 * If this is a symbolic link to an absolute path,
1210 * replace what we have by the absolute path.
1212 memset(path, 0, strlen(path));
1213 memcpy(path, linkbuf, sizeof(linkbuf));
1214 strcat(path, tmpbuf);
1217 memset(pathtocheck, 0, sizeof(pathtocheck));
1221 * If this is a symbolic link to a relative path,
1222 * replace only the last component with the link name.
1224 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1225 strcat(path, tmpbuf);
1227 if ((t = strrchr(pathtocheck, DIR))) {
1229 memset(t, 0, strlen(t));
1232 memset(pathtocheck, 0, sizeof(pathtocheck));
1236 last_comp = elast_comp;
1240 return(pathtocheck);
1244 add_hosts(char *file)
1247 char V = 'V'; /* AFS has problem on SunOS */
1249 struct ViceIoctl vio;
1250 char outbuf[BUFSIZ];
1256 memset(outbuf, 0, sizeof(outbuf));
1258 vio.out_size = sizeof(outbuf);
1262 afs_dprintf("Getting list of hosts for %s\n", file);
1264 /* Don't worry about errors. */
1265 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1266 phosts = (long *) outbuf;
1269 * Lists hosts that we care about. If ALLHOSTS is defined,
1270 * then all hosts that you ever may possible go through are
1271 * included in this list. If not, then only hosts that are
1272 * the only ones appear. That is, if a volume you must use
1273 * is replaced on only one server, that server is included.
1274 * If it is replicated on many servers, then none are included.
1275 * This is not perfect, but the result is that people don't
1276 * get subscribed to a lot of instances of FILSRV that they
1277 * probably won't need which reduces the instances of
1278 * people getting messages that don't apply to them.
1281 if (phosts[1] != '\0')
1284 for (i = 0; phosts[i]; i++) {
1286 in.s_addr = phosts[i];
1287 afs_dprintf("Got host %s\n", inet_ntoa(in));
1288 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1290 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1291 afs_dprintf("Got host %s\n", hp->h_name);
1292 ll_string(&zsublist, ll_s_add, hp->h_name);
1299 * This routine descends through a path to a directory, logging to
1300 * every cell it encounters along the way.
1303 auth_to_path(krb5_context context, const char *config, char *path)
1305 int status = AKLOG_SUCCESS;
1306 int auth_status = AKLOG_SUCCESS;
1309 char pathtocheck[MAXPATHLEN + 1];
1310 char mountpoint[MAXPATHLEN + 1];
1319 strlcpy(pathtocheck, path, sizeof(pathtocheck));
1321 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1322 fprintf(stderr, "Unable to find current working directory:\n");
1323 fprintf(stderr, "%s\n", pathtocheck);
1324 fprintf(stderr, "Try an absolute pathname.\n");
1325 exit(AKLOG_BADPATH);
1328 strlcat(pathtocheck, DIRSTRING, sizeof(pathtocheck));
1329 strlcat(pathtocheck, path, sizeof(pathtocheck));
1332 next_path(pathtocheck);
1334 /* Go on to the next level down the path */
1335 while ((nextpath = next_path(NULL))) {
1336 strlcpy(pathtocheck, nextpath, sizeof(pathtocheck));
1337 afs_dprintf("Checking directory %s\n", pathtocheck);
1339 * If this is an afs mountpoint, determine what cell from
1340 * the mountpoint name which is of the form
1341 * #cellname:volumename or %cellname:volumename.
1343 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1344 /* skip over the '#' or '%' */
1345 cell = mountpoint + 1;
1346 /* Add this (cell:volumename) to the list of zsubs */
1348 ll_string(&zsublist, ll_s_add, cell);
1350 add_hosts(pathtocheck);
1351 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1353 auth_status = auth_to_cell(context, config, cell, NULL, NULL);
1355 if (status == AKLOG_SUCCESS)
1356 status = auth_status;
1357 else if (status != auth_status)
1358 status = AKLOG_SOMETHINGSWRONG;
1363 if (isdir(pathtocheck, &isdirectory) < 0) {
1365 * If we've logged and still can't stat, there's
1368 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1369 pathtocheck, strerror(errno));
1370 return(AKLOG_BADPATH);
1372 else if (! isdirectory) {
1373 /* Allow only directories */
1374 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1376 return(AKLOG_BADPATH);
1386 /* Print usage message and exit */
1390 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1391 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1392 "[[-p | -path] pathname]\n",
1393 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1395 #ifndef HAVE_NO_KRB5_524
1399 fprintf(stderr, " -d gives debugging information.\n");
1400 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1401 fprintf(stderr, " pathname is the name of a directory to which ");
1402 fprintf(stderr, "you wish to authenticate.\n");
1403 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1404 fprintf(stderr, " -hosts gives host address information.\n");
1405 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1406 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1407 fprintf(stderr, " -force means replace identical tickets. \n");
1408 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1409 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1410 #ifndef HAVE_NO_KRB5_524
1411 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1413 fprintf(stderr, " No commandline arguments means ");
1414 fprintf(stderr, "authenticate to the local cell.\n");
1415 fprintf(stderr, "\n");
1420 main(int argc, char *argv[])
1422 krb5_context context;
1423 int status = AKLOG_SUCCESS;
1425 int somethingswrong = FALSE;
1427 cellinfo_t cellinfo;
1429 extern char *progname; /* Name of this program */
1431 int cmode = FALSE; /* Cellname mode */
1432 int pmode = FALSE; /* Path name mode */
1434 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1435 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1436 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1438 linked_list cells; /* List of cells to log to */
1439 linked_list paths; /* List of paths to log to */
1442 const char *config = AFSDIR_CLIENT_ETC_DIRPATH;
1444 memset(&cellinfo, 0, sizeof(cellinfo));
1446 memset(realm, 0, sizeof(realm));
1447 memset(cell, 0, sizeof(cell));
1448 memset(path, 0, sizeof(path));
1456 /* Store the program name here for error messages */
1457 if ((progname = strrchr(argv[0], DIR)))
1462 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1464 char *filepath = NULL, *newpath = NULL;
1465 #ifndef AFS_DARWIN_ENV
1466 char *defaultpath = "/etc/krb5.conf:/etc/krb5/krb5.conf";
1468 char *defaultpath = "~/Library/Preferences/edu.mit.Kerberos:/Library/Preferences/edu.mit.Kerberos";
1470 filepath = getenv("KRB5_CONFIG");
1472 /* only fiddle with KRB5_CONFIG if krb5-weak.conf actually exists */
1473 asprintf(&newpath, "%s/krb5-weak.conf", AFSDIR_CLIENT_ETC_DIRPATH);
1474 if (access(newpath, R_OK) == 0) {
1477 asprintf(&newpath, "%s:%s/krb5-weak.conf",
1478 filepath ? filepath : defaultpath,
1479 AFSDIR_CLIENT_ETC_DIRPATH);
1480 setenv("KRB5_CONFIG", newpath, 1);
1483 krb5_init_context(&context);
1485 #if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
1488 setenv("KRB5_CONFIG", filepath, 1);
1490 unsetenv("KRB5_CONFIG");
1493 initialize_KTC_error_table ();
1494 initialize_U_error_table();
1495 initialize_RXK_error_table();
1496 initialize_ACFG_error_table();
1497 initialize_PT_error_table();
1498 afs_set_com_err_hook(redirect_errors);
1501 * Enable DES enctypes, which are currently still required for AFS.
1502 * krb5_allow_weak_crypto is MIT Kerberos 1.8. krb5_enctype_enable is
1505 #if defined(HAVE_KRB5_ENCTYPE_ENABLE)
1506 i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
1508 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1509 #elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
1510 krb5_allow_weak_crypto(context, 1);
1513 /* Initialize list of cells to which we have authenticated */
1514 ll_init(&authedcells);
1516 /* Parse commandline arguments and make list of what to do. */
1517 for (i = 1; i < argc; i++) {
1518 if (strcmp(argv[i], "-d") == 0)
1520 else if (strcmp(argv[i], "-noauth") == 0)
1522 else if (strcmp(argv[i], "-zsubs") == 0)
1524 else if (strcmp(argv[i], "-hosts") == 0)
1526 else if (strcmp(argv[i], "-noprdb") == 0)
1528 else if (strcmp(argv[i], "-linked") == 0)
1530 else if (strcmp(argv[i], "-force") == 0)
1532 #ifndef HAVE_NO_KRB5_524
1533 else if (strcmp(argv[i], "-524") == 0)
1536 else if (strcmp(argv[i], "-setpag") == 0)
1538 else if (((strcmp(argv[i], "-cell") == 0) ||
1539 (strcmp(argv[i], "-c") == 0)) && !pmode)
1542 strlcpy(cell, argv[i], sizeof(cell));
1546 else if ((strcmp(argv[i], "-keytab") == 0))
1552 else if ((strcmp(argv[i], "-principal") == 0))
1558 else if (((strcmp(argv[i], "-path") == 0) ||
1559 (strcmp(argv[i], "-p") == 0)) && !cmode)
1562 strlcpy(path, argv[i], sizeof(path));
1566 else if (strcmp(argv[i], "-config") == 0)
1572 else if (argv[i][0] == '-')
1574 else if (!pmode && !cmode) {
1575 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1576 (strcmp(argv[i], "..") == 0)) {
1578 strlcpy(path, argv[i], sizeof(path));
1582 strlcpy(cell, argv[i], sizeof(path));
1589 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1592 strlcpy(realm, argv[i], sizeof(realm));
1596 /* Add this cell to list of cells */
1597 strcpy(cellinfo.cell, cell);
1598 strcpy(cellinfo.realm, realm);
1599 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1601 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1602 ll_add_data(cur_node, new_cellinfo);
1605 "%s: failure copying cellinfo.\n", progname);
1610 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1614 memset(&cellinfo, 0, sizeof(cellinfo));
1616 memset(cell, 0, sizeof(cell));
1617 memset(realm, 0, sizeof(realm));
1620 /* Add this path to list of paths */
1621 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1623 if ((new_path = strdup(path)))
1624 ll_add_data(cur_node, new_path);
1626 fprintf(stderr, "%s: failure copying path name.\n",
1632 fprintf(stderr, "%s: failure adding path to paths list.\n",
1637 memset(path, 0, sizeof(path));
1641 /* If nothing was given, log to the local cell. */
1642 if ((cells.nelements + paths.nelements) == 0) {
1645 status = auth_to_cell(context, config, NULL, NULL, &linkedcell);
1647 /* If this cell is linked to a DCE cell, and user requested -linked,
1648 * get tokens for both. This is very useful when the AFS cell is
1649 * linked to a DFS cell and this system does not also have DFS.
1652 if (!status && linked && linkedcell != NULL) {
1653 afs_dprintf("Linked cell: %s\n", linkedcell);
1654 status = auth_to_cell(context, config, linkedcell, NULL, NULL);
1662 * Local hack - if the person has a file in their home
1663 * directory called ".xlog", read that for a list of
1664 * extra cells to authenticate to
1667 if ((pwd = getpwuid(getuid())) != NULL) {
1670 char fcell[100], xlog_path[512];
1672 strlcpy(xlog_path, pwd->pw_dir, sizeof(xlog_path));
1673 strlcat(xlog_path, "/.xlog", sizeof(xlog_path));
1675 if ((stat(xlog_path, &sbuf) == 0) &&
1676 ((f = fopen(xlog_path, "r")) != NULL)) {
1678 afs_dprintf("Reading %s for cells to authenticate to.\n",
1681 while (fgets(fcell, 100, f) != NULL) {
1684 fcell[strlen(fcell) - 1] = '\0';
1686 afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
1688 auth_status = auth_to_cell(context, config, fcell, NULL, NULL);
1689 if (status == AKLOG_SUCCESS)
1690 status = auth_status;
1692 status = AKLOG_SOMETHINGSWRONG;
1698 /* Log to all cells in the cells list first */
1699 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1700 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1701 status = auth_to_cell(context, config, cellinfo.cell,
1702 cellinfo.realm, &linkedcell);
1706 if (linked && linkedcell != NULL) {
1707 afs_dprintf("Linked cell: %s\n", linkedcell);
1708 status = auth_to_cell(context, config, linkedcell,
1709 cellinfo.realm, NULL);
1713 if (linkedcell != NULL) {
1720 /* Then, log to all paths in the paths list */
1721 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1722 status = auth_to_path(context, config, cur_node->data);
1728 * If only one thing was logged to, we'll return the status
1729 * of the single call. Otherwise, we'll return a generic
1730 * something failed status.
1732 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1733 status = AKLOG_SOMETHINGSWRONG;
1736 /* If we are keeping track of zephyr subscriptions, print them. */
1738 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1739 printf("zsub: %s\n", cur_node->data);
1742 /* If we are keeping track of host information, print it. */
1744 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1745 printf("host: %s\n", cur_node->data);
1752 isdir(char *path, unsigned char *val)
1754 struct stat statbuf;
1756 if (lstat(path, &statbuf) < 0)
1759 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1767 static krb5_error_code
1768 get_credv5_akimpersonate(krb5_context context,
1770 krb5_principal service_principal,
1771 krb5_principal client_principal,
1774 int *allowed_enctypes,
1776 krb5_creds** out_creds /* out */ )
1778 #if defined(USING_HEIMDAL) || (defined(HAVE_ENCODE_KRB5_ENC_TKT) && defined(HAVE_ENCODE_KRB5_TICKET) && defined(HAVE_KRB5_C_ENCRYPT))
1779 krb5_error_code code;
1781 krb5_kt_cursor cursor[1];
1782 krb5_keytab_entry entry[1];
1784 krb5_creds *creds = 0;
1785 krb5_enctype enctype;
1787 krb5_keyblock session_key[1];
1789 Ticket ticket_reply[1];
1790 EncTicketPart enc_tkt_reply[1];
1791 krb5_address address[30];
1792 krb5_addresses faddr[1];
1793 unsigned int temp_vno[1];
1794 time_t temp_time[2];
1796 krb5_ticket ticket_reply[1];
1797 krb5_enc_tkt_part enc_tkt_reply[1];
1798 krb5_address address[30], *faddr[30];
1802 static int any_enctype[] = {0};
1804 if (!(creds = malloc(sizeof *creds))) {
1808 if (!allowed_enctypes)
1809 allowed_enctypes = any_enctype;
1812 enctype = 0; /* AKIMPERSONATE_IGNORE_ENCTYPE */
1813 kvno = 0; /* AKIMPERSONATE_IGNORE_VNO */
1814 memset((char*)creds, 0, sizeof *creds);
1815 memset((char*)entry, 0, sizeof *entry);
1816 memset((char*)session_key, 0, sizeof *session_key);
1817 memset((char*)ticket_reply, 0, sizeof *ticket_reply);
1818 memset((char*)enc_tkt_reply, 0, sizeof *enc_tkt_reply);
1819 code = krb5_kt_resolve(context, keytab, &kt);
1822 afs_com_err(progname, code, "while resolving keytab %s", keytab);
1824 afs_com_err(progname, code, "while resolving default keytab");
1828 if (service_principal) {
1829 for (i = 0; (enctype = allowed_enctypes[i]) || !i; ++i) {
1830 code = krb5_kt_get_entry(context,
1837 if (allowed_enctypes[i])
1838 deref_keyblock_enctype(session_key) = allowed_enctypes[i];
1843 afs_com_err(progname, code,"while scanning keytab entries");
1847 krb5_keytab_entry new[1];
1849 memset(new, 0, sizeof *new);
1850 if ((code == krb5_kt_start_seq_get(context, kt, cursor))) {
1851 afs_com_err(progname, code, "while starting keytab scan");
1854 while (!(code = krb5_kt_next_entry(context, kt, new, cursor))) {
1856 allowed_enctypes[i] && allowed_enctypes[i]
1857 != deref_entry_enctype(new); ++i)
1859 if ((!i || allowed_enctypes[i]) &&
1860 (best < 0 || best > i)) {
1861 krb5_free_keytab_entry_contents(context, entry);
1863 memset(new, 0, sizeof *new);
1864 } else krb5_free_keytab_entry_contents(context, new);
1866 if ((i = krb5_kt_end_seq_get(context, kt, cursor))) {
1867 afs_com_err(progname, i, "while ending keytab scan");
1872 afs_com_err(progname, code, "while scanning keytab");
1875 deref_keyblock_enctype(session_key) = deref_entry_enctype(entry);
1881 if ((code = krb5_generate_random_keyblock(context,
1882 deref_keyblock_enctype(session_key), session_key))) {
1883 afs_com_err(progname, code, "while making session key");
1886 enc_tkt_reply->flags.initial = 1;
1887 enc_tkt_reply->transited.tr_type = DOMAIN_X500_COMPRESS;
1888 enc_tkt_reply->cname = client_principal->name;
1889 enc_tkt_reply->crealm = client_principal->realm;
1890 enc_tkt_reply->key = *session_key;
1892 static krb5_data empty_string;
1893 enc_tkt_reply->transited.contents = empty_string;
1895 enc_tkt_reply->authtime = starttime;
1896 enc_tkt_reply->starttime = temp_time;
1897 *enc_tkt_reply->starttime = starttime;
1899 enc_tkt_reply->renew_till = temp_time + 1;
1900 *enc_tkt_reply->renew_till = endtime;
1902 enc_tkt_reply->endtime = endtime;
1904 if ((code = krb5_c_make_random_key(context,
1905 deref_keyblock_enctype(session_key), session_key))) {
1906 afs_com_err(progname, code, "while making session key");
1909 enc_tkt_reply->magic = KV5M_ENC_TKT_PART;
1910 #define DATACAST (unsigned char *)
1911 enc_tkt_reply->flags |= TKT_FLG_INITIAL;
1912 enc_tkt_reply->transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
1913 enc_tkt_reply->session = session_key;
1914 enc_tkt_reply->client = client_principal;
1916 static krb5_data empty_string;
1917 enc_tkt_reply->transited.tr_contents = empty_string;
1919 enc_tkt_reply->times.authtime = starttime;
1920 enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
1921 enc_tkt_reply->times.endtime = endtime;
1922 #endif /* USING_HEIMDAL */
1923 /* NB: We will discard address for now--ignoring caddr field
1924 in any case. MIT branch does what it always did. */
1926 if (paddress && *paddress) {
1927 deref_enc_tkt_addrs(enc_tkt_reply) = faddr;
1930 faddr->val = address;
1932 for (i = 0; paddress[i]; ++i) {
1934 address[i].addr_type = KRB5_ADDRESS_INET;
1935 address[i].address.data = (void*)(paddress+i);
1936 address[i].address.length = sizeof(paddress[i]);
1939 address[i].magic = KV5M_ADDRESS;
1940 address[i].addrtype = ADDRTYPE_INET;
1942 address[i].addrtype = AF_INET;
1944 address[i].contents = (void*)(paddress+i);
1945 address[i].length = sizeof(int);
1946 faddr[i] = address+i;
1957 ticket_reply->sname = service_principal->name;
1958 ticket_reply->realm = service_principal->realm;
1960 { /* crypto block */
1961 krb5_crypto crypto = 0;
1962 unsigned char *buf = 0;
1963 size_t buf_size, buf_len;
1966 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size,
1967 enc_tkt_reply, &buf_len, code);
1969 afs_com_err(progname, code, "while encoding ticket");
1973 if(buf_len != buf_size) {
1974 afs_com_err(progname, code,
1975 "%u != %u while encoding ticket (internal ASN.1 encoder error",
1976 (unsigned int)buf_len, (unsigned int)buf_size);
1979 what = "krb5_crypto_init";
1980 code = krb5_crypto_init(context,
1981 &deref_entry_keyblock(entry),
1982 deref_entry_enctype(entry),
1985 what = "krb5_encrypt";
1986 code = krb5_encrypt_EncryptedData(context, crypto, KRB5_KU_TICKET,
1987 buf, buf_len, entry->vno, &(ticket_reply->enc_part));
1990 if (crypto) krb5_crypto_destroy(context, crypto);
1992 afs_com_err(progname, code, "while %s", what);
1995 } /* crypto block */
1996 ticket_reply->enc_part.etype = deref_entry_enctype(entry);
1997 ticket_reply->enc_part.kvno = (void *)temp_vno;
1998 *ticket_reply->enc_part.kvno = entry->vno;
1999 ticket_reply->tkt_vno = 5;
2001 ticket_reply->server = service_principal;
2002 ticket_reply->enc_part2 = enc_tkt_reply;
2003 if ((code = krb5_encrypt_tkt_part(context, &deref_entry_keyblock(entry), ticket_reply))) {
2004 afs_com_err(progname, code, "while making ticket");
2007 ticket_reply->enc_part.kvno = entry->vno;
2010 /* Construct Creds */
2012 if ((code = krb5_copy_principal(context, service_principal,
2014 afs_com_err(progname, code, "while copying service principal");
2017 if ((code = krb5_copy_principal(context, client_principal,
2019 afs_com_err(progname, code, "while copying client principal");
2022 if ((code = krb5_copy_keyblock_contents(context, session_key,
2023 &deref_session_key(creds)))) {
2024 afs_com_err(progname, code, "while copying session key");
2029 creds->times.authtime = enc_tkt_reply->authtime;
2030 creds->times.starttime = *(enc_tkt_reply->starttime);
2031 creds->times.endtime = enc_tkt_reply->endtime;
2032 creds->times.renew_till = 0; /* *(enc_tkt_reply->renew_till) */
2033 creds->flags.b = enc_tkt_reply->flags;
2035 creds->times = enc_tkt_reply->times;
2036 creds->ticket_flags = enc_tkt_reply->flags;
2038 if (!deref_enc_tkt_addrs(enc_tkt_reply))
2040 else if ((code = krb5_copy_addresses(context,
2041 deref_enc_tkt_addrs(enc_tkt_reply), &creds->addresses))) {
2042 afs_com_err(progname, code, "while copying addresses");
2048 size_t creds_tkt_len;
2049 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
2050 ticket_reply, &creds_tkt_len, code);
2052 afs_com_err(progname, code, "while encoding ticket");
2057 if ((code = encode_krb5_ticket(ticket_reply, &temp))) {
2058 afs_com_err(progname, code, "while encoding ticket");
2061 creds->ticket = *temp;
2068 if (deref_enc_data(&ticket_reply->enc_part))
2069 free(deref_enc_data(&ticket_reply->enc_part));
2070 krb5_free_keytab_entry_contents(context, entry);
2071 if (client_principal)
2072 krb5_free_principal(context, client_principal);
2073 if (service_principal)
2074 krb5_free_principal(context, service_principal);
2076 krb5_cc_close(context, cc);
2078 krb5_kt_close(context, kt);
2079 if (creds) krb5_free_creds(context, creds);
2080 krb5_free_keyblock_contents(context, session_key);
2088 static krb5_error_code
2089 get_credv5(krb5_context context, char *name, char *inst, char *realm,
2094 static krb5_principal client_principal = 0;
2096 afs_dprintf("Getting tickets: %s%s%s@%s\n", name,
2097 (inst && inst[0]) ? "/" : "", inst ? inst : "", realm);
2099 memset(&increds, 0, sizeof(increds));
2100 /* ANL - instance may be ptr to a null string. Pass null then */
2101 if ((r = krb5_build_principal(context, &increds.server,
2102 strlen(realm), realm,
2104 (inst && strlen(inst)) ? inst : NULL,
2110 if (!_krb425_ccache) {
2111 r = krb5_cc_default(context, &_krb425_ccache);
2115 if (!client_principal) {
2117 r = krb5_parse_name(context, client, &client_principal);
2119 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2125 increds.client = client_principal;
2126 increds.times.endtime = 0;
2127 /* Ask for DES since that is what V4 understands */
2128 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
2131 int allowed_enctypes[] = {
2132 ENCTYPE_DES_CBC_CRC, 0
2135 r = get_credv5_akimpersonate(context,
2144 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
2151 get_user_realm(krb5_context context, char **realm)
2153 static krb5_principal client_principal = 0;
2154 krb5_error_code r = 0;
2158 if (!_krb425_ccache) {
2159 r = krb5_cc_default(context, &_krb425_ccache);
2163 if (!client_principal) {
2165 r = krb5_parse_name(context, client, &client_principal);
2167 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
2173 *realm = extract_realm(context, client_principal);