# HG changeset patch # User Olaf Wintermann # Date 1329949239 -3600 # Node ID a2c8fc23c90e2baf6d2d0c22e166fcb76da981c4 # Parent adb0bda54e6ba2104c8e6536261c4d4fb9343e80 Added basic authentication diff -r adb0bda54e6b -r a2c8fc23c90e src/server/Makefile --- a/src/server/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -28,6 +28,8 @@ BUILD_ROOT = ../../ +CFLAGS = + LDFLAGS = -L/usr/lib/mps -R/usr/lib/mps -lplds4 -lplc4 -lnspr4 -lpthread -ldl -lposix4 -lsocket -lnsl -lgen -lm -lsendfile -lxerces-c -pg OBJ_DIR = $(BUILD_ROOT)build/ @@ -42,13 +44,24 @@ include webdav/objs.mk include daemon/objs.mk include config/objs.mk +include admin/objs.mk -MAINOBJS = $(UCXOBJS) $(UTILOBJS) $(SAFOBJS) $(DAVOBJS) $(DAEMONOBJS) $(CONFOBJS) +include ucx/Makefile +include util/Makefile +include safs/Makefile +include webdav/Makefile +include daemon/Makefile +include config/Makefile +include admin/Makefile -OBJ_DIRS = daemon safs ucx util webdav config +MAINOBJS = $(UCXOBJS) $(UTILOBJS) $(SAFOBJS) $(DAVOBJS) $(DAEMONOBJS) $(CONFOBJS) $(ADMINOBJS) + +OBJ_DIRS = daemon safs ucx util webdav config admin MK_OBJ_DIRS = $(OBJ_DIRS:%=$(OBJ_DIR)server/%) MK_OBJ_DIRS += $(BUILD_ROOT)work/bin +include ucx/Makefile + preparation: $(MK_OBJ_DIRS) $(MK_OBJ_DIRS): @@ -57,16 +70,11 @@ $(MAIN_TARGET): $(MAINOBJS) CC -o $(MAIN_TARGET) $(LDFLAGS) $(MAINOBJS) -$(DAEMONOBJS): $(DAEMONSOURCE) - cd daemon; $(MAKE) all -$(UCXOBJS): $(UCXSOURCE) - cd ucx; $(MAKE) all -$(UTILOBJS): $(UTILSOURCE) - cd util; $(MAKE) all -$(SAFOBJS): $(SAFSOURCE) - cd safs; $(MAKE) all -$(CONFOBJS): $(CONFSOURCE) - cd config; $(MAKE) all -$(DAVOBJS): $(DAVSOURCE) - cd webdav; $(MAKE) all + +../../build/server/ucx/%.o: %.c + cc -o $@ -c $(CFLAGS) $< + + + + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/admin/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/admin/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,36 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Olaf Wintermann. 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. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. +# + +ADMIN_CFLAGS = -I/usr/include/mps -g + +$(ADMIN_OBJPRE)%.o: admin/%.c + cc -o $@ -c $(ADMIN_CFLAGS) $(CFLAGS) $< + +$(ADMIN_OBJPRE)%.o: admin/%.cpp + CC -o $@ -c $(ADMIN_CFLAGS) $(CFLAGS) $< + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/admin/admindex.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/admin/admindex.c Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,41 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. + */ + +#include "template.h" + +CSP_SERVICE_FUNC_RETURN adm_index(CSP_SERVICE_FUNC_ARGS) { + CSP_FUNCTION_START + + csp_start_response(); + +csp_write("\n\t\n\t\tWebserver Administration\n\t\n\t\n\t

Webserver

