File: command.c

package info (click to toggle)
gopher 2.3-2
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,364 kB
  • ctags: 2,030
  • sloc: ansic: 22,451; perl: 1,950; sh: 1,510; makefile: 397; asm: 1
file content (659 lines) | stat: -rw-r--r-- 15,264 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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/********************************************************************
 * $Author: lindner $
 * $Revision: 3.37 $
 * $Date: 1995/11/03 17:09:36 $
 * $Source: /home/arcwelder/GopherSrc/CVS/gopher+/gopherd/command.c,v $
 * $State: Exp $
 *
 * Paul Lindner, University of Minnesota CIS.
 *
 * Copyright 1991, 1992, 1993 by the Regents of the University of Minnesota
 * see the file "Copyright" in the distribution for conditions of use.
 *********************************************************************
 * MODULE: command.c
 * Routines to parse commands from the client.
 *********************************************************************
 * Revision History:
 * $Log: command.c,v $
 * Revision 3.37  1995/11/03  17:09:36  lindner
 * coen: add '4' and '5' to valid prefixes.
 *
 * Revision 3.36  1995/10/11  19:22:33  lindner
 * moo
 *
 * Revision 3.35  1995/10/11  19:21:24  lindner
 * moo
 *
 * Revision 3.34  1995/10/11  19:10:57  lindner
 * Fix for POST
 *
 * Revision 3.33  1995/10/11  04:58:46  lindner
 * fix for 512byte HTTP post limit.
 *
 * Revision 3.32  1995/09/25  05:02:30  lindner
 * Convert to ANSI C
 *
 * Revision 3.31  1995/08/30  19:27:23  lindner
 * Remove some tests..
 *
 * Revision 3.30  1995/08/29  07:10:12  lindner
 * Many changes to support HTTP
 *
 * Revision 3.29  1995/07/06  20:12:17  lindner
 * More mods for HTTP serving
 *
 * Revision 3.28  1995/06/09  04:48:16  lindner
 * Start incorporating Don Gilbert's POST code
 *
 * Revision 3.27  1995/05/02  04:04:24  lindner
 * Fix problem with NO_AUTH
 *
 * Revision 3.26  1995/05/01  04:00:46  lindner
 * Fixes for mindexd, etc from Steve Hsieh
 *
 * Revision 3.25  1995/04/15  07:12:35  lindner
 * add HTML processing
 *
 * Revision 3.24  1995/01/04  17:42:59  lindner
 * small renaming
 *
 * Revision 3.23  1994/12/10  08:24:56  lindner
 * Start building HTTP parser
 *
 * Revision 3.22  1994/12/03  02:14:44  lindner
 * Fix predone searches
 *
 * Revision 3.21  1994/12/02  19:51:14  lindner
 * Weird p removed and fix for prespecified searches
 *
 * Revision 3.20  1994/11/24  08:07:54  lindner
 * Fix for command routines with authentication
 *
 * Revision 3.19  1994/10/19  03:31:59  lindner
 * Another NO_AUTH code hack...
 *
 * Revision 3.18  1994/07/31  04:38:20  lindner
 * Add optional pre-specified search string..
 *
 * Revision 3.17  1994/06/29  05:29:19  lindner
 * Remove Admit1, put in cool stuff
 *
 * Revision 3.16  1994/04/25  20:49:03  lindner
 * Fix for debug code
 *
 * Revision 3.15  1994/04/08  21:08:34  lindner
 * Gcc -Wall fix
 *
 * Revision 3.14  1994/03/30  21:36:34  lindner
 * Fix for binary ask data from Don Gilbert
 *
 * Revision 3.13  1994/03/15  17:57:41  lindner
 * Fix for SCO compiler
 *
 * Revision 3.12  1994/03/08  15:55:25  lindner
 * gcc -Wall fixes
 *
 * Revision 3.11  1994/03/04  23:26:04  lindner
 * Fix for changes in strstring.h
 *
 * Revision 3.10  1994/01/20  06:37:03  lindner
 * Addition for 'h' prefix for selector string
 *
 * Revision 3.9  1993/11/02  05:53:40  lindner
 * more docs
 *
 * Revision 3.8  1993/10/04  06:49:20  lindner
 * ASK data stored in core, for a while..
 *
 * Revision 3.7  1993/09/30  16:56:53  lindner
 * Fix for WAIS and $ requests
 *
 * Revision 3.6  1993/07/29  20:13:32  lindner
 * Removed dead variables
 *
 * Revision 3.5  1993/07/27  05:27:40  lindner
 * Mondo Debug overhaul from Mitra
 *
 * Revision 3.4  1993/07/23  03:10:53  lindner
 * Added CMDgetFile() fcn
 *
 * Revision 3.3  1993/04/09  16:50:24  lindner
 * nothing
 *
 * Revision 3.2  1993/03/24  20:19:40  lindner
 * Fixed memory leak
 *
 * Revision 3.1  1993/03/19  19:56:53  lindner
 * New CMD object
 *
 *
 *********************************************************************/



