Fix.
[gsasl.git] / src / callbacks.c
blob53805c1de8800c916977648583c6df8d9099c446
1 /* callbacks.c --- Implementation of gsasl callbacks.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 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 Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "internal.h"
23 #include "callbacks.h"
25 #include "iconvme.h"
26 #include "readline.h"
28 static char *
29 locale_to_utf8 (char *str)
31 #if HAVE_LANGINFO_CODESET
32 if (str)
34 char *from = nl_langinfo (CODESET);
35 char *q = iconv_string (str, from, "UTF-8");
36 if (!q)
37 fprintf (stderr, "warning: Could not convert string to UTF-8...\n");
38 else
40 free (str);
41 str = q;
44 #endif
46 return str;
49 static char *
50 readutf8line (const char *prompt)
52 char *p = readline (prompt);
54 return locale_to_utf8 (p);
57 static char *
58 readutf8pass (const char *prompt)
60 char *p = getpass (prompt);
62 return locale_to_utf8 (p);
65 int
66 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
68 int rc = GSASL_NO_CALLBACK;
70 switch (prop)
72 case GSASL_ANONYMOUS_TOKEN:
73 if (args_info.anonymous_token_arg == NULL)
74 args_info.anonymous_token_arg =
75 readutf8line ("Enter anonymous token (e.g., email address): ");
77 gsasl_property_set (sctx, GSASL_ANONYMOUS_TOKEN,
78 args_info.anonymous_token_arg);
80 rc = GSASL_OK;
81 break;
83 case GSASL_PASSWORD:
84 if (args_info.password_arg == NULL)
85 args_info.password_arg = readutf8pass ("Enter password: ");
87 gsasl_property_set (sctx, GSASL_PASSWORD, args_info.password_arg);
89 rc = GSASL_OK;
90 break;
92 case GSASL_PASSCODE:
93 if (args_info.passcode_arg == NULL)
94 args_info.passcode_arg = readutf8pass ("Enter passcode: ");
96 gsasl_property_set (sctx, GSASL_PASSCODE, args_info.passcode_arg);
98 rc = GSASL_OK;
99 break;
101 case GSASL_AUTHID:
102 if (args_info.authentication_id_arg == NULL)
104 #if HAVE_GETPWUID
105 uid_t uid;
106 struct passwd *pw;
108 uid = getuid ();
109 pw = getpwuid (uid);
111 if (pw && pw->pw_name)
113 printf ("Using system username `%s' as "
114 "authentication identity.\n", pw->pw_name);
115 args_info.authentication_id_arg = strdup (pw->pw_name);
117 else
118 #endif
119 args_info.authentication_id_arg =
120 readutf8line ("Enter authentication ID: ");
123 gsasl_property_set (sctx, GSASL_AUTHID,
124 args_info.authentication_id_arg);
125 rc = GSASL_OK;
126 break;
128 case GSASL_AUTHZID:
129 gsasl_property_set (sctx, GSASL_AUTHZID,
130 args_info.authorization_id_arg);
131 rc = GSASL_OK;
132 break;
134 case GSASL_SERVICE:
135 if (args_info.service_arg == NULL)
136 args_info.service_arg =
137 readutf8line ("Enter GSSAPI service name (e.g. \"imap\"): ");
139 gsasl_property_set (sctx, GSASL_SERVICE, args_info.service_arg);
141 rc = GSASL_OK;
142 break;
144 case GSASL_HOSTNAME:
145 if (args_info.hostname_arg == NULL)
146 args_info.hostname_arg = readutf8line ("Enter hostname of server: ");
148 gsasl_property_set (sctx, GSASL_HOSTNAME, args_info.hostname_arg);
150 rc = GSASL_OK;
151 break;
153 case GSASL_REALM:
154 if (args_info.realm_arg == NULL)
155 args_info.realm_arg =
156 readutf8line ("Enter realm of server (optional): ");
158 if (args_info.realm_arg && *args_info.realm_arg)
159 gsasl_property_set (sctx, GSASL_REALM, args_info.realm_arg);
161 rc = GSASL_OK;
162 break;
164 default:
165 printf ("Mechanism requested unsupported property `%d'.\n", prop);
166 break;
169 return rc;