Rework set_header_word macros
[openafs.git] / src / kauth / kadatabase.c
index a1e6580..78f4900 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
-#include <sys/types.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <netinet/in.h>
-#endif
-#include <string.h>
 #include <ubik.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
 #include <afs/afsutil.h>
+
 #include "kauth.h"
 #include "kautils.h"
 #include "kaserver.h"
 
+#if !defined(offsetof)
+#include <stddef.h>             /* for definition of offsetof() */
+#endif
+
 extern Date cheaderReadTime;   /* time cheader last read in */
 
-#define set_header_word(tt,field,value) kawrite ((tt), ((char *)&(cheader.field) - (char *)&cheader), ((cheader.field = (value)), (char *)&(cheader.field)), sizeof(afs_int32))
+#define set_header_word(tt,field,value) \
+       ( \
+           (cheader.field) = (value), \
+           kawrite((tt), ((char *)&(cheader.field) - (char *)&cheader), \
+                   (char *)&(cheader.field), sizeof(afs_int32)) \
+       )
 
-#define inc_header_word(tt,field) kawrite ((tt), ((char *)&(cheader.field) - (char *)&cheader), ((cheader.field = (htonl(ntohl(cheader.field)+1))), (char *)&(cheader.field)), sizeof(afs_int32))
+#define inc_header_word(tt,field) kawrite ((tt), (offsetof(struct kaheader, field)), ((cheader.field = (htonl(ntohl(cheader.field)+1))), (char *)&(cheader.field)), sizeof(afs_int32))
 
-static int index_OK();
+static int index_OK(afs_int32);
 
 afs_int32
