Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/ensure_ssh_access.py
Views: 275
1
#!/usr/bin/python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation
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
import os, sys
24
25
path = sys.argv[1]
26
if not os.path.exists(path):
27
raise RuntimeError("no such directory -- %s" % path)
28
29
dot_ssh = os.path.join(path, '.ssh')
30
31
if os.path.exists(dot_ssh) and not os.path.isdir(dot_ssh):
32
os.unlink(dot_ssh)
33
34
if not os.path.exists(dot_ssh):
35
os.makedirs(dot_ssh)
36
37
target = os.path.join(dot_ssh, 'authorized_keys')
38
authorized_keys = '\n' + open(sys.argv[2]).read() + '\n'
39
40
if not os.path.exists(target) or authorized_keys not in open(target).read():
41
open(target, 'w').write(authorized_keys)
42
43
s = os.stat(path)
44
45
if os.system('chown -R %s:%s %s' % (s.st_uid, s.st_gid, dot_ssh)):
46
raise RuntimeError("failed to chown")
47
48
if os.system('chmod og-rwx -R %s' % dot_ssh):
49
raise RuntimeError("failed to chmod")
50
51