2 * Challenge Response Authentication Method using MD5 (CRAM-MD5)
4 * cram-md5 is based on RFC2104.
6 * Written for Bacula by Kern E. Sibbald, May MMI.
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 int cram_md5_auth(BSOCK *bs, char *password)
40 gettimeofday(&t1, &tz);
42 gettimeofday(&t2, &tz);
43 srandom((t1.tv_sec&0xffff) * (t2.tv_usec&0xff));
44 gethostname(host, sizeof(host));
45 sprintf((char *)chal, "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
46 if (!bnet_fsend(bs, "auth cram-md5 %s\n", chal)) {
49 Dmsg1(99, "%s", bs->msg);
50 if (bnet_wait_data(bs, 120) <= 0 || bnet_recv(bs) <= 0) {
54 hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
55 bin_to_base64(host, (char *)hmac, 16);
56 ok = strcmp(bs->msg, host) == 0;
58 Dmsg3(399, "Authenticate %s: wanted %s, got %s\n",
59 ok ? "OK" : "NOT OK", host, bs->msg);
61 Dmsg3(99, "Authenticate %s: wanted %s, got %s\n",
62 ok ? "OK" : "NOT OK", host, bs->msg);
65 bnet_fsend(bs, "1000 OK auth\n");
67 bnet_fsend(bs, "1999 No auth\n");
73 int cram_md5_get_auth(BSOCK *bs, char *password)
78 if (bnet_recv(bs) <= 0) {
82 if (sscanf(bs->msg, "auth cram-md5 %s", chal) != 1) {
86 hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
87 bs->msglen = bin_to_base64(bs->msg, (char *)hmac, 16);
91 Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
92 if (bnet_wait_data(bs, 120) <= 0 || bnet_recv(bs) <= 0) {
96 if (strcmp(bs->msg, "1000 OK auth\n") == 0) {