1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
/*
* nwrite: write replacement.
*
* Version 1.9.2 - February 9, 1998
*
****************************************************************************
* The nwrite software package and all files contained therein are covered
* by this copyright notice & license:
*
* Copyright (c) 1993 Marco Nicosia <marco@csua.berkeley.edu>
* Copyright (c) 1994-1995 Aaron C. Smith <aaron@csua.berkeley.edu>
* Copyright (c) 1995-1998 Alan Coopersmith <alanc@csua.berkeley.edu>
*
* Permission to use, copy, modify, and distribute this software in source
* and/or binary forms and its documentation, without fee and without a
* signed licensing agreement, is hereby granted, provided that the
* following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* IN NO EVENT SHALL THE AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
* AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY,
* PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHORS HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
****************************************************************************
*
*/
#ifndef lint
const char sccsid[] = "@(#)nwrite.c $Revision: 1.9.1 $ $Date: 97/11/15 18:40:38 $ Marco Nicosia, Aaron Smith, Alan Coopersmith";
const char rcsid[] = "$Id$";
#endif
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#include <sys/stat.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <utmp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
#endif
#ifndef TTYS_WORLD_WRITABLE
# define TTYS_WORLD_WRITABLE TRUE
#endif
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
#ifndef TTY_DIR
# ifdef _PATH_DEV
# define TTY_DIR _PATH_DEV
# else
# define TTY_DIR "/dev/"
# endif
#endif
#ifndef TTY_PREFIX
# ifdef PTS_STYLE_TTYS
# define TTY_PREFIX "pts/"
# else
# define TTY_PREFIX "tty"
# endif
#endif
#define TTY_PREFIX1 "pts/"
#define TTY_PREFIX2 "tty"
#ifndef TTYNAMESZ
# define TTYNAMESZ 24
#endif
#ifndef UT_NAMESIZE
# define UT_NAMESIZE sizeof(utmp_entry.ut_name)
#endif
#ifndef UT_LINESIZE
# define UT_LINESIZE sizeof(utmp_entry.ut_line)
#endif
#ifndef UTMP_FILE
# ifdef _PATH_UTMP /* newer BSD's */
# define UTMP_FILE _PATH_UTMP
# else
# define UTMP_FILE "/etc/utmp"
# endif
#endif
#ifndef PATH_MAX /* should be in <limits.h> on POSIX platforms */
# define PATH_MAX 1024
#endif
#ifndef HAVE_STRDUP
# define strdup mystrdup
static char *mystrdup(const char *);
#endif
#ifndef SIG_ERR
# define SIG_ERR BADSIG
#endif
#ifndef TRUE
# define FALSE 0
# define TRUE (!FALSE)
#endif
/* errno should be defined by including <errno.h> on ANSI-C platforms */
#ifdef NEED_ERRNO_DECL
extern int errno;
#endif
/**** End of portability sections ****/
extern char *optarg;
extern int optind, opterr;
#define BOLD_START "\033[1m" /* vt100-series specific */
#define BOLD_END "\033[m" /* unfortunately */
const char bold_start[] = BOLD_START;
const char bold_end[] = BOLD_END;
#define BUFSIZE 1024
#define RC_FILENAME "/.nwriterc"
/* for showtarg directive */
enum { TARG_NEVER = 0, TARG_MULTIPLE, TARG_ALWAYS };
enum { BOLD_NEVER = 0, BOLD_HEADER, BOLD_TAG, BOLD_ALL };
typedef struct pref_t {
int nodelay;
int beep;
int bold;
int headers;
int whoeof;
int showtarg;
char preftty[TTYNAMESZ];
char *line_header;
} pref_t;
typedef struct tty_info_t {
char name [TTYNAMESZ]; /* name of this tty */
FILE *file; /* file descriptor open & writing to it */
int found;
int specified; /* was it specifically asked for by name? */
struct tty_info_t *next;
} tty_info_t;
typedef struct local_user_t {
char *name;
tty_info_t *ttys;
pref_t pref;
int header_sent;
int ttys_requested, ttys_found;
} local_user_t;
typedef struct user_t {
struct user_t *next;
enum { LOCAL, REMOTE } type;
union {
local_user_t *local;
} info;
} user_t;
/* prototypes */
char * make_idle(int);
RETSIGTYPE sigint_handler();
void get_prefs(local_user_t *, char *);
void read_prefs(char *, pref_t *);
void send_header(user_t *);
/* globals */
FILE *utmpd;
/* user_t my; */
char *myname;
char mytty[TTYNAMESZ];
char *myrcname;
user_t *user_list = NULL;
int local_req_count = 0;
int local_req_found = 0;
struct utmp utmp_entry;
struct stat stat_buf;
struct passwd pwd_stack;
struct passwd *pwd = &pwd_stack;
time_t now;
int debugmode = 0;
struct pref_t default_pref = {
/* nodelay */ FALSE,
/* beep */ TRUE,
/* bold */ BOLD_NEVER,
/* headers */ TRUE,
/* whoeof */ FALSE,
/* showtarg */ TARG_MULTIPLE,
/* preftty */ "",
/* line_header */ NULL
};
tty_info_t *new_tty_info_t (void)
{
tty_info_t *new_tit;
new_tit = calloc(1, sizeof(tty_info_t));
if (new_tit == NULL) {
perror("nwrite: fatal memory allocation error");
exit(errno);
}
return (new_tit);
}
void init_globals()
{
char buf[BUFSIZE];
int myttyfd; /* file descriptor of input tty */
char * myttyname;
#ifdef sequent
extern char *ttyname();
#endif
time(&now); /* store the current time in now */
pwd = getpwuid(getuid());
if (pwd == NULL) {
fprintf(stderr, "nwrite: couldn't determine your login name.\n");
exit(1);
}
myname = strdup(pwd->pw_name);
/* determine controlling terminal */
if (isatty(fileno(stdin)))
myttyfd = fileno(stdin);
else if (isatty(fileno(stdout)))
myttyfd = fileno(stdout);
else if (isatty(fileno(stderr)))
myttyfd = fileno(stderr);
else {
fprintf(stderr, "nwrite: could not find your tty.\n");
exit(2);
}
if (fstat(myttyfd, &stat_buf) == -1) {
fprintf(stderr, "nwrite: could not stat your tty.\n");
exit(2);
}
#if TTYS_WORLD_WRITABLE
if (!(stat_buf.st_mode & (S_IWRITE >> 6)))
fprintf(stderr, "nwrite: your tty is not%s writable.\n",
debugmode ? " world" : "");
#else
if (!(stat_buf.st_mode & (S_IWRITE >> 3)))
fprintf(stderr, "nwrite: your tty is not%s writable.\n",
debugmode ? " group" : "");
#endif
myttyname = ttyname(myttyfd);
if (myttyname == NULL) {
fprintf(stderr, "nwrite: could not determine your tty.\n");
exit(3);
}
else
strncpy(buf, myttyname, TTYNAMESZ);
myttyname = strchr(buf + 1, '/');
if (myttyname != NULL)
{
strcpy(mytty, myttyname + 1);
}
else
strcpy(mytty, buf);
#ifdef SYSTEM_NWRITERC
read_prefs(SYSTEM_NWRITERC, &default_pref);
#endif
}
void get_prefs(local_user_t *usr, char *target)
{
char prefsFileName[PATH_MAX];
/* defaults */
usr->pref = default_pref;
if (target == NULL)
return;
snprintf(prefsFileName, sizeof(prefsFileName), "%s%s", target, RC_FILENAME);
read_prefs(prefsFileName, &(usr->pref));
}
/* parse pref file */
void read_prefs(char *prefsFileName, pref_t *prefTarget) {
char buf[BUFSIZE];
char option[BUFSIZE];
char pref_str[BUFSIZE];
char scratch_buf[BUFSIZE];
int value;
FILE *prefs = NULL;
*scratch_buf = '\0';
if (debugmode)
fprintf(stderr, "Opening %s...", prefsFileName);
prefs = fopen(prefsFileName, "r");
if (prefs == NULL) {
if (debugmode)
perror(prefsFileName);
return;
}
if (debugmode)
printf("succeeded\n");
while (fgets(buf, BUFSIZE, prefs) != NULL) {
sscanf(buf, "%s%s", option, pref_str);
if (debugmode)
printf("option: '%s'\tpref_str: '%s'\n", option, pref_str);
value = atoi(pref_str);
if (strncmp(option, "beep", sizeof("beep") - 1) == 0)
prefTarget->beep = value;
else if (strncmp(option, "nodelay", sizeof("nodelay") -1 ) == 0)
prefTarget->nodelay = value;
else if (strncmp(option, "boldhdr", sizeof("boldhdr") - 1) == 0)
prefTarget->bold = BOLD_TAG;
else if (strncmp(option, "bold", sizeof("bold") - 1) == 0) {
strcpy(scratch_buf, pref_str);
if (strncmp(scratch_buf, "never", sizeof("never") - 1) == 0)
prefTarget->bold = BOLD_NEVER;
else if (strncmp(scratch_buf, "header", sizeof("header") - 1) == 0)
prefTarget->bold = BOLD_HEADER;
else if (strncmp(scratch_buf, "tag", sizeof("tag") - 1) == 0)
prefTarget->bold = BOLD_TAG;
else if (strncmp(scratch_buf, "all", sizeof("all") - 1) == 0)
prefTarget->bold = BOLD_ALL;
else if (strlen(scratch_buf) == 1) /* backwards compat */
prefTarget->bold = atoi(scratch_buf);
}
else if (strncmp(option, "headers", sizeof("headers") - 1) == 0)
prefTarget->headers = value;
else if (strncmp(option, "whoeof", sizeof("whoeof") - 1) == 0)
prefTarget->whoeof = value;
else if (strncmp(option, "showtarg", sizeof("showtarg") - 1) == 0) {
strcpy(scratch_buf, pref_str);
if (strncmp(scratch_buf, "never", sizeof("never") - 1) == 0)
prefTarget->showtarg = TARG_NEVER;
else if (strncmp(scratch_buf, "multi", sizeof("multi") - 1) == 0)
prefTarget->showtarg = TARG_MULTIPLE;
else if (strncmp(scratch_buf, "always", sizeof("always") - 1) == 0)
prefTarget->showtarg = TARG_ALWAYS;
}
else if (strncmp(option, "preftty", sizeof("preftty") - 1) == 0)
strcpy(prefTarget->preftty, pref_str);
}
if (debugmode) {
printf("Options are:\n"
"Nodelay: %d\n"
"Beep: %d\n"
"Bold: %d\n"
"Headers: %d\n"
"WhoEOF: %d\n"
"ShowTarg: %d\n"
"PrefTTY: %s\n",
prefTarget->nodelay, prefTarget->beep, prefTarget->bold,
prefTarget->headers, prefTarget->whoeof, prefTarget->showtarg,
prefTarget->preftty);
}
}
int main(int argc, char *argv[])
{
char buf[BUFSIZE];
char *bufptr = buf;
int IFLAG = FALSE; /* perform idle checking? */
#ifdef CLEAR_PROCS
int AFLAG = FALSE;
#endif
time_t idle_limit = 0;
char *idle_string = NULL;
tty_info_t *the_tty;
user_t *usr;
user_t *prev;
/* install signal handlers */
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
fprintf(stderr, "nwrite: Error installing SIGINT handler.\n");
}
if (signal(SIGHUP, sigint_handler) == SIG_ERR) {
fprintf(stderr, "nwrite: Error installing SIGHUP handler.\n");
}
if (argc < 2) {
usage:
fprintf(stderr, "Usage: nwrite [flags] user[.tty] [user[.tty] ...]\n"
#ifdef CLEAR_PROCS
"\t-a <time>\t clear arguments in process table\n"
#endif
"\t-i <time>\t do not write to ttys more than <time> minutes idle\n");
exit(1);
}
else {
int c;
#ifdef CLEAR_PROCS
while ((c = getopt(argc, argv, "ai:D")) != -1)
#else
while ((c = getopt(argc, argv, "i:D")) != -1)
#endif
switch (c) {
case 'i':
IFLAG = TRUE;
idle_limit = atoi(optarg)*60;
break;
#ifdef CLEAR_PROCS
case 'a':
AFLAG = TRUE;
break;
#endif
case 'D':
debugmode = TRUE;
break;
default:
goto usage;
break;
}
}
if (optind == argc)
goto usage;
init_globals(); /* obtain utmp file descriptor, etc. */
while (optind < argc) {
char *ttyptr;
int new_usr = 0;
if (strchr(argv[optind], '@'))
{
/* eventually handle remote writes here */
fprintf(stderr,
"nwrite: writing to remote hosts not supported (yet)\n"
" skipping %s\n", argv[optind]);
}
else
{ /* write to local user */
tty_info_t *new_tty = NULL;
new_usr = 1;
ttyptr = strrchr(argv[optind], '.');
if (ttyptr != NULL)
*(ttyptr++) = '\0';
for (usr = user_list; (usr != NULL) && new_usr; )
{
if ( (usr->type == LOCAL) &&
!(strcmp(usr->info.local->name, argv[optind])))
{
new_usr = 0;
}
else
{
usr = usr->next;
}
}
if (new_usr) {
usr = (user_t *)malloc(sizeof(user_t));
usr->type = LOCAL;
usr->info.local = (local_user_t *) malloc(sizeof(local_user_t));
usr->info.local->name = strdup(argv[optind]);
pwd = getpwnam(usr->info.local->name);
if (pwd) {
get_prefs(usr->info.local, pwd->pw_dir);
} else {
/* fprintf(stderr, "nwrite: getpwnam(%s) failed", */
fprintf(stderr, "nwrite: could not find user named %s",
usr->info.local->name);
free(usr);
usr = user_list;
if ( (usr == NULL) || (ttyptr != NULL)
|| (usr->info.local->ttys->specified == TRUE) ) {
fprintf(stderr, " - skipping that user.\n");
}
else if (strncmp(argv[optind], TTY_PREFIX1,
sizeof(TTY_PREFIX1) - 1) == 0 ||
strncmp(argv[optind], TTY_PREFIX2,
sizeof(TTY_PREFIX2) - 1) == 0)
{
fprintf(stderr, " - assuming it's a tty name.\n");
strcpy(usr->info.local->ttys->name, TTY_DIR);
strcat(usr->info.local->ttys->name, argv[optind]);
usr->info.local->ttys->specified = TRUE;
}
else if (strncmp(argv[optind], TTY_DIR,
sizeof(TTY_DIR) - 1) == 0)
{
fprintf(stderr, " - assuming it's a tty name.\n");
strcat(usr->info.local->ttys->name, argv[optind]);
usr->info.local->ttys->specified = TRUE;
} else {
fprintf(stderr, " - skipping that user.\n");
}
optind++;
continue;
}
usr->info.local->ttys = NULL;
usr->info.local->ttys_found = 0;
usr->info.local->ttys_requested = 0;
usr->info.local->header_sent = 0;
usr->next = user_list;
user_list = usr;
}
/* Make new tty and put it on the front of the tty list */
new_tty = new_tty_info_t();
new_tty->file = NULL;
new_tty->next = usr->info.local->ttys;
usr->info.local->ttys = new_tty;
usr->info.local->ttys_requested++;
local_req_count++;
if (ttyptr == NULL) {
new_tty->specified = FALSE;
}
else
{ /* tty specified */
new_tty->specified = TRUE;
if (strncmp(ttyptr, TTY_DIR, sizeof(TTY_DIR) - 1) != 0)
{
/* add /dev/ prefix if user didn't supply it */
strncpy(new_tty->name, TTY_DIR, sizeof(new_tty->name));
if (strncmp(ttyptr, TTY_PREFIX1, sizeof(TTY_PREFIX1) - 1) != 0 &&
strncmp(ttyptr, TTY_PREFIX2, sizeof(TTY_PREFIX2) - 1) != 0)
{
/* add tty prefix if user didn't supply it */
strcat(new_tty->name, TTY_PREFIX2);
}
}
strcat(new_tty->name, ttyptr);
}
}
optind++;
}
#ifdef CLEAR_PROCS
if (AFLAG) {
int i;
/* clear argv */
for (i = 1; i < argc; i++) {
int j;
for (j = 0; argv[i][j]; j++)
argv[i][j] = '\0';
}
}
#endif
/* open the utmp */
if ((utmpd = fopen(UTMP_FILE, "r")) == NULL) {
/* don't use argv here - it might've been cleared */
perror("nwrite (" UTMP_FILE "):");
exit(1);
}
/* scan utmp for logins matching any of our targets */
while ( (local_req_found < local_req_count) &&
(fread((char *) &utmp_entry, sizeof(utmp_entry), 1, utmpd) == 1)) {
#ifdef non_user
if (non_user(utmp_entry))
continue;
#endif
#ifdef USER_PROCESS
if (utmp_entry.ut_type != USER_PROCESS)
continue;
#endif
/* skip blank lines in utmp */
if ( (utmp_entry.ut_line[0] == '\0') || (utmp_entry.ut_name[0] == '\0') )
continue;
for (usr = user_list; usr != NULL; usr = usr->next ) {
while ( (usr != NULL) && (usr->type != LOCAL) &&
(usr->info.local->ttys_requested >= usr->info.local->ttys_found)) {
usr = usr->next;
}
if (usr == NULL)
break;
if (!(strncmp(utmp_entry.ut_name, usr->info.local->name,
sizeof(utmp_entry.ut_name)))) {
time_t idle_time = 0;
int tty_fd;
int matched = 0;
for (the_tty = usr->info.local->ttys;
(the_tty != NULL) && !(matched);
the_tty = the_tty->next)
{
if (!(the_tty->found)) {
if (the_tty->specified) {
if (!strncmp(utmp_entry.ut_line,
the_tty->name + sizeof(TTY_DIR) - 1,
UT_LINESIZE))
{
the_tty->found = TRUE;
}
} else { /* tty not specified */
the_tty->found = TRUE;
matched++;
strcpy(the_tty->name, TTY_DIR);
strncat(the_tty->name, utmp_entry.ut_line,
sizeof(utmp_entry.ut_line));
}
if (the_tty->found == TRUE) {
if(stat(the_tty->name, &stat_buf) != 0)
fprintf(stderr, "nwrite: couldn't stat %s.\n",
the_tty->name);
else {
if (stat_buf.st_mtime < stat_buf.st_atime)
idle_time = now < stat_buf.st_mtime ? 0 :
now - stat_buf.st_mtime;
else
idle_time = now < stat_buf.st_atime ? 0 :
now - stat_buf.st_atime;
idle_string = make_idle(idle_time);
if (IFLAG && (the_tty->specified == FALSE)) {
if (idle_time > idle_limit) {
printf("ignoring %s on %s (idle %s)\n",
usr->info.local->name,
the_tty->name + sizeof(TTY_DIR) - 1,
idle_string);
the_tty->found = FALSE;
continue;
}
}
}
/* tell POSIX-ish systems not to make the target's tty the controlling */
/* tty for this process */
#ifdef O_NOCTTY
#define TTY_FLAGS O_WRONLY | O_NOCTTY
#else
#define TTY_FLAGS O_WRONLY
#endif
tty_fd = open(the_tty->name, TTY_FLAGS);
/* Paranoia - make sure we're writing to a tty, not
appending to a file someone has stuck there */
if ( (tty_fd < 0) || !(isatty(tty_fd)) ||
(the_tty->file = fdopen(tty_fd, "w")) == NULL ) {
if (buf == bufptr) {
strcpy(buf, "nwrite: could not open ");
bufptr += sizeof("nwrite: could not open ") - 1;
} else {
strcat(bufptr, ", ");
}
strncat(bufptr, utmp_entry.ut_name, UT_NAMESIZE);
strcat(bufptr, ".");
strncat(bufptr, utmp_entry.ut_line, UT_LINESIZE);
bufptr += strlen(bufptr);
the_tty->found = FALSE;
continue;
}
if (idle_time > 60)
printf("writing to %s on %s (idle %s)\n",
usr->info.local->name,
the_tty->name + sizeof(TTY_DIR) - 1,
idle_string);
else
printf("writing to %s on %s\n",
usr->info.local->name,
the_tty->name + sizeof(TTY_DIR) - 1);
usr->info.local->ttys_found++;
local_req_found++;
if (debugmode)
printf("local_req_count: %d\tlocal_req_found: %d\n",
local_req_count, local_req_found);
break;
}
}
}
}
}
}
if (bufptr != buf)
fprintf(stderr, "%s\n", buf);
fclose(utmpd);
if (IFLAG && idle_limit) {
idle_string = (char *) malloc(64);
snprintf(idle_string, 64, " that were less than %s idle",
make_idle(idle_limit));
} else {
idle_string = "";
}
usr = user_list;
prev = NULL;
while (usr) {
if (usr->type == LOCAL) {
if (usr->info.local->ttys_found < usr->info.local->ttys_requested) {
for (the_tty = usr->info.local->ttys; (the_tty != NULL);
the_tty = the_tty->next)
{
if (!(the_tty->found) && the_tty->specified) {
fprintf(stderr,
"nwrite: utmp says %s is not logged onto tty %s.\n",
usr->info.local->name, the_tty->name);
}
}
if (usr->info.local->ttys_found == 0)
{
user_t *next_usr;
fprintf(stderr, "nwrite: no writable ttys found for %s%s.\n",
usr->info.local->name, idle_string);
if (prev) {
prev->next = usr->next;
} else {
user_list = usr->next;
}
next_usr = usr->next;
local_req_count -= usr->info.local->ttys_requested;
free(usr); /* should free all associated structures as well */
usr = next_usr;
continue;
}
else
{ /* some, but not all ttys found */
tty_info_t *prev_tty, *cur_tty, *next_tty;
fprintf(stderr,
"nwrite: only found %d writable tty%s for %s%s.\n",
usr->info.local->ttys_found,
(usr->info.local->ttys_found > 1) ? "s" : "",
usr->info.local->name,
idle_string);
prev_tty = NULL;
cur_tty = usr->info.local->ttys;
while (cur_tty != NULL) {
if (cur_tty->found == FALSE) {
next_tty = cur_tty->next;
if (prev_tty == NULL)
usr->info.local->ttys = next_tty;
else
prev_tty->next = next_tty;
free(cur_tty);
cur_tty = next_tty;
}
else
{
prev_tty = cur_tty;
cur_tty = prev_tty->next;
}
}
}
}
}
prev = usr;
usr = usr->next;
}
if (debugmode) {
usr = user_list;
while (usr) {
if ( (usr->type == LOCAL) && (usr->info.local->ttys_found == 0) )
fprintf(stderr, "messed up: %s\n", usr->info.local->name);
usr = usr->next;
}
}
fflush(stderr);
if (local_req_found == 0) { /* REMOTE TODO: add test for remote conns */
exit(1);
}
for (usr = user_list; usr != NULL ; usr = usr->next) {
if (usr->info.local->pref.nodelay) {
send_header(usr);
}
}
while (fgets(buf, BUFSIZE, stdin) != NULL) {
char *p;
char *o;
int i;
char obuf[BUFSIZE + BUFSIZE];
/* strip newline */
if ((p = strrchr(buf, '\n'))) *p = '\0';
/* Copy buf to obuf, making control characters visible. */
o=obuf;
for (p=buf; *p!= '\0'; ++p) {
i = toascii(*p);
if (!isprint(i) && !isspace(i) && i != '\007') {
*(o++) = '^'; /* insert carat */
i = i ^ 0x40; /* DEL to ?, others to alpha */
}
*(o++) = i;
}
*o='\0';
usr = user_list;
while(usr) {
int ok = 0;
if (!usr->info.local->header_sent) {
send_header(usr);
}
for (the_tty = usr->info.local->ttys;
the_tty != NULL; the_tty = the_tty->next) {
if (usr->info.local->pref.bold == BOLD_ALL)
ok = fprintf(the_tty->file, "%s%s" BOLD_END " \r\n",
usr->info.local->pref.line_header, obuf);
else
ok = fprintf(the_tty->file, "%s%s\r\n",
usr->info.local->pref.line_header, obuf);
if ( (ok < 0) || (fflush(the_tty->file) == EOF) ) {
fprintf(stderr,
"\007nwrite: write to %s on %s failed, user logged out?\n",
usr->info.local->name, the_tty->name);
fclose(the_tty->file);
if (usr->info.local->ttys_found > 1) {
if (usr->info.local->ttys == the_tty)
usr->info.local->ttys = the_tty->next;
else {
tty_info_t *tmp_tty = usr->info.local->ttys;
while ( (tmp_tty != NULL) && (tmp_tty->next != the_tty) )
tmp_tty = tmp_tty->next;
if (tmp_tty != NULL) {
tmp_tty->next = the_tty->next;
free(the_tty);
}
}
} else { /* last tty - nuke the whole user entry */
if (usr == user_list) {
if (usr->next != NULL) {
user_list = usr->next;
free (usr);
local_req_count--;
}
else /* no one left to write to */
exit(1);
} else {
user_t *temp, *temp_prev;
temp_prev = user_list;
temp = user_list-> next;
while ( (temp != usr) && (temp != NULL) )
{
temp_prev = temp;
temp = temp->next;
}
temp_prev->next = usr->next;
free(usr);
local_req_count--;
}
}
}
}
usr = usr->next;
}
}
sigint_handler(); /* why reduplicate code? */
return 0; /* not reached */
}
char *make_idle(int diff)
{
/* this function lifted from WHO, by Marco Nicosia */
char *idle = (char *) malloc(20 * sizeof(char));
if (diff == 0)
idle[0] = '\0';
else if (diff < 60)
snprintf(idle, 20, "%ds", diff);
else if (diff < 3600)
snprintf(idle, 20, "%dm", diff / 60);
else if (diff < 86400)
snprintf(idle, 20, "%d:%.2d", diff / 3600, (diff % 3600) / 60);
else
snprintf(idle, 20, "%dd", diff / 86400);
return(idle);
}
RETSIGTYPE sigint_handler() {
user_t *usr;
tty_info_t *tty;
for (usr = user_list; usr != NULL; usr = usr->next) {
switch (usr->type)
{
case LOCAL:
if (usr->info.local != NULL) {
for (tty = usr->info.local->ttys;
tty != NULL; tty = tty->next ) {
if (usr->info.local->header_sent) {
if (usr->info.local->pref.bold)
fprintf(tty->file, BOLD_START "EOF");
else
fprintf(tty->file, "EOF");
if (usr->info.local->pref.whoeof && myname)
fprintf(tty->file, "(%s)", myname);
if (usr->info.local->pref.bold)
fprintf(tty->file, BOLD_END "\n");
else
fprintf(tty->file, "\n");
}
fclose(tty->file);
}
}
break;
case REMOTE:
default:
fprintf(stderr, "Non-local ttys not implemented yet");
break;
}
}
putchar('\n');
exit(0);
}
void send_header(user_t *usr)
{
user_t *tmp = user_list;
static char timebuf[32] = "";
tty_info_t *the_tty;
if (timebuf[0] == '\0') /* initialize time the first time through */
{
char *ttime;
#ifdef HAVE_STRFTIME
struct tm *the_time = localtime(&now);
if (the_time != NULL)
strftime(timebuf, sizeof(timebuf), "%X", the_time);
if (timebuf[0] == '\0') /* just in case strftime fails */
#endif
{
ttime = ctime(&now);
strncat(timebuf, ttime + 11, 8);
}
}
for (the_tty = usr->info.local->ttys; the_tty != NULL;
the_tty = the_tty->next)
{
if (the_tty->file != NULL)
{
fprintf(the_tty->file, "\r\n");
if (usr->info.local->pref.beep)
fprintf(the_tty->file, "\007\007\007");
if (usr->info.local->pref.bold)
fprintf(the_tty->file, "%s", bold_start);
fprintf(the_tty->file, "Message from %s on %s ", myname, mytty);
if ((usr->info.local->pref.showtarg == TARG_MULTIPLE &&
user_list->next)
|| usr->info.local->pref.showtarg == TARG_ALWAYS) {
fprintf(the_tty->file, "to ");
for (tmp = user_list; tmp != NULL; tmp = tmp->next) {
fprintf(the_tty->file, "%s ", tmp->info.local->name);
}
}
fprintf(the_tty->file, "at %s ... \r\n", timebuf);
if (usr->info.local->pref.bold)
fprintf(the_tty->file, "%s", bold_end);
fflush(the_tty->file);
}
}
/* Build header tag */
if (usr->info.local->pref.headers) {
char *temptr, *endptr;
size_t tempsize;
tempsize = sizeof(BOLD_START) + sizeof(BOLD_END) + 3 + strlen(myname);
usr->info.local->pref.line_header = malloc(tempsize);
temptr = usr->info.local->pref.line_header;
*temptr = '\0';
endptr= temptr + tempsize;
if (usr->info.local->pref.bold > BOLD_HEADER) {
strcpy(temptr, bold_start);
temptr += sizeof(BOLD_START) - 1;
}
snprintf(temptr, endptr - temptr, " %s> ", myname);
if (usr->info.local->pref.bold == BOLD_TAG)
strcat(temptr, bold_end);
if (debugmode)
printf("line header for writing to %s: %s\n",
usr->info.local->name, usr->info.local->pref.line_header);
}
else if (usr->info.local->pref.bold == BOLD_ALL) {
usr->info.local->pref.line_header = strdup(bold_start);
}
usr->info.local->header_sent = TRUE;
}
#ifndef HAVE_STRDUP
static char *mystrdup(const char *input)
{
char *output = malloc(strlen(input) + 1);
if (output != NULL)
strcpy(output, input);
return output;
}
#endif
#ifndef HAVE_SNPRINTF
# if __STDC__
# include <stdarg.h>
# else
# include <varargs.h>
# endif
#if __STDC__
int
snprintf(char *str, size_t n, char const *fmt, ...)
#else
int
snprintf(str, n, fmt, va_alist)
char *str;
size_t n;
char *fmt;
va_dcl
#endif
{
va_list ap;
int result;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
result = vsprintf(str, fmt, ap);
va_end(ap);
return result;
}
#endif /* don't HAVE_SNPRINTF */
|