[PC-BSD Commits] r17962 - pcbsd-projects/AD_4_PCBSD/krb

svn at pcbsd.org svn at pcbsd.org
Thu Jul 26 13:02:36 PDT 2012


Author: johnh
Date: 2012-07-26 20:02:36 +0000 (Thu, 26 Jul 2012)
New Revision: 17962

Modified:
   pcbsd-projects/AD_4_PCBSD/krb/krbconf.c
   pcbsd-projects/AD_4_PCBSD/krb/krbconf.h
Log:
progress commit. Working delete. modify and add are up next.



Modified: pcbsd-projects/AD_4_PCBSD/krb/krbconf.c
===================================================================
--- pcbsd-projects/AD_4_PCBSD/krb/krbconf.c	2012-07-26 19:38:15 UTC (rev 17961)
+++ pcbsd-projects/AD_4_PCBSD/krb/krbconf.c	2012-07-26 20:02:36 UTC (rev 17962)
@@ -7,33 +7,17 @@
 #include "krb5-parser.tab.h"
 #include "krb5-parser.tab.i"
 
-#define	KRB_HASH_MAX	1024
-
-#define	KRB_OP_ADD	1
+#define	KRB_OP_ADD		1
 #define	KRB_OP_MODIFY	2
-#define	KRB_OP_DELETE	3
+#define	KRB_OP_REMOVE	3
 
-static char *krb_sections[] = {
-	KRB_S_APPDEFAULTS,
-	KRB_S_LIBDEFAULTS,
-	KRB_S_DOMAIN_REALM,
-	KRB_S_REALMS,
-	KRB_S_CAPATHS,
-	KRB_S_LOGGING,
-	KRB_S_KDC,
-	KRB_S_KADMIN,
-	KRB_S_PASSWORD_QUALITY
-};
-static int krb_sections_size =
-	sizeof(krb_sections) / sizeof(krb_sections[0]);
-
 struct krb_modification {
 	TAILQ_ENTRY(krb_modification) entries;	
-	struct krb_section *s;
+	char *modstr;
 	int op;
 };
-TAILQ_HEAD(krb_modification_list, krb_modification) krb_modifications = 
-	TAILQ_HEAD_INITIALIZER(krb_modifications);
+TAILQ_HEAD(krb_modification_list, krb_modification) modifications = 
+	TAILQ_HEAD_INITIALIZER(modifications);
 
 struct krb_entry_list fentries = TAILQ_HEAD_INITIALIZER(fentries);
 struct krb_entry_list krbconf = TAILQ_HEAD_INITIALIZER(krbconf);
@@ -102,107 +86,54 @@
  		"\t-m <(+|-|^)string>\n"
 		"\n"
 		"Examples:\n\n"
-		"krbconf -f /etc/krb5.conf -m +appdefaults/pam/debug=true\n"
+		"krbconf -f /etc/krb5.conf -m +appdefaults.pam.debug=true\n"
 		"# Adds binding debug = true to the pam binding in the appdefaults section\n\n"
-		"krbconf -f /etc/krb5.conf -m -logging/default\n"
+		"krbconf -f /etc/krb5.conf -m -logging.default\n"
 		"# Removes the default binding from the logging section\n\n"
-		"krbconf -f /etc/krb5.conf -m ^libdefaults/clockskew=300\n"
+		"krbconf -f /etc/krb5.conf -m ^libdefaults.clockskew=300\n"
 		"# Updates the clockscew binding to 300 in the libdefaults section\n\n"
 	);
 
+
+
 	exit(1);
 }
 
