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

feat: permit-open #1402

Merged
merged 10 commits into from
Oct 1, 2024
Merged

feat: permit-open #1402

merged 10 commits into from
Oct 1, 2024

Conversation

XavierChanth
Copy link
Member

@XavierChanth XavierChanth commented Sep 28, 2024

- What I did

  • Implemented permit open
  • Refactored parsing of manager list using a better approach which is more similar to permit open's
  • cleaned up memory bugs in handle_ssh_request and handle_npt_request

- How I did it

- How to verify it

  • No tests yet. will do on Monday.

- Description for the changelog
feat: permit-open

Comment on lines -25 to +28
char **permitopen;
char **permitopen_hosts;
uint16_t *permitopen_ports; // 0 = '*'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously permitopen was a list of strings, but now it is split up into a list of hosts and list of ports, "*" for port = 0

Comment on lines +109 to +126
// NPT ONLY
// Don't try optimizing this to reuse the permitopen struct from main.c.
// none of the memory duplication here is expensive, and it's a surface for bugs
permitopen_params permitopen;
permitopen.permitopen_len = params->permitopen_len;
permitopen.permitopen_hosts = params->permitopen_hosts;
permitopen.permitopen_ports = params->permitopen_ports;
permitopen.requested_host = cJSON_GetStringValue(requested_host);
permitopen.requested_port = cJSON_GetNumberValue(requested_port);

if (!should_permitopen(&permitopen)) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Ignoring request to localhost:%d\n",
permitopen.requested_port);
cJSON_Delete(envelope);
return;
}
// END NPT ONLY

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle permit open check for npt, after envelope has been decoded

@@ -138,15 +156,13 @@ void handle_npt_request(atclient *atclient, pthread_mutex_t *atclient_lock, sshn
char *buffer = NULL;

res = atclient_get_public_key(atclient, &atkey, &buffer, NULL);
atclient_atkey_free(&atkey);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of this file is memory clean up

@@ -142,15 +142,13 @@ void handle_ssh_request(atclient *atclient, pthread_mutex_t *atclient_lock, sshn
char *buffer = NULL;

res = atclient_get_public_key(atclient, &atkey, &buffer, NULL);
atclient_atkey_free(&atkey);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same memory clean up is the only change to handle_ssh_request

Comment on lines +213 to +217
if (params.policy == NULL) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Policy Manager: NULL");
} else {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Policy Manager: %s", params.policy);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Started policy implementation, but the flag is disabled for now.

Comment on lines +240 to +245
char *buf = malloc(sizeof(char) * 1024);
for (int i = 0; i < params.permitopen_len; i++) {
cJSON_AddItemToArray(allowed_services, cJSON_CreateString(params.permitopen[i]));
sprintf(buf, "%s:%u", params.permitopen_hosts[i], (unsigned int)params.permitopen_ports[i]);
cJSON_AddItemToArray(allowed_services, cJSON_CreateString(buf));
}
free(buf);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I split params.permitopen into params.permitopen_hosts and params.permitopen_ports

Comment on lines +540 to +548
// permitopen happens first for ssh so we can avoid a bunch of unnecessary tasks
permitopen.requested_host = "localhost";
permitopen.requested_port = params.local_sshd_port;
if (!should_permitopen(&permitopen)) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Ignoring request to localhost:%d\n",
params.local_sshd_port);
// TODO notify daemon doesn't permit connections to $requested_host:$requested_port
break;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permit open for ssh can be checked earlier since we don't need to decode the json in the request to check it.

Comment on lines +89 to +94
if ((parse_permitopen(permitopen, &params->permitopen_hosts, &params->permitopen_ports, &params->permitopen_len,
false) != 0)) {
printf("Failed to parse permit-open string\n");
free(params->permitopen_str);
return 1;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse permitopen early so we can check for invalid input.

if (params->manager_list == NULL) {
printf("Failed to allocate memory for manager list\n");
free(params->permitopen_str);
return 1;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored the manager_list code to a cleaner solution more similar to permitopen...
I was referencing this as I was doing the permitopen parsing and realized this need to be changed.

Comment on lines +24 to +26
// malloc pointers to each string, but don't malloc any more memory for individual char storage
*permitopen_hosts = malloc((sep_count) * sizeof(char *));
*permitopen_ports = malloc((sep_count) * sizeof(uint16_t));
Copy link
Member Author

@XavierChanth XavierChanth Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need individual char storage because we are pointing these pointers to the memory passed into the argv of the program. I replaced the : with null terminators to make the original hosts passed in valid c strings.

@XavierChanth XavierChanth marked this pull request as draft September 28, 2024 01:35
@XavierChanth
Copy link
Member Author

Draft until I've written tests for parsing of permit-open and manager list

gkc
gkc previously approved these changes Sep 28, 2024
Copy link
Contributor

@gkc gkc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@XavierChanth XavierChanth marked this pull request as ready for review October 1, 2024 03:12
@gkc gkc merged commit 0379157 into trunk Oct 1, 2024
9 checks passed
@gkc gkc deleted the c-sshnpd-permit-open branch October 1, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants