C: check-root-user.c

This commit is contained in:
Alexander Popov 2024-05-16 20:40:00 +03:00
parent 60ba2c59e7
commit bbed68cff2
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
4 changed files with 123 additions and 110 deletions

5
code/C/.clang-format Normal file
View File

@ -0,0 +1,5 @@
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 2
ColumnLimit: 132
SortIncludes: Never

View File

@ -0,0 +1,18 @@
/**
* https://man.openbsd.org/getuid
*/
#include <unistd.h>
#include <stdio.h>
void main(int argc, char const *argv[]) {
uid_t user = getuid();
uid_t privs = geteuid();
printf("User: %u\n", user);
printf("Privs: %u\n", privs);
puts("::");
char *login = getlogin();
printf("%s\n", login);
}

View File

@ -3,10 +3,8 @@ atomic_bool running = true;
#define PORT 8000 #define PORT 8000
#define PREFIX "/api" #define PREFIX "/api"
int callback_default(const struct _u_request *request, int callback_default(const struct _u_request *request, struct _u_response *response, void *user_data);
struct _u_response *response, void *user_data); int callback_all_test_foo(const struct _u_request *request, struct _u_response *response, void *user_data);
int callback_all_test_foo(const struct _u_request *request,
struct _u_response *response, void *user_data);
void *thread_server(void *vargp) { void *thread_server(void *vargp) {
int ret; int ret;
@ -18,10 +16,8 @@ void *thread_server(void *vargp) {
instance.max_post_body_size = 1024; instance.max_post_body_size = 1024;
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/param/:name", 0, ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/param/:name", 0, &callback_all_test_foo, "user data 1");
&callback_all_test_foo, "user data 1"); ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, "/param/:name", 0, &callback_all_test_foo, "user data 2");
ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, "/param/:name", 0,
&callback_all_test_foo, "user data 2");
ulfius_set_default_endpoint(&instance, &callback_default, NULL); ulfius_set_default_endpoint(&instance, &callback_default, NULL);
@ -41,18 +37,15 @@ void *thread_server(void *vargp) {
ulfius_clean_instance(&instance); ulfius_clean_instance(&instance);
} }
int callback_default(const struct _u_request *request, int callback_default(const struct _u_request *request, struct _u_response *response, void *user_data) {
struct _u_response *response, void *user_data) {
(void)(request); (void)(request);
(void)(user_data); (void)(user_data);
ulfius_set_string_body_response(response, 404, ulfius_set_string_body_response(response, 404, "Page not found, do what you want");
"Page not found, do what you want");
return U_CALLBACK_CONTINUE; return U_CALLBACK_CONTINUE;
} }
int callback_all_test_foo(const struct _u_request *request, int callback_all_test_foo(const struct _u_request *request, struct _u_response *response, void *user_data) {
struct _u_response *response, void *user_data) {
// url_params = request->map_url // url_params = request->map_url
// post_params = request->map_post_body // post_params = request->map_post_body

View File

@ -2,128 +2,125 @@
#include "webui.h" #include "webui.h"
void my_function_string(webui_event_t* e) { void my_function_string(webui_event_t *e) {
// JavaScript: // JavaScript:
// webui_fn('MyID_One', 'Hello'); // webui_fn('MyID_One', 'Hello');
const char* str = webui_get_string(e); const char *str = webui_get_string(e);
printf("my_function_string: %s\n", str); // Hello printf("my_function_string: %s\n", str); // Hello
// Need Multiple Arguments? // Need Multiple Arguments?
// //
// WebUI support only one argument. To get multiple arguments // WebUI support only one argument. To get multiple arguments
// you can send a JSON string from JavaScript then decode it. // you can send a JSON string from JavaScript then decode it.
// Example: // Example:
// //
// my_json = my_json_decoder(str); // my_json = my_json_decoder(str);
// foo = my_json[0]; // foo = my_json[0];
// bar = my_json[1]; // bar = my_json[1];
} }
void my_function_integer(webui_event_t* e) { void my_function_integer(webui_event_t *e) {
// JavaScript: // JavaScript:
// webui_fn('MyID_Two', 123456789); // webui_fn('MyID_Two', 123456789);
long long number = webui_get_int(e); long long number = webui_get_int(e);
printf("my_function_integer: %lld\n", number); // 123456789 printf("my_function_integer: %lld\n", number); // 123456789
} }
void my_function_boolean(webui_event_t* e) { void my_function_boolean(webui_event_t *e) {
// JavaScript: // JavaScript:
// webui_fn('MyID_Three', true); // webui_fn('MyID_Three', true);
bool status = webui_get_bool(e); // True bool status = webui_get_bool(e); // True
if(status) if (status)
printf("my_function_boolean: True\n"); printf("my_function_boolean: True\n");
else else
printf("my_function_boolean: False\n"); printf("my_function_boolean: False\n");
} }
void my_function_with_response(webui_event_t* e) { void my_function_with_response(webui_event_t *e) {
// JavaScript: // JavaScript:
// const result = webui_fn('MyID_Four', number); // const result = webui_fn('MyID_Four', number);
long long number = webui_get_int(e); long long number = webui_get_int(e);
number = number * 2; number = number * 2;
printf("my_function_with_response: %lld\n", number); printf("my_function_with_response: %lld\n", number);
// Send back the response to JavaScript // Send back the response to JavaScript
webui_return_int(e, number); webui_return_int(e, number);
} }
int main() { int main() {
// HTML // HTML
const char* my_html = const char *my_html = "<html>"
"<html>" " <head>"
" <head>" " <title>Call C from JavaScript Example</title>"
" <title>Call C from JavaScript Example</title>" " <style>"
" <style>" " body {"
" body {" " color: white;"
" color: white;" " background: #0F2027;"
" background: #0F2027;" " text-align: center;"
" text-align: center;" " font-size: 16px;"
" font-size: 16px;" " font-family: sans-serif;"
" font-family: sans-serif;" " }"
" }" " </style>"
" </style>" " </head>"
" </head>" " <body>"
" <body>" " <h2>WebUI - Call C from JavaScript Example</h2>"
" <h2>WebUI - Call C from JavaScript Example</h2>" " <p>Call C function with argument (<em>See the logs in your terminal</em>)</p>"
" <p>Call C function with argument (<em>See the logs in your terminal</em>)</p>" " <br>"
" <br>" " <button onclick=\"webui_fn('MyID_One', 'Hello');\">Call my_function_string()</button>"
" <button onclick=\"webui_fn('MyID_One', 'Hello');\">Call my_function_string()</button>" " <br>"
" <br>" " <br>"
" <br>" " <button onclick=\"webui_fn('MyID_Two', 123456789);\">Call my_function_integer()</button>"
" <button onclick=\"webui_fn('MyID_Two', 123456789);\">Call my_function_integer()</button>" " <br>"
" <br>" " <br>"
" <br>" " <button onclick=\"webui_fn('MyID_Three', true);\">Call my_function_boolean()</button>"
" <button onclick=\"webui_fn('MyID_Three', true);\">Call my_function_boolean()</button>" " <br>"
" <br>" " <br>"
" <br>" " <p>Call C function and wait for the response</p>"
" <p>Call C function and wait for the response</p>" " <br>"
" <br>" " <button onclick=\"MyJS();\">Call my_function_with_response()</button>"
" <button onclick=\"MyJS();\">Call my_function_with_response()</button>" " <br>"
" <br>" " <br>"
" <br>" " <input type=\"text\" id=\"MyInputID\" value=\"2\">"
" <input type=\"text\" id=\"MyInputID\" value=\"2\">" " <script>"
" <script>" " function MyJS() {"
" function MyJS() {" " const MyInput = document.getElementById('MyInputID');"
" const MyInput = document.getElementById('MyInputID');" " const number = MyInput.value;"
" const number = MyInput.value;" " webui_fn('MyID_Four', number).then((response) => {"
" webui_fn('MyID_Four', number).then((response) => {" " MyInput.value = response;"
" MyInput.value = response;" " });"
" });" " }"
" }" " </script>"
" </script>" " <script src=\"/webui.js\"></script>"
" <script src=\"/webui.js\"></script>" " </body>"
" </body>" "</html>";
"</html>";
// Create a window // Create a window
size_t my_window = webui_new_window(); size_t my_window = webui_new_window();
// Bind HTML elements with C functions // Bind HTML elements with C functions
webui_bind(my_window, "MyID_One", my_function_string); webui_bind(my_window, "MyID_One", my_function_string);
webui_bind(my_window, "MyID_Two", my_function_integer); webui_bind(my_window, "MyID_Two", my_function_integer);
webui_bind(my_window, "MyID_Three", my_function_boolean); webui_bind(my_window, "MyID_Three", my_function_boolean);
webui_bind(my_window, "MyID_Four", my_function_with_response); webui_bind(my_window, "MyID_Four", my_function_with_response);
// Show the window // Show the window
webui_show(my_window, my_html); // webui_show_browser(my_window, my_html, Chrome); webui_show(my_window, my_html); // webui_show_browser(my_window, my_html, Chrome);
// Wait until all windows get closed // Wait until all windows get closed
webui_wait(); webui_wait();
return 0; return 0;
} }
#if defined(_MSC_VER) #if defined(_MSC_VER)
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) { int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) { return main(); }
return main();
}
#endif #endif