Bump versions.
[gsasl.git] / examples / client.c
blob24bea1776e77383e5625fc2302b825c29441f485
1 /* client.c --- Example SASL client.
2 * Copyright (C) 2004, 2005, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program 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 3 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <gsasl.h>
28 static void
29 client_authenticate (Gsasl * ctx, Gsasl_session * session)
31 char buf[BUFSIZ] = "";
32 char *p;
33 int rc;
35 /* This loop mimic a protocol where the client send data first. */
39 /* Generate client output. */
40 rc = gsasl_step64 (session, buf, &p);
42 if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
44 /* If sucessful, print it. */
45 printf ("Output:\n%s\n", p);
46 free (p);
49 if (rc == GSASL_NEEDS_MORE)
51 /* If the client need more data from server, get it here. */
52 printf ("Input base64 encoded data from server:\n");
53 fgets (buf, sizeof (buf) - 1, stdin);
54 if (buf[strlen (buf) - 1] == '\n')
55 buf[strlen (buf) - 1] = '\0';
58 while (rc == GSASL_NEEDS_MORE);
60 printf ("\n");
62 if (rc != GSASL_OK)
64 printf ("Authentication error (%d): %s\n", rc, gsasl_strerror (rc));
65 return;
68 /* The client is done. Here you would typically check if the server
69 let the client in. If not, you could try again. */
71 printf ("If server accepted us, we're done.\n");
74 static void
75 client (Gsasl * ctx)
77 Gsasl_session *session;
78 const char *mech = "PLAIN";
79 int rc;
81 /* Create new authentication session. */
82 if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
84 printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
85 return;
88 /* Set username and password in session handle. This info will be
89 lost when this session is deallocated below. */
90 gsasl_property_set (session, GSASL_AUTHID, "jas");
91 gsasl_property_set (session, GSASL_PASSWORD, "secret");
93 /* Do it. */
94 client_authenticate (ctx, session);
96 /* Cleanup. */
97 gsasl_finish (session);
101 main (int argc, char *argv[])
103 Gsasl *ctx = NULL;
104 int rc;
106 /* Initialize library. */
107 if ((rc = gsasl_init (&ctx)) != GSASL_OK)
109 printf ("Cannot initialize libgsasl (%d): %s", rc, gsasl_strerror (rc));
110 return 1;
113 /* Do it. */
114 client (ctx);
116 /* Cleanup. */
117 gsasl_done (ctx);
119 return 0;