Module Name:    src
Committed By:   bouyer
Date:           Wed Feb 16 21:07:21 UTC 2011

Modified Files:
        src/usr.sbin/ypserv/ypxfr [netbsd-5]: ypxfr.c

Log Message:
Pull up following revision(s) (requested by chuck in ticket #1550):
        usr.sbin/ypserv/ypxfr/ypxfr.c: revision 1.16 - 1.19
Fix WARNS=4 issues (-Wshadow -Wcast-qual -Wsign-compare)
Follow upstream's lead and remove third and fourth clauses (except on
from usr.sbin/mopd/common/pf.c, where only the ad clause is removed,
because it has a shared UCB copyright) on Mats O Jansson's files.
thorpej OK'd usr.sbin/rpc.yppasswdd/yppasswdd_mkpw.c, where he shares
copyright.
yppush RPC is supposed to return yppush_status error/status codes
(e.g. YPPUSH_SUCC, YPPUSH_NOMAP, etc.).
Functions like yp_master, yp_order_host, yp_master_host, and
yp_match_host return YPERR-style error codes.  YPERR-style error
codes do not directly correspond to yppush_status codes, so it
is a bad idea to just directly YPERR codes into the yppush return
status value (it causes failing yppush runs to print confusing
error messages).
Change ypxfr.c to convert the YPERR codes to YPPUSH codes.
rev 1.18 un-covered some bugs:
 1. add_interdomain and add_secure are optional... not all maps use these
    keys.  if we are unable to add them due to a YPERR_KEY (meaning they
    are not being used), then we should not flag this as a general error.
 2. if we have a failure (status <= 0) we unlink_db() the temp database
    as part of error handling.   but we should not overwrite our error
    status code with the return value from unlink_db() because if the
    unlink_db() works (likely true) than that success will wipe out our
    error code and the calling yppush will think we worked.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.15.8.1 src/usr.sbin/ypserv/ypxfr/ypxfr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ypserv/ypxfr/ypxfr.c
diff -u src/usr.sbin/ypserv/ypxfr/ypxfr.c:1.15 src/usr.sbin/ypserv/ypxfr/ypxfr.c:1.15.8.1
--- src/usr.sbin/ypserv/ypxfr/ypxfr.c:1.15	Fri Feb 29 03:00:47 2008
+++ src/usr.sbin/ypserv/ypxfr/ypxfr.c	Wed Feb 16 21:07:21 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: ypxfr.c,v 1.15 2008/02/29 03:00:47 lukem Exp $	*/
+/*	$NetBSD: ypxfr.c,v 1.15.8.1 2011/02/16 21:07:21 bouyer Exp $	*/
 
 /*
  * Copyright (c) 1994 Mats O Jansson <[email protected]>
@@ -12,11 +12,6 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by Mats O Jansson
- * 4. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@@ -33,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ypxfr.c,v 1.15 2008/02/29 03:00:47 lukem Exp $");
+__RCSID("$NetBSD: ypxfr.c,v 1.15.8.1 2011/02/16 21:07:21 bouyer Exp $");
 #endif
 
 #include <sys/param.h>
@@ -63,6 +58,7 @@
 
 DBM	*db;
 
+static	int yperr2yppush(int);
 static	int ypxfr_foreach(int, char *, int, char *, int, char *);
 
 int	main(int, char *[]);
@@ -155,7 +151,7 @@
 		fprintf(stderr, "usage: %s [-cf] [-d domain] [-h host] %s\n",
 		   getprogname(),
 		   "[-s domain] [-C tid prog ipadd port] mapname");
-		goto punt;
+		exit(1);
 	}
 
 #ifdef DEBUG
@@ -195,7 +191,7 @@
 		if (status == 0)
 			status = YPPUSH_SUCC;
 		else {
-			status = -status;
+			status = YPPUSH_MADDR;
 			goto punt;
 		}
 	}
@@ -245,7 +241,7 @@
 		if (status > 0)
 			status = install_db(domain, map, temp_map);
 		else
-			status = unlink_db(domain, map, temp_map);
+			(void) unlink_db(domain, map, temp_map);
 	}
 	
  punt:
@@ -271,6 +267,30 @@
 	exit (0);
 }
 
+/*
+ * yperr2yppush: convert error codes from functions like yp_order_host,
+ * yp_master_host, and yp_match_host into YPPUSH rpc status values.
+ */
+static int
+yperr2yppush(int yperr) {
+	switch (yperr) {
+	case YPERR_DOMAIN:
+		return(YPPUSH_NODOM);
+	case YPERR_MAP:
+		return(YPPUSH_NOMAP);
+	case YPERR_KEY:
+		return(YPPUSH_YPERR);
+	case YPERR_BADDB:
+		return(YPPUSH_YPERR);
+	}
+
+	/*
+	 * generic error status for the rest (BADARGS, RPC, YPERR, RESRC,
+	 * NOMORE, PMAP, YPBIND, YPSERV, NODOM, VERS, ACCESS, BUSY)
+	 */
+	return(YPPUSH_XFRERR);   /* generic error status */
+}
+
 static int
 ypxfr_foreach(int status, char *keystr, int keylen, char *valstr,
 	      int vallen, char *data)
@@ -289,6 +309,7 @@
 	val.dptr = valstr;
 	val.dsize = strlen(valstr);
 
+        /* XXX: suspect... ignoring return value here */
 	ypdb_store(db, key, val, YPDB_INSERT);
 
 	return (0);
@@ -301,9 +322,9 @@
 	char order_key[] = YP_LAST_KEY;
 	char order[MAX_LAST_LEN+1];
 	struct stat finfo;
