Add.
[gsasl.git] / examples / client.c
blob6e4d4da4467e5a6286a4e860721d967459fe0702
1 /* client.c --- Example SASL client.
2 * Copyright (C) 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU SASL; if not, write to the Free Software
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include <gsasl.h>
29 static void
30 client_authenticate (Gsasl * ctx, Gsasl_session * session)
32 char buf[BUFSIZ] = "";
33 char *p;
34 int rc;
36 /* This loop mimic a protocol where the client send data first. */
40 /* Generate client output. */
41 rc = gsasl_step64 (session, buf, &p);
43 if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
45 /* If sucessful, print it. */
46 printf ("Output:\n%s\n", p);
47 free (p);
50 if (rc == GSASL_NEEDS_MORE)
52 /* If the client need more data from server, get it here. */
53 printf ("Input base64 encoded data from server:\n");
54 fgets (buf, sizeof (buf) - 1, stdin);
55 if (buf[strlen (buf) - 1] == '\n')
56 buf[strlen (buf) - 1] = '\0';
59 while (rc == GSASL_NEEDS_MORE);
61 printf ("\n");
63 if (rc != GSASL_OK)
65 printf ("Authentication error (%d): %s\n", rc, gsasl_strerror (rc));
66 return;
69 /* The client is done. Here you would typically check if the server
70 let the client in. If not, you could try again. */
72 printf ("If server accepted us, we're done.\n");
75 static void
76 client (Gsasl * ctx)
78 Gsasl_session *session;
79 const char *mech = "PLAIN";
80 int rc;
82 /* Create new authentication session. */
83 if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
85 printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
86 return;
89 /* Set username and password in session handle. This info will be
90 lost when this session is deallocated below. */
91 gsasl_property_set (session, GSASL_AUTHID, "jas");
92 gsasl_property_set (session, GSASL_PASSWORD, "secret");
94 /* Do it. */
95 client_authenticate (ctx, session);
97 /* Cleanup. */
98 gsasl_finish (session);
102 main (int argc, char *argv[])
104 Gsasl *ctx = NULL;
105 int rc;
107 /* Initialize library. */
108 if ((rc = gsasl_init (&ctx)) != GSASL_OK)
110 printf ("Cannot initialize libgsasl (%d): %s", rc, gsasl_strerror (rc));
111 return 1;
114 /* Do it. */
115 client (ctx);
117 /* Cleanup. */
118 gsasl_done (ctx);
120 return 0;