forked from grob/ringo-ftpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
182 lines (168 loc) · 6.08 KB
/
users.js
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
var {Parser} = require("ringo/args");
var shell = require("ringo/shell");
var term = require("ringo/term");
var fs = require("fs");
var objects = require("ringo/utils/objects");
var {encryptPassword} = require("./lib/usermanager");
var parser = new Parser();
parser.addOption("f", "file", "file", "The JSON file containing the accounts");
var help = function() {
term.writeln("Usage:");
term.writeln("\n ringo users.js -f path/to/users.json <command> <username> \n");
term.writeln("Available options:");
term.writeln(parser.help());
term.writeln("Available commands:");
term.writeln(" add / edit / remove / enable / disable / list / password");
term.writeln();
};
var main = function(args) {
var opts = {};
try {
parser.parse(args, opts);
} catch (e) {
term.writeln(term.RED, e.message, term.RESET);
help();
return;
}
if (!opts.file) {
term.writeln(term.RED, "\nPlease specify the file containing the ftp accounts\n", term.RESET);
help();
return;
}
var users = {};
if (fs.exists(opts.file)) {
try {
users = JSON.parse(fs.read(opts.file));
} catch (e) {
term.writeln(opts.file, "is empty or invalid, initializing new account list");
}
}
var command = args.shift();
if (!command || command === "list") {
list(users);
} else if (["add", "edit", "remove", "enable", "disable", "password"].indexOf(command) > -1) {
var username = args.shift();
if (!username) {
term.writeln(term.RED, "Please specify the username", term.RESET);
} else if (this[command](users, username) === true) {
save(users, opts.file);
}
} else {
term.writeln(term.RED, "Unknown command", command, term.RESET);
help();
}
};
var save = function(users, file) {
fs.write(file, JSON.stringify(users, null, 4));
term.writeln(term.GREEN, "Saved user accounts in", file, term.RESET);
};
var getAccountData = function() {
var homeDirectory, canWrite, maxLogin, maxLoginPerIp, downloadRate,
uploadRate, maxIdleTime;
while (!homeDirectory) {
homeDirectory = shell.readln("Home directory: ").trim();
if (!fs.exists(homeDirectory)) {
term.writeln(term.BOLD, "\nHome directory doesn't exist, please try again.\n", term.RESET);
homeDirectory = null;
}
}
canWrite = shell.readln("Write permission (Y/n): ").toLowerCase() != "n";
maxLogin = parseInt(shell.readln("Max. concurrent logins (0 for no restriction): "), 10) || 0;
maxLoginPerIp = parseInt(shell.readln("Max. concurrent logins per IP address (0 for no restriction): "), 10) || 0;
downloadRate = parseInt(shell.readln("Max. download rate (0 = unlimited): "), 10) || 0;
uploadRate = parseInt(shell.readln("Max. upload rate (0 = unlimited): "), 10) || 0;
maxIdleTime = parseInt(shell.readln("Max. idle time (in seconds, 0 = unlimited): "), 10) || 0;
return {
"homeDirectory": homeDirectory,
"canWrite": canWrite,
"maxLogin": maxLogin,
"maxLoginPerIp": maxLoginPerIp,
"downloadRate": downloadRate,
"uploadRate": uploadRate,
"maxIdleTime": maxIdleTime
};
};
var getPassword = function() {
var password, passwordConfirm;
while (!password || (password !== passwordConfirm)) {
password = shell.readln("Password: ", "*").trim();
passwordConfirm = shell.readln("Confirm password: ", "*").trim();
if (password !== passwordConfirm) {
term.writeln(term.BOLD, "\nPasswords do not match, please try again.\n", term.RESET);
}
}
return encryptPassword(password);
};
var add = function(users, username) {
if (users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "already exists", term.RESET);
return false;
}
users[username] = objects.merge({
"name": username,
"password": getPassword(),
"isEnabled": true
}, getAccountData());
term.writeln(term.GREEN, "Added user", username, term.RESET);
return true;
};
var edit = function(users, username) {
if (!users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "doesn't exist", term.RESET);
return false;
}
users[username] = objects.merge(getAccountData(), users[username]);
term.writeln(term.GREEN, "Changed user", username, term.RESET);
return true;
};
var list = function(users) {
term.writeln(term.BOLD, "\nAvailable FTP accounts\n", term.RESET)
for each (let props in users) {
term.write(" ");
if (props.isEnabled === false) {
term.writeln("[disabled]");
}
term.writeln(props.name, "(" + props.homeDirectory + ")");
}
term.writeln();
return true;
};
var remove = function(users, username) {
if (!users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "doesn't exist");
return false;
}
delete users[username];
term.writeln(term.GREEN, "Removed user", username, term.RESET);
return true;
};
var enable = function(users, username) {
if (!users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "doesn't exist");
return false;
}
users[username].isEnabled = true;
term.writeln(term.GREEN, "Enabled user", username, term.RESET);
return true;
};
var disable = function(users, username) {
if (!users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "doesn't exist");
return false;
}
users[username].isEnabled = false;
term.writeln(term.GREEN, "Disabled user", username, term.RESET);
return true;
};
var password = function(users, username) {
if (!users.hasOwnProperty(username)) {
term.writeln(term.RED, "User", username, "doesn't exist");
return false;
}
users[username].password = getPassword();
term.writeln(term.GREEN, "Changed password of user", username, term.RESET);
return true;
};
if (require.main == module.id) {
main(require('system').args.splice(1));
}