e9b2972db97c96944a826a86cc4cd8e7358eeefb
[people/pmueller/ipfire-2.x.git] / src / misc-progs / sambactrl.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <fcntl.h>
7 #include "setuid.h"
8
9 #define BUFFER_SIZE 1024
10
11 char command[BUFFER_SIZE];
12
13 int main(int argc, char *argv[])
14 {
15
16 if (!(initsetuid()))
17 exit(1);
18
19 // Check what command is asked
20 if (argc==1)
21 {
22 fprintf (stderr, "Missing smbctrl command!\n");
23 return 1;
24 }
25 elsif (strcmp(argv[1], "smbuserdisable")==0)
26 {
27 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -d %s >/dev/null", argv[2]);
28 safe_system(command);
29 return 0;
30 }
31 elsif (strcmp(argv[1], "smbuserenable")==0)
32 {
33 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -e %s >/dev/null", argv[2]);
34 safe_system(command);
35 return 0;
36 }
37 elsif (strcmp(argv[1], "smbuserdelete")==0)
38 {
39 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -x %s >/dev/null", argv[2]);
40 safe_system(command);
41 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/userdel %s >/dev/null", argv[2]);
42 safe_system(command);
43 return 0;
44 }
45 elsif (strcmp(argv[1], "smbsafeconf")==0)
46 {
47 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
48 return 0;
49 }
50 elsif (strcmp(argv[1], "smbsafeconfcups")==0)
51 {
52 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares /var/ipfire/samba/printer > /var/ipfire/samba/smb.conf");
53 return 0;
54 }
55 elsif (strcmp(argv[1], "smbsafeconfpdc")==0)
56 {
57 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/pdc /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
58 return 0;
59 }
60 elsif (strcmp(argv[1], "smbsafeconfpdccups")==0)
61 {
62 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/pdc /var/ipfire/samba/shares /var/ipfire/samba/printer > /var/ipfire/samba/smb.conf");
63 return 0;
64 }
65 elsif (strcmp(argv[1], "smbglobalreset")==0)
66 {
67 safe_system("/bin/cat /var/ipfire/samba/default.global /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
68 safe_system("/bin/cat /var/ipfire/samba/default.settings > /var/ipfire/samba/settings");
69 safe_system("/bin/cat /var/ipfire/samba/default.global > /var/ipfire/samba/global");
70 safe_system("/bin/cat /var/ipfire/samba/default.pdc > /var/ipfire/samba/pdc");
71 return 0;
72 }
73 elsif (strcmp(argv[1], "smbsharesreset")==0)
74 {
75 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/default.shares > /var/ipfire/samba/smb.conf");
76 safe_system("/bin/cat /var/ipfire/samba/default.shares > /var/ipfire/samba/shares");
77 return 0;
78 }
79 elsif (strcmp(argv[1], "smbprinterreset")==0)
80 {
81 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares /var/default.printer > /var/ipfire/samba/smb.conf");
82 safe_system("/bin/cat /var/ipfire/samba/default.printer > /var/ipfire/samba/printer");
83 return 0;
84 }
85 elsif (strcmp(argv[1], "smbstop")==0)
86 {
87 safe_system("/etc/rc.d/init.d/samba stop >/dev/null");
88 safe_system("/usr/local/bin/sambactrl disable");
89 return 0;
90 }
91 elsif (strcmp(argv[1], "smbstart")==0)
92 {
93 safe_system("/etc/rc.d/init.d/samba start >/dev/null");
94 safe_system("/usr/local/bin/sambactrl enable");
95 return 0;
96 }
97 elsif (strcmp(argv[1], "smbrestart")==0)
98 {
99 safe_system("/etc/rc.d/init.d/samba restart >/dev/null");
100 return 0;
101 }
102 elsif (strcmp(argv[1], "smbreload")==0)
103 {
104 safe_system("/etc/rc.d/init.d/samba reload >/dev/null");
105 return 0;
106 }
107 elsif (strcmp(argv[1], "smbstatus")==0)
108 {
109 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbstatus 2>/dev/null");
110 safe_system(command);
111 return 0;
112 }
113 elsif (strcmp(argv[1], "smbuseradd")==0)
114 {
115 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/groupadd sambauser >/dev/null");
116 safe_system(command);
117 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/useradd -c 'Samba User' -m -g %s -s %s %s >/dev/null", argv[4], argv[5], argv[2]);
118 safe_system(command);
119 snprintf(command, BUFFER_SIZE-1, "echo %s:%s | chpasswd", argv[2], argv[3]);
120 safe_system(command);
121 snprintf(command, BUFFER_SIZE-1, "/usr/bin/printf '%s\n%s\n' | /usr/bin/smbpasswd -as %s >/dev/null", argv[3], argv[3], argv[2]);
122 safe_system(command);
123 return 0;
124 }
125 elsif (strcmp(argv[1], "smbpcadd")==0)
126 {
127 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/groupadd sambawks >/dev/null");
128 safe_system(command);
129 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/useradd -c 'Samba Workstation' -g %s -s %s %s >/dev/null", argv[3], argv[4], argv[2]);
130 safe_system(command);
131 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -a -m %s >/dev/null", argv[2]);
132 safe_system(command);
133 return 0;
134 }
135 elsif (strcmp(argv[1], "smbchangepw")==0)
136 {
137 snprintf(command, BUFFER_SIZE-1, "echo %s:%s | chpasswd", argv[2], argv[3]);
138 safe_system(command);
139 snprintf(command, BUFFER_SIZE-1, "/usr/bin/printf '%s\n%s\n' | /usr/bin/smbpasswd -as %s >/dev/null", argv[3], argv[3], argv[2]);
140 safe_system(command);
141 return 0;
142 }
143 elsif (strcmp(argv[1], "readsmbpasswd")==0)
144 {
145 safe_system("/bin/chown root:nobody /var/ipfire/samba/private >/dev/null");
146 safe_system("/bin/chown root:nobody /var/ipfire/samba/private/smbpasswd >/dev/null");
147 safe_system("/bin/chmod 640 /var/ipfire/samba/private/smbpasswd >/dev/null");
148 safe_system("/bin/chmod 650 /var/ipfire/samba/private >/dev/null");
149 return 0;
150 }
151 elsif (strcmp(argv[1], "locksmbpasswd")==0)
152 {
153 safe_system("/bin/chown root:root /var/ipfire/samba/private >/dev/null");
154 safe_system("/bin/chown root:root /var/ipfire/samba/private/smbpasswd >/dev/null");
155 safe_system("/bin/chmod 600 /var/ipfire/samba/private/smbpasswd >/dev/null");
156 safe_system("/bin/chmod 600 /var/ipfire/samba/private >/dev/null");
157 return 0;
158 }
159 elsif (strcmp(argv[1], "enable")==0)
160 {
161 safe_system("touch /var/ipfire/samba/enable");
162 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc3.d/S45samba");
163 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc0.d/K48samba");
164 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc6.d/K48samba");
165 return 0;
166 }
167 elsif (strcmp(argv[1], "disable")==0)
168 {
169 safe_system("unlink /var/ipfire/samba/enable");
170 safe_system("rm -rf /etc/rc.d/rc*.d/*samba");
171 return 0;
172 }
173 return 0;
174 }