-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwl2ktelnet.c
540 lines (466 loc) · 13.9 KB
/
wl2ktelnet.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2; c-brace-offset: -2; c-argdecl-indent: 2 -*- */
/* paclink-unix client for the Winlink 2000 ham radio email system.
*
* Copyright 2006 Nicholas S. Castellano <n2qz@arrl.net> and others,
* See the file AUTHORS for a list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef __RCSID
__RCSID("$Id$");
#endif
#if HAVE_STDIO_H
# include <stdio.h>
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_CTYPE_H
# include <ctype.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#if HAVE_NETDB_H
# include <netdb.h>
#endif
#ifndef bool
#include <stdbool.h>
#endif /* bool */
#include <getopt.h>
#include <gmime/gmime.h>
#include "compat.h"
#include "conf.h"
#include "timeout.h"
#include "wl2k.h"
#include "strutil.h"
/*
* Globals
*/
int gverbose_flag=FALSE;
int gsendmsgonly_flag=FALSE;
static bool loadconfig(int argc, char **argv, cfg_t *cfg);
static void usage(void);
static void displayversion(void);
static void displayconfig(cfg_t *cfg);
/**
* Function: main
*
* The calling convention for this function is:
*
* wl2ktelnet -c targetcall -t timeoutsecs -e emailaddress -p password HOSTNAME PORT
*
* The parameters are:
* mycall : my call sign, which MUST be set in wl2k.conf
* targetcall: callsign for the telnet server (WL2K)
* timeoutsecs: timeout in seconds
* emailaddress: email address where the retrieved message will be sent via sendmail
* password of the telnet host (CMSTelnet)
* hostname of the telnet host (server.winlink.org)
* port to be used for the telnet host (8772)
*
*/
int
main(int argc, char *argv[])
{
struct hostent *host;
int s;
struct sockaddr_in s_in;
FILE *fp;
char *line;
static cfg_t cfg;
loadconfig(argc, argv, &cfg);
#if (GMIME_VER > 2)
g_mime_init();
#else
g_mime_init(0);
#endif
setlinebuf(stdout);
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket()");
exit(EXIT_FAILURE);
}
memset(&s_in, '\0', sizeof(struct sockaddr_in));
#if HAVE_SOCKADDR_IN_SIN_LEN
s_in.sin_len = sizeof(struct sockaddr_in);
#endif
s_in.sin_family = AF_INET;
s_in.sin_addr.s_addr = inet_addr(cfg.hostname);
if ((int) s_in.sin_addr.s_addr == -1) {
host = gethostbyname(cfg.hostname);
if (host) {
memcpy(&s_in.sin_addr.s_addr, host->h_addr, (unsigned) host->h_length);
} else {
herror(cfg.hostname);
exit(EXIT_FAILURE);
}
}
s_in.sin_port = htons((unsigned short)cfg.hostport);
printf("Connecting to %s %d ...\n", cfg.hostname, cfg.hostport);
settimeout(cfg.timeoutsecs);
if (connect(s, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)) != 0) {
close(s);
perror("connect()");
exit(EXIT_FAILURE);
}
resettimeout();
printf("Connected.\n");
if ((fp = fdopen(s, "r+b")) == NULL) {
close(s);
perror("fdopen()");
exit(EXIT_FAILURE);
}
while ((line = wl2kgetline(fp)) != NULL) {
printf("%s", line);
if (strncmp("Callsign", line, 8) == 0) {
fprintf(fp, ".%s\r\n", cfg.mycall);
printf(" %s\n", cfg.mycall);
break;
}
putchar('\n');
}
if (line == NULL) {
fprintf(stderr, "Connection closed by foreign host.\n");
exit(EXIT_FAILURE);
}
while ((line = wl2kgetline(fp)) != NULL) {
printf("%s\n", line);
if (strncmp("Password", line, 8) == 0) {
fprintf(fp, "%s\r\n", cfg.password);
break;
}
}
if (line == NULL) {
fprintf(stderr, "Connection closed by foreign host.\n");
exit(EXIT_FAILURE);
}
wl2k_exchange(&cfg, fp, fp);
fclose(fp);
g_mime_shutdown();
exit(EXIT_SUCCESS);
return 1;
}
/*
* Print usage information and exit
* - does not return
*/
static void
usage(void)
{
printf("Usage: %s [options] [HOSTNAME PORT]\n", getprogname());
printf(" -c --target-call Set callsign to call\n");
printf(" -p --password Set password for telnet login\n");
printf(" -P --wl2k-passwd Set password for WL2K secure login\n");
printf(" -t --timeout Set timeout in seconds\n");
printf(" -e --email-address Set your e-mail address\n");
printf(" -s --send-only Send messages only\n");
printf(" -V --verbose Print verbose messages\n");
printf(" -v --version Display program version only\n");
printf(" -C --configuration Display configuration only\n");
printf(" -h --help Display this usage info\n");
exit(EXIT_SUCCESS);
}
/*
* Display package version of this program.
*/
static void
displayversion(void)
{
printf("%s %s", getprogname(), PACKAGE_VERSION);
/* Check verbose flag for displaying gmime version */
if(gverbose_flag) {
printf(", Using gmime version %d.%d.%d",
gmime_major_version, gmime_minor_version, gmime_micro_version);
}
printf("\n");
}
/*
* Display configuration parameters
* parsed from defaults, config file & command line
*/
static void
displayconfig(cfg_t *cfg)
{
printf("%s: Using this config:\n", getprogname());
if(cfg->mycall) {
printf(" My callsign: %s\n", cfg->mycall);
}
if(cfg->targetcall) {
printf(" Target callsign: %s\n", cfg->targetcall);
}
if(cfg->hostname) {
printf(" Host name: %s\n", cfg->hostname);
}
printf(" Host port: %d\n", cfg->hostport);
if(cfg->password) {
printf(" Login password: %s\n", cfg->password);
}
#ifdef DEBUG /* Do not display Winlink password */
if (cfg->wl2k_password) {
printf(" WL2K secure login password: %s\n", cfg->wl2k_password);
}
#endif /* DEBUG end */
printf(" Timeout: %d\n", cfg->timeoutsecs);
if(cfg->emailaddr) {
printf(" Email address: %s\n", cfg->emailaddr);
}
if(cfg->gridsquare) {
printf(" Grid square: %s\n", cfg->gridsquare);
}
printf(" Flags: verbose = %s, send-only = %s\n",
cfg->bVerbose ? "On" : "Off",
cfg->bSendonly ? "On" : "Off");
}
/* Load these 7 config parameters:
* mycall targetcall hostname port timeoutsecs password emailaddress
*/
static bool
loadconfig(int argc, char **argv, cfg_t *config)
{
struct conf *fileconf;
char *endp;
int next_option;
int option_index = 0; /* getopt_long stores the option index here. */
char *cfgbuf;
static int displayconfig_flag=FALSE;
bool bDisplayVersion_flag = FALSE;
bool bRequireConfig_pass = TRUE;
/* short options */
static const char *short_options = "hVvsCc:t:e:p:P:";
/* long options */
static struct option long_options[] =
{
/* These options set a flag. */
{"verbose", no_argument, &gverbose_flag, TRUE},
{"config", no_argument, &displayconfig_flag, TRUE},
{"send-only", no_argument, &gsendmsgonly_flag, TRUE},
/* These options don't set a flag.
We distinguish them by their indices. */
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"target-call", required_argument, NULL, 'c'},
{"timeout", required_argument, NULL, 't'},
{"email-address", required_argument, NULL, 'e'},
{"password", required_argument, NULL, 'p'},
{"wl2k-passwd", required_argument, NULL, 'P'},
{NULL, no_argument, NULL, 0} /* array termination */
};
/* get a temporary buffer to build strings */
cfgbuf = (char *)malloc(256);
if(cfgbuf == NULL) {
fprintf(stderr, "%s: loadconfig, out of memory\n", getprogname());
return(FALSE);
}
/*
* Initialize default config
*/
/* use either cuserid(NULL) or getenv("LOGNAME"),
* - getlogin() does NOT work */
sprintf(cfgbuf, "%s@localhost", cuserid(NULL) );
config->emailaddr = strdup(cfgbuf);
strcpy(cfgbuf, DFLT_TELNET_PASSWORD);
config->password = strdup(cfgbuf);
strcpy(cfgbuf, DFLT_TELNET_CALL);
config->targetcall = strdup(cfgbuf);
strcpy(cfgbuf, DFLT_TELNET_HOSTNAME);
config->hostname = strdup(cfgbuf);
free(cfgbuf);
config->mycall = NULL;
config->wl2k_password = NULL;
config->timeoutsecs = DFLT_TIMEOUTSECS;
config->hostport = DFLT_TELNET_PORT;
config->bVerbose = FALSE;
config->bSendonly = FALSE;
config->gridsquare = NULL;
/*
* Get config from config file
*/
fileconf = conf_read();
if ((config->mycall = conf_get(fileconf, "mycall")) == NULL) {
fprintf(stderr, "%s: failed to read mycall from configuration file\n", getprogname());
exit(EXIT_FAILURE);
}
if ((cfgbuf = conf_get(fileconf, "timeout")) != NULL) {
config->timeoutsecs = (unsigned int) strtol(cfgbuf, &endp, 10);
if (*endp != '\0') {
usage(); /* does not return */
}
}
if ((cfgbuf = conf_get(fileconf, "email")) != NULL) {
config->emailaddr = cfgbuf;
}
if ((cfgbuf = conf_get(fileconf, "password")) != NULL) {
config->password = cfgbuf;
}
if ((cfgbuf = conf_get(fileconf, "wl2k-password")) != NULL) {
config->wl2k_password = cfgbuf;
}
if ((cfgbuf = conf_get(fileconf, "gridsquare")) != NULL) {
config->gridsquare = cfgbuf;
}
if ((cfgbuf = conf_get(fileconf, "hostname")) != NULL) {
config->hostname = cfgbuf;
}
if ((cfgbuf = conf_get(fileconf, "hostport")) != NULL) {
config->hostport = (unsigned short) strtol(cfgbuf, &endp, 10);
if (*endp != '\0') {
usage(); /* does not return */
}
}
/*
* Get config from command line
*/
opterr = 0;
option_index = 0;
next_option = getopt_long (argc, argv, short_options,
long_options, &option_index);
while( next_option != -1 ) {
switch (next_option)
{
case 0: /* long option without a short arg */
/* If this option sets a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
fprintf (stderr, "Debug: option %s", long_options[option_index].name);
if (optarg)
fprintf (stderr," with arg %s", optarg);
fprintf (stderr,"\n");
break;
case 'v': /* set display version flag */
bDisplayVersion_flag = TRUE;
break;
case 'V': /* set verbose flag */
gverbose_flag = TRUE;
break;
case 'c': /* set callsign to contact */
config->targetcall = optarg;
break;
case 'C': /* set display config flag */
displayconfig_flag = TRUE;
break;
case 's': /* set send message only flag */
gsendmsgonly_flag = TRUE;
break;
case 't': /* set time out in seconds */
config->timeoutsecs = (unsigned int) strtol(optarg, &endp, 10);
if (*endp != '\0') {
usage(); /* does not return */
}
break;
case 'e': /* set email address */
config->emailaddr = optarg;
break;
case 'p': /* set telnet password */
config->password = optarg;
break;
case 'P': /* set secure login password */
config->wl2k_password = optarg;
break;
case 'h':
usage(); /* does not return */
break;
case '?':
if (isprint (optopt)) {
fprintf (stderr, "%s: Unknown option `-%c'.\n",
getprogname(), optopt);
} else {
fprintf (stderr,"%s: Unknown option character `\\x%x'.\n",
getprogname(), optopt);
}
/* fall through */
default:
usage(); /* does not return */
break;
}
next_option = getopt_long (argc, argv, short_options,
long_options, &option_index);
}
/* set send only flag here in case long option was used */
config->bSendonly = gsendmsgonly_flag;
/* set verbose flag here in case long option was used */
config->bVerbose = gverbose_flag;
/*
* Get positional command line args hostname hostport
*/
if(optind < argc) {
config->hostname = argv[optind++];
}
if(optind < argc) {
config->hostport = (unsigned short) strtol(argv[optind], &endp, 10);
if (*endp != '\0') {
usage(); /* does not return */
}
}
if(bDisplayVersion_flag) {
displayversion();
exit(EXIT_SUCCESS);
}
/* test for required parameters */
if(config->targetcall == NULL) {
fprintf(stderr, "%s: Need to specify target callsign\n", getprogname() );
bRequireConfig_pass = FALSE;
}
if(config->hostname == NULL) {
fprintf(stderr, "%s: Need to specify hostname\n", getprogname() );
bRequireConfig_pass = FALSE;
}
if(config->hostport == 0) {
fprintf(stderr, "%s: Need to specify hostport\n", getprogname() );
bRequireConfig_pass = FALSE;
}
/* display warning if gridsquare not set */
if(config->gridsquare == NULL) {
fprintf (stderr,"%s: WARNING: gridsquare not specified in config file\n", getprogname());
}
strupper((char *) config->mycall);
strupper((char *) config->targetcall);
/* If display config flag set just dump the configuration & exit */
if(displayconfig_flag) {
displayversion();
displayconfig(config);
exit(EXIT_SUCCESS);
}
/* Check configuration requirements */
if(!bRequireConfig_pass) {
usage(); /* does not return */
}
/* Be verbose */
if(config->bVerbose) {
displayversion();
displayconfig(config);
}
return(TRUE);
}
void disconnect(void) {
}