Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ libshadow_la_SOURCES = \
find_new_uid.c \
find_new_sub_gids.c \
find_new_sub_uids.c \
fs/copy/fcopy.c \
fs/copy/fcopy.h \
fs/mkstemp/fmkomstemp.c \
fs/mkstemp/fmkomstemp.h \
fs/mkstemp/mkomstemp.c \
Expand Down
13 changes: 2 additions & 11 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "atoi/getnum.h"
#include "commonio.h"
#include "defines.h"
#include "fs/copy/fcopy.h"
#include "fs/mkstemp/fmkomstemp.h"
#include "nscd.h"
#ifdef WITH_TCB
Expand Down Expand Up @@ -247,7 +248,6 @@ static int create_backup (const char *name, FILE * fp)
struct stat sb;
struct utimbuf ub;
FILE *bkfp;
int c;

stprintf_a(tmpf, "%s.cioXXXXXX", name);
if (fstat (fileno (fp), &sb) != 0) {
Expand All @@ -259,16 +259,7 @@ static int create_backup (const char *name, FILE * fp)
return -1;
}

/* TODO: faster copy, not one-char-at-a-time. --marekm */
c = 0;
if (fseek (fp, 0, SEEK_SET) == 0) {
while ((c = getc (fp)) != EOF) {
if (putc (c, bkfp) == EOF) {
break;
}
}
}
if ((c != EOF) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
if (fcopy(bkfp, fp) == -1) {
(void) fclose (bkfp);
unlink(tmpf);
return -1;
Expand Down
12 changes: 12 additions & 0 deletions lib/fs/copy/fcopy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "fs/copy/fcopy.h"

#include <stdio.h>


extern inline int fcopy(FILE *dst, FILE *src);
40 changes: 40 additions & 0 deletions lib/fs/copy/fcopy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_FS_COPY_FCOPY_H_
#define SHADOW_INCLUDE_LIB_FS_COPY_FCOPY_H_


#include "config.h"

#include <stdio.h>


inline int fcopy(FILE *dst, FILE *src);


// fcopy - FILE copy
inline int
fcopy(FILE *dst, FILE *src)
{
int c;

if (ftello(dst) != 0)
return -1;
if (fseek(src, 0, SEEK_SET) == -1)
return -1;

while (EOF != (c = fgetc(src))) {
if (fputc(c, dst) == EOF)
return -1;
}
if (ferror(src))
return -1;
if (fflush(dst) == -1)
return -1;
return 0;
}


#endif // include guard
13 changes: 3 additions & 10 deletions src/vipw.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#endif /* WITH_TCB */
#include "shadowlog.h"
#include "sssd.h"
#include "fs/copy/fcopy.h"
#include "fs/mkstemp/fmkomstemp.h"
#include "string/sprintf/aprintf.h"
#include "string/sprintf/snprintf.h"
Expand Down Expand Up @@ -108,21 +109,13 @@ static int create_backup_file (FILE * fp, char *backup, struct stat *sb)
{
struct utimbuf ub;
FILE *bkfp;
int c;

bkfp = fmkomstemp(backup, 0, 0600);
if (NULL == bkfp) {
return -1;
}

c = 0;
if (fseeko (fp, 0, SEEK_SET) == 0)
while ((c = getc (fp)) != EOF) {
if (putc (c, bkfp) == EOF) {
break;
}
}
if ((EOF != c) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
if (fcopy(bkfp, fp) == -1) {
fclose (bkfp);
unlink (backup);
return -1;
Expand All @@ -137,7 +130,7 @@ static int create_backup_file (FILE * fp, char *backup, struct stat *sb)
ub.modtime = sb->st_mtime;
if ( (utime (backup, &ub) != 0)
|| (fchown(fileno(bkfp), sb->st_uid, sb->st_gid) != 0)
|| (fchmod(fileno(bkfp), sb->st_mode) != 0)) {
|| (fchmod(fileno(bkfp), sb->st_mode & 0664) != 0)) {
fclose(bkfp);
unlink (backup);
return -1;
Expand Down
Loading