File: auth.c

package info (click to toggle)
nd 0.8.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 376 kB
  • ctags: 184
  • sloc: ansic: 2,585; sh: 717; makefile: 62
file content (355 lines) | stat: -rw-r--r-- 6,981 bytes parent folder | download | duplicates (6)
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
/*
 * auth.c - authentication module for nd.
 *
 * Copyright (c) 2002 Yuuichi Teranishi <teranisi@gohome.org>
 * For license terms, see the file COPYING in this directory.
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#include <ctype.h>

#include "nd.h"
#include <libxml/tree.h>

#define SKIP_BLANKS(p) {while(*(p)&&isspace(*(p)))(p)++;}

struct http_auth
{
  char            *name;
  ndAuthParamPtr (*param_fn) (void);
  xmlBufferPtr   (*auth_fn) (ndAuthParamPtr param);
};

ndAuthParamPtr ndAuthParamCreateBasic ();
ndAuthParamPtr ndAuthParamCreateDigest ();
xmlBufferPtr ndAuthBasic (ndAuthParamPtr param);
xmlBufferPtr ndAuthDigest (ndAuthParamPtr param);

struct http_auth www_auth [] =
  {
    {"Basic", ndAuthParamCreateBasic, ndAuthBasic},
    {"Digest", ndAuthParamCreateDigest, ndAuthDigest},
    {NULL, NULL}
  };

/* util for convinience */
void
xmlBufferAddChar (buf, ch)
     xmlBufferPtr buf;
     char ch;
{
  char str[2];
  str [0] = ch;
  str [1] = '\0';
  xmlBufferAdd(buf, str, 1);
}

char *
nd_extract_auth_val (char **q)
{
    unsigned char *qq = *(unsigned char **)q;
    int quoted = 0;
    xmlBufferPtr val = xmlBufferCreate ();
    char *ret;

    SKIP_BLANKS(qq);
    if (*qq == '"')
      {
	quoted = 1;
	xmlBufferAddChar (val, *qq++);
      }
    while (*qq != '\0')
      {
	if (quoted && *qq == '"')
	  {
	    xmlBufferAddChar (val, *qq++);
	    break;
	  }
	if (!quoted)
	  {
	    switch (*qq)
	      {
	      case '(':
	      case ')':
	      case '<':
	      case '>':
	      case '@':
	      case ',':
	      case ';':
	      case ':':
	      case '\\':
	      case '"':
	      case '/':
	      case '?':
	      case '=':
	      case ' ':
	      case '\t':
		qq++;
		goto end_token;
	      default:
		if (*qq <= 037 || *qq == 177)
		  {
		    qq++;
		    goto end_token;
		  }
	      }	
	  }
	else if (quoted && *qq == '\\')
	  xmlBufferAddChar (val, *qq++);
	xmlBufferAddChar (val, *qq++);
      }
 end_token:
    if (*qq != '\0')
      {
	SKIP_BLANKS(qq);
	if (*qq == ',')
	  qq++;
      }
    *q = qq;
    ret = (char *) xmlBufferContent(val);
    xmlFree (val);
    return ret;
}

ndAuthParamPtr
ndAuthParamCreate (hauth, p)
     struct http_auth *hauth;
     char *p;
{
  ndAuthParamPtr param, ap;

  /* Init Param */
  param = hauth->param_fn ();
  while (*p != '\0') {
    SKIP_BLANKS(p);
    for (ap = param; ap->name != NULL; ap++)
      {
	if (strncasecmp(p, ap->name, strlen(ap->name)) == 0)
	  {
	    p += strlen (ap->name);
	    SKIP_BLANKS(p);
	    if (*p != '=')
	      return NULL;
	    p++;
	    ap->val = nd_extract_auth_val (&p);
	    break;
	  }
      }
  }
  return param;
}

ndAuthParamPtr
ndAuthParamCreateBasic ()
{
  ndAuthParamPtr param = xmlMalloc (sizeof (ndAuthParam) * 5);

  param[0].name  = "name";
  param[0].val   = xmlMemStrdup ("Basic");
  param[1].name  = "realm";
  param[1].val   = NULL;
  param[2].name  = "user";
  param[2].val   = NULL;
  param[3].name  = "password";
  param[3].val   = NULL;
  param[4].name  = NULL;
  param[4].val   = NULL;

  return param;
}

void
ndAuthParamFree (auth_param)
     ndAuthParamPtr auth_param;
{
  ndAuthParamPtr ap;
  for (ap = auth_param; ap->name != NULL; ap++)
    {
      if (ap->val != NULL)
	xmlFree (ap->val);
    }
  xmlFree (auth_param);
}

char *
ndAuthParamValue (param, name)
     ndAuthParamPtr param;
     char *name;
{
  ndAuthParamPtr ap;
  for (ap = param; ap->name != NULL; ap++)
    {
      if (!strcmp (ap->name, name))
	return ap->val;
    }
  return NULL;
}