-NameHash(aname, ainstance)
-     register char *aname;
-     register char *ainstance;
+NameHash(char *aname, char *ainstance)
 {
-    register unsigned int hash;
-    register int i;
+    unsigned int hash;
+    int i;
 
     /* stolen directly from the HashString function in the vol package */
     hash = 0;
@@ -56,11 +56,7 @@ NameHash(aname, ainstance)
 /* package up seek and write into one procedure for ease of use */
 
 afs_int32
-kawrite(tt, pos, buff, len)
-     struct ubik_trans *tt;
-     afs_int32 pos;
-     char *buff;
-     afs_int32 len;
+kawrite(struct ubik_trans *tt, afs_int32 pos, char *buff, afs_int32 len)
 {
     afs_int32 code;
 
@@ -74,11 +70,7 @@ kawrite(tt, pos, buff, len)
 /* same thing for read */
 
 afs_int32
-karead(tt, pos, buff, len)
-     struct ubik_trans *tt;
-     afs_int32 pos;
-     char *buff;
-     afs_int32 len;
+karead(struct ubik_trans *tt, afs_int32 pos, char *buff, afs_int32 len)
 {
     afs_int32 code;
 
@@ -89,7 +81,6 @@ karead(tt, pos, buff, len)
     return code;
 }
 
-static struct Lock cheader_lock;
 static struct Lock keycache_lock;
 
 static int maxCachedKeys;
@@ -107,15 +98,13 @@ static afs_int32 keyCacheVersion = 0;
 static afs_int32 maxKeyLifetime;
 static int dbfixup = 0;
 
-init_kadatabase(initFlags)
-     int initFlags;            /* same as init_kaprocs (see which) */
+void
+init_kadatabase(int initFlags)
 {
-    Lock_Init(&cheader_lock);
     Lock_Init(&keycache_lock);
 
     maxCachedKeys = 10;
-    keyCache =
-       (struct cachedKey *)malloc(maxCachedKeys * sizeof(struct cachedKey));
+    keyCache = malloc(maxCachedKeys * sizeof(struct cachedKey));
     keyCacheVersion = 0;
     if (initFlags & 4) {
        maxKeyLifetime = 90;
@@ -128,21 +117,25 @@ init_kadatabase(initFlags)
 
 /* check that the database has been initialized.  Be careful to fail in a safe
    manner, to avoid bogusly reinitializing the db.  */
-
-afs_int32
-CheckInit(at, db_init)
-     struct ubik_trans *at;
-     int (*db_init) ();                /* procedure to call if rebuilding DB */
+/**
+ * reads in db cache from ubik.
+ *
+ * @param[in] ut ubik transaction
+ * @param[in] rock  opaque pointer to an int (*) (struct ubik_trans *), which
+ *                  will be called on rebuilding the database (or NULL to not
+ *                  rebuild the db)
+ *
+ * @return operation status
+ *   @retval 0 success
+ */
+static afs_int32
+UpdateCache(struct ubik_trans *at, void *rock)
 {
-    register afs_int32 code;
+    int (*db_init) (struct ubik_trans *) = rock;
+    afs_int32 code;
     afs_int32 iversion;
     afs_int32 tversion;
 
-    /* Don't read header if not necessary */
-    if (!ubik_CacheUpdate(at))
-       return 0;
-
-    ObtainWriteLock(&cheader_lock);
     if ((code = karead(at, 0, (char *)&iversion, sizeof(iversion)))
        || (code =
            karead(at, sizeof(cheader) - sizeof(afs_int32), (char *)&tversion,
@@ -168,7 +161,6 @@ CheckInit(at, db_init)
            code = KAIO;
        }
     }
-    ReleaseWriteLock(&cheader_lock);
     if (code == 0)
        return 0;
 
@@ -203,15 +195,20 @@ CheckInit(at, db_init)
     return db_init(at);                /* initialize the db */
 }
 
+afs_int32
+CheckInit(struct ubik_trans *at,
+          int (*db_init) (struct ubik_trans *))                /* procedure to call if rebuilding DB */
+{
+    return ubik_CheckCache(at, UpdateCache, db_init);
+}
+
 /* Allocate a free block of storage for entry, returning address of a new
    zeroed entry.  If zero is returned, a Ubik I/O error can be assumed.  */
 
 afs_int32
-AllocBlock(at, tentry)
-     register struct ubik_trans *at;
-     struct kaentry *tentry;
+AllocBlock(struct ubik_trans *at, struct kaentry *tentry)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 temp;
 
     if (cheader.freePtr) {
@@ -240,9 +237,7 @@ AllocBlock(at, tentry)
    Returns zero for success or an error code on failure. */
 
 afs_int32
-FreeBlock(at, index)
-     struct ubik_trans *at;
-     afs_int32 index;
+FreeBlock(struct ubik_trans *at, afs_int32 index)
 {
     struct kaentry tentry;
     int code;
@@ -273,15 +268,11 @@ FreeBlock(at, index)
    returned. */
 
 afs_int32
-FindBlock(at, aname, ainstance, toP, tentry)
-     struct ubik_trans *at;
-     char *aname;
-     char *ainstance;
-     afs_int32 *toP;
-     struct kaentry *tentry;
+FindBlock(struct ubik_trans *at, char *aname, char *ainstance, afs_int32 *toP,
+         struct kaentry *tentry)
 {
-    register afs_int32 i, code;
-    register afs_int32 to;
+    afs_int32 i, code;
+    afs_int32 to;
 
     *toP = 0;
     i = NameHash(aname, ainstance);
@@ -307,10 +298,8 @@ FindBlock(at, aname, ainstance, toP, tentry)
    returns zero if there were no errors. */
 
 afs_int32
-ThreadBlock(at, index, tentry)
-     struct ubik_trans *at;
-     afs_int32 index;
-     struct kaentry *tentry;
+ThreadBlock(struct ubik_trans *at, afs_int32 index,
+           struct kaentry *tentry)
 {
     int code;
     int hi;                    /* hash index */
@@ -332,12 +321,10 @@ ThreadBlock(at, index, tentry)
    error code. */
 
 afs_int32
-UnthreadBlock(at, aentry)
-     struct ubik_trans *at;
-     struct kaentry *aentry;
+UnthreadBlock(struct ubik_trans *at, struct kaentry *aentry)
 {
-    register afs_int32 i, code;
-    register afs_int32 to;
+    afs_int32 i, code;
+    afs_int32 to;
     afs_int32 lo;
     struct kaentry tentry;
 
@@ -380,11 +367,8 @@ UnthreadBlock(at, aentry)
    remaining count is negative.  */
 
 afs_int32
-NextBlock(at, index, tentry, remaining)
-     struct ubik_trans *at;
-     afs_int32 index;
-     struct kaentry *tentry;
-     afs_int32 *remaining;
+NextBlock(struct ubik_trans *at, afs_int32 index, struct kaentry *tentry,
+         afs_int32 *remaining)
 {
     int code;
     afs_int32 last;
@@ -420,11 +404,8 @@ NextBlock(at, index, tentry, remaining)
    and pointer to the user entry. */
 
 afs_int32
-ka_NewKey(tt, tentryaddr, tentry, key)
-     struct ubik_trans *tt;
-     afs_int32 tentryaddr;
-     struct kaentry *tentry;
-     struct ktc_encryptionKey *key;
+ka_NewKey(struct ubik_trans *tt, afs_int32 tentryaddr,
+         struct kaentry *tentry, struct ktc_encryptionKey *key)
 {
     struct kaOldKeys okeys;    /* old keys block */
     afs_int32 okeysaddr, nextaddr;     /* offset of old keys block */
@@ -433,7 +414,10 @@ ka_NewKey(tt, tentryaddr, tentry, key)
     Date now = time(0);
     afs_int32 newkeyver;       /* new key version number */
     afs_int32 newtotalkeyentries = 0, oldtotalkeyentries = 0, keyentries;
-    int foundcurrentkey = 0, addednewkey = 0, modified;
+    int addednewkey = 0, modified;
+#ifdef AUTH_DBM_LOG
+    int foundcurrentkey = 0;
+#endif
 
     es_Report("Newkey for %s.%s\n", tentry->userID.name,
              tentry->userID.instance);
@@ -482,8 +466,8 @@ ka_NewKey(tt, tentryaddr, tentry, key)
                            ("Warning: Entry %s.%s contains more than one valid key: fixing\n",
                             tentry->userID.name, tentry->userID.instance));
                }
-#endif
                foundcurrentkey = 1;
+#endif
            }
 
            /* If we find an oldkey of the same version or
@@ -597,16 +581,13 @@ ka_NewKey(tt, tentryaddr, tentry, key)
 }
 
 afs_int32
-ka_DelKey(tt, tentryaddr, tentry)
-     struct ubik_trans *tt;
-     afs_int32 tentryaddr;
-     struct kaentry *tentry;
+ka_DelKey(struct ubik_trans *tt, afs_int32 tentryaddr,
+         struct kaentry *tentry)
 {
     int code;
     struct kaOldKeys okeys;    /* old keys block */
     afs_int32 okeysaddr, nextaddr;     /* offset of old keys block */
     afs_int32 prevptr = 0;
-    Date now = time(0);
 
     es_Report("DelKey for %s.%s\n", tentry->userID.name,
              tentry->userID.instance);
@@ -658,12 +639,12 @@ ka_DelKey(tt, tentryaddr, tentry)
 }
 
 void
-ka_debugKeyCache(info)
-     struct ka_debugInfo *info;
+ka_debugKeyCache(struct ka_debugInfo *info)
 {
     int i;
 
-    memcpy(&info->cheader_lock, &cheader_lock, sizeof(info->cheader_lock));
+    /* cheader_lock no longer exists */
+    memset(&info->cheader_lock, 0, sizeof(info->cheader_lock));
     memcpy(&info->keycache_lock, &keycache_lock, sizeof(info->keycache_lock));
 
     info->kcVersion = keyCacheVersion;
@@ -703,12 +684,8 @@ ka_debugKeyCache(info)
 /* Add a key to the key cache, expanding it if necessary. */
 
 void
-ka_Encache(name, inst, kvno, key, superseded)
-     char *name;
-     char *inst;
-     afs_int32 kvno;
-     struct ktc_encryptionKey *key;
-     Date superseded;
+ka_Encache(char *name, char *inst, afs_int32 kvno,
+          struct ktc_encryptionKey *key, Date superseded)
 {
     int i;
 
@@ -733,10 +710,8 @@ ka_Encache(name, inst, kvno, key, superseded)
            return;
        }
     /* i == maxCachedKeys */
-    keyCache =
-       (struct cachedKey *)realloc(keyCache,
-                                   (maxCachedKeys *=
-                                    2) * sizeof(struct cachedKey));
+    keyCache = realloc(keyCache, (maxCachedKeys *=2)
+                                 * sizeof(struct cachedKey));
     if (keyCache == 0) {
        es_Report("Can't realloc keyCache! out of memory?");
        exit(123);
@@ -756,12 +731,8 @@ ka_Encache(name, inst, kvno, key, superseded)
    with tt==0, since Rx can't call Ubik. */
 
 afs_int32
-ka_LookupKvno(tt, name, inst, kvno, key)
-     struct ubik_trans *tt;
-     char *name;
-     char *inst;
-     afs_int32 kvno;
-     struct ktc_encryptionKey *key;
+ka_LookupKvno(struct ubik_trans *tt, char *name, char *inst, afs_int32 kvno,
+             struct ktc_encryptionKey *key)
 {
     int i;
     int code = 0;
@@ -825,12 +796,11 @@ ka_LookupKvno(tt, name, inst, kvno, key)
 /* Look up the primary key and key version for a principal. */
 
 afs_int32
-ka_LookupKey(tt, name, inst, kvno, key)
-     struct ubik_trans *tt;
-     char *name;
-     char *inst;
-     afs_int32 *kvno;          /* returned */
-     struct ktc_encryptionKey *key;    /* copied out */
+ka_LookupKey(struct ubik_trans *tt,
+            char *name,
+            char *inst,
+            afs_int32 *kvno,                   /* returned */
+            struct ktc_encryptionKey *key)     /* copied out */
 {
     int i;
     afs_int32 to;
@@ -877,8 +847,7 @@ ka_LookupKey(tt, name, inst, kvno, key)
    Ubik needs to use Rx. */
 
 afs_int32
-ka_FillKeyCache(tt)
-     struct ubik_trans *tt;
+ka_FillKeyCache(struct ubik_trans *tt)
 {
     int nfound;
     afs_int32 ko;
@@ -920,9 +889,7 @@ ka_FillKeyCache(tt)
 }
 
 afs_int32
-update_admin_count(tt, delta)
-     struct ubik_trans *tt;
-     int delta;
+update_admin_count(struct ubik_trans *tt, int delta)
 {
     afs_int32 to;
     afs_int32 code;
@@ -937,8 +904,7 @@ update_admin_count(tt, delta)
 }
 
 static int
-index_OK(index)
-     afs_int32 index;
+index_OK(afs_int32 index)
 {
     if ((index < sizeof(cheader)) || (index >= ntohl(cheader.eofPtr))
        || ((index - sizeof(cheader)) % sizeof(kaentry) != 0))
@@ -949,9 +915,7 @@ index_OK(index)
 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
 
 int
-name_instance_legal(name, instance)
-     char *name;
-     char *instance;
+name_instance_legal(char *name, char *instance)
 {
     int code;
 
@@ -970,10 +934,9 @@ name_instance_legal(name, instance)
     return code;
 }
 
+#if 0
 static int
-string_legal(str, map)
-     char *str;
-     char *map;
+string_legal(char *str, char *map)
 {
     int slen;
 
@@ -982,3 +945,5 @@ string_legal(str, map)
        return 0;               /* with trailing null must fit in data base */
     return (slen == strspn(str, map)); /* strspn returns length(str) if all chars in map */
 }
+#endif
+