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
|
/* See LICENSE file for license details. */
#define _XOPEN_SOURCE 700
#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 <termios.h>
#include <sys/wait.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 void
parseaccent(int *r, int *g, int *b)
{
unsigned int ri, gi, bi;
if (sscanf(accentcol + 1, "%02x%02x%02x", &ri, &gi, &bi) == 3) {
*r = ri;
*g = gi;
*b = bi;
} else {
*r = 0x00;
*g = 0x55;
*b = 0x77;
}
}
static void
colored(const char *msg)
{
int r, g, b;
parseaccent(&r, &g, &b);
printf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, msg);
}
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
readinput(char *buf, size_t len)
{
if (!fgets(buf, len, stdin))
buf[0] = '\0';
buf[strcspn(buf, "\n")] = '\0';
}
static void
readpass(char *buf, size_t len)
{
struct termios old, new;
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ECHO);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &new);
readinput(buf, len);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
}
static void
runsession(const char *user)
{
struct passwd *pw;
pid_t pid;
if (!(pw = getpwnam(user)))
return;
if ((pid = fork()) == 0) {
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);
if (chdir(pw->pw_dir) < 0)
die("slogin: chdir failed\n");
execvp(logincmd[0], (char *const *)logincmd);
_exit(1);
}
waitpid(pid, NULL, 0);
}
int
main(void)
{
char user[64];
char pass[64];
for (;;) {
updateaccent();
printf("\033[2J\033[H");
colored("login: ");
fflush(stdout);
readinput(user, sizeof user);
colored("password: ");
fflush(stdout);
readpass(pass, sizeof pass);
printf("\n");
if (auth(user, pass)) {
memset(pass, 0, sizeof pass);
runsession(user);
} else {
colored("Login incorrect\n");
memset(pass, 0, sizeof pass);
sleep(2);
}
}
return 0;
}
|