*** empty log message ***
[gsasl.git] / examples / client-mech.c
blob6f7398456c0626b65ae1965b03d76e947751b86e
1 /* client-mech.c --- Example SASL client, with a choice of mechanism to use.
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 server get to send data first. */
40 printf ("Input base64 encoded data from server:\n");
41 fgets (buf, sizeof (buf) - 1, stdin);
42 if (buf[strlen (buf) - 1] == '\n')
43 buf[strlen (buf) - 1] = '\0';
45 rc = gsasl_step64 (session, buf, &p);
47 if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
49 printf ("Output:\n%s\n", p);
50 free (p);
53 while (rc == GSASL_NEEDS_MORE);
55 printf ("\n");
57 if (rc != GSASL_OK)
59 printf ("Authentication error (%d): %s\n", rc, gsasl_strerror (rc));
60 return;
63 /* The client is done. Here you would typically check if the server
64 let the client in. If not, you could try again. */
66 printf ("If server accepted us, we're done.\n");
69 static const char *
70 client_mechanism (Gsasl * ctx)
72 static char mech[GSASL_MAX_MECHANISM_SIZE + 1] = "";
73 char mechlist[BUFSIZ] = "";
74 const char *suggestion;
76 printf ("Enter list of mechanism that server support, separate by SPC:\n");
77 fgets (mechlist, sizeof (mechlist) - 1, stdin);
79 suggestion = gsasl_client_suggest_mechanism (ctx, mechlist);
80 if (suggestion)
81 printf ("Library suggest use of `%s'.\n", suggestion);
83 printf ("Enter mechanism to use:\n");
84 fgets (mech, sizeof (mech) - 1, stdin);
85 mech[strlen (mech) - 1] = '\0';
87 return mech;
90 static void
91 client (Gsasl * ctx)
93 Gsasl_session *session;
94 const char *mech;
95 int rc;
97 /* Find out which mechanism to use. */
98 mech = client_mechanism (ctx);
100 /* Create new authentication session. */
101 if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
103 printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
104 return;
107 /* Set username and password in session handle. This info will be
108 lost when this session is deallocated below. */
109 gsasl_property_set (session, GSASL_AUTHID, "jas");
110 gsasl_property_set (session, GSASL_PASSWORD, "secret");
112 /* Do it. */
113 client_authenticate (ctx, session);
115 /* Cleanup. */
116 gsasl_finish (session);
120 main (int argc, char *argv[])
122 Gsasl *ctx = NULL;
123 int rc;
125 /* Initialize library. */
126 if ((rc = gsasl_init (&ctx)) != GSASL_OK)
128 printf ("Cannot initialize libgsasl (%d): %s", rc, gsasl_strerror (rc));
129 return 1;
132 /* Do it. */
133 client (ctx);
135 /* Cleanup. */
136 gsasl_done (ctx);
138 return 0;