summaryrefslogtreecommitdiffstats
path: root/mbbsd/passwd.c
blob: 2bdc93fd46593de5ffab8b2b906b3ce65efa52cf (plain) (blame)
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
/* $Id$ */
#define PWCU_IMPL
#include "bbs.h"

#ifdef _BBS_UTIL_C_
#error sorry, mbbsd/passwd.c does not support utility mode anymore. please use libcmbbs instead.
#endif

#ifdef CONST_CUSER
 #undef  cuser
 #define cuser pwcuser
#endif

void
passwd_force_update(int flag)
{
    if(!currutmp || (currutmp->alerts & ALERT_PWD) == 0)
    return;
    currutmp->alerts &= ~flag;
}

int 
initcuser(const char *userid)
{
    usernum = passwd_load_user(userid, &cuser);
    return usernum;
}

// XXX I don't like the stupid synchronization here,
// but simply following previous work here...
int
passwd_sync_update(int num, userec_t * buf)
{
    int alerts;

    if (num < 1 || num > MAX_USERS)
    return -1;

    // money update should be done before everything.
    buf->money = moneyof(num);

    if(usernum == num && currutmp && ((alerts = currutmp->alerts)  & ALERT_PWD))
    {
    userec_t u;
    if (passwd_sync_query(num, &u) != 0)
        return -1;

    if(alerts & ALERT_PWD_BADPOST)
       cuser.badpost = buf->badpost = u.badpost;
        if(alerts & ALERT_PWD_PERM) 
       cuser.userlevel = buf->userlevel = u.userlevel;
        if(alerts & ALERT_PWD_JUSTIFY)  
    {
        memcpy(buf->justify,  u.justify, sizeof(u.justify));
        memcpy(cuser.justify, u.justify, sizeof(u.justify));
        memcpy(buf->email,  u.email, sizeof(u.email));
        memcpy(cuser.email, u.email, sizeof(u.email));
    }
    currutmp->alerts &= ~ALERT_PWD;

    // ALERT_PWD_RELOAD: reload all! No need to write.
    if (alerts & ALERT_PWD_RELOAD)
    {
        memcpy(&cuser, &u, sizeof(u));
        return 0;
    }
    }

    if (passwd_update(num, buf) != 0)
    return -1;

    return 0;
}

// XXX I don't like the stupid synchronization here,
// but simply following previous work here...
int
passwd_sync_query(int num, userec_t * buf)
{
    if (passwd_query(num, buf) < 0)
    return -1;

    return 0;
}

// pwcu*: current user password helpers

static int
pwcuInitCUser(userec_t *u)
{
    assert(usernum > 0 && usernum <= MAX_USERS);
    if (passwd_query(usernum, u) != 0)
    return -1;
    assert(strncmp(u->userid, cuser.userid, IDLEN) == 0);
    if    (strncmp(u->userid, cuser.userid, IDLEN) != 0)
    return -1;
    return 0;
}

static int
pwcuFinalCUser(userec_t *u)
{
    assert(usernum > 0 && usernum <= MAX_USERS);
    assert(strcmp(u->userid, cuser.userid) == 0);
    if (passwd_update(usernum, u) != 0)
    return -1;
    return 0;
}

#define PWCU_START()  userec_t u; if(pwcuInitCUser (&u) != 0) return -1
#define PWCU_END()    if (pwcuFinalCUser(&u) != 0) return -1; return 0

#define _ENABLE_BIT( var,mask) var |=  (mask)
#define _DISABLE_BIT(var,mask)  var &= ~(mask)

int pwcuBitEnableLevel  (unsigned int mask)
{
    PWCU_START();
    _ENABLE_BIT(    u.userlevel, mask);
    _ENABLE_BIT(cuser.userlevel, mask);
    PWCU_END();
}

int pwcuBitDisableLevel (unsigned int mask)
{
    PWCU_START();
    _DISABLE_BIT(    u.userlevel, mask);
    _DISABLE_BIT(cuser.userlevel, mask);
    PWCU_END();
}

int 
pwcuIncNumPost()
{
    PWCU_START();
    cuser.numposts =  ++u.numposts;
    PWCU_END();
}

int 
pwcuDecNumPost()
{
    PWCU_START();
    if (u.numposts > 0)
    u.numposts--;
    cuser.numposts = u.numposts;
    PWCU_END();
}

int 
pwcuViolateLaw  ()
{
    PWCU_START();
    _ENABLE_BIT(    u.userlevel, PERM_VIOLATELAW);
    _ENABLE_BIT(cuser.userlevel, PERM_VIOLATELAW);
    u.timeviolatelaw     = now;
    cuser.timeviolatelaw = u.timeviolatelaw;
    u.vl_count++;
    cuser.vl_count = u.vl_count;
    PWCU_END();
}

int
pwcuSaveViolateLaw()
{
    PWCU_START();
    _DISABLE_BIT(    u.userlevel, PERM_VIOLATELAW);
    _DISABLE_BIT(cuser.userlevel, PERM_VIOLATELAW);
    PWCU_END();
}

int 
pwcuAddExMailBox(int m)
{
    PWCU_START();
    u.exmailbox    += m;
    cuser.exmailbox = u.exmailbox;
    PWCU_END();
}

int pwcuSetLastSongTime (time4_t clk)
{
    PWCU_START();
    u.lastsong     = clk;
    cuser.lastsong = clk;
    PWCU_END();
}

