static int write_temporary_passwd(const char *passwd_path, FILE **tmpfile, char **tmpfile_path) {
_cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
_cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
+ struct passwd *pw = NULL;
Iterator iterator;
Item *i;
int r;
original = fopen(passwd_path, "re");
if (original) {
- struct passwd *pw;
r = sync_rights(original, passwd);
if (r < 0)
return -EEXIST;
}
+ /* Make sure we keep the NIS entries (if any) at the end. */
+ if (IN_SET(pw->pw_name[0], '+', '-'))
+ break;
+
errno = 0;
if (putpwent(pw, passwd) < 0)
return errno ? -errno : -EIO;
return errno ? -errno : -EIO;
}
+ /* Append the remaining NIS entries if any */
+ while (pw) {
+ errno = 0;
+ if (putpwent(pw, passwd) < 0)
+ return errno ? -errno : -EIO;
+
+ pw = fgetpwent(original);
+ }
+ if (!IN_SET(errno, 0, ENOENT))
+ return -errno;
+
r = fflush_and_check(passwd);
if (r < 0)
return r;
_cleanup_fclose_ FILE *original = NULL, *group = NULL;
_cleanup_(unlink_and_freep) char *group_tmp = NULL;
bool group_changed = false;
+ struct group *gr = NULL;
Iterator iterator;
Item *i;
int r;
original = fopen(group_path, "re");
if (original) {
- struct group *gr;
r = sync_rights(original, group);
if (r < 0)
return -EEXIST;
}
+ /* Make sure we keep the NIS entries (if any) at the end. */
+ if (IN_SET(gr->gr_name[0], '+', '-'))
+ break;
+
r = putgrent_with_members(gr, group);
if (r < 0)
return r;
group_changed = true;
}
+ /* Append the remaining NIS entries if any */
+ while (gr) {
+ errno = 0;
+ if (putgrent(gr, group) != 0)
+ return errno > 0 ? -errno : -EIO;
+
+ gr = fgetgrent(original);
+ }
+ if (!IN_SET(errno, 0, ENOENT))
+ return -errno;
+
r = fflush_sync_and_check(group);
if (r < 0)
return r;