summaryrefslogtreecommitdiff
path: root/src/sh/user.cc
blob: 94285efc60698a906dc6c3dd84a5552928851d90 (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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 <sh/htc.h>

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()||now<atoi(b));
}

////////////////////////////////////////////////////////////////////////

user_t::user_t(const user_t& us) : list(user_tabs)
{
  for(int i=0;i<user_tabs;i++) list[i]=us.list[i];
}

void user_t::operator=(const user_t& us)
{
  list.resize(user_tabs);
  for(int i=0;i<user_tabs;i++) list[i]=us.list[i];
}

void user_t::reset()
{
  list.resize(user_tabs);
  uid()=itoa(0);
  group()=itoa(0);
  fail()=itoa(0);
  pin().clear();
  alias().clear();
  email().clear();
  name().clear();
  last()=itoa(0);
  lastin()=lastin_def;
  access().clear();
}

int user_t::allow(const sref& grp,time_t now,int rootok)
{
  sref first,rest=access();
  while(Split(first,rest)) if(::allow(first,grp,now,rootok)) return 1;
  return 0;
}

int user_t::allow_list(const sref& grps,time_t now,int rootok)
{
  sref first,rest=grps;
  while(Split(first,rest)) if(!allow(first,now,rootok)) return 0;
  return 1;
}

void user_t::add(const sref& grp)
{
  sref first,rest=access();
  while(Split(first,rest)) if(first.left_first(':')==grp.left_first(':')) return;
  mstring clear=grp;
  if(clear.find(':')<0) { clear+=':'; clear+=itoa(time(0)); }
  if(access().nempty()) access()+=' '; access()+=clear;
}

void user_t::add_list(const sref& grps) {
  sref first,rest=grps;
  while(Split(first,rest)) add(first);
}

void user_t::rem(const sref& grp)
{
  mstring m;
  sref first,rest=access();
  while(Split(first,rest))
    if(first.left_first(':')!=grp) { if(m.nempty()) m+=' '; m+=first; }
  access()=m;
}

void user_t::rem_list(const sref& grps)
{
  sref first,rest=grps;
  while(Split(first,rest)) rem(first);
}

////////////////////////////////////////////////////////////////////////

static dbmap* GetDB(Env& env,const sref& name)
{
  dbref_t* p=(dbref_t*)env.toks->get(name);
  if(!env.is_clear(p)||!p->is_dbref()) THROW("htc: could not find system db "<<name);
  p->db.remap();
  return &p->db;
}

static int FindUser(Env& env,const sref& name)
{
  dbmap* users=GetDB(env,USERS_tag);
  for(int i=0;i<users->size();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<<Req(env)->group; }

TOK_IMPL(getgroups) {
  dbmap* users=GetDB(env,USERS_tag);
  mset m;
  for(int i=0;i<users->size();i++) {
    sref first,rest=users->at(i,user_access);
    while(Split(first,rest)) m.insert(first.left_first(':'));
  }
  out<<m;
}

////////////////////////////////////////////////////////////////////////

TOK_IMPL(getuser) {
  out<<FindUser(env,env[0]);
}

TOK_IMPL(setuser) {
  dbmap* users=GetDB(env,USERS_tag);
  int row=users->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_id) { out<<Req(env)->user.uid(); }
TOK_IMPL(user_group) { out<<Req(env)->user.group(); }
TOK_IMPL(user_access) { out<<Req(env)->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<<first.left_first(':'); }
}

TOK_IMPL(user_fail) { out<<Req(env)->user.fail(); }
TOK_IMPL(user_pin) { out<<Req(env)->user.pin(); }
TOK_IMPL(user_alias) { out<<Req(env)->user.alias(); }
TOK_IMPL(user_email) { out<<First(Req(env)->user.email()); }
TOK_IMPL(user_emails) { out<<Req(env)->user.email(); }
TOK_IMPL(user_name) { out<<Req(env)->user.name(); }
TOK_IMPL(user_last) { out<<Req(env)->user.last(); }
TOK_IMPL(user_lastin) { out<<Req(env)->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}<else>\n"
          "%\n"
          "%If set successfully, evaluates body, otherwise <else>.\n"
          );

  ///////////////////////////////////////////////////

}