[PATCH 05/10] Create common functions to handle interactive prompts

Kevin Cernekee cernekee at gmail.com
Sun Oct 7 21:03:39 EDT 2012


The username/password prompts share a lot of logic, so consolidate them
into one place.

Signed-off-by: Kevin Cernekee <cernekee at gmail.com>
---
 main.c |  115 ++++++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 66 insertions(+), 49 deletions(-)

diff --git a/main.c b/main.c
index 6817032..e68ff29 100644
--- a/main.c
+++ b/main.c
@@ -1012,11 +1012,74 @@ static int validate_peer_cert(void *_vpninfo, OPENCONNECT_X509 *peer_cert,
 	}
 }
 
+/* Return value:
+ *  < 0, on error
+ *  >=0, otherwise, indicating the length of the response
+ */
+static int prompt_user(const char *prompt, char *buf, int max_len, int hide_chars)
+{
+	struct termios t;
+	char *p;
+
+	fprintf(stderr, "%s", prompt);
+	fflush(stderr);
+
+	tcgetattr(0, &t);
+	if (hide_chars)
+		t.c_lflag &= ~ECHO;
+	tcsetattr(0, TCSANOW, &t);
+
+	p = fgets(buf, max_len, stdin);
+
+	t.c_lflag |= ECHO;
+	tcsetattr(0, TCSANOW, &t);
+
+	if (hide_chars)
+		fprintf(stderr, "\n");
+
+	if (!p)
+		return -1;
+
+	p = strchr(buf, '\n');
+	if (p)
+		*p = 0;
+
+	return strlen(buf);
+}
+
+/* Return value:
+ *  < 0, on error
+ *  >=0, otherwise, indicating the length of the response
+ */
+static int prompt_opt(struct openconnect_info *vpninfo, struct oc_form_opt *opt,
+		      int hide_chars)
+{
+	int ret;
+
+	if (non_inter) {
+		vpn_progress(vpninfo, PRG_ERR,
+			     _("User input required in non-interactive mode\n"));
+		return -1;
+	}
+
+	opt->value=malloc(80);
+	if (!opt->value)
+		return -1;
+
+	ret = prompt_user(opt->label, opt->value, 80, hide_chars);
+
+	if (ret < 0) {
+		free(opt->value);
+		opt->value = NULL;
+	}
+
+	return ret;
+}
 
 /* Return value:
  *  < 0, on error
  *  = 0, when form was parsed and POST required
- *  = 1, when response was cancelled by user
+ *  = 1, when response was cancelled by user (allowed but unused)
  */
 static int process_auth_form(void *_vpninfo,
 			     struct oc_auth_form *form)
@@ -1117,25 +1180,8 @@ static int process_auth_form(void *_vpninfo,
 				opt->value = strdup(vpninfo->username);
 				if (!opt->value)
 					goto err;
-			} else if (non_inter) {
-				vpn_progress(vpninfo, PRG_ERR,
-					     _("User input required in non-interactive mode\n"));
+			} else if (prompt_opt(vpninfo, opt, 0) < 0)
 				goto err;
-			} else {
-				opt->value=malloc(80);
-				if (!opt->value)
-					goto err;
-
-				fprintf(stderr, "%s", opt->label);
-				fflush(stderr);
-
-				if (!fgets(opt->value, 80, stdin) || !strlen(opt->value))
-					goto err;
-
-				p = strchr(opt->value, '\n');
-				if (p)
-					*p = 0;
-			}
 
 		} else if (opt->type == OC_FORM_OPT_PASSWORD) {
 			if (vpninfo->password &&
@@ -1144,37 +1190,8 @@ static int process_auth_form(void *_vpninfo,
 				vpninfo->password = NULL;
 				if (!opt->value)
 					goto err;
-			} else if (non_inter) {
-				vpn_progress(vpninfo, PRG_ERR,
-					     _("User input required in non-interactive mode\n"));
+			} else if (prompt_opt(vpninfo, opt, 1) < 0)
 				goto err;
-			} else {
-				struct termios t;
-				opt->value=malloc(80);
-				if (!opt->value)
-					goto err;
-
-				fprintf(stderr, "%s", opt->label);
-				fflush(stderr);
-
-				tcgetattr(0, &t);
-				t.c_lflag &= ~ECHO;
-				tcsetattr(0, TCSANOW, &t);
-
-				p = fgets(opt->value, 80, stdin);
-
-				t.c_lflag |= ECHO;
-				tcsetattr(0, TCSANOW, &t);
-				fprintf(stderr, "\n");
-
-				if (!p || !strlen(opt->value))
-					goto err;
-
-				p = strchr(opt->value, '\n');
-				if (p)
-					*p = 0;
-			}
-
 		}
 	}
 
-- 
1.7.5.4




More information about the openconnect-devel mailing list