Blame view

mailbox/url.c 23.9 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2007, 2008, 2009, 2010 Free Software
   Foundation, Inc.
4

5 6 7
   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
8
   version 3 of the License, or (at your option) any later version.
9

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

15 16 17 18
   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */
19 20 21 22 23

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

24
#include <ctype.h>
25
#include <errno.h>
26 27
#include <stdlib.h>
#include <string.h>
28 29 30
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
31

32
#include <mailutils/mutil.h>
33
#include <mailutils/errno.h>
34
#include <mailutils/argcv.h>
35
#include <mailutils/secret.h>
36 37
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
38
#include <url0.h>
39

40 41
#define AC2(a,b) a ## b
#define AC4(a,b,c,d) a ## b ## c ## d
42

43
static int url_parse0 (mu_url_t, char *, size_t *poff);
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static int
parse_query (const char *query,
	     char *delim,
	     int *pargc, char ***pargv, const char **pend)
{
  size_t count, i;
  char **v;
  const char *p;

  for (p = query, count = 0; ; count++)
    {
      size_t len = strcspn (p, delim);
      p += len;
      if (!*p || *p == delim[1])
	break;
      p++;
    }

  if (pend)
    *pend = p;
  if (p == query)
    return 0;
  count++;
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  v = calloc (count + 1, sizeof (v[0]));
  for (i = 0, p = query; i < count; i++)
    {
      size_t len = strcspn (p, delim);
      v[i] = mu_url_decode_len (p, len);
      if (v[i] == NULL)
	{
	  mu_argcv_free (i, v);
	  return 1;
	}
      p += len + 1;
    }
  v[i] = NULL;

  *pargc = count;
  *pargv = v;
  return 0;
}
87

