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 */
63 void ServPage(int sockfd);
64 void PrintErr(char * error);
65 void print_headers(FILE *file, struct code_page code);
66 void print_error(FILE *file, struct code_page page, char * string);
68 /* Program beginning */
69 int main(int argc, char *argv[])
72 * 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, "hdicr:")) != -1) {
94 printf("Wrong -h option use\n");
111 case '?': /* unrecognized options */
112 printf("Unrecognized options -%c\n",optopt);
114 default: /* should not reached */
118 /* ***********************************************************
120 * Options processing completed
122 * Main code beginning
124 * ***********************************************************/
125 /* Main code begin here */
126 if (compat) { /* install signal handler */
127 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
129 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
131 /* create and bind socket */
132 if ( (list_fd = sockbind2(argv[optind], "www", 6, SOCK_STREAM)) < 0) {
135 /* chroot if requested */
137 if (chdir(rootdir)) {
138 perror("Cannot find directory to chroot");
141 if (chroot(rootdir)) {
142 perror("Cannot chroot");
146 /* release privileges and go daemon */
147 if (setgid(65534) !=0) { /* first give away group privileges */
148 perror("cannot give away group privileges");
151 if (setuid(65534) !=0) { /* and only after user ... */
152 perror("cannot give away user privileges");
155 if (demonize) { /* go daemon */
156 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
157 if (daemon(0, 0) != 0) {
158 perror("cannot start as daemon");
163 if (listen(list_fd, BACKLOG) < 0 ) {
164 PrintErr("listen error");
167 /* handle echo to client */
169 /* accept connection */
170 len = sizeof(cli_add);
171 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
172 < 0) && (errno == EINTR));
174 PrintErr("accept error");
178 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
179 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
181 syslog(LOG_DEBUG, debug);
186 /* fork to handle connection */
187 if ( (pid = fork()) < 0 ){
188 PrintErr("fork error");
191 if (pid == 0) { /* child */
192 close(list_fd); /* close listening socket */
193 ServPage(conn_fd); /* handle echo */
195 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
197 syslog(LOG_DEBUG, debug);
203 } else { /* parent */
204 close(conn_fd); /* close connected socket */
207 /* normal exit, never reached */
211 * routine to print usage info and exit
215 printf("Elementary echo server\n");
217 printf(" echod [-h] \n");
218 printf(" -h print this help\n");
219 printf(" -d write debug info\n");
220 printf(" -i use interactively\n");
221 printf(" -c disable BSD semantics\n");
222 printf(" -r /path chroot on /path\n");
226 * routine to handle echo for connection
228 void ServPage(int sockfd)
230 char buffer[MAXLINE];
233 char *line, *copy, *method, *ptr, *filename, *version;
234 char *methods[] = { "GET", "HEAD", NULL };
235 struct code_page codes[] = {
236 { "200", "OK", "%s"},
237 { "400", "Bad Request",
238 "Your browser sent a request that this server could not understand."
239 "<P>The request line<P>%s<P> is invalid following the protocol<P>"},
240 { "404", "Not Found",
241 "The requested URL %s was not found on this server.<P>"},
242 { "500", "Internal Server Error",
243 "We got an error processing your request.<P>Error is: %s<P>"},
244 { "405", "Method Not Allowed", "Method %s not allowed.<P>"},
245 { "403", "Forbidden", "You cannot access %s.<P>"}
250 sock = fdopen(sockfd, "w+");
251 /* main loop, reading 0 char means client close connection */
252 line = fgets(buffer, MAXLINE, sock);
254 PrintErr("Errore in lettura");
257 /* parsing first line, getting method and filename */
258 copy = strndupa(line, MAXLINE);
259 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
260 print_headers(sock, codes[1]);
261 print_error(sock, codes[1], line);
264 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
265 print_headers(sock, codes[1]);
266 print_error(sock, codes[1], line);
269 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
270 print_headers(sock, codes[1]);
271 print_error(sock, codes[1], line);
275 while ( (ptr = methods[i]) != NULL) {
276 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
282 print_headers(sock, codes[4]);
283 print_error(sock, codes[4], method);
287 while (strcmp(line,"\r\n")) {
288 line = fgets(buffer, MAXLINE, sock);
291 if ( (file = fopen(filename, "r")) == NULL) {
292 if ( (errno == EACCES)||(errno == EPERM) ) {
293 print_headers(sock, codes[5]);
294 print_error(sock, codes[5], filename);
296 print_headers(sock, codes[2]);
297 print_error(sock, codes[2], filename);
301 print_headers(sock, codes[0]);
302 while (!feof(file)) {
303 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
305 strncpy(buffer, strerror(errno), MAXLINE);
306 print_headers(sock, codes[3]);
307 print_error(sock, codes[3], buffer);
311 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
313 strncpy(buffer, strerror(errno), MAXLINE);
314 print_headers(sock, codes[3]);
315 print_error(sock, codes[3], buffer);
325 * routine to print error on stout or syslog
327 void PrintErr(char * error)
329 if (demonize) { /* daemon mode */
330 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
337 void print_headers(FILE *file, struct code_page code)
341 fprintf(file, "HTTP/1.0 %s %s \n", code.code, code.name);
343 fprintf(file, "Date: %s", ctime(&tempo));
344 fprintf(file, "Server: WWWd test server\n");
345 fprintf(file, "Connection: close\n");
346 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
351 void print_error(FILE *file, struct code_page page, char * string)
353 fprintf(file, "<HTML><HEAD><TITLE>%s %s</TITLE></HEAD>\n",
354 page.code, page.name);
355 fprintf(file, "<BODY><H1>%s</H1>\n", page.name);
356 fprintf(file, page.body, string);
357 fprintf(file, "<HR><ADDRESS>WWWd by S. Piccardi</ADDRESS></BODY></HTML>");