#include "Malloc.h"
#include "String.h"
#include <stdio.h>
#include "util.h"

#include "command.h"
#include "openers.h"
#include "Debug.h"

static char *CMDticketfromLine(CMDobj *cmd, char *input);
static void  CMDpushAskline(CMDobj *cmd, char *line);
static void  CMDgetXtra(CMDobj *cmd, int fd, int extradata);



CMDobj *
CMDnew()
{
     CMDobj *temp;

     temp = (CMDobj *) malloc(sizeof(CMDobj));

     temp->selstr  = temp->command = temp->search = NULL;

     temp->datafromnet   = STRnew();
     temp->asklines      = STAnew(5);

     temp->view          = STRnew();
     temp->secureuser    = STRnew();
     temp->ticket        = STRnew();
     temp->httpcmd       = UnknownHTTPcmd;

     CMDsetGplus(temp, FALSE);
     CMDsetProtocol(temp, UnknownProtocol);
     return(temp);
}


void
CMDdestroy(CMDobj *cmd)
{
     STRdestroy(cmd->datafromnet);
     STAdestroy(cmd->asklines);
     STRdestroy(cmd->view);
     STRdestroy(cmd->ticket);
     STRdestroy(cmd->secureuser);

     free(cmd);
}

static char*
CMDparseHTTP(CMDobj *cmd, char *cp)
{
     int len;
     if (cp == NULL)
	  return(NULL);

     len = strlen(cp);


     if ((len >8) && (strcmp(cp + len - 9, " HTTP/1.0")==0)) {
	  *(cp + len - 9) = '\0';
	  len = strlen(cp);
	  CMDsetProtocol(cmd, HTTP1_0);
     }


     if ((len >= 5)) {
	  if (strncmp(cp, "GET /",5) == 0) {
	  
	       cp += 5;
	       if (len == 5) {
		    /** Top level HTTP transaction **/
		    cp = cp -2;
		    *cp = 'h';
	       }
	       CMDsetHTTPcmd(cmd, GET);
	       if (CMDgetProtocol(cmd) == UnknownProtocol)
		    CMDsetProtocol(cmd, HTTP0_9);
	       Fromhexstr(cp, cp);

	  } else if (strncmp(cp, "POST /", 5) == 0) {
	       cp += 5;
	       *cp = 'h';
	       CMDsetHTTPcmd(cmd, POST);
	       if (CMDgetProtocol(cmd) == UnknownProtocol)
		    CMDsetProtocol(cmd, HTTP0_9);
	       Fromhexstr(cp, cp);
	  }
     }

     return(cp);
}


void
CMDfromNet(CMDobj *cmd, int sockfd)
{
     char *cp, *selstr;
     char inputline[512];
     char *field1=NULL, *field2=NULL, *field3 = NULL;
     char *extradata = NULL;
     int length;
     
     length = readline(sockfd, inputline, sizeof(inputline));

     /** Set the alarm signal for about an hour, just in case.. **/
     (void) alarm(60 * 60);


     if (length <= 0) {
	  close(sockfd);
	  LOGGopher("getcommand: readline error");
	  exit(-1);
     }

     ZapCRLF(inputline);

     Debug("Received: %s\n", inputline);
     if (strncmp("POST ",inputline,5) == 0) {
	  extradata = "4"; 
	  Debug("POST found\n",0);
     }

     CMDsetData(cmd, inputline);

     cp = CMDgetData(cmd);
     cp = CMDparseHTTP(cmd, cp);
     cp = CMDticketfromLine(cmd, cp);

     CMDsetSelstr(cmd, cp);
     selstr = cp;

     /** Find the first field, if it exists... **/
     cp = strchr(selstr, '\t');
     if (cp != NULL) {
	  *cp = '\0';
	  cp++;
	  
	  field1 = cp;
	  
	  /** find the second field, if it exists **/
	  cp = strchr(cp, '\t');
	  if (cp != NULL) {
	       *cp = '\0';
	       cp++;
	       field2 = cp;
	  } else {
	       /** find the third field, if it exists **/
	       if (cp != NULL) 
		    cp = strchr(cp, '\t');
	       if (cp != NULL) {
		    *cp = '\0';
		    cp++;
		    field3 = cp;
	       }
	  }

     }
     /** Okay, now decide which field is the search and 
       which is the command */
     
     if (*selstr == '7' || strncmp(selstr, "waissrc:",8)==0 ||
	 (field2 && strncmp(inputline, "exec+:", 6) == 0 && *field2 != '1') ||
	 strncmp(selstr, "mindex:",7) ==0 ||
	 strncmp(selstr, "halidate", 8)==0) {

	  char *questionmark;
	  /** If it's a type 7 search, search for a ? mark in the
    	   ** selector string.  (Yeah, yeah, it's weblike)
	   **/

	  questionmark = strrchr(selstr, '?');
	  if (questionmark != NULL) {
	       *questionmark = '\0';
	       CMDsetSearch(cmd, questionmark+1);
	       CMDsetCommand(cmd, field1);
	  } else {
	       CMDsetSearch(cmd, field1);
	       CMDsetCommand(cmd, field2);
	  }
     } else {
	  CMDsetCommand(cmd, field1);
	  CMDsetSearch(cmd, NULL);
     }

     /** Get the extra data (from an ask block etc.) if we've got
          an extra data flag... **/

     if (field3 != NULL)
	  extradata = field3;
     else if	 (CMDgetSearch(cmd) == NULL && field2 != NULL)
	  extradata = field2;


     if (extradata == NULL) {
	  int extraflags = 0;
	  if (CMDgetProtocol(cmd) == HTTP1_0)
	       extraflags |= 8;
	  if (CMDgetHTTPcmd(cmd) == POST)
	       extraflags |= 4;
	  CMDgetXtra(cmd, sockfd, extraflags);
     }
     else {
	  CMDgetXtra(cmd, sockfd, atoi(extradata));
     }

     /** Test to see if we've got Gplus behaviour */

     if (CMDgetCommand(cmd) != NULL && *CMDgetCommand(cmd) != '\0')
	  CMDsetGplus(cmd, TRUE);

     Debugmsg("Command:: ");
     Debug("selstr %s, ", CMDgetSelstr(cmd));
     Debug("command %s, ", CMDgetCommand(cmd));
     Debug("search %s, ", CMDgetSearch(cmd));
     Debug("user %s, ", CMDgetUser(cmd));
     Debug("ticket %s, ", CMDgetTicket(cmd));
}


