3 * httpd A Server implementing the HTTP protocol.
5 * usage: tcpd http httpd &
7 * 02/17/1996 Michael Temari <Michael@TemWare.Com>
8 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
9 * 12/29/2002 Michael Temari <Michael@TemWare.Com>
10 * 07/04/2003 Al Woodhull <awoodhull@hampshire.edu>
15 #include <sys/types.h>
25 FILE *stdlog
= (FILE *)NULL
;
26 FILE *dbglog
= (FILE *)NULL
;
30 _PROTOTYPE(int main
, (int argc
, char *argv
[]));
32 struct http_request request
;
33 struct http_reply reply
;
41 char *cfg
= (char *)NULL
;
45 strcpy(umsg
, "Usage: ");
46 strcat(umsg
, argv
[0]);
47 strcat(umsg
, " [-t|v] [config_file]\n");
49 /* parse program name */
50 prog
= strrchr(*argv
, '/');
51 if(prog
== (char *)NULL
)
60 if(argv
[0][0] == '-') {
66 case 'v' : fprintf(stderr
, VERSION
"\n");
69 default : fprintf(stderr
, VERSION
"\n");
70 fprintf(stderr
, umsg
);
75 /* Did they specify an alternate configuration file? */
81 /* Read the configuration settings */
82 if(readconfig(cfg
, opt_t
)) {
83 fprintf(stderr
, "httpd: Error reading configuration file.\n");
87 /* Option t is to test configuration only */
91 /* Open log file for append if it exists */
93 if((stdlog
= fopen(LogFile
, "r")) != (FILE *)NULL
) {
95 stdlog
= fopen(LogFile
, "a");
98 /* Open debug log file for append if it exists */
100 if((dbglog
= fopen(DbgFile
, "r")) != (FILE *)NULL
) {
102 dbglog
= fopen(DbgFile
, "a");
106 /* Get some network information */
110 /* If user defined then prepare to secure as user given */
112 if((pwd
= getpwnam(User
)) == (struct passwd
*)NULL
) {
113 fprintf(stderr
, "httpd: unable to find user %s\n", User
);
117 /* If Chroot defined then secure even more by doing a chroot */
120 fprintf(stderr
, "httpd: unable to chroot\n");
124 fprintf(stderr
, "httpd: unable to chroot\n");
129 /* If user defined then secure as user given */
131 if(setgid(pwd
->pw_gid
) || setuid(pwd
->pw_uid
)) {
132 fprintf(stderr
, "httpd: unable to set user\n");
138 if (strncmp(prog
, "in.", 3) != 0) {
139 /* Does not start with "in.", so not started from inetd/tcpd. */
140 /* XXX - Port name/number should be a config file option. */
145 /* Get some network information */
148 /* log a connection */
149 if(dbglog
!= (FILE *)NULL
) {
150 fprintf(dbglog
, "CONNECT: %d %s %s\n", getpid(),
151 rmthostname
, logdate((time_t *)NULL
));
155 /* loop getting, processing and replying to requests */
156 while(!(s
= getrequest(&request
))) {
157 if(processrequest(&request
, &reply
)) break;
158 if(stdlog
!= (FILE *)NULL
) {
159 fprintf(stdlog
, "%s %s %d %d %s\n",
160 logdate((time_t *)NULL
), rmthostname
,
161 request
.method
, reply
.status
, request
.url
);
164 if(sendreply(&reply
, &request
)) break;
165 if(!reply
.keepopen
) break;
167 if(s
== 1 && stdlog
!= (FILE *)NULL
) {
168 fprintf(stdlog
, "%s %s %d %d %s\n",
169 logdate((time_t *)NULL
), rmthostname
,
170 request
.method
, 999, request
.url
);