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