1 /* $NetBSD: write.c,v 1.24 2006/06/17 08:22:06 elad 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.24 2006/06/17 08:22:06 elad Exp $");
49 #include <sys/types.h>
50 #include <sys/param.h>
65 #include "utmpentry.h"
69 void do_write(int, const char *, const uid_t
);
70 void wr_fputs(char *);
71 int search_utmp(char *, char *, uid_t
, gid_t
);
72 int utmp_chk(const char *, const char *);
73 int main(int, char **);
77 main(int argc
, char **argv
)
83 gid_t saved_egid
= getegid();
85 if (setegid(getgid()) == -1)
90 mytty
= check_sender(&atime
, myuid
, saved_egid
);
95 ttyfd
= search_utmp(argv
[1], mytty
, myuid
, saved_egid
);
98 if (!strncmp(argv
[2], _PATH_DEV
, strlen(_PATH_DEV
)))
99 argv
[2] += strlen(_PATH_DEV
);
100 if (uid_from_user(argv
[1], &uid
) == -1)
101 errx(1, "%s: unknown user", argv
[1]);
102 if (utmp_chk(argv
[1], argv
[2]))
103 errx(1, "%s is not logged in on %s",
105 ttyfd
= term_chk(uid
, argv
[2], &msgsok
, &atime
, 0, saved_egid
);
107 err(1, "%s%s", _PATH_DEV
, argv
[2]);
108 if (myuid
&& !msgsok
)
109 errx(1, "%s has messages disabled on %s",
113 (void)fprintf(stderr
, "usage: write user [tty]\n");
116 if (setgid(getgid()) == -1)
118 do_write(ttyfd
, mytty
, myuid
);
127 * utmp_chk - checks that the given user is actually logged in on
131 utmp_chk(const char *user
, const char *tty
)
133 struct utmpentry
*ep
;
135 (void)getutentries(NULL
, &ep
);
137 for (; ep
; ep
= ep
->next
)
138 if (strcmp(user
, ep
->name
) == 0 && strcmp(tty
, ep
->line
) == 0)
144 * search_utmp - search utmp for the "best" terminal to write to
146 * Ignores terminals with messages disabled, and of the rest, returns
147 * the one with the most recent access time. Returns as value the number
148 * of the user's terminals with messages enabled, or -1 if the user is
149 * not logged in at all.
151 * Special case for writing to yourself - ignore the terminal you're
152 * writing from, unless that's the only terminal with messages enabled.
155 search_utmp(char *user
, char *mytty
, uid_t myuid
, gid_t saved_egid
)
157 char tty
[MAXPATHLEN
];
158 time_t bestatime
, atime
;
159 int nloggedttys
, nttys
, msgsok
, user_is_me
;
160 struct utmpentry
*ep
;
164 if (uid_from_user(user
, &uid
) == -1)
165 errx(1, "%s: unknown user", user
);
167 (void)getutentries(NULL
, &ep
);
169 nloggedttys
= nttys
= 0;
173 for (; ep
; ep
= ep
->next
)
174 if (strcmp(user
, ep
->name
) == 0) {
176 nfd
= term_chk(uid
, ep
->line
, &msgsok
, &atime
, 0,
179 continue; /* bad term? skip */
180 if (myuid
&& !msgsok
) {
182 continue; /* skip ttys with msgs off */
184 if (strcmp(ep
->line
, mytty
) == 0) {
190 continue; /* don't write to yourself */
193 if (atime
> bestatime
) {
195 (void)strlcpy(tty
, ep
->line
, sizeof(tty
));
202 if (nloggedttys
== 0)
203 errx(1, "%s is not logged in", user
);
205 if (user_is_me
) /* ok, so write to yourself! */
207 errx(1, "%s has messages disabled", user
);
208 } else if (nttys
> 1)
209 warnx("%s is logged in more than once; writing to %s",
215 * do_write - actually make the connection
218 do_write(int ttyfd
, const char *mytty
, const uid_t myuid
)
224 char host
[MAXHOSTNAMELEN
+ 1], line
[512];
226 /* Determine our login name before we re-open stdout */
227 if ((login
= getlogin()) == NULL
) {
228 if ((pwd
= getpwuid(myuid
)) != NULL
)
229 login
= pwd
->pw_name
;
233 if (dup2(ttyfd
, STDOUT_FILENO
) == -1)
236 (void)signal(SIGINT
, done
);
237 (void)signal(SIGHUP
, done
);
241 if (gethostname(host
, sizeof(host
)) < 0)
242 (void)strlcpy(host
, "???", sizeof(host
));
244 host
[sizeof(host
) - 1] = '\0';
245 now
= time((time_t *)NULL
);
248 (void)printf("\r\n\a\a\aMessage from %s@%s on %s at %s ...\r\n",
249 login
, host
, mytty
, nows
+ 11);
251 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
256 * done - cleanup and exit
262 (void)write(STDOUT_FILENO
, "EOF\r\n", sizeof("EOF\r\n") - 1);
270 * wr_fputs - like fputs(), but makes control characters visible and
278 #define PUTC(c) if (putchar(c) == EOF) goto err;
280 for (; *s
!= '\0'; ++s
) {
284 } else if (!isprint(c
) && !isspace(c
) && c
!= '\a') {
286 c
^= 0x40; /* DEL to ?, others to alpha */