home *** CD-ROM | disk | FTP | other *** search
-
- /* ====================================================================
- * Copyright (c) 1995 The Apache Group. All rights reserved.
- *
- * 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 above 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.
- *
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * 4. The names "Apache Server" and "Apache Group" must not be used to
- * endorse or promote products derived from this software without
- * prior written permission.
- *
- * 5. Redistributions of any form whatsoever must retain the following
- * acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
- * EXPRESSED 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 APACHE GROUP OR
- * IT'S 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.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Group and was originally based
- * on public domain software written at the National Center for
- * Supercomputing Applications, University of Illinois, Urbana-Champaign.
- * For more information on the Apache Group and the Apache HTTP server
- * project, please see <http://www.apache.org/>.
- *
- */
-
- /*
- * mod_limit.c: limits on daily usage by users
- * A user can only xfer so many bytes/period.
- *
- * Sameer Parekh
- */
-
-
- #include "httpd.h"
- #include "http_config.h"
- #include "mod_limit.h"
- #include <sys/mman.h>
-
- module limit_module;
-
- typedef struct {
- struct user_limit *tableptr;
- char *dataFile;
- } limit_state;
-
- void *
- make_limit_state (pool *p, server_rec *s)
- {
- limit_state *cls =
- (limit_state *)palloc (p, sizeof (limit_state));
-
- cls->tableptr == NULL;
- cls->dataFile == NULL;
-
- return (void *)cls;
- }
-
- char *
- set_limit (cmd_parms *parms, void *dummy, char *arg)
- {
- limit_state *cls = get_module_config (parms->server->module_config,
- &limit_module);
- /* Create mmaped data */
- cls->dataFile = arg;
-
- return NULL;
- }
-
- command_rec limit_cmds[] = {
- { "LimitServer", set_limit, NULL, RSRC_CONF, TAKE1,
- "the filename of the limitation database" },
- { NULL }
- };
-
- void
- init_limit (server_rec *s, pool *p)
- {
- limit_state *cls = get_module_config (s->module_config,
- &limit_module);
- FILE *fp;
- int uid, max;
- char name[MAX_STRING_LEN];
-
- cls->tableptr =
- (struct user_limit *) mmap((caddr_t)0, NUM_RECORDS *
- sizeof(struct user_limit), PROT_READ | PROT_WRITE,
- MAP_ANON | MAP_SHARED, -1, 0);
- if(cls->tableptr == (struct user_limit *) -1 )
- {
- perror("mmap");
- fprintf(stderr, "httpd: Could not map memory for limits\n");
- exit(1);
- }
-
- /* Open up the initialization file */
- cls->dataFile = server_root_relative(p, cls->dataFile);
- fp=fopen(cls->dataFile, "r");
- while(fscanf(fp, "%d %s %d", &uid, name, &max) != EOF)
- {
- if(uid > NUM_RECORDS)
- continue;
- strncpy(cls->tableptr[uid].account, name, 16);
- cls->tableptr[uid].bytes_sent = 0;
- cls->tableptr[uid].bytes_max = max;
- }
- fclose(fp);
- }
-
- int
- check_limits(request_rec *r)
- {
- limit_state *cls = get_module_config(r->server->module_config,
- &limit_module);
-
- if(r->finfo.st_uid > NUM_RECORDS)
- return OK;
-
- if(cls->tableptr[r->finfo.st_uid].bytes_max == 0)
- return OK;
-
- if(cls->tableptr[r->finfo.st_uid].bytes_max
- < cls->tableptr[r->finfo.st_uid].bytes_sent)
- {
- log_reason("user over limit", r->filename, r);
- return SERVICE_UNAVAILABLE;
- }
-
- return OK;
- }
-
- int
- limit_transaction(request_rec *orig)
- {
- /* Add some bits to a given user's data */
-
- limit_state *cls = get_module_config (orig->server->module_config,
- &limit_module);
-
- /* Find the actual request */
- for( ; orig->next != NULL; orig = orig->next);
-
- if(orig->finfo.st_mode == 0)
- {
- return OK;
- }
- if(orig->finfo.st_uid > NUM_RECORDS)
- return OK;
- if(orig->bytes_sent > 0)
- cls->tableptr[orig->finfo.st_uid].bytes_sent += orig->bytes_sent;
-
- return OK;
- }
-
- static int
- limit_trans(request_rec *r)
- {
- limit_state *cls = get_module_config(r->server->module_config,
- &limit_module);
-
- if(!strncmp("/limits/", r->uri, 8))
- {
- r->filename = pstrcat(r->pool, "limits:", r->uri+8, NULL);
- return OK;
- }
- return DECLINED;
- }
-
- static int
- limit_type(request_rec *r)
- {
- if(!strncmp(r->filename, "limits:", 7))
- {
- r->content_type = LIMIT_MAGIC_TYPE;
- return OK;
- }
- return DECLINED;
- }
-
- static int
- limit_handler(request_rec *r)
- {
- limit_state *cls = get_module_config(r->server->module_config,
- &limit_module);
- int index;
-
- /* Spew to client info about limits */
-
- /* This version ignores the file, later versions */
- /* will use the file to make info more specific */
- r->content_type = "text/html";
- send_http_header(r);
-
- rprintf(r, "<TABLE BORDER>\n"
- "<TR><TH>Name</TH><TH>UID</TH><TH>Used</TH><TH>Max</TH></TR>\n");
- for(index = 0; index < NUM_RECORDS; index++)
- {
- if(cls->tableptr[index].bytes_sent != 0)
- rprintf(r, "<TR><TD>%s</TD><TD>%d</TD><TD>%6.2f</TD><TD>%6.2f</TD>\n",
- cls->tableptr[index].account,
- index,
- (float) cls->tableptr[index].bytes_sent / (float) 1048576,
- (float) cls->tableptr[index].bytes_max / (float) 1048576);
- }
- rprintf(r, "</TABLE>\n");
- return OK;
- }
-
- handler_rec limit_handlers[] = {
- { LIMIT_MAGIC_TYPE, limit_handler },
- { NULL }
- };
-
- module limit_module = {
- STANDARD_MODULE_STUFF,
- init_limit, /* initializer */
- NULL, /* create per-dir config */
- NULL, /* merge per-dir config */
- make_limit_state, /* server config */
- NULL, /* merge server config */
- limit_cmds, /* command table */
- limit_handlers, /* handlers */
- limit_trans, /* filename translation */
- NULL, /* check_user_id */
- NULL, /* check auth */
- check_limits, /* check access */
- limit_type, /* type_checker */
- NULL, /* fixups */
- limit_transaction /* logger */
- };
-