summaryrefslogtreecommitdiff
path: root/libmailutils/url/create.c
blob: 182b8bd9ec526e05302a3f3a96002e159fad25eb (plain)
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2012, 2014-2015 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see 
   <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <limits.h>

#include <mailutils/wordsplit.h>
#include <mailutils/util.h>
#include <mailutils/errno.h>
#include <mailutils/argcv.h>
#include <mailutils/secret.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/url.h>

struct mu_url_ctx
{
  int flags;
  const char *input;
  const char *cur;
  mu_url_t url;

  size_t passoff;
  
  char *tokbuf;
  size_t toksize;
  size_t toklen;
};

static int
getkn (struct mu_url_ctx *ctx, char *delim)
{
  size_t n;

  if (*ctx->cur == 0)
    return MU_ERR_PARSE;
  n = strcspn (ctx->cur, delim);
  if (n + 1 > ctx->toksize)
    {
      char *p = realloc (ctx->tokbuf, n + 1);
      if (!p)
	return ENOENT;
      ctx->toksize = n + 1;
      ctx->tokbuf = p;
    }
  memcpy (ctx->tokbuf, ctx->cur, n);
  ctx->tokbuf[n] = 0;
  ctx->toklen = n;
  ctx->cur += n;
  return 0;
}

#define INIT_ARRAY_SIZE 16

static int
expand_array (size_t *pwc, char ***pwv, int incr)
{
  size_t wc = *pwc;
  char **wv = *pwv;

  if (!wv)
    {
      wv = calloc (INIT_ARRAY_SIZE, sizeof (wv[0]));
      wc = INIT_ARRAY_SIZE;
    }
  else
    {
      if (incr)
	wc += incr;
      else
	{
	  size_t newsize = wc * 2;
	  if (newsize < wc)
	    return ENOMEM;
	  wc = newsize;
	}
      wv = realloc (wv, sizeof (wv[0]) * wc);
    }
  if (!wv)
    return ENOMEM;
  *pwv = wv;
  *pwc = wc;
  return 0;
}
  
static int
parse_param (struct mu_url_ctx *ctx, char *delim, int *pargc, char ***pargv)
{
  int rc;
  size_t wc = 0, wn = 0;
  char **wv = NULL;

  while ((rc = getkn (ctx, delim)) == 0)
    {
      if (wn == wc)
	{
	  rc = expand_array (&wc, &wv, 0);
	  if (rc)
	    break;
	}
      wv[wn] = strdup (ctx->tokbuf);
      if (!wv[wn])
	{
	  rc = ENOMEM;
	  break;
	}
      wn++;
      if (*ctx->cur != delim[0])
	break;
      ctx->cur++;
    }

  if (rc == 0)
    {
      if (wn == wc)
	{
	  rc = expand_array (&wc, &wv, 1);
	  if (rc)
	    {
	      mu_argcv_free (wc, wv);
	      return ENOMEM;
	    }
	  wv[wn] = NULL;
	}

      *pargv = realloc (wv, sizeof (wv[0]) * (wn + 1));
      *pargc = wn;
    }
  else
    mu_argcv_free (wc, wv);

  return rc;
}

static int
_mu_url_ctx_parse_query (struct mu_url_ctx *ctx)
{
  int rc;
  
  ctx->cur++;
  rc = parse_param (ctx, "&", &ctx->url->qargc, &ctx->url->qargv);
  if (rc == 0 && ctx->url->qargc)
    ctx->url->flags |= MU_URL_QUERY;
  return rc;
}

static int
_mu_url_ctx_parse_param (struct mu_url_ctx *ctx)
{
  int rc;
  
  ctx->cur++;
  rc = parse_param (ctx, ";?", &ctx->url->fvcount, &ctx->url->fvpairs);
  if (rc)
    return rc;
  if (ctx->url->fvcount)
    ctx->url->flags |= MU_URL_PARAM;
  if (*ctx->cur == '?')
    return _mu_url_ctx_parse_query (ctx);
  return 0;
}

static int
str_assign (char **ptr, const char *str)
{
  *ptr = strdup (str);
  if (!*ptr)
    return ENOMEM;
  return 0;
}

static int
_mu_url_ctx_parse_path (struct mu_url_ctx *ctx)
{
  int rc;
  mu_url_t url = ctx->url;
  
  rc = getkn (ctx, ";?");
  if (rc)
    return rc;
  rc = str_assign (&url->path, ctx->tokbuf);
  if (rc == 0)
    url->flags |= MU_URL_PATH;
  if (*ctx->cur == ';')
    return _mu_url_ctx_parse_param (ctx);
  if (*ctx->cur == '?')
    return _mu_url_ctx_parse_query (ctx);
  return 0;
}

