-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.c
258 lines (201 loc) · 6.3 KB
/
commands.c
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
#include "modembank.h"
#include "commands.h"
void (* const command_list[COMMAND_COUNT]) ( data * mdata, user * muser, int argc, char * argv[]) = {
commandHelp,
commandExit,
commandClear,
commandTelnet
};
const char * command_alias[COMMAND_COUNT] = {
"help,?",
"exit,quit,logout",
"clear,cls",
"telnet,connect"
};
void (* const findCommand(const char * cmd)) ( data * mdata, user * muser, int argc, char * argv[] )
{
if ( cmd == NULL ) return NULL;
char * cmdtok;
char alias[256];
int i;
for ( i = 0; i < COMMAND_COUNT; i++ )
{
// Don't let strtok destroy the original
strcpy( alias, command_alias[i] );
// Check each alias
cmdtok = strtok( alias, "," );
while ( cmdtok != NULL )
{
if ( !strcmp( cmdtok, cmd ) )
{
return command_list[i];
}
cmdtok = strtok( NULL, "," );
}
}
return NULL;
}
void commandHelp( data * mdata, user * muser, int argc, char * argv[] )
{
char helppath[300];
char helpbuf[256];
int i;
FILE * fd;
if ( argc > 1 )
{
if ( findCommand( argv[1] ) == NULL )
{
// Print out error message
uprintf( muser, "%%help: unknown command: %s\r\n", argv[1] );
return;
}
sprintf( helppath, "assets/help/%s.hlp", argv[1] );
fd = fopen( helppath, "r" );
if ( fd == NULL )
{
zlog( "Unable to open help file: %s\n", helppath );
return;
}
// Discard first line
fgets( helpbuf, 256, fd );
while ( fgets( helpbuf, 256, fd ) != NULL )
{
int lln = strlen( helpbuf );
// Discard unix line endings
if ( helpbuf[lln - 1] == '\n' ) helpbuf[lln - 1] = '\0';
uprintf( muser, "%s\r\n", helpbuf );
}
// Close help file
fclose( fd );
return;
}
DIR * dr = opendir( "assets/help/" );
if ( dr == NULL )
{
zlog( "Unable to open help directory: assets/help/\n" );
return;
}
struct dirent * dir;
// Read each file in directory
while ( (dir = readdir( dr )) != NULL )
{
// Find location of "." in filename
char * dotpos = strrchr( dir->d_name, '.' );
// Not a help file
if ( dotpos == NULL || strcmp( dotpos, ".hlp" ) ) continue;
// Build help file path
sprintf( helppath, "assets/help/%s", dir->d_name );
fd = fopen( helppath, "r" );
if ( fd == NULL )
{
zlog( "Unable to open help file: %s\n", helppath );
return;
}
// Get first line
fgets( helpbuf, 256, fd );
// Remove unix line endings
int lln = strlen( helpbuf );
if ( helpbuf[lln - 1] == '\n' ) helpbuf[lln - 1] = '\0';
// Send first line
uprintf( muser, "%s\r\n", helpbuf );
// Close help file
fclose( fd );
}
// Close help directory
closedir( dr );
}
// Logout this user
void commandExit( data * mdata, user * muser, int argc, char * argv[] )
{
// Tell user they're being logged off
uprintf( muser, "%%%s: terminated session\r\n", argv[0] );
// Deep six their ass
muser->flags |= FLAG_GARB;
}
void commandClear( data * mdata, user * muser, int argc, char * argv[] )
{
// Transmit ANSI home & clear screen codes
uprintf( muser, "\e[H\e[2J" );
}
void commandTelnet( data * mdata, user * muser, int argc, char * argv[] )
{
// Need to specify an address
if ( argc <= 1 )
{
uprintf( muser, "%%%s: please specify a destination address", argv[0] );
return;
}
struct addrinfo hints, * res;
char addrstr[100];
char * port = "telnet";
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_INET; //AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;// | AI_NUMERICSERV;
// Did the user specify a port?
if ( argc > 2 ) port = argv[2];
if ( getaddrinfo( argv[1], port, &hints, &res ) )
{
uprintf( muser, "%%%s: invalid service/port or address: %s %s\r\n", argv[0], argv[1], port );
return;
}
if ( !res )
{
uprintf( muser, "%%%s: unable to resolve address: %s\r\n", argv[0], argv[1] );
return;
}
// Get actual addresss
struct sockaddr_in * destip = (struct sockaddr_in *)res->ai_addr;
// Get a human readable address
inet_ntop( res->ai_family, &( destip->sin_addr ), addrstr, 100 );
// Begin connection process
int sock = socket( res->ai_family, res->ai_socktype, 0 );
if ( sock < 0 )
{
xlog( muser, "req_opensock: unable to create socket\n" );
return;
}
// Generate new connection
conn * newconn = malloc( sizeof(conn) );
// Record time
time( &(newconn->first) );
time( &(newconn->last ) );
// Initialize conn
newconn->flags = FLAG_OUTG;
newconn->fd = sock;
newconn->buflen = 0;
newconn->org.addr = *(destip);
// Print out the port and string
sprintf( newconn->name, "%s:%hu", addrstr, newconn->org.addr.sin_port );
// Set non-blocking
if ( setBlocking( newconn, 0 ) < 0 )
{
xlog( muser, "req_opensock: failed to set non-blocking\n" );
close( sock );
free( newconn );
return;
}
if ( connect( sock, res->ai_addr, sizeof( *(res->ai_addr) ) ) < 0 )
{
if ( errno != EINPROGRESS )
{
uprintf( muser, "Failed to connect to %s (%s) on port %hu\r\n", addrstr, res->ai_canonname, htons(destip->sin_port) );
xlog( muser, "Failed to connect to %s (%s) on port %hu\r\n", addrstr, res->ai_canonname, htons(destip->sin_port) );
// Close and free conn
close( sock );
free( newconn );
return;
}
}
uprintf( muser, "Trying %s (%s) on port %hu\r\n", addrstr, res->ai_canonname, htons(destip->sin_port) );
xlog( muser, "Trying %s (%s) on port %hu\r\n", addrstr, res->ai_canonname, htons(destip->sin_port) );
// Set the stdout of the user
muser->stdout = newconn;
// Set the user into bridge mode
muser->flags |= FLAG_BRDG;
// Insert node
newconn->next = mdata->headconn;
mdata->headconn = newconn;
mdata->conn_count++;
return;
}