File: dntask.c

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (550 lines) | stat: -rw-r--r-- 12,637 bytes parent folder | download | duplicates (2)
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
541
542
543
544
545
546
547
548
549
550
/******************************************************************************
    (c) 1998      P.J. Caulfield          patrick@tykepenguin.cix.co.uk
                  K.   Humborg            kenn@avalon.wombat.ie

    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
    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.
 ******************************************************************************
 */
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include <fcntl.h>
#include <ctype.h>
#include "dn_endian.h"

struct	sockaddr_dn		sockaddr;
static  struct	nodeent		*np;
static  char			node[20];
static  int			sockfd;
static  int                     timeout = 60;

struct	sockaddr_dn		sockaddr;
struct	accessdata_dn		accessdata;
static  struct	nodeent        *binadr;
static  char    		filename[128];
static	unsigned char		buf[32760];
static  int			sockfd;
static  char                   *lasterror;
static  int                     binary_mode;

/* DECnet phase IV limits */
#define MAX_NODE      6
#define MAX_USER     12
#define MAX_PASSWORD 12
#define MAX_ACCOUNT  12
#ifndef FALSE
#define TRUE 1
#define FALSE 0
#endif

static int parse(char *);
static void usage(FILE *);
static char *connerror(int sockfd);
/*-------------------------------------------------------------------------*/

/*
 * Run an interactive command procedure. As we get input from either the
 * remote or local end we pass it on to the other.
 */
void be_interactive(void)
{
    fd_set         in;
    struct timeval tv;
    int            len;

    FD_ZERO(&in);
    FD_SET(sockfd, &in);
    FD_SET(STDIN_FILENO, &in);
    tv.tv_sec  = timeout;
    tv.tv_usec = 0;

    /* Loop for input */
    while ( select(sockfd+1, &in, NULL, NULL, (timeout==0?NULL:&tv)) > 0)
    {
	if (FD_ISSET(sockfd, &in)) // From VMS to us
	{
	    len = read(sockfd, buf, sizeof(buf));
	    if (len < 0)
	    {
		fprintf(stderr, "Read failed: %s\n", connerror(sockfd));
		return;
	    }
	    if (len == 0) /* EOF */
	    {
		return;
	    }

            if (binary_mode)
	    {
                write(STDOUT_FILENO, buf, len);
            }
	    else
	    {
	        buf[len] = '\0';
	        if (buf[len-1] == '\n') buf[len-1] = '\0';
		printf("%s\n", buf);
            }
	}
	if (FD_ISSET(STDIN_FILENO, &in)) // from us to VMS
	{
	    len = read(STDIN_FILENO, buf, sizeof(buf));
	    if (len < 0)
	    {
	        perror("reading from stdin");
		return;
	    }
	    if (len == 0) return; //EOF
	    if (buf[len-1] == '\n') buf[--len] = '\0';
	    write(sockfd, buf, len);
	}

	// Reset for another select
	FD_ZERO(&in);
	FD_SET(sockfd, &in);
	FD_SET(STDIN_FILENO, &in);
	tv.tv_sec  = timeout;
	tv.tv_usec = 0;
    }
    fprintf(stderr, "Time-out expired\n");
}

/*
 * Just dump whatever arrives onto stdout/
 */
void print_output(void)
{
    int len;

    while ( ((len = read(sockfd, buf, sizeof(buf)))) )
    {
        if (len == -1)
	{
	    if (errno != ENOTCONN)
		perror("Error reading from network");
	    else
		fprintf(stderr, "Read failed: %s\n", connerror(sockfd));
	    break;
        }

        if (binary_mode)
	{
            write(STDOUT_FILENO, buf, len);
        }
	else
	{
	    buf[len] = '\0';
	    if (buf[len-1] == '\n') buf[len-1] = '\0';
	    printf("%s\n", buf);
        }
    }
}

/*
 * Open the connection.
 */
