xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/rcpt_buf.c (revision 67b9b338a7386232ac596b5fd0cd5a9cc8a03c71)
1 /*	$NetBSD: rcpt_buf.c,v 1.4 2022/10/08 16:12:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	rcpt_buf
6 /* SUMMARY
7 /*	recipient buffer manager
8 /* SYNOPSIS
9 /*	#include <rcpt_buf.h>
10 /*
11 /*	typedef struct {
12 /*		RECIPIENT rcpt;		/* convenience */
13 /* .in +4
14 /*		VSTRING *address;	/* final recipient */
15 /*		VSTRING *orig_addr;	/* original recipient */
16 /*		VSTRING *dsn_orcpt;	/* dsn original recipient */
17 /*		int     dsn_notify;	/* DSN notify flags */
18 /*		long    offset;		/* REC_TYPE_RCPT byte */
19 /* .in -4
20 /*	} RCPT_BUF;
21 /*
22 /*	RECIPIENT *RECIPIENT_FROM_RCPT_BUF(rcpb)
23 /*	RCPT_BUF *rcpb;
24 /*
25 /*	RCPT_BUF *rcpb_create(void)
26 /*
27 /*	void	rcpb_reset(rcpb)
28 /*	RCPT_BUF *rcpb;
29 /*
30 /*	void	rcpb_free(rcpb)
31 /*	RCPT_BUF *rcpb;
32 /*
33 /*	int	rcpb_scan(scan_fn, stream, flags, ptr)
34 /*	ATTR_SCAN_COMMON_FN scan_fn;
35 /*	VSTREAM *stream;
36 /*	int	flags;
37 /*	void	*ptr;
38 /* DESCRIPTION
39 /*	RECIPIENT_FROM_RCPT_BUF() populates the rcpt member with
40 /*	a shallow copy of the contents of the other fields.
41 /*
42 /*	rcpb_scan() reads a recipient buffer from the named stream
43 /*	using the specified attribute scan routine. rcpb_scan()
44 /*	is meant to be passed as a call-back to attr_scan(), thusly:
45 /*
46 /*	... ATTR_TYPE_FUNC, rcpb_scan, (void *) rcpt_buf, ...
47 /*
48 /*	rcpb_create(), rcpb_reset() and rcpb_free() create, wipe
49 /*	and destroy recipient buffer instances.
50 /* DIAGNOSTICS
51 /*	Fatal: out of memory.
52 /* LICENSE
53 /* .ad
54 /* .fi
55 /*	The Secure Mailer license must be distributed with this software.
56 /* AUTHOR(S)
57 /*	Wietse Venema
58 /*	IBM T.J. Watson Research
59 /*	P.O. Box 704
60 /*	Yorktown Heights, NY 10598, USA
61 /*
62 /*	Wietse Venema
63 /*	Google, Inc.
64 /*	111 8th Avenue
65 /*	New York, NY 10011, USA
66 /*--*/
67 
68 /* System library. */
69 
70 #include <sys_defs.h>
71 
72 /* Utility library. */
73 
74 #include <mymalloc.h>
75 #include <vstring.h>
76 #include <vstream.h>
77 
78 /* Global library. */
79 
80 #include <mail_proto.h>
81 #include <rcpt_buf.h>
82 
83 /* Application-specific. */
84 
85 /* rcpb_create - create recipient buffer */
86 
rcpb_create(void)87 RCPT_BUF *rcpb_create(void)
88 {
89     RCPT_BUF *rcpt;
90 
91     rcpt = (RCPT_BUF *) mymalloc(sizeof(*rcpt));
92     rcpt->offset = 0;
93     rcpt->dsn_orcpt = vstring_alloc(10);
94     rcpt->dsn_notify = 0;
95     rcpt->orig_addr = vstring_alloc(10);
96     rcpt->address = vstring_alloc(10);
97     return (rcpt);
98 }
99 
100 /* rcpb_reset - reset recipient buffer */
101 
rcpb_reset(RCPT_BUF * rcpt)102 void    rcpb_reset(RCPT_BUF *rcpt)
103 {
104 #define BUF_TRUNCATE(s) (vstring_str(s)[0] = 0)
105 
106     rcpt->offset = 0;
107     BUF_TRUNCATE(rcpt->dsn_orcpt);
108     rcpt->dsn_notify = 0;
109     BUF_TRUNCATE(rcpt->orig_addr);
110     BUF_TRUNCATE(rcpt->address);
111 }
112 
113 /* rcpb_free - destroy recipient buffer */
114 
rcpb_free(RCPT_BUF * rcpt)115 void    rcpb_free(RCPT_BUF *rcpt)
116 {
117     vstring_free(rcpt->dsn_orcpt);
118     vstring_free(rcpt->orig_addr);
119     vstring_free(rcpt->address);
120     myfree((void *) rcpt);
121 }
122 
123 /* rcpb_scan - receive recipient buffer */
124 
rcpb_scan(ATTR_SCAN_COMMON_FN scan_fn,VSTREAM * fp,int flags,void * ptr)125 int     rcpb_scan(ATTR_SCAN_COMMON_FN scan_fn, VSTREAM *fp,
126 		          int flags, void *ptr)
127 {
128     RCPT_BUF *rcpt = (RCPT_BUF *) ptr;
129     int     ret;
130 
131     /*
132      * The order of attributes is determined by historical compatibility and
133      * can be fixed after all the ad-hoc read/write code is replaced.
134      */
135     ret = scan_fn(fp, flags | ATTR_FLAG_MORE,
136 		  RECV_ATTR_STR(MAIL_ATTR_ORCPT, rcpt->orig_addr),
137 		  RECV_ATTR_STR(MAIL_ATTR_RECIP, rcpt->address),
138 		  RECV_ATTR_LONG(MAIL_ATTR_OFFSET, &rcpt->offset),
139 		  RECV_ATTR_STR(MAIL_ATTR_DSN_ORCPT, rcpt->dsn_orcpt),
140 		  RECV_ATTR_INT(MAIL_ATTR_DSN_NOTIFY, &rcpt->dsn_notify),
141 		  ATTR_TYPE_END);
142     return (ret == 5 ? 1 : -1);
143 }
144