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 37#ifndef lint 38static const char copyright[] = 39"@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif 42 43#if 0 44#ifndef lint 45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 46#endif 47#endif 48 49#include <sys/cdefs.h>
| 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 37#ifndef lint 38static const char copyright[] = 39"@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif 42 43#if 0 44#ifndef lint 45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 46#endif 47#endif 48 49#include <sys/cdefs.h>
|
105 if (term_chk(mytty, &msgsok, &atime, 1)) 106 exit(1); 107 if (!msgsok) 108 errx(1, "you have write permission turned off"); 109 110 myuid = getuid(); 111 112 /* check args */ 113 switch (argc) { 114 case 1: 115 search_utmp(argv[0], tty, mytty, myuid); 116 do_write(tty, mytty, myuid); 117 break; 118 case 2: 119 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV))) 120 argv[1] += strlen(_PATH_DEV); 121 if (utmp_chk(argv[0], argv[1])) 122 errx(1, "%s is not logged in on %s", argv[0], argv[1]); 123 if (term_chk(argv[1], &msgsok, &atime, 1)) 124 exit(1); 125 if (myuid && !msgsok) 126 errx(1, "%s has messages disabled on %s", argv[0], argv[1]); 127 do_write(argv[1], mytty, myuid); 128 break; 129 default: 130 usage(); 131 } 132 done(0); 133 return (0); 134} 135 136static void 137usage(void) 138{ 139 (void)fprintf(stderr, "usage: write user [tty]\n"); 140 exit(1); 141} 142 143/* 144 * utmp_chk - checks that the given user is actually logged in on 145 * the given tty 146 */ 147int 148utmp_chk(char *user, char *tty) 149{ 150 struct utmp u; 151 int ufd; 152 153 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 154 return(0); /* ignore error, shouldn't happen anyway */ 155 156 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 157 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 158 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 159 (void)close(ufd); 160 return(0); 161 } 162 163 (void)close(ufd); 164 return(1); 165} 166 167/* 168 * search_utmp - search utmp for the "best" terminal to write to 169 * 170 * Ignores terminals with messages disabled, and of the rest, returns 171 * the one with the most recent access time. Returns as value the number 172 * of the user's terminals with messages enabled, or -1 if the user is 173 * not logged in at all. 174 * 175 * Special case for writing to yourself - ignore the terminal you're 176 * writing from, unless that's the only terminal with messages enabled. 177 */ 178void 179search_utmp(char *user, char *tty, char *mytty, uid_t myuid) 180{ 181 struct utmp u; 182 time_t bestatime, atime; 183 int ufd, nloggedttys, nttys, msgsok, user_is_me; 184 char atty[UT_LINESIZE + 1]; 185 186 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 187 err(1, "utmp"); 188 189 nloggedttys = nttys = 0; 190 bestatime = 0; 191 user_is_me = 0; 192 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 193 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 194 ++nloggedttys; 195 (void)strncpy(atty, u.ut_line, UT_LINESIZE); 196 atty[UT_LINESIZE] = '\0'; 197 if (term_chk(atty, &msgsok, &atime, 0)) 198 continue; /* bad term? skip */ 199 if (myuid && !msgsok) 200 continue; /* skip ttys with msgs off */ 201 if (strcmp(atty, mytty) == 0) { 202 user_is_me = 1; 203 continue; /* don't write to yourself */ 204 } 205 ++nttys; 206 if (atime > bestatime) { 207 bestatime = atime; 208 (void)strcpy(tty, atty); 209 } 210 } 211 212 (void)close(ufd); 213 if (nloggedttys == 0) 214 errx(1, "%s is not logged in", user); 215 if (nttys == 0) { 216 if (user_is_me) { /* ok, so write to yourself! */ 217 (void)strcpy(tty, mytty); 218 return; 219 } 220 errx(1, "%s has messages disabled", user); 221 } else if (nttys > 1) { 222 warnx("%s is logged in more than once; writing to %s", user, tty); 223 } 224} 225 226/* 227 * term_chk - check that a terminal exists, and get the message bit 228 * and the access time 229 */ 230int 231term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) 232{ 233 struct stat s; 234 char path[MAXPATHLEN]; 235 236 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 237 if (stat(path, &s) < 0) { 238 if (showerror) 239 warn("%s", path); 240 return(1); 241 } 242 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */ 243 *atimeP = s.st_atime; 244 return(0); 245} 246 247/* 248 * do_write - actually make the connection 249 */ 250void 251do_write(char *tty, char *mytty, uid_t myuid) 252{ 253 const char *login; 254 char *nows; 255 struct passwd *pwd; 256 time_t now; 257 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 258 259 /* Determine our login name before we reopen() stdout */ 260 if ((login = getlogin()) == NULL) { 261 if ((pwd = getpwuid(myuid))) 262 login = pwd->pw_name; 263 else 264 login = "???"; 265 } 266 267 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 268 if ((freopen(path, "w", stdout)) == NULL) 269 err(1, "%s", path); 270 271 (void)signal(SIGINT, done); 272 (void)signal(SIGHUP, done); 273 274 /* print greeting */ 275 if (gethostname(host, sizeof(host)) < 0) 276 (void)strcpy(host, "???"); 277 now = time((time_t *)NULL); 278 nows = ctime(&now); 279 nows[16] = '\0'; 280 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 281 login, host, mytty, nows + 11); 282 283 while (fgets(line, sizeof(line), stdin) != NULL) 284 wr_fputs(line); 285} 286 287/* 288 * done - cleanup and exit 289 */ 290void 291done(int n __unused) 292{ 293 (void)printf("EOF\r\n"); 294 exit(0); 295} 296 297/* 298 * wr_fputs - like fputs(), but makes control characters visible and 299 * turns \n into \r\n 300 */ 301void 302wr_fputs(unsigned char *s) 303{ 304 305#define PUTC(c) if (putchar(c) == EOF) err(1, NULL); 306 307 for (; *s != '\0'; ++s) { 308 if (*s == '\n') { 309 PUTC('\r'); 310 } else if (((*s & 0x80) && *s < 0xA0) || 311 /* disable upper controls */ 312 (!isprint(*s) && !isspace(*s) && 313 *s != '\a' && *s != '\b') 314 ) { 315 if (*s & 0x80) { 316 *s &= ~0x80; 317 PUTC('M'); 318 PUTC('-'); 319 } 320 if (iscntrl(*s)) { 321 *s ^= 0x40; 322 PUTC('^'); 323 } 324 } 325 PUTC(*s); 326 } 327 return; 328#undef PUTC 329}
| 104 if (term_chk(mytty, &msgsok, &atime, 1)) 105 exit(1); 106 if (!msgsok) 107 errx(1, "you have write permission turned off"); 108 109 myuid = getuid(); 110 111 /* check args */ 112 switch (argc) { 113 case 1: 114 search_utmp(argv[0], tty, mytty, myuid); 115 do_write(tty, mytty, myuid); 116 break; 117 case 2: 118 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV))) 119 argv[1] += strlen(_PATH_DEV); 120 if (utmp_chk(argv[0], argv[1])) 121 errx(1, "%s is not logged in on %s", argv[0], argv[1]); 122 if (term_chk(argv[1], &msgsok, &atime, 1)) 123 exit(1); 124 if (myuid && !msgsok) 125 errx(1, "%s has messages disabled on %s", argv[0], argv[1]); 126 do_write(argv[1], mytty, myuid); 127 break; 128 default: 129 usage(); 130 } 131 done(0); 132 return (0); 133} 134 135static void 136usage(void) 137{ 138 (void)fprintf(stderr, "usage: write user [tty]\n"); 139 exit(1); 140} 141 142/* 143 * utmp_chk - checks that the given user is actually logged in on 144 * the given tty 145 */ 146int 147utmp_chk(char *user, char *tty) 148{ 149 struct utmp u; 150 int ufd; 151 152 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 153 return(0); /* ignore error, shouldn't happen anyway */ 154 155 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 156 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 157 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 158 (void)close(ufd); 159 return(0); 160 } 161 162 (void)close(ufd); 163 return(1); 164} 165 166/* 167 * search_utmp - search utmp for the "best" terminal to write to 168 * 169 * Ignores terminals with messages disabled, and of the rest, returns 170 * the one with the most recent access time. Returns as value the number 171 * of the user's terminals with messages enabled, or -1 if the user is 172 * not logged in at all. 173 * 174 * Special case for writing to yourself - ignore the terminal you're 175 * writing from, unless that's the only terminal with messages enabled. 176 */ 177void 178search_utmp(char *user, char *tty, char *mytty, uid_t myuid) 179{ 180 struct utmp u; 181 time_t bestatime, atime; 182 int ufd, nloggedttys, nttys, msgsok, user_is_me; 183 char atty[UT_LINESIZE + 1]; 184 185 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 186 err(1, "utmp"); 187 188 nloggedttys = nttys = 0; 189 bestatime = 0; 190 user_is_me = 0; 191 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 192 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 193 ++nloggedttys; 194 (void)strncpy(atty, u.ut_line, UT_LINESIZE); 195 atty[UT_LINESIZE] = '\0'; 196 if (term_chk(atty, &msgsok, &atime, 0)) 197 continue; /* bad term? skip */ 198 if (myuid && !msgsok) 199 continue; /* skip ttys with msgs off */ 200 if (strcmp(atty, mytty) == 0) { 201 user_is_me = 1; 202 continue; /* don't write to yourself */ 203 } 204 ++nttys; 205 if (atime > bestatime) { 206 bestatime = atime; 207 (void)strcpy(tty, atty); 208 } 209 } 210 211 (void)close(ufd); 212 if (nloggedttys == 0) 213 errx(1, "%s is not logged in", user); 214 if (nttys == 0) { 215 if (user_is_me) { /* ok, so write to yourself! */ 216 (void)strcpy(tty, mytty); 217 return; 218 } 219 errx(1, "%s has messages disabled", user); 220 } else if (nttys > 1) { 221 warnx("%s is logged in more than once; writing to %s", user, tty); 222 } 223} 224 225/* 226 * term_chk - check that a terminal exists, and get the message bit 227 * and the access time 228 */ 229int 230term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) 231{ 232 struct stat s; 233 char path[MAXPATHLEN]; 234 235 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 236 if (stat(path, &s) < 0) { 237 if (showerror) 238 warn("%s", path); 239 return(1); 240 } 241 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */ 242 *atimeP = s.st_atime; 243 return(0); 244} 245 246/* 247 * do_write - actually make the connection 248 */ 249void 250do_write(char *tty, char *mytty, uid_t myuid) 251{ 252 const char *login; 253 char *nows; 254 struct passwd *pwd; 255 time_t now; 256 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 257 258 /* Determine our login name before we reopen() stdout */ 259 if ((login = getlogin()) == NULL) { 260 if ((pwd = getpwuid(myuid))) 261 login = pwd->pw_name; 262 else 263 login = "???"; 264 } 265 266 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 267 if ((freopen(path, "w", stdout)) == NULL) 268 err(1, "%s", path); 269 270 (void)signal(SIGINT, done); 271 (void)signal(SIGHUP, done); 272 273 /* print greeting */ 274 if (gethostname(host, sizeof(host)) < 0) 275 (void)strcpy(host, "???"); 276 now = time((time_t *)NULL); 277 nows = ctime(&now); 278 nows[16] = '\0'; 279 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 280 login, host, mytty, nows + 11); 281 282 while (fgets(line, sizeof(line), stdin) != NULL) 283 wr_fputs(line); 284} 285 286/* 287 * done - cleanup and exit 288 */ 289void 290done(int n __unused) 291{ 292 (void)printf("EOF\r\n"); 293 exit(0); 294} 295 296/* 297 * wr_fputs - like fputs(), but makes control characters visible and 298 * turns \n into \r\n 299 */ 300void 301wr_fputs(unsigned char *s) 302{ 303 304#define PUTC(c) if (putchar(c) == EOF) err(1, NULL); 305 306 for (; *s != '\0'; ++s) { 307 if (*s == '\n') { 308 PUTC('\r'); 309 } else if (((*s & 0x80) && *s < 0xA0) || 310 /* disable upper controls */ 311 (!isprint(*s) && !isspace(*s) && 312 *s != '\a' && *s != '\b') 313 ) { 314 if (*s & 0x80) { 315 *s &= ~0x80; 316 PUTC('M'); 317 PUTC('-'); 318 } 319 if (iscntrl(*s)) { 320 *s ^= 0x40; 321 PUTC('^'); 322 } 323 } 324 PUTC(*s); 325 } 326 return; 327#undef PUTC 328}
|