int
ndAuthParamSetValue (param, name, val)
     ndAuthParamPtr param;
     char *name;
     char *val;
{
  ndAuthParamPtr ap;
  for (ap = param; ap->name != NULL; ap++)
    {
      if (!strcmp (ap->name, name))
	{
	  if (ap->val != NULL)
	    xmlFree (ap->val);
	  ap->val = xmlMemStrdup (val);
	  return 0;
	}
    }
  return -1;
}

ndAuthParamPtr
ndAuthParamCreateDigest ()
{
  /* Not Implemented */
  return NULL;
}

/* Delived from mimehead.c */
static char Base64Table[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

xmlBufferPtr
ndAuthEncodeB(char *a)
{
    unsigned char d[3];
    unsigned char c1, c2, c3, c4;
    int i, n_pad;
    xmlBufferPtr w = xmlBufferCreate ();

    while (1) {
	if (*a == '\0')
	    break;
	n_pad = 0;
	d[1] = d[2] = 0;
	for (i = 0; i < 3; i++) {
	    d[i] = a[i];
	    if (a[i] == '\0') {
		n_pad = 3 - i;
		break;
	    }
	}
	c1 = d[0] >> 2;
	c2 = (((d[0] << 4) | (d[1] >> 4)) & 0x3f);
	if (n_pad == 2) {
	    c3 = c4 = 64;
	}
	else if (n_pad == 1) {
	    c3 = ((d[1] << 2) & 0x3f);
	    c4 = 64;
	}
	else {
	    c3 = (((d[1] << 2) | (d[2] >> 6)) & 0x3f);
	    c4 = (d[2] & 0x3f);
	}
	xmlBufferAddChar (w, Base64Table[c1]);
	xmlBufferAddChar (w, Base64Table[c2]);
	xmlBufferAddChar (w, Base64Table[c3]);
	xmlBufferAddChar (w, Base64Table[c4]);
	if (n_pad)
	    break;
	a += 3;
    }
    return w;
}

xmlBufferPtr
ndAuthBasic (param)
     ndAuthParamPtr param;
{
  xmlBufferPtr buf = xmlBufferCreate ();
  xmlBufferAdd (buf, ndAuthParamValue (param, "user"), -1);
  xmlBufferAdd (buf, ":", -1);
  xmlBufferAdd (buf, ndAuthParamValue (param, "password"), -1);
  return ndAuthEncodeB ((char *) xmlBufferContent (buf));
}

xmlBufferPtr
ndAuthDigest (param)
     ndAuthParamPtr param;
{
  return NULL;
}

int
ndAuthCreateHeader (str, fn, buf_return, is_proxy)
     char *str;
     ndAuthCallback fn;
     xmlBufferPtr *buf_return;
     int is_proxy;
{
  char *p;
  struct http_auth *ha;
  struct http_auth *hauth = NULL;
  ndAuthParamPtr param = NULL;

  p = str;
  for (ha = &www_auth[0]; ha->name != NULL; ha++)
    {
      if (strncasecmp (p, ha->name, strlen (ha->name)) == 0)
	{
	  hauth = ha;
	  p += strlen (ha->name);
	  SKIP_BLANKS(p);
	}
    }
  if (hauth != NULL)
    {
      param = ndAuthParamCreate (hauth, p);
    }
  if (param && !(fn (param, is_proxy)))
    {
      xmlBufferPtr tmp;
      xmlBufferPtr ret = xmlBufferCreate ();

      xmlBufferAdd (ret,
		    is_proxy? "Proxy-Authorization:" : "Authorization: ",
		    -1);
      xmlBufferAdd (ret, ndAuthParamValue (param, "name"), -1);
      xmlBufferAdd (ret, " ", -1);
      tmp = hauth->auth_fn (param);
      ndAuthParamFree (param);
      xmlBufferAdd (ret, xmlBufferContent (tmp), -1);
      xmlBufferAdd (ret, "\r\n", -1);
      *buf_return = ret;
      return 0;
    }
  return -1;
}

ndAuthCtxtPtr
ndCreateAuthCtxt (auth_cb, notify_cb, auth_realm, pauth_realm)
     ndAuthCallback auth_cb;
     ndAuthNotifyCallback notify_cb;
     char *auth_realm;
     char *pauth_realm;
{
  ndAuthCtxtPtr auth = xmlMalloc (sizeof (ndAuthCtxt));
  if (auth == NULL) return NULL;
  memset (auth, 0, sizeof (ndAuthCtxt));
  auth->auth_cb = auth_cb;
  auth->notify_cb = notify_cb;
  auth->auth_realm = auth_realm;
  auth->pauth_realm = pauth_realm;
  return auth;
}

void
ndFreeAuthCtxt (auth)
     ndAuthCtxtPtr auth;
{
  if (auth == NULL) return;
  xmlFree (auth);
}