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 - 2017 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
volatile ispipe
;
119 bool_t seen_default
, nl_last
;
120 struct nrc_node
* volatile ntail
, * volatile nhead
, * volatile nrc
;
123 n_UNINIT(ntail
, NULL
);
129 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
136 if ((netrc_load
= ok_vlook(netrc_pipe
)) != NULL
) {
138 if ((fi
= Popen(netrc_load
, "r", ok_vlook(SHELL
), NULL
, COMMAND_FD_NULL
)
140 n_perr(netrc_load
, 0);
144 if ((netrc_load
= fexpand(ok_vlook(NETRC
), FEXP_LOCAL
| FEXP_NOPROTO
)
148 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
149 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load
, FAL0
));
153 /* Be simple and apply rigid (permission) check(s) */
154 if (fstat(fileno(fi
), &sb
) == -1 || !S_ISREG(sb
.st_mode
) ||
155 (sb
.st_mode
& (S_IRWXG
| S_IRWXO
))) {
156 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
157 n_shexp_quote_cp(netrc_load
, FAL0
));
165 switch((t
= __nrc_token(fi
, buffer
, &nl_last
))) {
168 default: /* Doesn't happen (but on error?), keep CC happy */
171 /* We ignore the default entry (require an exact host match), and we also
172 * ignore anything after such an entry (faulty syntax) */
177 /* Normalize HOST to lowercase */
179 if (!seen_default
&& (t
= __nrc_token(fi
, host
, &nl_last
)) != NRC_INPUT
)
183 for (cp
= host
; *cp
!= '\0'; ++cp
)
184 *cp
= lowerconv(*cp
);
187 *user
= *pass
= '\0';
188 while ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_NONE
&&
189 t
!= NRC_MACHINE
&& t
!= NRC_DEFAULT
) {
192 if ((t
= __nrc_token(fi
, user
, &nl_last
)) != NRC_INPUT
)
196 if ((t
= __nrc_token(fi
, pass
, &nl_last
)) != NRC_INPUT
)
200 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
204 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
208 while ((c
= getc(fi
)) != EOF
)
209 if (c
== '\n') { /* xxx */
210 /* Don't care about comments here, since we parse until
211 * we've seen two successive newline characters */
225 if (!seen_default
&& (*user
!= '\0' || *pass
!= '\0')) {
226 size_t hl
= strlen(host
), ul
= strlen(user
), pl
= strlen(pass
);
227 struct nrc_node
*nx
= smalloc(n_VSTRUCT_SIZEOF(struct nrc_node
,
228 nrc_dat
) + 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(n_poption
& n_PO_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 /* Reverse solidus 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 /* Rverse solidus 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_flags
|= n_URL_HAD_USER
;
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
, "NAIL_USER", "=", urlp
->url_user
.s
,
531 env_addon
[1] = str_concat_csvl(&s
,
532 "NAIL_USER_ENC", "=", urlp
->url_user_enc
.s
, NULL
)->s
;
533 env_addon
[2] = str_concat_csvl(&s
, "NAIL_HOST", "=", urlp
->url_host
.s
,
535 env_addon
[3] = str_concat_csvl(&s
, "NAIL_HOST_PORT", "=", urlp
->url_h_p
.s
,
539 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
540 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm
);
544 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
545 if (u
.c
== '\n') /* xxx */
548 if (l
== sizeof(buf
) - 1) {
549 n_str_add_buf(&s
, buf
, l
);
554 n_str_add_buf(&s
, buf
, l
);
556 if (!Pclose(pbuf
, TRU1
)) {
557 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm
);
561 /* We are responsible for duplicating this buffer! */
563 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
565 urlp
->url_pass
.s
= n_UNCONST(n_empty
), urlp
->url_pass
.l
= 0;
573 #endif /* HAVE_AGENT */
576 (urlxenc
)(char const *cp
, bool_t ispath n_MEMORY_DEBUG_ARGS
)
585 if(i
>= UIZ_MAX
/ 3){
591 np
= n
= (n_autorec_alloc
)(NULL
, i n_MEMORY_DEBUG_ARGSCALL
);
594 for (; (c1
= *cp
) != '\0'; ++cp
) {
595 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
596 * ALPHA / DIGIT / "-" / "." / "_" / "~"
597 * However add a special is[file]path mode for file-system friendliness */
598 if (alnumchar(c1
) || c1
== '_')
601 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
604 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
609 n_c_to_hex_base16(np
+ 1, c1
);
620 (urlxdec
)(char const *cp n_MEMORY_DEBUG_ARGS
)
626 np
= n
= (n_autorec_alloc
)(NULL
, strlen(cp
) +1 n_MEMORY_DEBUG_ARGSCALL
);
628 while ((c
= (uc_i
)*cp
++) != '\0') {
629 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
631 if (n_LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
647 char const **argv
, *varname
, *varres
, *act
, *cp
;
651 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
654 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
656 if((ispath
= (*act
== 'p'))){
657 if(!ascncasecmp(++act
, "ath", 3))
662 alen
= PTR2SIZE(cp
- act
);
666 if(is_ascncaseprefix(act
, "encode", alen
)){
667 if((varres
= urlxenc(cp
, ispath
)) == NULL
){
671 }else if(is_ascncaseprefix(act
, "decode", alen
)){
672 if((varres
= urlxdec(cp
)) == NULL
){
681 if(!n_var_vset(varname
, (uintptr_t)varres
)){
688 in
.l
= strlen(in
.s
= n_UNCONST(varres
));
689 makeprint(&in
, &out
);
690 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0)
696 n_pstate_var__em
= n_0
;
699 return (cp
!= NULL
? 0 : 1);
701 n_err(_("Synopsis: urlcodec: "
702 "<[path]e[ncode]|[path]d[ecode]> <rest-of-line>\n"));
708 c_urlencode(void *v
) /* XXX IDNA?? */
713 n_OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
715 for (ap
= v
; *ap
!= NULL
; ++ap
) {
716 char *in
= *ap
, *out
= urlxenc(in
, FAL0
);
719 out
= n_UNCONST(V_(n_error
));
721 " in: <%s> (%" PRIuZ
" bytes)\nout: <%s> (%" PRIuZ
" bytes)\n",
722 in
, strlen(in
), out
, strlen(out
));
729 c_urldecode(void *v
) /* XXX IDNA?? */
734 n_OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
736 for (ap
= v
; *ap
!= NULL
; ++ap
) {
737 char *in
= *ap
, *out
= urlxdec(in
);
740 out
= n_UNCONST(V_(n_error
));
742 " in: <%s> (%" PRIuZ
" bytes)\nout: <%s> (%" PRIuZ
" bytes)\n",
743 in
, strlen(in
), out
, strlen(out
));
750 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
753 char const *mailtop_orig
;
756 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
760 mailtop
+= sizeof("mailto:") -1;
762 /* TODO This is all intermediate, and for now just enough to understand
763 * TODO a little bit of a little more advanced List-Post: headers. */
764 /* Strip any hfield additions, keep only to addr-spec's */
765 if((rv
= strchr(mailtop
, '?')) != NULL
)
766 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
768 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
772 /* Simply perform percent-decoding if there is a percent % */
773 if(memchr(rv
, '%', i
) != NULL
){
777 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
780 if((c
= *mailtop
++) == '%'){
783 if(i
< 3 || (cc
= n_c_from_hex_base16(mailtop
)) < 0){
784 if(!err
&& (err
= TRU1
, n_poption
& n_PO_D_V
))
785 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
786 n_shexp_quote_cp(mailtop_orig
, FAL0
));
807 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
814 { "submission", "587", 587},
815 { "smtps", "465", 465},
816 { "pop3", "110", 110},
817 { "pop3s", "995", 995},
818 { "imap", "143", 143},
819 { "imaps", "993", 993},
826 for(rv
= proto
; *rv
!= '\0'; ++rv
)
829 l
= PTR2SIZE(rv
- proto
);
831 for(rv
= NULL
, i
= 0; i
< n_NELEM(tbl
); ++i
)
832 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
834 if(irv_or_null
!= NULL
)
835 *irv_or_null
= tbl
[i
].portno
;
842 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
844 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
846 #if defined HAVE_SMTP && defined HAVE_POP3
849 #if defined HAVE_SMTP || defined HAVE_POP3
857 memset(urlp
, 0, sizeof *urlp
);
858 urlp
->url_input
= data
;
859 urlp
->url_cproto
= cproto
;
861 /* Network protocol */
862 #define a_PROTOX(X,Y,Z) \
863 urlp->url_portno = Y;\
864 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
865 urlp->url_proto[sizeof(X) -1] = '\0';\
866 urlp->url_proto_len = sizeof(X) -1;\
867 urlp->url_proto_xlen = sizeof(X "://") -1;\
869 #define a__IF(X,Y,Z) \
870 if(!ascncasecmp(data, X "://", sizeof(X "://") -1)){\
872 data += sizeof(X "://") -1;\
875 #define a_IF(X,Y) a__IF(X, Y, (void)0)
877 # define a_IFS(X,Y) a__IF(X, Y, urlp->url_flags |= n_URL_TLS_REQUIRED)
878 # define a_IFs(X,Y) a__IF(X, Y, urlp->url_flags |= n_URL_TLS_OPTIONAL)
880 # define a_IFS(X,Y) goto jeproto;
881 # define a_IFs(X,Y) a_IF(X, Y)
886 /* The special S/MIME etc. credential lookup */
888 a_PROTOX("ccred", 0, (void)0);
897 a_IFs("submission", 587)
898 a_PROTOX("smtp", 25, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
907 a_PROTOX("pop3", 110, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
916 a_PROTOX("imap", 143, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
929 if (strstr(data
, "://") != NULL
) {
930 #if !defined a_ALLPROTO || !defined HAVE_SSL
933 n_err(_("URL proto:// prefix invalid: %s\n"), urlp
->url_input
);
938 /* User and password, I */
940 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
942 char const *urlpe
, *d
;
945 l
= PTR2SIZE(cp
- data
);
948 urlp
->url_flags
|= n_URL_HAD_USER
;
951 /* And also have a password? */
952 if((cp
= memchr(d
, ':', l
)) != NULL
){
953 size_t i
= PTR2SIZE(cp
- d
);
956 memcpy(ub
, cp
+ 1, l
);
959 if((urlp
->url_pass
.s
= urlxdec(ub
)) == NULL
)
961 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
);
962 if((urlpe
= urlxenc(urlp
->url_pass
.s
, FAL0
)) == NULL
)
964 if(strcmp(ub
, urlpe
))
971 if((urlp
->url_user
.s
= urlxdec(ub
)) == NULL
)
973 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
974 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
)
976 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
978 if(urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)){
980 n_err(_("String is not properly URL percent encoded: %s\n"), ub
);
989 /* Servername and port -- and possible path suffix */
990 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, use IPAddress! */
991 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
992 if ((x
= strchr(x
, '/')) != NULL
) {
998 if((n_idec_ui16_cp(&urlp
->url_portno
, urlp
->url_port
, 10, NULL
999 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1000 ) != n_IDEC_STATE_CONSUMED
){
1001 n_err(_("URL with invalid port number: %s\n"), urlp
->url_input
);
1005 if ((x
= strchr(data
, '/')) != NULL
) {
1006 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
1010 cp
= n_UNCONST(data
+ strlen(data
));
1013 /* A (non-empty) path may only occur with IMAP */
1014 if (x
!= NULL
&& *x
!= '\0') {
1015 /* Take care not to count adjacent slashes for real, on either end */
1019 for(x2
= savestrbuf(x
, i
= strlen(x
)); i
> 0; --i
)
1020 if(x2
[i
- 1] != '/')
1026 if (cproto
!= CPROTO_IMAP
) {
1028 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
1033 urlp
->url_path
.l
= i
;
1034 urlp
->url_path
.s
= x2
;
1039 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
1041 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
1042 *cp
= lowerconv(*cp
);
1045 /* .url_h_p: HOST:PORT */
1047 struct str
*s
= &urlp
->url_h_p
;
1049 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
1050 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
1051 if (urlp
->url_port
!= NULL
) {
1052 size_t j
= strlen(urlp
->url_port
);
1054 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
1062 * If there was no user in the URL, do we have *user-HOST* or *user*? */
1063 if (!(urlp
->url_flags
& n_URL_HAD_USER
)) {
1064 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
1066 /* No, check whether .netrc lookup is desired */
1068 if (!ok_blook(v15_compat
) ||
1069 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
1070 !_nrc_lookup(urlp
, FAL0
))
1072 urlp
->url_user
.s
= n_UNCONST(ok_vlook(LOGNAME
));
1075 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
1076 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
1077 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
){
1078 n_err(_("Cannot URL encode %s\n"), urlp
->url_user
.s
);
1081 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
1084 /* And then there are a lot of prebuild string combinations TODO do lazy */
1086 /* .url_u_h: .url_user@.url_host
1087 * For SMTP we apply ridiculously complicated *v15-compat* plus
1088 * *smtp-hostname* / *hostname* dependent rules */
1092 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
1093 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
1096 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
1101 i
= urlp
->url_user
.l
;
1103 s
->s
= salloc(i
+ 1 + h
.l
+1);
1105 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1108 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
1113 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1114 { struct str
*s
= &urlp
->url_u_h_p
;
1115 size_t i
= urlp
->url_user
.l
;
1117 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1119 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1122 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1123 i
+= urlp
->url_h_p
.l
;
1127 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1128 { struct str
*s
= &urlp
->url_eu_h_p
;
1129 size_t i
= urlp
->url_user_enc
.l
;
1131 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1133 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1136 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1137 i
+= urlp
->url_h_p
.l
;
1141 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1143 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
1145 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1146 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
1147 urlp
->url_u_h_p
.l
+1);
1148 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1150 urlp
->url_p_u_h_p
= ud
;
1153 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1155 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1156 1 + urlp
->url_path
.l
+1);
1158 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1159 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1160 urlp
->url_eu_h_p
.l
+1);
1161 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1163 if (urlp
->url_path
.l
== 0)
1164 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1166 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1167 urlp
->url_p_eu_h_p_p
= ud
;
1170 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1175 #endif /* a_ANYPROTO */
1184 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1186 char const *pname
, *pxstr
, *authdef
, *s
;
1187 size_t pxlen
, addrlen
, i
;
1190 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1192 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1195 n_OBSOLETE(_("Use of old-style credentials, which will vanish in v15!\n"
1196 " Please read the manual section "
1197 "\"On URL syntax and credential lookup\""));
1199 memset(ccp
, 0, sizeof *ccp
);
1205 pxstr
= "smtp-auth";
1206 pxlen
= sizeof("smtp-auth") -1;
1207 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1208 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1210 addr_is_nuser
= TRU1
;
1214 pxstr
= "pop3-auth";
1215 pxlen
= sizeof("pop3-auth") -1;
1216 authmask
= AUTHTYPE_PLAIN
;
1221 ccp
->cc_cproto
= cproto
;
1222 addrlen
= strlen(addr
);
1223 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1224 memcpy(vbuf
, pxstr
, pxlen
);
1226 /* Authentication type */
1228 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1229 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1231 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
)
1232 s
= n_UNCONST(authdef
);
1235 if (!asccasecmp(s
, "none")) {
1236 ccp
->cc_auth
= "NONE";
1237 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1239 } else if (!asccasecmp(s
, "plain")) {
1240 ccp
->cc_auth
= "PLAIN";
1241 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1242 ware
= REQ_PASS
| REQ_USER
;
1243 } else if (!asccasecmp(s
, "login")) {
1244 ccp
->cc_auth
= "LOGIN";
1245 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1246 ware
= REQ_PASS
| REQ_USER
;
1247 } else if (!asccasecmp(s
, "cram-md5")) {
1248 ccp
->cc_auth
= "CRAM-MD5";
1249 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1250 ware
= REQ_PASS
| REQ_USER
;
1251 } else if (!asccasecmp(s
, "gssapi")) {
1252 ccp
->cc_auth
= "GSS-API";
1253 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1258 if (!(ccp
->cc_authtype
& authmask
)) {
1259 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1264 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1265 n_err(_("No CRAM-MD5 support compiled in\n"));
1270 # ifndef HAVE_GSSAPI
1271 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1272 n_err(_("No GSS-API support compiled in\n"));
1279 if (!(ware
& (WANT_USER
| REQ_USER
)))
1282 if (!addr_is_nuser
) {
1283 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1286 cp
= savestrbuf(addr
, PTR2SIZE(s
- addr
));
1288 if((ccp
->cc_user
.s
= urlxdec(cp
)) == NULL
){
1289 n_err(_("String is not properly URL percent encoded: %s\n"), cp
);
1293 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1294 } else if (ware
& REQ_USER
)
1299 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1301 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1302 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1304 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
&& (ware
& REQ_USER
)) {
1305 if ((s
= getuser(NULL
)) == NULL
) {
1306 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1307 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1308 * TODO check that first, then! change control flow, grow vbuf */
1309 n_err(_("A user is necessary for %s authentication\n"), pname
);
1315 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1319 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1322 if (!addr_is_nuser
) {
1323 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1325 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1328 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1329 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1331 if ((!addr_is_nuser
|| (s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) &&
1332 (ware
& REQ_PASS
)) {
1333 if ((s
= getpassword(savecat(_("Password for "), pname
))) == NULL
) {
1334 n_err(_("A password is necessary for %s authentication\n"),
1342 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1346 if (ccp
!= NULL
&& (n_poption
& n_PO_D_VV
))
1347 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1348 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1349 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1351 return (ccp
!= NULL
);
1355 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1358 char const *pstr
, *authdef
;
1360 enum okeys authokey
;
1361 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1365 memset(ccp
, 0, sizeof *ccp
);
1366 ccp
->cc_user
= urlp
->url_user
;
1370 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1372 authokey
= (enum okeys
)-1;
1373 authmask
= AUTHTYPE_PLAIN
;
1379 authokey
= ok_v_smtp_auth
;
1380 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1381 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1386 authokey
= ok_v_pop3_auth
;
1387 authmask
= AUTHTYPE_PLAIN
;
1393 /* Authentication type */
1394 if (authokey
== (enum okeys
)-1 ||
1395 (s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1396 s
= n_UNCONST(authdef
);
1398 if (!asccasecmp(s
, "none")) {
1399 ccp
->cc_auth
= "NONE";
1400 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1402 } else if (!asccasecmp(s
, "plain")) {
1403 ccp
->cc_auth
= "PLAIN";
1404 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1405 ware
= REQ_PASS
| REQ_USER
;
1406 } else if (!asccasecmp(s
, "login")) {
1407 ccp
->cc_auth
= "LOGIN";
1408 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1409 ware
= REQ_PASS
| REQ_USER
;
1410 } else if (!asccasecmp(s
, "cram-md5")) {
1411 ccp
->cc_auth
= "CRAM-MD5";
1412 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1413 ware
= REQ_PASS
| REQ_USER
;
1414 } else if (!asccasecmp(s
, "gssapi")) {
1415 ccp
->cc_auth
= "GSS-API";
1416 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1421 if (!(ccp
->cc_authtype
& authmask
)) {
1422 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1427 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1428 n_err(_("No CRAM-MD5 support compiled in\n"));
1433 # ifndef HAVE_GSSAPI
1434 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1435 n_err(_("No GSS-API support compiled in\n"));
1442 ccp
->cc_pass
= urlp
->url_pass
;
1443 if (ccp
->cc_pass
.s
!= NULL
)
1446 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1448 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1449 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1450 n_OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1451 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1452 if (!_agent_shell_lookup(urlp
, s
)) {
1455 } else if (urlp
->url_pass
.s
!= NULL
) {
1456 ccp
->cc_pass
= urlp
->url_pass
;
1462 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1463 ccp
->cc_pass
= urlp
->url_pass
;
1467 if (ware
& REQ_PASS
) {
1468 if((s
= getpassword(savecat(urlp
->url_u_h
.s
, _(" requires a password: ")))
1471 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1473 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1479 if(ccp
!= NULL
&& (n_poption
& n_PO_D_VV
))
1480 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1481 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1482 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1484 return (ccp
!= NULL
);
1486 #endif /* HAVE_SOCKETS */
1493 struct nrc_node
*nrc
;
1500 if (argv
[1] != NULL
)
1502 if (!asccasecmp(*argv
, "show"))
1505 if (!asccasecmp(*argv
, "load"))
1507 if (!asccasecmp(*argv
, "clear"))
1510 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1514 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1520 if (_nrc_list
== NULL
)
1522 if (_nrc_list
== NRC_NODE_ERR
) {
1523 n_err(_("Interpolate what file?\n"));
1530 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1531 n_perr(_("tmpfile"), 0);
1536 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1537 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
); /* XXX quote? */
1538 if (nrc
->nrc_ulen
> 0)
1539 fprintf(fp
, _("login \"%s\" "),
1540 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1541 if (nrc
->nrc_plen
> 0)
1542 fprintf(fp
, _("password \"%s\"\n"),
1543 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1548 page_or_print(fp
, l
);
1554 if (_nrc_list
== NRC_NODE_ERR
)
1556 while ((nrc
= _nrc_list
) != NULL
) {
1557 _nrc_list
= nrc
->nrc_next
;
1562 #endif /* HAVE_NETRC */
1566 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1568 char const *cp
= vp
;
1572 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1574 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1575 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1576 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1584 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1588 char digest
[16], *cp
;
1592 if(user
->l
>= UIZ_MAX
- 1 - MD5TOHEX_SIZE
- 1)
1594 if(pass
->l
>= INT_MAX
)
1597 in
.s
= n_UNCONST(b64
);
1598 in
.l
= strlen(in
.s
);
1599 if(!b64_decode(&out
, &in
))
1601 if(out
.l
>= INT_MAX
){
1607 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1609 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1611 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1612 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1613 memcpy(in
.s
, user
->s
, user
->l
);
1614 in
.s
[user
->l
] = ' ';
1615 memcpy(&in
.s
[user
->l
+ 1], cp
, MD5TOHEX_SIZE
);
1616 if(b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
) == NULL
)
1625 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1629 * This code is taken from
1631 * Network Working Group H. Krawczyk
1632 * Request for Comments: 2104 IBM
1633 * Category: Informational M. Bellare
1640 * HMAC: Keyed-Hashing for Message Authentication
1643 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1644 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1645 unsigned char tk
[16];
1649 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1654 md5_update(&tctx
, key
, key_len
);
1655 md5_final(tk
, &tctx
);
1661 /* the HMAC_MD5 transform looks like:
1663 * MD5(K XOR opad, MD5(K XOR ipad, text))
1665 * where K is an n byte key
1666 * ipad is the byte 0x36 repeated 64 times
1667 * opad is the byte 0x5c repeated 64 times
1668 * and text is the data being protected */
1670 /* start out by storing key in pads */
1671 memset(k_ipad
, 0, sizeof k_ipad
);
1672 memset(k_opad
, 0, sizeof k_opad
);
1673 memcpy(k_ipad
, key
, key_len
);
1674 memcpy(k_opad
, key
, key_len
);
1676 /* XOR key with ipad and opad values */
1677 for (i
=0; i
<64; i
++) {
1682 /* perform inner MD5 */
1683 md5_init(&context
); /* init context for 1st pass */
1684 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1685 md5_update(&context
, text
, text_len
); /* then text of datagram */
1686 md5_final(digest
, &context
); /* finish up 1st pass */
1688 /* perform outer MD5 */
1689 md5_init(&context
); /* init context for 2nd pass */
1690 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1691 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1692 md5_final(digest
, &context
); /* finish up 2nd pass */
1695 #endif /* HAVE_MD5 */