2 * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2001, Marek Michałkiewicz
4 * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2009, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 #include <selinux/selinux.h>
51 #include "prototypes.h"
54 /* local function prototypes */
55 static int lrename (const char *, const char *);
56 static int check_link_count (const char *file);
57 static int do_lock_file (const char *file, const char *lock);
58 static /*@null@*/ /*@dependent@*/FILE *fopen_set_perms (
61 const struct stat *sb);
62 static int create_backup (const char *, FILE *);
63 static void free_linked_list (struct commonio_db *);
64 static void add_one_entry (
65 struct commonio_db *db,
66 /*@owned@*/struct commonio_entry *p);
67 static bool name_is_nis (const char *name);
68 static int write_all (const struct commonio_db *);
69 static /*@dependent@*/ /*@null@*/struct commonio_entry *find_entry_by_name (
72 static /*@dependent@*/ /*@null@*/struct commonio_entry *next_entry_by_name (
74 /*@null@*/struct commonio_entry *pos,
77 static int lock_count = 0;
78 static bool nscd_need_reload = false;
81 * Simple rename(P) alternative that attempts to rename to symlink
84 int lrename (const char *old, const char *new)
87 char resolved_path[PATH_MAX];
92 if (lstat (new, &sb) == 0 && S_ISLNK (sb.st_mode)) {
93 if (realpath (new, resolved_path) == NULL) {
94 perror ("realpath in lrename()");
100 res = rename (old, new);
104 static int check_link_count (const char *file)
108 if (stat (file, &sb) != 0) {
112 if (sb.st_nlink != 2) {
120 static int do_lock_file (const char *file, const char *lock)
128 fd = open (file, O_CREAT | O_EXCL | O_WRONLY, 0600);
134 snprintf (buf, sizeof buf, "%lu", (unsigned long) pid);
135 len = (ssize_t) strlen (buf) + 1;
136 if (write (fd, buf, (size_t) len) != len) {
143 if (link (file, lock) == 0) {
144 retval = check_link_count (file);
149 fd = open (lock, O_RDWR);
155 len = read (fd, buf, sizeof (buf) - 1);
163 if (get_pid (buf, &pid) == 0) {
168 if (kill (pid, 0) == 0) {
173 if (unlink (lock) != 0) {
179 if ((link (file, lock) == 0) && (check_link_count (file) != 0)) {
188 static /*@null@*/ /*@dependent@*/FILE *fopen_set_perms (
191 const struct stat *sb)
197 fp = fopen (name, mode);
204 if (fchown (fileno (fp), sb->st_uid, sb->st_gid) != 0) {
208 if (chown (name, sb->st_mode) != 0) {
214 if (fchmod (fileno (fp), sb->st_mode & 0664) != 0) {
218 if (chmod (name, sb->st_mode & 0664) != 0) {
231 static int create_backup (const char *backup, FILE * fp)
239 if (fstat (fileno (fp), &sb) != 0) {
244 bkfp = fopen (backup, "w");
250 /* TODO: faster copy, not one-char-at-a-time. --marekm */
252 if (fseek (fp, 0, SEEK_SET) == 0) {
253 while ((c = getc (fp)) != EOF) {
254 if (putc (c, bkfp) == EOF) {
259 if ((c != EOF) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
263 if ( (fsync (fileno (bkfp)) != 0)
264 || (fclose (bkfp) != 0)) {
268 ub.actime = sb.st_atime;
269 ub.modtime = sb.st_mtime;
270 (void) utime (backup, &ub);
275 static void free_linked_list (struct commonio_db *db)
277 struct commonio_entry *p;
279 while (NULL != db->head) {
283 if (NULL != p->line) {
287 if (NULL != p->eptr) {
288 db->ops->free (p->eptr);
297 int commonio_setname (struct commonio_db *db, const char *name)
299 snprintf (db->filename, sizeof (db->filename), "%s", name);
304 bool commonio_present (const struct commonio_db *db)
306 return (access (db->filename, F_OK) == 0);
310 int commonio_lock_nowait (struct commonio_db *db)
319 snprintf (file, sizeof file, "%s.%lu",
320 db->filename, (unsigned long) getpid ());
321 snprintf (lock, sizeof lock, "%s.lock", db->filename);
322 if (do_lock_file (file, lock) != 0) {
331 int commonio_lock (struct commonio_db *db)
335 * only if the system libc has a real lckpwdf() - the one from
336 * lockpw.c calls us and would cause infinite recursion!
340 * Call lckpwdf() on the first lock.
341 * If it succeeds, call *_lock() only once
342 * (no retries, it should always succeed).
344 if (0 == lock_count) {
345 if (lckpwdf () == -1) {
346 return 0; /* failure */
350 if (commonio_lock_nowait (db) != 0) {
351 return 1; /* success */
355 return 0; /* failure */
360 * lckpwdf() not used - do it the old way.
363 #define LOCK_TRIES 15
369 for (i = 0; i < LOCK_TRIES; i++) {
371 sleep (LOCK_SLEEP); /* delay between retries */
373 if (commonio_lock_nowait (db) != 0) {
374 return 1; /* success */
376 /* no unnecessary retries on "permission denied" errors */
377 if (geteuid () != 0) {
381 return 0; /* failure */
385 static void dec_lock_count (void)
387 if (lock_count > 0) {
389 if (lock_count == 0) {
390 /* Tell nscd when lock count goes to zero,
391 if any of the files were changed. */
392 if (nscd_need_reload) {
393 nscd_flush_cache ("passwd");
394 nscd_flush_cache ("group");
395 nscd_need_reload = false;
405 int commonio_unlock (struct commonio_db *db)
411 if (commonio_close (db) == 0) {
420 * Unlock in reverse order: remove the lock file,
421 * then call ulckpwdf() (if used) on last unlock.
424 snprintf (lock, sizeof lock, "%s.lock", db->filename);
434 * Add an entry at the end.
436 * defines p->next, p->prev
437 * (unfortunately, owned special are not supported)
439 static void add_one_entry (struct commonio_db *db,
440 /*@owned@*/struct commonio_entry *p)
446 if (NULL == db->head) {
449 if (NULL != db->tail) {
456 static bool name_is_nis (const char *name)
458 return (('+' == name[0]) || ('-' == name[0]));
463 * New entries are inserted before the first NIS entry. Order is preserved
464 * when db is written out.
466 #ifndef KEEP_NIS_AT_END
467 #define KEEP_NIS_AT_END 1
471 static void add_one_entry_nis (struct commonio_db *db,
472 /*@owned@*/struct commonio_entry *newp);
475 * Insert an entry between the regular entries, and the NIS entries.
477 * defines newp->next, newp->prev
478 * (unfortunately, owned special are not supported)
480 static void add_one_entry_nis (struct commonio_db *db,
481 /*@owned@*/struct commonio_entry *newp)
483 struct commonio_entry *p;
485 for (p = db->head; NULL != p; p = p->next) {
486 if (name_is_nis (p->eptr ? db->ops->getname (p->eptr)
490 newp->prev = p->prev;
492 if (NULL != p->prev) {
493 p->prev->next = newp;
501 add_one_entry (db, newp);
503 #endif /* KEEP_NIS_AT_END */
505 /* Initial buffer size, as well as increment if not sufficient
506 (for reading very long lines in group files). */
509 int commonio_open (struct commonio_db *db, int mode)
514 struct commonio_entry *p;
523 || ( (O_RDONLY != mode)
524 && (O_RDWR != mode))) {
528 db->readonly = (mode == O_RDONLY);
529 if (!db->readonly && !db->locked) {
534 db->head = db->tail = NULL;
538 db->fp = fopen (db->filename, db->readonly ? "r" : "r+");
541 * If O_CREAT was specified and the file didn't exist, it will be
542 * created by commonio_close(). We have no entries to read yet. --marekm
544 if (NULL == db->fp) {
545 if (((flags & O_CREAT) != 0) && (ENOENT == errno)) {
552 /* Do not inherit fd in spawned processes (e.g. nscd) */
553 fcntl(fileno(db->fp), F_SETFD, FD_CLOEXEC);
557 if ((is_selinux_enabled () > 0) && (!db->readonly)) {
558 if (fgetfilecon (fileno (db->fp), &db->scontext) < 0) {
565 buf = (char *) malloc (buflen);
570 while (db->ops->fgets (buf, (int) buflen, db->fp) == buf) {
571 while ( ((cp = strrchr (buf, '\n')) == NULL)
572 && (feof (db->fp) == 0)) {
576 cp = (char *) realloc (buf, buflen);
582 if (db->ops->fgets (buf + len,
583 (int) (buflen - len),
588 cp = strrchr (buf, '\n');
598 if (name_is_nis (line)) {
601 eptr = db->ops->parse (line);
603 eptr = db->ops->dup (eptr);
610 p = (struct commonio_entry *) malloc (sizeof *p);
619 add_one_entry (db, p);
624 if (ferror (db->fp) != 0) {
628 if ((NULL != db->ops->open_hook) && (db->ops->open_hook () == 0)) {
637 db->ops->free (eptr);
647 free_linked_list (db);
649 if (db->scontext != NULL) {
650 freecon (db->scontext);
661 * Sort given db according to cmp function (usually compares uids)
664 commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
666 struct commonio_entry **entries, *ptr;
669 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
677 entries = malloc (n * sizeof (struct commonio_entry *));
678 if (entries == NULL) {
683 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
686 qsort (entries, n, sizeof (struct commonio_entry *), cmp);
688 db->head = entries[0];
689 db->tail = entries[--n];
690 db->head->prev = NULL;
691 db->head->next = entries[1];
692 db->tail->prev = entries[n - 1];
693 db->tail->next = NULL;
695 for (i = 1; i < n; i++) {
696 entries[i]->prev = entries[i - 1];
697 entries[i]->next = entries[i + 1];
707 * Sort entries in db according to order in another.
709 int commonio_sort_wrt (struct commonio_db *shadow, struct commonio_db *passwd)
711 struct commonio_entry *head = NULL, *pw_ptr, *spw_ptr;
714 if ((NULL == shadow) || (NULL == shadow->head)) {
718 for (pw_ptr = passwd->head; NULL != pw_ptr; pw_ptr = pw_ptr->next) {
719 if (NULL == pw_ptr->eptr) {
722 name = passwd->ops->getname (pw_ptr->eptr);
723 for (spw_ptr = shadow->head;
725 spw_ptr = spw_ptr->next) {
726 if (NULL == spw_ptr->eptr) {
729 if (strcmp (name, shadow->ops->getname (spw_ptr->eptr))
734 if (NULL == spw_ptr) {
737 commonio_del_entry (shadow, spw_ptr);
738 spw_ptr->next = head;
742 for (spw_ptr = head; NULL != spw_ptr; spw_ptr = head) {
745 if (NULL != shadow->head) {
746 shadow->head->prev = spw_ptr;
748 spw_ptr->next = shadow->head;
749 shadow->head = spw_ptr;
752 shadow->head->prev = NULL;
753 shadow->changed = true;
759 * write_all - Write the database to its file.
761 * It returns 0 if all the entries could be written correctly.
763 static int write_all (const struct commonio_db *db)
764 /*@requires notnull db->fp@*/
766 const struct commonio_entry *p;
769 for (p = db->head; NULL != p; p = p->next) {
772 assert (NULL != eptr);
773 if (db->ops->put (eptr, db->fp) != 0) {
776 } else if (NULL != p->line) {
777 if (db->ops->fputs (p->line, db->fp) == EOF) {
780 if (putc ('\n', db->fp) == EOF) {
789 int commonio_close (struct commonio_db *db)
790 /*@requires notnull db->fp@*/
797 /*@null@*/security_context_t old_context = NULL;
806 if (!db->changed || db->readonly) {
812 if ((NULL != db->ops->close_hook) && (db->ops->close_hook () == 0)) {
816 memzero (&sb, sizeof sb);
817 if (NULL != db->fp) {
818 if (fstat (fileno (db->fp), &sb) != 0) {
824 if (db->scontext != NULL) {
825 if (getfscreatecon (&old_context) < 0) {
829 if (setfscreatecon (db->scontext) < 0) {
836 * Create backup file.
838 snprintf (buf, sizeof buf, "%s-", db->filename);
840 if (create_backup (buf, db->fp) != 0) {
844 if (fclose (db->fp) != 0) {
854 * Default permissions for new [g]shadow files.
855 * (passwd and group always exist...)
862 snprintf (buf, sizeof buf, "%s+", db->filename);
864 db->fp = fopen_set_perms (buf, "w", &sb);
865 if (NULL == db->fp) {
869 if (write_all (db) != 0) {
873 if (fflush (db->fp) != 0) {
877 if (fsync (fileno (db->fp)) != 0) {
883 if (fclose (db->fp) != 0) {
894 if (lrename (buf, db->filename) != 0) {
898 nscd_need_reload = true;
905 if (db->scontext != NULL) {
906 if (NULL != old_context) {
907 if (setfscreatecon (old_context) < 0) {
910 freecon (old_context);
913 freecon (db->scontext);
917 free_linked_list (db);
921 static /*@dependent@*/ /*@null@*/struct commonio_entry *next_entry_by_name (
922 struct commonio_db *db,
923 /*@null@*/struct commonio_entry *pos,
926 struct commonio_entry *p;
933 for (p = pos; NULL != p; p = p->next) {
936 && (strcmp (db->ops->getname (ep), name) == 0)) {
943 static /*@dependent@*/ /*@null@*/struct commonio_entry *find_entry_by_name (
944 struct commonio_db *db,
947 return next_entry_by_name(db, db->head, name);
951 int commonio_update (struct commonio_db *db, const void *eptr)
953 struct commonio_entry *p;
956 if (!db->isopen || db->readonly) {
960 nentry = db->ops->dup (eptr);
961 if (NULL == nentry) {
965 p = find_entry_by_name (db, db->ops->getname (eptr));
967 if (next_entry_by_name (db, p->next, db->ops->getname (eptr)) != NULL) {
968 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), db->ops->getname (eptr), db->filename);
971 db->ops->free (p->eptr);
979 /* not found, new entry */
980 p = (struct commonio_entry *) malloc (sizeof *p);
982 db->ops->free (nentry);
992 add_one_entry_nis (db, p);
994 add_one_entry (db, p);
1002 void commonio_del_entry (struct commonio_db *db, const struct commonio_entry *p)
1004 if (p == db->cursor) {
1005 db->cursor = p->next;
1008 if (NULL != p->prev) {
1009 p->prev->next = p->next;
1014 if (NULL != p->next) {
1015 p->next->prev = p->prev;
1024 * commonio_remove - Remove the entry of the given name from the database.
1026 int commonio_remove (struct commonio_db *db, const char *name)
1028 struct commonio_entry *p;
1030 if (!db->isopen || db->readonly) {
1034 p = find_entry_by_name (db, name);
1039 if (next_entry_by_name (db, p->next, name) != NULL) {
1040 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), name, db->filename);
1044 commonio_del_entry (db, p);
1046 if (NULL != p->line) {
1050 if (NULL != p->eptr) {
1051 db->ops->free (p->eptr);
1058 * commonio_locate - Find the first entry with the specified name in
1061 * If found, it returns the entry and set the cursor of the database to
1064 * Otherwise, it returns NULL.
1066 /*@observer@*/ /*@null@*/const void *commonio_locate (struct commonio_db *db, const char *name)
1068 struct commonio_entry *p;
1074 p = find_entry_by_name (db, name);
1084 * commonio_rewind - Restore the database cursor to the first entry.
1086 * It returns 0 on error, 1 on success.
1088 int commonio_rewind (struct commonio_db *db)
1099 * commonio_next - Return the next entry of the specified database
1101 * It returns the next entry, or NULL if no other entries could be found.
1103 /*@observer@*/ /*@null@*/const void *commonio_next (struct commonio_db *db)
1111 if (NULL == db->cursor) {
1112 db->cursor = db->head;
1114 db->cursor = db->cursor->next;
1117 while (NULL != db->cursor) {
1118 eptr = db->cursor->eptr;
1123 db->cursor = db->cursor->next;