Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Fix issues identified by deepseek-coder-v2 #229

Merged
merged 15 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ static int pusb_conf_parse_options(
char *xpath = NULL;
size_t xpath_size;
int i;

// these can come from argv, so make sure nothing messes up snprintf later
char xpath_user[32] = { };
char xpath_service[32] = { };
snprintf(xpath_user, 32, "%s", user);
snprintf(xpath_service, 32, "%s", service);
snprintf(xpath_user, sizeof(xpath_user), "%s", user);
snprintf(xpath_service, sizeof(xpath_service), "%s", service);

struct s_opt_list opt_list[] = {
struct s_opt_list opt_list[] = {
{ CONF_DEVICE_XPATH, opts->device.name },
{ CONF_USER_XPATH, xpath_user },
{ CONF_SERVICE_XPATH, xpath_service },
Expand All @@ -71,12 +71,16 @@ static int pusb_conf_parse_options(
{
xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1;
xpath = xmalloc(xpath_size);
if (xpath == NULL) {
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, "");
pusb_conf_options_get_from(opts, xpath, doc);
xfree(xpath);
}
return (1);
return 1;
}

static int pusb_conf_device_get_property(
Expand All @@ -93,11 +97,15 @@ static int pusb_conf_device_get_property(

xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + strlen(property) + 1;
xpath = xmalloc(xpath_len);
if (xpath == NULL) {
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_len);
snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name, property);
retval = pusb_xpath_get_string(doc, xpath, store, size);
xfree(xpath);
return (retval);
return retval;
}

static int pusb_conf_parse_device(
Expand All @@ -108,13 +116,13 @@ static int pusb_conf_parse_device(
pusb_conf_device_get_property(opts, doc, "vendor", opts->device.vendor, sizeof(opts->device.vendor));
pusb_conf_device_get_property(opts, doc, "model", opts->device.model, sizeof(opts->device.model));

if (!pusb_conf_device_get_property(opts, doc, "serial", opts->device.serial, sizeof(opts->device.serial)))
if (!pusb_conf_device_get_property(opts, doc, "serial", opts->device.serial, sizeof(opts->device.serial)))
{
return (0);
return 0;
}

pusb_conf_device_get_property(opts, doc, "volume_uuid", opts->device.volume_uuid, sizeof(opts->device.volume_uuid));
return (1);
return 1;
}

int pusb_conf_init(t_pusb_options *opts)
Expand All @@ -125,10 +133,10 @@ int pusb_conf_init(t_pusb_options *opts)
if (uname(&u) == -1)
{
log_error("uname: %s\n", strerror(errno));
return (0);
return 0;
}
snprintf(opts->hostname, sizeof(opts->hostname), "%s", u.nodename);
if (strnlen(u.nodename, sizeof(u.nodename)) > sizeof(opts->hostname))
if (strnlen(u.nodename, sizeof(u.nodename)) > sizeof(opts->hostname))
{
log_info("Hostname \"%s\" is too long, truncating to \"%s\".\n", u.nodename, opts->hostname);
}
Expand All @@ -143,7 +151,7 @@ int pusb_conf_init(t_pusb_options *opts)
opts->one_time_pad = 1;
opts->pad_expiration = 3600;
opts->deny_remote = 1;
return (1);
return 1;
}

int pusb_conf_parse(
Expand All @@ -161,12 +169,12 @@ int pusb_conf_parse(
if (strnlen(user, sizeof(user)) > CONF_USER_MAXLEN)
{
log_error("Username \"%s\" is too long (max: %d).\n", user, CONF_USER_MAXLEN);
return (0);
return 0;
}
if (!(doc = xmlReadFile(file, NULL, 0)))
{
log_error("Unable to parse \"%s\".\n", file);
return (0);
return 0;
}
snprintf(device_xpath, sizeof(device_xpath), CONF_USER_XPATH, user, "device");
retval = pusb_xpath_get_string(
Expand All @@ -180,7 +188,7 @@ int pusb_conf_parse(
log_error("No authentication device configured for user \"%s\".\n", user);
xmlFreeDoc(doc);
xmlCleanupParser();
return (0);
return 0;
}
if (!pusb_conf_parse_options(opts, doc, user, service))
{
Expand Down
17 changes: 10 additions & 7 deletions src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,37 @@ static int pusb_device_connected(t_pusb_options *opts, UDisksClient *udisks)
{
drive = udisks_object_get_drive(object);
retval = strcmp(udisks_drive_get_serial(drive), opts->device.serial) == 0;
if (strcmp(opts->device.vendor, "Generic") != 0)

if (strcmp(opts->device.vendor, "Generic") != 0)
{
retval = retval && strcmp(udisks_drive_get_vendor(drive), opts->device.vendor) == 0;
}

if (strcmp(opts->device.model, "Generic") != 0)
if (strcmp(opts->device.model, "Generic") != 0)
{
retval = retval && strcmp(udisks_drive_get_model(drive), opts->device.model) == 0;
}

g_object_unref(drive);
if (retval) {
break;
}
}
}

if (retval)
if (retval)
{
log_info("Authentication device \"%s\" is connected.\n", opts->device.name);
}
else
else
{
log_error("Authentication device \"%s\" is not connected.\n", opts->device.name);
}

g_list_foreach(objects, (GFunc) g_object_unref, NULL);
for (i = 0; i < g_list_length(objects); ++i)
{
g_object_unref(g_list_nth(objects, i)->data);
}
g_list_free(objects);

return (retval);
Expand Down
8 changes: 7 additions & 1 deletion src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ char *pusb_get_tty_from_display_server(const char *display)
xfree(fd_path);
xfree(link_path);
xfree(fd_target);
closedir(d_proc);

return NULL;
}
Expand Down Expand Up @@ -221,6 +222,11 @@ char *pusb_get_tty_by_loginctl()
else
{
log_debug(" 'loginctl' returned nothing.\n");
if (pclose(fp))
{
log_debug(" Closing pipe for 'loginctl' failed, this is quite a wtf...\n");
}

return (0);
}
}
Expand Down Expand Up @@ -282,7 +288,7 @@ int pusb_local_login(t_pusb_options *opts, const char *user, const char *service

while (pid != 0)
{
pusb_get_process_name(pid, name);
pusb_get_process_name(pid, name, BUFSIZ);
log_debug(" Checking pid %6d (%s)...\n", pid, name);

if (strstr(name, "tmux") != NULL)
Expand Down
Loading
Loading