~ubuntu-branches/ubuntu/vivid/ecryptfs-utils/vivid

« back to all changes in this revision

Viewing changes to src/libecryptfs/messaging.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-04-09 09:54:00 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080409095400-cexrc09wzgqsk9bs
Tags: 43-1
* New upstream release.
* Removing watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Userspace side of communications with eCryptfs kernel module.
 
3
 *
 
4
 * Copyright (C) 2008 International Business Machines Corp.
 
5
 *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License as
 
9
 * published by the Free Software Foundation; either version 2 of the
 
10
 * License, or (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
 * 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include <errno.h>
 
24
#include <syslog.h>
 
25
#include <string.h>
 
26
#include <sys/types.h>
 
27
#include "config.h"
 
28
#include "../include/ecryptfs.h"
 
29
 
 
30
/**
 
31
 * ecryptfs_write_packet_length
 
32
 * @dest: The byte array target into which to write the
 
33
 *       length. Must have at least 5 bytes allocated.
 
34
 * @size: The length to write.
 
35
 * @packet_size_length: The number of bytes used to encode the
 
36
 *                      packet length is written to this address.
 
37
 *
 
38
 * Returns zero on success; non-zero on error.
 
39
 */
 
40
int ecryptfs_write_packet_length(char *dest, size_t size,
 
41
                                 size_t *packet_size_length)
 
42
{
 
43
        int rc = 0;
 
44
 
 
45
        if (size < 192) {
 
46
                dest[0] = size;
 
47
                (*packet_size_length) = 1;
 
48
        } else if (size < 65536) {
 
49
                dest[0] = (((size - 192) / 256) + 192);
 
50
                dest[1] = ((size - 192) % 256);
 
51
                (*packet_size_length) = 2;
 
52
        } else {
 
53
                rc = -EINVAL;
 
54
                syslog(LOG_ERR, "Unsupported packet size: [%d]\n",
 
55
                       size);
 
56
        }
 
57
        return rc;
 
58
}
 
59
 
 
60
/**
 
61
 * ecryptfs_parse_packet_length
 
62
 * @data: Pointer to memory containing length at offset
 
63
 * @size: This function writes the decoded size to this memory
 
64
 *        address; zero on error
 
65
 * @length_size: The number of bytes occupied by the encoded length
 
66
 *
 
67
 * Returns zero on success
 
68
 */
 
69
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
 
70
                                 size_t *length_size)
 
71
{
 
72
        int rc = 0;
 
73
 
 
74
        (*length_size) = 0;
 
75
        (*size) = 0;
 
76
        if (data[0] < 192) {
 
77
                /* One-byte length */
 
78
                (*size) = data[0];
 
79
                (*length_size) = 1;
 
80
        } else if (data[0] < 224) {
 
81
                /* Two-byte length */
 
82
                (*size) = ((data[0] - 192) * 256);
 
83
                (*size) += (data[1] + 192);
 
84
                (*length_size) = 2;
 
85
        } else if (data[0] == 255) {
 
86
                /* Five-byte length; we're not supposed to see this */
 
87
                rc = -EINVAL;
 
88
                syslog(LOG_ERR, "Five-byte packet length not "
 
89
                       "supported\n");
 
90
                goto out;
 
91
        } else {
 
92
                rc = -EINVAL;
 
93
                syslog(LOG_ERR, "Error parsing packet length\n");
 
94
                goto out;
 
95
        }
 
96
out:
 
97
        return rc;
 
98
}
 
99
 
 
100
/**
 
101
 * Called with mctx_mux held
 
102
 */
 
103
int ecryptfs_init_messaging(struct ecryptfs_messaging_ctx *mctx, uint32_t type)
 
