Skip to content

Commit

Permalink
support user cancel in openconnect_obtain_cookie()
Browse files Browse the repository at this point in the history
    Note changed return values:
     < 0  error
     = 0  no cookie (user cancel)
     = 1  obtained cookie
  • Loading branch information
Jussi Kukkonen committed Jan 24, 2009
1 parent 3513dd0 commit 05a691e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
66 changes: 46 additions & 20 deletions http.c
Expand Up @@ -355,6 +355,11 @@ static int parse_auth_choice(struct openconnect_info *vpninfo,
return 0;
}

/* Return value:
* < 0, on error
* = 0, when form was cancelled
* = 1, when form was parsed
*/
static int parse_form(struct openconnect_info *vpninfo, char *auth_id,
char *form_message, char *form_error, xmlNode *xml_node,
char *body, int bodylen)
Expand Down Expand Up @@ -483,11 +488,16 @@ static int parse_form(struct openconnect_info *vpninfo, char *auth_id,
UI_add_input_string(ui, pass_form_prompt, 0, password, 1, 80);
}

ret = UI_process(ui);
if (ret) {
switch (UI_process(ui)) {
case -2:
/* cancelled */
return 0;
case -1:
/* error */
vpninfo->progress(vpninfo, PRG_ERR, "Invalid inputs\n");
return -EINVAL;
}

if (user_form_id)
append_opt(body, bodylen, user_form_id,
vpninfo->username?:username);
Expand All @@ -496,17 +506,17 @@ static int parse_form(struct openconnect_info *vpninfo, char *auth_id,
/* First token request; mangle pin into _both_ first and next
token code */
int ret = add_securid_pin(vpninfo->sid_tokencode, tpin);
if (!ret)
if (ret < 0)
ret = add_securid_pin(vpninfo->sid_nexttokencode, tpin);
if (ret)
return ret;
if (ret < 0)
return -1;
passresult = vpninfo->sid_tokencode;
} else if (is_securid == 2 && vpninfo->sid_nexttokencode[0]) {
passresult = vpninfo->sid_nexttokencode;
} else if (is_securid && tpin[0]) {
ret = add_securid_pin(password, tpin);
if (ret)
return ret;
if (ret < 0)
return -1;
} else if (vpninfo->password)
passresult = vpninfo->password;

Expand All @@ -516,10 +526,16 @@ static int parse_form(struct openconnect_info *vpninfo, char *auth_id,
free(vpninfo->password);
vpninfo->password = NULL;
}
return 0;

return 1;
}

