File: auth.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (430 lines) | stat: -rw-r--r-- 8,966 bytes parent folder | download
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 *	auth.c
 *
 *	authentication for FTP/HTTP connections in RFC2617
 */

#include "headers.h"
#include "depend.h"
#include "printf.h"
#include "string.h"
#include "malloc.h"
#include "time.h"
#include "pathname.h"
#include "encode.h"
#include "parse.h"
#include "socket.h"
#include "auth.h"

#ifdef	MINIX
#include <minix/minlib.h>
#endif
#ifdef	FDSH
#include "sysemu.h"
#endif
#ifdef	DEP_ORIGSHELL
#include "system.h"
#endif
#ifdef	FD
#include "kanji.h"
#endif

#define	REALMSTR		"realm"
#define	NONCESTR		"nonce"
#define	ALGORITHMSTR		"algorithm"
#define	QOPSTR			"qop"
#define	OPAQUESTR		"opaque"
#define	MD5_STRSIZ		(MD5_BUFSIZ * 4 * 2)

typedef struct _digest_t {
	char *realm;
	char *nonce;
	char *algorithm;
	char *qop;
	char *opaque;
	char *cnonce;
} digest_t;

#ifdef	DEP_URLPATH

#ifdef	FD
extern VOID ttyiomode __P_((int));
extern VOID stdiomode __P_((VOID_A));
extern char *inputstr __P_((CONST char *, int, int, CONST char *, int));
extern char *inputpass __P_((VOID_A));
#endif

#ifdef	FD
extern int isttyiomode;
#endif

static char **NEAR getdigestfield __P_((CONST char *));
static VOID NEAR getdigestval __P_((char **, CONST char *, CONST char *));
static VOID NEAR freedigest __P_((digest_t *));
static int NEAR genhash __P_((char *, ALLOC_T, CONST char *, ...));
static int NEAR catprintf __P_((char **, int, CONST char *, ...));

static auth_t authlist[AUTHNOFILE];
static int maxauth = 0;


char *authgetuser(VOID_A)
{
#ifdef	FD
	int wastty;
#endif
	char *cp;

#ifdef	FD
	if (!(wastty = isttyiomode)) ttyiomode(1);
	cp = inputstr(USER_K, 1, -1, NULL, -1);
	if (!wastty) stdiomode();
#else
	cp = gets2("User: ");
#endif

	if (cp && !*cp) {
		Xfree(cp);
		cp = NULL;
	}

	return(cp);
}

char *authgetpass(VOID_A)
{
	char *cp;

#ifdef	FD
	cp = inputpass();
#else	/* !FD */
# ifdef	NOGETPASS
	VOID_C Xfputs("Password:", Xstderr);
	VOID_C Xfflush(Xstderr);
	if (!(cp = Xfgets(Xstdin))) cp = Xstrdup(nullstr);
# else
	if (!(cp = getpass("Password:"))) cp = vnullstr;
	cp = Xstrdup(cp);
# endif
#endif	/* !FD */

	return(cp);
}

int authfind(hp, type, index)
urlhost_t *hp;
int type, index;
{
	int i;

	if (index < 0 || index > maxauth) index = maxauth;
	for (i = index - 1; i >= 0; i--) {
		if (type != authlist[i].type) continue;
		if (hp -> port != authlist[i].host.port) continue;
		if (cmpsockaddr(hp -> host, authlist[i].host.host)) continue;
		if (!(hp -> user)) hp -> user = Xstrdup(authlist[i].host.user);
		else if (strcmp(hp -> user, authlist[i].host.user)) continue;

		Xfree(hp -> pass);
		hp -> pass = Xstrdup(authlist[i].host.pass);
		break;
	}

	return(i);
}

