3 * Copyright (C) 2005 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Elementary WWW server (port 80)
24 * Author: Simone Piccardi
27 * Usage: wwwd -h give all info
31 ****************************************************************/
33 * Include needed headers
36 #include <string.h> /* error strings */
37 #include <sys/types.h> /* predefined types */
38 #include <unistd.h> /* include unix standard library */
39 #include <arpa/inet.h> /* IP addresses conversion utiliites */
40 #include <sys/socket.h> /* socket library */
41 #include <stdio.h> /* include standard I/O library */
43 #include <syslog.h> /* syslog system functions */
44 #include <signal.h> /* signal functions */
45 #include <errno.h> /* error code */
52 int demonize = 1; /* daemon use option: default is daemon */
53 int debugging = 0; /* debug info printing option: default is no debug */
55 /* Subroutines declaration */
57 void ServPage(int sockfd);
58 void PrintErr(char * error);
59 void print_headers(FILE *file);
60 void print_err404(FILE *file, char *filename);
61 void print_err400(FILE *file, char *string);
62 void print_err500(FILE *file, char *string);
64 /* Program beginning */
65 int main(int argc, char *argv[])
68 * Variables definition
76 struct sockaddr_in cli_add;
78 char debug[MAXLINE], ipaddr[20];
80 * Input section: decode parameters passed in the calling
84 opterr = 0; /* don't want writing to stderr */
85 while ( (i = getopt(argc, argv, "hdicw:r:")) != -1) {
91 printf("Wrong -h option use\n");
105 waiting = strtol(optarg, NULL, 10);
112 case '?': /* unrecognized options */
113 printf("Unrecognized options -%c\n",optopt);
115 default: /* should not reached */
119 /* ***********************************************************
121 * Options processing completed
123 * Main code beginning
125 * ***********************************************************/
126 /* Main code begin here */
127 if (compat) { /* install signal handler */
128 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
130 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
132 /* create and bind socket */
133 if ( (list_fd = sockbind2(argv[optind], "www", 6, SOCK_STREAM)) < 0) {
136 /* chroot if requested */
138 printf("chroot to %s\n", rootdir);
139 if (chdir(rootdir)) {
140 perror("Cannot find directory to chroot");
143 if (chroot(rootdir)) {
144 perror("Cannot chroot");
148 /* release privileges and go daemon */
149 if (setgid(65534) !=0) { /* first give away group privileges */
150 perror("cannot give away group privileges");
153 if (setuid(65534) !=0) { /* and only after user ... */
154 perror("cannot give away user privileges");
157 if (demonize) { /* go daemon */
158 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
159 if (daemon(0, 0) != 0) {
160 perror("cannot start as daemon");
165 if (listen(list_fd, BACKLOG) < 0 ) {
166 PrintErr("listen error");
169 if (waiting) sleep(waiting);
170 /* handle echo to client */
172 /* accept connection */
173 len = sizeof(cli_add);
174 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
175 < 0) && (errno == EINTR));
177 PrintErr("accept error");
181 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
182 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
184 syslog(LOG_DEBUG, debug);
189 /* fork to handle connection */
190 if ( (pid = fork()) < 0 ){
191 PrintErr("fork error");
194 if (pid == 0) { /* child */
195 close(list_fd); /* close listening socket */
196 ServPage(conn_fd); /* handle echo */
198 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
200 syslog(LOG_DEBUG, debug);
206 } else { /* parent */
207 close(conn_fd); /* close connected socket */
210 /* normal exit, never reached */
214 * routine to print usage info and exit
218 printf("Elementary echo server\n");
220 printf(" echod [-h] \n");
221 printf(" -h print this help\n");
222 printf(" -d write debug info\n");
223 printf(" -i use interactively\n");
224 printf(" -c disable BSD semantics\n");
225 printf(" -w N wait N sec. before calling accept\n");
229 * routine to handle echo for connection
231 void ServPage(int sockfd)
233 char buffer[MAXLINE];
236 char *line, *copy, *method, *ptr, *filename, *version;
237 char *methods[] = { "GET", "PUT", NULL };
242 "500 Internal Server Error",
248 sock = fdopen(sockfd, "w+");
249 /* main loop, reading 0 char means client close connection */
250 line = fgets(buffer, MAXLINE, sock);
252 PrintErr("Errore in lettura");
255 /* parsing first line, getting method and filename */
256 copy = strndupa(line, MAXLINE);
257 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
258 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
260 print_err400(sock, line);
263 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
264 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
266 print_err400(sock, line);
269 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
270 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
272 print_err400(sock, line);
276 while ( (ptr = methods[i]) != NULL) {
277 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
283 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
285 print_err400(sock, line);
289 while (strcmp(line,"\r\n")) {
290 line = fgets(buffer, MAXLINE, sock);
293 if ( (file = fopen(filename, "r")) == NULL) {
294 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
296 print_err404(sock, filename);
299 fprintf(sock, "HTTP/1.0 %s\n", codes[0]);
303 while (!feof(file)) {
304 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
306 fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
308 snprintf(buffer, MAXLINE, "reading %s", filename);
309 print_err500(sock, buffer);
313 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
315 fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
317 snprintf(buffer, MAXLINE, "writing");
318 print_err500(sock, buffer);
328 * routine to print error on stout or syslog
330 void PrintErr(char * error)
332 if (demonize) { /* daemon mode */
333 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
340 void print_headers(FILE *file)
345 fprintf(file, "Date: %s", ctime(&tempo));
346 fprintf(file, "Server: WWWd test server\n");
347 fprintf(file, "Connection: close\n");
348 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
353 void print_err404(FILE *file, char *filename)
355 fprintf(file, "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
356 fprintf(file, "<BODY><H1>Not Found</H1>\nThe requested ");
357 fprintf(file, "URL %s was not found on this server.<P><HR>", filename);
358 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi</ADDRESS></BODY></HTML>");
362 void print_err400(FILE *file, char *string)
364 fprintf(file, "<HTML><HEAD><TITLE>404 Bad Request</TITLE></HEAD>\n");
365 fprintf(file, "<BODY><H1>Bad Request</H1>\n ");
366 fprintf(file, "Your browser sent a request that this server could not ");
367 fprintf(file, "understand.<P>The request line<P> %s <P>", string);
368 fprintf(file, "is invalid following the protocol<p><HR>");
369 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi </ADDRESS></BODY></HTML>");
374 void print_err500(FILE *file, char *string)
376 fprintf(file, "<HTML><HEAD><TITLE>500 Internal Server Error</TITLE>\n");
377 fprintf(file, "</HEAD><BODY><H1>Internal Server Error</H1>\n ");
378 fprintf(file, "We got an error processing your request.<P>");
379 fprintf(file, "Error is: %s <P><HR>\n", string);
380 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi </ADDRESS></BODY></HTML>");