File: auth.c

package info (click to toggle)
nd 0.8.2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,164 kB
  • ctags: 377
  • sloc: ansic: 7,810; sh: 717; makefile: 392
file content (512 lines) | stat: -rw-r--r-- 11,854 bytes parent folder | download | duplicates (3)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * 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 <time.h>

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

#include <mhash.h>

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

extern const char *export_method;
extern const char *export_uri;

static int nc = 0;
static char nonce_sensor[64] = "";

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);

xmlBufferPtr ndAuthEncodeDigest (xmlBufferPtr a);

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

/* utility for convenience */
void
xmlBufferAddChar (buf, ch)
     xmlBufferPtr buf;
     char ch;
{
  char str[2];
  str [0] = ch;
  str [1] = '\0';
  xmlBufferAdd(buf, (xmlChar *) 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;
	++qq;
      }
    while (*qq != '\0')
      {
	if (quoted && *qq == '"')
	  {
	    ++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 == 0177)
		  {
		    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 = (char *) 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 ();

  if ( param == NULL )
    return NULL;

  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);
	    if (*p == ',')
	      ++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 ()
{
  ndAuthParamPtr param = xmlMalloc (sizeof (ndAuthParam) * 13);
  xmlBufferPtr cnonce = xmlBufferCreate ();
  char timestring[12];

  param[0].name  = "name";
  param[0].val   = xmlMemStrdup ("Digest");
  param[1].name  = "realm";
  param[1].val   = NULL;
  param[2].name  = "domain";
  param[2].val   = NULL;
  param[3].name  = "nonce";
  param[3].val   = NULL;
  param[4].name  = "opaque";
  param[4].val   = NULL;
  param[5].name  = "stale";
  param[5].val   = NULL;
  param[6].name  = "algorithm";
  param[6].val   = NULL;
  param[7].name  = "qop";
  param[7].val   = NULL;
  param[8].name  = "user";
  param[8].val   = NULL;
  param[9].name  = "password";
  param[9].val   = NULL;
  param[10].name  = "uri";
  param[10].val   = NULL;

  param[11].name  = "cnonce";
  snprintf (timestring, sizeof (timestring), "%d", time (0));
  xmlBufferAdd (cnonce, (xmlChar *) timestring, -1);
  xmlBufferAdd (cnonce, (xmlChar *) ":123e5d7ff:nd", -1);
  param[11].val   = (char *) xmlBufferContent (ndAuthEncodeDigest (cnonce));
  xmlBufferFree (cnonce);

  param[12].name  = NULL;
  param[12].val   = NULL;

  return param;
}

/* 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
ndAuthEncodeDigest (xmlBufferPtr a)
{
  int j;
  char octet[3];
  unsigned char hash[16];
  MHASH context;
  xmlBufferPtr w = xmlBufferCreate ();

  context = mhash_init (MHASH_MD5);
  mhash (context, (char *) xmlBufferContent (a), xmlBufferLength (a));
  mhash_deinit (context, hash);
  for (j = 0; j < sizeof (hash); ++j) {
    snprintf (octet, sizeof (octet), "%02x", hash[j]);
    xmlBufferAdd (w, octet, strlen (octet));
  }

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

void
add_quoted_param (buf, param, name_str, param_name, flag)
     xmlBufferPtr buf;
     ndAuthParamPtr param;
     const char * name_str;
     char * param_name;
     int flag;
{
  if (flag)
    xmlBufferAdd (buf, (xmlChar *) ", ", -1);

  xmlBufferAdd (buf, (xmlChar *) name_str, -1);
  xmlBufferAdd (buf, (xmlChar *) "=\"", -1);
  xmlBufferAdd (buf, (xmlChar *) ndAuthParamValue (param, param_name), -1);
  xmlBufferAdd (buf, (xmlChar *) "\"", -1);
}

xmlBufferPtr
ndAuthDigest (param)
     ndAuthParamPtr param;
{
  char nc_string[9], *sensor;
  xmlBufferPtr buf1 = xmlBufferCreate ();
  xmlBufferPtr A1 = xmlBufferCreate ();
  xmlBufferPtr A2 = xmlBufferCreate ();
  xmlBufferPtr response;

  /* Identify the authenticated udr. */
  xmlBufferAdd (A1, (xmlChar *) ndAuthParamValue (param, "user"), -1);
  xmlBufferAdd (A1, (xmlChar *) ":", -1);
  xmlBufferAdd (A1, (xmlChar *) ndAuthParamValue (param, "realm"), -1);
  xmlBufferAdd (A1, (xmlChar *) ":", -1);
  xmlBufferAdd (A1, (xmlChar *) ndAuthParamValue (param, "password"), -1);

  response = ndAuthEncodeDigest (A1);

  xmlBufferAdd (A2, (xmlChar *) export_method, -1);
  xmlBufferAdd (A2, (xmlChar *) ":", -1);
  xmlBufferAdd (A2, (xmlChar *) export_uri, -1);

  add_quoted_param (buf1, param, "username", "user", 0);

  add_quoted_param (buf1, param, "realm", "realm", 1);

  add_quoted_param (buf1, param, "nonce", "nonce", 1);

  xmlBufferAdd (response, (xmlChar *) ":", -1);
  xmlBufferAdd (response, (xmlChar *) ndAuthParamValue (param, "nonce"), -1);

  /* Check if 'nonce' has changed. If so, increment 'nonce_count'. */
  if ( strncmp (nonce_sensor, ndAuthParamValue (param, "nonce"),
		sizeof (nonce_sensor)) ) {
    ++nc;
    strncpy (nonce_sensor, ndAuthParamValue (param, "nonce"),
		sizeof (nonce_sensor));
  }

  xmlBufferAdd (buf1, (xmlChar *) ", uri=\"", -1);
  xmlBufferAdd (buf1, (xmlChar *) export_uri, -1);
  xmlBufferAdd (buf1, (xmlChar *) "\"", -1);

  xmlBufferAdd (buf1, (xmlChar *) ", algorithm=\"MD5\"", -1);

  /* Use the new nonce_counter. */
  snprintf (nc_string, sizeof (nc_string), "%08x", nc);
  xmlBufferAdd (buf1, (xmlChar *) ", qop=\"auth\", nc=", -1);
  xmlBufferAdd (response, (xmlChar *) ":", -1);

  xmlBufferAdd (buf1, (xmlChar *) nc_string, -1);
  xmlBufferAdd (response, (xmlChar *) nc_string, -1);

  add_quoted_param (buf1, param, "cnonce", "cnonce", 1);

  xmlBufferAdd (response, (xmlChar *) ":", -1);
  xmlBufferAdd (response, (xmlChar *) ndAuthParamValue (param, "cnonce"), -1);
  xmlBufferAdd (response, (xmlChar *) ":auth:", -1);
  xmlBufferAdd (response, xmlBufferContent (ndAuthEncodeDigest (A2)), -1);

  xmlBufferFree (A2); /* Used inside 'response'. */

  xmlBufferAdd (buf1, (xmlChar *) ", response=\"", -1);
  xmlBufferAdd (buf1, xmlBufferContent (ndAuthEncodeDigest (response)), -1);
  xmlBufferAdd (buf1, (xmlChar *) "\"", -1);
  xmlBufferFree (response);

  if (ndAuthParamValue (param, "opaque"))
    add_quoted_param (buf1, param, "opaque", "opaque", 1);

  return buf1;
}

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 ? (xmlChar *) "Proxy-Authorization:"
                             : (xmlChar *) "Authorization: ",
                    -1);
      xmlBufferAdd (ret, (xmlChar *) ndAuthParamValue (param, "name"), -1);
      xmlBufferAdd (ret, (xmlChar *) " ", -1);
      tmp = hauth->auth_fn (param);
      ndAuthParamFree (param);
      xmlBufferAdd (ret, xmlBufferContent (tmp), -1);
      xmlBufferAdd (ret, (xmlChar *) "\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);
}