-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patheditsilly
executable file
·135 lines (110 loc) · 3.71 KB
/
editsilly
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
#!/usr/bin/env python3
import os, sys, getopt, random, string
_data = None
FILENAME = "users.dat"
def init():
global _data
_data = []
names = set()
if not os.path.isfile(FILENAME): return
with open(FILENAME, 'r') as handle:
for line in handle:
line = line.rstrip()
if len(line) == 0 or line[0] == '#':
_data.append(('text', line))
continue
i = line.find(':')
if i < 0: continue
j = line.find(':', i + 1)
if j < 0: continue
uname, pwd = line[:i].strip(), line[i+1:j]
if uname in names: continue
_data.append(('user', uname, pwd))
names.add(uname)
def usage():
print()
print("Usage: editsilly [options] username...")
print("with options:")
print(" -h, --help Output this online help.")
print(" -d, --delete Delete users instead of adding them.")
def make_pwd():
"""
Construct a non-pronounceable word.
THIS IS NOT SECURE, DO NOT USE IN PRODUCTION.
"""
text = string.ascii_letters + string.digits
text = text + ''.join(p for p in string.punctuation if p not in ':#')
pwd = []
for i in range(20):
pwd.append(text[random.randrange(len(text))])
return ''.join(pwd)
def check_uname(name):
has_lower = False
# First must be a letter
if name[0] in string.ascii_lowercase:
has_lower = True
elif name[0] in string.ascii_uppercase:
return False
for a in name[1:]:
if a in string.ascii_lowercase:
has_lower = True
elif a in string.ascii_uppercase:
pass
elif a in string.digits:
pass
else:
return False
if len(name) < 3 or not has_lower: return False
return True
def main():
global _data
init()
try:
opts, args = getopt.getopt(sys.argv[1:], "hd", ["help", "delete"])
except getopt.GetoptError as err:
# print help information and exit:
print("ERROR: " + str(err))
usage()
sys.exit(2)
delete = False
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(0)
if opt in ('-d', '--delete'):
delete = True
continue
raise ValueError("Unexpected option value {} encountered".format(repr(opt)))
if len(args) == 0:
print("ERROR: Missing username argument")
usage()
sys.exit(1)
for arg in args:
if delete:
_data = [line for line in _data if line[0] != 'user' or line[1] != arg]
else:
if not check_uname(arg):
msg = ("Username \"{}\" is not acceptable (only letters and digits, " +
"length at least 3, and at least one lower case letter).")
msg = msg.format(arg)
print(msg)
sys.exit(1)
_data = [line for line in _data if line[0] != 'user' or line[1] != arg]
pwd = make_pwd()
_data.append(('user', arg, pwd))
print("Username: " + arg)
print("Password: " + pwd)
print()
with open(FILENAME, 'w') as handle:
for line in _data:
if line[0] == 'text':
line = line[1]
if len(line) != 0 and line[0] != '#': line = '#' + line
handle.write(line + '\n')
elif line[0] == 'user':
handle.write(line[1] + ':' +line[2] + ':\n')
print(" ******************************************")
print(" * THIS SYSTEM USES UNENCRYPTED PASSWORDS *")
print(" * USE AT YOUR OWN RISK *")
print(" ******************************************")
main()