diff -uprk.orig netkit-ntalk-0.17.orig/talkd/announce.c netkit-ntalk-0.17/talkd/announce.c --- netkit-ntalk-0.17.orig/talkd/announce.c 2006-10-18 01:15:25 +0000 +++ netkit-ntalk-0.17/talkd/announce.c 2006-11-12 12:33:23 +0000 @@ -87,7 +87,7 @@ static int safechar(int ch) { * try to keep the message in one piece if the recipient * is in, say, vi at the time. */ -static void +static ssize_t print_mesg(int fd, CTL_MSG *request, const char *remote_machine) { struct timeval clocc; @@ -145,7 +145,7 @@ print_mesg(int fd, CTL_MSG *request, con *(bptr++) = '\n'; } *bptr = 0; - write(fd, big_buf, strlen(big_buf)); + return write(fd, big_buf, strlen(big_buf)); } /* @@ -168,12 +168,17 @@ announce_proc(CTL_MSG *request, const ch return (PERMISSION_DENIED); } if (fstat(fd, &stbuf) < 0) { + close(fd); return (PERMISSION_DENIED); } if ((stbuf.st_mode&020) == 0) { + close(fd); return (PERMISSION_DENIED); } - print_mesg(fd, request, remote_machine); + if (print_mesg(fd, request, remote_machine) < 0) { + close(fd); + return FAILED; + } close(fd); return SUCCESS; } diff -uprk.orig netkit-ntalk-0.17.orig/talkd/print.c netkit-ntalk-0.17/talkd/print.c --- netkit-ntalk-0.17.orig/talkd/print.c 2006-10-18 01:15:25 +0000 +++ netkit-ntalk-0.17/talkd/print.c 2006-11-12 14:07:30 +0000 @@ -80,30 +80,27 @@ static const char *answers[NANSWERS] = { "BADCTLADDR" }; -static int logging, badpackets; -static int logfd, packfd; +static int logfd = -1, packfd = -1; void -set_debug(int l, int b) +set_debug(int logging, int badpackets) { const char *file; - logging = l; - badpackets = b; if (logging) { file = _PATH_VAR_LOG "talkd.log"; logfd = open(file, O_WRONLY|O_APPEND); - if (logfd<0) { - syslog(LOG_WARNING, "%s: %s", file, strerror(errno)); - logging = 0; - } + if (logfd < 0) + syslog(LOG_WARNING, "%s: %m", file); + } else { + logfd = -1; } if (badpackets) { file = _PATH_VAR_LOG "talkd.packets"; packfd = open(file, O_WRONLY|O_APPEND); - if (packfd<0) { - syslog(LOG_WARNING, "%s: %s", file, strerror(errno)); - badpackets = 0; - } + if (packfd < 0) + syslog(LOG_WARNING, "%s: %m", file); + } else { + packfd = -1; } } @@ -135,7 +132,7 @@ print_request(const char *cp, const CTL_ char lu[NAME_SIZE+1], ru[NAME_SIZE+1], tt[TTY_SIZE+1]; char buf[1024]; const char *tp; - if (!logging) return; + if (logfd < 0) return; tp = print_type(mp->type); strncpy(lu, mp->l_name, sizeof(lu)); @@ -148,7 +145,11 @@ print_request(const char *cp, const CTL_ snprintf(buf, sizeof(buf), "%s: %s: id %u, l_user %s, r_user %s, r_tty %s\n", cp, tp, mp->id_num, lu, ru, tt); - write(logfd, buf, strlen(buf)); + if (write(logfd, buf, strlen(buf)) < 0) { + syslog(LOG_WARNING, "print_request: %m"); + close(logfd); + logfd = -1; + } } void @@ -156,7 +157,7 @@ print_response(const char *cp, const CTL { char buf[1024]; const char *tp, *ap; - if (!logging) return; + if (logfd < 0) return; tp = print_type(rp->type); ap = print_answer(rp->answer); @@ -164,7 +165,11 @@ print_response(const char *cp, const CTL snprintf(buf, sizeof(buf), "%s: %s <-- %s, id %d\n", cp, tp, ap, ntohl(rp->id_num)); - write(logfd, buf, strlen(buf)); + if (write(logfd, buf, strlen(buf)) < 0) { + syslog(LOG_WARNING, "print_response: %m"); + close(logfd); + logfd = -1; + } } void @@ -172,12 +177,16 @@ debug(const char *format, ...) { char buf[1024]; va_list ap; - if (!logging) return; + if (logfd < 0) return; va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); va_end(ap); - write(logfd, buf, strlen(buf)); + if (write(logfd, buf, strlen(buf)) < 0) { + syslog(LOG_WARNING, "debug: %m"); + close(logfd); + logfd = -1; + } } void @@ -185,14 +194,34 @@ print_broken_packet(const char *pack, si { size_t i; char tmp[4], buf[128]; - if (!badpackets) return; + if (packfd < 0) return; snprintf(buf, sizeof(buf), "From: %s [%u]", inet_ntoa(from->sin_addr), from->sin_addr.s_addr); - write(packfd, buf, strlen(buf)); + if (write(packfd, buf, strlen(buf)) < 0) { + syslog(LOG_WARNING, "print_broken_packet: %m"); + close(packfd); + packfd = -1; + return; + } for (i=0; i<len; i++) { - if (i%24 == 0) write(packfd, "\n ", 5); + if (i%24 == 0 && write(packfd, "\n ", 5) < 0) { + syslog(LOG_WARNING, "print_broken_packet: %m"); + close(packfd); + packfd = -1; + return; + } snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)pack[i]); - write(packfd, tmp, strlen(tmp)); + if (write(packfd, tmp, strlen(tmp)) < 0) { + syslog(LOG_WARNING, "print_broken_packet: %m"); + close(packfd); + packfd = -1; + return; + } + } + if (write(packfd, "\n", 1) < 0) { + syslog(LOG_WARNING, "print_broken_packet: %m"); + close(packfd); + packfd = -1; + return; } - write(packfd, "\n", 1); } diff -uprk.orig netkit-ntalk-0.17.orig/talkd/talkd.c netkit-ntalk-0.17/talkd/talkd.c --- netkit-ntalk-0.17.orig/talkd/talkd.c 2006-10-18 01:15:25 +0000 +++ netkit-ntalk-0.17/talkd/talkd.c 2006-11-12 12:33:23 +0000 @@ -330,8 +330,7 @@ main(int argc, char *argv[]) /* make sure we're a daemon */ if (getsockname(0, (struct sockaddr *)&sn, &sz)) { const char *msg = strerror(errno); - write(2, msg, strlen(msg)); - exit(1); + exit((write(2, msg, strlen(msg)) < 0) ? 2 : 1); } openlog("talkd", LOG_PID, LOG_DAEMON); if (gethostname(ourhostname, sizeof(ourhostname) - 1) < 0) {