Obsolete -E and -B (-> STDOUT always LBF, STDIN with -#)
[s-mailx.git] / urlcrecry.c
blob736c666519483cacd9ceb39b7f300bc2c87d0496
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 = fexpand(ok_vlook(NETRC), FEXP_LOCAL | FEXP_NOPROTO)
144 ) == NULL)
145 goto j_leave;
147 if ((fi = Fopen(netrc_load, "r")) == NULL) {
148 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load, FAL0));
149 goto j_leave;
152 /* Be simple and apply rigid (permission) check(s) */
153 if (fstat(fileno(fi), &sb) == -1 || !S_ISREG(sb.st_mode) ||
154 (sb.st_mode & (S_IRWXG | S_IRWXO))) {
155 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
156 n_shexp_quote_cp(netrc_load, FAL0));
157 goto jleave;
161 seen_default = FAL0;
162 nl_last = TRU1;
163 jnext:
164 switch((t = __nrc_token(fi, buffer, &nl_last))) {
165 case NRC_NONE:
166 break;
167 default: /* Doesn't happen (but on error?), keep CC happy */
168 case NRC_DEFAULT:
169 jdef:
170 /* We ignore the default entry (require an exact host match), and we also
171 * ignore anything after such an entry (faulty syntax) */
172 seen_default = TRU1;
173 /* FALLTHRU */
174 case NRC_MACHINE:
175 jm_h:
176 /* Normalize HOST to lowercase */
177 *host = '\0';
178 if (!seen_default && (t = __nrc_token(fi, host, &nl_last)) != NRC_INPUT)
179 goto jerr;
180 else {
181 char *cp;
182 for (cp = host; *cp != '\0'; ++cp)
183 *cp = lowerconv(*cp);
186 *user = *pass = '\0';
187 while ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_NONE &&
188 t != NRC_MACHINE && t != NRC_DEFAULT) {
189 switch(t) {
190 case NRC_LOGIN:
191 if ((t = __nrc_token(fi, user, &nl_last)) != NRC_INPUT)
192 goto jerr;
193 break;
194 case NRC_PASSWORD:
195 if ((t = __nrc_token(fi, pass, &nl_last)) != NRC_INPUT)
196 goto jerr;
197 break;
198 case NRC_ACCOUNT:
199 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
200 goto jerr;
201 break;
202 case NRC_MACDEF:
203 if ((t = __nrc_token(fi, buffer, &nl_last)) != NRC_INPUT)
204 goto jerr;
205 else {
206 int i = 0, c;
207 while ((c = getc(fi)) != EOF)
208 if (c == '\n') { /* xxx */
209 /* Don't care about comments here, since we parse until
210 * we've seen two successive newline characters */
211 if (i)
212 break;
213 i = 1;
214 } else
215 i = 0;
217 break;
218 default:
219 case NRC_ERROR:
220 goto jerr;
224 if (!seen_default && (*user != '\0' || *pass != '\0')) {
225 size_t hl = strlen(host), ul = strlen(user), pl = strlen(pass);
226 struct nrc_node *nx = smalloc(sizeof(*nx) -
227 n_VFIELD_SIZEOF(struct nrc_node, nrc_dat) +
228 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 {char const *cp; 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 u.cp = ok_vlook(TMPDIR);
538 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, "=", u.cp, NULL)->s;
539 env_addon[5] = str_concat_csvl(&s, "TMPDIR", "=", u.cp, NULL)->s;
541 env_addon[6] = NULL;
543 if ((pbuf = Popen(comm, "r", ok_vlook(SHELL), env_addon, -1)) == NULL) {
544 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm);
545 goto jleave;
548 for (s.s = NULL, s.l = cl = l = 0; (u.c = getc(pbuf)) != EOF; ++cl) {
549 if (u.c == '\n') /* xxx */
550 continue;
551 buf[l++] = u.c;
552 if (l == sizeof(buf) - 1) {
553 n_str_add_buf(&s, buf, l);
554 l = 0;
557 if (l > 0)
558 n_str_add_buf(&s, buf, l);
560 if (!Pclose(pbuf, TRU1)) {
561 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm);
562 goto jleave;
565 /* We are responsible for duplicating this buffer! */
566 if (s.s != NULL)
567 urlp->url_pass.s = savestrbuf(s.s, urlp->url_pass.l = s.l);
568 else if (cl > 0)
569 urlp->url_pass.s = n_UNCONST(n_empty), urlp->url_pass.l = 0;
570 rv = TRU1;
571 jleave:
572 if (s.s != NULL)
573 free(s.s);
574 NYD2_LEAVE;
575 return rv;
577 #endif /* HAVE_AGENT */
579 FL char *
580 (urlxenc)(char const *cp, bool_t ispath n_MEMORY_DEBUG_ARGS)
582 char *n, *np, c1;
583 NYD2_ENTER;
585 /* C99 */{
586 size_t i;
588 i = strlen(cp);
589 if(i >= UIZ_MAX / 3){
590 n = NULL;
591 goto jleave;
593 i *= 3;
594 ++i;
595 np = n = (n_autorec_alloc)(NULL, i n_MEMORY_DEBUG_ARGSCALL);
598 for (; (c1 = *cp) != '\0'; ++cp) {
599 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
600 * ALPHA / DIGIT / "-" / "." / "_" / "~"
601 * However add a special is[file]path mode for file-system friendliness */
602 if (alnumchar(c1) || c1 == '_')
603 *np++ = c1;
604 else if (!ispath) {
605 if (c1 != '-' && c1 != '.' && c1 != '~')
606 goto jesc;
607 *np++ = c1;
608 } else if (PTRCMP(np, >, n) && (*cp == '-' || *cp == '.')) /* XXX imap */
609 *np++ = c1;
610 else {
611 jesc:
612 np[0] = '%';
613 n_c_to_hex_base16(np + 1, c1);
614 np += 3;
617 *np = '\0';
618 jleave:
619 NYD2_LEAVE;
620 return n;
623 FL char *
624 (urlxdec)(char const *cp n_MEMORY_DEBUG_ARGS)
626 char *n, *np;
627 si32_t c;
628 NYD2_ENTER;
630 np = n = (n_autorec_alloc)(NULL, strlen(cp) +1 n_MEMORY_DEBUG_ARGSCALL);
632 while ((c = (uc_i)*cp++) != '\0') {
633 if (c == '%' && cp[0] != '\0' && cp[1] != '\0') {
634 si32_t o = c;
635 if (n_LIKELY((c = n_c_from_hex_base16(cp)) >= '\0'))
636 cp += 2;
637 else
638 c = o;
640 *np++ = (char)c;
642 *np = '\0';
643 NYD2_LEAVE;
644 return n;
647 FL int
648 c_urlcodec(void *v){
649 bool_t ispath;
650 char const **argv, *cp, *res;
651 NYD_ENTER;
653 if(*(cp = *(argv = v)) == 'p'){
654 if(!ascncasecmp(++cp, "ath", 3))
655 cp += 3;
656 ispath = TRU1;
659 if(is_prefix(cp, "encode")){
660 while((cp = *++argv) != NULL){
661 if((res = urlxenc(cp, ispath)) == NULL)
662 res = V_(n_error);
663 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
664 cp, strlen(cp), res, strlen(res));
666 }else if(is_prefix(cp, "decode")){
667 struct str in, out;
669 while((cp = *++argv) != NULL){
670 if((res = urlxdec(cp)) == NULL)
671 res = V_(n_error);
672 in.l = strlen(in.s = n_UNCONST(res)); /* logical */
673 makeprint(&in, &out);
674 printf(" in: %s (%" PRIuZ " bytes)\nout: %s (%" PRIuZ " bytes)\n",
675 cp, strlen(cp), out.s, in.l);
676 free(out.s);
678 }else{
679 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv);
680 cp = NULL;
682 NYD_LEAVE;
683 return (cp != NULL ? OKAY : STOP);
686 FL char *
687 url_mailto_to_address(char const *mailtop){ /* TODO hack! RFC 6068; factory? */
688 size_t i;
689 char *rv;
690 char const *mailtop_orig;
691 NYD_ENTER;
693 if(!is_prefix("mailto:", mailtop_orig = mailtop)){
694 rv = NULL;
695 goto jleave;
697 mailtop += sizeof("mailto:") -1;
699 /* TODO This is all intermediate, and for now just enough to understand
700 * TODO a little bit of a little more advanced List-Post: headers. */
701 /* Strip any hfield additions, keep only to addr-spec's */
702 if((rv = strchr(mailtop, '?')) != NULL)
703 rv = savestrbuf(mailtop, i = PTR2SIZE(rv - mailtop));
704 else
705 rv = savestrbuf(mailtop, i = strlen(mailtop));
707 i = strlen(rv);
709 /* Simply perform percent-decoding if there is a percent % */
710 if(memchr(rv, '%', i) != NULL){
711 char *rv_base;
712 bool_t err;
714 for(err = FAL0, mailtop = rv_base = rv; i > 0;){
715 char c;
717 if((c = *mailtop++) == '%'){
718 si32_t cc;
720 if(i < 3 || (cc = n_c_from_hex_base16(mailtop)) < 0){
721 if(!err && (err = TRU1, options & OPT_D_V))
722 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
723 n_shexp_quote_cp(mailtop_orig, FAL0));
724 goto jhex_putc;
726 *rv++ = (char)cc;
727 mailtop += 2;
728 i -= 3;
729 }else{
730 jhex_putc:
731 *rv++ = c;
732 --i;
735 *rv = '\0';
736 rv = rv_base;
738 jleave:
739 NYD_LEAVE;
740 return rv;
743 FL char const *
744 n_servbyname(char const *proto, ui16_t *irv_or_null){
745 static struct{
746 char const name[14];
747 char const port[8];
748 ui16_t portno;
749 } const tbl[] = {
750 { "smtp", "25", 25},
751 { "submission", "587", 587},
752 { "smtps", "465", 465},
753 { "pop3", "110", 110},
754 { "pop3s", "995", 995},
755 { "imap", "143", 143},
756 { "imaps", "993", 993},
757 { "file", "", 0}
759 char const *rv;
760 size_t l, i;
761 NYD2_ENTER;
763 for(rv = proto; *rv != '\0'; ++rv)
764 if(*rv == ':')
765 break;
766 l = PTR2SIZE(rv - proto);
768 for(rv = NULL, i = 0; i < n_NELEM(tbl); ++i)
769 if(!ascncasecmp(tbl[i].name, proto, l)){
770 rv = tbl[i].port;
771 if(irv_or_null != NULL)
772 *irv_or_null = tbl[i].portno;
773 break;
775 NYD2_LEAVE;
776 return rv;
779 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
780 FL bool_t
781 url_parse(struct url *urlp, enum cproto cproto, char const *data)
783 #if defined HAVE_SMTP && defined HAVE_POP3
784 # define __ALLPROTO
785 #endif
786 #if defined HAVE_SMTP || defined HAVE_POP3
787 # define __ANYPROTO
788 char *cp, *x;
789 #endif
790 bool_t rv = FAL0;
791 NYD_ENTER;
792 n_UNUSED(data);
794 memset(urlp, 0, sizeof *urlp);
795 urlp->url_input = data;
796 urlp->url_cproto = cproto;
798 /* Network protocol */
799 #define _protox(X,Y) \
800 urlp->url_portno = Y;\
801 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
802 urlp->url_proto[sizeof(X) -1] = '\0';\
803 urlp->url_proto_len = sizeof(X) -1;\
804 urlp->url_proto_xlen = sizeof(X "://") -1
805 #define __if(X,Y,Z) \
806 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
807 _protox(X, Y);\
808 data += sizeof(X "://") -1;\
809 do { Z; } while (0);\
810 goto juser;\
812 #define _if(X,Y) __if(X, Y, (void)0)
813 #ifdef HAVE_SSL
814 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
815 #else
816 # define _ifs(X,Y) goto jeproto;
817 #endif
819 switch (cproto) {
820 case CPROTO_SMTP:
821 #ifdef HAVE_SMTP
822 _if ("smtp", 25)
823 _if ("submission", 587)
824 _ifs ("smtps", 465)
825 _protox("smtp", 25);
826 break;
827 #else
828 goto jeproto;
829 #endif
830 case CPROTO_POP3:
831 #ifdef HAVE_POP3
832 _if ("pop3", 110)
833 _ifs ("pop3s", 995)
834 _protox("pop3", 110);
835 break;
836 #else
837 goto jeproto;
838 #endif
839 #if 0
840 case CPROTO_IMAP:
841 _if ("imap", 143)
842 _ifs ("imaps", 993)
843 _protox("imap", 143);
844 break;
845 goto jeproto;
846 #endif
849 #undef _ifs
850 #undef _if
851 #undef __if
852 #undef _protox
854 if (strstr(data, "://") != NULL) {
855 #if !defined __ALLPROTO || !defined HAVE_SSL
856 jeproto:
857 #endif
858 n_err(_("URL proto:// prefix invalid: %s\n"), urlp->url_input);
859 goto jleave;
861 #ifdef __ANYPROTO
863 /* User and password, I */
864 juser:
865 if ((cp = _url_last_at_before_slash(data)) != NULL) {
866 size_t l;
867 char const *urlpe, *d;
868 char *ub;
870 l = PTR2SIZE(cp - data);
871 ub = ac_alloc(l +1);
872 d = data;
873 urlp->url_had_user = TRU1;
874 data = &cp[1];
876 /* And also have a password? */
877 if((cp = memchr(d, ':', l)) != NULL){
878 size_t i = PTR2SIZE(cp - d);
880 l -= i + 1;
881 memcpy(ub, cp + 1, l);
882 ub[l] = '\0';
884 if((urlp->url_pass.s = urlxdec(ub)) == NULL)
885 goto jurlp_err;
886 urlp->url_pass.l = strlen(urlp->url_pass.s);
887 if((urlpe = urlxenc(urlp->url_pass.s, FAL0)) == NULL)
888 goto jurlp_err;
889 if(strcmp(ub, urlpe))
890 goto jurlp_err;
891 l = i;
894 memcpy(ub, d, l);
895 ub[l] = '\0';
896 if((urlp->url_user.s = urlxdec(ub)) == NULL)
897 goto jurlp_err;
898 urlp->url_user.l = strlen(urlp->url_user.s);
899 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL)
900 goto jurlp_err;
901 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
903 if(urlp->url_user_enc.l != l || memcmp(urlp->url_user_enc.s, ub, l)){
904 jurlp_err:
905 n_err(_("String is not properly URL percent encoded: %s\n"), ub);
906 d = NULL;
909 ac_free(ub);
910 if(d == NULL)
911 goto jleave;
914 /* Servername and port -- and possible path suffix */
915 if ((cp = strchr(data, ':')) != NULL) { /* TODO URL parse, IPv6 support */
916 char *eptr;
917 long l;
919 urlp->url_port = x = savestr(x = &cp[1]);
920 if ((x = strchr(x, '/')) != NULL) {
921 *x = '\0';
922 while(*++x == '/')
925 l = strtol(urlp->url_port, &eptr, 10);
926 if (*eptr != '\0' || l <= 0 || UICMP(32, l, >=, 0xFFFFu)) {
927 n_err(_("URL with invalid port number: %s\n"), urlp->url_input);
928 goto jleave;
930 urlp->url_portno = (ui16_t)l;
931 } else {
932 if ((x = strchr(data, '/')) != NULL) {
933 data = savestrbuf(data, PTR2SIZE(x - data));
934 while(*++x == '/')
937 cp = n_UNCONST(data + strlen(data));
940 /* A (non-empty) path may only occur with IMAP */
941 if (x != NULL && *x != '\0') {
942 /* Take care not to count adjacent slashes for real, on either end */
943 char *x2;
944 size_t i;
946 for(x2 = savestrbuf(x, i = strlen(x)); i > 0; --i)
947 if(x2[i - 1] != '/')
948 break;
949 x2[i] = '\0';
951 if (i > 0) {
952 #if 0
953 if (cproto != CPROTO_IMAP) {
954 #endif
955 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
956 urlp->url_input);
957 goto jleave;
958 #if 0
960 urlp->url_path.l = i;
961 urlp->url_path.s = x2;
962 #endif
966 urlp->url_host.s = savestrbuf(data, urlp->url_host.l = PTR2SIZE(cp - data));
967 { size_t i;
968 for (cp = urlp->url_host.s, i = urlp->url_host.l; i != 0; ++cp, --i)
969 *cp = lowerconv(*cp);
972 /* .url_h_p: HOST:PORT */
973 { size_t i;
974 struct str *s = &urlp->url_h_p;
976 s->s = salloc(urlp->url_host.l + 1 + sizeof("65536")-1 +1);
977 memcpy(s->s, urlp->url_host.s, i = urlp->url_host.l);
978 if (urlp->url_port != NULL) {
979 size_t j = strlen(urlp->url_port);
980 s->s[i++] = ':';
981 memcpy(s->s + i, urlp->url_port, j);
982 i += j;
984 s->s[i] = '\0';
985 s->l = i;
988 /* User, II
989 * If there was no user in the URL, do we have *user-HOST* or *user*? */
990 if (!urlp->url_had_user) {
991 if ((urlp->url_user.s = xok_vlook(user, urlp, OXM_PLAIN | OXM_H_P))
992 == NULL) {
993 /* No, check whether .netrc lookup is desired */
994 # ifdef HAVE_NETRC
995 if (!ok_blook(v15_compat) ||
996 !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) ||
997 !_nrc_lookup(urlp, FAL0))
998 # endif
999 urlp->url_user.s = n_UNCONST(myname);
1002 urlp->url_user.l = strlen(urlp->url_user.s);
1003 urlp->url_user.s = savestrbuf(urlp->url_user.s, urlp->url_user.l);
1004 if((urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0)) == NULL){
1005 n_err(_("Cannot URL encode %s\n"), urlp->url_user.s);
1006 goto jleave;
1008 urlp->url_user_enc.l = strlen(urlp->url_user_enc.s);
1011 /* And then there are a lot of prebuild string combinations TODO do lazy */
1013 /* .url_u_h: .url_user@.url_host
1014 * For SMTP we apply ridiculously complicated *v15-compat* plus
1015 * *smtp-hostname* / *hostname* dependent rules */
1016 { struct str h, *s;
1017 size_t i;
1019 if (cproto == CPROTO_SMTP && ok_blook(v15_compat) &&
1020 (cp = ok_vlook(smtp_hostname)) != NULL) {
1021 if (*cp == '\0')
1022 cp = nodename(1);
1023 h.s = savestrbuf(cp, h.l = strlen(cp));
1024 } else
1025 h = urlp->url_host;
1027 s = &urlp->url_u_h;
1028 i = urlp->url_user.l;
1030 s->s = salloc(i + 1 + h.l +1);
1031 if (i > 0) {
1032 memcpy(s->s, urlp->url_user.s, i);
1033 s->s[i++] = '@';
1035 memcpy(s->s + i, h.s, h.l +1);
1036 i += h.l;
1037 s->l = i;
1040 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1041 { struct str *s = &urlp->url_u_h_p;
1042 size_t i = urlp->url_user.l;
1044 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1045 if (i > 0) {
1046 memcpy(s->s, urlp->url_user.s, i);
1047 s->s[i++] = '@';
1049 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1050 i += urlp->url_h_p.l;
1051 s->l = i;
1054 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1055 { struct str *s = &urlp->url_eu_h_p;
1056 size_t i = urlp->url_user_enc.l;
1058 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
1059 if (i > 0) {
1060 memcpy(s->s, urlp->url_user_enc.s, i);
1061 s->s[i++] = '@';
1063 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
1064 i += urlp->url_h_p.l;
1065 s->l = i;
1068 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1069 { size_t i;
1070 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_u_h_p.l) +1);
1072 urlp->url_proto[urlp->url_proto_len] = ':';
1073 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_u_h_p.s,
1074 urlp->url_u_h_p.l +1);
1075 urlp->url_proto[urlp->url_proto_len] = '\0';
1077 urlp->url_p_u_h_p = ud;
1080 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1081 { size_t i;
1082 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_eu_h_p.l) +
1083 1 + urlp->url_path.l +1);
1085 urlp->url_proto[urlp->url_proto_len] = ':';
1086 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_eu_h_p.s,
1087 urlp->url_eu_h_p.l +1);
1088 urlp->url_proto[urlp->url_proto_len] = '\0';
1090 if (urlp->url_path.l == 0)
1091 urlp->url_p_eu_h_p = urlp->url_p_eu_h_p_p = ud;
1092 else {
1093 urlp->url_p_eu_h_p = savestrbuf(ud, i);
1094 urlp->url_p_eu_h_p_p = ud;
1095 ud += i;
1096 *ud++ = '/';
1097 memcpy(ud, urlp->url_path.s, urlp->url_path.l +1);
1101 rv = TRU1;
1102 #endif /* __ANYPROTO */
1103 jleave:
1104 NYD_LEAVE;
1105 return rv;
1106 #undef __ANYPROTO
1107 #undef __ALLPROTO
1110 FL bool_t
1111 ccred_lookup_old(struct ccred *ccp, enum cproto cproto, char const *addr)
1113 char const *pname, *pxstr, *authdef;
1114 size_t pxlen, addrlen, i;
1115 char *vbuf, *s;
1116 ui8_t authmask;
1117 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1118 ware = NONE;
1119 bool_t addr_is_nuser = FAL0; /* XXX v15.0 legacy! v15_compat */
1120 NYD_ENTER;
1122 memset(ccp, 0, sizeof *ccp);
1124 switch (cproto) {
1125 default:
1126 case CPROTO_SMTP:
1127 pname = "SMTP";
1128 pxstr = "smtp-auth";
1129 pxlen = sizeof("smtp-auth") -1;
1130 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1131 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1132 authdef = "none";
1133 addr_is_nuser = TRU1;
1134 break;
1135 case CPROTO_POP3:
1136 pname = "POP3";
1137 pxstr = "pop3-auth";
1138 pxlen = sizeof("pop3-auth") -1;
1139 authmask = AUTHTYPE_PLAIN;
1140 authdef = "plain";
1141 break;
1144 ccp->cc_cproto = cproto;
1145 addrlen = strlen(addr);
1146 vbuf = ac_alloc(pxlen + addrlen + sizeof("-password-")-1 +1);
1147 memcpy(vbuf, pxstr, pxlen);
1149 /* Authentication type */
1150 vbuf[pxlen] = '-';
1151 memcpy(vbuf + pxlen + 1, addr, addrlen +1);
1152 if ((s = vok_vlook(vbuf)) == NULL) {
1153 vbuf[pxlen] = '\0';
1154 if ((s = vok_vlook(vbuf)) == NULL)
1155 s = n_UNCONST(authdef);
1158 if (!asccasecmp(s, "none")) {
1159 ccp->cc_auth = "NONE";
1160 ccp->cc_authtype = AUTHTYPE_NONE;
1161 /*ware = NONE;*/
1162 } else if (!asccasecmp(s, "plain")) {
1163 ccp->cc_auth = "PLAIN";
1164 ccp->cc_authtype = AUTHTYPE_PLAIN;
1165 ware = REQ_PASS | REQ_USER;
1166 } else if (!asccasecmp(s, "login")) {
1167 ccp->cc_auth = "LOGIN";
1168 ccp->cc_authtype = AUTHTYPE_LOGIN;
1169 ware = REQ_PASS | REQ_USER;
1170 } else if (!asccasecmp(s, "cram-md5")) {
1171 ccp->cc_auth = "CRAM-MD5";
1172 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1173 ware = REQ_PASS | REQ_USER;
1174 } else if (!asccasecmp(s, "gssapi")) {
1175 ccp->cc_auth = "GSS-API";
1176 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1177 ware = REQ_USER;
1178 } /* no else */
1180 /* Verify method */
1181 if (!(ccp->cc_authtype & authmask)) {
1182 n_err(_("Unsupported %s authentication method: %s\n"), pname, s);
1183 ccp = NULL;
1184 goto jleave;
1186 # ifndef HAVE_MD5
1187 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1188 n_err(_("No CRAM-MD5 support compiled in\n"));
1189 ccp = NULL;
1190 goto jleave;
1192 # endif
1193 # ifndef HAVE_GSSAPI
1194 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1195 n_err(_("No GSS-API support compiled in\n"));
1196 ccp = NULL;
1197 goto jleave;
1199 # endif
1201 /* User name */
1202 if (!(ware & (WANT_USER | REQ_USER)))
1203 goto jpass;
1205 if (!addr_is_nuser) {
1206 if ((s = _url_last_at_before_slash(addr)) != NULL) {
1207 char *cp;
1209 cp = savestrbuf(addr, PTR2SIZE(s - addr));
1211 if((ccp->cc_user.s = urlxdec(cp)) == NULL){
1212 n_err(_("String is not properly URL percent encoded: %s\n"), cp);
1213 ccp = NULL;
1214 goto jleave;
1216 ccp->cc_user.l = strlen(ccp->cc_user.s);
1217 } else if (ware & REQ_USER)
1218 goto jgetuser;
1219 goto jpass;
1222 memcpy(vbuf + pxlen, "-user-", i = sizeof("-user-") -1);
1223 i += pxlen;
1224 memcpy(vbuf + i, addr, addrlen +1);
1225 if ((s = vok_vlook(vbuf)) == NULL) {
1226 vbuf[--i] = '\0';
1227 if ((s = vok_vlook(vbuf)) == NULL && (ware & REQ_USER)) {
1228 if ((s = getuser(NULL)) == NULL) {
1229 jgetuser: /* TODO v15.0: today we simply bail, but we should call getuser().
1230 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1231 * TODO check that first, then! change control flow, grow vbuf */
1232 n_err(_("A user is necessary for %s authentication\n"), pname);
1233 ccp = NULL;
1234 goto jleave;
1238 ccp->cc_user.l = strlen(ccp->cc_user.s = savestr(s));
1240 /* Password */
1241 jpass:
1242 if (!(ware & (WANT_PASS | REQ_PASS)))
1243 goto jleave;
1245 if (!addr_is_nuser) {
1246 memcpy(vbuf, "password-", i = sizeof("password-") -1);
1247 } else {
1248 memcpy(vbuf + pxlen, "-password-", i = sizeof("-password-") -1);
1249 i += pxlen;
1251 memcpy(vbuf + i, addr, addrlen +1);
1252 if ((s = vok_vlook(vbuf)) == NULL) {
1253 vbuf[--i] = '\0';
1254 if ((!addr_is_nuser || (s = vok_vlook(vbuf)) == NULL) &&
1255 (ware & REQ_PASS)) {
1256 if ((s = getpassword(NULL)) == NULL) {
1257 n_err(_("A password is necessary for %s authentication\n"),
1258 pname);
1259 ccp = NULL;
1260 goto jleave;
1264 if (s != NULL)
1265 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1267 jleave:
1268 ac_free(vbuf);
1269 if (ccp != NULL && (options & OPT_D_VV))
1270 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1271 addr, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1272 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1273 NYD_LEAVE;
1274 return (ccp != NULL);
1277 FL bool_t
1278 ccred_lookup(struct ccred *ccp, struct url *urlp)
1280 char const *pstr, *authdef;
1281 char *s;
1282 enum okeys authokey;
1283 ui8_t authmask;
1284 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1285 ware = NONE;
1286 NYD_ENTER;
1288 memset(ccp, 0, sizeof *ccp);
1289 ccp->cc_user = urlp->url_user;
1291 switch ((ccp->cc_cproto = urlp->url_cproto)) {
1292 default:
1293 case CPROTO_SMTP:
1294 pstr = "smtp";
1295 authokey = ok_v_smtp_auth;
1296 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1297 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1298 authdef = "plain";
1299 break;
1300 case CPROTO_POP3:
1301 pstr = "pop3";
1302 authokey = ok_v_pop3_auth;
1303 authmask = AUTHTYPE_PLAIN;
1304 authdef = "plain";
1305 break;
1308 /* Authentication type */
1309 if ((s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL)
1310 s = n_UNCONST(authdef);
1312 if (!asccasecmp(s, "none")) {
1313 ccp->cc_auth = "NONE";
1314 ccp->cc_authtype = AUTHTYPE_NONE;
1315 /*ware = NONE;*/
1316 } else if (!asccasecmp(s, "plain")) {
1317 ccp->cc_auth = "PLAIN";
1318 ccp->cc_authtype = AUTHTYPE_PLAIN;
1319 ware = REQ_PASS | REQ_USER;
1320 } else if (!asccasecmp(s, "login")) {
1321 ccp->cc_auth = "LOGIN";
1322 ccp->cc_authtype = AUTHTYPE_LOGIN;
1323 ware = REQ_PASS | REQ_USER;
1324 } else if (!asccasecmp(s, "cram-md5")) {
1325 ccp->cc_auth = "CRAM-MD5";
1326 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1327 ware = REQ_PASS | REQ_USER;
1328 } else if (!asccasecmp(s, "gssapi")) {
1329 ccp->cc_auth = "GSS-API";
1330 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1331 ware = REQ_USER;
1332 } /* no else */
1334 /* Verify method */
1335 if (!(ccp->cc_authtype & authmask)) {
1336 n_err(_("Unsupported %s authentication method: %s\n"), pstr, s);
1337 ccp = NULL;
1338 goto jleave;
1340 # ifndef HAVE_MD5
1341 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1342 n_err(_("No CRAM-MD5 support compiled in\n"));
1343 ccp = NULL;
1344 goto jleave;
1346 # endif
1347 # ifndef HAVE_GSSAPI
1348 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1349 n_err(_("No GSS-API support compiled in\n"));
1350 ccp = NULL;
1351 goto jleave;
1353 # endif
1355 /* Password */
1356 ccp->cc_pass = urlp->url_pass;
1357 if (ccp->cc_pass.s != NULL)
1358 goto jleave;
1360 if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL)
1361 goto js2pass;
1362 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1363 if ((s = xok_vlook(agent_shell_lookup, urlp, OXM_ALL)) != NULL) {
1364 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1365 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1366 if (!_agent_shell_lookup(urlp, s)) {
1367 ccp = NULL;
1368 goto jleave;
1369 } else if (urlp->url_pass.s != NULL) {
1370 ccp->cc_pass = urlp->url_pass;
1371 goto jleave;
1374 # endif
1375 # ifdef HAVE_NETRC
1376 if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) {
1377 ccp->cc_pass = urlp->url_pass;
1378 goto jleave;
1380 # endif
1381 if (ware & REQ_PASS) {
1382 if ((s = getpassword(NULL)) != NULL)
1383 js2pass:
1384 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1385 else {
1386 n_err(_("A password is necessary for %s authentication\n"), pstr);
1387 ccp = NULL;
1391 jleave:
1392 if (ccp != NULL && (options & OPT_D_VV))
1393 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1394 urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : n_empty),
1395 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : n_empty));
1396 NYD_LEAVE;
1397 return (ccp != NULL);
1399 #endif /* HAVE_SOCKETS */
1401 #ifdef HAVE_NETRC
1402 FL int
1403 c_netrc(void *v)
1405 char **argv = v;
1406 struct nrc_node *nrc;
1407 bool_t load_only;
1408 NYD_ENTER;
1410 load_only = FAL0;
1411 if (*argv == NULL)
1412 goto jlist;
1413 if (argv[1] != NULL)
1414 goto jerr;
1415 if (!asccasecmp(*argv, "show"))
1416 goto jlist;
1417 load_only = TRU1;
1418 if (!asccasecmp(*argv, "load"))
1419 goto jlist;
1420 if (!asccasecmp(*argv, "clear"))
1421 goto jclear;
1422 jerr:
1423 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1424 v = NULL;
1425 jleave:
1426 NYD_LEAVE;
1427 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1429 jlist: {
1430 FILE *fp;
1431 size_t l;
1433 if (_nrc_list == NULL)
1434 _nrc_init();
1435 if (_nrc_list == NRC_NODE_ERR) {
1436 n_err(_("Interpolate what file?\n"));
1437 v = NULL;
1438 goto jleave;
1440 if (load_only)
1441 goto jleave;
1443 if ((fp = Ftmp(NULL, "netrc", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL) {
1444 n_perr(_("tmpfile"), 0);
1445 v = NULL;
1446 goto jleave;
1449 for (l = 0, nrc = _nrc_list; nrc != NULL; ++l, nrc = nrc->nrc_next) {
1450 fprintf(fp, _("machine %s "), nrc->nrc_dat); /* XXX quote? */
1451 if (nrc->nrc_ulen > 0)
1452 fprintf(fp, _("login \"%s\" "),
1453 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1));
1454 if (nrc->nrc_plen > 0)
1455 fprintf(fp, _("password \"%s\"\n"),
1456 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1 + nrc->nrc_ulen +1));
1457 else
1458 putc('\n', fp);
1461 page_or_print(fp, l);
1462 Fclose(fp);
1464 goto jleave;
1466 jclear:
1467 if (_nrc_list == NRC_NODE_ERR)
1468 _nrc_list = NULL;
1469 while ((nrc = _nrc_list) != NULL) {
1470 _nrc_list = nrc->nrc_next;
1471 free(nrc);
1473 goto jleave;
1475 #endif /* HAVE_NETRC */
1477 #ifdef HAVE_MD5
1478 FL char *
1479 md5tohex(char hex[MD5TOHEX_SIZE], void const *vp)
1481 char const *cp = vp;
1482 size_t i, j;
1483 NYD_ENTER;
1485 for (i = 0; i < MD5TOHEX_SIZE / 2; ++i) {
1486 j = i << 1;
1487 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1488 hex[j] = __hex((cp[i] & 0xF0) >> 4);
1489 hex[++j] = __hex(cp[i] & 0x0F);
1490 # undef __hex
1492 NYD_LEAVE;
1493 return hex;
1496 FL char *
1497 cram_md5_string(struct str const *user, struct str const *pass,
1498 char const *b64)
1500 struct str in, out;
1501 char digest[16], *cp;
1502 NYD_ENTER;
1504 out.s = NULL;
1505 if(user->l >= UIZ_MAX - 1 - MD5TOHEX_SIZE - 1)
1506 goto jleave;
1507 if(pass->l >= INT_MAX)
1508 goto jleave;
1510 in.s = n_UNCONST(b64);
1511 in.l = strlen(in.s);
1512 if(!b64_decode(&out, &in))
1513 goto jleave;
1514 if(out.l >= INT_MAX){
1515 free(out.s);
1516 out.s = NULL;
1517 goto jleave;
1520 hmac_md5((uc_i*)out.s, out.l, (uc_i*)pass->s, pass->l, digest);
1521 free(out.s);
1522 cp = md5tohex(salloc(MD5TOHEX_SIZE +1), digest);
1524 in.l = user->l + MD5TOHEX_SIZE +1;
1525 in.s = ac_alloc(user->l + 1 + MD5TOHEX_SIZE +1);
1526 memcpy(in.s, user->s, user->l);
1527 in.s[user->l] = ' ';
1528 memcpy(&in.s[user->l + 1], cp, MD5TOHEX_SIZE);
1529 if(b64_encode(&out, &in, B64_SALLOC | B64_CRLF) == NULL)
1530 out.s = NULL;
1531 ac_free(in.s);
1532 jleave:
1533 NYD_LEAVE;
1534 return out.s;
1537 FL void
1538 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
1539 void *digest)
1542 * This code is taken from
1544 * Network Working Group H. Krawczyk
1545 * Request for Comments: 2104 IBM
1546 * Category: Informational M. Bellare
1547 * UCSD
1548 * R. Canetti
1549 * IBM
1550 * February 1997
1553 * HMAC: Keyed-Hashing for Message Authentication
1555 md5_ctx context;
1556 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
1557 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
1558 unsigned char tk[16];
1559 int i;
1560 NYD_ENTER;
1562 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1563 if (key_len > 64) {
1564 md5_ctx tctx;
1566 md5_init(&tctx);
1567 md5_update(&tctx, key, key_len);
1568 md5_final(tk, &tctx);
1570 key = tk;
1571 key_len = 16;
1574 /* the HMAC_MD5 transform looks like:
1576 * MD5(K XOR opad, MD5(K XOR ipad, text))
1578 * where K is an n byte key
1579 * ipad is the byte 0x36 repeated 64 times
1580 * opad is the byte 0x5c repeated 64 times
1581 * and text is the data being protected */
1583 /* start out by storing key in pads */
1584 memset(k_ipad, 0, sizeof k_ipad);
1585 memset(k_opad, 0, sizeof k_opad);
1586 memcpy(k_ipad, key, key_len);
1587 memcpy(k_opad, key, key_len);
1589 /* XOR key with ipad and opad values */
1590 for (i=0; i<64; i++) {
1591 k_ipad[i] ^= 0x36;
1592 k_opad[i] ^= 0x5c;
1595 /* perform inner MD5 */
1596 md5_init(&context); /* init context for 1st pass */
1597 md5_update(&context, k_ipad, 64); /* start with inner pad */
1598 md5_update(&context, text, text_len); /* then text of datagram */
1599 md5_final(digest, &context); /* finish up 1st pass */
1601 /* perform outer MD5 */
1602 md5_init(&context); /* init context for 2nd pass */
1603 md5_update(&context, k_opad, 64); /* start with outer pad */
1604 md5_update(&context, digest, 16); /* then results of 1st hash */
1605 md5_final(digest, &context); /* finish up 2nd pass */
1606 NYD_LEAVE;
1608 #endif /* HAVE_MD5 */
1610 /* s-it-mode */