3 * UNFS3 mount password support routines
4 * (C) 2004, Peter Astrand <astrand@cendio.se>
5 * see file LICENSE for license details
10 #include <sys/types.h>
19 #include <sys/times.h> /* times */
22 #include <sys/time.h> /* gettimeofday */
25 #include "daemon.h" /* logmsg */
28 int gen_nonce(char *nonce)
36 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
37 || ((fd = open("/dev/random", O_RDONLY)) != -1)) {
38 bytes_read = read(fd, nonce, 32);
44 /* No /dev/random; do it by hand */
45 arr = (unsigned int *) nonce;
50 arr[3] = times(&tmsbuf);
51 arr[4] = tmsbuf.tms_cutime;
52 arr[5] = tmsbuf.tms_cstime;
53 gettimeofday((struct timeval *) &arr[6], NULL);
56 md5_append(&state, (md5_byte_t *) nonce, 32);
57 md5_finish(&state, (md5_byte_t *) nonce);
62 static char nibble_as_hexchar(unsigned char c)
70 static void hexify(md5_byte_t digest[16], char hexdigest[32])
74 for (i = j = 0; i < 16; i++) {
77 /* The first four bits */
78 c = (digest[i] >> 4) & 0xf;
79 hexdigest[j++] = nibble_as_hexchar(c);
80 /* The next four bits */
81 c = (digest[i] & 0xf);
82 hexdigest[j++] = nibble_as_hexchar(c);
86 /* Handle mount commands:
87 * Advance dpath to first slash
88 * Copy command arguments to arg.
90 void mnt_cmd_argument(char **dpath, const char *cmd, char *arg, size_t maxlen)
94 *dpath += strlen(cmd);
95 strncpy(arg, *dpath, maxlen);
98 slash = strchr(arg, '/');
102 *dpath += strlen(arg);
105 void otp_digest(char nonce[32], char *password, char hexdigest[32])
108 md5_byte_t digest[16];
110 /* Calculate the digest, in the same way as the client did */
112 md5_append(&state, (md5_byte_t *) nonce, 32);
113 md5_append(&state, (md5_byte_t *) password, strlen(password));
114 md5_finish(&state, digest);
115 hexify(digest, hexdigest);