File: inf-test-set-acl.c

package info (click to toggle)
libinfinity 0.6.5-1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 13,528 kB
  • sloc: ansic: 103,421; sh: 11,583; xml: 1,841; makefile: 1,059
file content (367 lines) | stat: -rw-r--r-- 9,407 bytes parent folder | download | duplicates (3)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* libinfinity - a GObject-based infinote implementation
 * Copyright (C) 2007-2014 Armin Burgmeier <armin@arbur.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <libinfinity/client/infc-browser.h>
#include <libinfinity/communication/inf-communication-manager.h>
#include <libinfinity/common/inf-xmpp-connection.h>
#include <libinfinity/common/inf-tcp-connection.h>
#include <libinfinity/common/inf-ip-address.h>
#include <libinfinity/common/inf-standalone-io.h>
#include <libinfinity/common/inf-request-result.h>
#include <libinfinity/common/inf-io.h>
#include <libinfinity/common/inf-protocol.h>
#include <libinfinity/common/inf-cert-util.h>
#include <libinfinity/common/inf-init.h>

#include <string.h>

typedef struct _InfTestSetAcl InfTestSetAcl;
struct _InfTestSetAcl {
  InfStandaloneIo* io;
  InfXmppConnection* conn;
  InfBrowser* browser;
};

static void
inf_test_set_acl_set_acl_finished_cb(InfRequest* request,
                                     const InfRequestResult* result,
                                     const GError* error,
                                     gpointer user_data)
{
  InfTestSetAcl* test;
  const InfAclSheetSet* sheet_set;
  const InfBrowserIter* iter;
  guint i;

  test = (InfTestSetAcl*)user_data;

  if(error != NULL)
  {
    fprintf(stderr, "Failed to change root node ACL: %s\n", error->message);
  }
  else
  {
    inf_request_result_get_set_acl(result, NULL, &iter);
    sheet_set = inf_browser_get_acl(test->browser, iter);
    fprintf(stderr, "New root node ACL:\n");
    for(i = 0; i < sheet_set->n_sheets; ++i)
    {
      fprintf(
        stderr,
        "  %s: mask=%llx, perms=%llx\n",
        inf_acl_account_id_to_string(sheet_set->sheets[i].account),
        (unsigned long long)sheet_set->sheets[i].mask.mask[0],
        (unsigned long long)sheet_set->sheets[i].perms.mask[0]
      );
    }
  }

  inf_xml_connection_close(INF_XML_CONNECTION(test->conn));
}

static void
inf_test_set_acl_query_acl_finished_cb(InfRequest* request,
                                       const InfRequestResult* result,
                                       const GError* error,
                                       gpointer user_data)
{
  InfTestSetAcl* test;
  InfAclSheetSet* sheet_set;
  const InfAclAccount* account;
  InfAclSheet* sheet;
  const InfBrowserIter* iter;
  guint i;

  test = (InfTestSetAcl*)user_data;

  if(error != NULL)
  {
    fprintf(stderr, "ACL query failed: %s\n", error->message);
    inf_xml_connection_close(INF_XML_CONNECTION(test->conn));
  }
  else
  {
    inf_request_result_get_query_acl(result, NULL, &iter, NULL);

    sheet_set =
      inf_acl_sheet_set_copy(inf_browser_get_acl(test->browser, iter));

    fprintf(stderr, "Root node ACL:\n");
    for(i = 0; i < sheet_set->n_sheets; ++i)
    {
      fprintf(
        stderr,
        "  %s: mask=%llx, perms=%llx\n",
        inf_acl_account_id_to_string(sheet_set->sheets[i].account),
        (unsigned long long)sheet_set->sheets[i].mask.mask[0],
        (unsigned long long)sheet_set->sheets[i].perms.mask[0]
      );
    }

    account = inf_browser_get_acl_default_account(test->browser);
    sheet = inf_acl_sheet_set_add_sheet(sheet_set, account->id);

    fprintf(stderr, "Requesting CAN_SET_ACL permission for the root node\n");
    inf_acl_mask_or1(&sheet->mask, INF_ACL_CAN_SET_ACL);
    inf_acl_mask_or1(&sheet->perms, INF_ACL_CAN_SET_ACL);

    inf_browser_set_acl(
      test->browser,
      iter,
      sheet_set,
      inf_test_set_acl_set_acl_finished_cb,
      test
    );

    inf_acl_sheet_set_free(sheet_set);
  }
}

static void
inf_test_set_acl_query_account_list_finished_cb(InfRequest* request,
                                                const InfRequestResult* res,
                                                const GError* error,
                                                gpointer user_data)
{
  InfTestSetAcl* test;
  const InfAclAccount* accounts;
  guint n_accounts;
  guint i;

  InfBrowserIter iter;

  test = (InfTestSetAcl*)user_data;

  if(error != NULL)
  {
    fprintf(stderr, "Account List query failed: %s\n", error->message);
  }
  else
  {
    inf_request_result_get_query_acl_account_list(
      res,
      NULL,
      &accounts,
      &n_accounts,
      NULL
    );

    printf("Account List:\n");
    for(i = 0; i < n_accounts; ++i)
    {
      printf(
        "  * %s (%s)\n",
        inf_acl_account_id_to_string(accounts[i].id),
        accounts[i].name ? accounts[i].name : "<no name>"
      );
    }
  }

  fprintf(stderr, "Querying root node ACL...\n");

  inf_browser_get_root(test->browser, &iter);

  inf_browser_query_acl(
    test->browser,
    &iter,
    inf_test_set_acl_query_acl_finished_cb,
    test
  );
}

