make.rc, mk-conf.sh: OPT_DEVEL includes OPT_DEBUG, but not vice versa
[s-mailx.git] / urlcrecry.c
blobcba15d9406c9d6fcd05d8a8f1c6eeec4b00753b5
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[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 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 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 VFIELD_SIZEOF(struct nrc_node, nrc_dat) + hl +1 + ul +1 + pl +1);
228 if (nhead != NULL)
229 ntail->nrc_next = nx;
230 else
231 nhead = nx;
232 ntail = nx;
233 nx->nrc_next = NULL;
234 nx->nrc_mlen = hl;
235 nx->nrc_ulen = ul;
236 nx->nrc_plen = pl;
237 memcpy(nx->nrc_dat, host, ++hl);
238 memcpy(nx->nrc_dat + hl, user, ++ul);
239 memcpy(nx->nrc_dat + hl + ul, pass, ++pl);
241 if (t == NRC_MACHINE)
242 goto jm_h;
243 if (t == NRC_DEFAULT)
244 goto jdef;
245 if (t != NRC_NONE)
246 goto jnext;
247 break;
248 case NRC_ERROR:
249 jerr:
250 if (options & OPT_D_V)
251 n_err(_("Errors occurred while parsing %s\n"),
252 n_shexp_quote_cp(netrc_load, FAL0));
253 assert(nrc == NRC_NODE_ERR);
254 goto jleave;
257 if (nhead != NULL)
258 nrc = nhead;
259 n_sigman_cleanup_ping(&sm);
260 jleave:
261 if (fi != NULL) {
262 if (ispipe)
263 Pclose(fi, TRU1);
264 else
265 Fclose(fi);
267 if (nrc == NRC_NODE_ERR)
268 while (nhead != NULL) {
269 ntail = nhead;
270 nhead = nhead->nrc_next;
271 free(ntail);
273 j_leave:
274 _nrc_list = nrc;
275 NYD_LEAVE;
276 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
279 static enum nrc_token
280 __nrc_token(FILE *fi, char buffer[NRC_TOKEN_MAXLEN], bool_t *nl_last)
282 int c;
283 char *cp;
284 enum nrc_token rv;
285 NYD2_ENTER;
287 rv = NRC_NONE;
288 for (;;) {
289 bool_t seen_nl;
291 c = EOF;
292 if (feof(fi) || ferror(fi))
293 goto jleave;
295 for (seen_nl = *nl_last; (c = getc(fi)) != EOF && whitechar(c);)
296 seen_nl |= (c == '\n');
298 if (c == EOF)
299 goto jleave;
300 /* fetchmail and derived parsers support comments */
301 if ((*nl_last = seen_nl) && c == '#') {
302 while ((c = getc(fi)) != EOF && c != '\n')
304 continue;
306 break;
309 cp = buffer;
310 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
311 if (c == '"' || c == '\'') {
312 int quotec = c;
314 /* Not requiring the closing QM is (Net)BSD syntax */
315 while ((c = getc(fi)) != EOF && c != quotec) {
316 /* Backslash escaping the next character is (Net)BSD syntax */
317 if (c == '\\')
318 if ((c = getc(fi)) == EOF)
319 break;
320 *cp++ = c;
321 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
322 rv = NRC_ERROR;
323 goto jleave;
326 } else {
327 *cp++ = c;
328 while ((c = getc(fi)) != EOF && !whitechar(c)) {
329 /* Backslash escaping the next character is (Net)BSD syntax */
330 if (c == '\\' && (c = getc(fi)) == EOF)
331 break;
332 *cp++ = c;
333 if (PTRCMP(cp, ==, buffer + NRC_TOKEN_MAXLEN)) {
334 rv = NRC_ERROR;
335 goto jleave;
338 *nl_last = (c == '\n');
340 *cp = '\0';
342 if (*buffer == '\0')
343 do {/*rv = NRC_NONE*/} while (0);
344 else if (!strcmp(buffer, "default"))
345 rv = NRC_DEFAULT;
346 else if (!strcmp(buffer, "login"))
347 rv = NRC_LOGIN;
348 else if (!strcmp(buffer, "password") || !strcmp(buffer, "passwd"))
349 rv = NRC_PASSWORD;
350 else if (!strcmp(buffer, "account"))
351 rv = NRC_ACCOUNT;
352 else if (!strcmp(buffer, "macdef"))
353 rv = NRC_MACDEF;
354 else if (!strcmp(buffer, "machine"))
355 rv = NRC_MACHINE;
356 else
357 rv = NRC_INPUT;
358 jleave:
359 if (c == EOF && !feof(fi))
360 rv = NRC_ERROR;
361 NYD2_LEAVE;
362 return rv;
365 static bool_t
366 _nrc_lookup(struct url *urlp, bool_t only_pass)
368 struct nrc_node *nrc, *nrc_wild, *nrc_exact;
369 bool_t rv = FAL0;
370 NYD_ENTER;
372 assert(!only_pass || urlp->url_user.s != NULL);
373 assert(only_pass || urlp->url_user.s == NULL);
375 if (_nrc_list == NULL)
376 _nrc_init();
377 if (_nrc_list == NRC_NODE_ERR)
378 goto jleave;
380 nrc_wild = nrc_exact = NULL;
381 for (nrc = _nrc_list; nrc != NULL; nrc = nrc->nrc_next)
382 switch (__nrc_host_match(nrc, urlp)) {
383 case 1:
384 nrc->nrc_result = nrc_exact;
385 nrc_exact = nrc;
386 continue;
387 case -1:
388 nrc->nrc_result = nrc_wild;
389 nrc_wild = nrc;
390 /* FALLTHRU */
391 case 0:
392 continue;
395 if (!only_pass && urlp->url_user.s == NULL) {
396 /* Must be an unambiguous entry of its kind */
397 if (nrc_exact != NULL && nrc_exact->nrc_result != NULL)
398 goto jleave;
399 if (__nrc_find_user(urlp, nrc_exact))
400 goto j_user;
402 if (nrc_wild != NULL && nrc_wild->nrc_result != NULL)
403 goto jleave;
404 if (!__nrc_find_user(urlp, nrc_wild))
405 goto jleave;
406 j_user:
410 if (__nrc_find_pass(urlp, TRU1, nrc_exact) ||
411 __nrc_find_pass(urlp, TRU1, nrc_wild) ||
412 /* Do not try to find a password without exact user match unless we've
413 * been called during credential lookup, a.k.a. the second time */
414 !only_pass ||
415 __nrc_find_pass(urlp, FAL0, nrc_exact) ||
416 __nrc_find_pass(urlp, FAL0, nrc_wild))
417 rv = TRU1;
418 jleave:
419 NYD_LEAVE;
420 return rv;
423 static int
424 __nrc_host_match(struct nrc_node const *nrc, struct url const *urlp)
426 char const *d2, *d1;
427 size_t l2, l1;
428 int rv = 0;
429 NYD2_ENTER;
431 /* Find a matching machine -- entries are all lowercase normalized */
432 if (nrc->nrc_mlen == urlp->url_host.l) {
433 if (LIKELY(!memcmp(nrc->nrc_dat, urlp->url_host.s, urlp->url_host.l)))
434 rv = 1;
435 goto jleave;
438 /* Cannot be an exact match, but maybe the .netrc machine starts with
439 * a "*." glob, which we recognize as an extension, meaning "skip
440 * a single subdomain, then match the rest" */
441 d1 = nrc->nrc_dat + 2;
442 l1 = nrc->nrc_mlen;
443 if (l1 <= 2 || d1[-1] != '.' || d1[-2] != '*')
444 goto jleave;
445 l1 -= 2;
447 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
448 * in fact this even succeeds for ".host.com", but - why care, here? */
449 d2 = urlp->url_host.s;
450 l2 = urlp->url_host.l;
451 while (l2 > 0) {
452 --l2;
453 if (*d2++ == '.')
454 break;
457 if (l2 == l1 && !memcmp(d1, d2, l1))
458 /* This matches, but we won't use it directly but watch out for an
459 * exact match first! */
460 rv = -1;
461 jleave:
462 NYD2_LEAVE;
463 return rv;
466 static bool_t
467 __nrc_find_user(struct url *urlp, struct nrc_node const *nrc)
469 NYD2_ENTER;
471 for (; nrc != NULL; nrc = nrc->nrc_result)
472 if (nrc->nrc_ulen > 0) {
473 /* Fake it was part of URL otherwise XXX */
474 urlp->url_had_user = TRU1;
475 /* That buffer will be duplicated by url_parse() in this case! */
476 urlp->url_user.s = UNCONST(nrc->nrc_dat + nrc->nrc_mlen +1);
477 urlp->url_user.l = nrc->nrc_ulen;
478 break;
481 NYD2_LEAVE;
482 return (nrc != NULL);
485 static bool_t
486 __nrc_find_pass(struct url *urlp, bool_t user_match, struct nrc_node const *nrc)
488 NYD2_ENTER;
490 for (; nrc != NULL; nrc = nrc->nrc_result) {
491 bool_t um = (nrc->nrc_ulen == urlp->url_user.l &&
492 !memcmp(nrc->nrc_dat + nrc->nrc_mlen +1, urlp->url_user.s,
493 urlp->url_user.l));
495 if (user_match) {
496 if (!um)
497 continue;
498 } else if (!um && nrc->nrc_ulen > 0)
499 continue;
500 if (nrc->nrc_plen == 0)
501 continue;
503 /* We are responsible for duplicating this buffer! */
504 urlp->url_pass.s = savestrbuf(nrc->nrc_dat + nrc->nrc_mlen +1 +
505 nrc->nrc_ulen + 1, (urlp->url_pass.l = nrc->nrc_plen));
506 break;
509 NYD2_LEAVE;
510 return (nrc != NULL);
512 #endif /* HAVE_NETRC */
514 #ifdef HAVE_AGENT
515 static bool_t
516 _agent_shell_lookup(struct url *urlp, char const *comm) /* TODO v15-compat */
518 char buf[128];
519 char const *env_addon[8];
520 struct str s;
521 FILE *pbuf;
522 union {char const *cp; int c; sighandler_type sht;} u;
523 size_t cl, l;
524 bool_t rv = FAL0;
525 NYD2_ENTER;
527 env_addon[0] = str_concat_csvl(&s, AGENT_USER, "=", urlp->url_user.s,
528 NULL)->s;
529 env_addon[1] = str_concat_csvl(&s, AGENT_USER_ENC, "=", urlp->url_user_enc.s,
530 NULL)->s;
531 env_addon[2] = str_concat_csvl(&s, AGENT_HOST, "=", urlp->url_host.s,
532 NULL)->s;
533 env_addon[3] = str_concat_csvl(&s, AGENT_HOST_PORT, "=", urlp->url_h_p.s,
534 NULL)->s;
535 u.cp = ok_vlook(TMPDIR);
536 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, "=", u.cp, NULL)->s;
537 env_addon[5] = str_concat_csvl(&s, "TMPDIR", "=", u.cp, NULL)->s;
539 env_addon[6] = NULL;
541 if ((pbuf = Popen(comm, "r", ok_vlook(SHELL), env_addon, -1)) == NULL) {
542 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm);
543 goto jleave;
546 for (s.s = NULL, s.l = cl = l = 0; (u.c = getc(pbuf)) != EOF; ++cl) {
547 if (u.c == '\n') /* xxx */
548 continue;
549 buf[l++] = u.c;
550 if (l == sizeof(buf) - 1) {
551 n_str_add_buf(&s, buf, l);
552 l = 0;
555 if (l > 0)
556 n_str_add_buf(&s, buf, l);
558 if (!Pclose(pbuf, TRU1)) {
559 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm);
560 goto jleave;
563 /* We are responsible for duplicating this buffer! */
564 if (s.s != NULL)
565 urlp->url_pass.s = savestrbuf(s.s, urlp->url_pass.l = s.l);
566 else if (cl > 0)
567 urlp->url_pass.s = UNCONST(""), urlp->url_pass.l = 0;
568 rv = TRU1;
569 jleave:
570 if (s.s != NULL)
571 free(s.s);
572 NYD2_LEAVE;
573 return rv;
575 #endif /* HAVE_AGENT */
577 FL char *
578 (urlxenc)(char const *cp, bool_t ispath SALLOC_DEBUG_ARGS)
580 char *n, *np, c1;
581 NYD2_ENTER;
583 np = n = (salloc)(strlen(cp) * 3 +1 SALLOC_DEBUG_ARGSCALL);
585 for (; (c1 = *cp) != '\0'; ++cp) {
586 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
587 * ALPHA / DIGIT / "-" / "." / "_" / "~"
588 * However add a special is[file]path mode for file-system friendliness */
589 if (alnumchar(c1) || c1 == '_')
590 *np++ = c1;
591 else if (!ispath) {
592 if (c1 != '-' && c1 != '.' && c1 != '~')
593 goto jesc;
594 *np++ = c1;
595 } else if (PTRCMP(np, >, n) && (*cp == '-' || *cp == '.')) /* XXX imap */
596 *np++ = c1;
597 else {
598 jesc:
599 np[0] = '%';
600 n_c_to_hex_base16(np + 1, c1);
601 np += 3;
604 *np = '\0';
605 NYD2_LEAVE;
606 return n;
609 FL char *
610 (urlxdec)(char const *cp SALLOC_DEBUG_ARGS)
612 char *n, *np;
613 si32_t c;
614 NYD2_ENTER;
616 np = n = (salloc)(strlen(cp) +1 SALLOC_DEBUG_ARGSCALL);
618 while ((c = (uc_i)*cp++) != '\0') {
619 if (c == '%' && cp[0] != '\0' && cp[1] != '\0') {
620 si32_t o = c;
621 if (LIKELY((c = n_c_from_hex_base16(cp)) >= '\0'))
622 cp += 2;
623 else
624 c = o;
626 *np++ = (char)c;
628 *np = '\0';
629 NYD2_LEAVE;
630 return n;
633 FL char const *
634 n_servbyname(char const *proto, ui16_t *irv_or_null){
635 static struct{
636 char const name[14];
637 char const port[8];
638 ui16_t portno;
639 } const tbl[] = {
640 { "smtp", "25", 25},
641 { "submission", "587", 587},
642 { "smtps", "465", 465},
643 { "pop3", "110", 110},
644 { "pop3s", "995", 995},
645 { "imap", "143", 143},
646 { "imaps", "993", 993},
647 { "file", "", 0}
649 char const *rv;
650 size_t l, i;
651 NYD2_ENTER;
653 for(rv = proto; *rv != '\0'; ++rv)
654 if(*rv == ':')
655 break;
656 l = PTR2SIZE(rv - proto);
658 for(rv = NULL, i = 0; i < NELEM(tbl); ++i)
659 if(!ascncasecmp(tbl[i].name, proto, l)){
660 rv = tbl[i].port;
661 if(irv_or_null != NULL)
662 *irv_or_null = tbl[i].portno;
663 break;
665 NYD2_LEAVE;
666 return rv;
669 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
670 FL bool_t
671 url_parse(struct url *urlp, enum cproto cproto, char const *data)
673 #if defined HAVE_SMTP && defined HAVE_POP3
674 # define __ALLPROTO
675 #endif
676 #if defined HAVE_SMTP || defined HAVE_POP3
677 # define __ANYPROTO
678 char *cp, *x;
679 #endif
680 bool_t rv = FAL0;
681 NYD_ENTER;
682 UNUSED(data);
684 memset(urlp, 0, sizeof *urlp);
685 urlp->url_input = data;
686 urlp->url_cproto = cproto;
688 /* Network protocol */
689 #define _protox(X,Y) \
690 urlp->url_portno = Y;\
691 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
692 urlp->url_proto[sizeof(X) -1] = '\0';\
693 urlp->url_proto_len = sizeof(X) -1;\
694 urlp->url_proto_xlen = sizeof(X "://") -1
695 #define __if(X,Y,Z) \
696 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
697 _protox(X, Y);\
698 data += sizeof(X "://") -1;\
699 do { Z; } while (0);\
700 goto juser;\
702 #define _if(X,Y) __if(X, Y, (void)0)
703 #ifdef HAVE_SSL
704 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
705 #else
706 # define _ifs(X,Y) goto jeproto;
707 #endif
709 switch (cproto) {
710 case CPROTO_SMTP:
711 #ifdef HAVE_SMTP
712 _if ("smtp", 25)
713 _if ("submission", 587)
714 _ifs ("smtps", 465)
715 _protox("smtp", 25);
716 break;
717 #else
718 goto jeproto;
719 #endif
720 case CPROTO_POP3:
721 #ifdef HAVE_POP3
722 _if ("pop3", 110)
723 _ifs ("pop3s", 995)
724 _protox("pop3", 110);
725 break;
726 #else
727 goto jeproto;
728 #endif
729 #if 0
730 case CPROTO_IMAP:
731 _if ("imap", 143)
732 _ifs ("imaps", 993)
733 _protox("imap", 143);
734 break;
735 goto jeproto;
736 #endif
739 #undef _ifs
740 #undef _if
741 #undef __if
742 #undef _protox
744 if (strstr(data, "://") != NULL) {
745 #if !defined __ALLPROTO || !defined HAVE_SSL
746 jeproto:
747 #endif
748 n_err(_("URL proto:// prefix invalid: %s\n"), urlp->url_input);
749 goto jleave;
751 #ifdef __ANYPROTO
753 /* User and password, I */
754 juser:
755 if ((cp = _url_last_at_before_slash(data)) != NULL) {
756 size_t l = PTR2SIZE(cp - data);
757 char const *d = data;
758 char *ub = ac_alloc(l +1);
760 urlp->url_had_user = TRU1;
761 data = cp + 1;
763 /* And also have a password? */
764 if ((cp = memchr(d, ':', l)) != NULL) {
765 size_t i = PTR2SIZE(cp - d);
767 l -= i + 1;
768 memcpy(ub, cp + 1, l);
769 ub[l] = '\0';
770 urlp->url_pass.l = strlen(urlp->url_pass.s = urlxdec(ub));
772 if (strcmp(ub, urlxenc(urlp->url_pass.s, FAL0))) {
773 n_err(_("String is not properly URL percent encoded: %s\n"),
774 ub);
775 goto jleave;
777 l = i;
780 memcpy(ub, d, l);
781 ub[l] = '\0';
782 urlp->url_user.l = strlen(urlp->url_user.s = urlxdec(ub));
783 urlp->url_user_enc.l = strlen(
784 urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0));
786 if (urlp->url_user_enc.l != l || memcmp(urlp->url_user_enc.s, ub, l)) {
787 n_err(_("String is not properly URL percent encoded: %s\n"), ub);
788 goto jleave;
791 ac_free(ub);
794 /* Servername and port -- and possible path suffix */
795 if ((cp = strchr(data, ':')) != NULL) { /* TODO URL parse, IPv6 support */
796 char *eptr;
797 long l;
799 urlp->url_port = x = savestr(x = &cp[1]);
800 if ((x = strchr(x, '/')) != NULL) {
801 *x = '\0';
802 while(*++x == '/')
805 l = strtol(urlp->url_port, &eptr, 10);
806 if (*eptr != '\0' || l <= 0 || UICMP(32, l, >=, 0xFFFFu)) {
807 n_err(_("URL with invalid port number: %s\n"), urlp->url_input);
808 goto jleave;
810 urlp->url_portno = (ui16_t)l;
811 } else {
812 if ((x = strchr(data, '/')) != NULL) {
813 data = savestrbuf(data, PTR2SIZE(x - data));
814 while(*++x == '/')
817 cp = UNCONST(data + strlen(data));
820 /* A (non-empty) path may only occur with IMAP */
821 if (x != NULL && *x != '\0') {
822 /* Take care not to count adjacent slashes for real, on either end */
823 char *x2;
824 size_t i;
826 for(x2 = savestrbuf(x, i = strlen(x)); i > 0; --i)
827 if(x2[i - 1] != '/')
828 break;
829 x2[i] = '\0';
831 if (i > 0) {
832 #if 0
833 if (cproto != CPROTO_IMAP) {
834 #endif
835 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
836 urlp->url_input);
837 goto jleave;
838 #if 0
840 urlp->url_path.l = i;
841 urlp->url_path.s = x2;
842 #endif
846 urlp->url_host.s = savestrbuf(data, urlp->url_host.l = PTR2SIZE(cp - data));
847 { size_t i;
848 for (cp = urlp->url_host.s, i = urlp->url_host.l; i != 0; ++cp, --i)
849 *cp = lowerconv(*cp);
852 /* .url_h_p: HOST:PORT */
853 { size_t i;
854 struct str *s = &urlp->url_h_p;
856 s->s = salloc(urlp->url_host.l + 1 + sizeof("65536")-1 +1);
857 memcpy(s->s, urlp->url_host.s, i = urlp->url_host.l);
858 if (urlp->url_port != NULL) {
859 size_t j = strlen(urlp->url_port);
860 s->s[i++] = ':';
861 memcpy(s->s + i, urlp->url_port, j);
862 i += j;
864 s->s[i] = '\0';
865 s->l = i;
868 /* User, II
869 * If there was no user in the URL, do we have *user-HOST* or *user*? */
870 if (!urlp->url_had_user) {
871 if ((urlp->url_user.s = xok_vlook(user, urlp, OXM_PLAIN | OXM_H_P))
872 == NULL) {
873 /* No, check whether .netrc lookup is desired */
874 # ifdef HAVE_NETRC
875 if (!ok_blook(v15_compat) ||
876 !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) ||
877 !_nrc_lookup(urlp, FAL0))
878 # endif
879 urlp->url_user.s = UNCONST(myname);
882 urlp->url_user.l = strlen(urlp->url_user.s);
883 urlp->url_user.s = savestrbuf(urlp->url_user.s, urlp->url_user.l);
884 urlp->url_user_enc.l = strlen(
885 urlp->url_user_enc.s = urlxenc(urlp->url_user.s, FAL0));
888 /* And then there are a lot of prebuild string combinations TODO do lazy */
890 /* .url_u_h: .url_user@.url_host
891 * For SMTP we apply ridiculously complicated *v15-compat* plus
892 * *smtp-hostname* / *hostname* dependent rules */
893 { struct str h, *s;
894 size_t i;
896 if (cproto == CPROTO_SMTP && ok_blook(v15_compat) &&
897 (cp = ok_vlook(smtp_hostname)) != NULL) {
898 if (*cp == '\0')
899 cp = nodename(1);
900 h.s = savestrbuf(cp, h.l = strlen(cp));
901 } else
902 h = urlp->url_host;
904 s = &urlp->url_u_h;
905 i = urlp->url_user.l;
907 s->s = salloc(i + 1 + h.l +1);
908 if (i > 0) {
909 memcpy(s->s, urlp->url_user.s, i);
910 s->s[i++] = '@';
912 memcpy(s->s + i, h.s, h.l +1);
913 i += h.l;
914 s->l = i;
917 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
918 { struct str *s = &urlp->url_u_h_p;
919 size_t i = urlp->url_user.l;
921 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
922 if (i > 0) {
923 memcpy(s->s, urlp->url_user.s, i);
924 s->s[i++] = '@';
926 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
927 i += urlp->url_h_p.l;
928 s->l = i;
931 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
932 { struct str *s = &urlp->url_eu_h_p;
933 size_t i = urlp->url_user_enc.l;
935 s->s = salloc(i + 1 + urlp->url_h_p.l +1);
936 if (i > 0) {
937 memcpy(s->s, urlp->url_user_enc.s, i);
938 s->s[i++] = '@';
940 memcpy(s->s + i, urlp->url_h_p.s, urlp->url_h_p.l +1);
941 i += urlp->url_h_p.l;
942 s->l = i;
945 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
946 { size_t i;
947 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_u_h_p.l) +1);
949 urlp->url_proto[urlp->url_proto_len] = ':';
950 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_u_h_p.s,
951 urlp->url_u_h_p.l +1);
952 urlp->url_proto[urlp->url_proto_len] = '\0';
954 urlp->url_p_u_h_p = ud;
957 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
958 { size_t i;
959 char *ud = salloc((i = urlp->url_proto_xlen + urlp->url_eu_h_p.l) +
960 1 + urlp->url_path.l +1);
962 urlp->url_proto[urlp->url_proto_len] = ':';
963 memcpy(sstpcpy(ud, urlp->url_proto), urlp->url_eu_h_p.s,
964 urlp->url_eu_h_p.l +1);
965 urlp->url_proto[urlp->url_proto_len] = '\0';
967 if (urlp->url_path.l == 0)
968 urlp->url_p_eu_h_p = urlp->url_p_eu_h_p_p = ud;
969 else {
970 urlp->url_p_eu_h_p = savestrbuf(ud, i);
971 urlp->url_p_eu_h_p_p = ud;
972 ud += i;
973 *ud++ = '/';
974 memcpy(ud, urlp->url_path.s, urlp->url_path.l +1);
978 rv = TRU1;
979 #endif /* __ANYPROTO */
980 jleave:
981 NYD_LEAVE;
982 return rv;
983 #undef __ANYPROTO
984 #undef __ALLPROTO
987 FL bool_t
988 ccred_lookup_old(struct ccred *ccp, enum cproto cproto, char const *addr)
990 char const *pname, *pxstr, *authdef;
991 size_t pxlen, addrlen, i;
992 char *vbuf, *s;
993 ui8_t authmask;
994 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
995 ware = NONE;
996 bool_t addr_is_nuser = FAL0; /* XXX v15.0 legacy! v15_compat */
997 NYD_ENTER;
999 memset(ccp, 0, sizeof *ccp);
1001 switch (cproto) {
1002 default:
1003 case CPROTO_SMTP:
1004 pname = "SMTP";
1005 pxstr = "smtp-auth";
1006 pxlen = sizeof("smtp-auth") -1;
1007 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1008 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1009 authdef = "none";
1010 addr_is_nuser = TRU1;
1011 break;
1012 case CPROTO_POP3:
1013 pname = "POP3";
1014 pxstr = "pop3-auth";
1015 pxlen = sizeof("pop3-auth") -1;
1016 authmask = AUTHTYPE_PLAIN;
1017 authdef = "plain";
1018 break;
1021 ccp->cc_cproto = cproto;
1022 addrlen = strlen(addr);
1023 vbuf = ac_alloc(pxlen + addrlen + sizeof("-password-")-1 +1);
1024 memcpy(vbuf, pxstr, pxlen);
1026 /* Authentication type */
1027 vbuf[pxlen] = '-';
1028 memcpy(vbuf + pxlen + 1, addr, addrlen +1);
1029 if ((s = vok_vlook(vbuf)) == NULL) {
1030 vbuf[pxlen] = '\0';
1031 if ((s = vok_vlook(vbuf)) == NULL)
1032 s = UNCONST(authdef);
1035 if (!asccasecmp(s, "none")) {
1036 ccp->cc_auth = "NONE";
1037 ccp->cc_authtype = AUTHTYPE_NONE;
1038 /*ware = NONE;*/
1039 } else if (!asccasecmp(s, "plain")) {
1040 ccp->cc_auth = "PLAIN";
1041 ccp->cc_authtype = AUTHTYPE_PLAIN;
1042 ware = REQ_PASS | REQ_USER;
1043 } else if (!asccasecmp(s, "login")) {
1044 ccp->cc_auth = "LOGIN";
1045 ccp->cc_authtype = AUTHTYPE_LOGIN;
1046 ware = REQ_PASS | REQ_USER;
1047 } else if (!asccasecmp(s, "cram-md5")) {
1048 ccp->cc_auth = "CRAM-MD5";
1049 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1050 ware = REQ_PASS | REQ_USER;
1051 } else if (!asccasecmp(s, "gssapi")) {
1052 ccp->cc_auth = "GSS-API";
1053 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1054 ware = REQ_USER;
1055 } /* no else */
1057 /* Verify method */
1058 if (!(ccp->cc_authtype & authmask)) {
1059 n_err(_("Unsupported %s authentication method: %s\n"), pname, s);
1060 ccp = NULL;
1061 goto jleave;
1063 # ifndef HAVE_MD5
1064 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1065 n_err(_("No CRAM-MD5 support compiled in\n"));
1066 ccp = NULL;
1067 goto jleave;
1069 # endif
1070 # ifndef HAVE_GSSAPI
1071 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1072 n_err(_("No GSS-API support compiled in\n"));
1073 ccp = NULL;
1074 goto jleave;
1076 # endif
1078 /* User name */
1079 if (!(ware & (WANT_USER | REQ_USER)))
1080 goto jpass;
1082 if (!addr_is_nuser) {
1083 if ((s = _url_last_at_before_slash(addr)) != NULL) {
1084 ccp->cc_user.s = urlxdec(savestrbuf(addr, PTR2SIZE(s - addr)));
1085 ccp->cc_user.l = strlen(ccp->cc_user.s);
1086 } else if (ware & REQ_USER)
1087 goto jgetuser;
1088 goto jpass;
1091 memcpy(vbuf + pxlen, "-user-", i = sizeof("-user-") -1);
1092 i += pxlen;
1093 memcpy(vbuf + i, addr, addrlen +1);
1094 if ((s = vok_vlook(vbuf)) == NULL) {
1095 vbuf[--i] = '\0';
1096 if ((s = vok_vlook(vbuf)) == NULL && (ware & REQ_USER)) {
1097 if ((s = getuser(NULL)) == NULL) {
1098 jgetuser: /* TODO v15.0: today we simply bail, but we should call getuser().
1099 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1100 * TODO check that first, then! change control flow, grow vbuf */
1101 n_err(_("A user is necessary for %s authentication\n"), pname);
1102 ccp = NULL;
1103 goto jleave;
1107 ccp->cc_user.l = strlen(ccp->cc_user.s = savestr(s));
1109 /* Password */
1110 jpass:
1111 if (!(ware & (WANT_PASS | REQ_PASS)))
1112 goto jleave;
1114 if (!addr_is_nuser) {
1115 memcpy(vbuf, "password-", i = sizeof("password-") -1);
1116 } else {
1117 memcpy(vbuf + pxlen, "-password-", i = sizeof("-password-") -1);
1118 i += pxlen;
1120 memcpy(vbuf + i, addr, addrlen +1);
1121 if ((s = vok_vlook(vbuf)) == NULL) {
1122 vbuf[--i] = '\0';
1123 if ((!addr_is_nuser || (s = vok_vlook(vbuf)) == NULL) &&
1124 (ware & REQ_PASS)) {
1125 if ((s = getpassword(NULL)) == NULL) {
1126 n_err(_("A password is necessary for %s authentication\n"),
1127 pname);
1128 ccp = NULL;
1129 goto jleave;
1133 if (s != NULL)
1134 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1136 jleave:
1137 ac_free(vbuf);
1138 if (ccp != NULL && (options & OPT_D_VV))
1139 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1140 addr, (ccp->cc_user.s != NULL ? ccp->cc_user.s : ""),
1141 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : ""));
1142 NYD_LEAVE;
1143 return (ccp != NULL);
1146 FL bool_t
1147 ccred_lookup(struct ccred *ccp, struct url *urlp)
1149 char const *pstr, *authdef;
1150 char *s;
1151 enum okeys authokey;
1152 ui8_t authmask;
1153 enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3}
1154 ware = NONE;
1155 NYD_ENTER;
1157 memset(ccp, 0, sizeof *ccp);
1158 ccp->cc_user = urlp->url_user;
1160 switch ((ccp->cc_cproto = urlp->url_cproto)) {
1161 default:
1162 case CPROTO_SMTP:
1163 pstr = "smtp";
1164 authokey = ok_v_smtp_auth;
1165 authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN |
1166 AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI;
1167 authdef = "plain";
1168 break;
1169 case CPROTO_POP3:
1170 pstr = "pop3";
1171 authokey = ok_v_pop3_auth;
1172 authmask = AUTHTYPE_PLAIN;
1173 authdef = "plain";
1174 break;
1177 /* Authentication type */
1178 if ((s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL)
1179 s = UNCONST(authdef);
1181 if (!asccasecmp(s, "none")) {
1182 ccp->cc_auth = "NONE";
1183 ccp->cc_authtype = AUTHTYPE_NONE;
1184 /*ware = NONE;*/
1185 } else if (!asccasecmp(s, "plain")) {
1186 ccp->cc_auth = "PLAIN";
1187 ccp->cc_authtype = AUTHTYPE_PLAIN;
1188 ware = REQ_PASS | REQ_USER;
1189 } else if (!asccasecmp(s, "login")) {
1190 ccp->cc_auth = "LOGIN";
1191 ccp->cc_authtype = AUTHTYPE_LOGIN;
1192 ware = REQ_PASS | REQ_USER;
1193 } else if (!asccasecmp(s, "cram-md5")) {
1194 ccp->cc_auth = "CRAM-MD5";
1195 ccp->cc_authtype = AUTHTYPE_CRAM_MD5;
1196 ware = REQ_PASS | REQ_USER;
1197 } else if (!asccasecmp(s, "gssapi")) {
1198 ccp->cc_auth = "GSS-API";
1199 ccp->cc_authtype = AUTHTYPE_GSSAPI;
1200 ware = REQ_USER;
1201 } /* no else */
1203 /* Verify method */
1204 if (!(ccp->cc_authtype & authmask)) {
1205 n_err(_("Unsupported %s authentication method: %s\n"), pstr, s);
1206 ccp = NULL;
1207 goto jleave;
1209 # ifndef HAVE_MD5
1210 if (ccp->cc_authtype == AUTHTYPE_CRAM_MD5) {
1211 n_err(_("No CRAM-MD5 support compiled in\n"));
1212 ccp = NULL;
1213 goto jleave;
1215 # endif
1216 # ifndef HAVE_GSSAPI
1217 if (ccp->cc_authtype == AUTHTYPE_GSSAPI) {
1218 n_err(_("No GSS-API support compiled in\n"));
1219 ccp = NULL;
1220 goto jleave;
1222 # endif
1224 /* Password */
1225 ccp->cc_pass = urlp->url_pass;
1226 if (ccp->cc_pass.s != NULL)
1227 goto jleave;
1229 if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL)
1230 goto js2pass;
1231 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1232 if ((s = xok_vlook(agent_shell_lookup, urlp, OXM_ALL)) != NULL) {
1233 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1234 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1235 if (!_agent_shell_lookup(urlp, s)) {
1236 ccp = NULL;
1237 goto jleave;
1238 } else if (urlp->url_pass.s != NULL) {
1239 ccp->cc_pass = urlp->url_pass;
1240 goto jleave;
1243 # endif
1244 # ifdef HAVE_NETRC
1245 if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) {
1246 ccp->cc_pass = urlp->url_pass;
1247 goto jleave;
1249 # endif
1250 if (ware & REQ_PASS) {
1251 if ((s = getpassword(NULL)) != NULL)
1252 js2pass:
1253 ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s));
1254 else {
1255 n_err(_("A password is necessary for %s authentication\n"), pstr);
1256 ccp = NULL;
1260 jleave:
1261 if (ccp != NULL && (options & OPT_D_VV))
1262 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1263 urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : ""),
1264 (ccp->cc_pass.s != NULL ? ccp->cc_pass.s : ""));
1265 NYD_LEAVE;
1266 return (ccp != NULL);
1268 #endif /* HAVE_SOCKETS */
1270 #ifdef HAVE_NETRC
1271 FL int
1272 c_netrc(void *v)
1274 char **argv = v;
1275 struct nrc_node *nrc;
1276 bool_t load_only;
1277 NYD_ENTER;
1279 load_only = FAL0;
1280 if (*argv == NULL)
1281 goto jlist;
1282 if (argv[1] != NULL)
1283 goto jerr;
1284 if (!asccasecmp(*argv, "show"))
1285 goto jlist;
1286 load_only = TRU1;
1287 if (!asccasecmp(*argv, "load"))
1288 goto jlist;
1289 if (!asccasecmp(*argv, "clear"))
1290 goto jclear;
1291 jerr:
1292 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1293 v = NULL;
1294 jleave:
1295 NYD_LEAVE;
1296 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1298 jlist: {
1299 FILE *fp;
1300 size_t l;
1302 if (_nrc_list == NULL)
1303 _nrc_init();
1304 if (_nrc_list == NRC_NODE_ERR) {
1305 n_err(_("Interpolate what file?\n"));
1306 v = NULL;
1307 goto jleave;
1309 if (load_only)
1310 goto jleave;
1312 if ((fp = Ftmp(NULL, "netrc", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL) {
1313 n_perr(_("tmpfile"), 0);
1314 v = NULL;
1315 goto jleave;
1318 for (l = 0, nrc = _nrc_list; nrc != NULL; ++l, nrc = nrc->nrc_next) {
1319 fprintf(fp, _("machine %s "), nrc->nrc_dat); /* XXX quote? */
1320 if (nrc->nrc_ulen > 0)
1321 fprintf(fp, _("login \"%s\" "),
1322 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1));
1323 if (nrc->nrc_plen > 0)
1324 fprintf(fp, _("password \"%s\"\n"),
1325 string_quote(nrc->nrc_dat + nrc->nrc_mlen +1 + nrc->nrc_ulen +1));
1326 else
1327 putc('\n', fp);
1330 page_or_print(fp, l);
1331 Fclose(fp);
1333 goto jleave;
1335 jclear:
1336 if (_nrc_list == NRC_NODE_ERR)
1337 _nrc_list = NULL;
1338 while ((nrc = _nrc_list) != NULL) {
1339 _nrc_list = nrc->nrc_next;
1340 free(nrc);
1342 goto jleave;
1344 #endif /* HAVE_NETRC */
1346 #ifdef HAVE_MD5
1347 FL char *
1348 md5tohex(char hex[MD5TOHEX_SIZE], void const *vp)
1350 char const *cp = vp;
1351 size_t i, j;
1352 NYD_ENTER;
1354 for (i = 0; i < MD5TOHEX_SIZE / 2; ++i) {
1355 j = i << 1;
1356 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1357 hex[j] = __hex((cp[i] & 0xF0) >> 4);
1358 hex[++j] = __hex(cp[i] & 0x0F);
1359 # undef __hex
1361 NYD_LEAVE;
1362 return hex;
1365 FL char *
1366 cram_md5_string(struct str const *user, struct str const *pass,
1367 char const *b64)
1369 struct str in, out;
1370 char digest[16], *cp;
1371 NYD_ENTER;
1373 out.s = NULL;
1374 in.s = UNCONST(b64);
1375 in.l = strlen(in.s);
1376 b64_decode(&out, &in, NULL);
1377 assert(out.s != NULL);
1379 hmac_md5((uc_i*)out.s, out.l, (uc_i*)pass->s, pass->l, digest);
1380 free(out.s);
1381 cp = md5tohex(salloc(MD5TOHEX_SIZE +1), digest);
1383 in.l = user->l + MD5TOHEX_SIZE +1;
1384 in.s = ac_alloc(user->l + 1 + MD5TOHEX_SIZE +1);
1385 memcpy(in.s, user->s, user->l);
1386 in.s[user->l] = ' ';
1387 memcpy(in.s + user->l + 1, cp, MD5TOHEX_SIZE);
1388 b64_encode(&out, &in, B64_SALLOC | B64_CRLF);
1389 ac_free(in.s);
1390 NYD_LEAVE;
1391 return out.s;
1394 FL void
1395 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
1396 void *digest)
1399 * This code is taken from
1401 * Network Working Group H. Krawczyk
1402 * Request for Comments: 2104 IBM
1403 * Category: Informational M. Bellare
1404 * UCSD
1405 * R. Canetti
1406 * IBM
1407 * February 1997
1410 * HMAC: Keyed-Hashing for Message Authentication
1412 md5_ctx context;
1413 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
1414 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
1415 unsigned char tk[16];
1416 int i;
1417 NYD_ENTER;
1419 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1420 if (key_len > 64) {
1421 md5_ctx tctx;
1423 md5_init(&tctx);
1424 md5_update(&tctx, key, key_len);
1425 md5_final(tk, &tctx);
1427 key = tk;
1428 key_len = 16;
1431 /* the HMAC_MD5 transform looks like:
1433 * MD5(K XOR opad, MD5(K XOR ipad, text))
1435 * where K is an n byte key
1436 * ipad is the byte 0x36 repeated 64 times
1437 * opad is the byte 0x5c repeated 64 times
1438 * and text is the data being protected */
1440 /* start out by storing key in pads */
1441 memset(k_ipad, 0, sizeof k_ipad);
1442 memset(k_opad, 0, sizeof k_opad);
1443 memcpy(k_ipad, key, key_len);
1444 memcpy(k_opad, key, key_len);
1446 /* XOR key with ipad and opad values */
1447 for (i=0; i<64; i++) {
1448 k_ipad[i] ^= 0x36;
1449 k_opad[i] ^= 0x5c;
1452 /* perform inner MD5 */
1453 md5_init(&context); /* init context for 1st pass */
1454 md5_update(&context, k_ipad, 64); /* start with inner pad */
1455 md5_update(&context, text, text_len); /* then text of datagram */
1456 md5_final(digest, &context); /* finish up 1st pass */
1458 /* perform outer MD5 */
1459 md5_init(&context); /* init context for 2nd pass */
1460 md5_update(&context, k_opad, 64); /* start with outer pad */
1461 md5_update(&context, digest, 16); /* then results of 1st hash */
1462 md5_final(digest, &context); /* finish up 2nd pass */
1463 NYD_LEAVE;
1465 #endif /* HAVE_MD5 */
1467 /* s-it-mode */