1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Mail -- a mail program
34 *
35 * Author: Kurt Shoens (UCB) March 25, 1978
36 */
37
38#include <sys/param.h>
39#include <sys/stat.h>
40
41#include <ctype.h>
42#include <err.h>
43#include <paths.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <termios.h>
49#include <time.h>
50#include <unistd.h>
51
52#include "pathnames.h"
53
54#define	APPEND				/* New mail goes to end of mailbox */
55
56#define	ESCAPE		'~'		/* Default escape for sending */
57#define	NMLSIZE		1024		/* max names in a message list */
58#define	PATHSIZE	MAXPATHLEN	/* Size of pathnames throughout */
59#define	HSHSIZE		59		/* Hash size for aliases and vars */
60#define	LINESIZE	BUFSIZ		/* max readable line width */
61#define	STRINGSIZE	((unsigned)128)	/* Dynamic allocation units */
62#define	MAXARGC		1024		/* Maximum list of raw strings */
63#define	MAXEXP		25		/* Maximum expansion of aliases */
64
65#define	equal(a, b)	(strcmp(a,b)==0)/* A nice function to string compare */
66
67struct message {
68	short	m_flag;			/* flags, see below */
69	short	m_offset;		/* offset in block of message */
70	long	m_block;		/* block number of this message */
71	long	m_size;			/* Bytes in the message */
72	long	m_lines;		/* Lines in the message */
73};
74
75/*
76 * flag bits.
77 */
78
79#define	MUSED		(1<<0)		/* entry is used, but this bit isn't */
80#define	MDELETED	(1<<1)		/* entry has been deleted */
81#define	MSAVED		(1<<2)		/* entry has been saved */
82#define	MTOUCH		(1<<3)		/* entry has been noticed */
83#define	MPRESERVE	(1<<4)		/* keep entry in sys mailbox */
84#define	MMARK		(1<<5)		/* message is marked! */
85#define	MODIFY		(1<<6)		/* message has been modified */
86#define	MNEW		(1<<7)		/* message has never been seen */
87#define	MREAD		(1<<8)		/* message has been read sometime. */
88#define	MSTATUS		(1<<9)		/* message status has changed */
89#define	MBOX		(1<<10)		/* Send this to mbox, regardless */
90
91/*
92 * Given a file address, determine the block number it represents.
93 */
94#define blockof(off)			((int)((off) / 4096))
95#define boffsetof(off)			((int)((off) % 4096))
96#define positionof(block, offset)	((off_t)(block) * 4096 + (offset))
97
98/*
99 * Format of the command description table.
100 * The actual table is declared and initialized
101 * in lex.c
102 */
103struct cmd {
104	const	char *c_name;		/* Name of command */
105	int	(*c_func)(void *);	/* Implementor of the command */
106	short	c_argtype;		/* Type of arglist (see below) */
107	short	c_msgflag;		/* Required flags of messages */
108	short	c_msgmask;		/* Relevant flags of messages */
109};
110
111/* Yechh, can't initialize unions */
112
113#define	c_minargs c_msgflag		/* Minimum argcount for RAWLIST */
114#define	c_maxargs c_msgmask		/* Max argcount for RAWLIST */
115
116/*
117 * Argument types.
118 */
119
120#define	MSGLIST	 0		/* Message list type */
121#define	STRLIST	 1		/* A pure string */
122#define	RAWLIST	 2		/* Shell string list */
123#define	NOLIST	 3		/* Just plain 0 */
124#define	NDMLIST	 4		/* Message list, no defaults */
125
126#define	P	040		/* Autoprint dot after command */
127#define	I	0100		/* Interactive command bit */
128#define	M	0200		/* Legal from send mode bit */
129#define	W	0400		/* Illegal when read only bit */
130#define	F	01000		/* Is a conditional command */
131#define	T	02000		/* Is a transparent command */
132#define	R	04000		/* Cannot be called from collect */
133
134/*
135 * Oft-used mask values
136 */
137
138#define	MMNORM		(MDELETED|MSAVED)/* Look at both save and delete bits */
139#define	MMNDEL		MDELETED	/* Look only at deleted bit */
140
141/*
142 * Structure used to return a break down of a head
143 * line (hats off to Bill Joy!)
144 */
145
146struct headline {
147	char	*l_from;	/* The name of the sender */
148	char	*l_tty;		/* His tty string (if any) */
149	char	*l_date;	/* The entire date string */
150};
151
152#define	GTO	1		/* Grab To: line */
153#define	GSUBJECT 2		/* Likewise, Subject: line */
154#define	GCC	4		/* And the Cc: line */
155#define	GBCC	8		/* And also the Bcc: line */
156#define	GREPLYTO 0x10		/* And the Reply-To: line */
157#define	GINREPLYTO 0x20		/* The In-Reply-To: line */
158#define	GMASK	(GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO)
159				/* Mask of places from whence */
160
161#define	GNL	0x40		/* Print blank line after */
162#define	GDEL	0x80		/* Entity removed from list */
163#define	GCOMMA	0x100		/* detract puts in commas */
164
165/*
166 * Structure used to pass about the current
167 * state of the user-typed message header.
168 */
169
170struct header {
171	struct	name *h_bcc;		/* Blind carbon copies */
172	struct	name *h_cc;		/* Carbon copies string */
173	struct	name *h_smopts;		/* Sendmail options */
174	struct	name *h_to;		/* Dynamic "To:" string */
175	char	*h_inreplyto;		/* Reference */
176	char	*h_replyto;		/* Reply address */
177	char	*h_subject;		/* Subject string */
178};
179
180/*
181 * Structure of namelist nodes used in processing
182 * the recipients of mail and aliases and all that
183 * kind of stuff.
184 */
185
186struct name {
187	struct	name *n_flink;		/* Forward link in list. */
188	struct	name *n_blink;		/* Backward list link */
189	short	n_type;			/* From which list it came */
190	char	*n_name;		/* This fella's name */
191};
192
193/*
194 * Structure of a variable node.  All variables are
195 * kept on a singly-linked list of these, rooted by
196 * "variables"
197 */
198
199struct var {
200	struct	var *v_link;		/* Forward link to next variable */
201	char	*v_name;		/* The variable's name */
202	char	*v_value;		/* And it's current value */
203};
204
205struct group {
206	struct	group *ge_link;		/* Next person in this group */
207	char	*ge_name;		/* This person's user name */
208};
209
210struct grouphead {
211	struct	grouphead *g_link;	/* Next grouphead in list */
212	char	*g_name;		/* Name of this group */
213	struct	group *g_list;		/* Users in group. */
214};
215
216/*
217 * Structure of the hash table of ignored header fields
218 */
219struct ignoretab {
220	int	i_count;			/* Number of entries */
221	struct	ignore {
222		struct	ignore *i_link;	/* Next ignored field in bucket */
223		char	*i_field;	/* This ignored field */
224	} *i_head[HSHSIZE];
225};
226
227/*
228 * Token values returned by the scanner used for argument lists.
229 * Also, sizes of scanner-related things.
230 */
231
232#define	TEOL	0			/* End of the command line */
233#define	TNUMBER	1			/* A message number */
234#define	TDASH	2			/* A simple dash */
235#define	TSTRING	3			/* A string (possibly containing -) */
236#define	TDOT	4			/* A "." */
237#define	TUP	5			/* An "^" */
238#define	TDOLLAR	6			/* A "$" */
239#define	TSTAR	7			/* A "*" */
240#define	TOPEN	8			/* An '(' */
241#define	TCLOSE	9			/* A ')' */
242#define TPLUS	10			/* A '+' */
243#define TERROR	11			/* A lexical error */
244
245#define	REGDEP	2			/* Maximum regret depth. */
246#define	STRINGLEN	1024		/* Maximum length of string token */
247
248/*
249 * Constants for conditional commands.  These describe whether
250 * we should be executing stuff or not.
251 */
252
253#define	CANY		0		/* Execute in send or receive mode */
254#define	CRCV		1		/* Execute in receive mode only */
255#define	CSEND		2		/* Execute in send mode only */
256
257/*
258 * Kludges to handle the change from setexit / reset to setjmp / longjmp
259 */
260
261#define	setexit()	setjmp(srbuf)
262#define	reset(x)	longjmp(srbuf, x)
263
264/*
265 * Truncate a file to the last character written. This is
266 * useful just before closing an old file that was opened
267 * for read/write.
268 */
269#define trunc(stream) {							\
270	(void)fflush(stream); 						\
271	(void)ftruncate(fileno(stream), ftello(stream));		\
272}
273