/* Package Name : twhttpd * File Name : httplog.c * Author : Sam NG * All rights reserved. * * This package is an secure HTTP application proxy writen by Sam Ng. * The software is free for commercial and non-commercial use as long as * the following conditions are aheared to. * * Copyright remains Sam NG's, and as such any Copyright notices in * the code are not to be removed. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY SAM NG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ #include #include #include #include #include #include #include #include #include #include "config.h" #include "structs.h" /* Standard Log format */ /* IP IDENT USER [dd/MMM/yyyy HH:mm:ss +GMT] "GET URL HTTP/1.0" return_code byte_count "Referer" "User-Agent" */ /* for syslog, forget about the time field */ void http_log(http_header *hd, int len, int code) { time_t now; char timebuf[MAX_LINE]; char logurl[MAX_LOGURL]; char *ip, *user, *referer, *agent; int free, byte; /* code should note be 0, the real return code may be stored in hd->return_code */ if ( 0 == code ) code = hd->return_code; if ( strcmp(hd->pauth_username, "") && (NULL != hd->proxy_auth) ) { user = hd->pauth_username; } else if ( strcmp(hd->auth_username, "") && (NULL != hd->auth) ) { user = hd->auth_username; } else { user = "-"; } if ( NULL != hd->referer ) { referer = hd->referer; } else { referer = "-" ; } if ( NULL != hd->user_agent ) { agent = hd->user_agent; } else { agent = "-" ; } now = time( (time_t *)NULL ); if ( !strftime(timebuf, sizeof(timebuf)-1, "%d/%b/%Y %T", gmtime(&now)) ) { strcpy(timebuf, "-"); } /* setup hd->abs_url only if is not setup yet */ if ( !strcmp(hd->abs_url, "") ) { /* set abs_url to [ERROR] if gen_abs_url returns error */ if ( gen_abs_url(hd) < 0 ) strncpy(hd->abs_url, "[ERROR]", sizeof(hd->abs_url)); } /* CONNECT has another logging format */ if ( !strcmp(hd->method, "CONNECT") ) { /* is access_log defined? */ if ( NULL == (hd->sc)->alog ) { syslog(LOG_INFO, "%s - %s [%s +GMT] \"%s https://%s %s\" %d %s \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", timebuf, hd->method, hd->host, hd->version, code, "-", "-", "-"); } /* yes, access_log should goto a file */ else { fprintf((hd->sc)->alog, "%s - %s [%s +GMT] \"%s https://%s %s\" %d %s \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", timebuf, hd->method, hd->host, hd->version, code, "-", "-", "-"); } } else { if ( NULL != hd->query ) { /* is access_log defined? */ if ( NULL == (hd->sc)->alog ) { syslog(LOG_INFO, "%s - %s \"%s %s%s?%s %s\" %d %d \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", hd->method, hd->abs_url, hd->path, hd->query, hd->version, code, len, referer, agent); } /* yes, access_log should goto a file */ else { fprintf((hd->sc)->alog, "%s - %s [%s +GMT] \"%s %s%s?%s %s\" %d %d \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", timebuf, hd->method, hd->abs_url, hd->path, hd->query, hd->version, code, len, referer, agent); } } /* no query */ else { /* is access_log defined? */ if ( NULL == (hd->sc)->alog ) { syslog(LOG_INFO, "%s - %s \"%s %s%s %s\" %d %d \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", hd->method, hd->abs_url, hd->path, hd->version, code, len, referer, agent); } /* yes, access_log should goto a file */ else { fprintf((hd->sc)->alog, "%s - %s [%s +GMT] \"%s %s%s %s\" %d %d \"%s\" \"%s\"\n", inet_ntoa(hd->clt_addr.sin_addr), "-", timebuf, hd->method, hd->abs_url, hd->path, hd->version, code, len, referer, agent); } } } }