summaryrefslogtreecommitdiffstats
path: root/util/bbsctl.c
blob: 2cae3dd654d19f0f85b5c742fd039859d49d5255 (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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* $Id$ */
#include "bbs.h"

#ifdef __FreeBSD__
   #include <sys/syslimits.h>
   #include <sys/types.h>
   #include <signal.h>
   #include <grp.h>
   #define  SU      "/usr/bin/su" 
   #define  CP      "/bin/cp"
   #define  KILLALL "/usr/bin/killall"
   #define  IPCS    "/usr/bin/ipcs"
   #define  IPCRM   "/usr/bin/ipcrm"
   #define  AWK     "/usr/bin/awk"
#endif
#ifdef __linux__
   #include <linux/limits.h>
   #include <sys/types.h>
   #include <signal.h>
   #include <grp.h>
   #define  SU      "/bin/su"
   #define  CP      "/bin/cp"
   #define  KILLALL "/usr/bin/killall"
   #define  IPCS    "/usr/bin/ipcs"
   #define  IPCRM   "/usr/bin/ipcrm"
   #define  AWK     "/usr/bin/awk"
#endif

int HaveBBSADM(void)
{
    gid_t   gids[NGROUPS_MAX];
    int     i, ngids;
    struct  group *gr; 

    if( getuid() == 0 || getuid() == BBSUID )
    return 1;

    ngids = getgroups(NGROUPS_MAX, gids);
    if( (gr = getgrnam("bbsadm")) == NULL ){
    puts("group bbsadm not found");
    return 0;
    }

    for( i = 0 ; i < ngids ; ++i )
    if( gr->gr_gid == (int)gids[i] )
            return 1;

    return 0;
}

enum {
    BINDPORTS_MODE_NONE  = 0,
    BINDPORTS_MODE_MBBSD,
    BINDPORTS_MODE_LOGIND,
};

int parse_bindports_mode(const char *fn)
{
    int mode = BINDPORTS_MODE_NONE;
    char buf[PATHLEN], vprogram[STRLEN], vservice[STRLEN], vopt[STRLEN];
    FILE *fp = fopen(fn, "rt");
    if (!fp)
    return mode;

    while(fgets(buf, sizeof(buf), fp))
    {
    if (sscanf(buf, "%s%s%s", vprogram, vservice, vopt) != 3 ||
        strcmp(vprogram, "bbsctl") != 0)
        continue;

    // the only service we understand now is 'mode'
    if (strcmp(vservice, "mode") != 0)
    {
        printf("sorry, unknown service: %s", buf);
        break;
    }

    if (strcmp(vopt, "mbbsd") == 0)
    {
        mode = BINDPORTS_MODE_MBBSD;
        break;
    }
    else if (strcmp(vopt, "logind") == 0)
    {
        mode = BINDPORTS_MODE_LOGIND;
        break;
    }
    }

    fclose(fp);
    return mode;
}

int startbbs(int argc, char **argv)
{
    const char *bindports_fn = BBSHOME "/" FN_CONF_BINDPORTS;
    if( setuid(0) < 0 ){
    perror("setuid(0)");
    exit(1);
    }

    // if there's bindports.conf, use it.
    if (dashs(bindports_fn) > 0)
    {
    // rule 2, if bindports.conf exists, load it.
    printf("starting bbs by %s\n", bindports_fn);

    // load the conf and determine how should we start the services.
    switch(parse_bindports_mode(bindports_fn))
    {
        case BINDPORTS_MODE_NONE:
        printf("mode is not assigned. fallback to default ports.\n");
        break;

        case BINDPORTS_MODE_MBBSD:
        execl(BBSHOME "/bin/mbbsd", "mbbsd",   "-d", "-f", bindports_fn, NULL);
        printf("mbbsd startup failed...\n");
        break;

        case BINDPORTS_MODE_LOGIND:
        execl(BBSHOME "/bin/logind", "logind", "-d", "-f", bindports_fn, NULL);
        printf("logind startup failed...\n");
        break;
    }
    }

    // default: start listening ports with mbbsd.
    printf("starting mbbsd by at 23, 443, 3000-3010\n");
    execl(BBSHOME "/bin/mbbsd", "mbbsd", "-d", "-p23", "-p443",
        "-p3000", "-p3001", "-p3002", "-p3003", "-p3004", "-p3005",
        "-p3006", "-p3007", "-p3008", "-p3009", "-p3010", NULL);

    printf("starting daemons failed\n");
    return 1;
}

int stopbbs(int argc, char **argv)
{
    DIR     *dirp;
    struct  dirent *de;    
    FILE    *fp;
    char    buf[512], fn[512];
    if( !(dirp = opendir("run")) ){
    perror("open " BBSHOME "/run");
    exit(0);
    }

    while( (de = readdir(dirp)) ){
    if (!strstr(de->d_name, ".pid"))
        continue;
    // TODO use "mbbsd." and "logind." ?
    if( strstr(de->d_name, "mbbsd") || strstr(de->d_name, "logind")) {
        sprintf(fn, BBSHOME "/run/%s", de->d_name);
        if( (fp = fopen(fn, "r")) != NULL ){
        if( fgets(buf, sizeof(buf), fp) != NULL ){
            int pid = atoi(buf);
            if (pid < 2)
            {
            printf("invalid pid record: %s\n", fn);
            } else {
            char *sdot = NULL;
            strlcpy(buf, de->d_name, sizeof(buf));
            sdot = strchr(buf, '.');
            if (sdot) *sdot = 0;
            printf("stopping listening-%s at pid %5d\n", buf, pid);
            kill(pid, SIGKILL);
            }
        }
        fclose(fp);
        unlink(fn);
        }
    }
    }

    closedir(dirp);
    return 0;
}

int nonstopSTOP(int argc, char **argv)
{
    DIR     *dirp;
    struct  dirent *de;    
    FILE    *fp;
    char    buf[512];
    if( !(dirp = opendir("/proc")) ){
    perror("open /proc");
    exit(0);
    }

    while( (de = readdir(dirp)) ){
    if( de->d_type & DT_DIR ){
        sprintf(buf, "/proc/%s/cmdline", de->d_name);
        if( (fp = fopen(buf, "r")) ){
        if( fgets(buf, sizeof(buf), fp) != NULL ){
            if( strstr(buf, "mbbsd") ){
            kill(atoi(de->d_name), SIGHUP);
            printf("stopping mbbsd at pid %5d\n",
                   atoi(de->d_name));
            }
        }
        fclose(fp);
        }
    }
    }

    closedir(dirp);
    return 0;
}

int fakekill(pid_t pid, int sig)
{
  kill(pid, 0 /* dummy */);
  return 0;
}

int STOP(int argc, char **argv)
{
    DIR     *dirp;
    struct  dirent *de;    
    FILE    *fp;
    char    buf[512];
    int     num_kill_per_sec = 100;
    int     num_load_threshold = 200;
    int     num_wait_sec = 10;
    int     count = 0;
    int     i;

    if(argc > 0 && (i = atoi(argv[0])) > 0)
    {
      argc --, argv ++;
      num_kill_per_sec = i;
    }
    if(argc > 0 && (i = atoi(argv[0])) > 0)
    {
      argc --, argv ++;
      num_load_threshold = i;
    }
    if(argc > 0 && (i = atoi(argv[0])) > 0)
    {
      argc --, argv ++;
      num_wait_sec = i;
    }

    if( !(dirp = opendir("/proc")) ){
    perror("open /proc");
    exit(0);
    }

    while( (de = readdir(dirp)) ){
    if( de->d_type & DT_DIR ){
        int load;

        while((load = cpuload(NULL)) > num_load_threshold)
        {
          printf("Current load: %d, wait for %d sec...\n", load, num_wait_sec);
          sleep(num_wait_sec);
        }
        sprintf(buf, "/proc/%s/cmdline", de->d_name);
        if( (fp = fopen(buf, "r")) ){
        if( fgets(buf, sizeof(buf), fp) != NULL ){
            if( strstr(buf, "mbbsd") ){
                count ++;
            kill(atoi(de->d_name), SIGHUP);
            printf("stopping mbbsd at pid %5d\n",
                   atoi(de->d_name));
            }
        }
        fclose(fp);
        }
    }
    if(count >= num_kill_per_sec)
    {
      sleep(1);
      count = 0;
    }
    }

    closedir(dirp);
    return 0;
}

#define FAKESTOP_PID_LIMIT (100000)
int fakeSTOP(int argc, char **argv)
{
    DIR     *dirp;
    struct  dirent *de;    
    FILE    *fp;
    char    buf[512];
    int num_per_sec = 100;
    int num_load_threshold = 200;
    pid_t pids[FAKESTOP_PID_LIMIT];
    int num_pid = 0;
    int i;

    if(argc > 0)
    {
      int n;
      
      if((n = atoi(argv[0])) > 0)
        num_per_sec = n;

      if(argc > 1)
      {
        if((n = atoi(argv[1])) > 0)
          num_load_threshold = n;
      }
    }

    if( !(dirp = opendir("/proc")) ){
    perror("open /proc");
    exit(0);
    }

    printf("Now halting all mbbsd processes...\n");
    while( (de = readdir(dirp)) ){
    if( de->d_type & DT_DIR ){
        sprintf(buf, "/proc/%s/cmdline", de->d_name);
        if( (fp = fopen(buf, "r")) ){
        if( fgets(buf, sizeof(buf), fp) != NULL ){
            if( strstr(buf, "mbbsd") ){
                pid_t pid = atoi(de->d_name);

                if(num_pid < FAKESTOP_PID_LIMIT)
                {
                  pids[num_pid ++] = pid;
                }
            fakekill(pid, SIGQUIT);
            printf("halting(SIGQUIT) mbbsd at pid %5d\n",
                   pid);
            }
        }
        fclose(fp);
        }
    }
    }
    closedir(dirp);

    printf("Really killing them...\n");
    for(i = 0; i < num_pid; )
    {
      int c;
      int load;

      load = cpuload(NULL);
      if(load > num_load_threshold)
      {
        printf("current load: %d, waiting...\n", load);
        sleep(5);
        continue;
      }
      for(c = 0; c < num_per_sec && i < num_pid; c ++, i ++)
      {
        pid_t pid = pids[i];

        fakekill(pid, SIGHUP);
        printf("stopping(SIGHUP) mbbsd at pid %5d\n", pid);
      }
      sleep(1);
    }

    return 0;
}

int restartbbs(int argc, char **argv)
{
    stopbbs(argc, argv);
    sleep(1);
    startbbs(argc, argv);
    return 0;
}

int bbsadm(int argc, char **argv)
{
    if( setuid(0) < 0 ){
    perror("setuid(0)");
    return 1;
    }
    puts("permission granted");
    execl(SU, "su", "bbsadm", NULL);
    return 0;
}

int bbstest(int argc, char **argv)
{
    if( access("mbbsd", 0) < 0 ){
    perror("./mbbsd");
    return 1;
    }
    system(CP " -f mbbsd testmbbsd");
    if( setuid(0) < 0 ){
    perror("setuid(0)");
    return 1;
    }
    if( setuid(BBSUID) < 0 ){
    perror("setuid(BBSUID)");
    return 1;
    }
    system(KILLALL " testmbbsd");
    execl("./testmbbsd", "testmbbsd", "-d", "-p9000", NULL);
    perror("execl()");
    return 0;
}

int Xipcrm(int argc, char **argv)
{
#ifdef __FreeBSD__
    char    buf[256], cmd[256];
    FILE    *fp;
    setuid(BBSUID); /* drop privileges so we don't remove other users' IPC */
    sprintf(buf, IPCS " | " AWK " '{print $1 $2}'");
    if( !(fp = popen(buf, "r")) ){
    perror(buf);
    return 1;
    }
    while( fgets(buf, sizeof(buf), fp) != NULL ){
    if( buf[0] == 't' || buf[0] == 'm' || buf[0] == 's' ){
        buf[strlen(buf) - 1] = 0;
        sprintf(cmd, IPCRM " -%c %s\n", buf[0], &buf[1]);
        system(cmd);
    }
    }
    pclose(fp);
    system(IPCS);
    return 0;
#elif defined(__linux__)
    char    buf[256], cmd[256], *type = "ms";
    FILE    *fp;
    setuid(BBSUID); /* drop privileges so we don't remove other users' IPC */
    for( ;*type != '\0';type++ ){
        sprintf(buf, IPCS " -%c | " AWK " '{print $2}'", *type);
        if( !(fp = popen(buf, "r")) ){
            perror(buf);
            return 1;
        }
        while( fgets(buf, sizeof(buf), fp) != NULL ){
            if( isdigit(buf[0]) ){
                buf[strlen(buf) - 1] = 0;
                sprintf(cmd, IPCRM " -%c %s\n", *type, buf);
                system(cmd);
            }
        }
        pclose(fp);
    }
    system(IPCS);
    return 0;
#else
    puts("not implement!");
    return 1;
#endif
}

int permreport(int argc, char **argv)
{
    int     fd, i, count;
    userec_t usr;
    struct {
    int     perm;
    char    *desc;
    } check[] = {
    {PERM_BBSADM,   "PERM_BBSADM"},
    {PERM_SYSOP,    "PERM_SYSOP"},
    {PERM_ACCOUNTS, "PERM_ACCOUNTS  帳號總管"},
    {PERM_CHATROOM, "PERM_CHATROOM  聊天室總管"},
    {PERM_BOARD,    "PERM_BOARD     看板總管"},
    {PERM_PRG,  "PERM_PRG       程式組"},
    {PERM_VIEWSYSOP,"PERM_VIEWSYSOP 視覺站長"},
    {PERM_POLICE_MAN,"PERM_POLICE_MAN 警察總管"},
    {PERM_SYSSUPERSUBOP,"PERM_SYSSUPERSUBOP 群組長"},
    //{PERM_SYSSUBOP, "PERM_SYSSUBOP    小組長"},
    {PERM_ACCTREG,  "PERM_ACCTREG   帳號審核組"},
#if 0
         {PERM_RELATION, "PERM_RELATION"},
                 {PERM_PRG,      "PERM_PRG"},
                 {PERM_ACTION,   "PERM_ACTION"},
                 {PERM_PAINT,    "PERM_PAINT"},
                 {PERM_POLICE_MAN, "PERM_POLICE_MAN"},
                 {PERM_MSYSOP,   "PERM_MSYSOP"},
                 {PERM_PTT,      "PERM_PTT"},
#endif
         {0, NULL}};

    if( (fd = open(".PASSWDS", O_RDONLY)) < 0 ){
    perror(".PASSWDS");
    return 1;
    }
    for( count = i = 0 ; check[i].perm != 0 ; ++i ){
    count = 0;
    lseek(fd, 0, SEEK_SET);
    printf("%s\n", check[i].desc);
    while( read(fd, &usr, sizeof(usr)) > 0 ){
        if( usr.userid[0] != 0 && isalpha(usr.userid[0]) &&
        usr.userlevel & check[i].perm ){
        ++count;
        printf("%-20s%-10s\n", usr.userid, usr.realname);
        }
    }
    printf("total %d users\n\n", count);
    }
    return 0;
}

struct {
    int     (*func)(int, char **);
    char    *cmd, *descript;
} cmds[] =
    { {startbbs,   "start",      "start mbbsd/logind daemons "},
      {stopbbs,    "stop",       "killall listening daemons (mbbsd+logind)"},
      {restartbbs, "restart",    "stop and then start"},
      {bbsadm,     "bbsadm",     "switch to user: bbsadm"},
      {bbstest,    "test",       "run ./mbbsd as bbsadm"},
      {Xipcrm,     "ipcrm",      "ipcrm all msg, shm, sem"},
      {nonstopSTOP,"nonstopSTOP","killall ALL mbbsd (nonstop)"},
      {STOP,       "STOP",       "killall ALL mbbsd"},
      {fakeSTOP,   "fakeSTOP",   "fake killall ALL mbbsd"},
      {permreport, "permreport", "permission report"},
      {NULL,       NULL,       NULL} };

int main(int argc, char **argv)
{
    int     i = 0;
    if( !HaveBBSADM() )
    return 1;
    if( argc >= 2 ){
    chdir(BBSHOME);
    for( i = 0 ; cmds[i].func != NULL ; ++i )
        if( strcmp(cmds[i].cmd, argv[1]) == 0 ){
        cmds[i].func(argc - 2, &argv[2]);
        break;
        }
    }
    if( argc == 1 || cmds[i].func == NULL ){
    /* usage */
    printf("usage: bbsctl [command] [options]\n");
    printf("commands:\n");
    for( i = 0 ; cmds[i].func != NULL ; ++i )
        printf("\t%-15s%s\n", cmds[i].cmd, cmds[i].descript);
    if ( geteuid() != 0 )
        printf("Warning: bbsctl should be SUID\n");
    }
    return 0;
}