VOID authentry(hp, type)
urlhost_t *hp;
int type;
{
	auth_t tmp;
	int i;

	if (!hp || !(hp -> user) || !(hp -> pass)) return;

	for (i = maxauth - 1; i >= 0; i--) {
		if (type != authlist[i].type) continue;
		if (hp -> port != authlist[i].host.port) continue;
		if (cmpsockaddr(hp -> host, authlist[i].host.host)) continue;
		if (strcmp(hp -> user, authlist[i].host.user)) continue;
		if (strcmp(hp -> pass, authlist[i].host.pass)) continue;
		break;
	}

	if (i >= 0) {
		if (i == maxauth - 1) return;
		memcpy((char *)&tmp, (char *)&(authlist[i]), sizeof(tmp));
		memmove((char *)&(authlist[i]),
			(char *)&(authlist[i + 1]),
			(maxauth - i - 1) * sizeof(*authlist));
		memcpy((char *)&(authlist[maxauth - 1]),
			(char *)&tmp, sizeof(tmp));
		return;
	}

	if (maxauth < AUTHNOFILE) i = maxauth++;
	else {
		urlfreehost(&(authlist[0].host));
		memmove((char *)&(authlist[0]),
			(char *)&(authlist[1]),
			(maxauth - 1) * sizeof(*authlist));
		i = maxauth - 1;
	}

	authlist[i].host.user = Xstrdup(hp -> user);
	authlist[i].host.pass = Xstrdup(hp -> pass);
	authlist[i].host.host = Xstrdup(hp -> host);
	authlist[i].host.port = hp -> port;
	authlist[i].type = type;
}

VOID authfree(VOID_A)
{
	int i;

	for (i = 0; i < maxauth; i++) urlfreehost(&(authlist[i].host));
	maxauth = 0;
}

static char **NEAR getdigestfield(s)
CONST char *s;
{
	CONST char *cp;
	char **argv;
	int quote, argc;

	argv = (char **)Xmalloc(sizeof(*argv));
	argc = 0;

	for (;;) {
		s = skipspace(s);
		if (!*s) break;
		argv = (char **)Xrealloc(argv, (argc + 2) * sizeof(*argv));
		quote = '\0';
		for (cp = s; *cp; cp++) {
			if (*cp == quote) quote = '\0';
			else if (quote) /*EMPTY*/;
			else if (*cp == '"') quote = *cp;
			else if (*cp == ',') break;
		}
		argv[argc++] = Xstrndup(s, cp - s);
		if (*cp) cp++;
		s = cp;
	}
	argv[argc] = NULL;

	return(argv);
}

static VOID NEAR getdigestval(valp, s, name)
char **valp;
CONST char *s, *name;
{
	CONST char *cp;
	ALLOC_T len;
	int quote;

	if (!valp || *valp) return;
	len = strlen(name);
	if (Xstrncasecmp(s, name, len)) return;
	s += len;
	if (*(s++) != '=') return;

	quote = (*s == '"') ? *(s++) : '\0';
	for (cp = s; *cp; cp++) if (*cp == quote) break;

	*valp = Xstrndup(s, cp - s);
}

static VOID NEAR freedigest(dp)
digest_t *dp;
{
	int duperrno;

	if (!dp) return;

	duperrno = errno;
	Xfree(dp -> realm);
	Xfree(dp -> nonce);
	Xfree(dp -> algorithm);
	Xfree(dp -> qop);
	Xfree(dp -> opaque);
	Xfree(dp -> cnonce);
	errno = duperrno;
}

#ifdef	USESTDARGH
/*VARARGS3*/
static int NEAR genhash(char *buf, ALLOC_T size, CONST char *fmt, ...)
#else
/*VARARGS3*/
static int NEAR genhash(buf, size, fmt, va_alist)
char *buf;
ALLOC_T size;
CONST char *fmt;
va_dcl
#endif
{
	va_list args;
	u_char md5[MD5_BUFSIZ * 4];
	char *cp;
	ALLOC_T ptr, len;
	int n;

	VA_START(args, fmt);
	n = Xvasprintf(&cp, fmt, args);
	va_end(args);
	if (n < 0) return(-1);

	len = sizeof(md5);
	md5encode(md5, &len, (u_char *)cp, (ALLOC_T)-1);
	Xfree(cp);
	cp = buf;
	*buf = '\0';
	for (ptr = (ALLOC_T)0; ptr < len; ptr++) {
		n = Xsnprintf(cp, size, "%<02x", (int)(md5[ptr]));
		if (n < 0) break;
		cp += n;
		size -= n;
	}

	return(0);
}

#ifdef	USESTDARGH
/*VARARGS3*/
static int NEAR catprintf(char **sp, int len, CONST char *fmt, ...)
#else
/*VARARGS3*/
static int NEAR catprintf(sp, len, fmt, va_alist)
char **sp;
int len;
CONST char *fmt;
va_dcl
#endif
{
	va_list args;
	char *cp;
	int n;

	if (len < 0) return(-1);
	VA_START(args, fmt);
	n = Xvasprintf(&cp, fmt, args);
	va_end(args);
	if (n < 0) {
		Xfree(*sp);
		*sp = NULL;
		return(-1);
	}

#ifdef	CODEEUC
	n = strlen(cp);
#endif
	*sp = Xrealloc(*sp, len + n + 1);
	memcpy(&((*sp)[len]), cp, n);
	len += n;
	(*sp)[len] = '\0';
	Xfree(cp);

	return(len);
}

