-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmgod.c
1208 lines (1016 loc) · 23.5 KB
/
mgod.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mgod: mini gopher server for inetd
* Copyright (C) 2006-2008 Máté Nagy <[email protected]>
* Available under: GPL (version 3), see file "COPYING"
* -> gopher://port70.net/1mgod
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <time.h>
#include <stdarg.h>
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* size of maximum request string and config file line */
#define REQBUF 512
/* buffer size for file serving */
#define SERVBUF 2048
/* name of directory listing config file */
char DIRLIST[40] = ".gopher";
/* name of inheriting config file */
char INFCONFIG[40] = ".gopher.rec";
/* dirlist file extension */
#define DIRLISTEXT "g"
/* default server settings */
char * servername = "127.0.0.1";
int serverport = 70;
char * rootdir = "/var/gopher";
char * logfile = NULL;
/* file types */
char *filetypes[] = {
/* images */
"I", ".jpg.jpeg.jpe.png.bmp.",
"g", ".gif.",
/* binary formats */
"9", ".gz.tgz.tar.zip.bz2.rar.pdf.mid.xm.mod.",
";", ".avi.mp4.mpg.",
"s", ".wav.mp3.au.ogg.",
/* html */
"h", ".htm.html.swf.",
/* directory list */
"1", "." DIRLISTEXT ".",
NULL
};
/********************************** newhash */
typedef unsigned long int u4; /* unsigned 4-byte type */
typedef unsigned char u1; /* unsigned 1-byte type */
/* The mixing step */
#define mix(a,b,c) \
{ \
a=a-b; a=a-c; a=a^(c>>13); \
b=b-c; b=b-a; b=b^(a<<8); \
c=c-a; c=c-b; c=c^(b>>13); \
a=a-b; a=a-c; a=a^(c>>12); \
b=b-c; b=b-a; b=b^(a<<16); \
c=c-a; c=c-b; c=c^(b>>5); \
a=a-b; a=a-c; a=a^(c>>3); \
b=b-c; b=b-a; b=b^(a<<10); \
c=c-a; c=c-b; c=c^(b>>15); \
}
/* The whole new hash function */
u4 hash(k_str, length, initval)
const char *k_str; /* the key */
u4 length; /* the length of the key in bytes */
u4 initval; /* the previous hash, or an arbitrary value */
{
register const u1 *k = (const u1 *)k_str;
register u4 a,b,c; /* the internal state */
u4 len; /* how many key bytes still need mixing */
/* Set up the internal state */
len = length;
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
c = initval; /* variable initialization of internal state */
/*---------------------------------------- handle most of the key */
while (len >= 12)
{
a=a+(k[0]+((u4)k[1]<<8)+((u4)k[2]<<16) +((u4)k[3]<<24));
b=b+(k[4]+((u4)k[5]<<8)+((u4)k[6]<<16) +((u4)k[7]<<24));
c=c+(k[8]+((u4)k[9]<<8)+((u4)k[10]<<16)+((u4)k[11]<<24));
mix(a,b,c);
k = k+12; len = len-12;
}
/*------------------------------------- handle the last 11 bytes */
c = c+length;
switch(len) /* all the case statements fall through */
{
case 11: c=c+((u4)k[10]<<24);
case 10: c=c+((u4)k[9]<<16);
case 9 : c=c+((u4)k[8]<<8);
/* the first byte of c is reserved for the length */
case 8 : b=b+((u4)k[7]<<24);
case 7 : b=b+((u4)k[6]<<16);
case 6 : b=b+((u4)k[5]<<8);
case 5 : b=b+k[4];
case 4 : a=a+((u4)k[3]<<24);
case 3 : a=a+((u4)k[2]<<16);
case 2 : a=a+((u4)k[1]<<8);
case 1 : a=a+k[0];
/* case 0: nothing left to add */
}
mix(a,b,c);
/*-------------------------------------------- report the result */
return c;
}
/***********************************************************************/
/* maximum number of directory list entries to print */
int limit = -1;
/* output a single line, then \r\n EOL */
void outln(const char *str)
{
fputs(str, stdout);
fputs("\r\n", stdout);
}
/* print error message */
void errormsg(const char *e)
{
printf("3%s\tfake\t(NULL)\t0\r\n.\r\n", e);
}
/* print info line */
void infoline(char *str)
{
putchar('i');
fputs(str, stdout);
outln("\tfake\t(NULL)\t0");
}
/* append to log */
void logprintf(const char *format, ...)
{
FILE *fp;
va_list ap;
if(!logfile) return;
fp = fopen(logfile, "a");
if(!fp) {
perror("can't open log file");
return;
}
va_start(ap, format);
vfprintf(fp, format, ap);
va_end(ap);
fclose(fp);
}
/********************************** path */
/* structure for keeping current path */
typedef struct node node;
struct node {
char *text;
node *next;
};
/* current path */
node *path = NULL;
/* make a path node */
node *mkpn(const char *tx) {
node *n = (node *) malloc(sizeof(node));
if(!n) exit(1);
n->text = strdup(tx);
n->next = NULL;
return n;
}
/********************************** directory processors */
/* structure for keeping directory processors */
typedef struct dirproc dirproc;
struct dirproc {
char *name;
char *cmd;
int raw;
dirproc *next;
};
/* directory processors */
dirproc *dirprocs = NULL;
/* record a directory processor */
void adddirproc(char *name, char *cmd)
{
dirproc *n = (dirproc *) malloc(sizeof(dirproc));
n->name = strdup(name);
n->cmd = strdup(cmd);
n->next = dirprocs;
n->raw = 0;
dirprocs = n;
}
/* look up dirproc command by name */
dirproc *getdirproc(char *name)
{
dirproc *n;
for(n = dirprocs; n; n=n->next) {
if(!strcmp(n->name, name)) return n;
}
return NULL;
}
/* empty the list of dirprocs */
void cleardirprocs()
{
dirproc *n, *m = NULL;
for(n = dirprocs; n; n = n->next) {
free(m);
free(n->name);
free(n->cmd);
m = n;
}
free(m);
dirprocs = NULL;
}
/********************************** file name aliases */
/* structure for keeping file name aliases */
typedef struct alias alias;
struct alias {
u4 nhash;
char *name;
char *value;
struct alias *left;
struct alias *right;
};
alias *aliases = NULL;
/* create name alias node */
alias *mkalias(u4 nhash, const char *name, const char *value) {
alias *n = (alias *) malloc(sizeof(alias));
if(!n) exit(1);
n->nhash = nhash;
n->name = strdup(name);
n->value = strdup(value);
n->left = NULL;
n->right = NULL;
return n;
}
/* register name alias */
void regalias(char *name, const char *value) {
alias *n;
u4 nhash;
if(!aliases) {
aliases = mkalias(hash(name, strlen(name), 0), name, value);
return;
}
n = aliases;
nhash = hash(name, strlen(name), 0);
for(;;) {
if(n->nhash == nhash) {
if(strcmp(n->name, name)) {
if(n->left) {
n = n->left;
continue;
}
n->left = mkalias(nhash, name, value);
return;
} else {
n->value = strdup(value);
return;
}
} else if(nhash < n->nhash) {
if(n->left) {
n = n->left;
continue;
}
n->left = mkalias(nhash, name, value);
return;
} else {
if(n->right) {
n = n->right;
continue;
}
n->right = mkalias(nhash, name, value);
return;
}
}
}
/* retrieve alias by name */
char * getalias(const char *name) {
alias *n;
u4 nhash;
nhash = hash(name, strlen(name), 0);
n = aliases;
if(!n) return NULL;
for(;;) {
if(n->nhash == nhash) {
if(strcmp(n->name, name)) {
if(n->left) {
n = n->left;
continue;
}
return NULL;
} else return n->value;
} else if(nhash < n->nhash) {
if(n->left) {
n = n->left;
continue;
}
return NULL;
} else {
if(n->right) {
n = n->right;
continue;
}
return NULL;
}
}
}
/***********************************************************************/
/* helper functions for directory listing */
int dirfilter(const struct dirent *a)
{
if(a->d_name[0] == '.') return 0;
return 1;
}
/* file list generation properties */
int dirsortmode = 0;
int genlist = 1;
int listrev = 0;
int blogmode = 0;
int dirfirst = 1;
int textsummary = 0;
/* callback function for directory sorting */
int dirsort(const struct dirent **a, const struct dirent **b)
{
struct stat abuf, bbuf;
if(stat((*a)->d_name, &abuf)) return 0;
if(stat((*b)->d_name, &bbuf)) return 0;
if(dirfirst) {
if(S_ISDIR(abuf.st_mode) && !S_ISDIR(bbuf.st_mode)) return -1;
if(!S_ISDIR(abuf.st_mode) && S_ISDIR(bbuf.st_mode)) return 1;
}
switch(dirsortmode) {
case 1:
if(abuf.st_mtime < bbuf.st_mtime) return -1;
if(abuf.st_mtime > bbuf.st_mtime) return 1;
return strverscmp((*a)->d_name, (*b)->d_name);
default:
return strverscmp((*a)->d_name, (*b)->d_name);
}
}
/* print directory entry */
void printentry(char *e)
{
struct stat stbuf;
char *ext = NULL;
node *no;
char menuchar;
char *desc;
struct tm *mt;
char *fil = e; // file system name
int absolute = 0;
if(e[0] == '/') {
/* when given an absolute path, prepend server root... */
fil = (char *) alloca(strlen(rootdir) + strlen(e));
sprintf(fil, "%s%s", rootdir, e);
absolute = 1;
e ++;
}
if(stat(fil, &stbuf)) {
errormsg("can't stat file");
return;
}
if(!(stbuf.st_mode & S_IROTH)) {
/* not readable by all; skip */
return;
}
/* determine menu char */
if(S_ISDIR(stbuf.st_mode)) {
menuchar = '1';
} else if(S_ISREG(stbuf.st_mode)) {
ext = strrchr(e, '.');
if(ext) {
char **ft = filetypes;
char extwdot[9];
if(strlen(ext) > 7) {
menuchar = '0';
} else {
strcpy(extwdot, ext);
strcat(extwdot, ".");
for(; *ft; ft ++) {
char mch = *ft[0]; ft++;
if(strcasestr(*ft, extwdot)) {
menuchar = mch;
break;
}
}
if(!(*ft)) menuchar = '0';
}
ext ++; // skip dot
} else {
menuchar = '0';
}
} else {
return;
}
/* whether we're cutting off the .g ... */
int dotg = 0;
/* determine description */
desc = getalias(e);
if(desc) {
if(desc[0] == 0) return; /* desc aliased to empty; skip */
} else {
desc = e;
/* cut off .g from extension */
if(menuchar == '1' && ext && !strcasecmp(ext, DIRLISTEXT)) {
dotg = 1;
ext -= strlen(DIRLISTEXT);
*ext = 0;
}
}
putchar(menuchar);
mt = localtime(&stbuf.st_mtime);
if(blogmode) {
printf("%04d-%02d-%02d ", mt->tm_year + 1900, mt->tm_mon + 1, mt->tm_mday);
}
printf("%s\t", desc);
if(dotg) {
/* replace dot in the .g */
*ext = '.';
}
if(!absolute) {
/* print path */
for(no = path; no; no=no->next)
printf("%s/", no->text);
}
printf("%s\t%s\t%d\r\n", e, servername, serverport);
if(textsummary > 0 && menuchar == '0') {
/* output text summary for plain text file */
char buf[REQBUF];
FILE *fp = fopen(fil, "r");
if(fp) {
int i = 0;
int spccnt = 0;
while(!feof(fp) && i < textsummary) {
int c = fgetc(fp);
if(isspace(c)) {
c = ' ';
spccnt ++;
if(spccnt > 2) continue;
} else spccnt = 0;
if(isprint(c))
buf[i++] = c;
}
buf[i] = 0;
fclose(fp);
if(i > 0) infoline(buf);
}
}
}
/* special command token separator */
#define SPECSEP " \t"
void readdirlist(FILE *fp);
void runextern(char *ext);
/* interpret bang command */
void speccmd(char *cmd)
{
char *spec = strtok(cmd, SPECSEP);
if(!spec) {
errormsg("Empty ! command in config");
return;
}
if(!strcmp(spec, "nolist")) {
/* don't generate file list */
genlist = 0;
} else if(!strcmp(spec, "reverse")) {
/* reverse sort */
listrev = 1;
} else if(!strcmp(spec, "mtime")) {
/* sort by mtime */
dirsortmode = 1;
} else if(!strcmp(spec, "blog")) {
/* include mtime before file name in menu */
blogmode = 1;
} else if(!strcmp(spec, "dirmixed")) {
/* consider dirs as files while sorting */
dirfirst = 0;
} else if(!strcmp(spec, "summary")) {
/* enable text summary */
textsummary = 72;
char *sumlen = strtok(NULL, SPECSEP);
if(sumlen) textsummary = atoi(sumlen);
if(textsummary > REQBUF-1) textsummary = REQBUF-1;
if(textsummary < 0) textsummary = 0;
} else if(!strcmp(spec, "limit")) {
/* limit number of directory list entries */
char *slimit = strtok(NULL, SPECSEP);
if(slimit) limit = atoi(slimit); else limit = 20;
} else if(!strcmp(spec, "include")) {
/* include .gopher file */
char *file = strtok(NULL, "");
if(!file) {
errormsg("!include without arg in config");
return;
}
FILE *fp = fopen(file, "r");
if(!fp) {
errormsg("can't open !include file");
return;
}
readdirlist(fp);
fclose(fp);
} else if(!strcmp(spec, "proc")) {
/* include output from external processor */
char *ext = strtok(NULL, "");
if(!ext) {
errormsg("!proc without arg in config");
return;
}
runextern(ext);
} else if(!strcmp(spec, "dirproc") || !strcmp(spec, "dirproc-raw")) {
/* register directory processor */
char *name = strtok(NULL, SPECSEP);
if(!name) {
errormsg("!dirproc without name arg");
return;
}
char *cmd = strtok(NULL, "");
if(!cmd) {
errormsg("!dirproc without cmd arg");
return;
}
adddirproc(name, cmd);
if(!strcmp(spec, "dirproc-raw"))
dirprocs->raw = 1;
} else {
errormsg("Invalid !command in config");
}
}
/* print full version of link relative to current path */
void printrellink(const char *link)
{
node *no;
int sp = 0;
/* allocate enough space for full path + link string */
for(no = path; no; no=no->next)
sp += strlen(no->text) + 1;
sp += strlen(link) + 1;
char *buf = (char *) alloca(sp);
buf[0] = 0;
char *end = buf; // keep a pointer to the end of the buffer
/* append to the end of buffer */
inline void pathadd(const char *x, const char *until)
{
const char *c;
for(c = x; c != until && *c; c++)
*end++ = *c;
*end = 0;
}
/* append to the end of buffer
* add '/' if necessary */
inline void pathaddel(const char *x, const char *until)
{
if(end > buf) pathadd("/", NULL);
pathadd(x, until);
}
/* remove until last / */
inline void pathup()
{
char *c;
for(c = end; c > buf && *c != '/'; c --);
end = c;
*end = 0;
}
/* copy current path to buffer */
for(no = path; no; no=no->next) {
if(no != path) pathadd("/", NULL);
pathadd(no->text, NULL);
}
/* build path element by element */
const char *p = link;
const char *nd;
for(p = link; *p; p = nd + 1) {
nd = strchr(p, '/');
if(nd) {
if(!strncmp(".", p, nd - p)) continue;
else if(!strncmp("..", p, nd - p)) pathup();
else pathaddel(p, nd);
} else {
if(!strcmp(".", p)) continue;
else if(!strcmp("..", p)) pathup();
else pathaddel(p, NULL);
break;
}
}
/* print result */
fputs(buf, stdout);
}
/* process configuration file */
void readdirlist(FILE *fp)
{
char buf[REQBUF];
char *p;
while(!feof(fp)) {
if(!fgets(buf, REQBUF, fp)) break;
if(ferror(fp)) break;
if(ferror(stdout)) break;
for(p=buf; *p; p++)
if(*p == '\r' || *p == '\n')
{ *p = 0; break; }
switch(buf[0]) {
/* verbatim */
case ':':
outln(buf+1);
break;
/* verbatim local link */
case '.':
fputs(buf+1, stdout);
printf("\t%s\t%d\r\n", servername, serverport);
break;
/* local link, with path automatically prepended */
case '`':
p = strchr(buf+1, '\t');
if(p) {
*p = 0;
printf("%s\t", buf+1);
printrellink(p+1);
printf("\t%s\t%d\r\n", servername, serverport);
}
break;
/* special command */
case '!':
speccmd(buf + 1);
break;
/* comment */
case '#':
break;
/* description alias */
case '=':
p = strchr(buf+1, '\t');
if(*p) {
*p = 0;
regalias(p+1, buf+1);
}
break;
case '"':
/* verbatim info line */
infoline(buf + 1);
break;
/* info line */
default:
/* no special beginning characters.
* Let's try to find tabs */
{
char *info = buf;
char *sel = strchr(buf, '\t');
char *serv = NULL;
char *port = NULL;
if(!sel) {
/* just an infoline after all */
infoline(buf);
} else {
/* has tabs; try to interpret somewhat similarly
* to bucktooth (fill in missing fields) */
*sel++ = 0;
serv = strchr(sel, '\t');
if(serv) {
*serv++ = 0;
port = strchr(serv, '\t');
if(port) *port++ = 0;
}
if(!info[0]) {
/* missing info field */
if(!serv) {
/* if no serv specified, just do a printentry */
if(!strncmp(sel, "URL:", 4)) {
/* .. unless it begins with URL: */
printf("h%s\t%s\t%s\t%d\r\n", sel+4, sel, servername, serverport);
} else printentry(sel);
} else {
errormsg("known selector, but no info field on nonlocal serv");
}
} else {
/* info is specified. */
printf("%s\t", info);
if(!serv && sel[0] != '/' && strncmp(sel, "URL:", 4)) {
/* selector is relative and serv is local, print path */
printrellink(sel);
} else {
/* print selector verbatim */
if(sel[0] == '/') sel ++;
fputs(sel, stdout);
}
printf("\t%s\t%d\r\n", serv ? serv : servername,
port ? atoi(port) : (serv ? 70 : serverport));
}
}
}
}
}
}
/* directory listing */
void dirlist()
{
FILE *fp;
int n, i;
struct dirent **namelist;
/* process configuration file */
fp = fopen(DIRLIST, "r");
if(fp) {
readdirlist(fp);
fclose(fp);
}
/* generate directory listing */
if(genlist) {
n = scandir(".", &namelist, dirfilter, (int (*) (const struct dirent **, const struct dirent **) ) dirsort);
if(n < 0) {
perror("scandir");
exit(1);
}
if(listrev) {
while(n--) {
if(limit > -1) {
if(limit == 0) break;
limit --;
}
printentry(namelist[n]->d_name);
}
} else {
if(limit > -1) n = limit;
for(i=0; i<n; i++)
printentry(namelist[i]->d_name);
}
}
}
/* serve a file */
void serve(char *fn)
{
FILE *fp;
int r;
char buf[SERVBUF];
struct stat stbuf;
if(stat(fn, &stbuf)) {
errormsg("can't stat file");
exit(0);
}
if(!(stbuf.st_mode & S_IROTH)) {
/* not everyone has read access */
errormsg("access denied");
exit(0);
}
fp = fopen(fn, "rb");
if(!fp) {
errormsg("couldn't open file");
exit(0);
}
while((r = fread(buf, 1, SERVBUF, fp)) > 0) {
fwrite(buf, r, 1, stdout);
}
fclose(fp);
}
/* try to run/include an external processor */
void runextern(char *cmd)
{
FILE *fp = popen(cmd, "r");
if(!fp) {
errormsg("popen failed for runextern");
exit(0);
}
readdirlist(fp);
pclose(fp);
}
/* try to run/include a directory processor */
void rundirproc(dirproc *d, char *path)
{
setenv("QUERY", path, 1);
if(d->raw) {
char *args[4] = { "sh", "-c", d->cmd, NULL };
execv("/bin/sh", args);
errormsg("execv failed");
exit(0);
} else {
runextern(d->cmd);
}
}
/* print a stub for redirecting gopher+ clients to the non-gopher+ menu */
void gopherplus_stub(char *req)
{
/* this is a direct copy of the gopher.floodgap.com server notice */
outln("+-1");
printf("+INFO: 1Main menu (non-gopher+)\t\t%s\t%d\r\n", servername, serverport);
outln("+ADMIN:");
outln(" Admin: Server Administrator");
outln(" Server:");
outln("+VIEWS:");
outln(" application/gopher+-menu: <512b>");
outln("+ABSTRACT:");
outln(" This gopher supports standard gopher access only. Use this");
outln(" kludge to disable gopher+ client requests by your client.");
}
/* change to a directory; fail if directory is not readable by all */
void chdir_chk(const char *dir)
{
struct stat stbuf;
if(stat(dir, &stbuf)) {
errormsg("failed to stat directory");
exit(0);
}
if(!(stbuf.st_mode & S_IROTH)) {
errormsg("access to directory denied");
exit(0);
}
if(chdir(dir)) {
errormsg("chdir failed");
exit(0);
}
}
/* process request */
void procreq(char *request)
{
char *pathel;
char *sep;
struct stat stbuf;
node *no = NULL;
char *req = strsep(&request, "\t");
char *search = strsep(&request, "\t");
if(search) {
/* search string is specified */
if(!strcmp(search, "$")) {
/* gopher+ menu attempted, print gopher+ redirect */
gopherplus_stub(req);
exit(0);
}
/* set search string in environment
* (for the possibly run external search processor */
setenv("SEARCH", search, 1);
}
if(!strncmp(req, "URL:", 4)) {
/* generate html redirect page */
printf("<html><head>\r\n");
printf("<meta http-equiv=\"refresh\" content=\"5;url=%s\"/>\r\n", req+4);
printf("</head><body>\r\n");
printf("You'll be redirected in 5 seconds to:\r\n");
printf("<a href=\"%s\">%s</a>\r\n", req+4, req+4);
printf("</body></html>\r\n");
exit(0);
}
/* read .gopher.rec in server root if exists */
if(!stat(INFCONFIG, &stbuf)) {
FILE *fp = fopen(INFCONFIG, "r");
if(fp) {
readdirlist(fp);
fclose(fp);
}
}