Commits

Jeremy Rossi committed 0895ba0

more waf build

Comments (0)

Files changed (25)

src/addagent/Makefile

-# Makefile for addagent /manage_agents
-# Daniel B. Cid <dcid@ossec.net>
-# http://www.ossec.net
-
-NAME=manage_agents
-PT=../
-
-include $(PT)Config.Make
-
-OBJS = ${OS_SHARED} ${OS_REGEX} ${OS_ZLIB} ${OS_CRYPTO} ${OS_NET}
-
-handler:
-		$(CC) $(CFLAGS) ${OS_LINK} *.c $(OBJS) -o manage_agents
-clean:
-		$(CLEAN) manage_agents
-build:
-		${BUILD} 
-		cp -pr manage_agents ${PT}../bin

src/addagent/b64.c

-/* @(#) $Id$ */
-/*
- * Copyright (C), 2000-2004 by the monit project group.
- * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program 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
- * General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define TRUE    1
-#define FALSE   0
-
-char *decode_base64(const char *src);
-char *encode_base64(int size, char *src);
-
-/* Private prototypes */
-static int is_base64(char c);
-static char encode(unsigned char u);
-static unsigned char decode(char c);
-
-
-/**
- *  Implementation of base64 encoding/decoding. 
- *
- *  @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
- *
- *  @version \$Id$
- *
- *  @file
- */
-
-
-
-/**
- * Base64 encode and return size data in 'src'. The caller must free the
- * returned string.
- * @param size The size of the data in src
- * @param src The data to be base64 encode
- * @return encoded string otherwise NULL
- */
-char *encode_base64(int size, char *src) {
-
-    int i;
-    char *out, *p;
-
-    if(!src)
-        return NULL;
-
-    if(!size)
-        size= strlen((char *)src);
-
-    out = (char *)calloc(sizeof(char), size*4/3+4);
-    if(!out)
-        return NULL;
-    
-    p = out;
-
-    for(i = 0; i < size; i+=3) {
-
-        unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
-
-        b1 = src[i];
-
-        if(i+1<size)
-            b2 = src[i+1];
-
-        if(i+2<size)
-            b3 = src[i+2];
-
-        b4= b1>>2;
-        b5= ((b1&0x3)<<4)|(b2>>4);
-        b6= ((b2&0xf)<<2)|(b3>>6);
-        b7= b3&0x3f;
-
-        *p++= encode(b4);
-        *p++= encode(b5);
-
-        if(i+1<size) {
-            *p++= encode(b6);
-        } else {
-            *p++= '=';
-        }
-
-        if(i+2<size) {
-            *p++= encode(b7);
-        } else {
-            *p++= '=';
-        }
-
-    }
-
-    return out;
-
-}
-
-/**
- * Decode the base64 encoded string 'src' into the memory pointed to by
- * 'dest'. The dest buffer is NUL terminated.
- * Return NULL in case of error
- */
-char *decode_base64(const char *src) 
-{
-    if(src && *src) 
-    {
-        char *dest;
-        unsigned char *p;
-        int k, l = strlen(src)+1;
-        unsigned char *buf;
-        
-        /* The size of the dest will always be less than
-         * the source
-         */
-        dest = (char *)calloc(sizeof(char), l + 13);
-        if(!dest)
-            return(NULL);
-        
-        p = (unsigned char *)dest;
-         
-        buf = malloc(l);
-        if(!buf)
-            return(NULL);
-
-        /* Ignore non base64 chars as per the POSIX standard */
-        for(k=0, l=0; src[k]; k++) 
-        {
-            if(is_base64(src[k])) 
-            {
-                buf[l++]= src[k];
-            }
-        } 
-
-        for(k=0; k<l; k+=4) 
-        {
-            char c1='A', c2='A', c3='A', c4='A';
-            unsigned char b1=0, b2=0, b3=0, b4=0;
-
-            c1= buf[k];
-
-            if(k+1<l) 
-            {
-                c2= buf[k+1];
-            }
-
-            if(k+2<l) {
-                c3= buf[k+2];
-            }
-
-            if(k+3<l) {
-                c4= buf[k+3];
-            }
-
-            b1= decode(c1);
-            b2= decode(c2);
-            b3= decode(c3);
-            b4= decode(c4);
-
-            *p++=((b1<<2)|(b2>>4) );
-
-            if(c3 != '=') {
-                *p++=(((b2&0xf)<<4)|(b3>>2) );
-            }
-
-            if(c4 != '=') {
-                *p++=(((b3&0x3)<<6)|b4 );
-            }
-
-        }
-
-        free(buf);
-
-        /*return(p-dest); */
-        return(dest);
-
-    }
-
-    return(NULL);
-
-}
-
-
- /* ----------------------------------------------------------------- Private */
-
-static char encode(unsigned char u) {
-
-    if(u < 26)  return 'A'+u;
-    if(u < 52)  return 'a'+(u-26);
-    if(u < 62)  return '0'+(u-52);
-    if(u == 62) return '+';
-
-    return '/';
-
-}
-
-
-/**
- * Decode a base64 character
- */
-static unsigned char decode(char c) {
-
-    if(c >= 'A' && c <= 'Z') return(c - 'A');
-    if(c >= 'a' && c <= 'z') return(c - 'a' + 26);
-    if(c >= '0' && c <= '9') return(c - '0' + 52);
-    if(c == '+')             return 62;
-
-    return 63;
-
-}
-
-
-/**
- * Return TRUE if 'c' is a valid base64 character, otherwise FALSE
- */
-static int is_base64(char c) {
-
-    if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
-            (c >= '0' && c <= '9') || (c == '+')             ||
-            (c == '/')             || (c == '=')) {
-
-        return TRUE;
-
-    }
-
-    return FALSE;
-
-}
-
-
-/*
-int main(int argc, char **argv)
-{
-    char *s;
-    char *d;
-    
-    if(argc < 2)
-    {
-        printf("%s string\n",argv[0]);
-        exit(1);
-    }
-    s = encode_base64(strlen(argv[1]), argv[1]);
-
-    printf("b64:\n%s\n",s);
-
-
-    d = decode_base64(s);
-    printf("decode:%s\n",d);
-    
-    exit(0);
-}
-
-*/
-
-
-/* EOF */

