]> git.ipfire.org Git - ipfire-2.x.git/blame - src/misc-progs/addonctrl.c
misc-progs: addonctrl: Replace all sprintf() with snprintf()
[ipfire-2.x.git] / src / misc-progs / addonctrl.c
CommitLineData
33cb80ed
AF
1/* This file is part of the IPFire Firewall.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12#include <sys/types.h>
13#include <fcntl.h>
14#include "setuid.h"
15
16#define BUFFER_SIZE 1024
17
33cb80ed 18int main(int argc, char *argv[]) {
157d6425 19 char command[BUFFER_SIZE];
33cb80ed
AF
20
21 if (!(initsetuid()))
22 exit(1);
23
24 if (argc < 3) {
25 fprintf(stderr, "\nMissing arguments.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
26 exit(1);
27 }
cf9efe51
MT
28
29 const char* name = argv[1];
33cb80ed 30
cf9efe51 31 if (strlen(name) > 32) {
33cb80ed
AF
32 fprintf(stderr, "\nString to large.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
33 exit(1);
34 }
cf9efe51
MT
35
36 // Check if the input argument is valid
37 if (!is_valid_argument_alnum(name)) {
38 fprintf(stderr, "Invalid add-on name: %s\n", name);
39 exit(2);
33cb80ed 40 }
cf9efe51
MT
41
42 sprintf(command, "/opt/pakfire/db/installed/meta-%s", name);
33cb80ed
AF
43 FILE *fp = fopen(command,"r");
44 if ( fp ) {
45 fclose(fp);
46 } else {
cf9efe51 47 fprintf(stderr, "\nAddon '%s' not found.\n\naddonctrl addon (start|stop|restart|reload|status|enable|disable)\n\n", name);
33cb80ed
AF
48 exit(1);
49 }
157d6425 50
33cb80ed 51 if (strcmp(argv[2], "start") == 0) {
157d6425 52 snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s start", name);
33cb80ed
AF
53 safe_system(command);
54 } else if (strcmp(argv[2], "stop") == 0) {
157d6425 55 snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s stop", name);
33cb80ed
AF
56 safe_system(command);
57 } else if (strcmp(argv[2], "restart") == 0) {
157d6425 58 snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s restart", name);
33cb80ed
AF
59 safe_system(command);
60 } else if (strcmp(argv[2], "reload") == 0) {
157d6425 61 snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s reload", name);
33cb80ed
AF
62 safe_system(command);
63 } else if (strcmp(argv[2], "status") == 0) {
157d6425 64 snprintf(command, BUFFER_SIZE - 1, "/etc/rc.d/init.d/%s status", name);
33cb80ed
AF
65 safe_system(command);
66 } else if (strcmp(argv[2], "enable") == 0) {
157d6425 67 snprintf(command, BUFFER_SIZE - 1, "mv -f /etc/rc.d/rc3.d/off/S??%s /etc/rc.d/rc3.d" , name);
33cb80ed
AF
68 safe_system(command);
69 } else if (strcmp(argv[2], "disable") == 0) {
157d6425 70 snprintf(command, BUFFER_SIZE - 1, "mkdir -p /etc/rc.d/rc3.d/off");
33cb80ed 71 safe_system(command);
157d6425 72 snprintf(command, BUFFER_SIZE - 1, "mv -f /etc/rc.d/rc3.d/S??%s /etc/rc.d/rc3.d/off" , name);
33cb80ed
AF
73 safe_system(command);
74 } else {
75 fprintf(stderr, "\nBad argument given.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
76 exit(1);
77 }
78
79 return 0;
80}