aff1106a8fc70571b087a3334de6e3fe995d9a32
[openafs.git] / src / auth / authcon.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 #include <afsconfig.h>
11 #if defined(UKERNEL)
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID
18     ("$Header$");
19
20 #if defined(UKERNEL)
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/stds.h"
24 #include "afs/pthread_glock.h"
25 #include "des/des.h"
26 #include "rx/rxkad.h"
27 #include "rx/rx.h"
28 #include "afs/cellconfig.h"
29 #include "afs/keys.h"
30 #include "afs/auth.h"
31 #include "afs/pthread_glock.h"
32 #else /* defined(UKERNEL) */
33 #include <afs/stds.h>
34 #include <afs/pthread_glock.h>
35 #include <sys/types.h>
36 #ifdef AFS_NT40_ENV
37 #include <winsock2.h>
38 #else
39 #include <sys/file.h>
40 #include <sys/time.h>
41 #include <netinet/in.h>
42 #include <netdb.h>
43 #endif
44 #include <des.h>
45 #include <rx/rxkad.h>
46 #include <rx/rx.h>
47 #include "cellconfig.h"
48 #include "keys.h"
49 #include "auth.h"
50 #endif /* defined(UKERNEL) */
51
52 /* return a null security object if nothing else can be done */
53 static afs_int32
54 QuickAuth(astr, aindex)
55      struct rx_securityClass **astr;
56      afs_int32 *aindex;
57 {
58     register struct rx_securityClass *tc;
59     tc = rxnull_NewClientSecurityObject();
60     *astr = tc;
61     *aindex = 0;
62     return 0;
63 }
64
65 #if !defined(UKERNEL)
66 /* Return an appropriate security class and index */
67 afs_int32
68 afsconf_ServerAuth(adir, astr, aindex)
69      register struct afsconf_dir *adir;
70      struct rx_securityClass **astr;
71      afs_int32 *aindex;
72 {
73     register struct rx_securityClass *tclass;
74
75     LOCK_GLOBAL_MUTEX;
76     tclass = (struct rx_securityClass *)
77         rxkad_NewServerSecurityObject(0, adir, afsconf_GetKey, NULL);
78     if (tclass) {
79         *astr = tclass;
80         *aindex = 2;            /* kerberos security index */
81         UNLOCK_GLOBAL_MUTEX;
82         return 0;
83     } else {
84         UNLOCK_GLOBAL_MUTEX;
85         return 2;
86     }
87 }
88 #endif /* !defined(UKERNEL) */
89
90 static afs_int32
91 GenericAuth(adir, astr, aindex, enclevel)
92      struct afsconf_dir *adir;
93      struct rx_securityClass **astr;
94      afs_int32 *aindex;
95      rxkad_level enclevel;
96 {
97     char tbuffer[256];
98     struct ktc_encryptionKey key, session;
99     struct rx_securityClass *tclass;
100     afs_int32 kvno;
101     afs_int32 ticketLen;
102     register afs_int32 code;
103
104     /* first, find the right key and kvno to use */
105     code = afsconf_GetLatestKey(adir, &kvno, &key);
106     if (code) {
107         return QuickAuth(astr, aindex);
108     }
109
110     /* next create random session key, using key for seed to good random */
111     des_init_random_number_generator(&key);
112     code = des_random_key(&session);
113     if (code) {
114         return QuickAuth(astr, aindex);
115     }
116
117     /* now create the actual ticket */
118     ticketLen = sizeof(tbuffer);
119     memset(tbuffer, '\0', sizeof(tbuffer));
120     code =
121         tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
122                        0xffffffff, &session, 0, "afs", "");
123     /* parms were buffer, ticketlen, key to seal ticket with, principal
124      * name, instance and cell, start time, end time, session key to seal
125      * in ticket, inet host, server name and server instance */
126     if (code) {
127         return QuickAuth(astr, aindex);
128     }
129
130     /* Next, we have ticket, kvno and session key, authenticate the connection.
131      * We use a magic # instead of a constant because of basic compilation
132      * order when compiling the system from scratch (rx/rxkad.h isn't installed
133      * yet). */
134     tclass = (struct rx_securityClass *)
135         rxkad_NewClientSecurityObject(enclevel, &session, kvno, ticketLen,
136                                       tbuffer);
137     *astr = tclass;
138     *aindex = 2;                /* kerberos security index */
139     return 0;
140 }
141
142 /* build a fake ticket for 'afs' using keys from adir, returning an
143  * appropriate security class and index
144  */
145 afs_int32
146 afsconf_ClientAuth(struct afsconf_dir * adir, struct rx_securityClass ** astr,
147                    afs_int32 * aindex)
148 {
149     afs_int32 rc;
150
151     LOCK_GLOBAL_MUTEX;
152     rc = GenericAuth(adir, astr, aindex, rxkad_clear);
153     UNLOCK_GLOBAL_MUTEX;
154     return rc;
155 }
156
157 /* build a fake ticket for 'afs' using keys from adir, returning an
158  * appropriate security class and index.  This one, unlike the above,
159  * tells rxkad to encrypt the data, too.
160  */
161 afs_int32
162 afsconf_ClientAuthSecure(adir, astr, aindex)
163      struct afsconf_dir *adir;
164      struct rx_securityClass **astr;
165      afs_int32 *aindex;
166 {
167     afs_int32 rc;
168
169     LOCK_GLOBAL_MUTEX;
170     rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
171     UNLOCK_GLOBAL_MUTEX;
172     return rc;
173 }