Fixed compiler warning.
[strongswan.git] / src / stroke / stroke.c
index 9a15547..1cb8cc2 100644 (file)
@@ -12,8 +12,6 @@
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
- *
- * RCSID $Id$
  */
 
 #include <stdlib.h>
@@ -27,6 +25,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stddef.h>
+#include <string.h>
 
 #include "stroke_msg.h"
 #include "stroke_keywords.h"
@@ -54,13 +53,16 @@ static char* push_string(stroke_msg_t *msg, char *string)
 
 static int send_stroke_msg (stroke_msg_t *msg)
 {
-       struct sockaddr_un ctl_addr = { AF_UNIX, STROKE_SOCKET };
+       struct sockaddr_un ctl_addr;
        int sock;
-       char buffer[64];
+       char buffer[512];
        int byte_count;
-       
+
+       ctl_addr.sun_family = AF_UNIX;
+       strcpy(ctl_addr.sun_path, STROKE_SOCKET);
+
        msg->output_verbosity = 1; /* CONTROL */
-       
+
        sock = socket(AF_UNIX, SOCK_STREAM, 0);
        if (sock < 0)
        {
@@ -74,7 +76,7 @@ static int send_stroke_msg (stroke_msg_t *msg)
                close(sock);
                return -1;
        }
-       
+
        /* send message */
        if (write(sock, msg, msg->length) != msg->length)
        {
@@ -82,56 +84,68 @@ static int send_stroke_msg (stroke_msg_t *msg)
                close(sock);
                return -1;
        }
-       
+
        while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
        {
                buffer[byte_count] = '\0';
                printf("%s", buffer);
+
+               /* we prompt if we receive the "Passphrase:" magic keyword */
+               if (byte_count >= 12 &&
+                       strcmp(buffer + byte_count - 12, "Passphrase:\n") == 0)
+               {
+                       if (fgets(buffer, sizeof(buffer), stdin))
+                       {
+                               ignore_result(write(sock, buffer, strlen(buffer)));
+                       }
+               }
        }
        if (byte_count < 0)
        {
                fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
        }
-       
+
        close(sock);
        return 0;
 }
 
 static int add_connection(char *name,
-                                                 char *my_id, char *other_id, 
+                                                 char *my_id, char *other_id,
                                                  char *my_addr, char *other_addr,
                                                  char *my_nets, char *other_nets)
 {
        stroke_msg_t msg;
-       
+
        memset(&msg, 0, sizeof(msg));
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.type = STR_ADD_CONN;
-       
+
        msg.add_conn.name = push_string(&msg, name);
        msg.add_conn.ikev2 = 1;
        msg.add_conn.auth_method = 2;
        msg.add_conn.mode = 1;
        msg.add_conn.mobike = 1;
        msg.add_conn.dpd.action = 1;
-       
+
        msg.add_conn.me.id = push_string(&msg, my_id);
        msg.add_conn.me.address = push_string(&msg, my_addr);
+       msg.add_conn.me.ikeport = 500;
        msg.add_conn.me.subnets = push_string(&msg, my_nets);
        msg.add_conn.me.sendcert = 1;
-       
+
        msg.add_conn.other.id = push_string(&msg, other_id);
        msg.add_conn.other.address = push_string(&msg, other_addr);
+       msg.add_conn.other.ikeport = 500;
        msg.add_conn.other.subnets = push_string(&msg, other_nets);
        msg.add_conn.other.sendcert = 1;
-       
+
        return send_stroke_msg(&msg);
 }
 
 static int del_connection(char *name)
 {
        stroke_msg_t msg;
-       
+
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.type = STR_DEL_CONN;
        msg.initiate.name = push_string(&msg, name);
@@ -141,7 +155,7 @@ static int del_connection(char *name)
 static int initiate_connection(char *name)
 {
        stroke_msg_t msg;
-       
+
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.type = STR_INITIATE;
        msg.initiate.name = push_string(&msg, name);
@@ -151,7 +165,7 @@ static int initiate_connection(char *name)
 static int terminate_connection(char *name)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_TERMINATE;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.initiate.name = push_string(&msg, name);
@@ -161,7 +175,7 @@ static int terminate_connection(char *name)
 static int terminate_connection_srcip(char *start, char *end)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_TERMINATE_SRCIP;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.terminate_srcip.start = push_string(&msg, start);
@@ -172,7 +186,7 @@ static int terminate_connection_srcip(char *start, char *end)
 static int route_connection(char *name)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_ROUTE;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.route.name = push_string(&msg, name);
@@ -182,7 +196,7 @@ static int route_connection(char *name)
 static int unroute_connection(char *name)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_UNROUTE;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.unroute.name = push_string(&msg, name);
@@ -192,7 +206,7 @@ static int unroute_connection(char *name)
 static int show_status(stroke_keyword_t kw, char *connection)
 {
        stroke_msg_t msg;
-       
+
        msg.type = (kw == STROKE_STATUS)? STR_STATUS:STR_STATUS_ALL;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.status.name = push_string(&msg, connection);
@@ -217,7 +231,7 @@ static int list_flags[] = {
 static int list(stroke_keyword_t kw, int utc)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_LIST;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.list.utc = utc;
@@ -238,7 +252,7 @@ static int reread_flags[] = {
 static int reread(stroke_keyword_t kw)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_REREAD;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.reread.flags = reread_flags[kw - STROKE_REREAD_FIRST];
@@ -246,23 +260,36 @@ static int reread(stroke_keyword_t kw)
 }
 
 static int purge_flags[] = {
-       PURGE_OCSP
+       PURGE_OCSP,
+       PURGE_IKE,
 };
 
 static int purge(stroke_keyword_t kw)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_PURGE;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.purge.flags = purge_flags[kw - STROKE_PURGE_FIRST];
        return send_stroke_msg(&msg);
 }
 
+static int leases(stroke_keyword_t kw, char *pool, char *address)
+{
+
+       stroke_msg_t msg;
+
+       msg.type = STR_LEASES;
+       msg.length = offsetof(stroke_msg_t, buffer);
+       msg.leases.pool = push_string(&msg, pool);
+       msg.leases.address = push_string(&msg, address);
+       return send_stroke_msg(&msg);
+}
+
 static int set_loglevel(char *type, u_int level)
 {
        stroke_msg_t msg;
-       
+
        msg.type = STR_LOGLEVEL;
        msg.length = offsetof(stroke_msg_t, buffer);
        msg.loglevel.type = push_string(&msg, type);
@@ -318,6 +345,10 @@ static void exit_usage(char *error)
        printf("    stroke rereadsecrets|rereadcrls|rereadall\n");
        printf("  Purge ocsp cache entries:\n");
        printf("    stroke purgeocsp\n");
+       printf("  Purge IKE_SAs without a CHILD_SA:\n");
+       printf("    stroke purgeike\n");
+       printf("  Show leases of a pool:\n");
+       printf("    stroke leases [POOL [ADDRESS]]\n");
        exit_error(error);
 }
 
@@ -330,7 +361,7 @@ int main(int argc, char *argv[])
        {
                exit_usage(NULL);
        }
-       
+
        token = in_word_set(argv[1], strlen(argv[1]));
 
        if (token == NULL)
@@ -346,8 +377,8 @@ int main(int argc, char *argv[])
                                exit_usage("\"add\" needs more parameters...");
                        }
                        res = add_connection(argv[2],
-                                                                argv[3], argv[4], 
-                                                                argv[5], argv[6], 
+                                                                argv[3], argv[4],
+                                                                argv[5], argv[6],
                                                                 argv[7], argv[8]);
                        break;
                case STROKE_DELETE:
@@ -398,7 +429,7 @@ int main(int argc, char *argv[])
                        {
                                exit_usage("\"logtype\" needs more parameters...");
                        }
-                       res = set_loglevel(argv[2], atoi(argv[3])); 
+                       res = set_loglevel(argv[2], atoi(argv[3]));
                        break;
                case STROKE_STATUS:
                case STROKE_STATUSALL:
@@ -427,8 +458,13 @@ int main(int argc, char *argv[])
                        res = reread(token->kw);
                        break;
                case STROKE_PURGE_OCSP:
+               case STROKE_PURGE_IKE:
                        res = purge(token->kw);
                        break;
+               case STROKE_LEASES:
+                       res = leases(token->kw, argc > 2 ? argv[2] : NULL,
+                                                argc > 3 ? argv[3] : NULL);
+                       break;
                default:
                        exit_usage(NULL);
        }