[PC-BSD Commits] r18022 - pcbsd/current/src-sh/pc-adctl/nssldap

svn at pcbsd.org svn at pcbsd.org
Tue Jul 31 11:35:49 PDT 2012


Author: johnh
Date: 2012-07-31 18:35:49 +0000 (Tue, 31 Jul 2012)
New Revision: 18022

Modified:
   pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-lexer.l
   pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-parser.y
   pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.c
   pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.h
Log:
nss_ldap lexer/parser.. check!



Modified: pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-lexer.l
===================================================================
--- pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-lexer.l	2012-07-31 17:11:29 UTC (rev 18021)
+++ pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-lexer.l	2012-07-31 18:35:49 UTC (rev 18022)
@@ -17,17 +17,18 @@
 
 %%
 
-\n		{ return (NEWLINE); }
-[ \t]+
+\n				{ return (NEWLINE); }
+[[:space:]\t]+$	{ return (EMPTY); }
+[[:space:]\t]+	{ return (SPACE); }
 
 ([ \t]+)?(;|#)(.+)?$	{
 	yylval.str = strdup(clean(yytext));
 	return (COMMENT);
 }
 
-[^#]+[:space:]+[^#]+	{
+[^#[:space:]]+ {
 	yylval.str = strdup(clean(yytext));
-	return (PAIR);
+	return (WORD);
 }
 
 %%

Modified: pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-parser.y
===================================================================
--- pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-parser.y	2012-07-31 17:11:29 UTC (rev 18021)
+++ pcbsd/current/src-sh/pc-adctl/nssldap/nssldap-parser.y	2012-07-31 18:35:49 UTC (rev 18022)
@@ -12,6 +12,8 @@
 extern	FILE *yyin, *yyout;
 extern	unsigned int lineno;
 
+extern	struct nssldap_entry_list nssldapconf;
+
 %}
 
 %union {
@@ -20,9 +22,13 @@
 
 
 %token	NEWLINE
+%token	SPACE
+%token	EMPTY
 %token	STRING
+%token	WORD
 %token	PAIR
 
+%token	<str> WORD
 %token	<str> PAIR
 %token	<str> STRING
 %token	<str> COMMENT
@@ -40,44 +46,51 @@
 line:
 	newline |
 	comment |
-	pair
+	pair |
+	empty |
+	space
 
+space:
+	SPACE
+
+empty:
+	EMPTY {
+		struct nssldap_entry *ne = xalloc(sizeof(*ne));
+		ne->type = NSSLDAP_ENTRY_NULL;
+		TAILQ_INSERT_TAIL(&nssldapconf, ne, entries);
+	}
+
 pair:
-	PAIR {
-		char *name = NULL, *value = NULL;
+	WORD SPACE WORD {
+		struct nssldap_entry *ne = xalloc(sizeof(*ne));
 
-		split($1, &name, &value);
-		printf("name = '%s', value = '%s'\n", name, value);
+		ne->type = NSSLDAP_ENTRY_PAIR;
+		ne->nep_name = xstrdup(clean($1));
+		ne->nep_value = xstrdup(clean($3));
+
+		TAILQ_INSERT_TAIL(&nssldapconf, ne, entries);
 	}
 
 newline:
-	NEWLINE
+	NEWLINE {
+		lineno++;
+	}
 
 comment:
-	COMMENT
+	COMMENT {
+		struct nssldap_entry *ne = xalloc(sizeof(*ne));
 
+		ne->type = NSSLDAP_ENTRY_COMMENT;
+		ne->nec_text = xstrdup(clean($1));
+
+		TAILQ_INSERT_TAIL(&nssldapconf, ne, entries);
+	}
+
 %%
 
 unsigned int lineno = 0;
 
 void
-split(const char *line, char **name_pptr, char **value_pptr)
-{
-	char *tmp, *save, *ptr;
-
-	tmp = strdup(line);
-	save = tmp;
-
-	ptr = strsep(&tmp, " \t");
-	if (ptr != NULL)
-		*name_pptr = xstrdup(clean(ptr));
-	if (tmp != NULL)
-		*value_pptr = xstrdup(clean(tmp));
-
-	xfree(&save);
-}
-
-void
 yyerror(const char *str)
 {
 	fprintf(stderr, "%s:%d: error: %s\n", __FILE__, lineno, str);

Modified: pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.c
===================================================================
--- pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.c	2012-07-31 17:11:29 UTC (rev 18021)
+++ pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.c	2012-07-31 18:35:49 UTC (rev 18022)
@@ -7,6 +7,22 @@
 #include "nssldap-parser.tab.h"
 #include "nssldap-parser.tab.i"
 
+#define	NSSLDAP_OP_ADD		1
+#define	NSSLDAP_OP_MODIFY	2
+#define	NSSLDAP_OP_REMOVE	3
+
+struct nssldap_modification {
+	TAILQ_ENTRY(nssldap_modification) entries;
+	char *name;
+	char *value;
+	int create;
+	int op;
+};
+TAILQ_HEAD(nssldap_modification_list, nssldap_modification) modifications= 
+	TAILQ_HEAD_INITIALIZER(modifications);
+
+struct nssldap_entry_list nssldapconf = TAILQ_HEAD_INITIALIZER(nssldapconf);
+
 void *
 xalloc(size_t size)
 {
@@ -60,12 +76,296 @@
 	return (str);
 }
 
+static int
+add_modification(const char *m, int create)
+{
+	char *tmp, *save, *ptr;
+	struct nssldap_modification *nm;
 
+	if (m == NULL)
+		return (-1);
 
+	nm = xalloc(sizeof(*nm));
+	switch (m[0]) {
+		case '+':
+			nm->op = NSSLDAP_OP_ADD;
+			nm->create = 1;
+			m += 1;
+			break;
 
+		case '-':
+			nm->op = NSSLDAP_OP_REMOVE;
+			nm->create = 0;
+			m += 1;
+			break;
+
+		case '^':
+			nm->op = NSSLDAP_OP_MODIFY;
+			nm->create = create;
+			m += 1;
+			break;
+
+		default:
+			xfree(&nm);
+			return (-1);
+	}
+
+	ptr = xstrdup(m);
+	save = ptr;
+
+	tmp = strsep(&ptr, "=");
+	if (tmp == NULL || tmp[0] == 0) {
+		xfree(&nm);
+		return (-1);
+	}
+
+	if (tmp != NULL)
+		nm->name = xstrdup(clean(tmp));
+	if (ptr != NULL)
+		nm->value = xstrdup(clean(ptr));
+
+	TAILQ_INSERT_TAIL(&modifications, nm, entries);
+
+	xfree(&save);
+	return (0);
+}
+
+static int
+nssldap_op_add(struct nssldap_modification *nm)
+{
+	int exists = 0;
+	struct nssldap_entry *ne;
+
+	if (nm == NULL)
+		return (-1);
+
+	TAILQ_FOREACH(ne, &nssldapconf, entries) {
+		if (ne->type == NSSLDAP_ENTRY_PAIR &&
+			strcasecmp(ne->nep_name, nm->name) == 0) {
+			exists = 1;
+			break;
+		}
+	}
+
+	if (exists == 0) {
+		ne = xalloc(sizeof(*ne));
+
+		ne->type = NSSLDAP_ENTRY_PAIR;
+		ne->nep_name = xstrdup(nm->name);
+		ne->nep_value = xstrdup(nm->value);
+
+		TAILQ_INSERT_TAIL(&nssldapconf, ne, entries);
+	}
+
+	return (0);
+}
+
+static int
+nssldap_op_modify(struct nssldap_modification *nm)
+{
+	int exists = 0;
+	struct nssldap_entry *ne, *netmp;
+
+	if (nm == NULL)
+		return (-1);
+
+	TAILQ_FOREACH_SAFE(ne, &nssldapconf, entries, netmp) {
+		if (ne->type == NSSLDAP_ENTRY_PAIR &&
+			strcasecmp(ne->nep_name, nm->name) == 0) {
+			xfree(&ne->nep_value);
+			ne->nep_value = xstrdup(nm->value);
+			exists = 1;
+			break;
+		}
+	}
+
+	if (exists == 0 && nm->create > 0)
+		return (nssldap_op_add(nm));
+
+	return (0);
+}
+
+static int
+nssldap_op_remove(struct nssldap_modification *nm)
+{
+	struct nssldap_entry *ne, *netmp;
+
+	if (nm == NULL)
+		return (-1);
+
+	TAILQ_FOREACH_SAFE(ne, &nssldapconf, entries, netmp) {
+		if (ne->type == NSSLDAP_ENTRY_PAIR &&
+			strcasecmp(ne->nep_name, nm->name) == 0) {
+			TAILQ_REMOVE(&nssldapconf, ne, entries);
+			xfree(&ne->nep_name);
+			xfree(&ne->nep_value);
+			xfree(&ne);
+			break;
+		}
+	}
+
+	return (0);
+}
+
+static void
+do_modifications(void)
+{
+	struct nssldap_modification *nm, *nmtmp;
+
+	TAILQ_FOREACH_SAFE(nm, &modifications, entries, nmtmp) {
+		switch (nm->op) {
+			case NSSLDAP_OP_ADD:
+				if (nssldap_op_add(nm) < 0)
+					warnx("nssldap_op_add: returned -1\n");
+				break;
+
+			case NSSLDAP_OP_MODIFY:
+				if (nssldap_op_modify(nm) < 0)
+					warnx("nssldap_op_modify: returned -1\n");
+				break;
+
+			case NSSLDAP_OP_REMOVE:
+				if (nssldap_op_remove(nm) < 0)
+					warnx("nssldap_op_remove: returned -1\n");
+				break;
+		}
+
+		TAILQ_REMOVE(&modifications, nm, entries);
+		xfree(&nm->name);
+		xfree(&nm->value);
+		xfree(&nm);
+	}
+}
+
+static int
+write_nss_ldap_conf(void)
+{
+	struct nssldap_entry *ne;
+
+	TAILQ_FOREACH(ne, &nssldapconf, entries) {
+		switch (ne->type) {
+			case NSSLDAP_ENTRY_NULL:
+				fprintf(yyout, "\n");
+				break;
+
+			case NSSLDAP_ENTRY_PAIR:
+				fprintf(yyout, "%s\t%s\n", ne->nep_name, ne->nep_value);
+				break;
+
+			case NSSLDAP_ENTRY_COMMENT:
+				fprintf(yyout, "%s\n", ne->nec_text);
+				break;
+		}
+	}
+}
+
+static void
+nss_ldap_conf_free(void)
+{
+	struct nssldap_entry *ne, *netmp;
+
+	TAILQ_FOREACH_SAFE(ne, &nssldapconf, entries, netmp) {
+		TAILQ_REMOVE(&nssldapconf, ne, entries);
+
+		switch (ne->type) {
+			case NSSLDAP_ENTRY_NULL:
+				break;
+
+			case NSSLDAP_ENTRY_PAIR:
+				xfree(&ne->nep_name);
+				xfree(&ne->nep_value);
+				break;
+
+			case NSSLDAP_ENTRY_COMMENT:
+				xfree(&ne->nec_text);
+				break;
+		}
+
+		xfree(&ne);
+	}
+}
+
+static void
+usage(void)
+{
+	fprintf(stderr,
+		"Usage: nssldapconf [options]\n"
+		"Where options in:\n\n"
+		"\t-f <input file>\n"
+		"\t-o <output file>\n"
+		"\t-m <(+|-|^)name=value>\n\n"
+	);
+
+	exit (1);
+}
+
 int
 main(int argc, char **argv)
 {
-	yyparse();
+	int ch, create;
+	char *infile, *outfile;
+
+	if (argc <= 1)
+		usage();
+
+	create = 0;
+	infile = outfile = NULL;
+	while ((ch = getopt(argc, argv, "f:m:co:")) != -1) {
+		switch (ch) {
+			case 'f':
+				xfree(&infile);
+				infile = xstrdup(optarg);
+				break;
+
+			case 'c':
+				create++;
+				break;
+
+			case 'm':
+				add_modification(optarg, create);
+				create = 0;
+				break;
+
+			case 'o':
+				xfree(&outfile);
+				outfile = xstrdup(optarg);
+				break;
+
+			case '?':
+			default:
+				usage();
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if (infile != NULL) {
+		yyin = fopen(infile, "r");
+		if (yyin == NULL)
+			err(EX_NOINPUT, "%s", infile);
+	}
+
+	if (outfile != NULL) {
+		yyout = fopen(outfile, "w+");
+		if (yyout == NULL)
+			err(EX_NOINPUT, "%s", outfile);
+	}
+
+	if (infile != NULL)
+		yyparse();
+
+	do_modifications();
+	write_nss_ldap_conf();
+	nss_ldap_conf_free();
+
+	xfree(&outfile);
+	xfree(&infile);
+
+	if (yyout != NULL)
+		fclose(yyout);
+	if (yyin != NULL)
+		fclose(yyin);
+
 	return (0);
 }

Modified: pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.h
===================================================================
--- pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.h	2012-07-31 17:11:29 UTC (rev 18021)
+++ pcbsd/current/src-sh/pc-adctl/nssldap/nssldapconf.h	2012-07-31 18:35:49 UTC (rev 18022)
@@ -18,6 +18,39 @@
 #include <sysexits.h>
 #include <unistd.h>
 
+#define	NSSLDAP_ENTRY_NULL		0x00
+#define	NSSLDAP_ENTRY_PAIR		0x01
+#define	NSSLDAP_ENTRY_COMMENT	0x02
+
+struct nssldap_comment {
+	char *text;
+};
+
+struct nssldap_pair {
+	char *name;
+	char *value;
+};
+
+struct nssldap_entry {
+	unsigned int type;
+	union {
+		struct nssldap_pair ne_pair;
+		struct nssldap_comment ne_comment;
+	} ne;
+
+#define	nep	ne.ne_pair
+#define	nec	ne.ne_comment
+
+#define	nep_name	nep.name
+#define	nep_value	nep.value
+
+#define	nec_text	nec.text
+
+	TAILQ_ENTRY(nssldap_entry) entries;
+};
+
+TAILQ_HEAD(nssldap_entry_list, nssldap_entry);
+
 extern  void *xalloc(size_t);
 extern  void _xfree(char **);
 extern  char *xstrdup(const char *);



More information about the Commits mailing list