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/user.cc | 445 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 445 insertions(+) create mode 100644 src/sh/user.cc (limited to 'src/sh/user.cc') diff --git a/src/sh/user.cc b/src/sh/user.cc new file mode 100644 index 0000000..94285ef --- /dev/null +++ b/src/sh/user.cc @@ -0,0 +1,445 @@ +/************************************************************************* + * + * 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 + +int allow(const sref& s,const sref& grp,time_t now,int rootok) +{ + if(grp.empty()) return 1; + int n=s.find(':'); + sref a=s.left(n); + if(rootok>=0&&a==root_tag) return rootok; + else if(grp==root_tag||a!=grp) return 0; + n=(a=s.past(n,1)).find(':'); sref b=a.past(n,1); a=a.left(n); + return (a.empty()||atoi(a)<=now)&&(b.empty()||nowget(name); + if(!env.is_clear(p)||!p->is_dbref()) THROW("htc: could not find system db "<db.remap(); + return &p->db; +} + +static int FindUser(Env& env,const sref& name) +{ + dbmap* users=GetDB(env,USERS_tag); + for(int i=0;isize();i++) { + if(users->at(i,user_alias)==name) return atoi(users->at(i,user_uid)); + sref first,rest=users->at(i,user_email); + while(Split(first,rest)) if(eqn(first,name)) return atoi(users->at(i,user_uid)); + } + return 0; +} + +//////////////////////////////////////////////////////////////////////// + +TOK_IMPL(setgroup) { Req(env)->group=First(env[0]); } +TOK_IMPL(group) { out<group; } + +TOK_IMPL(getgroups) { + dbmap* users=GetDB(env,USERS_tag); + mset m; + for(int i=0;isize();i++) { + sref first,rest=users->at(i,user_access); + while(Split(first,rest)) m.insert(first.left_first(':')); + } + out<Find(env[0]); + if(row>=0) users->Get(row,Req(env)->user.list); + else Req(env)->user.reset(); +} + +TOK_IMPL(remuser) { + dbmap* users=GetDB(env,USERS_tag); + int row=users->Find(env[0]); + if(row>=0) users->Rem(row); +} + +TOK_IMPL(newuser) { + static const charset namends("*@.+"); + user_t user; + sref first,rest=env[0]; + Split(first,rest); + user.group()=first.left_first(':'); + do user.add(first); + while(Split(first,rest)); + user.pin()=Random(4); + user.email()=env[1]; + rest=env[1]; while(Split(first,rest)) if(FindUser(env,first)) { out<<0; return; } + mstring tag,alias=env[1].left_first_of(namends); + while(FindUser(env,alias+tag)) tag=itoa(atoi(tag)+1); + user.alias()=alias+tag; + user.name()=env[2]; + dbmap* users=GetDB(env,USERS_tag); + if(users->empty()) user.uid()=itoa(1); + else user.uid()=itoa(atoi(users->at(users->size()-1,user_uid))+1); + int row=users->Insert(user.list,1); + if(row<0) THROW("htc: could not add user - record not unique"); + out<user.uid(); } +TOK_IMPL(user_group) { out<user.group(); } +TOK_IMPL(user_access) { out<user.access(); } +TOK_IMPL(user_clear) { + sref first,rest=Req(env)->user.access(); int n=0; + while(Split(first,rest)) { if(n++) out.put(' '); out<user.fail(); } +TOK_IMPL(user_pin) { out<user.pin(); } +TOK_IMPL(user_alias) { out<user.alias(); } +TOK_IMPL(user_email) { out<user.email()); } +TOK_IMPL(user_emails) { out<user.email(); } +TOK_IMPL(user_name) { out<user.name(); } +TOK_IMPL(user_last) { out<user.last(); } +TOK_IMPL(user_lastin) { out<user.lastin(); } + +////////////////////////////////////////////////////////////////////////// + +TOK_IMPL(user_group_set) { Req(env)->user.group()=env[0]; } +TOK_IMPL(user_access_add) { Req(env)->user.add_list(env[0]); } +TOK_IMPL(user_access_rem) { Req(env)->user.rem_list(env[0]); } +TOK_IMPL(user_fail_set) { Req(env)->user.fail()=env[0]; } +TOK_IMPL(user_pin_set) { Req(env)->user.pin()=env[0]; } +TOK_IMPL(user_alias_set) { Req(env)->user.alias()=env[0]; } +TOK_IMPL(user_emails_set) { Req(env)->user.email()=env[0]; } +TOK_IMPL(user_name_set) { Req(env)->user.name()=env[0]; } +TOK_IMPL(user_last_set) { Req(env)->user.last()=env[0]; } +TOK_IMPL(user_lastin_set) { Req(env)->user.lastin()=env[0]; } + +////////////////////////////////////////////////////////// + +TOK_IMPL(user_update) { + htc_req* htc=Req(env); + int uid=atoi(htc->user.uid()); + int who=FindUser(env,htc->user.alias()); + if(who&&who!=uid) return; + sref first,rest=htc->user.email(); + while(Split(first,rest)) { + who=FindUser(env,first); + if(who&&who!=uid) return; + } + dbmap* users=GetDB(env,USERS_tag); + int row=users->Find(htc->user.uid()); + if(row>=0) users->Set(row,htc->user.list); +} + +////////////////////////////////////////////////////////// + +TOK_IMPL(su) { + htc_req* htc=Req(env); + user_t tmp(htc->user); + dbmap* users=GetDB(env,USERS_tag); + int row=users->Find(env[0]); + if(row>=0) { + try { + users->Get(row,htc->user.list); + env.parg(out,1); + htc->user=tmp; + } + catch(...) { + htc->user=tmp; + throw; + } + } + else env.parg(out,2); +} + +////////////////////////////////////////////////////////// + +void envAddUser(tokmap& T) throw(merror_t) +{ + TOK_ADD(setgroup,"x",tok_t::SYSONLY, + "%Set current group\n" + "%Syntax: \\setgroup{group}\n" + "%\n" + "%Nop function; only for logging and structure.\n" + ); + TOK_ADD(group,"",tok_t::PUBLIC, + "%Get current group\n" + "%Syntax: \\group\n" + "%\n" + "%Nop function; only for logging and structure.\n" + ); + + TOK_ADD(getgroups,"",tok_t::PUBLIC, + "%Get all groups\n" + "%Syntax: \\getgroups\n" + "%\n" + "%Scans users db for all groups used\n" + ); + + /////////////////////////////////////////////////// + + TOK_ADD(getuser,"x",tok_t::SYSONLY, + "%Get user ID\n" + "%Syntax: \\getuser{alias/email}\n" + "%\n" + "%Returns the uid if found, or zero if not found\n" + ); + TOK_ADD(setuser,"x",tok_t::SYSONLY, + "%Set current user\n" + "%Syntax: \\setuser{uid}\n" + ); + TOK_ADD(remuser,"x",tok_t::SYSONLY, + "%Remove user\n" + "%Syntax: \\remuser{uid}\n" + ); + TOK_ADD(newuser,"xxx",tok_t::SYSONLY, + "%Add a new user\n" + "%Syntax: \\newuser{clear}{email}{name}\n" + "%\n" + "%Returns the new uid if successful, otherwise zero.\n" + ); + + /////////////////////////////////////////////////// + + TOK_ADD(user_id,"",tok_t::PUBLIC, + "%Get uid of current user\n" + "%Syntax: \\user.id\n" + ); + TOK_ADD(user_group,"",tok_t::PUBLIC, + "%Get group of current user\n" + "%Syntax: \\user.group\n" + ); + TOK_ADD(user_access,"",tok_t::PUBLIC, + "%Get access of current user\n" + "%Syntax: \\user.access\n" + ); + TOK_ADD(user_clear,"",tok_t::PUBLIC, + "%Get clearance of current user\n" + "%Syntax: \\user.clear\n" + "%\n" + "%This is a compiled list, without the time info.\n" + ); + TOK_ADD(user_fail,"",tok_t::PUBLIC, + "%Get fail number of current user\n" + "%Syntax: \\user.fail\n" + ); + TOK_ADD(user_pin,"",tok_t::SYSONLY, + "%Get pin of current user\n" + "%Syntax: \\user.pin\n" + ); + TOK_ADD(user_alias,"",tok_t::PUBLIC, + "%Get alias of current user\n" + "%Syntax: \\user.alias\n" + ); + TOK_ADD(user_email,"",tok_t::PUBLIC, + "%Get email of current user\n" + "%Syntax: \\user.email\n" + "%\n" + "%Note that the email list may contain several addresses;\n" + "%this function returns the first in the list.\n" + ); + TOK_ADD(user_emails,"",tok_t::PUBLIC, + "%Get all email addresses of current user\n" + "%Syntax: \\user.emails\n" + ); + TOK_ADD(user_name,"",tok_t::PUBLIC, + "%Get name of current user\n" + "%Syntax: \\user.name\n" + ); + TOK_ADD(user_last,"",tok_t::PUBLIC, + "%Get last time logged in of current user\n" + "%Syntax: \\user.last\n" + ); + TOK_ADD(user_lastin,"",tok_t::PUBLIC, + "%Get last IP address of current user\n" + "%Syntax: \\user.lastin\n" + ); + + /////////////////////////////////////////////////// + + TOK_ADD(user_group_set,"x",tok_t::SYSONLY, + "%Set group of current user\n" + "%Syntax: \\user.group.set{group}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_access_add,"x",tok_t::SYSONLY, + "%Add access to current user\n" + "%Syntax: \\user.access.add{group:start:stop}\n" + "%\n" + "%The :start:stop part may be omitted, in which case\n" + "%a start value of now, and a stop value of nil is set.\n" + "%\n" + "%In case of several group strings, they are all added.\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_access_rem,"x",tok_t::SYSONLY, + "%Remove access from current user\n" + "%Syntax: \\user.access.rem{group}\n" + "%\n" + "%In case of several groups, they are all removed.\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_fail_set,"x",tok_t::SYSONLY, + "%Set fail number of current user\n" + "%Syntax: \\user.fail.set{fail}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_pin_set,"x",tok_t::SYSONLY, + "%Set pin of current user\n" + "%Syntax: \\user.pin.set{pin}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_alias_set,"x",tok_t::SYSONLY, + "%Set alias of current user\n" + "%Syntax: \\user.alias.set{alias}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_emails_set,"x",tok_t::SYSONLY, + "%Set all email addresses of current user\n" + "%Syntax: \\user.emails.set{emails}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_name_set,"x",tok_t::SYSONLY, + "%Set name of current user\n" + "%Syntax: \\user.name.set{name}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_last_set,"x",tok_t::SYSONLY, + "%Set last time logged in of current user\n" + "%Syntax: \\user.last.set{last}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + TOK_ADD(user_lastin_set,"x",tok_t::SYSONLY, + "%Set last IP address of current user\n" + "%Syntax: \\user.lastin.set{addr}\n" + "%\n" + "%Note that the changes take effect after a \\user.update\n" + ); + + /////////////////////////////////////////////////// + + TOK_ADD(user_update,"",tok_t::SYSONLY, + "%Update the current user\n" + "%Syntax: \\user.update\n" + "%\n" + "%All changes a synchronous and immediately available.\n" + ); + + /////////////////////////////////////////////////// + + TOK_ADD(su,"xr?",tok_t::SYSONLY, + "%Set user temporary\n" + "%Syntax: \\su{uid}{body}\n" + "%\n" + "%If set successfully, evaluates body, otherwise .\n" + ); + + /////////////////////////////////////////////////// + +} -- cgit v1.2.3