Translate messages from ktc_SetToken
[openafs.git] / src / aklog / aklog_main.c
index 147db90..3c2a421 100644 (file)
@@ -64,6 +64,7 @@
 
 #include <afs/stds.h>
 #include <krb5.h>
+#include <com_err.h>
 
 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
 #include <afs/com_err.h>
@@ -313,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 */
@@ -790,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;
                }
 
@@ -849,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;
        }
     }
@@ -1215,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;
@@ -1257,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);