View | Details | Raw Unified | Return to bug 76556
Collapse All | Expand All

(-)publicfile.new/Makefile (-2 / +68 lines)
Lines 7-25 Link Here
7
7
8
PORTNAME=	publicfile
8
PORTNAME=	publicfile
9
PORTVERSION=	0.52
9
PORTVERSION=	0.52
10
PORTREVISION=	1
10
PORTREVISION=	2
11
CATEGORIES=	www ftp
11
CATEGORIES=	www ftp
12
MASTER_SITES=	http://cr.yp.to/publicfile/ \
12
MASTER_SITES=	http://cr.yp.to/publicfile/ \
13
		ftp://cr.yp.to/publicfile/
13
		ftp://cr.yp.to/publicfile/
14
14
15
MAINTAINER=	ports@FreeBSD.org
15
MAINTAINER=	lx@redundancy.redundancy.org
16
COMMENT=	A secure, read-only, anonymous HTTP/FTP server
16
COMMENT=	A secure, read-only, anonymous HTTP/FTP server
17
17
18
RUN_DEPENDS=	setuidgid:${PORTSDIR}/sysutils/daemontools \
18
RUN_DEPENDS=	setuidgid:${PORTSDIR}/sysutils/daemontools \
19
		tcpserver:${PORTSDIR}/sysutils/ucspi-tcp
19
		tcpserver:${PORTSDIR}/sysutils/ucspi-tcp
20
20
21
pre-everything::
22
23
	@${ECHO_MSG}
24
	@${ECHO_MSG} "This port can use the following tuneables:"
25
	@${ECHO_MSG}
26
	@${ECHO_MSG} "WITH_BASICAUTH 		basic HTTP authentication support"
27
	@${ECHO_MSG} "WITH_COMMONLOG 		support for Apache common log format"
28
	@${ECHO_MSG} "WITH_REDIRECT_SLASH 	don't complain when omitting trailing slash"
29
	@${ECHO_MSG} "WITH_SSL 		provide SSL support through ucspi-ssl"
30
	@${ECHO_MSG}
31
32
.if defined(WITH_BASICAUTH) && defined(WITH_COMMONLOG)
33
	@${ECHO_MSG}
34
	@${ECHO_MSG} "Currently the common log and auth patches conflict."
35
	@${ECHO_MSG}
36
BROKEN=	"Currently the common log and auth patches conflict."
37
.endif
38
39
.if defined(WITH_BASICAUTH) && defined(WITH_REDIRECT_SLASH)
40
	@${ECHO_MSG}
41
	@${ECHO_MSG} "Currently the redirect slash and auth patches conflict."
42
	@${ECHO_MSG}
43
BROKEN=	"Currently the redirect slash and auth patches conflict."
44
.endif
45
46
.if defined(WITH_REDIRECT_SLASH)
47
pre-configure::
48
	@${PATCH} ${PATCH_ARGS} < ${PATCHDIR}/redirect-slash-patch
49
.endif
50
51
.if defined(WITH_ENV_FILETYPES)
52
pre-configure::
53
	@${PATCH} ${PATCH_ARGS} < ${PATCHDIR}/publicfile-0.52-filetype-diff
54
.endif
55
56
.if defined(WITH_SSL)
57
pre-configure::
58
	@${PATCH} ${PATCH_ARGS} < ${PATCHDIR}/publicfile.sslserver
59
RUN_DEPENDS+=	sslserver:${PORTSDIR}/sysutils/ucspi-ssl
60
.endif
61
62
.if defined(WITH_BASICAUTH)
63
pre-configure::
64
	@${SED} -e "s:__PORTSDIR__:${PORTSDIR}:g" \
65
		-e "s:__WRKSRC__:${WRKSRC}:g" \
66
		${PATCHDIR}/publicfile-0.52_basicauth.patch.in > \
67
		${PATCHDIR}/publicfile-0.52_basicauth.patch
68
	@${PATCH} ${PATCH_ARGS} -p1 < \
69
		${PATCHDIR}/publicfile-0.52_basicauth.patch
70
BUILD_DEPENDS+=	${NONEXISTENT}:${PORTSDIR}/databases/cdb:extract
71
PLIST_SUB+=	BASICAUTH=""
72
.else
73
PLIST_SUB+=	BASICAUTH="@comment "
74
.endif
75
76
.if defined(WITH_COMMONLOG)
77
pre-configure::
78
	@${PATCH} ${PATCH_ARGS} -p1 < \
79
		${PATCHDIR}/publicfile-0.52-commonlog-2.patch
80
.endif
81
21
ALL_TARGET=	it
82
ALL_TARGET=	it
22
INSTALL_TARGET=	setup check
83
INSTALL_TARGET=	setup check
84
85
post-extract:
86
.if defined(WITH_BASICAUTH)
87
	@${LN} -s ${PORTSDIR}/databases/cdb/work/cdb-0.75 ${WRKSRC}/cdb-0.75
88
.endif
23
89
24
post-patch:
90
post-patch:
25
	@${ECHO_CMD} "${CC} ${CFLAGS}" > ${WRKSRC}/conf-cc
91
	@${ECHO_CMD} "${CC} ${CFLAGS}" > ${WRKSRC}/conf-cc
(-)publicfile.new/distinfo (+1 lines)
Line 1 Link Here
1
MD5 (publicfile-0.52.tar.gz) = e493d69627b4fb2c7c764c0ff34330d7
1
MD5 (publicfile-0.52.tar.gz) = e493d69627b4fb2c7c764c0ff34330d7
2
SIZE (publicfile-0.52.tar.gz) = 34892
(-)publicfile.new/files/publicfile-0.52-commonlog-2.patch (+463 lines)
Line 0 Link Here
1
diff -rNC3 publicfile-0.52.orig/Makefile publicfile-0.52/Makefile
2
*** publicfile-0.52.orig/Makefile	Tue Nov  9 08:23:46 1999
3
--- publicfile-0.52/Makefile	Fri Nov  9 10:09:45 2001
4
***************
5
*** 450,460 ****
6
  stralloc.a: \
7
  makelib stralloc_cat.o stralloc_catb.o stralloc_cats.o \
8
  stralloc_copy.o stralloc_eady.o stralloc_opyb.o stralloc_opys.o \
9
! stralloc_pend.o stralloc_num.o
10
  	./makelib stralloc.a stralloc_cat.o stralloc_catb.o \
11
  	stralloc_cats.o stralloc_copy.o stralloc_eady.o \
12
  	stralloc_opyb.o stralloc_opys.o stralloc_pend.o \
13
! 	stralloc_num.o
14
  
15
  stralloc_cat.o: \
16
  compile stralloc_cat.c byte.h stralloc.h gen_alloc.h
17
--- 450,460 ----
18
  stralloc.a: \
19
  makelib stralloc_cat.o stralloc_catb.o stralloc_cats.o \
20
  stralloc_copy.o stralloc_eady.o stralloc_opyb.o stralloc_opys.o \
21
! stralloc_pend.o stralloc_num.o stralloc_opytrim.o
22
  	./makelib stralloc.a stralloc_cat.o stralloc_catb.o \
23
  	stralloc_cats.o stralloc_copy.o stralloc_eady.o \
24
  	stralloc_opyb.o stralloc_opys.o stralloc_pend.o \
25
! 	stralloc_num.o stralloc_opytrim.o
26
  
27
  stralloc_cat.o: \
28
  compile stralloc_cat.c byte.h stralloc.h gen_alloc.h
29
***************
30
*** 484,489 ****
31
--- 484,493 ----
32
  stralloc_opyb.o: \
33
  compile stralloc_opyb.c stralloc.h gen_alloc.h byte.h
34
  	./compile stralloc_opyb.c
35
+ 
36
+ stralloc_opytrim.o: \
37
+ compile stralloc_opytrim.c stralloc.h
38
+ 	./compile stralloc_opytrim.c
39
  
40
  stralloc_opys.o: \
41
  compile stralloc_opys.c byte.h str.h stralloc.h gen_alloc.h
