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.
35 #ident "$Id: commonio.c 2892 2009-05-10 13:49:03Z nekral-guest $"
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)
91 char resolved_path[PATH_MAX];
92 #endif /* !__GLIBC__ */
94 if (lstat (new, &sb) == 0 && S_ISLNK (sb.st_mode)) {
95 #ifdef __GLIBC__ /* now a POSIX.1-2008 feature */
96 r = realpath (new, NULL);
97 #else /* !__GLIBC__ */
98 r = realpath (new, resolved_path);
99 #endif /* !__GLIBC__ */
101 perror ("realpath in lrename()");
108 res = rename (old, new);
114 #endif /* __GLIBC__ */
119 static int check_link_count (const char *file)
123 if (stat (file, &sb) != 0) {
127 if (sb.st_nlink != 2) {
135 static int do_lock_file (const char *file, const char *lock)
143 fd = open (file, O_CREAT | O_EXCL | O_WRONLY, 0600);
149 snprintf (buf, sizeof buf, "%lu", (unsigned long) pid);
150 len = (ssize_t) strlen (buf) + 1;
151 if (write (fd, buf, (size_t) len) != len) {
158 if (link (file, lock) == 0) {
159 retval = check_link_count (file);
164 fd = open (lock, O_RDWR);
170 len = read (fd, buf, sizeof (buf) - 1);
178 if (get_pid (buf, &pid) == 0) {
183 if (kill (pid, 0) == 0) {
188 if (unlink (lock) != 0) {
194 if ((link (file, lock) == 0) && (check_link_count (file) != 0)) {
203 static /*@null@*/ /*@dependent@*/FILE *fopen_set_perms (
206 const struct stat *sb)
212 fp = fopen (name, mode);
219 if (fchown (fileno (fp), sb->st_uid, sb->st_gid) != 0) {
223 if (chown (name, sb->st_mode) != 0) {
229 if (fchmod (fileno (fp), sb->st_mode & 0664) != 0) {
233 if (chmod (name, sb->st_mode & 0664) != 0) {
241 /* fopen_set_perms is used for intermediate files */
242 (void) unlink (name);
247 static int create_backup (const char *backup, FILE * fp)
255 if (fstat (fileno (fp), &sb) != 0) {
260 bkfp = fopen (backup, "w");
266 /* TODO: faster copy, not one-char-at-a-time. --marekm */
268 if (fseek (fp, 0, SEEK_SET) == 0) {
269 while ((c = getc (fp)) != EOF) {
270 if (putc (c, bkfp) == EOF) {
275 if ((c != EOF) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
276 (void) fclose (bkfp);
277 /* FIXME: unlink the backup file? */
280 if ( (fsync (fileno (bkfp)) != 0)
281 || (fclose (bkfp) != 0)) {
282 /* FIXME: unlink the backup file? */
286 ub.actime = sb.st_atime;
287 ub.modtime = sb.st_mtime;
288 (void) utime (backup, &ub);
293 static void free_linked_list (struct commonio_db *db)
295 struct commonio_entry *p;
297 while (NULL != db->head) {
301 if (NULL != p->line) {
305 if (NULL != p->eptr) {
306 db->ops->free (p->eptr);
315 int commonio_setname (struct commonio_db *db, const char *name)
317 snprintf (db->filename, sizeof (db->filename), "%s", name);
322 bool commonio_present (const struct commonio_db *db)
324 return (access (db->filename, F_OK) == 0);
328 int commonio_lock_nowait (struct commonio_db *db)
337 snprintf (file, sizeof file, "%s.%lu",
338 db->filename, (unsigned long) getpid ());
339 snprintf (lock, sizeof lock, "%s.lock", db->filename);
340 if (do_lock_file (file, lock) != 0) {
349 int commonio_lock (struct commonio_db *db)
353 * only if the system libc has a real lckpwdf() - the one from
354 * lockpw.c calls us and would cause infinite recursion!
358 * Call lckpwdf() on the first lock.
359 * If it succeeds, call *_lock() only once
360 * (no retries, it should always succeed).
362 if (0 == lock_count) {
363 if (lckpwdf () == -1) {
364 return 0; /* failure */
368 if (commonio_lock_nowait (db) != 0) {
369 return 1; /* success */
373 return 0; /* failure */
378 * lckpwdf() not used - do it the old way.
381 #define LOCK_TRIES 15
387 for (i = 0; i < LOCK_TRIES; i++) {
389 sleep (LOCK_SLEEP); /* delay between retries */
391 if (commonio_lock_nowait (db) != 0) {
392 return 1; /* success */
394 /* no unnecessary retries on "permission denied" errors */
395 if (geteuid () != 0) {
399 return 0; /* failure */
403 static void dec_lock_count (void)
405 if (lock_count > 0) {
407 if (lock_count == 0) {
408 /* Tell nscd when lock count goes to zero,
409 if any of the files were changed. */
410 if (nscd_need_reload) {
411 nscd_flush_cache ("passwd");
412 nscd_flush_cache ("group");
413 nscd_need_reload = false;
423 int commonio_unlock (struct commonio_db *db)
429 if (commonio_close (db) == 0) {
438 * Unlock in reverse order: remove the lock file,
439 * then call ulckpwdf() (if used) on last unlock.
442 snprintf (lock, sizeof lock, "%s.lock", db->filename);
452 * Add an entry at the end.
454 * defines p->next, p->prev
455 * (unfortunately, owned special are not supported)
457 static void add_one_entry (struct commonio_db *db,
458 /*@owned@*/struct commonio_entry *p)
464 if (NULL == db->head) {
467 if (NULL != db->tail) {
474 static bool name_is_nis (const char *name)
476 return (('+' == name[0]) || ('-' == name[0]));
481 * New entries are inserted before the first NIS entry. Order is preserved
482 * when db is written out.
484 #ifndef KEEP_NIS_AT_END
485 #define KEEP_NIS_AT_END 1
489 static void add_one_entry_nis (struct commonio_db *db,
490 /*@owned@*/struct commonio_entry *newp);
493 * Insert an entry between the regular entries, and the NIS entries.
495 * defines newp->next, newp->prev
496 * (unfortunately, owned special are not supported)
498 static void add_one_entry_nis (struct commonio_db *db,
499 /*@owned@*/struct commonio_entry *newp)
501 struct commonio_entry *p;
503 for (p = db->head; NULL != p; p = p->next) {
504 if (name_is_nis (p->eptr ? db->ops->getname (p->eptr)
508 newp->prev = p->prev;
510 if (NULL != p->prev) {
511 p->prev->next = newp;
519 add_one_entry (db, newp);
521 #endif /* KEEP_NIS_AT_END */
523 /* Initial buffer size, as well as increment if not sufficient
524 (for reading very long lines in group files). */
527 int commonio_open (struct commonio_db *db, int mode)
532 struct commonio_entry *p;
541 || ( (O_RDONLY != mode)
542 && (O_RDWR != mode))) {
546 db->readonly = (mode == O_RDONLY);
547 if (!db->readonly && !db->locked) {
552 db->head = db->tail = NULL;
556 db->fp = fopen (db->filename, db->readonly ? "r" : "r+");
559 * If O_CREAT was specified and the file didn't exist, it will be
560 * created by commonio_close(). We have no entries to read yet. --marekm
562 if (NULL == db->fp) {
563 if (((flags & O_CREAT) != 0) && (ENOENT == errno)) {
570 /* Do not inherit fd in spawned processes (e.g. nscd) */
571 fcntl(fileno(db->fp), F_SETFD, FD_CLOEXEC);
575 if ((is_selinux_enabled () > 0) && (!db->readonly)) {
576 if (fgetfilecon (fileno (db->fp), &db->scontext) < 0) {
583 buf = (char *) malloc (buflen);
588 while (db->ops->fgets (buf, (int) buflen, db->fp) == buf) {
589 while ( ((cp = strrchr (buf, '\n')) == NULL)
590 && (feof (db->fp) == 0)) {
594 cp = (char *) realloc (buf, buflen);
600 if (db->ops->fgets (buf + len,
601 (int) (buflen - len),
606 cp = strrchr (buf, '\n');
616 if (name_is_nis (line)) {
619 eptr = db->ops->parse (line);
621 eptr = db->ops->dup (eptr);
628 p = (struct commonio_entry *) malloc (sizeof *p);
637 add_one_entry (db, p);
642 if (ferror (db->fp) != 0) {
646 if ((NULL != db->ops->open_hook) && (db->ops->open_hook () == 0)) {
655 db->ops->free (eptr);
665 free_linked_list (db);
667 if (db->scontext != NULL) {
668 freecon (db->scontext);
679 * Sort given db according to cmp function (usually compares uids)
682 commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
684 struct commonio_entry **entries, *ptr;
687 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
695 entries = malloc (n * sizeof (struct commonio_entry *));
696 if (entries == NULL) {
701 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
704 qsort (entries, n, sizeof (struct commonio_entry *), cmp);
706 db->head = entries[0];
707 db->tail = entries[--n];
708 db->head->prev = NULL;
709 db->head->next = entries[1];
710 db->tail->prev = entries[n - 1];
711 db->tail->next = NULL;
713 for (i = 1; i < n; i++) {
714 entries[i]->prev = entries[i - 1];
715 entries[i]->next = entries[i + 1];
725 * Sort entries in db according to order in another.
727 int commonio_sort_wrt (struct commonio_db *shadow, struct commonio_db *passwd)
729 struct commonio_entry *head = NULL, *pw_ptr, *spw_ptr;
732 if ((NULL == shadow) || (NULL == shadow->head)) {
736 for (pw_ptr = passwd->head; NULL != pw_ptr; pw_ptr = pw_ptr->next) {
737 if (NULL == pw_ptr->eptr) {
740 name = passwd->ops->getname (pw_ptr->eptr);
741 for (spw_ptr = shadow->head;
743 spw_ptr = spw_ptr->next) {
744 if (NULL == spw_ptr->eptr) {
747 if (strcmp (name, shadow->ops->getname (spw_ptr->eptr))
752 if (NULL == spw_ptr) {
755 commonio_del_entry (shadow, spw_ptr);
756 spw_ptr->next = head;
760 for (spw_ptr = head; NULL != spw_ptr; spw_ptr = head) {
763 if (NULL != shadow->head) {
764 shadow->head->prev = spw_ptr;
766 spw_ptr->next = shadow->head;
767 shadow->head = spw_ptr;
770 shadow->head->prev = NULL;
771 shadow->changed = true;
777 * write_all - Write the database to its file.
779 * It returns 0 if all the entries could be written correctly.
781 static int write_all (const struct commonio_db *db)
782 /*@requires notnull db->fp@*/
784 const struct commonio_entry *p;
787 for (p = db->head; NULL != p; p = p->next) {
790 assert (NULL != eptr);
791 if (db->ops->put (eptr, db->fp) != 0) {
794 } else if (NULL != p->line) {
795 if (db->ops->fputs (p->line, db->fp) == EOF) {
798 if (putc ('\n', db->fp) == EOF) {
807 int commonio_close (struct commonio_db *db)
808 /*@requires notnull db->fp@*/
815 /*@null@*/security_context_t old_context = NULL;
824 if (!db->changed || db->readonly) {
830 if ((NULL != db->ops->close_hook) && (db->ops->close_hook () == 0)) {
834 memzero (&sb, sizeof sb);
835 if (NULL != db->fp) {
836 if (fstat (fileno (db->fp), &sb) != 0) {
842 if (db->scontext != NULL) {
843 if (getfscreatecon (&old_context) < 0) {
847 if (setfscreatecon (db->scontext) < 0) {
854 * Create backup file.
856 snprintf (buf, sizeof buf, "%s-", db->filename);
858 if (create_backup (buf, db->fp) != 0) {
862 if (fclose (db->fp) != 0) {
872 * Default permissions for new [g]shadow files.
873 * (passwd and group always exist...)
880 snprintf (buf, sizeof buf, "%s+", db->filename);
882 db->fp = fopen_set_perms (buf, "w", &sb);
883 if (NULL == db->fp) {
887 if (write_all (db) != 0) {
891 if (fflush (db->fp) != 0) {
895 if (fsync (fileno (db->fp)) != 0) {
901 if (fclose (db->fp) != 0) {
912 if (lrename (buf, db->filename) != 0) {
916 nscd_need_reload = true;
923 if (db->scontext != NULL) {
924 if (NULL != old_context) {
925 if (setfscreatecon (old_context) < 0) {
928 freecon (old_context);
931 freecon (db->scontext);
935 free_linked_list (db);
939 static /*@dependent@*/ /*@null@*/struct commonio_entry *next_entry_by_name (
940 struct commonio_db *db,
941 /*@null@*/struct commonio_entry *pos,
944 struct commonio_entry *p;
951 for (p = pos; NULL != p; p = p->next) {
954 && (strcmp (db->ops->getname (ep), name) == 0)) {
961 static /*@dependent@*/ /*@null@*/struct commonio_entry *find_entry_by_name (
962 struct commonio_db *db,
965 return next_entry_by_name(db, db->head, name);
969 int commonio_update (struct commonio_db *db, const void *eptr)
971 struct commonio_entry *p;
974 if (!db->isopen || db->readonly) {
978 nentry = db->ops->dup (eptr);
979 if (NULL == nentry) {
983 p = find_entry_by_name (db, db->ops->getname (eptr));
985 if (next_entry_by_name (db, p->next, db->ops->getname (eptr)) != NULL) {
986 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), db->ops->getname (eptr), db->filename);
989 db->ops->free (p->eptr);
997 /* not found, new entry */
998 p = (struct commonio_entry *) malloc (sizeof *p);
1000 db->ops->free (nentry);
1010 add_one_entry_nis (db, p);
1012 add_one_entry (db, p);
1020 void commonio_del_entry (struct commonio_db *db, const struct commonio_entry *p)
1022 if (p == db->cursor) {
1023 db->cursor = p->next;
1026 if (NULL != p->prev) {
1027 p->prev->next = p->next;
1032 if (NULL != p->next) {
1033 p->next->prev = p->prev;
1042 * commonio_remove - Remove the entry of the given name from the database.
1044 int commonio_remove (struct commonio_db *db, const char *name)
1046 struct commonio_entry *p;
1048 if (!db->isopen || db->readonly) {
1052 p = find_entry_by_name (db, name);
1057 if (next_entry_by_name (db, p->next, name) != NULL) {
1058 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), name, db->filename);
1062 commonio_del_entry (db, p);
1064 if (NULL != p->line) {
1068 if (NULL != p->eptr) {
1069 db->ops->free (p->eptr);
1076 * commonio_locate - Find the first entry with the specified name in
1079 * If found, it returns the entry and set the cursor of the database to
1082 * Otherwise, it returns NULL.
1084 /*@observer@*/ /*@null@*/const void *commonio_locate (struct commonio_db *db, const char *name)
1086 struct commonio_entry *p;
1092 p = find_entry_by_name (db, name);
1102 * commonio_rewind - Restore the database cursor to the first entry.
1104 * It returns 0 on error, 1 on success.
1106 int commonio_rewind (struct commonio_db *db)
1117 * commonio_next - Return the next entry of the specified database
1119 * It returns the next entry, or NULL if no other entries could be found.
1121 /*@observer@*/ /*@null@*/const void *commonio_next (struct commonio_db *db)
1129 if (NULL == db->cursor) {
1130 db->cursor = db->head;
1132 db->cursor = db->cursor->next;
1135 while (NULL != db->cursor) {
1136 eptr = db->cursor->eptr;
1141 db->cursor = db->cursor->next;