4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/types.h>
40 #include <sys/utsname.h>
58 #define DATE_FMT "%a %b %e %H:%M:%S"
59 #define UTMP_HACK /* work around until utmpx is world writable */
62 * %a abbreviated weekday name
63 * %b abbreviated month name
65 * %H hour - 24 hour clock
71 static int permit1(int);
72 static int permit(char *);
73 static int readcsi(int, char *, int);
74 static void setsignals();
75 static void shellcmd(char *);
76 static void openfail();
79 static struct utsname utsn
;
81 static FILE *fp
; /* File pointer for receipient's terminal */
82 static char *rterm
, *receipient
; /* Pointer to receipient's terminal & name */
86 main(int argc
, char **argv
)
90 static struct utmpx self
;
91 char ownname
[sizeof (self
.ut_user
) + 1];
92 static char rterminal
[sizeof ("/dev/") + sizeof (self
.ut_line
)] =
94 extern char *rterm
, *receipient
;
95 char *terminal
, *ownterminal
, *oterminal
;
98 char input
[134+MB_LEN_MAX
];
102 struct passwd
*passptr
;
103 char badterm
[20][20];
112 (void) setlocale(LC_ALL
, "");
113 #if !defined(TEXT_DOMAIN)
114 #define TEXT_DOMAIN "SYS_TEST"
116 (void) textdomain(TEXT_DOMAIN
);
118 while ((c
= getopt(argc
, argv
, "")) != EOF
)
121 (void) fprintf(stderr
, "Usage: write %s\n",
122 gettext("user_name [terminal]"));
127 thissys
= utsn
.nodename
;
129 /* Set "rterm" to location where receipient's terminal will go. */
131 rterm
= &rterminal
[sizeof ("/dev/") - 1];
135 (void) fprintf(stderr
, "Usage: write %s\n",
136 gettext("user_name [terminal]"));
141 receipient
= *++argv
;
144 /* Was a terminal name supplied? If so, save it. */
147 (void) fprintf(stderr
, "Usage: write %s\n",
148 gettext("user_name [terminal]"));
154 /* One of the standard file descriptors must be attached to a */
155 /* terminal in "/dev". */
157 if ((ownterminal
= ttyname(fileno(stdin
))) == NULL
&&
158 (ownterminal
= ttyname(fileno(stdout
))) == NULL
&&
159 (ownterminal
= ttyname(fileno(stderr
))) == NULL
) {
160 (void) fprintf(stderr
,
161 gettext("I cannot determine your terminal name."
162 " No reply possible.\n"));
163 ownterminal
= "/dev/???";
167 * Set "ownterminal" past the "/dev/" at the beginning of
170 oterminal
= ownterminal
+ sizeof ("/dev/")-1;
173 * Scan through the "utmpx" file for your own entry and the
174 * entry for the person we want to send to.
176 for (self
.ut_pid
= 0, count
= 0; (ubuf
= getutxent()) != NULL
; ) {
177 /* Is this a USER_PROCESS entry? */
179 if (ubuf
->ut_type
== USER_PROCESS
) {
180 /* Is it our entry? (ie. The line matches ours?) */
182 if (strncmp(&ubuf
->ut_line
[0], oterminal
,
183 sizeof (ubuf
->ut_line
)) == 0) self
= *ubuf
;
185 /* Is this the person we want to send to? */
187 if (strncmp(receipient
, &ubuf
->ut_user
[0],
188 sizeof (ubuf
->ut_user
)) == 0) {
189 /* If a terminal name was supplied, is this login at the correct */
190 /* terminal? If not, ignore. If it is right place, copy over the */
193 if (terminal
!= NULL
) {
194 if (strncmp(terminal
, &ubuf
->ut_line
[0],
195 sizeof (ubuf
->ut_line
)) == 0) {
196 strlcpy(rterm
, &ubuf
->ut_line
[0],
197 sizeof (rterminal
) - (rterm
- rterminal
));
198 if (myuid
&& !permit(rterminal
)) {
205 /* If no terminal was supplied, then take this terminal if no */
206 /* other terminal has been encountered already. */
210 /* If this is the first encounter, copy the string into */
213 if (*rterm
== '\0') {
214 strlcpy(rterm
, &ubuf
->ut_line
[0],
215 sizeof (rterminal
) - (rterm
- rterminal
));
216 if (myuid
&& !permit(rterminal
)) {
218 strlcpy(badterm
[bad
++], rterm
,
219 sizeof (badterm
[bad
++]));
222 } else if (bad
> 0) {
223 (void) fprintf(stderr
,
225 "%s is logged on more than one place.\n"
226 "You are connected to \"%s\".\nOther locations are:\n"),
228 for (i
= 0; i
< bad
; i
++)
229 (void) fprintf(stderr
, "%s\n", badterm
[i
]);
233 /* If this is the second terminal, print out the first. In all */
234 /* cases of multiple terminals, list out all the other terminals */
235 /* so the user can restart knowing what her/his choices are. */
237 else if (terminal
== NULL
) {
238 if (count
== 1 && bad
== 0) {
239 (void) fprintf(stderr
,
241 "%s is logged on more than one place.\n"
242 "You are connected to \"%s\".\nOther locations are:\n"),
245 fwrite(&ubuf
->ut_line
[0], sizeof (ubuf
->ut_line
),
247 (void) fprintf(stderr
, "\n");
251 } /* End of "else" */
252 } /* End of "else if (strncmp" */
253 } /* End of "if (USER_PROCESS" */
254 } /* End of "for(count=0" */
256 /* Did we find a place to talk to? If we were looking for a */
257 /* specific spot and didn't find it, complain and quit. */
259 if (terminal
!= NULL
&& *rterm
== '\0') {
261 (void) fprintf(stderr
, gettext("Permission denied.\n"));
265 if (strlcat(rterminal
, terminal
, sizeof (rterminal
)) >=
266 sizeof (rterminal
)) {
267 (void) fprintf(stderr
,
268 gettext("Terminal name too long.\n"));
271 if (self
.ut_pid
== 0) {
272 if ((passptr
= getpwuid(getuid())) == NULL
) {
273 (void) fprintf(stderr
,
274 gettext("Cannot determine who you are.\n"));
277 (void) strlcpy(&ownname
[0], &passptr
->pw_name
[0],
280 (void) strlcpy(&ownname
[0], self
.ut_user
,
281 sizeof (self
.ut_user
));
283 if (!permit(rterminal
)) {
284 (void) fprintf(stderr
,
285 gettext("%s permission denied\n"), terminal
);
289 (void) fprintf(stderr
, gettext("%s is not at \"%s\".\n"),
290 receipient
, terminal
);
292 #endif /* UTMP_HACK */
296 /* If we were just looking for anyplace to talk and didn't find */
297 /* one, complain and quit. */
298 /* If permissions prevent us from sending to this person - exit */
300 else if (*rterm
== '\0') {
302 (void) fprintf(stderr
, gettext("Permission denied.\n"));
304 (void) fprintf(stderr
,
305 gettext("%s is not logged on.\n"), receipient
);
309 /* Did we find our own entry? */
311 else if (self
.ut_pid
== 0) {
312 /* Use the user id instead of utmp name if the entry in the */
313 /* utmp file couldn't be found. */
315 if ((passptr
= getpwuid(getuid())) == (struct passwd
*)NULL
) {
316 (void) fprintf(stderr
,
317 gettext("Cannot determine who you are.\n"));
320 strncpy(&ownname
[0], &passptr
->pw_name
[0], sizeof (ownname
));
324 strncpy(&ownname
[0], self
.ut_user
, sizeof (self
.ut_user
));
326 ownname
[sizeof (ownname
)-1] = '\0';
329 (void) fprintf(stderr
,
330 gettext("Warning: You have your terminal set to \"mesg -n\"."
331 " No reply possible.\n"));
332 /* Close the utmpx files. */
336 /* Try to open up the line to the receipient's terminal. */
338 signal(SIGALRM
, openfail
);
340 fp
= fopen(&rterminal
[0], "w");
343 /* Make sure executed subshell doesn't inherit this fd - close-on-exec */
345 if (fcntl(fileno(fp
), F_SETFD
, FD_CLOEXEC
) < 0) {
346 perror("fcntl(F_SETFD)");
350 /* Catch signals SIGHUP, SIGINT, SIGQUIT, and SIGTERM, and send */
351 /* <EOT> message to receipient before dying away. */
355 /* Get the time of day, convert it to a string and throw away the */
356 /* year information at the end of the string. */
359 (void) strftime(time_buf
, sizeof (time_buf
),
360 dcgettext(NULL
, DATE_FMT
, LC_TIME
), localtime(&tod
));
363 gettext("\n\007\007\007\tMessage from %s on %s (%s) [ %s ] ...\n"),
364 &ownname
[0], thissys
, oterminal
, time_buf
);
366 (void) fprintf(stderr
, "\007\007");
368 /* Get input from user and send to receipient unless it begins */
369 /* with a !, when it is to be a shell command. */
371 while ((i
= readcsi(0, &input
[0], sizeof (input
))) > 0) {
373 /* Is this a shell command? */
375 if ((newline
) && (*ptr
== '!'))
378 /* Send line to the receipient. */
381 if (myuid
&& !permit1(fileno(fp
))) {
382 (void) fprintf(stderr
,
383 gettext("Can no longer write to %s\n"), rterminal
);
388 * All non-printable characters are displayed using a special notation:
389 * Control characters shall be displayed using the two character
390 * sequence of ^ (carat) and the ASCII character - decimal 64 greater
391 * that the character being encoded - eg., a \003 is displayed ^C.
392 * Characters with the eighth bit set shall be displayed using
393 * the three or four character meta notation - e.g., \372 is
394 * displayed M-z and \203 is displayed M-^C.
398 for (bp
= &input
[0]; --i
>= 0; bp
++) {
404 *bp
== '\t' || *bp
== '\n' ||
405 *bp
== '\r' || *bp
== '\013' ||
408 } else if (((n
= mbtowc(&wc
, bp
, MB_CUR_MAX
)) > 0) &&
410 for (; n
> 0; --n
, --i
, ++bp
)
420 /* add decimal 64 to the control character */
421 putc(*bp
+ 0100, fp
);
428 if (ferror(fp
) || feof(fp
)) {
430 "\n\007Write failed (%s logged out?)\n"),
439 /* Since "end of file" received, send <EOT> message to receipient. */
450 signal(SIGHUP
, catch);
451 signal(SIGINT
, catch);
452 signal(SIGQUIT
, catch);
453 signal(SIGTERM
, catch);
461 register pid_t child
;
464 if ((child
= fork()) == (pid_t
)FAILURE
)
466 (void) fprintf(stderr
,
467 gettext("Unable to fork. Try again later.\n"));
469 } else if (child
== (pid_t
)0) {
470 /* Reset the signals to the default actions and exec a shell. */
472 if (setgid(getgid()) < 0)
474 execl("/usr/bin/sh", "sh", "-c", command
, 0);
479 /* Allow user to type <del> and <quit> without dying during */
482 signal(SIGINT
, SIG_IGN
);
483 signal(SIGQUIT
, SIG_IGN
);
485 /* As parent wait around for user to finish spunoff command. */
487 while (wait(NULL
) != child
);
489 /* Reset the signals to their normal state. */
493 (void) fprintf(stdout
, "!\n");
499 extern char *rterm
, *receipient
;
501 (void) fprintf(stderr
,
502 gettext("Timeout trying to open %s's line(%s).\n"),
512 (void) fprintf(fp
, "%s\n", gettext("<EOT>"));
517 * permit: check mode of terminal - if not writable by all disallow writing to
518 * (even the user him/herself cannot therefore write to their own tty)
528 if ((fildes
= open(term
, O_WRONLY
|O_NOCTTY
)) < 0)
530 /* check if the device really is a tty */
531 if (!isatty(fildes
)) {
532 (void) fprintf(stderr
,
533 gettext("%s in utmpx is not a tty\n"), term
);
534 openlog("write", 0, LOG_AUTH
);
535 syslog(LOG_CRIT
, "%s in utmpx is not a tty\n", term
);
542 return (buf
.st_mode
& (S_IWGRP
|S_IWOTH
));
548 * permit1: check mode of terminal - if not writable by all disallow writing
549 * to (even the user him/herself cannot therefore write to their own tty)
552 /* this is used with fstat (which is faster than stat) where possible */
561 return (buf
.st_mode
& (S_IWGRP
|S_IWOTH
));
566 * Read a string of multi-byte characters from specified file.
567 * The requested # of bytes are attempted to read.
568 * readcsi() tries to complete the last multibyte character
569 * by calling mbtowc(), if the leftovers form mbtowc(),
570 * left the last char imcomplete, moves into delta_spool to use later,
571 * next called. The caller must reserve
572 * nbytereq+MB_LEN_MAX bytes for the buffer. When the attempt
573 * is failed, it truncate the last char.
574 * Returns the number of bytes that constitutes the valid multi-byte characters.
578 static int readcsi(d
, buf
, nbytereq
)
583 static char delta_pool
[MB_LEN_MAX
* 2];
584 static char delta_size
;
585 char *cp
, *nextp
, *lastp
;
590 memcpy(buf
, delta_pool
, delta_size
);
591 cp
= buf
+ delta_size
;
592 r_size
= nbytereq
- delta_size
;
598 if ((r_size
= read(d
, cp
, r_size
)) < 0)
600 if ((n
= delta_size
+ r_size
) <= 0)
603 /* Scan the result to test the completeness of each EUC characters. */
605 lastp
= buf
+ n
; /* Lastp points to the first junk byte. */
606 while (nextp
< lastp
) {
607 if ((n
= (lastp
- nextp
)) > (unsigned int)MB_CUR_MAX
)
608 n
= (unsigned int)MB_CUR_MAX
;
609 if ((n
= mbtowc((wchar_t *)0, nextp
, n
)) <= 0) {
610 if ((lastp
- nextp
) < (unsigned int)MB_CUR_MAX
)
616 /* How many bytes needed to complete the last char? */
617 delta_size
= lastp
- nextp
;
618 if (delta_size
> 0) {
619 if (nextp
[delta_size
- 1] != '\n') {
620 /* the remnants store into delta_pool */
621 memcpy(delta_pool
, nextp
, delta_size
);
626 return (nextp
-buf
); /* Return # of bytes. */