int setup_link(void)
{
    if ( (np=getnodebyname(node)) == NULL)
    {
	printf("Unknown node name %s\n",node);
	exit(0);
    }

/* Limit on the length of a DECnet object name */
    if (strlen(filename) > 16)
    {
        printf("task name must be less than 17 characters\n");
        exit(2);
    }

    if ((sockfd=socket(AF_DECnet,SOCK_SEQPACKET,DNPROTO_NSP)) == -1)
    {
	perror("socket");
	exit(-1);
    }

    // If no taskname was given then use a default.
    if (filename[0] == '\0')
    {
	strcpy(filename, "TASK");
    }

    // Provide access control and proxy information
    if (setsockopt(sockfd,DNPROTO_NSP,SO_CONACCESS,&accessdata,
		   sizeof(accessdata)) < 0)
    {
	perror("setsockopt");
	exit(-1);
    }

    /* Open up object number 0 with the name of the task */
    sockaddr.sdn_family   = AF_DECnet;

    /* Old kernel patches need flags to be 1 for a named object.
       The newer kernel patches define this as SDF_WILD so that's
       how we know
     */
    sockaddr.sdn_flags	  = 0x00;
    sockaddr.sdn_objnum	  = 0x00;
    memcpy(sockaddr.sdn_objname, filename, strlen(filename));
    sockaddr.sdn_objnamel = dn_htons(strlen(filename));
    memcpy(sockaddr.sdn_add.a_addr, np->n_addr,2);
    sockaddr.sdn_add.a_len = 2;


    if (connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
    {
	fprintf(stderr, "Connect failed: %s\n", connerror(sockfd));
	exit(-1);
    }
    return TRUE;
}

/*
 * Start here
 */
int main(int argc, char *argv[])
{
    int opt;
    int interactive = 0;

    if (argc < 2)
    {
        usage(stdout);
        exit(1);
    }

    binary_mode = FALSE;

/* Get command-line options */
    opterr = 0;
    optind = 0;
    while ((opt=getopt(argc,argv,"?hVibt:")) != EOF)
    {
	switch(opt)
	{
	case 'h':
	    usage(stdout);
	    exit(1);

	case '?':
	    usage(stderr);
	    exit(1);

	case 'i':
	    interactive = 1;
	    break;

	case 't':
	    timeout = atoi(optarg);
	    break;

	case 'V':
	    printf("\ndntask from dntools version %s\n\n", VERSION);
	    exit(1);
	    break;

        case 'b':
            binary_mode = TRUE;
            break;
	}
    }
    if (!argv[optind])
    {
	usage(stderr);
	exit(1);
    }

    /* Parse the command into its parts */
    if (!parse(argv[optind]))
    {
	fprintf(stderr, "%s\n", lasterror);
	exit(2);
    }

    if (!setup_link()) return 2;

/* Do the real work */
    if (!interactive)
	print_output();
    else
	be_interactive();

    close(sockfd);
    return 0;
}


/*-------------------------------------------------------------------------*/
/* Parse the filename into its component parts */
static int parse(char *fname)
{
    enum  {NODE, USER, PASSWORD, ACCOUNT, NAME, FINISHED} state;
    int   n0=0;
    int   n1=0;
    char *local_user;

    state = NODE; /* Node is mandatory */

    while (state != FINISHED)
    {
	switch (state)
	{
	case NODE:
	    if (fname[n0] != ':' && fname[n0] != '\"' && fname[n0] != '\'')
	    {
		if (n1 >= MAX_NODE ||
		    fname[n0] == ' ' || fname[n0] == '\n')
		{
		    lasterror = "File name parse error";
		    return FALSE;
		}
		node[n1++] = fname[n0++];
	    }
	    else
	    {
		node[n1] = '\0';
		n1 = 0;
		if (fname[n0] == '\"')
		{
		    n0++;
		    state = USER;
		}
		else
		{
		    n0 += 2;
		    state = NAME;
		}
	    }
	    break;

	case USER:
	    if (fname[n0] != ' ' && fname[n0] != '\"' && fname[n0] != '\'')
	    {
		if (n1 >= MAX_USER)
		{
		    lasterror = "File name parse error";
		    return FALSE;
		}
		accessdata.acc_user[n1++] = fname[n0++];
	    }
	    else
	    {
		accessdata.acc_user[n1] = '\0';
		n1 = 0;
		if (fname[n0] == ' ')
		{
		    state = PASSWORD;
		    n0++;
		}
		else /* Must be a quote */
		{
		    state = NAME;
		    /* Check for :: */
		    n0 += 3;
		}
	    }
	    break;

	case PASSWORD:
	    if (fname[n0] != ' ' && fname[n0] != '\"' && fname[n0] != '\'')
	    {
		if (n1 >= MAX_PASSWORD)
		{
		    lasterror = "File name parse error";
		    return FALSE;
		}
		accessdata.acc_pass[n1++] = fname[n0++];
	    }
	    else
	    {
		accessdata.acc_pass[n1] = '\0';
		n1 = 0;
		if (fname[n0] == ' ')
		{
		    n0++;
		    state = ACCOUNT;
		}
		else /* Must be a quote */
		{
		    state = NAME;
		    /* Check for :: */
		    n0 += 3;
		}
	    }
	    break;

	case ACCOUNT:
	    if (fname[n0] != '\'' && fname[n0] != '\"')
	    {
		if (n1 >= MAX_ACCOUNT)
		{
		    lasterror = "File name parse error";
		    return FALSE;
		}
		accessdata.acc_acc[n1++] = fname[n0++];
	    }
	    else
	    {
		accessdata.acc_acc[n1] = '\0';;
		state = NAME;
		n1 = 0;
		/* Check for :: */
		n0 += 3;
	    }
	    break;

	case NAME:
	    strcpy(filename, fname+n0);
	    state = FINISHED;
	    break;

	case FINISHED: // To keep the compiler happy
	    break;
	} /* switch */
    }

/* tail end validation */
    binadr = getnodebyname(node);
    if (!binadr)
    {
	lasterror = "Unknown or invalid node name ";
	return FALSE;
    }

    // Try very hard to get the local username
    local_user = cuserid(NULL);
    if (!local_user || local_user == (char *)0xffffffff)
      local_user = getenv("LOGNAME");
    if (!local_user) local_user = getenv("USER");
    if (local_user)
    {
        int i;

	strcpy((char *)accessdata.acc_acc, local_user);
	accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
        for (i=0; i<accessdata.acc_accl; i++)
            accessdata.acc_acc[i] = toupper(accessdata.acc_acc[i]);
    }
    else
        accessdata.acc_acc[0] = '\0';



    /* If the password is "-" and fd 0 is a tty then
       prompt for a password */
    if (accessdata.acc_pass[0] == '-' && accessdata.acc_pass[1] == '\0' && isatty(0))
    {
	char *password = getpass("Password: ");
	if (password == NULL || strlen(password) > (unsigned int)MAX_PASSWORD)
	{
	    lasterror = "Password input cancelled";
	    return FALSE;
	}
	strcpy(accessdata.acc_pass, password);
    }


    /* Complete the accessdata structure */
    accessdata.acc_userl = strlen(accessdata.acc_user);
    accessdata.acc_passl = strlen(accessdata.acc_pass);
    accessdata.acc_accl  = strlen(accessdata.acc_acc);
    return TRUE;
}

static void usage(FILE *f)
{
    fprintf(f, "\nUSAGE: dntask [OPTIONS] 'node\"user password\"::commandprocedure'\n\n");
    fprintf(f, "NOTE: The VMS filename really should be in single quotes to\n");
    fprintf(f, "      protect it from the shell\n");

    fprintf(f, "\nOptions:\n");
    fprintf(f, "  -i           Interact with the command procedure\n");
    fprintf(f, "  -t           Timeout (in seconds) for interactive command procedure input\n");
    fprintf(f, "  -b           Treat received data as binary data\n");
    fprintf(f, "  -? -h        display this help message\n");
    fprintf(f, "  -V           show version number\n");

    fprintf(f, "\nExamples:\n\n");
    fprintf(f, " dntask 'myvax::'  - defaults to TASK.COM and proxy username\n");
    fprintf(f, " dntask -i 'clustr\"patrick thecats\"::do_dcl.com'\n");
    fprintf(f, "\n");
}

// Return the text of a connection error
static char *connerror(int sockfd)
{
#ifdef DSO_CONDATA
    struct optdata_dn optdata;
    unsigned int len = sizeof(optdata);
    char *msg;

    if (getsockopt(sockfd, DNPROTO_NSP, DSO_DISDATA,
		   &optdata, &len) == -1)
    {
	return strerror(errno);
    }

    // Turn the rejection reason into text
    switch (optdata.opt_status)
    {
    case DNSTAT_REJECTED: msg="Rejected by object";
	break;
    case DNSTAT_RESOURCES: msg="No resources available";
	break;
    case DNSTAT_NODENAME: msg="Unrecognised node name";
	break;
    case DNSTAT_LOCNODESHUT: msg="Local Node is shut down";
	break;
    case DNSTAT_OBJECT: msg="Unrecognised object";
	break;
    case DNSTAT_OBJNAMEFORMAT: msg="Invalid object name format";
	break;
    case DNSTAT_TOOBUSY: msg="Object too busy";
	break;
    case DNSTAT_NODENAMEFORMAT: msg="Invalid node name format";
	break;
    case DNSTAT_REMNODESHUT: msg="Remote Node is shut down";
	break;
    case DNSTAT_ACCCONTROL: msg="Login information invalid at remote node";
	break;
    case DNSTAT_NORESPONSE: msg="No response from object";
	break;
    case DNSTAT_NODEUNREACH: msg="Node Unreachable";
	break;
    case DNSTAT_MANAGEMENT: msg="Abort by management/third party";
	break;
    case DNSTAT_ABORTOBJECT: msg="Remote object aborted the link";
	break;
    case DNSTAT_NODERESOURCES: msg="Node does not have sufficient resources for a new link";
	break;
    case DNSTAT_OBJRESOURCES: msg="Object does not have sufficient resources for a new link";
	break;
    case DNSTAT_BADACCOUNT: msg="The Account field in unacceptable";
	break;
    case DNSTAT_TOOLONG: msg="A field in the access control message was too long";
	break;
    default: msg=strerror(errno);
	break;
    }
    return msg;
#else
    return strerror(errno);

#endif
}