xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/rcpt_buf.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: rcpt_buf.c,v 1.2 2017/02/14 01:16: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_MASTER_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 
63 /* Syste, library. */
64 
65 #include <sys_defs.h>
66 
67 /* Utility library. */
68 
69 #include <mymalloc.h>
70 #include <vstring.h>
71 #include <vstream.h>
72 
73 /* Global library. */
74 
75 #include <mail_proto.h>
76 #include <rcpt_buf.h>
77 
78 /* Application-specific. */
79 
80 /* rcpb_create - create recipient buffer */
81 
82 RCPT_BUF *rcpb_create(void)
83 {
84     RCPT_BUF *rcpt;
85 
86     rcpt = (RCPT_BUF *) mymalloc(sizeof(*rcpt));
87     rcpt->offset = 0;
88     rcpt->dsn_orcpt = vstring_alloc(10);
89     rcpt->dsn_notify = 0;
90     rcpt->orig_addr = vstring_alloc(10);
91     rcpt->address = vstring_alloc(10);
92     return (rcpt);
93 }
94 
95 /* rcpb_reset - reset recipient buffer */
96 
97 void    rcpb_reset(RCPT_BUF *rcpt)
98 {
99 #define BUF_TRUNCATE(s) (vstring_str(s)[0] = 0)
100 
101     rcpt->offset = 0;
102     BUF_TRUNCATE(rcpt->dsn_orcpt);
103     rcpt->dsn_notify = 0;
104     BUF_TRUNCATE(rcpt->orig_addr);
105     BUF_TRUNCATE(rcpt->address);
106 }
107 
108 /* rcpb_free - destroy recipient buffer */
109 
110 void    rcpb_free(RCPT_BUF *rcpt)
111 {
112     vstring_free(rcpt->dsn_orcpt);
113     vstring_free(rcpt->orig_addr);
114     vstring_free(rcpt->address);
115     myfree((void *) rcpt);
116 }
117 
118 /* rcpb_scan - receive recipient buffer */
119 
120 int     rcpb_scan(ATTR_SCAN_MASTER_FN scan_fn, VSTREAM *fp,
121 		          int flags, void *ptr)
122 {
123     RCPT_BUF *rcpt = (RCPT_BUF *) ptr;
124     int     ret;
125 
126     /*
127      * The order of attributes is determined by historical compatibility and
128      * can be fixed after all the ad-hoc read/write code is replaced.
129      */
130     ret = scan_fn(fp, flags | ATTR_FLAG_MORE,
131 		  RECV_ATTR_STR(MAIL_ATTR_ORCPT, rcpt->orig_addr),
132 		  RECV_ATTR_STR(MAIL_ATTR_RECIP, rcpt->address),
133 		  RECV_ATTR_LONG(MAIL_ATTR_OFFSET, &rcpt->offset),
134 		  RECV_ATTR_STR(MAIL_ATTR_DSN_ORCPT, rcpt->dsn_orcpt),
135 		  RECV_ATTR_INT(MAIL_ATTR_DSN_NOTIFY, &rcpt->dsn_notify),
136 		  ATTR_TYPE_END);
137     return (ret == 5 ? 1 : -1);
138 }
139