17c478bd9Sstevel@tonic-gate /* 2*faebf794Sgtb * CDDL HEADER START 3*faebf794Sgtb * 4*faebf794Sgtb * The contents of this file are subject to the terms of the 5*faebf794Sgtb * Common Development and Distribution License (the "License"). 6*faebf794Sgtb * You may not use this file except in compliance with the License. 7*faebf794Sgtb * 8*faebf794Sgtb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*faebf794Sgtb * or http://www.opensolaris.org/os/licensing. 10*faebf794Sgtb * See the License for the specific language governing permissions 11*faebf794Sgtb * and limitations under the License. 12*faebf794Sgtb * 13*faebf794Sgtb * When distributing Covered Code, include this CDDL HEADER in each 14*faebf794Sgtb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*faebf794Sgtb * If applicable, add the following below this CDDL HEADER, with the 16*faebf794Sgtb * fields enclosed by brackets "[]" replaced with your own identifying 17*faebf794Sgtb * information: Portions Copyright [yyyy] [name of copyright owner] 18*faebf794Sgtb * 19*faebf794Sgtb * CDDL HEADER END 20*faebf794Sgtb */ 21*faebf794Sgtb /* 22*faebf794Sgtb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <ctype.h> 297c478bd9Sstevel@tonic-gate #include <string.h> 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <signal.h> 327c478bd9Sstevel@tonic-gate #include <sys/wait.h> 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/stat.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <unistd.h> 377c478bd9Sstevel@tonic-gate #include <time.h> 387c478bd9Sstevel@tonic-gate #include <utmpx.h> 397c478bd9Sstevel@tonic-gate #include <pwd.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <stdarg.h> 427c478bd9Sstevel@tonic-gate #include <locale.h> 437c478bd9Sstevel@tonic-gate #include <stdlib.h> 447c478bd9Sstevel@tonic-gate #include <limits.h> 457c478bd9Sstevel@tonic-gate #include <wctype.h> 467c478bd9Sstevel@tonic-gate #include <errno.h> 477c478bd9Sstevel@tonic-gate #include <syslog.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define TRUE 1 507c478bd9Sstevel@tonic-gate #define FALSE 0 517c478bd9Sstevel@tonic-gate #define FAILURE -1 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * DATE-TIME format 547c478bd9Sstevel@tonic-gate * %a abbreviated weekday name 557c478bd9Sstevel@tonic-gate * %b abbreviated month name 567c478bd9Sstevel@tonic-gate * %e day of month 577c478bd9Sstevel@tonic-gate * %H hour - 24 hour clock 587c478bd9Sstevel@tonic-gate * %M minute 597c478bd9Sstevel@tonic-gate * %S second 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 63*faebf794Sgtb extern char myhostname[]; 64*faebf794Sgtb extern char progname[]; 65*faebf794Sgtb 66*faebf794Sgtb 677c478bd9Sstevel@tonic-gate static void openfail(int); 687c478bd9Sstevel@tonic-gate static void eof(void); 697c478bd9Sstevel@tonic-gate static void setsignals(void (*)()); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate static FILE *fp; /* File pointer for receipient's terminal */ 727c478bd9Sstevel@tonic-gate static char *rterm; /* Pointer to receipient's terminal */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate int 757c478bd9Sstevel@tonic-gate warn_send(char *receipient, char *msg) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate register struct utmpx *ubuf; 787c478bd9Sstevel@tonic-gate static char rterminal[] = "/dev/\0 2345678901"; 797c478bd9Sstevel@tonic-gate extern FILE *fp; 807c478bd9Sstevel@tonic-gate time_t tod; 817c478bd9Sstevel@tonic-gate char time_buf[40]; 827c478bd9Sstevel@tonic-gate register int bad = 0; 837c478bd9Sstevel@tonic-gate char *rcp1, *rcp2, *rcp3; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 867c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 877c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate /* Set "rterm" to location where receipient's terminal will go. */ 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate rterm = &rterminal[sizeof ("/dev/") - 1]; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* 977c478bd9Sstevel@tonic-gate * strip any realm or instance from principal so we can match against unix 987c478bd9Sstevel@tonic-gate * userid. 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate rcp1 = strdup(receipient); 1017c478bd9Sstevel@tonic-gate rcp2 = strtok(rcp1, "@"); 1027c478bd9Sstevel@tonic-gate rcp3 = strtok(rcp2, "/"); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * Scan through the "utmpx" file for the 1067c478bd9Sstevel@tonic-gate * entry for the person we want to send to. 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate setutxent(); 1107c478bd9Sstevel@tonic-gate while ((ubuf = getutxent()) != NULL) { 1117c478bd9Sstevel@tonic-gate if (ubuf->ut_type == USER_PROCESS) { 1127c478bd9Sstevel@tonic-gate if (strncmp(rcp3, ubuf->ut_user, 1137c478bd9Sstevel@tonic-gate sizeof (ubuf->ut_user)) == 0) { 1147c478bd9Sstevel@tonic-gate strncpy(rterm, &ubuf->ut_line[0], 1157c478bd9Sstevel@tonic-gate sizeof (ubuf->ut_line)+1); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* Try to open up the line to the receipient's terminal. */ 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate signal(SIGALRM, openfail); 1207c478bd9Sstevel@tonic-gate alarm(5); 1217c478bd9Sstevel@tonic-gate fp = fopen(&rterminal[0], "w"); 1227c478bd9Sstevel@tonic-gate alarm(0); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* Catch signals SIGHUP, SIGINT, SIGQUIT, and SIGTERM, and send */ 1257c478bd9Sstevel@tonic-gate /* <EOT> message to receipient. */ 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate setsignals(eof); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* Get the time of day, convert it to a string and throw away the */ 1307c478bd9Sstevel@tonic-gate /* year information at the end of the string. */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate time(&tod); 1337c478bd9Sstevel@tonic-gate cftime(time_buf, "%c", &tod); 1347c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext( 135*faebf794Sgtb "\r\n\007\007\007\tMessage from %s@%s [ %s ] ...\r\n"), 136*faebf794Sgtb progname, myhostname, time_buf); 1377c478bd9Sstevel@tonic-gate sleep(1); 138*faebf794Sgtb fprintf(fp, gettext("\r\nMessage to %s"), msg); 1397c478bd9Sstevel@tonic-gate fflush(fp); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* Since "end of file" received, send <EOT> message to receipient. */ 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate eof(); 1447c478bd9Sstevel@tonic-gate fclose(fp); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate free(rcp1); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* Did we find a place to talk to? If we were looking for a */ 1527c478bd9Sstevel@tonic-gate /* specific spot and didn't find it, complain and log it. */ 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate if (*rterm == '\0') 1557c478bd9Sstevel@tonic-gate if (bad > 0) { 1567c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, gettext("no place to send.\n")); 1577c478bd9Sstevel@tonic-gate return (1); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate endutxent(); 1617c478bd9Sstevel@tonic-gate return (0); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate static void 1657c478bd9Sstevel@tonic-gate setsignals(catch) 1667c478bd9Sstevel@tonic-gate void (*catch)(); 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate signal(SIGHUP, catch); 1697c478bd9Sstevel@tonic-gate signal(SIGINT, catch); 1707c478bd9Sstevel@tonic-gate signal(SIGQUIT, catch); 1717c478bd9Sstevel@tonic-gate signal(SIGTERM, catch); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate static void 1747c478bd9Sstevel@tonic-gate openfail(int i) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate extern char *rterm; 1777c478bd9Sstevel@tonic-gate #if 0 1787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1797c478bd9Sstevel@tonic-gate gettext("Timeout trying to open line(%s).\n"), 1807c478bd9Sstevel@tonic-gate rterm); 1817c478bd9Sstevel@tonic-gate #endif 1827c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("Timeout trying to open line(%s).\n"), 1837c478bd9Sstevel@tonic-gate rterm ? rterm : ""); 1847c478bd9Sstevel@tonic-gate exit(1); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate static void 1887c478bd9Sstevel@tonic-gate eof(void) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate extern FILE *fp; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s\r\n", gettext("<EOT>")); 1937c478bd9Sstevel@tonic-gate } 194