-#if 0
 static int
 add_modification(const char *m)
 {
-	int i, j;
-	char *l, *r, *s, *str, *ptr, *tokens[32];
 	struct krb_modification *km;
-	struct krb_section *ks;
 
 	if (m == NULL)
 		return (-1);
 
-	km = malloc(sizeof(*km));
+	km = xalloc(sizeof(*km));
 	switch (m[0]) {
 		case '+':
 			km->op = KRB_OP_ADD;
+			m += 1;
 			break;
+
 		case '-':
-			km->op = KRB_OP_DELETE;
+			km->op = KRB_OP_REMOVE;
+			m += 1;
 			break;
+
 		case '^':
 			km->op = KRB_OP_MODIFY;
+			m += 1;
 			break;
+
 		default:
+			xfree(&km);
 			return (-1);
 	}
 
-	str = strdup(&m[1]);
-	ptr = str;
+	km->modstr = xstrdup(m);
+	TAILQ_INSERT_TAIL(&modifications, km, entries);
 
-	/* value */
-	l = strsep(&ptr, "=");
-	r = ptr;
-	ptr = l;
-
-	i = 0;
-	while ((s = strsep(&ptr, "/")) != NULL) {
-		tokens[i++] = s;
-	}
-	
-	if (i <= 1) {
-		free(km);
-		free(str);
-		return (-1);
-	}
-
-	/*
-	 *	Create new section
-	 */
-	s = tokens[0];
-	ks = malloc(sizeof(*ks));
-	ks->name = strdup(s);
-	TAILQ_INIT(&ks->bindings);
-
-	/*
-	 *	Add section to modification list
-	 */
-	km->s = ks;
-	TAILQ_INSERT_TAIL(&krb_modifications, km, entries);
-
-	/*
-	 *	Create new bindings
-	 */
-	j = i - 1;
-	for (i = 1;i <= j;i++) {
-		struct krb_binding *b = malloc(sizeof(*b));
-		b->name = strdup(tokens[i]);
-		b->value = NULL;
-		b->flags = 0;
-		TAILQ_INSERT_TAIL(&ks->bindings, b, entries);
-	}
-
-	/*
-	 *	Create last binding with a name and value
-	 */
-	l = tokens[j];
-	{
-		struct krb_binding *b = malloc(sizeof(*b));
-		b->name = strdup(l);
-		b->value = NULL;
-		b->flags = 0;
-		if (r)
-			b->value = strdup(r);
-	}
-
-	free(str);
-
 	return (0);
 }
-#endif
 
 
 static void
@@ -290,6 +221,7 @@
 
 	/*
 	 *	Free everything from the fentries list.
+	 *	This is cheating.
 	 */
 	TAILQ_FOREACH_SAFE(ke, &fentries, fentries, ketmp) {
 		switch (ke->type) {
@@ -312,6 +244,475 @@
 	}
 }
 
