6d01e245300278d9f9ee136a8fdfb8df3369db06
[openafs.git] / src / kauth / user.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /* This file provides the easiest, turn-key interface to the authication
11  * package. */
12
13 #include <afsconfig.h>
14 #if defined(UKERNEL)
15 #include "afs/param.h"
16 #else
17 #include <afs/param.h>
18 #endif
19
20 RCSID
21     ("$Header$");
22
23 #if defined(UKERNEL)
24 #include "afs/sysincludes.h"
25 #include "afsincludes.h"
26 #include "afs/stds.h"
27 #include "afs/com_err.h"
28 #include "afs/cellconfig.h"
29 #include "afs/auth.h"
30 #include "afs/ptint.h"
31 #include "afs/pterror.h"
32 #include "afs/ptserver.h"
33 #include "rx/rx.h"
34 #include "rx/rx_globals.h"
35 #include "rx/rxkad.h"
36 #include "afs/kauth.h"
37 #include "afs/kautils.h"
38 #include "afs/afsutil.h"
39 #else /* defined(UKERNEL) */
40 #include <afs/stds.h>
41 #include <signal.h>
42 #include <afs/com_err.h>
43 #ifdef AFS_NT40_ENV
44 #include <winsock2.h>
45 #else
46 #include <netinet/in.h>
47 #endif
48 #ifdef HAVE_STRING_H
49 #include <string.h>
50 #else
51 #ifdef HAVE_STRINGS_H
52 #include <strings.h>
53 #endif
54 #endif
55 #include <afs/cellconfig.h>
56 #include <afs/auth.h>
57 #include <afs/ptint.h>
58 #include <afs/pterror.h>
59 #include <afs/ptserver.h>
60 #include <afs/afsutil.h>
61 #include <rx/rx.h>
62 #include <rx/rx_globals.h>
63 #include <rx/rxkad.h>           /* max ticket lifetime */
64 #include "kauth.h"
65 #include "kautils.h"
66 #endif /* defined(UKERNEL) */
67
68
69 afs_int32
70 GetTickets(char *name, char *instance, char *realm,
71            struct ktc_encryptionKey *key, Date lifetime,
72            afs_int32 * pwexpires, afs_int32 flags)
73 {
74     afs_int32 code;
75     struct ktc_token token;
76
77     code = ka_GetAuthToken(name, instance, realm, key, lifetime, pwexpires);
78     memset(key, 0, sizeof(*key));
79     if (code)
80         return code;
81     code = ka_GetAFSTicket(name, instance, realm, lifetime, flags);
82     return code;
83 }
84
85 /* 
86  * Requires that you already possess a TGT.
87  */
88 afs_int32
89 ka_GetAFSTicket(char *name, char *instance, char *realm, Date lifetime,
90                 afs_int32 flags)
91 {
92     afs_int32 code;
93     struct ktc_token token;
94     struct ktc_principal server, client;
95
96     code = ka_GetServerToken("afs", "", realm, lifetime, &token, /*new */ 1,
97                              /*dosetpag */ flags);
98     if (code)
99         return code;
100     if (ktc_OldPioctl()) {
101         int local;
102         char username[MAXKTCNAMELEN];
103         afs_int32 viceId;
104         int len;
105         char *whoami = "UserAuthenticate: ptserver";
106
107         strcpy(server.name, "afs");
108         strcpy(server.instance, "");
109         code = ka_ExpandCell(realm, server.cell, &local);
110         if (code)
111             return code;
112         code = pr_Initialize(0, AFSDIR_CLIENT_ETC_DIRPATH, server.cell);
113         if (code) {
114             com_err(whoami, code, "initializing ptserver in cell '%s'",
115                     server.cell);
116             return 0;
117         }
118         len = strlen(name);
119         if (instance[0])
120             len += strlen(instance) + 1;
121         if (len >= sizeof(username)) {
122             fprintf(stderr, "user's name '%s'.'%s' would be too large\n",
123                     name, instance);
124             return 0;
125         }
126         strcpy(username, name);
127         if (instance[0]) {
128             strcat(username, ".");
129             strcat(username, instance);
130         }
131         code = pr_SNameToId(username, &viceId);
132         /* Before going further, shutdown the pr ubik connection */
133         pr_End();
134         if ((code == 0) && (viceId == ANONYMOUSID))
135             code = PRNOENT;
136         if (code) {
137             com_err(whoami, code, "translating %s to id", username);
138             return 0;
139         }
140
141         sprintf(client.name, "AFS ID %d", viceId);
142         strcpy(client.instance, "");
143         strcpy(client.cell, server.cell);
144         code = ktc_SetToken(&server, &token, &client, /*dosetpag */ 0);
145         if (code)
146             return code;
147     }
148     return code;
149 }
150
151 #ifdef ka_UserAuthenticate
152 #undef ka_UserAuthenticate
153 #endif
154
155 afs_int32
156 ka_UserAuthenticateGeneral(afs_int32 flags, char *name, char *instance, char *realm, char *password, Date lifetime, afs_int32 * password_expires,       /* days 'til, or don't change if not set */
157                            afs_int32 spare2, char **reasonP)
158 {
159     int remainingTime = 0;
160     struct ktc_encryptionKey key;
161     afs_int32 code, dosetpag = 0;
162     int (*old) ();
163
164     if (reasonP)
165         *reasonP = "";
166     if ((flags & KA_USERAUTH_VERSION_MASK) != KA_USERAUTH_VERSION)
167         return KAOLDINTERFACE;
168     if ((strcmp(name, "root") == 0) && (instance == 0)) {
169         if (reasonP)
170             *reasonP = "root is only authenticated locally";
171         return KANOENT;
172     }
173     code = ka_Init(0);
174     if (code)
175         return code;
176
177     ka_StringToKey(password, realm, &key);
178
179 /* 
180  * alarm is set by klogin and kpasswd only so ignore for
181  * NT
182  */
183
184 #ifndef AFS_NT40_ENV
185     {                           /* Rx uses timers, save to be safe */
186         if (rx_socket) {
187             /* don't reset alarms, rx already running */
188             remainingTime = 0;
189         } else
190             remainingTime = alarm(0);
191     }
192 #endif
193
194 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_USR_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
195     /* handle smoothly the case where no AFS system calls exists (yet) */
196     old = (int (*)())signal(SIGSYS, SIG_IGN);
197 #endif
198 #ifdef  AFS_DECOSF_ENV
199     (void)signal(SIGTRAP, SIG_IGN);
200 #endif /* AFS_DECOSF_ENV */
201     if (instance == 0)
202         instance = "";
203     if (flags & KA_USERAUTH_ONLY_VERIFY) {
204         code = ka_VerifyUserToken(name, instance, realm, &key);
205         if (code == KABADREQUEST) {
206             des_string_to_key(password, &key);
207             code = ka_VerifyUserToken(name, instance, realm, &key);
208         }
209     } else {
210 #ifdef AFS_DUX40_ENV
211         if (flags & KA_USERAUTH_DOSETPAG)
212             afs_setpag();
213 #else
214 #if !defined(UKERNEL) && !defined(AFS_NT40_ENV)
215         if (flags & KA_USERAUTH_DOSETPAG)
216             setpag();
217 #endif
218 #endif
219         if (flags & KA_USERAUTH_DOSETPAG2)
220             dosetpag = 1;
221 #ifdef AFS_KERBEROS_ENV
222         if ((flags & KA_USERAUTH_DOSETPAG) || dosetpag)
223             ktc_newpag();
224 #endif
225         if (lifetime == 0)
226             lifetime = MAXKTCTICKETLIFETIME;
227         code =
228             GetTickets(name, instance, realm, &key, lifetime,
229                        password_expires, dosetpag);
230         if (code == KABADREQUEST) {
231             des_string_to_key(password, &key);
232             code =
233                 GetTickets(name, instance, realm, &key, lifetime,
234                            password_expires, dosetpag);
235         }
236
237 /* By the time 3.3 comes out, these "old-style" passwd programs should be 
238  * well and truly obsolete.  Any passwords set with such a program
239  * OUGHT to have been changed years ago.  Having 2 -or- 3
240  * authentication RPCs generated for every klog plays hob with the
241  * "failed login limits" code in the kaserver, and it's hard to
242  * explain to admins just how to set the limit properly.  By removing 
243  * this function, we can just double it internally in the kaserver, and 
244  * not document anything.  kpasswd had the TRUNCATEPASSWORD "feature"
245  * disabled on 10/02/90.
246  */
247 #ifdef OLDCRUFT
248         if ((code == KABADREQUEST) && (strlen(password) > 8)) {
249             /* try with only the first 8 characters incase they set their password
250              * with an old style passwd program. */
251             char pass8[9];
252             strncpy(pass8, password, 8);
253             pass8[8] = 0;
254             ka_StringToKey(pass8, realm, &key);
255             memset(pass8, 0, sizeof(pass8));
256             code =
257                 GetTickets(name, instance, realm, &key, lifetime,
258                            password_expires, dosetpag);
259             if (code == 0) {
260                 fprintf(stderr, "%s %s\n%s %s\n%s\n",
261                         "Warning: you have typed a password longer than 8",
262                         "characters, but only the",
263                         "first 8 characters were actually significant.  If",
264                         "you change your password",
265                         "again this warning message will go away.\n");
266             }
267         }
268 #endif /* OLDCRUFT */
269     }
270
271 #ifndef AFS_NT40_ENV
272     if (remainingTime) {
273         pr_End();
274         rx_Finalize();
275         alarm(remainingTime);   /* restore timer, if any */
276     }
277 #endif
278
279     if (code && reasonP)
280         switch (code) {
281         case KABADREQUEST:
282             *reasonP = "password was incorrect";
283             break;
284         case KAUBIKCALL:
285             *reasonP = "Authentication Server was unavailable";
286             break;
287         default:
288             *reasonP = (char *)error_message(code);
289         }
290     return code;
291 }
292
293 /* For backward compatibility */
294 afs_int32
295 ka_UserAuthenticate(char *name, char *instance, char *realm, char *password,
296                     int doSetPAG, char **reasonP)
297 {
298     return ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
299                                       ((doSetPAG) ? KA_USERAUTH_DOSETPAG : 0),
300                                       name, instance, realm, password,
301                                       /*lifetime */ 0, /*spare1,2 */ 0, 0,
302                                       reasonP);
303 }
304
305 #if !defined(UKERNEL)
306 afs_int32
307 ka_UserReadPassword(char *prompt, char *password, int plen, char **reasonP)
308 {
309     afs_int32 code = 0;
310
311     if (reasonP)
312         *reasonP = "";
313     code = ka_Init(0);
314     if (code)
315         return code;
316     code = read_pw_string(password, plen, prompt, 0);
317     if (code)
318         code = KAREADPW;
319     else if (strlen(password) == 0)
320         code = KANULLPASSWORD;
321     else
322         return 0;
323
324     if (reasonP) {
325         *reasonP = (char *)error_message(code);
326     }
327     return code;
328 }
329 #endif /* !defined(UKERNEL) */
330
331 afs_int32
332 ka_VerifyUserPassword(afs_int32 version, char *name, char *instance,
333                       char *realm, char *password, int spare, char **reasonP)
334 {
335     afs_int32 pwexpires;
336
337     version &= KA_USERAUTH_VERSION_MASK;
338     return ka_UserAuthenticateGeneral(version | KA_USERAUTH_ONLY_VERIFY, name,
339                                       instance, realm, password, 0,
340                                       &pwexpires, spare, reasonP);
341 }