forked from Fmstrat/samba-domain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
domain.sh
executable file
·184 lines (180 loc) · 4.4 KB
/
domain.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
if [ -z "${DOMAIN_DC}" ] || [ -z "${DOMAIN_EMAIL}" ]; then
echo 'You must have env variables set of:
DOMAIN_DC="dc=corp,dc=example,dc=com"
DOMAIN_EMAIL="example.com"
'
exit
fi
#--------------------------------------------
function usage() {
echo '
Usage:
domain info
domain ldapinfo
domain groups
domain group <group>
domain users
domain user <user>
domain create-group <group>
domain delete-group <group>
domain create-user <user>
domain delete-user <user>
domain change-password <user>
domain edit <user or group>
domain set-user-ssh-key <user> <pubkey>
domain set-user-photo-from-file <user> <"$(base64 -w0 /path/to/img)>">
domain set-user-photo-from-url <user> <url>
domain add-user-to-group <user> <group>
domain remove-user-from-group <user> <group>
domain update-ip <domain> <controller> <oldip> <newip>
domain flush-cache
domain reload-config
domain db-check-and-fix
';
}
case "${1}" in
info)
wbinfo -D CORP
;;
ldapinfo)
ldapsearch -b "${DOMAIN_DC}"
;;
edit)
ldbedit -H /var/lib/samba/private/sam.ldb "samaccountname=${2}"
;;
groups)
wbinfo -g
;;
group)
echo ""
echo "Info"
echo "----"
wbinfo --group-info "${2}"
echo ""
echo "Members"
echo "-------"
samba-tool group listmembers "${2}"
echo ""
;;
users)
#samba-tool user list
wbinfo -u
;;
user)
echo ""
echo "User:"
echo "-----"
wbinfo -i "${2}"
echo ""
echo "Groups:"
echo "-----"
GL=$(wbinfo -r "${2}" | sed 's/\r//g')
for G in ${GL}; do
wbinfo --gid-info "${G}"
done
echo ""
;;
create-group)
samba-tool group add "${2}"
;;
delete-group)
samba-tool group delete "${2}"
;;
create-user)
echo -n "Firstname: "
read F
echo -n "Lastname: "
read L
E="${2}@${DOMAIN_EMAIL}"
samba-tool user create "${2}" --surname "${L}" --given-name "${F}" --mail-address "${E}"
samba-tool user setexpiry "${2}" --noexpiry
;;
delete-user)
samba-tool user delete "${2}"
;;
change-password)
samba-tool user setpassword "${2}"
;;
add-user-to-group)
samba-tool group addmembers "${3}" "${2}"
;;
remove-user-from-group)
samba-tool group removemembers "${3}" "${2}"
;;
update-ip)
samba-tool dns update 127.0.0.1 ${2} ${3} A ${4} ${5} -U administrator
samba-tool dns update 127.0.0.1 ${2} @ A ${4} ${5} -U administrator
;;
flush-cache)
net cache flush
;;
reload-config)
if [ -f /etc/samba/external/smb.conf ]; then
cp -f /etc/samba/external/smb.conf /etc/samba/smb.conf
fi
net cache flush
;;
db-check-and-fix)
samba-tool dbcheck --cross-ncs --fix --yes
;;
set-user-ssh-key)
DN=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | grep ^dn: |sed 's/^dn: //g')
CURKEY=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | { grep ^sshPublicKey: || true; })
if [ -z "${CURKEY}" ]; then
MOD="dn: ${DN}
changetype: modify
add: objectClass
objectClass: ldapPublicKey"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
MOD="dn: ${DN}
changetype: modify
add: sshPublicKey
sshPublicKey: ${3}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
else
MOD="dn: ${DN}
changetype: modify
replace: sshPublicKey
sshPublicKey: ${3}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
fi
;;
set-user-photo-from-file)
DN=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | grep ^dn: |sed 's/^dn: //g')
CURPHOTO=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | { grep ^jpegPhoto: || true; })
if [ -z "${CURPHOTO}" ]; then
MOD="dn: ${DN}
changetype: modify
add: jpegPhoto
jpegPhoto::${3}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
else
MOD="dn: ${DN}
changetype: modify
replace: jpegPhoto
jpegPhoto::${3}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
fi
;;
set-user-photo-from-url)
DN=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | grep ^dn: |sed 's/^dn: //g')
CURPHOTO=$(ldbedit -H /var/lib/samba/private/sam.ldb -e cat "samaccountname=${2}" | { grep ^jpegPhoto: || true; })
B64=$(curl -s "${3}" |base64 -w0)
if [ -z "${CURPHOTO}" ]; then
MOD="dn: ${DN}
changetype: modify
add: jpegPhoto
jpegPhoto::${B64}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
else
MOD="dn: ${DN}
changetype: modify
replace: jpegPhoto
jpegPhoto::${B64}"
echo "${MOD}" | ldbmodify -H /var/lib/samba/private/sam.ldb
fi
;;
*)
usage;
esac