src/addagent/main.c

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation
- */
-
-
-
-#include "manage_agents.h"
-
-
-/** help **/
-void helpmsg()
-{
-    printf("\nOSSEC HIDS %s: Manage agents.\n", ARGV0);
-    printf("Available options:\n");
-    printf("\t-h          This help message.\n");
-    printf("\t-V          Display OSSEC version.\n");
-    printf("\t-l          List available agents.\n");
-    printf("\t-e <id>     Extracts key for an agent (Manager only).\n");
-    printf("\t-i <id>     Import authentication key (Agent only).\n\n");
-    exit(1);
-}
-
-
-/* print banner */
-void print_banner()
-{
-    printf("\n");
-    printf(BANNER, __name, __version);
-
-    #ifdef CLIENT
-    printf(BANNER_CLIENT);
-    #else
-    printf(BANNER_OPT);
-    #endif
-
-    return;
-}
-
-
-/* Clean shutdown on kill */
-void manage_shutdown()
-{
-    /* Checking if restart message is necessary */
-    if(restart_necessary)
-    {
-        printf(MUST_RESTART);
-    }
-    else
-    {
-        printf("\n");
-    }
-    printf(EXIT);
-
-    exit(0);
-}
-
-
-/** main **/
-int main(int argc, char **argv)
-{
-    char *user_msg;
-
-    int c = 0, cmdlist = 0;
-    char *cmdexport = NULL;
-    char *cmdimport = NULL;
-    
-    #ifndef WIN32
-    char *dir = DEFAULTDIR;
-    char *group = GROUPGLOBAL;
-    int gid;
-    #endif
-    
-
-    /* Setting the name */
-    OS_SetName(ARGV0);
-        
-
-    while((c = getopt(argc, argv, "Vhle:i:")) != -1){
-        switch(c){
-	        case 'V':
-		        print_version();
-		        break;
-            case 'h':
-                helpmsg();
-                break;
-            case 'd':
-                nowDebug();
-                break;
-            case 'e':
-                #ifdef CLIENT
-                ErrorExit("%s: You can't export keys on an agent", ARGV0);
-                #endif
-                if(!optarg)
-                    ErrorExit("%s: -e needs an argument",ARGV0);
-                cmdexport = optarg;
-                break;
-            case 'i':
-                #ifndef CLIENT
-                ErrorExit("%s: You can't import keys on the manager.", ARGV0);
-                #endif
-                if(!optarg)
-                    ErrorExit("%s: -i needs an argument",ARGV0);
-                cmdimport = optarg;
-                break;
-            case 'l':
-                cmdlist = 1;
-                break;
-            default:
-                helpmsg();
-                break;
-        }
-
-    }
-    
-
-   
-    /* Getting currently time */
-    time1 = time(0);
-    restart_necessary = 0;
-    
-    
-    #ifndef WIN32 
-    /* Getting the group name */
-    gid = Privsep_GetGroup(group);
-    if(gid < 0)
-    {
-	    ErrorExit(USER_ERROR, ARGV0, "", group);
-    }
-	
-    
-    /* Setting the group */
-    if(Privsep_SetGroup(gid) < 0)
-    {
-	    ErrorExit(SETGID_ERROR, ARGV0, group);
-    }
-    
-    
-    /* Chrooting to the default directory */
-    if(Privsep_Chroot(dir) < 0)
-    {
-        ErrorExit(CHROOT_ERROR, ARGV0, dir);
-    }
-
-
-    /* Inside chroot now */
-    nowChroot();
-
-
-    /* Starting signal handler */
-    StartSIG2(ARGV0, manage_shutdown);
-    #endif
-
-
-    if(cmdlist == 1)
-    {
-        list_agents(cmdlist);
-        exit(0);
-    }
-    else if(cmdimport)
-    {
-        k_import(cmdimport);
-        exit(0);
-    }
-    else if(cmdexport)
-    {
-        k_extract(cmdexport);
-        exit(0);
-    }
-
-
-
-    /* Little shell */
-    while(1)
-    {
-        int leave_s = 0;
-        print_banner();
-   
-        user_msg = read_from_user();
-        
-        /* All the allowed actions */
-        switch(user_msg[0])
-        {
-            case 'A':
-            case 'a':
-                add_agent();
-                break;
-            case 'e':
-            case 'E':
-                k_extract(NULL);
-                break;
-            case 'i':
-            case 'I':
-                k_import(NULL);
-                break;    
-            case 'l':
-            case 'L':
-                list_agents(0);
-                break;    
-            case 'r':
-            case 'R':
-                remove_agent();
-                break;
-            case 'q':
-            case 'Q':
-                leave_s = 1;
-                break;
-	        case 'V':
-		        print_version();   
-		        break;
-            default:    
-                printf("\n ** Invalid Action ** \n\n");
-                break;            
-        }
-
-        if(leave_s)
-        {
-            break;       
-        }
-        
-        continue;
-        
-    }
-
-    /* Checking if restart message is necessary */
-    if(restart_necessary)
-    {
-        printf(MUST_RESTART);
-    }
-    else
-    {
-        printf("\n");
-    }
-    printf(EXIT);
-    
-    return(0);
-}
-
-
-/* EOF */

