1 /* client-callback.c --- Example SASL client, with callback for user info.
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/>.
29 client_authenticate (Gsasl
* ctx
, Gsasl_session
* session
)
31 char buf
[BUFSIZ
] = "";
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
);
52 while (rc
== GSASL_NEEDS_MORE
);
58 printf ("Authentication error (%d): %s\n", rc
, gsasl_strerror (rc
));
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");
71 Gsasl_session
*session
;
72 const char *mech
= "SECURID";
75 /* Create new authentication session. */
76 if ((rc
= gsasl_client_start (ctx
, mech
, &session
)) != GSASL_OK
)
78 printf ("Cannot initialize client (%d): %s\n", rc
, gsasl_strerror (rc
));
83 client_authenticate (ctx
, session
);
86 gsasl_finish (session
);
90 callback (Gsasl
* ctx
, Gsasl_session
* sctx
, Gsasl_property prop
)
92 char buf
[BUFSIZ
] = "";
93 int rc
= GSASL_NO_CALLBACK
;
95 /* Get user info from user. */
97 printf ("Callback invoked, for property %d.\n", prop
);
102 printf ("Enter passcode:\n");
103 fgets (buf
, sizeof (buf
) - 1, stdin
);
104 buf
[strlen (buf
) - 1] = '\0';
106 gsasl_property_set (sctx
, GSASL_PASSCODE
, buf
);
111 printf ("Enter username:\n");
112 fgets (buf
, sizeof (buf
) - 1, stdin
);
113 buf
[strlen (buf
) - 1] = '\0';
115 gsasl_property_set (sctx
, GSASL_AUTHID
, buf
);
120 printf ("Unknown property! Don't worry.\n");
128 main (int argc
, char *argv
[])
133 /* Initialize library. */
134 if ((rc
= gsasl_init (&ctx
)) != GSASL_OK
)
136 printf ("Cannot initialize libgsasl (%d): %s", rc
, gsasl_strerror (rc
));
140 /* Set the callback handler for the library. */
141 gsasl_callback_set (ctx
, callback
);