1 /* netsec.h -- network-security library routines.
3 * These are a common set of routines to handle network security for
4 * things like SASL and OpenSSL.
7 struct _netsec_context
;
8 typedef struct _netsec_context netsec_context
;
11 * Create a network security context. Returns the allocated network
12 * security context. Cannot fail.
15 netsec_context
*netsec_init(void);
18 * Shuts down the security context for a connection and frees all
19 * associated resources.
23 * ns_context - Network security context
24 * closeflag - If set to 1, close the socket descriptor as well.
27 void netsec_shutdown(netsec_context
*ns_context
, int closeflag
);
30 * Sets the file descriptor for this connection. This will be used by
31 * the underlying communication.
35 * ns_context - Network security context
36 * readfd - Read file descriptor of remote connection.
37 * writefd - Write file descriptor of remote connection
40 void netsec_set_fd(netsec_context
*ns_context
, int readfd
, int writefd
);
43 * Set the userid used to authenticate to this connection.
47 * ns_context - Network security context
48 * userid - Userid to be used for authentication. Cannot be NULL.
51 void netsec_set_userid(netsec_context
*ns_context
, const char *userid
);
54 * Set the hostname of the server we're connecting to. This is used
55 * by the Cyrus-SASL library and by the TLS code. This must be called
56 * before netsec_negotiate_tls() or netsec_set_sasl_params().
60 * ns_context - Network security context
61 * hostname - FQDN of remote host. Cannot be NULL.
64 void netsec_set_hostname(netsec_context
*ns_context
, const char *hostname
);
67 * Returns "snoop" status on current connection.
71 * ns_context - Network security context
73 * Returns "1" if snoop is enabled, 0 if it is not.
76 int netsec_get_snoop(netsec_context
*ns_context
);
79 * Sets "snoop" status; if snoop is set to a nonzero value, network traffic
80 * will be logged on standard error.
84 * ns_context - Network security context
85 * snoop - Integer value; set to nonzero to enable traffic logging
88 void netsec_set_snoop(netsec_context
*ns_context
, int snoop
);
91 * A callback designed to handle the snoop output; it can be used by
92 * a protocol to massage the data in a more user-friendly way.
96 * ns_context - Network security context
97 * string - String to output
98 * len - Length of string
99 * context - "Extra" context information to be used by callback.
102 typedef void (netsec_snoop_callback
)(netsec_context
*ns_context
,
103 const char *string
, size_t len
,
107 * Set the snoop callback function; will be used to handle protocol-specific
108 * messages. Set to NULL to disable.
112 * ns_context - Network security context
113 * callback - Snoop callback
114 * context - Extra context information to be passed to callback.
117 void netsec_set_snoop_callback(netsec_context
*ns_context
,
118 netsec_snoop_callback
*callback
, void *context
);
121 * A sample callback protocols can utilize; decode base64 tokens in the
122 * output. The context is a pointer to an int which contains an offset
123 * into the data to start decoding.
126 extern netsec_snoop_callback netsec_b64_snoop_decoder
;
129 * Set the read timeout for this connection.
133 * ns_context - Network security context
134 * timeout - Read timeout, in seconds.
137 void netsec_set_timeout(netsec_context
*ns_context
, int timeout
);
140 * Read a "line" from the network. This reads one CR/LF terminated line.
141 * Returns a pointer to a NUL-terminated string. This memory is valid
142 * until the next call to any read function. Will return an error if
143 * the line does not terminate with a CR/LF.
147 * ns_context - Network security context
148 * length - Returned length of string
149 * errstr - Error string
151 * Returns pointer to string, or NULL on error.
154 char *netsec_readline(netsec_context
*ns_context
, size_t *length
,
158 * Read bytes from the network.
162 * ns_context - Network security context
163 * buffer - Read buffer
165 * errstr - Error size
167 * Returns number of bytes read, or -1 on error.
170 ssize_t
netsec_read(netsec_context
*ns_context
, void *buffer
, size_t size
,
174 * Write data to the network; if encryption is being performed, we will
175 * do it. Data may be buffered; use netsec_flush() to flush any outstanding
176 * data to the network.
180 * ns_context - Network security context
181 * buffer - Output buffer to write to network
182 * size - Size of data to write to network
183 * errstr - Error string
185 * Returns OK on success, NOTOK otherwise.
188 int netsec_write(netsec_context
*ns_context
, const void *buffer
, size_t size
,
192 * Write bytes using printf formatting
196 * ns_context - Network security context
197 * errstr - Error string
198 * format - Format string
199 * ... - Arguments for format string
201 * Returns OK on success, NOTOK on error.
204 int netsec_printf(netsec_context
*ns_context
, char **errstr
,
205 const char *format
, ...);
208 * Write bytes using a va_list argument.
212 * ns_context - Network security context
213 * errstr - Error string
214 * format - Format string
217 * Returns OK on success, NOTOK on error.
220 int netsec_vprintf(netsec_context
*ns_context
, char **errstr
,
221 const char *format
, va_list ap
);
224 * Flush any buffered bytes to the network.
228 * ns_context - Network security context
229 * errstr - Error string
231 * Returns OK on success, NOTOK on error.
234 int netsec_flush(netsec_context
*ns_context
, char **errstr
);
237 * Enumerated types for the type of message we are sending/receiving.
240 enum sasl_message_type
{
241 NETSEC_SASL_START
, /* Start of SASL authentication */
242 NETSEC_SASL_READ
, /* Reading a message */
243 NETSEC_SASL_WRITE
, /* Writing a message */
244 NETSEC_SASL_FINISH
, /* Complete SASL exchange */
245 NETSEC_SASL_CANCEL
/* Cancel a SASL exchange */
249 * The SASL callback; this is designed to parse a protocol-specific
250 * message and return the SASL protocol message back.
252 * The meaning of the arguments to the callback depend on the mtype
253 * arguments. See below for more detail.
257 * mtype - The type of message we are processing (read/write/cancel).
258 * indata - A pointer to any input data.
259 * indatasize - The size of the input data in bytes
260 * outdata - Output data (freed by caller)
261 * outdatasize - Size of output data
262 * errstr - An error string to be returned (freed by caller).
264 * As a general note, plugins should perform their own I/O. Buffers returned
265 * by NETSEC_SASL_READ should be allocated by the plugins and will be freed
266 * by the netsec package. Error messages returned should be created by
269 * Parameter interpretation based on mtype value:
271 * NETSEC_SASL_START - Create a protocol message that starts SASL
272 * authentication. If an initial response is
273 * supported, indata and indatasize will contain it.
274 * Otherwise they will be set to NULL and 0.
275 * NETSEC_SASL_READ - Parse and decode a protocol message and extract
276 * out the SASL payload data. indata will be set
277 * to NULL; the callback must read in the necessary
278 * data using the appropriate netsec function.
279 * outdata/outdatasize should contain the decoded
280 * SASL message (again, must be free()d by the caller).
281 * NETSEC_SASL_WRITE - Generate a protocol message to send over the
282 * network. indata/indatasize will contain the
284 * NETSEC_SASL_FINISH - Process the final SASL message exchange; at
285 * this point SASL exchange should have completed
286 * and we should get a message back from the server
287 * telling us whether or not authentication is
288 * successful. All buffer parameters are NULL.
289 * NETSEC_SASL_CANCEL - Generate a protocol message that cancels the
290 * SASL protocol exchange; outdata/outdatasize
291 * should contain this message.
293 * The callback should return OK on success, NOTOK on failure. Depending
294 * at the point of the authentication exchange, the callback may be asked
295 * to generate a cancel message.
298 typedef int (*netsec_sasl_callback
)(enum sasl_message_type mtype
,
299 unsigned const char *indata
,
300 unsigned int indatasize
,
301 unsigned char **outdata
,
302 unsigned int *outdatasize
, char **errstr
);
305 * Sets the SASL parameters for this connection. If this function is
306 * not called or is called with NULL for arguments, SASL authentication
307 * will not be attempted for this connection.
309 * The caller must provide a callback to parse the protocol and return
310 * the SASL protocol messages (see above for callback details).
314 * ns_context - Network security context
315 * service - Service name (set to NULL to disable SASL).
316 * mechanism - The mechanism desired by the user. If NULL, the SASL
317 * library will attempt to negotiate the best mechanism.
318 * callback - SASL protocol callbacks
319 * errstr - Error string.
321 * Returns NOTOK if SASL is not supported.
324 int netsec_set_sasl_params(netsec_context
*ns_context
, const char *service
,
325 const char *mechanism
,
326 netsec_sasl_callback callback
, char **errstr
);
329 * Start SASL negotiation. The Netsec library will use the callbacks
330 * supplied in netsec_set_sasl_params() to format and parse the protocol
335 * ns_context - Network security context
336 * mechlist - Space-separated list of supported SASL mechanisms
337 * errstr - An error string to be returned upon error.
339 * Returns OK on success, NOTOK on failure.
342 int netsec_negotiate_sasl(netsec_context
*ns_context
, const char *mechlist
,
346 * Returns the chosen SASL mechanism by the SASL library or netsec.
350 * ns_context - Network security context
352 * Returns a string containing the chosen mech, or NULL if SASL is not
353 * supported or in use.
356 char *netsec_get_sasl_mechanism(netsec_context
*ns_context
);
359 * Set the OAuth service name used to retrieve the OAuth parameters from
360 * user's profile. Just calling this function is not enough to guarantee
361 * that XOAUTH2 authentication will be performed; the appropriate mechanism
362 * name must be passed into netsec_set_sasl_params().
366 * ns_context - Network security context
367 * service - OAuth2 service names.
369 * Returns NOTOK if OAuth2 is not supported.
372 int netsec_set_oauth_service(netsec_context
*ns_context
, const char *service
);
375 * Controls whether or not TLS will be negotiated for this connection.
377 * Note: callers still have to call netsec_tls_negotiate() to start
378 * TLS negotiation at the appropriate point in the protocol. The
379 * remote hostname (controlled by netsec_set_hostname()) should have
380 * already been set before this function is called unless certificate
381 * verification is disabled.
385 * tls - If nonzero, enable TLS. Otherwise disable TLS
387 * noverify - If nonzero, disable server certificate and hostname
390 * Returns NOTOK if TLS is not supported or was unable to initialize.
393 int netsec_set_tls(netsec_context
*context
, int tls
, int noverify
,
397 * Start TLS negotiation on this protocol. This connection should have
398 * netsec_set_tls() called on it.
402 * ns_context - Network security context
403 * errstr - Error string upon failure.
405 * Returns OK on success, NOTOK on failure.
408 int netsec_negotiate_tls(netsec_context
*ns_context
, char **errstr
);
411 * Allocate and format an error string; should be used by plugins
416 * errstr - Error string to be returned
417 * format - printf(3) format string
418 * ... - Arguments to printf(3)
422 void netsec_err(char **errstr
, const char *format
, ...);