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
29 ****************************************************************/
31 * Include needed headers
34 #include <sys/types.h> /* primitive system data types */
35 #include <sys/stat.h> /* file characteristics constants and functions */
36 #include <unistd.h> /* unix standard library */
37 #include <stdlib.h> /* C standard library */
38 #include <arpa/inet.h> /* IP addresses conversion utilities */
39 #include <sys/socket.h> /* socket constants, types and functions */
40 #include <stdio.h> /* standard I/O library */
41 #include <time.h> /* date and time constants, types and functions */
42 #include <syslog.h> /* syslog system functions */
43 #include <signal.h> /* signal constants, types and functions */
44 #include <errno.h> /* error definitions and routines */
45 #include <string.h> /* C strings library */
51 int demonize = 1; /* daemon use option: default is daemon */
52 int debugging = 0; /* debug info printing option: default is no debug */
54 /* Subroutines declaration */
62 void ServPage(int sockfd);
63 void PrintErr(char * error);
64 void print_headers(FILE *file, struct code_page code);
65 void print_error(FILE *file, struct code_page page, char * string);
67 /* Program beginning */
68 int main(int argc, char *argv[])
71 * Variables definition
79 struct sockaddr_in cli_add;
81 char debug[MAXLINE], ipaddr[20];
83 * Input section: decode parameters passed in the calling
87 opterr = 0; /* don't want writing to stderr */
88 while ( (i = getopt(argc, argv, "hwdicr:")) != -1) {
94 printf("Wrong -h option use\n");
114 case '?': /* unrecognized options */
115 printf("Unrecognized options -%c\n",optopt);
117 default: /* should not reached */
121 /* ***********************************************************
123 * Options processing completed
125 * Main code beginning
127 * ***********************************************************/
128 /* Main code begin here */
129 if (compat) { /* install signal handler */
130 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
132 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
134 /* create and bind socket */
135 if ( (list_fd = sockbindopt(argv[optind], "www", 6,
136 SOCK_STREAM, reuse)) < 0) {
139 /* chroot if requested */
141 if (chdir(rootdir)) {
142 perror("Cannot find directory to chroot");
145 if (chroot(rootdir)) {
146 perror("Cannot chroot");
150 /* release privileges and go daemon */
151 if (setgid(65534) !=0) { /* first give away group privileges */
152 perror("cannot give away group privileges");
155 if (setuid(65534) !=0) { /* and only after user ... */
156 perror("cannot give away user privileges");
159 if (demonize) { /* go daemon */
160 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
161 if (daemon(0, 0) != 0) {
162 perror("cannot start as daemon");
167 if (listen(list_fd, BACKLOG) < 0 ) {
168 PrintErr("listen error");
171 /* handle echo to client */
173 /* accept connection */
174 len = sizeof(cli_add);
175 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
176 < 0) && (errno == EINTR));
178 PrintErr("accept error");
182 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
183 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
185 syslog(LOG_DEBUG, debug);
190 /* fork to handle connection */
191 if ( (pid = fork()) < 0 ){
192 PrintErr("fork error");
195 if (pid == 0) { /* child */
196 close(list_fd); /* close listening socket */
197 ServPage(conn_fd); /* handle echo */
199 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
201 syslog(LOG_DEBUG, debug);
207 } else { /* parent */
208 close(conn_fd); /* close connected socket */
211 /* normal exit, never reached */
215 * routine to print usage info and exit
219 printf("Elementary echo server\n");
221 printf(" wwwd [-h] \n");
222 printf(" -h print this help\n");
223 printf(" -d write debug info\n");
224 printf(" -i use interactively\n");
225 printf(" -c disable BSD semantics\n");
226 printf(" -r /path chroot on /path\n");
230 * routine to handle echo for connection
232 void ServPage(int sockfd)
234 char buffer[MAXLINE];
237 char *line, *copy, *method, *ptr, *filename, *version;
238 char *methods[] = { "GET", "HEAD", NULL };
239 struct code_page codes[] = {
240 { "200", "OK", "%s"},
241 { "400", "Bad Request",
242 "Your browser sent a request that this server could not understand."
243 "<P>The request line<P>%s<P> is invalid following the protocol<P>"},
244 { "404", "Not Found",
245 "The requested URL %s was not found on this server.<P>"},
246 { "500", "Internal Server Error",
247 "We got an error processing your request.<P>Error is: %s<P>"},
248 { "405", "Method Not Allowed", "Method %s not allowed.<P>"},
249 { "403", "Forbidden", "You cannot access %s.<P>"}
254 sock = fdopen(sockfd, "w+");
255 /* main loop, reading 0 char means client close connection */
256 line = fgets(buffer, MAXLINE, sock);
258 PrintErr("Errore in lettura");
261 /* parsing first line, getting method and filename */
262 copy = strndupa(line, MAXLINE);
263 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
264 print_headers(sock, codes[1]);
265 print_error(sock, codes[1], line);
268 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
269 print_headers(sock, codes[1]);
270 print_error(sock, codes[1], line);
273 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
274 print_headers(sock, codes[1]);
275 print_error(sock, codes[1], line);
279 while ( (ptr = methods[i]) != NULL) {
280 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
286 print_headers(sock, codes[4]);
287 print_error(sock, codes[4], method);
291 while (strcmp(line,"\r\n")) {
292 line = fgets(buffer, MAXLINE, sock);
295 if ( (file = fopen(filename, "r")) == NULL) {
296 if ( (errno == EACCES)||(errno == EPERM) ) {
297 print_headers(sock, codes[5]);
298 print_error(sock, codes[5], filename);
300 print_headers(sock, codes[2]);
301 print_error(sock, codes[2], filename);
305 print_headers(sock, codes[0]);
306 while (!feof(file)) {
307 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
309 strncpy(buffer, strerror(errno), MAXLINE);
310 print_headers(sock, codes[3]);
311 print_error(sock, codes[3], buffer);
315 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
317 strncpy(buffer, strerror(errno), MAXLINE);
318 print_headers(sock, codes[3]);
319 print_error(sock, codes[3], buffer);
329 * routine to print error on stout or syslog
331 void PrintErr(char * error)
333 if (demonize) { /* daemon mode */
334 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
341 void print_headers(FILE *file, struct code_page code)
345 fprintf(file, "HTTP/1.0 %s %s \n", code.code, code.name);
347 fprintf(file, "Date: %s", ctime(&tempo));
348 fprintf(file, "Server: WWWd test server\n");
349 fprintf(file, "Connection: close\n");
350 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
355 void print_error(FILE *file, struct code_page page, char * string)
357 fprintf(file, "<HTML><HEAD><TITLE>%s %s</TITLE></HEAD>\n",
358 page.code, page.name);
359 fprintf(file, "<BODY><H1>%s</H1>\n", page.name);
360 fprintf(file, page.body, string);
361 fprintf(file, "<HR><ADDRESS>WWWd by S. Piccardi</ADDRESS></BODY></HTML>");