stifle-valgrind-20030515
[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     memset(tbuffer, '\0', sizeof(tbuffer));
114     code = tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
115                            0xffffffff, &session, 0, "afs", "");
116     /* parms were buffer, ticketlen, key to seal ticket with, principal
117         name, instance and cell, start time, end time, session key to seal
118         in ticket, inet host, server name and server instance */
119     if (code) {
120         return QuickAuth(astr, aindex);
121     }
122
123     /* Next, we have ticket, kvno and session key, authenticate the connection.
124      * We use a magic # instead of a constant because of basic compilation
125      * order when compiling the system from scratch (rx/rxkad.h isn't installed
126      * yet). */
127     tclass = (struct rx_securityClass *)
128         rxkad_NewClientSecurityObject(enclevel, &session, kvno,
129                                       ticketLen, tbuffer);
130     *astr = tclass;
131     *aindex = 2;    /* kerberos security index */
132     return 0;
133 }
134
135 /* build a fake ticket for 'afs' using keys from adir, returning an
136  * appropriate security class and index
137  */
138 afs_int32 afsconf_ClientAuth(struct afsconf_dir *adir, 
139         struct rx_securityClass **astr, afs_int32 *aindex)
140 {
141     afs_int32 rc;
142
143     LOCK_GLOBAL_MUTEX
144     rc = GenericAuth(adir, astr, aindex, rxkad_clear);
145     UNLOCK_GLOBAL_MUTEX
146     return rc;
147 }
148
149 /* build a fake ticket for 'afs' using keys from adir, returning an
150  * appropriate security class and index.  This one, unlike the above,
151  * tells rxkad to encrypt the data, too.
152  */
153 afs_int32 afsconf_ClientAuthSecure(adir, astr, aindex)
154 struct afsconf_dir *adir;
155 struct rx_securityClass **astr;
156 afs_int32 *aindex; {
157     afs_int32 rc;
158
159     LOCK_GLOBAL_MUTEX
160     rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
161     UNLOCK_GLOBAL_MUTEX
162     return rc;
163 }
164