chiark / gitweb /
user-util: add new wrappers for reading/writing {passwd,shadow,gshadow} database...
authorFranck Bui <fbui@suse.com>
Wed, 21 Mar 2018 14:26:02 +0000 (15:26 +0100)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
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.

src/basic/user-util.c
src/basic/user-util.h

index 3cde7a0f29b2799b44de8602ba76eb1977576cd8..f7b2a7c9d9e8d663ca07d7a389ca023d26baaafc 100644 (file)
@@ -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
index 2bd1ee9a76ed0ad7a9fb81a95dea9fefced5d844..54581de15f14c3365f36aea067babda0861b9925 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+//#include <grp.h>
+//#include <gshadow.h>
+//#include <pwd.h>
+//#include <shadow.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <sys/types.h>
@@ -120,3 +124,14 @@ static inline bool valid_shell(const char *p) {
 int maybe_setgroups(size_t size, const gid_t *list);
 
 bool synthesize_nobody(void);
+
+int fgetpwent_sane(FILE *stream, struct passwd **pw);
+int fgetspent_sane(FILE *stream, struct spwd **sp);
+int fgetgrent_sane(FILE *stream, struct group **gr);
+int putpwent_sane(const struct passwd *pw, FILE *stream);
+int putspent_sane(const struct spwd *sp, FILE *stream);
+int putgrent_sane(const struct group *gr, FILE *stream);
+#ifdef ENABLE_GSHADOW
+int fgetsgent_sane(FILE *stream, struct sgrp **sg);
+int putsgent_sane(const struct sgrp *sg, FILE *stream);
+#endif