-	DBM *db;
+	DBM *ldb;
 	datum k, v;
-	int status;
+	unsigned int status;
 
 	status = YPPUSH_SUCC;
 
@@ -328,8 +349,8 @@
 	/* Open the map file. */
 	snprintf(map_path, sizeof(map_path), "%s/%s/%s",
 	    YP_DB_PATH, domain, map);
-	db = ypdb_open(map_path);
-	if (db == NULL) {
+	ldb = ypdb_open(map_path);
+	if (ldb == NULL) {
 		status = YPPUSH_DBM;
 		goto out;
 	}
@@ -337,7 +358,7 @@
 	k.dptr = (char *)&order_key;
 	k.dsize = YP_LAST_LEN;
 
-	v = ypdb_fetch(db, k);
+	v = ypdb_fetch(ldb, k);
 
 	if (v.dptr == NULL)
 		*lordernum = 0;
@@ -346,7 +367,7 @@
 		order[v.dsize] = '\0';
 		*lordernum = (u_int)atoi((char *)&order);
 	}
-	ypdb_close(db);
+	ypdb_close(ldb);
 
  out:
 	if ((status == YPPUSH_NOMAP) || (status == YPPUSH_DBM)) {
@@ -370,6 +391,8 @@
 			status = YPPUSH_AGE;
 		else
 			status = YPPUSH_SUCC;
+	} else {
+		status = yperr2yppush(status);
 	}
 
 	return status;
@@ -387,14 +410,14 @@
 create_db(char *domain, char *map, char *db_temp, size_t db_temp_len)
 {
 	static const char template[] = "ypdbXXXXXX";
-	DBM *db;
+	DBM *ldb;
 
 	snprintf(db_temp, db_temp_len, "%s/%s/%s",
 	    YP_DB_PATH, domain, template);
 
-	db = ypdb_mktemp(db_temp);
+	ldb = ypdb_mktemp(db_temp);
 
-	return db;
+	return ldb;
 }
 
 int
@@ -426,7 +449,7 @@
 }
 
 int
-add_order(DBM *db, u_int ordernum)
+add_order(DBM *ldb, u_int ordernum)
 {
 	char datestr[11];
 	datum key, val;
@@ -441,7 +464,7 @@
 	val.dptr = datestr;
 	val.dsize = strlen(datestr);
 	
-	status = ypdb_store(db, key, val, YPDB_INSERT);
+	status = ypdb_store(ldb, key, val, YPDB_INSERT);
 	if(status >= 0)
 		status = YPPUSH_SUCC;
 	else
@@ -451,7 +474,7 @@
 }
 
 int
-add_master(CLIENT *client, char *domain, char *map, DBM *db)
+add_master(CLIENT *client, char *domain, char *map, DBM *ldb)
 {
 	char keystr[] = YP_MASTER_KEY;
 	char *master;
@@ -470,18 +493,20 @@
 		val.dptr = master;
 		val.dsize = strlen(master);
 
-		status = ypdb_store(db, key, val, YPDB_INSERT);
+		status = ypdb_store(ldb, key, val, YPDB_INSERT);
 		if (status >= 0)
 			status = YPPUSH_SUCC;
 		else
 			status = YPPUSH_DBM;
+	} else {
+		status = yperr2yppush(status);
 	}
 
 	return status;
 }
 
 int
-add_interdomain(CLIENT *client, char *domain, char *map, DBM *db)
+add_interdomain(CLIENT *client, char *domain, char *map, DBM *ldb)
 {
 	char keystr[] = YP_INTERDOMAIN_KEY;
 	char *value;
@@ -496,24 +521,29 @@
 	status = yp_match_host(client, domain, map,
 	    k.dptr, k.dsize, &value, &vallen);
 
-	if (status == 0 && value) {
+	if (status == YPERR_KEY) {
+		/* this is an optional key/val, so it may not be present */
+		status = YPPUSH_SUCC;  
+	} else if (status == 0 && value) {
 		v.dptr = value;
 		v.dsize = vallen;
 		
 		if (v.dptr != NULL) {
-			status = ypdb_store(db, k, v, YPDB_INSERT);
+			status = ypdb_store(ldb, k, v, YPDB_INSERT);
 			if (status >= 0)
 				status = YPPUSH_SUCC;
 			else
 				status = YPPUSH_DBM;
 		}
+	} else {
+		status = yperr2yppush(status);
 	}
 
 	return status;
 }
 
 int
-add_secure(CLIENT *client, char *domain, char *map, DBM *db)
+add_secure(CLIENT *client, char *domain, char *map, DBM *ldb)
 {
 	char keystr[] = YP_SECURE_KEY;
 	char *value;
@@ -528,17 +558,22 @@
 	status = yp_match_host(client, domain, map,
 	    k.dptr, k.dsize, &value, &vallen);
 	
-	if (status == 0 && value > 0) {
+	if (status == YPERR_KEY) {
+		/* this is an optional key/val, so it may not be present */
+		status = YPPUSH_SUCC;  
+	} else if (status == 0 && value != 0) {
 		v.dptr = value;
 		v.dsize = vallen;
 		
 		if (v.dptr != NULL) {
-			status = ypdb_store(db, k, v, YPDB_INSERT);
+			status = ypdb_store(ldb, k, v, YPDB_INSERT);
 			if (status >= 0)
 				status = YPPUSH_SUCC;
 			else
 				status = YPPUSH_DBM;
 		}
+	} else {
+		status = yperr2yppush(status);
 	}
 
 	return status;

Reply via email to