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 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 #ifndef HAVE_AMALGAMATION
26 /* NetBSD usr.bin/ftp/ruserpass.c uses 100 bytes for that, we need four
27 * concurrently (dummy, host, user, pass), so make it a KB */
28 # define NRC_TOKEN_MAXLEN (1024 / 4)
43 struct nrc_node
*nrc_next
;
44 struct nrc_node
*nrc_result
; /* In match phase, former possible one */
45 ui32_t nrc_mlen
; /* Length of machine name */
46 ui32_t nrc_ulen
; /* Length of user name */
47 ui32_t nrc_plen
; /* Length of password */
48 char nrc_dat
[VFIELD_SIZE(4)];
50 # define NRC_NODE_ERR ((struct nrc_node*)-1)
52 static struct nrc_node
*_nrc_list
;
53 #endif /* HAVE_NETRC */
55 /* Find the last @ before a slash
56 * TODO Casts off the const but this is ok here; obsolete function! */
57 #ifdef HAVE_SOCKETS /* temporary (we'll have file://..) */
58 static char * _url_last_at_before_slash(char const *sp
);
62 /* Initialize .netrc cache */
63 static void _nrc_init(void);
64 static enum nrc_token
__nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
]);
66 /* We shall lookup a machine in .netrc says ok_blook(netrc_lookup).
67 * only_pass is true then the lookup is for the password only, otherwise we
68 * look for a user (and add password only if we have an exact machine match) */
69 static bool_t
_nrc_lookup(struct url
*urlp
, bool_t only_pass
);
71 /* 0=no match; 1=exact match; -1=wildcard match */
72 static int __nrc_host_match(struct nrc_node
const *nrc
,
73 struct url
const *urlp
);
74 static bool_t
__nrc_find_user(struct url
*urlp
,
75 struct nrc_node
const *nrc
);
76 static bool_t
__nrc_find_pass(struct url
*urlp
, bool_t user_match
,
77 struct nrc_node
const *nrc
);
78 #endif /* HAVE_NETRC */
80 /* The password can also be gained through external agents */
82 static bool_t
_agent_shell_lookup(struct url
*urlp
, char const *comm
);
87 _url_last_at_before_slash(char const *sp
)
93 for (cp
= sp
; (c
= *cp
) != '\0'; ++cp
)
96 while (cp
> sp
&& *--cp
!= '@')
109 char buffer
[NRC_TOKEN_MAXLEN
], host
[NRC_TOKEN_MAXLEN
],
110 user
[NRC_TOKEN_MAXLEN
], pass
[NRC_TOKEN_MAXLEN
], *netrc_load
;
115 struct nrc_node
*ntail
= NULL
/* CC happy */, *nhead
= NULL
,
119 if ((netrc_load
= getenv("NETRC")/* TODO */) == NULL
)
120 netrc_load
= UNCONST(NETRC
);
121 if ((netrc_load
= file_expand(netrc_load
)) == NULL
)
124 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
125 fprintf(stderr
, _("Cannot open `%s'\n"), netrc_load
);
129 /* Be simple and apply rigid (permission) check(s) */
130 if (fstat(fileno(fi
), &sb
) == -1 || !S_ISREG(sb
.st_mode
) ||
131 (sb
.st_mode
& (S_IRWXG
| S_IRWXO
))) {
133 _("Not a regular file, or accessible by non-user: `%s'\n"),
140 switch((t
= __nrc_token(fi
, buffer
))) {
143 default: /* Doesn't happen (but on error?), keep CC happy */
146 /* We ignore the default entry (require an exact host match), and we also
147 * ignore anything after such an entry (faulty syntax) */
152 /* Normalize HOST to lowercase */
154 if (!seen_default
&& (t
= __nrc_token(fi
, host
)) != NRC_INPUT
)
158 for (cp
= host
; *cp
!= '\0'; ++cp
)
159 *cp
= lowerconv(*cp
);
162 *user
= *pass
= '\0';
163 while ((t
= __nrc_token(fi
, buffer
)) != NRC_NONE
&& t
!= NRC_MACHINE
&&
167 if ((t
= __nrc_token(fi
, user
)) != NRC_INPUT
)
171 if ((t
= __nrc_token(fi
, pass
)) != NRC_INPUT
)
175 if ((t
= __nrc_token(fi
, buffer
)) != NRC_INPUT
)
179 if ((t
= __nrc_token(fi
, buffer
)) != NRC_INPUT
)
183 while ((c
= getc(fi
)) != EOF
)
184 if (c
== '\n') { /* xxx */
198 if (!seen_default
&& (*user
!= '\0' || *pass
!= '\0')) {
199 size_t hl
= strlen(host
), ul
= strlen(user
), pl
= strlen(pass
);
200 struct nrc_node
*nx
= smalloc(sizeof(*nx
) -
201 VFIELD_SIZEOF(struct nrc_node
, nrc_dat
) + hl
+1 + ul
+1 + pl
+1);
204 ntail
->nrc_next
= nx
;
212 memcpy(nx
->nrc_dat
, host
, ++hl
);
213 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
214 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
216 if (t
== NRC_MACHINE
)
218 if (t
== NRC_DEFAULT
)
225 if (options
& OPT_D_V
)
226 fprintf(stderr
, _("Errors occurred while parsing `%s'\n"), netrc_load
);
227 assert(nrc
== NRC_NODE_ERR
);
235 if (nrc
== NRC_NODE_ERR
)
236 while (nhead
!= NULL
) {
238 nhead
= nhead
->nrc_next
;
246 static enum nrc_token
247 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
])
251 enum nrc_token rv
= NRC_NONE
;
255 if (feof(fi
) || ferror(fi
))
258 while ((c
= getc(fi
)) != EOF
&& whitechar(c
))
264 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
265 if (c
== '"' || c
== '\'') {
268 /* Not requiring the closing QM is (Net)BSD syntax */
269 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
270 /* Backslash escaping the next character is (Net)BSD syntax */
272 if ((c
= getc(fi
)) == EOF
)
275 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
282 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
283 /* Backslash escaping the next character is (Net)BSD syntax */
284 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
287 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
296 do {/*rv = NRC_NONE*/} while (0);
297 else if (!strcmp(buffer
, "default"))
299 else if (!strcmp(buffer
, "login"))
301 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
303 else if (!strcmp(buffer
, "account"))
305 else if (!strcmp(buffer
, "macdef"))
307 else if (!strcmp(buffer
, "machine"))
312 if (c
== EOF
&& !feof(fi
))
319 _nrc_lookup(struct url
*urlp
, bool_t only_pass
) /* TODO optimize; too tricky!! */
321 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
325 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
326 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
328 if (_nrc_list
== NULL
)
330 if (_nrc_list
== NRC_NODE_ERR
)
333 nrc_wild
= nrc_exact
= NULL
;
334 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
335 switch (__nrc_host_match(nrc
, urlp
)) {
337 nrc
->nrc_result
= nrc_exact
;
341 nrc
->nrc_result
= nrc_wild
;
348 /* TODO _nrc_lookup(): PAIN! init: build sorted tree, single walk that!!
349 * TODO then: verify .netrc (unique fallback entries etc.) */
350 if (!only_pass
&& !__nrc_find_user(urlp
, nrc_exact
) &&
351 !__nrc_find_user(urlp
, nrc_wild
))
354 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
355 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
356 /* Do not try to find a password without exact user match unless we've
357 * been called during credential lookup, a.k.a. the second time */
359 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
360 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
368 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
375 /* Find a matching machine -- entries are all lowercase normalized */
376 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
377 if (LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
382 /* Cannot be an exact match, but maybe the .netrc machine starts with
383 * a `*.' glob, which we recognize as an extension, meaning "skip
384 * a single subdomain, then match the rest" */
385 d1
= nrc
->nrc_dat
+ 2;
387 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
391 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
392 * in fact this even succeeds for `.host.com', but - why care, here? */
393 d2
= urlp
->url_host
.s
;
394 l2
= urlp
->url_host
.l
;
401 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
402 /* This matches, but we won't use it directly but watch out for an
403 * exact match first! */
411 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
415 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
416 if (nrc
->nrc_ulen
> 0 && urlp
->url_user
.s
== NULL
) {
417 /* Fake it was part of URL otherwise XXX */
418 urlp
->url_had_user
= TRU1
;
419 /* That buffer will be duplicated by url_parse() in this case! */
420 urlp
->url_user
.s
= UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
421 urlp
->url_user
.l
= nrc
->nrc_ulen
;
426 return (nrc
!= NULL
);
430 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
434 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
435 if (user_match
&& (nrc
->nrc_ulen
!= urlp
->url_user
.l
||
436 memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
439 if (nrc
->nrc_plen
== 0)
442 /* We are responsible for duplicating this buffer! */
443 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
444 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
449 return (nrc
!= NULL
);
451 #endif /* HAVE_NETRC */
455 _agent_shell_lookup(struct url
*urlp
, char const *comm
)
458 char const *env_addon
[8];
461 union {char const *cp
; int c
; sighandler_type sht
;} u
;
466 env_addon
[0] = str_concat_csvl(&s
, AGENT_USER
, "=", urlp
->url_user
.s
,
468 env_addon
[1] = str_concat_csvl(&s
, AGENT_USER_ENC
, "=", urlp
->url_user_enc
.s
,
470 env_addon
[2] = str_concat_csvl(&s
, AGENT_HOST
, "=", urlp
->url_host
.s
,
472 env_addon
[3] = str_concat_csvl(&s
, AGENT_HOST_PORT
, "=", urlp
->url_h_p
.s
,
476 if ((u
.cp
= ok_vlook(SHELL
)) == NULL
)
478 if ((pbuf
= Popen(comm
, "r", u
.cp
, env_addon
, -1)) == NULL
) {
479 fprintf(stderr
, _("*agent-shell-lookup* startup failed (`%s')\n"),
484 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
485 if (u
.c
== '\n') /* xxx */
488 if (l
== sizeof(buf
) - 1) {
489 n_str_add_buf(&s
, buf
, l
);
494 n_str_add_buf(&s
, buf
, l
);
496 if (!Pclose(pbuf
, TRU1
)) {
497 if (options
& OPT_D_V
)
498 fprintf(stderr
, _("*agent-shell-lookup* execution failure (`%s')\n"),
503 /* We are responsible for duplicating this buffer! */
505 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
507 urlp
->url_pass
.s
= UNCONST(""), urlp
->url_pass
.l
= 0;
518 (urlxenc
)(char const *cp
, bool_t ispath SALLOC_DEBUG_ARGS
)
523 np
= n
= (salloc
)(strlen(cp
) * 3 +1 SALLOC_DEBUG_ARGSCALL
);
525 for (; (c1
= *cp
) != '\0'; ++cp
) {
526 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
527 * ALPHA / DIGIT / "-" / "." / "_" / "~"
528 * However add a special is[file]path mode for file-system friendliness */
529 if (alnumchar(c1
) || c1
== '_')
532 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
535 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
540 mime_char_to_hexseq(np
+ 1, c1
);
550 (urlxdec
)(char const *cp SALLOC_DEBUG_ARGS
)
556 np
= n
= (salloc
)(strlen(cp
) +1 SALLOC_DEBUG_ARGSCALL
);
558 while ((c
= (uc_it
)*cp
++) != '\0') {
559 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
561 if (LIKELY((c
= mime_hexseq_to_char(cp
)) >= '\0'))
573 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
575 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
577 #if defined HAVE_SMTP && defined HAVE_POP3 && defined HAVE_IMAP
580 #if defined HAVE_SMTP || defined HAVE_POP3 || defined HAVE_IMAP
588 memset(urlp
, 0, sizeof *urlp
);
589 urlp
->url_input
= data
;
590 urlp
->url_cproto
= cproto
;
592 /* Network protocol */
593 #define _protox(X,Y) \
594 urlp->url_portno = Y;\
595 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
596 urlp->url_proto[sizeof(X) -1] = '\0';\
597 urlp->url_proto_len = sizeof(X) -1;\
598 urlp->url_proto_xlen = sizeof(X "://") -1
599 #define __if(X,Y,Z) \
600 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
602 data += sizeof(X "://") -1;\
603 do { Z; } while (0);\
606 #define _if(X,Y) __if(X, Y, (void)0)
608 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
610 # define _ifs(X,Y) goto jeproto;
617 _if ("submission", 587)
628 _protox("pop3", 110);
637 _protox("imap", 143);
649 if (strstr(data
, "://") != NULL
) {
650 #if !defined __ALLPROTO || !defined HAVE_SSL
653 fprintf(stderr
, _("URL `proto://' prefix invalid: `%s'\n"),
659 /* User and password, I */
661 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
662 size_t l
= PTR2SIZE(cp
- data
);
663 char const *d
= data
;
664 char *ub
= ac_alloc(l
+1);
666 urlp
->url_had_user
= TRU1
;
669 /* And also have a password? */
670 if ((cp
= memchr(d
, ':', l
)) != NULL
) {
671 size_t i
= PTR2SIZE(cp
- d
);
674 memcpy(ub
, cp
+ 1, l
);
676 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(ub
));
678 if (strcmp(ub
, urlxenc(urlp
->url_pass
.s
, FAL0
))) {
680 _("String is not properly URL percent encoded: `%s'\n"), ub
);
688 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(ub
));
689 urlp
->url_user_enc
.l
= strlen(
690 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
692 if (urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)) {
694 _("String is not properly URL percent encoded: `%s'\n"), ub
);
701 /* Servername and port -- and possible path suffix */
702 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
706 urlp
->url_port
= x
= savestr(x
= cp
+ 1);
707 if ((x
= strchr(x
, '/')) != NULL
)
709 l
= strtol(urlp
->url_port
, &eptr
, 10);
710 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
711 fprintf(stderr
, _("URL with invalid port number: `%s'\n"),
715 urlp
->url_portno
= (ui16_t
)l
;
717 if ((x
= strchr(data
, '/')) != NULL
)
718 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
719 cp
= UNCONST(data
+ strlen(data
));
722 /* A (non-empty) path may only occur with IMAP */
723 if (x
!= NULL
&& x
[1] != '\0') {
724 if (cproto
!= CPROTO_IMAP
) {
725 fprintf(stderr
, _("URL protocol doesn't support paths: `%s'\n"),
729 urlp
->url_path
.l
= strlen(++x
);
730 urlp
->url_path
.s
= savestrbuf(x
, urlp
->url_path
.l
);
733 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
735 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
736 *cp
= lowerconv(*cp
);
739 /* .url_h_p: HOST:PORT */
741 struct str
*s
= &urlp
->url_h_p
;
743 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
744 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
745 if (urlp
->url_port
!= NULL
) {
746 size_t j
= strlen(urlp
->url_port
);
748 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
756 * If there was no user in the URL, do we have *user-HOST* or *user*? */
757 if (!urlp
->url_had_user
) {
758 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
760 /* No, check wether .netrc lookup is desired */
762 if (!ok_blook(v15_compat
) ||
763 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
764 !_nrc_lookup(urlp
, FAL0
))
766 urlp
->url_user
.s
= UNCONST(myname
);
769 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
770 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
771 urlp
->url_user_enc
.l
= strlen(
772 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
775 /* And then there are a lot of prebuild string combinations TODO do lazy */
777 /* .url_u_h: .url_user@.url_host
778 * For SMTP we apply ridiculously complicated *v15-compat* plus
779 * *smtp-hostname* / *hostname* dependent rules */
783 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
784 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
787 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
792 i
= urlp
->url_user
.l
;
794 s
->s
= salloc(i
+ 1 + h
.l
+1);
796 memcpy(s
->s
, urlp
->url_user
.s
, i
);
799 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
804 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
805 { struct str
*s
= &urlp
->url_u_h_p
;
806 size_t i
= urlp
->url_user
.l
;
808 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
810 memcpy(s
->s
, urlp
->url_user
.s
, i
);
813 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
814 i
+= urlp
->url_h_p
.l
;
818 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
819 { struct str
*s
= &urlp
->url_eu_h_p
;
820 size_t i
= urlp
->url_user_enc
.l
;
822 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
824 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
827 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
828 i
+= urlp
->url_h_p
.l
;
832 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
834 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
836 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
837 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
838 urlp
->url_u_h_p
.l
+1);
839 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
841 urlp
->url_p_u_h_p
= ud
;
844 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
846 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
847 1 + urlp
->url_path
.l
+1);
849 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
850 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
851 urlp
->url_eu_h_p
.l
+1);
852 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
854 if (urlp
->url_path
.l
== 0)
855 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
857 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
858 urlp
->url_p_eu_h_p_p
= ud
;
861 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
866 #endif /* __ANYPROTO */
875 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
877 char const *pname
, *pxstr
, *authdef
;
878 size_t pxlen
, addrlen
, i
;
881 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
883 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
886 memset(ccp
, 0, sizeof *ccp
);
893 pxlen
= sizeof("smtp-auth") -1;
894 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
895 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
897 addr_is_nuser
= TRU1
;
902 pxlen
= sizeof("pop3-auth") -1;
903 authmask
= AUTHTYPE_PLAIN
;
909 pxlen
= sizeof("imap-auth") -1;
910 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
915 ccp
->cc_cproto
= cproto
;
916 addrlen
= strlen(addr
);
917 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
918 memcpy(vbuf
, pxstr
, pxlen
);
920 /* Authentication type */
922 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
923 if ((s
= vok_vlook(vbuf
)) == NULL
) {
925 if ((s
= vok_vlook(vbuf
)) == NULL
)
926 s
= UNCONST(authdef
);
929 if (!asccasecmp(s
, "none")) {
930 ccp
->cc_auth
= "NONE";
931 ccp
->cc_authtype
= AUTHTYPE_NONE
;
933 } else if (!asccasecmp(s
, "plain")) {
934 ccp
->cc_auth
= "PLAIN";
935 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
936 ware
= REQ_PASS
| REQ_USER
;
937 } else if (!asccasecmp(s
, "login")) {
938 ccp
->cc_auth
= "LOGIN";
939 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
940 ware
= REQ_PASS
| REQ_USER
;
941 } else if (!asccasecmp(s
, "cram-md5")) {
942 ccp
->cc_auth
= "CRAM-MD5";
943 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
944 ware
= REQ_PASS
| REQ_USER
;
945 } else if (!asccasecmp(s
, "gssapi")) {
946 ccp
->cc_auth
= "GSS-API";
947 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
952 if (!(ccp
->cc_authtype
& authmask
)) {
953 fprintf(stderr
, _("Unsupported %s authentication method: %s\n"),
959 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
960 fprintf(stderr
, _("No CRAM-MD5 support compiled in.\n"));
966 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
967 fprintf(stderr
, _("No GSS-API support compiled in.\n"));
974 if (!(ware
& (WANT_USER
| REQ_USER
)))
977 if (!addr_is_nuser
) {
978 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
979 ccp
->cc_user
.s
= urlxdec(savestrbuf(addr
, PTR2SIZE(s
- addr
)));
980 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
981 } else if (ware
& REQ_USER
)
986 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
988 memcpy(vbuf
+ i
, addr
, addrlen
+1);
989 if ((s
= vok_vlook(vbuf
)) == NULL
) {
991 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
992 if ((s
= getuser(NULL
)) == NULL
) {
993 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
994 * TODO even better: introduce `PROTO-user' and `PROTO-pass' and
995 * TODO check that first, then! change control flow, grow `vbuf' */
996 fprintf(stderr
, _("A user is necessary for %s authentication.\n"),
1003 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1007 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1010 if (!addr_is_nuser
) {
1011 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1013 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1016 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1017 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1019 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1020 (ware
& REQ_PASS
)) {
1021 if ((s
= getpassword(NULL
)) == NULL
) {
1023 _("A password is necessary for %s authentication.\n"), pname
);
1030 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1034 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1035 fprintf(stderr
, _("Credentials: host `%s', user `%s', pass `%s'\n"),
1036 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1037 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1039 return (ccp
!= NULL
);
1043 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1045 char const *pstr
, *authdef
;
1047 enum okeys authokey
;
1049 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1053 memset(ccp
, 0, sizeof *ccp
);
1054 ccp
->cc_user
= urlp
->url_user
;
1056 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1060 authokey
= ok_v_smtp_auth
;
1061 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1062 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1067 authokey
= ok_v_pop3_auth
;
1068 authmask
= AUTHTYPE_PLAIN
;
1073 authokey
= ok_v_imap_auth
;
1074 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1079 /* Authentication type */
1080 if ((s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1081 s
= UNCONST(authdef
);
1083 if (!asccasecmp(s
, "none")) {
1084 ccp
->cc_auth
= "NONE";
1085 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1087 } else if (!asccasecmp(s
, "plain")) {
1088 ccp
->cc_auth
= "PLAIN";
1089 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1090 ware
= REQ_PASS
| REQ_USER
;
1091 } else if (!asccasecmp(s
, "login")) {
1092 ccp
->cc_auth
= "LOGIN";
1093 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1094 ware
= REQ_PASS
| REQ_USER
;
1095 } else if (!asccasecmp(s
, "cram-md5")) {
1096 ccp
->cc_auth
= "CRAM-MD5";
1097 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1098 ware
= REQ_PASS
| REQ_USER
;
1099 } else if (!asccasecmp(s
, "gssapi")) {
1100 ccp
->cc_auth
= "GSS-API";
1101 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1106 if (!(ccp
->cc_authtype
& authmask
)) {
1107 fprintf(stderr
, _("Unsupported %s authentication method: %s\n"), pstr
, s
);
1112 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1113 fprintf(stderr
, _("No CRAM-MD5 support compiled in.\n"));
1118 # ifndef HAVE_GSSAPI
1119 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1120 fprintf(stderr
, _("No GSS-API support compiled in.\n"));
1127 if ((ccp
->cc_pass
= urlp
->url_pass
).s
!= NULL
)
1130 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1133 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1134 if (!_agent_shell_lookup(urlp
, s
)) {
1137 } else if (urlp
->url_pass
.s
!= NULL
) {
1138 ccp
->cc_pass
= urlp
->url_pass
;
1144 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1145 ccp
->cc_pass
= urlp
->url_pass
;
1149 if (ware
& REQ_PASS
) {
1150 if ((s
= getpassword(NULL
)) != NULL
)
1152 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1154 fprintf(stderr
, _("A password is necessary for %s authentication.\n"),
1161 if (ccp
!= NULL
&& (options
& OPT_D_VV
))
1162 fprintf(stderr
, _("Credentials: host `%s', user `%s', pass `%s'\n"),
1163 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: ""),
1164 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: ""));
1166 return (ccp
!= NULL
);
1168 #endif /* HAVE_SOCKETS */
1175 struct nrc_node
*nrc
;
1180 if (argv
[1] != NULL
)
1182 if (!asccasecmp(*argv
, "show"))
1184 if (!asccasecmp(*argv
, "clear"))
1187 fprintf(stderr
, "Synopsis: netrc: %s\n",
1188 _("Either <show> (default) or <clear> the .netrc cache"));
1192 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1198 if (_nrc_list
== NULL
)
1200 if (_nrc_list
== NRC_NODE_ERR
) {
1201 fprintf(stderr
, _("Interpolate what file?\n"));
1206 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)
1213 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1214 fprintf(fp
, _("Host %s: "), nrc
->nrc_dat
);
1215 if (nrc
->nrc_ulen
> 0)
1216 fprintf(fp
, _("user %s, "), nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
1218 fputs(_("no user, "), fp
);
1219 if (nrc
->nrc_plen
> 0)
1220 fprintf(fp
, _("password %s.\n"),
1221 nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1);
1223 fputs(_("no password.\n"), fp
);
1226 page_or_print(fp
, l
);
1232 if (_nrc_list
== NRC_NODE_ERR
)
1234 while ((nrc
= _nrc_list
) != NULL
) {
1235 _nrc_list
= nrc
->nrc_next
;
1240 #endif /* HAVE_NETRC */
1244 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1246 char const *cp
= vp
;
1250 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1252 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1253 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1254 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1262 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1266 char digest
[16], *cp
;
1270 in
.s
= UNCONST(b64
);
1271 in
.l
= strlen(in
.s
);
1272 b64_decode(&out
, &in
, NULL
);
1273 assert(out
.s
!= NULL
);
1275 hmac_md5((uc_it
*)out
.s
, out
.l
, (uc_it
*)pass
->s
, pass
->l
, digest
);
1277 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1279 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1280 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1281 memcpy(in
.s
, user
->s
, user
->l
);
1282 in
.s
[user
->l
] = ' ';
1283 memcpy(in
.s
+ user
->l
+ 1, cp
, MD5TOHEX_SIZE
);
1284 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1291 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1295 * This code is taken from
1297 * Network Working Group H. Krawczyk
1298 * Request for Comments: 2104 IBM
1299 * Category: Informational M. Bellare
1306 * HMAC: Keyed-Hashing for Message Authentication
1309 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1310 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1311 unsigned char tk
[16];
1315 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1320 md5_update(&tctx
, key
, key_len
);
1321 md5_final(tk
, &tctx
);
1327 /* the HMAC_MD5 transform looks like:
1329 * MD5(K XOR opad, MD5(K XOR ipad, text))
1331 * where K is an n byte key
1332 * ipad is the byte 0x36 repeated 64 times
1333 * opad is the byte 0x5c repeated 64 times
1334 * and text is the data being protected */
1336 /* start out by storing key in pads */
1337 memset(k_ipad
, 0, sizeof k_ipad
);
1338 memset(k_opad
, 0, sizeof k_opad
);
1339 memcpy(k_ipad
, key
, key_len
);
1340 memcpy(k_opad
, key
, key_len
);
1342 /* XOR key with ipad and opad values */
1343 for (i
=0; i
<64; i
++) {
1348 /* perform inner MD5 */
1349 md5_init(&context
); /* init context for 1st pass */
1350 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1351 md5_update(&context
, text
, text_len
); /* then text of datagram */
1352 md5_final(digest
, &context
); /* finish up 1st pass */
1354 /* perform outer MD5 */
1355 md5_init(&context
); /* init context for 2nd pass */
1356 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1357 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1358 md5_final(digest
, &context
); /* finish up 2nd pass */
1361 #endif /* HAVE_MD5 */