*** empty log message ***
[gsasl.git] / examples / client-callback.c
blobaf15e08a96408eb6fa774bfc42a539b7c37526e8
1 /* client-callback.c --- Example SASL client, with callback for user info.
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 void
70 client (Gsasl * ctx)
72 Gsasl_session *session;
73 const char *mech = "SECURID";
74 int rc;
76 /* Create new authentication session. */
77 if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
79 printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
80 return;
83 /* Do it. */
84 client_authenticate (ctx, session);
86 /* Cleanup. */
87 gsasl_finish (session);
90 static int
91 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
93 char buf[BUFSIZ] = "";
94 int rc = GSASL_NO_CALLBACK;
96 /* Get user info from user. */
98 printf ("Callback invoked, for property %d.\n", prop);
100 switch (prop)
102 case GSASL_PASSCODE:
103 printf ("Enter passcode:\n");
104 fgets (buf, sizeof (buf) - 1, stdin);
105 buf[strlen (buf) - 1] = '\0';
107 gsasl_property_set (sctx, GSASL_PASSCODE, buf);
108 rc = GSASL_OK;
109 break;
111 case GSASL_AUTHID:
112 printf ("Enter username:\n");
113 fgets (buf, sizeof (buf) - 1, stdin);
114 buf[strlen (buf) - 1] = '\0';
116 gsasl_property_set (sctx, GSASL_AUTHID, buf);
117 rc = GSASL_OK;
118 break;
120 default:
121 printf ("Unknown property! Don't worry.\n");
122 break;
125 return rc;
129 main (int argc, char *argv[])
131 Gsasl *ctx = NULL;
132 int rc;
134 /* Initialize library. */
135 if ((rc = gsasl_init (&ctx)) != GSASL_OK)
137 printf ("Cannot initialize libgsasl (%d): %s", rc, gsasl_strerror (rc));
138 return 1;
141 /* Set the callback handler for the library. */
142 gsasl_callback_set (ctx, callback);
144 /* Do it. */
145 client (ctx);
147 /* Cleanup. */
148 gsasl_done (ctx);
150 return 0;