Add new key/salt tuple handling modules
authorPaul Park <pjpark@mit.edu>
Fri, 4 Aug 1995 20:31:47 +0000 (20:31 +0000)
committerPaul Park <pjpark@mit.edu>
Fri, 4 Aug 1995 20:31:47 +0000 (20:31 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@6413 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/kadm/.Sanitize
src/lib/kadm/ChangeLog
src/lib/kadm/Makefile.in
src/lib/kadm/alt_prof.c
src/lib/kadm/keysalt.c [new file with mode: 0644]

index 16808bd4ecf6ebcdb415ac1b87c7d5551dbb709c..5457e01bd949aa8e43bd71acdae5a013a6c5998a 100644 (file)
@@ -34,6 +34,7 @@ adm_kt_enc.c
 adm_rw.c
 configure.in
 configure
+keysalt.c
 str_conv.c
 t_dbentry.c
 t_ktentry.c
index c6dcb85840fa1316af1c74b328ef19acadccb410..7af5edab8c9c54a701ff9bd0df480dae7bc6f68e 100644 (file)
@@ -1,4 +1,8 @@
 
+Fri Aug 4 16:21:50 EDT 1995    Paul Park       (pjpark@mit.edu)
+       * Makefile.in, .Sanitize, keysalt.c - Add keysalt.c modules.
+
+
 Thu Aug 3 11:51:14 EDT 1995    Paul Park       (pjpark@mit.edu)
        * alt_prof.c - Actually pass back the parsed string value in krb5_aprof_
                get_deltat().
index c3bac9144e544faec076da9db0df5dc74bcebba9..88b7f1b9b4df77f39edd840e9779ab8223b7af85 100644 (file)
@@ -9,7 +9,8 @@ BASE_OBJS= adm_conn.$(OBJEXT)   \
        adm_kt_enc.$(OBJEXT)    \
        adm_rw.$(OBJEXT)        \
        alt_prof.$(OBJEXT)      \
-       str_conv.$(OBJEXT)
+       str_conv.$(OBJEXT)      \
+       keysalt.$(OBJEXT)
 
 UNIX_OBJS = logger.$(OBJEXT)
 
@@ -26,7 +27,8 @@ SRCS= $(srcdir)/adm_conn.c    \
        $(srcdir)/adm_kw_enc.c  \
        $(srcdir)/logger.c      \
        $(srcdir)/alt_prof.c    \
-       $(srcdir)/str_conv.c
+       $(srcdir)/str_conv.c    \
+       $(srcdir)/keysalt.c
 
 all:: all-$(WHAT) $(BASE_OBJS) 
 
index 4288ba88cd1eb1a36ed06ce5beb6507bd50586f4..10645f56a5f9d6e8643f50624e6400c5223130a1 100644 (file)
@@ -412,80 +412,12 @@ krb5_read_realm_params(kcontext, realm, kdcprofile, kdcenv, rparamp)
            /* Get the value for the supported keytype/salttype matrix */
            hierarchy[2] = "supported_keytypes";
            if (!krb5_aprof_get_string(aprofile, hierarchy, TRUE, &svalue)) {
-               char                    *kp, *sp, *ep, *tp;
-               krb5_keytype            ktype;
-               krb5_int32              stype;
-               krb5_key_salt_tuple     *savep;
-
-               kp = svalue;
-               while (kp) {
-                   if ((ep = strchr(kp, (int) ',')) ||
-                       (ep = strchr(kp, (int) ' ')) ||
-                       (ep = strchr(kp, (int) '\t'))) {
-                       /* Fill in trailing whitespace of kp */
-                       tp = ep - 1;
-                       while (isspace(*tp) && (tp < kp)) {
-                           *tp = '\0';
-                           tp--;
-                       }
-                       *ep = '\0';
-                       ep++;
-                       /* Skip trailing whitespace of ep */
-                       while (isspace(*ep) && (*ep)) ep++;
-                   }
-                   /*
-                    * kp points to something (hopefully) of the form:
-                    *  <keytype>:<salttype>
-                    */
-                   if(sp = strchr(kp, (int) ':')) {
-                       /* Separate keytype from salttype */
-                       *sp = '\0';
-                       sp++;
-                       /* Attempt to parse keytype and salttype */
-                       if (!krb5_string_to_keytype(kp, &ktype) &&
-                           !krb5_string_to_salttype(sp, &stype)) {
-
-                           /* Squirrel away old keysalt array */
-                           savep = rparams->realm_keysalts;
-
-                           /* Get new keysalt array */
-                           if (rparams->realm_keysalts =
-                               (krb5_key_salt_tuple *)
-                               malloc((rparams->realm_num_keysalts+1) *
-                                      sizeof(krb5_key_salt_tuple))) {
-
-                               /* Copy old keysalt if appropriate */
-                               if (savep) {
-                                   memcpy(rparams->realm_keysalts, savep,
-                                          rparams->realm_num_keysalts *
-                                          sizeof(krb5_key_salt_tuple));
-                                   krb5_xfree(savep);
-                               }
-
-                               /* Save our values */
-                               rparams->realm_keysalts[rparams->
-                                                       realm_num_keysalts].
-                                                           ks_keytype = ktype;
-                               rparams->realm_keysalts[rparams->
-                                                       realm_num_keysalts].
-                                                           ks_salttype =
-                                                               stype;
-                               rparams->realm_num_keysalts++;
-                           }
-                           else {
-                               if (savep)
-                                   krb5_xfree(savep);
-                               break;
-                           }
-                       }
-                   }
-                   kp = ep;
-               }
-               if (kp) {
-                   rparams->realm_num_keysalts = 0;
-                   krb5_xfree(rparams->realm_keysalts);
-                   rparams->realm_keysalts = (krb5_key_salt_tuple *) NULL;
-               }
+               krb5_string_to_keysalts(svalue,
+                                       ", \t", /* Tuple separators     */
+                                       ":.-",  /* Key/salt separators  */
+                                       0,      /* No duplicates        */
+                                       &rparams->realm_keysalts,
+                                       &rparams->realm_num_keysalts);
                krb5_xfree(svalue);
            }
        }
