2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* This file provides the easiest, turn-key interface to the authication
13 #include <afsconfig.h>
14 #include <afs/param.h>
19 #include <hcrypto/des.h>
20 #include <hcrypto/ui.h>
22 #include <afs/com_err.h>
23 #include <afs/cellconfig.h>
25 #include <afs/ptint.h>
26 #include <afs/pterror.h>
27 #include <afs/ptuser.h>
28 #include <afs/ptserver.h>
29 #include <afs/afsutil.h>
32 #include <afs/sys_prototypes.h>
36 #include <rx/rx_globals.h>
37 #include <rx/rxkad.h> /* max ticket lifetime */
45 GetTickets(char *name, char *instance, char *realm,
46 struct ktc_encryptionKey * key, Date lifetime,
47 afs_int32 * pwexpires, afs_int32 flags)
51 code = ka_GetAuthToken(name, instance, realm, key, lifetime, pwexpires);
52 memset(key, 0, sizeof(*key));
55 code = ka_GetAFSTicket(name, instance, realm, lifetime, flags);
60 * Requires that you already possess a TGT.
63 ka_GetAFSTicket(char *name, char *instance, char *realm, Date lifetime,
67 struct ktc_token token;
68 struct ktc_principal server, client;
70 code = ka_GetServerToken("afs", "", realm, lifetime, &token, /*new */ 1,
74 if (ktc_OldPioctl()) {
76 char username[MAXKTCNAMELEN];
79 char *whoami = "UserAuthenticate: ptserver";
81 strcpy(server.name, "afs");
82 strcpy(server.instance, "");
83 code = ka_ExpandCell(realm, server.cell, &local);
86 code = pr_Initialize(0, AFSDIR_CLIENT_ETC_DIRPATH, server.cell);
88 afs_com_err(whoami, code, "initializing ptserver in cell '%s'",
94 len += strlen(instance) + 1;
95 if (len >= sizeof(username)) {
96 fprintf(stderr, "user's name '%s'.'%s' would be too large\n",
100 strcpy(username, name);
102 strcat(username, ".");
103 strcat(username, instance);
105 code = pr_SNameToId(username, &viceId);
106 /* Before going further, shutdown the pr ubik connection */
108 if ((code == 0) && (viceId == ANONYMOUSID))
111 afs_com_err(whoami, code, "translating %s to id", username);
115 sprintf(client.name, "AFS ID %d", viceId);
116 strcpy(client.instance, "");
117 strcpy(client.cell, server.cell);
118 code = ktc_SetToken(&server, &token, &client, /*dosetpag */ 0);
125 #ifdef ka_UserAuthenticate
126 #undef ka_UserAuthenticate
130 ka_UserAuthenticateGeneral(afs_int32 flags, char *name, char *instance,
131 char *realm, char *password, Date lifetime,
132 afs_int32 * password_expires, /* days 'til, or don't change if not set */
133 afs_int32 spare2, char **reasonP)
135 int remainingTime = 0;
136 struct ktc_encryptionKey key;
137 afs_int32 code, dosetpag = 0;
138 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_USR_LINUX20_ENV) && !defined(AFS_XBSD_ENV) || defined(AFS_FBSD_ENV)
144 if ((flags & KA_USERAUTH_VERSION_MASK) != KA_USERAUTH_VERSION)
145 return KAOLDINTERFACE;
146 if ((strcmp(name, "root") == 0) && (instance == 0)) {
148 *reasonP = "root is only authenticated locally";
155 ka_StringToKey(password, realm, &key);
158 * alarm is set by klogin and kpasswd only so ignore for
163 { /* Rx uses timers, save to be safe */
165 /* don't reset alarms, rx already running */
168 remainingTime = alarm(0);
172 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_USR_LINUX20_ENV) && (!defined(AFS_XBSD_ENV) || defined(AFS_FBSD_ENV))
173 /* handle smoothly the case where no AFS system calls exists (yet) */
174 old = signal(SIGSYS, SIG_IGN);
176 #ifdef AFS_DECOSF_ENV
177 (void)signal(SIGTRAP, SIG_IGN);
178 #endif /* AFS_DECOSF_ENV */
181 if (flags & KA_USERAUTH_ONLY_VERIFY) {
182 code = ka_VerifyUserToken(name, instance, realm, &key);
183 if (code == KABADREQUEST) {
184 DES_string_to_key(password, ktc_to_cblockptr(&key));
185 code = ka_VerifyUserToken(name, instance, realm, &key);
189 if (flags & KA_USERAUTH_DOSETPAG)
192 #if !defined(UKERNEL) && !defined(AFS_NT40_ENV)
193 if (flags & KA_USERAUTH_DOSETPAG)
197 if (flags & KA_USERAUTH_DOSETPAG2)
199 #ifdef AFS_KERBEROS_ENV
200 if ((flags & KA_USERAUTH_DOSETPAG) || dosetpag)
204 lifetime = MAXKTCTICKETLIFETIME;
206 GetTickets(name, instance, realm, &key, lifetime,
207 password_expires, dosetpag);
208 if (code == KABADREQUEST) {
209 DES_string_to_key(password, ktc_to_cblockptr(&key));
211 GetTickets(name, instance, realm, &key, lifetime,
212 password_expires, dosetpag);
220 alarm(remainingTime); /* restore timer, if any */
227 *reasonP = "password was incorrect";
230 *reasonP = "Authentication Server was unavailable";
233 *reasonP = (char *)afs_error_message(code);
238 /* For backward compatibility */
240 ka_UserAuthenticate(char *name, char *instance, char *realm, char *password,
241 int doSetPAG, char **reasonP)
243 return ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
244 ((doSetPAG) ? KA_USERAUTH_DOSETPAG : 0),
245 name, instance, realm, password,
246 /*lifetime */ 0, /*spare1,2 */ 0, 0,
250 #if !defined(UKERNEL)
252 ka_UserReadPassword(char *prompt, char *password, int plen, char **reasonP)
261 code = UI_UTIL_read_pw_string(password, plen, prompt, 0);
264 else if (strlen(password) == 0)
265 code = KANULLPASSWORD;
270 *reasonP = (char *)afs_error_message(code);
274 #endif /* !defined(UKERNEL) */
277 ka_VerifyUserPassword(afs_int32 version, char *name, char *instance,
278 char *realm, char *password, int spare, char **reasonP)
282 version &= KA_USERAUTH_VERSION_MASK;
283 return ka_UserAuthenticateGeneral(version | KA_USERAUTH_ONLY_VERIFY, name,
284 instance, realm, password, 0,
285 &pwexpires, spare, reasonP);