read_message.c revision 178825
1331722Seadler/*
2256694Snp * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3256694Snp * (Royal Institute of Technology, Stockholm, Sweden).
4256694Snp * All rights reserved.
5256694Snp *
6256694Snp * Redistribution and use in source and binary forms, with or without
7256694Snp * modification, are permitted provided that the following conditions
8256694Snp * are met:
9256694Snp *
10256694Snp * 1. Redistributions of source code must retain the above copyright
11256694Snp *    notice, this list of conditions and the following disclaimer.
12256694Snp *
13256694Snp * 2. Redistributions in binary form must reproduce the above copyright
14256694Snp *    notice, this list of conditions and the following disclaimer in the
15256694Snp *    documentation and/or other materials provided with the distribution.
16256694Snp *
17256694Snp * 3. Neither the name of the Institute nor the names of its contributors
18256694Snp *    may be used to endorse or promote products derived from this software
19256694Snp *    without specific prior written permission.
20256694Snp *
21256694Snp * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22256694Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23256694Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24256694Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25256694Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26256694Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27256694Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28256694Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29256694Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30256694Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31256694Snp * SUCH DAMAGE.
32256694Snp */
33256694Snp
34256694Snp#include "krb5_locl.h"
35256694Snp
36331769ShselaskyRCSID("$Id: read_message.c 21750 2007-07-31 20:41:25Z lha $");
37331769Shselasky
38256694Snpkrb5_error_code KRB5_LIB_FUNCTION
39256694Snpkrb5_read_message (krb5_context context,
40256694Snp		   krb5_pointer p_fd,
41256694Snp		   krb5_data *data)
42256694Snp{
43256694Snp    krb5_error_code ret;
44256694Snp    uint32_t len;
45256694Snp    uint8_t buf[4];
46256694Snp
47256694Snp    krb5_data_zero(data);
48256694Snp
49256694Snp    ret = krb5_net_read (context, p_fd, buf, 4);
50256694Snp    if(ret == -1) {
51256694Snp	ret = errno;
52256694Snp	krb5_clear_error_string (context);
53256694Snp	return ret;
54256694Snp    }
55256694Snp    if(ret < 4) {
56256694Snp	krb5_clear_error_string(context);
57256694Snp	return HEIM_ERR_EOF;
58256694Snp    }
59256694Snp    len = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
60256694Snp    ret = krb5_data_alloc (data, len);
61256694Snp    if (ret) {
62256694Snp	krb5_clear_error_string(context);
63256694Snp	return ret;
64318798Snp    }
65331769Shselasky    if (krb5_net_read (context, p_fd, data->data, len) != len) {
66331769Shselasky	ret = errno;
67256694Snp	krb5_data_free (data);
68256694Snp	krb5_clear_error_string (context);
69256694Snp	return ret;
70256694Snp    }
71256694Snp    return 0;
72256694Snp}
73256694Snp
74256694Snpkrb5_error_code KRB5_LIB_FUNCTION
75256694Snpkrb5_read_priv_message(krb5_context context,
76256694Snp		       krb5_auth_context ac,
77256694Snp		       krb5_pointer p_fd,
78256694Snp		       krb5_data *data)
79256694Snp{
80256694Snp    krb5_error_code ret;
81256694Snp    krb5_data packet;
82256694Snp
83256694Snp    ret = krb5_read_message(context, p_fd, &packet);
84256694Snp    if(ret)
85256694Snp	return ret;
86256694Snp    ret = krb5_rd_priv (context, ac, &packet, data, NULL);
87256694Snp    krb5_data_free(&packet);
88256694Snp    return ret;
89256694Snp}
90256694Snp
91256694Snpkrb5_error_code KRB5_LIB_FUNCTION
92256694Snpkrb5_read_safe_message(krb5_context context,
93256694Snp		       krb5_auth_context ac,
94256694Snp		       krb5_pointer p_fd,
95256694Snp		       krb5_data *data)
96256694Snp{
97256694Snp    krb5_error_code ret;
98256694Snp    krb5_data packet;
99256694Snp
100256694Snp    ret = krb5_read_message(context, p_fd, &packet);
101331769Shselasky    if(ret)
102331769Shselasky	return ret;
103331769Shselasky    ret = krb5_rd_safe (context, ac, &packet, data, NULL);
104256694Snp    krb5_data_free(&packet);
105331769Shselasky    return ret;
106331769Shselasky}
107331769Shselasky