Auth using environment variables

Paul Walker <[email protected]> Tue, 9 Feb 2021 11:05:22 +0000
Newsgroups gmane.network.slrn.user
Message-ID <[email protected]>
--RnlQjJ0d97Da+TV1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi all

I've recently picked up slrn again, and I was looking for a method where I
didn't have to enter the password every time I wanted to use slrn, but
without putting the password in the slrnrc. Surprisingly, I couldn't find
one.

The attached patch adds NNTPUSER and NNTPPASS (to go with NNTPSERVER); I've
been using it for a week or so with no problems, but of course that's only
my experience.

Thanks
Paul

--RnlQjJ0d97Da+TV1
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="auth-env.patch"

diff --git a/doc/manual.txt b/doc/manual.txt
index f9e46e0..ff144d8 100644
--- a/doc/manual.txt
+++ b/doc/manual.txt
@@ -1368,6 +1368,10 @@
   startup. If your server requires a username, but no password, set it
   to a blank (" ") and you won't be prompted.
 
+  As an alternative to this, you can set NNTPUSER and NNTPPASS
+  environment variables. If those variables are set, slrn will try to use
+  those values for authenticating with the server.
+
   5.3.13.  posting_host
 
   Usage: posting_host hostname
diff --git a/doc/slrn.rc b/doc/slrn.rc
index b246f76..d198ddf 100644
--- a/doc/slrn.rc
+++ b/doc/slrn.rc
@@ -37,6 +37,7 @@
 
 % If a server requires authentication, add a nnrpaccess line for it.
 % If you leave username and/or password empty, slrn will prompt for it.
+% Alternatively you can set NNTPUSER and NNTPPASS environment variables.
 %nnrpaccess "news.doe.com" "john" "secret"
 
 % Some servers require authentication, but don't ask for it.
diff --git a/src/startup.c b/src/startup.c
index 4e4cf22..26ae6b4 100644
--- a/src/startup.c
+++ b/src/startup.c
@@ -1736,6 +1736,26 @@ static int nnrp_fun (int argc, SLcmd_Cmd_Table_Type *table) /*{{{*/
 }
 /*}}}*/
 
+static int slrn_get_auth_from_env(Server_List_Type *s) /*{{{*/
+{
+   extern char **environ;
+   size_t i;
+
+   for (i = 0; environ[i] != NULL; i++)
+   {
+     if (strncmp(environ[i], "NNTPUSER=", 9) == 0) {
+	char *user_start = &environ[i][9];
+	s->username = slrn_safe_strmalloc (user_start);
+     }
+     if (strncmp(environ[i], "NNTPPASS=", 9) == 0) {
+	char *pass_start = &environ[i][9];
+	s->password = slrn_safe_strmalloc (pass_start);
+     }
+   }
+   return 0;
+}
+/*}}}*/
+
 int slrn_get_authorization (char *host, int reqd, char **name, char **pass) /*{{{*/
 {
    Server_List_Type *s;
@@ -1753,6 +1773,8 @@ int slrn_get_authorization (char *host, int reqd, char **name, char **pass) /*{{
    if ((reqd == 0) && ((s->username == NULL) || (s->password == NULL)))
      return 0;
 
+   slrn_get_auth_from_env(s);
+
    if ((s->username == NULL) || (*s->username == 0))
      {
 	*buf = 0;

--RnlQjJ0d97Da+TV1
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--RnlQjJ0d97Da+TV1--