xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/mail_command_client.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: mail_command_client.c,v 1.3 2020/03/18 19:05:16 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	mail_command_client 3
6 /* SUMMARY
7 /*	single-command client
8 /* SYNOPSIS
9 /*	#include <mail_proto.h>
10 /*
11 /*	int	mail_command_client(class, name, type, attr, ...)
12 /*	const char *class;
13 /*	const char *name;
14 /*	int	type;
15 /*	const char *attr;
16 /* DESCRIPTION
17 /*	This module implements a client interface for single-command
18 /*	clients: a client that sends a single command and expects
19 /*	a single completion status code.
20 /*
21 /*	Arguments:
22 /* .IP class
23 /*	Service type: MAIL_CLASS_PUBLIC or MAIL_CLASS_PRIVATE
24 /* .IP name
25 /*	Service name (master.cf).
26 /* .IP "type, attr, ..."
27 /*	Attribute information as defined in attr_print(3).
28 /* DIAGNOSTICS
29 /*	The result is -1 if the request could not be sent, otherwise
30 /*	the result is the status reported by the server.
31 /*	Warnings: problems connecting to the requested service.
32 /*	Fatal: out of memory.
33 /* SEE ALSO
34 /*	attr_print(3), send attributes over byte stream
35 /*	mail_command_server(3), server interface
36 /*	mail_proto(3h), client-server protocol
37 /* LICENSE
38 /* .ad
39 /* .fi
40 /*	The Secure Mailer license must be distributed with this software.
41 /* AUTHOR(S)
42 /*	Wietse Venema
43 /*	IBM T.J. Watson Research
44 /*	P.O. Box 704
45 /*	Yorktown Heights, NY 10598, USA
46 /*
47 /*	Wietse Venema
48 /*	Google, Inc.
49 /*	111 8th Avenue
50 /*	New York, NY 10011, USA
51 /*--*/
52 
53 /* System library. */
54 
55 #include <sys_defs.h>
56 #include <stdlib.h>			/* 44BSD stdarg.h uses abort() */
57 #include <stdarg.h>
58 
59 /* Utility library. */
60 
61 #include <vstream.h>
62 #include <msg.h>
63 
64 /* Global library. */
65 
66 #include <mail_proto.h>
67 
68 /* mail_command_client - single-command transaction with completion status */
69 
70 int     mail_command_client(const char *class, const char *name,...)
71 {
72     va_list ap;
73     VSTREAM *stream;
74     int     status;
75 
76     /*
77      * Talk a little protocol with the specified service.
78      *
79      * This function is used for non-critical services where it is OK to back
80      * off after the first error. Log what communication stage failed, to
81      * facilitate trouble analysis.
82      */
83     if ((stream = mail_connect(class, name, BLOCKING)) == 0) {
84 	msg_warn("connect to %s/%s: %m", class, name);
85 	return (-1);
86     }
87     va_start(ap, name);
88     status = attr_vprint(stream, ATTR_FLAG_NONE, ap);
89     va_end(ap);
90     if (status != 0) {
91 	msg_warn("write %s: %m", VSTREAM_PATH(stream));
92 	status = -1;
93     } else if (attr_scan(stream, ATTR_FLAG_STRICT,
94 			 RECV_ATTR_INT(MAIL_ATTR_STATUS, &status), 0) != 1) {
95 	msg_warn("write/read %s: %m", VSTREAM_PATH(stream));
96 	status = -1;
97     }
98     (void) vstream_fclose(stream);
99     return (status);
100 }
101