xref: /freebsd/crypto/heimdal/lib/krb5/recvauth.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "krb5_locl.h"
35 
36 RCSID("$Id: recvauth.c,v 1.12 1999/12/02 17:05:12 joda Exp $");
37 
38 /*
39  * See `sendauth.c' for the format.
40  */
41 
42 static krb5_boolean
43 match_exact(void *data, const char *appl_version)
44 {
45     return strcmp(data, appl_version) == 0;
46 }
47 
48 krb5_error_code
49 krb5_recvauth(krb5_context context,
50 	      krb5_auth_context *auth_context,
51 	      krb5_pointer p_fd,
52 	      char *appl_version,
53 	      krb5_principal server,
54 	      int32_t flags,
55 	      krb5_keytab keytab,
56 	      krb5_ticket **ticket)
57 {
58     return krb5_recvauth_match_version(context, auth_context, p_fd,
59 				       match_exact, appl_version,
60 				       server, flags,
61 				       keytab, ticket);
62 }
63 
64 krb5_error_code
65 krb5_recvauth_match_version(krb5_context context,
66 			    krb5_auth_context *auth_context,
67 			    krb5_pointer p_fd,
68 			    krb5_boolean (*match_appl_version)(void *,
69 							       const char*),
70 			    void *match_data,
71 			    krb5_principal server,
72 			    int32_t flags,
73 			    krb5_keytab keytab,
74 			    krb5_ticket **ticket)
75 {
76   krb5_error_code ret;
77   const char *version = KRB5_SENDAUTH_VERSION;
78   char her_version[sizeof(KRB5_SENDAUTH_VERSION)];
79   char *her_appl_version;
80   u_int32_t len;
81   u_char repl;
82   krb5_data data;
83   krb5_flags ap_options;
84   ssize_t n;
85 
86   /*
87    * If there are no addresses in auth_context, get them from `fd'.
88    */
89 
90   if (*auth_context == NULL) {
91       ret = krb5_auth_con_init (context, auth_context);
92       if (ret)
93 	  return ret;
94   }
95 
96   ret = krb5_auth_con_setaddrs_from_fd (context,
97 					*auth_context,
98 					p_fd);
99   if (ret)
100       return ret;
101 
102   if(!(flags & KRB5_RECVAUTH_IGNORE_VERSION)) {
103     n = krb5_net_read (context, p_fd, &len, 4);
104     if (n < 0)
105 	return errno;
106     if (n == 0)
107 	return KRB5_SENDAUTH_BADAUTHVERS;
108     len = ntohl(len);
109     if (len != sizeof(her_version)
110 	|| krb5_net_read (context, p_fd, her_version, len) != len
111 	|| strncmp (version, her_version, len)) {
112       repl = 1;
113       krb5_net_write (context, p_fd, &repl, 1);
114       return KRB5_SENDAUTH_BADAUTHVERS;
115     }
116   }
117 
118   n = krb5_net_read (context, p_fd, &len, 4);
119   if (n < 0)
120       return errno;
121   if (n == 0)
122       return KRB5_SENDAUTH_BADAPPLVERS;
123   len = ntohl(len);
124   her_appl_version = malloc (len);
125   if (her_appl_version == NULL) {
126       repl = 2;
127       krb5_net_write (context, p_fd, &repl, 1);
128       return ENOMEM;
129   }
130   if (krb5_net_read (context, p_fd, her_appl_version, len) != len
131       || !(*match_appl_version)(match_data, her_appl_version)) {
132     repl = 2;
133     krb5_net_write (context, p_fd, &repl, 1);
134     free (her_appl_version);
135     return KRB5_SENDAUTH_BADAPPLVERS;
136   }
137   free (her_appl_version);
138 
139   repl = 0;
140   if (krb5_net_write (context, p_fd, &repl, 1) != 1)
141     return errno;
142 
143   krb5_data_zero (&data);
144   ret = krb5_read_message (context, p_fd, &data);
145   if (ret)
146       return ret;
147 
148   ret = krb5_rd_req (context,
149 		     auth_context,
150 		     &data,
151 		     server,
152 		     keytab,
153 		     &ap_options,
154 		     ticket);
155   krb5_data_free (&data);
156   if (ret) {
157       krb5_data error_data;
158       krb5_error_code ret2;
159 
160       ret2 = krb5_mk_error (context,
161 			    ret,
162 			    NULL,
163 			    NULL,
164 			    NULL,
165 			    server,
166 			    0,
167 			    &error_data);
168       if (ret2 == 0) {
169 	  krb5_write_message (context, p_fd, &error_data);
170 	  krb5_data_free (&error_data);
171       }
172       return ret;
173   }
174 
175   len = 0;
176   if (krb5_net_write (context, p_fd, &len, 4) != 4)
177     return errno;
178 
179   if (ap_options & AP_OPTS_MUTUAL_REQUIRED) {
180     ret = krb5_mk_rep (context, auth_context, &data);
181     if (ret)
182       return ret;
183 
184     ret = krb5_write_message (context, p_fd, &data);
185     if (ret)
186 	return ret;
187     krb5_data_free (&data);
188   }
189   return 0;
190 }
191