Going a little step in direction namespace cleanup
[s-mailx.git] / urlcrecry.c
blob121448d811c523c0a3ee10d4c94080724854d794
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ URL parsing, credential handling and crypto hooks.
3 *@ .netrc parser quite loosely based upon NetBSD usr.bin/ftp/
4 *@ $NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp $
6 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #undef n_FILE
21 #define n_FILE urlcrecry
23 #ifndef HAVE_AMALGAMATION
24 # include "nail.h"
25 #endif
27 #ifdef HAVE_NETRC
28 /* NetBSD usr.bin/ftp/ruserpass.c uses 100 bytes for that, we need four
29 * concurrently (dummy, host, user, pass), so make it a KB */
30 # define NRC_TOKEN_MAXLEN (1024 / 4)
32 enum nrc_token {
33 NRC_ERROR = -1,
34 NRC_NONE = 0,
35 NRC_DEFAULT,
36 NRC_LOGIN,
37 NRC_PASSWORD,
38 NRC_ACCOUNT,
39 NRC_MACDEF,
40 NRC_MACHINE,
41 NRC_INPUT
44 struct nrc_node {
45 struct nrc_node *nrc_next;
46 struct nrc_node *nrc_result; /* In match phase, former possible one */
47 ui32_t nrc_mlen; /* Length of machine name */
48 ui32_t nrc_ulen; /* Length of user name */
49 ui32_t nrc_plen; /* Length of password */
50 char nrc_dat[n_VFIELD_SIZE(sizeof(ui32_t))];
52 # define NRC_NODE_ERR ((struct nrc_node*)-1)
54 static struct nrc_node *_nrc_list;
55 #endif /* HAVE_NETRC */
57 /* Find the last @ before a slash
58 * TODO Casts off the const but this is ok here; obsolete function! */
59 #ifdef HAVE_SOCKETS /* temporary (we'll have file://..) */
60 static char * _url_last_at_before_slash(char const *sp);
61 #endif
63 #ifdef HAVE_NETRC
64 /* Initialize .netrc cache */
65 static void _nrc_init(void);
66 static enum nrc_token __nrc_token(FILE *fi, char buffer[NRC_TOKEN_MAXLEN],
67 bool_t *nl_last);
69 /* We shall lookup a machine in .netrc says ok_blook(netrc_lookup).
70 * only_pass is true then the lookup is for the password only, otherwise we
71 * look for a user (and add password only if we have an exact machine match) */
72 static bool_t _nrc_lookup(struct url *urlp, bool_t only_pass);
74 /* 0=no match; 1=exact match; -1=wildcard match */
75 static int __nrc_host_match(struct nrc_node const *nrc,
76 struct url const *urlp);
77 static bool_t __nrc_find_user(struct url *urlp,
78 struct nrc_node const *nrc);
79 static bool_t __nrc_find_pass(struct url *urlp, bool_t user_match,
80 struct nrc_node const *nrc);
81 #endif /* HAVE_NETRC */
83 /* The password can also be gained through external agents TODO v15-compat */
84 #ifdef HAVE_AGENT
85 static bool_t _agent_shell_lookup(struct url *urlp, char const *comm);
86 #endif
88 #ifdef HAVE_SOCKETS
89 static char *
90 _url_last_at_before_slash(char const *sp)
92 char const *cp;
93 char c;
94 NYD2_ENTER;
96 for (cp = sp; (c = *cp) != '\0'; ++cp)
97 if (c == '/')
98 break;
99 while (cp > sp && *--cp != '@')
101 if (*cp != '@')
102 cp = NULL;
103 NYD2_LEAVE;
104 return n_UNCONST(cp);
106 #endif
108 #ifdef HAVE_NETRC
109 static void
110 _nrc_init(void)
112 struct n_sigman sm;
113 char buffer[NRC_TOKEN_MAXLEN], host[NRC_TOKEN_MAXLEN],
114 user[NRC_TOKEN_MAXLEN], pass[NRC_TOKEN_MAXLEN], *netrc_load;
115 struct stat sb;
116 FILE * volatile fi;
117 enum nrc_token t;
118 bool_t volatile ispipe;
119 bool_t seen_default, nl_last;
120 struct nrc_node * volatile ntail, * volatile nhead, * volatile nrc;
121 NYD_ENTER;
123 n_UNINIT(ntail, NULL);
124 nhead = NULL;
125 nrc = NRC_NODE_ERR;
126 ispipe = FAL0;
127 fi = NULL;
129 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL) {
130 case 0:
131 break;
132 default:
133 goto jleave;
136 if ((netrc_load = ok_vlook(netrc_pipe)) != NULL) {
137 ispipe = TRU1;
138 if ((fi = Popen(netrc_load, "r", ok_vlook(SHELL), NULL, COMMAND_FD_NULL)
139 ) == NULL) {
140 n_perr(netrc_load, 0);
141 goto j_leave;
143 } else {
144 if ((netrc_load = fexpand(ok_vlook(NETRC), FEXP_LOCAL | FEXP_NOPROTO)
145 ) == NULL)
146 goto j_leave;
148 if ((fi = Fopen(netrc_load, "r")) == NULL) {
149 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load, FAL0));
150 goto j_leave;
153 /* Be simple and apply rigid (permission) check(s) */
154 if (fstat(fileno(fi), &sb) == -1 || !S_ISREG(sb.st_mode) ||
155 (sb.st_mode & (S_IRWXG | S_IRWXO))) {
156 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
157 n_shexp_quote_cp(netrc_load, FAL0));
158 goto jleave;
162 seen_default = FAL0;
163 nl_last = TRU1;
164 jnext:
165 switch((t = __nrc_token(fi, buffer, &nl_last))) {
166 case NRC_NONE:
167 break;
168 default: /* Doesn't happen (but on error?), keep CC happy */
169 case NRC_DEFAULT:
170 jdef:
171 /* We ignore the default entry (require an exact host match), and we also
172 * ignore anything after such an entry (faulty syntax) */
173 seen_default = TRU1;
174 /* FALLTHRU */
175 case NRC_MACHINE:
176 jm_h:
177 /* Normalize HOST to lowercase */
178 *host = '\0';
179 if (!seen_default && (t = __nrc_token(fi, host, &nl_last)) != NRC_INPUT)
180 goto jerr;
181 else {
182 char *cp;
183 for (cp = host; *cp != '\0'; ++cp)
184 *cp = lowerconv(*cp);
187 *user = *pass = '\0';
188 while ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_NONE &&
189 t != NRC_MACHINE && t != NRC_DEFAULT) {
190 switch(t) {
191 case NRC_LOGIN:
192 if ((t = __nrc_token(fi, user, &nl_last)) != NRC_INPUT)
193 goto jerr;
194 break;
195 case NRC_PASSWORD:
196 if ((t = __nrc_token(fi, pass, &nl_last)) != NRC_INPUT)
197 goto jerr;
198 break;
199 case NRC_ACCOUNT:
200 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
201 goto jerr;
202 break;
203 case NRC_MACDEF:
204 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
205 goto jerr;
206 else {
207 int i = 0, c;
208 while ((c = getc(fi)) != EOF)
209 if (c == '\n') { /* xxx */
210 /* Don't care about comments here, since we parse until
211 * we've seen two successive newline characters */
212 if (i)
213 break;
214 i = 1;
215 } else
216 i = 0;
218 break;
219 default:
220 case NRC_ERROR:
221 goto jerr;
225 if (!seen_default && (*user != '\0' || *pass != '\0')) {
226 size_t hl = strlen(host), ul = strlen(user), pl = strlen(pass);
227 struct nrc_node *nx = smalloc(n_VSTRUCT_SIZEOF(struct nrc_node,
228 nrc_dat) + hl +1 + ul +1 + pl +1);
230 if (nhead != NULL)
231 ntail->nrc_next = nx;
232 else
233 nhead = nx;
234 ntail = nx;
235 nx->nrc_next = NULL;
236 nx->nrc_mlen = hl;
237 nx->nrc_ulen = ul;
238 nx->nrc_plen = pl;
239 memcpy(nx->nrc_dat, host, ++hl);
240 memcpy(nx->nrc_dat + hl, user, ++ul);
241 memcpy(nx->nrc_dat + hl + ul, pass, ++pl);
243 if (t == NRC_MACHINE)
244 goto jm_h;
245 if (t == NRC_DEFAULT)
246 goto jdef;
247 if (t != NRC_NONE)
248 goto jnext;
249 break;
250 case NRC_ERROR:
251 jerr:
252 if(n_poption & n_PO_D_V)
253 n_err(_("Errors occurred while parsing %s\n"),
254 n_shexp_quote_cp(netrc_load, FAL0));
255 assert(nrc == NRC_NODE_ERR);
256 goto jleave;
259 if (nhead != NULL)
260 nrc = nhead;
261 n_sigman_cleanup_ping(&sm);
262 jleave:
263 if (fi != NULL) {
264 if (ispipe)
265 Pclose(fi, TRU1);
266 else
267 Fclose(fi);
269 if (nrc == NRC_NODE_ERR)
270 while (nhead != NULL) {
271 ntail = nhead;
272 nhead = nhead->nrc_next;
273 free(ntail);
275 j_leave:
276 _nrc_list = nrc;
277 NYD_LEAVE;
278 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
281 static enum nrc_token
282 __nrc_token(FILE *fi, char buffer[NRC_TOKEN_MAXLEN], bool_t *nl_last)
284 int c;
285 char *cp;
286 enum nrc_token rv;
287 NYD2_ENTER;
289 rv = NRC_NONE;
290 for (;;) {
291 bool_t seen_nl;
293 c = EOF;
294 if (feof(fi) || ferror(fi))
295 goto jleave;
297 for (seen_nl = *nl_last; (c = getc(fi)) != EOF && whitechar(c);)
298 seen_nl |= (c == '\n');
300 if (c == EOF)
301 goto jleave;
302 /* fetchmail and derived parsers support comments */
303 if ((*nl_last = seen_nl) && c == '#') {
304 while ((c = getc(fi)) != EOF && c != '\n')
306 continue;
308 break;
311 cp = buffer;
312 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
313 if (c == '"' || c == '\'') {
314 int quotec = c;
316 /* Not requiring the closing QM is (Net)BSD syntax */
317 while ((c = getc(fi)) != EOF && c != quotec) {
318 /* Backslash escaping the next character is (Net)BSD syntax */
319 if (c == '\\')
320 if ((c = getc(fi)) == EOF)
321 break;
322 *cp++ = c;
323 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
324 rv = NRC_ERROR;
325 goto jleave;
328 } else {
329 *cp++ = c;
330 while ((c = getc(fi)) != EOF && !whitechar(c)) {
331 /* Backslash escaping the next character is (Net)BSD syntax */
332 if (c == '\\' && (c = getc(fi)) == EOF)
333 break;
334 *cp++ = c;
335 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
336 rv = NRC_ERROR;
337 goto jleave;
340 *nl_last = (c == '\n');
342 *cp = '\0';
344 if (*buffer == '\0')
345 do {/*rv = NRC_NONE*/} while (0);
346 else if (!strcmp(buffer, "default"))
347 rv = NRC_DEFAULT;
348 else if (!strcmp(buffer, "login"))
349 rv = NRC_LOGIN;
350 else if (!strcmp(buffer, "password") || !strcmp(buffer, "passwd"))
351 rv = NRC_PASSWORD;
352 else if (!strcmp(buffer, "account"))
353 rv = NRC_ACCOUNT;
354 else if (!strcmp(buffer, "macdef"))
355 rv = NRC_MACDEF;
356 else if (!strcmp(buffer, "machine"))
357 rv = NRC_MACHINE;
358 else
359 rv = NRC_INPUT;
360 jleave:
361 if (c == EOF && !feof(fi))
362 rv = NRC_ERROR;
363 NYD2_LEAVE;
364 return rv;
367 static bool_t
368 _nrc_lookup(struct url *urlp, bool_t only_pass)
370 struct nrc_node *nrc, *nrc_wild, *nrc_exact;
371 bool_t rv = FAL0;
372 NYD_ENTER;
374 assert(!only_pass || urlp->url_user.s != NULL);
375 assert(only_pass || urlp->url_user.s == NULL);
377 if (_nrc_list == NULL)
378 _nrc_init();
379 if (_nrc_list == NRC_NODE_ERR)
380 goto jleave;
382 nrc_wild = nrc_exact = NULL;
383 for (nrc = _nrc_list; nrc != NULL; nrc = nrc->nrc_next)
384 switch (__nrc_host_match(nrc, urlp)) {
385 case 1:
386 nrc->nrc_result = nrc_exact;
387 nrc_exact = nrc;
388 continue;
389 case -1:
390 nrc->nrc_result = nrc_wild;
391 nrc_wild = nrc;
392 /* FALLTHRU */
393 case 0:
394 continue;
397 if (!only_pass && urlp->url_user.s == NULL) {
398 /* Must be an unambiguous entry of its kind */
399 if (nrc_exact != NULL && nrc_exact->nrc_result != NULL)
400 goto jleave;
401 if (__nrc_find_user(urlp, nrc_exact))
402 goto j_user;
404 if (nrc_wild != NULL && nrc_wild->nrc_result != NULL)
405 goto jleave;
406 if (!__nrc_find_user(urlp, nrc_wild))
407 goto jleave;
408 j_user:
412 if (__nrc_find_pass(urlp, TRU1, nrc_exact) ||
413 __nrc_find_pass(urlp, TRU1, nrc_wild) ||
414 /* Do not try to find a password without exact user match unless we've
415 * been called during credential lookup, a.k.a. the second time */
416 !only_pass ||
417 __nrc_find_pass(urlp, FAL0, nrc_exact) ||
418 __nrc_find_pass(urlp, FAL0, nrc_wild))
419 rv = TRU1;
420 jleave:
421 NYD_LEAVE;
422 return rv;
425 static int
426 __nrc_host_match(struct nrc_node const *nrc, struct url const *urlp)
428 char const *d2, *d1;
429 size_t l2, l1;
430 int rv = 0;
431 NYD2_ENTER;
433 /* Find a matching machine -- entries are all lowercase normalized */
434 if (nrc->nrc_mlen == urlp->url_host.l) {
435 if (n_LIKELY(!memcmp(nrc->nrc_dat, urlp->url_host.s, urlp->url_host.l)))
436 rv = 1;
437 goto jleave;
440 /* Cannot be an exact match, but maybe the .netrc machine starts with
441 * a "*." glob, which we recognize as an extension, meaning "skip
442 * a single subdomain, then match the rest" */
443 d1 = nrc->nrc_dat + 2;
444 l1 = nrc->nrc_mlen;
445 if (l1 <= 2 || d1[-1] != '.' || d1[-2] != '*')
446 goto jleave;
447 l1 -= 2;
449 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
450 * in fact this even succeeds for ".host.com", but - why care, here? */
451 d2 = urlp->url_host.s;
452 l2 = urlp->url_host.l;
453 while (l2 > 0) {
454 --l2;
455 if (*d2++ == '.')
456 break;
459 if (l2 == l1 && !memcmp(d1, d2, l1))
460 /* This matches, but we won't use it directly but watch out for an
461 * exact match first! */
462 rv = -1;
463 jleave:
464 NYD2_LEAVE;
465 return rv;
468 static bool_t
469 __nrc_find_user(struct url *urlp, struct nrc_node const *nrc)
471 NYD2_ENTER;
473 for (; nrc != NULL; nrc = nrc->nrc_result)
474 if (nrc->nrc_ulen > 0) {
475 /* Fake it was part of URL otherwise XXX */
476 urlp->url_had_user = TRU1;
477 /* That buffer will be duplicated by url_parse() in this case! */
478 urlp->url_user.s = n_UNCONST(nrc->nrc_dat + nrc->nrc_mlen +1);
479 urlp->url_user.l = nrc->nrc_ulen;
480 break;
483 NYD2_LEAVE;
484 return (nrc != NULL);
487 static bool_t
488 __nrc_find_pass(struct url *urlp, bool_t user_match, struct nrc_node const *nrc)
490 NYD2_ENTER;
492 for (; nrc != NULL; nrc = nrc->nrc_result) {
493 bool_t um = (nrc->nrc_ulen == urlp->url_user.l &&
494 !memcmp(nrc->nrc_dat + nrc->nrc_mlen +1, urlp->url_user.s,
495 urlp->url_user.l));
497 if (user_match) {
498 if (!um)
499 continue;
500 } else if (!um && nrc->nrc_ulen > 0)
501 continue;
502 if (nrc->nrc_plen == 0)
503 continue;
505 /* We are responsible for duplicating this buffer! */
506 urlp->url_pass.s = savestrbuf(nrc->nrc_dat + nrc->nrc_mlen +1 +
507 nrc->nrc_ulen + 1, (urlp->url_pass.l = nrc->nrc_plen));
508 break;
511 NYD2_LEAVE;
512 return (nrc != NULL);
514 #endif /* HAVE_NETRC */
516 #ifdef HAVE_AGENT
517 static bool_t
518 _agent_shell_lookup(struct url *urlp, char const *comm) /* TODO v15-compat */
520 char buf[128];
521 char const *env_addon[8];
522 struct str s;
523 FILE *pbuf;
524 union {int c; sighandler_type sht;} u;
525 size_t cl, l;
526 bool_t rv = FAL0;
527 NYD2_ENTER;
529 env_addon[0] = str_concat_csvl(&s, "NAIL_USER", "=", urlp->url_user.s,
530 NULL)->s;
531 env_addon[1] = str_concat_csvl(&s,
532 "NAIL_USER_ENC", "=", urlp->url_user_enc.s, NULL)->s;
533 env_addon[2] = str_concat_csvl(&s, "NAIL_HOST", "=", urlp->url_host.s,
534 NULL)->s;
535 env_addon[3] = str_concat_csvl(&s, "NAIL_HOST_PORT", "=", urlp->url_h_p.s,
536 NULL)->s;
537 env_addon[4] = NULL;
539 if ((pbuf = Popen(comm, "r", ok_vlook(SHELL), env_addon, -1)) == NULL) {
540 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm);
541 goto jleave;
544 for (s.s = NULL, s.l = cl = l = 0; (u.c = getc(pbuf)) != EOF; ++cl) {
545 if (u.c == '\n') /* xxx */
546 continue;
547 buf[l++] = u.c;
548 if (l == sizeof(buf) - 1) {
549 n_str_add_buf(&s, buf, l);
550 l = 0;
553 if (l > 0)
554 n_str_add_buf(&s, buf, l);
556 if (!Pclose(pbuf, TRU1)) {
557 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm);
558 goto jleave;
561 /* We are responsible for duplicating this buffer! */
562 if (s.s != NULL)
563 urlp->url_pass.s = savestrbuf(s.s, urlp->url_pass.l = s.l);
564 else if (cl > 0)
565 urlp->url_pass.s = n_UNCONST(n_empty), urlp->url_pass.l = 0;
566 rv = TRU1;
567 jleave:
568 if (s.s != NULL)
569 free(s.s);
570 NYD2_LEAVE;
571 return rv;
573 #endif /* HAVE_AGENT */
575 FL char *
576 (urlxenc)(char const *cp, bool_t ispath n_MEMORY_DEBUG_ARGS)
578 char *n, *np, c1;
579 NYD2_ENTER;
581 /* C99 */{
582 size_t i;
584 i = strlen(cp);
585 if(i >= UIZ_MAX / 3){
586 n = NULL;
587 goto jleave;
589 i *= 3;
590 ++i;
591 np = n = (n_autorec_alloc)(NULL, i n_MEMORY_DEBUG_ARGSCALL);
594 for (; (c1 = *cp) != '\0'; ++cp) {
595 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
596 * ALPHA / DIGIT / "-" / "." / "_" / "~"
597 * However add a special is[file]path mode for file-system friendliness */
598 if (alnumchar(c1) || c1 == '_')
599 *np++ = c1;
600 else if (!ispath) {
601 if (c1 != '-' && c1 != '.' && c1 != '~')
602 goto jesc;
603 *np++ = c1;
604 } else if (PTRCMP(np, >, n) && (*cp == '-' || *cp == '.')) /* XXX imap */
605 *np++ = c1;
606 else {
607 jesc:
608 np[0] = '%';
609 n_c_to_hex_base16(np + 1, c1);
610 np += 3;
613 *np = '\0';
614 jleave:
615 NYD2_LEAVE;
616 return n;
619 FL char *
620 (urlxdec)(char const *cp n_MEMORY_DEBUG_ARGS)
622 char *n, *np;
623 si32_t c;
624 NYD2_ENTER;
626 np = n = (n_autorec_alloc)(NULL, strlen(cp) +1 n_MEMORY_DEBUG_ARGSCALL);
628 while ((c = (uc_i)*cp++) != '\0') {
629 if (c == '%' && cp[0] != '\0' && cp[1] != '\0') {
630 si32_t o = c;
631 if (n_LIKELY((c = n_c_from_hex_base16(cp)) >= '\0'))
632 cp += 2;
633 else
634 c = o;
636 *np++ = (char)c;
638 *np = '\0';
639 NYD2_LEAVE;
640 return n;
643 FL int
644 c_urlcodec(void *v){
645 bool_t ispath;
646 char const **argv, *cp, *res;
647 NYD_ENTER;
649 if(*(cp = *(argv = v)) == 'p'){
650 if(!ascncasecmp(++cp, "ath", 3))
651 cp += 3;
652 ispath = TRU1;
655 if(is_asccaseprefix(cp, "encode")){
656 while((cp = *++argv) != NULL){
657 if((res = urlxenc(cp, ispath)) == NULL)
658 res = V_(n_error);
659 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
660 cp, strlen(cp), res, strlen(res));
662 }else if(is_asccaseprefix(cp, "decode")){
663 struct str in, out;
665 while((cp = *++argv) != NULL){
666 if((res = urlxdec(cp)) == NULL)
667 res = V_(n_error);
668 in.l = strlen(in.s = n_UNCONST(res)); /* logical */
669 makeprint(&in, &out);
670 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
671 cp, strlen(cp), out.s, in.l);
672 free(out.s);
674 }else{
675 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv);
676 cp = NULL;
678 NYD_LEAVE;
679 return (cp != NULL ? OKAY : STOP);
682 FL int
683 c_urlencode(void *v) /* XXX IDNA?? */
685 char **ap;
686 NYD_ENTER;
688 n_OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
690 for (ap = v; *ap != NULL; ++ap) {
691 char *in = *ap, *out = urlxenc(in, FAL0);
693 if(out == NULL)
694 out = n_UNCONST(V_(n_error));
695 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
696 in, strlen(in), out, strlen(out));
698 NYD_LEAVE;
699 return 0;
702 FL int
703 c_urldecode(void *v) /* XXX IDNA?? */
705 char **ap;
706 NYD_ENTER;
708 n_OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
710 for (ap = v; *ap != NULL; ++ap) {
711 char *in = *ap, *out = urlxdec(in);
713 if(out == NULL)
714 out = n_UNCONST(V_(n_error));
715 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
716 in, strlen(in), out, strlen(out));
718 NYD_LEAVE;
719 return 0;
722 FL char *
723 url_mailto_to_address(char const *mailtop){ /* TODO hack! RFC 6068; factory? */
724 size_t i;
725 char *rv;
726 char const *mailtop_orig;
727 NYD_ENTER;
729 if(!is_prefix("mailto:", mailtop_orig = mailtop)){
730 rv = NULL;
731 goto jleave;
733 mailtop += sizeof("mailto:") -1;
735 /* TODO This is all intermediate, and for now just enough to understand
736 * TODO a little bit of a little more advanced List-Post: headers. */
737 /* Strip any hfield additions, keep only to addr-spec's */
738 if((rv = strchr(mailtop, '?')) != NULL)
739 rv = savestrbuf(mailtop, i = PTR2SIZE(rv - mailtop));
740 else
741 rv = savestrbuf(mailtop, i = strlen(mailtop));
743 i = strlen(rv);
745 /* Simply perform percent-decoding if there is a percent % */
746 if(memchr(rv, '%', i) != NULL){
747 char *rv_base;
748 bool_t err;
750 for(err = FAL0, mailtop = rv_base = rv; i > 0;){
751 char c;
753 if((c = *mailtop++) == '%'){
754 si32_t cc;
756 if(i < 3 || (cc = n_c_from_hex_base16(mailtop)) < 0){
757 if(!err && (err = TRU1, n_poption & n_PO_D_V))
758 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
759 n_shexp_quote_cp(mailtop_orig, FAL0));
760 goto jhex_putc;
762 *rv++ = (char)cc;
763 mailtop += 2;
764 i -= 3;
765 }else{
766 jhex_putc:
767 *rv++ = c;
768 --i;
771 *rv = '\0';
772 rv = rv_base;
774 jleave:
775 NYD_LEAVE;
776 return rv;
779 FL char const *
780 n_servbyname(char const *proto, ui16_t *irv_or_null){
781 static struct{
782 char const name[14];
783 char const port[8];
784 ui16_t portno;
785 } const tbl[] = {
786 { "smtp", "25", 25},
787 { "submission", "587", 587},
788 { "smtps", "465", 465},
789 { "pop3", "110", 110},
790 { "pop3s", "995", 995},
791 { "imap", "143", 143},
792 { "imaps", "993", 993},
793 { "file", "", 0}
795 char const *rv;
796 size_t l, i;
797 NYD2_ENTER;
799 for(rv = proto; *rv != '\0'; ++rv)
800 if(*rv == ':')
801 break;
802 l = PTR2SIZE(rv - proto);
804 for(rv = NULL, i = 0; i < n_NELEM(tbl); ++i)
805 if(!ascncasecmp(tbl[i].name, proto, l)){
806 rv = tbl[i].port;
807 if(irv_or_null != NULL)
808 *irv_or_null = tbl[i].portno;
809 break;
811 NYD2_LEAVE;
812 return rv;
815 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
816 FL bool_t
817 url_parse(struct url *urlp, enum cproto cproto, char const *data)
819 #if defined HAVE_SMTP && defined HAVE_POP3
820 # define __ALLPROTO
821 #endif
822 #if defined HAVE_SMTP || defined HAVE_POP3
823 # define __ANYPROTO
824 char *cp, *x;
825 #endif
826 bool_t rv = FAL0;
827 NYD_ENTER;
828 n_UNUSED(data);
830 memset(urlp, 0, sizeof *urlp);
831 urlp->url_input = data;
832 urlp->url_cproto = cproto;
834 /* Network protocol */
835 #define _protox(X,Y) \
836 urlp->url_portno = Y;\
837 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
838 urlp->url_proto[sizeof(X) -1] = '\0';\
839 urlp->url_proto_len = sizeof(X) -1;\
840 urlp->url_proto_xlen = sizeof(X "://") -1
841 #define __if(X,Y,Z) \
842 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
843 _protox(X, Y);\
844 data += sizeof(X "://") -1;\
845 do { Z; } while (0);\
846 goto juser;\
848 #define _if(X,Y) __if(X, Y, (void)0)
849 #ifdef HAVE_SSL
850 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
851 #else
852 # define _ifs(X,Y) goto jeproto;
853 #endif
855 switch (cproto) {
856 case CPROTO_CCRED:
857 /* The special S/MIME etc. credential lookup */
858 #if defined HAVE_SSL
859 _protox("ccred", 0);
860 break;
861 #else
862 goto jeproto;
863 #endif
864 case CPROTO_SMTP:
865 #ifdef HAVE_SMTP
866 _if ("smtp", 25)
867 _if ("submission", 587)
868 _ifs ("smtps", 465)
869 _protox("smtp", 25);
870 break;
871 #else
872 goto jeproto;
873 #endif
874 case CPROTO_POP3:
875 #ifdef HAVE_POP3
876 _if ("pop3", 110)
877 _ifs ("pop3s", 995)
878 _protox("pop3", 110);
879 break;
880 #else
881 goto jeproto;
882 #endif
883 #if 0
884 case CPROTO_IMAP:
885 _if ("imap", 143)
886 _ifs ("imaps", 993)
887 _protox("imap", 143);
888 break;
889 goto jeproto;
890 #endif
893 #undef _ifs
894 #undef _if
895 #undef __if
896 #undef _protox
898 if (strstr(data, "://") != NULL) {
899 #if !defined __ALLPROTO || !defined HAVE_SSL
900 jeproto:
901 #endif
902 n_err(_("URL proto:// prefix invalid: %s\n"), urlp->url_input);
903 goto jleave;
905 #ifdef __ANYPROTO
907 /* User and password, I */
908 juser:
909 if ((cp = _url_last_at_before_slash(data)) != NULL) {
910 size_t l;
911 char const *urlpe, *d;
912 char *ub;
914 l = PTR2SIZE(cp - data);
915 ub = ac_alloc(l +1);
916 d = data;
917 urlp->url_had_user = TRU1;
918 data = &cp[1];
920 /* And also have a password? */
921 if((cp = memchr(d, ':', l)) != NULL){
922 size_t i = PTR2SIZE(cp - d);
924 l -= i + 1;
925 memcpy(ub, cp + 1, l);
926 ub[l] = '\0';
928 if((urlp->url_pass.s = urlxdec(ub)) == NULL)
929 goto jurlp_err;
930 urlp->url_pass.l = strlen(urlp->url_pass.s);
931 if((urlpe = urlxenc(urlp->url_pass.s, FAL0)) == NULL)
932 goto jurlp_err;
933 if(strcmp(ub, urlpe))
934 goto jurlp_err;
935 l = i;
938 memcpy(ub, d, l);
939 ub[l] = '\0';
940 if((urlp->url_user.s = urlxdec(ub)) == NULL)
941 goto jurlp_err;
942 urlp->url_user.l = strlen(urlp->url_user.s);
943 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL)
944 goto jurlp_err;
945 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
947 if(urlp->url_user_enc.l != l || memcmp(urlp->url_user_enc.s, ub, l)){
948 jurlp_err:
949 n_err(_("String is not properly URL percent encoded: %s\n"), ub);
950 d = NULL;
953 ac_free(ub);
954 if(d == NULL)
955 goto jleave;
958 /* Servername and port -- and possible path suffix */
959 if ((cp = strchr(data, ':')) != NULL) { /* TODO URL parse, IPv6 support */
960 char *eptr;
961 long l;
963 urlp->url_port = x = savestr(x = &cp[1]);
964 if ((x = strchr(x, '/')) != NULL) {
965 *x = '\0';
966 while(*++x == '/')
969 l = strtol(urlp->url_port, &eptr, 10);
970 if (*eptr != '\0' || l <= 0 || UICMP(32, l, >=, 0xFFFFu)) {
971 n_err(_("URL with invalid port number: %s\n"), urlp->url_input);
972 goto jleave;
974 urlp->url_portno = (ui16_t)l;
975 } else {
976 if ((x = strchr(data, '/')) != NULL) {
977 data = savestrbuf(data, PTR2SIZE(x - data));
978 while(*++x == '/')
981 cp = n_UNCONST(data + strlen(data));
984 /* A (non-empty) path may only occur with IMAP */
985 if (x != NULL && *x != '\0') {
986 /* Take care not to count adjacent slashes for real, on either end */
987 char *x2;
988 size_t i;
990 for(x2 = savestrbuf(x, i = strlen(x)); i > 0; --i)
991 if(x2[i - 1] != '/')
992 break;
993 x2[i] = '\0';
995 if (i > 0) {
996 #if 0
997 if (cproto != CPROTO_IMAP) {
998 #endif
999 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
1000 urlp->url_input);
1001 goto jleave;
1002 #if 0
1004 urlp->url_path.l = i;
1005 urlp->url_path.s = x2;
1006 #endif
1010 urlp->url_host.s = savestrbuf(data, urlp->url_host.l = PTR2SIZE(cp - data));
1011 { size_t i;
1012 for (cp = urlp->url_host.s, i = urlp->url_host.l; i != 0; ++cp, --i)
1013 *cp = lowerconv(*cp);
1016 /* .url_h_p: HOST:PORT */
1017 { size_t i;
1018 struct str *s = &urlp->url_h_p;
1020 s->s = salloc(urlp->url_host.l + 1 + sizeof("65536")-1 +1);
1021 memcpy(s->s, urlp->url_host.s, i = urlp->url_host.l);
1022 if (urlp->url_port != NULL) {
1023 size_t j = strlen(urlp->url_port);
1024 s->s[i++] = ':';
1025 memcpy(s->s + i, urlp->url_port, j);
1026 i += j;
1028 s->s[i] = '\0';
1029 s->l = i;
1032 /* User, II
1033 * If there was no user in the URL, do we have *user-HOST* or *user*? */
1034 if (!urlp->url_had_user) {
1035 if ((urlp->url_user.s = xok_vlook(user, urlp, OXM_PLAIN | OXM_H_P))
1036 == NULL) {
1037 /* No, check whether .netrc lookup is desired */
1038 # ifdef HAVE_NETRC
1039 if (!ok_blook(v15_compat) ||
1040 !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) ||
1041 !_nrc_lookup(urlp, FAL0))
1042 # endif
1043 urlp->url_user.s = n_UNCONST(ok_vlook(LOGNAME));
1046 urlp->url_user.l = strlen(urlp->url_user.s);
1047 urlp->url_user.s = savestrbuf(urlp->url_user.s, urlp->url_user.l);
1048 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL){
1049 n_err(_("Cannot URL encode %s\n"), urlp->url_user.s);
1050 goto jleave;
1052 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
1055 /* And then there are a lot of prebuild string combinations TODO do lazy */
1057 /* .url_u_h: .url_user@.url_host
1058 * For SMTP we apply ridiculously complicated *v15-compat* plus
1059 * *smtp-hostname* / *hostname* dependent rules */
1060 { struct str h, *s;
1061 size_t i;
1063 if (cproto == CPROTO_SMTP && ok_blook(v15_compat) &&
1064 (cp = ok_vlook(smtp_hostname)) != NULL) {
1065 if (*cp == '\0')
1066 cp = nodename(1);
1067 h.s = savestrbuf(cp, h.l = strlen(cp));
1068 } else
1069 h = urlp->url_host;
1071 s = &urlp->url_u_h;
1072 i = urlp->url_user.l;
1074 s->s = salloc(i + 1 + h.l +1);
1075 if (i > 0) {
1076 memcpy(s->s, urlp->url_user.s, i);
1077 s->s[i++] = '@';
1079 memcpy(s->s + i, h.s, h.l +1);
1080 i += h.l;
1081 s->l = i;
1084 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1085 { struct str *s = &urlp->url_u_h_p;
1086 size_t i = urlp->url_user.l;
1088 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1089 if (i > 0) {
1090 memcpy(s->s, urlp->url_user.s, i);
1091 s->s[i++] = '@';
1093 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1094 i += urlp->url_h_p.l;
1095 s->l = i;
1098 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1099 { struct str *s = &urlp->url_eu_h_p;
1100 size_t i = urlp->url_user_enc.l;
1102 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1103 if (i > 0) {
1104 memcpy(s->s, urlp->url_user_enc.s, i);
1105 s->s[i++] = '@';
1107 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1108 i += urlp->url_h_p.l;
1109 s->l = i;
1112 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1113 { size_t i;
1114 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_u_h_p.l) +1);
1116 urlp->url_proto[urlp->url_proto_len] = ':';
1117 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_u_h_p.s,
1118 urlp->url_u_h_p.l +1);
1119 urlp->url_proto[urlp->url_proto_len] = '\0';
1121 urlp->url_p_u_h_p = ud;
1124 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1125 { size_t i;
1126 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_eu_h_p.l) +
1127 1 + urlp->url_path.l +1);
1129 urlp->url_proto[urlp->url_proto_len] = ':';
1130 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_eu_h_p.s,
1131 urlp->url_eu_h_p.l +1);
1132 urlp->url_proto[urlp->url_proto_len] = '\0';
1134 if (urlp->url_path.l == 0)
1135 urlp->url_p_eu_h_p = urlp->url_p_eu_h_p_p = ud;
1136 else {
1137 urlp->url_p_eu_h_p = savestrbuf(ud, i);
1138 urlp->url_p_eu_h_p_p = ud;
1139 ud += i;
1140 *ud++ = '/';
1141 memcpy(ud, urlp->url_path.s, urlp->url_path.l +1);
1145 rv = TRU1;
1146 #endif /* __ANYPROTO */
1147 jleave:
1148 NYD_LEAVE;
1149 return rv;
1150 #undef __ANYPROTO
1151 #undef __ALLPROTO
1154 FL bool_t
1155 ccred_lookup_old(struct ccred *ccp, enum cproto cproto, char const *addr)
1157 char const *pname, *pxstr, *authdef;
1158 size_t pxlen, addrlen, i;
1159 char *vbuf, *s;
1160 ui8_t authmask;
1161 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1162 ware = NONE;
1163 bool_t addr_is_nuser = FAL0; /* XXX v15.0 legacy! v15_compat */
1164 NYD_ENTER;
1166 n_OBSOLETE(_("Use of old-style credentials, which will vanish in v15!\n"
1167 " Please read the manual section "
1168 "\"On URL syntax and credential lookup\""));
1170 memset(ccp, 0, sizeof *ccp);
1172 switch (cproto) {
1173 default:
1174 case CPROTO_SMTP:
1175 pname = "SMTP";
1176 pxstr = "smtp-auth";
1177 pxlen = sizeof("smtp-auth") -1;
1178 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1179 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1180 authdef = "none";
1181 addr_is_nuser = TRU1;
1182 break;
1183 case CPROTO_POP3:
1184 pname = "POP3";
1185 pxstr = "pop3-auth";
1186 pxlen = sizeof("pop3-auth") -1;
1187 authmask = AUTHTYPE_PLAIN;
1188 authdef = "plain";
1189 break;
1192 ccp->cc_cproto = cproto;
1193 addrlen = strlen(addr);
1194 vbuf = ac_alloc(pxlen + addrlen + sizeof("-password-")-1 +1);
1195 memcpy(vbuf, pxstr, pxlen);
1197 /* Authentication type */
1198 vbuf[pxlen] = '-';
1199 memcpy(vbuf + pxlen + 1, addr, addrlen +1);
1200 if ((s = vok_vlook(vbuf)) == NULL) {
1201 vbuf[pxlen] = '\0';
1202 if ((s = vok_vlook(vbuf)) == NULL)
1203 s = n_UNCONST(authdef);
1206 if (!asccasecmp(s, "none")) {
1207 ccp->cc_auth = "NONE";
1208 ccp->cc_authtype = AUTHTYPE_NONE;
1209 /*ware = NONE;*/
1210 } else if (!asccasecmp(s, "plain")) {
1211 ccp->cc_auth = "PLAIN";
1212 ccp->cc_authtype = AUTHTYPE_PLAIN;
1213 ware = REQ_PASS | REQ_USER;
1214 } else if (!asccasecmp(s, "login")) {
1215 ccp->cc_auth = "LOGIN";
1216 ccp->cc_authtype = AUTHTYPE_LOGIN;
1217 ware = REQ_PASS | REQ_USER;
1218 } else if (!asccasecmp(s, "cram-md5")) {
1219 ccp->cc_auth = "CRAM-MD5";
1220 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1221 ware = REQ_PASS | REQ_USER;
1222 } else if (!asccasecmp(s, "gssapi")) {
1223 ccp->cc_auth = "GSS-API";
1224 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1225 ware = REQ_USER;
1226 } /* no else */
1228 /* Verify method */
1229 if (!(ccp->cc_authtype & authmask)) {
1230 n_err(_("Unsupported %s authentication method: %s\n"), pname, s);
1231 ccp = NULL;
1232 goto jleave;
1234 # ifndef HAVE_MD5
1235 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1236 n_err(_("No CRAM-MD5 support compiled in\n"));
1237 ccp = NULL;
1238 goto jleave;
1240 # endif
1241 # ifndef HAVE_GSSAPI
1242 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1243 n_err(_("No GSS-API support compiled in\n"));
1244 ccp = NULL;
1245 goto jleave;
1247 # endif
1249 /* User name */
1250 if (!(ware & (WANT_USER | REQ_USER)))
1251 goto jpass;
1253 if (!addr_is_nuser) {
1254 if ((s = _url_last_at_before_slash(addr)) != NULL) {
1255 char *cp;
1257 cp = savestrbuf(addr, PTR2SIZE(s - addr));
1259 if((ccp->cc_user.s = urlxdec(cp)) == NULL){
1260 n_err(_("String is not properly URL percent encoded: %s\n"), cp);
1261 ccp = NULL;
1262 goto jleave;
1264 ccp->cc_user.l = strlen(ccp->cc_user.s);
1265 } else if (ware & REQ_USER)
1266 goto jgetuser;
1267 goto jpass;
1270 memcpy(vbuf + pxlen, "-user-", i = sizeof("-user-") -1);
1271 i += pxlen;
1272 memcpy(vbuf + i, addr, addrlen +1);
1273 if ((s = vok_vlook(vbuf)) == NULL) {
1274 vbuf[--i] = '\0';
1275 if ((s = vok_vlook(vbuf)) == NULL && (ware & REQ_USER)) {
1276 if ((s = getuser(NULL)) == NULL) {
1277 jgetuser: /* TODO v15.0: today we simply bail, but we should call getuser().
1278 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1279 * TODO check that first, then! change control flow, grow vbuf */
1280 n_err(_("A user is necessary for %s authentication\n"), pname);
1281 ccp = NULL;
1282 goto jleave;
1286 ccp->cc_user.l = strlen(ccp->cc_user.s = savestr(s));
1288 /* Password */
1289 jpass:
1290 if (!(ware & (WANT_PASS | REQ_PASS)))
1291 goto jleave;
1293 if (!addr_is_nuser) {
1294 memcpy(vbuf, "password-", i = sizeof("password-") -1);
1295 } else {
1296 memcpy(vbuf + pxlen, "-password-", i = sizeof("-password-") -1);
1297 i += pxlen;
1299 memcpy(vbuf + i, addr, addrlen +1);
1300 if ((s = vok_vlook(vbuf)) == NULL) {
1301 vbuf[--i] = '\0';
1302 if ((!addr_is_nuser || (s = vok_vlook(vbuf)) == NULL) &&
1303 (ware & REQ_PASS)) {
1304 if ((s = getpassword(savecat(_("Password for "), pname))) == NULL) {
1305 n_err(_("A password is necessary for %s authentication\n"),
1306 pname);
1307 ccp = NULL;
1308 goto jleave;
1312 if (s != NULL)
1313 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1315 jleave:
1316 ac_free(vbuf);
1317 if (ccp != NULL && (n_poption & n_PO_D_VV))
1318 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1319 addr, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1320 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1321 NYD_LEAVE;
1322 return (ccp != NULL);
1325 FL bool_t
1326 ccred_lookup(struct ccred *ccp, struct url *urlp)
1328 char *s;
1329 char const *pstr, *authdef;
1330 ui8_t authmask;
1331 enum okeys authokey;
1332 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1333 ware;
1334 NYD_ENTER;
1336 memset(ccp, 0, sizeof *ccp);
1337 ccp->cc_user = urlp->url_user;
1339 ware = NONE;
1341 switch ((ccp->cc_cproto = urlp->url_cproto)) {
1342 case CPROTO_CCRED:
1343 authokey = (enum okeys)-1;
1344 authmask = AUTHTYPE_PLAIN;
1345 authdef = "plain";
1346 pstr = "ccred";
1347 break;
1348 default:
1349 case CPROTO_SMTP:
1350 authokey = ok_v_smtp_auth;
1351 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1352 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1353 authdef = "plain";
1354 pstr = "smtp";
1355 break;
1356 case CPROTO_POP3:
1357 authokey = ok_v_pop3_auth;
1358 authmask = AUTHTYPE_PLAIN;
1359 authdef = "plain";
1360 pstr = "pop3";
1361 break;
1364 /* Authentication type */
1365 if (authokey == (enum okeys)-1 ||
1366 (s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL)
1367 s = n_UNCONST(authdef);
1369 if (!asccasecmp(s, "none")) {
1370 ccp->cc_auth = "NONE";
1371 ccp->cc_authtype = AUTHTYPE_NONE;
1372 /*ware = NONE;*/
1373 } else if (!asccasecmp(s, "plain")) {
1374 ccp->cc_auth = "PLAIN";
1375 ccp->cc_authtype = AUTHTYPE_PLAIN;
1376 ware = REQ_PASS | REQ_USER;
1377 } else if (!asccasecmp(s, "login")) {
1378 ccp->cc_auth = "LOGIN";
1379 ccp->cc_authtype = AUTHTYPE_LOGIN;
1380 ware = REQ_PASS | REQ_USER;
1381 } else if (!asccasecmp(s, "cram-md5")) {
1382 ccp->cc_auth = "CRAM-MD5";
1383 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1384 ware = REQ_PASS | REQ_USER;
1385 } else if (!asccasecmp(s, "gssapi")) {
1386 ccp->cc_auth = "GSS-API";
1387 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1388 ware = REQ_USER;
1389 } /* no else */
1391 /* Verify method */
1392 if (!(ccp->cc_authtype & authmask)) {
1393 n_err(_("Unsupported %s authentication method: %s\n"), pstr, s);
1394 ccp = NULL;
1395 goto jleave;
1397 # ifndef HAVE_MD5
1398 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1399 n_err(_("No CRAM-MD5 support compiled in\n"));
1400 ccp = NULL;
1401 goto jleave;
1403 # endif
1404 # ifndef HAVE_GSSAPI
1405 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1406 n_err(_("No GSS-API support compiled in\n"));
1407 ccp = NULL;
1408 goto jleave;
1410 # endif
1412 /* Password */
1413 ccp->cc_pass = urlp->url_pass;
1414 if (ccp->cc_pass.s != NULL)
1415 goto jleave;
1417 if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL)
1418 goto js2pass;
1419 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1420 if ((s = xok_vlook(agent_shell_lookup, urlp, OXM_ALL)) != NULL) {
1421 n_OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1422 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1423 if (!_agent_shell_lookup(urlp, s)) {
1424 ccp = NULL;
1425 goto jleave;
1426 } else if (urlp->url_pass.s != NULL) {
1427 ccp->cc_pass = urlp->url_pass;
1428 goto jleave;
1431 # endif
1432 # ifdef HAVE_NETRC
1433 if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) {
1434 ccp->cc_pass = urlp->url_pass;
1435 goto jleave;
1437 # endif
1438 if (ware & REQ_PASS) {
1439 if((s = getpassword(savecat(urlp->url_u_h.s, _(" requires a password: ")))
1440 ) != NULL)
1441 js2pass:
1442 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1443 else {
1444 n_err(_("A password is necessary for %s authentication\n"), pstr);
1445 ccp = NULL;
1449 jleave:
1450 if(ccp != NULL && (n_poption & n_PO_D_VV))
1451 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1452 urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1453 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1454 NYD_LEAVE;
1455 return (ccp != NULL);
1457 #endif /* HAVE_SOCKETS */
1459 #ifdef HAVE_NETRC
1460 FL int
1461 c_netrc(void *v)
1463 char **argv = v;
1464 struct nrc_node *nrc;
1465 bool_t load_only;
1466 NYD_ENTER;
1468 load_only = FAL0;
1469 if (*argv == NULL)
1470 goto jlist;
1471 if (argv[1] != NULL)
1472 goto jerr;
1473 if (!asccasecmp(*argv, "show"))
1474 goto jlist;
1475 load_only = TRU1;
1476 if (!asccasecmp(*argv, "load"))
1477 goto jlist;
1478 if (!asccasecmp(*argv, "clear"))
1479 goto jclear;
1480 jerr:
1481 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1482 v = NULL;
1483 jleave:
1484 NYD_LEAVE;
1485 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1487 jlist: {
1488 FILE *fp;
1489 size_t l;
1491 if (_nrc_list == NULL)
1492 _nrc_init();
1493 if (_nrc_list == NRC_NODE_ERR) {
1494 n_err(_("Interpolate what file?\n"));
1495 v = NULL;
1496 goto jleave;
1498 if (load_only)
1499 goto jleave;
1501 if ((fp = Ftmp(NULL, "netrc", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL) {
1502 n_perr(_("tmpfile"), 0);
1503 v = NULL;
1504 goto jleave;
1507 for (l = 0, nrc = _nrc_list; nrc != NULL; ++l, nrc = nrc->nrc_next) {
1508 fprintf(fp, _("machine %s "), nrc->nrc_dat); /* XXX quote? */
1509 if (nrc->nrc_ulen > 0)
1510 fprintf(fp, _("login \"%s\" "),
1511 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1));
1512 if (nrc->nrc_plen > 0)
1513 fprintf(fp, _("password \"%s\"\n"),
1514 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1 + nrc->nrc_ulen +1));
1515 else
1516 putc('\n', fp);
1519 page_or_print(fp, l);
1520 Fclose(fp);
1522 goto jleave;
1524 jclear:
1525 if (_nrc_list == NRC_NODE_ERR)
1526 _nrc_list = NULL;
1527 while ((nrc = _nrc_list) != NULL) {
1528 _nrc_list = nrc->nrc_next;
1529 free(nrc);
1531 goto jleave;
1533 #endif /* HAVE_NETRC */
1535 #ifdef HAVE_MD5
1536 FL char *
1537 md5tohex(char hex[MD5TOHEX_SIZE], void const *vp)
1539 char const *cp = vp;
1540 size_t i, j;
1541 NYD_ENTER;
1543 for (i = 0; i < MD5TOHEX_SIZE / 2; ++i) {
1544 j = i << 1;
1545 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1546 hex[j] = __hex((cp[i] & 0xF0) >> 4);
1547 hex[++j] = __hex(cp[i] & 0x0F);
1548 # undef __hex
1550 NYD_LEAVE;
1551 return hex;
1554 FL char *
1555 cram_md5_string(struct str const *user, struct str const *pass,
1556 char const *b64)
1558 struct str in, out;
1559 char digest[16], *cp;
1560 NYD_ENTER;
1562 out.s = NULL;
1563 if(user->l >= UIZ_MAX - 1 - MD5TOHEX_SIZE - 1)
1564 goto jleave;
1565 if(pass->l >= INT_MAX)
1566 goto jleave;
1568 in.s = n_UNCONST(b64);
1569 in.l = strlen(in.s);
1570 if(!b64_decode(&out, &in))
1571 goto jleave;
1572 if(out.l >= INT_MAX){
1573 free(out.s);
1574 out.s = NULL;
1575 goto jleave;
1578 hmac_md5((uc_i*)out.s, out.l, (uc_i*)pass->s, pass->l, digest);
1579 free(out.s);
1580 cp = md5tohex(salloc(MD5TOHEX_SIZE +1), digest);
1582 in.l = user->l + MD5TOHEX_SIZE +1;
1583 in.s = ac_alloc(user->l + 1 + MD5TOHEX_SIZE +1);
1584 memcpy(in.s, user->s, user->l);
1585 in.s[user->l] = ' ';
1586 memcpy(&in.s[user->l + 1], cp, MD5TOHEX_SIZE);
1587 if(b64_encode(&out, &in, B64_SALLOC | B64_CRLF) == NULL)
1588 out.s = NULL;
1589 ac_free(in.s);
1590 jleave:
1591 NYD_LEAVE;
1592 return out.s;
1595 FL void
1596 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
1597 void *digest)
1600 * This code is taken from
1602 * Network Working Group H. Krawczyk
1603 * Request for Comments: 2104 IBM
1604 * Category: Informational M. Bellare
1605 * UCSD
1606 * R. Canetti
1607 * IBM
1608 * February 1997
1611 * HMAC: Keyed-Hashing for Message Authentication
1613 md5_ctx context;
1614 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
1615 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
1616 unsigned char tk[16];
1617 int i;
1618 NYD_ENTER;
1620 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1621 if (key_len > 64) {
1622 md5_ctx tctx;
1624 md5_init(&tctx);
1625 md5_update(&tctx, key, key_len);
1626 md5_final(tk, &tctx);
1628 key = tk;
1629 key_len = 16;
1632 /* the HMAC_MD5 transform looks like:
1634 * MD5(K XOR opad, MD5(K XOR ipad, text))
1636 * where K is an n byte key
1637 * ipad is the byte 0x36 repeated 64 times
1638 * opad is the byte 0x5c repeated 64 times
1639 * and text is the data being protected */
1641 /* start out by storing key in pads */
1642 memset(k_ipad, 0, sizeof k_ipad);
1643 memset(k_opad, 0, sizeof k_opad);
1644 memcpy(k_ipad, key, key_len);
1645 memcpy(k_opad, key, key_len);
1647 /* XOR key with ipad and opad values */
1648 for (i=0; i<64; i++) {
1649 k_ipad[i] ^= 0x36;
1650 k_opad[i] ^= 0x5c;
1653 /* perform inner MD5 */
1654 md5_init(&context); /* init context for 1st pass */
1655 md5_update(&context, k_ipad, 64); /* start with inner pad */
1656 md5_update(&context, text, text_len); /* then text of datagram */
1657 md5_final(digest, &context); /* finish up 1st pass */
1659 /* perform outer MD5 */
1660 md5_init(&context); /* init context for 2nd pass */
1661 md5_update(&context, k_opad, 64); /* start with outer pad */
1662 md5_update(&context, digest, 16); /* then results of 1st hash */
1663 md5_final(digest, &context); /* finish up 2nd pass */
1664 NYD_LEAVE;
1666 #endif /* HAVE_MD5 */
1668 /* s-it-mode */