int pwcuSetMyAngel  (const char *angel_uid)
{
    PWCU_START();
    strlcpy(    u.myangel, angel_uid, sizeof(    u.myangel));
    strlcpy(cuser.myangel, angel_uid, sizeof(cuser.myangel));
    PWCU_END();
}

int pwcuSetNickname (const char *nickname)
{
    PWCU_START();
    strlcpy(    u.nickname, nickname, sizeof(    u.nickname));
    strlcpy(cuser.nickname, nickname, sizeof(cuser.nickname));
    PWCU_END();
}

int 
pwcuToggleOutMail()
{
    PWCU_START();
    u.uflag2     ^=  REJ_OUTTAMAIL;
    cuser.uflag2 &= ~REJ_OUTTAMAIL;
    cuser.uflag2 |= (REJ_OUTTAMAIL & u.uflag2);
    PWCU_END();
}

int 
pwcuSetLoginView(unsigned int bits)
{
    PWCU_START();
    u.loginview     = bits;
    cuser.loginview = u.loginview;
    PWCU_END();
}


// non-important variables (only save on exit)

int
pwcuSetSignature(unsigned char newsig)
{
    cuser.signature = newsig;
    return 0;
}

int 
pwcuSetWaterballMode(unsigned int bm)
{
    bm       &=  WATER_MASK;
    cuser.uflag2 &= ~WATER_MASK;  
    cuser.uflag2 |= bm;  
    return 0;
}

int pwcuToggleSortBoard ()
{
    cuser.uflag ^= BRDSORT_FLAG;
    return 0;
}

int pwcuToggleFriendList()
{
    cuser.uflag ^= FRIEND_FLAG;
    return 0;
}

// session save

// XXX this is a little different - only invoked at login,
// which we should update/calculate every variables to log.
int pwcuLoginSave   ()
{
    PWCU_START();   // XXX no need to reload for speed up?

    // new host from 'fromhost'
    strlcpy(cuser.lasthost, fromhost, sizeof(cuser.lasthost));

    // calculate numlogins (only increase one per each key)
    if (((login_start_time - cuser.firstlogin) % 86400) != 
    ((cuser.lastlogin  - cuser.firstlogin) % 86400) )
    cuser.numlogins++;

    // update last login time
    cuser.lastlogin = login_start_time;

    if (!PERM_HIDE(currutmp))
    cuser.lastseen = login_start_time;

    PWCU_END();
}

// XXX this is a little different - only invoked at exist,
// so no need to sync back to cuser.
int pwcuExitSave    ()
{
    PWCU_START();

    _DISABLE_BIT(u.uflag, (PAGER_FLAG | CLOAK_FLAG));
    if (currutmp->pager != PAGER_ON)
    _ENABLE_BIT(u.uflag, PAGER_FLAG);
    if (currutmp->invisible)
    _ENABLE_BIT(u.uflag, CLOAK_FLAG);

    u.invisible = currutmp->invisible;
    u.withme    = currutmp->withme;
    u.pager     = currutmp->pager;

    // XXX 當初設計的人把 mind 設計成非 NULL terminated 的...
    // assert(sizeof(u.mind) == sizeof(currutmp->mind));
    memcpy(u.mind,currutmp->mind, sizeof(u.mind));

    reload_money();

    PWCU_END();
}

// Initialization

void pwcuInitZero   ()
{
    bzero(&cuser, sizeof(cuser));
}

int pwcuInitAdminPerm   ()
{
    PWCU_START();
    cuser.userlevel = PERM_BASIC | PERM_CHAT | PERM_PAGE |
    PERM_POST | PERM_LOGINOK | PERM_MAILLIMIT |
    PERM_CLOAK | PERM_SEECLOAK | PERM_XEMPT |
    PERM_SYSOPHIDE | PERM_BM | PERM_ACCOUNTS |
    PERM_CHATROOM | PERM_BOARD | PERM_SYSOP | PERM_BBSADM;
    PWCU_END();
}

void pwcuInitGuestPerm  ()
{
    cuser.userlevel = 0;
    cuser.uflag = PAGER_FLAG | BRDSORT_FLAG | MOVIE_FLAG;
    cuser.uflag2= 0; // we don't need FAVNEW_FLAG or anything else.
# ifdef GUEST_DEFAULT_DBCS_NOINTRESC
    _ENABLE_BIT(cuser.uflag, DBCS_NOINTRESC);
# endif
}

#undef  DIM
#define DIM(x) (sizeof(x)/sizeof(x[0]))

void pwcuInitGuestInfo  ()
{
    int i;
    char *nick[] = {
    "椰子", "貝殼", "內衣", "寶特瓶", "翻車魚",
    "樹葉", "浮萍", "鞋子", "潛水艇", "魔王",
    "鐵罐", "考卷", "大美女"
    };

    i = random() % DIM(nick);
    snprintf(cuser.nickname, sizeof(cuser.nickname),
        "海邊漂來的%s", nick[i]);
    strlcpy(currutmp->nickname, cuser.nickname,
        sizeof(currutmp->nickname));
    strlcpy(cuser.realname, "guest", sizeof(cuser.realname));
    memset (cuser.mind, 0, sizeof(cuser.mind));
    cuser.sex = i % 8;
}