+struct krb_entry *
+section2entry(const char *section)
+{
+	int index;
+	int count;
+	char *ptr, *name;
+	struct krb_entry *ke, *ketmp, *entry = NULL;
+
+	if (section == NULL || section[0] == 0)
+		return (NULL);
+
+	index = -1;
+	name = xstrdup(section);
+	ptr = &name[strlen(name) - 1];
+	if (isdigit(*ptr)) {
+		while (isdigit(*(ptr - 1)))
+			ptr--;
+		index = strtol(ptr, NULL, 10);
+		*ptr = 0;
+	}
+
+	count = 0;
+	TAILQ_FOREACH_SAFE(ke, &krbconf, entries, ketmp) {
+		if (ke->type == KRB_ENTRY_SECTION &&
+			strcasecmp(ke->kes_name, name) == 0) {
+			if (index < 0 || index == count) {
+				entry = ke;
+				break;
+			}
+
+			count++;
+		}
+	}
+
+	xfree(&name);
+	return (entry);
+}
+
+struct krb_entry *
+binding2entry(struct krb_entry_list *el, const char *binding)
+{
+	int index;
+	int count;
+	char *ptr, *name;
+	struct krb_entry *ke, *ketmp, *entry = NULL;
+
+	if (el == NULL || binding == NULL || binding[0] == 0)
+		return (NULL);
+
+	index = -1;
+	name = xstrdup(binding);
+	ptr = &name[strlen(name) - 1];
+	if (isdigit(*ptr)) {
+		while (isdigit(*(ptr - 1)))
+			ptr--;
+		index = strtol(ptr, NULL, 10);
+		*ptr = 0;
+	}
+
+	count = 0;
+	TAILQ_FOREACH_SAFE(ke, el, entries, ketmp) {
+		if ((ke->type == KRB_ENTRY_BINDING ||
+			ke->type == KRB_ENTRY_BINDING_START) &&
+			strcasecmp(ke->keb_name, name) == 0) {
+			if (index < 0 || index == count) {
+				entry = ke;
+				break;
+			}
+		}
+
+		count++;
+	}
+
+	return (entry);
+}
+
+struct krb_entry *
+get_binding_parent(struct krb_entry *kp, struct krb_entry *kc)
+{
+	struct krb_entry *ke, *ketmp, *parent = NULL;
+	struct krb_entry_list *el;
+
+	if (kp != NULL && kp->type == KRB_ENTRY_SECTION) {
+		el = &kp->kes_bindings;
+	} else {
+		el = &kp->keb_bindings;
+	}
+
+	if (kp != NULL) {
+		TAILQ_FOREACH_SAFE(ke, el, entries, ketmp) {
+			if (ke == kc) {
+				parent = kp;
+				break;
+			}
+
+			if (ke->keb_nbindings > 0) {
+				parent = get_binding_parent(ke, kc);
+				if (parent != NULL)
+					 break;
+			}
+		}
+	}
+
+	return (parent);
+}
+
+struct krb_entry *
+get_entry_parent(struct krb_entry *ep, struct krb_entry *kc)
+{
+	struct krb_entry *ke, *ketmp, *parent = NULL;
+
+	TAILQ_FOREACH_SAFE(ke, &krbconf, entries, ketmp) {
+		if (ke == kc)
+			break;
+
+		if (ke->kes_nbindings > 0) {
+			parent = get_binding_parent(ke, kc);
+			if (parent != NULL)
+				break;
+		}
+	}
+
+	return (parent);
+}
+
+struct krb_entry_list *
+get_entry_list(struct krb_entry_list *el, struct krb_entry *kc)
+{
+	struct krb_entry *ke, *ketmp;
+	struct krb_entry_list *fel = NULL;
+
+	if (el == NULL) {
+		return (get_entry_list(&krbconf, kc));
+
+	} else {
+
+		TAILQ_FOREACH_SAFE(ke, el, entries, ketmp) {
+			switch (ke->type) {
+				case KRB_ENTRY_SECTION:
+					if (ke->kes_nbindings > 0) {
+						if ((fel = get_entry_list(&ke->kes_bindings, kc)) != NULL)
+							return (fel);
+						fel = NULL;
+					}
+					break;
+
+				case KRB_ENTRY_BINDING:
+				case KRB_ENTRY_BINDING_START:
+					if (ke->keb_nbindings > 0) {
+						if ((fel = get_entry_list(&ke->keb_bindings, kc)) != NULL)
+							return (fel);
+
+						fel = NULL;
+					}
+					break;
+			}
+
+			if (ke == kc) {
+				fel = el;
+				break;
+			}
+		}
+	}
+
+	return (fel);
+}
+
+struct krb_entry *
+km2entry(struct krb_modification *km)
+{
+	struct krb_entry *ke = NULL;
+	int i, bsize, nbindings, bmax = 128;
+	char *tmp, *ptr, *str, *save, *last, *buf, *bptr;
+	char *bindings[bmax];
+	char *section;
+
+	if (km == NULL)
+		return (ke);
+
+	save = xstrdup(km->modstr);
+	ptr = save;
+
+	/*
+	 *	+appdefaults.foo=bar
+	 *	+appdefaults.foo.bar=val
+	 *
+	 *	-appdefaults.foo
+	 *
+	 *	^appdefaults.foo=bar
+	 */
+
+	tmp = strsep(&ptr, "=");	
+	last = str = tmp;
+
+	bsize = 8192;
+	buf = xalloc(bsize);
+	bptr = buf;
+
+	i = nbindings = 0;
+	while (*tmp != 0) {
+		if (*tmp == '.' && *last != '\\' || *(tmp + 1) == 0) {
+			if (*tmp == '.' && *last != '\\') {
+				*bptr = 0;
+				bptr = buf;
+
+			} else if (*(tmp + 1) == 0) {
+				*bptr++ = *tmp;
+				*bptr = 0;
+				bptr = buf;
+			}
+
+			switch (i) {
+				case 0:
+					section = xstrdup(buf);
+					break;
+
+				default:
+					bindings[nbindings++] = xstrdup(buf);
+					break;
+			}
+
+			i++;
+
+		} else if (*tmp == '.' && *last == '\\') {
+			*(bptr - 1) = *tmp;
+
+		} else {
+			*bptr++ = *tmp;
+		}
+
+		last = tmp;
+		tmp++;
+	}
+
+	if (section != NULL) {
+		ke = section2entry(section);
+
+		if (ke != NULL && nbindings > 0) {
+			struct krb_entry_list *el = &ke->kes_bindings;
+
+			for (i = 0;i < nbindings;i++) {
+				ke = binding2entry(el, bindings[i]);
+				if (ke == NULL)
+					break;
+
+				el = &ke->keb_bindings;
+			}
+		}
+	}
+
+	xfree(&save);
+	return (ke);
+}
+
+static int
+krb_op_add(struct krb_entry *ke)
+{
+	return (0);
+}
+
+static int
+krb_op_modify(struct krb_entry *ke)
+{
+	return (0);
+}
+
+static int
+krb_fentries_remove_section(struct krb_entry *ke)
+{
+	int delete = 0;
+	struct krb_entry *ks, *kstmp;
+
+	TAILQ_FOREACH_SAFE(ks, &fentries, fentries, kstmp) {
+		if (ks == ke) {
+			TAILQ_REMOVE(&fentries, ks, fentries);
+			delete = 1;
+			continue;
+		}
+
+		if (ks->type == KRB_ENTRY_SECTION && delete == 1)
+			break;
+
+		if (delete == 1) {
+			TAILQ_REMOVE(&fentries, ks, fentries);
+			switch (ks->type) {
+				case KRB_ENTRY_COMMENT:
+					xfree(&ks->kec_text);
+					xfree(&ks);
+					break;
+
+				case KRB_ENTRY_NULL:
+				case KRB_ENTRY_BINDING_END:
+					xfree(&ks);
+					break;
+			}
+		}
+	}
+
+	return (0);
+}
+
+static int
+krb_fentries_remove_binding(struct krb_entry *ke)
+{
+	int bs, delete;
+	struct krb_entry *kb, *kbtmp;
+
+	if (ke == NULL)
+		return (-1);
+
+	bs = delete = 0;
+	TAILQ_FOREACH_SAFE(kb, &fentries, fentries, kbtmp) {
+		if (kb == ke && ke->type == KRB_ENTRY_BINDING) {
+			TAILQ_REMOVE(&fentries, kb, fentries);
+			break;
+		}
+
+		if (ke == kb)
+			delete = 1;
+
+		if (kb->type == KRB_ENTRY_BINDING_START) {
+			if (delete == 1)
+				bs++;
+		}
+
+		if (delete == 1) {
+			TAILQ_REMOVE(&fentries, kb, fentries);
+
+			switch (kb->type) {
+				case KRB_ENTRY_NULL:
+					xfree(&kb);
+					break;
+
+				case KRB_ENTRY_COMMENT:
+					xfree(&kb->kec_text);
+					xfree(&kb);
+					break;
+
+				case KRB_ENTRY_BINDING:
+					break;
+			}
+		}
+
+		if (kb->type == KRB_ENTRY_BINDING_END) {
+			if (delete == 1) {
+				xfree(&kb);
+				bs--;
+			}
+		}
+
+		if (delete == 1 && bs == 0)
+			break;
+	}
+
+	return (0);
+}
+
+static int
+krbconf_remove_binding(struct krb_entry_list *el, struct krb_entry *ke)
+{
+	if (ke == NULL || el == NULL)
+		return (-1);
+	if (ke->type != KRB_ENTRY_BINDING && ke->type != KRB_ENTRY_BINDING_START)
+		return (-1);
+
+	if (ke->keb_nbindings > 0) {
+		struct krb_entry *kb, *kbtmp;
+
+		TAILQ_FOREACH_SAFE(kb, &ke->keb_bindings, entries, kbtmp)
+			krbconf_remove_binding(&ke->keb_bindings, kb);
+	}
+
+	TAILQ_REMOVE(el, ke, entries);
+	xfree(&ke->keb_name);
+	xfree(&ke->keb_value);
+	xfree(&ke);
+
+	return (0);
+}
+
+static int
+krbconf_remove_section(struct krb_entry *ke)
+{
+	if (ke == NULL)
+		return (-1);
+	if (ke->type != KRB_ENTRY_SECTION)
+		return (-1);
+
+	if (ke->kes_nbindings > 0) {
+		struct krb_entry *ks, *kstmp;
+
+		TAILQ_FOREACH_SAFE(ks, &ke->kes_bindings, entries, kstmp) {
+			if (ks->keb_nbindings > 0) {
+				krbconf_remove_binding(&ke->kes_bindings, ks);
+
+			} else {
+				TAILQ_REMOVE(&ke->kes_bindings, ks, entries);
+				xfree(&ks->keb_name);
+				xfree(&ks->keb_value);
+				xfree(&ks);
+			}
+		}
+	}
+
+	TAILQ_REMOVE(&krbconf, ke, entries);
+	xfree(&ke->kes_name);
+	xfree(&ke);
+
+	return (0);
+}
+
+static int
+krb_op_remove(struct krb_entry *ke)
+{
+	if (ke == NULL)
+		return (-1);
+
+	switch (ke->type) {
+		case KRB_ENTRY_SECTION:
+			krb_fentries_remove_section(ke);
+			krbconf_remove_section(ke);
+			break;
+
+		case KRB_ENTRY_BINDING:
+		case KRB_ENTRY_BINDING_START: {
+			struct krb_entry_list *el = get_entry_list(NULL, ke);
+
+			krb_fentries_remove_binding(ke);
+			if (el != NULL)
+				krbconf_remove_binding(el, ke);
+			break;
+		}
+	}
+	
+	return (0);
+}
+
+static void
+do_modifications(void)
+{
+	struct krb_modification *km, *kmtmp;
+
+	TAILQ_FOREACH_SAFE(km, &modifications, entries, kmtmp) {
+		struct krb_entry *ke = km2entry(km);
+
+		if (ke != NULL) {
+			int res = -1;
+
+			switch (km->op) {
+				case KRB_OP_ADD:
+					res = krb_op_add(ke);
+					break;
+
+				case KRB_OP_MODIFY:
+					res = krb_op_modify(ke);
+					break;
+
+				case KRB_OP_REMOVE:
+					res = krb_op_remove(ke);
+					break;
+			}
+		}
+
+		TAILQ_REMOVE(&modifications, km, entries);
+		xfree(&km->modstr);
+		xfree(&km);
+	}
+}
+
 int
 main(int argc, char **argv)
 {
@@ -326,11 +727,9 @@
 				infile = xstrdup(optarg);
 				break;
 
-#if 0
 			case 'm':
 				add_modification(optarg);
 				break;
-#endif
 
 			case 'o':
 				xfree(&outfile);
@@ -363,6 +762,7 @@
 
 	yyparse();
 
+	do_modifications();
 	write_krb5_conf();
 	krb5_conf_free();
 

Modified: pcbsd-projects/AD_4_PCBSD/krb/krbconf.h
===================================================================
--- pcbsd-projects/AD_4_PCBSD/krb/krbconf.h	2012-07-26 19:38:15 UTC (rev 17961)
+++ pcbsd-projects/AD_4_PCBSD/krb/krbconf.h	2012-07-26 20:02:36 UTC (rev 17962)
@@ -25,17 +25,6 @@
 #define	KRB_ENTRY_BINDING_END	0x0008
 #define	KRB_ENTRY_COMMENT		0x0010
 
-
-#define	KRB_S_APPDEFAULTS		"appdefaults"
-#define	KRB_S_LIBDEFAULTS		"libdefaults"
-#define	KRB_S_DOMAIN_REALM		"domain_realm"
-#define	KRB_S_REALMS			"realms"
-#define	KRB_S_CAPATHS			"capaths"
-#define	KRB_S_LOGGING			"logging"
-#define	KRB_S_KDC				"kdc"
-#define	KRB_S_KADMIN			"kadmin"
-#define	KRB_S_PASSWORD_QUALITY	"password_quality"
-
 TAILQ_HEAD(krb_entry_list, krb_entry);
 
 struct krb_comment {



More information about the Commits mailing list