src/addagent/manage_agents.c

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation.
- *
- * License details at the LICENSE file included with OSSEC or 
- * online at: http://www.ossec.net/en/licensing.html
- */
-
-
-/* Manage agents tool
- * Add/extract and remove agents from a server.
- */
-
-
-#include "manage_agents.h"
-#include "os_crypto/md5/md5_op.h"
-
-
-
-/* Global internal variables */
-
-
-
-/* chomp: remove spaces, new lines, etc from a string */
-char *chomp(char *str)
-{
-    char *tmp_str;
-    int size = 0;
-
-    /* Removing spaces from the beginning */
-    while(*str == ' ' || *str == '\t')
-        str++;
-    
-    
-    /* Removing any trailing new lines or \r */
-    do
-    {
-        tmp_str = strchr(str, '\n');
-        if(tmp_str)
-        {
-            *tmp_str = '\0';
-            continue;
-        }
-
-        tmp_str = strchr(str, '\r');
-        if(tmp_str)
-        {
-            *tmp_str = '\0';
-        }
-    }while(tmp_str != NULL);
-
-    
-    /* Removing spaces at the end of the string */
-    tmp_str = str;
-    size = strlen(str)-1;
-    
-    while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
-    {
-        tmp_str[size] = '\0';
-        size--;
-    }
-    
-    return(str);
-}
-
-
-
-/* Add an agent */
-int add_agent()
-{
-    int i = 1;
-    FILE *fp;
-    char str1[STR_SIZE +1];
-    char str2[STR_SIZE +1];
-    
-    os_md5 md1;
-    os_md5 md2;
-    
-    char *user_input;
-    char *_name;
-    char *_id;
-    char *_ip;
-
-    char name[FILE_SIZE +1];
-    char id[FILE_SIZE +1];
-    char ip[FILE_SIZE +1];
-    os_ip *c_ip;
-
-
-    /* Checking if we can open the auth_file */
-    fp = fopen(AUTH_FILE,"a");
-    if(!fp)
-    {
-        ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
-    }
-    fclose(fp);
-
-
-    /* Allocating for c_ip */
-    os_calloc(1, sizeof(os_ip), c_ip);
-    
-    
-    #ifndef WIN32
-    chmod(AUTH_FILE, 0440);
-    #endif
-    
-    /* Setting time 2 */
-    time2 = time(0);
-
-    
-    /* Source is time1+ time2 +pid + ppid */
-    #ifndef WIN32
-        #ifdef __OpenBSD__
-        srandomdev();
-        #else
-        srandom(time2 + time1 + getpid() + getppid());
-        #endif
-    #else
-    srandom(time2 + time1 + getpid());
-    #endif
-
-    rand1 = random();
-
-    
-    /* Zeroing strings */
-    memset(str1,'\0', STR_SIZE +1);
-    memset(str2,'\0', STR_SIZE +1);
-
-
-    printf(ADD_NEW);
-
-    
-    /* Getting the name */
-    memset(name, '\0', FILE_SIZE +1);
-
-    do
-    {
-        printf(ADD_NAME);
-        fflush(stdout);
-        _name = read_from_user();
-
-        if(strcmp(_name, QUIT) == 0)
-            return(0);
-
-        strncpy(name, _name, FILE_SIZE -1);
-
-        /* check the name */
-        if(!OS_IsValidName(name))
-            printf(INVALID_NAME,name);
-
-        /* Search for name  -- no duplicates */
-        if(NameExist(name))
-            printf(ADD_ERROR_NAME, name);
-
-    } while(NameExist(name) || !OS_IsValidName(name));
-
-
-    /* Getting IP */
-    memset(ip, '\0', FILE_SIZE +1);
-
-    do
-    {
-      printf(ADD_IP);
-      fflush(stdout);
-    
-      _ip = read_from_user();
-      
-      /* quit */
-      if(strcmp(_ip, QUIT) == 0)
-          return(0);
-                              
-      strncpy(ip, _ip, FILE_SIZE -1);
-      
-      if(!OS_IsValidIP(ip, c_ip))
-      {
-          printf(IP_ERROR, ip);
-          _ip = NULL;
-      }
-
-    } while(!_ip);
-   
-    
-    do
-    {
-        /* Default ID */
-        i = 1024;
-        snprintf(id, 8, "%03d", i);
-        while(!IDExist(id))
-        {
-            i--;
-            snprintf(id, 8, "%03d", i);
-
-            /* No key present, use id 0 */
-            if(i <= 0)
-            {
-                i = 0;
-                break;
-            }
-        }
-        snprintf(id, 8, "%03d", i+1);
-
-        /* Getting ID */
-        printf(ADD_ID, id);
-        fflush(stdout);
-
-        _id = read_from_user();
-
-
-
-        /* quit */
-        if(strcmp(_id, QUIT) == 0)
-            return(0);
-
-
-        if(_id[0] != '\0')
-        {
-            strncpy(id, _id, FILE_SIZE -1);
-        }
-
-        if(!OS_IsValidID(id))
-            printf(INVALID_ID, id);
-
-        /* Search for ID KEY  -- no duplicates */
-        if(IDExist(id))
-            printf(ADD_ERROR_ID, id);
-
-    } while(IDExist(id) || !OS_IsValidID(id));
-    
-    
-
-    printf(AGENT_INFO, id, name, ip);
-    fflush(stdout);
-
-    do
-    {
-      printf(ADD_CONFIRM);
-      user_input = read_from_user();
-   
-      /* If user accepts to add */ 
-      if(user_input[0] == 'y' || user_input[0] == 'Y')
-      {
-        time3 = time(0);
-        rand2 = random();
-
-        fp = fopen(AUTH_FILE,"a");
-        if(!fp)
-        {
-            ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
-        }
-        #ifndef WIN32
-        chmod(AUTH_FILE, 0440);
-        #endif
-                
-        
-        /* Random 1: Time took to write the agent information.
-         * Random 2: Time took to choose the action.
-         * Random 3: All of this + time + pid
-         * Random 4: Md5 all of this + the name, key and ip
-         * Random 5: Final key
-         */
-        
-        snprintf(str1, STR_SIZE, "%d%s%d",time3-time2, name, rand1);
-        snprintf(str2, STR_SIZE, "%d%s%s%d", time2-time1, ip, id, rand2);
-
-        OS_MD5_Str(str1, md1);
-        OS_MD5_Str(str2, md2);
-
-        snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(), 
-                                            time3);
-        OS_MD5_Str(str1, md1);
-
-        fprintf(fp,"%s %s %s %s%s\n",id, name, c_ip->ip, md1,md2);
-
-        fclose(fp);
-
-        printf(AGENT_ADD);
-        restart_necessary = 1;
-        break;
-      }
-      else if(user_input[0] == 'n' || user_input[0] == 'N')
-      {
-        printf(ADD_NOT);
-        break;
-      }
-
-    } while(1);
-
-    return(0);
-}
-
-
-/* remove an agent */
-int remove_agent()
-{
-    FILE *fp;
-    char *user_input;
-    char u_id[FILE_SIZE +1];
-    
-    u_id[FILE_SIZE] = '\0';
-
-    if(!print_agents(0, 0, 0))
-    {
-        printf(NO_AGENT);
-        return(0);
-    }
-
-    do
-    {
-      printf(REMOVE_ID);
-      fflush(stdout);
-
-      user_input = read_from_user();
-
-      if(strcmp(user_input, QUIT) == 0)
-          return(0);
-
-      strncpy(u_id, user_input, FILE_SIZE);
-
-      if(!IDExist(user_input))
-      {
-        printf(NO_ID, user_input);
-      }
-    } while(!IDExist(user_input));
-    
-    do
-    {
-        printf(REMOVE_CONFIRM);
-        fflush(stdout);
-
-        user_input = read_from_user();
-
-        /* If user confirm */
-        if(user_input[0] == 'y' || user_input[0] == 'Y')
-        {
-            /* Getting full agent name */
-            char *full_name = getFullnameById(u_id);
-            if(!full_name)
-            {
-                ErrorExit(MEM_ERROR, ARGV0);
-            }
-            
-            fp = fopen(AUTH_FILE, "r+");
-            if(!fp)
-            {
-                free(full_name);
-                ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
-            }
-            #ifndef WIN32
-            chmod(AUTH_FILE, 0440);
-            #endif
-
-
-            /* Removing the agent, but keeping the id. */
-            fsetpos(fp, &fp_pos);
-            fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);
-
-            fclose(fp);
-
-
-            /* Remove counter for id */
-            delete_agentinfo(full_name); 
-            OS_RemoveCounter(u_id);
-            free(full_name);
-            full_name = NULL;
-
-
-            printf(REMOVE_DONE, u_id);
-            restart_necessary = 1;
-            break;
-        }
-        else if(user_input[0] == 'n' || user_input[0] == 'N')
-        {
-            printf(REMOVE_NOT);
-            break;
-        }
-
-    } while(1);
-
-    return(0);
-}
-
-
-int list_agents(int cmdlist)
-{
-    if(!print_agents(0, 0, 0))
-        printf(NO_AGENT);
-
-    printf("\n");
-    if(!cmdlist)
-    {
-        printf(PRESS_ENTER);
-        read_from_user();
-    }
-
-    return(0);
-
-}
-
-/* EOF */

