if only a username is given on init, it is used as principal. no

pubkey-handling
This commit is contained in:
Jakobus Schürz 2019-07-31 11:58:39 +02:00
parent a6c329e7be
commit 72b551f6b0

View file

@ -6,6 +6,7 @@ import errno
import logging import logging
import os import os
import sys import sys
import re
from pkg_resources import resource_filename from pkg_resources import resource_filename
from cStringIO import StringIO from cStringIO import StringIO
@ -32,19 +33,28 @@ class InsecureSSHKeyUsername(Exception):
return '%s: %s' % (self.__doc__, ': '.join(self.args)) return '%s: %s' % (self.__doc__, ': '.join(self.args))
def ssh_extract_user(pubkey): def ssh_extract_user(pubkey):
_, user = pubkey.rsplit(None, 1) if re.search(r"\s", pubkey)
_, user = pubkey.rsplit(None, 1)
else:
user = pubkey
if ssh.isSafeUsername(user): if ssh.isSafeUsername(user):
return user return user
else: else:
raise InsecureSSHKeyUsername(repr(user)) raise InsecureSSHKeyUsername(repr(user))
def initial_commit(git_dir, cfg, pubkey, user): def initial_commit(git_dir, cfg, pubkey, user):
if pubkey is None:
keyfile = 'keydir/principals'
content = user
else:
keyfile = 'keydir/%s.pub' % user
content = pubkey
repository.fast_import( repository.fast_import(
git_dir=git_dir, git_dir=git_dir,
commit_msg='Automatic creation of gitosis repository.', commit_msg='Automatic creation of gitosis repository.',
committer='Gitosis Admin <%s>' % user, committer='Gitosis Admin <%s>' % user,
files=[ files=[
('keydir/%s.pub' % user, pubkey), (keyfile, content),
('gitosis.conf', cfg), ('gitosis.conf', cfg),
], ],
) )
@ -124,6 +134,8 @@ class Main(app.App):
log.info('Reading SSH public key...') log.info('Reading SSH public key...')
pubkey = read_ssh_pubkey() pubkey = read_ssh_pubkey()
user = ssh_extract_user(pubkey) user = ssh_extract_user(pubkey)
if not re.search(r"\s", pubkey)
pubkey = None
if user is None: if user is None:
log.error('Cannot parse user from SSH public key.') log.error('Cannot parse user from SSH public key.')
sys.exit(1) sys.exit(1)