static void
CMDpushAskline(CMDobj *cmd, char *line)
{
     String *temp = STRnew();

     STRset(temp, line);
     STApush(cmd->asklines, temp);

     STRdestroy(temp);
}

/* dgg need for binary ask data */
static void
CMDpushAskBinary(CMDobj *cmd, char *buf, long buflen)
{
     String *temp = STRnew();
     temp->data= buf;
     temp->len= buflen;
     STApush(cmd->asklines, temp);
     temp->data= NULL; /* caller owns buf */
     STRdestroy(temp);
}

#ifndef NO_AUTHENTICATION
static char index_64[128] = {
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
    -1, 0, 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,-1, -1,-1,-1,-1,
    -1,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,-1, -1,-1,-1,-1
};

#define char64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])

static void
CMDparseBase64userpass(CMDobj *cmd, char *str)
{
     int c1, c2, c3, c4;
     int DataDone = 0;
     char *donebuf = NULL, *donecp, *cp;

     donebuf = (char*) malloc(strlen(str)*sizeof(char) + 1 );
     donecp = donebuf;

     /* always reinitialize */

     while ((c1 = *(str++)) != '\0') {
	  if (isspace(c1)) {
	       continue;
	  }
	  if (DataDone) continue;
	  do {
	       c2 = *(str++);
	       if (c2 == 0) return;
	  } while (isspace(c2));
	  do {
	       c3 = *(str++);
	       if (c3 == 0) return;
	  } while (isspace(c3));
	  do {
	       c4 = *(str++);
	       if (c4 == 0) return;
	  } while (isspace(c4));
	  if (c1 == '=' || c2 == '=') {
	       DataDone=1;
	       continue;
	  }
	  c1 = char64(c1);
	  c2 = char64(c2);
	  *donecp++ = ((c1<<2) | ((c2&0x30)>>4));
	  
	  if (c3 == '=') {
	       DataDone = 1;
	  } else {
	       c3 = char64(c3);
	       *donecp++ = (((c2&0XF) << 4) | ((c3&0x3C) >> 2));
	       if (c4 == '=') {
		    DataDone = 1;
	       } else {
		    c4 = char64(c4);
		    *donecp++ = (((c3&0x03) <<6) | c4);
	       }
	  }
     }
     /** donebuf should look like this now:  username:pass **/

     cp = strchr(donebuf, ':');
     if (cp == NULL)
	  return;

     *cp++ = '\0';
     CMDsetUser(cmd, donebuf);
/*     CMDsetPW(cmd, cp);*/
       
       
}

#endif

/*
 * Retrieve extra data from the client request..  This stuff is optional
 *
 */

