1IMCLIENT(3)                Library Functions Manual                IMCLIENT(3)
2
3
4

NAME

6       imclient  library  -  authenticating  callback  interface  to IMAP/IMSP
7       servers
8

SYNOPSIS

10       #include <cyrus/imclient.h>
11
12       int imclient_connect(struct imclient **imclient, const char *host,
13       const char *port);
14
15       void imclient_close (struct imclient *imclient);
16       void imclient_setflags(struct imclient *imclient, intflags);
17       void imclient_clearflags (struct imclient *imclient, intflags);
18       char* imclient_servername (struct imclient *imclient);
19       void imclient_addcallback (struct imclient * imclient ,...);
20       void imclient_send (struct imclient *imclient, void (*finishproc)(),
21       void *finishrock, const char *fmt, ...);
22       void imclient_getselectinfo (struct imclient *imclient, int *fd, int *
23       wanttowrite);
24       void imclient_processoneevent (struct imclient *imclient);
25       int imclient_authenticate (struct imclient *imclient, struct
26       sasl_client **availmech, const char *service, const char *user, int
27       protallowed);
28       int imclient_havetls ();
29       int imclient_starttls (struct imclient *imclient, char *cert_file, char
30       *key_file, char *CAfile, char *CApath);
31
32

DESCRIPTION

34       The imclient library functions are distributed with Cyrus IMAP and
35       IMSP.  These functions are used for building IMAP/IMSP client software.
36       These functions handle Kerberos authentication and can set callbacks
37       based on the keyword in untagged replies or based on the command tag at
38       the end of command replies.
39
40       Users must link with the -lcyrus switch, and must supply a function
41       called fatal to be called in case of any error within libcyrus.a.
42
43       All of the imclient functions begin with the prefix imclient and takes
44       an  argument of type struct imclient * as the first argument which is
45       initialized by imclient_connect and freed by imclient_close.
46
47       See below for a description of each function.
48
49
50       imclient_connect()
51            Connects the client server to the host. If successful, it returns
52            0 and sets the imclient argument to a pointer to an imclient
53            struct. The imclient struct represents the current connection,
54            flags, and  callbacks. On failure, the current errno is returned
55            if a system call failed,  -1 is returned if the host name was not
56            found, and  -2 is returned if the service name was not found.
57
58       imclient_close()
59            Closes and frees the imclient connection.
60
61        imclient_setflags()
62            Sets the flags specified by the flags argument on the imclient
63            connection. Currently the only  flag allowed is IMCLIENT_CONN_NON‐
64            SYNCLITERAL (this flag indicates that the server supports non-syn‐
65            chronizing literals described by the LITERAL+ extension).
66
67       imclient_clearflags()
68            Clears the flags specified by the flags argument on the imclient
69            connection.
70
71       imclient_servername()
72            Returns a  char * pointer to the name of the server connected to
73            by imclient.
74
75       imclient_addcallback()
76            Adds an untagged data callback to the imclient connection. The
77            function imclient_addcallback takes callbacks of the type
78            imclient_proc_t which is defined to be:
79               typedef void imclient_proc_t (struct imclient *imclient, void
80               *rock, struct imclient_reply *reply);
81            and struct imclient_reply * is defined to be:
82               struct imclient_reply {
83                        char *keyword;
84                        long msgno;
85                        char *text;
86               };
87
88            After the first argument imclient, there can be zero or more
89            instances of the set of keyword, flags, proc, and rock, each
90            adding or changing a single callback.  Each instance  adds or
91            changes the callback for keyword.  The argument, flags, specifies
92            information about the parsing of the untagged data.  proc and rock
93            specify the callback function and rock to invoke when the untagged
94            data is received.  proc may be a null pointer, in which case no
95            function is invoked.  The callback function may not call the func‐
96            tions imclient_close(), imclient_send(), imclient_eof(),
97            imclient_processoneevent(), or imclient_authenticate() on the con‐
98            nection. The callback function may over write  the text of
99            untagged data.
100
101       imclient_send()
102            Sends a new command to the imclient connection.  finishproc and
103            finnishrock are the function and rock called when the  command
104            completes.  functionproc may be a null pointer, in which case no
105            callback is made. The call back function may not call the func‐
106            tions imclient_close(), imclient_send(), imclient_eof(),
107            imclient_processoneevent(), or imclient_authenticate() on the con‐
108            nection.  The argument, fmt , is a print like specification of the
109            command. It must not include the tag as the tag is automatically
110            added by imclient_send().  The defined %-sequences are:
111               %% for %
112               %a for an IMAP atom
113               %s for an astring (which will be quoted or literalized as
114               needed)
115               %d for a decimal
116               %u for an unsigned  decimal
117               %v for #astring (argument is a null-terminated array of char *
118               which are written as space separated astrings)
119
120       imclient_getselectinfo()
121            Gets the information for calling select(2).  fd is filled in with
122            the file descriptor to select(2) for read.  wanttowrite is filled
123            in with a nonzero value if select should be used for write as
124            well.
125
126       imclient_processoneevent()
127            Processes one input or output event on the imclient connection.
128
129       imclient_authenticate()
130            Authenticates the imclient connection using one of the mechanisms
131            in availmech.  The argument, user, if not NULL, specifies the user
132            to authenticate as. If the user is NULL, the current user is used.
133            The argument protallowed is a bitmask of permissible protection
134            mechanisms.
135            On success, 0 is returned.  On failure (i.e., "BAD" keyboard, or
136            no authentication mechanisms worked), 1 is returned. On extreme
137            failure (premature "OK"), 2 is returned.
138
139       imclient_havetls()
140            Returns a Boolean indicating whether the imclient library was com‐
141            piled with TLS (SSL) support.  If so, imclient_starttls() may be
142            used to secure the IMAP connection.
143
144       imclient_starttls()
145            Issues a STARTTLS command on an existing IMAP connection and nego‐
146            tiates the secure link.  The cert_file and key_file arguments
147            specify the client certificate and secret key to use to authenti‐
148            cate ourselves to the server.  If client authentication is not
149            needed, set both of these arguments to NULL.
150
151            The CAfile and CApath arguments specify a file or directory,
152            respectively, of CA certificates for validating server certifi‐
153            cates.  (See SSL_CTX_load_verify_locations(3) for details.)  If
154            both of these are NULL, the client will be unable to validate the
155            server's certificate, in which case the connection may succeed but
156            a warning will be printed to stdout.
157
158