42
diff -rNC3 publicfile-0.52.orig/README.log_combined publicfile-0.52/README.log_combined
43
*** publicfile-0.52.orig/README.log_combined	Thu Jan  1 01:00:00 1970
44
--- publicfile-0.52/README.log_combined	Fri Nov  9 10:09:45 2001
45
***************
46
*** 0 ****
47
--- 1,36 ----
48
+ This version of publicfile has been patched to write *additional* log 
49
+ information to STDERR. This additional information conforms to Apache's
50
+ and Netscape's "common" or "combined" log formats, i. e. a line may look
51
+ like this (common log format):
52
+ 
53
+ 127.0.0.1 - - [23/Oct/2001:14:04:56 +0200] "HEAD /~conrad/hello_world_gen.swf HTTP/1.0" 200 0
54
+ 
55
+ or like this (combined log format):
56
+ 
57
+ 127.0.0.1 - - [23/Oct/2001:14:04:56 +0200] "HEAD /~conrad/hello_world_gen.swf HTTP/1.0" 200 0 "http://localhost/~conrad/hello_world.html" "Mozilla 4"
58
+ 
59
+ The meaning of the (space-separated) fields is as follows:
60
+ 
61
+ 1. Client IP-address
62
+ 2. Remote logname (from ident (RFC-...) lookup - always "-" in publicfile)
63
+ 3. Authenticated username (from Basic-Auth - always "-" in publicfile)
64
+ 4. Human-readable timestamp enclosed in []
65
+ 5. The request line enclosed in ""
66
+ 6. HTTP status code
67
+ 7. Number of bytes sent excluding HTTP headers
68
+ 8. Referrer information sent by the browser
69
+ 9. User-Agent information sent by the browser
70
+ 
71
+ These log lines can easily be distinguished from other log lines using the
72
+ second field: this will always be "-" for common and combined log lines,
73
+ and "read" or "dir" for publicfile standard log lines (see
74
+ http://cr.yp.to/publicfile/log.html). The script "splitPublicfileLog.pl"
75
+ may serve as an example how to split the log into interesting parts.
76
+ 
77
+ The logformat to be used can be selected via environment variables:
78
+ 
79
+  - HTTPD_LOG_COMMON selects common log format
80
+  - HTTPD_LOG_COMBINED selects combined log format
81
+ 
82
+ The latter takes precedence over the former.
83
+ 
84
diff -rNC3 publicfile-0.52.orig/httpd.c publicfile-0.52/httpd.c
85
*** publicfile-0.52.orig/httpd.c	Tue Nov  9 08:23:46 1999
86
--- publicfile-0.52/httpd.c	Fri Nov  9 10:10:51 2001
87
***************
88
*** 15,20 ****
89
--- 15,102 ----
90
  #include "substdio.h"
91
  #include "error.h"
92
  #include "getln.h"
93
+ #include "caltime.h"
94
+ #include "subfd.h"
95
+ #include "env.h"
96
+ 
97
+ char logformat = 0;
98
+ char *remoteip;
99
+ char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
100
+ 		  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
101
+ stralloc referer = {0};
102
+ stralloc agent = {0};
103
+ 
104
+ char strnum[FMT_ULONG];
105
+ stralloc method = {0};
106
+ stralloc url = {0};
107
+ stralloc protocol = {0};
108
+ stralloc host = {0};
109
+ 
110
+ void log(char *code, unsigned long length) {
111
+   struct caltime ct;
112
+   int dummy1, dummy2;
113
+   struct tai now;
114
+ 
115
+   if (!logformat) return;
116
+ 
117
+   substdio_puts(subfderr,remoteip);
118
+   substdio_puts(subfderr," - - [");
119
+   tai_now(&now);
120
+   caltime_utc(&ct, &now, &dummy1, &dummy2);
121
+   if (ct.date.day < 10)
122
+     substdio_puts(subfderr,"0");
123
+   dummy1 = fmt_ulong(strnum,ct.date.day);
124
+   substdio_put(subfderr,strnum,dummy1);
125
+   substdio_puts(subfderr,"/");
126
+   if (ct.date.month > 0 && ct.date.month < 13)
127
+     substdio_puts(subfderr,months[ct.date.month-1]);
128
+   substdio_puts(subfderr,"/");
129
+   dummy1 = fmt_ulong(strnum,ct.date.year);
130
+   substdio_put(subfderr,strnum,dummy1);
131
+   substdio_puts(subfderr,":");
132
+   if (ct.hour < 10)
133
+     substdio_puts(subfderr,"0");
134
+   dummy1 = fmt_ulong(strnum,ct.hour);
135
+   substdio_put(subfderr,strnum,dummy1);
136
+   substdio_puts(subfderr,":");
137
+   if (ct.minute < 10)
138
+     substdio_puts(subfderr,"0");
139
+   dummy1 = fmt_ulong(strnum,ct.minute);
140
+   substdio_put(subfderr,strnum,dummy1);
141
+   substdio_puts(subfderr,":");
142
+   if (ct.second < 10)
143
+     substdio_puts(subfderr,"0");
144
+   dummy1 = fmt_ulong(strnum,ct.second);
145
+   substdio_put(subfderr,strnum,dummy1);
146
+   substdio_puts(subfderr," +0000] \"");
147
+   substdio_put(subfderr,method.s,method.len - 1); /* len includes \0 */
148
+   substdio_puts(subfderr," ");
149
+   substdio_put(subfderr,url.s,url.len);
150
+   substdio_puts(subfderr," ");
151
+   substdio_put(subfderr,protocol.s,protocol.len > 0 ? protocol.len - 1 : 0); /* len includes \0 */
152
+   substdio_puts(subfderr,"\" ");
153
+   substdio_put(subfderr,code,str_len(code) < 3 ? str_len(code) : 3);
154
+   substdio_puts(subfderr," ");
155
+   if (length > 0) {
156
+     dummy1 = fmt_ulong(strnum,length);
157
+     substdio_put(subfderr,strnum,dummy1);
158
+   } else {
159
+     substdio_puts(subfderr,"-");
160
+   }
161
+ 
162
+   if (logformat > 1) {
163
+     substdio_puts(subfderr," \"");
164
+     substdio_put(subfderr,referer.s,referer.len);
165
+     substdio_puts(subfderr,"\" \"");
166
+     substdio_put(subfderr,agent.s,agent.len);
167
+     substdio_puts(subfderr,"\"");
168
+   }
169
+ 
170
+   substdio_puts(subfderr," \"");
171
+   substdio_put(subfderr,host.s,host.len);
172
+   substdio_puts(subfderr,"\"\n");
173
+   substdio_flush(subfderr);
174
+ }
175
  
176
  int safewrite(int fd,char *buf,int len)
177
  {
178
***************
179
*** 42,54 ****
180
    substdio_flush(&out);
181
  }
182
  
183
- char strnum[FMT_ULONG];
184
- 
185
- stralloc protocol = {0};
186
  int protocolnum = 0;
187
- stralloc method = {0};
188
- stralloc url = {0};
189
- stralloc host = {0};
190
  stralloc path = {0};
191
  stralloc ims = {0};
192
  int flagbody = 1;
193
--- 124,130 ----
194
***************
195
*** 93,98 ****
196
--- 169,175 ----
197
      out_puts("</body></html>\r\n");
198
    }
199
    out_flush();