static void
CMDgetXtra(CMDobj *cmd, int fd, int extradata)
{
     char inputline[512];

     /** Siphon off data if it's there.. **/

     /** A ticket? **/
     if ((extradata & 0x2) == 0x2) {
	  ;
     }

     /** An ask block **/
     if ((extradata & 0x1) == 0x1) {

	  /** Okay, the next line is either +-1, or +bytes .. **/
	  readline(fd, inputline, sizeof(inputline));
	  ZapCRLF(inputline);
	  if (strcmp(inputline, "+-1")==0) {
	       while (readline(fd, inputline, sizeof(inputline))>0)  {
		    ZapCRLF(inputline);
		    ZapCRLF(inputline);
		    if (*inputline == '.' && *(inputline+1) == '\0')
			 break;

		    CMDpushAskline(cmd, inputline);
	       }
	  }
	  /* dgg: patch for askfile +bytecount or +-2 input forms */
	  /* dgg++ -- need this to read binary file from client */
	  else { 
	       /*inputline == "+-2" or  "+bytes..." */
	       long count, nbytes;

	       nbytes= atol(inputline+1);
	       if (nbytes == -2) do { /* read til close */
		    count= readrecvbuf(fd,inputline,sizeof(inputline));
		    if (count>0) CMDpushAskBinary(cmd, inputline, count);
	       } while (count>0);
	       else while (nbytes>0) { /* read til nbytes found */
		    count= sizeof(inputline);
		    if (count>nbytes) count= nbytes;
		    count= readrecvbuf(fd,inputline,count);
		    if (count>0) CMDpushAskBinary(cmd, inputline, count);
		    nbytes -= count;
	       }
          }
     } else if ((extradata & (4|8))) {
	  /*** HTTP headers and POST lines ***/
	  boolean emptyLastLine= 0;
	  int postsize = 0;

	  Debug("HTTP and/or POST data\n",0);

	  while (readline(fd, inputline, sizeof(inputline))>0)  {
	       ZapCRLF(inputline);
	       ZapCRLF(inputline);
	       Debug("POSTed line: %s\n", inputline);
#ifndef NO_AUTHENTICATION	       
	       if (strncasecmp(inputline, "Authorization: Basic ",21) == 0) {
		    CMDparseBase64userpass(cmd, inputline+21);
	       } else if (strncasecmp(inputline, "Content-Length: ", 16) == 0) {
		    postsize = atol(inputline + 16);
	       }
#endif
	       CMDpushAskline(cmd, inputline);
	       

	       if (*inputline == '\0')  {
		    /*** end of HTTP headers ***/

		    if (postsize > 0) {
			 char *postdata;

			 postdata = (char*) malloc(sizeof(char) * (postsize + 1));

			 if (postdata != NULL) {
			      readrecvbuf(fd, postdata, postsize);
			 }
			 CMDpushAskline(cmd, postdata);
		    } 
		    break;
	       }
	  }
 	  Debug("POST data done.\n",0);
     }
 
}



static char *
CMDticketfromLine(CMDobj *cmd, char *input)
{
#ifndef NO_AUTHENTICATION
     char *cp;
     char *originput = input;
     
     if (*input != '*')
	  return(originput);

     input++;
     cp = strchr(input, ' ');
     if (cp == NULL)
	  return(originput);

     *cp = '\0';
     
     CMDsetUser(cmd,input);

     input = cp+1;
     cp = strchr(input, ' ');
     if (cp == NULL)
	  return(originput);

     *cp = '\0';
     CMDsetTicket(cmd, input);

     return(cp+1);
#else
     return(input);
#endif
}

/** Find an associated file from the selector string... Yuck! **/

char *
CMDgetFile(CMDobj *cmd)
{
     char *selstr = CMDgetSelstr(cmd);
     char *cp = NULL;


#ifndef NO_AUTHENTICATION
     if (strncmp(selstr, "validate ", 9) == 0)
	  selstr += 9;

#endif

     switch (*selstr) {
     case '0':
     case '1':
     case '4':
     case '5':
     case '7':
     case '9':
     case 'h':
     case 's':
     case 'I':
	  return(selstr+1);

     case 'm':
#ifndef NO_MINDEXD
	  if (strncmp(selstr,"mindex:",7)==0)
	       return(selstr+7);
	  else
#endif
	       return(selstr+1);

     case 'R':
	  cp = strchr(selstr, '-');
	  if (cp == NULL)  break;

	  cp++;
	  cp = strchr(cp, '-');
	  if (cp == NULL) break;
	  
	  return(cp+1);


	  /*** Exec: ***/
     case 'e':
	  cp = strrchr(selstr, ':');
	  if (cp!=NULL)
               return(cp+1);
	  else
               return(NULL);
	  break;


	  /** WAIS docid **/
#ifdef WAISSEARCH
     case 'w':
	  if (strncmp(selstr, "waissrc:",8)==0)
	       return(strchr(selstr, ':')+1);
#endif

     default:
	  if (*selstr == '\0') {
	       return("//");
	  } else {
	       return(NULL);
	  }
     }

     return(NULL);
     ;
}