-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadpasswd.js
187 lines (163 loc) · 4.27 KB
/
loadpasswd.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
183
184
185
186
187
///////////////////////////////////////////////////////////////////////
//
// loadpasswd.js
//
// Module containing functions to read and return users
//
// Date: 6 Sep 2018
// Author: Ron Currier
// Copyright 2018 by Sparkfish Heavy Industries
// License: MIT
//
///////////////////////////////////////////////////////////////////////
/*jshint esversion: 6 */
/*jshint node: true */
const _ = require ( 'lodash' );
const fs = require ( 'fs' ),
readline = require ( 'readline' ),
keys = [ "name", "x", "uid", "gid", "comment", "home", "shell" ];
var uid2pwdata = { },
user2uid = { };
module.exports = {
//
// Verify passwd file exists, set file changed watch callback
// and do initial file load
//
// Inputs: path to passwd file
// Returns: true if file successfully parsed, else false
//
loadData: function ( filename )
{
loading = false;
try
{
fs.accessSync ( filename );
}
catch ( err )
{
console.error( `${err}` );
return false;
}
fs.watch ( filename, ( eventType, file ) => {
if ( eventType == 'change' && !loading )
{
loading = true;
module.exports.loadDataEx ( filename );
}
else
{
setTimeout ( ( ) => {
loading = false;
}, 100 );
}
});
module.exports.loadDataEx ( filename );
return true;
},
//
// Read passwd file from disk, parse, and save in cache
//
// Inputs: path to passwd file
// Returns: None
//
loadDataEx: function ( filename )
{
if ( global.debug )
{
console.log ( `Loading passwd file ${filename}`);
}
uid2pwdata = { }; // Clear out old data
user2uid = { };
var instream = fs.createReadStream( filename ),
outstream = new ( require ( 'stream' ) ) ( ),
rl = readline.createInterface ( instream, outstream );
rl.on ( 'line', ( line ) => {
module.exports.parseLine ( line );
});
rl.on ( 'close', ( line ) => {
if ( line ) {
module.exports.parseLine ( line );
}
if ( global.debug )
{
console.log( 'Finished loading passwd file' );
console.log( 'uid2pwdata :', uid2pwdata );
console.log( 'user2uid :', user2uid );
}
});
return;
},
//
// Parse a line from the passwd file
//
// Inputs: line of text
// Returns: None
//
parseLine: function ( line )
{
var data = line.split( ":" ),
jsonData = { };
for ( var i = 0; i < data.length; i++ )
{
if ( keys [ i ] != 'x' )
{
if ( isNaN ( data [ i ] ) )
{
jsonData [keys [ i ] ] = data [ i ];
}
else
{
jsonData[ keys [ i ] ] = Number ( data [ i ] );
}
}
}
uid2pwdata [ jsonData.uid ] = jsonData;
user2uid [ jsonData.name ] = jsonData.uid;
return;
},
//
// Get a list of users that meet a specific criteria
//
// Inputs: Array of key/value pairs that represent the required criteria
// Returns: Array of key/value pairs for each user that meets the criteria
// Empty array if no users found
//
getUsers: function ( query )
{
if ( global.debug )
{
console.log ( `getUsers: query =`, query);
}
for ( var name in query ) // This just checks if query has any properties
{
// Convert any numeric strings to numbers
_.forEach ( query, ( value, key ) => {
if ( !isNaN ( value ) )
query [ key ] = Number ( value );
});
if ( global.debug )
{
console.log ( 'HTTP query =', query );
}
return _.filter ( uid2pwdata, query );
}
// query is empty - return all users
var result = [ ];
for ( var key in uid2pwdata )
{
result.push ( uid2pwdata [ key ] );
}
return result;
},
//
// Get the user that has a specific UID
//
// Inputs: UID for requested user
// Returns: Array of key/value pairs for the user
// Empty array if user not found
//
getUserByUID: function ( uid )
{
return uid2pwdata [ uid ];
}
};