Translate messages from ktc_SetToken
[openafs.git] / src / aklog / aklog_main.c
index e29ce6f..3c2a421 100644 (file)
@@ -35,8 +35,6 @@
  */
 
 #include <afsconfig.h>
-RCSID
-    ("$Header$");
 
 #include <stdio.h>
 #include <string.h>
@@ -66,6 +64,7 @@ RCSID
 
 #include <afs/stds.h>
 #include <krb5.h>
+#include <com_err.h>
 
 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
 #include <afs/com_err.h>
@@ -75,6 +74,10 @@ RCSID
 #ifdef AFS_SUN5_ENV
 #include <sys/ioccom.h>
 #endif
+
+/* Prevent inclusion of des.h to avoid conflicts with des types */
+#define NO_DES_H_INCLUDE
+
 #include <afs/auth.h>
 #include <afs/cellconfig.h>
 #include <afs/vice.h>
@@ -183,6 +186,8 @@ static int get_user_realm(krb5_context, char *);
 #endif
 
 #if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT) 
+extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep, krb5_data **code);
+
 krb5_error_code
 krb5_encrypt_tkt_part(krb5_context context,
                      const krb5_keyblock *key,
@@ -309,6 +314,33 @@ static linked_list zsublist;       /* List of zephyr subscriptions */
 static linked_list hostlist;   /* List of host addresses */
 static linked_list authedcells;        /* List of cells already logged to */
 
+/* A com_error bodge. The idea here is that this routine lets us lookup
+ * things in the system com_err, if the AFS one just tells us the error
+ * is unknown
+ */
+
+void
+redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
+{
+    if (who) {
+       fputs(who, stderr);
+       fputs(": ", stderr);
+    }
+    if (code) {
+       const char *str = afs_error_message(code);
+       if (strncmp(str, "unknown", strlen("unknown")) == 0) {
+           str = error_message(code);
+       }
+       fputs(str, stderr);
+       fputs(" ", stderr);
+    }
+    if (fmt) {
+       vfprintf(stderr, fmt, ap);
+    }
+    putc('\n', stderr);
+    fflush(stderr);
+}
+
 /* ANL - CMU lifetime convert routine */
 /* for K5.4.1 don't use this for now. Need to see if it is needed */
 /* maybe needed in the krb524d module as well */
@@ -331,7 +363,7 @@ static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *loc
     struct afsconf_dir *configdir;
 
     memset(local_cell, 0, sizeof(local_cell));
-    memset((char *)cellconfig, 0, sizeof(*cellconfig));
+    memset(cellconfig, 0, sizeof(*cellconfig));
 
     if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
        fprintf(stderr, 
@@ -626,13 +658,20 @@ static int auth_to_cell(krb5_context context, char *cell, char *realm)
         */
 
        if (! do524) {
+           char k4name[ANAME_SZ], k4inst[INST_SZ], k4realm[REALM_SZ];
+#ifdef HAVE_NO_KRB5_524
            char *p;
            int len;
+#endif
 
            if (dflag)
                printf("Using Kerberos V5 ticket natively\n");
 
-           status = krb5_524_conv_principal (context, v5cred->client, &k4name, &k4inst, &k4realm);
+#ifndef HAVE_NO_KRB5_524
+           status = krb5_524_conv_principal (context, v5cred->client,
+                                             (char *) &k4name,
+                                             (char *) &k4inst,
+                                             (char *) &k4realm);
            if (status) {
                afs_com_err(progname, status, "while converting principal "
                        "to Kerberos V4 format");
@@ -643,6 +682,22 @@ static int auth_to_cell(krb5_context context, char *cell, char *realm)
                strcat (username, ".");
                strcat (username, k4inst);
            }
+#else
+           len = min(get_princ_len(context, v5cred->client, 0),
+                     second_comp(context, v5cred->client) ?
+                     MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
+           strncpy(username, get_princ_str(context, v5cred->client, 0), len);
+           username[len] = '\0';
+           
+           if (second_comp(context, v5cred->client)) {
+               strcat(username, ".");
+               p = username + strlen(username);
+               len = min(get_princ_len(context, v5cred->client, 1),
+                         MAXKTCNAMELEN - strlen(username) - 1);
+               strncpy(p, get_princ_str(context, v5cred->client, 1), len);
+               p[len] = '\0';
+           }
+#endif
 
            memset(&atoken, 0, sizeof(atoken));
            atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
@@ -763,8 +818,9 @@ static int auth_to_cell(krb5_context context, char *cell, char *realm)
                strcpy(aclient.instance, "");
                strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
                if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
-                   fprintf(stderr, "%s: unable to obtain tokens for cell %s "
-                           "(status: %d).\n", progname, cell_to_use, status);
+                   afs_com_err(progname, status,
+                               "while obtaining tokens for cell %s",
+                               cell_to_use);
                    status = AKLOG_TOKEN;
                }
 
@@ -822,9 +878,8 @@ static int auth_to_cell(krb5_context context, char *cell, char *realm)
        write(2,"",0); /* dummy write */
 #endif
        if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
-           fprintf(stderr, 
-                   "%s: unable to obtain tokens for cell %s (status: %d).\n",
-                   progname, cell_to_use, status);
+           afs_com_err(progname, status, "while obtaining tokens for cell %s",
+                       cell_to_use);
            status = AKLOG_TOKEN;
        }
     }
@@ -1188,7 +1243,7 @@ static void usage(void)
 
 void aklog(int argc, char *argv[])
 {
-       krb5_context context;
+    krb5_context context;
     int status = AKLOG_SUCCESS;
     int i;
     int somethingswrong = FALSE;
@@ -1230,6 +1285,7 @@ void aklog(int argc, char *argv[])
 
     krb5_init_context(&context);
     initialize_ktc_error_table ();
+    afs_set_com_err_hook(redirect_errors);
 
     /* Initialize list of cells to which we have authenticated */
     (void)ll_init(&authedcells);
@@ -1843,7 +1899,7 @@ static krb5_error_code get_credv5(krb5_context context,
               ? "/" : "", inst ? inst : "", realm);
     }
     
-    memset((char *)&increds, 0, sizeof(increds));
+    memset(&increds, 0, sizeof(increds));
 /* ANL - instance may be ptr to a null string. Pass null then */
     if ((r = krb5_build_principal(context, &increds.server,
                                  strlen(realm), realm,