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
);
112 char buffer
[NRC_TOKEN_MAXLEN
], host
[NRC_TOKEN_MAXLEN
],
113 user
[NRC_TOKEN_MAXLEN
], pass
[NRC_TOKEN_MAXLEN
], *netrc_load
;
117 bool_t
volatile ispipe
;
118 bool_t seen_default
, nl_last
;
119 struct nrc_node
* volatile ntail
, * volatile nhead
, * volatile nrc
;
122 n_UNINIT(ntail
, NULL
);
128 hold_all_sigs(); /* todo */
130 if ((netrc_load
= ok_vlook(netrc_pipe
)) != NULL
) {
132 if ((fi
= Popen(netrc_load
, "r", ok_vlook(SHELL
), NULL
, n_CHILD_FD_NULL
)
134 n_perr(netrc_load
, 0);
138 if ((netrc_load
= fexpand(ok_vlook(NETRC
), FEXP_LOCAL
| FEXP_NOPROTO
)
142 if ((fi
= Fopen(netrc_load
, "r")) == NULL
) {
143 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(netrc_load
, FAL0
));
147 /* Be simple and apply rigid (permission) check(s) */
148 if (fstat(fileno(fi
), &sb
) == -1 || !S_ISREG(sb
.st_mode
) ||
149 (sb
.st_mode
& (S_IRWXG
| S_IRWXO
))) {
150 n_err(_("Not a regular file, or accessible by non-user: %s\n"),
151 n_shexp_quote_cp(netrc_load
, FAL0
));
159 switch((t
= __nrc_token(fi
, buffer
, &nl_last
))) {
162 default: /* Doesn't happen (but on error?), keep CC happy */
165 /* We ignore the default entry (require an exact host match), and we also
166 * ignore anything after such an entry (faulty syntax) */
171 /* Normalize HOST to lowercase */
173 if (!seen_default
&& (t
= __nrc_token(fi
, host
, &nl_last
)) != NRC_INPUT
)
177 for (cp
= host
; *cp
!= '\0'; ++cp
)
178 *cp
= lowerconv(*cp
);
181 *user
= *pass
= '\0';
182 while ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_NONE
&&
183 t
!= NRC_MACHINE
&& t
!= NRC_DEFAULT
) {
186 if ((t
= __nrc_token(fi
, user
, &nl_last
)) != NRC_INPUT
)
190 if ((t
= __nrc_token(fi
, pass
, &nl_last
)) != NRC_INPUT
)
194 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
198 if ((t
= __nrc_token(fi
, buffer
, &nl_last
)) != NRC_INPUT
)
202 while ((c
= getc(fi
)) != EOF
)
203 if (c
== '\n') { /* xxx */
204 /* Don't care about comments here, since we parse until
205 * we've seen two successive newline characters */
219 if (!seen_default
&& (*user
!= '\0' || *pass
!= '\0')) {
220 size_t hl
= strlen(host
), ul
= strlen(user
), pl
= strlen(pass
);
221 struct nrc_node
*nx
= smalloc(n_VSTRUCT_SIZEOF(struct nrc_node
,
222 nrc_dat
) + hl
+1 + ul
+1 + pl
+1);
225 ntail
->nrc_next
= nx
;
233 memcpy(nx
->nrc_dat
, host
, ++hl
);
234 memcpy(nx
->nrc_dat
+ hl
, user
, ++ul
);
235 memcpy(nx
->nrc_dat
+ hl
+ ul
, pass
, ++pl
);
237 if (t
== NRC_MACHINE
)
239 if (t
== NRC_DEFAULT
)
246 if(n_poption
& n_PO_D_V
)
247 n_err(_("Errors occurred while parsing %s\n"),
248 n_shexp_quote_cp(netrc_load
, FAL0
));
249 assert(nrc
== NRC_NODE_ERR
);
262 if (nrc
== NRC_NODE_ERR
)
263 while (nhead
!= NULL
) {
265 nhead
= nhead
->nrc_next
;
274 static enum nrc_token
275 __nrc_token(FILE *fi
, char buffer
[NRC_TOKEN_MAXLEN
], bool_t
*nl_last
)
287 if (feof(fi
) || ferror(fi
))
290 for (seen_nl
= *nl_last
; (c
= getc(fi
)) != EOF
&& whitechar(c
);)
291 seen_nl
|= (c
== '\n');
295 /* fetchmail and derived parsers support comments */
296 if ((*nl_last
= seen_nl
) && c
== '#') {
297 while ((c
= getc(fi
)) != EOF
&& c
!= '\n')
305 /* Is it a quoted token? At least IBM syntax also supports ' quotes */
306 if (c
== '"' || c
== '\'') {
309 /* Not requiring the closing QM is (Net)BSD syntax */
310 while ((c
= getc(fi
)) != EOF
&& c
!= quotec
) {
311 /* Reverse solidus escaping the next character is (Net)BSD syntax */
313 if ((c
= getc(fi
)) == EOF
)
316 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
323 while ((c
= getc(fi
)) != EOF
&& !whitechar(c
)) {
324 /* Rverse solidus escaping the next character is (Net)BSD syntax */
325 if (c
== '\\' && (c
= getc(fi
)) == EOF
)
328 if (PTRCMP(cp
, ==, buffer
+ NRC_TOKEN_MAXLEN
)) {
333 *nl_last
= (c
== '\n');
338 do {/*rv = NRC_NONE*/} while (0);
339 else if (!strcmp(buffer
, "default"))
341 else if (!strcmp(buffer
, "login"))
343 else if (!strcmp(buffer
, "password") || !strcmp(buffer
, "passwd"))
345 else if (!strcmp(buffer
, "account"))
347 else if (!strcmp(buffer
, "macdef"))
349 else if (!strcmp(buffer
, "machine"))
354 if (c
== EOF
&& !feof(fi
))
361 _nrc_lookup(struct url
*urlp
, bool_t only_pass
)
363 struct nrc_node
*nrc
, *nrc_wild
, *nrc_exact
;
367 assert(!only_pass
|| urlp
->url_user
.s
!= NULL
);
368 assert(only_pass
|| urlp
->url_user
.s
== NULL
);
370 if (_nrc_list
== NULL
)
372 if (_nrc_list
== NRC_NODE_ERR
)
375 nrc_wild
= nrc_exact
= NULL
;
376 for (nrc
= _nrc_list
; nrc
!= NULL
; nrc
= nrc
->nrc_next
)
377 switch (__nrc_host_match(nrc
, urlp
)) {
379 nrc
->nrc_result
= nrc_exact
;
383 nrc
->nrc_result
= nrc_wild
;
390 if (!only_pass
&& urlp
->url_user
.s
== NULL
) {
391 /* Must be an unambiguous entry of its kind */
392 if (nrc_exact
!= NULL
&& nrc_exact
->nrc_result
!= NULL
)
394 if (__nrc_find_user(urlp
, nrc_exact
))
397 if (nrc_wild
!= NULL
&& nrc_wild
->nrc_result
!= NULL
)
399 if (!__nrc_find_user(urlp
, nrc_wild
))
405 if (__nrc_find_pass(urlp
, TRU1
, nrc_exact
) ||
406 __nrc_find_pass(urlp
, TRU1
, nrc_wild
) ||
407 /* Do not try to find a password without exact user match unless we've
408 * been called during credential lookup, a.k.a. the second time */
410 __nrc_find_pass(urlp
, FAL0
, nrc_exact
) ||
411 __nrc_find_pass(urlp
, FAL0
, nrc_wild
))
419 __nrc_host_match(struct nrc_node
const *nrc
, struct url
const *urlp
)
426 /* Find a matching machine -- entries are all lowercase normalized */
427 if (nrc
->nrc_mlen
== urlp
->url_host
.l
) {
428 if (n_LIKELY(!memcmp(nrc
->nrc_dat
, urlp
->url_host
.s
, urlp
->url_host
.l
)))
433 /* Cannot be an exact match, but maybe the .netrc machine starts with
434 * a "*." glob, which we recognize as an extension, meaning "skip
435 * a single subdomain, then match the rest" */
436 d1
= nrc
->nrc_dat
+ 2;
438 if (l1
<= 2 || d1
[-1] != '.' || d1
[-2] != '*')
442 /* Brute skipping over one subdomain, no RFC 1035 or RFC 1122 checks;
443 * in fact this even succeeds for ".host.com", but - why care, here? */
444 d2
= urlp
->url_host
.s
;
445 l2
= urlp
->url_host
.l
;
452 if (l2
== l1
&& !memcmp(d1
, d2
, l1
))
453 /* This matches, but we won't use it directly but watch out for an
454 * exact match first! */
462 __nrc_find_user(struct url
*urlp
, struct nrc_node
const *nrc
)
466 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
)
467 if (nrc
->nrc_ulen
> 0) {
468 /* Fake it was part of URL otherwise XXX */
469 urlp
->url_flags
|= n_URL_HAD_USER
;
470 /* That buffer will be duplicated by url_parse() in this case! */
471 urlp
->url_user
.s
= n_UNCONST(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1);
472 urlp
->url_user
.l
= nrc
->nrc_ulen
;
477 return (nrc
!= NULL
);
481 __nrc_find_pass(struct url
*urlp
, bool_t user_match
, struct nrc_node
const *nrc
)
485 for (; nrc
!= NULL
; nrc
= nrc
->nrc_result
) {
486 bool_t um
= (nrc
->nrc_ulen
== urlp
->url_user
.l
&&
487 !memcmp(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1, urlp
->url_user
.s
,
493 } else if (!um
&& nrc
->nrc_ulen
> 0)
495 if (nrc
->nrc_plen
== 0)
498 /* We are responsible for duplicating this buffer! */
499 urlp
->url_pass
.s
= savestrbuf(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 +
500 nrc
->nrc_ulen
+ 1, (urlp
->url_pass
.l
= nrc
->nrc_plen
));
505 return (nrc
!= NULL
);
507 #endif /* HAVE_NETRC */
511 _agent_shell_lookup(struct url
*urlp
, char const *comm
) /* TODO v15-compat */
514 char const *env_addon
[8];
517 union {int c
; sighandler_type sht
;} u
;
522 env_addon
[0] = str_concat_csvl(&s
, "NAIL_USER", "=", urlp
->url_user
.s
,
524 env_addon
[1] = str_concat_csvl(&s
,
525 "NAIL_USER_ENC", "=", urlp
->url_user_enc
.s
, NULL
)->s
;
526 env_addon
[2] = str_concat_csvl(&s
, "NAIL_HOST", "=", urlp
->url_host
.s
,
528 env_addon
[3] = str_concat_csvl(&s
, "NAIL_HOST_PORT", "=", urlp
->url_h_p
.s
,
532 if ((pbuf
= Popen(comm
, "r", ok_vlook(SHELL
), env_addon
, -1)) == NULL
) {
533 n_err(_("*agent-shell-lookup* startup failed (%s)\n"), comm
);
537 for (s
.s
= NULL
, s
.l
= cl
= l
= 0; (u
.c
= getc(pbuf
)) != EOF
; ++cl
) {
538 if (u
.c
== '\n') /* xxx */
541 if (l
== sizeof(buf
) - 1) {
542 n_str_add_buf(&s
, buf
, l
);
547 n_str_add_buf(&s
, buf
, l
);
549 if (!Pclose(pbuf
, TRU1
)) {
550 n_err(_("*agent-shell-lookup* execution failure (%s)\n"), comm
);
554 /* We are responsible for duplicating this buffer! */
556 urlp
->url_pass
.s
= savestrbuf(s
.s
, urlp
->url_pass
.l
= s
.l
);
558 urlp
->url_pass
.s
= n_UNCONST(n_empty
), urlp
->url_pass
.l
= 0;
566 #endif /* HAVE_AGENT */
569 (urlxenc
)(char const *cp
, bool_t ispath n_MEMORY_DEBUG_ARGS
)
578 if(i
>= UIZ_MAX
/ 3){
584 np
= n
= (n_autorec_alloc_from_pool
)(NULL
, i n_MEMORY_DEBUG_ARGSCALL
);
587 for (; (c1
= *cp
) != '\0'; ++cp
) {
588 /* (RFC 1738) RFC 3986, 2.3 Unreserved Characters:
589 * ALPHA / DIGIT / "-" / "." / "_" / "~"
590 * However add a special is[file]path mode for file-system friendliness */
591 if (alnumchar(c1
) || c1
== '_')
594 if (c1
!= '-' && c1
!= '.' && c1
!= '~')
597 } else if (PTRCMP(np
, >, n
) && (*cp
== '-' || *cp
== '.')) /* XXX imap */
602 n_c_to_hex_base16(np
+ 1, c1
);
613 (urlxdec
)(char const *cp n_MEMORY_DEBUG_ARGS
)
619 np
= n
= (n_autorec_alloc_from_pool
)(NULL
, strlen(cp
) +1
620 n_MEMORY_DEBUG_ARGSCALL
);
622 while ((c
= (uc_i
)*cp
++) != '\0') {
623 if (c
== '%' && cp
[0] != '\0' && cp
[1] != '\0') {
625 if (n_LIKELY((c
= n_c_from_hex_base16(cp
)) >= '\0'))
638 c_urlcodec(void *vp
){
641 char const **argv
, *varname
, *varres
, *act
, *cp
;
645 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
648 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
650 if((ispath
= (*act
== 'p'))){
651 if(!ascncasecmp(++act
, "ath", 3))
656 alen
= PTR2SIZE(cp
- act
);
660 n_pstate_err_no
= n_ERR_NONE
;
662 if(is_ascncaseprefix(act
, "encode", alen
))
663 varres
= urlxenc(cp
, ispath
);
664 else if(is_ascncaseprefix(act
, "decode", alen
))
665 varres
= urlxdec(cp
);
670 n_pstate_err_no
= n_ERR_CANCELED
;
676 if(!n_var_vset(varname
, (uintptr_t)varres
)){
677 n_pstate_err_no
= n_ERR_NOTSUP
;
683 in
.l
= strlen(in
.s
= n_UNCONST(varres
));
684 makeprint(&in
, &out
);
685 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0){
686 n_pstate_err_no
= n_err_no
;
694 return (vp
!= NULL
? 0 : 1);
696 n_err(_("Synopsis: urlcodec: "
697 "<[path]e[ncode]|[path]d[ecode]> <rest-of-line>\n"));
698 n_pstate_err_no
= n_ERR_INVAL
;
704 c_urlencode(void *v
) /* XXX IDNA?? */
709 n_OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
711 for (ap
= v
; *ap
!= NULL
; ++ap
) {
712 char *in
= *ap
, *out
= urlxenc(in
, FAL0
);
715 out
= n_UNCONST(V_(n_error
));
717 " in: <%s> (%" PRIuZ
" bytes)\nout: <%s> (%" PRIuZ
" bytes)\n",
718 in
, strlen(in
), out
, strlen(out
));
725 c_urldecode(void *v
) /* XXX IDNA?? */
730 n_OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
732 for (ap
= v
; *ap
!= NULL
; ++ap
) {
733 char *in
= *ap
, *out
= urlxdec(in
);
736 out
= n_UNCONST(V_(n_error
));
738 " in: <%s> (%" PRIuZ
" bytes)\nout: <%s> (%" PRIuZ
" bytes)\n",
739 in
, strlen(in
), out
, strlen(out
));
746 url_mailto_to_address(char const *mailtop
){ /* TODO hack! RFC 6068; factory? */
749 char const *mailtop_orig
;
752 if(!is_prefix("mailto:", mailtop_orig
= mailtop
)){
756 mailtop
+= sizeof("mailto:") -1;
758 /* TODO This is all intermediate, and for now just enough to understand
759 * TODO a little bit of a little more advanced List-Post: headers. */
760 /* Strip any hfield additions, keep only to addr-spec's */
761 if((rv
= strchr(mailtop
, '?')) != NULL
)
762 rv
= savestrbuf(mailtop
, i
= PTR2SIZE(rv
- mailtop
));
764 rv
= savestrbuf(mailtop
, i
= strlen(mailtop
));
768 /* Simply perform percent-decoding if there is a percent % */
769 if(memchr(rv
, '%', i
) != NULL
){
773 for(err
= FAL0
, mailtop
= rv_base
= rv
; i
> 0;){
776 if((c
= *mailtop
++) == '%'){
779 if(i
< 3 || (cc
= n_c_from_hex_base16(mailtop
)) < 0){
780 if(!err
&& (err
= TRU1
, n_poption
& n_PO_D_V
))
781 n_err(_("Invalid RFC 6068 'mailto' URL: %s\n"),
782 n_shexp_quote_cp(mailtop_orig
, FAL0
));
803 n_servbyname(char const *proto
, ui16_t
*irv_or_null
){
810 { "submission", "587", 587},
811 { "smtps", "465", 465},
812 { "pop3", "110", 110},
813 { "pop3s", "995", 995},
814 { "imap", "143", 143},
815 { "imaps", "993", 993},
822 for(rv
= proto
; *rv
!= '\0'; ++rv
)
825 l
= PTR2SIZE(rv
- proto
);
827 for(rv
= NULL
, i
= 0; i
< n_NELEM(tbl
); ++i
)
828 if(!ascncasecmp(tbl
[i
].name
, proto
, l
)){
830 if(irv_or_null
!= NULL
)
831 *irv_or_null
= tbl
[i
].portno
;
838 #ifdef HAVE_SOCKETS /* Note: not indented for that -- later: file:// etc.! */
840 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
842 #if defined HAVE_SMTP && defined HAVE_POP3 && defined HAVE_IMAP
845 #if defined HAVE_SMTP || defined HAVE_POP3 || defined HAVE_IMAP
853 memset(urlp
, 0, sizeof *urlp
);
854 urlp
->url_input
= data
;
855 urlp
->url_cproto
= cproto
;
857 /* Network protocol */
858 #define a_PROTOX(X,Y,Z) \
859 urlp->url_portno = Y;\
860 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
861 urlp->url_proto[sizeof(X) -1] = '\0';\
862 urlp->url_proto_len = sizeof(X) -1;\
863 urlp->url_proto_xlen = sizeof(X "://") -1;\
865 #define a__IF(X,Y,Z) \
866 if(!ascncasecmp(data, X "://", sizeof(X "://") -1)){\
868 data += sizeof(X "://") -1;\
871 #define a_IF(X,Y) a__IF(X, Y, (void)0)
873 # define a_IFS(X,Y) a__IF(X, Y, urlp->url_flags |= n_URL_TLS_REQUIRED)
874 # define a_IFs(X,Y) a__IF(X, Y, urlp->url_flags |= n_URL_TLS_OPTIONAL)
876 # define a_IFS(X,Y) goto jeproto;
877 # define a_IFs(X,Y) a_IF(X, Y)
882 /* The special S/MIME etc. credential lookup */
884 a_PROTOX("ccred", 0, (void)0);
890 a_IF("socks5", 1080);
892 a_PROTOX("socks", 1080, (void)0);
898 a_IFs("submission", 587)
899 a_PROTOX("smtp", 25, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
908 a_PROTOX("pop3", 110, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
917 a_PROTOX("imap", 143, urlp
->url_flags
|= n_URL_TLS_OPTIONAL
);
930 if (strstr(data
, "://") != NULL
) {
931 #if !defined a_ALLPROTO || !defined HAVE_SSL
934 n_err(_("URL proto:// prefix invalid: %s\n"), urlp
->url_input
);
939 /* User and password, I */
941 if ((cp
= _url_last_at_before_slash(data
)) != NULL
) {
943 char const *urlpe
, *d
;
946 l
= PTR2SIZE(cp
- data
);
949 urlp
->url_flags
|= n_URL_HAD_USER
;
952 /* And also have a password? */
953 if((cp
= memchr(d
, ':', l
)) != NULL
){
954 size_t i
= PTR2SIZE(cp
- d
);
957 memcpy(ub
, cp
+ 1, l
);
960 if((urlp
->url_pass
.s
= urlxdec(ub
)) == NULL
)
962 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
);
963 if((urlpe
= urlxenc(urlp
->url_pass
.s
, FAL0
)) == NULL
)
965 if(strcmp(ub
, urlpe
))
972 if((urlp
->url_user
.s
= urlxdec(ub
)) == NULL
)
974 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
975 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
)
977 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
979 if(urlp
->url_user_enc
.l
!= l
|| memcmp(urlp
->url_user_enc
.s
, ub
, l
)){
981 n_err(_("String is not properly URL percent encoded: %s\n"), ub
);
990 /* Servername and port -- and possible path suffix */
991 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, use IPAddress! */
992 urlp
->url_port
= x
= savestr(x
= &cp
[1]);
993 if ((x
= strchr(x
, '/')) != NULL
) {
999 if((n_idec_ui16_cp(&urlp
->url_portno
, urlp
->url_port
, 10, NULL
1000 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1001 ) != n_IDEC_STATE_CONSUMED
){
1002 n_err(_("URL with invalid port number: %s\n"), urlp
->url_input
);
1006 if ((x
= strchr(data
, '/')) != NULL
) {
1007 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
1011 cp
= n_UNCONST(data
+ strlen(data
));
1014 /* A (non-empty) path may only occur with IMAP */
1015 if (x
!= NULL
&& *x
!= '\0') {
1016 /* Take care not to count adjacent solidus for real, on either end */
1021 for(trailsol
= FAL0
, x2
= savestrbuf(x
, i
= strlen(x
)); i
> 0;
1022 trailsol
= TRU1
, --i
)
1023 if(x2
[i
- 1] != '/')
1028 if (cproto
!= CPROTO_IMAP
) {
1029 n_err(_("URL protocol doesn't support paths: \"%s\"\n"),
1035 urlp
->url_path
.s
= n_autorec_alloc(i
+ sizeof("/INBOX"));
1036 memcpy(urlp
->url_path
.s
, x
, i
);
1037 memcpy(&urlp
->url_path
.s
[i
], "/INBOX", sizeof("/INBOX"));
1038 urlp
->url_path
.l
= (i
+= sizeof("/INBOX") -1);
1041 urlp
->url_path
.l
= i
, urlp
->url_path
.s
= x2
;
1045 if(cproto
== CPROTO_IMAP
&& urlp
->url_path
.s
== NULL
)
1046 urlp
->url_path
.s
= savestrbuf("INBOX",
1047 urlp
->url_path
.l
= sizeof("INBOX") -1);
1050 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
1052 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
1053 *cp
= lowerconv(*cp
);
1056 if(!ok_blook(idna_disable
)){
1057 struct n_string idna
;
1059 if(!n_idna_to_ascii(n_string_creat_auto(&idna
), urlp
->url_host
.s
,
1061 n_err(_("URL host fails IDNA conversion: %s\n"), urlp
->url_input
);
1064 urlp
->url_host
.s
= n_string_cp(&idna
);
1065 urlp
->url_host
.l
= idna
.s_len
;
1067 #endif /* HAVE_IDNA */
1069 /* .url_h_p: HOST:PORT */
1071 struct str
*s
= &urlp
->url_h_p
;
1073 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
1074 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
1075 if (urlp
->url_port
!= NULL
) {
1076 size_t j
= strlen(urlp
->url_port
);
1078 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
1086 * If there was no user in the URL, do we have *user-HOST* or *user*? */
1087 if (!(urlp
->url_flags
& n_URL_HAD_USER
)) {
1088 if ((urlp
->url_user
.s
= xok_vlook(user
, urlp
, OXM_PLAIN
| OXM_H_P
))
1090 /* No, check whether .netrc lookup is desired */
1092 if (!ok_blook(v15_compat
) ||
1093 !xok_blook(netrc_lookup
, urlp
, OXM_PLAIN
| OXM_H_P
) ||
1094 !_nrc_lookup(urlp
, FAL0
))
1096 urlp
->url_user
.s
= n_UNCONST(ok_vlook(LOGNAME
));
1099 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
);
1100 urlp
->url_user
.s
= savestrbuf(urlp
->url_user
.s
, urlp
->url_user
.l
);
1101 if((urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
)) == NULL
){
1102 n_err(_("Cannot URL encode %s\n"), urlp
->url_user
.s
);
1105 urlp
->url_user_enc
.l
= strlen(urlp
->url_user_enc
.s
);
1108 /* And then there are a lot of prebuild string combinations TODO do lazy */
1110 /* .url_u_h: .url_user@.url_host
1111 * For SMTP we apply ridiculously complicated *v15-compat* plus
1112 * *smtp-hostname* / *hostname* dependent rules */
1116 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
1117 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
1119 cp
= n_nodename(TRU1
);
1120 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
1125 i
= urlp
->url_user
.l
;
1127 s
->s
= salloc(i
+ 1 + h
.l
+1);
1129 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1132 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
1137 /* .url_u_h_p: .url_user@.url_host[:.url_port] */
1138 { struct str
*s
= &urlp
->url_u_h_p
;
1139 size_t i
= urlp
->url_user
.l
;
1141 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1143 memcpy(s
->s
, urlp
->url_user
.s
, i
);
1146 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1147 i
+= urlp
->url_h_p
.l
;
1151 /* .url_eu_h_p: .url_user_enc@.url_host[:.url_port] */
1152 { struct str
*s
= &urlp
->url_eu_h_p
;
1153 size_t i
= urlp
->url_user_enc
.l
;
1155 s
->s
= salloc(i
+ 1 + urlp
->url_h_p
.l
+1);
1157 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1160 memcpy(s
->s
+ i
, urlp
->url_h_p
.s
, urlp
->url_h_p
.l
+1);
1161 i
+= urlp
->url_h_p
.l
;
1165 /* .url_p_u_h_p: .url_proto://.url_u_h_p */
1167 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_u_h_p
.l
) +1);
1169 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1170 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_u_h_p
.s
,
1171 urlp
->url_u_h_p
.l
+1);
1172 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1174 urlp
->url_p_u_h_p
= ud
;
1177 /* .url_p_eu_h_p, .url_p_eu_h_p_p: .url_proto://.url_eu_h_p[/.url_path] */
1179 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_eu_h_p
.l
) +
1180 1 + urlp
->url_path
.l
+1);
1182 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1183 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_eu_h_p
.s
,
1184 urlp
->url_eu_h_p
.l
+1);
1185 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1187 if (urlp
->url_path
.l
== 0)
1188 urlp
->url_p_eu_h_p
= urlp
->url_p_eu_h_p_p
= ud
;
1190 urlp
->url_p_eu_h_p
= savestrbuf(ud
, i
);
1191 urlp
->url_p_eu_h_p_p
= ud
;
1194 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1199 #endif /* a_ANYPROTO */
1208 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1210 char const *pname
, *pxstr
, *authdef
, *s
;
1211 size_t pxlen
, addrlen
, i
;
1214 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1216 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1219 n_OBSOLETE(_("Use of old-style credentials, which will vanish in v15!\n"
1220 " Please read the manual section "
1221 "\"On URL syntax and credential lookup\""));
1223 memset(ccp
, 0, sizeof *ccp
);
1229 pxstr
= "smtp-auth";
1230 pxlen
= sizeof("smtp-auth") -1;
1231 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1232 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1234 addr_is_nuser
= TRU1
;
1238 pxstr
= "pop3-auth";
1239 pxlen
= sizeof("pop3-auth") -1;
1240 authmask
= AUTHTYPE_PLAIN
;
1246 pxstr
= "imap-auth";
1247 pxlen
= sizeof("imap-auth") -1;
1248 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1254 ccp
->cc_cproto
= cproto
;
1255 addrlen
= strlen(addr
);
1256 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1257 memcpy(vbuf
, pxstr
, pxlen
);
1259 /* Authentication type */
1261 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1262 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1264 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
)
1265 s
= n_UNCONST(authdef
);
1268 if (!asccasecmp(s
, "none")) {
1269 ccp
->cc_auth
= "NONE";
1270 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1272 } else if (!asccasecmp(s
, "plain")) {
1273 ccp
->cc_auth
= "PLAIN";
1274 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1275 ware
= REQ_PASS
| REQ_USER
;
1276 } else if (!asccasecmp(s
, "login")) {
1277 ccp
->cc_auth
= "LOGIN";
1278 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1279 ware
= REQ_PASS
| REQ_USER
;
1280 } else if (!asccasecmp(s
, "cram-md5")) {
1281 ccp
->cc_auth
= "CRAM-MD5";
1282 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1283 ware
= REQ_PASS
| REQ_USER
;
1284 } else if (!asccasecmp(s
, "gssapi")) {
1285 ccp
->cc_auth
= "GSS-API";
1286 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1291 if (!(ccp
->cc_authtype
& authmask
)) {
1292 n_err(_("Unsupported %s authentication method: %s\n"), pname
, s
);
1297 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1298 n_err(_("No CRAM-MD5 support compiled in\n"));
1303 # ifndef HAVE_GSSAPI
1304 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1305 n_err(_("No GSS-API support compiled in\n"));
1312 if (!(ware
& (WANT_USER
| REQ_USER
)))
1315 if (!addr_is_nuser
) {
1316 if ((s
= _url_last_at_before_slash(addr
)) != NULL
) {
1319 cp
= savestrbuf(addr
, PTR2SIZE(s
- addr
));
1321 if((ccp
->cc_user
.s
= urlxdec(cp
)) == NULL
){
1322 n_err(_("String is not properly URL percent encoded: %s\n"), cp
);
1326 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1327 } else if (ware
& REQ_USER
)
1332 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1334 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1335 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1337 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
&& (ware
& REQ_USER
)) {
1338 if ((s
= getuser(NULL
)) == NULL
) {
1339 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1340 * TODO even better: introduce "PROTO-user" and "PROTO-pass" and
1341 * TODO check that first, then! change control flow, grow vbuf */
1342 n_err(_("A user is necessary for %s authentication\n"), pname
);
1348 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= savestr(s
));
1352 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1355 if (!addr_is_nuser
) {
1356 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1358 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1361 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1362 if ((s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) {
1364 if ((!addr_is_nuser
|| (s
= n_var_vlook(vbuf
, FAL0
)) == NULL
) &&
1365 (ware
& REQ_PASS
)) {
1366 if ((s
= getpassword(savecat(_("Password for "), pname
))) == NULL
) {
1367 n_err(_("A password is necessary for %s authentication\n"),
1375 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1379 if (ccp
!= NULL
&& (n_poption
& n_PO_D_VV
))
1380 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1381 addr
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1382 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1384 return (ccp
!= NULL
);
1388 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1391 char const *pstr
, *authdef
;
1393 enum okeys authokey
;
1394 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1398 memset(ccp
, 0, sizeof *ccp
);
1399 ccp
->cc_user
= urlp
->url_user
;
1403 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1405 authokey
= (enum okeys
)-1;
1406 authmask
= AUTHTYPE_PLAIN
;
1412 authokey
= ok_v_smtp_auth
;
1413 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1414 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1419 authokey
= ok_v_pop3_auth
;
1420 authmask
= AUTHTYPE_PLAIN
;
1427 authokey
= ok_v_imap_auth
;
1428 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1434 /* Authentication type */
1435 if (authokey
== (enum okeys
)-1 ||
1436 (s
= xok_VLOOK(authokey
, urlp
, OXM_ALL
)) == NULL
)
1437 s
= n_UNCONST(authdef
);
1439 if (!asccasecmp(s
, "none")) {
1440 ccp
->cc_auth
= "NONE";
1441 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1443 } else if (!asccasecmp(s
, "plain")) {
1444 ccp
->cc_auth
= "PLAIN";
1445 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1446 ware
= REQ_PASS
| REQ_USER
;
1447 } else if (!asccasecmp(s
, "login")) {
1448 ccp
->cc_auth
= "LOGIN";
1449 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1450 ware
= REQ_PASS
| REQ_USER
;
1451 } else if (!asccasecmp(s
, "cram-md5")) {
1452 ccp
->cc_auth
= "CRAM-MD5";
1453 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1454 ware
= REQ_PASS
| REQ_USER
;
1455 } else if (!asccasecmp(s
, "gssapi")) {
1456 ccp
->cc_auth
= "GSS-API";
1457 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1462 if (!(ccp
->cc_authtype
& authmask
)) {
1463 n_err(_("Unsupported %s authentication method: %s\n"), pstr
, s
);
1468 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1469 n_err(_("No CRAM-MD5 support compiled in\n"));
1474 # ifndef HAVE_GSSAPI
1475 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1476 n_err(_("No GSS-API support compiled in\n"));
1483 ccp
->cc_pass
= urlp
->url_pass
;
1484 if (ccp
->cc_pass
.s
!= NULL
)
1487 if ((s
= xok_vlook(password
, urlp
, OXM_ALL
)) != NULL
)
1489 # ifdef HAVE_AGENT /* TODO v15-compat obsolete */
1490 if ((s
= xok_vlook(agent_shell_lookup
, urlp
, OXM_ALL
)) != NULL
) {
1491 n_OBSOLETE(_("*agent-shell-lookup* will vanish. Please use encrypted "
1492 "~/.netrc (via *netrc-pipe*), or simply `source' an encrypted file"));
1493 if (!_agent_shell_lookup(urlp
, s
)) {
1496 } else if (urlp
->url_pass
.s
!= NULL
) {
1497 ccp
->cc_pass
= urlp
->url_pass
;
1503 if (xok_blook(netrc_lookup
, urlp
, OXM_ALL
) && _nrc_lookup(urlp
, TRU1
)) {
1504 ccp
->cc_pass
= urlp
->url_pass
;
1508 if (ware
& REQ_PASS
) {
1509 if((s
= getpassword(savecat(urlp
->url_u_h
.s
, _(" requires a password: ")))
1512 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= savestr(s
));
1514 n_err(_("A password is necessary for %s authentication\n"), pstr
);
1520 if(ccp
!= NULL
&& (n_poption
& n_PO_D_VV
))
1521 n_err(_("Credentials: host %s, user %s, pass %s\n"),
1522 urlp
->url_h_p
.s
, (ccp
->cc_user
.s
!= NULL
? ccp
->cc_user
.s
: n_empty
),
1523 (ccp
->cc_pass
.s
!= NULL
? ccp
->cc_pass
.s
: n_empty
));
1525 return (ccp
!= NULL
);
1527 #endif /* HAVE_SOCKETS */
1534 struct nrc_node
*nrc
;
1541 if (argv
[1] != NULL
)
1543 if (!asccasecmp(*argv
, "show"))
1546 if (!asccasecmp(*argv
, "load"))
1548 if (!asccasecmp(*argv
, "clear"))
1551 n_err(_("Synopsis: netrc: (<show> or) <clear> the .netrc cache\n"));
1555 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1561 if (_nrc_list
== NULL
)
1563 if (_nrc_list
== NRC_NODE_ERR
) {
1564 n_err(_("Interpolate what file?\n"));
1571 if ((fp
= Ftmp(NULL
, "netrc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
) {
1572 n_perr(_("tmpfile"), 0);
1577 for (l
= 0, nrc
= _nrc_list
; nrc
!= NULL
; ++l
, nrc
= nrc
->nrc_next
) {
1578 fprintf(fp
, _("machine %s "), nrc
->nrc_dat
); /* XXX quote? */
1579 if (nrc
->nrc_ulen
> 0)
1580 fprintf(fp
, _("login \"%s\" "),
1581 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1));
1582 if (nrc
->nrc_plen
> 0)
1583 fprintf(fp
, _("password \"%s\"\n"),
1584 string_quote(nrc
->nrc_dat
+ nrc
->nrc_mlen
+1 + nrc
->nrc_ulen
+1));
1589 page_or_print(fp
, l
);
1595 if (_nrc_list
== NRC_NODE_ERR
)
1597 while ((nrc
= _nrc_list
) != NULL
) {
1598 _nrc_list
= nrc
->nrc_next
;
1603 #endif /* HAVE_NETRC */
1607 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1609 char const *cp
= vp
;
1613 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1615 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1616 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1617 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1625 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1629 char digest
[16], *cp
;
1633 if(user
->l
>= UIZ_MAX
- 1 - MD5TOHEX_SIZE
- 1)
1635 if(pass
->l
>= INT_MAX
)
1638 in
.s
= n_UNCONST(b64
);
1639 in
.l
= strlen(in
.s
);
1640 if(!b64_decode(&out
, &in
))
1642 if(out
.l
>= INT_MAX
){
1648 hmac_md5((uc_i
*)out
.s
, out
.l
, (uc_i
*)pass
->s
, pass
->l
, digest
);
1650 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1652 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1653 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1654 memcpy(in
.s
, user
->s
, user
->l
);
1655 in
.s
[user
->l
] = ' ';
1656 memcpy(&in
.s
[user
->l
+ 1], cp
, MD5TOHEX_SIZE
);
1657 if(b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
) == NULL
)
1666 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1670 * This code is taken from
1672 * Network Working Group H. Krawczyk
1673 * Request for Comments: 2104 IBM
1674 * Category: Informational M. Bellare
1681 * HMAC: Keyed-Hashing for Message Authentication
1684 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1685 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1686 unsigned char tk
[16];
1690 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1695 md5_update(&tctx
, key
, key_len
);
1696 md5_final(tk
, &tctx
);
1702 /* the HMAC_MD5 transform looks like:
1704 * MD5(K XOR opad, MD5(K XOR ipad, text))
1706 * where K is an n byte key
1707 * ipad is the byte 0x36 repeated 64 times
1708 * opad is the byte 0x5c repeated 64 times
1709 * and text is the data being protected */
1711 /* start out by storing key in pads */
1712 memset(k_ipad
, 0, sizeof k_ipad
);
1713 memset(k_opad
, 0, sizeof k_opad
);
1714 memcpy(k_ipad
, key
, key_len
);
1715 memcpy(k_opad
, key
, key_len
);
1717 /* XOR key with ipad and opad values */
1718 for (i
=0; i
<64; i
++) {
1723 /* perform inner MD5 */
1724 md5_init(&context
); /* init context for 1st pass */
1725 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1726 md5_update(&context
, text
, text_len
); /* then text of datagram */
1727 md5_final(digest
, &context
); /* finish up 1st pass */
1729 /* perform outer MD5 */
1730 md5_init(&context
); /* init context for 2nd pass */
1731 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1732 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1733 md5_final(digest
, &context
); /* finish up 2nd pass */
1736 #endif /* HAVE_MD5 */