diff options
author | Franck Bui <fbui@suse.com> | 2018-03-21 15:26:02 +0100 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-08-24 16:47:08 +0200 |
commit | be4e039d977bcee04fedcdb7a6a4b5c18cfaca38 (patch) | |
tree | ad48f23d74388d38c734ea6b2d2fdcd1ecc163cb /src/basic/user-util.c | |
parent | 74d8433aa68dd73a1ea04363d4d4fd30d90c49a2 (diff) |
user-util: add new wrappers for reading/writing {passwd,shadow,gshadow} database files (#8521)
The API povided by the glibc is too error-prone as one has to deal directly
with errno in order to detect if errors occured.
Suggested by Zbigniew.
Diffstat (limited to 'src/basic/user-util.c')
-rw-r--r-- | src/basic/user-util.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 3cde7a0f2..f7b2a7c9d 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -742,3 +742,123 @@ bool synthesize_nobody(void) { return cache; #endif } + +int putpwent_sane(const struct passwd *pw, FILE *stream) { + assert(pw); + assert(stream); + + errno = 0; + if (putpwent(pw, stream) != 0) + return errno > 0 ? -errno : -EIO; + + return 0; +} + +int putspent_sane(const struct spwd *sp, FILE *stream) { + assert(sp); + assert(stream); + + errno = 0; + if (putspent(sp, stream) != 0) + return errno > 0 ? -errno : -EIO; + + return 0; +} + +int putgrent_sane(const struct group *gr, FILE *stream) { + assert(gr); + assert(stream); + + errno = 0; + if (putgrent(gr, stream) != 0) + return errno > 0 ? -errno : -EIO; + + return 0; +} + +#if ENABLE_GSHADOW +int putsgent_sane(const struct sgrp *sg, FILE *stream) { + assert(sg); + assert(stream); + + errno = 0; + if (putsgent(sg, stream) != 0) + return errno > 0 ? -errno : -EIO; + + return 0; +} +#endif + +int fgetpwent_sane(FILE *stream, struct passwd **pw) { + struct passwd *p; + + assert(pw); + assert(stream); + + errno = 0; + p = fgetpwent(stream); + if (p == NULL) { + if (errno == ENOENT) + return false; + return errno > 0 ? -errno : -EIO; + } + + *pw = p; + return true; +} + +int fgetspent_sane(FILE *stream, struct spwd **sp) { + struct spwd *s; + + assert(sp); + assert(stream); + + errno = 0; + s = fgetspent(stream); + if (s == NULL) { + if (errno == ENOENT) + return false; + return errno > 0 ? -errno : -EIO; + } + + *sp = s; + return true; +} + +int fgetgrent_sane(FILE *stream, struct group **gr) { + struct group *g; + + assert(gr); + assert(stream); + + errno = 0; + g = fgetgrent(stream); + if (g == NULL) { + if (errno == ENOENT) + return false; + return errno > 0 ? -errno : -EIO; + } + + *gr = g; + return true; +} + +#if ENABLE_GSHADOW +int fgetsgent_sane(FILE *stream, struct sgrp **sg) { + struct sgrp *s; + + assert(sg); + assert(stream); + + errno = 0; + s = fgetsgent(stream); + if (s == NULL) { + if (errno == ENOENT) + return false; + return errno > 0 ? -errno : -EIO; + } + + *sg = s; + return true; +} +#endif |