mailcompat.c revision e9af4bc0b1cc30cea75d6ad4aa2fde97d985e9be
1N/A/*
1N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Copyright (c) 1983, 1984, 1986, 1986, 1987, 1988, 1989 AT&T
1N/A * All Rights Reserved
1N/A */
1N/A
1N/A/*
1N/A * Vacation
1N/A * Copyright (c) 1983 Eric P. Allman
1N/A * Berkeley, California
1N/A *
1N/A * Copyright (c) 1983 Regents of the University of California.
1N/A * All rights reserved. The Berkeley software License Agreement
1N/A * specifies the terms and conditions for redistribution.
1N/A */
1N/A
1N/A#include <pwd.h>
1N/A#include <stdio.h>
1N/A#include <stdarg.h>
1N/A#include <stdlib.h>
1N/A#include <unistd.h>
1N/A#include <sysexits.h>
1N/A#include <string.h>
1N/A#include <ctype.h>
1N/A#include <sm/bitops.h>
1N/A#include "conf.h"
1N/A
1N/A/*
1N/A * MAILCOMPAT -- Deliver mail to a user's mailbox with the "From's" stuffed.
1N/A */
1N/A
1N/Atypedef int bool;
1N/A
1N/A#define FALSE 0
1N/A#define TRUE 1
1N/A
1N/Abool Debug = FALSE;
1N/Achar *myname; /* person who is to have their mail filtered */
1N/Achar *homedir; /* home directory of said person */
1N/Achar *AliasList[MAXLINE]; /* list of aliases to allow */
1N/Achar *fromp;
1N/Achar *fromuser;
1N/Aint AliasCount = 0;
1N/A
1N/Astatic char *newstr();
1N/A
1N/Aint ask(char *);
1N/Aint sendmessage(char *);
1N/Avoid AutoInstall(void);
1N/Avoid usrerr(const char *, ...);
1N/A
1N/Aint
1N/Amain(argc, argv)
1N/A int argc;
1N/A char **argv;
1N/A{
1N/A register char *p;
1N/A struct passwd *pw;
1N/A extern char *getfrom();
1N/A
1N/A /* process arguments */
1N/A while (--argc > 0 && (p = *++argv) != NULL && *p == '-')
1N/A {
1N/A switch (*++p)
1N/A {
1N/A case 'd': /* debug */
1N/A Debug = TRUE;
1N/A break;
1N/A default:
1N/A usrerr("Unknown flag -%s", p);
1N/A exit(EX_USAGE);
1N/A }
1N/A }
1N/A
1N/A /* verify recipient argument */
1N/A if (argc != 1)
1N/A {
1N/A if (argc == 0)
1N/A AutoInstall();
1N/A else
1N/A usrerr("Usage: mailcompat username (or) mailcompat -r");
1N/A exit(EX_USAGE);
1N/A }
1N/A
1N/A myname = p;
1N/A /* find user's home directory */
1N/A pw = getpwnam(myname);
1N/A if (pw == NULL)
1N/A {
1N/A usrerr("user: %s look up failed, name services outage ?", myname);
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A homedir = newstr(pw->pw_dir);
1N/A
1N/A /* read message from standard input (just from line) */
1N/A fromuser = getfrom(&fromp);
1N/A return (sendmessage(fromuser));
1N/A}
1N/A
1N/A/*
1N/A** sendmessage -- read message from standard input do the from stuffing
1N/A** and forward to /bin/mail, Being sure to delete any
1N/A** content-length headers (/bin/mail recalculates them).
1N/A**
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A**
1N/A** Side Effects:
1N/A** Reads first line from standard input.
1N/A*/
1N/A
1N/A#define L_HEADER "Content-Length:"
1N/A#define LL_HEADER 15
1N/A
1N/Aint
1N/Asendmessage(from)
1N/Achar *from;
1N/A{
1N/A static char line[MAXLINE];
1N/A static char command[MAXLINE];
1N/A bool in_body = FALSE;
1N/A FILE *mail_fp;
1N/A static char user_name[L_cuserid];
1N/A
1N/A if (from == NULL)
1N/A from = cuserid(user_name);
1N/A
1N/A snprintf(command, sizeof (command), "/bin/mail -f %s -d %s", from,
1N/A myname);
1N/A mail_fp = popen(command, "w");
1N/A
1N/A /* read the line */
1N/A while (fgets(line, sizeof line, stdin) != NULL)
1N/A {
1N/A if (line[0] == (char)'\n') /* end of mail headers */
1N/A in_body = TRUE;
1N/A if (in_body && (strncmp(line, "From ", 5) == 0))
1N/A fprintf(mail_fp, ">");
1N/A if (in_body || (strncasecmp(line, L_HEADER, LL_HEADER) != 0))
1N/A fputs(line, mail_fp);
1N/A }
1N/A return (pclose(mail_fp));
1N/A}
1N/A
1N/Achar *
1N/Agetfrom(shortp)
char **shortp;
{
static char line[MAXLINE];
register char *p, *start, *at, *bang;
char saveat;
/* read the from line */
if (fgets(line, sizeof line, stdin) == NULL ||
strncmp(line, "From ", 5) != NULL)
{
usrerr("No initial From line");
exit(EX_USAGE);
}
/* find the end of the sender address and terminate it */
start = &line[5];
p = strchr(start, ' ');
if (p == NULL)
{
usrerr("Funny From line '%s'", line);
exit(EX_USAGE);
}
*p = '\0';
/*
* Strip all but the rightmost UUCP host
* to prevent loops due to forwarding.
* Start searching leftward from the leftmost '@'.
* a!b!c!d yields a short name of c!d
* a!b!c!d@e yields a short name of c!d@e
* e@a!b!c yields the same short name
*/
#ifdef VDEBUG
printf("start='%s'\n", start);
#endif /* VDEBUG */
*shortp = start; /* assume whole addr */
if ((at = strchr(start, '@')) == NULL) /* leftmost '@' */
at = p; /* if none, use end of addr */
saveat = *at;
*at = '\0';
if ((bang = strrchr(start, '!')) != NULL) { /* rightmost '!' */
char *bang2;
*bang = '\0';
if ((bang2 = strrchr(start, '!')) != NULL) /* 2nd rightmost '!' */
*shortp = bang2 + 1; /* move past ! */
*bang = '!';
}
*at = saveat;
#ifdef VDEBUG
printf("place='%s'\n", *shortp);
#endif /* VDEBUG */
/* return the sender address */
return newstr(start);
}
/*
** USRERR -- print user error
**
** Parameters:
** f -- format.
**
** Returns:
** none.
**
** Side Effects:
** none.
*/
void
usrerr(const char *f, ...)
{
va_list alist;
va_start(alist, f);
(void) fprintf(stderr, "mailcompat: ");
(void) vfprintf(stderr, f, alist);
(void) fprintf(stderr, "\n");
va_end(alist);
}
/*
** NEWSTR -- copy a string
**
** Parameters:
** s -- the string to copy.
**
** Returns:
** A copy of the string.
**
** Side Effects:
** none.
*/
char *
newstr(s)
char *s;
{
char *p;
size_t psize = strlen(s) + 1;
p = malloc(psize);
if (p == NULL)
{
usrerr("newstr: cannot alloc memory");
exit(EX_OSERR);
}
strlcpy(p, s, psize);
return (p);
}
/*
* When invoked with no arguments, we fall into an automatic installation
* mode, stepping the user through a default installation.
*/
void
AutoInstall()
{
char forward[MAXLINE];
char line[MAXLINE];
static char user_name[L_cuserid];
FILE *f;
myname = cuserid(user_name);
homedir = getenv("HOME");
if (homedir == NULL) {
usrerr("Home directory unknown");
exit(EX_CONFIG);
}
printf("This program can be used to store your mail in a format\n");
printf("that you can read with SunOS 4.X based mail readers\n");
(void) strlcpy(forward, homedir, sizeof (forward));
(void) strlcat(forward, "/.forward", sizeof (forward));
f = fopen(forward, "r");
if (f) {
printf("You have a .forward file in your home directory");
printf(" containing:\n");
while (fgets(line, MAXLINE, f))
printf(" %s", line);
fclose(f);
if (!ask("Would you like to remove it and disable the mailcompat feature"))
exit(0);
if (unlink(forward))
perror("Error removing .forward file:");
else
printf("Back to normal reception of mail.\n");
exit(0);
}
printf("To enable the mailcompat feature a \".forward\" ");
printf("file is created.\n");
if (!ask("Would you like to enable the mailcompat feature")) {
printf("OK, mailcompat feature NOT enabled.\n");
exit(0);
}
f = fopen(forward, "w");
if (f == NULL) {
perror("Error opening .forward file");
exit(EX_USAGE);
}
fprintf(f, "\"|/usr/bin/mailcompat %s\"\n", myname);
fclose(f);
printf("Mailcompat feature ENABLED.");
printf("Run mailcompat with no arguments to remove it\n");
}
/*
* Ask the user a question until we get a reasonable answer
*/
int
ask(prompt)
char *prompt;
{
char line[MAXLINE];
for (;;) {
printf("%s? ", prompt);
fflush(stdout);
fgets(line, sizeof (line), stdin);
if (line[0] == 'y' || line[0] == 'Y')
return (TRUE);
if (line[0] == 'n' || line[0] == 'N')
return (FALSE);
printf("Please reply \"yes\" or \"no\" (\'y\' or \'n\')\n");
}
/* NOTREACHED */
}