]> git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/addonctrl.c
c90eb7952243e9610a1624592dc61c2752b87cd8
[ipfire-2.x.git] / src / misc-progs / addonctrl.c
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
18 char command[BUFFER_SIZE];
19
20 int main(int argc, char *argv[]) {
21
22 if (!(initsetuid()))
23 exit(1);
24
25 if (argc < 3) {
26 fprintf(stderr, "\nMissing arguments.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
27 exit(1);
28 }
29
30 const char* name = argv[1];
31
32 if (strlen(name) > 32) {
33 fprintf(stderr, "\nString to large.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
34 exit(1);
35 }
36
37 // Check if the input argument is valid
38 if (!is_valid_argument_alnum(name)) {
39 fprintf(stderr, "Invalid add-on name: %s\n", name);
40 exit(2);
41 }
42
43 sprintf(command, "/opt/pakfire/db/installed/meta-%s", name);
44 FILE *fp = fopen(command,"r");
45 if ( fp ) {
46 fclose(fp);
47 } else {
48 fprintf(stderr, "\nAddon '%s' not found.\n\naddonctrl addon (start|stop|restart|reload|status|enable|disable)\n\n", name);
49 exit(1);
50 }
51
52 if (strcmp(argv[2], "start") == 0) {
53 sprintf(command,"/etc/rc.d/init.d/%s start", name);
54 safe_system(command);
55 } else if (strcmp(argv[2], "stop") == 0) {
56 sprintf(command,"/etc/rc.d/init.d/%s stop", name);
57 safe_system(command);
58 } else if (strcmp(argv[2], "restart") == 0) {
59 sprintf(command,"/etc/rc.d/init.d/%s restart", name);
60 safe_system(command);
61 } else if (strcmp(argv[2], "reload") == 0) {
62 sprintf(command,"/etc/rc.d/init.d/%s reload", name);
63 safe_system(command);
64 } else if (strcmp(argv[2], "status") == 0) {
65 sprintf(command,"/etc/rc.d/init.d/%s status", name);
66 safe_system(command);
67 } else if (strcmp(argv[2], "enable") == 0) {
68 sprintf(command,"mv -f /etc/rc.d/rc3.d/off/S??%s /etc/rc.d/rc3.d" , name);
69 safe_system(command);
70 } else if (strcmp(argv[2], "disable") == 0) {
71 sprintf(command,"mkdir -p /etc/rc.d/rc3.d/off");
72 safe_system(command);
73 sprintf(command,"mv -f /etc/rc.d/rc3.d/S??%s /etc/rc.d/rc3.d/off" , name);
74 safe_system(command);
75 } else {
76 fprintf(stderr, "\nBad argument given.\n\naddonctrl addon (start|stop|restart|reload|enable|disable)\n\n");
77 exit(1);
78 }
79
80 return 0;
81 }