OSDN Git Service

cleanup
[openpts/openpts.git] / src / policy.c
index 72a2b1b..4afb54d 100644 (file)
  * \brief policy
  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
  * @date 2010-06-19
- * cleanup 2011-01-22 SM
+ * cleanup 2012-01-05 SM
  *
  * Security Policy
  * - load
  * - verify
  * - print
  *
- *
  */
 
 
 #include <string.h>
 
 #include <openpts.h>
-// #include <log.h>
 
 /**
  * Free policy chain
  */
 int freePolicyChain(OPENPTS_POLICY *pol) {
+    /* check */
     if (pol == NULL) {
-        return PTS_INTERNAL_ERROR;
+        LOG(LOG_ERR, "null input");
+        return PTS_FATAL;
     }
 
+    /* chain */
     if (pol->next != NULL) {
         freePolicyChain(pol->next);
     }
@@ -68,26 +69,34 @@ int freePolicyChain(OPENPTS_POLICY *pol) {
  *   policy number
  */
 int loadPolicyFile(OPENPTS_CONTEXT *ctx, char * filename) {
-    FILE *fp;
     char buf[BUF_SIZE];  // SMBIOS
     char *eq;
     char *name;
     char *value;
     int cnt = 1;
     int len;
-    OPENPTS_POLICY *pol;
     int line = 0;
+    FILE *fp;
+    OPENPTS_POLICY *pol;
 
-    /* open */
+    /* check */
+    if (ctx == NULL) {
+        LOG(LOG_ERR, "null input");
+        return PTS_FATAL;
+    }
+    if (filename == NULL) {
+        LOG(LOG_ERR, "null input");
+        return PTS_FATAL;
+    }
 
+    /* open */
     if ((fp = fopen(filename, "r")) == NULL) {
-        fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_POLICY_FILE_OPEN_FAILED,
+        OUTPUT(NLS(MS_OPENPTS, OPENPTS_POLICY_FILE_OPEN_FAILED,
             "Failed to open policy file '%s'\n"), filename);
         return -1;
     }
 
     /* parse */
-
     while (fgets(buf, BUF_SIZE, fp) != NULL) {  // read line
         /* ignore comment, null line */
         if (buf[0] == '#') {
@@ -108,7 +117,7 @@ int loadPolicyFile(OPENPTS_CONTEXT *ctx, char * filename) {
             /* new  */
             pol = xmalloc(sizeof(OPENPTS_POLICY));
             if (pol == NULL) {
-                ERROR("no mem");
+                LOG(LOG_ERR, "no memory");
                 cnt = -1;  // return -1;
                 goto error;
             }
@@ -131,7 +140,7 @@ int loadPolicyFile(OPENPTS_CONTEXT *ctx, char * filename) {
             }
             cnt++;
         } else {
-            //
+            // unknown eq
         }
         line++;
     }
@@ -169,10 +178,16 @@ int checkPolicy(OPENPTS_CONTEXT *ctx) {
     int unknown = 0;
     int invalid = 0;
 
+    /* check */
+    if (ctx == NULL) {
+        LOG(LOG_ERR, "null input");
+        return PTS_FATAL;
+    }
     pol = ctx->policy_start;
 
     if (pol == NULL) {
         /* no policy to check */
+        DEBUG("There is no policy to check with. => Unknown");
         return OPENPTS_RESULT_UNKNOWN;
     }
 
@@ -208,21 +223,23 @@ int checkPolicy(OPENPTS_CONTEXT *ctx) {
 
     /* if any invalid exist */
     if (invalid > 0) {
+        DEBUG("Check policy => Invalid");
         return OPENPTS_RESULT_INVALID;
     }
 
     /* if any unknown exist */
     if (unknown > 0) {
+        DEBUG("Check policy => Unknown");
         return OPENPTS_RESULT_UNKNOWN;
     }
 
+    DEBUG("Check policy => Valid");
     return OPENPTS_RESULT_VALID;
 }
 
 /**
  * print policy and properties
  *
- *
  */
 int printPolicy(OPENPTS_CONTEXT *ctx) {
     OPENPTS_POLICY *pol;
@@ -230,7 +247,18 @@ int printPolicy(OPENPTS_CONTEXT *ctx) {
     char *proc_value;
     char *status;
 
+    /* check */
+    if (ctx == NULL) {
+        LOG(LOG_ERR, "null input");
+        return PTS_FATAL;
+    }
     pol = ctx->policy_start;
+    if (pol == NULL) {
+        /* no policy to print */
+        OUTPUT(NLS(MS_OPENPTS, OPENPTS_PRINT_POLICY_NULL,
+            "There is no policy to print."));
+        return PTS_SUCCESS;
+    }
 
     OUTPUT(NLS(MS_OPENPTS, OPENPTS_PRINT_POLICY,
            "  id "
@@ -265,16 +293,10 @@ int printPolicy(OPENPTS_CONTEXT *ctx) {
         }
 
         /* print */
-#if 0
-        OUTPUT("%5d %-25s %-13s\n",
-            pol->num,
-            pol->name, pol->value);
-#else
         OUTPUT("%5d %-35s %-28s %-28s %-10s\n",
             pol->num,
             pol->name, pol->value,
             proc_value, status);
-#endif
         pol = pol->next;
     }
 
@@ -282,5 +304,3 @@ int printPolicy(OPENPTS_CONTEXT *ctx) {
 
     return 0;
 }
-
-