1 /*++
2 /* NAME
3 /*	attr_print_plain 3
4 /* SUMMARY
5 /*	send attributes over byte stream
6 /* SYNOPSIS
7 /*	#include <attr.h>
8 /*
9 /*	int	attr_print_plain(fp, flags, type, name, ..., ATTR_TYPE_END)
10 /*	VSTREAM	fp;
11 /*	int	flags;
12 /*	int	type;
13 /*	char	*name;
14 /*
15 /*	int	attr_vprint_plain(fp, flags, ap)
16 /*	VSTREAM	fp;
17 /*	int	flags;
18 /*	va_list	ap;
19 /* DESCRIPTION
20 /*	attr_print_plain() takes zero or more (name, value) simple attributes
21 /*	and converts its input to a byte stream that can be recovered with
22 /*	attr_scan_plain(). The stream is not flushed.
23 /*
24 /*	attr_vprint_plain() provides an alternate interface that is convenient
25 /*	for calling from within variadic functions.
26 /*
27 /*	Attributes are sent in the requested order as specified with the
28 /*	attr_print_plain() argument list. This routine satisfies the formatting
29 /*	rules as outlined in attr_scan_plain(3).
30 /*
31 /*	Arguments:
32 /* .IP fp
33 /*	Stream to write the result to.
34 /* .IP flags
35 /*	The bit-wise OR of zero or more of the following.
36 /* .RS
37 /* .IP ATTR_FLAG_MORE
38 /*	After sending the requested attributes, leave the output stream in
39 /*	a state that is usable for more attribute sending operations on
40 /*	the same output attribute list.
41 /*	By default, attr_print_plain() automatically appends an attribute list
42 /*	terminator when it has sent the last requested attribute.
43 /* .RE
44 /* .IP List of attributes followed by terminator:
45 /* .RS
46 /* .IP "SEND_ATTR_INT(const char *name, int value)"
47 /*	The arguments are an attribute name and an integer.
48 /* .IP "SEND_ATTR_LONG(const char *name, long value)"
49 /*	The arguments are an attribute name and a long integer.
50 /* .IP "SEND_ATTR_STR(const char *name, const char *value)"
51 /*	The arguments are an attribute name and a null-terminated
52 /*	string.
53 /* .IP "SEND_ATTR_DATA(const char *name, ssize_t len, const void *value)"
54 /*	The arguments are an attribute name, an attribute value
55 /*	length, and an attribute value pointer.
56 /* .IP "SEND_ATTR_FUNC(ATTR_PRINT_CUSTOM_FN, const void *value)"
57 /*	The arguments are a function pointer and generic data
58 /*	pointer. The caller-specified function returns whatever the
59 /*	specified attribute printing function returns.
60 /* .IP "SEND_ATTR_HASH(const HTABLE *table)"
61 /* .IP "SEND_ATTR_NAMEVAL(const NVTABLE *table)"
62 /*	The content of the table is sent as a sequence of string-valued
63 /*	attributes with names equal to the table lookup keys.
64 /* .IP ATTR_TYPE_END
65 /*	This terminates the attribute list.
66 /* .RE
67 /* DIAGNOSTICS
68 /*	The result value is 0 in case of success, VSTREAM_EOF in case
69 /*	of trouble.
70 /*
71 /*	Panic: interface violation. All system call errors are fatal.
72 /* SEE ALSO
73 /*	attr_scan_plain(3) recover attributes from byte stream
74 /* LICENSE
75 /* .ad
76 /* .fi
77 /*	The Secure Mailer license must be distributed with this software.
78 /* AUTHOR(S)
79 /*	Wietse Venema
80 /*	IBM T.J. Watson Research
81 /*	P.O. Box 704
82 /*	Yorktown Heights, NY 10598, USA
83 /*
84 /*	Wietse Venema
85 /*	Google, Inc.
86 /*	111 8th Avenue
87 /*	New York, NY 10011, USA
88 /*--*/
89 
90 /* System library. */
91 
92 #include <sys_defs.h>
93 #include <stdarg.h>
94 #include <string.h>
95 
96 /* Utility library. */
97 
98 #include <msg.h>
99 #include <mymalloc.h>
100 #include <vstream.h>
101 #include <htable.h>
102 #include <base64_code.h>
103 #include <vstring.h>
104 #include <attr.h>
105 
106 #define STR(x)	vstring_str(x)
107 #define LEN(x)	VSTRING_LEN(x)
108 
109 /* attr_vprint_plain - send attribute list to stream */
110 
attr_vprint_plain(VSTREAM * fp,int flags,va_list ap)111 int     attr_vprint_plain(VSTREAM *fp, int flags, va_list ap)
112 {
113     const char *myname = "attr_print_plain";
114     int     attr_type;
115     char   *attr_name;
116     unsigned int_val;
117     unsigned long long_val;
118     char   *str_val;
119     HTABLE_INFO **ht_info_list;
120     HTABLE_INFO **ht;
121     static VSTRING *base64_buf;
122     ssize_t len_val;
123     ATTR_PRINT_CUSTOM_FN print_fn;
124     void   *print_arg;
125 
126     /*
127      * Sanity check.
128      */
129     if (flags & ~ATTR_FLAG_ALL)
130 	msg_panic("%s: bad flags: 0x%x", myname, flags);
131 
132     /*
133      * Iterate over all (type, name, value) triples, and produce output on
134      * the fly.
135      */
136     while ((attr_type = va_arg(ap, int)) != ATTR_TYPE_END) {
137 	switch (attr_type) {
138 	case ATTR_TYPE_INT:
139 	    attr_name = va_arg(ap, char *);
140 	    int_val = va_arg(ap, int);
141 	    vstream_fprintf(fp, "%s=%u\n", attr_name, (unsigned) int_val);
142 	    if (msg_verbose)
143 		msg_info("send attr %s = %u", attr_name, (unsigned) int_val);
144 	    break;
145 	case ATTR_TYPE_LONG:
146 	    attr_name = va_arg(ap, char *);
147 	    long_val = va_arg(ap, long);
148 	    vstream_fprintf(fp, "%s=%lu\n", attr_name, long_val);
149 	    if (msg_verbose)
150 		msg_info("send attr %s = %lu", attr_name, long_val);
151 	    break;
152 	case ATTR_TYPE_STR:
153 	    attr_name = va_arg(ap, char *);
154 	    str_val = va_arg(ap, char *);
155 	    vstream_fprintf(fp, "%s=%s\n", attr_name, str_val);
156 	    if (msg_verbose)
157 		msg_info("send attr %s = %s", attr_name, str_val);
158 	    break;
159 	case ATTR_TYPE_DATA:
160 	    attr_name = va_arg(ap, char *);
161 	    len_val = va_arg(ap, ssize_t);
162 	    str_val = va_arg(ap, char *);
163 	    if (base64_buf == 0)
164 		base64_buf = vstring_alloc(10);
165 	    base64_encode(base64_buf, str_val, len_val);
166 	    vstream_fprintf(fp, "%s=%s\n", attr_name, STR(base64_buf));
167 	    if (msg_verbose)
168 		msg_info("send attr %s = [data %ld bytes]",
169 			 attr_name, (long) len_val);
170 	    break;
171 	case ATTR_TYPE_FUNC:
172 	    print_fn = va_arg(ap, ATTR_PRINT_CUSTOM_FN);
173 	    print_arg = va_arg(ap, void *);
174 	    print_fn(attr_print_plain, fp, flags | ATTR_FLAG_MORE, print_arg);
175 	    break;
176 	case ATTR_TYPE_HASH:
177 	    vstream_fwrite(fp, ATTR_NAME_OPEN, sizeof(ATTR_NAME_OPEN));
178 	    VSTREAM_PUTC('\n', fp);
179 	    ht_info_list = htable_list(va_arg(ap, HTABLE *));
180 	    for (ht = ht_info_list; *ht; ht++) {
181 		vstream_fprintf(fp, "%s=%s\n", ht[0]->key, (char *) ht[0]->value);
182 		if (msg_verbose)
183 		    msg_info("send attr name %s value %s",
184 			     ht[0]->key, (char *) ht[0]->value);
185 	    }
186 	    myfree((void *) ht_info_list);
187 	    vstream_fwrite(fp, ATTR_NAME_CLOSE, sizeof(ATTR_NAME_CLOSE));
188 	    VSTREAM_PUTC('\n', fp);
189 	    break;
190 	default:
191 	    msg_panic("%s: unknown type code: %d", myname, attr_type);
192 	}
193     }
194     if ((flags & ATTR_FLAG_MORE) == 0)
195 	VSTREAM_PUTC('\n', fp);
196     return (vstream_ferror(fp));
197 }
198 
attr_print_plain(VSTREAM * fp,int flags,...)199 int     attr_print_plain(VSTREAM *fp, int flags,...)
200 {
201     va_list ap;
202     int     ret;
203 
204     va_start(ap, flags);
205     ret = attr_vprint_plain(fp, flags, ap);
206     va_end(ap);
207     return (ret);
208 }
209 
210 #ifdef TEST
211 
212  /*
213   * Proof of concept test program.  Mirror image of the attr_scan_plain test
214   * program.
215   */
216 #include <msg_vstream.h>
217 
main(int unused_argc,char ** argv)218 int     main(int unused_argc, char **argv)
219 {
220     HTABLE *table = htable_create(1);
221 
222     msg_vstream_init(argv[0], VSTREAM_ERR);
223     msg_verbose = 1;
224     htable_enter(table, "foo-name", mystrdup("foo-value"));
225     htable_enter(table, "bar-name", mystrdup("bar-value"));
226     attr_print_plain(VSTREAM_OUT, ATTR_FLAG_NONE,
227 		     SEND_ATTR_STR("protocol", "test"),
228 		     SEND_ATTR_INT(ATTR_NAME_INT, 4711),
229 		     SEND_ATTR_LONG(ATTR_NAME_LONG, 1234L),
230 		     SEND_ATTR_STR(ATTR_NAME_STR, "whoopee"),
231 	       SEND_ATTR_DATA(ATTR_NAME_DATA, strlen("whoopee"), "whoopee"),
232 		     SEND_ATTR_HASH(table),
233 		     SEND_ATTR_LONG(ATTR_NAME_LONG, 4321L),
234 		     ATTR_TYPE_END);
235     attr_print_plain(VSTREAM_OUT, ATTR_FLAG_NONE,
236 		     SEND_ATTR_STR("protocol", "test"),
237 		     SEND_ATTR_INT(ATTR_NAME_INT, 4711),
238 		     SEND_ATTR_LONG(ATTR_NAME_LONG, 1234L),
239 		     SEND_ATTR_STR(ATTR_NAME_STR, "whoopee"),
240 	       SEND_ATTR_DATA(ATTR_NAME_DATA, strlen("whoopee"), "whoopee"),
241 		     ATTR_TYPE_END);
242     attr_print_plain(VSTREAM_OUT, ATTR_FLAG_NONE,
243 		     SEND_ATTR_STR("protocol", "not-test"),
244 		     ATTR_TYPE_END);
245     if (vstream_fflush(VSTREAM_OUT) != 0)
246 	msg_fatal("write error: %m");
247 
248     htable_free(table, myfree);
249     return (0);
250 }
251 
252 #endif
253