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