diff options
Diffstat (limited to 'mail_cmds/comsat')
-rw-r--r-- | mail_cmds/comsat/Makefile | 11 | ||||
-rw-r--r-- | mail_cmds/comsat/comsat.8 | 115 | ||||
-rw-r--r-- | mail_cmds/comsat/comsat.c | 314 | ||||
-rw-r--r-- | mail_cmds/comsat/comsat.plist | 29 |
4 files changed, 469 insertions, 0 deletions
diff --git a/mail_cmds/comsat/Makefile b/mail_cmds/comsat/Makefile new file mode 100644 index 0000000..fd52e66 --- /dev/null +++ b/mail_cmds/comsat/Makefile @@ -0,0 +1,11 @@ +Project = comsat +Install_Dir = /usr/libexec + +CFILES = comsat.c +MANPAGES = comsat.8 +LAUNCHD_PLISTS = comsat.plist + +Extra_CC_Flags = -Wall -Werror -mdynamic-no-pic +Extra_LD_Flags = -dead_strip + +include $(MAKEFILEPATH)/CoreOS/ReleaseControl/BSDCommon.make diff --git a/mail_cmds/comsat/comsat.8 b/mail_cmds/comsat/comsat.8 new file mode 100644 index 0000000..2f73c78 --- /dev/null +++ b/mail_cmds/comsat/comsat.8 @@ -0,0 +1,115 @@ +.\" $NetBSD: comsat.8,v 1.6 1998/07/04 19:38:39 mrg Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" from: @(#)comsat.8 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 4, 1993 +.Dt COMSAT 8 +.Os BSD 4.2 +.Sh NAME +.Nm comsat +.Nd biff server +.Sh SYNOPSIS +.Nm +.Op Fl l +.Sh DESCRIPTION +.Nm +is the server process which receives reports of incoming mail +and notifies users if they have requested this service. +.Nm +receives messages on a datagram port associated with the +.Dq biff +service +specification (see +.Xr services 5 +and +.Xr inetd 8 ) . +The one line messages are of the form: +.Pp +.Dl user@mailbox-offset +.Pp +If the +.Em user +specified is logged in to the system and the associated terminal has +the owner execute bit turned on (by a +.Dq Li biff y ) , +the +.Em offset +is used as a seek offset into the appropriate mailbox file and +the first 7 lines or 560 characters of the message are printed +on the user's terminal. Lines which appear to be part of +the message header other than the +.Dq From , +.Dq \&To , +.Dq Date , +or +.Dq Subject +lines are not included in the displayed message. +.Sh OPTIONS +The +.Nm +program supports this option: +.Bl -tag -width 12345 +.It Fl l +The +.Fl l +option turns on +.Xr syslogd 8 +log messages. +.El +.Sh FILES +.Bl -tag -width /var/run/utmpx -compact +.It Pa /var/run/utmpx +to find out who's logged on and on what terminals +.El +.Sh SEE ALSO +.Xr biff 1 , +.Xr inetd 8 , +.Xr syslogd 8 . +.Sh BUGS +The message header filtering is prone to error. +The density of the information presented is near the theoretical minimum. +.Pp +Users should be notified of mail which arrives on other +machines than the one to which they are currently logged in. +.Pp +The notification should appear in a separate window so it +does not mess up the screen. +.Pp +.Nm +runs as root so that it can open the users maildrop. +.Sh HISTORY +The +.Nm +command appeared in +.Bx 4.2 . diff --git a/mail_cmds/comsat/comsat.c b/mail_cmds/comsat/comsat.c new file mode 100644 index 0000000..62ac5d5 --- /dev/null +++ b/mail_cmds/comsat/comsat.c @@ -0,0 +1,314 @@ +/* $NetBSD: comsat.c,v 1.14 1998/07/06 06:47:38 mrg Exp $ */ + +/* + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#ifndef lint +__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); +#if 0 +static char sccsid[] = "from: @(#)comsat.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: comsat.c,v 1.14 1998/07/06 06:47:38 mrg Exp $"); +#endif +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/file.h> +#include <sys/wait.h> + +#include <netinet/in.h> + +#include <ctype.h> +#include <errno.h> +#include <netdb.h> +#include <paths.h> +#include <pwd.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <termios.h> +#include <time.h> +#include <vis.h> +#include <unistd.h> +#include <utmpx.h> + +int logging; +int debug = 0; +#define dsyslog if (debug) syslog + +#define MAXIDLE 120 + +char hostname[MAXHOSTNAMELEN+1]; +struct utmpx *utmpx = NULL; +time_t lastmsgtime; +int nutmpx, uf; + +void jkfprintf __P((FILE *, char[], off_t)); +void mailfor __P((char *)); +void notify __P((struct utmpx *, off_t)); +void onalrm __P((int)); +void reapchildren __P((int)); +int main __P((int, char *[])); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + struct sockaddr_in from; + int cc, ch; + socklen_t fromlen; + char msgbuf[100]; + sigset_t sigset; + extern char *__progname; + + /* verify proper invocation */ + fromlen = sizeof(from); + if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { + (void)fprintf(stderr, + "comsat: getsockname: %s.\n", strerror(errno)); + exit(1); + } + + openlog("comsat", LOG_PID, LOG_DAEMON); + while ((ch = getopt(argc, argv, "l")) != -1) + switch (ch) { + case 'l': + logging = 1; + break; + default: + syslog(LOG_ERR, "usage: %s [-l]", __progname); + exit(1); + } + if (chdir(_PATH_MAILDIR)) { + syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR); + (void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0); + exit(1); + } + if ((uf = open(_PATH_UTMPX, O_RDONLY, 0)) < 0) { + syslog(LOG_ERR, "open: %s: %m", _PATH_UTMPX); + (void)recv(0, msgbuf, sizeof(msgbuf) - 1, 0); + exit(1); + } + (void)time(&lastmsgtime); + (void)gethostname(hostname, sizeof(hostname)); + hostname[sizeof(hostname) - 1] = '\0'; + onalrm(0); + (void)signal(SIGALRM, onalrm); + (void)signal(SIGTTOU, SIG_IGN); + (void)signal(SIGCHLD, reapchildren); + for (;;) { + cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0); + if (cc <= 0) { + if (errno != EINTR) + sleep(1); + errno = 0; + continue; + } + if (!nutmpx) /* no one has logged in yet */ + continue; + sigemptyset(&sigset); + sigaddset(&sigset, SIGALRM); + sigprocmask(SIG_SETMASK, &sigset, NULL); + msgbuf[cc] = '\0'; + (void)time(&lastmsgtime); + mailfor(msgbuf); + sigemptyset(&sigset); + sigprocmask(SIG_SETMASK, &sigset, NULL); + } +} + +void +reapchildren(signo) + int signo; +{ + + while (wait3(NULL, WNOHANG, NULL) > 0); +} + +void +onalrm(signo) + int signo; +{ + static u_int utmpxsize; /* last malloced size for utmpx */ + static time_t utmpxmtime; /* last modification time for utmpx */ + struct stat statbf; + + if (time(NULL) - lastmsgtime >= MAXIDLE) + exit(0); + (void)alarm((u_int)15); + (void)fstat(uf, &statbf); + if (statbf.st_mtime > utmpxmtime) { + utmpxmtime = statbf.st_mtime; + if (statbf.st_size > utmpxsize) { + utmpxsize = statbf.st_size + 10 * sizeof(struct utmpx); + if ((utmpx = realloc(utmpx, utmpxsize)) == NULL) { + syslog(LOG_ERR, "%s", strerror(errno)); + exit(1); + } + } + /* the first record is just a signature, so it is skipped */ + (void)lseek(uf, (off_t)sizeof(struct utmpx), SEEK_SET); + nutmpx = read(uf, utmpx, statbf.st_size - sizeof(struct utmpx))/sizeof(struct utmpx); + } +} + +void +mailfor(name) + char *name; +{ + struct utmpx *utp = &utmpx[nutmpx]; + char *cp; + off_t offset; + + if (!(cp = strchr(name, '@'))) + return; + *cp = '\0'; + offset = atoi(cp + 1); + while (--utp >= utmpx) + if (utp->ut_type == USER_PROCESS && !strncmp(utp->ut_user, name, sizeof(utp->ut_user))) + notify(utp, offset); +} + +static char *cr; + +void +notify(utp, offset) + struct utmpx *utp; + off_t offset; +{ + FILE *tp; + struct passwd *p; + struct stat stb; + struct termios ttybuf; + char tty[40], name[sizeof(utp->ut_user) + 1]; + + (void)snprintf(tty, sizeof(tty), "%s%.*s", + _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line); + if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) { + /* A slash is an attempt to break security... */ + /* + * XXX but what about something like "/dev/pts/5" + * that we may one day "support". ? + */ + syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty); + return; + } + if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) { + dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty); + return; + } + dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_user, tty); + if (fork()) + return; + (void)signal(SIGALRM, SIG_DFL); + (void)alarm((u_int)30); + if ((tp = fopen(tty, "w")) == NULL) { + dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno)); + _exit(-1); + } + (void)tcgetattr(fileno(tp), &ttybuf); + cr = (ttybuf.c_oflag & ONLCR) && (ttybuf.c_oflag & OPOST) ? + "\n" : "\n\r"; + (void)strncpy(name, utp->ut_user, sizeof(utp->ut_user)); + name[sizeof(name) - 1] = '\0'; + + /* Set uid/gid/groups to users in case mail drop is on nfs */ + if ((p = getpwnam(name)) == NULL || + initgroups(p->pw_name, p->pw_gid) < 0 || + setgid(p->pw_gid) < 0 || + setuid(p->pw_uid) < 0) + _exit(-1); + + if (logging) + syslog(LOG_INFO, "biff message for %s", name); + + (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s", + cr, name, (int)sizeof(hostname), hostname, cr, cr); + jkfprintf(tp, name, offset); + (void)fclose(tp); + _exit(0); +} + +void +jkfprintf(tp, name, offset) + FILE *tp; + char name[]; + off_t offset; +{ + FILE *fi; + int linecnt, charcnt, inheader; + char line[BUFSIZ], visline[BUFSIZ*4]; + + if ((fi = fopen(name, "r")) == NULL) + return; + + (void)fseek(fi, offset, SEEK_SET); + /* + * Print the first 7 lines or 560 characters of the new mail + * (whichever comes first). Skip header crap other than + * From, Subject, To, and Date. + */ + linecnt = 7; + charcnt = 560; + inheader = 1; + while (fgets(line, sizeof(line), fi) != NULL) { + if (inheader) { + if (line[0] == '\n') { + inheader = 0; + continue; + } + if (line[0] == ' ' || line[0] == '\t' || + (strncasecmp(line, "From:", 5) && + strncasecmp(line, "Subject:", 8))) + continue; + } + if (linecnt <= 0 || charcnt <= 0) { + (void)fprintf(tp, "...more...%s", cr); + (void)fclose(fi); + return; + } + /* strip weird stuff so can't trojan horse stupid terminals */ + (void)strvis(visline, line, VIS_CSTYLE); + fputs(visline, tp); + --linecnt; + } + (void)fprintf(tp, "----%s\n", cr); + (void)fclose(fi); +} diff --git a/mail_cmds/comsat/comsat.plist b/mail_cmds/comsat/comsat.plist new file mode 100644 index 0000000..48b772a --- /dev/null +++ b/mail_cmds/comsat/comsat.plist @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>Disabled</key> + <true/> + <key>Label</key> + <string>com.apple.comsat</string> + <key>ProgramArguments</key> + <array> + <string>/usr/libexec/comsat</string> + </array> + <key>inetdCompatibility</key> + <dict> + <key>Wait</key> + <true/> + </dict> + <key>Sockets</key> + <dict> + <key>Listeners</key> + <dict> + <key>SockServiceName</key> + <string>comsat</string> + <key>SockType</key> + <string>dgram</string> + </dict> + </dict> +</dict> +</plist> |