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