]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/lib/krb4.c
hello world
[icculus/iodoom3.git] / neo / curl / lib / krb4.c
1 /* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> for
2  * use in Curl. His latest changes were done 2000-09-18.
3  *
4  * It has since been patched away like a madman by Daniel Stenberg
5  * <daniel@haxx.se> to make it better applied to curl conditions, and to make
6  * it not use globals, pollute name space and more. This source code awaits a
7  * rewrite to work around the paragraph 2 in the BSD licenses as explained
8  * below.
9  *
10  * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
11  * (Royal Institute of Technology, Stockholm, Sweden).
12  * All rights reserved.
13  * 
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 
25  * 3. Neither the name of the Institute nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  * 
29  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.  */
40
41 #include "setup.h"
42
43 #ifndef CURL_DISABLE_FTP
44 #ifdef HAVE_KRB4
45
46 #include "security.h"
47 #include "base64.h"
48 #include <stdlib.h>
49 #ifdef HAVE_NETDB_H
50 #include <netdb.h>
51 #endif
52 #include <string.h>
53 #include <krb.h>
54 #include <des.h>
55
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h> /* for getpid() */
58 #endif
59
60 #include "ftp.h"
61 #include "sendf.h"
62 #include "krb4.h"
63
64 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
65 #include "inet_ntoa_r.h"
66 #endif
67
68 /* The last #include file should be: */
69 #ifdef CURLDEBUG
70 #include "memdebug.h"
71 #endif
72
73 #define LOCAL_ADDR (&conn->local_addr)
74 #define REMOTE_ADDR (&conn->serv_addr)
75 #define myctladdr LOCAL_ADDR
76 #define hisctladdr REMOTE_ADDR
77
78 struct krb4_data {
79   des_cblock key;
80   des_key_schedule schedule;
81   char name[ANAME_SZ];
82   char instance[INST_SZ];
83   char realm[REALM_SZ];
84 };
85
86 #ifndef HAVE_STRLCPY
87 /* if it ever goes non-static, make it Curl_ prefixed! */
88 static size_t
89 strlcpy (char *dst, const char *src, size_t dst_sz)
90 {
91   size_t n;
92   char *p;
93
94   for (p = dst, n = 0;
95        n + 1 < dst_sz && *src != '\0';
96        ++p, ++src, ++n)
97     *p = *src;
98   *p = '\0';
99   if (*src == '\0')
100     return n;
101   else
102     return n + strlen (src);
103 }
104 #else
105 size_t strlcpy (char *dst, const char *src, size_t dst_sz);
106 #endif
107
108 static int
109 krb4_check_prot(void *app_data, int level)
110 {
111   app_data = NULL; /* prevent compiler warning */
112   if(level == prot_confidential)
113     return -1;
114   return 0;
115 }
116
117 static int
118 krb4_decode(void *app_data, void *buf, int len, int level,
119             struct connectdata *conn)
120 {
121   MSG_DAT m;
122   int e;
123   struct krb4_data *d = app_data;
124   
125   if(level == prot_safe)
126     e = krb_rd_safe(buf, len, &d->key,
127                     (struct sockaddr_in *)REMOTE_ADDR,
128                     (struct sockaddr_in *)LOCAL_ADDR, &m);
129   else
130     e = krb_rd_priv(buf, len, d->schedule, &d->key, 
131                     (struct sockaddr_in *)REMOTE_ADDR,
132                     (struct sockaddr_in *)LOCAL_ADDR, &m);
133   if(e) {
134     struct SessionHandle *data = conn->data;
135     infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
136     return -1;
137   }
138   memmove(buf, m.app_data, m.app_length);
139   return m.app_length;
140 }
141
142 static int
143 krb4_overhead(void *app_data, int level, int len)
144 {
145   /* no arguments are used, just init them to prevent compiler warnings */
146   app_data = NULL;
147   level = 0;
148   len = 0;
149   return 31;
150 }
151
152 static int
153 krb4_encode(void *app_data, void *from, int length, int level, void **to,
154             struct connectdata *conn)
155 {
156   struct krb4_data *d = app_data;
157   *to = malloc(length + 31);
158   if(level == prot_safe)
159     return krb_mk_safe(from, *to, length, &d->key, 
160                        (struct sockaddr_in *)LOCAL_ADDR,
161                        (struct sockaddr_in *)REMOTE_ADDR);
162   else if(level == prot_private)
163     return krb_mk_priv(from, *to, length, d->schedule, &d->key, 
164                        (struct sockaddr_in *)LOCAL_ADDR,
165                        (struct sockaddr_in *)REMOTE_ADDR);
166   else
167     return -1;
168 }
169
170 static int
171 mk_auth(struct krb4_data *d, KTEXT adat, 
172         const char *service, char *host, int checksum)
173 {
174   int ret;
175   CREDENTIALS cred;
176   char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
177
178   strlcpy(sname, service, sizeof(sname));
179   strlcpy(inst, krb_get_phost(host), sizeof(inst));
180   strlcpy(realm, krb_realmofhost(host), sizeof(realm));
181   ret = krb_mk_req(adat, sname, inst, realm, checksum);
182   if(ret)
183     return ret;
184   strlcpy(sname, service, sizeof(sname));
185   strlcpy(inst, krb_get_phost(host), sizeof(inst));
186   strlcpy(realm, krb_realmofhost(host), sizeof(realm));
187   ret = krb_get_cred(sname, inst, realm, &cred);
188   memmove(&d->key, &cred.session, sizeof(des_cblock));
189   des_key_sched(&d->key, d->schedule);
190   memset(&cred, 0, sizeof(cred));
191   return ret;
192 }
193
194 #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
195 int krb_get_our_ip_for_realm(char *, struct in_addr *);
196 #endif
197
198 static int
199 krb4_auth(void *app_data, struct connectdata *conn)
200 {
201   int ret;
202   char *p;
203   int len;
204   KTEXT_ST adat;
205   MSG_DAT msg_data;
206   int checksum;
207   u_int32_t cs;
208   struct krb4_data *d = app_data;
209   char *host = conn->hostname;
210   ssize_t nread;
211   int l = sizeof(conn->local_addr);
212   struct SessionHandle *data = conn->data;
213   CURLcode result;
214
215   if(getsockname(conn->sock[FIRSTSOCKET],
216                  (struct sockaddr *)LOCAL_ADDR, &l) < 0)
217     perror("getsockname()");
218
219   checksum = getpid();
220   ret = mk_auth(d, &adat, "ftp", host, checksum);
221   if(ret == KDC_PR_UNKNOWN)
222     ret = mk_auth(d, &adat, "rcmd", host, checksum);
223   if(ret) {
224     Curl_infof(data, "%s\n", krb_get_err_text(ret));
225     return AUTH_CONTINUE;
226   }
227   
228 #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
229   if (krb_get_config_bool("nat_in_use")) {
230     struct sockaddr_in *localaddr  = (struct sockaddr_in *)LOCAL_ADDR;
231     struct in_addr natAddr;
232
233     if (krb_get_our_ip_for_realm(krb_realmofhost(host),
234                                  &natAddr) != KSUCCESS
235         && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
236       Curl_infof(data, "Can't get address for realm %s\n",
237                  krb_realmofhost(host));
238     else {
239       if (natAddr.s_addr != localaddr->sin_addr.s_addr) {
240 #ifdef HAVE_INET_NTOA_R
241         char ntoa_buf[64];
242         char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
243 #else
244         char *ip = (char *)inet_ntoa(natAddr);
245 #endif
246         Curl_infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
247         localaddr->sin_addr = natAddr;
248       }
249     }
250   }
251 #endif
252
253   if(Curl_base64_encode((char *)adat.dat, adat.length, &p) < 1) {
254     Curl_failf(data, "Out of memory base64-encoding");
255     return AUTH_CONTINUE;
256   }
257
258   result = Curl_ftpsendf(conn, "ADAT %s", p);
259
260   free(p);
261
262   if(result)
263     return -2;
264
265   if(Curl_GetFTPResponse(&nread, conn, NULL))
266     return -1;
267
268   if(data->state.buffer[0] != '2'){
269     Curl_failf(data, "Server didn't accept auth data");
270     return AUTH_ERROR;
271   }
272
273   p = strstr(data->state.buffer, "ADAT=");
274   if(!p) {
275     Curl_failf(data, "Remote host didn't send adat reply");
276     return AUTH_ERROR;
277   }
278   p += 5;
279   len = Curl_base64_decode(p, (char *)adat.dat);
280   if(len < 0) {
281     Curl_failf(data, "Failed to decode base64 from server");
282     return AUTH_ERROR;
283   }
284   adat.length = len;
285   ret = krb_rd_safe(adat.dat, adat.length, &d->key, 
286                     (struct sockaddr_in *)hisctladdr, 
287                     (struct sockaddr_in *)myctladdr, &msg_data);
288   if(ret) {
289     Curl_failf(data, "Error reading reply from server: %s", 
290                krb_get_err_text(ret));
291     return AUTH_ERROR;
292   }
293   krb_get_int(msg_data.app_data, &cs, 4, 0);
294   if(cs - checksum != 1) {
295     Curl_failf(data, "Bad checksum returned from server");
296     return AUTH_ERROR;
297   }
298   return AUTH_OK;
299 }
300
301 struct Curl_sec_client_mech Curl_krb4_client_mech = {
302     "KERBEROS_V4",
303     sizeof(struct krb4_data),
304     NULL, /* init */
305     krb4_auth,
306     NULL, /* end */
307     krb4_check_prot,
308     krb4_overhead,
309     krb4_encode,
310     krb4_decode
311 };
312
313 CURLcode Curl_krb_kauth(struct connectdata *conn)
314 {
315   des_cblock key;
316   des_key_schedule schedule;
317   KTEXT_ST tkt, tktcopy;
318   char *name;
319   char *p;
320   char passwd[100];
321   int tmp;
322   ssize_t nread;
323   int save;
324   CURLcode result;
325
326   save = Curl_set_command_prot(conn, prot_private);
327
328   result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
329
330   if(result)
331     return result;
332
333   result = Curl_GetFTPResponse(&nread, conn, NULL);
334   if(result)
335     return result;
336
337   if(conn->data->state.buffer[0] != '3'){
338     Curl_set_command_prot(conn, save);
339     return CURLE_FTP_WEIRD_SERVER_REPLY;
340   }
341
342   p = strstr(conn->data->state.buffer, "T=");
343   if(!p) {
344     Curl_failf(conn->data, "Bad reply from server");
345     Curl_set_command_prot(conn, save);
346     return CURLE_FTP_WEIRD_SERVER_REPLY;
347   }
348
349   p += 2;
350   tmp = Curl_base64_decode(p, (char *)tkt.dat);
351   if(tmp < 0) {
352     Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
353     Curl_set_command_prot(conn, save);
354     return CURLE_FTP_WEIRD_SERVER_REPLY;
355   }
356   tkt.length = tmp;
357   tktcopy.length = tkt.length;
358     
359   p = strstr(conn->data->state.buffer, "P=");
360   if(!p) {
361     Curl_failf(conn->data, "Bad reply from server");
362     Curl_set_command_prot(conn, save);
363     return CURLE_FTP_WEIRD_SERVER_REPLY;
364   }
365   name = p + 2;
366   for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
367   *p = 0;
368
369   des_string_to_key (conn->passwd, &key);
370   des_key_sched(&key, schedule);
371     
372   des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
373                    tkt.length,
374                    schedule, &key, DES_DECRYPT);
375   if (strcmp ((char*)tktcopy.dat + 8,
376               KRB_TICKET_GRANTING_TICKET) != 0) {
377     afs_string_to_key(passwd,
378                       krb_realmofhost(conn->hostname),
379                       &key);
380     des_key_sched(&key, schedule);
381     des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
382                      tkt.length,
383                      schedule, &key, DES_DECRYPT);
384   }
385   memset(key, 0, sizeof(key));
386   memset(schedule, 0, sizeof(schedule));
387   memset(passwd, 0, sizeof(passwd));
388   if(Curl_base64_encode((char *)tktcopy.dat, tktcopy.length, &p) < 1) {
389     failf(conn->data, "Out of memory base64-encoding.");
390     Curl_set_command_prot(conn, save);
391     return CURLE_OUT_OF_MEMORY;
392   }
393   memset (tktcopy.dat, 0, tktcopy.length);
394
395   result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
396   free(p);
397   if(result)
398     return result;
399
400   result = Curl_GetFTPResponse(&nread, conn, NULL);
401   if(result)
402     return result;
403   Curl_set_command_prot(conn, save);
404
405   return CURLE_OK;
406 }
407
408 #endif /* HAVE_KRB4 */
409 #endif /* CURL_DISABLE_FTP */