Exemplo n.º 1
0
void FileSet::list_files(const std::string &path, const std::set<std::string> &extensions)
{
	std::stringstream cmdstream;
	cmdstream << "find $(realpath ";
	cmdstream << path;
	cmdstream << ") -type f ";

	if (extensions.size() != 0)
	{
		auto end = extensions.end();
		auto it = extensions.begin();

		cmdstream << " -name *" << *it << " ";

		for (; it != end; ++it)
		{
			cmdstream << " -o -name *" << *it << " ";
		}
	}

	RunResult result = run_system_cmd(cmdstream);

	std::stringstream files(result.output);

	std::string file;
	while (std::getline(files, file))
	{
		File *file_object = create_file(file);
		if (file_object == nullptr)
		{
			return;
		}
		file_object->add_to_fileset(*this);
	}
}
Exemplo n.º 2
0
RunResult run_system_cmd(const std::stringstream &stream, bool need_output)
{
	return run_system_cmd(stream.str(), need_output);
}
Exemplo n.º 3
0
RunResult run_system_cmd(const Cmd& cmd, bool need_output)
{
	return run_system_cmd(cmd.to_string(), need_output);
}
Exemplo n.º 4
0
/*----------------------------------------------------------------------*/
int daf_service_delete(void)
{

    char cmd[1024];
    char msg[1024];
    char timestamp[16];
    char backup_inittab_filename[64];

#if defined AIX

    print_msg_to_console("Updating /etc/inittab entry\n");

    //--------------------------------------------------------------------------
    // remove any previous entry
    //--------------------------------------------------------------------------

    safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename));
    get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.');
    safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename));
    sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename);
    print_msg_to_console(msg);

    if (copy_file("/etc/inittab", backup_inittab_filename) != 0)
    {
        safecpy(msg, "Backup of inittab failed: ", sizeof(msg));
        safecat(msg, cmd, sizeof(msg));
        safecat(msg, "\n", sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

    print_msg_to_console("  Removing any previous daf service entry in /etc/inittab\n");
    safecpy(cmd, "cat /etc/inittab | grep \"daf:\"", sizeof(cmd));

    if (run_system_cmd(cmd, 1) == 0)
    {

        safecpy(cmd, "rmitab \"daf\"", sizeof(cmd));

        if (run_system_cmd_and_print_all_output(cmd) != 0)
        {
            safecpy(msg, "Removal of daf service entry in /etc/inittab failed: ", sizeof(msg));
            safecat(msg, cmd, sizeof(msg));
            safecat(msg, "\n", sizeof(msg));
            print_msg_to_console(msg);
            return(1);
        }

    }
    else
    {
        safecpy(msg, "No daf entry found in /etc/inittab so none was removed\n", sizeof(msg));
        print_msg_to_console(msg);
    }

    print_msg_to_console("/etc/inittab entry updated successfully\n");

#elif defined LINUX || defined SOLARIS || defined HPUX

    //--------------------------------------------------------------------------
    // remove any previous entry
    //--------------------------------------------------------------------------

    // This could be Debian/Ubuntu and could be before we have installed DAF so there might not
    // be a /etc/inittab file existing yet - check for this

    if (! does_file_exist("/etc/inittab"))
    {

        if (does_file_exist(DAF_DEBIAN_ETC_INIT_CONF_PATH))
        {
            safecpy(cmd, "rm " DAF_DEBIAN_ETC_INIT_CONF_PATH, sizeof(cmd));

            if (run_system_cmd(cmd, 1) != 0)
            {
                safecpy(msg, "Removal of " DAF_DEBIAN_ETC_INIT_CONF_PATH " failed\n", sizeof(msg));
                print_msg_to_console(msg);
                return(1);
            }
        }

        print_msg_to_console(DAF_DEBIAN_ETC_INIT_CONF_PATH " removed\n");

    }
    else
    {

        print_msg_to_console("Updating /etc/inittab entry\n");

        safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename));
        get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.');
        safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename));
        sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename);
        print_msg_to_console(msg);

        if (copy_file ("/etc/inittab", backup_inittab_filename) != 0)
        {
            safecpy(msg, "Backup of inittab failed: ", sizeof(msg));
            safecat(msg, cmd, sizeof(msg));
            safecat(msg, "\n", sizeof(msg));
            print_msg_to_console(msg);
            return(1);
        }

        print_msg_to_console("  Removing any previous daf service entry in /etc/inittab\n");
        safecpy(cmd, "cat ", sizeof(cmd));
        safecat(cmd, backup_inittab_filename, sizeof(cmd));
        safecat(cmd, " | grep -v \""DAF_SERVICE_INVOCATION"\" > /etc/inittab", sizeof(cmd));

        if (run_system_cmd(cmd, 0) != 0)
        {
            safecpy(msg, "Removal of previous daf service in inittab failed: ", sizeof(msg));
            safecat(msg, cmd, sizeof(msg));
            safecat(msg, "\n", sizeof(msg));
            print_msg_to_console(msg);
            return(1);
        }

        print_msg_to_console("/etc/inittab entry updated successfully\n");
    }


