Subject: Re: CVS commit: src/include
To: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
From: Elad Efrat <elad@NetBSD.org>
List: source-changes
Date: 02/18/2006 15:29:15
This is a multi-part message in MIME format.
--------------050702040602000704060104
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

YAMAMOTO Takashi wrote:

> does really "alternate policy" need to be application specific?

not sure i understand what you're saying here...

> IMO, hiding implementation details is a good enough reason to use malloc.

see attached diff -- would something like that be okay?

-e.

-- 
Elad Efrat

--------------050702040602000704060104
Content-Type: text/plain;
 name="pw_policy_opaque.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="pw_policy_opaque.diff"

Index: include/util.h
===================================================================
RCS file: /cvsroot/src/include/util.h,v
retrieving revision 1.39
diff -u -p -r1.39 util.h
--- include/util.h	18 Feb 2006 10:53:33 -0000	1.39
+++ include/util.h	18 Feb 2006 13:27:44 -0000
@@ -56,9 +56,6 @@
 #define	PW_POLICY_BYPASSWD	1
 #define	PW_POLICY_BYGROUP	2
 
-#define PW_POLICY_INIT	{ -1, -1, -1, -1, -1, -1, -1, \
-			  -1, -1, -1, -1, -1, -1, -1 }
-
 __BEGIN_DECLS
 struct disklabel;
 struct iovec;
@@ -67,23 +64,7 @@ struct termios;
 struct utmp;
 struct winsize;
 struct sockaddr;
-
-struct pw_policy {
-	int32_t minlen;
-	int32_t maxlen;
-	int32_t minupper;
-	int32_t maxupper;
-	int32_t minlower;
-	int32_t maxlower;
-	int32_t mindigits;
-	int32_t maxdigits;
-	int32_t minpunct;
-	int32_t maxpunct;
-	int32_t mintoggles;
-	int32_t maxtoggles;
-	int32_t minclasses;
-	int32_t maxclasses;
-};
+struct pw_policy;
 
 pid_t		forkpty(int *, char *, struct termios *, struct winsize *);
 const char     *getbootfile(void);
@@ -118,8 +99,9 @@ const char     *pw_getprefix(void);
 void		pw_init(void);
 int		pw_lock(int);
 int		pw_mkdb(const char *, int);
-int		pw_policy_load(struct pw_policy *, void *, int);
+struct pw_policy *pw_policy_load(void *, int);
 int		pw_policy_test(struct pw_policy *, char *);
+void		pw_policy_free(struct pw_policy *);
 void		pw_prompt(void);
 int		pw_setprefix(const char *);
 int		secure_path(const char *);
Index: lib/libutil/pw_policy.c
===================================================================
RCS file: /cvsroot/src/lib/libutil/pw_policy.c,v
retrieving revision 1.3
diff -u -p -r1.3 pw_policy.c
--- lib/libutil/pw_policy.c	18 Feb 2006 10:52:48 -0000	1.3
+++ lib/libutil/pw_policy.c	18 Feb 2006 13:27:44 -0000
@@ -59,6 +59,23 @@ static int pw_policy_handle_charclass(HA
 static int pw_policy_handle_nclasses(HANDLER_PROTO);
 static int pw_policy_handle_ntoggles(HANDLER_PROTO);
 
+struct pw_policy {
+	int32_t minlen;
+	int32_t maxlen;
+	int32_t minupper;
+	int32_t maxupper;
+	int32_t minlower;
+	int32_t maxlower;
+	int32_t mindigits;
+	int32_t maxdigits;
+	int32_t minpunct;
+	int32_t maxpunct;
+	int32_t mintoggles;
+	int32_t maxtoggles;
+	int32_t minclasses;
+	int32_t maxclasses;
+};
+
 struct pw_policy_handler {
 	const char *name;
 	int (*handler)(HANDLER_PROTO);
@@ -322,18 +339,19 @@ pw_policy_handle_ntoggles(HANDLER_ARGS)
 	return (0);
 }
 
-int
-pw_policy_load(struct pw_policy *policy, void *key, int how)
+struct pw_policy *
+pw_policy_load(void *key, int how)
 {
+	struct pw_policy *policy;
 	struct pw_policy_handler *hp;
 	char buf[BUFSIZ];
 
 	/* If there's no /etc/passwd.conf, don't touch the policy. */
-	if (access(_PATH_PASSWD_CONF, R_OK) == -1)
-		return (ENOENT);
+	if (access(_PATH_PASSWD_CONF, R_OK) == -1) {
+		errno = ENOENT;
 
-	if (policy == NULL)
-		return (EFAULT);
+		return (NULL);
+	}
 
 	/* No key provided. Use default. */
 	if (key == NULL) {
@@ -399,10 +417,18 @@ pw_policy_load(struct pw_policy *policy,
 		 * Fail the policy because we don't know how to parse the
 		 * key we were passed.
 		 */
-		return (EINVAL);
+		errno = EINVAL;
+
+		return (NULL);
 	}
 
  load_policies:
+	policy = malloc(sizeof(*policy));
+	if (policy == NULL)
+		return (NULL);
+
+	memset(policy, 0, sizeof(*policy));
+
 	hp = &handlers[0];
 	while (hp->name != NULL) {
 		int error;
@@ -412,14 +438,16 @@ pw_policy_load(struct pw_policy *policy,
 		if (*buf) {
 			error = hp->handler(policy, LOAD_POLICY, NULL, buf,
 					    hp->arg2);
-			if (error)
-				return (error);
+			if (error) {
+				errno = error;
+				return (NULL);
+			}
 		}
 
 		hp++;
 	}
 
-	return (0);
+	return (policy);
 }
 
 int
@@ -443,3 +471,9 @@ pw_policy_test(struct pw_policy *policy,
 
 	return (0);
 }
+
+void
+pw_policy_free(struct pw_policy *policy)
+{
+	free(policy);
+}

--------------050702040602000704060104--