quit()+descendants: add hold_sigs_on argument, let'em handle it
[s-mailx.git] / urlcrecry.c
blobcd1b81175498e66aeefdf0eeb9791089eb24439c
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 - 2016 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 * fi;
117 enum nrc_token t;
118 bool_t ispipe, seen_default, nl_last;
119 struct nrc_node *ntail, *nhead, *nrc;
120 NYD_ENTER;
122 n_UNINIT(ntail, NULL);
123 nhead = NULL;
124 nrc = NRC_NODE_ERR;
125 ispipe = FAL0;
126 fi = NULL;
128 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL) {
129 case 0:
130 break;
131 default:
132 goto jleave;
135 if ((netrc_load = ok_vlook(netrc_pipe)) != NULL) {
136 ispipe = TRU1;
137 if ((fi = Popen(netrc_load, "r", ok_vlook(SHELL), NULL, COMMAND_FD_NULL)
138 ) == NULL) {
139 n_perr(netrc_load, 0);
140 goto j_leave;
142 } else {
143 if ((netrc_load = file_expand(ok_vlook(NETRC))) == NULL)
144 goto j_leave;
146 if ((fi = Fopen(netrc_load, "r")) == NULL) {
147 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load, FAL0));
148 goto j_leave;
151 /* Be simple and apply rigid (permission) check(s) */
152 if (fstat(fileno(fi), &sb) == -1 || !S_ISREG(sb.st_mode) ||
153 (sb.st_mode & (S_IRWXG | S_IRWXO))) {
154 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
155 n_shexp_quote_cp(netrc_load, FAL0));
156 goto jleave;
160 seen_default = FAL0;
161 nl_last = TRU1;
162 jnext:
163 switch((t = __nrc_token(fi, buffer, &nl_last))) {
164 case NRC_NONE:
165 break;
166 default: /* Doesn't happen (but on error?), keep CC happy */
167 case NRC_DEFAULT:
168 jdef:
169 /* We ignore the default entry (require an exact host match), and we also
170 * ignore anything after such an entry (faulty syntax) */
171 seen_default = TRU1;
172 /* FALLTHRU */
173 case NRC_MACHINE:
174 jm_h:
175 /* Normalize HOST to lowercase */
176 *host = '\0';
177 if (!seen_default && (t = __nrc_token(fi, host, &nl_last)) != NRC_INPUT)
178 goto jerr;
179 else {
180 char *cp;
181 for (cp = host; *cp != '\0'; ++cp)
182 *cp = lowerconv(*cp);
185 *user = *pass = '\0';
186 while ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_NONE &&
187 t != NRC_MACHINE && t != NRC_DEFAULT) {
188 switch(t) {
189 case NRC_LOGIN:
190 if ((t = __nrc_token(fi, user, &nl_last)) != NRC_INPUT)
191 goto jerr;
192 break;
193 case NRC_PASSWORD:
194 if ((t = __nrc_token(fi, pass, &nl_last)) != NRC_INPUT)
195 goto jerr;
196 break;
197 case NRC_ACCOUNT:
198 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
199 goto jerr;
200 break;
201 case NRC_MACDEF:
202 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
203 goto jerr;
204 else {
205 int i = 0, c;
206 while ((c = getc(fi)) != EOF)
207 if (c == '\n') { /* xxx */
208 /* Don't care about comments here, since we parse until
209 * we've seen two successive newline characters */
210 if (i)
211 break;
212 i = 1;
213 } else
214 i = 0;
216 break;
217 default:
218 case NRC_ERROR:
219 goto jerr;
223 if (!seen_default && (*user != '\0' || *pass != '\0')) {
224 size_t hl = strlen(host), ul = strlen(user), pl = strlen(pass);
225 struct nrc_node *nx = smalloc(sizeof(*nx) -
226 n_VFIELD_SIZEOF(struct nrc_node, nrc_dat) +
227 hl +1 + ul +1 + pl +1);
229 if (nhead != NULL)
230 ntail->nrc_next = nx;
231 else
232 nhead = nx;
233 ntail = nx;
234 nx->nrc_next = NULL;
235 nx->nrc_mlen = hl;
236 nx->nrc_ulen = ul;
237 nx->nrc_plen = pl;
238 memcpy(nx->nrc_dat, host, ++hl);
239 memcpy(nx->nrc_dat + hl, user, ++ul);
240 memcpy(nx->nrc_dat + hl + ul, pass, ++pl);
242 if (t == NRC_MACHINE)
243 goto jm_h;
244 if (t == NRC_DEFAULT)
245 goto jdef;
246 if (t != NRC_NONE)
247 goto jnext;
248 break;
249 case NRC_ERROR:
250 jerr:
251 if (options & OPT_D_V)
252 n_err(_("Errors occurred while parsing %s\n"),
253 n_shexp_quote_cp(netrc_load, FAL0));
254 assert(nrc == NRC_NODE_ERR);
255 goto jleave;
258 if (nhead != NULL)
259 nrc = nhead;
260 n_sigman_cleanup_ping(&sm);
261 jleave:
262 if (fi != NULL) {
263 if (ispipe)
264 Pclose(fi, TRU1);
265 else
266 Fclose(fi);
268 if (nrc == NRC_NODE_ERR)
269 while (nhead != NULL) {
270 ntail = nhead;
271 nhead = nhead->nrc_next;
272 free(ntail);
274 j_leave:
275 _nrc_list = nrc;
276 NYD_LEAVE;
277 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
280 static enum nrc_token
281 __nrc_token(FILE *fi, char buffer[NRC_TOKEN_MAXLEN], bool_t *nl_last)
283 int c;
284 char *cp;
285 enum nrc_token rv;
286 NYD2_ENTER;
288 rv = NRC_NONE;
289 for (;;) {
290 bool_t seen_nl;
292 c = EOF;
293 if (feof(fi) || ferror(fi))
294 goto jleave;
296 for (seen_nl = *nl_last; (c = getc(fi)) != EOF && whitechar(c);)
297 seen_nl |= (c == '\n');
299 if (c == EOF)
300 goto jleave;
301 /* fetchmail and derived parsers support comments */
302 if ((*nl_last = seen_nl) && c == '#') {
303 while ((c = getc(fi)) != EOF && c != '\n')
305 continue;
307 break;
310 cp = buffer;
311 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
312 if (c == '"' || c == '\'') {
313 int quotec = c;
315 /* Not requiring the closing QM is (Net)BSD syntax */
316 while ((c = getc(fi)) != EOF && c != quotec) {
317 /* Backslash escaping the next character is (Net)BSD syntax */
318 if (c == '\\')
319 if ((c = getc(fi)) == EOF)
320 break;
321 *cp++ = c;
322 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
323 rv = NRC_ERROR;
324 goto jleave;
327 } else {
328 *cp++ = c;
329 while ((c = getc(fi)) != EOF && !whitechar(c)) {
330 /* Backslash escaping the next character is (Net)BSD syntax */
331 if (c == '\\' && (c = getc(fi)) == EOF)
332 break;
333 *cp++ = c;
334 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
335 rv = NRC_ERROR;
336 goto jleave;
339 *nl_last = (c == '\n');
341 *cp = '\0';
343 if (*buffer == '\0')
344 do {/*rv = NRC_NONE*/} while (0);
345 else if (!strcmp(buffer, "default"))
346 rv = NRC_DEFAULT;
347 else if (!strcmp(buffer, "login"))
348 rv = NRC_LOGIN;
349 else if (!strcmp(buffer, "password") || !strcmp(buffer, "passwd"))
350 rv = NRC_PASSWORD;
351 else if (!strcmp(buffer, "account"))
352 rv = NRC_ACCOUNT;
353 else if (!strcmp(buffer, "macdef"))
354 rv = NRC_MACDEF;
355 else if (!strcmp(buffer, "machine"))
356 rv = NRC_MACHINE;
357 else
358 rv = NRC_INPUT;
359 jleave:
360 if (c == EOF && !feof(fi))
361 rv = NRC_ERROR;
362 NYD2_LEAVE;
363 return rv;
366 static bool_t
367 _nrc_lookup(struct url *urlp, bool_t only_pass)
369 struct nrc_node *nrc, *nrc_wild, *nrc_exact;
370 bool_t rv = FAL0;
371 NYD_ENTER;
373 assert(!only_pass || urlp->url_user.s != NULL);
374 assert(only_pass || urlp->url_user.s == NULL);
376 if (_nrc_list == NULL)
377 _nrc_init();
378 if (_nrc_list == NRC_NODE_ERR)
379 goto jleave;
381 nrc_wild = nrc_exact = NULL;
382 for (nrc = _nrc_list; nrc != NULL; nrc = nrc->nrc_next)
383 switch (__nrc_host_match(nrc, urlp)) {
384 case 1:
385 nrc->nrc_result = nrc_exact;
386 nrc_exact = nrc;
387 continue;
388 case -1:
389 nrc->nrc_result = nrc_wild;
390 nrc_wild = nrc;
391 /* FALLTHRU */
392 case 0:
393 continue;
396 if (!only_pass && urlp->url_user.s == NULL) {
397 /* Must be an unambiguous entry of its kind */
398 if (nrc_exact != NULL && nrc_exact->nrc_result != NULL)
399 goto jleave;
400 if (__nrc_find_user(urlp, nrc_exact))
401 goto j_user;
403 if (nrc_wild != NULL && nrc_wild->nrc_result != NULL)
404 goto jleave;
405 if (!__nrc_find_user(urlp, nrc_wild))
406 goto jleave;
407 j_user:
411 if (__nrc_find_pass(urlp, TRU1, nrc_exact) ||
412 __nrc_find_pass(urlp, TRU1, nrc_wild) ||
413 /* Do not try to find a password without exact user match unless we've
414 * been called during credential lookup, a.k.a. the second time */
415 !only_pass ||
416 __nrc_find_pass(urlp, FAL0, nrc_exact) ||
417 __nrc_find_pass(urlp, FAL0, nrc_wild))
418 rv = TRU1;
419 jleave:
420 NYD_LEAVE;
421 return rv;
424 static int
425 __nrc_host_match(struct nrc_node const *nrc, struct url const *urlp)
427 char const *d2, *d1;
428 size_t l2, l1;
429 int rv = 0;
430 NYD2_ENTER;
432 /* Find a matching machine -- entries are all lowercase normalized */
433 if (nrc->nrc_mlen == urlp->url_host.l) {
434 if (n_LIKELY(!memcmp(nrc->nrc_dat, urlp->url_host.s, urlp->url_host.l)))
435 rv = 1;
436 goto jleave;
439 /* Cannot be an exact match, but maybe the .netrc machine starts with
440 * a "*." glob, which we recognize as an extension, meaning "skip
441 * a single subdomain, then match the rest" */
442 d1 = nrc->nrc_dat + 2;
443 l1 = nrc->nrc_mlen;
444 if (l1 <= 2 || d1[-1] != '.' || d1[-2] != '*')
445 goto jleave;
446 l1 -= 2;
448 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
449 * in fact this even succeeds for ".host.com", but - why care, here? */
450 d2 = urlp->url_host.s;
451 l2 = urlp->url_host.l;
452 while (l2 > 0) {
453 --l2;
454 if (*d2++ == '.')
455 break;
458 if (l2 == l1 && !memcmp(d1, d2, l1))
459 /* This matches, but we won't use it directly but watch out for an
460 * exact match first! */
461 rv = -1;
462 jleave:
463 NYD2_LEAVE;
464 return rv;
467 static bool_t
468 __nrc_find_user(struct url *urlp, struct nrc_node const *nrc)
470 NYD2_ENTER;
472 for (; nrc != NULL; nrc = nrc->nrc_result)
473 if (nrc->nrc_ulen > 0) {
474 /* Fake it was part of URL otherwise XXX */
475 urlp->url_had_user = TRU1;
476 /* That buffer will be duplicated by url_parse() in this case! */
477 urlp->url_user.s = n_UNCONST(nrc->nrc_dat + nrc->nrc_mlen +1);
478 urlp->url_user.l = nrc->nrc_ulen;
479 break;
482 NYD2_LEAVE;
483 return (nrc != NULL);
486 static bool_t
487 __nrc_find_pass(struct url *urlp, bool_t user_match, struct nrc_node const *nrc)
489 NYD2_ENTER;
491 for (; nrc != NULL; nrc = nrc->nrc_result) {
492 bool_t um = (nrc->nrc_ulen == urlp->url_user.l &&
493 !memcmp(nrc->nrc_dat + nrc->nrc_mlen +1, urlp->url_user.s,
494 urlp->url_user.l));
496 if (user_match) {
497 if (!um)
498 continue;
499 } else if (!um && nrc->nrc_ulen > 0)
500 continue;
501 if (nrc->nrc_plen == 0)
502 continue;
504 /* We are responsible for duplicating this buffer! */
505 urlp->url_pass.s = savestrbuf(nrc->nrc_dat + nrc->nrc_mlen +1 +
506 nrc->nrc_ulen + 1, (urlp->url_pass.l = nrc->nrc_plen));
507 break;
510 NYD2_LEAVE;
511 return (nrc != NULL);
513 #endif /* HAVE_NETRC */
515 #ifdef HAVE_AGENT
516 static bool_t
517 _agent_shell_lookup(struct url *urlp, char const *comm) /* TODO v15-compat */
519 char buf[128];
520 char const *env_addon[8];
521 struct str s;
522 FILE *pbuf;
523 union {char const *cp; int c; sighandler_type sht;} u;
524 size_t cl, l;
525 bool_t rv = FAL0;
526 NYD2_ENTER;
528 env_addon[0] = str_concat_csvl(&s, AGENT_USER, "=", urlp->url_user.s,
529 NULL)->s;
530 env_addon[1] = str_concat_csvl(&s, AGENT_USER_ENC, "=", urlp->url_user_enc.s,
531 NULL)->s;
532 env_addon[2] = str_concat_csvl(&s, AGENT_HOST, "=", urlp->url_host.s,
533 NULL)->s;
534 env_addon[3] = str_concat_csvl(&s, AGENT_HOST_PORT, "=", urlp->url_h_p.s,
535 NULL)->s;
536 u.cp = ok_vlook(TMPDIR);
537 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, "=", u.cp, NULL)->s;
538 env_addon[5] = str_concat_csvl(&s, "TMPDIR", "=", u.cp, NULL)->s;
540 env_addon[6] = 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 char *
686 url_mailto_to_address(char const *mailtop){ /* TODO hack! RFC 6068; factory? */
687 size_t i;
688 char *rv;
689 char const *mailtop_orig;
690 NYD_ENTER;
692 if(!is_prefix("mailto:", mailtop_orig = mailtop)){
693 rv = NULL;
694 goto jleave;
696 mailtop += sizeof("mailto:") -1;
698 /* TODO This is all intermediate, and for now just enough to understand
699 * TODO a little bit of a little more advanced List-Post: headers. */
700 /* Strip any hfield additions, keep only to addr-spec's */
701 if((rv = strchr(mailtop, '?')) != NULL)
702 rv = savestrbuf(mailtop, i = PTR2SIZE(rv - mailtop));
703 else
704 rv = savestrbuf(mailtop, i = strlen(mailtop));
706 i = strlen(rv);
708 /* Simply perform percent-decoding if there is a percent % */
709 if(memchr(rv, '%', i) != NULL){
710 char *rv_base;
711 bool_t err;
713 for(err = FAL0, mailtop = rv_base = rv; i > 0;){
714 char c;
716 if((c = *mailtop++) == '%'){
717 si32_t cc;
719 if(i < 3 || (cc = n_c_from_hex_base16(mailtop)) < 0){
720 if(!err && (err = TRU1, options & OPT_D_V))
721 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
722 n_shexp_quote_cp(mailtop_orig, FAL0));
723 goto jhex_putc;
725 *rv++ = (char)cc;
726 mailtop += 2;
727 i -= 3;
728 }else{
729 jhex_putc:
730 *rv++ = c;
731 --i;
734 *rv = '\0';
735 rv = rv_base;
737 jleave:
738 NYD_LEAVE;
739 return rv;
742 FL char const *
743 n_servbyname(char const *proto, ui16_t *irv_or_null){
744 static struct{
745 char const name[14];
746 char const port[8];
747 ui16_t portno;
748 } const tbl[] = {
749 { "smtp", "25", 25},
750 { "submission", "587", 587},
751 { "smtps", "465", 465},
752 { "pop3", "110", 110},
753 { "pop3s", "995", 995},
754 { "imap", "143", 143},
755 { "imaps", "993", 993},
756 { "file", "", 0}
758 char const *rv;
759 size_t l, i;
760 NYD2_ENTER;
762 for(rv = proto; *rv != '\0'; ++rv)
763 if(*rv == ':')
764 break;
765 l = PTR2SIZE(rv - proto);
767 for(rv = NULL, i = 0; i < n_NELEM(tbl); ++i)
768 if(!ascncasecmp(tbl[i].name, proto, l)){
769 rv = tbl[i].port;
770 if(irv_or_null != NULL)
771 *irv_or_null = tbl[i].portno;
772 break;
774 NYD2_LEAVE;
775 return rv;
778 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
779 FL bool_t
780 url_parse(struct url *urlp, enum cproto cproto, char const *data)
782 #if defined HAVE_SMTP && defined HAVE_POP3
783 # define __ALLPROTO
784 #endif
785 #if defined HAVE_SMTP || defined HAVE_POP3
786 # define __ANYPROTO
787 char *cp, *x;
788 #endif
789 bool_t rv = FAL0;
790 NYD_ENTER;
791 n_UNUSED(data);
793 memset(urlp, 0, sizeof *urlp);
794 urlp->url_input = data;
795 urlp->url_cproto = cproto;
797 /* Network protocol */
798 #define _protox(X,Y) \
799 urlp->url_portno = Y;\
800 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
801 urlp->url_proto[sizeof(X) -1] = '\0';\
802 urlp->url_proto_len = sizeof(X) -1;\
803 urlp->url_proto_xlen = sizeof(X "://") -1
804 #define __if(X,Y,Z) \
805 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
806 _protox(X, Y);\
807 data += sizeof(X "://") -1;\
808 do { Z; } while (0);\
809 goto juser;\
811 #define _if(X,Y) __if(X, Y, (void)0)
812 #ifdef HAVE_SSL
813 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
814 #else
815 # define _ifs(X,Y) goto jeproto;
816 #endif
818 switch (cproto) {
819 case CPROTO_SMTP:
820 #ifdef HAVE_SMTP
821 _if ("smtp", 25)
822 _if ("submission", 587)
823 _ifs ("smtps", 465)
824 _protox("smtp", 25);
825 break;
826 #else
827 goto jeproto;
828 #endif
829 case CPROTO_POP3:
830 #ifdef HAVE_POP3
831 _if ("pop3", 110)
832 _ifs ("pop3s", 995)
833 _protox("pop3", 110);
834 break;
835 #else
836 goto jeproto;
837 #endif
838 #if 0
839 case CPROTO_IMAP:
840 _if ("imap", 143)
841 _ifs ("imaps", 993)
842 _protox("imap", 143);
843 break;
844 goto jeproto;
845 #endif
848 #undef _ifs
849 #undef _if
850 #undef __if
851 #undef _protox
853 if (strstr(data, "://") != NULL) {
854 #if !defined __ALLPROTO || !defined HAVE_SSL
855 jeproto:
856 #endif
857 n_err(_("URL proto:// prefix invalid: %s\n"), urlp->url_input);
858 goto jleave;
860 #ifdef __ANYPROTO
862 /* User and password, I */
863 juser:
864 if ((cp = _url_last_at_before_slash(data)) != NULL) {
865 size_t l;
866 char const *urlpe, *d;
867 char *ub;
869 l = PTR2SIZE(cp - data);
870 ub = ac_alloc(l +1);
871 d = data;
872 urlp->url_had_user = TRU1;
873 data = &cp[1];
875 /* And also have a password? */
876 if((cp = memchr(d, ':', l)) != NULL){
877 size_t i = PTR2SIZE(cp - d);
879 l -= i + 1;
880 memcpy(ub, cp + 1, l);
881 ub[l] = '\0';
883 if((urlp->url_pass.s = urlxdec(ub)) == NULL)
884 goto jurlp_err;
885 urlp->url_pass.l = strlen(urlp->url_pass.s);
886 if((urlpe = urlxenc(urlp->url_pass.s, FAL0)) == NULL)
887 goto jurlp_err;
888 if(strcmp(ub, urlpe))
889 goto jurlp_err;
890 l = i;
893 memcpy(ub, d, l);
894 ub[l] = '\0';
895 if((urlp->url_user.s = urlxdec(ub)) == NULL)
896 goto jurlp_err;
897 urlp->url_user.l = strlen(urlp->url_user.s);
898 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL)
899 goto jurlp_err;
900 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
902 if(urlp->url_user_enc.l != l || memcmp(urlp->url_user_enc.s, ub, l)){
903 jurlp_err:
904 n_err(_("String is not properly URL percent encoded: %s\n"), ub);
905 d = NULL;
908 ac_free(ub);
909 if(d == NULL)
910 goto jleave;
913 /* Servername and port -- and possible path suffix */
914 if ((cp = strchr(data, ':')) != NULL) { /* TODO URL parse, IPv6 support */
915 char *eptr;
916 long l;
918 urlp->url_port = x = savestr(x = &cp[1]);
919 if ((x = strchr(x, '/')) != NULL) {
920 *x = '\0';
921 while(*++x == '/')
924 l = strtol(urlp->url_port, &eptr, 10);
925 if (*eptr != '\0' || l <= 0 || UICMP(32, l, >=, 0xFFFFu)) {
926 n_err(_("URL with invalid port number: %s\n"), urlp->url_input);
927 goto jleave;
929 urlp->url_portno = (ui16_t)l;
930 } else {
931 if ((x = strchr(data, '/')) != NULL) {
932 data = savestrbuf(data, PTR2SIZE(x - data));
933 while(*++x == '/')
936 cp = n_UNCONST(data + strlen(data));
939 /* A (non-empty) path may only occur with IMAP */
940 if (x != NULL && *x != '\0') {
941 /* Take care not to count adjacent slashes for real, on either end */
942 char *x2;
943 size_t i;
945 for(x2 = savestrbuf(x, i = strlen(x)); i > 0; --i)
946 if(x2[i - 1] != '/')
947 break;
948 x2[i] = '\0';
950 if (i > 0) {
951 #if 0
952 if (cproto != CPROTO_IMAP) {
953 #endif
954 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
955 urlp->url_input);
956 goto jleave;
957 #if 0
959 urlp->url_path.l = i;
960 urlp->url_path.s = x2;
961 #endif
965 urlp->url_host.s = savestrbuf(data, urlp->url_host.l = PTR2SIZE(cp - data));
966 { size_t i;
967 for (cp = urlp->url_host.s, i = urlp->url_host.l; i != 0; ++cp, --i)
968 *cp = lowerconv(*cp);
971 /* .url_h_p: HOST:PORT */
972 { size_t i;
973 struct str *s = &urlp->url_h_p;
975 s->s = salloc(urlp->url_host.l + 1 + sizeof("65536")-1 +1);
976 memcpy(s->s, urlp->url_host.s, i = urlp->url_host.l);
977 if (urlp->url_port != NULL) {
978 size_t j = strlen(urlp->url_port);
979 s->s[i++] = ':';
980 memcpy(s->s + i, urlp->url_port, j);
981 i += j;
983 s->s[i] = '\0';
984 s->l = i;
987 /* User, II
988 * If there was no user in the URL, do we have *user-HOST* or *user*? */
989 if (!urlp->url_had_user) {
990 if ((urlp->url_user.s = xok_vlook(user, urlp, OXM_PLAIN | OXM_H_P))
991 == NULL) {
992 /* No, check whether .netrc lookup is desired */
993 # ifdef HAVE_NETRC
994 if (!ok_blook(v15_compat) ||
995 !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) ||
996 !_nrc_lookup(urlp, FAL0))
997 # endif
998 urlp->url_user.s = n_UNCONST(myname);
1001 urlp->url_user.l = strlen(urlp->url_user.s);
1002 urlp->url_user.s = savestrbuf(urlp->url_user.s, urlp->url_user.l);
1003 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL){
1004 n_err(_("Cannot URL encode %s\n"), urlp->url_user.s);
1005 goto jleave;
1007 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
1010 /* And then there are a lot of prebuild string combinations TODO do lazy */
1012 /* .url_u_h: .url_user@.url_host
1013 * For SMTP we apply ridiculously complicated *v15-compat* plus
1014 * *smtp-hostname* / *hostname* dependent rules */
1015 { struct str h, *s;
1016 size_t i;
1018 if (cproto == CPROTO_SMTP && ok_blook(v15_compat) &&
1019 (cp = ok_vlook(smtp_hostname)) != NULL) {
1020 if (*cp == '\0')
1021 cp = nodename(1);
1022 h.s = savestrbuf(cp, h.l = strlen(cp));
1023 } else
1024 h = urlp->url_host;
1026 s = &urlp->url_u_h;
1027 i = urlp->url_user.l;
1029 s->s = salloc(i + 1 + h.l +1);
1030 if (i > 0) {
1031 memcpy(s->s, urlp->url_user.s, i);
1032 s->s[i++] = '@';
1034 memcpy(s->s + i, h.s, h.l +1);
1035 i += h.l;
1036 s->l = i;
1039 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1040 { struct str *s = &urlp->url_u_h_p;
1041 size_t i = urlp->url_user.l;
1043 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1044 if (i > 0) {
1045 memcpy(s->s, urlp->url_user.s, i);
1046 s->s[i++] = '@';
1048 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1049 i += urlp->url_h_p.l;
1050 s->l = i;
1053 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1054 { struct str *s = &urlp->url_eu_h_p;
1055 size_t i = urlp->url_user_enc.l;
1057 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1058 if (i > 0) {
1059 memcpy(s->s, urlp->url_user_enc.s, i);
1060 s->s[i++] = '@';
1062 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1063 i += urlp->url_h_p.l;
1064 s->l = i;
1067 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1068 { size_t i;
1069 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_u_h_p.l) +1);
1071 urlp->url_proto[urlp->url_proto_len] = ':';
1072 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_u_h_p.s,
1073 urlp->url_u_h_p.l +1);
1074 urlp->url_proto[urlp->url_proto_len] = '\0';
1076 urlp->url_p_u_h_p = ud;
1079 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1080 { size_t i;
1081 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_eu_h_p.l) +
1082 1 + urlp->url_path.l +1);
1084 urlp->url_proto[urlp->url_proto_len] = ':';
1085 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_eu_h_p.s,
1086 urlp->url_eu_h_p.l +1);
1087 urlp->url_proto[urlp->url_proto_len] = '\0';
1089 if (urlp->url_path.l == 0)
1090 urlp->url_p_eu_h_p = urlp->url_p_eu_h_p_p = ud;
1091 else {
1092 urlp->url_p_eu_h_p = savestrbuf(ud, i);
1093 urlp->url_p_eu_h_p_p = ud;
1094 ud += i;
1095 *ud++ = '/';
1096 memcpy(ud, urlp->url_path.s, urlp->url_path.l +1);
1100 rv = TRU1;
1101 #endif /* __ANYPROTO */
1102 jleave:
1103 NYD_LEAVE;
1104 return rv;
1105 #undef __ANYPROTO
1106 #undef __ALLPROTO
1109 FL bool_t
1110 ccred_lookup_old(struct ccred *ccp, enum cproto cproto, char const *addr)
1112 char const *pname, *pxstr, *authdef;
1113 size_t pxlen, addrlen, i;
1114 char *vbuf, *s;
1115 ui8_t authmask;
1116 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1117 ware = NONE;
1118 bool_t addr_is_nuser = FAL0; /* XXX v15.0 legacy! v15_compat */
1119 NYD_ENTER;
1121 memset(ccp, 0, sizeof *ccp);
1123 switch (cproto) {
1124 default:
1125 case CPROTO_SMTP:
1126 pname = "SMTP";
1127 pxstr = "smtp-auth";
1128 pxlen = sizeof("smtp-auth") -1;
1129 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1130 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1131 authdef = "none";
1132 addr_is_nuser = TRU1;
1133 break;
1134 case CPROTO_POP3:
1135 pname = "POP3";
1136 pxstr = "pop3-auth";
1137 pxlen = sizeof("pop3-auth") -1;
1138 authmask = AUTHTYPE_PLAIN;
1139 authdef = "plain";
1140 break;
1143 ccp->cc_cproto = cproto;
1144 addrlen = strlen(addr);
1145 vbuf = ac_alloc(pxlen + addrlen + sizeof("-password-")-1 +1);
1146 memcpy(vbuf, pxstr, pxlen);
1148 /* Authentication type */
1149 vbuf[pxlen] = '-';
1150 memcpy(vbuf + pxlen + 1, addr, addrlen +1);
1151 if ((s = vok_vlook(vbuf)) == NULL) {
1152 vbuf[pxlen] = '\0';
1153 if ((s = vok_vlook(vbuf)) == NULL)
1154 s = n_UNCONST(authdef);
1157 if (!asccasecmp(s, "none")) {
1158 ccp->cc_auth = "NONE";
1159 ccp->cc_authtype = AUTHTYPE_NONE;
1160 /*ware = NONE;*/
1161 } else if (!asccasecmp(s, "plain")) {
1162 ccp->cc_auth = "PLAIN";
1163 ccp->cc_authtype = AUTHTYPE_PLAIN;
1164 ware = REQ_PASS | REQ_USER;
1165 } else if (!asccasecmp(s, "login")) {
1166 ccp->cc_auth = "LOGIN";
1167 ccp->cc_authtype = AUTHTYPE_LOGIN;
1168 ware = REQ_PASS | REQ_USER;
1169 } else if (!asccasecmp(s, "cram-md5")) {
1170 ccp->cc_auth = "CRAM-MD5";
1171 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1172 ware = REQ_PASS | REQ_USER;
1173 } else if (!asccasecmp(s, "gssapi")) {
1174 ccp->cc_auth = "GSS-API";
1175 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1176 ware = REQ_USER;
1177 } /* no else */
1179 /* Verify method */
1180 if (!(ccp->cc_authtype & authmask)) {
1181 n_err(_("Unsupported %s authentication method: %s\n"), pname, s);
1182 ccp = NULL;
1183 goto jleave;
1185 # ifndef HAVE_MD5
1186 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1187 n_err(_("No CRAM-MD5 support compiled in\n"));
1188 ccp = NULL;
1189 goto jleave;
1191 # endif
1192 # ifndef HAVE_GSSAPI
1193 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1194 n_err(_("No GSS-API support compiled in\n"));
1195 ccp = NULL;
1196 goto jleave;
1198 # endif
1200 /* User name */
1201 if (!(ware & (WANT_USER | REQ_USER)))
1202 goto jpass;
1204 if (!addr_is_nuser) {
1205 if ((s = _url_last_at_before_slash(addr)) != NULL) {
1206 char *cp;
1208 cp = savestrbuf(addr, PTR2SIZE(s - addr));
1210 if((ccp->cc_user.s = urlxdec(cp)) == NULL){
1211 n_err(_("String is not properly URL percent encoded: %s\n"), cp);
1212 ccp = NULL;
1213 goto jleave;
1215 ccp->cc_user.l = strlen(ccp->cc_user.s);
1216 } else if (ware & REQ_USER)
1217 goto jgetuser;
1218 goto jpass;
1221 memcpy(vbuf + pxlen, "-user-", i = sizeof("-user-") -1);
1222 i += pxlen;
1223 memcpy(vbuf + i, addr, addrlen +1);
1224 if ((s = vok_vlook(vbuf)) == NULL) {
1225 vbuf[--i] = '\0';
1226 if ((s = vok_vlook(vbuf)) == NULL && (ware & REQ_USER)) {
1227 if ((s = getuser(NULL)) == NULL) {
1228 jgetuser: /* TODO v15.0: today we simply bail, but we should call getuser().
1229 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1230 * TODO check that first, then! change control flow, grow vbuf */
1231 n_err(_("A user is necessary for %s authentication\n"), pname);
1232 ccp = NULL;
1233 goto jleave;
1237 ccp->cc_user.l = strlen(ccp->cc_user.s = savestr(s));
1239 /* Password */
1240 jpass:
1241 if (!(ware & (WANT_PASS | REQ_PASS)))
1242 goto jleave;
1244 if (!addr_is_nuser) {
1245 memcpy(vbuf, "password-", i = sizeof("password-") -1);
1246 } else {
1247 memcpy(vbuf + pxlen, "-password-", i = sizeof("-password-") -1);
1248 i += pxlen;
1250 memcpy(vbuf + i, addr, addrlen +1);
1251 if ((s = vok_vlook(vbuf)) == NULL) {
1252 vbuf[--i] = '\0';
1253 if ((!addr_is_nuser || (s = vok_vlook(vbuf)) == NULL) &&
1254 (ware & REQ_PASS)) {
1255 if ((s = getpassword(NULL)) == NULL) {
1256 n_err(_("A password is necessary for %s authentication\n"),
1257 pname);
1258 ccp = NULL;
1259 goto jleave;
1263 if (s != NULL)
1264 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1266 jleave:
1267 ac_free(vbuf);
1268 if (ccp != NULL && (options & OPT_D_VV))
1269 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1270 addr, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1271 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1272 NYD_LEAVE;
1273 return (ccp != NULL);
1276 FL bool_t
1277 ccred_lookup(struct ccred *ccp, struct url *urlp)
1279 char const *pstr, *authdef;
1280 char *s;
1281 enum okeys authokey;
1282 ui8_t authmask;
1283 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1284 ware = NONE;
1285 NYD_ENTER;
1287 memset(ccp, 0, sizeof *ccp);
1288 ccp->cc_user = urlp->url_user;
1290 switch ((ccp->cc_cproto = urlp->url_cproto)) {
1291 default:
1292 case CPROTO_SMTP:
1293 pstr = "smtp";
1294 authokey = ok_v_smtp_auth;
1295 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1296 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1297 authdef = "plain";
1298 break;
1299 case CPROTO_POP3:
1300 pstr = "pop3";
1301 authokey = ok_v_pop3_auth;
1302 authmask = AUTHTYPE_PLAIN;
1303 authdef = "plain";
1304 break;
1307 /* Authentication type */
1308 if ((s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL)
1309 s = n_UNCONST(authdef);
1311 if (!asccasecmp(s, "none")) {
1312 ccp->cc_auth = "NONE";
1313 ccp->cc_authtype = AUTHTYPE_NONE;
1314 /*ware = NONE;*/
1315 } else if (!asccasecmp(s, "plain")) {
1316 ccp->cc_auth = "PLAIN";
1317 ccp->cc_authtype = AUTHTYPE_PLAIN;
1318 ware = REQ_PASS | REQ_USER;
1319 } else if (!asccasecmp(s, "login")) {
1320 ccp->cc_auth = "LOGIN";
1321 ccp->cc_authtype = AUTHTYPE_LOGIN;
1322 ware = REQ_PASS | REQ_USER;
1323 } else if (!asccasecmp(s, "cram-md5")) {
1324 ccp->cc_auth = "CRAM-MD5";
1325 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1326 ware = REQ_PASS | REQ_USER;
1327 } else if (!asccasecmp(s, "gssapi")) {
1328 ccp->cc_auth = "GSS-API";
1329 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1330 ware = REQ_USER;
1331 } /* no else */
1333 /* Verify method */
1334 if (!(ccp->cc_authtype & authmask)) {
1335 n_err(_("Unsupported %s authentication method: %s\n"), pstr, s);
1336 ccp = NULL;
1337 goto jleave;
1339 # ifndef HAVE_MD5
1340 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1341 n_err(_("No CRAM-MD5 support compiled in\n"));
1342 ccp = NULL;
1343 goto jleave;
1345 # endif
1346 # ifndef HAVE_GSSAPI
1347 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1348 n_err(_("No GSS-API support compiled in\n"));
1349 ccp = NULL;
1350 goto jleave;
1352 # endif
1354 /* Password */
1355 ccp->cc_pass = urlp->url_pass;
1356 if (ccp->cc_pass.s != NULL)
1357 goto jleave;
1359 if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL)
1360 goto js2pass;
1361 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1362 if ((s = xok_vlook(agent_shell_lookup, urlp, OXM_ALL)) != NULL) {
1363 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1364 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1365 if (!_agent_shell_lookup(urlp, s)) {
1366 ccp = NULL;
1367 goto jleave;
1368 } else if (urlp->url_pass.s != NULL) {
1369 ccp->cc_pass = urlp->url_pass;
1370 goto jleave;
1373 # endif
1374 # ifdef HAVE_NETRC
1375 if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) {
1376 ccp->cc_pass = urlp->url_pass;
1377 goto jleave;
1379 # endif
1380 if (ware & REQ_PASS) {
1381 if ((s = getpassword(NULL)) != NULL)
1382 js2pass:
1383 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1384 else {
1385 n_err(_("A password is necessary for %s authentication\n"), pstr);
1386 ccp = NULL;
1390 jleave:
1391 if (ccp != NULL && (options & OPT_D_VV))
1392 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1393 urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1394 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1395 NYD_LEAVE;
1396 return (ccp != NULL);
1398 #endif /* HAVE_SOCKETS */
1400 #ifdef HAVE_NETRC
1401 FL int
1402 c_netrc(void *v)
1404 char **argv = v;
1405 struct nrc_node *nrc;
1406 bool_t load_only;
1407 NYD_ENTER;
1409 load_only = FAL0;
1410 if (*argv == NULL)
1411 goto jlist;
1412 if (argv[1] != NULL)
1413 goto jerr;
1414 if (!asccasecmp(*argv, "show"))
1415 goto jlist;
1416 load_only = TRU1;
1417 if (!asccasecmp(*argv, "load"))
1418 goto jlist;
1419 if (!asccasecmp(*argv, "clear"))
1420 goto jclear;
1421 jerr:
1422 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1423 v = NULL;
1424 jleave:
1425 NYD_LEAVE;
1426 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1428 jlist: {
1429 FILE *fp;
1430 size_t l;
1432 if (_nrc_list == NULL)
1433 _nrc_init();
1434 if (_nrc_list == NRC_NODE_ERR) {
1435 n_err(_("Interpolate what file?\n"));
1436 v = NULL;
1437 goto jleave;
1439 if (load_only)
1440 goto jleave;
1442 if ((fp = Ftmp(NULL, "netrc", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL) {
1443 n_perr(_("tmpfile"), 0);
1444 v = NULL;
1445 goto jleave;
1448 for (l = 0, nrc = _nrc_list; nrc != NULL; ++l, nrc = nrc->nrc_next) {
1449 fprintf(fp, _("machine %s "), nrc->nrc_dat); /* XXX quote? */
1450 if (nrc->nrc_ulen > 0)
1451 fprintf(fp, _("login \"%s\" "),
1452 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1));
1453 if (nrc->nrc_plen > 0)
1454 fprintf(fp, _("password \"%s\"\n"),
1455 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1 + nrc->nrc_ulen +1));
1456 else
1457 putc('\n', fp);
1460 page_or_print(fp, l);
1461 Fclose(fp);
1463 goto jleave;
1465 jclear:
1466 if (_nrc_list == NRC_NODE_ERR)
1467 _nrc_list = NULL;
1468 while ((nrc = _nrc_list) != NULL) {
1469 _nrc_list = nrc->nrc_next;
1470 free(nrc);
1472 goto jleave;
1474 #endif /* HAVE_NETRC */
1476 #ifdef HAVE_MD5
1477 FL char *
1478 md5tohex(char hex[MD5TOHEX_SIZE], void const *vp)
1480 char const *cp = vp;
1481 size_t i, j;
1482 NYD_ENTER;
1484 for (i = 0; i < MD5TOHEX_SIZE / 2; ++i) {
1485 j = i << 1;
1486 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1487 hex[j] = __hex((cp[i] & 0xF0) >> 4);
1488 hex[++j] = __hex(cp[i] & 0x0F);
1489 # undef __hex
1491 NYD_LEAVE;
1492 return hex;
1495 FL char *
1496 cram_md5_string(struct str const *user, struct str const *pass,
1497 char const *b64)
1499 struct str in, out;
1500 char digest[16], *cp;
1501 NYD_ENTER;
1503 out.s = NULL;
1504 if(user->l >= UIZ_MAX - 1 - MD5TOHEX_SIZE - 1)
1505 goto jleave;
1506 if(pass->l >= INT_MAX)
1507 goto jleave;
1509 in.s = n_UNCONST(b64);
1510 in.l = strlen(in.s);
1511 if(!b64_decode(&out, &in))
1512 goto jleave;
1513 if(out.l >= INT_MAX){
1514 free(out.s);
1515 out.s = NULL;
1516 goto jleave;
1519 hmac_md5((uc_i*)out.s, out.l, (uc_i*)pass->s, pass->l, digest);
1520 free(out.s);
1521 cp = md5tohex(salloc(MD5TOHEX_SIZE +1), digest);
1523 in.l = user->l + MD5TOHEX_SIZE +1;
1524 in.s = ac_alloc(user->l + 1 + MD5TOHEX_SIZE +1);
1525 memcpy(in.s, user->s, user->l);
1526 in.s[user->l] = ' ';
1527 memcpy(&in.s[user->l + 1], cp, MD5TOHEX_SIZE);
1528 if(b64_encode(&out, &in, B64_SALLOC | B64_CRLF) == NULL)
1529 out.s = NULL;
1530 ac_free(in.s);
1531 jleave:
1532 NYD_LEAVE;
1533 return out.s;
1536 FL void
1537 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
1538 void *digest)
1541 * This code is taken from
1543 * Network Working Group H. Krawczyk
1544 * Request for Comments: 2104 IBM
1545 * Category: Informational M. Bellare
1546 * UCSD
1547 * R. Canetti
1548 * IBM
1549 * February 1997
1552 * HMAC: Keyed-Hashing for Message Authentication
1554 md5_ctx context;
1555 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
1556 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
1557 unsigned char tk[16];
1558 int i;
1559 NYD_ENTER;
1561 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1562 if (key_len > 64) {
1563 md5_ctx tctx;
1565 md5_init(&tctx);
1566 md5_update(&tctx, key, key_len);
1567 md5_final(tk, &tctx);
1569 key = tk;
1570 key_len = 16;
1573 /* the HMAC_MD5 transform looks like:
1575 * MD5(K XOR opad, MD5(K XOR ipad, text))
1577 * where K is an n byte key
1578 * ipad is the byte 0x36 repeated 64 times
1579 * opad is the byte 0x5c repeated 64 times
1580 * and text is the data being protected */
1582 /* start out by storing key in pads */
1583 memset(k_ipad, 0, sizeof k_ipad);
1584 memset(k_opad, 0, sizeof k_opad);
1585 memcpy(k_ipad, key, key_len);
1586 memcpy(k_opad, key, key_len);
1588 /* XOR key with ipad and opad values */
1589 for (i=0; i<64; i++) {
1590 k_ipad[i] ^= 0x36;
1591 k_opad[i] ^= 0x5c;
1594 /* perform inner MD5 */
1595 md5_init(&context); /* init context for 1st pass */
1596 md5_update(&context, k_ipad, 64); /* start with inner pad */
1597 md5_update(&context, text, text_len); /* then text of datagram */
1598 md5_final(digest, &context); /* finish up 1st pass */
1600 /* perform outer MD5 */
1601 md5_init(&context); /* init context for 2nd pass */
1602 md5_update(&context, k_opad, 64); /* start with outer pad */
1603 md5_update(&context, digest, 16); /* then results of 1st hash */
1604 md5_final(digest, &context); /* finish up 2nd pass */
1605 NYD_LEAVE;
1607 #endif /* HAVE_MD5 */
1609 /* s-it-mode */