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