From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/sh/tree.cc | 304 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 src/sh/tree.cc (limited to 'src/sh/tree.cc') diff --git a/src/sh/tree.cc b/src/sh/tree.cc new file mode 100644 index 0000000..0ec8f16 --- /dev/null +++ b/src/sh/tree.cc @@ -0,0 +1,304 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +const mstring index_tag="index.htc"; +const mstring sindex_tag="/index.htc"; +const mstring htc_mime="text/x-htc"; +const mstring htx_mime="text/x-htx"; +const mstring html_mime="text/html"; + +const mstring uri_tag="stat.URI"; +const mstring refer_tag="stat.REFER"; +const mstring parent_tag="stat.PARENT"; +const mstring mime_tag="stat.MIME"; + +const mstring mod_tag="stat.MOD"; +const mstring mtime_tag="stat.LASTMOD"; +const mstring uid_tag="stat.UID"; +const mstring gid_tag="stat.GID"; +const mstring size_tag="stat.SIZE"; + +const mstring alias_tag="stat.ALIAS"; +const mstring banner_tag="stat.BANNER"; + +const mstring xclear_tag="stat.X.CLEAR"; +const mstring xcookie_tag="stat.X.COOKIE"; +const mstring domain_tag="stat.X.DOMAIN"; +const mstring user_tag="stat.X.USER"; +const mstring xcache_tag="stat.X.CACHE"; + +const mstring xmod_tag="stat.XMOD"; + +///////////////////////////////////////////////////////////////// + +TOK_IMPL(alias) { + mstring acc; + Env nenv(env,XSYSTEM,env.pwd); + acc=env.arg(0)->expr; + nenv.sloc(alias_tag,acc); +} + +TOK_IMPL(banner) { + mstring acc1; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw(acc1); + nenv.parse(sw,env.arg(0)); + acc1+=' '; acc1+=env.arg(1)->expr; + nenv.sloc(banner_tag,acc1); +} + +///////////////////////////////////////////////////////////////// + +TOK_IMPL(clearance) { + mstring acc; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw(acc); + nenv.parse(sw,env.arg(0)); + sref first,rest=acc; + while(Split(first,rest)) + if(first==root_tag) THROW("user: ROOT group not allowed in clearance"); + nenv.sloc(xclear_tag,acc); +} + +TOK_IMPL(clearance_cookie) { + mstring acc1,acc2; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw1(acc1); + nenv.parse(sw1,env.arg(0)); + swrite sw2(acc2); + nenv.parse(sw2,env.arg(1)); + acc1+=' '; acc1+=acc2; + nenv.sloc(xcookie_tag,acc1); +} + +TOK_IMPL(clearance_domain) { + mstring acc; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw(acc); + nenv.parse(sw,env.arg(0)); + nenv.sloc(domain_tag,acc); +} + +TOK_IMPL(clearance_user) { + mstring acc; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw(acc); + nenv.parse(sw,env.arg(0)); + nenv.sloc(user_tag,acc); +} + +TOK_IMPL(cache) { + mstring acc; + Env nenv(env,XSYSTEM,env.pwd); + swrite sw(acc); + nenv.parse(sw,env.arg(0)); + nenv.sloc(xcache_tag,acc); +} + +///////////////////////////////////////////////////////////////// + +inline int reachable(const sref& uri) +{ + static const mstring sys_tag="/sys/"; + static const mstring bin_tag="/bin/"; + static const mstring htc_tag="/htc/"; + static const mstring sccs_tag="/SCCS/"; + static const mstring cvs_tag="/CVS/"; + if(uri.right_last(sindex_tag)==sindex_tag) return 0; + if(uri.find(sys_tag)>=0) return 0; + if(uri.find(bin_tag)>=0) return 0; + if(uri.find(htc_tag)>=0) return 0; + if(uri.find(sccs_tag)>=0) return 0; + if(uri.find(cvs_tag)>=0) return 0; + return 1; +} + +TOK_IMPL(staturi) { + struct stat fs; int ok=0; + mstring parent,refer,uri=env.URI(env[0]); + if(reachable(uri)) { + refer=uri; parent=refer.leftand_last('/'); + if(refer.back()=='/') { refer+=index_tag; parent=parent.popb().leftand_last('/'); } + int s=stat(DocPath(env,refer).c_str(),&fs); + if(s>=0) { + if(fs.st_mode&24576) { + uri+='/'; refer=uri+index_tag; + s=stat(DocPath(env,refer).c_str(),&fs); + } + if(s>=0) { + const sref* pmime=file2mime(refer); + mstring mime=pmime?*pmime:http_mime_default; + mstring index=refer.leftand_last('/')+index_tag; + + Env nenv(env); + nenv.aloc(uri_tag,uri); + nenv.aloc(refer_tag,refer); + nenv.aloc(parent_tag,parent); + nenv.aloc(mime_tag,mime); + + nenv.aloc(mod_tag,itoa(fs.st_mode)); + nenv.aloc(uid_tag,itoa(fs.st_uid)); + nenv.aloc(gid_tag,itoa(fs.st_gid)); + nenv.aloc(size_tag,itoa(fs.st_size)); + nenv.aloc(mtime_tag,itoa(fs.st_mtime)); + + nenv.aloc(alias_tag,ms_empty); + nenv.aloc(banner_tag,ms_empty); + + nenv.aloc(xclear_tag,root_tag); + nenv.aloc(xcookie_tag,ms_empty); + nenv.aloc(domain_tag,ms_empty); + nenv.aloc(user_tag,ms_empty); + nenv.aloc(xcache_tag,ms_empty); + + nenv.aloc(xmod_tag,itoa(time(0))); + + mstring buffer; + if(mime==htc_mime||mime==htx_mime||mime==html_mime) { + LoadBuffer(buffer,DocPath(nenv,refer)); + try { + Env e(nenv,XINIT,refer); + mstream m; + e.parse(m,buffer); + } + catch(const merror_t& e) { + THROW(refer<<": "<=0) { + try { + Env e(nenv,XINIT,index); + mstream m; + e.parse(m,buffer); + nenv.sloc(alias_tag,ms_empty); + nenv.sloc(banner_tag,ms_empty); + } + catch(const merror_t& e) { + THROW(index<<": "<\n" + "%\n" + "%Do body if a reference to uri is found.\n" + "%In the body environment, the record stat. is set\n" + "%with the following variables:\n" + "%\n" + "%URI the canonized URI\n" + "%REFER the file uri it refers to\n" + "%PARENT the parent uri\n" + "%MOD the file mode bits (unix)\n" + "%UID the file uid (unix)\n" + "%GID the file gid (unix)\n" + "%SIZE the file size (unix)\n" + "%LASTMOD the file modification time (unix)\n" + "%MIME the MIME type\n" + "%ALIAS alias used in tree\n" + "%X.CLEAR clerarance\n" + "%X.COOKIE cookie clearance\n" + "%X.DOMAIN domain clearance\n" + "%X.USER user clearance\n" + "%X.CACHE cache time in seconds\n" + "%\n" + "%The X. variables are inherited from index.htc\n" + "%if X.CLEAR remains equal to ROOT after init.\n" + "%\n" + "%Unreachable directories: /sys/, /bin/, /htc/, /SCCS/ and /CVS/.\n" + ); +} -- cgit v1.2.3