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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <netdb.h>
#include <sys/time.h>
#include <histedit.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define LINELEN 2048
static char Buffer[LINELEN], Command[LINELEN];
static int
Usage()
{
fprintf(stderr, "Usage: pppctl [-v] [ -t n ] [ -p passwd ] "
"Port|LocalSock [command[;command]...]\n");
fprintf(stderr, " -v tells pppctl to output all"
" conversation\n");
fprintf(stderr, " -t n specifies a timeout of n"
" seconds (default 2)\n");
fprintf(stderr, " -p passwd specifies your password\n");
return 1;
}
static int TimedOut = 0;
static void
Timeout(int Sig)
{
TimedOut = 1;
}
#define REC_PASSWD (1)
#define REC_SHOW (2)
#define REC_VERBOSE (4)
static char *passwd;
static char *prompt;
static char *
GetPrompt(EditLine *e)
{
if (prompt == NULL)
prompt = "";
return prompt;
}
static int
Receive(int fd, unsigned TimeoutVal, int display)
{
int Result;
struct sigaction act, oact;
int len;
char *last;
TimedOut = 0;
if (TimeoutVal) {
act.sa_handler = Timeout;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, &oact);
alarm(TimeoutVal);
}
prompt = Buffer;
len = 0;
while (Result = read(fd, Buffer+len, sizeof(Buffer)-len-1), Result != -1) {
len += Result;
Buffer[len] = '\0';
if (TimedOut) {
if (display & REC_VERBOSE)
write(1,Buffer,len);
Result = -1;
break;
} else if (len > 2 && !strcmp(Buffer+len-2, "> ")) {
prompt = strrchr(Buffer, '\n');
if (display & (REC_SHOW|REC_VERBOSE)) {
if (display & REC_VERBOSE)
last = Buffer+len-1;
else
last = prompt;
if (last) {
last++;
write(1, Buffer, last-Buffer);
}
}
prompt = prompt == NULL ? Buffer : prompt+1;
for (last = Buffer+len-2; last > Buffer && *last != ' '; last--)
;
if (last > Buffer+3 && !strncmp(last-3, " on", 3)) {
/* a password is required ! */
if (display & REC_PASSWD) {
if (TimeoutVal) {
alarm(0);
sigaction(SIGALRM, &oact, 0);
}
/* password time */
if (!passwd)
passwd = getpass("Password: ");
sprintf(Buffer, "passwd %s\n", passwd);
memset(passwd, '\0', strlen(passwd));
if (display & REC_VERBOSE)
write(1, Buffer, strlen(Buffer));
write(fd, Buffer, strlen(Buffer));
memset(Buffer, '\0', strlen(Buffer));
return Receive(fd, TimeoutVal, display & ~REC_PASSWD);
}
Result = 1;
} else
Result = 0;
break;
}
}
if (TimedOut)
Result = -1;
if (TimeoutVal) {
alarm(0);
sigaction(SIGALRM, &oact, 0);
}
return Result;
}
static int data = -1;
static void
check_fd(int sig)
{
if (data != -1) {
struct timeval t;
fd_set f;
static char buf[LINELEN];
FD_ZERO(&f);
FD_SET(data, &f);
t.tv_sec = t.tv_usec = 0;
if (select(data+1, &f, NULL, NULL, &t) > 0)
write(1, buf, read(data, buf, sizeof buf));
}
}
static const char *
smartgets(EditLine *e, int *count, int fd)
{
const char *result;
/* struct itimerval it; */
data = fd;
signal(SIGALRM, check_fd);
ualarm(500000, 500000);
result = el_gets(e, count);
ualarm(0,0);
signal(SIGALRM, SIG_DFL);
data = -1;
return result;
}
int
main(int argc, char **argv)
{
struct servent *s;
struct hostent *h;
struct sockaddr *sock;
struct sockaddr_in ifsin;
struct sockaddr_un ifsun;
int socksz, arg, fd, len, verbose;
unsigned TimeoutVal;
char *DoneWord = "x", *next, *start;
struct sigaction act, oact;
verbose = 0;
TimeoutVal = 2;
for (arg = 1; arg < argc; arg++)
if (*argv[arg] == '-') {
for (start = argv[arg] + 1; *start; start++)
switch (*start) {
case 't':
TimeoutVal = (unsigned)atoi
(start[1] ? start + 1 : argv[++arg]);
start = DoneWord;
break;
case 'v':
verbose = REC_VERBOSE;
break;
case 'p':
passwd = (start[1] ? start + 1 : argv[++arg]);
start = DoneWord;
break;
default:
return Usage();
}
}
else
break;
if (argc < arg + 1)
return Usage();
if (*argv[arg] == '/') {
sock = (struct sockaddr *)&ifsun;
socksz = sizeof ifsun;
ifsun.sun_len = strlen(argv[arg]);
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
fprintf(stderr, "%s: Path too long\n", argv[arg]);
return 1;
}
ifsun.sun_family = AF_LOCAL;
strcpy(ifsun.sun_path, argv[arg]);
if (fd = socket(AF_LOCAL, SOCK_STREAM, 0), fd < 0) {
fprintf(stderr, "Cannot create local domain socket\n");
return 2;
}
} else {
char *port, *host, *colon;
int hlen;
colon = strchr(argv[arg], ':');
if (colon) {
port = colon + 1;
*colon = '\0';
host = argv[arg];
} else {
port = argv[arg];
host = "127.0.0.1";
}
sock = (struct sockaddr *)&ifsin;
socksz = sizeof ifsin;
hlen = strlen(host);
if (strspn(host, "0123456789.") == hlen) {
if (!inet_aton(host, (struct in_addr *)&ifsin.sin_addr.s_addr)) {
fprintf(stderr, "Cannot translate %s\n", host);
return 1;
}
} else if ((h = gethostbyname(host)) == 0) {
fprintf(stderr, "Cannot resolve %s\n", host);
return 1;
}
else
ifsin.sin_addr.s_addr = *(u_long *)h->h_addr_list[0];
if (colon)
*colon = ':';
if (strspn(port, "0123456789") == strlen(port))
ifsin.sin_port = htons(atoi(port));
else if (s = getservbyname(port, "tcp"), !s) {
fprintf(stderr, "%s isn't a valid port or service!\n", port);
return Usage();
}
else
ifsin.sin_port = s->s_port;
ifsin.sin_len = sizeof(ifsin);
ifsin.sin_family = AF_INET;
if (fd = socket(AF_INET, SOCK_STREAM, 0), fd < 0) {
fprintf(stderr, "Cannot create internet socket\n");
return 2;
}
}
TimedOut = 0;
if (TimeoutVal) {
act.sa_handler = Timeout;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, &oact);
alarm(TimeoutVal);
}
if (connect(fd, sock, socksz) < 0) {
if (TimeoutVal) {
alarm(0);
sigaction(SIGALRM, &oact, 0);
}
if (TimedOut)
fputs("Timeout: ", stderr);
fprintf(stderr, "Cannot connect to socket %s\n", argv[arg]);
close(fd);
return 3;
}
if (TimeoutVal) {
alarm(0);
sigaction(SIGALRM, &oact, 0);
}
len = 0;
Command[sizeof(Command)-1] = '\0';
for (arg++; arg < argc; arg++) {
if (len && len < sizeof(Command)-1)
strcpy(Command+len++, " ");
strncpy(Command+len, argv[arg], sizeof(Command)-len-1);
len += strlen(Command+len);
}
switch (Receive(fd, TimeoutVal, verbose | REC_PASSWD))
{
case 1:
fprintf(stderr, "Password incorrect\n");
break;
case 0:
if (len == 0) {
EditLine *edit;
History *hist;
const char *l, *env;
int size;
hist = history_init();
if ((env = getenv("EL_SIZE"))) {
size = atoi(env);
if (size < 0)
size = 20;
} else
size = 20;
history(hist, H_EVENT, size);
edit = el_init("pppctl", stdin, stdout);
el_source(edit, NULL);
el_set(edit, EL_PROMPT, GetPrompt);
if ((env = getenv("EL_EDITOR")))
if (!strcmp(env, "vi"))
el_set(edit, EL_EDITOR, "vi");
else if (!strcmp(env, "emacs"))
el_set(edit, EL_EDITOR, "emacs");
el_set(edit, EL_SIGNAL, 1);
el_set(edit, EL_HIST, history, (const char *)hist);
while ((l = smartgets(edit, &len, fd))) {
if (len > 1)
history(hist, H_ENTER, l);
write(fd, l, len);
if (!strcasecmp(l, "quit\n") ||
!strcasecmp(l, "bye\n")) /* ok, we're cheating */
break;
if (Receive(fd, TimeoutVal, REC_SHOW) != 0) {
fprintf(stderr, "Connection closed\n");
break;
}
}
el_end(edit);
history_end(hist);
} else {
start = Command;
do {
next = strchr(start, ';');
while (*start == ' ' || *start == '\t')
start++;
if (next)
*next = '\0';
strcpy(Buffer, start);
Buffer[sizeof(Buffer)-2] = '\0';
strcat(Buffer, "\n");
if (verbose)
write(1, Buffer, strlen(Buffer));
write(fd, Buffer, strlen(Buffer));
if (Receive(fd, TimeoutVal, verbose | REC_SHOW) != 0) {
fprintf(stderr, "No reply from ppp\n");
break;
}
if (next)
start = ++next;
} while (next && *next);
if (verbose)
puts("");
}
break;
default:
fprintf(stderr, "ppp is not responding\n");
break;
}
close(fd);
return 0;
}
|