src/addagent/manage_agents.h

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation.
- *
- * License details at the LICENSE file included with OSSEC or 
- * online at: http://www.ossec.net/en/licensing.html
- */
-
-
-#include "shared.h"
-#include "sec.h"
-
-
-/** Prototypes **/
-
-/* Read any input from the user (stdin) */
-char *read_from_user();
-
-/* Add or remove an agent */
-int add_agent();
-int remove_agent();
-
-/* Extract or import a key */
-int k_extract(char *cmdextract);
-int k_import(char *cmdimport);
-
-/* Validation functions */
-int OS_IsValidName(char *u_name);
-int OS_IsValidID(char *id);
-int IDExist(char *id);
-int NameExist(char *u_name);
-char *getFullnameById(char *id);
-
-
-/* Print available agents */
-int print_agents(int print_status, int active_only, int csv_output);
-int list_agents(int cmdlist);
-    
-/* clear a line */
-char *chomp(char *str);
-
-
-/* Shared variables */
-int restart_necessary;
-int time1;
-int time2;
-int time3;
-int rand1;
-int rand2;
-fpos_t fp_pos;
-
-
-/* Internal defines */
-#define USER_SIZE       514
-#define FILE_SIZE       257
-#define STR_SIZE        66
-
-/* Internal strings */
-#define QUIT                "\\q"
-
-/* Print agents */
-#define PRINT_AVAILABLE     "\nAvailable agents: \n"
-#define PRINT_AGENT         "   ID: %s, Name: %s, IP: %s\n"
-#define PRINT_AGENT_STATUS  "   ID: %s, Name: %s, IP: %s, %s\n"
-
-
-/* Add new agent */
-#define ADD_NEW         "\n- Adding a new agent"\
-                        " (use '\\q' to return to the main menu).\n"\
-                        "  Please provide the following:\n"
-#define ADD_NAME        "   * A name for the new agent: "
-#define ADD_IP          "   * The IP Address of the new agent: "
-#define ADD_ID          "   * An ID for the new agent[%s]: "
-#define AGENT_INFO      "Agent information:\n   ID:%s\n   Name:%s\n   " \
-                        "IP Address:%s\n\n"
-#define ADD_CONFIRM     "Confirm adding it?(y/n): "
-#define AGENT_ADD       "Agent added.\n"
-#define ADDED           "Added.\n"
-#define ADD_NOT         "Not Adding ..\n"
-#define PRESS_ENTER     "** Press ENTER to return to the main menu.\n"
-#define MUST_RESTART    "\n** You must restart the server for your changes" \
-                        " to have effect.\n\n"
-
-/* Add errors */
-#define ADD_ERROR_ID    "\n** ID '%s' already present. They must be unique.\n\n"
-#define ADD_ERROR_NAME  "\n** Name '%s' already present. Please enter a new name.\n\n"
-#define IP_ERROR        "\n** Invalid IP '%s'. Please enter a valid IP Address.\n\n"
-#define NO_AGENT        "\n** No agent available. You need to add one first.\n"
-#define NO_ID           "\n** Invalid ID '%s' given. ID is not present.\n"
-#define NO_KEY          "\n** Invalid authentication key. Starting over again.\n"
-#define INVALID_ID      "\n** Invalid ID '%s' given. ID must be numeric (max 5 digits).\n\n"
-#define INVALID_NAME    "\n** Invalid name '%s' given. Name must contain only alphanumeric characters (min=2, max=32).\n\n"
-
-/* Remove agent */
-#define REMOVE_ID       "Provide the ID of the agent to be removed (or '\\q' to quit): "
-#define REMOVE_CONFIRM  "Confirm deleting it?(y/n): "
-#define REMOVE_DONE     "Agent '%s' removed.\n"
-#define REMOVE_NOT      "Not removing ..\n"
-
-/* Import agent */    
-#define IMPORT_KEY      "\n* Provide the Key generated by the server.\n" \
-                        "* The best approach is to cut and paste it.\n" \
-                        "*** OBS: Do not include spaces or new lines.\n\n" \
-                        "Paste it here (or '\\q' to quit): "
-    
-/* extract key */    
-#define EXTRACT_KEY     "Provide the ID of the agent to extract " \
-                        "the key (or '\\q' to quit): "
-#define EXTRACT_MSG     "\nAgent key information for '%s' is: \n%s\n"
-
-
-/* Commom errors */
-#define ERROR_KEYS      "Unable to handle keys file. Exiting.\n"
-#define EXTRACT_ERROR   "Unable to extract agent key.\n"
-#define INPUT_LARGE     ARGV0 ": Input too large. Not adding it.\n"
-#define EXIT            ARGV0 ": Exiting ..\n"
-
-#define BANNER          "\n****************************************" \
-                        "\n* %s %s Agent manager.     *" \
-                        "\n* The following options are available: *" \
-                        "\n****************************************\n"
-    
-#define BANNER_OPT      "   (A)dd an agent (A).\n" \
-                        "   (E)xtract key for an agent (E).\n" \
-                        "   (L)ist already added agents (L).\n" \
-                        "   (R)emove an agent (R).\n" \
-                        "   (Q)uit.\n" \
-                        "Choose your action: A,E,L,R or Q: "
-
-#define BANNER_CLIENT   "   (I)mport key from the server (I).\n" \
-                        "   (Q)uit.\n" \
-                        "Choose your action: I or Q: "
-                        
-/* EOF */

