From 2cc93ff31f16e65c1c1f94b0044b32f6be67cbd3 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 11 Feb 2007 21:57:49 +0100 Subject: [PATCH] simplify lookup_user() It's easier to just pass the name of the user to be looked up. This fixes a memory leak btw. because u.name was strdupped and never freed. That leak wasn't serious though as is happened in the child process. --- command.c | 22 ++++++++++++---------- error.h | 2 +- user_list.c | 15 +++++++-------- user_list.h | 2 +- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/command.c b/command.c index 6f6fad7c..ffd660d6 100644 --- a/command.c +++ b/command.c @@ -793,7 +793,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) int numbytes, ret, argc, use_rc4 = 0; char buf[4096]; unsigned char crypt_buf[MAXLINE]; - struct user u; + struct user *u; struct server_command *cmd = NULL; long unsigned challenge_nr, chall_response; char **argv = NULL; @@ -825,16 +825,18 @@ int handle_connect(int fd, struct sockaddr_in *addr) goto err_out; if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9)) - u.name = para_strdup(buf + 5); /* client version < 0.2.6 */ + p = buf + 5; /* client version < 0.2.6 */ else { - u.name = para_strdup(buf + 9); /* client version >= 0.2.6 */ + p = buf + 9; /* client version >= 0.2.6 */ use_rc4 = 1; } PARA_DEBUG_LOG("received %s request for user %s\n", - use_rc4? "rc4" : "auth", u.name); - if ((ret = lookup_user(&u)) < 0) + use_rc4? "rc4" : "auth", p); + ret = -E_BAD_USER; + u = lookup_user(p); + if (!u) goto err_out; - ret = para_encrypt_challenge(u.rsa, challenge_nr, crypt_buf); + ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf); if (ret <= 0) goto err_out; numbytes = ret; @@ -855,11 +857,11 @@ int handle_connect(int fd, struct sockaddr_in *addr) || chall_response != challenge_nr) goto err_out; /* auth successful. Send 'Proceed' message */ - PARA_INFO_LOG("good auth for %s (%lu)\n", u.name, challenge_nr); + PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr); sprintf(buf, "%s", PROCEED_MSG); if (use_rc4) { init_rc4_keys(); - ret = para_encrypt_buffer(u.rsa, rc4_buf, 2 * RC4_KEY_LEN, + ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN, (unsigned char *)buf + PROCEED_MSG_LEN + 1); if (ret <= 0) goto err_out; @@ -891,7 +893,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) if (!(cmd = parse_cmd(command))) goto err_out; /* valid command, check permissions */ - ret = check_perms(u.perms, cmd); + ret = check_perms(u->perms, cmd); if (ret < 0) goto err_out; /* valid command and sufficient perms */ @@ -900,7 +902,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) mmd_lock(); mmd->num_commands++; mmd_unlock(); - PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u.name, + PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name, inet_ntoa(addr->sin_addr)); ret = cmd->handler(fd, argc, argv); if (ret >= 0) { diff --git a/error.h b/error.h index 34b18f19..b9709477 100644 --- a/error.h +++ b/error.h @@ -97,7 +97,6 @@ extern const char **para_errlist[]; #define USER_LIST_ERRORS \ PARA_ERROR(USERLIST, "failed to open user list file"), \ - PARA_ERROR(BAD_USER, "you don't exist. Go away."), \ #define OSX_WRITE_ERRORS \ @@ -203,6 +202,7 @@ extern const char **para_errlist[]; PARA_ERROR(AUDIOD_SYNTAX, "syntax error"), \ PARA_ERROR(UCRED_PERM, "permission denied"), \ PARA_ERROR(INVALID_AUDIOD_CMD, "invalid command"), \ + PARA_ERROR(BAD_USER, "you don't exist. Go away."), \ #define FILTER_CHAIN_ERRORS \ diff --git a/user_list.c b/user_list.c index b7d739c1..c6d45ca5 100644 --- a/user_list.c +++ b/user_list.c @@ -122,19 +122,18 @@ void init_user_list(char *user_list_file) /** * lookup user in user_list. * - * \param user: must initially contain the name of the user and is filled - * in by this function on success. + * \param name of the user * - * \return 1 on success and < 0 on errors. + * \return a pointer to the corresponding user struct if the user was found, + * \p NULL otherwise. */ -int lookup_user(struct user *user) +struct user *lookup_user(const char *name) { struct user *u; list_for_each_entry(u, &user_list, node) { - if (strcmp(u->name, user->name)) + if (strcmp(u->name, name)) continue; - *user = *u; - return 1; + return u; } - return -E_BAD_USER; + return NULL; } diff --git a/user_list.h b/user_list.h index 1ee73521..ce940376 100644 --- a/user_list.h +++ b/user_list.h @@ -46,4 +46,4 @@ struct user { }; void init_user_list(char *user_list_file); -int lookup_user(struct user *user); +struct user *lookup_user(const char *name); -- 2.39.2