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(sizeof(*nx
) -
227 n_VFIELD_SIZEOF(struct nrc_node
, nrc_dat
) +
228 hl
+1 + ul
+1 + pl
+1);
231 ntail
->nrc_next
= nx
;
239 memcpy(nx
->nrc_dat
, host
, ++hl
);
240 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
241 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
243 if (t
== NRC_MACHINE
)
245 if (t
== NRC_DEFAULT
)
252 if (options
& OPT_D_V
)
253 n_err(_("Errors occurred while parsing %s\n"),
254 n_shexp_quote_cp(netrc_load
, FAL0
));
255 assert(nrc
== NRC_NODE_ERR
);
261 n_sigman_cleanup_ping(&sm
);
269 if (nrc
== NRC_NODE_ERR
)
270 while (nhead
!= NULL
) {
272 nhead
= nhead
->nrc_next
;
278 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
281 static enum nrc_token
282 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
], bool_t
*nl_last
)
294 if (feof(fi
) || ferror(fi
))
297 for (seen_nl
= *nl_last
; (c
= getc(fi
)) != EOF
&& whitechar(c
);)
298 seen_nl
|= (c
== '\n');
302 /* fetchmail and derived parsers support comments */
303 if ((*nl_last
= seen_nl
) && c
== '#') {
304 while ((c
= getc(fi
)) != EOF
&& c
!= '\n')
312 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
313 if (c
== '"' || c
== '\'') {
316 /* Not requiring the closing QM is (Net)BSD syntax */
317 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
318 /* Backslash escaping the next character is (Net)BSD syntax */
320 if ((c
= getc(fi
)) == EOF
)
323 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
330 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
331 /* Backslash escaping the next character is (Net)BSD syntax */
332 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
335 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
340 *nl_last
= (c
== '\n');
345 do {/*rv = NRC_NONE*/} while (0);
346 else if (!strcmp(buffer
, "default"))
348 else if (!strcmp(buffer
, "login"))
350 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
352 else if (!strcmp(buffer
, "account"))
354 else if (!strcmp(buffer
, "macdef"))
356 else if (!strcmp(buffer
, "machine"))
361 if (c
== EOF
&& !feof(fi
))
368 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
370 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
374 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
375 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
377 if (_nrc_list
== NULL
)
379 if (_nrc_list
== NRC_NODE_ERR
)
382 nrc_wild
= nrc_exact
= NULL
;
383 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
384 switch (__nrc_host_match(nrc
, urlp
)) {
386 nrc
->nrc_result
= nrc_exact
;
390 nrc
->nrc_result
= nrc_wild
;
397 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
398 /* Must be an unambiguous entry of its kind */
399 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
401 if (__nrc_find_user(urlp
, nrc_exact
))
404 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
406 if (!__nrc_find_user(urlp
, nrc_wild
))
412 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
413 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
414 /* Do not try to find a password without exact user match unless we've
415 * been called during credential lookup, a.k.a. the second time */
417 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
418 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
426 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
433 /* Find a matching machine -- entries are all lowercase normalized */
434 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
435 if (n_LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
440 /* Cannot be an exact match, but maybe the .netrc machine starts with
441 * a "*." glob, which we recognize as an extension, meaning "skip
442 * a single subdomain, then match the rest" */
443 d1
= nrc
->nrc_dat
+ 2;
445 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
449 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
450 * in fact this even succeeds for ".host.com", but - why care, here? */
451 d2
= urlp
->url_host
.s
;
452 l2
= urlp
->url_host
.l
;
459 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
460 /* This matches, but we won't use it directly but watch out for an
461 * exact match first! */
469 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
473 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
474 if (nrc
->nrc_ulen
> 0) {
475 /* Fake it was part of URL otherwise XXX */
476 urlp
->url_had_user
= TRU1
;
477 /* That buffer will be duplicated by url_parse() in this case! */
478 urlp
->url_user
.s
= n_UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
479 urlp
->url_user
.l
= nrc
->nrc_ulen
;
484 return (nrc
!= NULL
);
488 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
492 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
493 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
494 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
500 } else if (!um
&& nrc
->nrc_ulen
> 0)
502 if (nrc
->nrc_plen
== 0)
505 /* We are responsible for duplicating this buffer! */
506 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
507 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
512 return (nrc
!= NULL
);
514 #endif /* HAVE_NETRC */
518 _agent_shell_lookup(struct url
*urlp
, char const *comm
) /* TODO v15-compat */
521 char const *env_addon
[8];
524 union {int c
; sighandler_type sht
;} u
;
529 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
531 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
533 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
535 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
537 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, /* TODO v15 */
538 "=", ok_vlook(TMPDIR
), NULL
)->s
;
542 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
543 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm
);
547 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
548 if (u
.c
== '\n') /* xxx */
551 if (l
== sizeof(buf
) - 1) {
552 n_str_add_buf(&s
, buf
, l
);
557 n_str_add_buf(&s
, buf
, l
);
559 if (!Pclose(pbuf
, TRU1
)) {
560 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm
);
564 /* We are responsible for duplicating this buffer! */
566 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
568 urlp
->url_pass
.s
= n_UNCONST(n_empty
), urlp
->url_pass
.l
= 0;
576 #endif /* HAVE_AGENT */
579 (urlxenc
)(char const *cp
, bool_t ispath n_MEMORY_DEBUG_ARGS
)
588 if(i
>= UIZ_MAX
/ 3){
594 np
= n
= (n_autorec_alloc
)(NULL
, i n_MEMORY_DEBUG_ARGSCALL
);
597 for (; (c1
= *cp
) != '\0'; ++cp
) {
598 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
599 * ALPHA / DIGIT / "-" / "." / "_" / "~"
600 * However add a special is[file]path mode for file-system friendliness */
601 if (alnumchar(c1
) || c1
== '_')
604 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
607 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
612 n_c_to_hex_base16(np
+ 1, c1
);
623 (urlxdec
)(char const *cp n_MEMORY_DEBUG_ARGS
)
629 np
= n
= (n_autorec_alloc
)(NULL
, strlen(cp
) +1 n_MEMORY_DEBUG_ARGSCALL
);
631 while ((c
= (uc_i
)*cp
++) != '\0') {
632 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
634 if (n_LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
649 char const **argv
, *cp
, *res
;
652 if(*(cp
= *(argv
= v
)) == 'p'){
653 if(!ascncasecmp(++cp
, "ath", 3))
658 if(is_prefix(cp
, "encode")){
659 while((cp
= *++argv
) != NULL
){
660 if((res
= urlxenc(cp
, ispath
)) == NULL
)
662 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
663 cp
, strlen(cp
), res
, strlen(res
));
665 }else if(is_prefix(cp
, "decode")){
668 while((cp
= *++argv
) != NULL
){
669 if((res
= urlxdec(cp
)) == NULL
)
671 in
.l
= strlen(in
.s
= n_UNCONST(res
)); /* logical */
672 makeprint(&in
, &out
);
673 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
674 cp
, strlen(cp
), out
.s
, in
.l
);
678 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv
);
682 return (cp
!= NULL
? OKAY
: STOP
);
686 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
689 char const *mailtop_orig
;
692 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
696 mailtop
+= sizeof("mailto:") -1;
698 /* TODO This is all intermediate, and for now just enough to understand
699 * TODO a little bit of a little more advanced List-Post: headers. */
700 /* Strip any hfield additions, keep only to addr-spec's */
701 if((rv
= strchr(mailtop
, '?')) != NULL
)
702 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
704 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
708 /* Simply perform percent-decoding if there is a percent % */
709 if(memchr(rv
, '%', i
) != NULL
){
713 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
716 if((c
= *mailtop
++) == '%'){
719 if(i
< 3 || (cc
= n_c_from_hex_base16(mailtop
)) < 0){
720 if(!err
&& (err
= TRU1
, options
& OPT_D_V
))
721 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
722 n_shexp_quote_cp(mailtop_orig
, FAL0
));
743 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
750 { "submission", "587", 587},
751 { "smtps", "465", 465},
752 { "pop3", "110", 110},
753 { "pop3s", "995", 995},
754 { "imap", "143", 143},
755 { "imaps", "993", 993},
762 for(rv
= proto
; *rv
!= '\0'; ++rv
)
765 l
= PTR2SIZE(rv
- proto
);
767 for(rv
= NULL
, i
= 0; i
< n_NELEM(tbl
); ++i
)
768 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
770 if(irv_or_null
!= NULL
)
771 *irv_or_null
= tbl
[i
].portno
;
778 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
780 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
782 #if defined HAVE_SMTP && defined HAVE_POP3
785 #if defined HAVE_SMTP || defined HAVE_POP3
793 memset(urlp
, 0, sizeof *urlp
);
794 urlp
->url_input
= data
;
795 urlp
->url_cproto
= cproto
;
797 /* Network protocol */
798 #define _protox(X,Y) \
799 urlp->url_portno = Y;\
800 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
801 urlp->url_proto[sizeof(X) -1] = '\0';\
802 urlp->url_proto_len = sizeof(X) -1;\
803 urlp->url_proto_xlen = sizeof(X "://") -1
804 #define __if(X,Y,Z) \
805 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
807 data += sizeof(X "://") -1;\
808 do { Z; } while (0);\
811 #define _if(X,Y) __if(X, Y, (void)0)
813 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
815 # define _ifs(X,Y) goto jeproto;
822 _if ("submission", 587)
833 _protox("pop3", 110);
842 _protox("imap", 143);
853 if (strstr(data
, "://") != NULL
) {
854 #if !defined __ALLPROTO || !defined HAVE_SSL
857 n_err(_("URL proto:// prefix invalid: %s\n"), urlp
->url_input
);
862 /* User and password, I */
864 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
866 char const *urlpe
, *d
;
869 l
= PTR2SIZE(cp
- data
);
872 urlp
->url_had_user
= TRU1
;
875 /* And also have a password? */
876 if((cp
= memchr(d
, ':', l
)) != NULL
){
877 size_t i
= PTR2SIZE(cp
- d
);
880 memcpy(ub
, cp
+ 1, l
);
883 if((urlp
->url_pass
.s
= urlxdec(ub
)) == NULL
)
885 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
);
886 if((urlpe
= urlxenc(urlp
->url_pass
.s
, FAL0
)) == NULL
)
888 if(strcmp(ub
, urlpe
))
895 if((urlp
->url_user
.s
= urlxdec(ub
)) == NULL
)
897 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
898 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
)
900 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
902 if(urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)){
904 n_err(_("String is not properly URL percent encoded: %s\n"), ub
);
913 /* Servername and port -- and possible path suffix */
914 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
918 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
919 if ((x
= strchr(x
, '/')) != NULL
) {
924 l
= strtol(urlp
->url_port
, &eptr
, 10);
925 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
926 n_err(_("URL with invalid port number: %s\n"), urlp
->url_input
);
929 urlp
->url_portno
= (ui16_t
)l
;
931 if ((x
= strchr(data
, '/')) != NULL
) {
932 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
936 cp
= n_UNCONST(data
+ strlen(data
));
939 /* A (non-empty) path may only occur with IMAP */
940 if (x
!= NULL
&& *x
!= '\0') {
941 /* Take care not to count adjacent slashes for real, on either end */
945 for(x2
= savestrbuf(x
, i
= strlen(x
)); i
> 0; --i
)
952 if (cproto
!= CPROTO_IMAP
) {
954 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
959 urlp
->url_path
.l
= i
;
960 urlp
->url_path
.s
= x2
;
965 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
967 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
968 *cp
= lowerconv(*cp
);
971 /* .url_h_p: HOST:PORT */
973 struct str
*s
= &urlp
->url_h_p
;
975 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
976 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
977 if (urlp
->url_port
!= NULL
) {
978 size_t j
= strlen(urlp
->url_port
);
980 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
988 * If there was no user in the URL, do we have *user-HOST* or *user*? */
989 if (!urlp
->url_had_user
) {
990 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
992 /* No, check whether .netrc lookup is desired */
994 if (!ok_blook(v15_compat
) ||
995 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
996 !_nrc_lookup(urlp
, FAL0
))
998 urlp
->url_user
.s
= n_UNCONST(myname
);
1001 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
1002 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
1003 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
){
1004 n_err(_("Cannot URL encode %s\n"), urlp
->url_user
.s
);
1007 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
1010 /* And then there are a lot of prebuild string combinations TODO do lazy */
1012 /* .url_u_h: .url_user@.url_host
1013 * For SMTP we apply ridiculously complicated *v15-compat* plus
1014 * *smtp-hostname* / *hostname* dependent rules */
1018 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
1019 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
1022 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
1027 i
= urlp
->url_user
.l
;
1029 s
->s
= salloc(i
+ 1 + h
.l
+1);
1031 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1034 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
1039 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1040 { struct str
*s
= &urlp
->url_u_h_p
;
1041 size_t i
= urlp
->url_user
.l
;
1043 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1045 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1048 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1049 i
+= urlp
->url_h_p
.l
;
1053 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1054 { struct str
*s
= &urlp
->url_eu_h_p
;
1055 size_t i
= urlp
->url_user_enc
.l
;
1057 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1059 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1062 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1063 i
+= urlp
->url_h_p
.l
;
1067 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1069 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
1071 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1072 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
1073 urlp
->url_u_h_p
.l
+1);
1074 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1076 urlp
->url_p_u_h_p
= ud
;
1079 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1081 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1082 1 + urlp
->url_path
.l
+1);
1084 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1085 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1086 urlp
->url_eu_h_p
.l
+1);
1087 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1089 if (urlp
->url_path
.l
== 0)
1090 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1092 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1093 urlp
->url_p_eu_h_p_p
= ud
;
1096 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1101 #endif /* __ANYPROTO */
1110 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1112 char const *pname
, *pxstr
, *authdef
;
1113 size_t pxlen
, addrlen
, i
;
1116 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1118 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1121 memset(ccp
, 0, sizeof *ccp
);
1127 pxstr
= "smtp-auth";
1128 pxlen
= sizeof("smtp-auth") -1;
1129 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1130 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1132 addr_is_nuser
= TRU1
;
1136 pxstr
= "pop3-auth";
1137 pxlen
= sizeof("pop3-auth") -1;
1138 authmask
= AUTHTYPE_PLAIN
;
1143 ccp
->cc_cproto
= cproto
;
1144 addrlen
= strlen(addr
);
1145 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1146 memcpy(vbuf
, pxstr
, pxlen
);
1148 /* Authentication type */
1150 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1151 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1153 if ((s
= vok_vlook(vbuf
)) == NULL
)
1154 s
= n_UNCONST(authdef
);
1157 if (!asccasecmp(s
, "none")) {
1158 ccp
->cc_auth
= "NONE";
1159 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1161 } else if (!asccasecmp(s
, "plain")) {
1162 ccp
->cc_auth
= "PLAIN";
1163 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1164 ware
= REQ_PASS
| REQ_USER
;
1165 } else if (!asccasecmp(s
, "login")) {
1166 ccp
->cc_auth
= "LOGIN";
1167 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1168 ware
= REQ_PASS
| REQ_USER
;
1169 } else if (!asccasecmp(s
, "cram-md5")) {
1170 ccp
->cc_auth
= "CRAM-MD5";
1171 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1172 ware
= REQ_PASS
| REQ_USER
;
1173 } else if (!asccasecmp(s
, "gssapi")) {
1174 ccp
->cc_auth
= "GSS-API";
1175 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1180 if (!(ccp
->cc_authtype
& authmask
)) {
1181 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1186 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1187 n_err(_("No CRAM-MD5 support compiled in\n"));
1192 # ifndef HAVE_GSSAPI
1193 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1194 n_err(_("No GSS-API support compiled in\n"));
1201 if (!(ware
& (WANT_USER
| REQ_USER
)))
1204 if (!addr_is_nuser
) {
1205 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1208 cp
= savestrbuf(addr
, PTR2SIZE(s
- addr
));
1210 if((ccp
->cc_user
.s
= urlxdec(cp
)) == NULL
){
1211 n_err(_("String is not properly URL percent encoded: %s\n"), cp
);
1215 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1216 } else if (ware
& REQ_USER
)
1221 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1223 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1224 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1226 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1227 if ((s
= getuser(NULL
)) == NULL
) {
1228 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1229 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1230 * TODO check that first, then! change control flow, grow vbuf */
1231 n_err(_("A user is necessary for %s authentication\n"), pname
);
1237 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1241 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1244 if (!addr_is_nuser
) {
1245 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1247 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1250 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1251 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1253 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1254 (ware
& REQ_PASS
)) {
1255 if ((s
= getpassword(NULL
)) == NULL
) {
1256 n_err(_("A password is necessary for %s authentication\n"),
1264 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1268 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1269 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1270 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1271 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1273 return (ccp
!= NULL
);
1277 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1279 char const *pstr
, *authdef
;
1281 enum okeys authokey
;
1283 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1287 memset(ccp
, 0, sizeof *ccp
);
1288 ccp
->cc_user
= urlp
->url_user
;
1290 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1294 authokey
= ok_v_smtp_auth
;
1295 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1296 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1301 authokey
= ok_v_pop3_auth
;
1302 authmask
= AUTHTYPE_PLAIN
;
1307 /* Authentication type */
1308 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1309 s
= n_UNCONST(authdef
);
1311 if (!asccasecmp(s
, "none")) {
1312 ccp
->cc_auth
= "NONE";
1313 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1315 } else if (!asccasecmp(s
, "plain")) {
1316 ccp
->cc_auth
= "PLAIN";
1317 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1318 ware
= REQ_PASS
| REQ_USER
;
1319 } else if (!asccasecmp(s
, "login")) {
1320 ccp
->cc_auth
= "LOGIN";
1321 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1322 ware
= REQ_PASS
| REQ_USER
;
1323 } else if (!asccasecmp(s
, "cram-md5")) {
1324 ccp
->cc_auth
= "CRAM-MD5";
1325 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1326 ware
= REQ_PASS
| REQ_USER
;
1327 } else if (!asccasecmp(s
, "gssapi")) {
1328 ccp
->cc_auth
= "GSS-API";
1329 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1334 if (!(ccp
->cc_authtype
& authmask
)) {
1335 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1340 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1341 n_err(_("No CRAM-MD5 support compiled in\n"));
1346 # ifndef HAVE_GSSAPI
1347 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1348 n_err(_("No GSS-API support compiled in\n"));
1355 ccp
->cc_pass
= urlp
->url_pass
;
1356 if (ccp
->cc_pass
.s
!= NULL
)
1359 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1361 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1362 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1363 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1364 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1365 if (!_agent_shell_lookup(urlp
, s
)) {
1368 } else if (urlp
->url_pass
.s
!= NULL
) {
1369 ccp
->cc_pass
= urlp
->url_pass
;
1375 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1376 ccp
->cc_pass
= urlp
->url_pass
;
1380 if (ware
& REQ_PASS
) {
1381 if ((s
= getpassword(NULL
)) != NULL
)
1383 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1385 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1391 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1392 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1393 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1394 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1396 return (ccp
!= NULL
);
1398 #endif /* HAVE_SOCKETS */
1405 struct nrc_node
*nrc
;
1412 if (argv
[1] != NULL
)
1414 if (!asccasecmp(*argv
, "show"))
1417 if (!asccasecmp(*argv
, "load"))
1419 if (!asccasecmp(*argv
, "clear"))
1422 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1426 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1432 if (_nrc_list
== NULL
)
1434 if (_nrc_list
== NRC_NODE_ERR
) {
1435 n_err(_("Interpolate what file?\n"));
1442 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1443 n_perr(_("tmpfile"), 0);
1448 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1449 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
); /* XXX quote? */
1450 if (nrc
->nrc_ulen
> 0)
1451 fprintf(fp
, _("login \"%s\" "),
1452 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1453 if (nrc
->nrc_plen
> 0)
1454 fprintf(fp
, _("password \"%s\"\n"),
1455 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1460 page_or_print(fp
, l
);
1466 if (_nrc_list
== NRC_NODE_ERR
)
1468 while ((nrc
= _nrc_list
) != NULL
) {
1469 _nrc_list
= nrc
->nrc_next
;
1474 #endif /* HAVE_NETRC */
1478 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1480 char const *cp
= vp
;
1484 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1486 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1487 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1488 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1496 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1500 char digest
[16], *cp
;
1504 if(user
->l
>= UIZ_MAX
- 1 - MD5TOHEX_SIZE
- 1)
1506 if(pass
->l
>= INT_MAX
)
1509 in
.s
= n_UNCONST(b64
);
1510 in
.l
= strlen(in
.s
);
1511 if(!b64_decode(&out
, &in
))
1513 if(out
.l
>= INT_MAX
){
1519 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1521 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1523 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1524 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1525 memcpy(in
.s
, user
->s
, user
->l
);
1526 in
.s
[user
->l
] = ' ';
1527 memcpy(&in
.s
[user
->l
+ 1], cp
, MD5TOHEX_SIZE
);
1528 if(b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
) == NULL
)
1537 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1541 * This code is taken from
1543 * Network Working Group H. Krawczyk
1544 * Request for Comments: 2104 IBM
1545 * Category: Informational M. Bellare
1552 * HMAC: Keyed-Hashing for Message Authentication
1555 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1556 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1557 unsigned char tk
[16];
1561 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1566 md5_update(&tctx
, key
, key_len
);
1567 md5_final(tk
, &tctx
);
1573 /* the HMAC_MD5 transform looks like:
1575 * MD5(K XOR opad, MD5(K XOR ipad, text))
1577 * where K is an n byte key
1578 * ipad is the byte 0x36 repeated 64 times
1579 * opad is the byte 0x5c repeated 64 times
1580 * and text is the data being protected */
1582 /* start out by storing key in pads */
1583 memset(k_ipad
, 0, sizeof k_ipad
);
1584 memset(k_opad
, 0, sizeof k_opad
);
1585 memcpy(k_ipad
, key
, key_len
);
1586 memcpy(k_opad
, key
, key_len
);
1588 /* XOR key with ipad and opad values */
1589 for (i
=0; i
<64; i
++) {
1594 /* perform inner MD5 */
1595 md5_init(&context
); /* init context for 1st pass */
1596 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1597 md5_update(&context
, text
, text_len
); /* then text of datagram */
1598 md5_final(digest
, &context
); /* finish up 1st pass */
1600 /* perform outer MD5 */
1601 md5_init(&context
); /* init context for 2nd pass */
1602 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1603 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1604 md5_final(digest
, &context
); /* finish up 2nd pass */
1607 #endif /* HAVE_MD5 */