summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_repos/authz-test.c
blob: 6ee24485907cd5c42d40ec54cb3b0358163c374f (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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* authz-test.c --- tests for authorization system
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <apr_fnmatch.h>

#include "svn_pools.h"
#include "svn_iter.h"
#include "svn_hash.h"
#include "private/svn_subr_private.h"

#include "../../libsvn_repos/authz.h"

#include "../svn_test.h"

/* Used to terminate lines in large multi-line string literals. */
#define NL APR_EOL_STR

static svn_error_t *
print_group_member(void *baton,
                   const void *key, apr_ssize_t klen, void *val,
                   apr_pool_t *pool)
{
  svn_boolean_t *first = baton;
  const char *member = key;
  printf("%s%s", (*first ? "" : ", "), member);
  *first = FALSE;
  return SVN_NO_ERROR;
}


static svn_error_t *
print_group(void *baton,
            const void *key, apr_ssize_t klen, void *val,
            apr_pool_t *pool)
{
  const char *group = key;
  apr_hash_t *members = val;
  svn_boolean_t first = TRUE;
  svn_error_t *err;

  printf("   %s = ", group);
  err = svn_iter_apr_hash(NULL, members,
                          print_group_member, &first, pool);
  printf("\n");

  return err;
}


static const char *
access_string(authz_access_t access)
{
  switch (access & authz_access_write)
    {
    case authz_access_none: return ""; break;
    case authz_access_read_flag: return "r"; break;
    case authz_access_write_flag: return "w"; break;
    default:
      return "rw";
    }
}

static svn_error_t *
print_repos_rights(void *baton,
                   const void *key, apr_ssize_t klen,
                   void *val,
                   apr_pool_t *pool)
{
  const char *repos = key;
  authz_rights_t *rights = val;
  printf("      %s = all:%s  some:%s\n", repos,
         access_string(rights->min_access),
         access_string(rights->max_access));
  return SVN_NO_ERROR;
}

static svn_error_t *
print_user_rights(void *baton, const void *key, apr_ssize_t klen,
                  void *val,
                  apr_pool_t *pool)
{
  authz_global_rights_t *gr = val;

  printf("   %s\n", gr->user);
  printf("      [all] = all:%s  some:%s\n",
         access_string(gr->all_repos_rights.min_access),
         access_string(gr->all_repos_rights.max_access));
  printf("      [any] = all:%s  some:%s\n",
         access_string(gr->any_repos_rights.min_access),
         access_string(gr->any_repos_rights.max_access));
  SVN_ERR(svn_iter_apr_hash(NULL, gr->per_repos_rights,
                            print_repos_rights, NULL, pool));
  return SVN_NO_ERROR;
}

static const char*
rule_string(authz_rule_t* rule, apr_pool_t *pool)
{
  svn_stringbuf_t *str;
  int i;

  if (rule->len == 0)
    return "/";

  str = svn_stringbuf_create_empty(pool);

  for (i = 0; i < rule->len; ++i)
    {
      authz_rule_segment_t *segment = &rule->path[i];

      switch(segment->kind)
        {
        case authz_rule_any_segment:
          svn_stringbuf_appendcstr(str, "/*");
          break;

        case authz_rule_any_recursive:
          svn_stringbuf_appendcstr(str, "/**");
          break;

        case authz_rule_prefix:
          svn_stringbuf_appendcstr(str, "/#");
          svn_stringbuf_appendcstr(str, segment->pattern.data);
          svn_stringbuf_appendbyte(str, '*');
          break;

        case authz_rule_suffix:
          svn_stringbuf_appendcstr(str, "/#*");
          svn_stringbuf_appendcstr(str, segment->pattern.data);
          svn_authz__reverse_string(
              str->data + str->len - segment->pattern.len,
              segment->pattern.len);
          break;

        case authz_rule_fnmatch:
          svn_stringbuf_appendcstr(str, "/%");
          svn_stringbuf_appendcstr(str, segment->pattern.data);
          break;

        default:                /* literal */
          svn_stringbuf_appendcstr(str, "//");
          svn_stringbuf_appendcstr(str, segment->pattern.data);
        }
    }
  return str->data;
}


static svn_boolean_t
has_glob(authz_rule_t* rule)
{
  int i;
  for (i = 0; i < rule->len; ++i)
    {
      authz_rule_segment_t *segment = &rule->path[i];
      if (segment->kind != authz_rule_literal)
        return TRUE;
    }
  return FALSE;
}


static svn_error_t *
test_authz_parse(const svn_test_opts_t *opts,
                 apr_pool_t *pool)
{
  const char *srcdir;
  const char *rules_path;
  apr_file_t *rules_file;
  svn_stream_t *rules;
  const char *groups_path;
  apr_file_t *groups_file;
  svn_stream_t *groups;
  authz_full_t *authz;
  apr_hash_t *groupdefs = svn_hash__make(pool);
  int i;

  const char *check_user = "wunga";
  const char *check_repo = "bloop";
  authz_rights_t global_rights;
  svn_boolean_t global_explicit;


  SVN_ERR(svn_test_get_srcdir(&srcdir, opts, pool));
  rules_path = svn_dirent_join(srcdir, "authz.rules", pool);
  groups_path = svn_dirent_join(srcdir, "authz.groups", pool);

  SVN_ERR(svn_io_file_open(&rules_file, rules_path,
                           APR_READ, APR_OS_DEFAULT,
                           pool));
  rules = svn_stream_from_aprfile2(rules_file, FALSE, pool);
  SVN_ERR(svn_io_file_open(&groups_file, groups_path,
                           APR_READ, APR_OS_DEFAULT,
                           pool));
  groups = svn_stream_from_aprfile2(groups_file, FALSE, pool);
  SVN_ERR(svn_authz__parse(&authz, rules, groups, pool, pool));

  printf("Access check for ('%s', '%s')\n", check_user, check_repo);

  global_explicit = svn_authz__get_global_rights(&global_rights, authz,
                                                 check_user, check_repo);
  printf("Global rights: min=%s, max=%s (%s)\n\n",
         access_string(global_rights.min_access),
         access_string(global_rights.max_access),
         (global_explicit ? "explicit" : "implicit"));

  printf("[rules]\n");
  for (i = 0; i < authz->acls->nelts; ++i)
    {
      authz_acl_t *acl = &APR_ARRAY_IDX(authz->acls, i, authz_acl_t);
      const authz_access_t all_access =
        (acl->anon_access & acl->authn_access);
      authz_access_t access;
      svn_boolean_t has_access =
        svn_authz__get_acl_access(&access, acl, check_user, check_repo);
      int j;

      printf("%s%s%s   Sequence:   %d\n"
             "   Repository: [%s]\n"
             "   Rule:  %s[%s]\n",
             (has_access ? "Match = " : ""),
             (has_access ? access_string(access) : ""),
             (has_access ? "\n" : ""),
             acl->sequence_number,
             acl->rule.repos,
             (has_glob(&acl->rule) ? "glob:" : "     "),
             rule_string(&acl->rule, pool));

      if (acl->has_anon_access && acl->has_authn_access)
        printf("       * = %s\n", access_string(all_access));

      if (acl->has_anon_access
          && (acl->anon_access & ~all_access) != svn_authz_none)
        printf("       $anonymous = %s\n",
               access_string(acl->anon_access));

      if (acl->has_authn_access
          && (acl->authn_access & ~all_access) != svn_authz_none)
        printf("       $authenticated = %s\n",
               access_string(acl->authn_access));

      for (j = 0; j < acl->user_access->nelts; ++j)
        {
          authz_ace_t *ace = &APR_ARRAY_IDX(acl->user_access, j, authz_ace_t);
          printf("      %c%s = %s\n",
                 (ace->inverted ? '~' : ' '),
                 ace->name, access_string(ace->access));
          if (ace->members)
            svn_hash_sets(groupdefs, ace->name, ace->members);
        }
      printf("\n\n");
    }

  printf("[groups]\n");
  SVN_ERR(svn_iter_apr_hash(NULL, groupdefs,
                            print_group, NULL, pool));
  printf("\n\n");

  printf("[users]\n");
  if (authz->has_anon_rights)
    print_user_rights(NULL, NULL, 0, &authz->anon_rights, pool);
  if (authz->has_authn_rights)
    print_user_rights(NULL, NULL, 0, &authz->authn_rights, pool);
  SVN_ERR(svn_iter_apr_hash(NULL, authz->user_rights,
                            print_user_rights, NULL, pool));
  printf("\n\n");

  return SVN_NO_ERROR;
}

typedef struct global_right_text_case_t
{
  const char *repos;
  const char *user;
  authz_rights_t rights;
  svn_boolean_t found;
} global_right_text_case_t;

static svn_error_t *
run_global_rights_tests(const char *contents,
                        const global_right_text_case_t *test_cases,
                        apr_pool_t *pool)
{
  svn_authz_t *authz;

  svn_stringbuf_t *buffer = svn_stringbuf_create(contents, pool);
  svn_stream_t *stream = svn_stream_from_stringbuf(buffer, pool);
  SVN_ERR(svn_repos_authz_parse(&authz, stream, NULL, pool));

  for (; test_cases->repos; ++test_cases)
    {
      authz_rights_t rights = { authz_access_write, authz_access_none };
      svn_boolean_t found = svn_authz__get_global_rights(&rights, authz->full,
                                                         test_cases->user,
                                                         test_cases->repos);

      printf("%s %s {%d %d} %d => {%d %d} %d\n",
        test_cases->repos, test_cases->user,
        test_cases->rights.min_access, test_cases->rights.max_access,
        test_cases->found, rights.min_access, rights.max_access, found);
      SVN_TEST_ASSERT(found == test_cases->found);
      SVN_TEST_ASSERT(rights.min_access == test_cases->rights.min_access);
      SVN_TEST_ASSERT(rights.max_access == test_cases->rights.max_access);
    }

  return SVN_NO_ERROR;
}

static svn_error_t *
test_global_rights(apr_pool_t *pool)
{
  const char* authz1 =
    "[/public]"                                                          NL
    "* = r"                                                              NL
    ""                                                                   NL
    "[greek:/A]"                                                         NL
    "userA = rw"                                                         NL
    ""                                                                   NL
    "[repo:/A]"                                                          NL
    "userA = r"                                                          NL
    ""                                                                   NL
    "[repo:/B]"                                                          NL
    "userA = rw"                                                         NL
    ""                                                                   NL
    "[greek:/B]"                                                         NL
    "userB = rw"                                                         NL;

  const global_right_text_case_t test_cases1[] =
    {
      /* Everyone may get read access b/c there might be a "/public" path. */
      {      "",      "", { authz_access_none, authz_access_read  },  TRUE },
      {      "", "userA", { authz_access_none, authz_access_read  },  TRUE },
      {      "", "userB", { authz_access_none, authz_access_read  },  TRUE },
      {      "", "userC", { authz_access_none, authz_access_read  },  TRUE },

      /* Two users do even get write access on some paths in "greek".
       * The root always defaults to n/a due to the default rule. */
      { "greek",      "", { authz_access_none, authz_access_read  }, FALSE },
      { "greek", "userA", { authz_access_none, authz_access_write },  TRUE },
      { "greek", "userB", { authz_access_none, authz_access_write },  TRUE },
      { "greek", "userC", { authz_access_none, authz_access_read  }, FALSE },

      /* One users has write access to some paths in "repo". */
      {  "repo",      "", { authz_access_none, authz_access_read  }, FALSE },
      {  "repo", "userA", { authz_access_none, authz_access_write },  TRUE },
      {  "repo", "userB", { authz_access_none, authz_access_read  }, FALSE },
      {  "repo", "userC", { authz_access_none, authz_access_read  }, FALSE },

      /* For unknown repos, we default to the global settings. */
      {     "X",      "", { authz_access_none, authz_access_read  }, FALSE },
      {     "X", "userA", { authz_access_none, authz_access_read  }, FALSE },
      {     "X", "userB", { authz_access_none, authz_access_read  }, FALSE },
      {     "X", "userC", { authz_access_none, authz_access_read  }, FALSE },

      { NULL }
    };

  const char* authz2 =
    "[/]"                                                                NL
    "userA = r"                                                          NL
    ""                                                                   NL
    "[/public]"                                                          NL
    "userB = rw"                                                         NL
    ""                                                                   NL
    "[repo:/]"                                                           NL
    "userA = rw"                                                         NL;

  const global_right_text_case_t test_cases2[] =
    {
      /* Everyone may get read access b/c there might be a "/public" path. */
      {      "",      "", { authz_access_none, authz_access_none  },  TRUE },
      {      "", "userA", { authz_access_none, authz_access_read  },  TRUE },
      {      "", "userB", { authz_access_none, authz_access_write },  TRUE },
      {      "", "userC", { authz_access_none, authz_access_none  },  TRUE },

      /* Two users do even get write access on some paths in "greek".
       * The root always defaults to n/a due to the default rule. */
      { "greek",      "", { authz_access_none, authz_access_none  }, FALSE },
      { "greek", "userA", { authz_access_none, authz_access_read  }, FALSE },
      { "greek", "userB", { authz_access_none, authz_access_write }, FALSE },
      { "greek", "userC", { authz_access_none, authz_access_none  }, FALSE },

      { NULL }
    };

  const char* authz3 =
    "[/]"                                                                NL
    "userA = r"                                                          NL
    ""                                                                   NL
    "[greek:/public]"                                                    NL
    "userB = rw"                                                         NL
    ""                                                                   NL
    "[repo:/users]"                                                      NL
    "$authenticated = rw"                                                NL;

  const global_right_text_case_t test_cases3[] =
    {
      /* Everyone may get read access b/c there might be a "/public" path. */
      {      "",      "", { authz_access_none, authz_access_none  },  TRUE },
      {      "", "userA", { authz_access_none, authz_access_read  },  TRUE },
      {      "", "userB", { authz_access_none, authz_access_none  },  TRUE },
      {      "", "userC", { authz_access_none, authz_access_none  },  TRUE },

      /* Two users do even get write access on some paths in "greek".
       * The root always defaults to n/a due to the default rule. */
      { "greek",      "", { authz_access_none, authz_access_none  }, FALSE },
      { "greek", "userA", { authz_access_none, authz_access_read  }, FALSE },
      { "greek", "userB", { authz_access_none, authz_access_write },  TRUE },
      { "greek", "userC", { authz_access_none, authz_access_none  }, FALSE },

      /* Two users do even get write access on some paths in "greek".
       * The root always defaults to n/a due to the default rule. */
      {  "repo",      "", { authz_access_none, authz_access_none  }, FALSE },
      {  "repo", "userA", { authz_access_none, authz_access_write },  TRUE },
      {  "repo", "userB", { authz_access_none, authz_access_write },  TRUE },
      {  "repo", "userC", { authz_access_none, authz_access_write },  TRUE },

      { NULL }
    };

  SVN_ERR(run_global_rights_tests(authz1, test_cases1, pool));
  SVN_ERR(run_global_rights_tests(authz2, test_cases2, pool));
  SVN_ERR(run_global_rights_tests(authz3, test_cases3, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
issue_4741_groups(apr_pool_t *pool)
{
   const char rules[] =
     "[groups]"      NL
     "g1 = userA"    NL
     "g2 = userB"    NL
     "g = @g1, @g2"  NL
     ""              NL
     "[/]"           NL
     "* ="           NL
     "@g = rw"       NL
     ;

   svn_stringbuf_t *buf = svn_stringbuf_create(rules, pool);
   svn_stream_t *stream = svn_stream_from_stringbuf(buf, pool);
   svn_authz_t *authz;
   svn_boolean_t access_granted;

   SVN_ERR(svn_repos_authz_parse(&authz, stream, NULL, pool));

   SVN_ERR(svn_repos_authz_check_access(authz, "repo", "/", "userA",
                                        svn_authz_write, &access_granted,
                                        pool));
   SVN_TEST_ASSERT(access_granted == TRUE);

   SVN_ERR(svn_repos_authz_check_access(authz, "repo", "/", "userB",
                                        svn_authz_write, &access_granted,
                                        pool));
   SVN_TEST_ASSERT(access_granted == TRUE);

   return SVN_NO_ERROR;
}

static int max_threads = 4;

static struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_OPTS_PASS(test_authz_parse,
                       "test svn_authz__parse"),
    SVN_TEST_PASS2(test_global_rights,
                   "test svn_authz__get_global_rights"),
    SVN_TEST_PASS2(issue_4741_groups,
                   "issue 4741 groups"),
    SVN_TEST_NULL
  };

SVN_TEST_MAIN