samba: Add GUI functionality to join a domain.
[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         if (!(initsetuid()))
15                 exit(1);
16
17         // Check what command is asked
18         if (argc == 1) {
19                 fprintf (stderr, "Missing smbctrl command!\n");
20                 return 1;
21
22         } else if (strcmp(argv[1], "smbuserdisable") == 0) {
23                 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -d %s >/dev/null", argv[2]);
24                 safe_system(command);
25
26         } else if (strcmp(argv[1], "smbuserenable") == 0) {
27                 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -e %s >/dev/null", argv[2]);
28                 safe_system(command);
29
30         } else if (strcmp(argv[1], "smbuserdelete") == 0) {
31                 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -x %s >/dev/null", argv[2]);
32                 safe_system(command);
33
34                 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/userdel %s >/dev/null", argv[2]);
35                 safe_system(command);
36
37         } else if (strcmp(argv[1], "smbsafeconf") == 0) {
38                 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
39
40         } else if (strcmp(argv[1], "smbsafeconfcups") == 0) {
41                 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares /var/ipfire/samba/printer > /var/ipfire/samba/smb.conf");
42
43         } else if (strcmp(argv[1], "smbsafeconfpdc") == 0) {
44                 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/pdc /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
45
46         } else if (strcmp(argv[1], "smbsafeconfpdccups") == 0) {
47                 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");
48
49         } else if (strcmp(argv[1], "smbglobalreset") == 0) {
50                 safe_system("/bin/cat /var/ipfire/samba/default.global /var/ipfire/samba/shares > /var/ipfire/samba/smb.conf");
51                 safe_system("/bin/cat /var/ipfire/samba/default.settings > /var/ipfire/samba/settings");
52                 safe_system("/bin/cat /var/ipfire/samba/default.global > /var/ipfire/samba/global");
53                 safe_system("/bin/cat /var/ipfire/samba/default.pdc > /var/ipfire/samba/pdc");
54
55         } else if (strcmp(argv[1], "smbsharesreset") == 0) {
56                 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/default.shares > /var/ipfire/samba/smb.conf");
57                 safe_system("/bin/cat /var/ipfire/samba/default.shares > /var/ipfire/samba/shares");
58
59         } else if (strcmp(argv[1], "smbprinterreset") == 0) {
60                 safe_system("/bin/cat /var/ipfire/samba/global /var/ipfire/samba/shares /var/default.printer > /var/ipfire/samba/smb.conf");
61                 safe_system("/bin/cat /var/ipfire/samba/default.printer > /var/ipfire/samba/printer");
62
63         } else if (strcmp(argv[1], "smbstop") == 0) {
64                 safe_system("/etc/rc.d/init.d/samba stop >/dev/null");
65                 safe_system("/usr/local/bin/sambactrl disable");
66
67         } else if (strcmp(argv[1], "smbstart") == 0) {
68                 safe_system("/etc/rc.d/init.d/samba start >/dev/null");
69                 safe_system("/usr/local/bin/sambactrl enable");
70
71         } else if (strcmp(argv[1], "smbrestart") == 0) {
72                 safe_system("/etc/rc.d/init.d/samba restart >/dev/null");
73
74         } else if (strcmp(argv[1], "smbreload") == 0) {
75                 safe_system("/etc/rc.d/init.d/samba reload >/dev/null");
76
77         } else if (strcmp(argv[1], "smbstatus") == 0) {
78                 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbstatus 2>/dev/null");
79                 safe_system(command);
80
81         } else if (strcmp(argv[1], "smbuseradd") == 0) {
82                 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/groupadd sambauser >/dev/null");
83                 safe_system(command);
84
85                 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]);
86                 safe_system(command);
87
88                 snprintf(command, BUFFER_SIZE-1, "echo %s:%s | chpasswd", argv[2], argv[3]);
89                 safe_system(command);
90
91                 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]);
92                 safe_system(command);
93
94         } else if (strcmp(argv[1], "smbpcadd") == 0) {
95                 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/groupadd sambawks >/dev/null");
96                 safe_system(command);
97
98                 snprintf(command, BUFFER_SIZE-1, "/usr/sbin/useradd -c 'Samba Workstation' -g %s -s %s %s >/dev/null", argv[3], argv[4], argv[2]);
99                 safe_system(command);
100
101                 snprintf(command, BUFFER_SIZE-1, "/usr/bin/smbpasswd -a -m %s >/dev/null", argv[2]);
102                 safe_system(command);
103
104         } else if (strcmp(argv[1], "smbchangepw") == 0) {
105                 snprintf(command, BUFFER_SIZE-1, "echo %s:%s | chpasswd", argv[2], argv[3]);
106                 safe_system(command);
107
108                 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]);
109                 safe_system(command);
110
111         } else if (strcmp(argv[1], "readsmbpasswd") == 0) {
112                 safe_system("/bin/chown root:nobody /var/ipfire/samba/private >/dev/null");
113                 safe_system("/bin/chown root:nobody /var/ipfire/samba/private/smbpasswd >/dev/null");
114                 safe_system("/bin/chmod 640 /var/ipfire/samba/private/smbpasswd >/dev/null");
115                 safe_system("/bin/chmod 650 /var/ipfire/samba/private >/dev/null");
116
117         } else if (strcmp(argv[1], "locksmbpasswd") == 0) {
118                 safe_system("/bin/chown root:root /var/ipfire/samba/private >/dev/null");
119                 safe_system("/bin/chown root:root /var/ipfire/samba/private/smbpasswd >/dev/null");
120                 safe_system("/bin/chmod 600 /var/ipfire/samba/private/smbpasswd >/dev/null");
121                 safe_system("/bin/chmod 600 /var/ipfire/samba/private >/dev/null");
122
123         } else if (strcmp(argv[1], "enable") == 0) {
124                 safe_system("touch /var/ipfire/samba/enable");
125                 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc3.d/S45samba");
126                 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc0.d/K48samba");
127                 safe_system("ln -snf /etc/rc.d/init.d/samba /etc/rc.d/rc6.d/K48samba");
128
129         } else if (strcmp(argv[1], "disable") == 0) {
130                 safe_system("unlink /var/ipfire/samba/enable");
131                 safe_system("rm -rf /etc/rc.d/rc*.d/*samba");
132
133         } else if (strcmp(argv[1], "join") == 0) {
134                 if (argc == 4) {
135                         snprintf(command, BUFFER_SIZE - 1, "/usr/bin/net join -U \"%s%%%s\"",
136                                 argv[2], argv[3]);
137                         return safe_system(command);
138                 } else {
139                         fprintf(stderr, "Wrong number of arguments. Need username and password.\n");
140                         return 1;
141                 }
142         }
143
144         return 0;
145 }