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