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
|
#!/usr/bin/env python
# -*- coding: ISO-8859-15 -*-
#
# Copyright (C) 2005-2007 David Guerizec <david@guerizec.net>
#
# Last modified: 2007 Dec 08, 20:10:26 by david
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os, os.path, sha
from ConfigParser import NoSectionError, SafeConfigParser as ConfigParser
from sshproxy.config import ConfigSection, path, get_config
from sshproxy.client import ClientDB, ClientInfo
from sshproxy.util import istrue
class FileClientConfigSection(ConfigSection):
section_id = 'client_db.ini'
section_defaults = {
'file': '@client.db',
}
types = {
'file': path,
}
FileClientConfigSection.register()
class FileClientInfo(ClientInfo):
def get_config_file(self):
clientfile = get_config('client_db.ini')['file']
if not os.path.exists(clientfile):
open(clientfile, 'w').close()
os.chmod(clientfile, 0600)
# no need to parse an empty file
return None
file = ConfigParser()
file.read(clientfile)
return file
def load(self):
file = self.get_config_file()
if not file:
return
try:
tokens = dict(file.items(self.username))
except NoSectionError:
return
self.set_tokens(**tokens)
def save(self, file=None):
if not file:
file = self.get_config_file()
if not file:
return
if self.username:
if not file.has_section(self.username):
file.add_section(self.username)
for tag, value in self.tokens.items():
if tag in ('username', 'ip_addr'):
continue
elif value and str(value):
file.set(self.username, tag, str(value))
elif file.has_option(self.username, tag):
file.remove_option(self.username, tag)
clientfile = get_config('client_db.ini')['file']
fd = open(clientfile+'.new', 'w')
file.write(fd)
fd.close()
os.rename(clientfile+'.new', clientfile)
def delete(self, username):
file = self.get_config_file()
if not file:
return
if file.has_section(username):
file.remove_section(username)
self.save(file)
def auth_token_order(self):
return ('pubkey', 'pkey', 'password')
def authenticate(self, **tokens):
from sshproxy import log
resp = False
for token in self.auth_token_order():
if token in tokens.keys() and tokens[token] is not None:
if token == 'password':
if (sha.new(tokens[token]).hexdigest()
== self.get_token(token)):
resp = True
break
elif token in ('pubkey', 'pkey'):
pubkeys = self.get_token('pubkey',
self.get_token('pkey', '')).split('\n')
pubkeys = [ pk.split()[0] for pk in pubkeys if len(pk) ]
for pk in pubkeys:
if pk == tokens[token]:
resp = True
break
ClientDB()._unauth_pubkey = tokens[token]
elif self.get_token(token) == tokens[token]:
resp = True
break
return resp
def exists(self, username):
file = self.get_config_file()
if not file:
return
return file.has_section(username)
def list_clients(self, **kw):
file = self.get_config_file()
if not file:
return
return file.sections()
class FileClientDB(ClientDB):
def exists(self, username, **tokens):
if not getattr(self, 'clientinfo', None):
return ClientInfo(None).exists(username)
return self.clientinfo.exists(username)
def list_clients(self, **kw):
return ClientInfo(None).list_clients(**kw)
def add_client(self, username, **tokens):
if self.exists(username):
return 'Client %s does already exist' % username
client = ClientInfo(username, **tokens)
client.save()
return 'Client %s added' % username
def del_client(self, username, **tokens):
if not self.exists(username):
return 'Client %s does not exist'
ClientInfo(None).delete(username)
return 'Client %s deleted.' % username
|