dovecot-2.0: Added i_getpw*() and i_getgr*() wrappers for getpw*...
dovecot at dovecot.org
dovecot at dovecot.org
Thu Mar 17 16:21:04 EET 2011
details: http://hg.dovecot.org/dovecot-2.0/rev/7f5735ab7c35
changeset: 12689:7f5735ab7c35
user: Timo Sirainen <tss at iki.fi>
date: Thu Mar 17 16:20:12 2011 +0200
description:
Added i_getpw*() and i_getgr*() wrappers for getpw*()/getgr*().
These functions make error handling easier.
diffstat:
src/lib/Makefile.am | 2 +
src/lib/ipwd.c | 90 +++++++++++++++++++++++++++++++++++++++++++++
src/lib/ipwd.h | 23 +++++++++++
3 files changed, 115 insertions(+), 0 deletions(-)
diffs (140 lines):
diff -r 26d5351649ac -r 7f5735ab7c35 src/lib/Makefile.am
--- a/src/lib/Makefile.am Wed Mar 16 19:49:27 2011 +0200
+++ b/src/lib/Makefile.am Thu Mar 17 16:20:12 2011 +0200
@@ -45,6 +45,7 @@
home-expand.c \
hostpid.c \
imem.c \
+ ipwd.c \
iostream.c \
istream.c \
istream-base64-encoder.c \
@@ -157,6 +158,7 @@
home-expand.h \
hostpid.h \
imem.h \
+ ipwd.h \
iostream-internal.h \
istream.h \
istream-base64-encoder.h \
diff -r 26d5351649ac -r 7f5735ab7c35 src/lib/ipwd.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ipwd.c Thu Mar 17 16:20:12 2011 +0200
@@ -0,0 +1,90 @@
+/* Copyright (c) 2011 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "ipwd.h"
+
+#include <unistd.h>
+
+#define DEFAULT_PWBUF_SIZE 16384
+#define DEFAULT_GRBUF_SIZE 16384
+
+static void *pwbuf = NULL, *grbuf = NULL;
+static size_t pwbuf_size, grbuf_size;
+
+static void pw_init(void)
+{
+ long size;
+
+ if (pwbuf == NULL) {
+ size = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (size < 0)
+ size = DEFAULT_PWBUF_SIZE;
+
+ pwbuf_size = size;
+ pwbuf = i_malloc(pwbuf_size);
+ }
+}
+
+static void gr_init(void)
+{
+ long size;
+
+ if (grbuf == NULL) {
+ size = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (size < 0)
+ size = DEFAULT_GRBUF_SIZE;
+
+ grbuf_size = size;
+ grbuf = i_malloc(grbuf_size);
+ }
+}
+
+void ipwd_deinit(void)
+{
+ i_free_and_null(pwbuf);
+ i_free_and_null(grbuf);
+}
+
+int i_getpwnam(const char *name, struct passwd *pwd_r)
+{
+ struct passwd *result;
+
+ pw_init();
+ errno = getpwnam_r(name, pwd_r, pwbuf, pwbuf_size, &result);
+ if (result != NULL)
+ return 1;
+ return errno == 0 ? 0 : -1;
+}
+
+int i_getpwuid(uid_t uid, struct passwd *pwd_r)
+{
+ struct passwd *result;
+
+ pw_init();
+ errno = getpwuid_r(uid, pwd_r, pwbuf, pwbuf_size, &result);
+ if (result != NULL)
+ return 1;
+ return errno == 0 ? 0 : -1;
+}
+
+int i_getgrnam(const char *name, struct group *grp_r)
+{
+ struct group *result;
+
+ gr_init();
+ errno = getgrnam_r(name, grp_r, grbuf, grbuf_size, &result);
+ if (result != NULL)
+ return 1;
+ return errno == 0 ? 0 : -1;
+}
+
+int i_getgrgid(gid_t gid, struct group *grp_r)
+{
+ struct group *result;
+
+ gr_init();
+ errno = getgrgid_r(gid, grp_r, grbuf, grbuf_size, &result);
+ if (result != NULL)
+ return 1;
+ return errno == 0 ? 0 : -1;
+}
diff -r 26d5351649ac -r 7f5735ab7c35 src/lib/ipwd.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ipwd.h Thu Mar 17 16:20:12 2011 +0200
@@ -0,0 +1,23 @@
+#ifndef IPWD_H
+#define IPWD_H
+
+#include <pwd.h>
+#include <grp.h>
+
+/* Replacements for standard getpw/gr*(), fixing their ability to report errors
+ properly. As with standard getpw/gr*(), second call overwrites data used
+ by the first one.
+
+ Functions return 1 if user/group is found, 0 if not or
+ -1 if error (with errno set). */
+
+int i_getpwnam(const char *name, struct passwd *pwd_r);
+int i_getpwuid(uid_t uid, struct passwd *pwd_r);
+
+int i_getgrnam(const char *name, struct group *grp_r);
+int i_getgrgid(gid_t gid, struct group *grp_r);
+
+/* Free memory used by above functions. */
+void ipwd_deinit(void);
+
+#endif
More information about the dovecot-cvs
mailing list