Skip to content

Commit

Permalink
Enhancement: Fix issues identified by deepseek-coder-v2 (#229)
Browse files Browse the repository at this point in the history
- fixes issues identified by deepseek-coder-v2
- tiny enhancements
  • Loading branch information
mcdope authored Jul 8, 2024
1 parent c021bd0 commit a2d14f8
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 173 deletions.
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
23 changes: 17 additions & 6 deletions 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 All @@ -234,7 +240,7 @@ int pusb_is_loginctl_local()
if ((fp = popen(loginctl_cmd, "r")) == NULL)
{
log_debug(" Opening pipe for 'loginctl' failed, this is quite a wtf...\n");
return (0);
return 0;
}

char *is_remote = NULL;
Expand All @@ -250,17 +256,17 @@ int pusb_is_loginctl_local()

if (strcmp(is_remote, "no") == 0)
{
return (1);
return 1;
}
else
{
return (0);
return -1;
}
}
else
{
log_debug(" 'loginctl' returned nothing.\n");
return (0);
return 0;
}
}

Expand Down Expand Up @@ -288,7 +294,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 Expand Up @@ -386,11 +392,16 @@ int pusb_local_login(t_pusb_options *opts, const char *user, const char *service
log_debug(" Trying to check for remote access by loginctl\n");

int loginctl_remote = pusb_is_loginctl_local();
if (loginctl_remote != 0)
if (loginctl_remote == 1)
{
log_debug(" loginctl says this session is local\n");
local_request = 1;
}
else if (loginctl_remote == -1)
{
log_debug(" loginctl says this session is remote\n");
return 0;
}
else
{
log_debug(" Trying to get tty by loginctl\n");
Expand Down
Loading

0 comments on commit a2d14f8

Please sign in to comment.