src/addagent/manage_keys.c

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation.
- *
- * License details at the LICENSE file included with OSSEC or 
- * online at: http://www.ossec.net/en/licensing.html
- */
-
-
-#include "manage_agents.h"
-
-
-/* b64 function prototypes */
-char *decode_base64(const char *src);
-char *encode_base64(int size, char *src);
-
-
-/* Import a key */
-int k_import(char *cmdimport)
-{
-    FILE *fp;
-    char *user_input;
-    char *b64_dec;
-   
-    char *name; char *ip; char *tmp_key;
-     
-    char line_read[FILE_SIZE +1];
-    
-
-    /* Parsing user argument. */
-    if(cmdimport)
-    {
-        user_input = cmdimport;
-    }
-    else
-    {
-        printf(IMPORT_KEY);
-
-        user_input = read_from_user();
-    }
-
-
-    /* quit */
-    if(strcmp(user_input, QUIT) == 0)
-        return(0);
-    
-    b64_dec = decode_base64(user_input);
-    if(b64_dec == NULL)
-    {
-        printf(NO_KEY);
-        printf(PRESS_ENTER);
-        read_from_user();
-        return(0);
-    }
-
-    
-    memset(line_read, '\0', FILE_SIZE +1);
-    strncpy(line_read, b64_dec, FILE_SIZE);
-
-
-    name = strchr(b64_dec, ' ');
-    if(name && strlen(line_read) < FILE_SIZE)
-    {
-        *name = '\0';
-        name++;
-        ip = strchr(name, ' ');
-        if(ip)
-        {
-            *ip = '\0';
-            ip++;
-
-            tmp_key = strchr(ip, ' ');
-            if(!tmp_key)
-            {
-                printf(NO_KEY);
-                return(0);
-            }
-            *tmp_key = '\0';
-        
-            printf("\n");   
-            printf(AGENT_INFO, b64_dec, name, ip);
-            
-            while(1)
-            {
-                printf(ADD_CONFIRM);
-                fflush(stdout);
-
-                user_input = read_from_user();
-
-                if(user_input[0] == 'y' || user_input[0] == 'Y')
-                {
-                    fp = fopen(KEYS_FILE,"w");
-                    if(!fp)
-                    {
-                        ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
-                    }
-                    fprintf(fp,"%s\n",line_read);
-                    fclose(fp);
-                    #ifndef WIN32
-                    chmod(KEYS_FILE, 0440);
-                    #endif
-
-                    /* Removing sender counter. */
-                    OS_RemoveCounter("sender");
-                            
-                    printf(ADDED);
-                    printf(PRESS_ENTER);
-                    read_from_user();
-                    restart_necessary = 1;
-                    return(1);
-                }
-                else if(user_input[0] == 'n' || user_input[0] == 'N')
-                {
-                    printf("%s", ADD_NOT);
-                    return(0);
-                }
-            }
-        }
-    }
-    
-    printf(NO_KEY);
-    printf(PRESS_ENTER);
-    read_from_user();
-    return(0);
-
-}
-
-
-/* extract base64 for a specific agent */
-int k_extract(char *cmdextract)
-{
-    FILE *fp;
-    char *user_input;
-    char *b64_enc;
-    char line_read[FILE_SIZE +1];
-    char n_id[USER_SIZE +1];
-
-
-    if(cmdextract)
-    {
-        user_input = cmdextract;
-
-        if(!IDExist(user_input))
-        {
-            printf(NO_ID, user_input);
-            exit(1);
-        }
-    }
-
-    else
-    {
-        if(!print_agents(0, 0, 0))
-        {
-            printf(NO_AGENT);
-            printf(PRESS_ENTER);
-            read_from_user();
-            return(0);
-        }
-
-        do
-        {
-            printf(EXTRACT_KEY);
-            fflush(stdout);
-            user_input = read_from_user();
-
-            /* quit */
-            if(strcmp(user_input, QUIT) == 0)
-                return(0);
-
-            if(!IDExist(user_input))
-                printf(NO_ID, user_input);
-
-        } while(!IDExist(user_input));
-    }
-
-    
-    /* Trying to open the auth file */
-    fp = fopen(AUTH_FILE, "r");
-    if(!fp)
-    {
-        ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
-    }
-    
-    fsetpos(fp, &fp_pos);
-
-    memset(n_id, '\0', USER_SIZE +1);
-    strncpy(n_id, user_input, USER_SIZE -1);
-    
-    
-    if(fgets(line_read, FILE_SIZE, fp) == NULL)
-    {
-        printf(ERROR_KEYS);
-        fclose(fp);
-        exit(1);
-    }
-    chomp(line_read);
-
-    
-    b64_enc = encode_base64(strlen(line_read),line_read);
-    if(b64_enc == NULL)
-    {
-        printf(EXTRACT_ERROR);
-        fclose(fp);
-        exit(1);
-    }
-
-    printf(EXTRACT_MSG, n_id, b64_enc);
-    if(!cmdextract)
-    {
-        printf("\n" PRESS_ENTER);
-        read_from_user();
-    }
-
-    free(b64_enc);
-    fclose(fp);
-
-    return(0);
-}
-
-
-/* EOF */

src/addagent/read_from_user.c

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation
- */
-
-
-
-#include "manage_agents.h"
-
-char __user_buffer[USER_SIZE +1];
-char *__user_buffer_pt;
-
-
-/** read_from_user **/
-char *read_from_user()
-{
-    memset(__user_buffer, '\0', USER_SIZE +1);
-
-    if((fgets(__user_buffer, USER_SIZE -1, stdin) == NULL) || 
-       (strlen(__user_buffer) >= (USER_SIZE -2)))
-    {
-        printf(INPUT_LARGE);
-        exit(1);
-    }
-
-    __user_buffer_pt = chomp(__user_buffer);
-
-    return(__user_buffer_pt);
-}
-
-
-/* EOF */

src/addagent/validate.c

