[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[msmtp-users] interactively reading password from user



Hello,

I just did a quick hack to msmtp to use getpass() to ask the user for a
password if none is supplied in msmtprc and an option getpass is set to
on. I don't actually use it now since I set up authentication using
SSL/TLS certificates where libcrypto will ask me for the passphrase. But
perhaps the attached patch is of any help to anyone.

Thanks a lot for this nice program,
-- 
bye, Micha
--- msmtp-1.3.5/src/conf.c.org	Sat Feb  5 15:25:37 2005
+++ msmtp-1.3.5/src/conf.c	Sat Feb  5 15:58:42 2005
@@ -74,6 +74,7 @@
     a->auth_mech = NULL;
     a->username = NULL;
     a->password = NULL;
+    a->get_pass = 0;
     a->ntlmdomain = NULL;
     a->tls = 0;
     a->tls_key_file = NULL;
@@ -114,6 +115,7 @@
 	a->auth_mech = acc->auth_mech ? xstrdup(acc->auth_mech) : NULL;
 	a->username = acc->username ? xstrdup(acc->username) : NULL;
 	a->password = acc->password ? xstrdup(acc->password) : NULL;
+	a->get_pass = acc->get_pass;
 	a->ntlmdomain = acc->ntlmdomain ? xstrdup(acc->ntlmdomain) : NULL;
 	a->tls = acc->tls;
 	a->tls_key_file = acc->tls_key_file ? xstrdup(acc->tls_key_file) : NULL;
@@ -396,6 +398,10 @@
 	free(acc1->password);
 	acc1->password = acc2->password ? xstrdup(acc2->password) : NULL;
     }
