Right now, it is already possible to script retrieving passwords from
arbitrary scripts. This goes only half the way though to allowing
automated derival of login details, as some environments may also
have different user names based on the system.

Add a new option UserCmd, which acts exactly the same as the already
existing PassCmd option.
---
 NEWS           |  2 ++
 src/drv_imap.c | 16 +++++++++++++++-
 src/mbsync.1   | 10 ++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index bea8968..ea7dc7f 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@
 
 The 'isync' compatibility wrapper was removed.
 
+IMAP user query can be scripted.
+
 [1.3.0]
 
 Network timeout handling has been added.
diff --git a/src/drv_imap.c b/src/drv_imap.c
index 30a3856..d8aff75 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -50,6 +50,7 @@ typedef struct imap_server_conf {
        char *name;
        server_conf_t sconf;
        char *user;
+       char *user_cmd;
        char *pass;
        char *pass_cmd;
        int max_in_progress;
@@ -1887,7 +1888,13 @@ spawn_cmd(const char *cmd, const char *type, const char 
*name)
 static const char *
 ensure_user( imap_server_conf_t *srvc )
 {
-       if (!srvc->user) {
+       if (srvc->user_cmd) {
+               char *user = spawn_cmd( srvc->user_cmd, "user", srvc->name );
+               if (!user)
+                       return 0;
+               free( srvc->user ); /* From previous runs */
+               srvc->user = user;
+       } else if (!srvc->user) {
                error( "Skipping account %s, no user\n", srvc->name );
                return 0;
        }
@@ -3135,6 +3142,8 @@ imap_parse_store( conffile_t *cfg, store_conf_t **storep )
                }
                else if (!strcasecmp( "User", cfg->cmd ))
                        server->user = nfstrdup( cfg->val );
+               else if (!strcasecmp( "UserCmd", cfg->cmd ))
+                       server->user_cmd = nfstrdup( cfg->val );
                else if (!strcasecmp( "Pass", cfg->cmd ))
                        server->pass = nfstrdup( cfg->val );
                else if (!strcasecmp( "PassCmd", cfg->cmd ))
@@ -3284,6 +3293,11 @@ imap_parse_store( conffile_t *cfg, store_conf_t **storep 
)
                        cfg->err = 1;
                        return 1;
                }
+               if (server->user && server->user_cmd) {
+                       error( "%s '%s' has both User and UserCmd\n", type, 
name );
+                       cfg->err = 1;
+                       return 1;
+               }
                if (server->pass && server->pass_cmd) {
                        error( "%s '%s' has both Pass and PassCmd\n", type, 
name );
                        cfg->err = 1;
diff --git a/src/mbsync.1 b/src/mbsync.1
index 4dcd5aa..3a2ce87 100644
--- a/src/mbsync.1
+++ b/src/mbsync.1
@@ -316,6 +316,16 @@ Zero means unlimited.
 Specify the login name on the IMAP server.
 ..
 .TP
+\fBUserCmd\fR [\fB+\fR]\fIcommand\fR
+Specify a shell command to obtain a user rather than specifying a
+user directly. This allows you to script retrieving user names.
+The command must produce exactly one line on stdout; the trailing newline is
+optional.
+Prepend \fB+\fR to the command to indicate that it produces TTY output
+(e.g., a prompt); failure to do so will merely produce messier
+output.
+..
+.TP
 \fBPass\fR \fIpassword\fR
 Specify the password for \fIusername\fR on the IMAP server.
 Note that this option is \fInot\fR required.
-- 
2.16.1


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to