]> git.ipfire.org Git - ipfire-2.x.git/blame - src/misc-progs/ipfirereboot.c
suricata: Fix amount of listened nfqueues
[ipfire-2.x.git] / src / misc-progs / ipfirereboot.c
CommitLineData
12938118
MT
1/*
2 * This file is part of the IPCop Firewall.
3 *
4 * IPCop is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * IPCop is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with IPCop; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright (C) 2005-10-25 Franck Bourdonnec
19 *
20 * $Id: ipcopreboot.c,v 1.1.2.2 2005/10/24 23:05:50 franck78 Exp $
21 *
22 */
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include "setuid.h"
28
29
30/* define operations */
31#define OP_REBOOT "boot"
32#define OP_REBOOT_FS "bootfs" // add filesystem check option (not yet in GUI)
33#define OP_SHUTDOWN "down"
34#define OP_SCHEDULE_ADD "cron+"
35#define OP_SCHEDULE_REM "cron-"
36#define OP_SCHEDULE_GET "cron?"
37
38int main(int argc, char**argv)
39{
40
41 if (!(initsetuid()))
42 return 1;
43
44 // Check what command is asked
45 if (argc==1)
46 {
47 fprintf (stderr, "Missing reboot command!\n");
48 return 1;
49 }
50
51 if (argc==2 && strcmp(argv[1], OP_SHUTDOWN)==0)
52 {
53 safe_system("/sbin/shutdown -h now");
54 return 0;
55 }
56
57 if (argc==2 && strcmp(argv[1], OP_REBOOT)==0)
58 {
59 safe_system("/sbin/shutdown -r now");
60 return 0;
61 }
62
63 if (argc==2 && strcmp(argv[1], OP_REBOOT_FS)==0)
64 {
65 safe_system("/sbin/shutdown -F -r now");
66 return 0;
67 }
68
69 // output schedule to stdout
70 if (argc==2 && strcmp(argv[1], OP_SCHEDULE_GET)==0)
71 {
72 safe_system("/bin/grep /sbin/shutdown /var/spool/cron/root.orig");
73 return 0;
74 }
75
76 if (argc==2 && strcmp(argv[1], OP_SCHEDULE_REM)==0)
77 {
78 safe_system("/usr/bin/perl -i -p -e 's/^.*\\/sbin\\/shutdown.*$//s' /var/spool/cron/root.orig");
79 safe_system("/usr/bin/fcrontab -u root -z");
80 return 0;
81 }
82
83 if (argc==6 && strcmp(argv[1], OP_SCHEDULE_ADD)==0)
84 {
85 // check args
86 if (!( strlen(argv[2])<3 &&
87 strspn(argv[2], "0123456789") == strlen (argv[2]) &&
88 strlen(argv[3])<3 &&
89 strspn(argv[3], "0123456789") == strlen (argv[3]) &&
90 strlen(argv[4])<14 &&
91 strspn(argv[4], "1234567,*") == strlen (argv[4]) &&
92 ((strcmp(argv[5], "-r")==0) || //reboot
93 (strcmp(argv[5], "-h")==0)) ) //hangup
94 ) {
95 fprintf (stderr, "Bad cron+ parameters!\n");
96 return 1;
97 }
98
99 // remove old entry
100 safe_system("/usr/bin/perl -i -p -e 's/^.*\\/sbin\\/shutdown.*$//s' /var/spool/cron/root.orig");
101
102 // add new entry
103 FILE *fd = NULL;
104 if ((fd = fopen("/var/spool/cron/root.orig", "a")))
105 {
106 fprintf (fd,"%s %s * * %s /sbin/shutdown %s 1\n",argv[2],argv[3],argv[4],argv[5]);
107 fclose (fd);
108 }
109
110 // inform cron
111 safe_system("/usr/bin/fcrontab -u root -z");
112 return 0;
113 }
114
115 fprintf (stderr, "Bad reboot command!\n");
116 return 1;
117}