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