@@ -518,3 +450,4 @@ krb5_free_realm_params(kcontext, rparams)
     }
     return(0);
 }
+
diff --git a/src/lib/kadm/keysalt.c b/src/lib/kadm/keysalt.c
new file mode 100644 (file)
index 0000000..2802de2
--- /dev/null
@@ -0,0 +1,206 @@
+/*
+ * lib/kadm/keysalt.c
+ *
+ * Copyright 1995 by the Massachusetts Institute of Technology.
+ * All Rights Reserved.
+ *
+ * Export of this software from the United States of America may
+ *   require a specific license from the United States Government.
+ *   It is the responsibility of any person or organization contemplating
+ *   export to obtain such a license before exporting.
+ *
+ * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
+ * distribute this software and its documentation for any purpose and
+ * without fee is hereby granted, provided that the above copyright
+ * notice appear in all copies and that both that copyright notice and
+ * this permission notice appear in supporting documentation, and that
+ * the name of M.I.T. not be used in advertising or publicity pertaining
+ * to distribution of the software without specific, written prior
+ * permission.  M.I.T. makes no representations about the suitability of
+ * this software for any purpose.  It is provided "as is" without express
+ * or implied warranty.
+ *
+ */
+
+/*
+ * keysalt.c   - Routines to handle key/salt tuples.
+ */
+#include "k5-int.h"
+#include "adm.h"
+#include "adm_proto.h"
+
+static const char default_tupleseps[]  = ", \t";
+static const char default_ksaltseps[]  = ":.";
+
+/*
+ * krb5_keysalt_is_present()   - Determine if a key/salt pair is present
+ *                               in a list of key/salt tuples.
+ *
+ *     Salttype may be negative to indicate a search for only a keytype.
+ */
+krb5_boolean
+krb5_keysalt_is_present(ksaltlist, nksalts, keytype, salttype)
+    krb5_key_salt_tuple        *ksaltlist;
+    krb5_int32         nksalts;
+    krb5_keytype       keytype;
+    krb5_int32         salttype;
+{
+    krb5_boolean       foundit;
+    int                        i;
+
+    foundit = 0;
+    if (ksaltlist) {
+       for (i=0; i<nksalts; i++) {
+           if ((ksaltlist[i].ks_keytype == keytype) &&
+               ((ksaltlist[i].ks_salttype == salttype) ||
+                (salttype < 0))) {
+               foundit = 1;
+               break;
+           }
+       }
+    }
+    return(foundit);
+}
+
+/*
+ * krb5_keysalt_iterate()      - Do something for each unique key/salt
+ *                               combination.
+ *
+ * If ignoresalt set, then salttype is ignored.
+ */
+krb5_error_code
+krb5_keysalt_iterate(ksaltlist, nksalt, ignoresalt, iterator, arg)
+    krb5_key_salt_tuple        *ksaltlist;
+    krb5_int32         nksalt;
+    krb5_boolean       ignoresalt;
+    krb5_error_code    (*iterator) KRB5_NPROTOTYPE((krb5_key_salt_tuple *,
+                                                    krb5_pointer));
+    krb5_pointer       arg;
+{
+    int                        i;
+    krb5_error_code    kret;
+    krb5_key_salt_tuple        scratch;
+
+    kret = 0;
+    for (i=0; i<nksalt; i++) {
+       scratch.ks_keytype = ksaltlist[i].ks_keytype;
+       scratch.ks_salttype = (ignoresalt) ? -1 : ksaltlist[i].ks_salttype;
+       if (!krb5_keysalt_is_present(ksaltlist,
+                                    i,
+                                    scratch.ks_keytype,
+                                    scratch.ks_salttype)) {
+           if (kret = (*iterator)(&scratch, arg))
+               break;
+       }
+    }
+    return(kret);
+}
+
+/*
+ * krb5_string_to_keysalts()   - Convert a string representation to a list
+ *                               of key/salt tuples.
+ */
+krb5_error_code
+krb5_string_to_keysalts(string, tupleseps, ksaltseps, dups, ksaltp, nksaltp)
+    char               *string;
+    const char         *tupleseps;
+    const char         *ksaltseps;
+    krb5_boolean       dups;
+    krb5_key_salt_tuple        **ksaltp;
+    krb5_int32         *nksaltp;
+{
+    krb5_error_code    kret;
+    char               *kp, *sp, *ep, *tp;
+    krb5_keytype       ktype;
+    krb5_int32         stype;
+    krb5_key_salt_tuple        *savep;
+    const char         *tseplist;
+    const char         *ksseplist;
+    const char         *septmp;
+    
+    kret = 0;
+    kp = string;
+    tseplist = (tupleseps) ? tupleseps : default_tupleseps;
+    ksseplist = (ksaltseps) ? ksaltseps : default_ksaltseps;
+    while (kp) {
+       /* Attempt to find a separator */
+       ep = (char *) NULL;
+       if (*tseplist) {
+           septmp = tseplist;
+           for (ep = strchr(kp, (int) *septmp);
+                *(++septmp) && !ep;
+                ep = strchr(kp, (int) *septmp));
+       }
+
+       if (ep) {
+           /* Fill in trailing whitespace of kp */
+           tp = ep - 1;
+           while (isspace(*tp) && (tp < kp)) {
+               *tp = '\0';
+               tp--;
+           }
+           *ep = '\0';
+           ep++;
+           /* Skip trailing whitespace of ep */
+           while (isspace(*ep) && (*ep)) ep++;
+       }
+       /*
+        * kp points to something (hopefully) of the form:
+        *      <keytype><ksseplist><salttype>
+        *      or
+        *      <keytype>
+        */
+       sp = (char *) NULL;
+       /* Attempt to find a separator */
+       septmp = ksseplist;
+       for (sp = strchr(kp, (int) *septmp);
+            *(++septmp) && !sp;
+            ep = strchr(kp, (int) *septmp));
+
+       if (sp) {
+           /* Separate keytype from salttype */
+           *sp = '\0';
+           sp++;
+       }
+       else
+           stype = -1;
+
+       /*
+        * Attempt to parse keytype and salttype.  If we parse well
+        * then make sure that it specifies a unique key/salt combo
+        */
+       if (!krb5_string_to_keytype(kp, &ktype) &&
+           (!sp || !krb5_string_to_salttype(sp, &stype)) &&
+           (dups ||
+            !krb5_keysalt_is_present(*ksaltp, *nksaltp, ktype, stype))) {
+
+           /* Squirrel away old keysalt array */
+           savep = *ksaltp;
+
+           /* Get new keysalt array */
+           if (*ksaltp = (krb5_key_salt_tuple *)
+               malloc(((*nksaltp)+1) * sizeof(krb5_key_salt_tuple))) {
+
+               /* Copy old keysalt if appropriate */
+               if (savep) {
+                   memcpy(*ksaltp, savep,
+                          (*nksaltp) * sizeof(krb5_key_salt_tuple));
+                   krb5_xfree(savep);
+               }
+
+               /* Save our values */
+               (*ksaltp)[(*nksaltp)].ks_keytype = ktype;
+               (*ksaltp)[(*nksaltp)].ks_salttype = stype;
+               (*nksaltp)++;
+           }
+           else {
+               *ksaltp = savep;
+               break;
+           }
+       }
+       kp = ep;
+    }
+    return(kret);
+}
+
+