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
75 struct sockaddr_in cli_add;
77 char debug[MAXLINE], ipaddr[20];
79 * Input section: decode parameters passed in the calling
83 opterr = 0; /* don't want writing to stderr */
84 while ( (i = getopt(argc, argv, "hdicw:r:")) != -1) {
90 printf("Wrong -h option use\n");
107 case '?': /* unrecognized options */
108 printf("Unrecognized options -%c\n",optopt);
110 default: /* should not reached */
114 /* ***********************************************************
116 * Options processing completed
118 * Main code beginning
120 * ***********************************************************/
121 /* Main code begin here */
122 if (compat) { /* install signal handler */
123 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
125 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
127 /* create and bind socket */
128 if ( (list_fd = sockbind2(argv[optind], "www", 6, SOCK_STREAM)) < 0) {
131 /* chroot if requested */
133 if (chdir(rootdir)) {
134 perror("Cannot find directory to chroot");
137 if (chroot(rootdir)) {
138 perror("Cannot chroot");
142 /* release privileges and go daemon */
143 if (setgid(65534) !=0) { /* first give away group privileges */
144 perror("cannot give away group privileges");
147 if (setuid(65534) !=0) { /* and only after user ... */
148 perror("cannot give away user privileges");
151 if (demonize) { /* go daemon */
152 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
153 if (daemon(0, 0) != 0) {
154 perror("cannot start as daemon");
159 if (listen(list_fd, BACKLOG) < 0 ) {
160 PrintErr("listen error");
163 /* handle echo to client */
165 /* accept connection */
166 len = sizeof(cli_add);
167 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
168 < 0) && (errno == EINTR));
170 PrintErr("accept error");
174 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
175 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
177 syslog(LOG_DEBUG, debug);
182 /* fork to handle connection */
183 if ( (pid = fork()) < 0 ){
184 PrintErr("fork error");
187 if (pid == 0) { /* child */
188 close(list_fd); /* close listening socket */
189 ServPage(conn_fd); /* handle echo */
191 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
193 syslog(LOG_DEBUG, debug);
199 } else { /* parent */
200 close(conn_fd); /* close connected socket */
203 /* normal exit, never reached */
207 * routine to print usage info and exit
211 printf("Elementary echo server\n");
213 printf(" echod [-h] \n");
214 printf(" -h print this help\n");
215 printf(" -d write debug info\n");
216 printf(" -i use interactively\n");
217 printf(" -c disable BSD semantics\n");
218 printf(" -w N wait N sec. before calling accept\n");
222 * routine to handle echo for connection
224 void ServPage(int sockfd)
226 char buffer[MAXLINE];
229 char *line, *copy, *method, *ptr, *filename, *version;
230 char *methods[] = { "GET", "PUT", NULL };
235 "500 Internal Server Error",
241 sock = fdopen(sockfd, "w+");
242 /* main loop, reading 0 char means client close connection */
243 line = fgets(buffer, MAXLINE, sock);
245 PrintErr("Errore in lettura");
248 /* parsing first line, getting method and filename */
249 copy = strndupa(line, MAXLINE);
250 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
251 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
253 print_err400(sock, line);
256 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
257 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
259 print_err400(sock, line);
262 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
263 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
265 print_err400(sock, line);
269 while ( (ptr = methods[i]) != NULL) {
270 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
276 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
278 print_err400(sock, line);
282 while (strcmp(line,"\r\n")) {
283 line = fgets(buffer, MAXLINE, sock);
286 if ( (file = fopen(filename, "r")) == NULL) {
287 fprintf(sock, "HTTP/1.0 %s\n", codes[2]);
289 print_err404(sock, filename);
292 fprintf(sock, "HTTP/1.0 %s\n", codes[0]);
296 while (!feof(file)) {
297 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
299 fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
301 snprintf(buffer, MAXLINE, "reading %s", filename);
302 print_err500(sock, buffer);
306 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
308 fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
310 snprintf(buffer, MAXLINE, "writing");
311 print_err500(sock, buffer);
321 * routine to print error on stout or syslog
323 void PrintErr(char * error)
325 if (demonize) { /* daemon mode */
326 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
333 void print_headers(FILE *file)
338 fprintf(file, "Date: %s", ctime(&tempo));
339 fprintf(file, "Server: WWWd test server\n");
340 fprintf(file, "Connection: close\n");
341 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
346 void print_err404(FILE *file, char *filename)
348 fprintf(file, "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
349 fprintf(file, "<BODY><H1>Not Found</H1>\nThe requested ");
350 fprintf(file, "URL %s was not found on this server.<P><HR>", filename);
351 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi</ADDRESS></BODY></HTML>");
355 void print_err400(FILE *file, char *string)
357 fprintf(file, "<HTML><HEAD><TITLE>404 Bad Request</TITLE></HEAD>\n");
358 fprintf(file, "<BODY><H1>Bad Request</H1>\n ");
359 fprintf(file, "Your browser sent a request that this server could not ");
360 fprintf(file, "understand.<P>The request line<P> %s <P>", string);
361 fprintf(file, "is invalid following the protocol<p><HR>");
362 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi </ADDRESS></BODY></HTML>");
367 void print_err500(FILE *file, char *string)
369 fprintf(file, "<HTML><HEAD><TITLE>500 Internal Server Error</TITLE>\n");
370 fprintf(file, "</HEAD><BODY><H1>Internal Server Error</H1>\n ");
371 fprintf(file, "We got an error processing your request.<P>");
372 fprintf(file, "Error is: %s <P><HR>\n", string);
373 fprintf(file, "<ADDRESS>WWWd by Simone Piccardi </ADDRESS></BODY></HTML>");