summaryrefslogtreecommitdiff
path: root/dbus/kdbus-common.c
blob: a55be2703374a36425cf1c5b30ebea25f901e3fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* kdbus-common.c  kdbus related utils for daemon and libdbus
 *
 * Copyright (C) 2013  Samsung Electronics
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version and under the terms of the GNU
 * Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#include "kdbus.h"
#include "kdbus-common.h"
#include "dbus-transport-kdbus.h"
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-shared.h>

static struct kdbus_item *make_policy_name(const char *name)
{
  struct kdbus_item *p;
  __u64 size;

  size = offsetof(struct kdbus_item, policy.name) + strlen(name) + 1;
  p = malloc(size);
  if (!p)
	  return NULL;
  memset(p, 0, size);
  p->size = size;
  p->type = KDBUS_ITEM_POLICY_NAME;
  memcpy(p->policy.name, name, strlen(name) + 1);

  return p;
}

static  struct kdbus_item *make_policy_access(__u64 type, __u64 bits, __u64 id)
{
  struct kdbus_item *p;
  __u64 size = sizeof(*p);

  p = malloc(size);
  if (!p)
	  return NULL;

  memset(p, 0, size);
  p->size = size;
  p->type = KDBUS_ITEM_POLICY_ACCESS;
  p->policy.access.type = type;
  p->policy.access.bits = bits;
  p->policy.access.id = id;

  return p;
}

static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_item *policy, __u64 max_size)
{
  struct kdbus_item *dst = (struct kdbus_item *) ((char *) cmd_policy + cmd_policy->size);

  if (cmd_policy->size + policy->size > max_size)
	  return;

  memcpy(dst, policy, policy->size);
  cmd_policy->size += KDBUS_ALIGN8(policy->size);
  free(policy);
}

/**
 * Registers kdbus policy for given connection.
 *
 * Policy sets rights of the name (unique or well known) on the bus. Without policy it is
 * not possible to send or receive messages. It must be set separately for unique id and
 * well known name of the connection. It is set after registering on the bus, but before
 * requesting for name. The policy is valid for the given name, not for the connection.
 *
 * Name of the policy equals name on the bus.
 *
 * @param name name of the policy = name of the connection
 * @param transport - transport
 * @param owner_uid - uid or euid of the process being owner of the name
 *
 * @returns #TRUE on success
 */
dbus_bool_t register_kdbus_policy(const char* name, DBusTransport *transport, unsigned long int owner_uid)
{
  struct kdbus_cmd_policy *cmd_policy;
  struct kdbus_item *policy;
  int size = 0xffff;
  int fd;

  if(!_dbus_transport_get_socket_fd (transport, &fd))
    return FALSE;

  cmd_policy = alloca(size);
  memset(cmd_policy, 0, size);

  policy = (struct kdbus_item *) cmd_policy->policies;
  cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);

  policy = make_policy_name(name);
  append_policy(cmd_policy, policy, size);

  policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, owner_uid);
  append_policy(cmd_policy, policy, size);

  policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
  append_policy(cmd_policy, policy, size);

  policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
  append_policy(cmd_policy, policy, size);

  if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
    {
      _dbus_verbose ("Error setting policy: %m, %d\n", errno);
      return FALSE;
    }

  _dbus_verbose("Policy %s set correctly\n", name);
  return TRUE;
}

/**
 *
 * Asks the bus to assign the given name to the connection.
 *
 * Use same flags as original dbus version with one exception below.
 * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
 * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
 * is returned by kdbus.
 *
 * @param fd - file descriptor of the connection
 * @param name the name to request
 * @param flags flags
 * @param id unique id of the connection for which the name is being registered
 * @returns a DBus result code on success, -errno on error
 */
int request_kdbus_name(int fd, const char *name, const __u64 flags, __u64 id)
{
  struct kdbus_cmd_name *cmd_name;

  __u64 size = sizeof(*cmd_name) + strlen(name) + 1;
  __u64 flags_kdbus = 0;

  cmd_name = alloca(size);

  strcpy(cmd_name->name, name);
  cmd_name->size = size;

  if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
    flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
  if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
    flags_kdbus |= KDBUS_NAME_QUEUE;
  if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
    flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
  if(flags & KDBUS_NAME_STARTER_NAME)
    flags_kdbus |= KDBUS_NAME_STARTER_NAME;

  cmd_name->flags = flags_kdbus;
  cmd_name->id = id;

  _dbus_verbose("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->flags);

  if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
    {
      _dbus_verbose ("error acquiring name '%s': %m, %d\n", name, errno);
      if(errno == EEXIST)
        return DBUS_REQUEST_NAME_REPLY_EXISTS;
      if(errno == EALREADY)
        return DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
      return -errno;
    }

  _dbus_verbose("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->flags);

  if(cmd_name->flags & KDBUS_NAME_IN_QUEUE)
    return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
  else
    return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
}

/**
 *
 * Releases well-known name - the connections resign from the name
 * which can be then assigned to another connection or the connection
 * is being removed from the queue for that name
 *
 * @param fd - file descriptor of the connection
 * @param name the name to request
 * @param id unique id of the connection for which the name is being released
 * @returns a DBus result code on success, -errno on error
 */
int release_kdbus_name(int fd, const char *name, __u64 id)
{
  struct kdbus_cmd_name *cmd_name;

  __u64 size = sizeof(*cmd_name) + strlen(name) + 1;

  cmd_name = alloca(size);
  cmd_name->id = id;
  strcpy(cmd_name->name, name);
  cmd_name->size = size;

  if (ioctl(fd, KDBUS_CMD_NAME_RELEASE, cmd_name))
    {
      if(errno == ESRCH)
        return DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
      else if (errno == EPERM)
        return DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
      _dbus_verbose ("error releasing name '%s' for id:%llu. Error: %m, %d\n", name, (unsigned long long)id, errno);
      return -errno;
    }

  _dbus_verbose("Name '%s' released\n", name);

  return DBUS_RELEASE_NAME_REPLY_RELEASED;
}