-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhomedir.py
352 lines (325 loc) · 11.7 KB
/
homedir.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: utf-8 -*-
# v.0.2
class userInfo(object):
'''
userInfo class to get and set info or set last matched otp
'''
def __init__(self, config, userinfostore):
self.config = config
self.log = self.config.log
self.valid = True
self.username = userinfostore.get_username()
self.eusername = userinfostore.get_eusername()
self.decrypt = userinfostore.crypt.decrypt
try:
from pwd import getpwnam
self.userid = getpwnam(self.username).pw_uid
except:
import traceback
self.log.debug('There is no userid! Traceback: %s' % (
str(traceback.format_exc())), 1)
self.userid = None
self.valid = False
try:
from grp import getgrnam
self.groupid = getgrnam(self.username).gr_gid
except:
import traceback
self.log.debug('There is no groupid! Traceback: %s' % (
str(traceback.format_exc())), 1)
self.groupid = None
self.valid = False
import os
try:
cdir = self.config.get('homedir', 'dir')
except:
cdir = os.path.join('.config', 'motpy')
try:
self.motp_dir = os.path.join(getpwnam(self.username).pw_dir, cdir)
self.lastotp_file = os.path.join(self.motp_dir, 'lastotp.pickle')
self.db_file = os.path.join(self.motp_dir, 'motpy.pickle')
except:
import traceback
self.log.debug('There is no userdata dir! Traceback: %s' % (
str(traceback.format_exc())), 1)
self.motp_dir = None
self.lastopt_file = None
self.db_file = None
self.valid = False
def isValidDbForUser(self):
'''
Return true if there is a motp set already
'''
import os
if not self.valid:
return False
if os.access(self.db_file, os.R_OK):
return True
return False
def checkPath(self, path):
'''
Check if path exist and has right ownership and rights
'''
import os
ret = True
#
# Check exists
# #############
if not os.access(path, os.R_OK):
self.log.log("ERR: %s path doesn't exist or not readable" % path)
return False
#
# Check mode
# ###########
fstat = os.lstat(path)
if fstat.st_mode != 16832 and fstat.st_mode != 33152:
self.log.log('ERR: %s path has invalid mode' % path)
ret = False
#
# Check owner
# ############
from pwd import getpwnam
if fstat.st_uid != getpwnam(self.username)[2]:
self.log.log('ERR: %s path has invalid ownership' % path)
ret = False
return ret
def getAllLastOtps(self):
'''
Return the list of saved Otps or None if something went wrong
'''
if not self.valid:
return None
#
# Pre checkings - path
# #####################
if not self.checkPath(self.motp_dir) or \
not self.checkPath(self.lastotp_file):
return None
import cPickle
try:
flastotp = open(self.lastotp_file, 'rb')
picklelastotp = dict(cPickle.load(flastotp))
flastotp.close()
except:
import traceback
self.log.log('ERR: Something went wrong with lastotp: %s' % \
str(traceback.format_exc()))
return None
self.log.debug('DEBUG: picklelastotp: [[%s]]' % str(picklelastotp), 10)
return dict(picklelastotp)
def getLastOtps(self):
'''
Get the last valid otps for the user
'''
lastotps = self.getAllLastOtps()
if not lastotps:
return None
ret = list()
try:
for i in lastotps.keys():
if self.decrypt(i) == self.username:
ret = list(lastotps[i])
break
except:
ret = list()
return ret
def getAllInfo(self):
'''
get all info dict
'''
#
# Pre checkings - path
# #####################
if not self.valid:
return None
if not self.checkPath(self.motp_dir) or \
not self.checkPath(self.db_file):
return None
import cPickle
try:
fuserinfo = open(self.db_file, 'rb')
pickleuserinfo = dict(cPickle.load(fuserinfo))
fuserinfo.close()
except:
import traceback
self.log.log('ERR: Something went wrong with userinfo db: %s' % \
str(traceback.format_exc()))
return None
return pickleuserinfo
def getInfo(self):
'''
get all info dict
'''
userinfo = dict(pin=None,
secret=None,
offset=None)
allinfo = self.getAllInfo()
try:
if allinfo:
for i in allinfo.keys():
if self.decrypt(i):
userinfo = dict(allinfo[i])
break
except:
import traceback
print traceback.format_exc()
return dict(pin=None,
secret=None,
offset=None)
return userinfo
def setLastOtp(self, otp):
'''
store the matched otp that it can't be used next
'''
lastotp = self.getAllLastOtps()
self.log.debug('DEBUG: before lastotp: [[%s]] otp: <%s>' % (
str(lastotp),
str(otp)), 10)
if not self.valid:
return None
if not lastotp:
lastotp = dict()
username_in_lastopt = None
for i in lastotp:
if self.decrypt(i) == self.username:
username_in_lastopt = i
break
if not username_in_lastopt:
lastotp[self.eusername] = list()
username_in_lastopt = self.eusername
lastotp[username_in_lastopt].insert(0, otp)
lastotp[username_in_lastopt] = lastotp[username_in_lastopt][:10]
self.log.debug('DEBUG: after lastotp: [[%s]]' % str(lastotp), 10)
import cPickle
try:
flastotp = open(self.lastotp_file, 'wb')
cPickle.dump(lastotp, flastotp)
flastotp.close()
except:
import traceback
self.log.log("ERR: Can't store lastotps in file: %s" % \
str(traceback.format_exc()))
return None
return True
def setUserInfo(self, pin, secret=None, offset=None):
'''
set the userinfo
'''
import os
if not self.valid:
return False
#
# Check or create motp config dir
# ################################
if not os.access(self.motp_dir, os.R_OK):
try:
os.makedirs(self.motp_dir, 0700)
os.chown(self.motp_dir, self.userid, self.groupid)
except:
import traceback
self.log.log(
"ERR: Something went wrong with" + \
" %s path creating! Traceback: %s" % (
self.motp_dir, str(traceback.format_exc())
)
)
return False
#
# Check or create lastotp database
# #################################
if not os.access(self.lastotp_file, os.R_OK):
import cPickle
try:
flastotp = open(self.lastotp_file, 'wb')
cPickle.dump(list(), flastotp)
flastotp.close()
except:
import traceback
self.log.log("ERR: Can't inicialise lastotps %s file: %s" % (
self.lastotp_file,
str(traceback.format_exc())
)
)
return False
try:
os.chmod(self.lastotp_file, 0600)
os.chown(self.lastotp_file, self.userid, self.groupid)
except:
import traceback
self.log.log("ERR: Something went wrong with" + \
" %s lastotp inicialization! Traceback: %s" % (
self.lastotp_file,
str(traceback.format_exc())
)
)
return False
#
# Check or create motp database
# ###############################
if not os.access(self.db_file, os.R_OK):
import cPickle
try:
fdb = open(self.db_file, 'wb')
cPickle.dump(dict(), fdb)
fdb.close()
except:
import traceback
self.log.log("ERR: Can't inicialise motp db %s file: %s" % (
self.db_file,
str(traceback.format_exc())
)
)
return False
try:
os.chmod(self.db_file, 0600)
os.chown(self.db_file, self.userid, self.groupid)
except:
import traceback
self.log.log("ERR: Something went wrong with" + \
" %s motp db inicialization! Traceback: %s" % (
self.db_file,
str(traceback.format_exc())
)
)
return False
#
# Load existing motp database
# ############################
try:
alluserinfo = dict(self.getAllInfo())
except:
return False
username_in_alluserinfo = None
for i in alluserinfo.keys():
if self.decrypt(i) == self.username:
username_in_alluserinfo = i
break
if not username_in_alluserinfo:
username_in_alluserinfo = self.eusername
alluserinfo[username_in_alluserinfo] = dict(offset=0)
try:
if not secret:
secret = alluserinfo[username_in_alluserinfo]['secret']
except:
self.log.log("ERR: No secret is set for the account!")
return False
if not offset:
offset = alluserinfo[username_in_alluserinfo]['offset']
#
# Replace with new motp database
# ###############################
alluserinfo[username_in_alluserinfo] = dict(pin=pin,
secret=secret,
offset=offset)
import cPickle
try:
fdb = open(self.db_file, 'wb')
cPickle.dump(alluserinfo, fdb)
fdb.close()
except:
import traceback
self.log.log("ERR: Can't store motp data in %s file: %s" % (
self.db_file,
str(traceback.format_exc())))
return False
return True