reindent-20030715
[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 tclass = (struct rx_securityClass *)
76         rxkad_NewServerSecurityObject(0, adir, afsconf_GetKey, NULL);
77     if (tclass) {
78         *astr = tclass;
79         *aindex = 2;            /* kerberos security index */
80         UNLOCK_GLOBAL_MUTEX return 0;
81     } else {
82         UNLOCK_GLOBAL_MUTEX return 2;
83     }
84 }
85 #endif /* !defined(UKERNEL) */
86
87 static afs_int32
88 GenericAuth(adir, astr, aindex, enclevel)
89      struct afsconf_dir *adir;
90      struct rx_securityClass **astr;
91      afs_int32 *aindex;
92      rxkad_level enclevel;
93 {
94     char tbuffer[256];
95     struct ktc_encryptionKey key, session;
96     struct rx_securityClass *tclass;
97     afs_int32 kvno;
98     afs_int32 ticketLen;
99     register afs_int32 code;
100
101     /* first, find the right key and kvno to use */
102     code = afsconf_GetLatestKey(adir, &kvno, &key);
103     if (code) {
104         return QuickAuth(astr, aindex);
105     }
106
107     /* next create random session key, using key for seed to good random */
108     des_init_random_number_generator(&key);
109     code = des_random_key(&session);
110     if (code) {
111         return QuickAuth(astr, aindex);
112     }
113
114     /* now create the actual ticket */
115     ticketLen = sizeof(tbuffer);
116     memset(tbuffer, '\0', sizeof(tbuffer));
117     code =
118         tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
119                        0xffffffff, &session, 0, "afs", "");
120     /* parms were buffer, ticketlen, key to seal ticket with, principal
121      * name, instance and cell, start time, end time, session key to seal
122      * in ticket, inet host, server name and server instance */
123     if (code) {
124         return QuickAuth(astr, aindex);
125     }
126
127     /* Next, we have ticket, kvno and session key, authenticate the connection.
128      * We use a magic # instead of a constant because of basic compilation
129      * order when compiling the system from scratch (rx/rxkad.h isn't installed
130      * yet). */
131     tclass = (struct rx_securityClass *)
132         rxkad_NewClientSecurityObject(enclevel, &session, kvno, ticketLen,
133                                       tbuffer);
134     *astr = tclass;
135     *aindex = 2;                /* kerberos security index */
136     return 0;
137 }
138
139 /* build a fake ticket for 'afs' using keys from adir, returning an
140  * appropriate security class and index
141  */
142 afs_int32
143 afsconf_ClientAuth(struct afsconf_dir * adir, struct rx_securityClass ** astr,
144                    afs_int32 * aindex)
145 {
146     afs_int32 rc;
147
148     LOCK_GLOBAL_MUTEX rc = GenericAuth(adir, astr, aindex, rxkad_clear);
149     UNLOCK_GLOBAL_MUTEX return rc;
150 }
151
152 /* build a fake ticket for 'afs' using keys from adir, returning an
153  * appropriate security class and index.  This one, unlike the above,
154  * tells rxkad to encrypt the data, too.
155  */
156 afs_int32
157 afsconf_ClientAuthSecure(adir, astr, aindex)
158      struct afsconf_dir *adir;
159      struct rx_securityClass **astr;
160      afs_int32 *aindex;
161 {
162     afs_int32 rc;
163
164     LOCK_GLOBAL_MUTEX rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
165     UNLOCK_GLOBAL_MUTEX return rc;
166 }