-/* @(#) $Id$ */
-
-/* Copyright (C) 2009 Trend Micro Inc.
- * All rights reserved.
- *
- * This program is a free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public
- * License (version 2) as published by the FSF - Free Software
- * Foundation
- */
-
-
-
-#include "manage_agents.h"
-
-
-int OS_IsValidID(char *id)
-{
-    int id_len = 0;
-    int i = 0;
-    
-    /* ID must not be null */ 
-    if(!id)
-      return(0);
-
-    id_len = strlen(id);
-
-    /* Check ID length, it should contain max. 5 characters */
-    if (id_len > 8)
-      return(0);
-
-    /* Check ID if it contains only numeric characters [0-9] */
-    for(i = 0; i < id_len; i++)
-    {
-      if(!(isdigit((int)id[i])))
-        return(0);
-    }
-    
-    return(1);
-}
-
-
-/* Get full agent name (name + ip) of ID.
- */
-char *getFullnameById(char *id)
-{
-    FILE *fp;
-    char line_read[FILE_SIZE +1];
-    line_read[FILE_SIZE] = '\0';
-
-    /* ID must not be null */
-    if(!id)
-        return(NULL);
-
-    fp = fopen(AUTH_FILE, "r");
-    if(!fp)
-        return(NULL);
-
-
-    while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
-    {
-        char *name;
-        char *ip;
-        char *tmp_str;
-
-        if(line_read[0] == '#')
-        {
-            continue;
-        }
-
-        name = strchr(line_read, ' ');
-        if(name)
-        {
-            *name = '\0';
-            /* Didn't match */
-            if(strcmp(line_read,id) != 0)
-            {
-                continue;
-            }
-
-            name++;
-
-            /* Removed entry */
-            if(*name == '#')
-            {
-                continue;
-            }
-            
-            ip = strchr(name, ' ');
-            if(ip)
-            {
-                *ip = '\0';
-                ip++;
-
-                /* Cleaning up ip */
-                tmp_str = strchr(ip, ' ');
-                if(tmp_str)
-                {
-                    char *final_str;
-                    *tmp_str = '\0';
-                    tmp_str = strchr(ip, '/');
-                    if(tmp_str)
-                        *tmp_str = '\0';
-
-                    /* If we reached here, we found the IP and name */
-                    os_calloc(1, FILE_SIZE, final_str);
-                    snprintf(final_str, FILE_SIZE -1, "%s-%s", name, ip);
-
-                    fclose(fp);
-                    return(final_str);        
-                }
-            }
-        }
-    }
-
-    fclose(fp);
-    return(NULL);
-}
-
-
-/* ID Search (is valid ID) */
-int IDExist(char *id)
-{
-    FILE *fp;
-    char line_read[FILE_SIZE +1];
-    line_read[FILE_SIZE] = '\0';
-   
-    /* ID must not be null */ 
-    if(!id)
-        return(0);
-
-    fp = fopen(AUTH_FILE, "r");
-    if(!fp)
-        return(0);
-        
-    fseek(fp, 0, SEEK_SET);
-    fgetpos(fp, &fp_pos);
-    
-    while(fgets(line_read,FILE_SIZE -1, fp) != NULL)
-    {
-        char *name;
-
-        if(line_read[0] == '#')
-        {
-            fgetpos(fp, &fp_pos);
-            continue;
-        }
-        
-        name = strchr(line_read, ' ');
-        if(name)
-        {
-            *name = '\0';
-            name++;
-
-            if(strcmp(line_read,id) == 0)
-            {
-                fclose(fp);
-                return (1); /*(fp_pos);*/
-            }
-        }
-
-        fgetpos(fp, &fp_pos);
-    }
-
-    fclose(fp);
-    return(0);
-}
-
-
-/* Validate agent name.
- */
-int OS_IsValidName(char *u_name)
-{
-    int i = 0;
-
-    /* We must have something in the name */
-    if(strlen(u_name) < 2 || strlen(u_name) > 128)
-      return(0);
-
-    /* check if it contains any non-alphanumeric characters */
-    for(i = 0; i < strlen(u_name); i++)
-    {
-      if(!isalnum((int)u_name[i]) && (u_name[i] != '-') && 
-         (u_name[i] != '_') && (u_name[i] != '.'))
-        return(0);
-    }
-
-    return(1);
-}
-
-
-/* Is_Name (is valid name) */
-int NameExist(char *u_name)
-{
-    FILE *fp;
-    char line_read[FILE_SIZE +1];
-    line_read[FILE_SIZE] = '\0';
-
-    if((!u_name)||
-       (*u_name == '\0')||
-       (*u_name == '\r')||
-       (*u_name == '\n'))
-        return(0);
-
-    fp = fopen(AUTH_FILE, "r");
-    if(!fp)
-        return(0);
-
-
-    fseek(fp, 0, SEEK_SET);
-    fgetpos(fp, &fp_pos);
-
-
-    while(fgets(line_read, FILE_SIZE-1, fp) != NULL)
-    {
-        char *name;
-
-        if(line_read[0] == '#')
-            continue;
-
-        name = strchr(line_read, ' ');
-        if(name)
-        {
-            char *ip;
-            name++;
-
-            if(*name == '#')
-            {
-                continue;
-            }
-            
-            ip = strchr(name, ' ');
-            if(ip)
-            {
-                *ip = '\0';
-                if(strcmp(u_name, name) == 0)
-                {
-                    fclose(fp);
-                    return(1);
-                }
-            }
-        }
-        fgetpos(fp, &fp_pos);
-    }
-
-    fclose(fp);
-    return(0);
-}
-
-
-/* print available agents */
-int print_agents(int print_status, int active_only, int csv_output)
-{
-    int total = 0;
-    FILE *fp;
-    char line_read[FILE_SIZE +1];
-    line_read[FILE_SIZE] = '\0';
-
-    fp = fopen(AUTH_FILE, "r");
-    if(!fp)
-        return(0);
-
-    fseek(fp, 0, SEEK_SET);
-    
-    memset(line_read,'\0',FILE_SIZE);
-    
-    while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
-    {
-        char *name;
-
-        if(line_read[0] == '#')
-            continue;
-            
-        name = strchr(line_read, ' ');
-        if(name)
-        {
-            char *ip;
-            *name = '\0';
-            name++;
-
-
-            /* Removed agent. */
-            if(*name == '#')
-            {
-                continue;
-            }
-            
-            ip = strchr(name, ' ');
-            if(ip)
-            {
-                char *key;
-                *ip = '\0';
-                ip++;
-                key = strchr(ip, ' ');
-                if(key)
-                {
-                    *key = '\0';
-                    if(!total && !print_status)
-                        printf(PRINT_AVAILABLE);
-                    total++;
-
-                    
-                    if(print_status)
-                    {
-                        int agt_status = get_agent_status(name, ip);
-                        if(active_only && (agt_status != GA_STATUS_ACTIVE))
-                        {
-                            continue;
-                        }
-            
-                        if(csv_output)
-                        {
-                            printf("%s,%s,%s,%s,\n", line_read, name, ip, 
-                                                  print_agent_status(agt_status));  
-                        }
-                        else
-                        {
-                            printf(PRINT_AGENT_STATUS, line_read, name, ip, 
-                                   print_agent_status(agt_status));
-                        }
-                    }
-                    else
-                    {
-                        printf(PRINT_AGENT, line_read, name, ip);
-                    }
-                }
-                
-            }
-        }
-    }
-
-
-    /* Only print agentless for non-active only searches */
-    if(!active_only && print_status)
-    {
-        char *aip = NULL;
-        DIR *dirp;
-        struct dirent *dp;
-        
-        if(!csv_output)
-        {
-            printf("\nList of agentless devices:\n");
-        }
-
-        dirp = opendir(AGENTLESS_ENTRYDIR);
-        if(dirp)
-        {
-            while ((dp = readdir(dirp)) != NULL)
-            {
-                if(strncmp(dp->d_name, ".", 1) == 0)
-                {
-                    continue;
-                }
-
-                aip = strchr(dp->d_name, '@');
-                if(aip)
-                {
-                    aip++;
-                }
-                else
-                {
-                    aip = "<na>";
-                }
-
-                if(csv_output)
-                {
-                    printf("na,%s,%s,agentless,\n", dp->d_name, aip);
-                }
-                else
-                {
-                    printf("   ID: na, Name: %s, IP: %s, agentless\n",
-                           dp->d_name, aip);
-                }
-            }
-            closedir(dirp);
-        }
-    }
-
-    fclose(fp);
-    if(total)
-        return(1);
-    
-    return(0);    
-}
-
-
-/* EOF */