static int
_mu_url_ctx_parse_host (struct mu_url_ctx *ctx, int has_host)
{
  int rc;
  mu_url_t url = ctx->url;
  
  if (ctx->flags & MU_URL_PARSE_LOCAL)
    return _mu_url_ctx_parse_path (ctx);
  
  rc = getkn (ctx, "[:/;?");
  if (rc)
    return rc;

  if (*ctx->cur == '[')
    {
      /* Possibly IPv6 address */
      rc = getkn (ctx, "]/;?");
      if (rc)
	return rc;
      if (*ctx->cur == ']')
	{
	  ctx->cur++;
	  rc = str_assign (&url->host, ctx->tokbuf + 1);
	  if (rc)
	    return rc;
	  url->flags |= MU_URL_HOST | MU_URL_IPV6;
	  has_host = 1;
	}
    }

  if (!(url->flags & MU_URL_HOST) && ctx->toklen)
    {
      rc = str_assign (&url->host, ctx->tokbuf);
      if (rc)
	return rc;
      url->flags |= MU_URL_HOST;
      has_host = 1;
    }
  
  if (*ctx->cur == ':')
    {
      has_host = 1;
      ctx->cur++;
      rc = getkn (ctx, ":/;?");
      if (rc)
	return rc;
      rc = str_assign (&url->portstr, ctx->tokbuf);
      if (rc)
	return rc;
      url->flags |= MU_URL_PORT;
    }
    
  if (*ctx->cur == '/')
    {
      if (has_host)
	ctx->cur++;
      return _mu_url_ctx_parse_path (ctx);
    }
  
  if (*ctx->cur == ';')
    return _mu_url_ctx_parse_param (ctx);

  if (*ctx->cur == '?')
    return _mu_url_ctx_parse_query (ctx);
  return 0;
}

static int
_mu_url_ctx_parse_cred (struct mu_url_ctx *ctx)
{
  int rc, has_cred;
  mu_url_t url = ctx->url;
  const char *save = ctx->cur;

  if (*ctx->cur == 0)
    return 0;
  rc = getkn (ctx, "@");
  if (rc)
    return rc;
  has_cred = *ctx->cur == '@';
  /* restore the pointer */
  ctx->cur = save;  
  if (has_cred)
    {
      /* Try to split the user into a:
	 <user>:<password>
	 or
	 <user>:<password>;AUTH=<auth>
      */
      rc = getkn (ctx, ":;@");
      if (rc)
	return rc;

      if (ctx->toklen)
	{
	  rc = str_assign (&url->user, ctx->tokbuf);
	  if (rc)
	    return rc;
	  url->flags |= MU_URL_USER;
	}
      
      if (*ctx->cur == ':')
	{
	  ctx->cur++;
	  ctx->passoff = ctx->cur - ctx->input;
	  
	  rc = getkn (ctx, ";@");
	  if (rc)
	    return rc;

	  if (ctx->toklen)
	    {
	      if (mu_secret_create (&url->secret, ctx->tokbuf, ctx->toklen))
		return ENOMEM;
	      else
		/* Clear password */
		memset (ctx->tokbuf, 0, ctx->toklen);
	      url->flags |= MU_URL_SECRET;
	    }
	}
      if (*ctx->cur == ';')
	{
	  ctx->cur++;

	  rc = getkn (ctx, "@");
	  if (rc)
	    return rc;
	  
	  /* Make sure it's the auth token. */
	  if (mu_c_strncasecmp (ctx->tokbuf, "auth=", 5) == 0)
	    {
	      rc = str_assign (&url->auth, ctx->tokbuf + 5);
	      if (rc)
		return rc;
	      url->flags |= MU_URL_AUTH;
	    }
	}

      /* Skip @ sign */
      ctx->cur++;
    }
  return _mu_url_ctx_parse_host (ctx, has_cred);
}

int
_mu_url_ctx_parse (struct mu_url_ctx *ctx)
{
  int rc;
  mu_url_t url = ctx->url;
  const char *save = ctx->cur;
  
  /* Parse the scheme part */
  if (*ctx->cur == ':')
    return _mu_url_ctx_parse_cred (ctx);
  
  rc = getkn (ctx, ":/");
  if (rc)
    return rc;
  if (*ctx->cur == ':'
      && ((ctx->flags & MU_URL_PARSE_DSLASH_OPTIONAL)
	  || (ctx->cur[1] == '/' && ctx->cur[2] == '/')))
    {
      rc = str_assign (&url->scheme, ctx->tokbuf);
      if (rc)
	return rc;
      url->flags |= MU_URL_SCHEME;
      ctx->cur++;
    }
  else
    {
      ctx->cur = save;
      return _mu_url_ctx_parse_cred (ctx);
    }
  
  if (*ctx->cur == 0)
    return 0;

  if (ctx->cur[0] == '/' && ctx->cur[1] == '/')
    {
      ctx->cur += 2;
      return _mu_url_ctx_parse_cred (ctx);
    }

  return _mu_url_ctx_parse_path (ctx);
}