static void
inf_test_set_acl_error_cb(InfcBrowser* browser,
                          GError* error,
                          gpointer user_data)
{
  fprintf(stderr, "Connection error: %s\n", error->message);
}

static void
inf_test_set_acl_notify_status_cb(GObject* object,
                                  GParamSpec* pspec,
                                  gpointer user_data)
{
  InfTestSetAcl* test;
  InfBrowserStatus status;
  const InfAclAccount* account;

  test = (InfTestSetAcl*)user_data;
  g_object_get(G_OBJECT(test->browser), "status", &status, NULL);

  if(status == INF_BROWSER_OPEN)
  {
    account = inf_browser_get_acl_local_account(test->browser);

    fprintf(stderr, "Connection established, querying account list...\n");

    fprintf(
      stderr,
      "Local account: %s (%s)\n",
      inf_acl_account_id_to_string(account->id),
      account->name
    );

    inf_browser_query_acl_account_list(
      test->browser,
      inf_test_set_acl_query_account_list_finished_cb,
      test
    );
  }
  else if(status == INF_BROWSER_CLOSED)
  {
    if(inf_standalone_io_loop_running(test->io))
      inf_standalone_io_loop_quit(test->io);
  }
}

int
main(int argc, char* argv[])
{
  InfTestSetAcl test;
  InfIpAddress* address;
  InfCommunicationManager* manager;
  InfTcpConnection* tcp_conn;
  GPtrArray* array;
  gnutls_x509_privkey_t key;
  gnutls_x509_crt_t* certs;
  guint n_certs;
  guint i;
  InfCertificateCredentials* creds;
  GError* error;

  key = NULL;
  certs = NULL;
  n_certs = 0;
  creds = NULL;

  error = NULL;
  inf_init(NULL);

  test.io = inf_standalone_io_new();
  address = inf_ip_address_new_loopback4();

  tcp_conn = inf_tcp_connection_new_and_open(
    INF_IO(test.io),
    address,
    inf_protocol_get_default_port(),
    &error
  );

  inf_ip_address_free(address);

  if(tcp_conn == NULL)
  {
    fprintf(stderr, "Could not open TCP connection: %s\n", error->message);
    g_error_free(error);
    return EXIT_FAILURE;
  }

  if(argc >= 2)
  {
    array = inf_cert_util_read_certificate(argv[1], NULL, &error);

    if(error != NULL)
    {
      fprintf(stderr, "Failed to read certificate: %s\n", error->message);
      g_error_free(error);
      return EXIT_FAILURE;
    }

    n_certs = array->len;
    certs = (gnutls_x509_crt_t*)g_ptr_array_free(array, FALSE);

    if(n_certs == 0)
    {
      fprintf(stderr, "File does not contain a certificate\n");
      return EXIT_FAILURE;
    }

    key = inf_cert_util_read_private_key(argv[1], &error);

    if(error != NULL)
    {
      fprintf(stderr, "Failed to read key: %s\n", error->message);
      g_error_free(error);
      return EXIT_FAILURE;
    }

    creds = inf_certificate_credentials_new();

    gnutls_certificate_set_x509_key(
      inf_certificate_credentials_get(creds),
      certs,
      n_certs,
      key
    );
  }

  test.conn = inf_xmpp_connection_new(
    tcp_conn,
    INF_XMPP_CONNECTION_CLIENT,
    NULL,
    "localhost",
    INF_XMPP_CONNECTION_SECURITY_BOTH_PREFER_TLS,
    creds,
    NULL,
    NULL
  );

  g_object_unref(tcp_conn);

  manager = inf_communication_manager_new();
  test.browser = INF_BROWSER(
    infc_browser_new(
      INF_IO(test.io),
      manager,
      INF_XML_CONNECTION(test.conn)
    )
  );

  g_signal_connect_after(
    G_OBJECT(test.browser),
    "notify::status",
    G_CALLBACK(inf_test_set_acl_notify_status_cb),
    &test
  );

  g_signal_connect(
    G_OBJECT(test.browser),
    "error",
    G_CALLBACK(inf_test_set_acl_error_cb),
    &test
  );

  inf_standalone_io_loop(test.io);
  g_object_unref(manager);
  g_object_unref(test.browser);
  /* TODO: Wait until the XMPP connection is in status closed */
  g_object_unref(test.conn);

  if(creds != NULL) inf_certificate_credentials_unref(creds);
  for(i = 0; i < n_certs; ++i)
    gnutls_x509_crt_deinit(certs[i]);
  g_free(certs);
  if(key != NULL) gnutls_x509_privkey_deinit(key);

  g_object_unref(test.io);
  return 0;
}

/* vim:set et sw=2 ts=2: */