]> andersk Git - moira.git/blobdiff - clients/lib/utils.c
Build without krb4 if it's unavailable.
[moira.git] / clients / lib / utils.c
index 7a89c93b5e7fedc2d4d7033bafb15f55fc3f6c2e..207e1bdeda4e011a535fdc863775e00d4e8325c6 100644 (file)
@@ -12,7 +12,7 @@
 #include <mrclient.h>
 
 #include <com_err.h>
-#include <krb.h>
+#include <krb5.h>
 
 #include <sys/types.h>
 
@@ -34,6 +34,7 @@
 RCSID("$Header$");
 
 extern char *whoami;
+extern krb5_context context;
 
 int mrcl_connect(char *server, char *client, int version, int auth)
 {
@@ -88,7 +89,12 @@ int mrcl_connect(char *server, char *client, int version, int auth)
 
   if (auth)
     {
-      status = mr_auth(client);
+      status = mr_krb5_auth(client);
+
+      /* New client talking to old server, try krb4. */
+      if (status == MR_UNKNOWN_PROC)
+       status = mr_auth(client);
+
       if (status)
        {
          com_err(whoami, status, "while authenticating to Moira.");
@@ -102,26 +108,44 @@ int mrcl_connect(char *server, char *client, int version, int auth)
 
 char *mrcl_krb_user(void)
 {
-  int status;
-  static char pname[ANAME_SZ];
+  int flags = 0;
+  krb5_ccache cache = NULL;
+  krb5_principal princ = NULL;
+  krb5_error_code status;
+  char *username = NULL;
+
+  if (!context)
+    krb5_init_context(&context);
 
-  status = tf_init(TKT_FILE, R_TKT_FIL);
-  if (status == KSUCCESS)
+  status = krb5_cc_default(context, &cache);
+  if (status)
     {
-      status = tf_get_pname(pname);
-      tf_close();
+      com_err(whoami, status, "while reading Kerberos ticket file.");
+      goto out;
     }
 
-  if (status != KSUCCESS)
+  status = krb5_cc_get_principal(context, cache, &princ);
+  if (status)
     {
-      /* In case mr_init hasn't been called yet. */
-      initialize_krb_error_table();
-      status += ERROR_TABLE_BASE_krb;
-      com_err(whoami, status, "reading Kerberos ticket file.");
-      return NULL;
+      com_err(whoami, status, "while retrieving principal name.");
+      goto out;
     }
 
-  return pname;
+  username = malloc(krb5_princ_component(context, princ, 0)->length + 1);
+  if (!username)
+    goto out;
+
+  strncpy(username, krb5_princ_component(context, princ, 0)->data,
+         krb5_princ_component(context, princ, 0)->length);
+  username[krb5_princ_component(context, princ, 0)->length] = '\0';
+
+ out:
+  if (cache)
+    krb5_cc_close(context, cache);
+  if (princ)
+    krb5_free_principal(context, princ);
+
+  return username;
 }
 
 char *partial_canonicalize_hostname(char *s)
This page took 0.039479 seconds and 4 git commands to generate.