Bump versions.
[gsasl.git] / examples / client-mech.c
blobeb9104773c3dc280a4aca7b0d7daf1500eb0d172
1 /* client-mech.c --- Example SASL client, with a choice of mechanism to use.
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 server get to send data first. */
39 printf ("Input base64 encoded data from server:\n");
40 fgets (buf, sizeof (buf) - 1, stdin);
41 if (buf[strlen (buf) - 1] == '\n')
42 buf[strlen (buf) - 1] = '\0';
44 rc = gsasl_step64 (session, buf, &p);
46 if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
48 printf ("Output:\n%s\n", p);
49 free (p);
52 while (rc == GSASL_NEEDS_MORE);
54 printf ("\n");
56 if (rc != GSASL_OK)
58 printf ("Authentication error (%d): %s\n", rc, gsasl_strerror (rc));
59 return;
62 /* The client is done. Here you would typically check if the server
63 let the client in. If not, you could try again. */
65 printf ("If server accepted us, we're done.\n");
68 static const char *
69 client_mechanism (Gsasl * ctx)
71 static char mech[GSASL_MAX_MECHANISM_SIZE + 1] = "";
72 char mechlist[BUFSIZ] = "";
73 const char *suggestion;
75 printf ("Enter list of mechanism that server support, separate by SPC:\n");
76 fgets (mechlist, sizeof (mechlist) - 1, stdin);
78 suggestion = gsasl_client_suggest_mechanism (ctx, mechlist);
79 if (suggestion)
80 printf ("Library suggest use of `%s'.\n", suggestion);
82 printf ("Enter mechanism to use:\n");
83 fgets (mech, sizeof (mech) - 1, stdin);
84 mech[strlen (mech) - 1] = '\0';
86 return mech;
89 static void
90 client (Gsasl * ctx)
92 Gsasl_session *session;
93 const char *mech;
94 int rc;
96 /* Find out which mechanism to use. */
97 mech = client_mechanism (ctx);
99 /* Create new authentication session. */
100 if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
102 printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
103 return;
106 /* Set username and password in session handle. This info will be
107 lost when this session is deallocated below. */
108 gsasl_property_set (session, GSASL_AUTHID, "jas");
109 gsasl_property_set (session, GSASL_PASSWORD, "secret");
111 /* Do it. */
112 client_authenticate (ctx, session);
114 /* Cleanup. */
115 gsasl_finish (session);
119 main (int argc, char *argv[])
121 Gsasl *ctx = NULL;
122 int rc;
124 /* Initialize library. */
125 if ((rc = gsasl_init (&ctx)) != GSASL_OK)
127 printf ("Cannot initialize libgsasl (%d): %s", rc, gsasl_strerror (rc));
128 return 1;
131 /* Do it. */
132 client (ctx);
134 /* Cleanup. */
135 gsasl_done (ctx);
137 return 0;