104
{
 
105
        int rc = 0;
 
106
 
 
107
        memset(mctx, 0, sizeof(*mctx));
 
108
        switch (type) {
 
109
        case ECRYPTFS_MESSAGING_TYPE_NETLINK:
 
110
                mctx->type = ECRYPTFS_MESSAGING_TYPE_NETLINK;
 
111
                rc = ecryptfs_init_netlink(&mctx->ctx.nl_ctx);
 
112
                break;
 
113
        case ECRYPTFS_MESSAGING_TYPE_PROC:
 
114
                mctx->type = ECRYPTFS_MESSAGING_TYPE_PROC;
 
115
                rc = ecryptfs_init_proc(&mctx->ctx.proc_ctx);
 
116
                break;
 
117
        default:
 
118
                rc = -EINVAL;
 
119
                goto out;
 
120
        };
 
121
out:
 
122
        return rc;
 
123
}
 
124
 
 
125
int ecryptfs_messaging_exit(struct ecryptfs_messaging_ctx *mctx)
 
126
{
 
127
        int rc = 0;
 
128
 
 
129
        switch (mctx->type) {
 
130
        case ECRYPTFS_MESSAGING_TYPE_NETLINK:
 
131
                ecryptfs_release_netlink(&mctx->ctx.nl_ctx);
 
132
                break;
 
133
        case ECRYPTFS_MESSAGING_TYPE_PROC:
 
134
                ecryptfs_release_proc(&mctx->ctx.proc_ctx);
 
135
                break;
 
136
        default:
 
137
                rc = -EINVAL;
 
138
                goto out;
 
139
        };
 
140
out:
 
141
        return rc;
 
142
}
 
143
 
 
144
/**
 
145
 * ecryptfs_send_message
 
146
 * @mctx: Parent context for eCryptfs messaging with the kernel
 
147
 * @msg: Message to send (struct ecryptfs_message with data appended)
 
148
 * @msg_type: Message type to send
 
149
 * @msg_flags: Flags for sending message
 
150
 * @msg_seq: Message sequence number
 
151
 * 
 
152
 */
 
153
int ecryptfs_send_message(struct ecryptfs_messaging_ctx *mctx,
 
154
                          struct ecryptfs_message *msg,
 
155
                          unsigned char msg_type, uint16_t msg_flags,
 
156
                          uint32_t msg_seq)
 
157
{
 
158
        int rc = 0;
 
159
 
 
160
        switch (mctx->type) {
 
161
        case ECRYPTFS_MESSAGING_TYPE_NETLINK:
 
162
                rc = ecryptfs_send_netlink(&mctx->ctx.nl_ctx, msg, msg_type,
 
163
                                           msg_flags, msg_seq);
 
164
                if (rc) {
 
165
                        syslog(LOG_ERR, "%s: Failed to register netlink daemon "
 
166
                               "with the eCryptfs kernel module; rc = [%d]\n",
 
167
                               __FUNCTION__, rc);
 
168
 
 
169
                }
 
170
                break;
 
171
        case ECRYPTFS_MESSAGING_TYPE_PROC:
 
172
                rc = ecryptfs_send_proc(&mctx->ctx.proc_ctx, msg, msg_type,
 
173
                                        msg_flags, msg_seq);
 
174
                if (rc) {
 
175
                        syslog(LOG_ERR, "%s: Failed to register proc daemon "
 
176
                               "with the eCryptfs kernel module; rc = [%d]\n",
 
177
                               __FUNCTION__, rc);
 
178
                }
 
179
                break;
 
180
        default:
 
181
                rc = -EINVAL;
 
182
                goto out;
 
183
        };
 
184
out:
 
185
        return rc;
 
186
}
 
187
 
 
188
int ecryptfs_run_daemon(struct ecryptfs_messaging_ctx *mctx)
 
189
{
 
190
        int rc;
 
191
 
 
192
        switch (mctx->type) {
 
193
        case ECRYPTFS_MESSAGING_TYPE_NETLINK:
 
194
                rc = ecryptfs_run_netlink_daemon(&mctx->ctx.nl_ctx);
 
195
                if (rc)
 
196
                        goto out;
 
197
                break;
 
198
        case ECRYPTFS_MESSAGING_TYPE_PROC:
 
199
                rc = ecryptfs_run_proc_daemon(&mctx->ctx.proc_ctx);
 
200
                if (rc)
 
201
                        goto out;
 
202
                break;
 
203
        default:
 
204
                rc = -EINVAL;
 
205
                goto out;
 
206
        }
 
207
out:
 
208
        return rc;
 
209
}