summaryrefslogtreecommitdiff
path: root/core/slogin/slogin.c
blob: 24a534b0fc155fcded6050e0cdc9ac52cfb49de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* See LICENSE file for license details. */
#define _XOPEN_SOURCE 700
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <X11/keysym.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <shadow.h>
#include <crypt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "../accent.h"
#include "config.h"

static ColorShm *accentshm;
static char accentcol[8] = "#005577";

static void
die(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(1);
}

static void
updateaccent(void)
{
    if (!readaccent(&accentshm, accentcol))
        memcpy(accentcol, "#005577", 8);
}

static const char *
gethash(const char *user)
{
    struct passwd *pw;
    const char *hash;
    if (!(pw = getpwnam(user)))
        return NULL;
    hash = pw->pw_passwd;
#if HAVE_SHADOW_H
    if (!strcmp(hash, "x")) {
        struct spwd *sp;
        if (!(sp = getspnam(user)))
            return NULL;
        hash = sp->sp_pwdp;
    }
#endif
    return hash;
}

static int
auth(const char *user, const char *pass)
{
    const char *hash = gethash(user);
    char *cryptpw;
    if (!hash)
        return 0;
    if (!(cryptpw = crypt(pass, hash)))
        return 0;
    return !strcmp(cryptpw, hash);
}

static void
runsession(const char *user, const char *pass)
{
    struct passwd *pw;
    if (!auth(user, pass))
        return;
    if (!(pw = getpwnam(user)))
        return;
    if (initgroups(user, pw->pw_gid) < 0)
        die("slogin: initgroups failed\n");
    if (setgid(pw->pw_gid) < 0)
        die("slogin: setgid failed\n");
    if (setuid(pw->pw_uid) < 0)
        die("slogin: setuid failed\n");
    setenv("HOME", pw->pw_dir, 1);
    chdir(pw->pw_dir);
    setsid();
    execvp(logincmd[0], (char *const *)logincmd);
    die("slogin: exec failed\n");
}

static void
drawscreen(Display *dpy, Window win, XftDraw *draw, XftFont *font,
           XftColor *fg, XftColor *accent,
           const char *user, const char *pass, int stage)
{
    char buf[256];
    int width = DisplayWidth(dpy, DefaultScreen(dpy));
    int height = DisplayHeight(dpy, DefaultScreen(dpy));
    int w = 400, h = 100;
    int x = (width - w)/2;
    int y = (height - h)/2;
    GC gc = DefaultGC(dpy, DefaultScreen(dpy));

    XClearWindow(dpy, win);
    XSetForeground(dpy, gc, accent->pixel);
    XDrawRectangle(dpy, win, gc, x, y, w, h);

    if (stage == 0)
        snprintf(buf, sizeof buf, "login: %s", user);
    else {
        char stars[sizeof buf];
        size_t i, len = strlen(pass);
        for (i = 0; i < len && i < sizeof stars - 1; i++)
            stars[i] = '*';
        stars[i] = '\0';
        snprintf(buf, sizeof buf, "password: %s", stars);
    }
    XftDrawStringUtf8(draw, fg, font, x + 20, y + h/2 + font->ascent/2,
                      (XftChar8 *)buf, strlen(buf));
    XFlush(dpy);
}

int
main(void)
{
    Display *dpy;
    int scr;
    Window root, win;
    XEvent ev;
    XftDraw *xdraw;
    XftFont *font;
    XftColor xfg, xbg, xaccent;
    char user[64] = "", pass[64] = "";
    int stage = 0;
    KeySym ksym;
    char buf[32];
    int len;

    updateaccent();
    if (!(dpy = XOpenDisplay(NULL)))
        die("slogin: cannot open display\n");
    scr = DefaultScreen(dpy);
    root = RootWindow(dpy, scr);
    win = XCreateSimpleWindow(dpy, root, 0, 0,
        DisplayWidth(dpy, scr), DisplayHeight(dpy, scr),
        0, BlackPixel(dpy, scr), BlackPixel(dpy, scr));
    XSelectInput(dpy, win, KeyPressMask | ExposureMask);
    XMapRaised(dpy, win);

    font = XftFontOpenName(dpy, scr, fontname);
    if (!font)
        die("slogin: cannot load font\n");
    xdraw = XftDrawCreate(dpy, win, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr));
    XftColorAllocName(dpy, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr), fgcolor, &xfg);
    XftColorAllocName(dpy, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr), bgcolor, &xbg);
    XftColorAllocName(dpy, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr), accentcol, &xaccent);
    XSetWindowBackground(dpy, win, xbg.pixel);
    XClearWindow(dpy, win);

    for (;;) {
        XNextEvent(dpy, &ev);
        if (ev.type == Expose) {
            drawscreen(dpy, win, xdraw, font, &xfg, &xaccent,
                       user, pass, stage);
        } else if (ev.type == KeyPress) {
            len = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, NULL);
            if (ksym == XK_Return) {
                if (stage == 0)
                    stage = 1;
                else {
                    runsession(user, pass);
                    stage = 0;
                    user[0] = pass[0] = '\0';
                }
            } else if (ksym == XK_BackSpace) {
                if (stage == 0 && strlen(user) > 0)
                    user[strlen(user)-1] = '\0';
                else if (stage == 1 && strlen(pass) > 0)
                    pass[strlen(pass)-1] = '\0';
            } else if (len && buf[0] >= ' ' && buf[0] <= '~') {
                if (stage == 0 && strlen(user) + len < sizeof user) {
                    strncat(user, buf, len);
                    user[strlen(user)] = '\0';
                } else if (stage == 1 && strlen(pass) + len < sizeof pass) {
                    strncat(pass, buf, len);
                    pass[strlen(pass)] = '\0';
                }
            }
            drawscreen(dpy, win, xdraw, font, &xfg, &xaccent,
                       user, pass, stage);
        }
    }
}