200
+   log(code, str_len(message) + 28);
201
    if (protocolnum >= 2) {
202
      shutdown(1,1);
203
      sleep(1); /* XXX */
204
***************
205
*** 105,108 ****
206
--- 182,186 ----
207
  
208
  void get(void)
209
  {
210
+   char *code;
211
    unsigned long length;
212
***************
213
*** 133,143 ****
214
      tai_now(&now);
215
      if (!httpdate(&mtimestr,&mtime)) _exit(21);
216
      if ((ims.len < mtimestr.len) || byte_diff(mtimestr.s,mtimestr.len,ims.s))
217
!       header("200 ","OK");
218
      else {
219
!       header("304 ","OK");
220
        flagbody = 0;
221
      }
222
      if (tai_less(&mtime,&now)) {
223
        tai_sub(&mtimeage,&now,&mtime);
224
        if (tai_approx(&mtimeage) >= 60.0) {
225
--- 211,222 ----
226
      tai_now(&now);
227
      if (!httpdate(&mtimestr,&mtime)) _exit(21);
228
      if ((ims.len < mtimestr.len) || byte_diff(mtimestr.s,mtimestr.len,ims.s))
229
!       code = "200 ";
230
      else {
231
!       code = "304 ";
232
        flagbody = 0;
233
      }
234
+     header(code,"OK");
235
      if (tai_less(&mtime,&now)) {
236
        tai_sub(&mtimeage,&now,&mtime);
237
        if (tai_approx(&mtimeage) >= 60.0) {
238
***************
239
*** 166,171 ****
240
--- 245,253 ----
241
          if (r == 0) break;
242
          out_put(filebuf,r);
243
        }
244
+     else
245
+       length = 0;
246
+     log(code,length);
247
      out_flush();
248
      _exit(0);
249
    }
250
***************
251
*** 180,185 ****
252
--- 262,270 ----
253
        out_put(filebuf,r);
254
        out_puts("\r\n");
255
      }
256
+   else
257
+     length = 0;
258
+   log(code,length);
259
  
260
    close(fd);
261
  }
262
***************
263
*** 216,221 ****
264
--- 301,314 ----
265
  
266
    sig_pipeignore();
267
  
268
+   if (env_get("HTTPD_LOG_COMMON"))
269
+     logformat = 1;
270
+   if (env_get("HTTPD_LOG_COMBINED"))
271
+     logformat = 2;
272
+   remoteip = env_get("TCPREMOTEIP");
273
+   if (!remoteip)
274
+     remoteip = "0";
275
+ 
276
    for (;;) {
277
      readline();
278
  
279
***************
280
*** 227,232 ****
281
--- 320,327 ----
282
      if (!stralloc_copys(&path,"")) _exit(21);
283
      if (!stralloc_copys(&protocol,"")) _exit(21);
284
      if (!stralloc_copys(&ims,"")) _exit(21);
285
+     if (!stralloc_copys(&referer,"")) _exit(21);
286
+     if (!stralloc_copys(&agent,"")) _exit(21);
287
      protocolnum = 2;
288
  
289
      spaces = 0;
290
***************
291
*** 294,299 ****
292
--- 389,398 ----
293
              barf("412 ","I do not accept If-None-Match");
294
            if (case_startb(field.s,field.len,"if-unmodified-since:"))
295
              barf("412 ","I do not accept If-Unmodified-Since");
296
+           if (case_startb(field.s,field.len,"referer:"))
297
+ 	    if (!stralloc_copytrim(&referer,field.s + 8,field.len - 8)) _exit(21);
298
+           if (case_startb(field.s,field.len,"user-agent:"))
299
+ 	    if (!stralloc_copytrim(&agent,field.s + 11,field.len - 11)) _exit(21);
300
            if (case_startb(field.s,field.len,"host:"))
301
              if (!host.len)
302
                for (i = 5;i < field.len;++i)
303
diff -rNC3 publicfile-0.52.orig/splitPublicfileLog.pl publicfile-0.52/splitPublicfileLog.pl
304
*** publicfile-0.52.orig/splitPublicfileLog.pl	Thu Jan  1 01:00:00 1970
305
--- publicfile-0.52/splitPublicfileLog.pl	Fri Nov  9 10:09:45 2001
306
***************
307
*** 0 ****
308
--- 1,119 ----
309
+ #!/usr/bin/perl -w
310
+ 
311
+ # This script will read lines from stdin. All lines starting with
312
+ # "a.b.c.d - " (for an IP-number a.b.c.d) will be written to the
313
+ # "access logfile" given as the first argument. Anything else will be
314
+ # prepended with a timestamp and
315
+ # written to the "error logfile" given as the second argument.
316
+ # Note that the messages usually are not really *error* messages, but
317
+ # simply the standard publicfile output.
318
+ 
319
+ use strict;
320
+ use Symbol;
321
+ 
322
+ if ($#ARGV < 0 || $#ARGV > 3) {
323
+     &usage();
324
+ }
325
+ 
326
+ local $main::logdir;
327
+ local $main::accessfile;
328
+ local $main::errorfile;
329
+ local $main::multilog;
330
+ local %main::handles = ();
331
+ 
332
+ if ($ARGV[0] eq "-m") {
333
+     $main::multilog = 1;
334
+     if ($#ARGV != 3) { &usage(); }
335
+     $main::logdir = $ARGV[1]."/";
336
+     $main::accessfile = $main::logdir.$ARGV[2];
337
+     $main::errorfile = $main::logdir.$ARGV[3];
338
+ } else {
339
+     $main::multilog = 0;
340
+     if ($#ARGV != 1) { &usage(); }
341
+     $main::accessfile = $ARGV[0];
342
+     $main::errorfile = $ARGV[1];
343
+ }
344
+ 
345
+ #open(ACCESS, ">>".$ARGV[0]) || die("Can't open access logfile!");
346
+ #open(ERROR, ">>".$ARGV[1]) || die("Can't open error logfile!");
347
+ $SIG{PIPE} = \&closeAndExit;
348
+ $SIG{TERM} = \&closeAndExit;
349
+ $SIG{QUIT} = \&closeAndExit;
350
+ $SIG{INT} = \&closeAndExit;
351
+ $SIG{HUP} = \&closeAll;
352
+ 
353
+ while($_ = <STDIN>) {
354
+     if (/^\d+\.\d+\.\d+\.\d+ - /) {
355
+ 	my $host = "";
356
+ 	if ($main::multilog && / \"([^\"\/]*)\"[\r\n]*$/) {
357
+ 	    $_ = $`."\n";
358
+ 	    $host = $1;
359
+ 	}
360
+ 	my $fh = &getHandle($host);
361
+ 	print $fh $_;
362
+     } else {
363
+ 	my $fh = &getHandle($main::errorfile);
364
+ 	print $fh time()." $_";
365
+     }
366
+ }
367
+ 
368
+ closeAndExit("End of input");
369
+ 
370
+ sub closeAll {
371
+     foreach my $key (keys %main::handles) {
372
+ 	my $fh = $main::handles{$key};
373
+ 	close $fh;
374
+ 	delete $main::handles{$key};
375
+     }
376
+ }
377
+ 
378
+ sub closeAndExit {
379
+     my $reason = shift;
380
+     my $now = time();
381
+ 
382
+     my $fh = &getHandle($main::errorfile);
383
+     print $fh "$now $reason received\n";
384
+     print STDERR "$now $reason received\n";
385
+     &closeAll();
386
+     exit 0;
387
+ }
388
+ 
389
+ sub getHandle {
390
+     my $key = shift;
391
+     my $res = 0;
392
+ 
393
+     if (defined($main::handles{$key})) {
394
+ 	return $main::handles{$key};
395
+     }
396
+ 
397
+     my $fh = Symbol::gensym();
398
+     if ($key eq $main::errorfile) {
399
+ 	open($fh, ">>".$main::errorfile) || die("Can't open error logfile!");
400
+     } else {
401
+ 	if ($main::multilog) {
402
+             my $fn = $main::logdir.$key;
403
+             if (-f $fn && -w $fn) {
404
+ 	        $res = open($fh, ">>$fn");
405
+ 	    }
406
+         }
407
+ 	if (!$res) {
408
+ 	    open($fh, ">>".$main::accessfile) || die("Can't open error logfile!");
409
+ 	}
410
+     }
411
+ 
412
+     $main::handles{$key} = $fh;
413
+     return $fh;
414
+ }
415
+ 
416
+ sub usage {
417
+     print STDERR "Usage: $0 <access-path> <error-path>\n";
418
+     print STDERR "   or: $0 -m <logdir> <access-file> <error-file>\n";
419
+     print STDERR "The first form will separate publicfile standard messages from \"common\" log\n";
420
+     print STDERR "lines. The second form will write \"common\" lines to <logdir>/<hostname>\n";
421
+     print STDERR "where <hostname> is the host given in the request. The file <logdir>/<hostname>\n";
422
+     print STDERR "must exist before it will be used. Anything for which no suitable logfile can\n";
423
+     print STDERR "be found will be logged to <logdir>/<access-path>.\n";
424
+ 
425
+     exit 1;
426
+ }
427
+ 
428
diff -rNC3 publicfile-0.52.orig/stralloc.h publicfile-0.52/stralloc.h
429
*** publicfile-0.52.orig/stralloc.h	Tue Nov  9 08:23:46 1999
430
--- publicfile-0.52/stralloc.h	Fri Nov  9 10:09:45 2001
431
***************
432
*** 12,17 ****
433
--- 12,18 ----
434
  extern int stralloc_copys();
435
  extern int stralloc_cats();
436
  extern int stralloc_copyb();
437
+ extern int stralloc_copytrim();
438
  extern int stralloc_catb();
439
  extern int stralloc_append(); /* beware: this takes a pointer to 1 char */
440
  extern int stralloc_starts();
441
diff -rNC3 publicfile-0.52.orig/stralloc_opytrim.c publicfile-0.52/stralloc_opytrim.c
442
*** publicfile-0.52.orig/stralloc_opytrim.c	Thu Jan  1 01:00:00 1970
443
--- publicfile-0.52/stralloc_opytrim.c	Fri Nov  9 10:09:45 2001
444
***************
445
*** 0 ****
446
--- 1,17 ----
447
+ #include "stralloc.h"
448
+ 
449
+ int stralloc_copytrim(sa,s,n)
450
+ stralloc *sa;
451
+ char *s;
452
+ unsigned int n;
453
+ {
454
+   while (n > 0 &&
455
+ 	 (s[n] == ' ' || s[n] == '\t' || s[n] == '\n' || s[n] == '\r')) n--;
456
+   while (n > 0 &&
457
+ 	 (s[0] == ' ' || s[0] == '\t' || s[0] == '\n' || s[0] == '\r')) {
458
+     n--;
459
+     s++;
460
+   }
461
+ 
462
+   return stralloc_copyb(sa,s,n);
463
+ }
(-)publicfile.new/files/publicfile-0.52-filetype-diff (+34 lines)
Line 0 Link Here
1
--- filetype.c.old	Mon Dec  6 10:43:36 1999
2
+++ filetype.c	Mon Dec  6 10:50:36 1999
3
@@ -1,5 +1,6 @@
4
 #include "filetype.h"
5
 #include "str.h"
6
+#include "env.h"
7
 
8
 void filetype(char *fn,stralloc *contenttype)
9
 {
10
@@ -22,7 +23,7 @@
11
       if (!stralloc_append(contenttype,&ch)) _exit(21);
12
     }
13
   else {
14
-    result = "text/plain";
15
+    result = 0;
16
     if (str_equal(x,".html")) result = "text/html";
17
     else if (str_equal(x,".gz")) result = "application/x-gzip";
18
     else if (str_equal(x,".dvi")) result = "application/x-dvi";
19
@@ -32,6 +33,15 @@
20
     else if (str_equal(x,".jpeg")) result = "image/jpeg";
21
     else if (str_equal(x,".png")) result = "image/png";
22
     else if (str_equal(x,".mpeg")) result = "video/mpeg";
23
+	if (!result) {
24
+		stralloc envname = {0};
25
+		if (!stralloc_copys(&envname,"CT_")) _exit(21);
26
+		if (!stralloc_cats(&envname,x+1)) _exit(21);
27
+		if (!stralloc_0(&envname)) _exit(21);
28
+		result=env_get(envname.s);
29
+		alloc_free(envname.s); /* is this the right function */
30
+	}
31
+	if (!result) result="text/plain";
32
 
33
     if (!stralloc_cats(contenttype,result)) _exit(21);
34
   }
(-)publicfile.new/files/publicfile-0.52_basicauth.patch (+638 lines)
Line 0 Link Here
1
diff -N -u -r publicfile-0.52.orig/Makefile publicfile-0.52/Makefile
2
--- publicfile-0.52.orig/Makefile	Mon Nov  8 23:23:46 1999
3
+++ publicfile-0.52/Makefile	Wed Aug 29 20:27:09 2001
4
@@ -234,21 +234,43 @@
5
 compile hier.c auto_home.h
6
 	./compile hier.c
7
 
8
+htrules: \
9
+load htrules.o base64.o
10
+	./load htrules cdb.a base64.o byte.a getln.a stralloc.a alloc.a \
11
+	substdio.a str.a buffer.a unix.a 
12
+
13
+htrules.o: \
14
+compile htrules.c strerr.h stralloc.h gen_alloc.h getln.h buffer.h \
15
+stralloc.h buffer.h exit.h fmt.h byte.h cdb_make.h buffer.h uint32.h \
16
+base64.h
17
+	./compile htrules.c
18
+
19
+base64.o: \
20
+compile base64.c base64.h
21
+	./compile base64.c
22
+
23
 httpd: \
24
 load httpd.o main.o pathdecode.o file.o filetype.o httpdate.o \
25
 percent.o prot.o timeoutread.o timeoutwrite.o libtai.a case.a getln.a \
26
 stralloc.a alloc.a substdio.a error.a open.a sig.a env.a str.a fs.a \
27
-socket.lib
28
+socket.lib readclose.o openreadclose.o
29
 	./load httpd main.o pathdecode.o file.o filetype.o \
30
 	httpdate.o percent.o prot.o timeoutread.o timeoutwrite.o \
31
 	libtai.a case.a getln.a stralloc.a alloc.a substdio.a \
32
-	error.a open.a sig.a env.a str.a fs.a  `cat socket.lib`
33
+	error.a open.a sig.a env.a str.a fs.a cdb.a byte.a seek_set.o \
34
+	readclose.o openreadclose.o `cat socket.lib`
35
+
36
+cdb:
37
+	(cd /usr/ports/databases/cdb/work/cdb-0.75 && \
38
+	 make && \
39
+	 cp -p cdb_make.h buffer.h cdb.h uint32.h cdb.a byte.a seek_set.o \
40
+	 cdb_make.o error.c buffer.a unix.a /usr/ports/www/publicfile.new/work/publicfile-0.52/)
41
 
42
 httpd.o: \
43
 compile httpd.c pathdecode.h stralloc.h gen_alloc.h file.h tai.h \
44
 uint64.h filetype.h stralloc.h percent.h stralloc.h stralloc.h sig.h \
45
 exit.h fmt.h case.h str.h tai.h httpdate.h stralloc.h tai.h \
46
-timeoutread.h timeoutwrite.h substdio.h error.h getln.h
47
+timeoutread.h timeoutwrite.h substdio.h error.h getln.h byte.h
48
 	./compile httpd.c
49
 
50
 httpdate.o: \
51
@@ -358,6 +380,11 @@
52
 compile open_trunc.c open.h
53
 	./compile open_trunc.c
54
 
55
+openreadclose.o: \
56
+compile openreadclose.c error.h open.h readclose.h stralloc.h \
57
+gen_alloc.h openreadclose.h stralloc.h
58
+	./compile openreadclose.c
59
+
60
 pathdecode.o: \
61
 compile pathdecode.c pathdecode.h stralloc.h gen_alloc.h
62
 	./compile pathdecode.c
63
@@ -367,7 +394,7 @@
64
 	./compile percent.c
65
 
66
 prog: \
67
-configure httpd ftpd rts utime
68
+cdb configure httpd ftpd rts utime htrules
69
 
70
 prot.o: \
71
 compile prot.c hasshsgr.h prot.h
72
diff -N -u -r publicfile-0.52.orig/README.basicauth publicfile-0.52/README.basicauth
73
--- publicfile-0.52.orig/README.basicauth	Wed Dec 31 16:00:00 1969
74
+++ publicfile-0.52/README.basicauth	Wed Aug 29 22:16:02 2001
75
@@ -0,0 +1,100 @@
76
+Here is a patch for publicfile to allow for Basic Auth.
77
+
78
+Building Instructions:
79
+
80
+Save this patch as publicfile-0.52.basicauth.patch
81
+Download publicfile-0.52
82
+Download cdb-0.75
83
+
84
+gunzip publicfile-0.52.tar
85
+gunzip cdb-0.75.tar
86
+tar -xf publicfile-0.52.tar
87
+cd publicfile-0.52
88
+tar -xf ../cdb-0.75.tar
89
+patch -p1 < publicfile-0.52.basicauth.patch
90
+
91
+Follow normal installation instructions for publicfile beginning with
92
+'make setup check'
93
+
94
+Usage Instructions:
95
+
96
+Once this patch has been applied, httpd will check for a file
97
+called '.access' in the current directory of any requested
98
+file. e.g, if /public/file/0/path/to/file.html is requested, httpd
99
+will first check for /public/file/0/path/to/.access.
100
+
101
+.access should have the format:
102
+  realm_id:realm_txt
103
+
104
+realm_id is used as documented below. realm_txt is typically
105
+presented by the user's browser. .access must be readable by httpd
106
+and only protects a specific directory. Sub-directories are not
107
+protected unless they also contain a .access file.
108
+
109
+An additional program will be installed in /usr/local/publicfile (or
110
+whatever conf-home is) called htrules. Use this like tcprules:
111
+
112
+cd /public/file
113
+htrules access.cdb access.tmp < access
114
+
115
+This may safely be run at any time.
116
+
117
+access should have the format:
118
+
119
+  # this is a comment. blank lines are allowed too.
120
+  # the next line authorizes a user to a specific realm_id
121
+  realm_id:username:password
122
+  # the next line authorizes host class.
123
+  realm_id:LOCALHOST
124
+
125
+access.cdb must be readable by httpd.
126
+
127
+realm_id corresponds to the realm_id in the .access file(s).
128
+
129
+Each realm_id line specifies either a username:password combination
130
+or a host class. Note that the same username may have different 
131
+passwords in different realm_id's.
132
+
133
+A host is mapped into a host class via the environment
134
+variable HTTPCLIENT. This environment variable should be
135
+set in tcpserver's rules.cdb. 
136
+
137
+Here is an example:
138
+
139
+  === /public/file/0/private1/.access ===
140
+  realm1:Dr. Suess
141
+
142
+  === /public/file/0/private2/.access ===
143
+  realm2:Sesame Street
144
+
145
+  === /public/file/access ===
146
+  # realm1 are Dr Suess users/clients
147
+  realm1:john:catinthehat
148
+  realm1:mary:greeneggswithham
149
+  realm1:LOCALHOST
150
+  realm1:DR SUESS
151
+  # realm1 are Sesame Street users/clients
152
+  realm2:tom:bigbird
153
+  realm2:abi:cookiemonster
154
+  realm2:mary:earnie
155
+  realm2:LOCALHOST
156
+  realm2:SESAME STREET
157
+
158
+  === /etc/rules ===
159
+  127.0.0.1:allow,HTTPCLIENT="LOCALHOST"
160
+  10.0.0.:allow,HTTPCLIENT="DR SUESS"
161
+  10.1.0.:allow,HTTPCLIENT="SESAME STREET"
162
+  :allow
163
+
164
+The changes to the Makefile aren't very clean, but everything compiles
165
+correctly.
166
+
167
+Thanks to Eric M. Johnston's for base64.{c,h} from YAQSAP
168
+(Yet Another qmail SMTP AUTH Patch) -
169
+http://qmail.goof.com/qmail-auth-20010105.tar.gz
170
+
171
+This patch available at
172
+http://www.soffian.org/downloads/publicfile-0.52_basicauth.patch
173
+
174
+Jay Soffian <jay@soffian.org> 29 Aug 2001
175
+
176
diff -N -u -r publicfile-0.52.orig/base64.c publicfile-0.52/base64.c
177
--- publicfile-0.52.orig/base64.c	Wed Dec 31 16:00:00 1969
178
+++ publicfile-0.52/base64.c	Wed Aug 22 22:17:39 2001
179
@@ -0,0 +1,90 @@
180
+#include "base64.h"
181
+#include "stralloc.h"
182
+#include "substdio.h"
183
+#include "str.h"
184
+
185
+static char *b64alpha =
186
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
187
+#define B64PAD '='
188
+
189
+/* returns 0 ok, 1 illegal, -1 problem */
190
+
191
+int b64decode(in,l,out)
192
+const unsigned char *in;
193
+int l;
194
+stralloc *out; /* not null terminated */
195
+{
196
+  int i, j;
197
+  unsigned char a[4];
198
+  unsigned char b[3];
199
+  char *s;
200
+
201
+  if (l == 0)
202
+  {
203
+    if (!stralloc_copys(out,"")) return -1;
204
+    return 0;
205
+  }
206
+
207
+  if (!stralloc_ready(out,l + 2)) return -1; /* XXX generous */
208
+  s = out->s;
209
+
210
+  for (i = 0;i < l;i += 4) {
211
+    for (j = 0;j < 4;j++)
212
+      if ((i + j) < l && in[i + j] != B64PAD)
213
+      {
214
+        a[j] = str_chr(b64alpha,in[i + j]);
215
+        if (a[j] > 63) return 1;
216
+      }
217
+      else a[j] = 0;
218
+
219
+    b[0] = (a[0] << 2) | (a[1] >> 4);
220
+    b[1] = (a[1] << 4) | (a[2] >> 2);
221
+    b[2] = (a[2] << 6) | (a[3]);
222
+
223
+    *s++ = b[0];
224
+
225
+    if (in[i + 1] == B64PAD) break;
226
+    *s++ = b[1];
227
+
228
+    if (in[i + 2] == B64PAD) break;
229
+    *s++ = b[2];
230
+  }
231
+  out->len = s - out->s;
232
+  while (out->len && !out->s[out->len - 1]) --out->len; /* XXX avoid? */
233
+  return 0;
234
+}
235
+
236
+int b64encode(in,out)
237
+stralloc *in;
238
+stralloc *out; /* not null terminated */
239
+{
240
+  unsigned char a, b, c;
241
+  int i;
242
+  char *s;
243
+
244
+  if (in->len == 0)
245
+  {
246
+    if (!stralloc_copys(out,"")) return -1;
247
+    return 0;
248
+  }
249
+
250
+  if (!stralloc_ready(out,in->len / 3 * 4 + 4)) return -1;
251
+  s = out->s;
252
+
253
+  for (i = 0;i < in->len;i += 3) {
254
+    a = in->s[i];
255
+    b = i + 1 < in->len ? in->s[i + 1] : 0;
256
+    c = i + 2 < in->len ? in->s[i + 2] : 0;
257
+
258
+    *s++ = b64alpha[a >> 2];
259
+    *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
260
+
261
+    if (i + 1 >= in->len) *s++ = B64PAD;
262
+    else *s++ = b64alpha[((b & 15) << 2) | (c >> 6)];
263
+
264
+    if (i + 2 >= in->len) *s++ = B64PAD;
265
+    else *s++ = b64alpha[c & 63];
266
+  }
267
+  out->len = s - out->s;
268
+  return 0;
269
+}
270
diff -N -u -r publicfile-0.52.orig/base64.h publicfile-0.52/base64.h
271
--- publicfile-0.52.orig/base64.h	Wed Dec 31 16:00:00 1969
272
+++ publicfile-0.52/base64.h	Wed Aug 22 22:17:39 2001
273
@@ -0,0 +1,7 @@
274
+#ifndef BASE64_H
275
+#define BASE64_H
276
+
277
+extern int b64decode();
278
+extern int b64encode();
279
+
280
+#endif
281
diff -N -u -r publicfile-0.52.orig/hier.c publicfile-0.52/hier.c
282
--- publicfile-0.52.orig/hier.c	Mon Nov  8 23:23:46 1999
283
+++ publicfile-0.52/hier.c	Wed Aug 22 22:17:39 2001
284
@@ -7,6 +7,7 @@
285
   d(auto_home,"bin",-1,-1,02755);
286
 
287
   c(auto_home,"bin","configure",-1,-1,0755);
288
+  c(auto_home,"bin","htrules",-1,-1,0755);
289
   c(auto_home,"bin","httpd",-1,-1,0755);
290
   c(auto_home,"bin","ftpd",-1,-1,0755);
291
 }
292
diff -N -u -r publicfile-0.52.orig/htrules.c publicfile-0.52/htrules.c
293
--- publicfile-0.52.orig/htrules.c	Wed Dec 31 16:00:00 1969
294
+++ publicfile-0.52/htrules.c	Wed Aug 29 21:27:42 2001
295
@@ -0,0 +1,117 @@
296
+#include "strerr.h"
297
+#include "stralloc.h"
298
+#include "getln.h"
299
+#include "buffer.h"
300
+#include "exit.h"
301
+#include "fmt.h"
302
+#include "byte.h"
303
+#include "base64.h"
304
+#include "cdb_make.h"
305
+
306
+#define FATAL "htrules: fatal: "
307
+
308
+unsigned long linenum = 0;
309
+char *fntemp;
310
+char *fn;
311
+
312
+stralloc line = {0};
313
+int match = 1;
314
+
315
+stralloc base64 = {0};
316
+stralloc key = {0};
317
+stralloc realm = {0};
318
+stralloc userpass = {0};
319
+
320
+struct cdb_make c;
321
+
322
+void nomem(void)
323
+{
324
+  strerr_die2x(111,FATAL,"out of memory");
325
+}
326
+void usage(void)
327
+{
328
+  strerr_die1x(100,"htrules: usage: htrules access.cdb access.tmp");
329
+}
330
+void die_bad(void)
331
+{
332
+  if (!stralloc_0(&line)) nomem();
333
+  strerr_die3x(100,FATAL,"unable to parse this line: ",line.s);
334
+}
335
+void die_write(void)
336
+{
337
+  strerr_die4sys(111,FATAL,"unable to write to ",fntemp,": ");
338
+}
339
+
340
+main(int argc,char **argv)
341
+{
342
+  int colon;
343
+  char *x;
344
+  int len;
345
+  int fd;
346
+  int i;
347
+  char ch;
348
+
349
+  fn = argv[1];
350
+  if (!fn) usage();
351
+  fntemp = argv[2];
352
+  if (!fntemp) usage();
353
+
354
+  fd = open_trunc(fntemp);
355
+  if (fd == -1)
356
+    strerr_die4sys(111,FATAL,"unable to create ",fntemp,": ");
357
+  if (cdb_make_start(&c,fd) == -1) die_write();
358
+
359
+  while (match) {
360
+    if (getln(buffer_0,&line,&match,'\n') == -1)
361
+      strerr_die2sys(111,FATAL,"unable to read input: ");
362
+
363
+    x = line.s; len = line.len;
364
+
365
+    if (!len) break;
366
+    if (x[0] == '#') continue;
367
+    if (x[0] == '\n') continue;
368
+
369
+    while (len) {
370
+      ch = x[len - 1];
371
+      if (ch != '\n') if (ch != ' ') if (ch != '\t') break;
372
+      --len;
373
+    }
374
+    line.len = len; /* for die_bad() */
375
+    if (!len) continue;
376
+
377
+    colon = byte_chr(x,len,':');
378
+    if (!colon || colon == len) die_bad();
379
+    if (!stralloc_copyb(&realm,x,colon)) nomem();
380
+    x += colon + 1; len -= colon + 1;
381
+
382
+    colon = byte_chr(x,len,':');
383
+    if (colon == len) {
384
+      if (!stralloc_copyb(&key,"C",1)) nomem();
385
+      if (!stralloc_cat(&key,&realm)) nomem();
386
+      if (!stralloc_catb(&key,":",1)) nomem();
387
+      if (!stralloc_catb(&key,x,len)) nomem();
388
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
389
+    } else {
390
+      if (!stralloc_copyb(&userpass,x,len)) nomem();
391
+      if (b64encode(&userpass,&base64) == -1) nomem();
392
+
393
+      if (!stralloc_copyb(&key,"U",1)) nomem();
394
+      if (!stralloc_cat(&key,&base64)) nomem();
395
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
396
+
397
+      if (!stralloc_copyb(&key,"R",1)) nomem();
398
+      if (!stralloc_cat(&key,&realm)) nomem();
399
+      if (!stralloc_catb(&key,":",1)) nomem();
400
+      if (!stralloc_cat(&key,&base64)) nomem();
401
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
402
+    } 
403
+  }
404
+
405
+  if (cdb_make_finish(&c) == -1) die_write();
406
+  if (fsync(fd) == -1) die_write();
407
+  if (close(fd) == -1) die_write(); /* NFS stupidity */
408
+  if (rename(fntemp,fn))
409
+    strerr_die6sys(111,FATAL,"unable to move ",fntemp," to ",fn,": ");
410
+
411
+  _exit(0);
412
+}
413
diff -N -u -r publicfile-0.52.orig/httpd.c publicfile-0.52/httpd.c
414
--- publicfile-0.52.orig/httpd.c	Mon Nov  8 23:23:46 1999
415
+++ publicfile-0.52/httpd.c	Wed Aug 29 21:30:34 2001
416
@@ -15,6 +15,10 @@
417
 #include "substdio.h"
418
 #include "error.h"
419
 #include "getln.h"
420
+#include "byte.h"
421
+#include "cdb.h"
422
+#include "openreadclose.h"
423
+#include "env.h"
424
 
425
 int safewrite(int fd,char *buf,int len)
426
 {
427
@@ -51,6 +55,7 @@
428
 stralloc host = {0};
429
 stralloc path = {0};
430
 stralloc ims = {0};
431
+stralloc basic_auth = {0};
432
 int flagbody = 1;
433
 
434
 char filebuf[1024];
435
@@ -75,11 +80,16 @@
436
   out_puts("\r\n");
437
 }
438
 
439
-void barf(char *code,char *message)
440
+void barf2(char *code,char *message,char *realm)
441
 {
442
   if (protocolnum > 0) {
443
     tai_now(&now);
444
     header(code,message);
445
+    if(realm) {
446
+      out_puts("WWW-Authenticate: Basic realm=\"");
447
+      out_puts(realm);
448
+      out_puts("\"\r\n");
449
+    }
450
     out_puts("Content-Length: ");
451
     out_put(strnum,fmt_ulong(strnum,str_len(message) + 28));
452
     out_puts("\r\n");
453
@@ -100,8 +110,81 @@
454
   _exit(0);
455
 }
456
 
457
+void barf(char *code,char *message)
458
+{
459
+  barf2(code,message,(char *)0);
460
+}
461
+
462
 stralloc fn = {0};
463
+stralloc accessfn = {0};
464
 stralloc contenttype = {0};
465
+stralloc realm = {0};
466
+stralloc realmtxt = {0};
467
+stralloc key = {0};
468
+
469
+void checkauth(void)
470
+{
471
+  int len;
472
+  int fd;
473
+  int colon;
474
+  static struct cdb c;
475
+  char *x;
476
+ 
477
+  len = byte_rchr(fn.s,fn.len,'/');
478
+  if (!stralloc_copyb(&accessfn,fn.s,len)) _exit(21);
479
+  if (!stralloc_cats(&accessfn,"/.access")) _exit(21);
480
+  if (!stralloc_0(&accessfn)) _exit(21);
481
+
482
+  if (openreadclose(accessfn.s,&realm,256) == 0) return;
483
+  if (!realm.len) _exit(23); /* no realm */
484
+  realm.len = byte_chr(realm.s,realm.len,'\n');
485
+  while (realm.len) {
486
+    if (realm.s[realm.len - 1] != ' ')
487
+      if (realm.s[realm.len - 1] != '\t')
488
+        break;
489
+    --realm.len;
490
+  }
491
+  colon = byte_chr(realm.s,realm.len,':');
492
+  if (!colon) _exit(23); /* no realm */
493
+  if (colon == realm.len) {
494
+    if (!stralloc_copys(&realmtxt,"restricted access")) _exit(21);
495
+  } else {
496
+    if (!stralloc_copyb(&realmtxt,realm.s+colon+1,realm.len-(colon+1))) _exit(21);
497
+    realm.len = colon;
498
+  }
499
+  if (!stralloc_0(&realmtxt)) _exit(21);
500
+
501
+  fd = open_read("/access.cdb");
502
+  if (fd == -1) _exit(23);
503
+  cdb_init(&c,fd);
504
+
505
+  x = env_get("HTTPCLIENT");
506
+  if (x) {
507
+    if (!stralloc_copyb(&key,"C",1)) _exit(21);
508
+    if (!stralloc_cat(&key,&realm)) _exit(21);
509
+    if (!stralloc_catb(&key,":",1)) _exit(21);
510
+    if (!stralloc_cats(&key,x)) _exit(21);
511
+    if (cdb_find(&c,key.s,key.len) == 1) goto AUTH_OK;
512
+  }
513
+
514
+  if (!basic_auth.len) barf2("401 ","Authorization Required", realmtxt.s);
515
+
516
+  if (!stralloc_copyb(&key,"U",1)) _exit(21);
517
+  if (!stralloc_cat(&key,&basic_auth)) _exit(21);
518
+  if (cdb_find(&c,key.s,key.len) != 1)
519
+    barf2("401 ","Authorization Required",realmtxt.s);
520
+
521
+  if (!stralloc_copyb(&key,"R",1)) _exit(21);
522
+  if (!stralloc_cat(&key,&realm)) _exit(21);
523
+  if (!stralloc_catb(&key,":",1)) _exit(21);
524
+  if (!stralloc_cat(&key,&basic_auth)) _exit(21);
525
+  if (cdb_find(&c,key.s,key.len) != 1) barf("403 ","Forbidden");
526
+
527
+  AUTH_OK:
528
+  alloc_free(key);
529
+  cdb_free(&c);
530
+  close(fd);
531
+}
532
 
533
 void get(void)
534
 {
535
@@ -124,6 +207,8 @@
536
   if (!stralloc_cat(&fn,&path)) _exit(21);
537
   pathdecode(&fn);
538
   if (!stralloc_0(&fn)) _exit(21);
539
+  
540
+  checkauth();
541
 
542
   fd = file_open(fn.s,&mtime,&length,1);
543
   if (fd == -1)
544
@@ -227,6 +312,7 @@
545
     if (!stralloc_copys(&path,"")) _exit(21);
546
     if (!stralloc_copys(&protocol,"")) _exit(21);
547
     if (!stralloc_copys(&ims,"")) _exit(21);
548
+    if (!stralloc_copys(&basic_auth,"")) _exit(21);
549
     protocolnum = 2;
550
 
551
     spaces = 0;
552
@@ -302,6 +388,8 @@
553
                     if (!stralloc_append(&host,&field.s[i])) _exit(21);
554
           if (case_startb(field.s,field.len,"if-modified-since:"))
555
 	    if (!stralloc_copyb(&ims,field.s + 18,field.len - 18)) _exit(21);
556
+	  if (case_startb(field.s,field.len,"authorization: basic "))
557
+	    if (!stralloc_copyb(&basic_auth,field.s + 21,field.len - 21)) _exit(21);
558
           field.len = 0;
559
         }
560
         if (!line.len) break;
561
diff -N -u -r publicfile-0.52.orig/openreadclose.c publicfile-0.52/openreadclose.c
562
--- publicfile-0.52.orig/openreadclose.c	Wed Dec 31 16:00:00 1969
563
+++ publicfile-0.52/openreadclose.c	Wed Aug 29 14:24:21 2001
564
@@ -0,0 +1,18 @@
565
+/* Public domain. */
566
+
567
+#include "error.h"
568
+#include "open.h"
569
+#include "readclose.h"
570
+#include "openreadclose.h"
571
+
572
+int openreadclose(const char *fn,stralloc *sa,unsigned int bufsize)
573
+{
574
+  int fd;
575
+  fd = open_read(fn);
576
+  if (fd == -1) {
577
+    if (errno == error_noent) return 0;
578
+    return -1;
579
+  }
580
+  if (readclose(fd,sa,bufsize) == -1) return -1;
581
+  return 1;
582
+}
583
diff -N -u -r publicfile-0.52.orig/openreadclose.h publicfile-0.52/openreadclose.h
584
--- publicfile-0.52.orig/openreadclose.h	Wed Dec 31 16:00:00 1969
585
+++ publicfile-0.52/openreadclose.h	Wed Aug 29 14:24:21 2001
586
@@ -0,0 +1,10 @@
587
+/* Public domain. */
588
+
589
+#ifndef OPENREADCLOSE_H
590
+#define OPENREADCLOSE_H
591
+
592
+#include "stralloc.h"
593
+
594
+extern int openreadclose(const char *,stralloc *,unsigned int);
595
+
596
+#endif
597
diff -N -u -r publicfile-0.52.orig/readclose.c publicfile-0.52/readclose.c
598
--- publicfile-0.52.orig/readclose.c	Wed Dec 31 16:00:00 1969
599
+++ publicfile-0.52/readclose.c	Wed Aug 29 14:30:52 2001
600
@@ -0,0 +1,23 @@
601
+/* Public domain. */
602
+
603
+#include <unistd.h>
604
+#include "error.h"
605
+#include "readclose.h"
606
+
607
+int readclose_append(int fd,stralloc *sa,unsigned int bufsize)
608
+{
609
+  int r;
610
+  for (;;) {
611
+    if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
612
+    r = read(fd,sa->s + sa->len,bufsize);
613
+    if (r == -1) if (errno == error_intr) continue;
614
+    if (r <= 0) { close(fd); return r; }
615
+    sa->len += r;
616
+  }
617
+}
618
+
619
+int readclose(int fd,stralloc *sa,unsigned int bufsize)
620
+{
621
+  if (!stralloc_copys(sa,"")) { close(fd); return -1; }
622
+  return readclose_append(fd,sa,bufsize);
623
+}
624
diff -N -u -r publicfile-0.52.orig/readclose.h publicfile-0.52/readclose.h
625
--- publicfile-0.52.orig/readclose.h	Wed Dec 31 16:00:00 1969
626
+++ publicfile-0.52/readclose.h	Wed Aug 29 14:30:52 2001
627
@@ -0,0 +1,11 @@
628
+/* Public domain. */
629
+
630
+#ifndef READCLOSE_H
631
+#define READCLOSE_H
632
+
633
+#include "stralloc.h"
634
+
635
+extern int readclose_append(int,stralloc *,unsigned int);
636
+extern int readclose(int,stralloc *,unsigned int);
637
+
638
+#endif
(-)publicfile.new/files/publicfile-0.52_basicauth.patch.in (+638 lines)
Line 0 Link Here
1
diff -N -u -r publicfile-0.52.orig/Makefile publicfile-0.52/Makefile
2
--- publicfile-0.52.orig/Makefile	Mon Nov  8 23:23:46 1999
3
+++ publicfile-0.52/Makefile	Wed Aug 29 20:27:09 2001
4
@@ -234,21 +234,43 @@
5
 compile hier.c auto_home.h
6
 	./compile hier.c
7
 
8
+htrules: \
9
+load htrules.o base64.o
10
+	./load htrules cdb.a base64.o byte.a getln.a stralloc.a alloc.a \
11
+	substdio.a str.a buffer.a unix.a 
12
+
13
+htrules.o: \
14
+compile htrules.c strerr.h stralloc.h gen_alloc.h getln.h buffer.h \
15
+stralloc.h buffer.h exit.h fmt.h byte.h cdb_make.h buffer.h uint32.h \
16
+base64.h
17
+	./compile htrules.c
18
+
19
+base64.o: \
20
+compile base64.c base64.h
21
+	./compile base64.c
22
+
23
 httpd: \
24
 load httpd.o main.o pathdecode.o file.o filetype.o httpdate.o \
25
 percent.o prot.o timeoutread.o timeoutwrite.o libtai.a case.a getln.a \
26
 stralloc.a alloc.a substdio.a error.a open.a sig.a env.a str.a fs.a \
27
-socket.lib
28
+socket.lib readclose.o openreadclose.o
29
 	./load httpd main.o pathdecode.o file.o filetype.o \
30
 	httpdate.o percent.o prot.o timeoutread.o timeoutwrite.o \
31
 	libtai.a case.a getln.a stralloc.a alloc.a substdio.a \
32
-	error.a open.a sig.a env.a str.a fs.a  `cat socket.lib`
33
+	error.a open.a sig.a env.a str.a fs.a cdb.a byte.a seek_set.o \
34
+	readclose.o openreadclose.o `cat socket.lib`
35
+
36
+cdb:
37
+	(cd __PORTSDIR__/databases/cdb/work/cdb-0.75 && \
38
+	 make && \
39
+	 cp -p cdb_make.h buffer.h cdb.h uint32.h cdb.a byte.a seek_set.o \
40
+	 cdb_make.o error.c buffer.a unix.a __WRKSRC__/)
41
 
42
 httpd.o: \
43
 compile httpd.c pathdecode.h stralloc.h gen_alloc.h file.h tai.h \
44
 uint64.h filetype.h stralloc.h percent.h stralloc.h stralloc.h sig.h \
45
 exit.h fmt.h case.h str.h tai.h httpdate.h stralloc.h tai.h \
46
-timeoutread.h timeoutwrite.h substdio.h error.h getln.h
47
+timeoutread.h timeoutwrite.h substdio.h error.h getln.h byte.h
48
 	./compile httpd.c
49
 
50
 httpdate.o: \
51
@@ -358,6 +380,11 @@
52
 compile open_trunc.c open.h
53
 	./compile open_trunc.c
54
 
55
+openreadclose.o: \
56
+compile openreadclose.c error.h open.h readclose.h stralloc.h \
57
+gen_alloc.h openreadclose.h stralloc.h
58
+	./compile openreadclose.c
59
+
60
 pathdecode.o: \
61
 compile pathdecode.c pathdecode.h stralloc.h gen_alloc.h
62
 	./compile pathdecode.c
63
@@ -367,7 +394,7 @@
64
 	./compile percent.c
65
 
66
 prog: \
67
-configure httpd ftpd rts utime
68
+cdb configure httpd ftpd rts utime htrules
69
 
70
 prot.o: \
71
 compile prot.c hasshsgr.h prot.h
72
diff -N -u -r publicfile-0.52.orig/README.basicauth publicfile-0.52/README.basicauth
73
--- publicfile-0.52.orig/README.basicauth	Wed Dec 31 16:00:00 1969
74
+++ publicfile-0.52/README.basicauth	Wed Aug 29 22:16:02 2001
75
@@ -0,0 +1,100 @@
76
+Here is a patch for publicfile to allow for Basic Auth.
77
+
78
+Building Instructions:
79
+
80
+Save this patch as publicfile-0.52.basicauth.patch
81
+Download publicfile-0.52
82
+Download cdb-0.75
83
+
84
+gunzip publicfile-0.52.tar
85
+gunzip cdb-0.75.tar
86
+tar -xf publicfile-0.52.tar
87
+cd publicfile-0.52
88
+tar -xf ../cdb-0.75.tar
89
+patch -p1 < publicfile-0.52.basicauth.patch
90
+
91
+Follow normal installation instructions for publicfile beginning with
92
+'make setup check'
93
+
94
+Usage Instructions:
95
+
96
+Once this patch has been applied, httpd will check for a file
97
+called '.access' in the current directory of any requested
98
+file. e.g, if /public/file/0/path/to/file.html is requested, httpd
99
+will first check for /public/file/0/path/to/.access.
100
+
101
+.access should have the format:
102
+  realm_id:realm_txt
103
+
104
+realm_id is used as documented below. realm_txt is typically
105
+presented by the user's browser. .access must be readable by httpd
106
+and only protects a specific directory. Sub-directories are not
107
+protected unless they also contain a .access file.
108
+
109
+An additional program will be installed in /usr/local/publicfile (or
110
+whatever conf-home is) called htrules. Use this like tcprules:
111
+
112
+cd /public/file
113
+htrules access.cdb access.tmp < access
114
+
115
+This may safely be run at any time.
116
+
117
+access should have the format:
118
+
119
+  # this is a comment. blank lines are allowed too.
120
+  # the next line authorizes a user to a specific realm_id
121
+  realm_id:username:password
122
+  # the next line authorizes host class.
123
+  realm_id:LOCALHOST
124
+
125
+access.cdb must be readable by httpd.
126
+
127
+realm_id corresponds to the realm_id in the .access file(s).
128
+
129
+Each realm_id line specifies either a username:password combination
130
+or a host class. Note that the same username may have different 
131
+passwords in different realm_id's.
132
+
133
+A host is mapped into a host class via the environment
134
+variable HTTPCLIENT. This environment variable should be
135
+set in tcpserver's rules.cdb. 
136
+
137
+Here is an example:
138
+
139
+  === /public/file/0/private1/.access ===
140
+  realm1:Dr. Suess
141
+
142
+  === /public/file/0/private2/.access ===
143
+  realm2:Sesame Street
144
+
145
+  === /public/file/access ===
146
+  # realm1 are Dr Suess users/clients
147
+  realm1:john:catinthehat
148
+  realm1:mary:greeneggswithham
149
+  realm1:LOCALHOST
150
+  realm1:DR SUESS
151
+  # realm1 are Sesame Street users/clients
152
+  realm2:tom:bigbird
153
+  realm2:abi:cookiemonster
154
+  realm2:mary:earnie
155
+  realm2:LOCALHOST
156
+  realm2:SESAME STREET
157
+
158
+  === /etc/rules ===
159
+  127.0.0.1:allow,HTTPCLIENT="LOCALHOST"
160
+  10.0.0.:allow,HTTPCLIENT="DR SUESS"
161
+  10.1.0.:allow,HTTPCLIENT="SESAME STREET"
162
+  :allow
163
+
164
+The changes to the Makefile aren't very clean, but everything compiles
165
+correctly.
166
+
167
+Thanks to Eric M. Johnston's for base64.{c,h} from YAQSAP
168
+(Yet Another qmail SMTP AUTH Patch) -
169
+http://qmail.goof.com/qmail-auth-20010105.tar.gz
170
+
171
+This patch available at
172
+http://www.soffian.org/downloads/publicfile-0.52_basicauth.patch
173
+
174
+Jay Soffian <jay@soffian.org> 29 Aug 2001
175
+
176
diff -N -u -r publicfile-0.52.orig/base64.c publicfile-0.52/base64.c
177
--- publicfile-0.52.orig/base64.c	Wed Dec 31 16:00:00 1969
178
+++ publicfile-0.52/base64.c	Wed Aug 22 22:17:39 2001
179
@@ -0,0 +1,90 @@
180
+#include "base64.h"
181
+#include "stralloc.h"
182
+#include "substdio.h"
183
+#include "str.h"
184
+
185
+static char *b64alpha =
186
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
187
+#define B64PAD '='
188
+
189
+/* returns 0 ok, 1 illegal, -1 problem */
190
+
191
+int b64decode(in,l,out)
192
+const unsigned char *in;
193
+int l;
194
+stralloc *out; /* not null terminated */
195
+{
196
+  int i, j;
197
+  unsigned char a[4];
198
+  unsigned char b[3];
199
+  char *s;
200
+
201
+  if (l == 0)
202
+  {
203
+    if (!stralloc_copys(out,"")) return -1;
204
+    return 0;
205
+  }
206
+
207
+  if (!stralloc_ready(out,l + 2)) return -1; /* XXX generous */
208
+  s = out->s;
209
+
210
+  for (i = 0;i < l;i += 4) {
211
+    for (j = 0;j < 4;j++)
212
+      if ((i + j) < l && in[i + j] != B64PAD)
213
+      {
214
+        a[j] = str_chr(b64alpha,in[i + j]);
215
+        if (a[j] > 63) return 1;
216
+      }
217
+      else a[j] = 0;
218
+
219
+    b[0] = (a[0] << 2) | (a[1] >> 4);
220
+    b[1] = (a[1] << 4) | (a[2] >> 2);
221
+    b[2] = (a[2] << 6) | (a[3]);
222
+
223
+    *s++ = b[0];
224
+
225
+    if (in[i + 1] == B64PAD) break;
226
+    *s++ = b[1];
227
+
228
+    if (in[i + 2] == B64PAD) break;
229
+    *s++ = b[2];
230
+  }
231
+  out->len = s - out->s;
232
+  while (out->len && !out->s[out->len - 1]) --out->len; /* XXX avoid? */
233
+  return 0;
234
+}
235
+
236
+int b64encode(in,out)
237
+stralloc *in;
238
+stralloc *out; /* not null terminated */
239
+{
240
+  unsigned char a, b, c;
241
+  int i;
242
+  char *s;
243
+
244
+  if (in->len == 0)
245
+  {
246
+    if (!stralloc_copys(out,"")) return -1;
247
+    return 0;
248
+  }
249
+
250
+  if (!stralloc_ready(out,in->len / 3 * 4 + 4)) return -1;
251
+  s = out->s;
252
+
253
+  for (i = 0;i < in->len;i += 3) {
254
+    a = in->s[i];
255
+    b = i + 1 < in->len ? in->s[i + 1] : 0;
256
+    c = i + 2 < in->len ? in->s[i + 2] : 0;
257
+
258
+    *s++ = b64alpha[a >> 2];
259
+    *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
260
+
261
+    if (i + 1 >= in->len) *s++ = B64PAD;
262
+    else *s++ = b64alpha[((b & 15) << 2) | (c >> 6)];
263
+
264
+    if (i + 2 >= in->len) *s++ = B64PAD;
265
+    else *s++ = b64alpha[c & 63];
266
+  }
267
+  out->len = s - out->s;
268
+  return 0;
269
+}
270
diff -N -u -r publicfile-0.52.orig/base64.h publicfile-0.52/base64.h
271
--- publicfile-0.52.orig/base64.h	Wed Dec 31 16:00:00 1969
272
+++ publicfile-0.52/base64.h	Wed Aug 22 22:17:39 2001
273
@@ -0,0 +1,7 @@
274
+#ifndef BASE64_H
275
+#define BASE64_H
276
+
277
+extern int b64decode();
278
+extern int b64encode();
279
+
280
+#endif
281
diff -N -u -r publicfile-0.52.orig/hier.c publicfile-0.52/hier.c
282
--- publicfile-0.52.orig/hier.c	Mon Nov  8 23:23:46 1999
283
+++ publicfile-0.52/hier.c	Wed Aug 22 22:17:39 2001
284
@@ -7,6 +7,7 @@
285
   d(auto_home,"bin",-1,-1,02755);
286
 
287
   c(auto_home,"bin","configure",-1,-1,0755);
288
+  c(auto_home,"bin","htrules",-1,-1,0755);
289
   c(auto_home,"bin","httpd",-1,-1,0755);
290
   c(auto_home,"bin","ftpd",-1,-1,0755);
291
 }
292
diff -N -u -r publicfile-0.52.orig/htrules.c publicfile-0.52/htrules.c
293
--- publicfile-0.52.orig/htrules.c	Wed Dec 31 16:00:00 1969
294
+++ publicfile-0.52/htrules.c	Wed Aug 29 21:27:42 2001
295
@@ -0,0 +1,117 @@
296
+#include "strerr.h"
297
+#include "stralloc.h"
298
+#include "getln.h"
299
+#include "buffer.h"
300
+#include "exit.h"
301
+#include "fmt.h"
302
+#include "byte.h"
303
+#include "base64.h"
304
+#include "cdb_make.h"
305
+
306
+#define FATAL "htrules: fatal: "
307
+
308
+unsigned long linenum = 0;
309
+char *fntemp;
310
+char *fn;
311
+
312
+stralloc line = {0};
313
+int match = 1;
314
+
315
+stralloc base64 = {0};
316
+stralloc key = {0};
317
+stralloc realm = {0};
318
+stralloc userpass = {0};
319
+
320
+struct cdb_make c;
321
+
322
+void nomem(void)
323
+{
324
+  strerr_die2x(111,FATAL,"out of memory");
325
+}
326
+void usage(void)
327
+{
328
+  strerr_die1x(100,"htrules: usage: htrules access.cdb access.tmp");
329
+}
330
+void die_bad(void)
331
+{
332
+  if (!stralloc_0(&line)) nomem();
333
+  strerr_die3x(100,FATAL,"unable to parse this line: ",line.s);
334
+}
335
+void die_write(void)
336
+{
337
+  strerr_die4sys(111,FATAL,"unable to write to ",fntemp,": ");
338
+}
339
+
340
+main(int argc,char **argv)
341
+{
342
+  int colon;
343
+  char *x;
344
+  int len;
345
+  int fd;
346
+  int i;
347
+  char ch;
348
+
349
+  fn = argv[1];
350
+  if (!fn) usage();
351
+  fntemp = argv[2];
352
+  if (!fntemp) usage();
353
+
354
+  fd = open_trunc(fntemp);
355
+  if (fd == -1)
356
+    strerr_die4sys(111,FATAL,"unable to create ",fntemp,": ");
357
+  if (cdb_make_start(&c,fd) == -1) die_write();
358
+
359
+  while (match) {
360
+    if (getln(buffer_0,&line,&match,'\n') == -1)
361
+      strerr_die2sys(111,FATAL,"unable to read input: ");
362
+
363
+    x = line.s; len = line.len;
364
+
365
+    if (!len) break;
366
+    if (x[0] == '#') continue;
367
+    if (x[0] == '\n') continue;
368
+
369
+    while (len) {
370
+      ch = x[len - 1];
371
+      if (ch != '\n') if (ch != ' ') if (ch != '\t') break;
372
+      --len;
373
+    }
374
+    line.len = len; /* for die_bad() */
375
+    if (!len) continue;
376
+
377
+    colon = byte_chr(x,len,':');
378
+    if (!colon || colon == len) die_bad();
379
+    if (!stralloc_copyb(&realm,x,colon)) nomem();
380
+    x += colon + 1; len -= colon + 1;
381
+
382
+    colon = byte_chr(x,len,':');
383
+    if (colon == len) {
384
+      if (!stralloc_copyb(&key,"C",1)) nomem();
385
+      if (!stralloc_cat(&key,&realm)) nomem();
386
+      if (!stralloc_catb(&key,":",1)) nomem();
387
+      if (!stralloc_catb(&key,x,len)) nomem();
388
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
389
+    } else {
390
+      if (!stralloc_copyb(&userpass,x,len)) nomem();
391
+      if (b64encode(&userpass,&base64) == -1) nomem();
392
+
393
+      if (!stralloc_copyb(&key,"U",1)) nomem();
394
+      if (!stralloc_cat(&key,&base64)) nomem();
395
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
396
+
397
+      if (!stralloc_copyb(&key,"R",1)) nomem();
398
+      if (!stralloc_cat(&key,&realm)) nomem();
399
+      if (!stralloc_catb(&key,":",1)) nomem();
400
+      if (!stralloc_cat(&key,&base64)) nomem();
401
+      if (cdb_make_add(&c,key.s,key.len,"",0) == -1) die_write();
402
+    } 
403
+  }
404
+
405
+  if (cdb_make_finish(&c) == -1) die_write();
406
+  if (fsync(fd) == -1) die_write();
407
+  if (close(fd) == -1) die_write(); /* NFS stupidity */
408
+  if (rename(fntemp,fn))
409
+    strerr_die6sys(111,FATAL,"unable to move ",fntemp," to ",fn,": ");
410
+
411
+  _exit(0);
412
+}
413
diff -N -u -r publicfile-0.52.orig/httpd.c publicfile-0.52/httpd.c
414
--- publicfile-0.52.orig/httpd.c	Mon Nov  8 23:23:46 1999
415
+++ publicfile-0.52/httpd.c	Wed Aug 29 21:30:34 2001
416
@@ -15,6 +15,10 @@
417
 #include "substdio.h"
418
 #include "error.h"
419
 #include "getln.h"
420
+#include "byte.h"
421
+#include "cdb.h"
422
+#include "openreadclose.h"
423
+#include "env.h"
424
 
425
 int safewrite(int fd,char *buf,int len)
426
 {
427
@@ -51,6 +55,7 @@
428
 stralloc host = {0};
429
 stralloc path = {0};
430
 stralloc ims = {0};
431
+stralloc basic_auth = {0};
432
 int flagbody = 1;
433
 
434
 char filebuf[1024];
435
@@ -75,11 +80,16 @@
436
   out_puts("\r\n");
437
 }
438
 
439
-void barf(char *code,char *message)
440
+void barf2(char *code,char *message,char *realm)
441
 {
442
   if (protocolnum > 0) {
443
     tai_now(&now);
444
     header(code,message);
445
+    if(realm) {
446
+      out_puts("WWW-Authenticate: Basic realm=\"");
447
+      out_puts(realm);
448
+      out_puts("\"\r\n");
449
+    }
450
     out_puts("Content-Length: ");
451
     out_put(strnum,fmt_ulong(strnum,str_len(message) + 28));
452
     out_puts("\r\n");
453
@@ -100,8 +110,81 @@
454
   _exit(0);
455
 }
456
 
457
+void barf(char *code,char *message)
458
+{
459
+  barf2(code,message,(char *)0);
460
+}
461
+
462
 stralloc fn = {0};
463
+stralloc accessfn = {0};
464
 stralloc contenttype = {0};
465
+stralloc realm = {0};
466
+stralloc realmtxt = {0};
467
+stralloc key = {0};
468
+
469
+void checkauth(void)
470
+{
471
+  int len;
472
+  int fd;
473
+  int colon;
474
+  static struct cdb c;
475
+  char *x;
476
+ 
477
+  len = byte_rchr(fn.s,fn.len,'/');
478
+  if (!stralloc_copyb(&accessfn,fn.s,len)) _exit(21);
479
+  if (!stralloc_cats(&accessfn,"/.access")) _exit(21);
480
+  if (!stralloc_0(&accessfn)) _exit(21);
481
+
482
+  if (openreadclose(accessfn.s,&realm,256) == 0) return;
483
+  if (!realm.len) _exit(23); /* no realm */
484
+  realm.len = byte_chr(realm.s,realm.len,'\n');
485
+  while (realm.len) {
486
+    if (realm.s[realm.len - 1] != ' ')
487
+      if (realm.s[realm.len - 1] != '\t')
488
+        break;
489
+    --realm.len;
490
+  }
491
+  colon = byte_chr(realm.s,realm.len,':');
492
+  if (!colon) _exit(23); /* no realm */
493
+  if (colon == realm.len) {
494
+    if (!stralloc_copys(&realmtxt,"restricted access")) _exit(21);
495
+  } else {
496
+    if (!stralloc_copyb(&realmtxt,realm.s+colon+1,realm.len-(colon+1))) _exit(21);
497
+    realm.len = colon;
498
+  }
499
+  if (!stralloc_0(&realmtxt)) _exit(21);
500
+
501
+  fd = open_read("/access.cdb");
502
+  if (fd == -1) _exit(23);
503
+  cdb_init(&c,fd);
504
+
505
+  x = env_get("HTTPCLIENT");
506
+  if (x) {
507
+    if (!stralloc_copyb(&key,"C",1)) _exit(21);
508
+    if (!stralloc_cat(&key,&realm)) _exit(21);
509
+    if (!stralloc_catb(&key,":",1)) _exit(21);
510
+    if (!stralloc_cats(&key,x)) _exit(21);
511
+    if (cdb_find(&c,key.s,key.len) == 1) goto AUTH_OK;
512
+  }
513
+
514
+  if (!basic_auth.len) barf2("401 ","Authorization Required", realmtxt.s);
515
+
516
+  if (!stralloc_copyb(&key,"U",1)) _exit(21);
517
+  if (!stralloc_cat(&key,&basic_auth)) _exit(21);
518
+  if (cdb_find(&c,key.s,key.len) != 1)
519
+    barf2("401 ","Authorization Required",realmtxt.s);
520
+
521
+  if (!stralloc_copyb(&key,"R",1)) _exit(21);
522
+  if (!stralloc_cat(&key,&realm)) _exit(21);
523
+  if (!stralloc_catb(&key,":",1)) _exit(21);
524
+  if (!stralloc_cat(&key,&basic_auth)) _exit(21);
525
+  if (cdb_find(&c,key.s,key.len) != 1) barf("403 ","Forbidden");
526
+
527
+  AUTH_OK:
528
+  alloc_free(key);
529
+  cdb_free(&c);
530
+  close(fd);
531
+}
532
 
533
 void get(void)
534
 {
535
@@ -124,6 +207,8 @@
536
   if (!stralloc_cat(&fn,&path)) _exit(21);
537
   pathdecode(&fn);
538
   if (!stralloc_0(&fn)) _exit(21);
539
+  
540
+  checkauth();
541
 
542
   fd = file_open(fn.s,&mtime,&length,1);
543
   if (fd == -1)
544
@@ -227,6 +312,7 @@
545
     if (!stralloc_copys(&path,"")) _exit(21);
546
     if (!stralloc_copys(&protocol,"")) _exit(21);
547
     if (!stralloc_copys(&ims,"")) _exit(21);
548
+    if (!stralloc_copys(&basic_auth,"")) _exit(21);
549
     protocolnum = 2;
550
 
551
     spaces = 0;
552
@@ -302,6 +388,8 @@
553
                     if (!stralloc_append(&host,&field.s[i])) _exit(21);
554
           if (case_startb(field.s,field.len,"if-modified-since:"))
555
 	    if (!stralloc_copyb(&ims,field.s + 18,field.len - 18)) _exit(21);
556
+	  if (case_startb(field.s,field.len,"authorization: basic "))
557
+	    if (!stralloc_copyb(&basic_auth,field.s + 21,field.len - 21)) _exit(21);
558
           field.len = 0;
559
         }
560
         if (!line.len) break;
561
diff -N -u -r publicfile-0.52.orig/openreadclose.c publicfile-0.52/openreadclose.c
562
--- publicfile-0.52.orig/openreadclose.c	Wed Dec 31 16:00:00 1969
563
+++ publicfile-0.52/openreadclose.c	Wed Aug 29 14:24:21 2001
564
@@ -0,0 +1,18 @@
565
+/* Public domain. */
566
+
567
+#include "error.h"
568
+#include "open.h"
569
+#include "readclose.h"
570
+#include "openreadclose.h"
571
+
572
+int openreadclose(const char *fn,stralloc *sa,unsigned int bufsize)
573
+{
574
+  int fd;
575
+  fd = open_read(fn);
576
+  if (fd == -1) {
577
+    if (errno == error_noent) return 0;
578
+    return -1;
579
+  }
580
+  if (readclose(fd,sa,bufsize) == -1) return -1;
581
+  return 1;
582
+}
583
diff -N -u -r publicfile-0.52.orig/openreadclose.h publicfile-0.52/openreadclose.h
584
--- publicfile-0.52.orig/openreadclose.h	Wed Dec 31 16:00:00 1969
585
+++ publicfile-0.52/openreadclose.h	Wed Aug 29 14:24:21 2001
586
@@ -0,0 +1,10 @@
587
+/* Public domain. */
588
+
589
+#ifndef OPENREADCLOSE_H
590
+#define OPENREADCLOSE_H
591
+
592
+#include "stralloc.h"
593
+
594
+extern int openreadclose(const char *,stralloc *,unsigned int);
595
+
596
+#endif
597
diff -N -u -r publicfile-0.52.orig/readclose.c publicfile-0.52/readclose.c
598
--- publicfile-0.52.orig/readclose.c	Wed Dec 31 16:00:00 1969
599
+++ publicfile-0.52/readclose.c	Wed Aug 29 14:30:52 2001
600
@@ -0,0 +1,23 @@
601
+/* Public domain. */
602
+
603
+#include <unistd.h>
604
+#include "error.h"
605
+#include "readclose.h"
606
+
607
+int readclose_append(int fd,stralloc *sa,unsigned int bufsize)
608
+{
609
+  int r;
610
+  for (;;) {
611
+    if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
612
+    r = read(fd,sa->s + sa->len,bufsize);
613
+    if (r == -1) if (errno == error_intr) continue;
614
+    if (r <= 0) { close(fd); return r; }
615
+    sa->len += r;
616
+  }
617
+}
618
+
619
+int readclose(int fd,stralloc *sa,unsigned int bufsize)
620
+{
621
+  if (!stralloc_copys(sa,"")) { close(fd); return -1; }
622
+  return readclose_append(fd,sa,bufsize);
623
+}
624
diff -N -u -r publicfile-0.52.orig/readclose.h publicfile-0.52/readclose.h
625
--- publicfile-0.52.orig/readclose.h	Wed Dec 31 16:00:00 1969
626
+++ publicfile-0.52/readclose.h	Wed Aug 29 14:30:52 2001
627
@@ -0,0 +1,11 @@
628
+/* Public domain. */
629
+
630
+#ifndef READCLOSE_H
631
+#define READCLOSE_H
632
+
633
+#include "stralloc.h"
634
+
635
+extern int readclose_append(int,stralloc *,unsigned int);
636
+extern int readclose(int,stralloc *,unsigned int);
637
+
638
+#endif
(-)publicfile.new/files/publicfile.sslserver (+36 lines)
Line 0 Link Here
1
--- httpd.c.orig	Tue Nov  9 02:23:46 1999
2
+++ httpd.c	Sun Dec  9 21:30:59 2001
3
@@ -271,8 +271,16 @@
4
       if (!stralloc_copyb(&path,host.s + i,host.len - i)) _exit(21);
5
       host.len = i;
6
     }
7
-    else
8
-      if (!stralloc_copy(&path,&url)) _exit(21);
9
+    else {
10
+      if (case_startb(url.s,url.len,"https://")) {
11
+	if (!stralloc_copyb(&host,url.s + 8,url.len - 8)) _exit(21);
12
+	i = byte_chr(host.s,host.len,'/');
13
+	if (!stralloc_copyb(&path,host.s + i,host.len - i)) _exit(21);
14
+	host.len = i;
15
+      }
16
+      else
17
+	if (!stralloc_copy(&path,&url)) _exit(21);
18
+    }
19
 
20
     if (!path.len || (path.s[path.len - 1] == '/'))
21
       if (!stralloc_cats(&path,"index.html")) _exit(21);
22
--- file.c.orig	Wed Dec 12 07:09:57 2001
23
+++ file.c	Wed Dec 12 07:09:33 2001
24
@@ -15,7 +15,11 @@
25
   char *x;
26
 
27
   x = env_get("TCPREMOTEIP");
28
-  if (!x) x = "0";
29
+  if (!x) {
30
+    x = env_get("SSLREMOTEIP");
31
+    if (!x)
32
+      x = "0";
33
+  }
34
   substdio_puts(subfderr,x);
35
   substdio_puts(subfderr,flagread ? " read ": " dir ");
36
 
(-)publicfile.new/files/redirect-slash-patch (+217 lines)
Line 0 Link Here
1
From: Giles Lean <giles@nemeton.com.au>
2
To: publicfile@list.cr.yp.to
3
Subject: redirect patch for publicfile-0.52
4
Date: Sun, 05 Dec 1999 08:43:14 +1100
5
6
------- =_aaaaaaaaaa0
7
Content-Type: text/plain; charset="us-ascii"
8
Content-ID: <23894.944342900.1@nemeton.com.au>
9
10
I have a patch for publicfile-0.52 that will make it send redirects
11
instead of "404 access denied" when directories are requested
12
without a trailing slash.
13
14
Comments and suggestions are welcome, particularly if someone can
15
see where I've either done something wrong protocol-wise or if the
16
integration with Dan's code can be improved.
17
18
This patch is only appropriate for publicfile installations where
19
an existing server is being replaced or where lots of third party
20
HTML is installed without editing.  This is not the target market
21
Dan Bernstein claims for publicfile, but some of us will use it
22
anyway.  (My #1 reason? The low memory requirement.)
23
24
After this patch is applied log messages about "is a directory"
25
will show up if the redirect code is used:
26
27
@4000000038490b1f26a03cac 127.0.0.1 read ./localhost.nemeton.com.au/cr.yp.to/publicfile: is a directory
28
@40000000384973483712ee2c 127.0.0.1 read ./localhost.nemeton.com.au/susv2: is a directory
29
30
Regards,
31
32
Giles
33
34
35
------- =_aaaaaaaaaa0
36
Content-Type: text/plain; name="diff"; charset="us-ascii"
37
Content-ID: <23894.944342900.2@nemeton.com.au>
38
39
Index: error.c
40
===================================================================
41
RCS file: /a/CVS/net/publicfile/error.c,v
42
retrieving revision 1.1.1.1
43
retrieving revision 1.1.1.1.2.1
44
diff -c -r1.1.1.1 -r1.1.1.1.2.1
45
*** error.c	1999/12/01 19:38:15	1.1.1.1
46
--- error.c	1999/12/04 21:15:37	1.1.1.1.2.1
47
***************
48
*** 100,102 ****
49
--- 100,109 ----
50
  #else
51
  -14;
52
  #endif
53
+ 
54
+ int error_isdir =
55
+ #ifdef EISDIR
56
+ EISDIR;
57
+ #else
58
+ -15;
59
+ #endif
60
Index: error.h
61
===================================================================
62
RCS file: /a/CVS/net/publicfile/error.h,v
63
retrieving revision 1.1.1.1
64
retrieving revision 1.1.1.1.2.1
65
diff -c -r1.1.1.1 -r1.1.1.1.2.1
66
*** error.h	1999/12/01 19:38:15	1.1.1.1
67
--- error.h	1999/12/04 21:15:37	1.1.1.1.2.1
68
***************
69
*** 17,22 ****
70
--- 17,23 ----
71
  extern int error_perm;
72
  extern int error_acces;
73
  extern int error_nodevice;
74
+ extern int error_isdir;
75
  
76
  extern char *error_str();
77
  extern int error_temp();
78
Index: error_str.c
79
===================================================================
80
RCS file: /a/CVS/net/publicfile/error_str.c,v
81
retrieving revision 1.1.1.1
82
retrieving revision 1.1.1.1.2.1
83
diff -c -r1.1.1.1 -r1.1.1.1.2.1
84
*** error_str.c	1999/12/01 19:38:15	1.1.1.1
85
--- error_str.c	1999/12/04 21:15:38	1.1.1.1.2.1
86
***************
87
*** 21,26 ****
88
--- 21,27 ----
89
    X(error_perm,"permission denied")
90
    X(error_acces,"access denied")
91
    X(error_nodevice,"device not configured")
92
+   X(error_isdir,"is a directory")
93
  #ifdef ESRCH
94
    X(ESRCH,"no such process")
95
  #endif
96
Index: file.c
97
===================================================================
98
RCS file: /a/CVS/net/publicfile/file.c,v
99
retrieving revision 1.1.1.1
100
retrieving revision 1.1.1.1.2.1
101
diff -c -r1.1.1.1 -r1.1.1.1.2.1
102
*** file.c	1999/12/01 19:38:14	1.1.1.1
103
--- file.c	1999/12/04 21:15:38	1.1.1.1.2.1
104
***************
105
*** 65,77 ****
106
      errno = error_acces;
107
      return -1;
108
    }
109
!   if (flagread)
110
!     if ((st.st_mode & S_IFMT) != S_IFREG) {
111
        log(fn,": ","not a regular file",flagread);
112
        close(fd);
113
        errno = error_acces;
114
-       return -1;
115
      }
116
  
117
    log(fn,": ","success",flagread);
118
  
119
--- 65,83 ----
120
      errno = error_acces;
121
      return -1;
122
    }
123
!   if (flagread && (st.st_mode & S_IFMT) != S_IFREG) {
124
!     if ((st.st_mode & S_IFMT) == S_IFDIR) {
125
!       log(fn,": ",error_str(error_isdir),flagread);
126
!       close(fd);
127
!       errno = error_isdir;
128
!     }
129
!     else {
130
        log(fn,": ","not a regular file",flagread);
131
        close(fd);
132
        errno = error_acces;
133
      }
134
+     return -1;
135
+   }
136
  
137
    log(fn,": ","success",flagread);
138
  
139
Index: httpd.c
140
===================================================================
141
RCS file: /a/CVS/net/publicfile/httpd.c,v
142
retrieving revision 1.1.1.1
143
retrieving revision 1.1.1.1.2.1
144
diff -c -r1.1.1.1 -r1.1.1.1.2.1
145
*** httpd.c	1999/12/01 19:38:14	1.1.1.1
146
--- httpd.c	1999/12/04 21:15:38	1.1.1.1.2.1
147
***************
148
*** 103,114 ****
149
--- 103,142 ----
150
  stralloc fn = {0};
151
  stralloc contenttype = {0};
152
  
153
+ void redirect(void)
154
+ {
155
+   if (!stralloc_cats(&url, "/")) _exit(21);
156
+   if (protocolnum > 0) {
157
+     tai_now(&now);
158
+     header("301 ", "Moved Permanently");
159
+     out_puts("Content-Length: ");
160
+     out_put(strnum,fmt_ulong(strnum,url.len * 2 + 127));
161
+     out_puts("\r\nLocation: ");
162
+     out_put(url.s, url.len);
163
+     out_puts("\r\nContent-Type: text/html\r\n\r\n");
164
+   }
165
+   if (flagbody) {
166
+     out_puts("<html><body>The document you requested is a directory.  Try adding a trailing slash to the URL: <A HREF=\"");
167
+     out_put(url.s,url.len);
168
+     out_puts("\">");
169
+     out_put(url.s,url.len);
170
+     out_puts("</A></body></html>\r\n");
171
+   }
172
+   out_flush();
173
+ }
174
+ 
175
  void get(void)
176
  {
177
    unsigned long length;
178
    int fd;
179
    int r;
180
  
181
+   if (!case_startb(url.s,url.len,"http://")) {
182
+     if (!stralloc_copys(&url,"http://")) _exit(21);
183
+     if (!stralloc_cat(&url,&host)) _exit(21);
184
+     if (!stralloc_cat(&url,&path)) _exit(21);
185
+   }
186
+ 
187
    host.len = byte_chr(host.s,host.len,':');
188
    if (!host.len) {
189
      if (protocolnum > 1)
190
***************
191
*** 126,133 ****
192
    if (!stralloc_0(&fn)) _exit(21);
193
  
194
    fd = file_open(fn.s,&mtime,&length,1);
195
!   if (fd == -1)
196
      barf("404 ",error_str(errno));
197
  
198
    if (protocolnum > 0) {
199
      tai_now(&now);
200
--- 154,167 ----
201
    if (!stralloc_0(&fn)) _exit(21);
202
  
203
    fd = file_open(fn.s,&mtime,&length,1);
204
!   if (fd == -1) {
205
!     if (errno == error_isdir) {
206
!       redirect();
207
!       if (protocolnum < 2) _exit(0);
208
!       return;
209
!     }
210
      barf("404 ",error_str(errno));
211
+   }
212
  
213
    if (protocolnum > 0) {
214
      tai_now(&now);
215
216
------- =_aaaaaaaaaa0--
217
(-)publicfile.new/pkg-plist (+1 lines)
Lines 1-3 Link Here
1
bin/configure
1
bin/configure
2
bin/httpd
2
bin/httpd
3
bin/ftpd
3
bin/ftpd
4
%%BASICAUTH%%bin/htrules

Return to bug 76556