<commit_before>/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of signon
 *
 * Copyright (C) 2009-2010 Nokia Corporation.
 *
 * Contact: <NAME> <<EMAIL>>
 * Contact: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <sys/mount.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <libcryptsetup.h>

#include <QDataStream>
#include <QTextStream>
#include <QProcess>
#include <QLatin1Char>
#include <QFileInfo>
#include <QDir>

#include "cryptohandlers.h"
#include "signond-common.h"
#include "misc.h"


#define SIGNON_LUKS_DEFAULT_HASH  "ripemd160"

#define SIGNON_LUKS_CIPHER        "aes-xts-plain"
#define SIGNON_LUKS_KEY_SIZE	  256
#define SIGNON_LUKS_BASE_KEYSLOT  0

#define SIGNON_EXTERNAL_PROCESS_READ_TIMEOUT 300

namespace SignonDaemonNS {

    /*  ------------------- SystemCommandLineCallHandler implementation ------------------- */

    SystemCommandLineCallHandler::SystemCommandLineCallHandler()
    {
        connect(&m_process, SIGNAL(error(QProcess::ProcessError)),
                this, SLOT(error(QProcess::ProcessError)));
    }

    SystemCommandLineCallHandler::~SystemCommandLineCallHandler()
    {
    }

    bool SystemCommandLineCallHandler::makeCall(const QString &appPath,
                                                const QStringList &args,
                                                bool readOutput)
    {
        QString trace;
        QTextStream stream(&trace);
        stream << appPath << QLatin1Char(' ') << args.join(QLatin1String(" "));
        TRACE() << trace;

        m_process.start(appPath, args);
        if (!m_process.waitForStarted()) {
            BLAME() << "Wait for started failed";
            return false;
        }

        if (readOutput) {
            m_output.clear();

            if (m_process.waitForReadyRead(SIGNON_EXTERNAL_PROCESS_READ_TIMEOUT)) {
                if (!m_process.bytesAvailable()) {
                    BLAME() << "Coult not read output of external process ";
                    return false;
                }

                while(m_process.bytesAvailable())
                    m_output += m_process.readAllStandardOutput();
            }
        }

        if (!m_process.waitForFinished()) {
            TRACE() << "Wait for finished failed";
            return false;
        }

        return true;
    }

    void SystemCommandLineCallHandler::error(QProcess::ProcessError err)
    {
        TRACE() << "Process erorr:" << err;
    }


    /*  ------------------------ PartitionHandler implementation ------------------------ */

    bool PartitionHandler::createPartitionFile(const QString &fileName, const quint32 fileSize)
    {
        SystemCommandLineCallHandler handler;
        bool ret = handler.makeCall(
            QLatin1String("/bin/dd"),
            QStringList() << QLatin1String("if=/dev/urandom")
                          << QString::fromLatin1("of=%1").arg(fileName)
                          << QLatin1String("bs=1M")
                          << QString::fromLatin1("count=%1").arg(fileSize));

        if (!setFilePermissions(fileName, signonFilePermissions))
            TRACE() << "Failed to set file permissions "
                       "for the secure storage container.";

        if (!setUserOwnership(fileName))
            TRACE() << "Failed to set User ownership "
                       "for the secure storage container.";
        return ret;
    }
<commit_msg>Using ftruncate to create the signon secure FS partition file.<commit_after>/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of signon
 *
 * Copyright (C) 2009-2010 Nokia Corporation.
 *
 * Contact: <NAME> <<EMAIL>>
 * Contact: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <sys/mount.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <libcryptsetup.h>

#include <QDataStream>
#include <QTextStream>
#include <QProcess>
#include <QLatin1Char>
#include <QFileInfo>
#include <QDir>

#include "cryptohandlers.h"
#include "signond-common.h"
#include "misc.h"


#define SIGNON_LUKS_DEFAULT_HASH  "ripemd160"

#define SIGNON_LUKS_CIPHER        "aes-xts-plain"
#define SIGNON_LUKS_KEY_SIZE	  256
#define SIGNON_LUKS_BASE_KEYSLOT  0

#define SIGNON_EXTERNAL_PROCESS_READ_TIMEOUT 300

#define KILO_BYTE_SIZE 1024
#define MEGA_BYTE_SIZE (KILO_BYTE_SIZE * 1024)

namespace SignonDaemonNS {

    /*  ------------------- SystemCommandLineCallHandler implementation ------------------- */

    SystemCommandLineCallHandler::SystemCommandLineCallHandler()
    {
        connect(&m_process, SIGNAL(error(QProcess::ProcessError)),
                this, SLOT(error(QProcess::ProcessError)));
    }

    SystemCommandLineCallHandler::~SystemCommandLineCallHandler()
    {
    }

    bool SystemCommandLineCallHandler::makeCall(const QString &appPath,
                                                const QStringList &args,
                                                bool readOutput)
    {
        QString trace;
        QTextStream stream(&trace);
        stream << appPath << QLatin1Char(' ') << args.join(QLatin1String(" "));
        TRACE() << trace;

        m_process.start(appPath, args);
        if (!m_process.waitForStarted()) {
            BLAME() << "Wait for started failed";
            return false;
        }

        if (readOutput) {
            m_output.clear();

            if (m_process.waitForReadyRead(SIGNON_EXTERNAL_PROCESS_READ_TIMEOUT)) {
                if (!m_process.bytesAvailable()) {
                    BLAME() << "Coult not read output of external process ";
                    return false;
                }

                while(m_process.bytesAvailable())
                    m_output += m_process.readAllStandardOutput();
            }
        }

        if (!m_process.waitForFinished()) {
            TRACE() << "Wait for finished failed";
            return false;
        }

        return true;
    }

    void SystemCommandLineCallHandler::error(QProcess::ProcessError err)
    {
        TRACE() << "Process erorr:" << err;
    }


    /*  ------------------------ PartitionHandler implementation ------------------------ */

    bool PartitionHandler::createPartitionFile(const QString &fileName, const quint32 fileSize)
    {
        int fd = open(fileName.toLatin1().data(),
                      O_RDWR | O_CREAT,
                      666);

        if (fd < 0) {
            BLAME() << "FAILED to create signon secure FS partition file. ERRNO:"
                    << errno;
            return false;
        }

        if (ftruncate(fd, fileSize * MEGA_BYTE_SIZE) == -1) {
            BLAME() << "FAILED to set signon secure FS partition file size. ERRNO:"
                    << errno;
            return false;
        }

        if (close(fd) < 0)
            TRACE() << "Failed to close secure FS partition file after creation.";

        if (!setFilePermissions(fileName, signonFilePermissions))
            TRACE() << "Failed to set file permissions "
                       "for the secure storage container.";

        if (!setUserOwnership(fileName))
            TRACE() << "Failed to set User ownership "
                       "for the secure storage container.";

        return true;
    }

{'fraction_non_alphanumeric': 0.11975279850746269, 'fraction_numerical': 0.011310634328358209, 'mean_word_length': 2.188475836431227, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6694881', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2298, 'n_words': 657}