dovecot-2.0: Replaced all getpw/gr*() to use i_getpw/gr*() with ...

dovecot at dovecot.org dovecot at dovecot.org
Thu Mar 17 16:21:05 EET 2011


details:   http://hg.dovecot.org/dovecot-2.0/rev/02829f7f79c7
changeset: 12690:02829f7f79c7
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Mar 17 16:21:00 2011 +0200
description:
Replaced all getpw/gr*() to use i_getpw/gr*() with improved error handling.

diffstat:

 src/auth/passdb-bsdauth.c              |  17 +++++---
 src/auth/passdb-passwd.c               |  26 +++++++-----
 src/auth/userdb-passwd.c               |  24 +++++++-----
 src/auth/userdb.c                      |  27 ++++++++-----
 src/lda/main.c                         |  13 ++++--
 src/lib-storage/index/mbox/mbox-lock.c |  14 ++++---
 src/lib-storage/mail-storage-service.c |  63 +++++++++++++++++--------------
 src/lib/eacces-error.c                 |  46 +++++++++++++---------
 src/lib/home-expand.c                  |  24 ++++++++---
 src/lib/ioloop-notify-inotify.c        |   9 ++--
 src/lib/lib.c                          |   2 +
 src/lib/mkdir-parents.c                |  15 +++----
 src/lib/restrict-access.c              |  29 +++++++-------
 src/master/main.c                      |  33 ++++++++++------
 src/master/service-process.c           |   2 -
 15 files changed, 200 insertions(+), 144 deletions(-)

diffs (truncated from 902 to 300 lines):

diff -r 7f5735ab7c35 -r 02829f7f79c7 src/auth/passdb-bsdauth.c
--- a/src/auth/passdb-bsdauth.c	Thu Mar 17 16:20:12 2011 +0200
+++ b/src/auth/passdb-bsdauth.c	Thu Mar 17 16:21:00 2011 +0200
@@ -7,23 +7,28 @@
 
 #include "safe-memset.h"
 #include "auth-cache.h"
+#include "ipwd.h"
 #include "mycrypt.h"
 
 #include <login_cap.h>
 #include <bsd_auth.h>
