1 /* $NetBSD: write.c,v 1.27 2011/09/06 18:46:35 joerg Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)write.c 8.2 (Berkeley) 4/27/95";
45 __RCSID("$NetBSD: write.c,v 1.27 2011/09/06 18:46:35 joerg Exp $");
49 #include <sys/types.h>
50 #include <sys/param.h>
65 #include "utmpentry.h"
68 __dead
static void done(int);
69 static void do_write(int, const char *, const uid_t
);
70 static void wr_fputs(char *);
71 static int search_utmp(char *, char *, uid_t
, gid_t
);
72 static int utmp_chk(const char *, const char *);
75 main(int argc
, char **argv
)
81 gid_t saved_egid
= getegid();
83 if (setegid(getgid()) == -1)
88 mytty
= check_sender(&atime
, myuid
, saved_egid
);
93 ttyfd
= search_utmp(argv
[1], mytty
, myuid
, saved_egid
);
96 if (!strncmp(argv
[2], _PATH_DEV
, strlen(_PATH_DEV
)))
97 argv
[2] += strlen(_PATH_DEV
);
98 if (uid_from_user(argv
[1], &uid
) == -1)
99 errx(1, "%s: unknown user", argv
[1]);
100 if (utmp_chk(argv
[1], argv
[2]))
101 errx(1, "%s is not logged in on %s",
103 ttyfd
= term_chk(uid
, argv
[2], &msgsok
, &atime
, 0, saved_egid
);
105 err(1, "%s%s", _PATH_DEV
, argv
[2]);
106 if (myuid
&& !msgsok
)
107 errx(1, "%s has messages disabled on %s",
111 (void)fprintf(stderr
, "usage: write user [tty]\n");
114 if (setgid(getgid()) == -1)
116 do_write(ttyfd
, mytty
, myuid
);
125 * utmp_chk - checks that the given user is actually logged in on
129 utmp_chk(const char *user
, const char *tty
)
131 struct utmpentry
*ep
;
133 (void)getutentries(NULL
, &ep
);
135 for (; ep
; ep
= ep
->next
)
136 if (strcmp(user
, ep
->name
) == 0 && strcmp(tty
, ep
->line
) == 0)
142 * search_utmp - search utmp for the "best" terminal to write to
144 * Ignores terminals with messages disabled, and of the rest, returns
145 * the one with the most recent access time. Returns as value the number
146 * of the user's terminals with messages enabled, or -1 if the user is
147 * not logged in at all.
149 * Special case for writing to yourself - ignore the terminal you're
150 * writing from, unless that's the only terminal with messages enabled.
153 search_utmp(char *user
, char *mytty
, uid_t myuid
, gid_t saved_egid
)
155 char tty
[MAXPATHLEN
];
156 time_t bestatime
, atime
;
157 int nloggedttys
, nttys
, msgsok
, user_is_me
;
158 struct utmpentry
*ep
;
162 if (uid_from_user(user
, &uid
) == -1)
163 errx(1, "%s: unknown user", user
);
165 (void)getutentries(NULL
, &ep
);
167 nloggedttys
= nttys
= 0;
171 for (; ep
; ep
= ep
->next
)
172 if (strcmp(user
, ep
->name
) == 0) {
174 nfd
= term_chk(uid
, ep
->line
, &msgsok
, &atime
, 0,
177 continue; /* bad term? skip */
178 if (myuid
&& !msgsok
) {
180 continue; /* skip ttys with msgs off */
182 if (strcmp(ep
->line
, mytty
) == 0) {
188 continue; /* don't write to yourself */
191 if (atime
> bestatime
) {
193 (void)strlcpy(tty
, ep
->line
, sizeof(tty
));
200 if (nloggedttys
== 0)
201 errx(1, "%s is not logged in", user
);
203 if (user_is_me
) /* ok, so write to yourself! */
205 errx(1, "%s has messages disabled", user
);
206 } else if (nttys
> 1)
207 warnx("%s is logged in more than once; writing to %s",
213 * do_write - actually make the connection
216 do_write(int ttyfd
, const char *mytty
, const uid_t myuid
)
222 char host
[MAXHOSTNAMELEN
+ 1], line
[512];
224 /* Determine our login name before we re-open stdout */
225 if ((login
= getlogin()) == NULL
) {
226 if ((pwd
= getpwuid(myuid
)) != NULL
)
227 login
= pwd
->pw_name
;
231 if (dup2(ttyfd
, STDOUT_FILENO
) == -1)
234 (void)signal(SIGINT
, done
);
235 (void)signal(SIGHUP
, done
);
239 if (gethostname(host
, sizeof(host
)) < 0)
240 (void)strlcpy(host
, "???", sizeof(host
));
242 host
[sizeof(host
) - 1] = '\0';
246 (void)printf("\r\n\a\a\aMessage from %s@%s on %s at %s ...\r\n",
247 login
, host
, mytty
, nows
+ 11);
249 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
254 * done - cleanup and exit
260 (void)write(STDOUT_FILENO
, "EOF\r\n", sizeof("EOF\r\n") - 1);
268 * wr_fputs - like fputs(), but makes control characters visible and
276 #define PUTC(c) if (putchar(c) == EOF) goto err;
278 for (; *s
!= '\0'; ++s
) {
282 } else if (!isprint(c
) && !isspace(c
) && c
!= '\a') {
284 c
^= 0x40; /* DEL to ?, others to alpha */