[pacman-dev] [PATCH 3/3] Implement parsing of the new SigLevel directive

Kerrick Staley mail at kerrickstaley.com
Mon Jul 18 02:35:27 EDT 2011


Add code to conf.c that parses the new SigLevel directive.

Signed-off-by: Kerrick Staley <mail at kerrickstaley.com>
---
 src/pacman/conf.c |   97 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 80 insertions(+), 17 deletions(-)

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index fac6da3..de5b463 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -222,20 +222,83 @@ int config_set_arch(const char *arch)
 	return 0;
 }
 
-static alpm_siglevel_t option_verifysig(const char *value)
+/**
+ * Parse a signature verification level. Destroys the passed string.
+ * @param str the string listing the siglevel options
+ * @return -1 if value was empty, -2 if value contained unknown
+ * options, -3 if some of the options were contradictory, the siglevel
+ * value otherwise.
+ */
+static alpm_siglevel_t option_siglevel(char *str)
 {
-	alpm_siglevel_t level;
-	if(strcmp(value, "Always") == 0) {
-		level = ALPM_SIG_PACKAGE | ALPM_SIG_DATABASE;
-	} else if(strcmp(value, "Optional") == 0) {
-		level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL |
-			ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL;
-	} else if(strcmp(value, "Never") == 0) {
-		level = 0;
-	} else {
+	alpm_siglevel_t level = 0;
+
+	char *strtok_state;
+	char *tok = strtok_r(str, " \t", &strtok_state);
+
+	if (!tok) {
 		return -1;
 	}
-	pm_printf(ALPM_LOG_DEBUG, "config: VerifySig = %s (%d)\n", value, level);
+
+	do {
+		if(strcmp(tok, "Never") == 0) {
+					level = level ? -3 : 0;
+					break;
+		} else if(strcmp(tok, "Optional") == 0) {
+			level =  level ? -3 : ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL |
+				ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL;
+			break;
+		} else if(strcmp(tok, "Required") == 0) {
+			level = level ? -3 : ALPM_SIG_PACKAGE | ALPM_SIG_DATABASE;
+			break;
+		} else if(strcmp(tok, "PackageHash") == 0) {
+			if (level & ALPM_SIG_DATABASE_OPTIONAL) {
+				level = -3;
+				break;
+			}
+			level |= ALPM_SIG_PACKAGE_HASH_OK;
+		} else if(strcmp(tok, "PackageRequired") == 0) {
+			if (level & ALPM_SIG_PACKAGE_OPTIONAL) {
+				level = -3;
+				break;
+			}
+			level |= ALPM_SIG_PACKAGE;
+		} else if(strcmp(tok, "DatabaseRequired") == 0) {
+			if (level & ALPM_SIG_DATABASE_OPTIONAL) {
+				level = -3;
+				break;
+			}
+			level |= ALPM_SIG_DATABASE;
+		} else if(strcmp(tok, "PackageOptional") == 0) {
+			if (level & ALPM_SIG_PACKAGE) {
+				level = -3;
+				break;
+			}
+			level |= ALPM_SIG_PACKAGE_OPTIONAL;
+		} else if(strcmp(tok, "DatabaseOptional") == 0) {
+			if (level & ALPM_SIG_PACKAGE) {
+				level = -3;
+				break;
+			}
+			level |= ALPM_SIG_DATABASE_OPTIONAL;
+		} else if(strcmp(tok, "PackageAllowMarginal") == 0) {
+			level |= ALPM_SIG_PACKAGE_MARGINAL_OK;
+		} else if(strcmp(tok, "DatabaseAllowMarginal") == 0) {
+			level |= ALPM_SIG_DATABASE_MARGINAL_OK;
+		} else if(strcmp(tok, "PackageAllowUnknown") == 0) {
+			level |= ALPM_SIG_PACKAGE_UNKNOWN_OK;
+		} else if(strcmp(tok, "DatabaseAllowUnknown") == 0) {
+			level |= ALPM_SIG_DATABASE_UNKNOWN_OK;
+		} else {
+			level = -2;
+			break;
+		}
+	} while ((tok = strtok_r(NULL, " \t", &strtok_state)));
+
+	if (level >= 0) {
+		pm_printf(ALPM_LOG_DEBUG, "config: SigLevel = %s (%d)\n", str, level);
+	}
+
 	return level;
 }
 
@@ -359,9 +422,9 @@ static int _parse_options(const char *key, char *value,
 				return 1;
 			}
 			FREELIST(methods);
-		} else if(strcmp(key, "VerifySig") == 0) {
-			alpm_siglevel_t level = option_verifysig(value);
-			if(level != -1) {
+		} else if(strcmp(key, "SigLevel") == 0) {
+			alpm_siglevel_t level = option_siglevel(value);
+			if(level >= 0) {
 				config->siglevel = level;
 			} else {
 				pm_printf(ALPM_LOG_ERROR,
@@ -726,9 +789,9 @@ static int _parseconfig(const char *file, struct section_t *section,
 					goto cleanup;
 				}
 				section->servers = alpm_list_add(section->servers, strdup(value));
-			} else if(strcmp(key, "VerifySig") == 0) {
-				alpm_siglevel_t level = option_verifysig(value);
-				if(level != -1) {
+			} else if(strcmp(key, "SigLevel") == 0) {
+				alpm_siglevel_t level = option_siglevel(value);
+				if(level >= 0) {
 					section->siglevel = level;
 				} else {
 					pm_printf(ALPM_LOG_ERROR,
-- 
1.7.6



More information about the pacman-dev mailing list