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
80 struct sockaddr_in cli_add;
82 char debug[MAXLINE], ipaddr[20];
84 * Input section: decode parameters passed in the calling
88 opterr = 0; /* don't want writing to stderr */
89 while ( (i = getopt(argc, argv, "hwdicr:")) != -1) {
95 printf("Wrong -h option use\n");
115 case '?': /* unrecognized options */
116 printf("Unrecognized options -%c\n",optopt);
118 default: /* should not reached */
122 /* ***********************************************************
124 * Options processing completed
126 * Main code beginning
128 * ***********************************************************/
129 /* Main code begin here */
130 if (compat) { /* install signal handler */
131 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
133 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
135 /* create and bind socket */
136 if ( (list_fd = sockbindopt(argv[optind], "www", 6,
137 SOCK_STREAM, reuse)) < 0) {
140 /* chroot if requested */
142 if (chdir(rootdir)) {
143 perror("Cannot find directory to chroot");
146 if (chroot(rootdir)) {
147 perror("Cannot chroot");
151 /* release privileges and go daemon */
152 if (setgid(65534) !=0) { /* first give away group privileges */
153 perror("cannot give away group privileges");
156 if (setuid(65534) !=0) { /* and only after user ... */
157 perror("cannot give away user privileges");
160 if (demonize) { /* go daemon */
161 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
162 if (daemon(0, 0) != 0) {
163 perror("cannot start as daemon");
168 if (listen(list_fd, BACKLOG) < 0 ) {
169 PrintErr("listen error");
172 /* handle echo to client */
174 /* accept connection */
175 len = sizeof(cli_add);
176 while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len))
177 < 0) && (errno == EINTR));
179 PrintErr("accept error");
183 inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
184 snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
186 syslog(LOG_DEBUG, debug);
191 /* fork to handle connection */
192 if ( (pid = fork()) < 0 ){
193 PrintErr("fork error");
196 if (pid == 0) { /* child */
197 close(list_fd); /* close listening socket */
198 ServPage(conn_fd); /* handle echo */
200 snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr);
202 syslog(LOG_DEBUG, debug);
208 } else { /* parent */
209 close(conn_fd); /* close connected socket */
212 /* normal exit, never reached */
216 * routine to print usage info and exit
220 printf("Elementary echo server\n");
222 printf(" echod [-h] \n");
223 printf(" -h print this help\n");
224 printf(" -d write debug info\n");
225 printf(" -i use interactively\n");
226 printf(" -c disable BSD semantics\n");
227 printf(" -r /path chroot on /path\n");
231 * routine to handle echo for connection
233 void ServPage(int sockfd)
235 char buffer[MAXLINE];
238 char *line, *copy, *method, *ptr, *filename, *version;
239 char *methods[] = { "GET", "HEAD", NULL };
240 struct code_page codes[] = {
241 { "200", "OK", "%s"},
242 { "400", "Bad Request",
243 "Your browser sent a request that this server could not understand."
244 "<P>The request line<P>%s<P> is invalid following the protocol<P>"},
245 { "404", "Not Found",
246 "The requested URL %s was not found on this server.<P>"},
247 { "500", "Internal Server Error",
248 "We got an error processing your request.<P>Error is: %s<P>"},
249 { "405", "Method Not Allowed", "Method %s not allowed.<P>"},
250 { "403", "Forbidden", "You cannot access %s.<P>"}
255 sock = fdopen(sockfd, "w+");
256 /* main loop, reading 0 char means client close connection */
257 line = fgets(buffer, MAXLINE, sock);
259 PrintErr("Errore in lettura");
262 /* parsing first line, getting method and filename */
263 copy = strndupa(line, MAXLINE);
264 if ((method = strtok_r(copy, " ", &ptr)) == NULL) {
265 print_headers(sock, codes[1]);
266 print_error(sock, codes[1], line);
269 if ((filename = strtok_r(NULL, " ", &ptr)) == NULL) {
270 print_headers(sock, codes[1]);
271 print_error(sock, codes[1], line);
274 if ((version = strtok_r(NULL, " ", &ptr)) == NULL) {
275 print_headers(sock, codes[1]);
276 print_error(sock, codes[1], line);
280 while ( (ptr = methods[i]) != NULL) {
281 if ( (strncmp(ptr, method, strlen(ptr)) == 0)) {
287 print_headers(sock, codes[4]);
288 print_error(sock, codes[4], method);
292 while (strcmp(line,"\r\n")) {
293 line = fgets(buffer, MAXLINE, sock);
296 if ( (file = fopen(filename, "r")) == NULL) {
297 if ( (errno == EACCES)||(errno == EPERM) ) {
298 print_headers(sock, codes[5]);
299 print_error(sock, codes[5], filename);
301 print_headers(sock, codes[2]);
302 print_error(sock, codes[2], filename);
306 print_headers(sock, codes[0]);
307 while (!feof(file)) {
308 if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
310 strncpy(buffer, strerror(errno), MAXLINE);
311 print_headers(sock, codes[3]);
312 print_error(sock, codes[3], buffer);
316 if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
318 strncpy(buffer, strerror(errno), MAXLINE);
319 print_headers(sock, codes[3]);
320 print_error(sock, codes[3], buffer);
330 * routine to print error on stout or syslog
332 void PrintErr(char * error)
334 if (demonize) { /* daemon mode */
335 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
342 void print_headers(FILE *file, struct code_page code)
346 fprintf(file, "HTTP/1.0 %s %s \n", code.code, code.name);
348 fprintf(file, "Date: %s", ctime(&tempo));
349 fprintf(file, "Server: WWWd test server\n");
350 fprintf(file, "Connection: close\n");
351 fprintf(file, "Content-Type: text/html; charset=iso-8859-1\n");
356 void print_error(FILE *file, struct code_page page, char * string)
358 fprintf(file, "<HTML><HEAD><TITLE>%s %s</TITLE></HEAD>\n",
359 page.code, page.name);
360 fprintf(file, "<BODY><H1>%s</H1>\n", page.name);
361 fprintf(file, page.body, string);
362 fprintf(file, "<HR><ADDRESS>WWWd by S. Piccardi</ADDRESS></BODY></HTML>");