-#include <pwd.h>
 
 static void
 bsdauth_verify_plain(struct auth_request *request, const char *password,
 		    verify_plain_callback_t *callback)
 {
-	struct passwd *pw;
+	struct passwd pw;
 	int result;
 
 	auth_request_log_debug(request, "bsdauth", "lookup");
 
-	pw = getpwnam(request->user);
-	if (pw == NULL) {
+	switch (i_getpwnam(request->user, &pw)) {
+	case -1:
+		auth_request_log_error(request, "bsdauth",
+				       "getpwnam() failed: %m");
+		callback(PASSDB_RESULT_INTERNAL_FAILURE, request);
+		return;
+	case 0:
 		auth_request_log_info(request, "bsdauth", "unknown user");
 		callback(PASSDB_RESULT_USER_UNKNOWN, request);
 		return;
@@ -34,7 +39,7 @@
 			       t_strdup_noconst(password));
 
 	/* clear the passwords from memory */
-	safe_memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
+	safe_memset(pw.pw_passwd, 0, strlen(pw.pw_passwd));
 
 	if (result == 0) {
 		auth_request_log_password_mismatch(request, "bsdauth");
@@ -43,7 +48,7 @@
 	}
 
 	/* make sure we're using the username exactly as it's in the database */
-        auth_request_set_field(request, "user", pw->pw_name, NULL);
+        auth_request_set_field(request, "user", pw.pw_name, NULL);
 
 	callback(PASSDB_RESULT_OK, request);
 }
diff -r 7f5735ab7c35 -r 02829f7f79c7 src/auth/passdb-passwd.c
--- a/src/auth/passdb-passwd.c	Thu Mar 17 16:20:12 2011 +0200
+++ b/src/auth/passdb-passwd.c	Thu Mar 17 16:21:00 2011 +0200
@@ -6,8 +6,7 @@
 #ifdef PASSDB_PASSWD
 
 #include "safe-memset.h"
-
-#include <pwd.h>
+#include "ipwd.h"
 
 #define PASSWD_CACHE_KEY "%u"
 #define PASSWD_PASS_SCHEME "CRYPT"
@@ -16,35 +15,40 @@
 passwd_verify_plain(struct auth_request *request, const char *password,
 		    verify_plain_callback_t *callback)
 {
-	struct passwd *pw;
+	struct passwd pw;
 	int ret;
 
 	auth_request_log_debug(request, "passwd", "lookup");
 
-	pw = getpwnam(request->user);
-	if (pw == NULL) {
+	switch (i_getpwnam(request->user, &pw)) {
+	case -1:
+		auth_request_log_error(request, "passwd",
+				       "getpwnam() failed: %m");
+		callback(PASSDB_RESULT_INTERNAL_FAILURE, request);
+		return;
+	case 0:
 		auth_request_log_info(request, "passwd", "unknown user");
 		callback(PASSDB_RESULT_USER_UNKNOWN, request);
 		return;
 	}
 
-	if (!IS_VALID_PASSWD(pw->pw_passwd)) {
+	if (!IS_VALID_PASSWD(pw.pw_passwd)) {
 		auth_request_log_info(request, "passwd",
-			"invalid password field '%s'", pw->pw_passwd);
+			"invalid password field '%s'", pw.pw_passwd);
 		callback(PASSDB_RESULT_USER_DISABLED, request);
 		return;
 	}
 
 	/* save the password so cache can use it */
-	auth_request_set_field(request, "password", pw->pw_passwd,
+	auth_request_set_field(request, "password", pw.pw_passwd,
 			       PASSWD_PASS_SCHEME);
 
 	/* check if the password is valid */
-	ret = auth_request_password_verify(request, password, pw->pw_passwd,
+	ret = auth_request_password_verify(request, password, pw.pw_passwd,
 					   PASSWD_PASS_SCHEME, "passwd");
 
 	/* clear the passwords from memory */
-	safe_memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
+	safe_memset(pw.pw_passwd, 0, strlen(pw.pw_passwd));
 
 	if (ret <= 0) {
 		callback(PASSDB_RESULT_PASSWORD_MISMATCH, request);
@@ -52,7 +56,7 @@
 	}
 
 	/* make sure we're using the username exactly as it's in the database */
-        auth_request_set_field(request, "user", pw->pw_name, NULL);
+        auth_request_set_field(request, "user", pw.pw_name, NULL);
 
 	callback(PASSDB_RESULT_OK, request);
 }
diff -r 7f5735ab7c35 -r 02829f7f79c7 src/auth/userdb-passwd.c
--- a/src/auth/userdb-passwd.c	Thu Mar 17 16:20:12 2011 +0200
+++ b/src/auth/userdb-passwd.c	Thu Mar 17 16:21:00 2011 +0200
@@ -6,10 +6,9 @@
 #ifdef USERDB_PASSWD
 
 #include "ioloop.h"
+#include "ipwd.h"
 #include "userdb-static.h"
 
-#include <pwd.h>
-
 #define USER_CACHE_KEY "%u"
 
 struct passwd_userdb_module {
@@ -32,18 +31,23 @@
 	struct userdb_module *_module = auth_request->userdb->userdb;
 	struct passwd_userdb_module *module =
 		(struct passwd_userdb_module *)_module;
-	struct passwd *pw;
+	struct passwd pw;
 
 	auth_request_log_debug(auth_request, "passwd", "lookup");
 
-	pw = getpwnam(auth_request->user);
-	if (pw == NULL) {
+	switch (i_getpwnam(auth_request->user, &pw)) {
+	case -1:
+		auth_request_log_error(auth_request, "passwd",
+				       "getpwnam() failed: %m");
+		callback(USERDB_RESULT_INTERNAL_FAILURE, auth_request);
+		return;
+	case 0:
 		auth_request_log_info(auth_request, "passwd", "unknown user");
 		callback(USERDB_RESULT_USER_UNKNOWN, auth_request);
 		return;
 	}
 
-	auth_request_set_field(auth_request, "user", pw->pw_name, NULL);
+	auth_request_set_field(auth_request, "user", pw.pw_name, NULL);
 
 	auth_request_init_userdb_reply(auth_request);
 	userdb_static_template_export(module->tmpl, auth_request);
@@ -53,18 +57,18 @@
 	    !userdb_static_template_isset(module->tmpl, "system_user")) {
 		auth_request_set_userdb_field(auth_request,
 					      "system_groups_user",
-					      pw->pw_name);
+					      pw.pw_name);
 	}
 	if (!userdb_static_template_isset(module->tmpl, "uid")) {
 		auth_request_set_userdb_field(auth_request,
-					      "uid", dec2str(pw->pw_uid));
+					      "uid", dec2str(pw.pw_uid));
 	}
 	if (!userdb_static_template_isset(module->tmpl, "gid")) {
 		auth_request_set_userdb_field(auth_request,
-					      "gid", dec2str(pw->pw_gid));
+					      "gid", dec2str(pw.pw_gid));
 	}
 	if (!userdb_static_template_isset(module->tmpl, "home"))
-		auth_request_set_userdb_field(auth_request, "home", pw->pw_dir);
+		auth_request_set_userdb_field(auth_request, "home", pw.pw_dir);
 
 	callback(USERDB_RESULT_OK, auth_request);
 }
diff -r 7f5735ab7c35 -r 02829f7f79c7 src/auth/userdb.c
--- a/src/auth/userdb.c	Thu Mar 17 16:20:12 2011 +0200
+++ b/src/auth/userdb.c	Thu Mar 17 16:21:00 2011 +0200
@@ -2,12 +2,11 @@
 
 #include "auth-common.h"
 #include "array.h"
+#include "ipwd.h"
 #include "auth-worker-server.h"
 #include "userdb.h"
 
 #include <stdlib.h>
-#include <pwd.h>
-#include <grp.h>
 
 static ARRAY_DEFINE(userdb_interfaces, struct userdb_module_interface *);
 static ARRAY_DEFINE(userdb_modules, struct userdb_module *);
@@ -61,7 +60,7 @@
 
 uid_t userdb_parse_uid(struct auth_request *request, const char *str)
 {
-	struct passwd *pw;
+	struct passwd pw;
 	uid_t uid;
 
 	if (str == NULL)
@@ -70,20 +69,24 @@
 	if (str_to_uid(str, &uid) == 0)
 		return uid;
 
-	pw = getpwnam(str);
-	if (pw == NULL) {
+	switch (i_getpwnam(str, &pw)) {
+	case -1:
+		i_error("getpwnam() failed: %m");
+		return (uid_t)-1;
+	case 0:
 		if (request != NULL) {
 			auth_request_log_error(request, "userdb",
 					       "Invalid UID value '%s'", str);
 		}
 		return (uid_t)-1;
+	default:
+		return pw.pw_uid;
 	}
-	return pw->pw_uid;
 }
 
 gid_t userdb_parse_gid(struct auth_request *request, const char *str)
 {
-	struct group *gr;
+	struct group gr;
 	gid_t gid;
 
 	if (str == NULL)
@@ -92,15 +95,19 @@
 	if (str_to_gid(str, &gid) == 0)
 		return gid;
 
-	gr = getgrnam(str);
-	if (gr == NULL) {
+	switch (i_getgrnam(str, &gr)) {
+	case -1:
+		i_error("getgrnam() failed: %m");
+		return (gid_t)-1;
+	case 0:
 		if (request != NULL) {
 			auth_request_log_error(request, "userdb",
 					       "Invalid GID value '%s'", str);
 		}
 		return (gid_t)-1;
+	default:
+		return gr.gr_gid;
 	}
-	return gr->gr_gid;
 }
 
 static struct userdb_module *
diff -r 7f5735ab7c35 -r 02829f7f79c7 src/lda/main.c
--- a/src/lda/main.c	Thu Mar 17 16:20:12 2011 +0200
+++ b/src/lda/main.c	Thu Mar 17 16:21:00 2011 +0200
@@ -10,6 +10,7 @@
 #include "abspath.h"
 #include "safe-mkstemp.h"
 #include "eacces-error.h"
+#include "ipwd.h"
 #include "mkdir-parents.h"
 #include "str.h"
 #include "str-sanitize.h"
@@ -31,7 +32,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <pwd.h>
 #include <sysexits.h>
 
 #define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
@@ -315,18 +315,21 @@
 		;
 	else if (process_euid != 0) {
 		/* we're non-root. get our username and possibly our home. */
-		struct passwd *pw;
+		struct passwd pw;
 		const char *home;
 
 		home = getenv("HOME");
 		if (user != NULL && home != NULL) {
 			/* no need for a pw lookup */
 			user_source = "USER environment";


More information about the dovecot-cvs mailing list