diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/serv/auth.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/serv/auth.cc')
| -rw-r--r-- | src/serv/auth.cc | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/serv/auth.cc b/src/serv/auth.cc new file mode 100644 index 0000000..abf16b3 --- /dev/null +++ b/src/serv/auth.cc | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 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 | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <serv/htcd.h> | ||
| 21 | |||
| 22 | const mstring default_tag=".default"; | ||
| 23 | |||
| 24 | ///////////////////////////////////////////////////////////////// | ||
| 25 | |||
| 26 | TOK_IMPL(remote_name) { out<<Reqd(env)->tcp->node.name; } | ||
| 27 | TOK_IMPL(remote_address) { out<<Reqd(env)->tcp->node.ip; } | ||
| 28 | TOK_IMPL(remote_port) { out<<Reqd(env)->tcp->port; } | ||
| 29 | |||
| 30 | ///////////////////////////////////////////////////////////////// | ||
| 31 | |||
| 32 | inline int match(const sref& rem,const sref& longrem,const sref& dom) | ||
| 33 | { | ||
| 34 | if(rem==dom) return 1; | ||
| 35 | if(*dom!='.') return 0; | ||
| 36 | if(longrem.right_last(dom)==dom) return 1; | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | inline int allow(const sref& uname,const sref& list) | ||
| 41 | { | ||
| 42 | mstring remote=First(uname); | ||
| 43 | mstring longrem=remote; if(longrem.find('.')<0) longrem+=default_tag; | ||
| 44 | if(remote.empty()) return 0; | ||
| 45 | if(First(list).empty()) return 1; | ||
| 46 | sref first,rest=list; | ||
| 47 | while(Split(first,rest)) if(match(remote,longrem,first)) return 1; | ||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | TOK_IMPL(remote_allow) { out<<allow(Reqd(env)->tcp->node.name,env[0]); } | ||
| 52 | TOK_IMPL(bindroot) { Reqd(env)->htcd()->bindroot=env[0]; } | ||
| 53 | |||
| 54 | ///////////////////////////////////////////////////////////////// | ||
| 55 | |||
| 56 | TOK_IMPL(user_allow) { | ||
| 57 | htcd_req* htcd=Reqd(env); | ||
| 58 | mstring opt=env.parg(-1); | ||
| 59 | int now=opt.empty()?time(0):atoi(opt); | ||
| 60 | out<<htcd->user.allow_list(env[0],now,htcd->RootAllowed()); | ||
| 61 | } | ||
| 62 | |||
| 63 | TOK_IMPL(on_clear) { | ||
| 64 | htcd_req* htcd=Reqd(env); | ||
| 65 | if(htcd->user.allow_list(env[0],time(0),htcd->RootAllowed())) env.parg(out,1); | ||
| 66 | else env.parg(out,2); | ||
| 67 | } | ||
| 68 | |||
| 69 | ///////////////////////////////////////////////////////////////// | ||
| 70 | |||
| 71 | TOK_IMPL(allowed) { | ||
| 72 | int ok=0; | ||
| 73 | if(!env.db) THROW("htc: no open database"); | ||
| 74 | if(env.dbat>=0&&env.dbat<env.db->size()) { | ||
| 75 | user_t user; | ||
| 76 | env.db->Get(env.dbat,user.list); | ||
| 77 | if(user.list.size()!=user_tabs) | ||
| 78 | THROW("htc: cannot test - probably not a users database"); | ||
| 79 | mstring opt=env.parg(-1); | ||
| 80 | int now=opt.empty()?time(0):atoi(opt); | ||
| 81 | ok=user.allow_list(env[0],now,Reqd(env)->RootAllowed()); | ||
| 82 | } | ||
| 83 | out<<ok; | ||
| 84 | } | ||
| 85 | |||
| 86 | TOK_IMPL(ingroups) { | ||
| 87 | int ok=0; | ||
| 88 | if(!env.db) THROW("htc: no open database"); | ||
| 89 | if(env.dbat>=0&&env.dbat<env.db->size()) { | ||
| 90 | user_t user; | ||
| 91 | env.db->Get(env.dbat,user.list); | ||
| 92 | if(user.list.size()!=user_tabs) | ||
| 93 | THROW("htc: cannot test - probably not a users database"); | ||
| 94 | mstring opt=env.parg(-1); | ||
| 95 | int now=opt.empty()?time(0):atoi(opt); | ||
| 96 | ok=user.allow_list(env[0],now,-1); | ||
| 97 | } | ||
| 98 | out<<ok; | ||
| 99 | } | ||
| 100 | |||
| 101 | ///////////////////////////////////////////////////////////////// | ||
| 102 | |||
| 103 | void envAddAuth(tokmap& T) | ||
| 104 | { | ||
| 105 | TOK_ADD(remote_name,"",tok_t::PUBLIC, | ||
| 106 | "%Remote hostname\n" | ||
| 107 | "%Syntax: \\remote.name\n" | ||
| 108 | ); | ||
| 109 | TOK_ADD(remote_address,"",tok_t::PUBLIC, | ||
| 110 | "%Remote address\n" | ||
| 111 | "%Syntax: \\remote.address\n" | ||
| 112 | ); | ||
| 113 | TOK_ADD(remote_port,"",tok_t::PUBLIC, | ||
| 114 | "%Remote port\n" | ||
| 115 | "%Syntax: \\remote.port\n" | ||
| 116 | ); | ||
| 117 | |||
| 118 | ///////////////////////////////////////////////////////////// | ||
| 119 | |||
| 120 | TOK_ADD(remote_allow,"x",tok_t::PUBLIC, | ||
| 121 | "%True if remote hostname allowed by domain\n" | ||
| 122 | "%Syntax: \\remote.allow{domain list}\n" | ||
| 123 | "%\n" | ||
| 124 | "%May be used to restrict access without any group\n" | ||
| 125 | ); | ||
| 126 | TOK_ADD(bindroot,"x",tok_t::SYSONLY, | ||
| 127 | "%Bind root access to explicit addresses\n" | ||
| 128 | "%Syntax: \\bindroot{address list}\n" | ||
| 129 | "%\n" | ||
| 130 | "%Note that domain syntax does not work here; you\n" | ||
| 131 | "%want to have total control over all root access.\n" | ||
| 132 | ); | ||
| 133 | |||
| 134 | ///////////////////////////////////////////////////////////// | ||
| 135 | |||
| 136 | TOK_ADD(user_allow,"x?",tok_t::SYSONLY, | ||
| 137 | "%Core test on user clearance\n" | ||
| 138 | "%Syntax: \\user.allow{groups}<time>\n" | ||
| 139 | "%\n" | ||
| 140 | "%In case of several groups, all must be allowed.\n" | ||
| 141 | ); | ||
| 142 | TOK_ADD(on_clear,"xr?",tok_t::SYSONLY, | ||
| 143 | "%Do if user is allowed in all groups now\n" | ||
| 144 | "%Syntax: \\on.clear{groups}{body}<else>\n" | ||
| 145 | ); | ||
| 146 | |||
| 147 | ///////////////////////////////////////////////////////////// | ||
| 148 | |||
| 149 | TOK_ADD(allowed,"x?",tok_t::SYSONLY, | ||
| 150 | "%True if the current record is allowed in all groups now\n" | ||
| 151 | "%Syntax: \\allowed{groups}<time>\n" | ||
| 152 | "%\n" | ||
| 153 | "%Only used as a query function\n" | ||
| 154 | ); | ||
| 155 | |||
| 156 | TOK_ADD(ingroups,"x?",tok_t::SYSONLY, | ||
| 157 | "%True if the current record is part of all groups now\n" | ||
| 158 | "%Syntax: \\ingroups{groups}<time>\n" | ||
| 159 | "%\n" | ||
| 160 | "%Only used as a query function\n" | ||
| 161 | ); | ||
| 162 | } | ||
