1 /* $OpenBSD: write.c,v 1.25 2008/07/06 13:42:35 sobrado 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.25 2008/07/06 13:42:35 sobrado 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 <stdlib.h> 54 #include <string.h> 55 #include <signal.h> 56 #include <time.h> 57 #include <fcntl.h> 58 #include <paths.h> 59 #include <pwd.h> 60 #include <unistd.h> 61 #include <utmp.h> 62 #include <err.h> 63 #include <vis.h> 64 65 void done(int sig); 66 void do_write(char *, char *, uid_t); 67 void wr_fputs(char *); 68 void search_utmp(char *, char *, int, char *, uid_t); 69 int term_chk(char *, int *, time_t *, int); 70 int utmp_chk(char *, char *); 71 72 int 73 main(int argc, char *argv[]) 74 { 75 char tty[MAXPATHLEN], *mytty, *cp; 76 int msgsok, myttyfd; 77 time_t atime; 78 uid_t myuid; 79 80 /* check that sender has write enabled */ 81 if (isatty(fileno(stdin))) 82 myttyfd = fileno(stdin); 83 else if (isatty(fileno(stdout))) 84 myttyfd = fileno(stdout); 85 else if (isatty(fileno(stderr))) 86 myttyfd = fileno(stderr); 87 else 88 errx(1, "can't find your tty"); 89 if (!(mytty = ttyname(myttyfd))) 90 errx(1, "can't find your tty's name"); 91 if ((cp = strrchr(mytty, '/'))) 92 mytty = cp + 1; 93 if (term_chk(mytty, &msgsok, &atime, 1)) 94 exit(1); 95 if (!msgsok) 96 warnx("you have write permission turned off"); 97 98 myuid = getuid(); 99 100 /* check args */ 101 switch (argc) { 102 case 2: 103 search_utmp(argv[1], tty, sizeof tty, mytty, myuid); 104 do_write(tty, mytty, myuid); 105 break; 106 case 3: 107 if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) - 1)) 108 argv[2] += sizeof(_PATH_DEV) - 1; 109 if (utmp_chk(argv[1], argv[2])) 110 errx(1, "%s is not logged in on %s", 111 argv[1], argv[2]); 112 if (term_chk(argv[2], &msgsok, &atime, 1)) 113 exit(1); 114 if (myuid && !msgsok) 115 errx(1, "%s has messages disabled on %s", 116 argv[1], argv[2]); 117 do_write(argv[2], mytty, myuid); 118 break; 119 default: 120 (void)fprintf(stderr, "usage: write user [ttyname]\n"); 121 exit(1); 122 } 123 done(0); 124 125 /* NOTREACHED */ 126 return (0); 127 } 128 129 /* 130 * utmp_chk - checks that the given user is actually logged in on 131 * the given tty 132 */ 133 int 134 utmp_chk(char *user, char *tty) 135 { 136 struct utmp u; 137 int ufd; 138 139 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 140 return(1); /* no utmp, cannot talk to users */ 141 142 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 143 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 144 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 145 (void)close(ufd); 146 return(0); 147 } 148 149 (void)close(ufd); 150 return(1); 151 } 152 153 /* 154 * search_utmp - search utmp for the "best" terminal to write to 155 * 156 * Ignores terminals with messages disabled, and of the rest, returns 157 * the one with the most recent access time. Returns as value the number 158 * of the user's terminals with messages enabled, or -1 if the user is 159 * not logged in at all. 160 * 161 * Special case for writing to yourself - ignore the terminal you're 162 * writing from, unless that's the only terminal with messages enabled. 163 */ 164 void 165 search_utmp(char *user, char *tty, int ttyl, char *mytty, uid_t myuid) 166 { 167 struct utmp u; 168 time_t bestatime, atime; 169 int ufd, nloggedttys, nttys, msgsok, user_is_me; 170 char atty[UT_LINESIZE + 1]; 171 172 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 173 err(1, "%s", _PATH_UTMP); 174 175 nloggedttys = nttys = 0; 176 bestatime = 0; 177 user_is_me = 0; 178 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 179 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 180 ++nloggedttys; 181 (void)strncpy(atty, u.ut_line, UT_LINESIZE); 182 atty[UT_LINESIZE] = '\0'; 183 if (term_chk(atty, &msgsok, &atime, 0)) 184 continue; /* bad term? skip */ 185 if (myuid && !msgsok) 186 continue; /* skip ttys with msgs off */ 187 if (strcmp(atty, mytty) == 0) { 188 user_is_me = 1; 189 continue; /* don't write to yourself */ 190 } 191 ++nttys; 192 if (atime > bestatime) { 193 bestatime = atime; 194 (void)strlcpy(tty, atty, ttyl); 195 } 196 } 197 198 (void)close(ufd); 199 if (nloggedttys == 0) 200 errx(1, "%s is not logged in", user); 201 if (nttys == 0) { 202 if (user_is_me) { /* ok, so write to yourself! */ 203 (void)strlcpy(tty, mytty, ttyl); 204 return; 205 } 206 errx(1, "%s has messages disabled", user); 207 } else if (nttys > 1) 208 warnx("%s is logged in more than once; writing to %s", 209 user, tty); 210 } 211 212 /* 213 * term_chk - check that a terminal exists, and get the message bit 214 * and the access time 215 */ 216 int 217 term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) 218 { 219 struct stat s; 220 char path[MAXPATHLEN]; 221 222 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 223 if (stat(path, &s) < 0) { 224 if (showerror) 225 warn("%s", path); 226 return(1); 227 } 228 *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */ 229 *atimeP = s.st_atime; 230 return(0); 231 } 232 233 /* 234 * do_write - actually make the connection 235 */ 236 void 237 do_write(char *tty, char *mytty, uid_t myuid) 238 { 239 char *login, *nows; 240 struct passwd *pwd; 241 time_t now; 242 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 243 gid_t gid; 244 245 /* Determine our login name before the we reopen() stdout */ 246 if ((login = getlogin()) == NULL) { 247 if ((pwd = getpwuid(myuid))) 248 login = pwd->pw_name; 249 else 250 login = "???"; 251 } 252 253 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 254 if ((freopen(path, "w", stdout)) == NULL) 255 err(1, "%s", path); 256 257 /* revoke privs, now that we have opened the tty */ 258 gid = getgid(); 259 if (setresgid(gid, gid, gid) == -1) 260 err(1, "setresgid"); 261 262 (void)signal(SIGINT, done); 263 (void)signal(SIGHUP, done); 264 265 /* print greeting */ 266 if (gethostname(host, sizeof(host)) < 0) 267 (void)strlcpy(host, "???", sizeof host); 268 now = time((time_t *)NULL); 269 nows = ctime(&now); 270 nows[16] = '\0'; 271 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 272 login, host, mytty, nows + 11); 273 274 while (fgets(line, sizeof(line), stdin) != NULL) 275 wr_fputs(line); 276 } 277 278 /* 279 * done - cleanup and exit 280 */ 281 void 282 done(int sig) 283 { 284 (void)write(STDOUT_FILENO, "EOF\r\n", 5); 285 if (sig) 286 _exit(0); 287 else 288 exit(0); 289 } 290 291 /* 292 * wr_fputs - like fputs(), but makes control characters visible and 293 * turns \n into \r\n 294 */ 295 void 296 wr_fputs(char *s) 297 { 298 u_char c; 299 char visout[5], *s2; 300 301 #define PUTC(c) if (putchar(c) == EOF) goto err; 302 303 for (; *s != '\0'; ++s) { 304 c = toascii(*s); 305 if (c == '\n') { 306 PUTC('\r'); 307 PUTC('\n'); 308 continue; 309 } 310 vis(visout, c, VIS_SAFE|VIS_NOSLASH, s[1]); 311 for (s2 = visout; *s2; s2++) 312 PUTC(*s2); 313 } 314 return; 315 316 err: err(1, NULL); 317 #undef PUTC 318 } 319