Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39549
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation in the Cloud
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
24
25
"""
26
Complete delete a given unix user
27
28
You should put the following in visudo:
29
30
salvus ALL=(ALL) NOPASSWD: /usr/local/bin/create_unix_user.py ""
31
salvus ALL=(ALL) NOPASSWD: /usr/local/bin/delete_unix_user.py *
32
"""
33
34
from subprocess import Popen, PIPE
35
import os, sys
36
37
def cmd(args, exit_on_error=True):
38
print ' '.join(args)
39
out = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
40
e = out.wait()
41
stdout = out.stdout.read()
42
stderr = out.stderr.read()
43
if e:
44
print "ERROR --", e
45
sys.stdout.write(stdout)
46
sys.stderr.write(stderr)
47
sys.stdout.flush(); sys.stderr.flush()
48
if exit_on_error:
49
sys.exit(e)
50
51
52
def deluser(username):
53
if len(username) != 8:
54
sys.stderr.write("Suspicious username '%s' doesn't have length -- refusing to delete!\n"%username)
55
sys.exit(1)
56
else:
57
# We use the deluser unix command.
58
# deluser [options] [--force] [--remove-home] [--remove-all-files]
59
home = os.popen("echo ~%s"%username).read().strip()
60
cmd(['killall', '-9', '-u', username], exit_on_error=False)
61
cmd(['deluser', '--force', username], exit_on_error=True)
62
cmd(['rm', '-rf', home], exit_on_error=False)
63
64
if len(sys.argv) != 2:
65
sys.stderr.write("Usage: sudo %s <username>\n"%sys.argv[0])
66
sys.stderr.flush()
67
sys.exit(1)
68
else:
69
deluser(sys.argv[1])
70
71
72
73