#endif

    if (is_daf_service_running())
    {
        print_msg_to_console("daf daemon is running - and will now be stopped\n");

        if (stop_daf_service_running() != 0)
        {
            print_msg_to_console("Problem trying to stop daf daemon\n");
        }
    }

    return(0);

}
Exemplo n.º 5
0
/*----------------------------------------------------------------------*/
int daf_service_install(char *daf_binary_path, char *argv0)
{

    char cmd[1024];
    char msg[1024];
    char daf_path[64];
    char daf_binary_to_install[64];
    char timestamp[16];
    char backup_inittab_filename[64];

    struct stat fileStat;
    char *pathname = "/etc/inittab";

    if (daf_binary_path == NULL)
    {
        if (get_current_executable_path(argv0, daf_binary_to_install, sizeof(daf_binary_to_install)) !=0)
        {
            print_msg_to_console("Problem trying to determine path of current executable\n");
            return(1);
        }
    }
    else
    {
        safecpy(daf_binary_to_install, daf_binary_path, sizeof(daf_binary_to_install));
    }

    // ensure that directories that are needed do actually exist and delelete existing profile
    // and start up script

    ensure_directory_path_exists(DAF_SERVICE_BINARY_DIR);
    ensure_directory_path_exists(DAF_SERVICE_LOG_DIR);
    delete_file(DAF_SERVICE_START_SCRIPT_PATH);
    delete_file(DAF_SERVICE_PROFILE_PATH);

    // are we trying to copy the same file onto itself, -if so then
    // do not do the copy as it it will fail
    if (strcmp(daf_binary_to_install, DAF_SERVICE_BINARY_PATH) == 0)
    {

        sprintf(msg, "Requested install image %s is identical to install destination: %s\n",
                daf_binary_to_install, DAF_SERVICE_BINARY_PATH);
        print_msg_to_console(msg);

    }
    else
    {

        if (copy_file(daf_binary_to_install, DAF_SERVICE_BINARY_PATH) != 0)
        {
            /* make sure the SETuid bit is on so that daf can assume root priviledges if it is run by a non root user */
            /* is this a security hole ?? <<<<<<<<<< */
            sprintf(msg, "Could not copy %s into installation directory at %s\n", daf_binary_to_install, DAF_SERVICE_BINARY_PATH);
            print_msg_to_console(msg);
            return(1);
        }
        else
        {
            if (chmod_file(DAF_SERVICE_BINARY_PATH, "u+s") != 0)
            {
                sprintf(msg, "Could not set setuid permission bit on %s\n", DAF_SERVICE_BINARY_PATH);
                print_msg_to_console(msg);
                return(1);
            }
        }

    }

    if (create_default_daf_service_profile() != 0)
    {
        print_msg_to_console("Could not install command profile into standard daf service directory\n");
        return(1);
    }
    else
    {
        print_msg_to_console("daf service command profile installed successfully\n");
    }


    if (create_daf_service_start_script() != 0)
    {
        print_msg_to_console("Could not install start script into standard daf service directory\n");
        return(1);
    }
    else
    {
        print_msg_to_console("daf service start script installed successfully\n");
    }

    //--------------------------------------------------------------------------
    //
    //--------------------------------------------------------------------------

    safecpy(daf_path, DAF_SERVICE_BINARY_PATH, sizeof(daf_path));

    if (is_daf_service_running())
    {
        print_msg_to_console("daf daemon is running - and will now be stopped\n");

        if (stop_daf_service_running() != 0)
        {
            print_msg_to_console("Problem trying to stop daf daemon\n");
        }
    }

    print_msg_to_console("Updating /etc/inittab entry\n");

#if defined AIX

    //--------------------------------------------------------------------------
    // remove any previous entry
    //--------------------------------------------------------------------------

    print_msg_to_console("Removing any previous daf service entry in /etc/inittab\n");
    safecpy(cmd, "cat /etc/inittab | grep \"daf:\"", sizeof(cmd));

    if (run_system_cmd(cmd, 1) == 0)
    {

        safecpy(cmd, "rmitab \"daf\"", sizeof(cmd));

        if (run_system_cmd_and_print_all_output(cmd) != 0)
        {
            safecpy(msg, "Removal of daf service entry in /etc/inittab failed: ", sizeof(msg));
            safecat(msg, cmd, sizeof(msg));
            safecat(msg, "\n", sizeof(msg));
            print_msg_to_console(msg);
            return(1);
        }
    }

    //--------------------------------------------------------------------------
    // Add daf daemon to /etc/inittab
    //--------------------------------------------------------------------------

    safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename));
    get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.');
    safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename));
    sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename);
    print_msg_to_console(msg);

    if (copy_file("/etc/inittab", backup_inittab_filename) != 0)
    {
        safecpy(msg, "Backup of inittab failed: ", sizeof(msg));
        safecat(msg, "Could not copy /etc/inittab to %s", backup_inittabl_filename, sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

    print_msg_to_console("Adding daf service entry to /etc/inittab\n");

    safecpy(cmd, "mkitab \"daf:2:once:", sizeof(cmd));
    safecat(cmd, DAF_SERVICE_INVOCATION, sizeof(cmd));
    safecat(cmd, " >/dev/null 2>&1", sizeof(cmd));
    safecat(cmd, "\"", sizeof(cmd));

    if (run_system_cmd_and_print_all_output(cmd) != 0)
    {
        safecpy(msg, "Update of inittab failed: ", sizeof(msg));
        safecat(msg, cmd, sizeof(msg));
        safecat(msg, "\n", sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

#elif defined LINUX || defined SOLARIS || defined HPUX

    //--------------------------------------------------------------------------
    // Add daf daemon to /etc/inittab
    // First check to see if /etc/inittab exists - if it does not this may be
    // Debian or Ubuntu in which case we must create an empty file
    // (Debian/Ubuntu use the Startup mechanism instead of /etc/inittab but
    // still support the old /etc/inittab file it is present
    // see for instance:  http://upstart.ubuntu.com/cookbook/
    //--------------------------------------------------------------------------

    if (!does_file_exist("/etc/inittab"))
    {
        sprintf(msg, "/etc/inittab did not exist - assuming Debian/Ubuntu - so creating /etc/init entry\n");
        print_msg_to_console(msg);
        create_etc_init_conf();
        return(0);
    }

    if (stat(pathname, &fileStat) < 0)
    {

        sprintf(msg, "stat(%s) failed - it seems %s is missing !!!!!!!!!!!!!!!!!!!!! \n", pathname, pathname );
        print_msg_to_console(msg);
        return(1);
    }

    // Later versions of Redhat etc do not use /etc/inittab (but will still honour it if exists - so if the file is empty,
    // we just put a single comment line into so that later code that updates the /etc/inittab file will work
    if (fileStat.st_size == 0)
    {

        if (run_system_cmd("cat '* just a comment' >>/etc/inittab", 0) != 0)
        {
            safecpy(msg, "cat \"* just a comment\" >>/etc/inittab failed\n", sizeof(msg));
            print_msg_to_console(msg);
            return(1);
        }
    }

    //--------------------------------------------------------------------------
    // remove any previous entry, making sure we backup /etc/inittab
    //--------------------------------------------------------------------------

    safecpy(backup_inittab_filename, "/etc/inittab.", sizeof(backup_inittab_filename));
    get_current_time_as_timestamp(timestamp, sizeof(timestamp), '.');
    safecat(backup_inittab_filename, timestamp, sizeof(backup_inittab_filename));
    sprintf(msg, "Backing up /etc/inittab in %s\n", backup_inittab_filename);
    print_msg_to_console(msg);
    print_msg_to_console("Removing any previous daf service entry in /etc/inittab\n");

    if (copy_file("/etc/inittab", backup_inittab_filename) != 0)
    {
        snprintf(msg, sizeof(msg), "Copy of /etc/inittab to %s failed\n", backup_inittab_filename);
        print_msg_to_console(msg);
        return(1);
    }

    safecpy(cmd, "cat ", sizeof(cmd));
    safecat(cmd, backup_inittab_filename, sizeof(cmd));
    safecat(cmd, " | grep -v \""DAF_SERVICE_INVOCATION"\" > /etc/inittab", sizeof(cmd));

    if (run_system_cmd(cmd, 0) != 0)
    {
        safecpy(msg, "Removal of previous daf service in inittab failed: ", sizeof(msg));
        safecat(msg, cmd, sizeof(msg));
        safecat(msg, "\n", sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

    safecpy(cmd, "cat ", sizeof(cmd));
    safecat(cmd, backup_inittab_filename, sizeof(cmd));
    safecat(cmd, " | grep -v \""DAF_SERVICE_INVOCATION"\" > /etc/inittab", sizeof(cmd));

    if (run_system_cmd(cmd, 0) != 0)
    {
        safecpy(msg, "Removal of previous daf service in inittab failed: ", sizeof(msg));
        safecat(msg, cmd, sizeof(msg));
        safecat(msg, "\n", sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

    //--------------------------------------------------------------------------
    // Add daf daemon to /etc/inittab
    //--------------------------------------------------------------------------

    print_msg_to_console("Adding daf service entry to /etc/inittab\n");

    safecpy(cmd, "echo \"daf:345:once:", sizeof(msg));
    safecat(cmd, DAF_SERVICE_INVOCATION, sizeof(cmd));
    safecat(cmd, " \" 2>/dev/null >>/etc/inittab", sizeof(cmd));

    if (run_system_cmd(cmd, 0) != 0)
    {
        safecpy(msg, "Adding daf service entry to /etc/inittab: ", sizeof(msg));
        safecat(msg, cmd, sizeof(msg));
        safecat(msg, "\n", sizeof(msg));
        print_msg_to_console(msg);
        return(1);
    }

#endif

    print_msg_to_console("/etc/inittab entry updated successfully\n");

    return(0);

}