From patchwork Thu Oct 6 17:59:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Roevens X-Patchwork-Id: 6044 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4MjzwG3sGdz3wgH for ; Thu, 6 Oct 2022 18:09:02 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4Mjzw82Rjwz2bD; Thu, 6 Oct 2022 18:08:56 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4Mjzw72nH9z301x; Thu, 6 Oct 2022 18:08:55 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4Mjzw62ycCz2yk8 for ; Thu, 6 Oct 2022 18:08:54 +0000 (UTC) Received: from knopi.disroot.org (knopi.disroot.org [178.21.23.139]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPS id 4Mjzw61sLHz1Cl for ; Thu, 6 Oct 2022 18:08:54 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id F06824C9D3 for ; Thu, 6 Oct 2022 20:08:53 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with UTF8SMTP id Xn6qVkapGXS4 for ; Thu, 6 Oct 2022 20:08:52 +0200 (CEST) Received: from chojin.sicho.home (amaterasu.sicho.home [192.168.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (no client certificate requested) (Authenticated sender) by hachiman (MailScanner Milter) with SMTP id ED47873539; Thu, 6 Oct 2022 20:00:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1665079223; bh=WV6TmhAUoB1JEDQCFVznE9wvNfIU3cDxriR1fnKb99I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Oy911lpdsOzvYrxHosHUJpc48Enlc9HwDXgiWSB09JxIbr3VIg3SYmDt1eCyTJw/t 7QJeKRZ85jQcrcAFGnRZgFhcSk/4P+gaSDknSnSqked23jnJaFjFRV/arwm+qaXjVR 1wnjivyiSB9kmbuQ49A8/hEWMjc0NC9F7OzGS0TnWJnyD2ma8CfdSRWiN4flN8xLwe ks+uqNfumJengOpdIVOu6CKbqiFtpczrCuOoEJQKWLjbfRCevCFVadTo9rfgbPrqv3 H5Znp14FuZK95Y31DVlwOlQEypvv/fAvOS3fydjsM6pLUpj9WAzFkISQG4pM/IGvKb NTbd6bi7HPt6w== From: Robin Roevens To: development@lists.ipfire.org Subject: [PATCH v2 1/5] misc-progs: addonctrl: Add support for 'Services' metadata Date: Thu, 6 Oct 2022 19:59:54 +0200 Message-Id: <20221006175958.11036-2-robin.roevens@disroot.org> In-Reply-To: <20221006175958.11036-1-robin.roevens@disroot.org> References: <20221006175958.11036-1-robin.roevens@disroot.org> Mime-Version: 1.0 X-sicho-MailScanner-ID: ED47873539.A8A80 X-sicho-MailScanner: Found to be clean X-sicho-MailScanner-From: robin.roevens@disroot.org X-sicho-MailScanner-Watermark: 1665684008.83121@k9GbiPT/YvuaQD0erXTyNw ARC-Seal: i=1; s=202003rsa; d=lists.ipfire.org; t=1665079734; a=rsa-sha256; cv=none; b=N7tFJo09w9FKhqRgVuo0m0Ss2HTXN+nc47w0fLE1NRIXlf0Y3hUL4z67x7dYMMIFQucVG3 5MpyxJI4pbx5Pa2qWVgGSmErstxO/UOBdokUPXgmVwk+sbD6FYo4KoCGUAJiV31pQUAohB N80+Kur6ydBMJymHGSQyb6CmqY2wlfW+cJUkzX4/+YdiZfn9jQ5B/J/Obo4jKY2APwls81 2LcH6wnJYS49a8v/pNioenVp0Tuy/zZx74L9xQ5Z7JlxsrY9BjInsw3dLTyc9WPX5r0HmZ WKA4eDMbSKMOr9SzHVNhNVYIVOvaykrlVNN6OogUSZyY1s41vKvIwPcS8MHyKQ== ARC-Authentication-Results: i=1; mail01.ipfire.org; dkim=pass header.d=disroot.org header.s=mail header.b=Oy911lpd; spf=pass (mail01.ipfire.org: domain of robin.roevens@disroot.org designates 178.21.23.139 as permitted sender) smtp.mailfrom=robin.roevens@disroot.org; dmarc=pass (policy=quarantine) header.from=disroot.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.ipfire.org; s=202003rsa; t=1665079734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1TjuutwJUcGfOMHg404O+6HgwKhv/A9yk6tNXhxSHMg=; b=iBFA7lErNDN1Di3mZb2F4uNUZSkteGsnmS5s2Bf1yfYyhtutC8OHg4/aGLIOnM+sVzUCPs tErgcb0iaZR66U9O7Tm1ezwKFa+8cpE27/iH1GeoKQD5oGva7mKyZS7aZIHeRQrl5RTDaK JErSBIlJtnMVk5YUADdEPFhUdZmdMsQllAs5B6qb2J7KJBoCNh6BCeQyiN2z7c3maF/maD JopypXSKosNyai1aq0WrNDB1+nxwy15BhesNgxhEqI4U0j22ql3KdcAQJOQFITDGAJNmso kruOIX4rW3uPctNXmih6yla/vHqFzWzA14L5rEAd0/rqbP7jj2DvAheKA1IcCg== Authentication-Results: mail01.ipfire.org; dkim=pass header.d=disroot.org header.s=mail header.b=Oy911lpd; spf=pass (mail01.ipfire.org: domain of robin.roevens@disroot.org designates 178.21.23.139 as permitted sender) smtp.mailfrom=robin.roevens@disroot.org; dmarc=pass (policy=quarantine) header.from=disroot.org X-Rspamd-Server: mail01.haj.ipfire.org X-Spamd-Result: default: False [-5.12 / 11.00]; BAYES_HAM(-3.00)[99.99%]; IP_REPUTATION_HAM(-1.10)[asn: 50673(-0.31), country: NL(-0.01), ip: 178.21.23.139(-0.78)]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM(-1.00)[-1.000]; DKIM_REPUTATION(-0.79)[-0.79182035360089]; SPF_REPUTATION_HAM(-0.68)[-0.68195333597372]; DMARC_POLICY_ALLOW(-0.50)[disroot.org,quarantine]; R_MISSING_CHARSET(0.50)[]; MV_CASE(0.50)[]; R_DKIM_ALLOW(0.26)[disroot.org:s=mail]; R_SPF_ALLOW(-0.20)[+a:c]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[development@lists.ipfire.org]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; RCVD_COUNT_THREE(0.00)[4]; ARC_SIGNED(0.00)[lists.ipfire.org:s=202003rsa:i=1]; ASN(0.00)[asn:50673, ipnet:178.21.23.0/24, country:NL]; DKIM_TRACE(0.00)[disroot.org:+]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[] X-Rspamd-Queue-Id: 4Mjzw61sLHz1Cl X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" * Addonctrl will now check in addon metadata for the exact initscript names (Services). If more than one initscript is defined for an addon, the requested action will be performed on all listed initscripts. * Added posibility to perform action on a specific initscript of an addon instead of on all initscripts of the addon. * New action 'list-services' to display a list of services related to an addon. * New action 'boot-status' to display wether service(s) are enabled to start on boot or not. * More error checking and cleaner error reporting to user * General cleanup and code restructuring to avoid code duplication * Updated and made usage instructions more verbose. Fixes: Bug#12935 Signed-off-by: Robin Roevens --- src/misc-progs/addonctrl.c | 397 +++++++++++++++++++++++++++++++------ 1 file changed, 336 insertions(+), 61 deletions(-) diff --git a/src/misc-progs/addonctrl.c b/src/misc-progs/addonctrl.c index 14b4b1325..1687aac19 100644 --- a/src/misc-progs/addonctrl.c +++ b/src/misc-progs/addonctrl.c @@ -10,71 +10,346 @@ #include #include #include +#include #include +#include +#include +#include #include "setuid.h" #define BUFFER_SIZE 1024 +const char *enabled_path = "/etc/rc.d/rc3.d"; +const char *disabled_path = "/etc/rc.d/rc3.d/off"; + +char errormsg[BUFFER_SIZE] = ""; +const char *usage = + "Usage\n" + " addonctrl (start|stop|restart|reload|enable|disable|status|boot-status|list-services) []\n" + "\n" + "Options:\n" + " \t\tName of the addon to control\n" + " \t\tSpecific service of the addon to control (optional)\n" + " \t\t\tBy default the requested action is performed on all related services. See also 'list-services'.\n" + " start\t\t\tStart service(s) of the addon\n" + " stop\t\t\tStop service(s) of the addon\n" + " restart\t\tRestart service(s) of the addon\n" + " enable\t\tEnable service(s) of the addon to start at boot\n" + " disable\t\tDisable service(s) of the addon to start at boot\n" + " status\t\tDisplay current state of addon service(s)\n" + " boot-status\t\tDisplay wether service(s) is enabled on boot or not\n" + " list-services\t\tDisplay a list of services related to the addon"; + +// Find a file using as glob pattern. +// Returns the found filename or NULL if not found +char *find_file_in_dir(const char *path, const char *filepattern) +{ + struct dirent *entry; + DIR *dp; + char *found = NULL; + + if ((dp = opendir(path)) != NULL) { + while(found == NULL && (entry = readdir(dp)) != NULL) + if (fnmatch(filepattern, entry->d_name, FNM_PATHNAME) == 0) + found = strdup(entry->d_name); + + closedir(dp); + } + + return found; +} + +// Reads Services metadata for . +// Returns pointer to array of strings containing the services for +// and sets to the number of found services +char **get_addon_services(const char *addon, int *servicescnt, const char *filter) { + const char *metafile_prefix = "/opt/pakfire/db/installed/meta-"; + const char *metadata_key = "Services"; + const char *keyvalue_delim = ":"; + const char *service_delim = " "; + char *token; + char **services = NULL; + char *service; + char *line = NULL; + size_t line_len = 0; + int i = 0; + char *metafile; + + if (addon == NULL) { + errno = EINVAL; + return NULL; + } + + if (asprintf(&metafile, "%s%s", metafile_prefix, addon) == -1) { + errno = ENOMEM; + return NULL; + } + + FILE *fp = fopen(metafile,"r"); + if (fp != NULL) { + // Get initscript(s) for addon from meta-file + while (getline(&line, &line_len, fp) != -1 && services == NULL) { + // Strip newline + char *newline = strchr(line, '\n'); + if (newline) *newline = 0; + + // Parse key/value and look for required key. + token = strtok(line, keyvalue_delim); + if (token != NULL && strcmp(token, metadata_key) == 0) { + token = strtok(NULL, keyvalue_delim); + if (token != NULL) { + // Put each service in services array + service = strtok(token, service_delim); + while (service != NULL) { + // if filter is set, only select filtered service + if ((filter != NULL && strcmp(filter, service) == 0) || + filter == NULL) { + services = reallocarray(services ,i+1 ,sizeof (char *)); + if (services != NULL) + services[i++] = strdup(service); + else + break; + } + service = strtok(NULL, service_delim); + } + } + } + } + + free(line); + fclose(fp); + } else { + snprintf(errormsg, BUFFER_SIZE - 1, "Addon '%s' not found.\n\n%s", addon, usage); + } + + free(metafile); + *servicescnt = i; + return services; +} + +// Calls initscript with parameter +int initscript_action(const char *service, const char *action) { + const char *initd_path = "/etc/rc.d/init.d"; + char *initscript; + char *argv[] = { + action, + NULL + }; + int r = 0; + + if ((r = asprintf(&initscript, "%s/%s", initd_path, service)) != -1) { + r = run(initscript, argv); + free(initscript); + } else { + errno = ENOMEM; + } + + return r; +} + +// Move an initscript with filepattern from to +// Returns: +// -1: Error during move or memory allocation. Details in errno +// 0: Success +// 1: file was not moved, but is already in +// 2: file does not exist in either in or +int move_initscript_by_pattern(const char *src_path, const char *dest_path, const char *filepattern) { + char *src = NULL; + char *dest = NULL; + int r = 1; + char *filename = NULL; + + if ((filename = find_file_in_dir(src_path, filepattern)) != NULL) { + if ((r = asprintf(&src, "%s/%s", src_path, filename)) != -1 && + (r = asprintf(&dest, "%s/%s", dest_path, filename) != -1)) { + // move initscript + r = rename(src, dest); + } else { + errno = ENOMEM; + } + + if (src != NULL) + free(src); + if (dest != NULL) + free(dest); + } else { + if ((filename = find_file_in_dir(dest_path, filepattern)) == NULL) + r = 2; + } + + if (filename != NULL) + free(filename); + + return r; +} + +// Enable/Disable addon service(s) by moving initscript symlink from/to disabled_path +int toggle_service(const char *service, const char *action) { + const char *src_path, *dest_path; + char *filepattern; + int r = 0; + + if (asprintf(&filepattern, "S??%s", service) == -1) { + errno = ENOMEM; + return -1; + } + + if (strcmp(action, "enable") == 0) { + src_path = disabled_path; + dest_path = enabled_path; + } else { + src_path = enabled_path; + dest_path = disabled_path; + } + + // Ensure disabled_path exists + errno = 0; + if (mkdir(disabled_path, S_IRWXU + S_IRGRP + S_IXGRP + S_IROTH + S_IXOTH) == -1 && errno != EEXIST) { + r = 1; + snprintf(errormsg, BUFFER_SIZE -1, "Error creating %s. (Error: %d)", disabled_path, errno); + } else { + r = move_initscript_by_pattern(src_path, dest_path, filepattern); + if (r == -1 ) { + r = 1; + snprintf(errormsg, BUFFER_SIZE - 1, "Could not %s %s. (Error: %d)", action, service, errno); + } else if (r == 1) { + snprintf(errormsg, BUFFER_SIZE - 1, "Service %s is already %sd. Skipping...", service, action); + } else if (r == 2) { + snprintf(errormsg, BUFFER_SIZE - 1, "Unable to %s service %s. (Service has no valid symlink in %s).", action, service, src_path); + } + } + + free(filepattern); + + return r; +} + +// Print to stdout wether is enabled or disabled on boot +// Prints as Not available when initscript is not found +// in either enabled_path or disabled_path. +void print_boot_status(char *service) { + char *filepattern; + if (asprintf(&filepattern, "S??%s", service) == -1) { + errno = ENOMEM; + return; + } + + if (find_file_in_dir(enabled_path, filepattern) != NULL) + fprintf(stdout, "%s is enabled on boot.\n", service); + else if (find_file_in_dir(disabled_path, filepattern) != NULL) + fprintf(stdout, "%s is disabled on boot.\n", service); + else + fprintf(stdout, "%s is not available for boot. (Service has no valid symlink in either %s or %s).\n", service, enabled_path, disabled_path); + + free(filepattern); +} + int main(int argc, char *argv[]) { - char command[BUFFER_SIZE]; - - if (!(initsetuid())) - exit(1); - - if (argc < 3) { - fprintf(stderr, "\nMissing arguments.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n"); - exit(1); - } - - const char* name = argv[1]; - - if (strlen(name) > 32) { - fprintf(stderr, "\nString to large.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n"); - exit(1); - } - - // Check if the input argument is valid - if (!is_valid_argument_alnum(name)) { - fprintf(stderr, "Invalid add-on name: %s\n", name); - exit(2); - } - - sprintf(command, "/opt/pakfire/db/installed/meta-%s", name); - FILE *fp = fopen(command,"r"); - if ( fp ) { - fclose(fp); - } else { - fprintf(stderr, "\nAddon '%s' not found.\n\naddonctrl addon (start|stop|restart|reload|status|enable|disable)\n\n", name); - exit(1); - } - - if (strcmp(argv[2], "start") == 0) { - snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s start", name); - safe_system(command); - } else if (strcmp(argv[2], "stop") == 0) { - snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s stop", name); - safe_system(command); - } else if (strcmp(argv[2], "restart") == 0) { - snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s restart", name); - safe_system(command); - } else if (strcmp(argv[2], "reload") == 0) { - snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s reload", name); - safe_system(command); - } else if (strcmp(argv[2], "status") == 0) { - snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s status", name); - safe_system(command); - } else if (strcmp(argv[2], "enable") == 0) { - snprintf(command, BUFFER_SIZE - 1, "mv -f /etc/rc.d/rc3.d/off/S??%s /etc/rc.d/rc3.d" , name); - safe_system(command); - } else if (strcmp(argv[2], "disable") == 0) { - snprintf(command, BUFFER_SIZE - 1, "mkdir -p /etc/rc.d/rc3.d/off"); - safe_system(command); - snprintf(command, BUFFER_SIZE - 1, "mv -f /etc/rc.d/rc3.d/S??%s /etc/rc.d/rc3.d/off" , name); - safe_system(command); - } else { - fprintf(stderr, "\nBad argument given.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n"); - exit(1); - } - - return 0; + char **services = NULL; + int servicescnt = 0; + char *addon = argv[1]; + char *action = argv[2]; + char *service_filter = NULL; + int r = 0; + + if (!(initsetuid())) + exit(1); + + if (argc < 3) { + fprintf(stderr, "\nMissing arguments.\n\n%s\n\n", usage); + exit(1); + } + + if (argc == 4 && strcmp(action, "list-services") != 0) + service_filter = argv[3]; + + if (strlen(addon) > 32) { + fprintf(stderr, "\nString too large.\n\n%s\n\n", usage); + exit(1); + } + + // Check if the input argument is valid + if (!is_valid_argument_alnum(addon)) { + fprintf(stderr, "Invalid add-on name: %s.\n", addon); + exit(2); + } + + // Get initscript name(s) from addon metadata + errno = 0; + services = get_addon_services(addon, &servicescnt, service_filter); + if (services == NULL || *services == 0) { + if (errno != 0) + fprintf(stderr, "\nSystem error occured. (Error: %d)\n\n", errno); + else if (strcmp(errormsg, "") != 0) + fprintf(stderr, "\n%s\n\n", errormsg); + else if (service_filter != NULL) + fprintf(stderr, "\nNo service '%s' found for addon '%s'. Use 'list-services' to get a list of available services\n\n%s\n\n", service_filter, addon, usage); + else + fprintf(stderr, "\nAddon '%s' has no services.\n\n", addon); + exit(1); + } + + // Handle requested action + if (strcmp(action, "start") == 0 || + strcmp(action, "stop") == 0 || + strcmp(action, "restart") == 0 || + strcmp(action, "reload") == 0 || + strcmp(action, "status") == 0) { + + errno = 0; + for(int i = 0; i < servicescnt; i++) { + if (initscript_action(services[i], action) != 0) { + r = 1; + if (errno != 0) + fprintf(stderr, "\nSystem error occured. (Error: %d)\n\n", errno); + break; + } + } + + } else if (strcmp(action, "enable") == 0 || + strcmp(action, "disable") == 0) { + + errno = 0; + for(int i = 0; i < servicescnt; i++) { + if (toggle_service(services[i], action) == 0) { + fprintf(stdout, "%sd service %s\n", action, services[i]); + } else if (errno != 0) { + r = 1; + fprintf(stderr, "\nSystem error occured. (Error: %d)\n\n", errno); + break; + } else { + r = 1; + fprintf(stderr, "\n%s\n\n", errormsg); + } + } + + } else if (strcmp(action, "boot-status") == 0) { + errno = 0; + for(int i = 0; i < servicescnt; i++) { + print_boot_status(services[i]); + if (errno != 0) { + r = 1; + fprintf(stderr, "\nSystem error occured. (Error: %d)\n\n", errno); + break; + } + } + + } else if (strcmp(action, "list-services") == 0) { + fprintf(stdout, "\nServices for addon %s:\n", addon); + for(int i = 0; i < servicescnt; i++) { + fprintf(stdout, " %s\n", services[i]); + } + fprintf(stdout, "\n"); + + } else { + fprintf(stderr, "\nBad argument given.\n\n%s\n\n", usage); + r = 1; + } + + // Cleanup + for(int i = 0; i < servicescnt; i++) + free(services[i]); + free(services); + + return r; }