/* Return value:
* < 0, on error
* = 0,
* = 1, when response was parsed
* = 2, when response was cancelled
*/
static int parse_xml_response(struct openconnect_info *vpninfo, char *response,
char *request_body, int req_len)
{
Expand Down Expand Up @@ -564,6 +580,8 @@ static int parse_xml_response(struct openconnect_info *vpninfo, char *response,
form_error = (char *)xmlNodeGetContent(xml_node);
} else if (!strcmp((char *)xml_node->name, "form")) {
char *form_method, *form_action;
int ret;

form_method = (char *)xmlGetProp(xml_node, (unsigned char *)"method");
form_action = (char *)xmlGetProp(xml_node, (unsigned char *)"action");
if (strcasecmp(form_method, "POST")) {
Expand All @@ -574,17 +592,21 @@ static int parse_xml_response(struct openconnect_info *vpninfo, char *response,
}
free(vpninfo->urlpath);
vpninfo->urlpath = strdup(form_action);

if (parse_form(vpninfo, auth_id, form_message,
form_error, xml_node, request_body,
req_len)) {

ret = parse_form(vpninfo, auth_id, form_message,
form_error, xml_node, request_body,
req_len);
if (ret < 0) {
/* fail */
xmlFreeDoc(xml_doc);
return -EINVAL;
} else if (ret == 0) {
/* cancel */
return 2;
}

/* Let the caller know there's a form to be submitted */
return 1;

}
}

Expand Down Expand Up @@ -647,6 +669,11 @@ static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
return vpninfo->write_new_config(vpninfo, buf, buflen);
}

/* Return value:
* < 0, on error
* = 0, no cookie (user cancel)
* = 1, obtained cookie
*/
int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
{
struct vpn_option *opt, *next;
Expand Down Expand Up @@ -749,7 +776,10 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo)

request_body[0] = 0;
result = parse_xml_response(vpninfo, buf, request_body, sizeof(request_body));
if (result > 0) {
if (result == 2) {
/* cancel */
return 0;
} else if (result == 1) {
method = "POST";
request_body_type = "application/x-www-form-urlencoded";
if (0) {
Expand Down Expand Up @@ -792,11 +822,7 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
}
}

if (vpninfo->cookie)
return 0;

vpninfo->progress(vpninfo, PRG_ERR, "Server claimed successful login, but no cookie!\n");
return -1;
return 1;
}

char *openconnect_create_useragent(char *base)
Expand Down
2 changes: 1 addition & 1 deletion main.c
Expand Up @@ -312,7 +312,7 @@ int main(int argc, char **argv)
set_openssl_ui();
#endif

if (!vpninfo->cookie && openconnect_obtain_cookie(vpninfo)) {
if (!vpninfo->cookie && openconnect_obtain_cookie(vpninfo) != 1) {
fprintf(stderr, "Failed to obtain WebVPN cookie\n");
exit(1);
}
Expand Down
22 changes: 13 additions & 9 deletions nm-auth-dialog.c
Expand Up @@ -409,16 +409,17 @@ static int choose_vpnhost(struct openconnect_info *vpninfo)

}

/* Return value:
* < 0, error
* = 0, no cookie (user cancel)
* = 1, got cookie
*/
static int get_cookie(struct openconnect_info *vpninfo)
{
if (vpnhosts && choose_vpnhost(vpninfo))
return -ENOENT;
openconnect_init_openssl();
openconnect_obtain_cookie(vpninfo);

if (!vpninfo->cookie)
return -ENOENT;
return 0;
return openconnect_obtain_cookie(vpninfo);
}

int write_new_config(struct openconnect_info *vpninfo, char *buf, int buflen)
Expand Down Expand Up @@ -460,6 +461,7 @@ int main (int argc, char **argv)
struct openconnect_info *vpninfo;
int opt;
char read_buf;
int ret;

while ((opt = getopt_long(argc, argv, "ru:n:s:", long_options, NULL))) {
if (opt < 0)
Expand Down Expand Up @@ -526,7 +528,8 @@ int main (int argc, char **argv)
return 1;
}

if (get_cookie(vpninfo) || !vpninfo->hostname || !vpninfo->cookie) {
ret = get_cookie(vpninfo);
if (ret < 0) {
if (last_message) {
char *title, *msg;
GtkWidget *dlg;
Expand All @@ -547,12 +550,13 @@ int main (int argc, char **argv)
gtk_widget_destroy (dlg);
}
return 1;
} else if (ret == 1) {
printf("%s\n%s\n", NM_OPENCONNECT_KEY_GATEWAY, vpninfo->hostname);
printf("%s\n%s\n", NM_OPENCONNECT_KEY_COOKIE, vpninfo->cookie);
memset((void *)vpninfo->cookie, 0, strlen(vpninfo->cookie));
}
printf("%s\n%s\n", NM_OPENCONNECT_KEY_GATEWAY, vpninfo->hostname);
printf("%s\n%s\n", NM_OPENCONNECT_KEY_COOKIE, vpninfo->cookie);
printf("\n\n");

memset((void *)vpninfo->cookie, 0, strlen(vpninfo->cookie));

fflush (stdout);
(void)read(0, &read_buf, 1);
Expand Down

0 comments on commit 05a691e

Please sign in to comment.