static int
_mu_url_create_internal (struct mu_url_ctx *ctx, mu_url_t hint)
{
  int rc = 0;
  mu_url_t url = ctx->url;
  const char *scheme = NULL;
  
  if ((ctx->flags & MU_URL_PARSE_PIPE) && ctx->input[0] == '|')
    {
      struct mu_wordsplit ws;
      size_t i;
      
      scheme = "prog";
      ctx->flags &= ~MU_URL_PARSE_HEXCODE;
      if (mu_wordsplit (ctx->input + 1, &ws, MU_WRDSF_DEFFLAGS))
	return errno;
      url->path = ws.ws_wordv[0];
      url->flags |= MU_URL_PATH;
      
      url->qargc = ws.ws_wordc - 1;
      url->qargv = calloc (url->qargc + 1, sizeof (url->qargv[0]));
      if (!url->qargv)
	{
	  mu_wordsplit_free (&ws);
	  return ENOMEM;
	}
      for (i = 0; i < url->qargc; i++)
	url->qargv[i] = ws.ws_wordv[i+1];
      ws.ws_wordc = 0;
      mu_wordsplit_free (&ws);
      url->flags |= MU_URL_QUERY;
    }
  else if ((ctx->flags & MU_URL_PARSE_SLASH) &&
	   (ctx->input[0] == '/' ||
	    (ctx->input[0] == '.' && ctx->input[1] == '/')))
    {
      scheme = "file";
      ctx->flags &= ~MU_URL_PARSE_HEXCODE;
      rc = str_assign (&url->path, ctx->input);
      if (rc == 0)
	url->flags |= MU_URL_PATH;
    }
  else
    rc = _mu_url_ctx_parse (ctx);
  
  if (rc)
    return rc;

  if (hint)
    {
      /* Fill in missing values */
      rc = mu_url_copy_hints (url, hint);
      if (rc)
	return rc;
    }

  if (!(url->flags & MU_URL_SCHEME))
    {
      if (scheme)
	{
	  rc = str_assign (&url->scheme, scheme);
	  if (rc)
	    return rc;
	  url->flags |= MU_URL_SCHEME;
	}
      else
	return MU_ERR_URL_MISS_PARTS;
    }
  
  /* RFC 1738, section 2.1, lower the scheme case */
  mu_strlower (url->scheme);

  if ((url->flags & MU_URL_PORT) && url->port == 0)
    {
      /* Convert port string to number */
      unsigned long n;
      char *p;
      
      n = strtoul (url->portstr, &p, 10);
      if (*p)
	{
	  if (ctx->flags & MU_URL_PARSE_PORTSRV)
	    {
	      /* FIXME: Another proto? */
	      struct servent *sp = getservbyname (url->portstr, "tcp");
	      if (!sp)
		return MU_ERR_TCP_NO_PORT; 
	      url->port = ntohs (sp->s_port);
	    }
	  else
	    return MU_ERR_TCP_NO_PORT;
	}
      else if (n > USHRT_MAX)
	return ERANGE;
      else
	url->port = n;
    }

  if (ctx->flags & MU_URL_PARSE_HEXCODE)
    {
      /* Decode the %XX notations */
      rc = mu_url_decode (url);
      if (rc)
	return rc;
    }
  
  if ((url->flags & MU_URL_SECRET) &&
      (ctx->flags & MU_URL_PARSE_HIDEPASS))
    {
      /* Obfuscate the password */
#define PASS_REPL "***"
#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
      size_t plen = mu_secret_length (url->secret);
      size_t nlen = strlen (url->name);
      size_t len = nlen - plen + PASS_REPL_LEN + 1;
      char *newname;
      
      memset (url->name + ctx->passoff, 0, plen);
      if (len > nlen + 1)
	{
	  newname = realloc (url->name, len);
	  if (!newname)
	    return rc;
	  url->name = newname;
	}
      else
	newname = url->name;
      memmove (newname + ctx->passoff + PASS_REPL_LEN,
	       newname + ctx->passoff + plen,
	       nlen - (ctx->passoff + plen) + 1);
      memcpy (newname + ctx->passoff, PASS_REPL, PASS_REPL_LEN);
    }

  return 0;
}

int
mu_url_create_hint (mu_url_t *purl, const char *str, int flags,
		    mu_url_t hint)
{
  int rc;
  struct mu_url_ctx ctx;
  mu_url_t url;

  if (!purl)
    return EINVAL;
  url = calloc (1, sizeof (*url));
  if (url == NULL)
    return ENOMEM;
  url->name = strdup (str);
  if (!url->name)
    {
      free (url);
      return ENOMEM;
    }
  memset (&ctx, 0, sizeof (ctx));
  ctx.flags = flags;
  ctx.input = str;
  ctx.cur = ctx.input;
  ctx.url = url;
  rc = _mu_url_create_internal (&ctx, hint);
  free (ctx.tokbuf);
  if (rc)
    mu_url_destroy (&url);
  else
    *purl = url;
  return rc;
}
      
int
mu_url_create (mu_url_t *purl, const char *str)
{
  return mu_url_create_hint (purl, str,
			     MU_URL_PARSE_HEXCODE |
			     MU_URL_PARSE_HIDEPASS |
			     MU_URL_PARSE_PORTSRV |
			     MU_URL_PARSE_PIPE |
			     MU_URL_PARSE_SLASH |
			     MU_URL_PARSE_DSLASH_OPTIONAL, NULL);
}

Return to:

Send suggestions and report system problems to the System administrator.