src/addagent/wscript

-#!/usr/bin/env python
-#from waflib.Configure import conf
-
-
-def build(ctx):
-    
-    ctx.program(
-        source = ctx.path.ant_glob("*.c"),
-        target = "manage_agents",
-        defines = ['ARGV0="manage_agents"'],
-        includes = ".",
-        use = "os_headers os_zlib os_core ",
-    )

src/libos_cli/Makefile

+# Makefile for addagent /manage_agents
+# Daniel B. Cid <dcid@ossec.net>
+# http://www.ossec.net
+
+NAME=manage_agents
+PT=../
+
+include $(PT)Config.Make
+
+OBJS = ${OS_SHARED} ${OS_REGEX} ${OS_ZLIB} ${OS_CRYPTO} ${OS_NET}
+
+handler:
+		$(CC) $(CFLAGS) ${OS_LINK} *.c $(OBJS) -o manage_agents
+clean:
+		$(CLEAN) manage_agents
+build:
+		${BUILD} 
+		cp -pr manage_agents ${PT}../bin

src/libos_cli/b64.c

+/* @(#) $Id$ */
+/*
+ * Copyright (C), 2000-2004 by the monit project group.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define TRUE    1
+#define FALSE   0
+
+char *decode_base64(const char *src);
+char *encode_base64(int size, char *src);
+
+/* Private prototypes */
+static int is_base64(char c);
+static char encode(unsigned char u);
+static unsigned char decode(char c);
+
+
+/**
+ *  Implementation of base64 encoding/decoding. 
+ *
+ *  @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
+ *
+ *  @version \$Id$
+ *
+ *  @file
+ */
+
+
+
+/**
+ * Base64 encode and return size data in 'src'. The caller must free the
+ * returned string.
+ * @param size The size of the data in src
+ * @param src The data to be base64 encode
+ * @return encoded string otherwise NULL
+ */
+char *encode_base64(int size, char *src) {
+
+    int i;
+    char *out, *p;
+
+    if(!src)
+        return NULL;
+
+    if(!size)
+        size= strlen((char *)src);
+
+    out = (char *)calloc(sizeof(char), size*4/3+4);
+    if(!out)
+        return NULL;
+    
+    p = out;
+
+    for(i = 0; i < size; i+=3) {
+
+        unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
+
+        b1 = src[i];
+
+        if(i+1<size)
+            b2 = src[i+1];
+
+        if(i+2<size)
+            b3 = src[i+2];
+
+        b4= b1>>2;
+        b5= ((b1&0x3)<<4)|(b2>>4);
+        b6= ((b2&0xf)<<2)|(b3>>6);
+        b7= b3&0x3f;
+
+        *p++= encode(b4);
+        *p++= encode(b5);
+
+        if(i+1<size) {
+            *p++= encode(b6);
+        } else {
+            *p++= '=';
+        }
+
+        if(i+2<size) {
+            *p++= encode(b7);
+        } else {
+            *p++= '=';
+        }
+
+    }
+
+    return out;
+
+}
+
+/**
+ * Decode the base64 encoded string 'src' into the memory pointed to by
+ * 'dest'. The dest buffer is NUL terminated.
+ * Return NULL in case of error
+ */
+char *decode_base64(const char *src) 
+{
+    if(src && *src) 
+    {
+        char *dest;
+        unsigned char *p;
+        int k, l = strlen(src)+1;
+        unsigned char *buf;
+        
+        /* The size of the dest will always be less than
+         * the source
+         */
+        dest = (char *)calloc(sizeof(char), l + 13);
+        if(!dest)
+            return(NULL);
+        
+        p = (unsigned char *)dest;
+         
+        buf = malloc(l);
+        if(!buf)
+            return(NULL);
+
+        /* Ignore non base64 chars as per the POSIX standard */
+        for(k=0, l=0; src[k]; k++) 
+        {
+            if(is_base64(src[k])) 
+            {
+                buf[l++]= src[k];
+            }
+        } 
+
+        for(k=0; k<l; k+=4) 
+        {
+            char c1='A', c2='A', c3='A', c4='A';
+            unsigned char b1=0, b2=0, b3=0, b4=0;
+
+            c1= buf[k];
+
+            if(k+1<l) 
+            {
+                c2= buf[k+1];
+            }
+
+            if(k+2<l) {
+                c3= buf[k+2];
+            }
+
+            if(k+3<l) {
+                c4= buf[k+3];
+            }
+
+            b1= decode(c1);
+            b2= decode(c2);
+            b3= decode(c3);
+            b4= decode(c4);
+
+            *p++=((b1<<2)|(b2>>4) );
+
+            if(c3 != '=') {
+                *p++=(((b2&0xf)<<4)|(b3>>2) );
+            }
+
+            if(c4 != '=') {
+                *p++=(((b3&0x3)<<6)|b4 );
+            }
+
+        }
+
+        free(buf);
+
+        /*return(p-dest); */
+        return(dest);
+
+    }
+
+    return(NULL);
+
+}
+
+
+ /* ----------------------------------------------------------------- Private */
+
+static char encode(unsigned char u) {
+
+    if(u < 26)  return 'A'+u;
+    if(u < 52)  return 'a'+(u-26);
+    if(u < 62)  return '0'+(u-52);
+    if(u == 62) return '+';
+
+    return '/';
+
+}
+
+
+/**
+ * Decode a base64 character
+ */
+static unsigned char decode(char c) {
+
+    if(c >= 'A' && c <= 'Z') return(c - 'A');
+    if(c >= 'a' && c <= 'z') return(c - 'a' + 26);
+    if(c >= '0' && c <= '9') return(c - '0' + 52);
+    if(c == '+')             return 62;
+
+    return 63;
+
+}
+
+
+/**
+ * Return TRUE if 'c' is a valid base64 character, otherwise FALSE
+ */
+static int is_base64(char c) {
+
+    if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
+            (c >= '0' && c <= '9') || (c == '+')             ||
+            (c == '/')             || (c == '=')) {
+
+        return TRUE;
+
+    }
+
+    return FALSE;
+
+}
+
+
+/*
+int main(int argc, char **argv)
+{
+    char *s;
+    char *d;
+    
+    if(argc < 2)
+    {
+        printf("%s string\n",argv[0]);
+        exit(1);
+    }
+    s = encode_base64(strlen(argv[1]), argv[1]);
+
+    printf("b64:\n%s\n",s);
+
+
+    d = decode_base64(s);
+    printf("decode:%s\n",d);
+    
+    exit(0);
+}
+
+*/
+
+
+/* EOF */

