2 * Network security library routines for nmh.
4 * These are a common set of routines to handle network security for
5 * things like SASL and OpenSSL.
8 struct _netsec_context
;
9 typedef struct _netsec_context netsec_context
;
12 * Create a network security context. Returns the allocated network
13 * security context. Cannot fail.
16 netsec_context
*netsec_init(void);
19 * Shuts down the security context for a connection and frees all
20 * associated resources.
24 * ns_context - Network security context
27 void netsec_shutdown(netsec_context
*ns_context
);
30 * Sets the file descriptor for this connection. This will be used by
31 * the underlying communication.
35 * ns_context - Network security context
36 * fd - File descriptor of network connection.
39 void netset_set_fd(netsec_context
*ns_context
, int fd
);
42 * Set the userid used to authenticate to this connection.
46 * ns_context - Network security context
47 * userid - Userid to be used for authentication. Cannot be NULL.
50 void netsec_set_userid(netsec_context
*ns_context
, const char *userid
);
53 * Sets "snoop" status; if snoop is set to a nonzero value, network traffic
54 * will be logged on standard error.
58 * ns_context - Network security context
59 * snoop - Integer value; set to nonzero to enable traffic logging
62 void netsec_set_snoop(netsec_context
*ns_context
, int snoop
);
65 * Read a "line" from the network. This reads one CR/LF terminated line.
66 * Returns a pointer to a NUL-terminated string. This memory is valid
67 * until the next call to any read function. Will return an error if
68 * the line does not terminated with CR/LF. Note that this will not work
69 * if the data might have embedded NULs.
73 * ns_context - Network security context
74 * errstr - Error string
76 * Returns pointer to string, or NULL on error.
79 char *netsec_readline(netsec_context
*ns_context
, char **errstr
);
82 * Read bytes from the network.
86 * ns_context - Network security context
87 * buffer - Read buffer
91 * Returns number of bytes read, or -1 on error.
94 ssize_t
netsec_read(netsec_context
*ns_context
, void *buffer
, size_t size
,
98 * Write data to the network; if encryption is being performed, we will
99 * do it. Data may be buffered; use netsec_flush() to flush any outstanding
100 * data to the network.
104 * ns_context - Network security context
105 * buffer - Output buffer to write to network
106 * size - Size of data to write to network
107 * errstr - Error string
109 * Returns OK on success, NOTOK otherwise.
112 int netsec_write(netsec_context
*ns_context
, const void *buffer
, size_t size
,
116 * Write bytes using printf formatting
120 * ns_context - Network security context
121 * errstr - Error string
122 * format - Format string
123 * ... - Arguments for format string
125 * Returns OK on success, NOTOK on error.
128 int netsec_printf(netsec_context
*ns_context
, char **errstr
,
129 const char *format
, ...);
132 * Flush any buffered bytes to the network.
136 * ns_context - Network security context
137 * errstr - Error string
139 * Returns OK on success, NOTOK on error.
142 int netsec_flush(netsec_context
*ns_context
, char **errstr
);
145 * Enumerated types for the type of message we are sending/receiving.
148 enum sasl_message_type
{
149 NETSEC_SASL_START
, /* Start of SASL authentication */
150 NETSEC_SASL_READ
, /* Reading a message */
151 NETSEC_SASL_WRITE
, /* Writing a message */
152 NETSEC_SASL_CANCEL
/* Cancel a SASL exchange */
156 * The SASL callback; this is designed to parse a protocol-specific
157 * message and return the SASL protocol message back.
159 * The meaning of the arguments to the callback depend on the mtype
160 * arguments. See below for more detail.
164 * mtype - The type of message we are processing (read/write/cancel).
165 * indata - A pointer to any input data.
166 * indatasize - The size of the input data in bytes
167 * outdata - Output data (freed by caller)
168 * outdatasize - Size of output data
169 * snoop - If set to true, plugin should log SASL exchange to stderr.
170 * errstr - An error string to be returned (freed by caller).
172 * Parameter interpretation based on mtype value:
174 * NETSEC_SASL_START - Create a protocol message that starts SASL
175 * authentication. If an initial response is
176 * supported, indata and indatasize will contain it.
177 * Otherwise they will be set to NULL and 0.
178 * The complete protocol message should be
179 * stored in outdata/outdatasize, to be free()d
181 * NETSEC_SASL_READ - Parse and decode a protocol message and extract
182 * out the SASL payload data. indata will be set
183 * to NULL; the callback must read in the necessary
184 * data using the appropriate netsec function.
185 * outdata/outdatasize should contain the decoded
186 * SASL message (again, must be free()d by the caller).
187 * NETSEC_SASL_WRITE - Generate a protocol message to send over the
188 * network. indata/indatasize will contain the
189 * SASL payload data. outdata/outdatasize should
190 * contain the complete protocol message.
191 * NETSEC_SASL_CANCEL - Generate a protocol message that cancels the
192 * SASL protocol exchange; outdata/outdatasize
193 * should contain this message.
195 * The callback should return OK on success, NOTOK on failure. Depending
196 * at the point of the authentication exchange, the callback may be asked
197 * to generate a cancel message.
200 typedef int (*netsec_sasl_callback
)(enum sasl_message_type mtype
,
201 unsigned const char *indata
,
202 unsigned int indatasize
,
203 unsigned char **outdata
,
204 unsigned int *outdatasize
,
205 int snoop
, char **errstr
);
208 * Sets the SASL parameters for this connection. If this function is
209 * not called or is called with NULL for arguments, SASL authentication
210 * will not be attempted for this connection.
212 * The caller must provide a callback to parse the protocol and return
213 * the SASL protocol messages (see above for callback details).
217 * ns_context - Network security context
218 * hostname - Fully qualified hostname of remote host.
219 * service - Service name (set to NULL to disable SASL).
220 * mechlist - A space-separated list of mechanisms supported by the server.
221 * mechanism - The mechanism desired by the user. If NULL, the SASL
222 * library will attempt to negotiate the best mechanism.
223 * callback - SASL protocol callbacks
224 * errstr - Error string.
226 * Returns NOTOK if SASL is not supported.
229 int netsec_set_sasl_params(netsec_context
*ns_context
, const char *hostname
,
230 const char *service
, const char *mechanism
,
231 netsec_sasl_callback callback
, char **errstr
);
234 * Start SASL negotiation. The Netsec library will use the callbacks
235 * supplied in netsec_set_sasl_params() to format and parse the protocol
240 * ns_context - Network security context
241 * mechlist - Space-separated list of supported SASL mechanisms
242 * errstr - An error string to be returned upon error.
244 * Returns OK on success, NOTOK on failure.
247 int netsec_negotiate_sasl(netsec_context
*ns_context
, const char *mechlist
,
251 * Returns the chosen SASL mechanism by the SASL library or netsec.
255 * ns_context - Network security context
257 * Returns a string containing the chosen mech, or NULL if SASL is not
258 * supported or in use.
261 char *netsec_get_sasl_mechanism(netsec_context
*ns_context
);
264 * Set the OAuth service name used to retrieve the OAuth parameters from
265 * user's profile. Just calling this function is not enough to guarantee
266 * that XOAUTH2 authentication will be performed; the appropriate mechanism
267 * name must be passed into netsec_set_sasl_params().
271 * ns_context - Network security context
272 * service - OAuth2 service names.
274 * Returns NOTOK if OAuth2 is not supported.
277 int netsec_set_oauth_service(netsec_context
*ns_context
, const char *service
);
280 * Controls whether or not TLS will be negotiated for this connection.
282 * Note: callers still have to call netsec_tls_negotiate() to start
283 * TLS negotiation at the appropriate point in the protocol.
287 * tls - If nonzero, enable TLS. Otherwise disable TLS
290 * Returns NOTOK if TLS is not supported or was unable to initialize.
293 int netsec_set_tls(netsec_context
*context
, int tls
, char **errstr
);
296 * Start TLS negotiation on this protocol. This connection should have
297 * netsec_set_tls() called on it.
301 * ns_context - Network security context
302 * errstr - Error string upon failure.
304 * Returns OK on success, NOTOK on failure.
307 int netsec_negotiate_tls(netsec_context
*ns_context
, char **errstr
);