diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:37 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:37 +0000 |
commit | b6b00dd55e035bfbe311a527b567962ffa77ee43 (patch) | |
tree | cafc4d13785448e5a78bd40a51697ee07f07ac12 /lib/commonio.c | |
parent | Adding debian version 1:4.13+dfsg1-5. (diff) | |
download | shadow-b6b00dd55e035bfbe311a527b567962ffa77ee43.tar.xz shadow-b6b00dd55e035bfbe311a527b567962ffa77ee43.zip |
Merging upstream version 1:4.15.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/commonio.c')
-rw-r--r-- | lib/commonio.c | 121 |
1 files changed, 54 insertions, 67 deletions
diff --git a/lib/commonio.c b/lib/commonio.c index 9a02ce1..01a26c9 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -21,6 +21,9 @@ #include <errno.h> #include <stdio.h> #include <signal.h> + +#include "alloc.h" +#include "memzero.h" #include "nscd.h" #include "sssd.h" #ifdef WITH_TCB @@ -29,6 +32,8 @@ #include "prototypes.h" #include "commonio.h" #include "shadowlog_internal.h" +#include "string/sprintf.h" + /* local function prototypes */ static int lrename (const char *, const char *); @@ -106,9 +111,9 @@ static int check_link_count (const char *file, bool log) if (sb.st_nlink != 2) { if (log) { - (void) fprintf (shadow_logfd, - "%s: %s: lock file already used (nlink: %u)\n", - shadow_progname, file, sb.st_nlink); + fprintf(shadow_logfd, + "%s: %s: lock file already used (nlink: %ju)\n", + shadow_progname, file, (uintmax_t) sb.st_nlink); } return 0; } @@ -119,11 +124,11 @@ static int check_link_count (const char *file, bool log) static int do_lock_file (const char *file, const char *lock, bool log) { - int fd; - pid_t pid; - ssize_t len; - int retval; - char buf[32]; + int fd; + int retval; + char buf[32]; + pid_t pid; + ssize_t len; fd = open (file, O_CREAT | O_TRUNC | O_WRONLY, 0600); if (-1 == fd) { @@ -136,9 +141,9 @@ static int do_lock_file (const char *file, const char *lock, bool log) } pid = getpid (); - snprintf (buf, sizeof buf, "%lu", (unsigned long) pid); + SNPRINTF(buf, "%lu", (unsigned long) pid); len = (ssize_t) strlen (buf) + 1; - if (write (fd, buf, (size_t) len) != len) { + if (write_full(fd, buf, len) == -1) { if (log) { (void) fprintf (shadow_logfd, "%s: %s file write error: %s\n", @@ -190,7 +195,7 @@ static int do_lock_file (const char *file, const char *lock, bool log) return 0; } buf[len] = '\0'; - if (get_pid (buf, &pid) == 0) { + if (get_pid(buf, &pid) == -1) { if (log) { (void) fprintf (shadow_logfd, "%s: existing lock file %s with an invalid PID '%s'\n", @@ -251,25 +256,13 @@ static /*@null@*/ /*@dependent@*/FILE *fopen_set_perms ( return NULL; } -#ifdef HAVE_FCHOWN if (fchown (fileno (fp), sb->st_uid, sb->st_gid) != 0) { goto fail; } -#else /* !HAVE_FCHOWN */ - if (chown (name, sb->st_mode) != 0) { - goto fail; - } -#endif /* !HAVE_FCHOWN */ - -#ifdef HAVE_FCHMOD if (fchmod (fileno (fp), sb->st_mode & 0664) != 0) { goto fail; } -#else /* !HAVE_FCHMOD */ - if (chmod (name, sb->st_mode & 0664) != 0) { - goto fail; - } -#endif /* !HAVE_FCHMOD */ + return fp; fail: @@ -349,7 +342,7 @@ static void free_linked_list (struct commonio_db *db) int commonio_setname (struct commonio_db *db, const char *name) { - snprintf (db->filename, sizeof (db->filename), "%s", name); + SNPRINTF(db->filename, "%s", name); db->setname = true; return 1; } @@ -363,33 +356,25 @@ bool commonio_present (const struct commonio_db *db) int commonio_lock_nowait (struct commonio_db *db, bool log) { - char* file = NULL; - char* lock = NULL; - size_t lock_file_len; - size_t file_len; - int err = 0; + int err = 0; + char *file = NULL; + char *lock = NULL; if (db->locked) { return 1; } - file_len = strlen(db->filename) + 11;/* %lu max size */ - lock_file_len = strlen(db->filename) + 6; /* sizeof ".lock" */ - file = (char*)malloc(file_len); - if (file == NULL) { + + if (asprintf(&file, "%s.%ju", db->filename, (uintmax_t) getpid()) == -1) goto cleanup_ENOMEM; - } - lock = (char*)malloc(lock_file_len); - if (lock == NULL) { + if (asprintf(&lock, "%s.lock", db->filename) == -1) goto cleanup_ENOMEM; - } - snprintf (file, file_len, "%s.%lu", - db->filename, (unsigned long) getpid ()); - snprintf (lock, lock_file_len, "%s.lock", db->filename); + if (do_lock_file (file, lock, log) != 0) { db->locked = true; lock_count++; err = 1; } + cleanup_ENOMEM: free(file); free(lock); @@ -483,7 +468,7 @@ static void dec_lock_count (void) int commonio_unlock (struct commonio_db *db) { - char lock[1024]; + char lock[1029]; if (db->isopen) { db->readonly = true; @@ -500,7 +485,7 @@ int commonio_unlock (struct commonio_db *db) * then call ulckpwdf() (if used) on last unlock. */ db->locked = false; - snprintf (lock, sizeof lock, "%s.lock", db->filename); + SNPRINTF(lock, "%s.lock", db->filename); unlink (lock); dec_lock_count (); return 1; @@ -618,7 +603,7 @@ int commonio_open (struct commonio_db *db, int mode) fd = open (db->filename, (db->readonly ? O_RDONLY : O_RDWR) - | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW); + | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC); saved_errno = errno; db->fp = NULL; if (fd >= 0) { @@ -649,22 +634,19 @@ int commonio_open (struct commonio_db *db, int mode) return 0; } - /* Do not inherit fd in spawned processes (e.g. nscd) */ - fcntl (fileno (db->fp), F_SETFD, FD_CLOEXEC); - buflen = BUFLEN; - buf = (char *) malloc (buflen); + buf = MALLOC(buflen, char); if (NULL == buf) { goto cleanup_ENOMEM; } - while (db->ops->fgets (buf, (int) buflen, db->fp) == buf) { - while ( ((cp = strrchr (buf, '\n')) == NULL) + while (db->ops->fgets (buf, buflen, db->fp) == buf) { + while ( (strrchr (buf, '\n') == NULL) && (feof (db->fp) == 0)) { size_t len; buflen += BUFLEN; - cp = (char *) realloc (buf, buflen); + cp = REALLOC(buf, buflen, char); if (NULL == cp) { goto cleanup_buf; } @@ -698,7 +680,7 @@ int commonio_open (struct commonio_db *db, int mode) } } - p = (struct commonio_entry *) malloc (sizeof *p); + p = MALLOC(1, struct commonio_entry); if (NULL == p) { goto cleanup_entry; } @@ -775,7 +757,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *)) return 0; } - entries = malloc (n * sizeof (struct commonio_entry *)); + entries = MALLOC(n, struct commonio_entry *); if (entries == NULL) { return -1; } @@ -912,9 +894,9 @@ static int write_all (const struct commonio_db *db) int commonio_close (struct commonio_db *db) { - char buf[1024]; - int errors = 0; - struct stat sb; + int errors = 0; + char buf[1024]; + struct stat sb; if (!db->isopen) { errno = EINVAL; @@ -945,7 +927,11 @@ int commonio_close (struct commonio_db *db) /* * Create backup file. */ - snprintf (buf, sizeof buf, "%s-", db->filename); + if (SNPRINTF(buf, "%s-", db->filename) == -1) { + (void) fclose (db->fp); + db->fp = NULL; + goto fail; + } #ifdef WITH_SELINUX if (set_selinux_file_context (db->filename, S_IFREG) != 0) { @@ -960,15 +946,15 @@ int commonio_close (struct commonio_db *db) errors++; } + db->fp = NULL; + #ifdef WITH_SELINUX if (reset_selinux_file_context () != 0) { errors++; } #endif - if (errors != 0) { - db->fp = NULL; + if (errors != 0) goto fail; - } } else { /* * Default permissions for new [g]shadow files. @@ -978,7 +964,8 @@ int commonio_close (struct commonio_db *db) sb.st_gid = db->st_gid; } - snprintf (buf, sizeof buf, "%s+", db->filename); + if (SNPRINTF(buf, "%s+", db->filename) == -1) + goto fail; #ifdef WITH_SELINUX if (set_selinux_file_context (db->filename, S_IFREG) != 0) { @@ -998,13 +985,11 @@ int commonio_close (struct commonio_db *db) if (fflush (db->fp) != 0) { errors++; } -#ifdef HAVE_FSYNC + if (fsync (fileno (db->fp)) != 0) { errors++; } -#else /* !HAVE_FSYNC */ - sync (); -#endif /* !HAVE_FSYNC */ + if (fclose (db->fp) != 0) { errors++; } @@ -1096,7 +1081,7 @@ int commonio_update (struct commonio_db *db, const void *eptr) return 1; } /* not found, new entry */ - p = (struct commonio_entry *) malloc (sizeof *p); + p = MALLOC(1, struct commonio_entry); if (NULL == p) { db->ops->free (nentry); errno = ENOMEM; @@ -1133,7 +1118,7 @@ int commonio_append (struct commonio_db *db, const void *eptr) return 0; } /* new entry */ - p = (struct commonio_entry *) malloc (sizeof *p); + p = MALLOC(1, struct commonio_entry); if (NULL == p) { db->ops->free (nentry); errno = ENOMEM; @@ -1200,6 +1185,8 @@ int commonio_remove (struct commonio_db *db, const char *name) db->ops->free (p->eptr); } + free(p); + return 1; } |