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
[VFIELD_SIZE(sizeof(ui32_t
))];
52 # define NRC_NODE_ERR ((struct nrc_node*)-1)
54 static struct nrc_node
*_nrc_list
;
55 #endif /* HAVE_NETRC */
57 /* Find the last @ before a slash
58 * TODO Casts off the const but this is ok here; obsolete function! */
59 #ifdef HAVE_SOCKETS /* temporary (we'll have file://..) */
60 static char * _url_last_at_before_slash(char const *sp
);
64 /* Initialize .netrc cache */
65 static void _nrc_init(void);
66 static enum nrc_token
__nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
],
69 /* We shall lookup a machine in .netrc says ok_blook(netrc_lookup).
70 * only_pass is true then the lookup is for the password only, otherwise we
71 * look for a user (and add password only if we have an exact machine match) */
72 static bool_t
_nrc_lookup(struct url
*urlp
, bool_t only_pass
);
74 /* 0=no match; 1=exact match; -1=wildcard match */
75 static int __nrc_host_match(struct nrc_node
const *nrc
,
76 struct url
const *urlp
);
77 static bool_t
__nrc_find_user(struct url
*urlp
,
78 struct nrc_node
const *nrc
);
79 static bool_t
__nrc_find_pass(struct url
*urlp
, bool_t user_match
,
80 struct nrc_node
const *nrc
);
81 #endif /* HAVE_NETRC */
83 /* The password can also be gained through external agents TODO v15-compat */
85 static bool_t
_agent_shell_lookup(struct url
*urlp
, char const *comm
);
90 _url_last_at_before_slash(char const *sp
)
96 for (cp
= sp
; (c
= *cp
) != '\0'; ++cp
)
99 while (cp
> sp
&& *--cp
!= '@')
113 char buffer
[NRC_TOKEN_MAXLEN
], host
[NRC_TOKEN_MAXLEN
],
114 user
[NRC_TOKEN_MAXLEN
], pass
[NRC_TOKEN_MAXLEN
], *netrc_load
;
118 bool_t ispipe
, seen_default
, nl_last
;
119 struct nrc_node
*ntail
, *nhead
, *nrc
;
128 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
135 if ((netrc_load
= ok_vlook(netrc_pipe
)) != NULL
) {
137 if ((fi
= Popen(netrc_load
, "r", ok_vlook(SHELL
), NULL
, COMMAND_FD_NULL
)
139 n_perr(netrc_load
, 0);
143 if ((netrc_load
= file_expand(ok_vlook(NETRC
))) == NULL
)
146 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
147 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load
, FAL0
));
151 /* Be simple and apply rigid (permission) check(s) */
152 if (fstat(fileno(fi
), &sb
) == -1 || !S_ISREG(sb
.st_mode
) ||
153 (sb
.st_mode
& (S_IRWXG
| S_IRWXO
))) {
154 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
155 n_shexp_quote_cp(netrc_load
, FAL0
));
163 switch((t
= __nrc_token(fi
, buffer
, &nl_last
))) {
166 default: /* Doesn't happen (but on error?), keep CC happy */
169 /* We ignore the default entry (require an exact host match), and we also
170 * ignore anything after such an entry (faulty syntax) */
175 /* Normalize HOST to lowercase */
177 if (!seen_default
&& (t
= __nrc_token(fi
, host
, &nl_last
)) != NRC_INPUT
)
181 for (cp
= host
; *cp
!= '\0'; ++cp
)
182 *cp
= lowerconv(*cp
);
185 *user
= *pass
= '\0';
186 while ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_NONE
&&
187 t
!= NRC_MACHINE
&& t
!= NRC_DEFAULT
) {
190 if ((t
= __nrc_token(fi
, user
, &nl_last
)) != NRC_INPUT
)
194 if ((t
= __nrc_token(fi
, pass
, &nl_last
)) != NRC_INPUT
)
198 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
202 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
206 while ((c
= getc(fi
)) != EOF
)
207 if (c
== '\n') { /* xxx */
208 /* Don't care about comments here, since we parse until
209 * we've seen two successive newline characters */
223 if (!seen_default
&& (*user
!= '\0' || *pass
!= '\0')) {
224 size_t hl
= strlen(host
), ul
= strlen(user
), pl
= strlen(pass
);
225 struct nrc_node
*nx
= smalloc(sizeof(*nx
) -
226 VFIELD_SIZEOF(struct nrc_node
, nrc_dat
) + hl
+1 + ul
+1 + pl
+1);
229 ntail
->nrc_next
= nx
;
237 memcpy(nx
->nrc_dat
, host
, ++hl
);
238 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
239 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
241 if (t
== NRC_MACHINE
)
243 if (t
== NRC_DEFAULT
)
250 if (options
& OPT_D_V
)
251 n_err(_("Errors occurred while parsing %s\n"),
252 n_shexp_quote_cp(netrc_load
, FAL0
));
253 assert(nrc
== NRC_NODE_ERR
);
259 n_sigman_cleanup_ping(&sm
);
267 if (nrc
== NRC_NODE_ERR
)
268 while (nhead
!= NULL
) {
270 nhead
= nhead
->nrc_next
;
276 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
279 static enum nrc_token
280 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
], bool_t
*nl_last
)
292 if (feof(fi
) || ferror(fi
))
295 for (seen_nl
= *nl_last
; (c
= getc(fi
)) != EOF
&& whitechar(c
);)
296 seen_nl
|= (c
== '\n');
300 /* fetchmail and derived parsers support comments */
301 if ((*nl_last
= seen_nl
) && c
== '#') {
302 while ((c
= getc(fi
)) != EOF
&& c
!= '\n')
310 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
311 if (c
== '"' || c
== '\'') {
314 /* Not requiring the closing QM is (Net)BSD syntax */
315 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
316 /* Backslash escaping the next character is (Net)BSD syntax */
318 if ((c
= getc(fi
)) == EOF
)
321 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
328 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
329 /* Backslash escaping the next character is (Net)BSD syntax */
330 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
333 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
338 *nl_last
= (c
== '\n');
343 do {/*rv = NRC_NONE*/} while (0);
344 else if (!strcmp(buffer
, "default"))
346 else if (!strcmp(buffer
, "login"))
348 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
350 else if (!strcmp(buffer
, "account"))
352 else if (!strcmp(buffer
, "macdef"))
354 else if (!strcmp(buffer
, "machine"))
359 if (c
== EOF
&& !feof(fi
))
366 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
368 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
372 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
373 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
375 if (_nrc_list
== NULL
)
377 if (_nrc_list
== NRC_NODE_ERR
)
380 nrc_wild
= nrc_exact
= NULL
;
381 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
382 switch (__nrc_host_match(nrc
, urlp
)) {
384 nrc
->nrc_result
= nrc_exact
;
388 nrc
->nrc_result
= nrc_wild
;
395 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
396 /* Must be an unambiguous entry of its kind */
397 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
399 if (__nrc_find_user(urlp
, nrc_exact
))
402 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
404 if (!__nrc_find_user(urlp
, nrc_wild
))
410 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
411 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
412 /* Do not try to find a password without exact user match unless we've
413 * been called during credential lookup, a.k.a. the second time */
415 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
416 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
424 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
431 /* Find a matching machine -- entries are all lowercase normalized */
432 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
433 if (LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
438 /* Cannot be an exact match, but maybe the .netrc machine starts with
439 * a "*." glob, which we recognize as an extension, meaning "skip
440 * a single subdomain, then match the rest" */
441 d1
= nrc
->nrc_dat
+ 2;
443 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
447 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
448 * in fact this even succeeds for ".host.com", but - why care, here? */
449 d2
= urlp
->url_host
.s
;
450 l2
= urlp
->url_host
.l
;
457 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
458 /* This matches, but we won't use it directly but watch out for an
459 * exact match first! */
467 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
471 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
472 if (nrc
->nrc_ulen
> 0) {
473 /* Fake it was part of URL otherwise XXX */
474 urlp
->url_had_user
= TRU1
;
475 /* That buffer will be duplicated by url_parse() in this case! */
476 urlp
->url_user
.s
= UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
477 urlp
->url_user
.l
= nrc
->nrc_ulen
;
482 return (nrc
!= NULL
);
486 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
490 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
491 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
492 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
498 } else if (!um
&& nrc
->nrc_ulen
> 0)
500 if (nrc
->nrc_plen
== 0)
503 /* We are responsible for duplicating this buffer! */
504 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
505 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
510 return (nrc
!= NULL
);
512 #endif /* HAVE_NETRC */
516 _agent_shell_lookup(struct url
*urlp
, char const *comm
) /* TODO v15-compat */
519 char const *env_addon
[8];
522 union {char const *cp
; int c
; sighandler_type sht
;} u
;
527 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
529 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
531 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
533 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
535 u
.cp
= ok_vlook(TMPDIR
);
536 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", u
.cp
, NULL
)->s
;
537 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", u
.cp
, NULL
)->s
;
541 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
542 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm
);
546 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
547 if (u
.c
== '\n') /* xxx */
550 if (l
== sizeof(buf
) - 1) {
551 n_str_add_buf(&s
, buf
, l
);
556 n_str_add_buf(&s
, buf
, l
);
558 if (!Pclose(pbuf
, TRU1
)) {
559 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm
);
563 /* We are responsible for duplicating this buffer! */
565 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
567 urlp
->url_pass
.s
= UNCONST(""), urlp
->url_pass
.l
= 0;
575 #endif /* HAVE_AGENT */
578 (urlxenc
)(char const *cp
, bool_t ispath SALLOC_DEBUG_ARGS
)
583 np
= n
= (salloc
)(strlen(cp
) * 3 +1 SALLOC_DEBUG_ARGSCALL
);
585 for (; (c1
= *cp
) != '\0'; ++cp
) {
586 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
587 * ALPHA / DIGIT / "-" / "." / "_" / "~"
588 * However add a special is[file]path mode for file-system friendliness */
589 if (alnumchar(c1
) || c1
== '_')
592 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
595 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
600 n_c_to_hex_base16(np
+ 1, c1
);
610 (urlxdec
)(char const *cp SALLOC_DEBUG_ARGS
)
616 np
= n
= (salloc
)(strlen(cp
) +1 SALLOC_DEBUG_ARGSCALL
);
618 while ((c
= (uc_i
)*cp
++) != '\0') {
619 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
621 if (LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
634 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
637 char const *mailtop_orig
;
640 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
644 mailtop
+= sizeof("mailto:") -1;
646 /* TODO This is all intermediate, and for now just enough to understand
647 * TODO a little bit of a little more advanced List-Post: headers. */
648 /* Strip any hfield additions, keep only to addr-spec's */
649 if((rv
= strchr(mailtop
, '?')) != NULL
)
650 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
652 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
656 /* Simply perform percent-decoding if there is a percent % */
657 if(memchr(rv
, '%', i
) != NULL
){
661 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
664 if((c
= *mailtop
++) == '%'){
667 if(i
< 3 || (cc
= n_c_from_hex_base16(mailtop
)) < 0){
668 if(!err
&& (err
= TRU1
, options
& OPT_D_V
))
669 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
670 n_shexp_quote_cp(mailtop_orig
, FAL0
));
691 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
698 { "submission", "587", 587},
699 { "smtps", "465", 465},
700 { "pop3", "110", 110},
701 { "pop3s", "995", 995},
702 { "imap", "143", 143},
703 { "imaps", "993", 993},
710 for(rv
= proto
; *rv
!= '\0'; ++rv
)
713 l
= PTR2SIZE(rv
- proto
);
715 for(rv
= NULL
, i
= 0; i
< NELEM(tbl
); ++i
)
716 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
718 if(irv_or_null
!= NULL
)
719 *irv_or_null
= tbl
[i
].portno
;
726 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
728 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
730 #if defined HAVE_SMTP && defined HAVE_POP3
733 #if defined HAVE_SMTP || defined HAVE_POP3
741 memset(urlp
, 0, sizeof *urlp
);
742 urlp
->url_input
= data
;
743 urlp
->url_cproto
= cproto
;
745 /* Network protocol */
746 #define _protox(X,Y) \
747 urlp->url_portno = Y;\
748 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
749 urlp->url_proto[sizeof(X) -1] = '\0';\
750 urlp->url_proto_len = sizeof(X) -1;\
751 urlp->url_proto_xlen = sizeof(X "://") -1
752 #define __if(X,Y,Z) \
753 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
755 data += sizeof(X "://") -1;\
756 do { Z; } while (0);\
759 #define _if(X,Y) __if(X, Y, (void)0)
761 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
763 # define _ifs(X,Y) goto jeproto;
770 _if ("submission", 587)
781 _protox("pop3", 110);
790 _protox("imap", 143);
801 if (strstr(data
, "://") != NULL
) {
802 #if !defined __ALLPROTO || !defined HAVE_SSL
805 n_err(_("URL proto:// prefix invalid: %s\n"), urlp
->url_input
);
810 /* User and password, I */
812 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
813 size_t l
= PTR2SIZE(cp
- data
);
814 char const *d
= data
;
815 char *ub
= ac_alloc(l
+1);
817 urlp
->url_had_user
= TRU1
;
820 /* And also have a password? */
821 if ((cp
= memchr(d
, ':', l
)) != NULL
) {
822 size_t i
= PTR2SIZE(cp
- d
);
825 memcpy(ub
, cp
+ 1, l
);
827 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(ub
));
829 if (strcmp(ub
, urlxenc(urlp
->url_pass
.s
, FAL0
))) {
830 n_err(_("String is not properly URL percent encoded: %s\n"),
839 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(ub
));
840 urlp
->url_user_enc
.l
= strlen(
841 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
843 if (urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)) {
844 n_err(_("String is not properly URL percent encoded: %s\n"), ub
);
851 /* Servername and port -- and possible path suffix */
852 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
856 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
857 if ((x
= strchr(x
, '/')) != NULL
) {
862 l
= strtol(urlp
->url_port
, &eptr
, 10);
863 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
864 n_err(_("URL with invalid port number: %s\n"), urlp
->url_input
);
867 urlp
->url_portno
= (ui16_t
)l
;
869 if ((x
= strchr(data
, '/')) != NULL
) {
870 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
874 cp
= UNCONST(data
+ strlen(data
));
877 /* A (non-empty) path may only occur with IMAP */
878 if (x
!= NULL
&& *x
!= '\0') {
879 /* Take care not to count adjacent slashes for real, on either end */
883 for(x2
= savestrbuf(x
, i
= strlen(x
)); i
> 0; --i
)
890 if (cproto
!= CPROTO_IMAP
) {
892 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
897 urlp
->url_path
.l
= i
;
898 urlp
->url_path
.s
= x2
;
903 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
905 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
906 *cp
= lowerconv(*cp
);
909 /* .url_h_p: HOST:PORT */
911 struct str
*s
= &urlp
->url_h_p
;
913 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
914 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
915 if (urlp
->url_port
!= NULL
) {
916 size_t j
= strlen(urlp
->url_port
);
918 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
926 * If there was no user in the URL, do we have *user-HOST* or *user*? */
927 if (!urlp
->url_had_user
) {
928 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
930 /* No, check whether .netrc lookup is desired */
932 if (!ok_blook(v15_compat
) ||
933 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
934 !_nrc_lookup(urlp
, FAL0
))
936 urlp
->url_user
.s
= UNCONST(myname
);
939 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
940 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
941 urlp
->url_user_enc
.l
= strlen(
942 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
945 /* And then there are a lot of prebuild string combinations TODO do lazy */
947 /* .url_u_h: .url_user@.url_host
948 * For SMTP we apply ridiculously complicated *v15-compat* plus
949 * *smtp-hostname* / *hostname* dependent rules */
953 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
954 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
957 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
962 i
= urlp
->url_user
.l
;
964 s
->s
= salloc(i
+ 1 + h
.l
+1);
966 memcpy(s
->s
, urlp
->url_user
.s
, i
);
969 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
974 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
975 { struct str
*s
= &urlp
->url_u_h_p
;
976 size_t i
= urlp
->url_user
.l
;
978 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
980 memcpy(s
->s
, urlp
->url_user
.s
, i
);
983 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
984 i
+= urlp
->url_h_p
.l
;
988 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
989 { struct str
*s
= &urlp
->url_eu_h_p
;
990 size_t i
= urlp
->url_user_enc
.l
;
992 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
994 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
997 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
998 i
+= urlp
->url_h_p
.l
;
1002 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1004 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
1006 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1007 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
1008 urlp
->url_u_h_p
.l
+1);
1009 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1011 urlp
->url_p_u_h_p
= ud
;
1014 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1016 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1017 1 + urlp
->url_path
.l
+1);
1019 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1020 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1021 urlp
->url_eu_h_p
.l
+1);
1022 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1024 if (urlp
->url_path
.l
== 0)
1025 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1027 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1028 urlp
->url_p_eu_h_p_p
= ud
;
1031 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1036 #endif /* __ANYPROTO */
1045 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1047 char const *pname
, *pxstr
, *authdef
;
1048 size_t pxlen
, addrlen
, i
;
1051 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1053 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1056 memset(ccp
, 0, sizeof *ccp
);
1062 pxstr
= "smtp-auth";
1063 pxlen
= sizeof("smtp-auth") -1;
1064 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1065 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1067 addr_is_nuser
= TRU1
;
1071 pxstr
= "pop3-auth";
1072 pxlen
= sizeof("pop3-auth") -1;
1073 authmask
= AUTHTYPE_PLAIN
;
1078 ccp
->cc_cproto
= cproto
;
1079 addrlen
= strlen(addr
);
1080 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1081 memcpy(vbuf
, pxstr
, pxlen
);
1083 /* Authentication type */
1085 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1086 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1088 if ((s
= vok_vlook(vbuf
)) == NULL
)
1089 s
= UNCONST(authdef
);
1092 if (!asccasecmp(s
, "none")) {
1093 ccp
->cc_auth
= "NONE";
1094 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1096 } else if (!asccasecmp(s
, "plain")) {
1097 ccp
->cc_auth
= "PLAIN";
1098 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1099 ware
= REQ_PASS
| REQ_USER
;
1100 } else if (!asccasecmp(s
, "login")) {
1101 ccp
->cc_auth
= "LOGIN";
1102 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1103 ware
= REQ_PASS
| REQ_USER
;
1104 } else if (!asccasecmp(s
, "cram-md5")) {
1105 ccp
->cc_auth
= "CRAM-MD5";
1106 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1107 ware
= REQ_PASS
| REQ_USER
;
1108 } else if (!asccasecmp(s
, "gssapi")) {
1109 ccp
->cc_auth
= "GSS-API";
1110 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1115 if (!(ccp
->cc_authtype
& authmask
)) {
1116 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1121 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1122 n_err(_("No CRAM-MD5 support compiled in\n"));
1127 # ifndef HAVE_GSSAPI
1128 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1129 n_err(_("No GSS-API support compiled in\n"));
1136 if (!(ware
& (WANT_USER
| REQ_USER
)))
1139 if (!addr_is_nuser
) {
1140 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1141 ccp
->cc_user
.s
= urlxdec(savestrbuf(addr
, PTR2SIZE(s
- addr
)));
1142 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1143 } else if (ware
& REQ_USER
)
1148 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1150 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1151 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1153 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1154 if ((s
= getuser(NULL
)) == NULL
) {
1155 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1156 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1157 * TODO check that first, then! change control flow, grow vbuf */
1158 n_err(_("A user is necessary for %s authentication\n"), pname
);
1164 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1168 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1171 if (!addr_is_nuser
) {
1172 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1174 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1177 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1178 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1180 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1181 (ware
& REQ_PASS
)) {
1182 if ((s
= getpassword(NULL
)) == NULL
) {
1183 n_err(_("A password is necessary for %s authentication\n"),
1191 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1195 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1196 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1197 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1198 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1200 return (ccp
!= NULL
);
1204 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1206 char const *pstr
, *authdef
;
1208 enum okeys authokey
;
1210 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1214 memset(ccp
, 0, sizeof *ccp
);
1215 ccp
->cc_user
= urlp
->url_user
;
1217 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1221 authokey
= ok_v_smtp_auth
;
1222 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1223 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1228 authokey
= ok_v_pop3_auth
;
1229 authmask
= AUTHTYPE_PLAIN
;
1234 /* Authentication type */
1235 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1236 s
= UNCONST(authdef
);
1238 if (!asccasecmp(s
, "none")) {
1239 ccp
->cc_auth
= "NONE";
1240 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1242 } else if (!asccasecmp(s
, "plain")) {
1243 ccp
->cc_auth
= "PLAIN";
1244 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1245 ware
= REQ_PASS
| REQ_USER
;
1246 } else if (!asccasecmp(s
, "login")) {
1247 ccp
->cc_auth
= "LOGIN";
1248 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1249 ware
= REQ_PASS
| REQ_USER
;
1250 } else if (!asccasecmp(s
, "cram-md5")) {
1251 ccp
->cc_auth
= "CRAM-MD5";
1252 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1253 ware
= REQ_PASS
| REQ_USER
;
1254 } else if (!asccasecmp(s
, "gssapi")) {
1255 ccp
->cc_auth
= "GSS-API";
1256 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1261 if (!(ccp
->cc_authtype
& authmask
)) {
1262 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1267 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1268 n_err(_("No CRAM-MD5 support compiled in\n"));
1273 # ifndef HAVE_GSSAPI
1274 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1275 n_err(_("No GSS-API support compiled in\n"));
1282 ccp
->cc_pass
= urlp
->url_pass
;
1283 if (ccp
->cc_pass
.s
!= NULL
)
1286 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1288 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1289 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1290 OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1291 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1292 if (!_agent_shell_lookup(urlp
, s
)) {
1295 } else if (urlp
->url_pass
.s
!= NULL
) {
1296 ccp
->cc_pass
= urlp
->url_pass
;
1302 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1303 ccp
->cc_pass
= urlp
->url_pass
;
1307 if (ware
& REQ_PASS
) {
1308 if ((s
= getpassword(NULL
)) != NULL
)
1310 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1312 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1318 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1319 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1320 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1321 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1323 return (ccp
!= NULL
);
1325 #endif /* HAVE_SOCKETS */
1332 struct nrc_node
*nrc
;
1339 if (argv
[1] != NULL
)
1341 if (!asccasecmp(*argv
, "show"))
1344 if (!asccasecmp(*argv
, "load"))
1346 if (!asccasecmp(*argv
, "clear"))
1349 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1353 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1359 if (_nrc_list
== NULL
)
1361 if (_nrc_list
== NRC_NODE_ERR
) {
1362 n_err(_("Interpolate what file?\n"));
1369 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1370 n_perr(_("tmpfile"), 0);
1375 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1376 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
); /* XXX quote? */
1377 if (nrc
->nrc_ulen
> 0)
1378 fprintf(fp
, _("login \"%s\" "),
1379 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1380 if (nrc
->nrc_plen
> 0)
1381 fprintf(fp
, _("password \"%s\"\n"),
1382 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1387 page_or_print(fp
, l
);
1393 if (_nrc_list
== NRC_NODE_ERR
)
1395 while ((nrc
= _nrc_list
) != NULL
) {
1396 _nrc_list
= nrc
->nrc_next
;
1401 #endif /* HAVE_NETRC */
1405 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1407 char const *cp
= vp
;
1411 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1413 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1414 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1415 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1423 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1427 char digest
[16], *cp
;
1431 in
.s
= UNCONST(b64
);
1432 in
.l
= strlen(in
.s
);
1433 b64_decode(&out
, &in
, NULL
);
1434 assert(out
.s
!= NULL
);
1436 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1438 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1440 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1441 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1442 memcpy(in
.s
, user
->s
, user
->l
);
1443 in
.s
[user
->l
] = ' ';
1444 memcpy(in
.s
+ user
->l
+ 1, cp
, MD5TOHEX_SIZE
);
1445 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1452 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1456 * This code is taken from
1458 * Network Working Group H. Krawczyk
1459 * Request for Comments: 2104 IBM
1460 * Category: Informational M. Bellare
1467 * HMAC: Keyed-Hashing for Message Authentication
1470 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1471 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1472 unsigned char tk
[16];
1476 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1481 md5_update(&tctx
, key
, key_len
);
1482 md5_final(tk
, &tctx
);
1488 /* the HMAC_MD5 transform looks like:
1490 * MD5(K XOR opad, MD5(K XOR ipad, text))
1492 * where K is an n byte key
1493 * ipad is the byte 0x36 repeated 64 times
1494 * opad is the byte 0x5c repeated 64 times
1495 * and text is the data being protected */
1497 /* start out by storing key in pads */
1498 memset(k_ipad
, 0, sizeof k_ipad
);
1499 memset(k_opad
, 0, sizeof k_opad
);
1500 memcpy(k_ipad
, key
, key_len
);
1501 memcpy(k_opad
, key
, key_len
);
1503 /* XOR key with ipad and opad values */
1504 for (i
=0; i
<64; i
++) {
1509 /* perform inner MD5 */
1510 md5_init(&context
); /* init context for 1st pass */
1511 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1512 md5_update(&context
, text
, text_len
); /* then text of datagram */
1513 md5_final(digest
, &context
); /* finish up 1st pass */
1515 /* perform outer MD5 */
1516 md5_init(&context
); /* init context for 2nd pass */
1517 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1518 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1519 md5_final(digest
, &context
); /* finish up 2nd pass */
1522 #endif /* HAVE_MD5 */