char *authencode(hp, digest, method, path)
urlhost_t *hp;
CONST char *digest, *method, *path;
{
	static int count = 0;
	digest_t auth;
	char *cp, *buf, **argv, res[MD5_STRSIZ + 1];
	char a1[MD5_STRSIZ + 1], a2[MD5_STRSIZ + 1], cnonce[MD5_STRSIZ + 1];
	ALLOC_T size;
	int n;

	if (!digest) {
		n = Xasprintf(&cp, "%s:%s", hp -> user, hp -> pass);
		if (n < 0) return(NULL);
#ifdef	CODEEUC
		n = strlen(cp);
#endif
		size = ((n - 1) / BASE64_ORGSIZ + 1)
			* BASE64_ENCSIZ;
		buf = Xmalloc(strsize(AUTHBASIC) + 1 + size + 1);
		memcpy(buf, AUTHBASIC, strsize(AUTHBASIC));
		buf[strsize(AUTHBASIC)] = ' ';
		n = base64encode(&(buf[strsize(AUTHBASIC) + 1]), size + 1,
			(u_char *)cp, n);
		Xfree(cp);
		if (n < 0) {
			Xfree(buf);
			return(NULL);
		}
		return(buf);
	}

	argv = getdigestfield(digest);
	auth.realm = auth.nonce =
	auth.algorithm = auth.qop = auth.opaque = auth.cnonce = NULL;
	for (n = 0; argv[n]; n++) {
		getdigestval(&(auth.realm), argv[n], REALMSTR);
		getdigestval(&(auth.nonce), argv[n], NONCESTR);
		getdigestval(&(auth.algorithm), argv[n], ALGORITHMSTR);
		getdigestval(&(auth.qop), argv[n], QOPSTR);
		getdigestval(&(auth.opaque), argv[n], OPAQUESTR);
	}
	freevar(argv);

	if (!auth.realm || !auth.nonce) {
		freedigest(&auth);
		errno = EINVAL;
		return(NULL);
	}

	n = genhash(a1, sizeof(a1),
		"%s:%s:%s", hp -> user, auth.realm, hp -> pass);
	if (n < 0) {
		freedigest(&auth);
		return(NULL);
	}
	n = genhash(a2, sizeof(a2), "%s:%s", method, path);
	if (n < 0) {
		freedigest(&auth);
		return(NULL);
	}
	if (!auth.qop)
		n = genhash(res, sizeof(res), "%s:%s:%s", a1, auth.nonce, a2);
	else {
		n = genhash(cnonce, sizeof(cnonce),
			"%s:%d", hp -> host, Xtime(NULL));
		if (n < 0) {
			freedigest(&auth);
			return(NULL);
		}
		n = genhash(res, sizeof(res), "%s:%s:%08x:%s:%s:%s",
			a1, auth.nonce, ++count, cnonce, auth.qop, a2);
	}
	if (n < 0) {
		freedigest(&auth);
		return(NULL);
	}

	buf = NULL;
	n = 0;
	n = catprintf(&buf, n, "%s username=\"%s\"", AUTHDIGEST, hp -> user);
	n = catprintf(&buf, n, ", %s=\"%s\"", REALMSTR, auth.realm);
	n = catprintf(&buf, n, ", %s=\"%s\"", NONCESTR, auth.nonce);
	n = catprintf(&buf, n, ", uri=\"%s\"", path);
	if (auth.algorithm) n = catprintf(&buf, n,
		", %s=\"%s\"", ALGORITHMSTR, auth.algorithm);
	if (auth.qop) {
		n = catprintf(&buf, n, ", %s=\"%s\"", QOPSTR, auth.qop);
		n = catprintf(&buf, n, ", nc=%08x", count);
		n = catprintf(&buf, n, ", cnonce=\"%s\"", cnonce);
	}
	if (auth.opaque) n = catprintf(&buf, n,
		", %s=\"%s\"", OPAQUESTR, auth.opaque);
	n = catprintf(&buf, n, ", response=\"%s\"", res);
	freedigest(&auth);

	return(buf);
}
#endif	/* DEP_URLPATH */