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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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.
21 #define n_FILE urlcrecry
23 #ifndef HAVE_AMALGAMATION
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)
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
);
64 /* Initialize .netrc cache */
65 static void _nrc_init(void);
66 static enum nrc_token
__nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
],
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 */
85 static bool_t
_agent_shell_lookup(struct url
*urlp
, char const *comm
);
90 _url_last_at_before_slash(char const *sp
)
96 for (cp
= sp
; (c
= *cp
) != '\0'; ++cp
)
99 while (cp
> sp
&& *--cp
!= '@')
113 char buffer
[NRC_TOKEN_MAXLEN
], host
[NRC_TOKEN_MAXLEN
],
114 user
[NRC_TOKEN_MAXLEN
], pass
[NRC_TOKEN_MAXLEN
], *netrc_load
;
118 bool_t ispipe
, seen_default
, nl_last
;
119 struct nrc_node
*ntail
, *nhead
, *nrc
;
128 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
135 if ((netrc_load
= ok_vlook(netrc_pipe
)) != NULL
) {
137 if ((fi
= Popen(netrc_load
, "r", ok_vlook(SHELL
), NULL
, COMMAND_FD_NULL
)
139 n_perr(netrc_load
, 0);
143 if ((netrc_load
= file_expand(ok_vlook(NETRC
))) == NULL
)
146 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
147 n_err(_("Cannot open \"%s\"\n"), netrc_load
);
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"),
163 switch((t
= __nrc_token(fi
, buffer
, &nl_last
))) {
166 default: /* Doesn't happen (but on error?), keep CC happy */
169 /* We ignore the default entry (require an exact host match), and we also
170 * ignore anything after such an entry (faulty syntax) */
175 /* Normalize HOST to lowercase */
177 if (!seen_default
&& (t
= __nrc_token(fi
, host
, &nl_last
)) != NRC_INPUT
)
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
) {
190 if ((t
= __nrc_token(fi
, user
, &nl_last
)) != NRC_INPUT
)
194 if ((t
= __nrc_token(fi
, pass
, &nl_last
)) != NRC_INPUT
)
198 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
202 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
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 */
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);
229 ntail
->nrc_next
= nx
;
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
)
243 if (t
== NRC_DEFAULT
)
250 if (options
& OPT_D_V
)
251 n_err(_("Errors occurred while parsing \"%s\"\n"), netrc_load
);
252 assert(nrc
== NRC_NODE_ERR
);
258 n_sigman_cleanup_ping(&sm
);
266 if (nrc
== NRC_NODE_ERR
)
267 while (nhead
!= NULL
) {
269 nhead
= nhead
->nrc_next
;
275 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
278 static enum nrc_token
279 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
], bool_t
*nl_last
)
291 if (feof(fi
) || ferror(fi
))
294 for (seen_nl
= *nl_last
; (c
= getc(fi
)) != EOF
&& whitechar(c
);)
295 seen_nl
|= (c
== '\n');
299 /* fetchmail and derived parsers support comments */
300 if ((*nl_last
= seen_nl
) && c
== '#') {
301 while ((c
= getc(fi
)) != EOF
&& c
!= '\n')
309 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
310 if (c
== '"' || c
== '\'') {
313 /* Not requiring the closing QM is (Net)BSD syntax */
314 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
315 /* Backslash escaping the next character is (Net)BSD syntax */
317 if ((c
= getc(fi
)) == EOF
)
320 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
327 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
328 /* Backslash escaping the next character is (Net)BSD syntax */
329 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
332 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
337 *nl_last
= (c
== '\n');
342 do {/*rv = NRC_NONE*/} while (0);
343 else if (!strcmp(buffer
, "default"))
345 else if (!strcmp(buffer
, "login"))
347 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
349 else if (!strcmp(buffer
, "account"))
351 else if (!strcmp(buffer
, "macdef"))
353 else if (!strcmp(buffer
, "machine"))
358 if (c
== EOF
&& !feof(fi
))
365 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
367 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
371 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
372 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
374 if (_nrc_list
== NULL
)
376 if (_nrc_list
== NRC_NODE_ERR
)
379 nrc_wild
= nrc_exact
= NULL
;
380 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
381 switch (__nrc_host_match(nrc
, urlp
)) {
383 nrc
->nrc_result
= nrc_exact
;
387 nrc
->nrc_result
= nrc_wild
;
394 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
395 /* Must be an unambiguous entry of its kind */
396 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
398 if (__nrc_find_user(urlp
, nrc_exact
))
401 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
403 if (!__nrc_find_user(urlp
, nrc_wild
))
409 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
410 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
411 /* Do not try to find a password without exact user match unless we've
412 * been called during credential lookup, a.k.a. the second time */
414 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
415 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
423 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
430 /* Find a matching machine -- entries are all lowercase normalized */
431 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
432 if (LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
437 /* Cannot be an exact match, but maybe the .netrc machine starts with
438 * a "*." glob, which we recognize as an extension, meaning "skip
439 * a single subdomain, then match the rest" */
440 d1
= nrc
->nrc_dat
+ 2;
442 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
446 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
447 * in fact this even succeeds for ".host.com", but - why care, here? */
448 d2
= urlp
->url_host
.s
;
449 l2
= urlp
->url_host
.l
;
456 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
457 /* This matches, but we won't use it directly but watch out for an
458 * exact match first! */
466 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
470 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
471 if (nrc
->nrc_ulen
> 0) {
472 /* Fake it was part of URL otherwise XXX */
473 urlp
->url_had_user
= TRU1
;
474 /* That buffer will be duplicated by url_parse() in this case! */
475 urlp
->url_user
.s
= UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
476 urlp
->url_user
.l
= nrc
->nrc_ulen
;
481 return (nrc
!= NULL
);
485 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
489 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
490 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
491 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
497 } else if (!um
&& nrc
->nrc_ulen
> 0)
499 if (nrc
->nrc_plen
== 0)
502 /* We are responsible for duplicating this buffer! */
503 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
504 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
509 return (nrc
!= NULL
);
511 #endif /* HAVE_NETRC */
515 _agent_shell_lookup(struct url
*urlp
, char const *comm
) /* TODO v15-compat */
518 char const *env_addon
[8];
521 union {char const *cp
; int c
; sighandler_type sht
;} u
;
526 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
528 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
530 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
532 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
535 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", tempdir
, NULL
)->s
;
536 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", tempdir
, NULL
)->s
;
540 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
541 n_err(_("*agent-shell-lookup* startup failed (\"%s\")\n"), comm
);
545 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
546 if (u
.c
== '\n') /* xxx */
549 if (l
== sizeof(buf
) - 1) {
550 n_str_add_buf(&s
, buf
, l
);
555 n_str_add_buf(&s
, buf
, l
);
557 if (!Pclose(pbuf
, TRU1
)) {
558 n_err(_("*agent-shell-lookup* execution failure (\"%s\")\n"), comm
);
562 /* We are responsible for duplicating this buffer! */
564 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
566 urlp
->url_pass
.s
= UNCONST(""), urlp
->url_pass
.l
= 0;
574 #endif /* HAVE_AGENT */
577 (urlxenc
)(char const *cp
, bool_t ispath SALLOC_DEBUG_ARGS
)
582 np
= n
= (salloc
)(strlen(cp
) * 3 +1 SALLOC_DEBUG_ARGSCALL
);
584 for (; (c1
= *cp
) != '\0'; ++cp
) {
585 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
586 * ALPHA / DIGIT / "-" / "." / "_" / "~"
587 * However add a special is[file]path mode for file-system friendliness */
588 if (alnumchar(c1
) || c1
== '_')
591 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
594 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
599 n_c_to_hex_base16(np
+ 1, c1
);
609 (urlxdec
)(char const *cp SALLOC_DEBUG_ARGS
)
615 np
= n
= (salloc
)(strlen(cp
) +1 SALLOC_DEBUG_ARGSCALL
);
617 while ((c
= (uc_i
)*cp
++) != '\0') {
618 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
620 if (LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
633 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
640 { "submission", "587", 587},
641 { "smtps", "465", 465},
642 { "pop3", "110", 110},
643 { "pop3s", "995", 995},
644 { "imap", "143", 143},
645 { "imaps", "993", 993},
652 for(rv
= proto
; *rv
!= '\0'; ++rv
)
655 l
= PTR2SIZE(rv
- proto
);
657 for(rv
= NULL
, i
= 0; i
< NELEM(tbl
); ++i
)
658 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
660 if(irv_or_null
!= NULL
)
661 *irv_or_null
= tbl
[i
].portno
;
668 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
670 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
672 #if defined HAVE_SMTP && defined HAVE_POP3
675 #if defined HAVE_SMTP || defined HAVE_POP3
683 memset(urlp
, 0, sizeof *urlp
);
684 urlp
->url_input
= data
;
685 urlp
->url_cproto
= cproto
;
687 /* Network protocol */
688 #define _protox(X,Y) \
689 urlp->url_portno = Y;\
690 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
691 urlp->url_proto[sizeof(X) -1] = '\0';\
692 urlp->url_proto_len = sizeof(X) -1;\
693 urlp->url_proto_xlen = sizeof(X "://") -1
694 #define __if(X,Y,Z) \
695 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
697 data += sizeof(X "://") -1;\
698 do { Z; } while (0);\
701 #define _if(X,Y) __if(X, Y, (void)0)
703 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
705 # define _ifs(X,Y) goto jeproto;
712 _if ("submission", 587)
723 _protox("pop3", 110);
732 _protox("imap", 143);
743 if (strstr(data
, "://") != NULL
) {
744 #if !defined __ALLPROTO || !defined HAVE_SSL
747 n_err(_("URL \"proto://\" prefix invalid: \"%s\"\n"), urlp
->url_input
);
752 /* User and password, I */
754 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
755 size_t l
= PTR2SIZE(cp
- data
);
756 char const *d
= data
;
757 char *ub
= ac_alloc(l
+1);
759 urlp
->url_had_user
= TRU1
;
762 /* And also have a password? */
763 if ((cp
= memchr(d
, ':', l
)) != NULL
) {
764 size_t i
= PTR2SIZE(cp
- d
);
767 memcpy(ub
, cp
+ 1, l
);
769 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(ub
));
771 if (strcmp(ub
, urlxenc(urlp
->url_pass
.s
, FAL0
))) {
772 n_err(_("String is not properly URL percent encoded: \"%s\"\n"),
781 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(ub
));
782 urlp
->url_user_enc
.l
= strlen(
783 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
785 if (urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)) {
786 n_err(_("String is not properly URL percent encoded: \"%s\"\n"), ub
);
793 /* Servername and port -- and possible path suffix */
794 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
798 urlp
->url_port
= x
= savestr(x
= cp
+ 1);
799 if ((x
= strchr(x
, '/')) != NULL
)
801 l
= strtol(urlp
->url_port
, &eptr
, 10);
802 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
803 n_err(_("URL with invalid port number: \"%s\"\n"), urlp
->url_input
);
806 urlp
->url_portno
= (ui16_t
)l
;
808 if ((x
= strchr(data
, '/')) != NULL
)
809 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
810 cp
= UNCONST(data
+ strlen(data
));
813 /* A (non-empty) path may only occur with IMAP */
814 if (x
!= NULL
&& x
[1] != '\0') {
816 if (cproto
!= CPROTO_IMAP
) {
818 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
823 urlp
->url_path
.l
= strlen(++x
);
824 urlp
->url_path
.s
= savestrbuf(x
, urlp
->url_path
.l
);
828 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
830 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
831 *cp
= lowerconv(*cp
);
834 /* .url_h_p: HOST:PORT */
836 struct str
*s
= &urlp
->url_h_p
;
838 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
839 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
840 if (urlp
->url_port
!= NULL
) {
841 size_t j
= strlen(urlp
->url_port
);
843 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
851 * If there was no user in the URL, do we have *user-HOST* or *user*? */
852 if (!urlp
->url_had_user
) {
853 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
855 /* No, check wether .netrc lookup is desired */
857 if (!ok_blook(v15_compat
) ||
858 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
859 !_nrc_lookup(urlp
, FAL0
))
861 urlp
->url_user
.s
= UNCONST(myname
);
864 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
865 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
866 urlp
->url_user_enc
.l
= strlen(
867 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
870 /* And then there are a lot of prebuild string combinations TODO do lazy */
872 /* .url_u_h: .url_user@.url_host
873 * For SMTP we apply ridiculously complicated *v15-compat* plus
874 * *smtp-hostname* / *hostname* dependent rules */
878 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
879 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
882 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
887 i
= urlp
->url_user
.l
;
889 s
->s
= salloc(i
+ 1 + h
.l
+1);
891 memcpy(s
->s
, urlp
->url_user
.s
, i
);
894 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
899 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
900 { struct str
*s
= &urlp
->url_u_h_p
;
901 size_t i
= urlp
->url_user
.l
;
903 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
905 memcpy(s
->s
, urlp
->url_user
.s
, i
);
908 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
909 i
+= urlp
->url_h_p
.l
;
913 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
914 { struct str
*s
= &urlp
->url_eu_h_p
;
915 size_t i
= urlp
->url_user_enc
.l
;
917 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
919 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
922 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
923 i
+= urlp
->url_h_p
.l
;
927 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
929 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
931 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
932 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
933 urlp
->url_u_h_p
.l
+1);
934 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
936 urlp
->url_p_u_h_p
= ud
;
939 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
941 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
942 1 + urlp
->url_path
.l
+1);
944 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
945 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
946 urlp
->url_eu_h_p
.l
+1);
947 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
949 if (urlp
->url_path
.l
== 0)
950 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
952 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
953 urlp
->url_p_eu_h_p_p
= ud
;
956 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
961 #endif /* __ANYPROTO */
970 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
972 char const *pname
, *pxstr
, *authdef
;
973 size_t pxlen
, addrlen
, i
;
976 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
978 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
981 memset(ccp
, 0, sizeof *ccp
);
988 pxlen
= sizeof("smtp-auth") -1;
989 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
990 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
992 addr_is_nuser
= TRU1
;
997 pxlen
= sizeof("pop3-auth") -1;
998 authmask
= AUTHTYPE_PLAIN
;
1003 ccp
->cc_cproto
= cproto
;
1004 addrlen
= strlen(addr
);
1005 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1006 memcpy(vbuf
, pxstr
, pxlen
);
1008 /* Authentication type */
1010 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1011 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1013 if ((s
= vok_vlook(vbuf
)) == NULL
)
1014 s
= UNCONST(authdef
);
1017 if (!asccasecmp(s
, "none")) {
1018 ccp
->cc_auth
= "NONE";
1019 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1021 } else if (!asccasecmp(s
, "plain")) {
1022 ccp
->cc_auth
= "PLAIN";
1023 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1024 ware
= REQ_PASS
| REQ_USER
;
1025 } else if (!asccasecmp(s
, "login")) {
1026 ccp
->cc_auth
= "LOGIN";
1027 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1028 ware
= REQ_PASS
| REQ_USER
;
1029 } else if (!asccasecmp(s
, "cram-md5")) {
1030 ccp
->cc_auth
= "CRAM-MD5";
1031 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1032 ware
= REQ_PASS
| REQ_USER
;
1033 } else if (!asccasecmp(s
, "gssapi")) {
1034 ccp
->cc_auth
= "GSS-API";
1035 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1040 if (!(ccp
->cc_authtype
& authmask
)) {
1041 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1046 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1047 n_err(_("No CRAM-MD5 support compiled in\n"));
1052 # ifndef HAVE_GSSAPI
1053 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1054 n_err(_("No GSS-API support compiled in\n"));
1061 if (!(ware
& (WANT_USER
| REQ_USER
)))
1064 if (!addr_is_nuser
) {
1065 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1066 ccp
->cc_user
.s
= urlxdec(savestrbuf(addr
, PTR2SIZE(s
- addr
)));
1067 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1068 } else if (ware
& REQ_USER
)
1073 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1075 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1076 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1078 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1079 if ((s
= getuser(NULL
)) == NULL
) {
1080 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1081 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1082 * TODO check that first, then! change control flow, grow vbuf */
1083 n_err(_("A user is necessary for %s authentication\n"), pname
);
1089 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1093 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1096 if (!addr_is_nuser
) {
1097 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1099 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1102 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1103 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1105 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1106 (ware
& REQ_PASS
)) {
1107 if ((s
= getpassword(NULL
)) == NULL
) {
1108 n_err(_("A password is necessary for %s authentication\n"),
1116 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1120 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1121 n_err(_("Credentials: host \"%s\", user \"%s\", pass \"%s\"\n"),
1122 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1123 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1125 return (ccp
!= NULL
);
1129 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1131 char const *pstr
, *authdef
;
1133 enum okeys authokey
;
1135 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1139 memset(ccp
, 0, sizeof *ccp
);
1140 ccp
->cc_user
= urlp
->url_user
;
1142 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1146 authokey
= ok_v_smtp_auth
;
1147 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1148 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1153 authokey
= ok_v_pop3_auth
;
1154 authmask
= AUTHTYPE_PLAIN
;
1159 /* Authentication type */
1160 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1161 s
= UNCONST(authdef
);
1163 if (!asccasecmp(s
, "none")) {
1164 ccp
->cc_auth
= "NONE";
1165 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1167 } else if (!asccasecmp(s
, "plain")) {
1168 ccp
->cc_auth
= "PLAIN";
1169 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1170 ware
= REQ_PASS
| REQ_USER
;
1171 } else if (!asccasecmp(s
, "login")) {
1172 ccp
->cc_auth
= "LOGIN";
1173 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1174 ware
= REQ_PASS
| REQ_USER
;
1175 } else if (!asccasecmp(s
, "cram-md5")) {
1176 ccp
->cc_auth
= "CRAM-MD5";
1177 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1178 ware
= REQ_PASS
| REQ_USER
;
1179 } else if (!asccasecmp(s
, "gssapi")) {
1180 ccp
->cc_auth
= "GSS-API";
1181 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1186 if (!(ccp
->cc_authtype
& authmask
)) {
1187 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1192 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1193 n_err(_("No CRAM-MD5 support compiled in\n"));
1198 # ifndef HAVE_GSSAPI
1199 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1200 n_err(_("No GSS-API support compiled in\n"));
1207 ccp
->cc_pass
= urlp
->url_pass
;
1208 if (ccp
->cc_pass
.s
!= NULL
)
1211 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1213 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1214 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1215 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1216 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1217 if (!_agent_shell_lookup(urlp
, s
)) {
1220 } else if (urlp
->url_pass
.s
!= NULL
) {
1221 ccp
->cc_pass
= urlp
->url_pass
;
1227 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1228 ccp
->cc_pass
= urlp
->url_pass
;
1232 if (ware
& REQ_PASS
) {
1233 if ((s
= getpassword(NULL
)) != NULL
)
1235 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1237 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1243 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1244 n_err(_("Credentials: host \"%s\", user \"%s\", pass \"%s\"\n"),
1245 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1246 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1248 return (ccp
!= NULL
);
1250 #endif /* HAVE_SOCKETS */
1257 struct nrc_node
*nrc
;
1264 if (argv
[1] != NULL
)
1266 if (!asccasecmp(*argv
, "show"))
1269 if (!asccasecmp(*argv
, "load"))
1271 if (!asccasecmp(*argv
, "clear"))
1274 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1278 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1284 if (_nrc_list
== NULL
)
1286 if (_nrc_list
== NRC_NODE_ERR
) {
1287 n_err(_("Interpolate what file?\n"));
1294 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1295 n_perr(_("tmpfile"), 0);
1300 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1301 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
);
1302 if (nrc
->nrc_ulen
> 0)
1303 fprintf(fp
, _("login \"%s\" "),
1304 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1305 if (nrc
->nrc_plen
> 0)
1306 fprintf(fp
, _("password \"%s\"\n"),
1307 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1312 page_or_print(fp
, l
);
1318 if (_nrc_list
== NRC_NODE_ERR
)
1320 while ((nrc
= _nrc_list
) != NULL
) {
1321 _nrc_list
= nrc
->nrc_next
;
1326 #endif /* HAVE_NETRC */
1330 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1332 char const *cp
= vp
;
1336 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1338 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1339 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1340 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1348 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1352 char digest
[16], *cp
;
1356 in
.s
= UNCONST(b64
);
1357 in
.l
= strlen(in
.s
);
1358 b64_decode(&out
, &in
, NULL
);
1359 assert(out
.s
!= NULL
);
1361 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1363 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1365 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1366 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1367 memcpy(in
.s
, user
->s
, user
->l
);
1368 in
.s
[user
->l
] = ' ';
1369 memcpy(in
.s
+ user
->l
+ 1, cp
, MD5TOHEX_SIZE
);
1370 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1377 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1381 * This code is taken from
1383 * Network Working Group H. Krawczyk
1384 * Request for Comments: 2104 IBM
1385 * Category: Informational M. Bellare
1392 * HMAC: Keyed-Hashing for Message Authentication
1395 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1396 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1397 unsigned char tk
[16];
1401 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1406 md5_update(&tctx
, key
, key_len
);
1407 md5_final(tk
, &tctx
);
1413 /* the HMAC_MD5 transform looks like:
1415 * MD5(K XOR opad, MD5(K XOR ipad, text))
1417 * where K is an n byte key
1418 * ipad is the byte 0x36 repeated 64 times
1419 * opad is the byte 0x5c repeated 64 times
1420 * and text is the data being protected */
1422 /* start out by storing key in pads */
1423 memset(k_ipad
, 0, sizeof k_ipad
);
1424 memset(k_opad
, 0, sizeof k_opad
);
1425 memcpy(k_ipad
, key
, key_len
);
1426 memcpy(k_opad
, key
, key_len
);
1428 /* XOR key with ipad and opad values */
1429 for (i
=0; i
<64; i
++) {
1434 /* perform inner MD5 */
1435 md5_init(&context
); /* init context for 1st pass */
1436 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1437 md5_update(&context
, text
, text_len
); /* then text of datagram */
1438 md5_final(digest
, &context
); /* finish up 1st pass */
1440 /* perform outer MD5 */
1441 md5_init(&context
); /* init context for 2nd pass */
1442 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1443 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1444 md5_final(digest
, &context
); /* finish up 2nd pass */
1447 #endif /* HAVE_MD5 */