2
* Copyright (c) 1989, 1993
3
* The Regents of the University of California. All rights reserved.
5
* This code is derived from software contributed to Berkeley by
6
* Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
* Modified for Linux, Mon Mar 8 18:16:24 1993, faith@cs.unc.edu
37
* Wed Jun 22 21:41:56 1994, faith@cs.unc.edu:
38
* Added fix from Mike Grupenhoff (kashmir@umiacs.umd.edu)
39
* Mon Jul 1 17:01:39 MET DST 1996, janl@math.uio.no:
40
* - Added fix from David.Chapell@mail.trincoll.edu enabeling daemons
42
* - ANSIed it since I was working on it anyway.
43
* 1999-02-22 Arkadiusz Miļæ½kiewicz <misiek@pld.ORG.PL>
44
* - added Native Language Support
57
#include <sys/param.h>
60
#include <asm/param.h>
63
#include "carefulputc.h"
66
static void __attribute__ ((__noreturn__)) usage(FILE * out);
67
void search_utmp(char *, char *, char *, uid_t);
68
void do_write(char *, char *, uid_t);
69
void wr_fputs(char *);
70
static void __attribute__ ((__noreturn__)) done(int);
71
int term_chk(char *, int *, time_t *, int);
72
int utmp_chk(char *, char *);
76
static void __attribute__ ((__noreturn__)) usage(FILE * out)
78
fputs(_("\nUsage:\n"), out);
80
_(" %s [options] <user> [<ttyname>]\n"),
81
program_invocation_short_name);
83
fputs(_("\nOptions:\n"), out);
84
fputs(_(" -V, --version output version information and exit\n"
85
" -h, --help display this help and exit\n\n"), out);
87
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
90
int main(int argc, char **argv)
94
int msgsok, myttyfd, c;
95
char tty[MAXPATHLEN], *mytty;
97
static const struct option longopts[] = {
98
{"version", no_argument, NULL, 'V'},
99
{"help", no_argument, NULL, 'h'},
103
setlocale(LC_ALL, "");
104
bindtextdomain(PACKAGE, LOCALEDIR);
107
while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
110
printf(_("%s from %s\n"),
111
program_invocation_short_name,
122
/* check that sender has write enabled */
123
if (isatty(fileno(stdin)))
124
myttyfd = fileno(stdin);
125
else if (isatty(fileno(stdout)))
126
myttyfd = fileno(stdout);
127
else if (isatty(fileno(stderr)))
128
myttyfd = fileno(stderr);
133
if (!(mytty = ttyname(myttyfd)))
135
_("can't find your tty's name"));
138
* We may have /dev/ttyN but also /dev/pts/xx. Below,
139
* term_chk() will put "/dev/" in front, so remove that
142
if (!strncmp(mytty, "/dev/", 5))
144
if (term_chk(mytty, &msgsok, &atime, 1))
148
_("you have write permission turned off"));
158
search_utmp(argv[1], tty, mytty, myuid);
159
do_write(tty, mytty, myuid);
162
if (!strncmp(argv[2], "/dev/", 5))
164
if (utmp_chk(argv[1], argv[2]))
166
_("%s is not logged in on %s"),
168
if (term_chk(argv[2], &msgsok, &atime, 1))
170
if (myuid && !msgsok)
172
_("%s has messages disabled on %s"),
174
do_write(argv[2], mytty, myuid);
187
* utmp_chk - checks that the given user is actually logged in on
190
int utmp_chk(char *user, char *tty)
196
utmpname(_PATH_UTMP);
199
while ((uptr = getutent())) {
200
memcpy(&u, uptr, sizeof(u));
201
if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
202
strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
213
* search_utmp - search utmp for the "best" terminal to write to
215
* Ignores terminals with messages disabled, and of the rest, returns
216
* the one with the most recent access time. Returns as value the number
217
* of the user's terminals with messages enabled, or -1 if the user is
218
* not logged in at all.
220
* Special case for writing to yourself - ignore the terminal you're
221
* writing from, unless that's the only terminal with messages enabled.
223
void search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
227
time_t bestatime, atime;
228
int nloggedttys, nttys, msgsok, user_is_me;
229
char atty[sizeof(u.ut_line) + 1];
231
utmpname(_PATH_UTMP);
234
nloggedttys = nttys = 0;
237
while ((uptr = getutent())) {
238
memcpy(&u, uptr, sizeof(u));
239
if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
241
strncpy(atty, u.ut_line, sizeof(u.ut_line));
242
atty[sizeof(u.ut_line)] = '\0';
243
if (term_chk(atty, &msgsok, &atime, 0))
246
if (myuid && !msgsok)
247
/* skip ttys with msgs off */
249
if (strcmp(atty, mytty) == 0) {
251
/* don't write to yourself */
254
if (u.ut_type != USER_PROCESS)
255
/* it's not a valid entry */
258
if (atime > bestatime) {
266
if (nloggedttys == 0)
267
errx(EXIT_FAILURE, _("%s is not logged in"), user);
270
/* ok, so write to yourself! */
274
errx(EXIT_FAILURE, _("%s has messages disabled"), user);
275
} else if (nttys > 1) {
276
warnx(_("%s is logged in more than once; writing to %s"),
282
* term_chk - check that a terminal exists, and get the message bit
283
* and the access time
285
int term_chk(char *tty, int *msgsokP, time_t * atimeP, int showerror)
288
char path[MAXPATHLEN];
290
if (strlen(tty) + 6 > sizeof(path))
292
sprintf(path, "/dev/%s", tty);
293
if (stat(path, &s) < 0) {
299
/* group write bit and group ownership */
300
*msgsokP = (s.st_mode & (S_IWRITE >> 3)) && myegid == s.st_gid;
301
*atimeP = s.st_atime;
306
* do_write - actually make the connection
308
void do_write(char *tty, char *mytty, uid_t myuid)
310
char *login, *pwuid, *nows;
313
char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
315
/* Determine our login name(s) before the we reopen() stdout */
316
if ((pwd = getpwuid(myuid)) != NULL)
317
pwuid = pwd->pw_name;
320
if ((login = getlogin()) == NULL)
323
if (strlen(tty) + 6 > sizeof(path))
324
errx(EXIT_FAILURE, _("tty path %s too long"), tty);
325
snprintf(path, sizeof(path), "/dev/%s", tty);
326
if ((freopen(path, "w", stdout)) == NULL)
327
err(EXIT_FAILURE, "%s", path);
329
signal(SIGINT, done);
330
signal(SIGHUP, done);
333
if (gethostname(host, sizeof(host)) < 0)
335
now = time((time_t *) NULL);
338
printf("\r\n\007\007\007");
339
if (strcmp(login, pwuid))
340
printf(_("Message from %s@%s (as %s) on %s at %s ..."),
341
login, host, pwuid, mytty, nows + 11);
343
printf(_("Message from %s@%s on %s at %s ..."),
344
login, host, mytty, nows + 11);
347
while (fgets(line, sizeof(line), stdin) != NULL)
352
* done - cleanup and exit
354
static void __attribute__ ((__noreturn__))
355
done(int dummy __attribute__ ((__unused__)))
362
* wr_fputs - like fputs(), but makes control characters visible and
363
* turns \n into \r\n.
365
void wr_fputs(char *s)
369
#define PUTC(c) if (carefulputc(c, stdout) == EOF) \
370
err(EXIT_FAILURE, _("carefulputc failed"));