1 /* $OpenBSD: write.c,v 1.21 2003/06/10 22:20:54 deraadt Exp $ */ 2 /* $NetBSD: write.c,v 1.5 1995/08/31 21:48:32 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1989, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)write.c 8.2 (Berkeley) 4/27/95"; 45 #endif 46 static char *rcsid = "$OpenBSD: write.c,v 1.21 2003/06/10 22:20:54 deraadt Exp $"; 47 #endif /* not lint */ 48 49 #include <sys/param.h> 50 #include <sys/stat.h> 51 #include <ctype.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include <signal.h> 55 #include <time.h> 56 #include <fcntl.h> 57 #include <paths.h> 58 #include <pwd.h> 59 #include <unistd.h> 60 #include <utmp.h> 61 #include <err.h> 62 #include <vis.h> 63 64 void done(int sig); 65 void do_write(char *, char *, uid_t); 66 void wr_fputs(char *); 67 void search_utmp(char *, char *, int, char *, uid_t); 68 int term_chk(char *, int *, time_t *, int); 69 int utmp_chk(char *, char *); 70 71 int 72 main(int argc, char *argv[]) 73 { 74 char tty[MAXPATHLEN], *mytty, *cp; 75 int msgsok, myttyfd; 76 time_t atime; 77 uid_t myuid; 78 79 /* check that sender has write enabled */ 80 if (isatty(fileno(stdin))) 81 myttyfd = fileno(stdin); 82 else if (isatty(fileno(stdout))) 83 myttyfd = fileno(stdout); 84 else if (isatty(fileno(stderr))) 85 myttyfd = fileno(stderr); 86 else 87 errx(1, "can't find your tty"); 88 if (!(mytty = ttyname(myttyfd))) 89 errx(1, "can't find your tty's name"); 90 if ((cp = strrchr(mytty, '/'))) 91 mytty = cp + 1; 92 if (term_chk(mytty, &msgsok, &atime, 1)) 93 exit(1); 94 if (!msgsok) 95 warnx("you have write permission turned off"); 96 97 myuid = getuid(); 98 99 /* check args */ 100 switch (argc) { 101 case 2: 102 search_utmp(argv[1], tty, sizeof tty, mytty, myuid); 103 do_write(tty, mytty, myuid); 104 break; 105 case 3: 106 if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) - 1)) 107 argv[2] += sizeof(_PATH_DEV) - 1; 108 if (utmp_chk(argv[1], argv[2])) 109 errx(1, "%s is not logged in on %s", 110 argv[1], argv[2]); 111 if (term_chk(argv[2], &msgsok, &atime, 1)) 112 exit(1); 113 if (myuid && !msgsok) 114 errx(1, "%s has messages disabled on %s", 115 argv[1], argv[2]); 116 do_write(argv[2], mytty, myuid); 117 break; 118 default: 119 (void)fprintf(stderr, "usage: write user [tty]\n"); 120 exit(1); 121 } 122 done(0); 123 124 /* NOTREACHED */ 125 return (0); 126 } 127 128 /* 129 * utmp_chk - checks that the given user is actually logged in on 130 * the given tty 131 */ 132 int 133 utmp_chk(char *user, char *tty) 134 { 135 struct utmp u; 136 int ufd; 137 138 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 139 return(0); /* ignore error, shouldn't happen anyway */ 140 141 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 142 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 143 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 144 (void)close(ufd); 145 return(0); 146 } 147 148 (void)close(ufd); 149 return(1); 150 } 151 152 /* 153 * search_utmp - search utmp for the "best" terminal to write to 154 * 155 * Ignores terminals with messages disabled, and of the rest, returns 156 * the one with the most recent access time. Returns as value the number 157 * of the user's terminals with messages enabled, or -1 if the user is 158 * not logged in at all. 159 * 160 * Special case for writing to yourself - ignore the terminal you're 161 * writing from, unless that's the only terminal with messages enabled. 162 */ 163 void 164 search_utmp(char *user, char *tty, int ttyl, char *mytty, uid_t myuid) 165 { 166 struct utmp u; 167 time_t bestatime, atime; 168 int ufd, nloggedttys, nttys, msgsok, user_is_me; 169 char atty[UT_LINESIZE + 1]; 170 171 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 172 err(1, "%s", _PATH_UTMP); 173 174 nloggedttys = nttys = 0; 175 bestatime = 0; 176 user_is_me = 0; 177 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 178 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 179 ++nloggedttys; 180 (void)strncpy(atty, u.ut_line, UT_LINESIZE); 181 atty[UT_LINESIZE] = '\0'; 182 if (term_chk(atty, &msgsok, &atime, 0)) 183 continue; /* bad term? skip */ 184 if (myuid && !msgsok) 185 continue; /* skip ttys with msgs off */ 186 if (strcmp(atty, mytty) == 0) { 187 user_is_me = 1; 188 continue; /* don't write to yourself */ 189 } 190 ++nttys; 191 if (atime > bestatime) { 192 bestatime = atime; 193 (void)strlcpy(tty, atty, ttyl); 194 } 195 } 196 197 (void)close(ufd); 198 if (nloggedttys == 0) 199 errx(1, "%s is not logged in", user); 200 if (nttys == 0) { 201 if (user_is_me) { /* ok, so write to yourself! */ 202 (void)strlcpy(tty, mytty, ttyl); 203 return; 204 } 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", 208 user, tty); 209 } 210 211 /* 212 * term_chk - check that a terminal exists, and get the message bit 213 * and the access time 214 */ 215 int 216 term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) 217 { 218 struct stat s; 219 char path[MAXPATHLEN]; 220 221 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 222 if (stat(path, &s) < 0) { 223 if (showerror) 224 warn("%s", path); 225 return(1); 226 } 227 *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */ 228 *atimeP = s.st_atime; 229 return(0); 230 } 231 232 /* 233 * do_write - actually make the connection 234 */ 235 void 236 do_write(char *tty, char *mytty, uid_t myuid) 237 { 238 char *login, *nows; 239 struct passwd *pwd; 240 time_t now; 241 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 242 243 /* Determine our login name before the we reopen() stdout */ 244 if ((login = getlogin()) == NULL) { 245 if ((pwd = getpwuid(myuid))) 246 login = pwd->pw_name; 247 else 248 login = "???"; 249 } 250 251 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 252 if ((freopen(path, "w", stdout)) == NULL) 253 err(1, "%s", path); 254 255 /* revoke privs, now that we have opened the tty */ 256 setegid(getgid()); 257 setgid(getgid()); 258 259 (void)signal(SIGINT, done); 260 (void)signal(SIGHUP, done); 261 262 /* print greeting */ 263 if (gethostname(host, sizeof(host)) < 0) 264 (void)strlcpy(host, "???", sizeof host); 265 now = time((time_t *)NULL); 266 nows = ctime(&now); 267 nows[16] = '\0'; 268 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 269 login, host, mytty, nows + 11); 270 271 while (fgets(line, sizeof(line), stdin) != NULL) 272 wr_fputs(line); 273 } 274 275 /* 276 * done - cleanup and exit 277 */ 278 void 279 done(int sig) 280 { 281 (void)write(STDOUT_FILENO, "EOF\r\n", 5); 282 if (sig) 283 _exit(0); 284 else 285 exit(0); 286 } 287 288 /* 289 * wr_fputs - like fputs(), but makes control characters visible and 290 * turns \n into \r\n 291 */ 292 void 293 wr_fputs(char *s) 294 { 295 u_char c; 296 char visout[5], *s2; 297 298 #define PUTC(c) if (putchar(c) == EOF) goto err; 299 300 for (; *s != '\0'; ++s) { 301 c = toascii(*s); 302 if (c == '\n') { 303 PUTC('\r'); 304 PUTC('\n'); 305 continue; 306 } 307 vis(visout, c, VIS_SAFE|VIS_NOSLASH, s[1]); 308 for (s2 = visout; *s2; s2++) 309 PUTC(*s2); 310 } 311 return; 312 313 err: err(1, NULL); 314 #undef PUTC 315 } 316