Temporary function smack_create_default_config_files().
authorJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Tue, 30 Nov 2010 12:44:12 +0000 (04:44 -0800)
committerJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Tue, 30 Nov 2010 12:44:12 +0000 (04:44 -0800)
src/Makefile.am
src/smack.h
src/smack_misc.c [new file with mode: 0644]

index 8d84b34..8a21bfa 100644 (file)
@@ -2,6 +2,6 @@ ACLOCAL_AMFLAGS = -I m4
 lib_LTLIBRARIES = libsmack.la
 
 libsmack_la_LDFLAGS = -version-info 1:0
-libsmack_la_SOURCES = smack_internal.c smack_rules.c smack_xattr.c smack_labels.c
+libsmack_la_SOURCES = smack_internal.c smack_rules.c smack_xattr.c smack_labels.c smack_misc.c
 
 EXTRA_DIST=smack_internal.h
index 53d414a..e086d72 100644 (file)
@@ -61,6 +61,16 @@ extern "C" {
 #endif
 
 /*!
+ * Create default config files for Smack. Creates only those files that
+ * don't yet exist. NOTE: this is temporary function and will be removed
+ * from API at some point.
+ *
+ * @return 0 when files are created succesfully and negative number on
+ * failure.
+ */
+extern int smack_create_default_config_files();
+
+/*!
  * Create a new rule set. The returned rule set must be freed with
  * smack_rule_set_delete().
  *
diff --git a/src/smack_misc.c b/src/smack_misc.c
new file mode 100644 (file)
index 0000000..2caaf35
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * This file is part of libsmack
+ *
+ * Copyright (C) 2010 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * Authors:
+ * Jarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
+ */
+
+#include "smack.h"
+#include "smack_internal.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define SMACK_DIR_PATH "/etc/smack"
+
+int smack_create_default_config_files()
+{
+       int ret, fd;
+
+       ret = access(SMACK_DIR_PATH, F_OK);
+       if (ret != 0 && errno != ENOENT)
+               return -1;
+       if (ret != 0) {
+               mkdir(SMACK_DIR_PATH,  S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+       }
+
+       ret = access(SMACK_ACCESSES_PATH, F_OK);
+       if (ret != 0 && errno != ENOENT)
+               return -1;
+       if (ret != 0) {
+               fd = creat(SMACK_ACCESSES_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               if (fd == -1)
+                       return -1;
+               close(fd);
+       }
+
+       ret = access(SMACK_LABELS_PATH, F_OK);
+       if (ret != 0 && errno != ENOENT)
+               return -1;
+       if (ret != 0) {
+               fd = creat(SMACK_LABELS_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               if (fd == -1)
+                       return -1;
+               close(fd);
+       }
+
+       return 0;
+}