From 86f4814245998f4b43e136101897557e4c2f5e54 Mon Sep 17 00:00:00 2001
From: dimitri staessens <dimitri.staessens@ugent.be>
Date: Mon, 20 Feb 2017 14:42:43 +0100
Subject: lib: Revise CACEP

Revises CACEP policies to stateless library calls. It provides two
policies: an anonymous authentication policy that will generate random
credentials for the peer, and a simple authentication policy that will
return a name for the peer and an address.

Changes the normal IPCP to use the updates API calls.
---
 src/lib/pol/cacep_anonymous_auth.c  |  81 +++++++++++++++++++++
 src/lib/pol/cacep_anonymous_auth.h  |  33 +++++++++
 src/lib/pol/cacep_simple_auth.c     | 137 ++++++++++++++++++++++++++++++++++++
 src/lib/pol/cacep_simple_auth.h     |  33 +++++++++
 src/lib/pol/cacep_simple_auth.proto |  29 ++++++++
 5 files changed, 313 insertions(+)
 create mode 100644 src/lib/pol/cacep_anonymous_auth.c
 create mode 100644 src/lib/pol/cacep_anonymous_auth.h
 create mode 100644 src/lib/pol/cacep_simple_auth.c
 create mode 100644 src/lib/pol/cacep_simple_auth.h
 create mode 100644 src/lib/pol/cacep_simple_auth.proto

(limited to 'src/lib/pol')

