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