home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / syslogd / wall / wall.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  5.8 KB  |  204 lines

  1. /*
  2.  * Copyright (c) 1988, 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static const char copyright[] =
  36. "@(#) Copyright (c) 1988, 1990, 1993\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. #if 0
  42. static char sccsid[] = "@(#)wall.c    8.2 (Berkeley) 11/16/93";
  43. #endif
  44. static const char rcsid[] =
  45.     "$Id: wall.c,v 1.3.2.2 1997/08/29 05:30:12 imp Exp $";
  46. #endif /* not lint */
  47.  
  48. /*
  49.  * This program is not related to David Wall, whose Stanford Ph.D. thesis
  50.  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <sys/stat.h>
  55. #include <sys/time.h>
  56. #include <sys/uio.h>
  57.  
  58. #include <ctype.h>
  59. #include <err.h>
  60. #include <paths.h>
  61. #include <pwd.h>
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <unistd.h>
  66. #include <utmp.h>
  67.  
  68. void    makemsg __P((char *));
  69. static void usage __P((void));
  70.  
  71. #define    IGNOREUSER    "sleeper"
  72.  
  73. int nobanner;
  74. int mbufsize;
  75. char *mbuf;
  76.  
  77. /* ARGSUSED */
  78. int
  79. main(argc, argv)
  80.     int argc;
  81.     char **argv;
  82. {
  83.     int ch;
  84.     struct iovec iov;
  85.     struct utmp utmp;
  86.     FILE *fp;
  87.     char *p, *ttymsg();
  88.     char line[sizeof(utmp.ut_line) + 1];
  89.  
  90.     while ((ch = getopt(argc, argv, "n")) !=  -1)
  91.         switch (ch) {
  92.         case 'n':
  93.             /* undoc option for shutdown: suppress banner */
  94.             if (geteuid() == 0)
  95.                 nobanner = 1;
  96.             break;
  97.         case '?':
  98.         default:
  99.             usage();
  100.         }
  101.     argc -= optind;
  102.     argv += optind;
  103.     if (argc > 1)
  104.         usage();
  105.  
  106.     makemsg(*argv);
  107.  
  108.     if (!(fp = fopen(_PATH_UTMP, "r")))
  109.         errx(1, "cannot read %s", _PATH_UTMP);
  110.     iov.iov_base = mbuf;
  111.     iov.iov_len = mbufsize;
  112.     /* NOSTRICT */
  113.     while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
  114.         if (!utmp.ut_name[0] ||
  115.             !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
  116.             continue;
  117.         strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
  118.         line[sizeof(utmp.ut_line)] = '\0';
  119.         if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
  120.             warnx("%s", p);
  121.     }
  122.     exit(0);
  123. }
  124.  
  125. static void
  126. usage()
  127. {
  128.     (void)fprintf(stderr, "usage: wall [file]\n");
  129.     exit(1);
  130. }
  131.  
  132. void
  133. makemsg(fname)
  134.     char *fname;
  135. {
  136.     register int ch, cnt;
  137.     struct tm *lt;
  138.     struct passwd *pw;
  139.     struct stat sbuf;
  140.     time_t now, time();
  141.     FILE *fp;
  142.     int fd;
  143.     char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
  144.     char *getlogin(), *strcpy(), *ttyname();
  145.  
  146.     (void)strcpy(tmpname, _PATH_TMP);
  147.     (void)strcat(tmpname, "/wall.XXXXXX");
  148.     if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
  149.         errx(1, "can't open temporary file");
  150.     (void)unlink(tmpname);
  151.  
  152.     if (!nobanner) {
  153.         if (!(whom = getlogin()))
  154.             whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
  155.         (void)gethostname(hostname, sizeof(hostname));
  156.         (void)time(&now);
  157.         lt = localtime(&now);
  158.  
  159.         /*
  160.          * all this stuff is to blank out a square for the message;
  161.          * we wrap message lines at column 79, not 80, because some
  162.          * terminals wrap after 79, some do not, and we can't tell.
  163.          * Which means that we may leave a non-blank character
  164.          * in column 80, but that can't be helped.
  165.          */
  166.         (void)fprintf(fp, "\r%79s\r\n", " ");
  167.         (void)sprintf(lbuf, "Broadcast Message from %s@%s",
  168.             whom, hostname);
  169.         (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
  170.         (void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
  171.             lt->tm_hour, lt->tm_min);
  172.         (void)fprintf(fp, "%-79.79s\r\n", lbuf);
  173.     }
  174.     (void)fprintf(fp, "%79s\r\n", " ");
  175.  
  176.     if (fname && !(freopen(fname, "r", stdin)))
  177.         errx(1, "can't read %s", fname);
  178.     while (fgets(lbuf, sizeof(lbuf), stdin))
  179.         for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
  180.             if (cnt == 79 || ch == '\n') {
  181.                 for (; cnt < 79; ++cnt)
  182.                     putc(' ', fp);
  183.                 putc('\r', fp);
  184.                 putc('\n', fp);
  185.                 cnt = 0;
  186.             } else if (!isprint(ch) && !isspace(ch) && ch != '\007') {
  187.                 putc('^', fp);
  188.                 putc(ch^0x40, fp);    /* DEL to ?, others to alpha */
  189.             } else
  190.                 putc(ch, fp);
  191.         }
  192.     (void)fprintf(fp, "%79s\r\n", " ");
  193.     rewind(fp);
  194.  
  195.     if (fstat(fd, &sbuf))
  196.         errx(1, "can't stat temporary file");
  197.     mbufsize = sbuf.st_size;
  198.     if (!(mbuf = malloc((u_int)mbufsize)))
  199.         errx(1, "out of memory");
  200.     if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
  201.         errx(1, "can't read temporary file");
  202.     (void)close(fd);
  203. }
  204.