88
int
89
mu_url_create (mu_url_t *purl, const char *name)
90
{
91
  mu_url_t url = calloc (1, sizeof (*url));
92 93
  if (url == NULL)
    return ENOMEM;
94

95
  url->name = strdup (name);
96 97 98 99 100
  if (url->name == NULL)
    {
      free (url);
      return ENOMEM;
    }
101 102
  *purl = url;
  return 0;
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
static char **
argcv_copy (size_t argc, char **argv)
{
  size_t i;
  char **nv = calloc (argc + 1, sizeof (nv[0]));
  if (!nv)
    return NULL;
  for (i = 0; i < argc; i++)
    if ((nv[i] = strdup (argv[i])) == NULL)
      {
	mu_argcv_free (i, nv);
	free (nv);
	return NULL;
      }
  return nv;
}

static int
mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
{
  const char *str;
  size_t argc;
  char **argv;
  int rc;
129 130
  mu_secret_t sec;

131 132 133 134 135 136 137 138
#define URLCOPY(what)						\
  do								\
    {								\
      rc = AC2(mu_url_sget_,what) (old_url, &str);		\
      if (rc == 0)						\
	{							\
	  if ((new_url->what = strdup (str)) == NULL)		\
	    return ENOMEM;					\
139
	}							\
140 141 142 143 144 145 146
      else if (rc != MU_ERR_NOENT)				\
	return rc;						\
    }								\
  while (0);

  URLCOPY (scheme);
  URLCOPY (user);
147 148 149 150 151 152 153 154 155 156 157 158 159

  rc = mu_url_get_secret (old_url, &sec);
  if (rc == MU_ERR_NOENT)
    new_url->secret = NULL;
  else if (rc)
    return rc;
  else
    {
      rc = mu_secret_dup (sec, &new_url->secret);
      if (rc)
	return rc;
    }
  
160 161 162 163 164 165 166 167 168 169 170 171
  URLCOPY (auth);
  URLCOPY (host);
  new_url->port = old_url->port;
  URLCOPY (path);

  rc = mu_url_sget_fvpairs (old_url, &argc, &argv);
  if (rc == 0 && argc)
    {
      if ((new_url->fvpairs = argcv_copy (argc, argv)) == NULL)
	return ENOMEM;
      new_url->fvcount = argc;
    }
172

173 174 175 176 177 178 179 180 181
  rc = mu_url_sget_query (old_url, &argc, &argv);
  if (rc == 0 && argc)
    {
      if ((new_url->qargv = argcv_copy (argc, argv)) == NULL)
	return ENOMEM;
      new_url->qargc = argc;
    }
  return 0;
#undef URLCOPY
182 183
}

184 185 186 187 188
int
mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
{
  mu_url_t url;
  int rc = mu_url_create (&url, mu_url_to_string (old_url));
189

190 191 192 193 194 195 196 197 198
  if (rc)
    return rc;

  rc = mu_url_copy0 (old_url, url);
  if (rc == 0)
    *new_url = url;
  else
    mu_url_destroy (&url);
  return rc;
199 200 201 202 203 204 205 206
}

int
mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
{
  int rc;
  char *p;
  mu_url_t new_url;
207 208 209

  if (url->_uplevel)
    return url->_uplevel (url, upurl);
210

211 212 213
  if (!url->path)
    return MU_ERR_NOENT;
  p = strrchr (url->path, '/');
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  rc = mu_url_dup (url, &new_url);
  if (rc == 0)
    {
      if (!p || p == url->path)
	{
	  free (new_url->path);
	  new_url->path = NULL;
	}
      else
	{
	  size_t size = p - url->path;
	  new_url->path = realloc (new_url->path, size + 1);
	  if (!new_url->path)
	    {
	      mu_url_destroy (&new_url);
	      return ENOMEM;
	    }
	  memcpy (new_url->path, url->path, size);
	  new_url->path[size] = 0;
	}
      *upurl = new_url;
    }
  return rc;
238 239
}

240
void
241
mu_url_destroy (mu_url_t * purl)
242 243 244
{
  if (purl && *purl)
    {
245
      mu_url_t url = (*purl);
246

247 248
      if (url->_destroy)
	url->_destroy (url);
249

250 251
      if (url->name)
	free (url->name);
252

253 254
      if (url->scheme)
	free (url->scheme);
255

256 257
      if (url->user)
	free (url->user);
258

259
      mu_secret_destroy (&url->secret);
260

261 262 263
      if (url->auth)
	free (url->auth);

264 265
      if (url->host)
	free (url->host);
266

267 268 269
      if (url->path)
	free (url->path);

270 271
      if (url->fvcount)
	mu_argcv_free (url->fvcount, url->fvpairs);
272 273

      mu_argcv_free (url->qargc, url->qargv);
274

275 276
      free (url);

277 278
      *purl = NULL;
    }
279 280
}

281
int
282
mu_url_parse (mu_url_t url)
283 284 285
{
  int err = 0;
  char *n = NULL;
286
  struct _mu_url u;
287 288
  size_t pstart;
  mu_secret_t newsec;
289 290 291 292

  if (!url || !url->name)
    return EINVAL;

293
  memset (&u, 0, sizeof u);
294
  /* can't have been parsed already */
295
  if (url->scheme || url->user || url->secret || url->auth ||
296
      url->host || url->path || url->qargc)
297 298 299 300 301 302 303
    return EINVAL;

  n = strdup (url->name);

  if (!n)
    return ENOMEM;

304
  err = url_parse0 (&u, n, &pstart);
305 306 307

  if (!err)
    {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
      if (u.secret)
	{
	  /* Obfuscate the password */
#define PASS_REPL "***"
#define PASS_REPL_LEN (sizeof (PASS_REPL) - 1)
	  size_t plen = mu_secret_length (u.secret);
	  size_t nlen = strlen (url->name);
	  size_t len = nlen - plen + PASS_REPL_LEN + 1;
	  char *newname;

	  memset (url->name + pstart, 0, plen);
	  newname = realloc (url->name, len);
	  if (!newname)
	    goto CLEANUP;
	  memmove (newname + pstart + PASS_REPL_LEN, newname + pstart + plen,
		   nlen - (pstart + plen) + 1);
	  memcpy (newname + pstart, PASS_REPL, PASS_REPL_LEN);
	  url->name = newname;
	}

328
      /* Dup the strings we found. We wouldn't have to do this
329 330 331
	 if we did a single alloc of the source url name, and
	 kept it around. It's also a good time to do hex decoding,
	 though.
332 333
       */

334 335 336 337 338 339 340 341 342 343 344
#define UALLOC(X)                                          \
  if (u.X && u.X[0] && (url->X = mu_url_decode(u.X)) == 0) \
    {                                                      \
       err = ENOMEM;                                       \
       goto CLEANUP;                                       \
    }                                                      \
  else                                                     \
    {                                                      \
       /* Set zero-length strings to NULL. */              \
	u.X = NULL; \
    }
345

346 347
      UALLOC (scheme);
      UALLOC (user);
348 349 350 351 352 353 354 355 356 357 358 359 360

      if (u.secret)
	{
	  char *pass = mu_url_decode (mu_secret_password (u.secret));
	  err = mu_secret_create (&newsec, pass, strlen (pass));
	  memset (pass, 0, strlen (pass));
	  mu_secret_destroy (&u.secret);
	  if (err)
	    goto CLEANUP;

	  url->secret = newsec;
	}

361 362 363
      UALLOC (auth);
      UALLOC (host);
      UALLOC (path);
364

365
#undef UALLOC
366 367
      url->fvcount = u.fvcount;
      url->fvpairs = u.fvpairs;
368 369 370

      url->qargc = u.qargc;
      url->qargv = u.qargv;
371

372
      url->port = u.port;
373 374 375
    }

CLEANUP:
376
  memset (n, 0, strlen (n));
377 378 379 380
  free (n);

  if (err)
    {
381
#define UFREE(X) if (X) { free(X); X = 0; }
382

383 384
      UFREE (url->scheme);
      UFREE (url->user);
385
      mu_secret_destroy (&u.secret);
386 387 388
      UFREE (url->auth);
      UFREE (url->host);
      UFREE (url->path);
389 390
      mu_argcv_free (url->fvcount, url->fvpairs);
      mu_argcv_free (url->qargc, url->qargv);
391 392 393 394 395 396 397 398 399 400 401 402
#undef UFREE
    }

  return err;
}

/*

Syntax, condensed from RFC 1738, and extended with the ;auth=
of RFC 2384 (for POP) and RFC 2192 (for IMAP):

url =
403
    scheme ":" [ "//"
404 405 406 407 408

    [ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ]

    host [ ":" port ]

409
    [ ( "/" urlpath ) | ( "?" query ) ] ]
410 411 412 413 414 415 416 417 418 419 420 421 422

All hell will break loose in this parser if the user/pass/auth
portion is missing, and the urlpath has any @ or : characters
in it. A imap mailbox, say, named after the email address of
the person the mail is from:

  imap://imap.uniserve.com/alain@qnx.com

Is this required to be % quoted, though? I hope so!

*/

static int
423
url_parse0 (mu_url_t u, char *name, size_t *poff)
424
{
425
  char *start = name;
Sergey Poznyakoff authored
426
  char *p;			/* pointer into name */
427 428 429 430 431

  /* reject the obvious */
  if (name == NULL)
    return EINVAL;

432 433 434 435
  if (name[0] == '/')
    {
      u->scheme = "file";
    }
436 437 438 439 440 441 442 443 444 445 446 447 448
  else if (name[0] == '|')
    {
      int rc;
      u->scheme = "prog";
      rc = mu_argcv_get (name + 1, NULL, NULL, &u->qargc, &u->qargv);
      if (rc == 0)
	{
	  u->path = strdup (u->qargv[0]);
	  if (!u->path)
	    rc = ENOMEM;
	}
      return rc;
    }
449 450 451 452 453 454
  else
    {
      /* Parse out the SCHEME. */
      p = strchr (name, ':');
      if (p == NULL)
	return MU_ERR_PARSE;
455

456
      *p++ = 0;
457

458
      u->scheme = name;
459

460 461
      /* RFC 1738, section 2.1, lower the scheme case */
      for (; name < p; name++)
462
	*name = mu_tolower (*name);
463

464 465
      name = p;
    }
466

467
  /* Check for nothing following the scheme. */
Sergey Poznyakoff authored
468
  if (!*name)
469 470
    return 0;

471
  if (strncmp (name, "//", 2) == 0)
Sergey Poznyakoff authored
472
    {
473
      name += 2;
474

475
      if (name[0] == '/')
476
	{
477 478 479 480 481 482 483 484 485 486
	  u->path = name;
	  p = u->path + strcspn (u->path, ";?");
	}
      else
	{
	  /* Split into LHS and RHS of the '@', and then parse each side. */
	  u->host = strchr (name, '@');
	  if (u->host == NULL)
	    u->host = name;
	  else
487
	    {
488 489
	      char *pass = NULL;

490 491
	      /* Parse the LHS into an identification/authentication pair. */
	      *u->host++ = 0;
492

493
	      u->user = name;
494

495 496 497
	      /* Try to split the user into a:
		 <user>:<password>
		 or
498
		 <user>:<password>;AUTH=<auth>
499
	      */
500

501 502
	      for (; *name; name++)
		{
503 504 505
		  if (*name == ':')
		    {
		      *name++ = 0;
506 507
		      pass = name;
		      *poff = pass - start;
508 509
		    }
		  else if (*name == ';')
510 511
		    {
		      /* Make sure it's the auth token. */
512
		      if (mu_c_strncasecmp (name + 1, "auth=", 5) == 0)
513 514 515 516 517 518 519
			{
			  *name++ = 0;
			  name += 5;
			  u->auth = name;
			  break;
			}
		    }
520
		}
521 522 523 524 525 526 527 528 529

	      if (pass)
		{
		  if (mu_secret_create (&u->secret, pass, strlen (pass)))
		    return ENOMEM;
		  else
		    /* Obfuscate password */
		    memset (pass, 0, strlen (pass));
		}
530
	    }
531 532 533 534

	  /* Parse the host and port from the RHS. */
	  p = strchr (u->host, ':');
	  if (p)
535
	    {
536 537 538 539 540 541 542
	      *p++ = 0;
	      u->port = strtol (p, &p, 10);

	      /* Check for garbage after the port: we should be on the start
		 of a path, a query, or at the end of the string. */
	      if (*p && strcspn (p, "/?") != 0)
		return MU_ERR_PARSE;
543
	    }
544 545
	  else
	    p = u->host + strcspn (u->host, ";/?");
546 547
	}
    }
548 549 550 551 552
  else
    {
      u->path = name;
      p = u->path + strcspn (u->path, ";?");
    }
553

554 555
  /* Either way, if we're not at a nul, we're at a path or query. */
  if (u->path == NULL && *p == '/')
556
    {
557
      /* found a path */
558
      *p++ = 0;
559 560 561
      u->path = p;
      p = u->path + strcspn (u->path, ";?");
    }
562

563 564 565
  if (*p == ';')
    {
      *p++ = 0;
566 567
      if (parse_query (p, ";?", &u->fvcount, &u->fvpairs, (const char **)&p))
	return ENOMEM;
568 569 570 571 572 573
    }

  if (*p == '?')
    {
      /* found a query */
      *p++ = 0;
574 575
      if (parse_query (p, "&", &u->qargc, &u->qargv, NULL))
	return ENOMEM;
576
    }
Sergey Poznyakoff authored
577

578 579 580
  return 0;
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

/* General accessors: */
#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)

#define DECL_SGET(field)						  \
int									  \
ACCESSOR(sget,field) (mu_url_t url, char const **sptr)	                  \
{									  \
  if (url == NULL)							  \
    return EINVAL;							  \
  if (!url->field)							  \
    {									  \
      if (url->AC2(_get_,field))					  \
	{								  \
	  size_t n;							  \
	  char *buf;							  \
597
									  \
598 599 600
	  int status = url->AC2(_get_,field) (url, NULL, 0, &n);	  \
	  if (status)							  \
	    return status;						  \
601
									  \
602 603 604
	  buf = malloc (n + 1);						  \
	  if (!buf)							  \
	    return ENOMEM;						  \
605
									  \
606
	  status = url->AC2(_get_,field) (url, buf, n + 1, NULL);	  \
607 608 609 610 611
	  if (status)				          \
	    return status;						  \
									  \
	  if (buf[0])                                                     \
	    {                                                             \
612 613 614
	       url->field = mu_url_decode (buf);			  \
	       free (buf);						  \
	    }                                                             \
615 616
	  else                                                            \
	    url->field = buf;                                             \
617 618 619 620
	  if (!url->field)						  \
	    return ENOMEM;						  \
	}								  \
      else								  \
621
	return MU_ERR_NOENT;			                          \
622 623 624
    }									  \
  *sptr = url->field;							  \
  return 0;								  \
625 626
}

627 628 629 630 631 632 633
#define DECL_GET(field)							  \
int									  \
ACCESSOR(get,field) (mu_url_t url, char *buf, size_t len, size_t *n)      \
{									  \
  size_t i;								  \
  const char *str;							  \
  int status = ACCESSOR(sget, field) (url, &str);			  \
634
									  \
635 636 637 638 639 640 641
  if (status)								  \
    return status;							  \
									  \
  i = mu_cpystr (buf, str, len);					  \
  if (n)								  \
    *n = i;								  \
  return 0;								  \
642 643
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
#define DECL_AGET(field)						  \
int									  \
ACCESSOR(aget, field) (mu_url_t url, char **buf)	                  \
{									  \
  const char *str;							  \
  int status = ACCESSOR(sget, field) (url, &str);			  \
									  \
  if (status)								  \
    return status;							  \
									  \
  if (str)								  \
    {									  \
      *buf = strdup (str);						  \
      if (!*buf)							  \
	status = ENOMEM;						  \
    }									  \
  else									  \
    *buf = NULL;							  \
  return status;							  \
663 664
}

665 666 667 668 669 670 671
#define DECL_CMP(field)							  \
int									  \
ACCESSOR(is_same,field) (mu_url_t url1, mu_url_t url2)		          \
{									  \
  const char *s1, *s2;							  \
  int status1, status2;							  \
									  \
Sergey Poznyakoff authored
672
  status1 = ACCESSOR(sget, field) (url1, &s1);				  \
673 674
  if (status1 && status1 != MU_ERR_NOENT)				  \
    return 0;								  \
Sergey Poznyakoff authored
675
  status2 = ACCESSOR(sget, field) (url2, &s2);				  \
676 677 678 679 680
  if (status2 && status2 != MU_ERR_NOENT)				  \
    return 0;								  \
									  \
  if (status1 && status1 == status2) /* Both fields are missing */	  \
    return 1;								  \
681
  return mu_c_strcasecmp (s1, s2) == 0;					  \
682 683
}

684 685 686 687 688 689 690 691 692 693 694 695 696
#define DECL_ACCESSORS(field)			                          \
DECL_SGET(field)				                          \
DECL_GET(field)					                          \
DECL_AGET(field)                                                          \
DECL_CMP(field)


/* Declare particular accessors */
DECL_ACCESSORS (scheme)
DECL_ACCESSORS (user)
DECL_ACCESSORS (auth)
DECL_ACCESSORS (host)
DECL_ACCESSORS (path)
697 698

int
699 700 701 702 703 704 705 706 707 708 709 710
mu_url_get_secret (const mu_url_t url, mu_secret_t *psecret)
{
  if (url->_get_secret)
    return url->_get_secret (url, psecret);
  if (url->secret == NULL)
    return MU_ERR_NOENT;
  mu_secret_ref (url->secret);
  *psecret = url->secret;
  return 0;
}

int
711 712
mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
{
713
  if (url == NULL)
714 715 716 717 718 719 720 721 722 723 724 725 726
    return EINVAL;
  /* See FIXME below */
  *qc = url->qargc;
  *qv = url->qargv;
  return 0;
}

int
mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
{
  size_t qargc, i;
  char **qargv;
  char **qcopy;
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
  int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
  if (rc)
    return rc;

  qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
  if (!qcopy)
    return errno;
  for (i = 0; i < qargc; i++)
    {
      if (!(qcopy[i] = strdup (qargv[i])))
	{
	  mu_argcv_free (i, qcopy);
	  return errno;
	}
    }
  qcopy[i] = NULL;
  *qc = qargc;
  *qv = qcopy;
  return 0;
}
748

749 750 751
/* field-value pairs accessors */
int
mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
752 753
{
  if (url == NULL)
754 755 756 757 758 759 760
    return EINVAL;
  /* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
     anyway */
  *fvc = url->fvcount;
  *fvp = url->fvpairs;
  return 0;
}
761

762 763 764 765 766 767
int
mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
{
  size_t fvc, i;
  char **fvp;
  char **fvcopy;
768

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
  if (rc)
    return rc;

  fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
  if (!fvcopy)
    return errno;
  for (i = 0; i < fvc; i++)
    {
      if (!(fvcopy[i] = strdup (fvp[i])))
	{
	  mu_argcv_free (i, fvcopy);
	  return errno;
	}
    }
  fvcopy[i] = NULL;
  *pfvc = fvc;
  *pfvp = fvcopy;
  return 0;
}
789

790
int
791
mu_url_get_port (const mu_url_t url, long *pport)
792
{
793 794 795 796 797
  if (url == NULL)
    return EINVAL;
  if (url->_get_port)
    return url->_get_port (url, pport);
  *pport = url->port;
798 799 800
  return 0;
}

801
const char *
802
mu_url_to_string (const mu_url_t url)
803
{
804 805 806
  if (url == NULL || url->name == NULL)
    return "";
  return url->name;
807
}
808

Sergey Poznyakoff authored
809
int
810 811 812 813 814 815 816 817 818 819 820 821 822
mu_url_set_scheme (mu_url_t url, const char *scheme)
{
  char *p;
  if (!url || !scheme)
    return EINVAL;
  p = realloc (url->scheme, strlen (scheme) + 1);
  if (!p)
    return ENOMEM;
  strcpy (url->scheme, scheme);
  return 0;
}

int
823
mu_url_is_scheme (mu_url_t url, const char *scheme)
824
{
825 826
  if (url && scheme && url->scheme 
      && mu_c_strcasecmp (url->scheme, scheme) == 0)
827 828 829 830 831
    return 1;

  return 0;
}

832
int
833
mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
834 835 836
{
  long p1 = 0, p2 = 0;

837 838
  mu_url_get_port (url1, &p1);
  mu_url_get_port (url2, &p2);
839 840
  return (p1 == p2);
}
841 842 843

/* From RFC 1738, section 2.2 */
char *
844
mu_url_decode_len (const char *s, size_t len)
845
{
846 847
  char *d;
  const char *eos = s + len;
848 849
  int i;

850
  d = malloc (len + 1);
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
  if (!d)
    return NULL;

  for (i = 0; s < eos; i++)
    {
      if (*s != '%')
	{
	  d[i] = *s;
	  s++;
	}
      else
	{
	  unsigned long ul = 0;

	  s++;

	  /* don't check return value, it's correctly coded, or it's not,
	     in which case we just skip the garbage, this is a decoder,
	     not an AI project */

	  mu_hexstr2ul (&ul, s, 2);

	  s += 2;

	  d[i] = (char) ul;
	}
    }

  d[i] = 0;

  return d;
}

884 885 886 887 888 889
char *
mu_url_decode (const char *s)
{
  return mu_url_decode_len (s, strlen (s));
}

Sergey Poznyakoff authored
890 891
static int
defined (const char *s)
892
{
Sergey Poznyakoff authored
893 894 895
  if (s && strcmp ("*", s) != 0)
    return 1;
  return 0;
896 897 898
}

int
899
mu_url_is_ticket (mu_url_t ticket, mu_url_t url)
900
{
Sergey Poznyakoff authored
901
  if (!ticket || !url)
902 903 904 905
    return 0;

  /* If ticket has a scheme, host, port, or path, then the queries
     equivalent must be defined and match. */
Sergey Poznyakoff authored
906 907
  if (defined (ticket->scheme))
    {
908
      if (!url->scheme || mu_c_strcasecmp (ticket->scheme, url->scheme) != 0)
Sergey Poznyakoff authored
909 910 911 912
	return 0;
    }
  if (defined (ticket->host))
    {
913
      if (!url->host || mu_c_strcasecmp (ticket->host, url->host) != 0)
Sergey Poznyakoff authored
914 915 916
	return 0;
    }
  if (ticket->port && ticket->port != url->port)
917
    return 0;
918
  /* If ticket has a user or pass, but url doesn't, that's OK, we were
919 920
     urling for this info. But if url does have a user/pass, it
     must match the ticket. */
Sergey Poznyakoff authored
921 922 923 924 925
  if (url->user)
    {
      if (defined (ticket->user) && strcmp (ticket->user, url->user) != 0)
	return 0;
    }
926 927 928 929

  /* Guess it matches. */
  return 1;
}
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

int
mu_url_init (mu_url_t url, int port, const char *scheme)
{
  int status = 0;

  url->_destroy = NULL;

  status = mu_url_parse (url);
  if (status)
    return status;

  if (!mu_url_is_scheme (url, scheme))
    return EINVAL;

  if (url->port == 0)
    url->port = port;

  return status;
}
950 951 952 953 954

/* Default mailbox path generator */
static char *
_url_path_default (const char *spooldir, const char *user, int unused)
{
Sergey Poznyakoff authored
955
  char *mbox = malloc (strlen (spooldir) + strlen (user) + 2);
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
  if (!mbox)
    errno = ENOMEM;
  else
    sprintf (mbox, "%s/%s", spooldir, user);
  return mbox;
}

/* Hashed indexing */
static char *
_url_path_hashed (const char *spooldir, const char *user, int param)
{
  int i;
  int ulen = strlen (user);
  char *mbox;
  unsigned hash;

  if (param > ulen)
    param = ulen;
974
  for (i = 0, hash = 0; i < param; i++)
975 976 977 978 979 980 981 982 983
    hash += user[i];

  mbox = malloc (ulen + strlen (spooldir) + 5);
  sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
  return mbox;
}

static int transtab[] = {
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
  'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
  'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
  'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
};

/* Forward Indexing */
static char *
_url_path_index (const char *spooldir, const char *iuser, int index_depth)
{
  const unsigned char* user = (const unsigned char*) iuser;
  int i, ulen = strlen (iuser);
  char *mbox, *p;
1024

1025 1026
  if (ulen == 0)
    return NULL;
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
  strcpy (mbox, spooldir);
  p = mbox + strlen (mbox);
  for (i = 0; i < index_depth && i < ulen; i++)
    {
      *p++ = '/';
      *p++ = transtab[ user[i] ];
    }
  for (; i < index_depth; i++)
    {
      *p++ = '/';
      *p++ = transtab[ user[ulen-1] ];
    }
  *p++ = '/';
  strcpy (p, iuser);
  return mbox;
}

/* Reverse Indexing */
static char *
_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
{
  const unsigned char* user = (const unsigned char*) iuser;
  int i, ulen = strlen (iuser);
  char *mbox, *p;
1053

1054 1055
  if (ulen == 0)
    return NULL;
1056

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
  strcpy (mbox, spooldir);
  p = mbox + strlen (mbox);
  for (i = 0; i < index_depth && i < ulen; i++)
    {
      *p++ = '/';
      *p++ = transtab[ user[ulen - i - 1] ];
    }
  for (; i < index_depth; i++)
    {
      *p++ = '/';
      *p++ = transtab[ user[0] ];
    }
  *p++ = '/';
  strcpy (p, iuser);
  return mbox;
}

static int
rmselector (const char *p, void *data MU_ARG_UNUSED)
{
  return strncmp (p, "type=", 5) == 0
1079 1080
	 || strncmp (p, "user=", 5) == 0
	 || strncmp (p, "param=", 6) == 0;
1081
}
1082

1083 1084 1085 1086 1087 1088 1089 1090
int
mu_url_expand_path (mu_url_t url)
{
  size_t i;
  char *user = NULL;
  int param = 0;
  char *p;
  char *(*fun) (const char *, const char *, int) = _url_path_default;
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  if (url->fvcount == 0)
    return 0;

  for (i = 0; i < url->fvcount; i++)
    {
      p = url->fvpairs[i];
      if (strncmp (p, "type=", 5) == 0)
	{
	  char *type = p + 5;

	  if (strcmp (type, "hash") == 0)
	    fun = _url_path_hashed;
	  else if (strcmp (type, "index") == 0)
	    fun = _url_path_index;
	  else if (strcmp (type, "rev-index") == 0)
	    fun = _url_path_rev_index;
	  else
	    return MU_ERR_NOENT;
	}
      else if (strncmp (p, "user=", 5) == 0)
	{
	  user = p + 5;
	}
      else if (strncmp (p, "param=", 6) == 0)
	{
	  param = strtoul (p + 6, NULL, 0);
	}
    }
1120

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  if (user)
    {
      char *p = fun (url->path, user, param);
      if (p)
	{
	  free (url->path);
	  url->path = p;
	}
      mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
    }
  else
    return MU_ERR_NOENT;

  return 0;
}