xref: /netbsd-src/usr.bin/mail/def.h (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: def.h,v 1.26 2007/10/27 15:14:50 christos Exp $	*/
2 /*
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)def.h	8.4 (Berkeley) 4/20/95
31  *	$NetBSD: def.h,v 1.26 2007/10/27 15:14:50 christos Exp $
32  */
33 
34 /*
35  * Mail -- a mail program
36  *
37  * Author: Kurt Shoens (UCB) March 25, 1978
38  */
39 
40 #ifndef __DEF_H__
41 #define __DEF_H__
42 
43 #include <sys/types.h>
44 #include <sys/file.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/wait.h>
50 
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <termios.h>
62 #include <time.h>
63 #include <unistd.h>
64 #include "pathnames.h"
65 
66 #define	APPEND				/* New mail goes to end of mailbox */
67 
68 #define COMMENT_CHAR	'#'		/* Comment character when sourcing */
69 #define	ESCAPE		'~'		/* Default escape for sending */
70 #define	NMLSIZE		1024		/* max names in a message list */
71 #define	PATHSIZE	MAXPATHLEN	/* Size of pathnames throughout */
72 #define	HSHSIZE		59		/* Hash size for aliases and vars */
73 #define	LINESIZE	BUFSIZ		/* max readable line width */
74 #define	MAXARGC		1024		/* Maximum list of raw strings */
75 #define	MAXEXP		25		/* Maximum expansion of aliases */
76 
77 #define PUBLIC			/* make it easy to find the entry points */
78 
79 /*
80  * User environment variable names.
81  * See complete.h, mime.h, and thread.h for names specific to those modules.
82  */
83 #define	ENAME_INDENT_POSTSCRIPT	"indentpostscript"
84 #define	ENAME_INDENT_PREAMBLE	"indentpreamble"
85 #define ENAME_APPEND		"append"
86 #define ENAME_ASK		"ask"
87 #define ENAME_ASKBCC		"askbcc"
88 #define ENAME_ASKCC		"askcc"
89 #define ENAME_ASKSUB		"asksub"
90 #define ENAME_AUTOINC		"autoinc"
91 #define ENAME_AUTOPRINT		"autoprint"
92 #define ENAME_CRT		"crt"
93 #define ENAME_DEAD		"DEAD"
94 #define ENAME_DEBUG		"debug"
95 #define ENAME_DONTSENDEMPTY	"dontsendempty"
96 #define ENAME_DOT		"dot"
97 #define ENAME_EDITOR		"EDITOR"
98 #define ENAME_ENABLE_PIPES	"enable-pipes"
99 #define ENAME_ESCAPE		"escape"
100 #define ENAME_FOLDER		"folder"
101 #define ENAME_HEADER_FORMAT	"header-format"
102 #define ENAME_HOLD		"hold"
103 #define ENAME_IGNORE		"ignore"
104 #define ENAME_IGNOREEOF		"ignoreeof"
105 #define ENAME_INDENTPREFIX	"indentprefix"
106 #define ENAME_INTERACTIVE	"interactive"
107 #define ENAME_KEEP		"keep"
108 #define ENAME_KEEPSAVE		"keepsave"
109 #define ENAME_LISTER		"LISTER"
110 #define ENAME_MBOX		"MBOX"
111 #define ENAME_METOO		"metoo"
112 #define ENAME_NOHEADER		"noheader"
113 #define ENAME_NOSAVE		"nosave"
114 #define ENAME_PAGER		"PAGER"
115 #define ENAME_PAGER_OFF		"pager-off"
116 #define ENAME_PROMPT		"prompt"
117 #define ENAME_QUIET		"quiet"
118 #define ENAME_RECORD		"record"
119 #define ENAME_REGEX_SEARCH	"regex-search"
120 #define ENAME_REPLYALL		"Replyall"
121 #define ENAME_REPLYASRECIPIENT	"ReplyAsRecipient"
122 #define ENAME_SCREEN		"screen"
123 #define ENAME_SCREENHEIGHT	"screenheight"
124 #define ENAME_SCREENWIDTH	"screenwidth"
125 #define ENAME_SEARCHHEADERS	"searchheaders"
126 #define ENAME_SENDMAIL		"sendmail"
127 #define ENAME_SHELL		"SHELL"
128 #define ENAME_SHOW_RCPT		"show-rcpt"
129 #define ENAME_SMOPTS_VERIFY	"smopts-verify"
130 #define ENAME_TOPLINES		"toplines"
131 #define ENAME_VERBOSE		"verbose"
132 #define ENAME_VISUAL		"VISUAL"
133 
134 #define sizeofarray(a)	(sizeof(a)/sizeof(*a))
135 #define	equal(a, b)	(strcmp(a,b)==0)/* A nice function to string compare */
136 
137 struct message {
138 	short	m_flag;			/* flags, see below */
139 	short	m_offset;		/* offset in block of message */
140 	long	m_block;		/* block number of this message */
141 	long	m_lines;		/* Lines in the message */
142 	off_t	m_size;			/* Bytes in the message */
143 	long	m_blines;		/* Body (non-header) lines */
144 
145 	/*
146 	 * threading fields
147 	 */
148 	int		m_index;	/* message index in this thread */
149 	int		m_depth;	/* depth in thread */
150 	struct message *m_flink;	/* link to next message */
151 	struct message *m_blink;	/* link to previous message */
152 	struct message *m_clink;	/* link to child of this message */
153 	struct message *m_plink;	/* link to parent of thread */
154 };
155 typedef struct mime_info mime_info_t;	/* phantom structure only to attach.c */
156 
157 /*
158  * flag bits.
159  */
160 
161 #define	MUSED		(1<<0)		/* entry is used, but this bit isn't */
162 #define	MDELETED	(1<<1)		/* entry has been deleted */
163 #define	MSAVED		(1<<2)		/* entry has been saved */
164 #define	MTOUCH		(1<<3)		/* entry has been noticed */
165 #define	MPRESERVE	(1<<4)		/* keep entry in sys mailbox */
166 #define	MMARK		(1<<5)		/* message is marked! */
167 #define	MMODIFY		(1<<6)		/* message has been modified */
168 #define	MNEW		(1<<7)		/* message has never been seen */
169 #define	MREAD		(1<<8)		/* message has been read sometime. */
170 #define	MSTATUS		(1<<9)		/* message status has changed */
171 #define	MBOX		(1<<10)		/* Send this to mbox, regardless */
172 #define MTAGGED		(1<<11)		/* message has been tagged */
173 
174 /*
175  * Given a file address, determine the block number it represents.
176  */
177 #define blockof(off)			((int) ((off) / 4096))
178 #define blkoffsetof(off)		((int) ((off) % 4096))
179 #define positionof(block, offset)	((off_t)(block) * 4096 + (offset))
180 
181 /*
182  * Format of the command description table.
183  * The actual table is declared and initialized
184  * in lex.c
185  */
186 struct cmd {
187 	const char *c_name;		/* Name of command */
188 	int	(*c_func)(void *);	/* Implementor of the command */
189 	int	c_pipe;			/* Pipe output through the pager */
190 # define C_PIPE_PAGER	1		/* enable use of pager */
191 # define C_PIPE_CRT	2		/* use the pager if CRT is defined */
192 # define C_PIPE_SHELL	4		/* enable shell pipes */
193 #ifdef USE_EDITLINE
194 	const char *c_complete;		/* String describing completion */
195 #endif
196 	short	c_argtype;		/* Type of arglist (see below) */
197 	short	c_msgflag;		/* Required flags of messages */
198 	short	c_msgmask;		/* Relevant flags of messages */
199 };
200 
201 /* Yechh, can't initialize unions */
202 
203 #define	c_minargs c_msgflag		/* Minimum argcount for RAWLIST */
204 #define	c_maxargs c_msgmask		/* Max argcount for RAWLIST */
205 
206 /*
207  * Argument types.
208  */
209 
210 #define	MSGLIST	 0		/* Message list type */
211 #define	STRLIST	 1		/* A pure string */
212 #define	RAWLIST	 2		/* Shell string list */
213 #define	NOLIST	 3		/* Just plain 0 */
214 #define	NDMLIST	 4		/* Message list, no defaults */
215 
216 #define	P	0x010		/* Autoprint dot after command */
217 #define	I	0x020		/* Interactive command bit */
218 #define	M	0x040		/* Legal from send mode bit */
219 #define	W	0x080		/* Illegal when read only bit */
220 #define	F	0x100		/* Is a conditional command */
221 #define	T	0x200		/* Is a transparent command */
222 #define	R	0x400		/* Cannot be called from collect */
223 #define ARGTYPE_MASK	~(P|I|M|W|F|T|R)
224 
225 /*
226  * Oft-used mask values
227  */
228 
229 #define	MMNORM		(MDELETED|MSAVED)/* Look at both save and delete bits */
230 #define	MMNDEL		MDELETED	/* Look only at deleted bit */
231 
232 /*
233  * Structure used to return a break down of a head
234  * line (hats off to Bill Joy!)
235  */
236 
237 struct headline {
238 	char	*l_from;	/* The name of the sender */
239 	char	*l_tty;		/* His tty string (if any) */
240 	char	*l_date;	/* The entire date string */
241 };
242 
243 #define	GTO	 0x001		/* Grab To: line */
244 #define	GSUBJECT 0x002		/* Likewise, Subject: line */
245 #define	GCC	 0x004		/* And the Cc: line */
246 #define	GBCC	 0x008		/* And also the Bcc: line */
247 #define GSMOPTS  0x010		/* Grab the sendmail options */
248 #define GMISC	 0x020		/* miscellaneous extra fields for sending */
249 #ifdef MIME_SUPPORT
250 #define GMIME    0x040		/* mime flag */
251 #endif
252 #define	GMASK	(GTO|GSUBJECT|GCC|GBCC|GSMOPTS)
253 				/* Mask of places from whence */
254 
255 #define	GNL	 0x100		/* Print blank line after */
256 #define	GDEL	 0x200		/* Entity removed from list */
257 #define	GCOMMA	 0x400		/* detract puts in commas */
258 
259 #ifdef MIME_SUPPORT
260 /*
261  * Structure of MIME content.
262  */
263 struct Content {
264 	const char *C_type;		/* content type */
265 	const char *C_encoding;		/* content transfer encoding */
266 	const char *C_disposition;	/* content disposition */
267 	const char *C_description;	/* content description */
268 	const char *C_id;		/* content id */
269 };
270 /* Header strings corresponding to the above Content fields. */
271 #define MIME_HDR_TYPE		"Content-Type"
272 #define MIME_HDR_ENCODING	"Content-Transfer-Encoding"
273 #define MIME_HDR_DISPOSITION	"Content-Disposition"
274 #define MIME_HDR_ID		"Content-ID"
275 #define MIME_HDR_DESCRIPTION	"Content-Description"
276 #define MIME_HDR_VERSION	"MIME-Version"
277 /* the value of the MIME-Version field */
278 #define MIME_VERSION		"1.0"
279 
280 typedef enum {
281 	ATTACH_INVALID = 0,	/* do not use! */
282 	ATTACH_FNAME = 1,
283 	ATTACH_MSG = 2,
284 	ATTACH_FILENO = 3
285 } attach_t;
286 
287 /*
288  * Structure of a MIME attachment.
289  */
290 struct attachment {
291 	struct attachment *a_flink;	/* Forward link in list. */
292 	struct attachment *a_blink;	/* Backward list link */
293 
294 	attach_t a_type;		/* attachment type */
295 #if 1
296 	union {
297 		char *u_name;		/* file name */
298 		struct message *u_msg;	/* message */
299 		int u_fileno;		/* file number */
300 	} a_u;
301 
302 	#define a_name		a_u.u_name
303 	#define a_msg		a_u.u_msg
304 	#define a_fileno	a_u.u_fileno
305 #else
306 	char *a_name;			/* file name */
307 	struct message *a_msg;		/* message */
308 	int a_fileno;			/* file number */
309 #endif
310 
311 	struct Content a_Content;	/* MIME content strings */
312 };
313 #endif /* MIME_SUPPORT */
314 
315 /*
316  * Structure used to pass about the current
317  * state of the user-typed message header.
318  */
319 
320 struct header {
321 	struct name *h_to;		/* Dynamic "To:" string */
322 	char *h_subject;		/* Subject string */
323 	struct name *h_cc;		/* Carbon copies string */
324 	struct name *h_bcc;		/* Blind carbon copies */
325 	struct name *h_smopts;		/* Sendmail options */
326 	char *h_in_reply_to;
327 	struct name *h_references;
328 	char *h_extra;			/* extra lines to output */
329 #ifdef MIME_SUPPORT
330 	char *h_mime_boundary;		/* MIME multipart boundary string */
331 	struct Content h_Content;	/* MIME content for message */
332 	struct attachment *h_attach;	/* MIME attachments */
333 #endif
334 };
335 
336 /*
337  * Structure of namelist nodes used in processing
338  * the recipients of mail and aliases and all that
339  * kind of stuff.
340  */
341 
342 struct name {
343 	struct	name *n_flink;		/* Forward link in list. */
344 	struct	name *n_blink;		/* Backward list link */
345 	short	n_type;			/* From which list it came */
346 	char	*n_name;		/* This fella's name */
347 };
348 
349 /*
350  * Structure of a variable node.  All variables are
351  * kept on a singly-linked list of these, rooted by
352  * "variables"
353  */
354 
355 struct var {
356 	struct	var *v_link;		/* Forward link to next variable */
357 	char	*v_name;		/* The variable's name */
358 	char	*v_value;		/* And it's current value */
359 };
360 
361 struct group {
362 	struct	group *ge_link;		/* Next person in this group */
363 	char	*ge_name;		/* This person's user name */
364 };
365 
366 struct grouphead {
367 	struct	grouphead *g_link;	/* Next grouphead in list */
368 	char	*g_name;		/* Name of this group */
369 	struct	group *g_list;		/* Users in group. */
370 };
371 
372 struct smopts_s {
373 	struct smopts_s *s_link;	/* Link to next smopts_s in list */
374 	char *s_name;			/* Name of this smopts_s */
375 	struct name *s_smopts;		/* sendmail options name list */
376 };
377 
378 /*
379  * Structure of the hash table of ignored header fields
380  */
381 struct ignoretab {
382 	size_t i_count;			/* Number of entries */
383 	struct ignore {
384 		struct ignore *i_link;	/* Next ignored field in bucket */
385 		char *i_field;		/* This ignored field */
386 	} *i_head[HSHSIZE];
387 };
388 
389 /*
390  * Constants for conditional commands.  These control whether we
391  * should be executing commands or not.
392  */
393 struct cond_stack_s {
394 	struct cond_stack_s *c_next;
395 	int c_cond;
396 };
397 #define	CNONE		0x00		/* Execute everything */
398 #define	CSKIP		0x01		/* Do not execute commands */
399 #define	CIF		0x02		/* Inside if/endif block */
400 #define	CELSE		0x04		/* The last conditional was else */
401 #define	CIGN		0x08		/* Conditional in a skipped block */
402 
403 enum mailmode_e {
404 	mm_receiving,			/* receiving mail mode */
405 	mm_sending,			/* sending mail mode */
406 	mm_hdrsonly			/* headers only mode */
407 };
408 
409 /*
410  * Kludges to handle the change from setexit / reset to setjmp / longjmp
411  */
412 
413 #define	setexit()	(void)setjmp(srbuf)
414 #define	reset(x)	longjmp(srbuf, x)
415 
416 /*
417  * Truncate a file to the last character written. This is
418  * useful just before closing an old file that was opened
419  * for read/write.
420  */
421 #define trunc(stream) {							\
422 	(void)fflush(stream); 						\
423 	(void)ftruncate(fileno(stream), (off_t)ftell(stream));		\
424 }
425 
426 /*
427  * White Space (WSP) as specified in see RFC 2822.
428  *
429  * NOTE: Use this in place of isblank() so it is inline.  Also, unlike
430  * the table implemented ctype(3) routines, this does not have input
431  * range issues caused by sign extensions.
432  *
433  * See mime_header.h for the related is_FWS().
434  */
435 static inline int
436 is_WSP(int c)
437 {
438 	return c == ' ' || c == '\t';
439 }
440 
441 static inline char*
442 skip_WSP(const char *cp)
443 {
444 	while (is_WSP(*cp))
445 		cp++;
446 	return __UNCONST(cp);
447 }
448 
449 static inline char*
450 skip_space(char *p)
451 {
452 	while (isspace((unsigned char)*p))
453 		p++;
454 	return p;
455 }
456 
457 #endif /* __DEF_H__ */
458