Add.
[gsasl.git] / src / callbacks.c
blobc86bf74e3dd5b95fc656c3ec81d87a7124e05755
1 /* callbacks.c --- Implementation of gsasl callbacks.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 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 "internal.h"
22 #include "callbacks.h"
24 #include "striconv.h"
25 #include "readline.h"
27 static char *
28 locale_to_utf8 (char *str)
30 #if HAVE_LANGINFO_CODESET
31 if (str)
33 char *from = nl_langinfo (CODESET);
34 char *q = str_iconv (str, from, "UTF-8");
35 if (!q)
36 fprintf (stderr, "warning: Could not convert string to UTF-8...\n");
37 else
39 free (str);
40 str = q;
43 #endif
45 return str;
48 static char *
49 readutf8line (const char *prompt)
51 char *p = readline (prompt);
53 return locale_to_utf8 (p);
56 static char *
57 readutf8pass (const char *prompt)
59 char *p = getpass (prompt);
61 return locale_to_utf8 (p);
64 int
65 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
67 int rc = GSASL_NO_CALLBACK;
69 switch (prop)
71 case GSASL_ANONYMOUS_TOKEN:
72 if (args_info.anonymous_token_arg == NULL)
73 args_info.anonymous_token_arg =
74 readutf8line ("Enter anonymous token (e.g., email address): ");
76 gsasl_property_set (sctx, GSASL_ANONYMOUS_TOKEN,
77 args_info.anonymous_token_arg);
79 rc = GSASL_OK;
80 break;
82 case GSASL_PASSWORD:
83 if (args_info.password_arg == NULL)
84 args_info.password_arg = readutf8pass ("Enter password: ");
86 gsasl_property_set (sctx, GSASL_PASSWORD, args_info.password_arg);
88 rc = GSASL_OK;
89 break;
91 case GSASL_PASSCODE:
92 if (args_info.passcode_arg == NULL)
93 args_info.passcode_arg = readutf8pass ("Enter passcode: ");
95 gsasl_property_set (sctx, GSASL_PASSCODE, args_info.passcode_arg);
97 rc = GSASL_OK;
98 break;
100 case GSASL_AUTHID:
101 if (args_info.authentication_id_arg == NULL)
103 #if HAVE_GETPWUID
104 uid_t uid;
105 struct passwd *pw;
107 uid = getuid ();
108 pw = getpwuid (uid);
110 if (pw && pw->pw_name)
112 printf ("Using system username `%s' as "
113 "authentication identity.\n", pw->pw_name);
114 args_info.authentication_id_arg = strdup (pw->pw_name);
116 else
117 #endif
118 args_info.authentication_id_arg =
119 readutf8line ("Enter authentication ID: ");
122 gsasl_property_set (sctx, GSASL_AUTHID,
123 args_info.authentication_id_arg);
124 rc = GSASL_OK;
125 break;
127 case GSASL_AUTHZID:
128 gsasl_property_set (sctx, GSASL_AUTHZID,
129 args_info.authorization_id_arg);
130 rc = GSASL_OK;
131 break;
133 case GSASL_SERVICE:
134 if (args_info.service_arg == NULL)
135 args_info.service_arg =
136 readutf8line ("Enter GSSAPI service name (e.g. \"imap\"): ");
138 gsasl_property_set (sctx, GSASL_SERVICE, args_info.service_arg);
140 rc = GSASL_OK;
141 break;
143 case GSASL_HOSTNAME:
144 if (args_info.hostname_arg == NULL)
145 args_info.hostname_arg = readutf8line ("Enter hostname of server: ");
147 gsasl_property_set (sctx, GSASL_HOSTNAME, args_info.hostname_arg);
149 rc = GSASL_OK;
150 break;
152 case GSASL_REALM:
153 if (args_info.realm_arg == NULL)
154 args_info.realm_arg =
155 readutf8line ("Enter realm of server (optional): ");
157 if (args_info.realm_arg && *args_info.realm_arg)
158 gsasl_property_set (sctx, GSASL_REALM, args_info.realm_arg);
160 rc = GSASL_OK;
161 break;
163 case GSASL_VALIDATE_GSSAPI:
165 char *str;
166 printf ("Authzid: %s\nDisplay Name: %s\n",
167 gsasl_property_fast (sctx, GSASL_AUTHZID),
168 gsasl_property_fast (sctx, GSASL_GSSAPI_DISPLAY_NAME));
169 str = readutf8line ("Validate GSS-API user? (y/n) ");
170 if (strcmp (str, "y") == 0 || strcmp (str, "Y") == 0)
171 rc = GSASL_OK;
172 else
173 rc = GSASL_AUTHENTICATION_ERROR;
174 free (str);
176 break;
178 default:
179 printf ("Mechanism requested unsupported property `%d'.\n", prop);
180 break;
183 return rc;