#include <afsconfig.h>
#include <afs/param.h>
-RCSID("$Header$");
+RCSID
+ ("$Header$");
#include "apache_afs_cache.h"
#define ROTL(n, x) (((x) << (n)) | ((x) >> (SHA_BITS_PER_INT - (n))))
#ifdef DISABLED_CODE_HERE
-static sha_int f(int t, sha_int x, sha_int y, sha_int z)
+static sha_int
+f(int t, sha_int x, sha_int y, sha_int z)
{
- if (t < 0 || t >= SHA_ROUNDS) return 0;
+ if (t < 0 || t >= SHA_ROUNDS)
+ return 0;
if (t < 20)
- return (z ^ (x & (y ^ z)));
+ return (z ^ (x & (y ^ z)));
if (t < 40)
- return (x ^ y ^ z);
+ return (x ^ y ^ z);
if (t < 60)
- return ((x & y) | (z & (x | y))); /* saves 1 boolean op */
+ return ((x & y) | (z & (x | y))); /* saves 1 boolean op */
return (x ^ y ^ z); /* 60-79 same as 40-59 */
}
#endif
/* This macro/function supplies the "magic" constant for each round. */
/* The function call version preserved above until stable */
-static const sha_int k_vals[] = { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
+static const sha_int k_vals[] =
+ { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
#define K(t) ( (t < 0 || t >= SHA_ROUNDS) ? 0 : k_vals[ t/20 ] )
/*
* Update the internal state based on the given chunk.
*/
-static void transform(shaState *shaStateP, sha_int *chunk)
+static void
+transform(shaState * shaStateP, sha_int * chunk)
{
sha_int A = shaStateP->digest[0];
sha_int B = shaStateP->digest[1];
for (t = 0; t < SHA_CHUNK_INTS; t++)
W[t] = chunk[t];
for (; t < SHA_ROUNDS; t++) {
- TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
+ TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
W[t] = ROTL(1, TEMP);
}
for (t = 0; t < SHA_ROUNDS; t++) {
TEMP = ROTL(5, A) + f(t, B, C, D) + E + W[t] + K(t);
- E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
+ E = D;
+ D = C;
+ C = ROTL(30, B);
+ B = A;
+ A = TEMP;
}
shaStateP->digest[0] += A;
* as input and produces an output array of ints that is
* SHA_CHUNK_INTS long.
*/
-static void buildInts(const char *data, sha_int *chunk)
+static void
+buildInts(const char *data, sha_int * chunk)
{
/*
* Need to copy the data because we can't be certain that
* buildInts to break the input up into chunks and repeatedly passing
* these chunks to transform().
*/
-void sha_update(shaState *shaStateP, const char *buffer, int bufferLen)
+void
+sha_update(shaState * shaStateP, const char *buffer, int bufferLen)
{
int i;
sha_int chunk[SHA_CHUNK_INTS];
shaStateP->bitcountHi++;
shaStateP->bitcountLo = newLo;
shaStateP->bitcountHi += ((bufferLen >> (SHA_BITS_PER_INT - 3)) & 0x07);
-
+
/*
* If we won't have enough for a full chunk, just tack this
* buffer onto the leftover piece and return.
bufferLen -= SHA_CHUNK_BYTES;
}
/* assert((bufferLen >= 0) && (bufferLen < SHA_CHUNK_BYTES)); */
- if ((bufferLen < 0) || (bufferLen > SHA_CHUNK_BYTES) ) {
- fprintf(stderr, "apache_afs_cache: ASSERTION FAILED...exiting\n");
- exit(-1);
+ if ((bufferLen < 0) || (bufferLen > SHA_CHUNK_BYTES)) {
+ fprintf(stderr, "apache_afs_cache: ASSERTION FAILED...exiting\n");
+ exit(-1);
}
if (bufferLen > 0) {
* of the hash bitcount to finish the hash. The hash value
* is not valid until finish() has been called.
*/
-void sha_finish(shaState *shaStateP)
+void
+sha_finish(shaState * shaStateP)
{
sha_int chunk[SHA_CHUNK_INTS];
int i;
* SHS standard, and clear out the bitcount and leftover vars.
* This should be used to initialize an shaState.
*/
-void sha_clear(shaState *shaStateP)
+void
+sha_clear(shaState * shaStateP)
{
big_endian = (0x01020304 == htonl(0x01020304));
- memcpy((void *)&shaStateP->digest[0], (void *)&hashinit[0], SHA_HASH_BYTES);
+ memcpy((void *)&shaStateP->digest[0], (void *)&hashinit[0],
+ SHA_HASH_BYTES);
shaStateP->bitcountLo = shaStateP->bitcountHi = 0;
shaStateP->leftoverLen = 0;
}
/*
* Hash the buffer and place the result in *shaStateP.
*/
-void sha_hash(shaState *shaStateP, const char *buffer, int bufferLen)
+void
+sha_hash(shaState * shaStateP, const char *buffer, int bufferLen)
{
sha_clear(shaStateP);
sha_update(shaStateP, buffer, bufferLen);
* Returns the current state of the hash as an array of 20 bytes.
* This will be an interim result if finish() has not yet been called.
*/
-void sha_bytes(const shaState *shaStateP, char *bytes)
+void
+sha_bytes(const shaState * shaStateP, char *bytes)
{
sha_int temp[SHA_HASH_INTS];
- int i;
+ int i;
for (i = 0; i < SHA_HASH_INTS; i++)
temp[i] = htonl(shaStateP->digest[i]);
/*
* Hash function for the AFS login cache
*/
-int weblog_login_hash(char *name, char *cell)
+int
+weblog_login_hash(char *name, char *cell)
{
char *p;
afs_uint32 val;
- for (val = *name , p = name ; *p != '\0' ; p++) {
- val = (val << 2) ^ val ^ (afs_uint32)(*p);
+ for (val = *name, p = name; *p != '\0'; p++) {
+ val = (val << 2) ^ val ^ (afs_uint32) (*p);
}
- for (p = cell ; *p != '\0' ; p++) {
- val = (val << 2) ^ val ^ (afs_uint32)(*p);
+ for (p = cell; *p != '\0'; p++) {
+ val = (val << 2) ^ val ^ (afs_uint32) (*p);
}
- return val & (WEBLOG_LOGIN_HASH_SIZE-1);
+ return val & (WEBLOG_LOGIN_HASH_SIZE - 1);
}
/*
* Compute a SHA checksum on the username, cellname, and password
*/
-void weblog_login_checksum(
- char *user,
- char *cell,
- char *passwd,
- char *cksum)
+void
+weblog_login_checksum(char *user, char *cell, char *passwd, char *cksum)
{
- int passwdLen;
- int userLen;
- int cellLen;
- char *shaBuffer;
- shaState state;
-
- /*
- * Compute SHA(username,SHA(password,pad))
- */
- passwdLen = strlen(passwd);
- userLen = strlen(user);
- cellLen = strlen(cell);
- shaBuffer = (char *) malloc(MAX(userLen+cellLen,passwdLen)+SHA_HASH_BYTES);
- strcpy(shaBuffer, passwd);
- memcpy((void *)(shaBuffer + passwdLen), (void *)(&weblog_login_pad[0]),
- SHA_HASH_BYTES);
- sha_clear(&state);
- sha_hash(&state, shaBuffer, passwdLen+SHA_HASH_BYTES);
- memcpy(shaBuffer, user, userLen);
- memcpy(shaBuffer+userLen, cell, cellLen);
- sha_bytes(&state, shaBuffer+userLen+cellLen);
- sha_clear(&state);
- sha_hash(&state, shaBuffer, userLen+cellLen+SHA_HASH_BYTES);
- sha_bytes(&state, &cksum[0]);
- memset(shaBuffer, 0, MAX(userLen+cellLen,passwdLen)+SHA_HASH_BYTES);
- free(shaBuffer);
+ int passwdLen;
+ int userLen;
+ int cellLen;
+ char *shaBuffer;
+ shaState state;
+
+ /*
+ * Compute SHA(username,SHA(password,pad))
+ */
+ passwdLen = strlen(passwd);
+ userLen = strlen(user);
+ cellLen = strlen(cell);
+ shaBuffer =
+ (char *)malloc(MAX(userLen + cellLen, passwdLen) + SHA_HASH_BYTES);
+ strcpy(shaBuffer, passwd);
+ memcpy((void *)(shaBuffer + passwdLen), (void *)(&weblog_login_pad[0]),
+ SHA_HASH_BYTES);
+ sha_clear(&state);
+ sha_hash(&state, shaBuffer, passwdLen + SHA_HASH_BYTES);
+ memcpy(shaBuffer, user, userLen);
+ memcpy(shaBuffer + userLen, cell, cellLen);
+ sha_bytes(&state, shaBuffer + userLen + cellLen);
+ sha_clear(&state);
+ sha_hash(&state, shaBuffer, userLen + cellLen + SHA_HASH_BYTES);
+ sha_bytes(&state, &cksum[0]);
+ memset(shaBuffer, 0, MAX(userLen + cellLen, passwdLen) + SHA_HASH_BYTES);
+ free(shaBuffer);
}
/*
* given username, and the SHA checksums match, then
* set the token parameter and return 1, otherwise return 0.
*/
-int weblog_login_lookup(
- char *user,
- char *cell,
- char *cksum,
- char *token)
+int
+weblog_login_lookup(char *user, char *cell, char *cksum, char *token)
{
- int index;
- long curTime;
- struct weblog_login *loginP, *tmpP, loginTmp;
-
- /*
- * Search the hash chain for a matching entry, free
- * expired entries as we search
- */
- index = weblog_login_hash(user, cell);
- curTime = time(NULL);
-
- loginP = weblog_login_cache[index].head;
- while (loginP != NULL) {
- if (loginP->expiration < curTime) {
- tmpP = loginP;
- loginP = tmpP->next;
-
- DLL_DELETE(tmpP, weblog_login_cache[index].head,
- weblog_login_cache[index].tail, next, prev);
- free(tmpP);
- continue;
- }
- if (strcmp(loginP->username, user) == 0 &&
- strcmp(loginP->cellname, cell) == 0 &&
- memcmp((void *)&loginP->cksum[0], (void *)cksum,SHA_HASH_BYTES) == 0) {
-
- memcpy((void *)token, (void *) &loginP->token[0], MAXBUFF);
- return loginP->tokenLen;
+ int index;
+ long curTime;
+ struct weblog_login *loginP, *tmpP, loginTmp;
+
+ /*
+ * Search the hash chain for a matching entry, free
+ * expired entries as we search
+ */
+ index = weblog_login_hash(user, cell);
+ curTime = time(NULL);
+
+ loginP = weblog_login_cache[index].head;
+ while (loginP != NULL) {
+ if (loginP->expiration < curTime) {
+ tmpP = loginP;
+ loginP = tmpP->next;
+
+ DLL_DELETE(tmpP, weblog_login_cache[index].head,
+ weblog_login_cache[index].tail, next, prev);
+ free(tmpP);
+ continue;
+ }
+ if (strcmp(loginP->username, user) == 0
+ && strcmp(loginP->cellname, cell) == 0
+ && memcmp((void *)&loginP->cksum[0], (void *)cksum,
+ SHA_HASH_BYTES) == 0) {
+
+ memcpy((void *)token, (void *)&loginP->token[0], MAXBUFF);
+ return loginP->tokenLen;
+ }
+ loginP = loginP->next;
}
- loginP = loginP->next;
- }
- return 0;
+ return 0;
}
/*
* Insert a login token into the cache. If the user already has an entry,
* then overwrite the old entry.
*/
-int weblog_login_store(
- char *user,
- char *cell,
- char *cksum,
- char *token,
- int tokenLen,
- afs_uint32 expiration)
+int
+weblog_login_store(char *user, char *cell, char *cksum, char *token,
+ int tokenLen, afs_uint32 expiration)
{
- int index;
- long curTime;
- struct weblog_login *loginP, *tmpP, loginTmp;
-
- int parseToken(char *tokenBuf);
-
- /*
- * Search the hash chain for a matching entry, free
- * expired entries as we search
- */
- index = weblog_login_hash(user, cell);
- curTime = time(NULL);
- loginP = weblog_login_cache[index].head;
-
- while (loginP != NULL) {
- if (strcmp(loginP->username, user) == 0 &&
- strcmp(loginP->cellname, cell) == 0) {
- break;
+ int index;
+ long curTime;
+ struct weblog_login *loginP, *tmpP, loginTmp;
+
+ int parseToken(char *tokenBuf);
+
+ /*
+ * Search the hash chain for a matching entry, free
+ * expired entries as we search
+ */
+ index = weblog_login_hash(user, cell);
+ curTime = time(NULL);
+ loginP = weblog_login_cache[index].head;
+
+ while (loginP != NULL) {
+ if (strcmp(loginP->username, user) == 0
+ && strcmp(loginP->cellname, cell) == 0) {
+ break;
+ }
+ if (loginP->expiration < curTime) {
+ tmpP = loginP;
+ loginP = tmpP->next;
+
+ DLL_DELETE(tmpP, weblog_login_cache[index].head,
+ weblog_login_cache[index].tail, next, prev);
+ free(tmpP);
+ continue;
+ }
+ loginP = loginP->next;
}
- if (loginP->expiration < curTime) {
- tmpP = loginP;
- loginP = tmpP->next;
-
- DLL_DELETE(tmpP, weblog_login_cache[index].head,
- weblog_login_cache[index].tail, next, prev);
- free(tmpP);
- continue;
+ if (loginP == NULL) {
+ loginP = (struct weblog_login *)malloc(sizeof(struct weblog_login));
+ strcpy(&loginP->username[0], user);
+ strcpy(&loginP->cellname[0], cell);
+ } else {
+ DLL_DELETE(loginP, weblog_login_cache[index].head,
+ weblog_login_cache[index].tail, next, prev);
}
- loginP = loginP->next;
- }
- if (loginP == NULL) {
- loginP = (struct weblog_login *)malloc(sizeof(struct weblog_login));
- strcpy(&loginP->username[0], user);
- strcpy(&loginP->cellname[0], cell);
- }
- else {
- DLL_DELETE(loginP, weblog_login_cache[index].head,
- weblog_login_cache[index].tail, next, prev);
- }
-
- memcpy((void *)&loginP->cksum[0], (void *)cksum, SHA_HASH_BYTES);
- loginP->expiration = expiration;
- loginP->tokenLen=getTokenLen(token);
- memcpy((void *)&loginP->token[0], (void *)token, MAXBUFF);
-
- DLL_INSERT_TAIL(loginP, weblog_login_cache[index].head,
- weblog_login_cache[index].tail, next, prev);
- return 0;
+
+ memcpy((void *)&loginP->cksum[0], (void *)cksum, SHA_HASH_BYTES);
+ loginP->expiration = expiration;
+ loginP->tokenLen = getTokenLen(token);
+ memcpy((void *)&loginP->token[0], (void *)token, MAXBUFF);
+
+ DLL_INSERT_TAIL(loginP, weblog_login_cache[index].head,
+ weblog_login_cache[index].tail, next, prev);
+ return 0;
}
token_cache_init()
{
- int i;
- for (i = 0 ; i < WEBLOG_LOGIN_HASH_SIZE ; i++) {
- DLL_INIT_LIST(weblog_login_cache[i].head,
- weblog_login_cache[i].tail);
- }
+ int i;
+ for (i = 0; i < WEBLOG_LOGIN_HASH_SIZE; i++) {
+ DLL_INIT_LIST(weblog_login_cache[i].head, weblog_login_cache[i].tail);
+ }
}
-int getTokenLen(char *buf)
+int
+getTokenLen(char *buf)
{
- afs_int32 len=0;
- afs_int32 rc=0;
- char cellName[WEBLOG_CELLNAME_MAX];
- register char *tp;
- int n = sizeof(afs_int32);
- struct ClearToken {
- afs_int32 AuthHandle;
- char HandShakeKey[8];
- afs_int32 ViceId;
- afs_int32 BeginTimestamp;
- afs_int32 EndTimestamp;
- } token;
- tp=buf;
- memcpy(&len, tp, sizeof(afs_int32)); /* get size of secret token */
- rc=(len+sizeof(afs_int32));
- tp += (sizeof(afs_int32) + len); /* skip secret token and its length */
- memcpy(&len, tp, sizeof(afs_int32)); /* get size of clear token */
- if (len != sizeof(struct ClearToken)) {
+ afs_int32 len = 0;
+ afs_int32 rc = 0;
+ char cellName[WEBLOG_CELLNAME_MAX];
+ register char *tp;
+ int n = sizeof(afs_int32);
+ struct ClearToken {
+ afs_int32 AuthHandle;
+ char HandShakeKey[8];
+ afs_int32 ViceId;
+ afs_int32 BeginTimestamp;
+ afs_int32 EndTimestamp;
+ } token;
+ tp = buf;
+ memcpy(&len, tp, sizeof(afs_int32)); /* get size of secret token */
+ rc = (len + sizeof(afs_int32));
+ tp += (sizeof(afs_int32) + len); /* skip secret token and its length */
+ memcpy(&len, tp, sizeof(afs_int32)); /* get size of clear token */
+ if (len != sizeof(struct ClearToken)) {
#ifdef DEBUG
- fprintf(stderr, "apache_afs_cache.c:getExpiration:"
- "something's wrong with the length of ClearToken:%d\n", len);
+ fprintf(stderr,
+ "apache_afs_cache.c:getExpiration:"
+ "something's wrong with the length of ClearToken:%d\n", len);
#endif
- return -1;
- }
- rc+=(sizeof(afs_int32)+len); /* length of clear token + length itself */
- tp+=(sizeof(afs_int32)+len); /* skip clear token and its length */
- rc+=sizeof(afs_int32); /* length of primary flag */
- tp+=sizeof(afs_int32); /* skip over primary flag */
- strcpy(cellName, tp);
- if (cellName != NULL)
- rc+=strlen(cellName);
- return rc;
+ return -1;
+ }
+ rc += (sizeof(afs_int32) + len); /* length of clear token + length itself */
+ tp += (sizeof(afs_int32) + len); /* skip clear token and its length */
+ rc += sizeof(afs_int32); /* length of primary flag */
+ tp += sizeof(afs_int32); /* skip over primary flag */
+ strcpy(cellName, tp);
+ if (cellName != NULL)
+ rc += strlen(cellName);
+ return rc;
}
-long getExpiration(char *buf)
+long
+getExpiration(char *buf)
{
- afs_int32 len=0;
- register char *tp;
- int n = sizeof(afs_int32);
- struct ClearToken {
- afs_int32 AuthHandle;
- char HandShakeKey[8];
- afs_int32 ViceId;
- afs_int32 BeginTimestamp;
- afs_int32 EndTimestamp;
- } token;
-
- tp = buf;
- memcpy(&len, tp, sizeof(afs_int32)); /* get size of secret token */
- tp += (sizeof(afs_int32) + len); /* skip secret token and its length */
- memcpy(&len, tp, sizeof(afs_int32)); /* get size of clear token */
- if (len != sizeof(struct ClearToken)) {
+ afs_int32 len = 0;
+ register char *tp;
+ int n = sizeof(afs_int32);
+ struct ClearToken {
+ afs_int32 AuthHandle;
+ char HandShakeKey[8];
+ afs_int32 ViceId;
+ afs_int32 BeginTimestamp;
+ afs_int32 EndTimestamp;
+ } token;
+
+ tp = buf;
+ memcpy(&len, tp, sizeof(afs_int32)); /* get size of secret token */
+ tp += (sizeof(afs_int32) + len); /* skip secret token and its length */
+ memcpy(&len, tp, sizeof(afs_int32)); /* get size of clear token */
+ if (len != sizeof(struct ClearToken)) {
#ifdef DEBUG
- fprintf(stderr, "apache_afs_cache.c:getExpiration:"
- "something's wrong with the length of ClearToken:%d\n", len);
+ fprintf(stderr,
+ "apache_afs_cache.c:getExpiration:"
+ "something's wrong with the length of ClearToken:%d\n", len);
#endif
- return -1;
- }
-
- tp += sizeof(afs_int32); /* skip length of clear token */
- memcpy(&token, tp, sizeof(struct ClearToken)); /* copy the token */
- return token.EndTimestamp;
+ return -1;
+ }
+
+ tp += sizeof(afs_int32); /* skip length of clear token */
+ memcpy(&token, tp, sizeof(struct ClearToken)); /* copy the token */
+ return token.EndTimestamp;
}