1 /* $NetBSD: write.c,v 1.25 2008/07/21 14:19:28 lukem 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ 38 The Regents of the University of California. All rights reserved."); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)write.c 8.2 (Berkeley) 4/27/95"; 44 #else 45 __RCSID("$NetBSD: write.c,v 1.25 2008/07/21 14:19:28 lukem Exp $"); 46 #endif 47 #endif /* not lint */ 48 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <ctype.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <signal.h> 57 #include <time.h> 58 #include <fcntl.h> 59 #include <paths.h> 60 #include <pwd.h> 61 #include <unistd.h> 62 #include <err.h> 63 #include <errno.h> 64 65 #include "utmpentry.h" 66 #include "term_chk.h" 67 68 void done(int); 69 void do_write(int, const char *, const uid_t); 70 void wr_fputs(char *); 71 int search_utmp(char *, char *, uid_t, gid_t); 72 int utmp_chk(const char *, const char *); 73 int main(int, char **); 74 75 76 int 77 main(int argc, char **argv) 78 { 79 time_t atime; 80 uid_t myuid, uid; 81 int msgsok, ttyfd; 82 char *mytty; 83 gid_t saved_egid = getegid(); 84 85 if (setegid(getgid()) == -1) 86 err(1, "setegid"); 87 myuid = getuid(); 88 ttyfd = -1; 89 90 mytty = check_sender(&atime, myuid, saved_egid); 91 92 /* check args */ 93 switch (argc) { 94 case 2: 95 ttyfd = search_utmp(argv[1], mytty, myuid, saved_egid); 96 break; 97 case 3: 98 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV))) 99 argv[2] += strlen(_PATH_DEV); 100 if (uid_from_user(argv[1], &uid) == -1) 101 errx(1, "%s: unknown user", argv[1]); 102 if (utmp_chk(argv[1], argv[2])) 103 errx(1, "%s is not logged in on %s", 104 argv[1], argv[2]); 105 ttyfd = term_chk(uid, argv[2], &msgsok, &atime, 0, saved_egid); 106 if (ttyfd == -1) 107 err(1, "%s%s", _PATH_DEV, argv[2]); 108 if (myuid && !msgsok) 109 errx(1, "%s has messages disabled on %s", 110 argv[1], argv[2]); 111 break; 112 default: 113 (void)fprintf(stderr, "usage: write user [tty]\n"); 114 exit(1); 115 } 116 if (setgid(getgid()) == -1) 117 err(1, "setgid"); 118 do_write(ttyfd, mytty, myuid); 119 done(0); 120 /* NOTREACHED */ 121 #ifdef __GNUC__ 122 return (0); 123 #endif 124 } 125 126 /* 127 * utmp_chk - checks that the given user is actually logged in on 128 * the given tty 129 */ 130 int 131 utmp_chk(const char *user, const char *tty) 132 { 133 struct utmpentry *ep; 134 135 (void)getutentries(NULL, &ep); 136 137 for (; ep; ep = ep->next) 138 if (strcmp(user, ep->name) == 0 && strcmp(tty, ep->line) == 0) 139 return(0); 140 return(1); 141 } 142 143 /* 144 * search_utmp - search utmp for the "best" terminal to write to 145 * 146 * Ignores terminals with messages disabled, and of the rest, returns 147 * the one with the most recent access time. Returns as value the number 148 * of the user's terminals with messages enabled, or -1 if the user is 149 * not logged in at all. 150 * 151 * Special case for writing to yourself - ignore the terminal you're 152 * writing from, unless that's the only terminal with messages enabled. 153 */ 154 int 155 search_utmp(char *user, char *mytty, uid_t myuid, gid_t saved_egid) 156 { 157 char tty[MAXPATHLEN]; 158 time_t bestatime, atime; 159 int nloggedttys, nttys, msgsok, user_is_me; 160 struct utmpentry *ep; 161 int fd, nfd; 162 uid_t uid; 163 164 if (uid_from_user(user, &uid) == -1) 165 errx(1, "%s: unknown user", user); 166 167 (void)getutentries(NULL, &ep); 168 169 nloggedttys = nttys = 0; 170 bestatime = 0; 171 user_is_me = 0; 172 fd = -1; 173 for (; ep; ep = ep->next) 174 if (strcmp(user, ep->name) == 0) { 175 ++nloggedttys; 176 nfd = term_chk(uid, ep->line, &msgsok, &atime, 0, 177 saved_egid); 178 if (nfd == -1) 179 continue; /* bad term? skip */ 180 if (myuid && !msgsok) { 181 close(nfd); 182 continue; /* skip ttys with msgs off */ 183 } 184 if (strcmp(ep->line, mytty) == 0) { 185 user_is_me = 1; 186 if (fd == -1) 187 fd = nfd; 188 else 189 close(nfd); 190 continue; /* don't write to yourself */ 191 } 192 ++nttys; 193 if (atime > bestatime) { 194 bestatime = atime; 195 (void)strlcpy(tty, ep->line, sizeof(tty)); 196 close(fd); 197 fd = nfd; 198 } else 199 close(nfd); 200 } 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 return fd; 207 errx(1, "%s has messages disabled", user); 208 } else if (nttys > 1) 209 warnx("%s is logged in more than once; writing to %s", 210 user, tty); 211 return fd; 212 } 213 214 /* 215 * do_write - actually make the connection 216 */ 217 void 218 do_write(int ttyfd, const char *mytty, const uid_t myuid) 219 { 220 const char *login; 221 char *nows; 222 struct passwd *pwd; 223 time_t now; 224 char host[MAXHOSTNAMELEN + 1], line[512]; 225 226 /* Determine our login name before we re-open stdout */ 227 if ((login = getlogin()) == NULL) { 228 if ((pwd = getpwuid(myuid)) != NULL) 229 login = pwd->pw_name; 230 else login = "???"; 231 } 232 233 if (dup2(ttyfd, STDOUT_FILENO) == -1) 234 err(1, "dup2"); 235 236 (void)signal(SIGINT, done); 237 (void)signal(SIGHUP, done); 238 (void)close(ttyfd); 239 240 /* print greeting */ 241 if (gethostname(host, sizeof(host)) < 0) 242 (void)strlcpy(host, "???", sizeof(host)); 243 else 244 host[sizeof(host) - 1] = '\0'; 245 now = time((time_t *)NULL); 246 nows = ctime(&now); 247 nows[16] = '\0'; 248 (void)printf("\r\n\a\a\aMessage from %s@%s on %s at %s ...\r\n", 249 login, host, mytty, nows + 11); 250 251 while (fgets(line, sizeof(line), stdin) != NULL) 252 wr_fputs(line); 253 } 254 255 /* 256 * done - cleanup and exit 257 */ 258 void 259 done(int signo) 260 { 261 262 (void)write(STDOUT_FILENO, "EOF\r\n", sizeof("EOF\r\n") - 1); 263 if (signo == 0) 264 exit(0); 265 else 266 _exit(0); 267 } 268 269 /* 270 * wr_fputs - like fputs(), but makes control characters visible and 271 * turns \n into \r\n 272 */ 273 void 274 wr_fputs(char *s) 275 { 276 unsigned char c; 277 278 #define PUTC(c) if (putchar(c) == EOF) goto err; 279 280 for (; *s != '\0'; ++s) { 281 c = toascii(*s); 282 if (c == '\n') { 283 PUTC('\r'); 284 } else if (!isprint(c) && !isspace(c) && c != '\a') { 285 PUTC('^'); 286 c ^= 0x40; /* DEL to ?, others to alpha */ 287 } 288 PUTC(c); 289 } 290 return; 291 292 err: err(1, NULL); 293 #undef PUTC 294 } 295