-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.c
332 lines (304 loc) · 8.99 KB
/
auth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*-
* Copyright (c) 2003-2007, Petar Zhivkov Petrov <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "auth.h"
#include "config.h"
#include "misc.h"
#include "proto.h"
#include "stream.h"
/* This should be at least 2 * MD5_DIGEST_LENGTH + 6 (length of "$md5$" + 1) */
#define MD5_CHARS_MAX (2*(MD5_DIGEST_LENGTH)+6)
struct srvrecord {
char server[MAXHOSTNAMELEN];
char client[256];
char password[256];
};
static int auth_domd5auth(struct config *);
static int auth_lookuprecord(char *, struct srvrecord *);
static int auth_parsetoken(char **, char *, int);
static void auth_makesecret(struct srvrecord *, char *);
static void auth_makeresponse(char *, char *, char *);
static void auth_readablesum(unsigned char *, char *);
static void auth_makechallenge(struct config *, char *);
static int auth_checkresponse(char *, char *, char *);
int
auth_login(struct config *config)
{
struct stream *s;
char hostbuf[MAXHOSTNAMELEN];
char *login, *host;
int error;
s = config->server;
error = gethostname(hostbuf, sizeof(hostbuf));
hostbuf[sizeof(hostbuf) - 1] = '\0';
if (error)
host = NULL;
else
host = hostbuf;
login = getlogin();
proto_printf(s, "USER %s %s\n", login != NULL ? login : "?",
host != NULL ? host : "?");
stream_flush(s);
error = auth_domd5auth(config);
return (error);
}
static int
auth_domd5auth(struct config *config)
{
struct stream *s;
char *line, *cmd, *challenge, *realm, *client, *srvresp, *msg;
char shrdsecret[MD5_CHARS_MAX], resp[MD5_CHARS_MAX];
char clichallenge[MD5_CHARS_MAX];
struct srvrecord auth;
int error;
s = config->server;
line = stream_getln(s, NULL);
cmd = proto_get_ascii(&line);
realm = proto_get_ascii(&line);
challenge = proto_get_ascii(&line);
if (challenge == NULL || line != NULL || strcmp(cmd, "AUTHMD5") != 0) {
lprintf(-1, "Invalid server reply to USER\n");
return (STATUS_FAILURE);
}
client = NULL;
resp[0] = clichallenge[0] = '.';
resp[1] = clichallenge[1] = 0;
if (config->reqauth || strcmp(challenge, ".") != 0) {
if (strcmp(realm, ".") == 0) {
lprintf(-1, "Authentication required, "
"but not enabled on server\n");
return (STATUS_FAILURE);
}
error = auth_lookuprecord(realm, &auth);
if (error != STATUS_SUCCESS)
return (error);
client = auth.client;
auth_makesecret(&auth, shrdsecret);
}
if (strcmp(challenge, ".") != 0)
auth_makeresponse(challenge, shrdsecret, resp);
if (config->reqauth)
auth_makechallenge(config, clichallenge);
proto_printf(s, "AUTHMD5 %s %s %s\n",
client == NULL ? "." : client, resp, clichallenge);
stream_flush(s);
line = stream_getln(s, NULL);
cmd = proto_get_ascii(&line);
if (cmd == NULL || line == NULL)
goto bad;
if (strcmp(cmd, "OK") == 0) {
srvresp = proto_get_ascii(&line);
if (srvresp == NULL)
goto bad;
if (config->reqauth &&
!auth_checkresponse(srvresp, clichallenge, shrdsecret)) {
lprintf(-1, "Server failed to authenticate itself "
"to client\n");
return (STATUS_FAILURE);
}
return (STATUS_SUCCESS);
}
if (strcmp(cmd, "!") == 0) {
msg = proto_get_rest(&line);
if (msg == NULL)
goto bad;
lprintf(-1, "Server error: %s\n", msg);
return (STATUS_FAILURE);
}
bad:
lprintf(-1, "Invalid server reply to AUTHMD5\n");
return (STATUS_FAILURE);
}
static int
auth_lookuprecord(char *server, struct srvrecord *auth)
{
char *home, *line, authfile[FILENAME_MAX];
struct stream *s;
int linenum = 0, error;
home = getenv("HOME");
if (home == NULL) {
lprintf(-1, "Environment variable \"HOME\" is not set\n");
return (STATUS_FAILURE);
}
snprintf(authfile, sizeof(authfile), "%s/%s", home, AUTHFILE);
s = stream_open_file(authfile, O_RDONLY);
if (s == NULL) {
lprintf(-1, "Could not open file %s\n", authfile);
return (STATUS_FAILURE);
}
while ((line = stream_getln(s, NULL)) != NULL) {
linenum++;
if (line[0] == '#' || line[0] == '\0')
continue;
error = auth_parsetoken(&line, auth->server,
sizeof(auth->server));
if (error != STATUS_SUCCESS) {
lprintf(-1, "%s:%d Missing client name\n",
authfile, linenum);
goto close;
}
/* Skip the rest of this line, it isn't what
we are looking for. */
if (strcasecmp(auth->server, server) != 0)
continue;
error = auth_parsetoken(&line, auth->client,
sizeof(auth->client));
if (error != STATUS_SUCCESS) {
lprintf(-1, "%s:%d Missing password\n",
authfile, linenum);
goto close;
}
error = auth_parsetoken(&line, auth->password,
sizeof(auth->password));
if (error != STATUS_SUCCESS) {
lprintf(-1, "%s:%d Missing comment\n",
authfile, linenum);
goto close;
}
stream_close(s);
lprintf(2, "Found authentication record for server \"%s\"\n",
server);
return (STATUS_SUCCESS);
}
lprintf(-1, "Unknown server \"%s\". Fix your %s\n", server , authfile);
memset(auth->password, 0, sizeof(auth->password));
close:
stream_close(s);
return (STATUS_FAILURE);
}
static int
auth_parsetoken(char **line, char *buf, int len)
{
char *colon;
colon = strchr(*line, ':');
if (colon == NULL)
return (STATUS_FAILURE);
*colon = 0;
buf[len - 1] = 0;
strncpy(buf, *line, len - 1);
*line = colon + 1;
return (STATUS_SUCCESS);
}
static void
auth_makesecret(struct srvrecord *auth, char *secret)
{
char *s, ch;
const char *md5salt = "$md5$";
unsigned char md5sum[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
for (s = auth->client; *s != 0; ++s) {
ch = tolower(*s);
MD5_Update(&md5, &ch, 1);
}
MD5_Update(&md5, ":", 1);
for (s = auth->server; *s != 0; ++s) {
ch = tolower(*s);
MD5_Update(&md5, &ch, 1);
}
MD5_Update(&md5, ":", 1);
MD5_Update(&md5, auth->password, strlen(auth->password));
MD5_Final(md5sum, &md5);
memset(secret, 0, MD5_CHARS_MAX);
strcpy(secret, md5salt);
auth_readablesum(md5sum, secret + strlen(md5salt));
}
static void
auth_makeresponse(char *challenge, char *sharedsecret, char *resp)
{
MD5_CTX md5;
unsigned char md5sum[MD5_DIGEST_LENGTH];
MD5_Init(&md5);
MD5_Update(&md5, sharedsecret, strlen(sharedsecret));
MD5_Update(&md5, ":", 1);
MD5_Update(&md5, challenge, strlen(challenge));
MD5_Final(md5sum, &md5);
auth_readablesum(md5sum, resp);
}
/*
* Generates a challenge string which is an MD5 sum
* of a fairly random string. The purpose is to decrease
* the possibility of generating the same challenge
* string (even by different clients) more then once
* for the same server.
*/
static void
auth_makechallenge(struct config *config, char *challenge)
{
MD5_CTX md5;
unsigned char md5sum[MD5_DIGEST_LENGTH];
char buf[128];
struct timeval tv;
struct sockaddr_in laddr;
pid_t pid, ppid;
socklen_t addrlen;
int error;
gettimeofday(&tv, NULL);
pid = getpid();
ppid = getppid();
srandom(tv.tv_usec ^ tv.tv_sec ^ pid);
addrlen = sizeof(laddr);
error = getsockname(config->socket,
(struct sockaddr *)&laddr, &addrlen);
if (error < 0) {
memset(&laddr, 0, sizeof(laddr));
}
gettimeofday(&tv, NULL);
MD5_Init(&md5);
snprintf(buf, sizeof(buf), "%s:%jd:%ld:%ld:%d:%d",
inet_ntoa(laddr.sin_addr), (intmax_t)tv.tv_sec, tv.tv_usec,
random(), pid, ppid);
MD5_Update(&md5, buf, strlen(buf));
MD5_Final(md5sum, &md5);
auth_readablesum(md5sum, challenge);
}
static int
auth_checkresponse(char *resp, char *challenge, char *secret)
{
char correctresponse[MD5_CHARS_MAX];
auth_makeresponse(challenge, secret, correctresponse);
return (strcmp(resp, correctresponse) == 0);
}
static void
auth_readablesum(unsigned char *md5sum, char *readable)
{
unsigned int i;
char *s = readable;
for (i = 0; i < MD5_DIGEST_LENGTH; ++i, s += 2) {
sprintf(s, "%.2x", md5sum[i]);
}
}