From ce25837d92ae5b642c7598a277cfbfa98f497484 Mon Sep 17 00:00:00 2001 From: in2 Date: Thu, 7 Mar 2002 18:02:07 +0000 Subject: bbsctl bbsadm git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@6 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- util/bbsctl.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/util/bbsctl.c b/util/bbsctl.c index bf66df35..62d00300 100644 --- a/util/bbsctl.c +++ b/util/bbsctl.c @@ -4,9 +4,23 @@ #include #include #include +#include +#ifdef FreeBSD + #include + #define PS "/bin/ps" + #define GREP "/usr/bin/grep" + #define SU "/usr/bin/su" +#endif +#ifdef Linux + #include + #define PS "/bin/ps" + #define GREP "/bin/grep" + #define SU "/bin/su" +#endif + void usage(void) { - printf("usage: bbsctl [start|stop|restart]\n"); + printf("usage: bbsctl [start|stop|restart|bbsadm]\n"); exit(0); } @@ -34,8 +48,8 @@ void stopbbs(void) { char buf[1024]; int pid; - FILE *fp = popen("/bin/ps -ax | /usr/bin/grep mbbsd | " - "/usr/bin/grep listen", "r"); + FILE *fp = popen(PS " -ax | " GREP " mbbsd | " + GREP " listen", "r"); while( fgets(buf, sizeof(buf), fp) != NULL ){ sscanf(buf, "%d", &pid); printf("stopping %d\n", pid); @@ -49,15 +63,56 @@ void restartbbs(void) startbbs(); } +void bbsadm(void) +{ + gid_t gids[NGROUPS_MAX]; + int i, ngids; + struct group *gr; + ngids = getgroups(NGROUPS_MAX, gids); + if( (gr = getgrnam("bbsadm")) == NULL ){ + puts("group bbsadm not found"); + return; + } + + for( i = 0 ; i < ngids ; ++i ) + if( gr->gr_gid == gids[i] ) + break; + + if( i == ngids ){ + puts("permission denied"); + return; + } + + if( setuid(0) < 0 ){ + perror("setuid(0)"); + return; + } + puts("permission granted"); + execl(SU, "su", "bbsadm", NULL); +} + +struct { + char *cmd; + void (*func)(); +}cmds[] = { {"start", startbbs}, + {"stop", stopbbs}, + {"restart", restartbbs}, + {"bbsadm", bbsadm}, + {NULL, NULL} }; + int main(int argc, char **argv) { + int i; if( argc == 1 ) usage(); - if( strcmp(argv[1], "start") == 0 ) - startbbs(); - else if( strcmp(argv[1], "stop") == 0 ) - stopbbs(); - else if( strcmp(argv[1], "restart") == 0 ) - restartbbs(); + for( i = 0 ; cmds[i].cmd != NULL ; ++i ) + if( strcmp(cmds[i].cmd, argv[1]) == 0 ){ + cmds[i].func(); + break; + } + if( cmds[i].cmd == NULL ){ + printf("command %s not found\n", argv[1]); + usage(); + } return 0; } -- cgit v1.2.3