+    if (acc2->mask & ACC_GET_PASS)
+    {
+	acc1->get_pass = acc2->get_pass;
+    }
     if (acc2->mask & ACC_NTLMDOMAIN)
     {
 	free(acc1->ntlmdomain);
@@ -816,6 +822,32 @@
 	    acc->mask |= ACC_PASSWORD;
 	    free(acc->password);
 	    acc->password = (*arg == '\0') ? NULL : xstrdup(arg);
+	}
+	else if (strcmp(cmd, "getpass") == 0)
+	{
+	    acc->mask |= ACC_GET_PASS;
+	    if (*arg == '\0')
+	    {
+		snprintf(errstr, errstr_bufsize, 
+			"line %d: command %s needs an argument", line, cmd);
+		e = CONF_ESYNTAX;
+		break;
+	    }
+	    else if (is_on(arg))
+	    {
+		acc->get_pass = 0;
+	    }
+	    else if (is_off(arg))
+	    {
+		acc->get_pass = 1;
+	    }
+	    else
+	    {
+		snprintf(errstr, errstr_bufsize, 
+			"line %d: invalid argument %s for command %s", line, arg, cmd);
+		e = CONF_ESYNTAX;
+		break;
+	    }
 	}
 	else if (strcmp(cmd, "ntlmdomain") == 0)
 	{
--- msmtp-1.3.5/src/conf.h.org	Sat Feb  5 15:30:30 2005
+++ msmtp-1.3.5/src/conf.h	Sat Feb  5 15:56:34 2005
@@ -66,6 +66,7 @@
 #define ACC_TLS_NOSTARTTLS	131072
 #define ACC_LOGFILE		262144
 #define ACC_SYSLOG		524228
+#define ACC_GET_PASS		1048576
 
 typedef struct
 {
@@ -87,6 +88,7 @@
     char *auth_mech;		/* authentication mechanism */
     char *username;		/* username for authentication */
     char *password;		/* password for authentication */
+    int get_pass;		/* get password from user if not specified */
     char *ntlmdomain;		/* domain for NTLM authentication */
     /* TLS / SSL */
     int tls;			/* flag: use TLS? */
--- msmtp-1.3.5/src/msmtp.c.org	Sat Feb  5 15:35:44 2005
+++ msmtp-1.3.5/src/msmtp.c	Sat Feb  5 18:26:54 2005
@@ -495,7 +495,7 @@
 	    msmtp_endsession(&srv, 1);
 	    return EX_UNAVAILABLE;
 	}
-	if ((e = smtp_auth(&srv, acc->host, acc->username, acc->password, 
+	if ((e = smtp_auth(&srv, acc->host, acc->username, acc->password, acc->get_pass,
 			acc->ntlmdomain, acc->auth_mech, msg, errstr)) != SMTP_EOK)
 	{
 	    msmtp_endsession(&srv, 1);
@@ -1354,7 +1354,7 @@
 	    e = EX_UNAVAILABLE;
 	    goto error_exit;
 	}
-	if ((e = smtp_auth(&srv, acc->host, acc->username, acc->password, 
+	if ((e = smtp_auth(&srv, acc->host, acc->username, acc->password, acc->get_pass,
 			acc->ntlmdomain, acc->auth_mech, msg, errstr)) != SMTP_EOK)
 	{
 	    msmtp_endsession(&srv, 1);
@@ -2658,6 +2658,7 @@
     }
     printf("user            = %s\n"
 	    "password        = %s\n"
+	    "getpass         = %s\n"
 	    "ntlmdomain      = %s\n"
 	    "tls             = %s\n"
 	    "tls_trust_file  = %s\n"
@@ -2667,6 +2668,7 @@
 	    "tls_certcheck   = %s\n",
 	    account->username ? account->username : "(not set)",
 	    account->password ? "*" : "(not set)",
+	    account->get_pass ? "off" : "on",
 	    account->ntlmdomain ? account->ntlmdomain : "(not set)",
 	    account->tls ? "on" : "off", 
 	    account->tls_trust_file ? account->tls_trust_file : "(not set)",
--- msmtp-1.3.5/src/smtp.c.org	Sat Feb  5 15:34:27 2005
+++ msmtp-1.3.5/src/smtp.c	Sat Feb  5 18:29:23 2005
@@ -36,6 +36,8 @@
 #include <ctype.h>
 #include <errno.h>
 extern int errno;
+#include <pwd.h>
+#include <unistd.h>
 
 #ifdef USE_GSASL
 #include <gsasl.h>
@@ -999,13 +1001,16 @@
 	const char *hostname,
 	const char *user, 
 	const char *password,
+	const int get_pass,
 	const char *ntlmdomain,
 	const char *auth_mech,
 	list_t **error_msg,
 	char *errstr)
 {
-#ifdef USE_GSASL
+    char *pw;
     int e;
+
+#ifdef USE_GSASL
     list_t *msg;
     Gsasl *ctx;
     Gsasl_session *sctx;
@@ -1302,29 +1307,49 @@
     }
     if (!password)
     {
+        char *gpw;
+
+	if (get_pass) {
+	    snprintf(errstr, errstr_bufsize,
+		    "authentication mechanism %s needs a password", auth_mech);
+	    return SMTP_EUNAVAIL;
+	}
+
+	gpw = getpass("smtp auth password: "); 
+	pw = strdup(gpw);
+	memset(gpw, 0, strlen(gpw));
+    } else {
+	pw = strdup(password);
+    }
+
+    if (!pw) {
 	snprintf(errstr, errstr_bufsize,
-		"authentication mechanism %s needs a password", auth_mech);
-	return SMTP_EUNAVAIL;
+		"error allocating memory for authentication password");
+	return SMTP_EINVAL;
     }
 
     if (strcmp(auth_mech, "CRAM-MD5") == 0)
     {
-	return smtp_auth_cram_md5(srv, user, password, error_msg, errstr);
+	e = smtp_auth_cram_md5(srv, user, pw, error_msg, errstr);
     }
     else if (strcmp(auth_mech, "PLAIN") == 0)
     {
-	return smtp_auth_plain(srv, user, password, error_msg, errstr);
+	e = smtp_auth_plain(srv, user, pw, error_msg, errstr);
     }
     else if (strcmp(auth_mech, "LOGIN") == 0)
     {
-	return smtp_auth_login(srv, user, password, error_msg, errstr);
+	e = smtp_auth_login(srv, user, pw, error_msg, errstr);
     }
     else
     {
 	snprintf(errstr, errstr_bufsize, 
 		"authentication mechanism %s not supported", auth_mech);
-	return SMTP_ELIBFAILED;
+	e = SMTP_ELIBFAILED;
     }
+
+    memset(pw, 0, strlen(pw));
+    free(pw);
+    return e;
 
 #endif /* not USE_GSASL */
 }
--- msmtp-1.3.5/src/smtp.h.org	Sat Feb  5 18:37:32 2005
+++ msmtp-1.3.5/src/smtp.h	Sat Feb  5 18:37:55 2005
@@ -233,6 +233,7 @@
 	const char *hostname,
 	const char *user, 
 	const char *password,
+	int get_pass,
 	const char *ntlmdomain,
 	const char *auth_mech,
 	list_t **error_msg,