2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #include <hcrypto/des.h>
20 #include <afs/afsutil.h>
28 /* This should match the behavior of ParseLoginName on input so that the output
29 * and input are compatible. In names "." should show as \056 and in names and
30 * instances "@" should show as \100 */
33 ka_PrintUserID(char *prefix, /* part to be output before userID */
34 char *name, /* user name */
35 char *instance, /* instance, possible null or len=0 */
37 { /* for output following userID */
40 for (c = (unsigned char *)name; *c; c++)
41 if (isalnum(*c) || (ispunct(*c) && (*c != '.') && (*c != '@')))
45 if (instance && strlen(instance)) {
47 for (c = (unsigned char *)instance; *c; c++)
48 if (isalnum(*c) || (ispunct(*c) && (*c != '@')))
53 printf("%s", postfix);
57 ka_PrintBytes(char bs[], int bl)
61 for (i = 0; i < bl; i++) {
62 unsigned char c = bs[i];
67 /* converts a byte string to ascii. Return the number of unconverted bytes. */
70 ka_ConvertBytes(char *ascii, /* output buffer */
71 int alen, /* buffer length */
72 char bs[], /* byte string */
74 { /* number of bytes */
78 alen--; /* make room for termination */
79 for (i = 0; i < bl; i++) {
83 if (isalnum(c) || ispunct(c))
84 (*ascii++ = c), alen--;
89 *ascii++ = (c >> 6) + '0';
90 *ascii++ = (c >> 3 & 7) + '0';
91 *ascii++ = (c & 7) + '0';
95 *ascii = 0; /* terminate string */
96 return 0; /* all OK */
99 /* This is the inverse of the above function. The return value is the number
100 of bytes read. The blen parameter gived the maximum size of the output
104 ka_ReadBytes(char *ascii, char *binary, int blen)
109 while ((i < blen) && *cp) { /* get byte till null or full */
110 if (*cp == '\\') { /* get byte in octal */
112 c = (c << 3) + (*++cp) - '0';
113 c = (c << 3) + (*++cp) - '0';
116 c = *cp++; /* get byte */
123 umin(afs_uint32 a, afs_uint32 b)
131 /* ka_KeyCheckSum - returns a 32 bit cryptographic checksum of a DES encryption
132 * key. It encrypts a block of zeros and uses first 4 bytes as cksum. */
135 ka_KeyCheckSum(char *key, afs_uint32 * cksumP)
144 code = DES_key_sched(charptr_to_cblock(key), &s);
147 DES_ecb_encrypt(&block, &block, &s, ENCRYPT);
148 memcpy(&cksum, &block, sizeof(afs_int32));
149 *cksumP = ntohl(cksum);
153 /* is the key all zeros? */
155 ka_KeyIsZero(char *akey, int alen)
158 for (i = 0; i < alen; i++) {
166 ka_timestr(afs_int32 time, char *tstr, afs_int32 tlen)
168 char tbuffer[32]; /* need at least 26 bytes */
169 time_t passtime; /* modern systems have 64 bit time */
172 strcpy(tstr, "no date"); /* special case this */
173 else if (time == NEVERDATE)
174 strcpy(tstr, "never");
177 strncpy(tstr, afs_ctime(&passtime, tbuffer, sizeof(tbuffer)), tlen);
178 tstr[strlen(tstr) - 1] = '\0'; /* punt the newline character */