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.
48 #include <selinux/selinux.h>
49 static security_context_t old_context = NULL;
53 /* local function prototypes */
54 static int lrename (const char *, const char *);
55 static int check_link_count (const char *file);
56 static int do_lock_file (const char *file, const char *lock);
57 static FILE *fopen_set_perms (const char *, const char *, const struct stat *);
58 static int create_backup (const char *, FILE *);
59 static void free_linked_list (struct commonio_db *);
60 static void add_one_entry (struct commonio_db *, struct commonio_entry *);
61 static bool name_is_nis (const char *name);
62 static int write_all (const struct commonio_db *);
63 static struct commonio_entry *find_entry_by_name (struct commonio_db *,
65 static struct commonio_entry *next_entry_by_name (struct commonio_db *,
66 struct commonio_entry *pos,
69 static int lock_count = 0;
70 static bool nscd_need_reload = false;
73 * Simple rename(P) alternative that attempts to rename to symlink
76 int lrename (const char *old, const char *new)
79 char resolved_path[PATH_MAX];
84 if (lstat (new, &sb) == 0 && S_ISLNK (sb.st_mode)) {
85 if (realpath (new, resolved_path) == NULL) {
86 perror ("realpath in lrename()");
92 res = rename (old, new);
96 static int check_link_count (const char *file)
100 if (stat (file, &sb) != 0) {
104 if (sb.st_nlink != 2) {
112 static int do_lock_file (const char *file, const char *lock)
120 fd = open (file, O_CREAT | O_EXCL | O_WRONLY, 0600);
126 snprintf (buf, sizeof buf, "%lu", (unsigned long) pid);
127 len = (ssize_t) strlen (buf) + 1;
128 if (write (fd, buf, (size_t) len) != len) {
135 if (link (file, lock) == 0) {
136 retval = check_link_count (file);
141 fd = open (lock, O_RDWR);
147 len = read (fd, buf, sizeof (buf) - 1);
155 /* FIXME: use a get_pid */
156 pid = strtol (buf, (char **) 0, 10);
162 if (kill (pid, 0) == 0) {
167 if (unlink (lock) != 0) {
173 if ((link (file, lock) == 0) && (check_link_count (file) != 0)) {
182 static FILE *fopen_set_perms (const char *name, const char *mode,
183 const struct stat *sb)
189 fp = fopen (name, mode);
196 if (fchown (fileno (fp), sb->st_uid, sb->st_gid) != 0) {
200 if (chown (name, sb->st_mode) != 0) {
206 if (fchmod (fileno (fp), sb->st_mode & 0664) != 0) {
210 if (chmod (name, sb->st_mode & 0664) != 0) {
223 static int create_backup (const char *backup, FILE * fp)
231 if (fstat (fileno (fp), &sb) != 0) {
236 bkfp = fopen (backup, "w");
242 /* TODO: faster copy, not one-char-at-a-time. --marekm */
244 if (fseek (fp, 0, SEEK_SET) == 0) {
245 while ((c = getc (fp)) != EOF) {
246 if (putc (c, bkfp) == EOF) {
251 if ((c != EOF) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
255 if ( (fsync (fileno (bkfp)) != 0)
256 || (fclose (bkfp) != 0)) {
260 ub.actime = sb.st_atime;
261 ub.modtime = sb.st_mtime;
262 (void) utime (backup, &ub);
267 static void free_linked_list (struct commonio_db *db)
269 struct commonio_entry *p;
271 while (NULL != db->head) {
275 if (NULL != p->line) {
279 if (NULL != p->eptr) {
280 db->ops->free (p->eptr);
289 int commonio_setname (struct commonio_db *db, const char *name)
291 snprintf (db->filename, sizeof (db->filename), "%s", name);
296 bool commonio_present (const struct commonio_db *db)
298 return (access (db->filename, F_OK) == 0);
302 int commonio_lock_nowait (struct commonio_db *db)
311 snprintf (file, sizeof file, "%s.%lu",
312 db->filename, (unsigned long) getpid ());
313 snprintf (lock, sizeof lock, "%s.lock", db->filename);
314 if (do_lock_file (file, lock) != 0) {
323 int commonio_lock (struct commonio_db *db)
327 * only if the system libc has a real lckpwdf() - the one from
328 * lockpw.c calls us and would cause infinite recursion!
332 * Call lckpwdf() on the first lock.
333 * If it succeeds, call *_lock() only once
334 * (no retries, it should always succeed).
336 if (0 == lock_count) {
337 if (lckpwdf () == -1) {
338 return 0; /* failure */
342 if (commonio_lock_nowait (db) != 0) {
343 return 1; /* success */
347 return 0; /* failure */
352 * lckpwdf() not used - do it the old way.
355 #define LOCK_TRIES 15
361 for (i = 0; i < LOCK_TRIES; i++) {
363 sleep (LOCK_SLEEP); /* delay between retries */
365 if (commonio_lock_nowait (db) != 0) {
366 return 1; /* success */
368 /* no unnecessary retries on "permission denied" errors */
369 if (geteuid () != 0) {
373 return 0; /* failure */
377 static void dec_lock_count (void)
379 if (lock_count > 0) {
381 if (lock_count == 0) {
382 /* Tell nscd when lock count goes to zero,
383 if any of the files were changed. */
384 if (nscd_need_reload) {
385 nscd_flush_cache ("passwd");
386 nscd_flush_cache ("group");
387 nscd_need_reload = false;
397 int commonio_unlock (struct commonio_db *db)
403 if (commonio_close (db) == 0) {
412 * Unlock in reverse order: remove the lock file,
413 * then call ulckpwdf() (if used) on last unlock.
416 snprintf (lock, sizeof lock, "%s.lock", db->filename);
425 static void add_one_entry (struct commonio_db *db, struct commonio_entry *p)
429 if (NULL == db->head) {
432 if (NULL != db->tail) {
439 static bool name_is_nis (const char *name)
441 return (('+' == name[0]) || ('-' == name[0]));
446 * New entries are inserted before the first NIS entry. Order is preserved
447 * when db is written out.
449 #ifndef KEEP_NIS_AT_END
450 #define KEEP_NIS_AT_END 1
454 static void add_one_entry_nis (struct commonio_db *, struct commonio_entry *);
457 * Insert an entry between the regular entries, and the NIS entries.
460 add_one_entry_nis (struct commonio_db *db, struct commonio_entry *newp)
462 struct commonio_entry *p;
464 for (p = db->head; NULL != p; p = p->next) {
465 if (name_is_nis (p->eptr ? db->ops->getname (p->eptr)
468 newp->prev = p->prev;
469 if (NULL != p->prev) {
470 p->prev->next = newp;
478 add_one_entry (db, newp);
480 #endif /* KEEP_NIS_AT_END */
482 /* Initial buffer size, as well as increment if not sufficient
483 (for reading very long lines in group files). */
486 int commonio_open (struct commonio_db *db, int mode)
491 struct commonio_entry *p;
500 || ( (O_RDONLY != mode)
501 && (O_RDWR != mode))) {
505 db->readonly = (mode == O_RDONLY);
506 if (!db->readonly && !db->locked) {
511 db->head = db->tail = db->cursor = NULL;
514 db->fp = fopen (db->filename, db->readonly ? "r" : "r+");
517 * If O_CREAT was specified and the file didn't exist, it will be
518 * created by commonio_close(). We have no entries to read yet. --marekm
520 if (NULL == db->fp) {
521 if (((flags & O_CREAT) != 0) && (ENOENT == errno)) {
528 /* Do not inherit fd in spawned processes (e.g. nscd) */
529 fcntl(fileno(db->fp), F_SETFD, FD_CLOEXEC);
533 if ((is_selinux_enabled () > 0) && (!db->readonly)) {
534 if (fgetfilecon (fileno (db->fp), &db->scontext) < 0) {
541 buf = (char *) malloc (buflen);
546 while (db->ops->fgets (buf, buflen, db->fp) == buf) {
547 while ( ((cp = strrchr (buf, '\n')) == NULL)
548 && (feof (db->fp) == 0)) {
552 cp = (char *) realloc (buf, buflen);
558 if (db->ops->fgets (buf + len, buflen - len, db->fp) == NULL) {
562 cp = strrchr (buf, '\n');
572 if (name_is_nis (line)) {
575 eptr = db->ops->parse (line);
577 eptr = db->ops->dup (eptr);
584 p = (struct commonio_entry *) malloc (sizeof *p);
593 add_one_entry (db, p);
598 if (ferror (db->fp) != 0) {
602 if ((NULL != db->ops->open_hook) && (db->ops->open_hook () == 0)) {
611 db->ops->free (eptr);
621 free_linked_list (db);
623 if (db->scontext != NULL) {
624 freecon (db->scontext);
635 * Sort given db according to cmp function (usually compares uids)
638 commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
640 struct commonio_entry **entries, *ptr;
643 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
651 entries = malloc (n * sizeof (struct commonio_entry *));
652 if (entries == NULL) {
657 for (ptr = db->head; NULL != ptr; ptr = ptr->next) {
660 qsort (entries, n, sizeof (struct commonio_entry *), cmp);
662 db->head = entries[0];
663 db->tail = entries[--n];
664 db->head->prev = NULL;
665 db->head->next = entries[1];
666 db->tail->prev = entries[n - 1];
667 db->tail->next = NULL;
669 for (i = 1; i < n; i++) {
670 entries[i]->prev = entries[i - 1];
671 entries[i]->next = entries[i + 1];
681 * Sort entries in db according to order in another.
683 int commonio_sort_wrt (struct commonio_db *shadow, struct commonio_db *passwd)
685 struct commonio_entry *head = NULL, *pw_ptr, *spw_ptr;
688 if ((NULL == shadow) || (NULL == shadow->head)) {
692 for (pw_ptr = passwd->head; NULL != pw_ptr; pw_ptr = pw_ptr->next) {
693 if (NULL == pw_ptr->eptr) {
696 name = passwd->ops->getname (pw_ptr->eptr);
697 for (spw_ptr = shadow->head;
699 spw_ptr = spw_ptr->next) {
700 if (strcmp (name, shadow->ops->getname (spw_ptr->eptr))
705 if (NULL == spw_ptr) {
708 commonio_del_entry (shadow, spw_ptr);
709 spw_ptr->next = head;
713 for (spw_ptr = head; NULL != spw_ptr; spw_ptr = head) {
716 if (NULL != shadow->head) {
717 shadow->head->prev = spw_ptr;
719 spw_ptr->next = shadow->head;
720 shadow->head = spw_ptr;
723 shadow->head->prev = NULL;
724 shadow->changed = true;
730 * write_all - Write the database to its file.
732 * It returns 0 if all the entries could be written correctly.
734 static int write_all (const struct commonio_db *db)
736 const struct commonio_entry *p;
739 for (p = db->head; NULL != p; p = p->next) {
742 if (db->ops->put (eptr, db->fp) != 0) {
745 } else if (NULL != p->line) {
746 if (db->ops->fputs (p->line, db->fp) == EOF) {
749 if (putc ('\n', db->fp) == EOF) {
758 int commonio_close (struct commonio_db *db)
770 if (!db->changed || db->readonly) {
776 if ((NULL != db->ops->close_hook) && (db->ops->close_hook () == 0)) {
780 memzero (&sb, sizeof sb);
781 if (NULL != db->fp) {
782 if (fstat (fileno (db->fp), &sb) != 0) {
788 if (db->scontext != NULL) {
789 if (getfscreatecon (&old_context) < 0) {
793 if (setfscreatecon (db->scontext) < 0) {
800 * Create backup file.
802 snprintf (buf, sizeof buf, "%s-", db->filename);
804 if (create_backup (buf, db->fp) != 0) {
808 if (fclose (db->fp) != 0) {
818 * Default permissions for new [g]shadow files.
819 * (passwd and group always exist...)
826 snprintf (buf, sizeof buf, "%s+", db->filename);
828 db->fp = fopen_set_perms (buf, "w", &sb);
829 if (NULL == db->fp) {
833 if (write_all (db) != 0) {
837 if (fflush (db->fp) != 0) {
841 if (fsync (fileno (db->fp)) != 0) {
847 if (fclose (db->fp) != 0) {
858 if (lrename (buf, db->filename) != 0) {
862 nscd_need_reload = true;
869 if (db->scontext != NULL) {
870 if (setfscreatecon (old_context) < 0) {
873 if (NULL != old_context) {
874 freecon (old_context);
877 freecon (db->scontext);
881 free_linked_list (db);
885 static struct commonio_entry *next_entry_by_name (struct commonio_db *db,
886 struct commonio_entry *pos,
889 struct commonio_entry *p;
896 for (p = pos; NULL != p; p = p->next) {
899 && (strcmp (db->ops->getname (ep), name) == 0)) {
906 static struct commonio_entry *find_entry_by_name (struct commonio_db *db,
909 return next_entry_by_name(db, db->head, name);
913 int commonio_update (struct commonio_db *db, const void *eptr)
915 struct commonio_entry *p;
918 if (!db->isopen || db->readonly) {
922 nentry = db->ops->dup (eptr);
923 if (NULL == nentry) {
927 p = find_entry_by_name (db, db->ops->getname (eptr));
929 if (next_entry_by_name (db, p->next, db->ops->getname (eptr)) != NULL) {
930 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), db->ops->getname (eptr), db->filename);
933 db->ops->free (p->eptr);
941 /* not found, new entry */
942 p = (struct commonio_entry *) malloc (sizeof *p);
944 db->ops->free (nentry);
954 add_one_entry_nis (db, p);
956 add_one_entry (db, p);
964 void commonio_del_entry (struct commonio_db *db, const struct commonio_entry *p)
966 if (p == db->cursor) {
967 db->cursor = p->next;
970 if (NULL != p->prev) {
971 p->prev->next = p->next;
976 if (NULL != p->next) {
977 p->next->prev = p->prev;
986 * commonio_remove - Remove the entry of the given name from the database.
988 int commonio_remove (struct commonio_db *db, const char *name)
990 struct commonio_entry *p;
992 if (!db->isopen || db->readonly) {
996 p = find_entry_by_name (db, name);
1001 if (next_entry_by_name (db, p->next, name) != NULL) {
1002 fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), name, db->filename);
1006 commonio_del_entry (db, p);
1008 if (NULL != p->line) {
1012 if (NULL != p->eptr) {
1013 db->ops->free (p->eptr);
1020 * commonio_locate - Find the first entry with the specified name in
1023 * If found, it returns the entry and set the cursor of the database to
1026 * Otherwise, it returns NULL.
1028 /*@observer@*/ /*@null@*/const void *commonio_locate (struct commonio_db *db, const char *name)
1030 struct commonio_entry *p;
1036 p = find_entry_by_name (db, name);
1046 * commonio_rewind - Restore the database cursor to the first entry.
1048 * It returns 0 on error, 1 on success.
1050 int commonio_rewind (struct commonio_db *db)
1061 * commonio_next - Return the next entry of the specified database
1063 * It returns the next entry, or NULL if no other entries could be found.
1065 /*@observer@*/ /*@null@*/const void *commonio_next (struct commonio_db *db)
1073 if (NULL == db->cursor) {
1074 db->cursor = db->head;
1076 db->cursor = db->cursor->next;
1079 while (NULL != db->cursor) {
1080 eptr = db->cursor->eptr;
1085 db->cursor = db->cursor->next;