privsep.c: and just verify the box is also in CWD (wapiflapi)
[s-mailx.git] / urlcrecry.c
blob19e2309cea5602c5cc4f0ec1ab43a5699c292797
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 (options & OPT_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, AGENT_USER, "=", urlp->url_user.s,
530 NULL)->s;
531 env_addon[1] = str_concat_csvl(&s, AGENT_USER_ENC, "=", urlp->url_user_enc.s,
532 NULL)->s;
533 env_addon[2] = str_concat_csvl(&s, AGENT_HOST, "=", urlp->url_host.s,
534 NULL)->s;
535 env_addon[3] = str_concat_csvl(&s, AGENT_HOST_PORT, "=", urlp->url_h_p.s,
536 NULL)->s;
537 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, /* TODO v15 */
538 "=", ok_vlook(TMPDIR), NULL)->s;
540 env_addon[5] = NULL;
542 if ((pbuf = Popen(comm, "r", ok_vlook(SHELL), env_addon, -1)) == NULL) {
543 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm);
544 goto jleave;
547 for (s.s = NULL, s.l = cl = l = 0; (u.c = getc(pbuf)) != EOF; ++cl) {
548 if (u.c == '\n') /* xxx */
549 continue;
550 buf[l++] = u.c;
551 if (l == sizeof(buf) - 1) {
552 n_str_add_buf(&s, buf, l);
553 l = 0;
556 if (l > 0)
557 n_str_add_buf(&s, buf, l);
559 if (!Pclose(pbuf, TRU1)) {
560 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm);
561 goto jleave;
564 /* We are responsible for duplicating this buffer! */
565 if (s.s != NULL)
566 urlp->url_pass.s = savestrbuf(s.s, urlp->url_pass.l = s.l);
567 else if (cl > 0)
568 urlp->url_pass.s = n_UNCONST(n_empty), urlp->url_pass.l = 0;
569 rv = TRU1;
570 jleave:
571 if (s.s != NULL)
572 free(s.s);
573 NYD2_LEAVE;
574 return rv;
576 #endif /* HAVE_AGENT */
578 FL char *
579 (urlxenc)(char const *cp, bool_t ispath n_MEMORY_DEBUG_ARGS)
581 char *n, *np, c1;
582 NYD2_ENTER;
584 /* C99 */{
585 size_t i;
587 i = strlen(cp);
588 if(i >= UIZ_MAX / 3){
589 n = NULL;
590 goto jleave;
592 i *= 3;
593 ++i;
594 np = n = (n_autorec_alloc)(NULL, i n_MEMORY_DEBUG_ARGSCALL);
597 for (; (c1 = *cp) != '\0'; ++cp) {
598 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
599 * ALPHA / DIGIT / "-" / "." / "_" / "~"
600 * However add a special is[file]path mode for file-system friendliness */
601 if (alnumchar(c1) || c1 == '_')
602 *np++ = c1;
603 else if (!ispath) {
604 if (c1 != '-' && c1 != '.' && c1 != '~')
605 goto jesc;
606 *np++ = c1;
607 } else if (PTRCMP(np, >, n) && (*cp == '-' || *cp == '.')) /* XXX imap */
608 *np++ = c1;
609 else {
610 jesc:
611 np[0] = '%';
612 n_c_to_hex_base16(np + 1, c1);
613 np += 3;
616 *np = '\0';
617 jleave:
618 NYD2_LEAVE;
619 return n;
622 FL char *
623 (urlxdec)(char const *cp n_MEMORY_DEBUG_ARGS)
625 char *n, *np;
626 si32_t c;
627 NYD2_ENTER;
629 np = n = (n_autorec_alloc)(NULL, strlen(cp) +1 n_MEMORY_DEBUG_ARGSCALL);
631 while ((c = (uc_i)*cp++) != '\0') {
632 if (c == '%' && cp[0] != '\0' && cp[1] != '\0') {
633 si32_t o = c;
634 if (n_LIKELY((c = n_c_from_hex_base16(cp)) >= '\0'))
635 cp += 2;
636 else
637 c = o;
639 *np++ = (char)c;
641 *np = '\0';
642 NYD2_LEAVE;
643 return n;
646 FL int
647 c_urlcodec(void *v){
648 bool_t ispath;
649 char const **argv, *cp, *res;
650 NYD_ENTER;
652 if(*(cp = *(argv = v)) == 'p'){
653 if(!ascncasecmp(++cp, "ath", 3))
654 cp += 3;
655 ispath = TRU1;
658 if(is_prefix(cp, "encode")){
659 while((cp = *++argv) != NULL){
660 if((res = urlxenc(cp, ispath)) == NULL)
661 res = V_(n_error);
662 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
663 cp, strlen(cp), res, strlen(res));
665 }else if(is_prefix(cp, "decode")){
666 struct str in, out;
668 while((cp = *++argv) != NULL){
669 if((res = urlxdec(cp)) == NULL)
670 res = V_(n_error);
671 in.l = strlen(in.s = n_UNCONST(res)); /* logical */
672 makeprint(&in, &out);
673 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
674 cp, strlen(cp), out.s, in.l);
675 free(out.s);
677 }else{
678 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv);
679 cp = NULL;
681 NYD_LEAVE;
682 return (cp != NULL ? OKAY : STOP);
685 FL int
686 c_urlencode(void *v) /* XXX IDNA?? */
688 char **ap;
689 NYD_ENTER;
691 OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
693 for (ap = v; *ap != NULL; ++ap) {
694 char *in = *ap, *out = urlxenc(in, FAL0);
696 if(out == NULL)
697 out = n_UNCONST(V_(n_error));
698 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
699 in, strlen(in), out, strlen(out));
701 NYD_LEAVE;
702 return 0;
705 FL int
706 c_urldecode(void *v) /* XXX IDNA?? */
708 char **ap;
709 NYD_ENTER;
711 OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
713 for (ap = v; *ap != NULL; ++ap) {
714 char *in = *ap, *out = urlxdec(in);
716 if(out == NULL)
717 out = n_UNCONST(V_(n_error));
718 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
719 in, strlen(in), out, strlen(out));
721 NYD_LEAVE;
722 return 0;
725 FL char *
726 url_mailto_to_address(char const *mailtop){ /* TODO hack! RFC 6068; factory? */
727 size_t i;
728 char *rv;
729 char const *mailtop_orig;
730 NYD_ENTER;
732 if(!is_prefix("mailto:", mailtop_orig = mailtop)){
733 rv = NULL;
734 goto jleave;
736 mailtop += sizeof("mailto:") -1;
738 /* TODO This is all intermediate, and for now just enough to understand
739 * TODO a little bit of a little more advanced List-Post: headers. */
740 /* Strip any hfield additions, keep only to addr-spec's */
741 if((rv = strchr(mailtop, '?')) != NULL)
742 rv = savestrbuf(mailtop, i = PTR2SIZE(rv - mailtop));
743 else
744 rv = savestrbuf(mailtop, i = strlen(mailtop));
746 i = strlen(rv);
748 /* Simply perform percent-decoding if there is a percent % */
749 if(memchr(rv, '%', i) != NULL){
750 char *rv_base;
751 bool_t err;
753 for(err = FAL0, mailtop = rv_base = rv; i > 0;){
754 char c;
756 if((c = *mailtop++) == '%'){
757 si32_t cc;
759 if(i < 3 || (cc = n_c_from_hex_base16(mailtop)) < 0){
760 if(!err && (err = TRU1, options & OPT_D_V))
761 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
762 n_shexp_quote_cp(mailtop_orig, FAL0));
763 goto jhex_putc;
765 *rv++ = (char)cc;
766 mailtop += 2;
767 i -= 3;
768 }else{
769 jhex_putc:
770 *rv++ = c;
771 --i;
774 *rv = '\0';
775 rv = rv_base;
777 jleave:
778 NYD_LEAVE;
779 return rv;
782 FL char const *
783 n_servbyname(char const *proto, ui16_t *irv_or_null){
784 static struct{
785 char const name[14];
786 char const port[8];
787 ui16_t portno;
788 } const tbl[] = {
789 { "smtp", "25", 25},
790 { "submission", "587", 587},
791 { "smtps", "465", 465},
792 { "pop3", "110", 110},
793 { "pop3s", "995", 995},
794 { "imap", "143", 143},
795 { "imaps", "993", 993},
796 { "file", "", 0}
798 char const *rv;
799 size_t l, i;
800 NYD2_ENTER;
802 for(rv = proto; *rv != '\0'; ++rv)
803 if(*rv == ':')
804 break;
805 l = PTR2SIZE(rv - proto);
807 for(rv = NULL, i = 0; i < n_NELEM(tbl); ++i)
808 if(!ascncasecmp(tbl[i].name, proto, l)){
809 rv = tbl[i].port;
810 if(irv_or_null != NULL)
811 *irv_or_null = tbl[i].portno;
812 break;
814 NYD2_LEAVE;
815 return rv;
818 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
819 FL bool_t
820 url_parse(struct url *urlp, enum cproto cproto, char const *data)
822 #if defined HAVE_SMTP && defined HAVE_POP3
823 # define __ALLPROTO
824 #endif
825 #if defined HAVE_SMTP || defined HAVE_POP3
826 # define __ANYPROTO
827 char *cp, *x;
828 #endif
829 bool_t rv = FAL0;
830 NYD_ENTER;
831 n_UNUSED(data);
833 memset(urlp, 0, sizeof *urlp);
834 urlp->url_input = data;
835 urlp->url_cproto = cproto;
837 /* Network protocol */
838 #define _protox(X,Y) \
839 urlp->url_portno = Y;\
840 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
841 urlp->url_proto[sizeof(X) -1] = '\0';\
842 urlp->url_proto_len = sizeof(X) -1;\
843 urlp->url_proto_xlen = sizeof(X "://") -1
844 #define __if(X,Y,Z) \
845 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
846 _protox(X, Y);\
847 data += sizeof(X "://") -1;\
848 do { Z; } while (0);\
849 goto juser;\
851 #define _if(X,Y) __if(X, Y, (void)0)
852 #ifdef HAVE_SSL
853 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
854 #else
855 # define _ifs(X,Y) goto jeproto;
856 #endif
858 switch (cproto) {
859 case CPROTO_CCRED:
860 /* The special S/MIME etc. credential lookup */
861 #if defined HAVE_SSL
862 _protox("ccred", 0);
863 break;
864 #else
865 goto jeproto;
866 #endif
867 case CPROTO_SMTP:
868 #ifdef HAVE_SMTP
869 _if ("smtp", 25)
870 _if ("submission", 587)
871 _ifs ("smtps", 465)
872 _protox("smtp", 25);
873 break;
874 #else
875 goto jeproto;
876 #endif
877 case CPROTO_POP3:
878 #ifdef HAVE_POP3
879 _if ("pop3", 110)
880 _ifs ("pop3s", 995)
881 _protox("pop3", 110);
882 break;
883 #else
884 goto jeproto;
885 #endif
886 #if 0
887 case CPROTO_IMAP:
888 _if ("imap", 143)
889 _ifs ("imaps", 993)
890 _protox("imap", 143);
891 break;
892 goto jeproto;
893 #endif
896 #undef _ifs
897 #undef _if
898 #undef __if
899 #undef _protox
901 if (strstr(data, "://") != NULL) {
902 #if !defined __ALLPROTO || !defined HAVE_SSL
903 jeproto:
904 #endif
905 n_err(_("URL proto:// prefix invalid: %s\n"), urlp->url_input);
906 goto jleave;
908 #ifdef __ANYPROTO
910 /* User and password, I */
911 juser:
912 if ((cp = _url_last_at_before_slash(data)) != NULL) {
913 size_t l;
914 char const *urlpe, *d;
915 char *ub;
917 l = PTR2SIZE(cp - data);
918 ub = ac_alloc(l +1);
919 d = data;
920 urlp->url_had_user = TRU1;
921 data = &cp[1];
923 /* And also have a password? */
924 if((cp = memchr(d, ':', l)) != NULL){
925 size_t i = PTR2SIZE(cp - d);
927 l -= i + 1;
928 memcpy(ub, cp + 1, l);
929 ub[l] = '\0';
931 if((urlp->url_pass.s = urlxdec(ub)) == NULL)
932 goto jurlp_err;
933 urlp->url_pass.l = strlen(urlp->url_pass.s);
934 if((urlpe = urlxenc(urlp->url_pass.s, FAL0)) == NULL)
935 goto jurlp_err;
936 if(strcmp(ub, urlpe))
937 goto jurlp_err;
938 l = i;
941 memcpy(ub, d, l);
942 ub[l] = '\0';
943 if((urlp->url_user.s = urlxdec(ub)) == NULL)
944 goto jurlp_err;
945 urlp->url_user.l = strlen(urlp->url_user.s);
946 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL)
947 goto jurlp_err;
948 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
950 if(urlp->url_user_enc.l != l || memcmp(urlp->url_user_enc.s, ub, l)){
951 jurlp_err:
952 n_err(_("String is not properly URL percent encoded: %s\n"), ub);
953 d = NULL;
956 ac_free(ub);
957 if(d == NULL)
958 goto jleave;
961 /* Servername and port -- and possible path suffix */
962 if ((cp = strchr(data, ':')) != NULL) { /* TODO URL parse, IPv6 support */
963 char *eptr;
964 long l;
966 urlp->url_port = x = savestr(x = &cp[1]);
967 if ((x = strchr(x, '/')) != NULL) {
968 *x = '\0';
969 while(*++x == '/')
972 l = strtol(urlp->url_port, &eptr, 10);
973 if (*eptr != '\0' || l <= 0 || UICMP(32, l, >=, 0xFFFFu)) {
974 n_err(_("URL with invalid port number: %s\n"), urlp->url_input);
975 goto jleave;
977 urlp->url_portno = (ui16_t)l;
978 } else {
979 if ((x = strchr(data, '/')) != NULL) {
980 data = savestrbuf(data, PTR2SIZE(x - data));
981 while(*++x == '/')
984 cp = n_UNCONST(data + strlen(data));
987 /* A (non-empty) path may only occur with IMAP */
988 if (x != NULL && *x != '\0') {
989 /* Take care not to count adjacent slashes for real, on either end */
990 char *x2;
991 size_t i;
993 for(x2 = savestrbuf(x, i = strlen(x)); i > 0; --i)
994 if(x2[i - 1] != '/')
995 break;
996 x2[i] = '\0';
998 if (i > 0) {
999 #if 0
1000 if (cproto != CPROTO_IMAP) {
1001 #endif
1002 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
1003 urlp->url_input);
1004 goto jleave;
1005 #if 0
1007 urlp->url_path.l = i;
1008 urlp->url_path.s = x2;
1009 #endif
1013 urlp->url_host.s = savestrbuf(data, urlp->url_host.l = PTR2SIZE(cp - data));
1014 { size_t i;
1015 for (cp = urlp->url_host.s, i = urlp->url_host.l; i != 0; ++cp, --i)
1016 *cp = lowerconv(*cp);
1019 /* .url_h_p: HOST:PORT */
1020 { size_t i;
1021 struct str *s = &urlp->url_h_p;
1023 s->s = salloc(urlp->url_host.l + 1 + sizeof("65536")-1 +1);
1024 memcpy(s->s, urlp->url_host.s, i = urlp->url_host.l);
1025 if (urlp->url_port != NULL) {
1026 size_t j = strlen(urlp->url_port);
1027 s->s[i++] = ':';
1028 memcpy(s->s + i, urlp->url_port, j);
1029 i += j;
1031 s->s[i] = '\0';
1032 s->l = i;
1035 /* User, II
1036 * If there was no user in the URL, do we have *user-HOST* or *user*? */
1037 if (!urlp->url_had_user) {
1038 if ((urlp->url_user.s = xok_vlook(user, urlp, OXM_PLAIN | OXM_H_P))
1039 == NULL) {
1040 /* No, check whether .netrc lookup is desired */
1041 # ifdef HAVE_NETRC
1042 if (!ok_blook(v15_compat) ||
1043 !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) ||
1044 !_nrc_lookup(urlp, FAL0))
1045 # endif
1046 urlp->url_user.s = n_UNCONST(ok_vlook(LOGNAME));
1049 urlp->url_user.l = strlen(urlp->url_user.s);
1050 urlp->url_user.s = savestrbuf(urlp->url_user.s, urlp->url_user.l);
1051 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL){
1052 n_err(_("Cannot URL encode %s\n"), urlp->url_user.s);
1053 goto jleave;
1055 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
1058 /* And then there are a lot of prebuild string combinations TODO do lazy */
1060 /* .url_u_h: .url_user@.url_host
1061 * For SMTP we apply ridiculously complicated *v15-compat* plus
1062 * *smtp-hostname* / *hostname* dependent rules */
1063 { struct str h, *s;
1064 size_t i;
1066 if (cproto == CPROTO_SMTP && ok_blook(v15_compat) &&
1067 (cp = ok_vlook(smtp_hostname)) != NULL) {
1068 if (*cp == '\0')
1069 cp = nodename(1);
1070 h.s = savestrbuf(cp, h.l = strlen(cp));
1071 } else
1072 h = urlp->url_host;
1074 s = &urlp->url_u_h;
1075 i = urlp->url_user.l;
1077 s->s = salloc(i + 1 + h.l +1);
1078 if (i > 0) {
1079 memcpy(s->s, urlp->url_user.s, i);
1080 s->s[i++] = '@';
1082 memcpy(s->s + i, h.s, h.l +1);
1083 i += h.l;
1084 s->l = i;
1087 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1088 { struct str *s = &urlp->url_u_h_p;
1089 size_t i = urlp->url_user.l;
1091 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1092 if (i > 0) {
1093 memcpy(s->s, urlp->url_user.s, i);
1094 s->s[i++] = '@';
1096 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1097 i += urlp->url_h_p.l;
1098 s->l = i;
1101 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1102 { struct str *s = &urlp->url_eu_h_p;
1103 size_t i = urlp->url_user_enc.l;
1105 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1106 if (i > 0) {
1107 memcpy(s->s, urlp->url_user_enc.s, i);
1108 s->s[i++] = '@';
1110 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1111 i += urlp->url_h_p.l;
1112 s->l = i;
1115 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1116 { size_t i;
1117 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_u_h_p.l) +1);
1119 urlp->url_proto[urlp->url_proto_len] = ':';
1120 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_u_h_p.s,
1121 urlp->url_u_h_p.l +1);
1122 urlp->url_proto[urlp->url_proto_len] = '\0';
1124 urlp->url_p_u_h_p = ud;
1127 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1128 { size_t i;
1129 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_eu_h_p.l) +
1130 1 + urlp->url_path.l +1);
1132 urlp->url_proto[urlp->url_proto_len] = ':';
1133 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_eu_h_p.s,
1134 urlp->url_eu_h_p.l +1);
1135 urlp->url_proto[urlp->url_proto_len] = '\0';
1137 if (urlp->url_path.l == 0)
1138 urlp->url_p_eu_h_p = urlp->url_p_eu_h_p_p = ud;
1139 else {
1140 urlp->url_p_eu_h_p = savestrbuf(ud, i);
1141 urlp->url_p_eu_h_p_p = ud;
1142 ud += i;
1143 *ud++ = '/';
1144 memcpy(ud, urlp->url_path.s, urlp->url_path.l +1);
1148 rv = TRU1;
1149 #endif /* __ANYPROTO */
1150 jleave:
1151 NYD_LEAVE;
1152 return rv;
1153 #undef __ANYPROTO
1154 #undef __ALLPROTO
1157 FL bool_t
1158 ccred_lookup_old(struct ccred *ccp, enum cproto cproto, char const *addr)
1160 char const *pname, *pxstr, *authdef;
1161 size_t pxlen, addrlen, i;
1162 char *vbuf, *s;
1163 ui8_t authmask;
1164 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1165 ware = NONE;
1166 bool_t addr_is_nuser = FAL0; /* XXX v15.0 legacy! v15_compat */
1167 NYD_ENTER;
1169 memset(ccp, 0, sizeof *ccp);
1171 switch (cproto) {
1172 default:
1173 case CPROTO_SMTP:
1174 pname = "SMTP";
1175 pxstr = "smtp-auth";
1176 pxlen = sizeof("smtp-auth") -1;
1177 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1178 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1179 authdef = "none";
1180 addr_is_nuser = TRU1;
1181 break;
1182 case CPROTO_POP3:
1183 pname = "POP3";
1184 pxstr = "pop3-auth";
1185 pxlen = sizeof("pop3-auth") -1;
1186 authmask = AUTHTYPE_PLAIN;
1187 authdef = "plain";
1188 break;
1191 ccp->cc_cproto = cproto;
1192 addrlen = strlen(addr);
1193 vbuf = ac_alloc(pxlen + addrlen + sizeof("-password-")-1 +1);
1194 memcpy(vbuf, pxstr, pxlen);
1196 /* Authentication type */
1197 vbuf[pxlen] = '-';
1198 memcpy(vbuf + pxlen + 1, addr, addrlen +1);
1199 if ((s = vok_vlook(vbuf)) == NULL) {
1200 vbuf[pxlen] = '\0';
1201 if ((s = vok_vlook(vbuf)) == NULL)
1202 s = n_UNCONST(authdef);
1205 if (!asccasecmp(s, "none")) {
1206 ccp->cc_auth = "NONE";
1207 ccp->cc_authtype = AUTHTYPE_NONE;
1208 /*ware = NONE;*/
1209 } else if (!asccasecmp(s, "plain")) {
1210 ccp->cc_auth = "PLAIN";
1211 ccp->cc_authtype = AUTHTYPE_PLAIN;
1212 ware = REQ_PASS | REQ_USER;
1213 } else if (!asccasecmp(s, "login")) {
1214 ccp->cc_auth = "LOGIN";
1215 ccp->cc_authtype = AUTHTYPE_LOGIN;
1216 ware = REQ_PASS | REQ_USER;
1217 } else if (!asccasecmp(s, "cram-md5")) {
1218 ccp->cc_auth = "CRAM-MD5";
1219 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1220 ware = REQ_PASS | REQ_USER;
1221 } else if (!asccasecmp(s, "gssapi")) {
1222 ccp->cc_auth = "GSS-API";
1223 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1224 ware = REQ_USER;
1225 } /* no else */
1227 /* Verify method */
1228 if (!(ccp->cc_authtype & authmask)) {
1229 n_err(_("Unsupported %s authentication method: %s\n"), pname, s);
1230 ccp = NULL;
1231 goto jleave;
1233 # ifndef HAVE_MD5
1234 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1235 n_err(_("No CRAM-MD5 support compiled in\n"));
1236 ccp = NULL;
1237 goto jleave;
1239 # endif
1240 # ifndef HAVE_GSSAPI
1241 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1242 n_err(_("No GSS-API support compiled in\n"));
1243 ccp = NULL;
1244 goto jleave;
1246 # endif
1248 /* User name */
1249 if (!(ware & (WANT_USER | REQ_USER)))
1250 goto jpass;
1252 if (!addr_is_nuser) {
1253 if ((s = _url_last_at_before_slash(addr)) != NULL) {
1254 char *cp;
1256 cp = savestrbuf(addr, PTR2SIZE(s - addr));
1258 if((ccp->cc_user.s = urlxdec(cp)) == NULL){
1259 n_err(_("String is not properly URL percent encoded: %s\n"), cp);
1260 ccp = NULL;
1261 goto jleave;
1263 ccp->cc_user.l = strlen(ccp->cc_user.s);
1264 } else if (ware & REQ_USER)
1265 goto jgetuser;
1266 goto jpass;
1269 memcpy(vbuf + pxlen, "-user-", i = sizeof("-user-") -1);
1270 i += pxlen;
1271 memcpy(vbuf + i, addr, addrlen +1);
1272 if ((s = vok_vlook(vbuf)) == NULL) {
1273 vbuf[--i] = '\0';
1274 if ((s = vok_vlook(vbuf)) == NULL && (ware & REQ_USER)) {
1275 if ((s = getuser(NULL)) == NULL) {
1276 jgetuser: /* TODO v15.0: today we simply bail, but we should call getuser().
1277 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1278 * TODO check that first, then! change control flow, grow vbuf */
1279 n_err(_("A user is necessary for %s authentication\n"), pname);
1280 ccp = NULL;
1281 goto jleave;
1285 ccp->cc_user.l = strlen(ccp->cc_user.s = savestr(s));
1287 /* Password */
1288 jpass:
1289 if (!(ware & (WANT_PASS | REQ_PASS)))
1290 goto jleave;
1292 if (!addr_is_nuser) {
1293 memcpy(vbuf, "password-", i = sizeof("password-") -1);
1294 } else {
1295 memcpy(vbuf + pxlen, "-password-", i = sizeof("-password-") -1);
1296 i += pxlen;
1298 memcpy(vbuf + i, addr, addrlen +1);
1299 if ((s = vok_vlook(vbuf)) == NULL) {
1300 vbuf[--i] = '\0';
1301 if ((!addr_is_nuser || (s = vok_vlook(vbuf)) == NULL) &&
1302 (ware & REQ_PASS)) {
1303 if ((s = getpassword(savecat(_("Password for "), pname))) == NULL) {
1304 n_err(_("A password is necessary for %s authentication\n"),
1305 pname);
1306 ccp = NULL;
1307 goto jleave;
1311 if (s != NULL)
1312 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1314 jleave:
1315 ac_free(vbuf);
1316 if (ccp != NULL && (options & OPT_D_VV))
1317 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1318 addr, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1319 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1320 NYD_LEAVE;
1321 return (ccp != NULL);
1324 FL bool_t
1325 ccred_lookup(struct ccred *ccp, struct url *urlp)
1327 char *s;
1328 char const *pstr, *authdef;
1329 ui8_t authmask;
1330 enum okeys authokey;
1331 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1332 ware;
1333 NYD_ENTER;
1335 memset(ccp, 0, sizeof *ccp);
1336 ccp->cc_user = urlp->url_user;
1338 ware = NONE;
1340 switch ((ccp->cc_cproto = urlp->url_cproto)) {
1341 case CPROTO_CCRED:
1342 authokey = (enum okeys)-1;
1343 authmask = AUTHTYPE_PLAIN;
1344 authdef = "plain";
1345 pstr = "ccred";
1346 break;
1347 default:
1348 case CPROTO_SMTP:
1349 authokey = ok_v_smtp_auth;
1350 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1351 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1352 authdef = "plain";
1353 pstr = "smtp";
1354 break;
1355 case CPROTO_POP3:
1356 authokey = ok_v_pop3_auth;
1357 authmask = AUTHTYPE_PLAIN;
1358 authdef = "plain";
1359 pstr = "pop3";
1360 break;
1363 /* Authentication type */
1364 if (authokey == (enum okeys)-1 ||
1365 (s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL)
1366 s = n_UNCONST(authdef);
1368 if (!asccasecmp(s, "none")) {
1369 ccp->cc_auth = "NONE";
1370 ccp->cc_authtype = AUTHTYPE_NONE;
1371 /*ware = NONE;*/
1372 } else if (!asccasecmp(s, "plain")) {
1373 ccp->cc_auth = "PLAIN";
1374 ccp->cc_authtype = AUTHTYPE_PLAIN;
1375 ware = REQ_PASS | REQ_USER;
1376 } else if (!asccasecmp(s, "login")) {
1377 ccp->cc_auth = "LOGIN";
1378 ccp->cc_authtype = AUTHTYPE_LOGIN;
1379 ware = REQ_PASS | REQ_USER;
1380 } else if (!asccasecmp(s, "cram-md5")) {
1381 ccp->cc_auth = "CRAM-MD5";
1382 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1383 ware = REQ_PASS | REQ_USER;
1384 } else if (!asccasecmp(s, "gssapi")) {
1385 ccp->cc_auth = "GSS-API";
1386 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1387 ware = REQ_USER;
1388 } /* no else */
1390 /* Verify method */
1391 if (!(ccp->cc_authtype & authmask)) {
1392 n_err(_("Unsupported %s authentication method: %s\n"), pstr, s);
1393 ccp = NULL;
1394 goto jleave;
1396 # ifndef HAVE_MD5
1397 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1398 n_err(_("No CRAM-MD5 support compiled in\n"));
1399 ccp = NULL;
1400 goto jleave;
1402 # endif
1403 # ifndef HAVE_GSSAPI
1404 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1405 n_err(_("No GSS-API support compiled in\n"));
1406 ccp = NULL;
1407 goto jleave;
1409 # endif
1411 /* Password */
1412 ccp->cc_pass = urlp->url_pass;
1413 if (ccp->cc_pass.s != NULL)
1414 goto jleave;
1416 if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL)
1417 goto js2pass;
1418 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1419 if ((s = xok_vlook(agent_shell_lookup, urlp, OXM_ALL)) != NULL) {
1420 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1421 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1422 if (!_agent_shell_lookup(urlp, s)) {
1423 ccp = NULL;
1424 goto jleave;
1425 } else if (urlp->url_pass.s != NULL) {
1426 ccp->cc_pass = urlp->url_pass;
1427 goto jleave;
1430 # endif
1431 # ifdef HAVE_NETRC
1432 if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) {
1433 ccp->cc_pass = urlp->url_pass;
1434 goto jleave;
1436 # endif
1437 if (ware & REQ_PASS) {
1438 if((s = getpassword(savecat(urlp->url_u_h.s, _(" requires a password: ")))
1439 ) != NULL)
1440 js2pass:
1441 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1442 else {
1443 n_err(_("A password is necessary for %s authentication\n"), pstr);
1444 ccp = NULL;
1448 jleave:
1449 if (ccp != NULL && (options & OPT_D_VV))
1450 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1451 urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1452 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1453 NYD_LEAVE;
1454 return (ccp != NULL);
1456 #endif /* HAVE_SOCKETS */
1458 #ifdef HAVE_NETRC
1459 FL int
1460 c_netrc(void *v)
1462 char **argv = v;
1463 struct nrc_node *nrc;
1464 bool_t load_only;
1465 NYD_ENTER;
1467 load_only = FAL0;
1468 if (*argv == NULL)
1469 goto jlist;
1470 if (argv[1] != NULL)
1471 goto jerr;
1472 if (!asccasecmp(*argv, "show"))
1473 goto jlist;
1474 load_only = TRU1;
1475 if (!asccasecmp(*argv, "load"))
1476 goto jlist;
1477 if (!asccasecmp(*argv, "clear"))
1478 goto jclear;
1479 jerr:
1480 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1481 v = NULL;
1482 jleave:
1483 NYD_LEAVE;
1484 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1486 jlist: {
1487 FILE *fp;
1488 size_t l;
1490 if (_nrc_list == NULL)
1491 _nrc_init();
1492 if (_nrc_list == NRC_NODE_ERR) {
1493 n_err(_("Interpolate what file?\n"));
1494 v = NULL;
1495 goto jleave;
1497 if (load_only)
1498 goto jleave;
1500 if ((fp = Ftmp(NULL, "netrc", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL) {
1501 n_perr(_("tmpfile"), 0);
1502 v = NULL;
1503 goto jleave;
1506 for (l = 0, nrc = _nrc_list; nrc != NULL; ++l, nrc = nrc->nrc_next) {
1507 fprintf(fp, _("machine %s "), nrc->nrc_dat); /* XXX quote? */
1508 if (nrc->nrc_ulen > 0)
1509 fprintf(fp, _("login \"%s\" "),
1510 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1));
1511 if (nrc->nrc_plen > 0)
1512 fprintf(fp, _("password \"%s\"\n"),
1513 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1 + nrc->nrc_ulen +1));
1514 else
1515 putc('\n', fp);
1518 page_or_print(fp, l);
1519 Fclose(fp);
1521 goto jleave;
1523 jclear:
1524 if (_nrc_list == NRC_NODE_ERR)
1525 _nrc_list = NULL;
1526 while ((nrc = _nrc_list) != NULL) {
1527 _nrc_list = nrc->nrc_next;
1528 free(nrc);
1530 goto jleave;
1532 #endif /* HAVE_NETRC */
1534 #ifdef HAVE_MD5
1535 FL char *
1536 md5tohex(char hex[MD5TOHEX_SIZE], void const *vp)
1538 char const *cp = vp;
1539 size_t i, j;
1540 NYD_ENTER;
1542 for (i = 0; i < MD5TOHEX_SIZE / 2; ++i) {
1543 j = i << 1;
1544 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1545 hex[j] = __hex((cp[i] & 0xF0) >> 4);
1546 hex[++j] = __hex(cp[i] & 0x0F);
1547 # undef __hex
1549 NYD_LEAVE;
1550 return hex;
1553 FL char *
1554 cram_md5_string(struct str const *user, struct str const *pass,
1555 char const *b64)
1557 struct str in, out;
1558 char digest[16], *cp;
1559 NYD_ENTER;
1561 out.s = NULL;
1562 if(user->l >= UIZ_MAX - 1 - MD5TOHEX_SIZE - 1)
1563 goto jleave;
1564 if(pass->l >= INT_MAX)
1565 goto jleave;
1567 in.s = n_UNCONST(b64);
1568 in.l = strlen(in.s);
1569 if(!b64_decode(&out, &in))
1570 goto jleave;
1571 if(out.l >= INT_MAX){
1572 free(out.s);
1573 out.s = NULL;
1574 goto jleave;
1577 hmac_md5((uc_i*)out.s, out.l, (uc_i*)pass->s, pass->l, digest);
1578 free(out.s);
1579 cp = md5tohex(salloc(MD5TOHEX_SIZE +1), digest);
1581 in.l = user->l + MD5TOHEX_SIZE +1;
1582 in.s = ac_alloc(user->l + 1 + MD5TOHEX_SIZE +1);
1583 memcpy(in.s, user->s, user->l);
1584 in.s[user->l] = ' ';
1585 memcpy(&in.s[user->l + 1], cp, MD5TOHEX_SIZE);
1586 if(b64_encode(&out, &in, B64_SALLOC | B64_CRLF) == NULL)
1587 out.s = NULL;
1588 ac_free(in.s);
1589 jleave:
1590 NYD_LEAVE;
1591 return out.s;
1594 FL void
1595 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
1596 void *digest)
1599 * This code is taken from
1601 * Network Working Group H. Krawczyk
1602 * Request for Comments: 2104 IBM
1603 * Category: Informational M. Bellare
1604 * UCSD
1605 * R. Canetti
1606 * IBM
1607 * February 1997
1610 * HMAC: Keyed-Hashing for Message Authentication
1612 md5_ctx context;
1613 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
1614 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
1615 unsigned char tk[16];
1616 int i;
1617 NYD_ENTER;
1619 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1620 if (key_len > 64) {
1621 md5_ctx tctx;
1623 md5_init(&tctx);
1624 md5_update(&tctx, key, key_len);
1625 md5_final(tk, &tctx);
1627 key = tk;
1628 key_len = 16;
1631 /* the HMAC_MD5 transform looks like:
1633 * MD5(K XOR opad, MD5(K XOR ipad, text))
1635 * where K is an n byte key
1636 * ipad is the byte 0x36 repeated 64 times
1637 * opad is the byte 0x5c repeated 64 times
1638 * and text is the data being protected */
1640 /* start out by storing key in pads */
1641 memset(k_ipad, 0, sizeof k_ipad);
1642 memset(k_opad, 0, sizeof k_opad);
1643 memcpy(k_ipad, key, key_len);
1644 memcpy(k_opad, key, key_len);
1646 /* XOR key with ipad and opad values */
1647 for (i=0; i<64; i++) {
1648 k_ipad[i] ^= 0x36;
1649 k_opad[i] ^= 0x5c;
1652 /* perform inner MD5 */
1653 md5_init(&context); /* init context for 1st pass */
1654 md5_update(&context, k_ipad, 64); /* start with inner pad */
1655 md5_update(&context, text, text_len); /* then text of datagram */
1656 md5_final(digest, &context); /* finish up 1st pass */
1658 /* perform outer MD5 */
1659 md5_init(&context); /* init context for 2nd pass */
1660 md5_update(&context, k_opad, 64); /* start with outer pad */
1661 md5_update(&context, digest, 16); /* then results of 1st hash */
1662 md5_final(digest, &context); /* finish up 2nd pass */
1663 NYD_LEAVE;
1665 #endif /* HAVE_MD5 */
1667 /* s-it-mode */