diff --git a/src/lib/pol/cacep_anonymous_auth.c b/src/lib/pol/cacep_anonymous_auth.c
new file mode 100644
index 00000000..d450fdc5
--- /dev/null
+++ b/src/lib/pol/cacep_anonymous_auth.c
@@ -0,0 +1,81 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Anonymous policy for CACEP
+ *
+ *    Dimitri Staessens <dimitri.staessens@ugent.be>
+ *    Sander Vrijders   <sander.vrijders@ugent.be>
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <ouroboros/config.h>
+#include <ouroboros/cacep.h>
+#include <ouroboros/time_utils.h>
+
+#include "cacep_anonymous_auth.h"
+
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
+
+#define NAME_LEN 8
+
+/* this policy generates a hex string */
+static struct cacep_info * anonymous_info(void)
+{
+        struct cacep_info * info;
+        struct timespec t;
+
+        info = malloc(sizeof(*info));
+        if (info == NULL)
+                return NULL;
+
+        info->name = malloc(NAME_LEN + 1);
+        if (info->name == NULL) {
+                free(info);
+                return NULL;
+        }
+
+        clock_gettime(CLOCK_REALTIME, &t);
+        srand(t.tv_nsec);
+
+        sprintf(info->name, "%8x",
+                (uint32_t)((rand() % RAND_MAX) & 0xFFFFFFFF));
+
+        info->addr = 0;
+
+        return info;
+}
+
+struct cacep_info * cacep_anonymous_auth(int                       fd,
+                                         const struct cacep_info * info)
+{
+        (void) fd;
+        (void) info;
+
+        return anonymous_info();
+}
+
+
+struct cacep_info * cacep_anonymous_auth_wait(int                       fd,
+                                              const struct cacep_info * info)
+{
+        (void) fd;
+        (void) info;
+
+        return anonymous_info();
+}
diff --git a/src/lib/pol/cacep_anonymous_auth.h b/src/lib/pol/cacep_anonymous_auth.h
new file mode 100644
index 00000000..d0229b05
--- /dev/null
+++ b/src/lib/pol/cacep_anonymous_auth.h
@@ -0,0 +1,33 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Anonymous policy for CACEP
+ *
+ *    Dimitri Staessens <dimitri.staessens@ugent.be>
+ *    Sander Vrijders   <sander.vrijders@ugent.be>
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef OUROBOROS_LIB_CACEP_ANONYMOUS_AUTH_H
+#define OUROBOROS_LIB_CACEP_ANONYMOUS_AUTH_H
+
+struct cacep_info * cacep_anonymous_auth(int                       fd,
+                                         const struct cacep_info * info);
+
+struct cacep_info * cacep_anonymous_auth_wait(int                       fd,
+                                              const struct cacep_info * info);
+
+#endif /* OUROBOROS_LIB_CACEP_ANONYMOUS_AUTH_H */
diff --git a/src/lib/pol/cacep_simple_auth.c b/src/lib/pol/cacep_simple_auth.c
new file mode 100644
index 00000000..1e052f3d
--- /dev/null
+++ b/src/lib/pol/cacep_simple_auth.c
@@ -0,0 +1,137 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Simple authentication policy for CACEP
+ *
+ *    Dimitri Staessens <dimitri.staessens@ugent.be>
+ *    Sander Vrijders   <sander.vrijders@ugent.be>
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <ouroboros/config.h>
+#include <ouroboros/cacep.h>
+#include <ouroboros/dev.h>
+#include <ouroboros/errno.h>
+
+#include "cacep_simple_auth.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "cacep_simple_auth.pb-c.h"
+typedef CacepSimpleAuthMsg cacep_simple_auth_msg_t;
+
+#define BUF_SIZE 2048
+
+static struct cacep_info * read_msg(int fd)
+{
+        struct cacep_info *       tmp;
+        uint8_t                   buf[BUF_SIZE];
+        cacep_simple_auth_msg_t * msg;
+        ssize_t                   len;
+
+        len = flow_read(fd, buf, BUF_SIZE);
+        if (len < 0)
+                return NULL;
+
+        msg = cacep_simple_auth_msg__unpack(NULL, len, buf);
+        if (msg == NULL)
+                return NULL;
+
+        tmp = malloc(sizeof(*tmp));
+        if (tmp == NULL) {
+                cacep_simple_auth_msg__free_unpacked(msg, NULL);
+                return NULL;
+        }
+
+        tmp->addr = msg->addr;
+        tmp->name = strdup(msg->name);
+        if (tmp->name == NULL) {
+                free(tmp);
+                cacep_simple_auth_msg__free_unpacked(msg, NULL);
+                return NULL;
+        }
+
+        cacep_simple_auth_msg__free_unpacked(msg, NULL);
+
+        return tmp;
+}
+
+static int send_msg(int                       fd,
+                    const struct cacep_info * info)
+{
+        cacep_simple_auth_msg_t msg = CACEP_SIMPLE_AUTH_MSG__INIT;
+        int                     ret = 0;
+        uint8_t *               data = NULL;
+        size_t                  len = 0;
+
+        msg.name = info->name;
+        msg.addr = info->addr;
+
+        len = cacep_simple_auth_msg__get_packed_size(&msg);
+        if (len == 0)
+                return -1;
+
+        data = malloc(len);
+        if (data == NULL)
+                return -ENOMEM;
+
+        cacep_simple_auth_msg__pack(&msg, data);
+
+        if (flow_write(fd, data, len) < 0)
+                ret = -1;
+
+        free(data);
+
+        return ret;
+}
+
+struct cacep_info * cacep_simple_auth_auth(int                       fd,
+                                           const struct cacep_info * info)
+{
+        struct cacep_info * tmp;
+
+        assert(info);
+
+        if (send_msg(fd, info))
+                return NULL;
+
+        tmp = read_msg(fd);
+        if (tmp == NULL)
+                return NULL;
+
+        return tmp;
+}
+
+
+struct cacep_info * cacep_simple_auth_auth_wait(int                       fd,
+                                                const struct cacep_info * info)
+{
+        struct cacep_info * tmp;
+
+        assert(info);
+
+        tmp = read_msg(fd);
+        if (tmp == NULL)
+                return NULL;
+
+        if (send_msg(fd, info)) {
+                free(tmp);
+                return NULL;
+        }
+
+        return tmp;
+}
diff --git a/src/lib/pol/cacep_simple_auth.h b/src/lib/pol/cacep_simple_auth.h
new file mode 100644
index 00000000..bbdbe9b9
--- /dev/null
+++ b/src/lib/pol/cacep_simple_auth.h
@@ -0,0 +1,33 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Simple authentication policy for CACEP
+ *
+ *    Dimitri Staessens <dimitri.staessens@ugent.be>
+ *    Sander Vrijders   <sander.vrijders@ugent.be>
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef OUROBOROS_LIB_CACEP_SIMPLE_AUTH_H
+#define OUROBOROS_LIB_CACEP_SIMPLE_AUTH_H
+
+struct cacep_info * cacep_simple_auth_auth(int                       fd,
+                                           const struct cacep_info * info);
+
+struct cacep_info * cacep_simple_auth_auth_wait(int                       fd,
+                                                const struct cacep_info * info);
+
+#endif /* OUROBOROS_LIB_CACEP_SIMPLE_AUTH_H */
diff --git a/src/lib/pol/cacep_simple_auth.proto b/src/lib/pol/cacep_simple_auth.proto
new file mode 100644
index 00000000..d20f8780
--- /dev/null
+++ b/src/lib/pol/cacep_simple_auth.proto
@@ -0,0 +1,29 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Message for no authentication CACEP policy
+ *
+ *    Dimitri Staessens <dimitri.staessens@ugent.be>
+ *    Sander Vrijders   <sander.vrijders@ugent.be>
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+syntax = "proto2";
+
+message cacep_simple_auth_msg {
+        required string name = 1;
+        required uint64 addr = 2;
+}
-- 
cgit v1.2.3