\n\t\n\n",46); + + CSP_FUNCTION_END +} + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/admin/adminui.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/admin/adminui.h Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,25 @@ +/* + * File: adminui.h + * Author: olaf + * + * Created on 18. Februar 2012, 17:23 + */ + +#ifndef ADMINUI_H +#define ADMINUI_H + +#include "../public/nsapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int adm_index(pblock *pb, Session *sn, Request *rq); + + +#ifdef __cplusplus +} +#endif + +#endif /* ADMINUI_H */ + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/admin/objs.mk --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/admin/objs.mk Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,37 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Olaf Wintermann. 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. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. +# + +ADMIN_SRC_DIR = server/admin/ + +ADMIN_OBJPRE = $(OBJ_DIR)$(ADMIN_SRC_DIR) + +ADMINOBJ = admindex.o + +ADMINOBJS = $(ADMINOBJ:%=$(ADMIN_OBJPRE)%) +ADMINSOURCE = $(ADMINOBJ:%.o=admin/%.c) + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/admin/template.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/admin/template.h Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,43 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. + */ + +#include "../public/nsapi.h" + +#define CSP_SERVICE_FUNC_RETURN int +#define CSP_SERVICE_FUNC_ARGS pblock *pb, Session *sn, Request *rq +#define CSP_SERVICE_CALL_ARGS pb, sn, rq + +#define CSP_FUNCTION_START // nothing +#define CSP_FUNCTION_END return REQ_PROCEED; + + +#define csp_add_header(n,v) pblock_nvinsert(n, v, rq->rq->srvhdrs) +#define csp_start_response() http_start_response(sn, rq) +#define csp_write(str,len) net_write(sn->csd, str, len) + +#define csp_printf(...) net_printf(sn, __VA_ARGS__) diff -r adb0bda54e6b -r a2c8fc23c90e src/server/config/Makefile --- a/src/server/config/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/config/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,9 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -g -LDFLAGS = - -include objs.mk - -all: $(CONFOBJS) - -$(CONF_OBJPRE)%.o: %.c +$(CONF_OBJPRE)%.o: config/%.c cc -o $@ -c $(CFLAGS) $< -$(CONF_OBJPRE)%.o: %.cpp +$(CONF_OBJPRE)%.o: config/%.cpp CC -o $@ -c $(CFLAGS) $< diff -r adb0bda54e6b -r a2c8fc23c90e src/server/config/conf.c --- a/src/server/config/conf.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/config/conf.c Wed Feb 22 23:20:39 2012 +0100 @@ -46,7 +46,7 @@ while((l = cfg_readln(in)).ptr != NULL) { // put the line to the list ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine); - line->line = sstrdub_mp(parser->mp, l); + line->line = sstrdub(l); line->object = NULL; line->type = LINE_OTHER; parser->lines = ucx_dlist_append(parser->lines, line); @@ -276,7 +276,7 @@ // create directive object ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective); - directive->directive_type = sstrdub_mp(mp, name); + directive->directive_type = sstrdub(name); directive->type_num = cfg_get_directive_type_num(name); directive->condition = NULL; // set later by main parsing function directive->param = NULL; @@ -301,10 +301,10 @@ * Wenn man sstrdub_mp statt sstrdub nimmt, wird der Inhalt von pname * verunstaltet. Warum? */ - param->name = sstrdub_mp(mp, pname); + param->name = sstrdub(pname); if(pvalue.length > 0) { - param->value = sstrdub_mp(mp, pvalue); + param->value = sstrdub(pvalue); } else { param->value.ptr = NULL; param->value.length = 0; diff -r adb0bda54e6b -r a2c8fc23c90e src/server/config/objconf.c --- a/src/server/config/objconf.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/config/objconf.c Wed Feb 22 23:20:39 2012 +0100 @@ -48,6 +48,7 @@ conf->parser.parse = objconf_parse; conf->file = file; conf->conditions = NULL; + conf->levels = NULL; int r = cfg_parse_basic_file((ConfigParser*)conf, in); if(r != 0) { diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/Makefile --- a/src/server/daemon/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,11 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -I/usr/include/mps -g -LDFLAGS = - -include objs.mk +DMN_CFLAGS = -I/usr/include/mps -g -all: $(DAEMONOBJS) +$(DMN_OBJPRE)%.o: daemon/%.c + cc -o $@ -c $(DMN_CFLAGS) $(CFLAGS) $< -$(DMN_OBJPRE)%.o: %.c - cc -o $@ -c $(CFLAGS) $< - -$(DMN_OBJPRE)%.o: %.cpp - CC -o $@ -c $(CFLAGS) $< +$(DMN_OBJPRE)%.o: daemon/%.cpp + CC -o $@ -c $(DMN_CFLAGS) $(CFLAGS) $< diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/func.c --- a/src/server/daemon/func.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/func.c Wed Feb 22 23:20:39 2012 +0100 @@ -30,7 +30,7 @@ #include #include "../public/nsapi.h" - +#include "../util/pblock.h" #include "../ucx/map.h" #include "func.h" @@ -59,3 +59,26 @@ FuncStruct* get_function(char *name) { return ucx_map_cstr_get(function_map, name); } + +FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq) { + char *name = pblock_findkeyval(pb_key_fn, pb); + if(name == NULL) { + // TODO: error + return NULL; + } + + FuncStruct *func = get_function(name); + if(func == NULL) { + // TODO: error + } + return func; +} + +int func_exec (pblock *pb, Session *sn, Request *rq) { + FuncStruct *func = func_resolve(pb, sn, rq); + if(func == NULL) { + return REQ_ABORTED; + } + return func->func(pb, sn, rq); +} + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/httplistener.c --- a/src/server/daemon/httplistener.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/httplistener.c Wed Feb 22 23:20:39 2012 +0100 @@ -52,6 +52,7 @@ #include "session.h" #include "configmanager.h" +UcxMap *listener_map = NULL; int start_all_listener() { ServerConfiguration *conf = cfgmgr_get_server_config(); @@ -67,10 +68,20 @@ HttpListener* http_listener_new(ListenerConfig *conf) { + if(listener_map == NULL) { + listener_map = ucx_map_new(16); + } + + HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); + if(fl != NULL) { + return fl; + } + HttpListener *listener = malloc(sizeof(HttpListener)); listener->name = conf->name; listener->session_handler = create_basic_session_handler(); listener->nacceptors = conf->nacceptors; + ucx_map_sstr_put(listener_map, listener->name, listener); struct sockaddr_in servaddr; /* server address */ diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/httprequest.c --- a/src/server/daemon/httprequest.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/httprequest.c Wed Feb 22 23:20:39 2012 +0100 @@ -159,7 +159,7 @@ (int*)&absPath.length); /* Decode the abs_path */ - // TODO: decode abs_path + // TODO: decode abs_path (done?) /* Pass the abs_path as 'uri' in reqpb */ pblock_kvinsert( @@ -301,6 +301,10 @@ } case NSAPIPathCheck: { //printf(">>> PathCheck\n"); + r = nsapi_pathcheck(sn, rq); + if(r != REQ_PROCEED) { + break; + } rq->phase++; nsapi_context_next_stage(&rq->context); } @@ -334,6 +338,7 @@ } } while (r == REQ_RESTART); + r = nsapi_finish_request(sn, rq); return r; } @@ -449,6 +454,47 @@ return REQ_PROCEED; } +int nsapi_pathcheck(NSAPISession *sn, NSAPIRequest *rq) { + //printf("nsapi_pathcheck\n"); + httpd_objset *objset = rq->rq.os; + + if(NCX_OI(rq) == -1) { + NCX_OI(rq) = objset->pos - 1; + } + + int ret = rq->context.last_req_code; + for(int i=NCX_OI(rq);i>=0;i--) { + httpd_object *obj = objset->obj[i]; + dtable *dt = object_get_dtable(obj, NSAPIPathCheck); + + // execute directives + for(int j=0;jndir;j++) { + directive *d = dt->dirs[j]; + + /* execute the saf */ + ret = d->func->func(d->param, (Session*)sn, (Request*)rq); + if(ret != REQ_NOACTION) { + if(ret == REQ_PROCESSING) { + /* save nsapi context */ + rq->context.objset_index = i; + + /* add +1 to start next round with next function */ + rq->context.dtable_index = j + 1; + } else if(ret == REQ_ABORTED) { + if(rq->rq.status_num == PROTOCOL_UNAUTHORIZED) { + // TODO: unify error handling + protocol_start_response((Session*)sn, (Request*)rq); + } + } + + return ret; + } + } + } + + return ret; +} + int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) { //printf("nsapi_objecttype\n"); httpd_objset *objset = rq->rq.os; diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/main.c --- a/src/server/daemon/main.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/main.c Wed Feb 22 23:20:39 2012 +0100 @@ -42,6 +42,7 @@ #include "httprequest.h" +#include "configmanager.h" int std_pipe_fds[2]; @@ -55,6 +56,10 @@ void sig_usr1_reload(int sig) { printf("reload\n"); + if(cfgmgr_load_config() != 0) { + fprintf(stderr, "Error: cannot reload configuration"); + } + signal(SIGUSR1, sig_usr1_reload); } diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/protocol.c --- a/src/server/daemon/protocol.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/protocol.c Wed Feb 22 23:20:39 2012 +0100 @@ -234,6 +234,9 @@ sbuf_write(out, status_code_str, sc_len); char *scmsg = pblock_findkeyval(pb_key_status, rq->srvhdrs); + if(scmsg == NULL) { + scmsg = "OK"; + } sbuf_write(out, scmsg, strlen(scmsg)); sbuf_write(out, "\r\n", 2); @@ -276,6 +279,10 @@ int http_start_response(Session *sn, Request *rq) { int fd = ((SystemIOStream*)sn->csd)->fd; + if(rq->status_num == -1) { + protocol_status(sn, rq, 200, "OK"); + } + /* set socket blocking */ int flags; flags = fcntl(fd, F_GETFL, 0); diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/request.c --- a/src/server/daemon/request.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/request.c Wed Feb 22 23:20:39 2012 +0100 @@ -94,5 +94,9 @@ nrq->context.objset_index = -1; nrq->context.dtable_index = 0; + + // new + rq->status_num = -1; + return 0; } diff -r adb0bda54e6b -r a2c8fc23c90e src/server/daemon/ws-fn.c --- a/src/server/daemon/ws-fn.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/daemon/ws-fn.c Wed Feb 22 23:20:39 2012 +0100 @@ -28,12 +28,16 @@ #include "../public/nsapi.h" +#include "../safs/auth.h" #include "../safs/nametrans.h" +#include "../safs/pathcheck.h" #include "../safs/objecttype.h" #include "../safs/service.h" #include "../safs/init.h" #include "../webdav/webdav.h" +#include "../admin/adminui.h" + struct FuncStruct webserver_funcs[] = { { "init-test", init_test, NULL, 0 }, { "load-modules", load_modules, NULL, 0}, @@ -45,5 +49,8 @@ { "service-hello", service_hello, NULL, 0}, { "send-options", send_options, NULL, 0}, { "webdav-service", webdav_service, NULL, 0}, + { "admin-index", adm_index, NULL, 0}, + { "auth-basic", auth_basic, NULL, 0 }, + { "require-auth", require_auth, NULL, 0}, {NULL, NULL, NULL, 0} }; diff -r adb0bda54e6b -r a2c8fc23c90e src/server/public/nsapi.h --- a/src/server/public/nsapi.h Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/public/nsapi.h Wed Feb 22 23:20:39 2012 +0100 @@ -1164,6 +1164,9 @@ ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes); +ssize_t net_printf(SYS_NETFD fd, char *format, ...); + + NSAPI_PUBLIC pb_param *INTparam_create(const char *name, const char *value); NSAPI_PUBLIC int INTparam_free(pb_param *pp); @@ -1242,6 +1245,10 @@ #define pblock_fr INTpblock_fr #define pblock_replace INTpblock_replace +// func util functions +FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq); +int func_exec (pblock *pb, Session *sn, Request *rq); + void protocol_status(Session *sn, Request *rq, int n, const char *m); diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/Makefile --- a/src/server/safs/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/safs/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,11 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -I/usr/include/mps -g -LDFLAGS = - -include objs.mk +SAFS_CFLAGS = -I/usr/include/mps -g -all: $(SAFOBJS) +$(SAFS_OBJPRE)%.o: safs/%.c + cc -o $@ -c $(SAFS_CFLAGS) $(CFLAGS) $< -$(SAFS_OBJPRE)%.o: %.c - cc -o $@ -c $(CFLAGS) $< - -$(SAFS_OBJPRE)%.o: %.cpp - CC -o $@ -c $(CFLAGS) $< +$(SAFS_OBJPRE)%.o: /safs/%.cpp + CC -o $@ -c $(SAFS_CFLAGS) $(CFLAGS) $< diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/auth.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/auth.c Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,199 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * + * THE BSD LICENSE + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 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. + * + * Neither the name of the nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "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 COPYRIGHT OWNER + * 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. + */ + +#include + +#include "auth.h" + + +/* ------------------------------ _uudecode ------------------------------- */ + +const unsigned char pr2six[256]={ + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63, + 52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9, + 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27, + 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64 +}; + +char *_uudecode(char *bufcoded) +{ + register char *bufin = bufcoded; + register unsigned char *bufout; + register int nprbytes; + unsigned char *bufplain; + int nbytesdecoded; + + /* Find the length */ + while(pr2six[(int)*(bufin++)] <= 63); + nprbytes = bufin - bufcoded - 1; + nbytesdecoded = ((nprbytes+3)/4) * 3; + + bufout = (unsigned char *) malloc(nbytesdecoded + 1); + bufplain = bufout; + + bufin = bufcoded; + + while (nprbytes > 0) { + *(bufout++) = (unsigned char) + (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4); + *(bufout++) = (unsigned char) + (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2); + *(bufout++) = (unsigned char) + (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]); + bufin += 4; + nprbytes -= 4; + } + + if(nprbytes & 03) { + if(pr2six[(int)bufin[-2]] > 63) + nbytesdecoded -= 2; + else + nbytesdecoded -= 1; + } + bufplain[nbytesdecoded] = '\0'; + + return (char *)bufplain; +} + +/* ------------------------------ auth_basic ------------------------------ */ + +int auth_basic(pblock *param, Session *sn, Request *rq) +{ + char *pwfile, *grpfile, *type, *auth, *user, *pw; + char *pwfn, *grpfn; + pblock *npb; + pb_param *pp; + int ret; + + /* Although this is authorization (which is not cacheable) the + * check is not actually done until we call require-auth. So + * this part is cacheable; require-auth can be cacheable if the + * user has limited the auth to only affect a certain set of + * paths. + */ + rq->directive_is_cacheable = 1; + + if(request_header("authorization", &auth, sn, rq) == REQ_ABORTED) + return REQ_ABORTED; + + if(!auth) + return REQ_NOACTION; + + type = pblock_findval("auth-type", param); + pwfile = pblock_findval("userdb", param); + grpfile = pblock_findval("groupdb", param); + pwfn = pblock_findval("userfn", param); + grpfn = pblock_findval("groupfn", param); + + if((!type) || (!pwfile) || (!pwfn) || (grpfile && !grpfn)) { + // TODO: log error + //log_error(LOG_MISCONFIG, "basic-auth", sn, rq, + // XP_GetAdminStr(DBT_authError1)); + protocol_status(sn, rq, PROTOCOL_SERVER_ERROR, NULL); + return REQ_ABORTED; + } + + /* Skip leading whitespace */ + while(*auth && (*auth == ' ')) + ++auth; + if(!(*auth)) { + protocol_status(sn, rq, PROTOCOL_FORBIDDEN, NULL); + return REQ_ABORTED; + } + + /* Verify correct type */ + if((strlen(auth) < 6) || strncasecmp(auth, "basic ", 6)) + return REQ_NOACTION; + + /* Skip whitespace */ + auth += 6; + while(*auth && (*auth == ' ')) + ++auth; + + if(!*auth) + return REQ_NOACTION; + + /* Uuencoded user:password now */ + if(!(user = _uudecode(auth))) + return REQ_NOACTION; + + if(!(pw = strchr(user, ':'))) { + free(user); + return REQ_NOACTION; + } + *pw++ = '\0'; + + npb = pblock_create(4); + pblock_nvinsert("user", user, npb); + pblock_nvinsert("pw", pw, npb); + pblock_nvinsert("userdb", pwfile, npb); + if(grpfile) + pblock_nvinsert("groupdb", grpfile, npb); + pblock_nvinsert("fn", pwfn, npb); + + if ((ret = func_exec(npb, sn, rq)) != REQ_PROCEED) + { + goto bye; + } + + pblock_nvinsert("auth-type", "basic", rq->vars); + pblock_nvinsert("auth-user", user, rq->vars); + pblock_nvinsert("auth-db", pwfile, rq->vars); +#if defined(XP_WIN32) || defined(MCC_ADMSERV) + /* MLM - the admin server needs this password information, + * so I'm putting it back */ + pblock_nvinsert("auth-password", pw, rq->vars); +#endif /* XP_WIN32 */ + + if(grpfile) { + pblock_nvinsert("groupdb", grpfile, npb); + pp = pblock_find("fn", npb); + free(pp->value); + pp->value = strdup(grpfn); + + if( (ret = func_exec(npb, sn, rq)) != REQ_PROCEED ) + goto bye; + } + ret = REQ_PROCEED; + bye: + pblock_free(npb); + free(user); + return ret; +} diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/auth.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/auth.h Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,46 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. + */ + +#ifndef AUTH_H +#define AUTH_H + +#include "../public/nsapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int auth_basic(pblock *param, Session *sn, Request *rq); + + +#ifdef __cplusplus +} +#endif + +#endif /* AUTH_H */ + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/objs.mk --- a/src/server/safs/objs.mk Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/safs/objs.mk Wed Feb 22 23:20:39 2012 +0100 @@ -34,6 +34,8 @@ SAFOBJ += objecttype.o SAFOBJ += service.o SAFOBJ += init.o +SAFOBJ += auth.o +SAFOBJ += pathcheck.o SAFOBJS = $(SAFOBJ:%=$(SAFS_OBJPRE)%) SAFSOURCE = $(SAFOBJ:%.o=safs/%.c) diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/pathcheck.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/pathcheck.c Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,47 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. + */ + +#include "pathcheck.h" + +#include "../util/pblock.h" + +int require_auth(pblock *pb, Session *sn, Request *rq) { + char *user = pblock_findkeyval(pb_key_auth_user, rq->vars); + + if(user == NULL) { + pblock_nvinsert( + "www-authenticate", + "Basic realm=\"Webserver\"", + rq->srvhdrs); + + protocol_status(sn, rq, PROTOCOL_UNAUTHORIZED, NULL); + return REQ_ABORTED; + } + + return REQ_PROCEED; +} diff -r adb0bda54e6b -r a2c8fc23c90e src/server/safs/pathcheck.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/pathcheck.h Wed Feb 22 23:20:39 2012 +0100 @@ -0,0 +1,45 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. + */ + +#ifndef PATHCHECK_H +#define PATHCHECK_H + +#include "../public/nsapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int require_auth(pblock *pb, Session *sn, Request *rq); + +#ifdef __cplusplus +} +#endif + +#endif /* PATHCHECK_H */ + diff -r adb0bda54e6b -r a2c8fc23c90e src/server/ucx/Makefile --- a/src/server/ucx/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/ucx/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,6 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -g -LDFLAGS = - -include objs.mk - -all: $(UCXOBJS) - -$(UCX_OBJPRE)%.o: %.c +$(UCX_OBJPRE)%.o: ucx/%.c cc -o $@ -c $(CFLAGS) $< - -$(UCX_OBJPRE)%.o: %.cpp - CC -o $@ -c $(CFLAGS) $< diff -r adb0bda54e6b -r a2c8fc23c90e src/server/ucx/list.c --- a/src/server/ucx/list.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/ucx/list.c Wed Feb 22 23:20:39 2012 +0100 @@ -15,7 +15,7 @@ int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) { if (l1 == l2) return 1; - + while (l1 != NULL && l2 != NULL) { if (fnc == NULL) { if (l1->data != l2->data) return 0; diff -r adb0bda54e6b -r a2c8fc23c90e src/server/util/Makefile --- a/src/server/util/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/util/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,11 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -I/usr/include/mps -g -LDFLAGS = - -include objs.mk +UTIL_CFLAGS = -I/usr/include/mps -g -all: $(UTILOBJS) +$(UTIL_OBJPRE)%.o: util/%.c + cc -o $@ -c $(UTIL_CFLAGS) $(CFLAGS) $< -$(UTIL_OBJPRE)%.o: %.c - cc -o $@ -c $(CFLAGS) $< - -$(UTIL_OBJPRE)%.o: %.cpp - CC -o $@ -c $(CFLAGS) $< +$(UTIL_OBJPRE)%.o: util/%.cpp + CC -o $@ -c $(UTIL_CFLAGS) $(CFLAGS) $< diff -r adb0bda54e6b -r a2c8fc23c90e src/server/util/io.c --- a/src/server/util/io.c Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/util/io.c Wed Feb 22 23:20:39 2012 +0100 @@ -99,6 +99,16 @@ return r; } +ssize_t net_printf(SYS_NETFD fd, char *format, ...) { + va_list arg; + va_start(arg, format); + char *buf; + ssize_t len = vasprintf(&buf, format, arg); + net_write(fd, buf, len); + free(buf); + return len; +} + /* iovec buffer */ iovec_buf_t *iovec_buf_create(pool_handle_t *pool) { diff -r adb0bda54e6b -r a2c8fc23c90e src/server/webdav/Makefile --- a/src/server/webdav/Makefile Thu Feb 16 15:08:38 2012 +0100 +++ b/src/server/webdav/Makefile Wed Feb 22 23:20:39 2012 +0100 @@ -26,19 +26,11 @@ # POSSIBILITY OF SUCH DAMAGE. # -BUILD_ROOT = ../../../ -OBJ_DIR = $(BUILD_ROOT)build/ - -CFLAGS = -I/usr/include/mps -g -LDFLAGS = - -include objs.mk +DAV_CFLAGS = -I/usr/include/mps -g -all: $(DAVOBJS) +$(DAV_OBJPRE)%.o: webdav/%.c + cc -o $@ -c $(DAV_CFLAGS) $(CFLAGS) $< -$(DAV_OBJPRE)%.o: %.c - cc -o $@ -c $(CFLAGS) $< - -$(DAV_OBJPRE)%.o: %.cpp - CC -o $@ -c $(CFLAGS) $< +$(DAV_OBJPRE)%.o: webdav/%.cpp + CC -o $@ -c $(DAV_CFLAGS) $(CFLAGS) $<