From af8b7248c877663879e3e114a7150c78e8456fa4 Mon Sep 17 00:00:00 2001 From: brapru Date: Sat, 29 May 2021 08:40:16 -0400 Subject: [PATCH] LibC: Allow empty spwd members when writing shadow entries via putspent Previously there was no way to output an empty value into the shadow file entries when the spwd members were disabled. This would cause new user entries to the shadow file to be cluttered with disabled values. This commit checks if the spwd member value is diabled (-1) and will output as appropriate. --- Userland/Libraries/LibC/shadow.cpp | 68 +++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibC/shadow.cpp b/Userland/Libraries/LibC/shadow.cpp index 7dce928c61..628925ea29 100644 --- a/Userland/Libraries/LibC/shadow.cpp +++ b/Userland/Libraries/LibC/shadow.cpp @@ -236,8 +236,72 @@ int putspent(struct spwd* p, FILE* stream) return -1; } - int nwritten = fprintf(stream, "%s:%s:%ld:%ld:%ld:%ld:%ld:%ld:%ld\n", p->sp_namp, - p->sp_pwdp, p->sp_lstchg, p->sp_min, p->sp_max, p->sp_warn, p->sp_inact, p->sp_expire, p->sp_flag); + int nwritten; + + nwritten = fprintf(stream, "%s:%s:", p->sp_namp, p->sp_pwdp); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_lstchg != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_lstchg); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_min != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_min); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_max != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_max); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_warn != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_warn); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_inact != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_inact); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_expire != (long int)-1) + nwritten = fprintf(stream, "%ld:", p->sp_expire); + else + nwritten = fprintf(stream, "%c", ':'); + if (!nwritten || nwritten < 0) { + errno = ferror(stream); + return -1; + } + + if (p->sp_flag != (unsigned long int)-1) + nwritten = fprintf(stream, "%ld\n", p->sp_flag); + else + nwritten = fprintf(stream, "%c", '\n'); if (!nwritten || nwritten < 0) { errno = ferror(stream); return -1;