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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1989, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif 38 39 #if 0 40 #ifndef lint 41 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 42 #endif 43 #endif 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/signal.h> 50 #include <sys/stat.h> 51 #include <sys/file.h> 52 #include <sys/time.h> 53 #include <ctype.h> 54 #include <err.h> 55 #include <locale.h> 56 #include <paths.h> 57 #include <pwd.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <utmpx.h> 63 64 void done(int); 65 void do_write(char *, char *, uid_t); 66 static void usage(void); 67 int term_chk(char *, int *, time_t *, int); 68 void wr_fputs(unsigned char *s); 69 void search_utmp(char *, char *, char *, uid_t); 70 int utmp_chk(char *, char *); 71 72 int 73 main(int argc, char **argv) 74 { 75 time_t atime; 76 uid_t myuid; 77 int msgsok, myttyfd; 78 char tty[MAXPATHLEN], *mytty; 79 80 (void)setlocale(LC_CTYPE, ""); 81 82 while (getopt(argc, argv, "") != -1) 83 usage(); 84 argc -= optind; 85 argv += optind; 86 87 /* check that sender has write enabled */ 88 if (isatty(fileno(stdin))) 89 myttyfd = fileno(stdin); 90 else if (isatty(fileno(stdout))) 91 myttyfd = fileno(stdout); 92 else if (isatty(fileno(stderr))) 93 myttyfd = fileno(stderr); 94 else 95 errx(1, "can't find your tty"); 96 if (!(mytty = ttyname(myttyfd))) 97 errx(1, "can't find your tty's name"); 98 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV))) 99 mytty += strlen(_PATH_DEV); 100 if (term_chk(mytty, &msgsok, &atime, 1)) 101 exit(1); 102 if (!msgsok) 103 errx(1, "you have write permission turned off"); 104 105 myuid = getuid(); 106 107 /* check args */ 108 switch (argc) { 109 case 1: 110 search_utmp(argv[0], tty, mytty, myuid); 111 do_write(tty, mytty, myuid); 112 break; 113 case 2: 114 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV))) 115 argv[1] += strlen(_PATH_DEV); 116 if (utmp_chk(argv[0], argv[1])) 117 errx(1, "%s is not logged in on %s", argv[0], argv[1]); 118 if (term_chk(argv[1], &msgsok, &atime, 1)) 119 exit(1); 120 if (myuid && !msgsok) 121 errx(1, "%s has messages disabled on %s", argv[0], argv[1]); 122 do_write(argv[1], mytty, myuid); 123 break; 124 default: 125 usage(); 126 } 127 done(0); 128 return (0); 129 } 130 131 static void 132 usage(void) 133 { 134 (void)fprintf(stderr, "usage: write user [tty]\n"); 135 exit(1); 136 } 137 138 /* 139 * utmp_chk - checks that the given user is actually logged in on 140 * the given tty 141 */ 142 int 143 utmp_chk(char *user, char *tty) 144 { 145 struct utmpx lu, *u; 146 147 strncpy(lu.ut_line, tty, sizeof lu.ut_line); 148 setutxent(); 149 while ((u = getutxline(&lu)) != NULL) 150 if (u->ut_type == USER_PROCESS && 151 strcmp(user, u->ut_user) == 0) { 152 endutxent(); 153 return(0); 154 } 155 endutxent(); 156 return(1); 157 } 158 159 /* 160 * search_utmp - search utmp for the "best" terminal to write to 161 * 162 * Ignores terminals with messages disabled, and of the rest, returns 163 * the one with the most recent access time. Returns as value the number 164 * of the user's terminals with messages enabled, or -1 if the user is 165 * not logged in at all. 166 * 167 * Special case for writing to yourself - ignore the terminal you're 168 * writing from, unless that's the only terminal with messages enabled. 169 */ 170 void 171 search_utmp(char *user, char *tty, char *mytty, uid_t myuid) 172 { 173 struct utmpx *u; 174 time_t bestatime, atime; 175 int nloggedttys, nttys, msgsok, user_is_me; 176 177 nloggedttys = nttys = 0; 178 bestatime = 0; 179 user_is_me = 0; 180 181 setutxent(); 182 while ((u = getutxent()) != NULL) 183 if (u->ut_type == USER_PROCESS && 184 strcmp(user, u->ut_user) == 0) { 185 ++nloggedttys; 186 if (term_chk(u->ut_line, &msgsok, &atime, 0)) 187 continue; /* bad term? skip */ 188 if (myuid && !msgsok) 189 continue; /* skip ttys with msgs off */ 190 if (strcmp(u->ut_line, 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)strlcpy(tty, u->ut_line, MAXPATHLEN); 198 } 199 } 200 endutxent(); 201 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)strlcpy(tty, mytty, MAXPATHLEN); 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(char *tty, int *msgsokP, time_t *atimeP, int showerror) 221 { 222 struct stat s; 223 char path[MAXPATHLEN]; 224 225 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 226 if (stat(path, &s) < 0) { 227 if (showerror) 228 warn("%s", path); 229 return(1); 230 } 231 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */ 232 *atimeP = s.st_atime; 233 return(0); 234 } 235 236 /* 237 * do_write - actually make the connection 238 */ 239 void 240 do_write(char *tty, char *mytty, uid_t myuid) 241 { 242 const char *login; 243 char *nows; 244 struct passwd *pwd; 245 time_t now; 246 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 247 248 /* Determine our login name before we reopen() stdout */ 249 if ((login = getlogin()) == NULL) { 250 if ((pwd = getpwuid(myuid))) 251 login = pwd->pw_name; 252 else 253 login = "???"; 254 } 255 256 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 257 if ((freopen(path, "w", stdout)) == NULL) 258 err(1, "%s", path); 259 260 (void)signal(SIGINT, done); 261 (void)signal(SIGHUP, done); 262 263 /* print greeting */ 264 if (gethostname(host, sizeof(host)) < 0) 265 (void)strcpy(host, "???"); 266 now = time((time_t *)NULL); 267 nows = ctime(&now); 268 nows[16] = '\0'; 269 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 270 login, host, mytty, nows + 11); 271 272 while (fgets(line, sizeof(line), stdin) != NULL) 273 wr_fputs(line); 274 } 275 276 /* 277 * done - cleanup and exit 278 */ 279 void 280 done(int n __unused) 281 { 282 (void)printf("EOF\r\n"); 283 exit(0); 284 } 285 286 /* 287 * wr_fputs - like fputs(), but makes control characters visible and 288 * turns \n into \r\n 289 */ 290 void 291 wr_fputs(unsigned char *s) 292 { 293 294 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL); 295 296 for (; *s != '\0'; ++s) { 297 if (*s == '\n') { 298 PUTC('\r'); 299 } else if (((*s & 0x80) && *s < 0xA0) || 300 /* disable upper controls */ 301 (!isprint(*s) && !isspace(*s) && 302 *s != '\a' && *s != '\b') 303 ) { 304 if (*s & 0x80) { 305 *s &= ~0x80; 306 PUTC('M'); 307 PUTC('-'); 308 } 309 if (iscntrl(*s)) { 310 *s ^= 0x40; 311 PUTC('^'); 312 } 313 } 314 PUTC(*s); 315 } 316 return; 317 #undef PUTC 318 } 319