HelperApp.cpp 4.91 KB
Newer Older
David Edmundson's avatar
David Edmundson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Main authentication application class
 * Copyright (C) 2013 Martin B????za <mbriza@redhat.com>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include "HelperApp.h"
#include "UserSession.h"

David Edmundson's avatar
David Edmundson committed
24 25
#include "backend/PamHandle.h"

David Edmundson's avatar
David Edmundson committed
26 27 28
#include <QtCore/QTimer>
#include <QtCore/QFile>
#include <QtCore/QDebug>
29
#include <QtCore/QSettings>
David Edmundson's avatar
David Edmundson committed
30 31 32 33 34

#include <iostream>
#include <unistd.h>
#include <sys/socket.h>

35
#include <pwd.h>
36 37
#include <fcntl.h>
#include <sys/ioctl.h>
38

39
#include "VirtualTerminal.h"
40

David Edmundson's avatar
David Edmundson committed
41 42 43 44
namespace SDDM {
    HelperApp::HelperApp(int& argc, char** argv)
            : QCoreApplication(argc, argv)
            , m_session(new UserSession(this))
45
    {
David Edmundson's avatar
David Edmundson committed
46 47
        QStringList args = QCoreApplication::arguments();
        int pos;
48

49 50
        int vt = SDDM::VirtualTerminal::setUpNewVt();
        SDDM::VirtualTerminal::jumpToVt(vt);
David Edmundson's avatar
David Edmundson committed
51 52
        
        if ((pos = args.indexOf("--exec")) >= 0) {
David Edmundson's avatar
David Edmundson committed
53
            if (pos >= args.length() - 1) {
David Edmundson's avatar
David Edmundson committed
54
                exit(-1);
David Edmundson's avatar
David Edmundson committed
55 56
            }
            m_session->setPath(args[pos + 1]);
57 58 59 60 61 62 63
        } else if ((pos = args.indexOf("--session")) >= 0) {
            if (pos >= args.length() - 1) {
                exit(-1);
            }

            QSettings sessionFile(args[pos + 1], QSettings::IniFormat);
            m_session->setPath(sessionFile.value(QLatin1String("Desktop Entry/Exec")).toString());
David Edmundson's avatar
David Edmundson committed
64 65 66 67
        }

        if ((pos = args.indexOf("--user")) >= 0) {
            if (pos >= args.length() - 1) {
David Edmundson's avatar
David Edmundson committed
68
                exit(-1);
David Edmundson's avatar
David Edmundson committed
69 70 71
            }
            m_user = args[pos + 1];
        }
David Edmundson's avatar
David Edmundson committed
72 73 74 75 76
        
        if (m_session->path().isEmpty() || m_user.isEmpty()) 
            qFatal("pass some args please");
        
        PamHandle *pamHandle = new PamHandle; //TODO fix leak
David Edmundson's avatar
David Edmundson committed
77

David Edmundson's avatar
David Edmundson committed
78 79
        if (! pamHandle->start("sddm-autologin" /*PAM session*/, m_user)) //Martin check this exists
            qFatal("Could not start PAM");
David Edmundson's avatar
David Edmundson committed
80 81 82 83 84 85 86

        QProcessEnvironment env = m_session->processEnvironment();

//         env.insert("DISPLAY", name());
        env.insert("XDG_SEAT", "seat0");
//         env.insert("XDG_SEAT_PATH", daemonApp->displayManager()->seatPath(seat()->name()));
//         env.insert("XDG_SESSION_PATH", daemonApp->displayManager()->sessionPath(QString("Session%1").arg(daemonApp->newSessionId())));
87
        env.insert("XDG_VTNR", QString::number(vt));
David Edmundson's avatar
David Edmundson committed
88 89 90
        env.insert("DESKTOP_SESSION", "KDE");
        env.insert("XDG_CURRENT_DESKTOP", "KDE");
        env.insert("XDG_SESSION_CLASS", "user");
David Edmundson's avatar
David Edmundson committed
91
        env.insert("XDG_SESSION_TYPE", "wayland");
David Edmundson's avatar
David Edmundson committed
92
//         env.insert("XDG_SESSION_DESKTOP", xdgSessionName);
93 94 95 96 97 98 99 100 101 102 103

        struct passwd *pw;
        pw = getpwnam(qPrintable(m_user));
        if (pw) {
            env.insert("HOME", pw->pw_dir);
            env.insert("PWD", pw->pw_dir);
            env.insert("SHELL", pw->pw_shell);
            env.insert("USER", pw->pw_name);
            env.insert("LOGNAME", pw->pw_name);
        }

David Edmundson's avatar
David Edmundson committed
104 105
        pamHandle->putEnv(env);

106
        pamHandle->setItem(PAM_TTY, qPrintable(QString("/dev/tty%1").arg(vt)));
David Edmundson's avatar
David Edmundson committed
107

David Edmundson's avatar
David Edmundson committed
108 109 110
        if (!pamHandle->authenticate())
            qFatal("Could not auth");

David Edmundson's avatar
David Edmundson committed
111 112 113 114 115
        if (!pamHandle->acctMgmt())
            qFatal("Could not do account management");

        if (!pamHandle->setCred(PAM_ESTABLISH_CRED))
            qFatal("Could not establish credentials");
David Edmundson's avatar
David Edmundson committed
116 117 118 119 120 121 122
        
        if (!pamHandle->openSession())
            qFatal("Could not open pam session");
        
        qDebug() << "startng process";
        
        connect(m_session, SIGNAL(finished(int)), SLOT(sessionFinished(int)));
David Edmundson's avatar
David Edmundson committed
123 124 125 126

        connect(m_session, &QProcess::readyReadStandardError, m_session, [this](){qDebug() << m_session->readAllStandardError();});
        connect(m_session, &QProcess::readyReadStandardOutput, m_session, [this](){qDebug() << m_session->readAllStandardOutput();});

David Edmundson's avatar
David Edmundson committed
127 128
        env.insert(pamHandle->getEnv());
        m_session->setProcessEnvironment(env);
David Edmundson's avatar
David Edmundson committed
129 130
        m_session->start();
        
David Edmundson's avatar
David Edmundson committed
131 132 133
    }

    void HelperApp::sessionFinished(int status) {
134 135 136
        //leave open so that simplelogin stays open and it's easier to debug
        //long term; probably restart kwin at least once
//         exit(status);
David Edmundson's avatar
David Edmundson committed
137
    }
David Edmundson's avatar
David Edmundson committed
138 139 140
    
    const QString& HelperApp::user() const
    {
David Edmundson's avatar
David Edmundson committed
141 142 143
        return m_user;
    }

David Edmundson's avatar
David Edmundson committed
144

David Edmundson's avatar
David Edmundson committed
145 146 147 148 149 150 151 152 153
    HelperApp::~HelperApp() {

    }
}

int main(int argc, char** argv) {
    SDDM::HelperApp app(argc, argv);
    return app.exec();
}