1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ URL parsing, credential handling and crypto hooks.
3 *@ .netrc parser quite loosely based upon NetBSD usr.bin/ftp/
4 *@ $NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp $
6 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #define n_FILE urlcrecry
23 #ifndef HAVE_AMALGAMATION
28 /* NetBSD usr.bin/ftp/ruserpass.c uses 100 bytes for that, we need four
29 * concurrently (dummy, host, user, pass), so make it a KB */
30 # define NRC_TOKEN_MAXLEN (1024 / 4)
45 struct nrc_node
*nrc_next
;
46 struct nrc_node
*nrc_result
; /* In match phase, former possible one */
47 ui32_t nrc_mlen
; /* Length of machine name */
48 ui32_t nrc_ulen
; /* Length of user name */
49 ui32_t nrc_plen
; /* Length of password */
50 char nrc_dat
[VFIELD_SIZE(sizeof(ui32_t
))];
52 # define NRC_NODE_ERR ((struct nrc_node*)-1)
54 static struct nrc_node
*_nrc_list
;
55 #endif /* HAVE_NETRC */
57 /* Find the last @ before a slash
58 * TODO Casts off the const but this is ok here; obsolete function! */
59 #ifdef HAVE_SOCKETS /* temporary (we'll have file://..) */
60 static char * _url_last_at_before_slash(char const *sp
);
64 /* Initialize .netrc cache */
65 static void _nrc_init(void);
66 static enum nrc_token
__nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
]);
68 /* We shall lookup a machine in .netrc says ok_blook(netrc_lookup).
69 * only_pass is true then the lookup is for the password only, otherwise we
70 * look for a user (and add password only if we have an exact machine match) */
71 static bool_t
_nrc_lookup(struct url
*urlp
, bool_t only_pass
);
73 /* 0=no match; 1=exact match; -1=wildcard match */
74 static int __nrc_host_match(struct nrc_node
const *nrc
,
75 struct url
const *urlp
);
76 static bool_t
__nrc_find_user(struct url
*urlp
,
77 struct nrc_node
const *nrc
);
78 static bool_t
__nrc_find_pass(struct url
*urlp
, bool_t user_match
,
79 struct nrc_node
const *nrc
);
80 #endif /* HAVE_NETRC */
82 /* The password can also be gained through external agents */
84 static bool_t
_agent_shell_lookup(struct url
*urlp
, char const *comm
);
89 _url_last_at_before_slash(char const *sp
)
95 for (cp
= sp
; (c
= *cp
) != '\0'; ++cp
)
98 while (cp
> sp
&& *--cp
!= '@')
111 char buffer
[NRC_TOKEN_MAXLEN
], host
[NRC_TOKEN_MAXLEN
],
112 user
[NRC_TOKEN_MAXLEN
], pass
[NRC_TOKEN_MAXLEN
], *netrc_load
;
117 struct nrc_node
*ntail
= NULL
/* CC happy */, *nhead
= NULL
,
121 if ((netrc_load
= env_vlook("NETRC", FAL0
)) == NULL
)
122 netrc_load
= UNCONST(NETRC
);
123 if ((netrc_load
= file_expand(netrc_load
)) == NULL
)
126 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
127 n_err(_("Cannot open \"%s\"\n"), netrc_load
);
131 /* Be simple and apply rigid (permission) check(s) */
132 if (fstat(fileno(fi
), &sb
) == -1 || !S_ISREG(sb
.st_mode
) ||
133 (sb
.st_mode
& (S_IRWXG
| S_IRWXO
))) {
134 n_err(_("Not a regular file, or accessible by non-user: \"%s\"\n"),
141 switch((t
= __nrc_token(fi
, buffer
))) {
144 default: /* Doesn't happen (but on error?), keep CC happy */
147 /* We ignore the default entry (require an exact host match), and we also
148 * ignore anything after such an entry (faulty syntax) */
153 /* Normalize HOST to lowercase */
155 if (!seen_default
&& (t
= __nrc_token(fi
, host
)) != NRC_INPUT
)
159 for (cp
= host
; *cp
!= '\0'; ++cp
)
160 *cp
= lowerconv(*cp
);
163 *user
= *pass
= '\0';
164 while ((t
= __nrc_token(fi
, buffer
)) != NRC_NONE
&& t
!= NRC_MACHINE
&&
168 if ((t
= __nrc_token(fi
, user
)) != NRC_INPUT
)
172 if ((t
= __nrc_token(fi
, pass
)) != NRC_INPUT
)
176 if ((t
= __nrc_token(fi
, buffer
)) != NRC_INPUT
)
180 if ((t
= __nrc_token(fi
, buffer
)) != NRC_INPUT
)
184 while ((c
= getc(fi
)) != EOF
)
185 if (c
== '\n') { /* xxx */
199 if (!seen_default
&& (*user
!= '\0' || *pass
!= '\0')) {
200 size_t hl
= strlen(host
), ul
= strlen(user
), pl
= strlen(pass
);
201 struct nrc_node
*nx
= smalloc(sizeof(*nx
) -
202 VFIELD_SIZEOF(struct nrc_node
, nrc_dat
) + hl
+1 + ul
+1 + pl
+1);
205 ntail
->nrc_next
= nx
;
213 memcpy(nx
->nrc_dat
, host
, ++hl
);
214 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
215 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
217 if (t
== NRC_MACHINE
)
219 if (t
== NRC_DEFAULT
)
226 if (options
& OPT_D_V
)
227 n_err(_("Errors occurred while parsing \"%s\"\n"), netrc_load
);
228 assert(nrc
== NRC_NODE_ERR
);
236 if (nrc
== NRC_NODE_ERR
)
237 while (nhead
!= NULL
) {
239 nhead
= nhead
->nrc_next
;
247 static enum nrc_token
248 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
])
252 enum nrc_token rv
= NRC_NONE
;
256 if (feof(fi
) || ferror(fi
))
259 while ((c
= getc(fi
)) != EOF
&& whitechar(c
))
265 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
266 if (c
== '"' || c
== '\'') {
269 /* Not requiring the closing QM is (Net)BSD syntax */
270 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
271 /* Backslash escaping the next character is (Net)BSD syntax */
273 if ((c
= getc(fi
)) == EOF
)
276 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
283 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
284 /* Backslash escaping the next character is (Net)BSD syntax */
285 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
288 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
297 do {/*rv = NRC_NONE*/} while (0);
298 else if (!strcmp(buffer
, "default"))
300 else if (!strcmp(buffer
, "login"))
302 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
304 else if (!strcmp(buffer
, "account"))
306 else if (!strcmp(buffer
, "macdef"))
308 else if (!strcmp(buffer
, "machine"))
313 if (c
== EOF
&& !feof(fi
))
320 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
322 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
326 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
327 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
329 if (_nrc_list
== NULL
)
331 if (_nrc_list
== NRC_NODE_ERR
)
334 nrc_wild
= nrc_exact
= NULL
;
335 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
336 switch (__nrc_host_match(nrc
, urlp
)) {
338 nrc
->nrc_result
= nrc_exact
;
342 nrc
->nrc_result
= nrc_wild
;
349 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
350 /* Must be an unambiguous entry of its kind */
351 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
353 if (__nrc_find_user(urlp
, nrc_exact
))
356 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
358 if (!__nrc_find_user(urlp
, nrc_wild
))
364 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
365 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
366 /* Do not try to find a password without exact user match unless we've
367 * been called during credential lookup, a.k.a. the second time */
369 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
370 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
378 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
385 /* Find a matching machine -- entries are all lowercase normalized */
386 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
387 if (LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
392 /* Cannot be an exact match, but maybe the .netrc machine starts with
393 * a "*." glob, which we recognize as an extension, meaning "skip
394 * a single subdomain, then match the rest" */
395 d1
= nrc
->nrc_dat
+ 2;
397 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
401 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
402 * in fact this even succeeds for ".host.com", but - why care, here? */
403 d2
= urlp
->url_host
.s
;
404 l2
= urlp
->url_host
.l
;
411 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
412 /* This matches, but we won't use it directly but watch out for an
413 * exact match first! */
421 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
425 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
426 if (nrc
->nrc_ulen
> 0) {
427 /* Fake it was part of URL otherwise XXX */
428 urlp
->url_had_user
= TRU1
;
429 /* That buffer will be duplicated by url_parse() in this case! */
430 urlp
->url_user
.s
= UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
431 urlp
->url_user
.l
= nrc
->nrc_ulen
;
436 return (nrc
!= NULL
);
440 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
444 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
445 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
446 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
452 } else if (!um
&& nrc
->nrc_ulen
> 0)
454 if (nrc
->nrc_plen
== 0)
457 /* We are responsible for duplicating this buffer! */
458 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
459 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
464 return (nrc
!= NULL
);
466 #endif /* HAVE_NETRC */
470 _agent_shell_lookup(struct url
*urlp
, char const *comm
)
473 char const *env_addon
[8];
476 union {char const *cp
; int c
; sighandler_type sht
;} u
;
481 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
483 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
485 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
487 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
490 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", tempdir
, NULL
)->s
;
491 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", tempdir
, NULL
)->s
;
495 if ((u
.cp
= ok_vlook(SHELL
)) == NULL
)
497 if ((pbuf
= Popen(comm
, "r", u
.cp
, env_addon
, -1)) == NULL
) {
498 n_err(_("*agent-shell-lookup* startup failed (`%s')\n"), comm
);
502 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
503 if (u
.c
== '\n') /* xxx */
506 if (l
== sizeof(buf
) - 1) {
507 n_str_add_buf(&s
, buf
, l
);
512 n_str_add_buf(&s
, buf
, l
);
514 if (!Pclose(pbuf
, TRU1
)) {
515 if (options
& OPT_D_V
)
516 n_err(_("*agent-shell-lookup* execution failure (`%s')\n"), comm
);
520 /* We are responsible for duplicating this buffer! */
522 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
524 urlp
->url_pass
.s
= UNCONST(""), urlp
->url_pass
.l
= 0;
535 (urlxenc
)(char const *cp
, bool_t ispath SALLOC_DEBUG_ARGS
)
540 np
= n
= (salloc
)(strlen(cp
) * 3 +1 SALLOC_DEBUG_ARGSCALL
);
542 for (; (c1
= *cp
) != '\0'; ++cp
) {
543 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
544 * ALPHA / DIGIT / "-" / "." / "_" / "~"
545 * However add a special is[file]path mode for file-system friendliness */
546 if (alnumchar(c1
) || c1
== '_')
549 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
552 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
557 mime_char_to_hexseq(np
+ 1, c1
);
567 (urlxdec
)(char const *cp SALLOC_DEBUG_ARGS
)
573 np
= n
= (salloc
)(strlen(cp
) +1 SALLOC_DEBUG_ARGSCALL
);
575 while ((c
= (uc_i
)*cp
++) != '\0') {
576 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
578 if (LIKELY((c
= mime_hexseq_to_char(cp
)) >= '\0'))
593 char const **argv
, *cp
, *res
;
596 if(*(cp
= *(argv
= v
)) == 'p'){
597 if(!ascncasecmp(++cp
, "ath", 3))
602 if(is_prefix(cp
, "encode")){
603 while((cp
= *++argv
) != NULL
){
604 res
= urlxenc(cp
, ispath
);
605 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
606 cp
, strlen(cp
), res
, strlen(res
));
608 }else if(is_prefix(cp
, "decode")){
609 while((cp
= *++argv
) != NULL
){
611 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s (%" PRIuZ
" bytes)\n",
612 cp
, strlen(cp
), res
, strlen(res
));
616 n_err(_("`urlcodec': invalid subcommand: %s\n"), *argv
);
620 return (cp
!= NULL
? OKAY
: STOP
);
624 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
627 char const *mailtop_orig
;
630 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
634 mailtop
+= sizeof("mailto:") -1;
636 /* TODO This is all intermediate, and for now just enough to understand
637 * TODO a little bit of a little more advanced List-Post: headers. */
638 /* Strip any hfield additions, keep only to addr-spec's */
639 if((rv
= strchr(mailtop
, '?')) != NULL
)
640 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
642 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
646 /* Simply perform percent-decoding if there is a percent % */
647 if(memchr(rv
, '%', i
) != NULL
){
651 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
654 if((c
= *mailtop
++) == '%'){
657 if(i
< 3 || (cc
= mime_hexseq_to_char(mailtop
)) < 0){
658 if(!err
&& (err
= TRU1
, options
& OPT_D_V
))
659 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"), mailtop_orig
);
679 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
681 url_servbyname(struct url
const *urlp
, ui16_t
*irv_or_null
)
689 { "submission", "587", 587},
690 { "smtps", "465", 465},
691 { "pop3", "110", 110},
692 { "pop3s", "995", 995},
693 { "imap", "143", 143},
694 { "imaps", "993", 993}
700 for (rv
= NULL
, i
= 0; i
< NELEM(tbl
); ++i
)
701 if (!asccasecmp(tbl
[i
].name
, urlp
->url_proto
)) {
703 if (irv_or_null
!= NULL
)
704 *irv_or_null
= tbl
[i
].portno
;
712 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
714 #if defined HAVE_SMTP && defined HAVE_POP3 && defined HAVE_IMAP
717 #if defined HAVE_SMTP || defined HAVE_POP3 || defined HAVE_IMAP
725 memset(urlp
, 0, sizeof *urlp
);
726 urlp
->url_input
= data
;
727 urlp
->url_cproto
= cproto
;
729 /* Network protocol */
730 #define _protox(X,Y) \
731 urlp->url_portno = Y;\
732 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
733 urlp->url_proto[sizeof(X) -1] = '\0';\
734 urlp->url_proto_len = sizeof(X) -1;\
735 urlp->url_proto_xlen = sizeof(X "://") -1
736 #define __if(X,Y,Z) \
737 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
739 data += sizeof(X "://") -1;\
740 do { Z; } while (0);\
743 #define _if(X,Y) __if(X, Y, (void)0)
745 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
747 # define _ifs(X,Y) goto jeproto;
754 _if ("submission", 587)
765 _protox("pop3", 110);
774 _protox("imap", 143);
786 if (strstr(data
, "://") != NULL
) {
787 #if !defined __ALLPROTO || !defined HAVE_SSL
790 n_err(_("URL \"proto://\" prefix invalid: \"%s\"\n"), urlp
->url_input
);
795 /* User and password, I */
797 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
798 size_t l
= PTR2SIZE(cp
- data
);
799 char const *d
= data
;
800 char *ub
= ac_alloc(l
+1);
802 urlp
->url_had_user
= TRU1
;
805 /* And also have a password? */
806 if ((cp
= memchr(d
, ':', l
)) != NULL
) {
807 size_t i
= PTR2SIZE(cp
- d
);
810 memcpy(ub
, cp
+ 1, l
);
812 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(ub
));
814 if (strcmp(ub
, urlxenc(urlp
->url_pass
.s
, FAL0
))) {
815 n_err(_("String is not properly URL percent encoded: \"%s\"\n"),
824 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(ub
));
825 urlp
->url_user_enc
.l
= strlen(
826 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
828 if (urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)) {
829 n_err(_("String is not properly URL percent encoded: \"%s\"\n"), ub
);
836 /* Servername and port -- and possible path suffix */
837 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
841 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
842 if ((x
= strchr(x
, '/')) != NULL
) {
847 l
= strtol(urlp
->url_port
, &eptr
, 10);
848 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
849 n_err(_("URL with invalid port number: \"%s\"\n"), urlp
->url_input
);
852 urlp
->url_portno
= (ui16_t
)l
;
854 if ((x
= strchr(data
, '/')) != NULL
) {
855 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
859 cp
= UNCONST(data
+ strlen(data
));
862 /* A (non-empty) path may only occur with IMAP */
863 if (x
!= NULL
&& *x
) {
866 if((i
= strlen(x
)) > 0){
868 while(i
-- > 0 && x
[i
- 1] == '/')
870 /* If the name ends with a slash, we need to append INBOX for IMAP */
871 if(cproto
== CPROTO_IMAP
){
872 urlp
->url_path
.s
= salloc(i
+ sizeof("/INBOX"));
873 memcpy(urlp
->url_path
.s
, x
, i
);
874 memcpy(&urlp
->url_path
.s
[i
], "/INBOX", sizeof("/INBOX"));
875 urlp
->url_path
.l
= i
+ sizeof("/INBOX") -1;
877 urlp
->url_path
.l
= i
;
878 }else if(cproto
== CPROTO_IMAP
){
879 urlp
->url_path
.s
= x
;
880 urlp
->url_path
.l
= i
;
882 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
889 if(cproto
== CPROTO_IMAP
&& urlp
->url_path
.s
== NULL
)
890 urlp
->url_path
.s
= savestrbuf("INBOX",
891 urlp
->url_path
.l
= sizeof("INBOX") -1);
893 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
895 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
896 *cp
= lowerconv(*cp
);
899 /* .url_h_p: HOST:PORT */
901 struct str
*s
= &urlp
->url_h_p
;
903 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
904 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
905 if (urlp
->url_port
!= NULL
) {
906 size_t j
= strlen(urlp
->url_port
);
908 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
916 * If there was no user in the URL, do we have *user-HOST* or *user*? */
917 if (!urlp
->url_had_user
) {
918 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
920 /* No, check wether .netrc lookup is desired */
922 if (!ok_blook(v15_compat
) ||
923 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
924 !_nrc_lookup(urlp
, FAL0
))
926 urlp
->url_user
.s
= UNCONST(myname
);
929 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
930 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
931 urlp
->url_user_enc
.l
= strlen(
932 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
935 /* And then there are a lot of prebuild string combinations TODO do lazy */
937 /* .url_u_h: .url_user@.url_host
938 * For SMTP we apply ridiculously complicated *v15-compat* plus
939 * *smtp-hostname* / *hostname* dependent rules */
943 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
944 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
947 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
952 i
= urlp
->url_user
.l
;
954 s
->s
= salloc(i
+ 1 + h
.l
+1);
956 memcpy(s
->s
, urlp
->url_user
.s
, i
);
959 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
964 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
965 { struct str
*s
= &urlp
->url_u_h_p
;
966 size_t i
= urlp
->url_user
.l
;
968 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
970 memcpy(s
->s
, urlp
->url_user
.s
, i
);
973 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
974 i
+= urlp
->url_h_p
.l
;
978 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
979 { struct str
*s
= &urlp
->url_eu_h_p
;
980 size_t i
= urlp
->url_user_enc
.l
;
982 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
984 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
987 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
988 i
+= urlp
->url_h_p
.l
;
992 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
994 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
996 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
997 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
998 urlp
->url_u_h_p
.l
+1);
999 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1001 urlp
->url_p_u_h_p
= ud
;
1004 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1006 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1007 1 + urlp
->url_path
.l
+1);
1009 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1010 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1011 urlp
->url_eu_h_p
.l
+1);
1012 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1014 if (urlp
->url_path
.l
== 0)
1015 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1017 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1018 urlp
->url_p_eu_h_p_p
= ud
;
1021 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1026 #endif /* __ANYPROTO */
1035 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1037 char const *pname
, *pxstr
, *authdef
;
1038 size_t pxlen
, addrlen
, i
;
1041 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1043 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1046 memset(ccp
, 0, sizeof *ccp
);
1052 pxstr
= "smtp-auth";
1053 pxlen
= sizeof("smtp-auth") -1;
1054 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1055 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1057 addr_is_nuser
= TRU1
;
1061 pxstr
= "pop3-auth";
1062 pxlen
= sizeof("pop3-auth") -1;
1063 authmask
= AUTHTYPE_PLAIN
;
1068 pxstr
= "imap-auth";
1069 pxlen
= sizeof("imap-auth") -1;
1070 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1075 ccp
->cc_cproto
= cproto
;
1076 addrlen
= strlen(addr
);
1077 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1078 memcpy(vbuf
, pxstr
, pxlen
);
1080 /* Authentication type */
1082 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1083 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1085 if ((s
= vok_vlook(vbuf
)) == NULL
)
1086 s
= UNCONST(authdef
);
1089 if (!asccasecmp(s
, "none")) {
1090 ccp
->cc_auth
= "NONE";
1091 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1093 } else if (!asccasecmp(s
, "plain")) {
1094 ccp
->cc_auth
= "PLAIN";
1095 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1096 ware
= REQ_PASS
| REQ_USER
;
1097 } else if (!asccasecmp(s
, "login")) {
1098 ccp
->cc_auth
= "LOGIN";
1099 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1100 ware
= REQ_PASS
| REQ_USER
;
1101 } else if (!asccasecmp(s
, "cram-md5")) {
1102 ccp
->cc_auth
= "CRAM-MD5";
1103 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1104 ware
= REQ_PASS
| REQ_USER
;
1105 } else if (!asccasecmp(s
, "gssapi")) {
1106 ccp
->cc_auth
= "GSS-API";
1107 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1112 if (!(ccp
->cc_authtype
& authmask
)) {
1113 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1118 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1119 n_err(_("No CRAM-MD5 support compiled in\n"));
1124 # ifndef HAVE_GSSAPI
1125 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1126 n_err(_("No GSS-API support compiled in\n"));
1133 if (!(ware
& (WANT_USER
| REQ_USER
)))
1136 if (!addr_is_nuser
) {
1137 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1138 ccp
->cc_user
.s
= urlxdec(savestrbuf(addr
, PTR2SIZE(s
- addr
)));
1139 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1140 } else if (ware
& REQ_USER
)
1145 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1147 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1148 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1150 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1151 if ((s
= getuser(NULL
)) == NULL
) {
1152 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1153 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1154 * TODO check that first, then! change control flow, grow vbuf */
1155 n_err(_("A user is necessary for %s authentication\n"), pname
);
1161 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1165 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1168 if (!addr_is_nuser
) {
1169 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1171 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1174 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1175 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1177 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1178 (ware
& REQ_PASS
)) {
1179 if ((s
= getpassword(NULL
)) == NULL
) {
1180 n_err(_("A password is necessary for %s authentication\n"),
1188 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1192 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1193 n_err(_("Credentials: host \"%s\", user \"%s\", pass \"%s\"\n"),
1194 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1195 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1197 return (ccp
!= NULL
);
1201 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1203 char const *pstr
, *authdef
;
1205 enum okeys authokey
;
1207 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1211 memset(ccp
, 0, sizeof *ccp
);
1212 ccp
->cc_user
= urlp
->url_user
;
1214 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1218 authokey
= ok_v_smtp_auth
;
1219 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1220 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1225 authokey
= ok_v_pop3_auth
;
1226 authmask
= AUTHTYPE_PLAIN
;
1231 authokey
= ok_v_imap_auth
;
1232 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1237 /* Authentication type */
1238 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1239 s
= UNCONST(authdef
);
1241 if (!asccasecmp(s
, "none")) {
1242 ccp
->cc_auth
= "NONE";
1243 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1245 } else if (!asccasecmp(s
, "plain")) {
1246 ccp
->cc_auth
= "PLAIN";
1247 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1248 ware
= REQ_PASS
| REQ_USER
;
1249 } else if (!asccasecmp(s
, "login")) {
1250 ccp
->cc_auth
= "LOGIN";
1251 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1252 ware
= REQ_PASS
| REQ_USER
;
1253 } else if (!asccasecmp(s
, "cram-md5")) {
1254 ccp
->cc_auth
= "CRAM-MD5";
1255 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1256 ware
= REQ_PASS
| REQ_USER
;
1257 } else if (!asccasecmp(s
, "gssapi")) {
1258 ccp
->cc_auth
= "GSS-API";
1259 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1264 if (!(ccp
->cc_authtype
& authmask
)) {
1265 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1270 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1271 n_err(_("No CRAM-MD5 support compiled in\n"));
1276 # ifndef HAVE_GSSAPI
1277 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1278 n_err(_("No GSS-API support compiled in\n"));
1285 ccp
->cc_pass
= urlp
->url_pass
;
1286 if (ccp
->cc_pass
.s
!= NULL
)
1289 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1292 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1293 if (!_agent_shell_lookup(urlp
, s
)) {
1296 } else if (urlp
->url_pass
.s
!= NULL
) {
1297 ccp
->cc_pass
= urlp
->url_pass
;
1303 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1304 ccp
->cc_pass
= urlp
->url_pass
;
1308 if (ware
& REQ_PASS
) {
1309 if ((s
= getpassword(NULL
)) != NULL
)
1311 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1313 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1319 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1320 n_err(_("Credentials: host \"%s\", user \"%s\", pass \"%s\"\n"),
1321 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1322 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1324 return (ccp
!= NULL
);
1326 #endif /* HAVE_SOCKETS */
1333 struct nrc_node
*nrc
;
1338 if (argv
[1] != NULL
)
1340 if (!asccasecmp(*argv
, "show"))
1342 if (!asccasecmp(*argv
, "clear"))
1345 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1349 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1355 if (_nrc_list
== NULL
)
1357 if (_nrc_list
== NRC_NODE_ERR
) {
1358 n_err(_("Interpolate what file?\n"));
1363 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)
1365 n_perr(_("tmpfile"), 0);
1370 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1371 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
);
1372 if (nrc
->nrc_ulen
> 0)
1373 fprintf(fp
, _("login \"%s\" "),
1374 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1375 if (nrc
->nrc_plen
> 0)
1376 fprintf(fp
, _("password \"%s\"\n"),
1377 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1382 page_or_print(fp
, l
);
1388 if (_nrc_list
== NRC_NODE_ERR
)
1390 while ((nrc
= _nrc_list
) != NULL
) {
1391 _nrc_list
= nrc
->nrc_next
;
1396 #endif /* HAVE_NETRC */
1400 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1402 char const *cp
= vp
;
1406 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1408 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1409 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1410 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1418 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1422 char digest
[16], *cp
;
1426 in
.s
= UNCONST(b64
);
1427 in
.l
= strlen(in
.s
);
1428 b64_decode(&out
, &in
, NULL
);
1429 assert(out
.s
!= NULL
);
1431 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1433 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1435 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1436 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1437 memcpy(in
.s
, user
->s
, user
->l
);
1438 in
.s
[user
->l
] = ' ';
1439 memcpy(in
.s
+ user
->l
+ 1, cp
, MD5TOHEX_SIZE
);
1440 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1447 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1451 * This code is taken from
1453 * Network Working Group H. Krawczyk
1454 * Request for Comments: 2104 IBM
1455 * Category: Informational M. Bellare
1462 * HMAC: Keyed-Hashing for Message Authentication
1465 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1466 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1467 unsigned char tk
[16];
1471 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1476 md5_update(&tctx
, key
, key_len
);
1477 md5_final(tk
, &tctx
);
1483 /* the HMAC_MD5 transform looks like:
1485 * MD5(K XOR opad, MD5(K XOR ipad, text))
1487 * where K is an n byte key
1488 * ipad is the byte 0x36 repeated 64 times
1489 * opad is the byte 0x5c repeated 64 times
1490 * and text is the data being protected */
1492 /* start out by storing key in pads */
1493 memset(k_ipad
, 0, sizeof k_ipad
);
1494 memset(k_opad
, 0, sizeof k_opad
);
1495 memcpy(k_ipad
, key
, key_len
);
1496 memcpy(k_opad
, key
, key_len
);
1498 /* XOR key with ipad and opad values */
1499 for (i
=0; i
<64; i
++) {
1504 /* perform inner MD5 */
1505 md5_init(&context
); /* init context for 1st pass */
1506 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1507 md5_update(&context
, text
, text_len
); /* then text of datagram */
1508 md5_final(digest
, &context
); /* finish up 1st pass */
1510 /* perform outer MD5 */
1511 md5_init(&context
); /* init context for 2nd pass */
1512 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1513 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1514 md5_final(digest
, &context
); /* finish up 2nd pass */
1517 #endif /* HAVE_MD5 */