/* * bb.c * kreator, 1999. * * 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. */ #include #include #include #include #include #include #include #include #include #include #define BBFILENAME "/DATA" #define TMPFILE "/tmp/bb.tmp" int add_luser (char *luser); int find_luser (char *luser); int display_help (char *bbname); extern char *strncat (char *, const char *, size_t); extern char *strcat (char *, const char *); extern char *strncpy (char *, const char *, size_t); extern int strncmp (const char *, const char *, size_t); extern char *strstr (const char *, const char *); int main(int argc, char *argv[]) { /* better safe than sorry */ umask(077); if (!geteuid() || !getuid()) fprintf(stderr, "NOTICE: You are running BB as root\n\n"); /* parse argv, lousy :-( */ if (argc>1) { if (argc>2 && *argv[2]=='-') add_luser(argv[1]); else find_luser(argv[1]); } else display_help(argv[0]); return EXIT_SUCCESS; } int add_luser(char *luser) { int fd, fdtmp, len; char *editor, cmdline[512], *asctime; void *buffptr; struct stat statbuf, statbuf2; time_t timeofday; /* time is _required_! */ if ((timeofday=time(NULL))==-1) { fprintf(stderr, "ERROR: Clock Failure (%d)\n", errno); exit(errno); } asctime=ctime(&timeofday); /* get editor */ if (((editor=getenv("EDITOR"))==NULL) && ((editor=getenv("VISUAL"))==NULL)) { fprintf(stderr, "ERROR: Undefined editor\n"); exit(EXIT_FAILURE); } /* if there is write-protected tmpfile! */ unlink(TMPFILE); /* open tmpfile, append @@@ delimiter, user and date. */ if ((fdtmp=open(TMPFILE, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR))==-1) { fprintf(stderr, "ERROR: Failure opening TMP file (%d)\n", errno); exit(errno); } /* construct info string */ memcpy(cmdline, "@@@\n", 6); strncat(cmdline, luser, 8); strcat(cmdline, "\n"); strcat(cmdline, asctime); strcat(cmdline, "\n"); if (write(fdtmp, cmdline, strlen(cmdline))==-1) { fprintf(stderr, "ERROR: Failure writing to TMP file (%d)\n", errno); exit(errno); } if (fstat(fdtmp, &statbuf2)==-1) { fprintf(stderr, "ERROR: Failure getting TMP file status (%d)\n", errno); exit(errno); } close(fdtmp); /* construct command line, system-it.. */ if ((len=strlen(editor))>256) { fprintf(stderr, "ERROR: Check your EDITOR/VISUAL variable\n"); exit(EXIT_FAILURE); } memcpy(cmdline, editor, len+1); strcat(cmdline, " +4 "); strcat(cmdline, TMPFILE); system(cmdline); /* ...then get its size and append it to blackbook file */ if (stat(TMPFILE, &statbuf)==-1) { fprintf(stderr, "ERROR: Failure getting file status (%d)\n", errno); exit(errno); } if (!(statbuf2.st_size-statbuf.st_size)) { fprintf(stderr, "ERROR: Zero-size comment file\n"); exit(EXIT_FAILURE); } if ((buffptr=malloc(statbuf.st_size))==NULL) { fprintf(stderr, "ERROR: Failure allocating memory (%d)\n", errno); exit(errno); } if ((fdtmp=open(TMPFILE, O_RDONLY, S_IRUSR|S_IWUSR))==-1) { fprintf(stderr, "ERROR: Failure opening TMP file (%d)\n", errno); exit(errno); } if (read(fdtmp, buffptr, statbuf.st_size)==-1) { fprintf(stderr, "ERROR: Failure reading TMP file (%d)\n", errno); exit(errno); } close(fdtmp); unlink(TMPFILE); /* open blackbook file */ if ((fd=open(BBFILENAME, O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR))==-1) { fprintf(stderr, "ERROR: Failure opening blackbook file (%d)\n", errno); exit(errno); } /* lock it and make it available only to this process */ /*flock(fd, LOCK_EX); */ if (write(fd, buffptr, statbuf.st_size)==-1) { fprintf(stderr, "ERROR: Failure appending to blackbook file (%d)\n", errno); exit(errno); } /* unlock file */ /* flock(fd, LOCK_UN); */ /* close, free and exit */ close(fd); free(buffptr); return EXIT_SUCCESS; } int find_luser(char *luser) { int fd, len, lamercounter=0; caddr_t mmapptr, currptr, maxptr; struct stat statbuf; char *buff=NULL, *delimpos; char lzr[10]; size_t memsz; /* open file */ if ((fd=open(BBFILENAME, O_RDONLY, S_IRUSR|S_IWUSR))==-1) { fprintf(stderr, "ERROR: Failure opening blackbook file (%d)\n", errno); exit(errno); } /* get its stats */ if (fstat(fd, &statbuf)==-1) { fprintf(stderr, "ERROR: Failure getting blackbook file status (%d)\n", errno); exit(errno); } /* map it into memory */ if ((mmapptr=mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))==-1) { fprintf(stderr, "ERROR: Failure mapping file into memory (%d)\n", errno); exit(errno); } /* finding and printing code */ maxptr=mmapptr+statbuf.st_size; strncpy(lzr, luser, 9); strcat(lzr, "\n"); len=strlen(lzr); for (currptr=mmapptr; currptr+35\t\tgives blackbook entry on given user bb -\tmakes a blackbook entry on given user\n", bbname); return EXIT_SUCCESS; }