Ejemplo n.º 1
0
void no_input_file_and_output_file(struct list_s *list, int reverse)
{
    read_from_user(list);
    printf("\n\n");
    sort(list, reverse);
    printf("sorted lists: \n");
    print_list(list);
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
/* 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 = getenv("OSSEC_AGENT_KEY");
        if (user_input == NULL) {
          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 = getenv("OSSEC_ACTION_CONFIRMED");
                if (user_input == NULL) {
                  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);

}
Ejemplo n.º 4
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);
}
Ejemplo n.º 5
0
/** main **/
int main(int argc, char **argv)
{
    char *user_msg;

    int c = 0, cmdlist = 0;
    char *cmdexport = NULL;
    char *cmdimport = NULL;
    char *cmdbulk = NULL;

    #ifndef WIN32
    char *dir = DEFAULTDIR;
    char *group = GROUPGLOBAL;
    int gid;
    #else
    FILE *fp;
    TCHAR path[2048];
    DWORD last_error;
    int ret;
    #endif


    /* Setting the name */
    OS_SetName(ARGV0);


    while((c = getopt(argc, argv, "Vhle:r:i:f:")) != -1){
        switch(c){
            case 'V':
                print_version();
                break;
            case 'h':
                helpmsg();
                break;
            case 'e':
                #ifdef CLIENT
                ErrorExit("%s: Key export only available on a master.", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -e needs an argument.", ARGV0);
                cmdexport = optarg;
                break;
            case 'r':
                #ifdef CLIENT
                ErrorExit("%s: Key removal only available on a master.", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -r needs an argument.", ARGV0);

                /* Use environment variables already available to remove_agent() */
                setenv("OSSEC_ACTION", "r", 1);
                setenv("OSSEC_AGENT_ID", optarg, 1);
                setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
                break;
            case 'i':
                #ifndef CLIENT
                ErrorExit("%s: Key import only available on an agent.", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -i needs an argument.", ARGV0);
                cmdimport = optarg;
                break;
            case 'f':
                #ifdef CLIENT
                ErrorExit("%s: Bulk generate keys only available on a master.", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -f needs an argument.", ARGV0);
                cmdbulk = optarg;
                printf("Bulk load file: %s\n", cmdbulk);
                break;
            case 'l':
                cmdlist = 1;
                break;
            default:
                helpmsg();
                break;
        }

    }



    /* Get current 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);

    #else

    /* Get full path to the directory this
     * executable lives in
     */
    ret = GetModuleFileName(NULL, path, sizeof(path));

    /* check for errors */
    if(!ret)
    {
        ErrorExit(GMF_ERROR);
    }

    /* Get last error */
    last_error = GetLastError();

    /* Look for errors */
    if(last_error != ERROR_SUCCESS)
    {
        if(last_error == ERROR_INSUFFICIENT_BUFFER)
        {
            ErrorExit(GMF_BUFF_ERROR, ret, sizeof(path));
        }
        else
        {
            ErrorExit(GMF_UNKN_ERROR, last_error);
        }
    }

    /* Remove file name from path */
    PathRemoveFileSpec(path);

    /* Move to correct directory */
    if(chdir(path))
    {
        ErrorExit(CHDIR_ERROR_2, path);
    }

    /* Check permissions */
    fp = fopen(OSSECCONF, "r");
    if(fp)
    {
        fclose(fp);
    }
    else
    {
        ErrorExit(CONF_ERROR, OSSECCONF);
    }

    #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);
    }
    else if(cmdbulk)
    {
        k_bulkload(cmdbulk);
        exit(0);
    }



    /* Little shell */
    while(1)
    {
        int leave_s = 0;
        print_banner();

        /* Get ACTION from the environment. If ACTION is specified,
         * we must set leave_s = 1 to ensure that the loop will end */
        user_msg = getenv("OSSEC_ACTION");
        if (user_msg == NULL) {
          user_msg = read_from_user();
        }
        else{
          leave_s = 1;
        }

        /* 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);
}
Ejemplo n.º 6
0
/* Import a key */
int k_import(const char *cmdimport)
{
    FILE *fp;
    const char *user_input;
    char *b64_dec;

    char *name;
    char *ip;
    char *tmp_key;

    char line_read[FILE_SIZE + 1];

    char auth_file_tmp[] = AUTH_FILE;
    char *keys_file = basename_ex(auth_file_tmp);

    char tmp_path[strlen(TMP_DIR) + 1 + strlen(keys_file) + 6 + 1];

    snprintf(tmp_path, sizeof(tmp_path), "%s/%sXXXXXX", TMP_DIR, keys_file);

    /* Parse user argument */
    if (cmdimport) {
        user_input = cmdimport;
    } else {
        printf(IMPORT_KEY);

        user_input = getenv("OSSEC_AGENT_KEY");
        if (user_input == NULL) {
            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);
                free(b64_dec);
                return (0);
            }
            *tmp_key = '\0';

            printf("\n");
            printf(AGENT_INFO, b64_dec, name, ip);

            while (1) {
                printf(ADD_CONFIRM);
                fflush(stdout);

                user_input = getenv("OSSEC_ACTION_CONFIRMED");
                if (user_input == NULL) {
                    user_input = read_from_user();
                }

                if (user_input[0] == 'y' || user_input[0] == 'Y') {
                    if (mkstemp_ex(tmp_path)) {
                        ErrorExit(MKSTEMP_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                    }

#ifndef WIN32
                    if (chmod(tmp_path, 0440) == -1) {
                        if (unlink(tmp_path)) {
                            verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                        }

                        ErrorExit(CHMOD_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                    }
#endif

                    fp = fopen(tmp_path, "w");
                    if (!fp) {
                        if (unlink(tmp_path)) {
                            verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                        }

                        ErrorExit(FOPEN_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                    }
                    fprintf(fp, "%s\n", line_read);
                    fclose(fp);

                    if (rename_ex(tmp_path, KEYS_FILE)) {
                        if (unlink(tmp_path)) {
                            verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
                        }

                        ErrorExit(RENAME_ERROR, ARGV0, tmp_path, KEYS_FILE, errno, strerror(errno));
                    }

                    /* Remove sender counter */
                    OS_RemoveCounter("sender");

                    printf(ADDED);
                    printf(PRESS_ENTER);
                    read_from_user();
                    restart_necessary = 1;

                    free(b64_dec);
                    return (1);
                } else { /* if(user_input[0] == 'n' || user_input[0] == 'N') */
                    printf("%s", ADD_NOT);

                    free(b64_dec);
                    return (0);
                }
            }
        }
    }

    printf(NO_KEY);
    printf(PRESS_ENTER);
    read_from_user();

    free(b64_dec);
    return (0);
}
Ejemplo n.º 7
0
/* Extract base64 for a specific agent */
int k_extract(const 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 = strdup(cmdextract);
        FormatID(user_input);

        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);
        }

        while (1) {
            printf(EXTRACT_KEY);
            fflush(stdout);
            user_input = read_from_user();

            /* quit */
            if (strcmp(user_input, QUIT) == 0) {
                return (0);
            }

            FormatID(user_input);

            if (IDExist(user_input)) {
                break;
            } else {
                printf(NO_ID, user_input);
            }

        }
    }

    /* Try to open the auth file */
    fp = fopen(AUTH_FILE, "r");
    if (!fp) {
        ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
    }

    if (fsetpos(fp, &fp_pos)) {
        merror("%s: Can not set fileposition.", ARGV0);
        exit(1);
    }

    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);
}
Ejemplo n.º 8
0
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;

    /* Check if we can open the auth_file */
    fp = fopen(AUTH_FILE, "a");
    if (!fp) {
        ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
    }
    fclose(fp);

    /* Allocate for c_ip */
    os_calloc(1, sizeof(os_ip), c_ip);

#ifndef WIN32
    if (chmod(AUTH_FILE, 0440) == -1) {
        ErrorExit(CHMOD_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
    }
#endif

    /* Set time 2 */
    time2 = time(0);

    rand1 = random();

    /* Zero strings */
    memset(str1, '\0', STR_SIZE + 1);
    memset(str2, '\0', STR_SIZE + 1);

    printf(ADD_NEW);

    /* Get the name */
    memset(name, '\0', FILE_SIZE + 1);

    do {
        printf(ADD_NAME);
        fflush(stdout);
        /* Read the agent's name from user environment. If it is invalid
         * we should force user to provide a name from input device.
         */
        _name = getenv("OSSEC_AGENT_NAME");
        if (_name == NULL || NameExist(_name) || !OS_IsValidName(_name)) {
            _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));

    /* Get IP */
    memset(ip, '\0', FILE_SIZE + 1);

    do {
        printf(ADD_IP);
        fflush(stdout);

        /* Read IP address from user's environment. If that IP is invalid,
         * force user to provide IP from input device */
        _ip = getenv("OSSEC_AGENT_IP");
        if (_ip == NULL || !OS_IsValidIP(_ip, c_ip)) {
            _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 = MAX_AGENTS + 32512;
        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);

        /* Get ID */
        printf(ADD_ID, id);
        fflush(stdout);

        /* Get Agent ID from environment. If 0, use default ID. If null,
         * get from user input. If value from environment is invalid,
         * we force user to specify an ID from the terminal. Otherwise,
         * our program goes to infinite loop.
         */
        _id = getenv("OSSEC_AGENT_ID");
        if (_id == NULL || IDExist(_id) || !OS_IsValidID(_id)) {
            _id = read_from_user();
        }

        /* If user specified 0 as Agent ID, he meant use default value.
         * NOTE: a bad condition can cause infinite loop.
         */
        if (strcmp(_id, "0") == 0) {
            strncpy(_id, id, FILE_SIZE - 1);
        }

        /* 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);
        /* Confirmation by an environment variable. The valid value is y/Y.
         * If the user provides anything other string, it is considered as
         * n/N; please note that the old code only accepts y/Y/n/N. So if
         * the variable OSSEC_ACTION_CONFIRMED is 'foobar', the program will
         * go into an infinite loop.
         */
        user_input = getenv("OSSEC_ACTION_CONFIRMED");
        if (user_input == NULL) {
            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, errno, strerror(errno));
            }
#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", (int)(time3 - time2), name, (int)rand1);
            snprintf(str2, STR_SIZE, "%d%s%s%d", (int)(time2 - time1), ip, id, (int)rand2);

            OS_MD5_Str(str1, md1);
            OS_MD5_Str(str2, md2);

            snprintf(str1, STR_SIZE, "%s%d%d%d", md1, (int)getpid(), (int)random(),
                     (int)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);
}
Ejemplo n.º 9
0
int remove_agent()
{
    FILE *fp;
    char *user_input;
    char u_id[FILE_SIZE + 1];
    int id_exist;

    u_id[FILE_SIZE] = '\0';

    if (!print_agents(0, 0, 0)) {
        printf(NO_AGENT);
        return (0);
    }

    do {
        printf(REMOVE_ID);
        fflush(stdout);

        user_input = getenv("OSSEC_AGENT_ID");
        if (user_input == NULL) {
            user_input = read_from_user();
        } else {
            printf("%s\n", user_input);
        }

        if (strcmp(user_input, QUIT) == 0) {
            return (0);
        }

        strncpy(u_id, user_input, FILE_SIZE);

        id_exist = IDExist(user_input);

        if (!id_exist) {
            printf(NO_ID, user_input);

            /* Exit here if we are using environment variables
             * and our ID does not exist
             */
            if (getenv("OSSEC_AGENT_ID")) {
                return (1);
            }
        }
    } while (!id_exist);

    do {
        printf(REMOVE_CONFIRM);
        fflush(stdout);

        user_input = getenv("OSSEC_ACTION_CONFIRMED");
        if (user_input == NULL) {
            user_input = read_from_user();
        } else {
            printf("%s\n", user_input);
        }

        /* If user confirms */
        if (user_input[0] == 'y' || user_input[0] == 'Y') {
            /* Get full agent name */
            char *full_name = getFullnameById(u_id);
            if (!full_name) {
                printf(NO_ID, u_id);
                return (1);
            }

            fp = fopen(AUTH_FILE, "r+");
            if (!fp) {
                free(full_name);
                ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
            }
#ifndef WIN32
            chmod(AUTH_FILE, 0440);
#endif

            /* Remove the agent, but keep 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);
}
Ejemplo n.º 10
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 = getenv("OSPATROL_AGENT_ID");
      if (user_input == NULL || !IDExist(user_input)) {
        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 = getenv("OSPATROL_ACTION_CONFIRMED");
        if (user_input == NULL) {
          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);
}
Ejemplo n.º 11
0
/** main **/
int main(int argc, char **argv)
{
    char *user_msg;

    int c = 0, cmdlist = 0;
    char *cmdexport = NULL;
    char *cmdimport = NULL;
    char *cmdbulk = NULL;

    #ifndef WIN32
    char *dir = DEFAULTDIR;
    char *group = GROUPGLOBAL;
    int gid;
    #endif


    /* Setting the name */
    OS_SetName(ARGV0);


    while((c = getopt(argc, argv, "Vhle:r:i:f:")) != -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 'r':
                #ifdef CLIENT
                ErrorExit("%s: You can't remove keys on an agent", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -r needs an argument",ARGV0);
	            
                /* Use environment variables already available to remove_agent() */           	
                setenv("OSSEC_ACTION", "r", 1);
                setenv("OSSEC_AGENT_ID", optarg, 1);
                setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
                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 'f':
                #ifdef CLIENT
                ErrorExit("%s: You can't bulk generate keys on an agent.", ARGV0);
                #endif
                if(!optarg)
                    ErrorExit("%s: -f needs an argument",ARGV0);
                cmdbulk = optarg;
                printf("Bulk load file: %s\n", cmdbulk);
                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);
    }
    else if(cmdbulk)
    {
        k_bulkload(cmdbulk);
        exit(0);
    }



    /* Little shell */
    while(1)
    {
        int leave_s = 0;
        print_banner();

        /* Get ACTION from the environment. If ACTION is specified,
         * we must set leave_s = 1 to ensure that the loop will end */
        user_msg = getenv("OSSEC_ACTION");
        if (user_msg == NULL) {
          user_msg = read_from_user();
        }
        else{
          leave_s = 1;
        }

        /* 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);
}