1IMCLIENT(3)                Library Functions Manual                IMCLIENT(3)
2
3
4
5 *
6

NAME

8       imclient  library  -  authenticating  callback  interface  to IMAP/IMSP
9       servers
10

SYNOPSIS

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

DESCRIPTION

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

EXAMPLES

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

BUGS

246       No known bugs.
247
248

SEE ALSO

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

KEYWORDS

255       IMAP, ACAP, IMSP, Kerberos, Authentication
256
257
258
259CMU                              Project Cyrus                     IMCLIENT(3)
Impressum