1 /* Create simple DB database from textual input.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
36 #include <sys/param.h>
39 #include "nss_db/nss_db.h"
41 /* Get libc version number. */
42 #include "../version.h"
44 /* The hashing function we use. */
45 #include "../intl/hash-string.h"
47 /* SELinux support. */
49 # include <selinux/selinux.h>
53 # define MAP_POPULATE 0
56 #define PACKAGE _libc_intl_domainname
58 /* List of data bases. */
63 struct database
*next
;
72 static size_t ndatabases
;
73 static size_t nhashentries_total
;
74 static size_t valstrlen
;
75 static void *valstrtree
;
76 static char *valstrtab
;
77 static size_t extrastrlen
;
87 /* Stored string entry. */
96 /* True if any entry has been added. */
97 static bool any_dbentry
;
99 /* If non-zero convert key to lower case. */
100 static int to_lowercase
;
102 /* If non-zero print content of input file, one entry per line. */
105 /* If non-zero do not print informational messages. */
108 /* Name of output file. */
109 static const char *output_name
;
111 /* Name and version of program. */
112 static void print_version (FILE *stream
, struct argp_state
*state
);
113 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
115 /* Definitions of arguments for argp functions. */
116 static const struct argp_option options
[] =
118 { "fold-case", 'f', NULL
, 0, N_("Convert key to lower case") },
119 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
120 { "quiet", 'q', NULL
, 0,
121 N_("Do not print messages while building database") },
122 { "undo", 'u', NULL
, 0,
123 N_("Print content of database file, one entry a line") },
124 { "generated", 'g', N_("CHAR"), 0,
125 N_("Generated line not part of iteration") },
126 { NULL
, 0, NULL
, 0, NULL
}
129 /* Short description of program. */
130 static const char doc
[] = N_("Create simple database from textual input.");
132 /* Strings for arguments in help texts. */
133 static const char args_doc
[] = N_("\
134 INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE");
136 /* Prototype for option handler. */
137 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
139 /* Function to print some extra text in the help message. */
140 static char *more_help (int key
, const char *text
, void *input
);
142 /* Data structure to communicate with argp functions. */
143 static struct argp argp
=
145 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
149 /* List of databases which are not part of the iteration table. */
150 static struct db_option
153 struct db_option
*next
;
157 /* Prototypes for local functions. */
158 static int process_input (FILE *input
, const char *inname
,
159 int to_lowercase
, int be_quiet
);
160 static int print_database (int fd
);
161 static void compute_tables (void);
162 static int write_output (int fd
);
164 /* SELinux support. */
166 /* Set the SELinux file creation context for the given file. */
167 static void set_file_creation_context (const char *outname
, mode_t mode
);
168 static void reset_file_creation_context (void);
170 # define set_file_creation_context(_outname,_mode)
171 # define reset_file_creation_context()
175 /* External functions. */
176 #include <programs/xmalloc.h>
180 main (int argc
, char *argv
[])
182 const char *input_name
;
187 /* Set locale via LC_ALL. */
188 setlocale (LC_ALL
, "");
190 /* Set the text message domain. */
191 textdomain (_libc_intl_domainname
);
193 /* Initialize local variables. */
196 /* Parse and process arguments. */
197 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
199 /* Determine file names. */
200 if (do_undo
|| output_name
!= NULL
)
202 if (remaining
+ 1 != argc
)
205 error (0, 0, gettext ("wrong number of arguments"));
206 argp_help (&argp
, stdout
, ARGP_HELP_SEE
,
207 program_invocation_short_name
);
210 input_name
= argv
[remaining
];
214 if (remaining
+ 2 != argc
)
215 goto wrong_arguments
;
217 input_name
= argv
[remaining
++];
218 output_name
= argv
[remaining
];
221 /* Special handling if we are asked to print the database. */
224 int fd
= open (input_name
, O_RDONLY
);
226 error (EXIT_FAILURE
, errno
, gettext ("cannot open database file `%s'"),
229 int status
= print_database (fd
);
236 /* Open input file. */
237 if (strcmp (input_name
, "-") == 0 || strcmp (input_name
, "/dev/stdin") == 0)
243 input_file
= fopen64 (input_name
, "r");
244 if (input_file
== NULL
)
245 error (EXIT_FAILURE
, errno
, gettext ("cannot open input file `%s'"),
248 /* Get the access rights from the source file. The output file should
250 if (fstat64 (fileno (input_file
), &st
) >= 0)
251 mode
= st
.st_mode
& ACCESSPERMS
;
254 /* Start the real work. */
255 int status
= process_input (input_file
, input_name
, to_lowercase
, be_quiet
);
258 if (input_file
!= stdin
)
261 /* No need to continue when we did not read the file successfully. */
262 if (status
!= EXIT_SUCCESS
)
265 /* Bail out if nothing is to be done. */
271 error (EXIT_SUCCESS
, 0, gettext ("no entries to be processed"));
274 /* Compute hash and string tables. */
277 /* Open output file. This must not be standard output so we don't
278 handle "-" and "/dev/stdout" special. */
279 char *tmp_output_name
;
280 if (asprintf (&tmp_output_name
, "%s.XXXXXX", output_name
) == -1)
281 error (EXIT_FAILURE
, errno
, gettext ("cannot create temporary file name"));
283 set_file_creation_context (output_name
, mode
);
284 int fd
= mkstemp (tmp_output_name
);
285 reset_file_creation_context ();
287 error (EXIT_FAILURE
, errno
, gettext ("cannot create temporary file"));
289 status
= write_output (fd
);
291 if (status
== EXIT_SUCCESS
)
295 if (fstat64 (fd
, &st
) == 0)
297 if ((st
.st_mode
& ACCESSPERMS
) != mode
)
298 /* We ignore problems with changing the mode. */
303 error (0, errno
, gettext ("cannot stat newly created file"));
304 status
= EXIT_FAILURE
;
310 if (status
== EXIT_SUCCESS
)
312 if (rename (tmp_output_name
, output_name
) != 0)
314 error (0, errno
, gettext ("cannot rename temporary file"));
315 status
= EXIT_FAILURE
;
321 unlink (tmp_output_name
);
327 /* Handle program arguments. */
329 parse_opt (int key
, char *arg
, struct argp_state
*state
)
331 struct db_option
*newp
;
348 newp
= xmalloc (sizeof (*newp
));
350 newp
->next
= db_options
;
354 return ARGP_ERR_UNKNOWN
;
361 more_help (int key
, const char *text
, void *input
)
366 case ARGP_KEY_HELP_EXTRA
:
367 /* We print some extra information. */
368 if (asprintf (&tp
, gettext ("\
369 For bug reporting instructions, please see:\n\
370 %s.\n"), REPORT_BUGS_TO
) < 0)
376 return (char *) text
;
379 /* Print the version information. */
381 print_version (FILE *stream
, struct argp_state
*state
)
383 fprintf (stream
, "makedb %s%s\n", PKGVERSION
, VERSION
);
384 fprintf (stream
, gettext ("\
385 Copyright (C) %s Free Software Foundation, Inc.\n\
386 This is free software; see the source for copying conditions. There is NO\n\
387 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
389 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
394 dbentry_compare (const void *p1
, const void *p2
)
396 const struct dbentry
*d1
= (const struct dbentry
*) p1
;
397 const struct dbentry
*d2
= (const struct dbentry
*) p2
;
399 if (d1
->hashval
!= d2
->hashval
)
400 return d1
->hashval
< d2
->hashval
? -1 : 1;
402 return strcmp (d1
->str
, d2
->str
);
407 valstr_compare (const void *p1
, const void *p2
)
409 const struct valstrentry
*d1
= (const struct valstrentry
*) p1
;
410 const struct valstrentry
*d2
= (const struct valstrentry
*) p2
;
412 return strcmp (d1
->str
, d2
->str
);
417 process_input (input
, inname
, to_lowercase
, be_quiet
)
430 status
= EXIT_SUCCESS
;
433 struct database
*last_database
= NULL
;
435 while (!feof_unlocked (input
))
437 ssize_t n
= getline (&line
, &linelen
, input
);
439 /* This means end of file or some bug. */
442 /* Short read. Probably interrupted system call. */
447 if (line
[n
- 1] == '\n')
448 /* Remove trailing newline. */
452 while (isspace (*cp
))
455 if (*cp
== '#' || *cp
== '\0')
456 /* First non-space character in line '#': it's a comment.
457 Also go to the next line if it is empty except for whitespaces. */
460 /* Skip over the character indicating the database so that it is not
461 affected by TO_LOWERCASE. */
463 while (*cp
!= '\0' && !isspace (*cp
))
471 /* It's a line without a value field. */
475 size_t keylen
= cp
- key
;
477 while (isspace (*cp
))
481 size_t datalen
= (&line
[n
] - cp
) + 1;
483 /* Find the database. */
484 if (last_database
== NULL
|| last_database
->dbid
!= key
[0])
486 last_database
= databases
;
487 while (last_database
!= NULL
&& last_database
->dbid
!= key
[0])
488 last_database
= last_database
->next
;
490 if (last_database
== NULL
)
492 last_database
= xmalloc (sizeof (*last_database
));
493 last_database
->dbid
= key
[0];
494 last_database
->extra_string
= false;
495 last_database
->next
= databases
;
496 last_database
->entries
= NULL
;
497 last_database
->nentries
= 0;
498 last_database
->keystrlen
= 0;
499 databases
= last_database
;
501 struct db_option
*runp
= db_options
;
503 if (runp
->dbid
== key
[0])
505 last_database
->extra_string
= true;
513 /* Skip the database selector. */
517 /* Store the data. */
518 struct valstrentry
*nentry
= xmalloc (sizeof (struct valstrentry
)
520 if (last_database
->extra_string
)
521 nentry
->idx
= extrastrlen
;
523 nentry
->idx
= valstrlen
;
524 nentry
->extra_string
= last_database
->extra_string
;
525 memcpy (nentry
->str
, data
, datalen
);
527 struct valstrentry
**fdata
= tsearch (nentry
, &valstrtree
,
530 error (EXIT_FAILURE
, errno
, gettext ("cannot create search tree"));
532 if (*fdata
!= nentry
)
534 /* We can reuse a string. */
539 if (last_database
->extra_string
)
540 extrastrlen
+= datalen
;
542 valstrlen
+= datalen
;
545 struct dbentry
*newp
= xmalloc (sizeof (struct dbentry
) + keylen
);
546 newp
->validx
= nentry
->idx
;
547 newp
->hashval
= __hash_string (key
);
548 memcpy (newp
->str
, key
, keylen
);
550 struct dbentry
**found
= tsearch (newp
, &last_database
->entries
,
553 error (EXIT_FAILURE
, errno
, gettext ("cannot create search tree"));
559 error_at_line (0, 0, inname
, linenr
, gettext ("duplicate key"));
563 ++last_database
->nentries
;
564 last_database
->keystrlen
+= keylen
;
569 if (ferror_unlocked (input
))
571 error (0, 0, gettext ("problems while reading `%s'"), inname
);
572 status
= EXIT_FAILURE
;
580 copy_valstr (const void *nodep
, const VISIT which
, const int depth
)
582 if (which
!= leaf
&& which
!= postorder
)
585 const struct valstrentry
*p
= *(const struct valstrentry
**) nodep
;
587 strcpy (valstrtab
+ (p
->extra_string
? valstrlen
: 0) + p
->idx
, p
->str
);
591 /* Determine if the candidate is prime by using a modified trial division
592 algorithm. The candidate must be both odd and greater than 4. */
594 is_prime (size_t candidate
)
597 size_t sq
= divn
* divn
;
599 assert (candidate
> 4 && candidate
% 2 != 0);
601 while (sq
< candidate
&& candidate
% divn
!= 0)
608 return candidate
% divn
!= 0;
613 next_prime (size_t seed
)
615 /* Make sure that we're always greater than 4. */
616 seed
= (seed
+ 4) | 1;
618 while (!is_prime (seed
))
626 compute_tables (void)
628 valstrtab
= xmalloc (roundup (valstrlen
+ extrastrlen
, sizeof (stridx_t
)));
629 while ((valstrlen
+ extrastrlen
) % sizeof (stridx_t
) != 0)
630 valstrtab
[valstrlen
++] = '\0';
631 twalk (valstrtree
, copy_valstr
);
633 static struct database
*db
;
634 for (db
= databases
; db
!= NULL
; db
= db
->next
)
635 if (db
->nentries
!= 0)
639 /* We simply use an odd number large than twice the number of
640 elements to store in the hash table for the size. This gives
641 enough efficiency. */
642 #define TEST_RANGE 30
643 size_t nhashentries_min
= next_prime (db
->nentries
< TEST_RANGE
645 : db
->nentries
* 2 - TEST_RANGE
);
646 size_t nhashentries_max
= MAX (nhashentries_min
, db
->nentries
* 4);
647 size_t nhashentries_best
= nhashentries_min
;
648 size_t chainlength_best
= db
->nentries
;
650 db
->hashtable
= xmalloc (2 * nhashentries_max
* sizeof (stridx_t
)
652 db
->keyidxtab
= db
->hashtable
+ nhashentries_max
;
653 db
->keystrtab
= (char *) (db
->keyidxtab
+ nhashentries_max
);
655 static size_t max_chainlength
;
657 static size_t nhashentries
;
658 static bool copy_string
;
660 void add_key(const void *nodep
, const VISIT which
, const int depth
)
662 if (which
!= leaf
&& which
!= postorder
)
665 const struct dbentry
*dbe
= *(const struct dbentry
**) nodep
;
670 stridx
= wp
- db
->keystrtab
;
671 wp
= stpcpy (wp
, dbe
->str
) + 1;
676 size_t hidx
= dbe
->hashval
% nhashentries
;
677 size_t hval2
= 1 + dbe
->hashval
% (nhashentries
- 2);
678 size_t chainlength
= 0;
680 while (db
->hashtable
[hidx
] != ~((stridx_t
) 0))
683 if ((hidx
+= hval2
) >= nhashentries
)
684 hidx
-= nhashentries
;
687 db
->hashtable
[hidx
] = ((db
->extra_string
? valstrlen
: 0)
689 db
->keyidxtab
[hidx
] = stridx
;
691 max_chainlength
= MAX (max_chainlength
, chainlength
);
695 nhashentries
= nhashentries_min
;
696 for (size_t cnt
= 0; cnt
< TEST_RANGE
; ++cnt
)
698 memset (db
->hashtable
, '\xff', nhashentries
* sizeof (stridx_t
));
703 twalk (db
->entries
, add_key
);
705 if (max_chainlength
== 0)
707 /* No need to look further, this is as good as it gets. */
708 nhashentries_best
= nhashentries
;
712 if (max_chainlength
< chainlength_best
)
714 chainlength_best
= max_chainlength
;
715 nhashentries_best
= nhashentries
;
718 nhashentries
= next_prime (nhashentries
+ 1);
719 if (nhashentries
> nhashentries_max
)
723 /* Recompute the best table again, this time fill in the strings. */
724 nhashentries
= nhashentries_best
;
725 memset (db
->hashtable
, '\xff',
726 2 * nhashentries_max
* sizeof (stridx_t
));
730 twalk (db
->entries
, add_key
);
732 db
->nhashentries
= nhashentries_best
;
733 nhashentries_total
+= nhashentries_best
;
739 write_output (int fd
)
741 struct nss_db_header
*header
;
742 uint64_t file_offset
= (sizeof (struct nss_db_header
)
743 + (ndatabases
* sizeof (header
->dbs
[0])));
744 header
= alloca (file_offset
);
746 header
->magic
= NSS_DB_MAGIC
;
747 header
->ndbs
= ndatabases
;
748 header
->valstroffset
= file_offset
;
749 header
->valstrlen
= valstrlen
;
751 size_t filled_dbs
= 0;
752 struct iovec iov
[2 + ndatabases
* 3];
753 iov
[0].iov_base
= header
;
754 iov
[0].iov_len
= file_offset
;
756 iov
[1].iov_base
= valstrtab
;
757 iov
[1].iov_len
= valstrlen
+ extrastrlen
;
758 file_offset
+= iov
[1].iov_len
;
760 size_t keydataoffset
= file_offset
+ nhashentries_total
* sizeof (stridx_t
);
761 for (struct database
*db
= databases
; db
!= NULL
; db
= db
->next
)
762 if (db
->entries
!= NULL
)
764 assert (file_offset
% sizeof (stridx_t
) == 0);
765 assert (filled_dbs
< ndatabases
);
767 header
->dbs
[filled_dbs
].id
= db
->dbid
;
768 memset (header
->dbs
[filled_dbs
].pad
, '\0',
769 sizeof (header
->dbs
[0].pad
));
770 header
->dbs
[filled_dbs
].hashsize
= db
->nhashentries
;
772 iov
[2 + filled_dbs
].iov_base
= db
->hashtable
;
773 iov
[2 + filled_dbs
].iov_len
= db
->nhashentries
* sizeof (stridx_t
);
774 header
->dbs
[filled_dbs
].hashoffset
= file_offset
;
775 file_offset
+= iov
[2 + filled_dbs
].iov_len
;
777 iov
[2 + ndatabases
+ filled_dbs
* 2].iov_base
= db
->keyidxtab
;
778 iov
[2 + ndatabases
+ filled_dbs
* 2].iov_len
779 = db
->nhashentries
* sizeof (stridx_t
);
780 header
->dbs
[filled_dbs
].keyidxoffset
= keydataoffset
;
781 keydataoffset
+= iov
[2 + ndatabases
+ filled_dbs
* 2].iov_len
;
783 iov
[3 + ndatabases
+ filled_dbs
* 2].iov_base
= db
->keystrtab
;
784 iov
[3 + ndatabases
+ filled_dbs
* 2].iov_len
= db
->keystrlen
;
785 header
->dbs
[filled_dbs
].keystroffset
= keydataoffset
;
786 keydataoffset
+= iov
[3 + ndatabases
+ filled_dbs
* 2].iov_len
;
791 assert (filled_dbs
== ndatabases
);
792 assert (file_offset
== (iov
[0].iov_len
+ iov
[1].iov_len
793 + nhashentries_total
* sizeof (stridx_t
)));
794 header
->allocate
= file_offset
;
796 if (writev (fd
, iov
, 2 + ndatabases
* 3) != keydataoffset
)
798 error (0, errno
, gettext ("failed to write new database file"));
807 print_database (int fd
)
810 if (fstat64 (fd
, &st
) != 0)
811 error (EXIT_FAILURE
, errno
, gettext ("cannot stat database file"));
813 const struct nss_db_header
*header
= mmap (NULL
, st
.st_size
, PROT_READ
,
814 MAP_PRIVATE
|MAP_POPULATE
, fd
, 0);
815 if (header
== MAP_FAILED
)
816 error (EXIT_FAILURE
, errno
, gettext ("cannot map database file"));
818 if (header
->magic
!= NSS_DB_MAGIC
)
819 error (EXIT_FAILURE
, 0, gettext ("file not a database file"));
821 const char *valstrtab
= (const char *) header
+ header
->valstroffset
;
823 for (unsigned int dbidx
= 0; dbidx
< header
->ndbs
; ++dbidx
)
825 const stridx_t
*stridxtab
826 = ((const stridx_t
*) ((const char *) header
827 + header
->dbs
[dbidx
].keyidxoffset
));
828 const char *keystrtab
829 = (const char *) header
+ header
->dbs
[dbidx
].keystroffset
;
830 const stridx_t
*hashtab
831 = (const stridx_t
*) ((const char *) header
832 + header
->dbs
[dbidx
].hashoffset
);
834 for (uint32_t hidx
= 0; hidx
< header
->dbs
[dbidx
].hashsize
; ++hidx
)
835 if (hashtab
[hidx
] != ~((stridx_t
) 0))
837 header
->dbs
[dbidx
].id
,
838 keystrtab
+ stridxtab
[hidx
],
839 valstrtab
+ hashtab
[hidx
]);
848 set_file_creation_context (const char *outname
, mode_t mode
)
851 static int enforcing
;
852 security_context_t ctx
;
854 /* Check if SELinux is enabled, and remember. */
856 enabled
= is_selinux_enabled () ? 1 : -1;
860 /* Check if SELinux is enforcing, and remember. */
862 enforcing
= security_getenforce () ? 1 : -1;
864 /* Determine the context which the file should have. */
866 if (matchpathcon (outname
, S_IFREG
| mode
, &ctx
) == 0 && ctx
!= NULL
)
868 if (setfscreatecon (ctx
) != 0)
869 error (enforcing
> 0 ? EXIT_FAILURE
: 0, 0,
870 gettext ("cannot set file creation context for `%s'"),
878 reset_file_creation_context (void)
880 setfscreatecon (NULL
);