From b9c9fd975f5ed0013b79016e48b5c1eee67b2728 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:02:14 +0100 Subject: [PATCH 01/16] Put, rather than print, single characters. --- uip/msgchk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uip/msgchk.c b/uip/msgchk.c index d7b2c2f0..0f164c39 100644 --- a/uip/msgchk.c +++ b/uip/msgchk.c @@ -336,7 +336,7 @@ checkmail (char *user, char *home, int datesw, int notifysw, int personal) if (datesw && st.st_atime) printf ("; last read on %s", dtime (&st.st_atime, 1)); if (notifysw) - printf ("\n"); + putchar('\n'); return status; } -- 2.56.1 From b525efe3dc0d0a8299ea5f691de5d9810197e795 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:02:14 +0100 Subject: [PATCH 02/16] Put, rather than print, single characters. --- uip/post.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uip/post.c b/uip/post.c index 13dbb58a..553a2258 100644 --- a/uip/post.c +++ b/uip/post.c @@ -1333,7 +1333,7 @@ pl (void) printf ("\n\t-- Folder Copies --\nfcc:\t"); for (i = 0; i < fccind; i++) printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : ""); - printf ("\n"); + putchar('\n'); } -- 2.56.1 From e8fe77294085e66b09cbb8043458f22b5b2ef1e6 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:02:14 +0100 Subject: [PATCH 03/16] Put, rather than print, single characters. --- uip/sendsbr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uip/sendsbr.c b/uip/sendsbr.c index 327a1ee4..66ce65d1 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -377,7 +377,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft, fclose (out); if (!pushsw && verbsw) { - printf ("\n"); + putchar('\n'); fflush (stdout); } -- 2.56.1 From 26ad296a3104a934bff84c1e77b6f73508169959 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:08:50 +0100 Subject: [PATCH 04/16] Add LEN(s) definition. LEN gives the strlen() of string constant s, excluding the terminating NUL. --- h/mh.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/h/mh.h b/h/mh.h index 513248cf..3c2fb061 100644 --- a/h/mh.h +++ b/h/mh.h @@ -43,6 +43,10 @@ typedef unsigned char boolean; /* not int so we can pack in a structure */ #define NMH_UNUSED(i) i #endif +/* LEN gives the strlen() of string constant s, excluding the + * terminating NUL. */ +#define LEN(s) (sizeof (s) - 1) + /* * char array that keeps track of size in both bytes and characters * Usage note: -- 2.56.1 From 2ff6abb1d4635521329716ea00f2c6c919929be5 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:09:47 +0100 Subject: [PATCH 05/16] Use LEN() on string constants instead of strlen(). --- sbr/seq_read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbr/seq_read.c b/sbr/seq_read.c index 6f807c65..8b58639a 100644 --- a/sbr/seq_read.c +++ b/sbr/seq_read.c @@ -142,7 +142,7 @@ seq_private (struct msgs *mp) char *cp; struct node *np; - alen = strlen ("atr-"); + alen = LEN("atr-"); plen = strlen (mp->foldpath) + 1; for (np = m_defs; np; np = np->n_next) { -- 2.56.1 From 13326ef23c4764f02e63a549b14492d80a7869a8 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:09:47 +0100 Subject: [PATCH 06/16] Use LEN() on string constants instead of strlen(). --- uip/rmf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uip/rmf.c b/uip/rmf.c index 31774745..4558ca30 100644 --- a/uip/rmf.c +++ b/uip/rmf.c @@ -216,7 +216,7 @@ rma (char *folder) char *cp; struct node *np, *pp; - alen = strlen ("atr-"); + alen = LEN("atr-"); plen = strlen (cp = m_mailpath (folder)) + 1; /* -- 2.56.1 From b22a846ca4b5e21a3bc3b6d48cf3aa12f181ede0 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:09:47 +0100 Subject: [PATCH 07/16] Use LEN() on string constants instead of strlen(). --- uip/slocal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uip/slocal.c b/uip/slocal.c index 789a5977..254744b2 100644 --- a/uip/slocal.c +++ b/uip/slocal.c @@ -1165,7 +1165,7 @@ get_sender (char *envelope, char **sender) return; } - i = strlen ("From "); + i = LEN("From "); strncpy (buffer, envelope + i, sizeof(buffer)); if ((cp = strchr(buffer, '\n'))) { *cp = 0; @@ -1251,7 +1251,7 @@ you_lose: * and massage the headers. Save * a copy of the "From " line for later. */ - i = strlen ("From "); + i = LEN("From "); while (fgets (buffer, sizeof(buffer), qfp)) { if (first) { first = 0; -- 2.56.1 From 61f3b98910da20d4f0935d34a69fd203c092bd8a Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:24:20 +0100 Subject: [PATCH 08/16] Replace strlen(), malloc(), and sprintf() with concat(). --- sbr/oauth_prof.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sbr/oauth_prof.c b/sbr/oauth_prof.c index e0fd7584..ab8bc5aa 100644 --- a/sbr/oauth_prof.c +++ b/sbr/oauth_prof.c @@ -139,10 +139,7 @@ mh_oauth_cred_fn(const char *svc) free(component); if (result == NULL) { - result = mh_xmalloc(sizeof "oauth-" - 1 - + strlen(svc) - + 1 /* '\0' */); - sprintf(result, "oauth-%s", svc); + result = concat("oauth-", svc, NULL); result_if_allocated = result; } else { result_if_allocated = NULL; -- 2.56.1 From 1555816b6ceb82559fdaff41df7fed70ca07e628 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:32:30 +0100 Subject: [PATCH 09/16] Replace sizeof string constant - 1 with LEN(). --- mts/smtp/smtp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mts/smtp/smtp.c b/mts/smtp/smtp.c index 26241ae1..4fe71547 100644 --- a/mts/smtp/smtp.c +++ b/mts/smtp/smtp.c @@ -813,7 +813,7 @@ again: ; &errstr)) != NULL ; ) { if (doingEHLO - && strncmp (buffer, "250", sizeof("250") - 1) == 0 + && strncmp (buffer, "250", LEN("250")) == 0 && (buffer[3] == '-' || doingEHLO == 2) && buffer[4]) { if (doingEHLO == 2) { -- 2.56.1 From c99fde787781d43d2ada1e89d82a0e6a561656b6 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:32:37 +0100 Subject: [PATCH 10/16] Replace sizeof string constant - 1 with LEN(). --- uip/show.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uip/show.c b/uip/show.c index a3970d86..b7f85fe1 100644 --- a/uip/show.c +++ b/uip/show.c @@ -447,7 +447,7 @@ invalid: if (*dp) { if ((result = !uprf (dp, "charset"))) goto out; - dp += sizeof("charset") - 1; + dp += LEN("charset"); while (isspace ((unsigned char) *dp)) dp++; if (*dp++ != '=') -- 2.56.1 From d404c6fa5232e0e8ed10ee90c261b50d60fd08ea Mon Sep 17 00:00:00 2001 From: David Levine Date: Wed, 19 Oct 2016 15:00:56 -0400 Subject: [PATCH 11/16] Pass CFLAGS to make distcheck via DISTCHECK_CONFIGURE_FLAGS. --- docs/contrib/build_nmh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/contrib/build_nmh b/docs/contrib/build_nmh index 73111439..6a9ede8c 100755 --- a/docs/contrib/build_nmh +++ b/docs/contrib/build_nmh @@ -277,8 +277,10 @@ fi [ $verbose -ge 1 ] && echo configuring . . . if [ -z "$CFLAGS" ]; then #### Only use these flags with gcc. - if cc -dM -E - < /dev/null 2>&1 | egrep __GNUC__ >/dev/null; then - CFLAGS="-g -O2 -ansi -pedantic" + if cc -dM -E - &1 | egrep __GNUC__ >/dev/null; then + #### configure will supply -g -O2 with gcc, but only if CFLAGS + #### isn't defined. + CFLAGS='-g -O2 -ansi -pedantic' fi fi @@ -302,6 +304,13 @@ if [ $status -eq 0 ]; then fi fi + if [ -n "$CFLAGS" ]; then + #### Pass DISTCHECK_CONFIGURE_FLAGS through an environment + #### variable to avoid automake's quoting. + DISTCHECK_CONFIGURE_FLAGS="CFLAGS='${CFLAGS}'" + export DISTCHECK_CONFIGURE_FLAGS + fi + [ $verbose -ge 1 ] && echo testing . . . checkoutput=`make $check AM_COLOR_TESTS=always 2>>"$logfile"` status=$? -- 2.56.1 From bed6327e0962c95394b530b7b1ebc40e2af28320 Mon Sep 17 00:00:00 2001 From: David Levine Date: Wed, 19 Oct 2016 19:53:42 -0400 Subject: [PATCH 12/16] Added locale profile component, value is passed to nmh_init(). --- man/mh-profile.man | 21 ++++++++++++++++- sbr/utils.c | 25 +++++++++++++-------- test/mhbuild/test-utf8-body | 45 ++++++++++++++++++++++++++++++++----- test/mhshow/test-charset | 14 +++++++++++- 4 files changed, 89 insertions(+), 16 deletions(-) diff --git a/man/mh-profile.man b/man/mh-profile.man index ae085cc4..0bf8cc84 100644 --- a/man/mh-profile.man +++ b/man/mh-profile.man @@ -1,4 +1,4 @@ -.TH MH-PROFILE %manext5% "September 29, 2016" "%nmhversion%" +.TH MH-PROFILE %manext5% "October 19, 2016" "%nmhversion%" .\" .\" %nmhwarning% .\" @@ -62,6 +62,21 @@ transactions in directory \*(lqMail\*(rq. This is the only mandatory profile entry. (profile, no default) .RE .PP +.BR locale : +locale +.RS 5 +Set the locale for all +.B nmh +programs except +.BR post , +.BR install-mh , +and +.BR slocal . +See the LC_ALL, LC_CTYPE, and LANG variables in the "ENVIRONMENT" +section below for a reference on how the locale is set if this profile +component is not used. +.RE +.PP .BR context : context .RS 5 @@ -800,6 +815,10 @@ variables whose names are all upper-case are user-settable; those whose names are lower-case only are used internally by nmh and should not generally be set by the user. .TP +LC_ALL, LC_CTYPE, and LANG +These variables are used to set the locale, see locale(1). +The \*(lqlocale\*(rq profile entry supersedes these. +.TP MAILDROP This variable tells .B inc diff --git a/sbr/utils.c b/sbr/utils.c index 73061709..fff1aacd 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -383,10 +383,8 @@ void TrimSuffixC(char *s, int c) int nmh_init(const char *argv0, int read_context) { - if (! setlocale(LC_ALL, "")) { - admonish(NULL, "setlocale failed, check your LC_ALL, LC_CTYPE, and " - "LANG environment variables"); - } + int status = OK; + char *locale; invo_name = r1bindex ((char *) argv0, '/'); @@ -439,15 +437,24 @@ nmh_init(const char *argv0, int read_context) { putchar ('\n'); } } - - return OK; } else { - int status = context_foil(NULL); - if (status != OK) { + if ((status = context_foil(NULL)) != OK) { advise("", "failed to create minimal profile/context"); } - return status; } + + /* Allow the user to set a locale in their profile. Otherwise, use the + "" string to pull it from their environment, see setlocale(3). */ + if ((locale = context_find ("locale")) == NULL) { + locale = ""; + } + + if (! setlocale (LC_ALL, locale)) { + admonish (NULL, "setlocale failed, check your LC_ALL, LC_CTYPE, and " + "LANG environment variables"); + } + + return status; } diff --git a/test/mhbuild/test-utf8-body b/test/mhbuild/test-utf8-body index 22de535c..5e1b3256 100755 --- a/test/mhbuild/test-utf8-body +++ b/test/mhbuild/test-utf8-body @@ -40,7 +40,8 @@ test_attachment () # # It's hard to calculate the exact Date: header post is going to # use, so we'll just use sed to remove the actual date so we can easily - # compare it against our "correct" output. And same for charset. + # compare it against our "correct" output. And same for charset, + # because mime_type() can rely on file(1) --mime-encoding. # sed -e 's/^Date:.*/Date:/' -e 's/; charset="[^;]*//' \ "`mhpath cur`" > "${testname}.actual" @@ -59,7 +60,7 @@ cat > "${MH_TEST_DIR}/Mail/draft" < To: Somebody Subject: Test -Attach: $MH_TEST_DIR/attachment.txt +Nmh-Attach: $MH_TEST_DIR/attachment.txt This is a test. EOF @@ -97,7 +98,7 @@ cat > "${MH_TEST_DIR}/Mail/draft" < To: Somebody Subject: Test -Attach: $MH_TEST_DIR/attachment.txt +Nmh-Attach: $MH_TEST_DIR/attachment.txt ¡Ay, caramba! EOF @@ -141,7 +142,7 @@ cat > "${MH_TEST_DIR}/Mail/draft" < To: Somebody Subject: Test -Attach: $MH_TEST_DIR/attachment.txt +Nmh-Attach: $MH_TEST_DIR/attachment.txt EOF cat > "${testname}.expected" < "${MH_TEST_DIR}/Mail/draft" < To: Somebody Subject: Test -Attach: $MH_TEST_DIR/attachment.txt +Nmh-Attach: $MH_TEST_DIR/attachment.txt ¡Ay, caramba! EOF @@ -187,6 +188,40 @@ EOF check "$expected" "$actual" +# previous test, but with locale profile entry +start_test '8-bit body with C locale with locale profile entry' + +printf 'locale: en_US.UTF-8\n' >> "${MH}" + +cat > "${testname}.expected" < +To: Somebody +Subject: Test +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" +Content-Transfer-Encoding: 8bit +Date: + +------- =_aaaaaaaaaa0 +Content-Type: text/plain +Content-Transfer-Encoding: 8bit + +¡Ay, caramba! + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; name="attachment.txt" +Content-Description: attachment.txt +Content-Disposition: attachment; filename="attachment.txt" +Content-Transfer-Encoding: 8bit + +8-bit attachment, ¡Ay, caramba! + +------- =_aaaaaaaaaa0-- +EOF + +test_attachment "${testname}.expected" + + rm -f ${MHMTSCONF} "${MH_TEST_DIR}/attachment.txt" finish_test diff --git a/test/mhshow/test-charset b/test/mhshow/test-charset index 9039764f..52a75c51 100755 --- a/test/mhshow/test-charset +++ b/test/mhshow/test-charset @@ -26,6 +26,7 @@ actual=$MH_TEST_DIR/$$.actual echo 'mhshow-charset-windows-1252: echo "This is a test"' >> "$MH" # Write message with bogus quoted-printable data. +start_test 'Write message with bogus quoted-printable data' msgfile=`mhpath new` msgnum=`basename $msgfile` cat > $msgfile < $actual 2>&1 -check "$expected" "$actual" : bogus quoted-printable data +check "$expected" "$actual" 'keep first' : bogus quoted-printable data + +start_test 'locale profile component' +#### Used LC_ALL above, now use locale profile component instead. +printf "locale: $LC_ALL\n" >> "${MH}" +unset LC_ALL + +run_prog mhshow $msgnum > $actual 2>&1 +check "$expected" "$actual" : locale profile component + + +finish_test exit $failed -- 2.56.1 From 90edb255effd0d29d94e662ca5bf3e9eda7ed122 Mon Sep 17 00:00:00 2001 From: Ken Hornstein Date: Thu, 20 Oct 2016 13:37:29 -0400 Subject: [PATCH 13/16] Rototill credentials code so that we only prompt for the username and password when it is necessary. --- h/mh.h | 7 +--- h/popsbr.h | 4 +-- h/prototypes.h | 59 +++++++++++++++++++++++++++++-- sbr/credentials.c | 88 +++++++++++++++++++++++++++++++++++++++-------- sbr/netsec.c | 69 ++++++++----------------------------- sbr/ruserpass.c | 6 ++-- uip/inc.c | 8 ++--- uip/mhparse.c | 2 +- uip/msgchk.c | 7 ++-- uip/popsbr.c | 25 +++++++++----- 10 files changed, 174 insertions(+), 101 deletions(-) diff --git a/h/mh.h b/h/mh.h index 3c2fb061..55825da0 100644 --- a/h/mh.h +++ b/h/mh.h @@ -396,12 +396,7 @@ typedef struct m_getfld_state *m_getfld_state_t; /* * credentials management */ -struct nmh_creds { - char *host; - char *user; - char *password; -}; - +struct nmh_creds; typedef struct nmh_creds *nmh_creds_t; /* diff --git a/h/popsbr.h b/h/popsbr.h index c155fd12..96d21a33 100644 --- a/h/popsbr.h +++ b/h/popsbr.h @@ -3,8 +3,8 @@ * popsbr.h -- header for POP client subroutines */ -int pop_init (char *, char *, char *, char *, char *, int, int, char *, int, - const char *); +int pop_init (char *, char *, char *, char *, int, int, char *, int, + const char *); int pop_fd (char *, int, char *, int); int pop_stat (int *, int *); int pop_retr (int, int (*)(char *)); diff --git a/h/prototypes.h b/h/prototypes.h index f06322b5..a17d12f9 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -325,7 +325,23 @@ char *pwd (void); char *r1bindex(char *, int); void readconfig (struct node **, FILE *, const char *, int); int refile (char **, char *); -void ruserpass (char *, char **, char **); + +/* + * Read our credentials file and (optionally) ask the user for anything + * missing. + * + * Arguments: + * + * host - Hostname (to scan credentials file) + * aname - Pointer to filled-in username + * apass - Pointer to filled-in password + * flags - One or more of RUSERPASS_NO_PROMPT_USER, + * RUSERPASS_NO_PROMPT_PASSWORD + */ +void ruserpass (const char *host, char **aname, char **apass, int flags); +#define RUSERPASS_NO_PROMPT_USER 0x01 +#define RUSERPASS_NO_PROMPT_PASSWORD 0x02 + int remdir (char *); void scan_detect_mbox_style (FILE *); void scan_finished (); @@ -497,7 +513,46 @@ void hexify (const unsigned char *, size_t, char **); * credentials management */ void init_credentials_file (); -int nmh_get_credentials (char *, char *, int, nmh_creds_t); + +/* + * Allocate and return a credentials structure. The credentials structure + * is now opaque; you need to use accessors to get inside of it. The + * accessors will only prompt the user for missing fields if they are + * needed. + * + * Arguments: + * + * host - Hostname we're connecting to (used to search credentials file) + * user - Username we are logging in as; can be NULL. + * + * Returns NULL on error, otherwise an allocated nmh_creds structure. + */ +nmh_creds_t nmh_get_credentials (const char *host, const char *user); + +/* + * Retrieve the user from a nmh_creds structure. May prompt the user + * if one is not defined. + * + * Arguments: + * + * creds - Structure from previous nmh_get_credentials() call + * + * Returns NULL on error, otherwise a NUL-termined string containing + * the username. Points to allocated memory in the credentials structure + * that is free()d by nmh_free_credentials(). + */ +const char *nmh_cred_get_user(nmh_creds_t creds); + +/* + * Retrieve the password from an nmh_creds structure. Otherwise identical + * to nmh_cred_get_user(). + */ +const char *nmh_cred_get_password(nmh_creds_t creds); + +/* + * Free an allocated nmh_creds structure. + */ +void nmh_credentials_free(nmh_creds_t creds); /* * program initialization diff --git a/sbr/credentials.c b/sbr/credentials.c index 9b5dd537..9000e5ed 100644 --- a/sbr/credentials.c +++ b/sbr/credentials.c @@ -8,6 +8,12 @@ #include #include +struct nmh_creds { + char *host; /* Hostname corresponding to credentials */ + char *user; /* Username corresponding to credentials */ + char *pass; /* (Optional) password used by credentials */ +}; + void init_credentials_file () { if (credentials_file == NULL) { @@ -44,21 +50,23 @@ init_credentials_file () { } } -int -nmh_get_credentials (char *host, char *user, int sasl, nmh_creds_t creds) { +nmh_creds_t +nmh_get_credentials (const char *host, const char *user) +{ + nmh_creds_t creds; + char *cred_style = context_find ("credentials"); init_credentials_file (); - creds->host = host; - if (cred_style == NULL || ! strcmp (cred_style, "legacy")) { - creds->user = user == NULL ? getusername () : user; - if (sasl) { + creds = mh_xmalloc(sizeof(*creds)); - /* This is what inc.c and msgchk.c used to contain. */ - /* Only inc.c and msgchk.c do this. smtp.c doesn't. */ - creds->password = getusername (); - } + creds->host = getcpy(host); + creds->user = NULL; + creds->pass = NULL; + + if (cred_style == NULL || ! strcmp (cred_style, "legacy")) { + creds->user = user == NULL ? getcpy(getusername ()) : getcpy(user); } else if (! strncasecmp (cred_style, "file:", 5) || ! strncasecmp (cred_style, "file-nopermcheck:", 17)) { /* @@ -69,12 +77,64 @@ nmh_get_credentials (char *host, char *user, int sasl, nmh_creds_t creds) { * 3) interactively request from user (as long as the * credentials file didn't have a "default" token) */ - creds->user = user; + creds->user = user == NULL ? NULL : getcpy(user); } else { admonish (NULL, "unknown credentials style %s", cred_style); - return NOTOK; + return NULL; + } + + ruserpass(creds->host, &creds->user, &creds->pass, + RUSERPASS_NO_PROMPT_USER | RUSERPASS_NO_PROMPT_PASSWORD); + + return creds; +} + +/* + * Retrieve the username + */ + +const char * +nmh_cred_get_user(nmh_creds_t creds) +{ + if (! creds->user) { + ruserpass(creds->host, &creds->user, &creds->pass, + RUSERPASS_NO_PROMPT_PASSWORD); + } + + return creds->user; +} + +/* + * Retrieve the password + */ + +const char * +nmh_cred_get_password(nmh_creds_t creds) +{ + if (! creds->pass) { + ruserpass(creds->host, &creds->user, &creds->pass, 0); + } + + return creds->pass; +} + +/* + * Free our credentials + */ + +void +nmh_credentials_free(nmh_creds_t creds) +{ + if (creds->host) + free(creds->host); + + if (creds->user) + free(creds->user); + + if (creds->pass) { + memset(creds->pass, 0, strlen(creds->pass)); + free(creds->pass); } - ruserpass (host, &creds->user, &creds->password); - return OK; + free(creds); } diff --git a/sbr/netsec.c b/sbr/netsec.c index 788b23bf..990b2318 100644 --- a/sbr/netsec.c +++ b/sbr/netsec.c @@ -209,12 +209,8 @@ netsec_shutdown(netsec_context *nsc, int closeflag) free(nsc->sasl_hostname); if (nsc->sasl_cbs) free(nsc->sasl_cbs); - if (nsc->sasl_creds) { - if (nsc->sasl_creds->password) - memset(nsc->sasl_creds->password, 0, - strlen(nsc->sasl_creds->password)); - free(nsc->sasl_creds); - } + if (nsc->sasl_creds) + nmh_credentials_free(nsc->sasl_creds); if (nsc->sasl_secret) { if (nsc->sasl_secret->len > 0) { memset(nsc->sasl_secret->data, 0, nsc->sasl_secret->len); @@ -1003,6 +999,13 @@ netsec_set_sasl_params(netsec_context *nsc, const char *hostname, } nsc->sasl_hostname = mh_xstrdup(hostname); + + /* + * Set up our credentials + */ + + nsc->sasl_creds = nmh_get_credentials(nsc->sasl_hostname, nsc->ns_userid); + #else /* CYRUS_SASL */ NMH_UNUSED(hostname); NMH_UNUSED(service); @@ -1043,35 +1046,10 @@ int netsec_get_user(void *context, int id, const char **result, if (! result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) return SASL_BADPARAM; - if (nsc->ns_userid == NULL) { - /* - * Pass the 1 third argument to nmh_get_credentials() so that - * a default user if the -user switch wasn't supplied, and so - * that a default password will be supplied. That's used when - * those values really don't matter, and only with legacy/.netrc, - * i.e., with a credentials profile entry. - */ - - if (nsc->sasl_creds == NULL) { - NEW(nsc->sasl_creds); - nsc->sasl_creds->user = NULL; - nsc->sasl_creds->password = NULL; - } - - if (nmh_get_credentials(nsc->sasl_hostname, nsc->ns_userid, 1, - nsc->sasl_creds) != OK) - return SASL_BADPARAM; + *result = nmh_cred_get_user(nsc->sasl_creds); - if (nsc->ns_userid != nsc->sasl_creds->user) { - if (nsc->ns_userid) - free(nsc->ns_userid); - nsc->ns_userid = getcpy(nsc->sasl_creds->user); - } - } - - *result = nsc->ns_userid; if (len) - *len = strlen(nsc->ns_userid); + *len = strlen(*result); return SASL_OK; } @@ -1085,6 +1063,7 @@ netsec_get_password(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret) { netsec_context *nsc = (netsec_context *) context; + const char *password; int len; NMH_UNUSED(conn); @@ -1092,27 +1071,9 @@ netsec_get_password(sasl_conn_t *conn, void *context, int id, if (! psecret || id != SASL_CB_PASS) return SASL_BADPARAM; - if (nsc->sasl_creds == NULL) { - NEW(nsc->sasl_creds); - nsc->sasl_creds->user = NULL; - nsc->sasl_creds->password = NULL; - } - - if (nsc->sasl_creds->password == NULL) { - /* - * Pass the 0 third argument to nmh_get_credentials() so - * that the default password isn't used. With legacy/.netrc - * credentials support, we'll only get here if the -user - * switch to send(1)/post(8) wasn't used. - */ - - if (nmh_get_credentials(nsc->sasl_hostname, nsc->ns_userid, 0, - nsc->sasl_creds) != OK) { - return SASL_BADPARAM; - } - } + password = nmh_cred_get_password(nsc->sasl_creds); - len = strlen(nsc->sasl_creds->password); + len = strlen(password); /* * sasl_secret_t includes 1 bytes for "data" already, so that leaves @@ -1125,7 +1086,7 @@ netsec_get_password(sasl_conn_t *conn, void *context, int id, return SASL_NOMEM; (*psecret)->len = len; - strcpy((char *) (*psecret)->data, nsc->sasl_creds->password); + strcpy((char *) (*psecret)->data, password); nsc->sasl_secret = *psecret; diff --git a/sbr/ruserpass.c b/sbr/ruserpass.c index de0f951b..610f32a5 100644 --- a/sbr/ruserpass.c +++ b/sbr/ruserpass.c @@ -59,7 +59,7 @@ static int token(char *); void -ruserpass(char *host, char **aname, char **apass) +ruserpass(const char *host, char **aname, char **apass, int flags) { int t, usedefault = 0; struct stat stb; @@ -133,7 +133,7 @@ ruserpass(char *host, char **aname, char **apass) } } - if (!*aname) { + if (!*aname && ! (flags & RUSERPASS_NO_PROMPT_USER)) { char tmp[80]; char *myname; @@ -156,7 +156,7 @@ ruserpass(char *host, char **aname, char **apass) *aname = mh_xstrdup(myname); } - if (!*apass) { + if (!*apass && ! (flags & RUSERPASS_NO_PROMPT_PASSWORD)) { char prompt[256]; char *mypass; diff --git a/uip/inc.c b/uip/inc.c index 83b6ec96..aadfe98b 100644 --- a/uip/inc.c +++ b/uip/inc.c @@ -413,25 +413,21 @@ main (int argc, char **argv) * a POP server? */ if (inc_type == INC_POP) { - struct nmh_creds creds = { 0, 0, 0 }; - if (auth_svc == NULL) { if (saslmech && ! strcasecmp(saslmech, "xoauth2")) { adios (NULL, "must specify -authservice with -saslmech xoauth2"); } - nmh_get_credentials (host, user, sasl, &creds); } else { if (user == NULL) { adios (NULL, "must specify -user with -saslmech xoauth2"); } - creds.user = user; } /* * initialize POP connection */ - if (pop_init (host, port, creds.user, creds.password, proxy, snoop, - sasl, saslmech, tls, auth_svc) == NOTOK) + if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, + tls, auth_svc) == NOTOK) adios (NULL, "%s", response); /* Check if there are any messages */ diff --git a/uip/mhparse.c b/uip/mhparse.c index cb7d5652..69ed3bd4 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -2515,7 +2515,7 @@ openFTP (CT ct, char **file) LocalName (1)); pass = buffer; } else { - ruserpass (e->eb_site, &username, &password); + ruserpass (e->eb_site, &username, &password, 0); user = username; pass = password; } diff --git a/uip/msgchk.c b/uip/msgchk.c index 0f164c39..a4613c27 100644 --- a/uip/msgchk.c +++ b/uip/msgchk.c @@ -350,23 +350,20 @@ remotemail (char *host, char *port, char *user, char *proxy, int notifysw, const char *auth_svc) { int nmsgs, nbytes, status; - struct nmh_creds creds = { 0, 0, 0 }; if (auth_svc == NULL) { if (saslmech && ! strcasecmp(saslmech, "xoauth2")) { adios (NULL, "must specify -authservice with -saslmech xoauth2"); } - nmh_get_credentials (host, user, sasl, &creds); } else { if (user == NULL) { adios (NULL, "must specify -user with -saslmech xoauth2"); } - creds.user = user; } /* open the POP connection */ - if (pop_init (host, port, creds.user, creds.password, proxy, snoop, sasl, - saslmech, tls, auth_svc) == NOTOK + if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, tls, + auth_svc) == NOTOK || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */ || pop_quit () == NOTOK) { /* quit POP connection */ advise (NULL, "%s", response); diff --git a/uip/popsbr.c b/uip/popsbr.c index cf6b8042..50cdf09b 100644 --- a/uip/popsbr.c +++ b/uip/popsbr.c @@ -18,7 +18,6 @@ #define TRMLEN (sizeof TRM - 1) static int poprint = 0; -static int pophack = 0; char response[BUFSIZ]; static netsec_context *nsc = NULL; @@ -134,8 +133,8 @@ parse_proxy(char *proxy, char *host) } int -pop_init (char *host, char *port, char *user, char *pass, char *proxy, - int snoop, int sasl, char *mech, int tls, const char *oauth_svc) +pop_init (char *host, char *port, char *user, char *proxy, int snoop, + int sasl, char *mech, int tls, const char *oauth_svc) { int fd1, fd2; char buffer[BUFSIZ]; @@ -253,11 +252,21 @@ pop_init (char *host, char *port, char *user, char *pass, char *proxy, return NOTOK; } return OK; - } else - if (command ("USER %s", user) != NOTOK - && command ("%s %s", (pophack++, "PASS"), - pass) != NOTOK) - return OK; + } else { + nmh_creds_t creds; + + if (!(creds = nmh_get_credentials(host, user))) + return NOTOK; + if (command ("USER %s", nmh_cred_get_user(creds)) + != NOTOK) { + if (command("PASS %s", nmh_cred_get_password(creds)) + != NOTOK) { + nmh_credentials_free(creds); + return OK; + } + } + nmh_credentials_free(creds); + } } strncpy (buffer, response, sizeof(buffer)); command ("QUIT"); -- 2.56.1 From b1722537108cceec472480a551b9a7fb9c660c55 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 00:43:52 +0100 Subject: [PATCH 14/16] Hoist test of search string for NULL out of loop. --- uip/mhparam.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/uip/mhparam.c b/uip/mhparam.c index bf0dfae7..55ac3c31 100644 --- a/uip/mhparam.c +++ b/uip/mhparam.c @@ -265,8 +265,10 @@ p_find(char *str) { struct proc *ps; + if (!str) + str = ""; for (ps = procs; ps->p_name; ps++) - if (!strcasecmp (ps->p_name, str ? str : "")) + if (!strcasecmp (ps->p_name, str)) return (*ps->p_field); return NULL; -- 2.56.1 From 0fe9d768626320191fb3224c6e4f1896cd16fc8c Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 23:15:53 +0100 Subject: [PATCH 15/16] Don't `else' after return. Simplify control flow. --- config/config.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/config.c b/config/config.c index e1c4e5b8..020d610d 100644 --- a/config/config.c +++ b/config/config.c @@ -72,9 +72,8 @@ try_it: /* Will leak because caller doesn't know cp was dynamically allocated. */ return cp; - } else { - free (cp); } + free (cp); } /* Check nmh `etc' directory */ -- 2.56.1 From c6b3a45192568ef4679d9ca32af1806522bd295c Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Thu, 20 Oct 2016 23:16:21 +0100 Subject: [PATCH 16/16] Don't `else' after return. Simplify control flow. --- sbr/datetime.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sbr/datetime.c b/sbr/datetime.c index 8478414e..fb7f7c29 100644 --- a/sbr/datetime.c +++ b/sbr/datetime.c @@ -143,9 +143,9 @@ parse_datetime (const char *datetime, const char *zone, int dst, } return OK; - } else { - return NOTOK; } + + return NOTOK; } tzdesc_t @@ -359,10 +359,9 @@ format_datetime (tzdesc_t timezones, const contentline *node) { Form #2: DATE WITH UTC TIME */ if (parse_datetime (node->value, NULL, 0, &tws[0]) == OK) { return strdup (dasctime (&tws[0], 0)); - } else { - advise (NULL, "unable to parse datetime %s", node->value); - return NULL; } + advise (NULL, "unable to parse datetime %s", node->value); + return NULL; } /* -- 2.56.1