diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-03-08 02:02:07 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-03-08 02:02:07 +0800 |
commit | ce25837d92ae5b642c7598a277cfbfa98f497484 (patch) | |
tree | bd951f075949b21c5f72ebe889481a322ef9fd5f | |
parent | c3d0f4182648977dad47ded96ac8b480dfe43124 (diff) | |
download | pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar.gz pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar.bz2 pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar.lz pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar.xz pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.tar.zst pttbbs-ce25837d92ae5b642c7598a277cfbfa98f497484.zip |
bbsctl bbsadm
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@6 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | util/bbsctl.c | 73 |
1 files 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 <string.h> #include <signal.h> #include <sys/types.h> +#include <grp.h> +#ifdef FreeBSD + #include <sys/syslimits.h> + #define PS "/bin/ps" + #define GREP "/usr/bin/grep" + #define SU "/usr/bin/su" +#endif +#ifdef Linux + #include <linux/limits.h> + #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; } |