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.
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
[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
);
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
!= '@')
104 return n_UNCONST(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
;
122 n_UNINIT(ntail
, NULL
);
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
= fexpand(ok_vlook(NETRC
), FEXP_LOCAL
| FEXP_NOPROTO
)
147 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
148 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load
, FAL0
));
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
));
164 switch((t
= __nrc_token(fi
, buffer
, &nl_last
))) {
167 default: /* Doesn't happen (but on error?), keep CC happy */
170 /* We ignore the default entry (require an exact host match), and we also
171 * ignore anything after such an entry (faulty syntax) */
176 /* Normalize HOST to lowercase */
178 if (!seen_default
&& (t
= __nrc_token(fi
, host
, &nl_last
)) != NRC_INPUT
)
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
) {
191 if ((t
= __nrc_token(fi
, user
, &nl_last
)) != NRC_INPUT
)
195 if ((t
= __nrc_token(fi
, pass
, &nl_last
)) != NRC_INPUT
)
199 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
203 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
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 */
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(n_VSTRUCT_SIZEOF(struct nrc_node
,
227 nrc_dat
) + hl
+1 + ul
+1 + pl
+1);
230 ntail
->nrc_next
= nx
;
238 memcpy(nx
->nrc_dat
, host
, ++hl
);
239 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
240 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
242 if (t
== NRC_MACHINE
)
244 if (t
== NRC_DEFAULT
)
251 if (options
& OPT_D_V
)
252 n_err(_("Errors occurred while parsing %s\n"),
253 n_shexp_quote_cp(netrc_load
, FAL0
));
254 assert(nrc
== NRC_NODE_ERR
);
260 n_sigman_cleanup_ping(&sm
);
268 if (nrc
== NRC_NODE_ERR
)
269 while (nhead
!= NULL
) {
271 nhead
= nhead
->nrc_next
;
277 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
280 static enum nrc_token
281 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
], bool_t
*nl_last
)
293 if (feof(fi
) || ferror(fi
))
296 for (seen_nl
= *nl_last
; (c
= getc(fi
)) != EOF
&& whitechar(c
);)
297 seen_nl
|= (c
== '\n');
301 /* fetchmail and derived parsers support comments */
302 if ((*nl_last
= seen_nl
) && c
== '#') {
303 while ((c
= getc(fi
)) != EOF
&& c
!= '\n')
311 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
312 if (c
== '"' || c
== '\'') {
315 /* Not requiring the closing QM is (Net)BSD syntax */
316 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
317 /* Backslash escaping the next character is (Net)BSD syntax */
319 if ((c
= getc(fi
)) == EOF
)
322 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
329 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
330 /* Backslash escaping the next character is (Net)BSD syntax */
331 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
334 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
339 *nl_last
= (c
== '\n');
344 do {/*rv = NRC_NONE*/} while (0);
345 else if (!strcmp(buffer
, "default"))
347 else if (!strcmp(buffer
, "login"))
349 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
351 else if (!strcmp(buffer
, "account"))
353 else if (!strcmp(buffer
, "macdef"))
355 else if (!strcmp(buffer
, "machine"))
360 if (c
== EOF
&& !feof(fi
))
367 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
369 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
373 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
374 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
376 if (_nrc_list
== NULL
)
378 if (_nrc_list
== NRC_NODE_ERR
)
381 nrc_wild
= nrc_exact
= NULL
;
382 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
383 switch (__nrc_host_match(nrc
, urlp
)) {
385 nrc
->nrc_result
= nrc_exact
;
389 nrc
->nrc_result
= nrc_wild
;
396 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
397 /* Must be an unambiguous entry of its kind */
398 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
400 if (__nrc_find_user(urlp
, nrc_exact
))
403 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
405 if (!__nrc_find_user(urlp
, nrc_wild
))
411 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
412 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
413 /* Do not try to find a password without exact user match unless we've
414 * been called during credential lookup, a.k.a. the second time */
416 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
417 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
425 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
432 /* Find a matching machine -- entries are all lowercase normalized */
433 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
434 if (n_LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
439 /* Cannot be an exact match, but maybe the .netrc machine starts with
440 * a "*." glob, which we recognize as an extension, meaning "skip
441 * a single subdomain, then match the rest" */
442 d1
= nrc
->nrc_dat
+ 2;
444 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
448 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
449 * in fact this even succeeds for ".host.com", but - why care, here? */
450 d2
= urlp
->url_host
.s
;
451 l2
= urlp
->url_host
.l
;
458 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
459 /* This matches, but we won't use it directly but watch out for an
460 * exact match first! */
468 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
472 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
473 if (nrc
->nrc_ulen
> 0) {
474 /* Fake it was part of URL otherwise XXX */
475 urlp
->url_had_user
= TRU1
;
476 /* That buffer will be duplicated by url_parse() in this case! */
477 urlp
->url_user
.s
= n_UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
478 urlp
->url_user
.l
= nrc
->nrc_ulen
;
483 return (nrc
!= NULL
);
487 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
491 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
492 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
493 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
499 } else if (!um
&& nrc
->nrc_ulen
> 0)
501 if (nrc
->nrc_plen
== 0)
504 /* We are responsible for duplicating this buffer! */
505 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
506 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
511 return (nrc
!= NULL
);
513 #endif /* HAVE_NETRC */
517 _agent_shell_lookup(struct url
*urlp
, char const *comm
) /* TODO v15-compat */
520 char const *env_addon
[8];
523 union {int c
; sighandler_type sht
;} u
;
528 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
530 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
532 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
534 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
536 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, /* TODO v15 */
537 "=", ok_vlook(TMPDIR
), NULL
)->s
;
541 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
542 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm
);
546 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
547 if (u
.c
== '\n') /* xxx */
550 if (l
== sizeof(buf
) - 1) {
551 n_str_add_buf(&s
, buf
, l
);
556 n_str_add_buf(&s
, buf
, l
);
558 if (!Pclose(pbuf
, TRU1
)) {
559 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm
);
563 /* We are responsible for duplicating this buffer! */
565 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
567 urlp
->url_pass
.s
= n_UNCONST(n_empty
), urlp
->url_pass
.l
= 0;
575 #endif /* HAVE_AGENT */
578 (urlxenc
)(char const *cp
, bool_t ispath n_MEMORY_DEBUG_ARGS
)
587 if(i
>= UIZ_MAX
/ 3){
593 np
= n
= (n_autorec_alloc
)(NULL
, i n_MEMORY_DEBUG_ARGSCALL
);
596 for (; (c1
= *cp
) != '\0'; ++cp
) {
597 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
598 * ALPHA / DIGIT / "-" / "." / "_" / "~"
599 * However add a special is[file]path mode for file-system friendliness */
600 if (alnumchar(c1
) || c1
== '_')
603 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
606 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
611 n_c_to_hex_base16(np
+ 1, c1
);
622 (urlxdec
)(char const *cp n_MEMORY_DEBUG_ARGS
)
628 np
= n
= (n_autorec_alloc
)(NULL
, strlen(cp
) +1 n_MEMORY_DEBUG_ARGSCALL
);
630 while ((c
= (uc_i
)*cp
++) != '\0') {
631 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
633 if (n_LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
648 char const **argv
, *cp
, *res
;
651 if(*(cp
= *(argv
= v
)) == 'p'){
652 if(!ascncasecmp(++cp
, "ath", 3))
657 if(is_prefix(cp
, "encode")){
658 while((cp
= *++argv
) != NULL
){
659 if((res
= urlxenc(cp
, ispath
)) == NULL
)
661 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
662 cp
, strlen(cp
), res
, strlen(res
));
664 }else if(is_prefix(cp
, "decode")){
667 while((cp
= *++argv
) != NULL
){
668 if((res
= urlxdec(cp
)) == NULL
)
670 in
.l
= strlen(in
.s
= n_UNCONST(res
)); /* logical */
671 makeprint(&in
, &out
);
672 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
673 cp
, strlen(cp
), out
.s
, in
.l
);
677 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv
);
681 return (cp
!= NULL
? OKAY
: STOP
);
685 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
688 char const *mailtop_orig
;
691 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
695 mailtop
+= sizeof("mailto:") -1;
697 /* TODO This is all intermediate, and for now just enough to understand
698 * TODO a little bit of a little more advanced List-Post: headers. */
699 /* Strip any hfield additions, keep only to addr-spec's */
700 if((rv
= strchr(mailtop
, '?')) != NULL
)
701 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
703 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
707 /* Simply perform percent-decoding if there is a percent % */
708 if(memchr(rv
, '%', i
) != NULL
){
712 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
715 if((c
= *mailtop
++) == '%'){
718 if(i
< 3 || (cc
= n_c_from_hex_base16(mailtop
)) < 0){
719 if(!err
&& (err
= TRU1
, options
& OPT_D_V
))
720 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
721 n_shexp_quote_cp(mailtop_orig
, FAL0
));
742 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
749 { "submission", "587", 587},
750 { "smtps", "465", 465},
751 { "pop3", "110", 110},
752 { "pop3s", "995", 995},
753 { "imap", "143", 143},
754 { "imaps", "993", 993},
761 for(rv
= proto
; *rv
!= '\0'; ++rv
)
764 l
= PTR2SIZE(rv
- proto
);
766 for(rv
= NULL
, i
= 0; i
< n_NELEM(tbl
); ++i
)
767 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
769 if(irv_or_null
!= NULL
)
770 *irv_or_null
= tbl
[i
].portno
;
777 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
779 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
781 #if defined HAVE_SMTP && defined HAVE_POP3
784 #if defined HAVE_SMTP || defined HAVE_POP3
792 memset(urlp
, 0, sizeof *urlp
);
793 urlp
->url_input
= data
;
794 urlp
->url_cproto
= cproto
;
796 /* Network protocol */
797 #define _protox(X,Y) \
798 urlp->url_portno = Y;\
799 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
800 urlp->url_proto[sizeof(X) -1] = '\0';\
801 urlp->url_proto_len = sizeof(X) -1;\
802 urlp->url_proto_xlen = sizeof(X "://") -1
803 #define __if(X,Y,Z) \
804 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
806 data += sizeof(X "://") -1;\
807 do { Z; } while (0);\
810 #define _if(X,Y) __if(X, Y, (void)0)
812 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
814 # define _ifs(X,Y) goto jeproto;
821 _if ("submission", 587)
832 _protox("pop3", 110);
841 _protox("imap", 143);
852 if (strstr(data
, "://") != NULL
) {
853 #if !defined __ALLPROTO || !defined HAVE_SSL
856 n_err(_("URL proto:// prefix invalid: %s\n"), urlp
->url_input
);
861 /* User and password, I */
863 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
865 char const *urlpe
, *d
;
868 l
= PTR2SIZE(cp
- data
);
871 urlp
->url_had_user
= TRU1
;
874 /* And also have a password? */
875 if((cp
= memchr(d
, ':', l
)) != NULL
){
876 size_t i
= PTR2SIZE(cp
- d
);
879 memcpy(ub
, cp
+ 1, l
);
882 if((urlp
->url_pass
.s
= urlxdec(ub
)) == NULL
)
884 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
);
885 if((urlpe
= urlxenc(urlp
->url_pass
.s
, FAL0
)) == NULL
)
887 if(strcmp(ub
, urlpe
))
894 if((urlp
->url_user
.s
= urlxdec(ub
)) == NULL
)
896 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
897 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
)
899 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
901 if(urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)){
903 n_err(_("String is not properly URL percent encoded: %s\n"), ub
);
912 /* Servername and port -- and possible path suffix */
913 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
917 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
918 if ((x
= strchr(x
, '/')) != NULL
) {
923 l
= strtol(urlp
->url_port
, &eptr
, 10);
924 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
925 n_err(_("URL with invalid port number: %s\n"), urlp
->url_input
);
928 urlp
->url_portno
= (ui16_t
)l
;
930 if ((x
= strchr(data
, '/')) != NULL
) {
931 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
935 cp
= n_UNCONST(data
+ strlen(data
));
938 /* A (non-empty) path may only occur with IMAP */
939 if (x
!= NULL
&& *x
!= '\0') {
940 /* Take care not to count adjacent slashes for real, on either end */
944 for(x2
= savestrbuf(x
, i
= strlen(x
)); i
> 0; --i
)
951 if (cproto
!= CPROTO_IMAP
) {
953 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
958 urlp
->url_path
.l
= i
;
959 urlp
->url_path
.s
= x2
;
964 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
966 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
967 *cp
= lowerconv(*cp
);
970 /* .url_h_p: HOST:PORT */
972 struct str
*s
= &urlp
->url_h_p
;
974 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
975 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
976 if (urlp
->url_port
!= NULL
) {
977 size_t j
= strlen(urlp
->url_port
);
979 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
987 * If there was no user in the URL, do we have *user-HOST* or *user*? */
988 if (!urlp
->url_had_user
) {
989 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
991 /* No, check whether .netrc lookup is desired */
993 if (!ok_blook(v15_compat
) ||
994 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
995 !_nrc_lookup(urlp
, FAL0
))
997 urlp
->url_user
.s
= n_UNCONST(myname
);
1000 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
1001 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
1002 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
){
1003 n_err(_("Cannot URL encode %s\n"), urlp
->url_user
.s
);
1006 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
1009 /* And then there are a lot of prebuild string combinations TODO do lazy */
1011 /* .url_u_h: .url_user@.url_host
1012 * For SMTP we apply ridiculously complicated *v15-compat* plus
1013 * *smtp-hostname* / *hostname* dependent rules */
1017 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
1018 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
1021 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
1026 i
= urlp
->url_user
.l
;
1028 s
->s
= salloc(i
+ 1 + h
.l
+1);
1030 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1033 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
1038 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1039 { struct str
*s
= &urlp
->url_u_h_p
;
1040 size_t i
= urlp
->url_user
.l
;
1042 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1044 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1047 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1048 i
+= urlp
->url_h_p
.l
;
1052 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1053 { struct str
*s
= &urlp
->url_eu_h_p
;
1054 size_t i
= urlp
->url_user_enc
.l
;
1056 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1058 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1061 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1062 i
+= urlp
->url_h_p
.l
;
1066 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1068 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
1070 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1071 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
1072 urlp
->url_u_h_p
.l
+1);
1073 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1075 urlp
->url_p_u_h_p
= ud
;
1078 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1080 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1081 1 + urlp
->url_path
.l
+1);
1083 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1084 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1085 urlp
->url_eu_h_p
.l
+1);
1086 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1088 if (urlp
->url_path
.l
== 0)
1089 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1091 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1092 urlp
->url_p_eu_h_p_p
= ud
;
1095 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1100 #endif /* __ANYPROTO */
1109 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1111 char const *pname
, *pxstr
, *authdef
;
1112 size_t pxlen
, addrlen
, i
;
1115 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1117 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1120 memset(ccp
, 0, sizeof *ccp
);
1126 pxstr
= "smtp-auth";
1127 pxlen
= sizeof("smtp-auth") -1;
1128 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1129 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1131 addr_is_nuser
= TRU1
;
1135 pxstr
= "pop3-auth";
1136 pxlen
= sizeof("pop3-auth") -1;
1137 authmask
= AUTHTYPE_PLAIN
;
1142 ccp
->cc_cproto
= cproto
;
1143 addrlen
= strlen(addr
);
1144 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1145 memcpy(vbuf
, pxstr
, pxlen
);
1147 /* Authentication type */
1149 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1150 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1152 if ((s
= vok_vlook(vbuf
)) == NULL
)
1153 s
= n_UNCONST(authdef
);
1156 if (!asccasecmp(s
, "none")) {
1157 ccp
->cc_auth
= "NONE";
1158 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1160 } else if (!asccasecmp(s
, "plain")) {
1161 ccp
->cc_auth
= "PLAIN";
1162 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1163 ware
= REQ_PASS
| REQ_USER
;
1164 } else if (!asccasecmp(s
, "login")) {
1165 ccp
->cc_auth
= "LOGIN";
1166 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1167 ware
= REQ_PASS
| REQ_USER
;
1168 } else if (!asccasecmp(s
, "cram-md5")) {
1169 ccp
->cc_auth
= "CRAM-MD5";
1170 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1171 ware
= REQ_PASS
| REQ_USER
;
1172 } else if (!asccasecmp(s
, "gssapi")) {
1173 ccp
->cc_auth
= "GSS-API";
1174 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1179 if (!(ccp
->cc_authtype
& authmask
)) {
1180 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1185 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1186 n_err(_("No CRAM-MD5 support compiled in\n"));
1191 # ifndef HAVE_GSSAPI
1192 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1193 n_err(_("No GSS-API support compiled in\n"));
1200 if (!(ware
& (WANT_USER
| REQ_USER
)))
1203 if (!addr_is_nuser
) {
1204 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1207 cp
= savestrbuf(addr
, PTR2SIZE(s
- addr
));
1209 if((ccp
->cc_user
.s
= urlxdec(cp
)) == NULL
){
1210 n_err(_("String is not properly URL percent encoded: %s\n"), cp
);
1214 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1215 } else if (ware
& REQ_USER
)
1220 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1222 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1223 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1225 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1226 if ((s
= getuser(NULL
)) == NULL
) {
1227 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1228 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1229 * TODO check that first, then! change control flow, grow vbuf */
1230 n_err(_("A user is necessary for %s authentication\n"), pname
);
1236 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1240 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1243 if (!addr_is_nuser
) {
1244 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1246 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1249 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1250 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1252 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1253 (ware
& REQ_PASS
)) {
1254 if ((s
= getpassword(NULL
)) == NULL
) {
1255 n_err(_("A password is necessary for %s authentication\n"),
1263 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1267 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1268 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1269 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1270 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1272 return (ccp
!= NULL
);
1276 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1278 char const *pstr
, *authdef
;
1280 enum okeys authokey
;
1282 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1286 memset(ccp
, 0, sizeof *ccp
);
1287 ccp
->cc_user
= urlp
->url_user
;
1289 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1293 authokey
= ok_v_smtp_auth
;
1294 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1295 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1300 authokey
= ok_v_pop3_auth
;
1301 authmask
= AUTHTYPE_PLAIN
;
1306 /* Authentication type */
1307 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1308 s
= n_UNCONST(authdef
);
1310 if (!asccasecmp(s
, "none")) {
1311 ccp
->cc_auth
= "NONE";
1312 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1314 } else if (!asccasecmp(s
, "plain")) {
1315 ccp
->cc_auth
= "PLAIN";
1316 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1317 ware
= REQ_PASS
| REQ_USER
;
1318 } else if (!asccasecmp(s
, "login")) {
1319 ccp
->cc_auth
= "LOGIN";
1320 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1321 ware
= REQ_PASS
| REQ_USER
;
1322 } else if (!asccasecmp(s
, "cram-md5")) {
1323 ccp
->cc_auth
= "CRAM-MD5";
1324 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1325 ware
= REQ_PASS
| REQ_USER
;
1326 } else if (!asccasecmp(s
, "gssapi")) {
1327 ccp
->cc_auth
= "GSS-API";
1328 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1333 if (!(ccp
->cc_authtype
& authmask
)) {
1334 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1339 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1340 n_err(_("No CRAM-MD5 support compiled in\n"));
1345 # ifndef HAVE_GSSAPI
1346 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1347 n_err(_("No GSS-API support compiled in\n"));
1354 ccp
->cc_pass
= urlp
->url_pass
;
1355 if (ccp
->cc_pass
.s
!= NULL
)
1358 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1360 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1361 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1362 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1363 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1364 if (!_agent_shell_lookup(urlp
, s
)) {
1367 } else if (urlp
->url_pass
.s
!= NULL
) {
1368 ccp
->cc_pass
= urlp
->url_pass
;
1374 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1375 ccp
->cc_pass
= urlp
->url_pass
;
1379 if (ware
& REQ_PASS
) {
1380 if ((s
= getpassword(NULL
)) != NULL
)
1382 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1384 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1390 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1391 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1392 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1393 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1395 return (ccp
!= NULL
);
1397 #endif /* HAVE_SOCKETS */
1404 struct nrc_node
*nrc
;
1411 if (argv
[1] != NULL
)
1413 if (!asccasecmp(*argv
, "show"))
1416 if (!asccasecmp(*argv
, "load"))
1418 if (!asccasecmp(*argv
, "clear"))
1421 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1425 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1431 if (_nrc_list
== NULL
)
1433 if (_nrc_list
== NRC_NODE_ERR
) {
1434 n_err(_("Interpolate what file?\n"));
1441 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1442 n_perr(_("tmpfile"), 0);
1447 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1448 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
); /* XXX quote? */
1449 if (nrc
->nrc_ulen
> 0)
1450 fprintf(fp
, _("login \"%s\" "),
1451 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1452 if (nrc
->nrc_plen
> 0)
1453 fprintf(fp
, _("password \"%s\"\n"),
1454 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1459 page_or_print(fp
, l
);
1465 if (_nrc_list
== NRC_NODE_ERR
)
1467 while ((nrc
= _nrc_list
) != NULL
) {
1468 _nrc_list
= nrc
->nrc_next
;
1473 #endif /* HAVE_NETRC */
1477 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1479 char const *cp
= vp
;
1483 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1485 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1486 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1487 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1495 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1499 char digest
[16], *cp
;
1503 if(user
->l
>= UIZ_MAX
- 1 - MD5TOHEX_SIZE
- 1)
1505 if(pass
->l
>= INT_MAX
)
1508 in
.s
= n_UNCONST(b64
);
1509 in
.l
= strlen(in
.s
);
1510 if(!b64_decode(&out
, &in
))
1512 if(out
.l
>= INT_MAX
){
1518 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1520 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1522 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1523 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1524 memcpy(in
.s
, user
->s
, user
->l
);
1525 in
.s
[user
->l
] = ' ';
1526 memcpy(&in
.s
[user
->l
+ 1], cp
, MD5TOHEX_SIZE
);
1527 if(b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
) == NULL
)
1536 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1540 * This code is taken from
1542 * Network Working Group H. Krawczyk
1543 * Request for Comments: 2104 IBM
1544 * Category: Informational M. Bellare
1551 * HMAC: Keyed-Hashing for Message Authentication
1554 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1555 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1556 unsigned char tk
[16];
1560 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1565 md5_update(&tctx
, key
, key_len
);
1566 md5_final(tk
, &tctx
);
1572 /* the HMAC_MD5 transform looks like:
1574 * MD5(K XOR opad, MD5(K XOR ipad, text))
1576 * where K is an n byte key
1577 * ipad is the byte 0x36 repeated 64 times
1578 * opad is the byte 0x5c repeated 64 times
1579 * and text is the data being protected */
1581 /* start out by storing key in pads */
1582 memset(k_ipad
, 0, sizeof k_ipad
);
1583 memset(k_opad
, 0, sizeof k_opad
);
1584 memcpy(k_ipad
, key
, key_len
);
1585 memcpy(k_opad
, key
, key_len
);
1587 /* XOR key with ipad and opad values */
1588 for (i
=0; i
<64; i
++) {
1593 /* perform inner MD5 */
1594 md5_init(&context
); /* init context for 1st pass */
1595 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1596 md5_update(&context
, text
, text_len
); /* then text of datagram */
1597 md5_final(digest
, &context
); /* finish up 1st pass */
1599 /* perform outer MD5 */
1600 md5_init(&context
); /* init context for 2nd pass */
1601 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1602 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1603 md5_final(digest
, &context
); /* finish up 2nd pass */
1606 #endif /* HAVE_MD5 */