]> diplodocus.org Git - nmh/blob - h/netsec.h
db65db16731006200f25afa75d9b60dba95aa5ab
[nmh] / h / netsec.h
1 /*
2 * Network security library routines for nmh.
3 *
4 * These are a common set of routines to handle network security for
5 * things like SASL and OpenSSL.
6 */
7
8 struct _netsec_context;
9 typedef struct _netsec_context netsec_context;
10
11 /*
12 * Create a network security context. Returns the allocated network
13 * security context. Cannot fail.
14 */
15
16 netsec_context *netsec_init(void);
17
18 /*
19 * Shuts down the security context for a connection and frees all
20 * associated resources.
21 *
22 * Arguments:
23 *
24 * ns_context - Network security context
25 */
26
27 void netsec_shutdown(netsec_context *ns_context);
28
29 /*
30 * Sets the file descriptor for this connection. This will be used by
31 * the underlying communication.
32 *
33 * Arguments:
34 *
35 * ns_context - Network security context
36 * fd - File descriptor of network connection.
37 */
38
39 void netset_set_fd(netsec_context *ns_context, int fd);
40
41 /*
42 * Set the userid used to authenticate to this connection.
43 *
44 * Arguments:
45 *
46 * ns_context - Network security context
47 * userid - Userid to be used for authentication. Cannot be NULL.
48 */
49
50 void netsec_set_userid(netsec_context *ns_context, const char *userid);
51
52 /*
53 * Sets "snoop" status; if snoop is set to a nonzero value, network traffic
54 * will be logged on standard error.
55 *
56 * Arguments:
57 *
58 * ns_context - Network security context
59 * snoop - Integer value; set to nonzero to enable traffic logging
60 */
61
62 void netsec_set_snoop(netsec_context *ns_context, int snoop);
63
64 /*
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.
70 *
71 * Arguments:
72 *
73 * ns_context - Network security context
74 * errstr - Error string
75 *
76 * Returns pointer to string, or NULL on error.
77 */
78
79 char *netsec_readline(netsec_context *ns_context, char **errstr);
80
81 /*
82 * Read bytes from the network.
83 *
84 * Arguments:
85 *
86 * ns_context - Network security context
87 * buffer - Read buffer
88 * size - Buffer size
89 * errstr - Error size
90 *
91 * Returns number of bytes read, or -1 on error.
92 */
93
94 ssize_t netsec_read(netsec_context *ns_context, void *buffer, size_t size,
95 char **errstr);
96
97 /*
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.
101 *
102 * Arguments:
103 *
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
108 *
109 * Returns OK on success, NOTOK otherwise.
110 */
111
112 int netsec_write(netsec_context *ns_context, const void *buffer, size_t size,
113 char **errstr);
114
115 /*
116 * Write bytes using printf formatting
117 *
118 * Arguments:
119 *
120 * ns_context - Network security context
121 * errstr - Error string
122 * format - Format string
123 * ... - Arguments for format string
124 *
125 * Returns OK on success, NOTOK on error.
126 */
127
128 int netsec_printf(netsec_context *ns_context, char **errstr,
129 const char *format, ...);
130
131 /*
132 * Flush any buffered bytes to the network.
133 *
134 * Arguments:
135 *
136 * ns_context - Network security context
137 * errstr - Error string
138 *
139 * Returns OK on success, NOTOK on error.
140 */
141
142 int netsec_flush(netsec_context *ns_context, char **errstr);
143
144 /*
145 * Enumerated types for the type of message we are sending/receiving.
146 */
147
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 */
153 };
154
155 /*
156 * The SASL callback; this is designed to parse a protocol-specific
157 * message and return the SASL protocol message back.
158 *
159 * The meaning of the arguments to the callback depend on the mtype
160 * arguments. See below for more detail.
161 *
162 * Arguments:
163 *
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).
171 *
172 * Parameter interpretation based on mtype value:
173 *
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
180 * by the caller.
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.
194 *
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.
198 */
199
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);
206
207 /*
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.
211 *
212 * The caller must provide a callback to parse the protocol and return
213 * the SASL protocol messages (see above for callback details).
214 *
215 * Arguments:
216 *
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.
225 *
226 * Returns NOTOK if SASL is not supported.
227 */
228
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);
232
233 /*
234 * Start SASL negotiation. The Netsec library will use the callbacks
235 * supplied in netsec_set_sasl_params() to format and parse the protocol
236 * messages.
237 *
238 * Arguments:
239 *
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.
243 *
244 * Returns OK on success, NOTOK on failure.
245 */
246
247 int netsec_negotiate_sasl(netsec_context *ns_context, const char *mechlist,
248 char **errstr);
249
250 /*
251 * Returns the chosen SASL mechanism by the SASL library or netsec.
252 *
253 * Arguments:
254 *
255 * ns_context - Network security context
256 *
257 * Returns a string containing the chosen mech, or NULL if SASL is not
258 * supported or in use.
259 */
260
261 char *netsec_get_sasl_mechanism(netsec_context *ns_context);
262
263 /*
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().
268 *
269 * Arguments:
270 *
271 * ns_context - Network security context
272 * service - OAuth2 service names.
273 *
274 * Returns NOTOK if OAuth2 is not supported.
275 */
276
277 int netsec_set_oauth_service(netsec_context *ns_context, const char *service);
278
279 /*
280 * Controls whether or not TLS will be negotiated for this connection.
281 *
282 * Note: callers still have to call netsec_tls_negotiate() to start
283 * TLS negotiation at the appropriate point in the protocol.
284 *
285 * Arguments
286 *
287 * tls - If nonzero, enable TLS. Otherwise disable TLS
288 * negotiation.
289 *
290 * Returns NOTOK if TLS is not supported or was unable to initialize.
291 */
292
293 int netsec_set_tls(netsec_context *context, int tls, char **errstr);
294
295 /*
296 * Start TLS negotiation on this protocol. This connection should have
297 * netsec_set_tls() called on it.
298 *
299 * Arguments:
300 *
301 * ns_context - Network security context
302 * errstr - Error string upon failure.
303 *
304 * Returns OK on success, NOTOK on failure.
305 */
306
307 int netsec_negotiate_tls(netsec_context *ns_context, char **errstr);