From 319d7107dfbed2e6248f61ad463fa6ac55ebe178 Mon Sep 17 00:00:00 2001 Message-Id: <319d7107dfbed2e6248f61ad463fa6ac55ebe178.1715974712.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 29 Jun 2008 15:42:43 +0100 Subject: [PATCH] Disobedience login window now only remembers password etc if they actually worked. Organization: Straylight/Edgeware From: Richard Kettlewell --- disobedience/client.c | 2 +- disobedience/login.c | 39 ++++++++++++++++++++++++++++----------- lib/client-common.c | 10 ++++++---- lib/client-common.h | 2 +- lib/client.c | 22 +++++++++++++--------- lib/client.h | 6 ++++++ lib/configuration.c | 4 ++-- lib/configuration.h | 1 + lib/eclient.c | 2 +- 9 files changed, 59 insertions(+), 29 deletions(-) diff --git a/disobedience/client.c b/disobedience/client.c index 2406903..80aef88 100644 --- a/disobedience/client.c +++ b/disobedience/client.c @@ -130,7 +130,7 @@ static void gtkclient_comms_error(void attribute((unused)) *u, */ static void gtkclient_protocol_error(void attribute((unused)) *u, void attribute((unused)) *v, - int code, + int attribute((unused)) code, const char *msg) { D(("gtkclient_protocol_error %s", msg)); gtk_label_set_text(GTK_LABEL(report_label), msg); diff --git a/disobedience/login.c b/disobedience/login.c index 7cb64c9..73aaada 100644 --- a/disobedience/login.c +++ b/disobedience/login.c @@ -49,7 +49,7 @@ struct login_window_item { const char *(*get)(void); /** @brief Set a new value */ - void (*set)(const char *value); + void (*set)(struct config *c, const char *value); /** @brief Flags * @@ -80,10 +80,21 @@ static const char *get_service(void) { return config->connect.s[1]; } static const char *get_username(void) { return config->username; } static const char *get_password(void) { return config->password ? config->password : ""; } -static void set_hostname(const char *s) { config->connect.s[0] = (char *)s; } -static void set_service(const char *s) { config->connect.s[1] = (char *)s; } -static void set_username(const char *s) { config->username = s; } -static void set_password(const char *s) { config->password = s; } +static void set_hostname(struct config *c, const char *s) { + c->connect.s[0] = (char *)s; +} + +static void set_service(struct config *c, const char *s) { + c->connect.s[1] = (char *)s; +} + +static void set_username(struct config *c, const char *s) { + c->username = s; +} + +static void set_password(struct config *c, const char *s) { + c->password = s; +} /** @brief Table used to generate the form */ static const struct login_window_item lwis[] = { @@ -96,11 +107,15 @@ static const struct login_window_item lwis[] = { static GtkWidget *lwi_entry[NLWIS]; -static void login_update_config(void) { +static void login_update_config(struct config *c) { size_t n; + if(c->connect.n < 2) { + c->connect.n = 2; + c->connect.s = xcalloc(2, sizeof (char *)); + } for(n = 0; n < NLWIS; ++n) - lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])))); + lwis[n].set(c, xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])))); } /** @brief Save current login details */ @@ -148,13 +163,17 @@ done: static void login_ok(GtkButton attribute((unused)) *button, gpointer attribute((unused)) userdata) { disorder_client *c; + struct config *tmpconfig = xmalloc(sizeof *tmpconfig); /* Copy the new config into @ref config */ - login_update_config(); + login_update_config(tmpconfig); /* Attempt a login with the new details */ c = disorder_new(0); - if(!disorder_connect(c)) { + if(!disorder_connect_generic(tmpconfig, c, + tmpconfig->username, tmpconfig->password, + NULL/*cookie*/)) { /* Success; save the config and start using it */ + login_update_config(config); login_save_config(); logged_in(); /* Pop down login window */ @@ -162,8 +181,6 @@ static void login_ok(GtkButton attribute((unused)) *button, } else { /* Failed to connect - report the error */ popup_msg(GTK_MESSAGE_ERROR, disorder_last(c)); - /* TODO it would be nice to restore the config (not the entry contents!) to - * the last known good one if we were already connected to something. */ } disorder_close(c); /* no use for this any more */ } diff --git a/lib/client-common.c b/lib/client-common.c index 53da8ac..2c63934 100644 --- a/lib/client-common.c +++ b/lib/client-common.c @@ -32,11 +32,13 @@ #include "mem.h" /** @brief Figure out what address to connect to + * @param c Configuration to honor * @param sap Where to store pointer to sockaddr * @param namep Where to store socket name * @return Socket length, or (socklen_t)-1 */ -socklen_t find_server(struct sockaddr **sap, char **namep) { +socklen_t find_server(struct config *c, + struct sockaddr **sap, char **namep) { struct sockaddr *sa; struct sockaddr_un su; struct addrinfo *res = 0; @@ -50,13 +52,13 @@ socklen_t find_server(struct sockaddr **sap, char **namep) { .ai_protocol = IPPROTO_TCP, }; - if(config->connect.n) { - res = get_address(&config->connect, &pref, &name); + if(c->connect.n) { + res = get_address(&c->connect, &pref, &name); if(!res) return -1; sa = res->ai_addr; len = res->ai_addrlen; } else { - name = config_get_file("socket"); + name = config_get_file2(c, "socket"); if(strlen(name) >= sizeof su.sun_path) { error(errno, "socket path is too long"); return -1; diff --git a/lib/client-common.h b/lib/client-common.h index dc50ee9..d642278 100644 --- a/lib/client-common.h +++ b/lib/client-common.h @@ -24,7 +24,7 @@ #include #include -socklen_t find_server(struct sockaddr **sap, char **namep); +socklen_t find_server(struct config *c, struct sockaddr **sap, char **namep); #endif /* CLIENT_COMMON_H */ diff --git a/lib/client.c b/lib/client.c index 84eb77e..ba1477a 100644 --- a/lib/client.c +++ b/lib/client.c @@ -37,7 +37,6 @@ #include "log.h" #include "mem.h" -#include "configuration.h" #include "queue.h" #include "client.h" #include "charset.h" @@ -248,6 +247,7 @@ static int dequote(int rc, char **rp) { } /** @brief Generic connection routine + * @param conf Configuration to follow * @param c Client * @param username Username to log in with or NULL * @param password Password to log in with or NULL @@ -258,10 +258,11 @@ static int dequote(int rc, char **rp) { * username must not be. If @p username is not NULL then nor may @p * password be. */ -static int disorder_connect_generic(disorder_client *c, - const char *username, - const char *password, - const char *cookie) { +int disorder_connect_generic(struct config *conf, + disorder_client *c, + const char *username, + const char *password, + const char *cookie) { int fd = -1, fd2 = -1, nrvec, rc; unsigned char *nonce; size_t nl; @@ -271,7 +272,7 @@ static int disorder_connect_generic(disorder_client *c, struct sockaddr *sa; socklen_t salen; - if((salen = find_server(&sa, &c->ident)) == (socklen_t)-1) + if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1) return -1; c->fpin = c->fpout = 0; if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) { @@ -363,7 +364,8 @@ error_rc: int disorder_connect_user(disorder_client *c, const char *username, const char *password) { - return disorder_connect_generic(c, + return disorder_connect_generic(config, + c, username, password, 0); @@ -399,7 +401,8 @@ int disorder_connect(disorder_client *c) { error(0, "no password configured"); return -1; } - return disorder_connect_generic(c, + return disorder_connect_generic(config, + c, username, password, 0); @@ -416,7 +419,8 @@ int disorder_connect(disorder_client *c) { */ int disorder_connect_cookie(disorder_client *c, const char *cookie) { - return disorder_connect_generic(c, + return disorder_connect_generic(config, + c, "guest", "", cookie); diff --git a/lib/client.h b/lib/client.h index 912fa32..094147c 100644 --- a/lib/client.h +++ b/lib/client.h @@ -28,6 +28,7 @@ #define CLIENT_H #include +#include "configuration.h" /** @brief Client data */ typedef struct disorder_client disorder_client; @@ -42,6 +43,11 @@ int disorder_connect_user(disorder_client *c, const char *username, const char *password); int disorder_connect_cookie(disorder_client *c, const char *cookie); +int disorder_connect_generic(struct config *conf, + disorder_client *c, + const char *username, + const char *password, + const char *cookie); int disorder_close(disorder_client *c); int disorder_version(disorder_client *c, char **versionp); int disorder_play(disorder_client *c, const char *track); diff --git a/lib/configuration.c b/lib/configuration.c index 241d101..540084c 100644 --- a/lib/configuration.c +++ b/lib/configuration.c @@ -1213,7 +1213,7 @@ static struct config *config_default(void) { return c; } -static char *get_file(struct config *c, const char *name) { +char *config_get_file2(struct config *c, const char *name) { char *s; byte_xasprintf(&s, "%s/%s", c->home, name); @@ -1411,7 +1411,7 @@ char *config_usersysconf(const struct passwd *pw) { } char *config_get_file(const char *name) { - return get_file(config, name); + return config_get_file2(config, name); } /* diff --git a/lib/configuration.h b/lib/configuration.h index cc7e1e3..ab8917a 100644 --- a/lib/configuration.h +++ b/lib/configuration.h @@ -311,6 +311,7 @@ int config_read(int server); /* re-read config, return 0 on success or non-0 on error. * Only updates @config@ if the new configuration is valid. */ +char *config_get_file2(struct config *c, const char *name); char *config_get_file(const char *name); /* get a filename within the home directory */ diff --git a/lib/eclient.c b/lib/eclient.c index e7353df..f86ecc7 100644 --- a/lib/eclient.c +++ b/lib/eclient.c @@ -490,7 +490,7 @@ static int start_connect(disorder_eclient *c) { socklen_t len; D(("start_connect")); - if((len = find_server(&sa, &c->ident)) == (socklen_t)-1) + if((len = find_server(config, &sa, &c->ident)) == (socklen_t)-1) return comms_error(c, "cannot look up server"); /* TODO better error */ if(c->fd != -1) { xclose(c->fd); -- [mdw]