src/libos_cli/manage_agents.c

+/* @(#) $Id$ */
+
+/* Copyright (C) 2009 Trend Micro Inc.
+ * All rights reserved.
+ *
+ * This program is a free software; you can redistribute it
+ * and/or modify it under the terms of the GNU General Public
+ * License (version 2) as published by the FSF - Free Software
+ * Foundation.
+ *
+ * License details at the LICENSE file included with OSSEC or 
+ * online at: http://www.ossec.net/en/licensing.html
+ */
+
+
+/* Manage agents tool
+ * Add/extract and remove agents from a server.
+ */
+
+
+#include "manage_agents.h"
+#include "os_crypto/md5/md5_op.h"
+
+
+
+/* Global internal variables */
+
+
+
+/* chomp: remove spaces, new lines, etc from a string */
+char *chomp(char *str)
+{
+    char *tmp_str;
+    int size = 0;
+
+    /* Removing spaces from the beginning */
+    while(*str == ' ' || *str == '\t')
+        str++;
+    
+    
+    /* Removing any trailing new lines or \r */
+    do
+    {
+        tmp_str = strchr(str, '\n');
+        if(tmp_str)
+        {
+            *tmp_str = '\0';
+            continue;
+        }
+
+        tmp_str = strchr(str, '\r');
+        if(tmp_str)
+        {
+            *tmp_str = '\0';
+        }
+    }while(tmp_str != NULL);
+
+    
+    /* Removing spaces at the end of the string */
+    tmp_str = str;
+    size = strlen(str)-1;
+    
+    while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
+    {
+        tmp_str[size] = '\0';
+        size--;
+    }
+    
+    return(str);
+}
+
+
+
+/* Add an agent */
+int add_agent()
+{
+    int i = 1;
+    FILE *fp;
+    char str1[STR_SIZE +1];
+    char str2[STR_SIZE +1];
+    
+    os_md5 md1;
+    os_md5 md2;
+    
+    char *user_input;
+    char *_name;
+    char *_id;
+    char *_ip;
+
+    char name[FILE_SIZE +1];
+    char id[FILE_SIZE +1];
+    char ip[FILE_SIZE +1];
+    os_ip *c_ip;
+
+
+    /* Checking if we can open the auth_file */
+    fp = fopen(AUTH_FILE,"a");
+    if(!fp)
+    {
+        ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
+    }
+    fclose(fp);
+
+
+    /* Allocating for c_ip */
+    os_calloc(1, sizeof(os_ip), c_ip);
+    
+    
+    #ifndef WIN32
+    chmod(AUTH_FILE, 0440);
+    #endif
+    
+    /* Setting time 2 */
+    time2 = time(0);
+
+    
+    /* Source is time1+ time2 +pid + ppid */
+    #ifndef WIN32
+        #ifdef __OpenBSD__
+        srandomdev();
+        #else
+        srandom(time2 + time1 + getpid() + getppid());
+        #endif
+    #else
+    srandom(time2 + time1 + getpid());
+    #endif
+
+    rand1 = random();
+
+    
+    /* Zeroing strings */
+    memset(str1,'\0', STR_SIZE +1);
+    memset(str2,'\0', STR_SIZE +1);
+
+
+    printf(ADD_NEW);
+
+    
+    /* Getting the name */
+    memset(name, '\0', FILE_SIZE +1);
+
+    do
+    {
+        printf(ADD_NAME);
+        fflush(stdout);
+        _name = read_from_user();
+
+        if(strcmp(_name, QUIT) == 0)
+            return(0);
+
+        strncpy(name, _name, FILE_SIZE -1);
+
+        /* check the name */
+        if(!OS_IsValidName(name))
+            printf(INVALID_NAME,name);
+
+        /* Search for name  -- no duplicates */
+        if(NameExist(name))
+            printf(ADD_ERROR_NAME, name);
+
+    } while(NameExist(name) || !OS_IsValidName(name));
+
+
+    /* Getting IP */
+    memset(ip, '\0', FILE_SIZE +1);
+
+    do
+    {
+      printf(ADD_IP);
+      fflush(stdout);
+    
+      _ip = read_from_user();
+      
+      /* quit */
+      if(strcmp(_ip, QUIT) == 0)
+          return(0);
+                              
+      strncpy(ip, _ip, FILE_SIZE -1);
+      
+      if(!OS_IsValidIP(ip, c_ip))
+      {
+          printf(IP_ERROR, ip);
+          _ip = NULL;
+      }
+
+    } while(!_ip);
+   
+    
+    do
+    {
+        /* Default ID */
+        i = 1024;
+        snprintf(id, 8, "%03d", i);
+        while(!IDExist(id))
+        {
+            i--;
+            snprintf(id, 8, "%03d", i);
+
+            /* No key present, use id 0 */
+            if(i <= 0)
+            {
+                i = 0;
+                break;
+            }
+        }
+        snprintf(id, 8, "%03d", i+1);
+
+        /* Getting ID */
+        printf(ADD_ID, id);