-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (175 loc) · 4.61 KB
/
index.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
188
189
190
191
192
///////////////////////////////////////////////////////////////////////
//
// index.js
//
// Entry point for pwdserver
//
// Date: 6 Sep 2018
// Author: Ron Currier
// Copyright 2018 by Sparkfish Heavy Industries
// License: MIT
//
///////////////////////////////////////////////////////////////////////
/*jshint esversion: 6 */
/*jshint node: true */
const express = require ( 'express' );
const app = express();
const passwd = require ( './loadpasswd.js' );
const group = require ( './loadgroup.js' );
//
// Configure options
//
argv = require ( 'yargs' )
.usage ( '$0 [options]' )
.options ( {
'd': {
alias: 'debug',
default: false,
describe: 'Output excessive internal details to console',
type: 'boolean'
},
'g': {
alias: 'groupfile',
default: '/etc/group',
describe: 'File path to group file',
nargs: 1,
normalize: true,
requiresArg: true,
type: 'string'
},
'n': {
alias: 'nowarn',
default: false,
describe: 'Suppress the /etc/passwd and /etc/group usage warning',
type: 'boolean'
},
'o': {
alias: 'outputtype',
describe: 'Format to use for output',
default: 'text',
nargs: 1,
requiresArg: true,
type: 'string',
choices: ['json', 'text'],
},
'p': {
alias: 'passwdfile',
default: '/etc/passwd',
describe: 'File path to passwd file',
nargs: 1,
normalize: true,
requiresArg: true,
type: 'string'
},
'port': {
default: 3000,
describe: 'Port to listen on',
type: 'number'
}
} )
.argv;
global.debug = argv.debug;
global.jsonOutput = ( argv.outputtype=="json" );
if ( !argv.nowarn && ( argv.passwdfile == '/etc/passwd' || argv.groupfile == '/etc/group' ) )
{
console.warn (
' +=======================================================+\n' +
' | WARNING |\n' +
' | You are using the system passwd and group files. |\n' +
' | Use with care as this potentially exposes system |\n' +
' | level information to less than reputable individuals. |\n' +
' | This warning can be suppressed by using the --nowarn |\n' +
' | flag when starting. |\n' +
' +=======================================================+\n'
);
}
//
// Initialize the caches
//
if ( !passwd.loadData ( argv.passwdfile ) || !group.loadData ( argv.groupfile ) )
{
process.exit ( 1 );
}
//
// Routers
//
app.get ( '/users', usersHandler );
app.get ( '/users/query', usersHandler );
app.get ( '/users/:uid', usersHandler );
app.get ( '/users/:uid/groups', usersGroupsHandler );
app.get ( '/groups', groupsHandler );
app.get ( '/groups/query', groupsHandler );
app.get ( '/groups/:gid', groupsHandler );
app.listen ( argv.port, ( ) => {
console.log ( `Listening on port ${argv.port}` );
} );
//
// Route handlers
//
function usersHandler ( req, res, next )
{
if ( !req.route.path.includes ( "query" ) )
{
req.query = { };
}
if ( !isNaN ( req.params.uid ) )
{
// uid was provided, overwrite query parameters
// and search just for uid
req.query = { uid: req.params.uid };
}
result = passwd.getUsers ( req.query );
if ( result.length == 0 )
{
res.status ( 404 ).send ( '404 - Not found' );
}
else
{
if ( global.jsonOutput )
res.send ( result );
else
res.send ( "<pre>" + JSON.stringify(result, null, 2 ) + "</pre>" );
}
}
function usersGroupsHandler ( req, res, next )
{
user = passwd.getUserByUID ( req.params.uid );
if ( !user )
{
res.status ( 404 ).send ( '404 - User not found' );
}
else
{
query = { member: user.name };
result = group.getGroups ( query );
if ( global.jsonOutput )
res.send ( result );
else
res.send ( "<pre>" + JSON.stringify ( result, null, 2 ) + "</pre>" );
}
}
function groupsHandler ( req, res, next )
{
if ( !req.route.path.includes ( "query" ) )
{
req.query = { };
}
if ( !isNaN ( req.params.gid ) )
{
// gid was provided, overwrite query parameters
// and search just for gid
req.query = { gid: req.params.gid };
}
result = group.getGroups ( req.query );
if ( result.length == 0 )
{
res.status ( 404 ).send ( '404 - Not found' );
}
else
{
if ( global.jsonOutput )
res.send ( result );
else
res.send ( "<pre>" + JSON.stringify(result, null, 2 ) + "</pre>" );
}
}