From 72c0a8ccb363fd1be14275cba938dbeca0f12a68 Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Fri, 11 Jul 2014 21:42:07 +0200 Subject: [PATCH] Simplify ccred_lookup().. While here simplify _nrc_lookup() (the .netrc lookup) to return boolean and drop enum nrc_result -- if we will ever validate the content of .netrc then this will be during parse, never during lookup, so that is a stale leftover from when i've implemented that first. --- nail.h | 2 +- urlcrecry.c | 76 +++++++++++++++++++------------------------------------------ 2 files changed, 24 insertions(+), 54 deletions(-) diff --git a/nail.h b/nail.h index fe4e4247..211aef47 100644 --- a/nail.h +++ b/nail.h @@ -144,7 +144,7 @@ #define ESCAPE '~' /* Default escape for sending */ #define FIO_STACK_SIZE 20 /* Maximum recursion for sourcing */ #define HIST_SIZE 242 /* tty.c: history list default size */ -#define HSHSIZE 23 /* Hash prime (aliases, vars, macros) */ +#define HSHSIZE 23 /* Hash prime TODO make dynamic, obsolete */ #define MAXARGC 1024 /* Maximum list of raw strings */ #define MAXEXP 25 /* Maximum expansion of aliases */ #define PROMPT_BUFFER_SIZE 80 /* getprompt() bufsize (> 3!) */ diff --git a/urlcrecry.c b/urlcrecry.c index 23f6194a..a263f615 100644 --- a/urlcrecry.c +++ b/urlcrecry.c @@ -39,12 +39,6 @@ enum nrc_token { NRC_INPUT }; -enum nrc_result { - NRC_RESERROR = -1, - NRC_RESOK, - NRC_RESNONE -}; - struct nrc_node { struct nrc_node *nrc_next; struct nrc_node *nrc_result; /* In match phase, former possible one */ @@ -72,7 +66,7 @@ static enum nrc_token __nrc_token(FILE *fi, char buffer[NRC_TOKEN_MAXLEN]); /* We shall lookup a machine in .netrc says ok_blook(netrc_lookup). * only_pass is true then the lookup is for the password only, otherwise we * look for a user (and add password only if we have an exact machine match) */ -static enum nrc_result _nrc_lookup(struct url *urlp, bool_t only_pass); +static bool_t _nrc_lookup(struct url *urlp, bool_t only_pass); /* 0=no match; 1=exact match; -1=wildcard match */ static int __nrc_host_match(struct nrc_node const *nrc, @@ -301,11 +295,11 @@ jleave: return rv; } -static enum nrc_result +static bool_t _nrc_lookup(struct url *urlp, bool_t only_pass) /* TODO optimize; too tricky!! */ { struct nrc_node *nrc, *nrc_wild, *nrc_exact; - enum nrc_result rv = NRC_RESNONE; + bool_t rv = FAL0; NYD_ENTER; assert(!only_pass || urlp->url_user.s != NULL); @@ -344,7 +338,7 @@ _nrc_lookup(struct url *urlp, bool_t only_pass) /* TODO optimize; too tricky!! * !only_pass || __nrc_find_pass(urlp, FAL0, nrc_exact) || __nrc_find_pass(urlp, FAL0, nrc_wild)) - rv = NRC_RESOK; + rv = TRU1; jleave: NYD_LEAVE; return rv; @@ -683,7 +677,7 @@ juser: #ifdef HAVE_NETRC if (!ok_blook(v15_compat) || !xok_blook(netrc_lookup, urlp, OXM_PLAIN | OXM_H_P) || - _nrc_lookup(urlp, FAL0) != NRC_RESOK) + !_nrc_lookup(urlp, FAL0)) #endif urlp->url_user.s = UNCONST(myname); } @@ -965,8 +959,8 @@ FL bool_t ccred_lookup(struct ccred *ccp, struct url *urlp) { char const *pstr, *authdef; - size_t plen, i; - char *vbuf, *s; + char *s; + enum okeys authokey; ui8_t authmask; enum {NONE=0, WANT_PASS=1<<0, REQ_PASS=1<<1, WANT_USER=1<<2, REQ_USER=1<<3} ware = NONE; @@ -979,42 +973,28 @@ ccred_lookup(struct ccred *ccp, struct url *urlp) default: case CPROTO_SMTP: pstr = "smtp"; - plen = sizeof("smtp") -1; + authokey = ok_v_smtp_auth; authmask = AUTHTYPE_NONE | AUTHTYPE_PLAIN | AUTHTYPE_LOGIN | AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI; authdef = "none"; break; case CPROTO_POP3: pstr = "pop3"; - plen = sizeof("pop3") -1; + authokey = ok_v_pop3_auth; authmask = AUTHTYPE_PLAIN; authdef = "plain"; break; case CPROTO_IMAP: pstr = "imap"; - plen = sizeof("imap") -1; + authokey = ok_v_imap_auth; authmask = AUTHTYPE_LOGIN | AUTHTYPE_CRAM_MD5 | AUTHTYPE_GSSAPI; authdef = "login"; break; } - /* Note: "password-" is longer than "-auth-", .url_u_h_p and .url_h_p */ - vbuf = ac_alloc(plen + sizeof("password-")-1 + urlp->url_u_h_p.l +1); - memcpy(vbuf, pstr, plen); - /* Authentication type */ - memcpy(vbuf + plen, "-auth-", i = sizeof("-auth-") -1); - i += plen; - /* -USER@HOST, -HOST, '' */ - memcpy(vbuf + i, urlp->url_u_h_p.s, urlp->url_u_h_p.l +1); - if ((s = vok_vlook(vbuf)) == NULL) { - memcpy(vbuf + i, urlp->url_h_p.s, urlp->url_h_p.l +1); - if ((s = vok_vlook(vbuf)) == NULL) { - vbuf[plen + sizeof("-auth") -1] = '\0'; - if ((s = vok_vlook(vbuf)) == NULL) - s = UNCONST(authdef); - } - } + if ((s = xok_VLOOK(authokey, urlp, OXM_ALL)) == NULL) + s = UNCONST(authdef); if (!asccasecmp(s, "none")) { ccp->cc_auth = "NONE"; @@ -1063,36 +1043,26 @@ ccred_lookup(struct ccred *ccp, struct url *urlp) if ((ccp->cc_pass = urlp->url_pass).s != NULL) goto jleave; - if ((s = xok_vlook(password, urlp, OXM_ALL)) == NULL) { - /* But before we go and deal with the absolute fallbacks, check wether - * we may look into .netrc */ + if ((s = xok_vlook(password, urlp, OXM_ALL)) != NULL) + goto js2pass; # ifdef HAVE_NETRC - if (xok_blook(netrc_lookup, urlp, OXM_ALL)) - switch (_nrc_lookup(urlp, TRU1)) { - default: - break; - case NRC_RESOK: - ccp->cc_pass = urlp->url_pass; - goto jleave; - case NRC_RESERROR: - fprintf(stderr, _(".netrc authentification failed " - "(missing password or user mismatch)\n")); - ccp = NULL; - goto jleave; - } + if (xok_blook(netrc_lookup, urlp, OXM_ALL) && _nrc_lookup(urlp, TRU1)) { + ccp->cc_pass = urlp->url_pass; + goto jleave; + } # endif - if ((ware & REQ_PASS) && (s = getpassword(NULL)) == NULL) { + if (ware & REQ_PASS) { + if ((s = getpassword(NULL)) != NULL) +js2pass: + ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s)); + else { fprintf(stderr, _("A password is necessary for %s authentication.\n"), pstr); ccp = NULL; - goto jleave; } } - if (s != NULL) - ccp->cc_pass.l = strlen(ccp->cc_pass.s = savestr(s)); jleave: - ac_free(vbuf); if (ccp != NULL && (options & OPT_D_VV)) fprintf(stderr, _("Credentials: host `%s', user `%s', pass `%s'\n"), urlp->url_h_p.s, (ccp->cc_user.s != NULL ? ccp->cc_user.s : ""), -- 2.11.4.GIT