EXAMPLES

160       The following code is a possible skeletion of imclient that relies on
161       Kerberos to do authentication.  This code preforms an IMAP CAPABILITY
162       request and prints out the result.
163
164       struct sasl_client;
165       #include <cyrus/xmalloc.h> /* example uses xstrdup */
166       #include <cyrus/sasl.h>
167       #include <cyrus/imclient.h>
168       #include <stdio.h>
169
170       extern struct sasl_client krb_sasl_client;
171
172       struct sasl_client *login_sasl_client[] = {
173           &krb_sasl_client,
174           NULL
175       };
176       struct imclient *imclient;
177       char server[] = "cyrus.andrew.cmu.edu" ;
178       char port[] = "imap";
179
180       void fatal(char* message, int rc) {
181           fprintf(stderr, "fatal error: %s\n", message);
182           exit(rc);
183       }
184
185       static void callback_capability(struct imclient *imclient,
186                           void *rock,
187                           struct imclient_reply *reply) {
188           if (reply->text != NULL) {
189            *((char**)rock) = xstrdup( reply->text );
190           }
191       }
192
193       static void end_command (struct imclient *connection, void*
194                      rock,  struct imclient_reply *inmsg) {
195           (*(int*)rock)--;
196       }
197
198       main() {
199           char* capability_string;
200           int nc;
201
202           if (imclient_connect(&imclient, server, port)) {
203            fprintf(stderr,
204                 "error: Couldn't connect to %s %s\n",
205                 server, port);
206            exit(1);
207           }
208
209           if (imclient_authenticate(imclient, login_sasl_client, "imap"
210                            /* service */,
211                            NULL /* user */, SASL_PROT_ANY)) {
212            exit(1);
213           }
214
215           imclient_addcallback(imclient, "CAPABILITY",
216                       CALLBACK_NOLITERAL,
217                       callback_capability,
218                       &capability_string,
219                       NULL);
220
221           nc = 1;
222
223           imclient_send(imclient, end_command,
224                   (void*) &nc, "CAPABILITY");
225
226           while(nc > 0) {
227            imclient_processoneevent(imclient);
228           }
229
230           if (strstr("LITERAL+", capability_string)) {
231            imclient_setflags(imclient, IMCLIENT_CONN_NONSYNCLITERAL);
232           }
233
234           imclient_send(imclient, NULL, NULL, "LOGOUT");
235           imclient_close(imclient);
236
237           printf("capability text is: %s\n", capability_string);
238
239           free(capability_string);
240       }
241
242

BUGS

244       No known bugs.
245
246

SEE ALSO

248       cyradm, imapd, imspd, RFC2033 (IMAP LITERAL+ extension), RFC2060
249       (IMAP4rev1 specification), and select(2)
250
251

KEYWORDS

253       IMAP, ACAP, IMSP, Kerberos, Authentication
254
255
257       Copyright 1997-2002, Carnegie Mellon University.  All Rights Reserved.
258
259       See the source distribution for copying information.
260
261
262
263CMU                              Project Cyrus                     IMCLIENT(3)
Impressum