~ubuntu-branches/ubuntu/trusty/libnss-db/trusty-proposed

« back to all changes in this revision

Viewing changes to src/makedb.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-10-05 00:29:58 UTC
  • Revision ID: package-import@ubuntu.com-20121005002958-krtscomqp8o3a3bp
Tags: 2.2.3pre1-5
* QA upload.
* Remove old debian/packages file, unused since conversion from yada.
* Convert to source format 3.0 (quilt) (closes: #679676).  Add po/Makevars
  to 080-translations.patch.  Refresh all patches.
* Use dh_installman to install makedb.1.
* Add debhelper token to libnss-db.postinst.
* Link to a versioned copy of the GPL in debian/copyright.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <string.h>
37
37
#include <sys/stat.h>
38
38
 
 
39
#ifdef SELINUX
 
40
#include <selinux/selinux.h>
 
41
#endif
 
42
 
39
43
#include "db-compat.h"
40
44
 
41
45
#define N_(Text) Text
99
103
                          int to_lowercase, int be_quiet);
100
104
static int print_database (DB *db);
101
105
 
 
106
#ifdef SELINUX
 
107
/* Set the SELinux file creation context for the given file. */
 
108
static void set_file_creation_context (const char *outname, mode_t mode);
 
109
#else
 
110
#define set_file_creation_context(_outname,_mode)
 
111
#endif
102
112
 
103
113
int
104
114
main (int argc, char *argv[])
181
191
 
182
192
  /* Open output file.  This must not be standard output so we don't
183
193
     handle "-" and "/dev/stdout" special.  */
 
194
  set_file_creation_context (output_name, mode);
184
195
  status = db_open (output_name, DB_BTREE, DB_CREATE | DB_TRUNCATE, mode,
185
196
                    NULL, NULL, &db_file);
 
197
  set_file_creation_context (NULL, 0);
186
198
  if (status)
187
199
    error (EXIT_FAILURE, 0, gettext ("cannot open output file `%s': %s"),
188
200
           output_name, db_strerror (status));
393
405
 
394
406
  return EXIT_SUCCESS;
395
407
}
 
408
 
 
409
 
 
410
#ifdef SELINUX
 
411
static void
 
412
set_file_creation_context (const char *outname, mode_t mode)
 
413
{
 
414
  static int enabled = -1, enforcing = -1;
 
415
  security_context_t ctx;
 
416
  /* Handle the "reset the context" case. */
 
417
  if (outname == NULL)
 
418
    {
 
419
      setfscreatecon (NULL);
 
420
      return;
 
421
    }
 
422
  /* Check if SELinux is enabled, and remember. */
 
423
  if (enabled == -1)
 
424
    {
 
425
      enabled = is_selinux_enabled ();
 
426
    }
 
427
  if (enabled == 0)
 
428
    {
 
429
      return;
 
430
    }
 
431
  /* Check if SELinux is enforcing, and remember. */
 
432
  if (enforcing == -1)
 
433
    {
 
434
      enforcing = security_getenforce();
 
435
    }
 
436
  /* Determine the context which the file should have. */
 
437
  ctx = NULL;
 
438
  if ((matchpathcon (outname, S_IFREG | mode, &ctx) == 0) &&
 
439
      (ctx != NULL))
 
440
    {
 
441
      if (setfscreatecon (ctx) != 0)
 
442
        {
 
443
          if (enforcing)
 
444
            {
 
445
              error (EXIT_FAILURE, 0,
 
446
                     gettext ("cannot set file creation context for `%s'"),
 
447
                     outname);
 
448
            }
 
449
          else
 
450
            {
 
451
              error (0, 0,
 
452
                     gettext ("cannot set file creation context for `%s'"),
 
453
                     outname);
 
454
            }
 
455
        }
 
456
      freecon (ctx);
 
457
    }
 
458
}
 
459
#endif