2 * Copyright 2007, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
10 #include <SupportDefs.h>
21 #include "multiuser_utils.h"
24 extern const char* __progname
;
25 const char* kProgramName
= __progname
;
27 const uint32 kRetries
= 3;
28 const uint32 kTimeout
= 60;
32 read_string(char* string
, size_t bufferSize
)
34 // TODO: setup timeout handler
36 // read everything until the next carriage return
38 while ((c
= fgetc(stdin
)) != EOF
&& c
!= '\r' && c
!= '\n') {
46 if (ferror(stdin
) != 0)
55 login(const char* user
, struct passwd
** _passwd
)
61 if (gethostname(host
, sizeof(host
)) != 0)
69 status_t status
= read_string(userBuffer
, sizeof(userBuffer
));
77 // if no user is given, we exit immediately
82 status_t status
= read_password("password: ", password
, sizeof(password
),
90 struct passwd
* passwd
= getpwnam(user
);
91 struct spwd
* spwd
= getspnam(user
);
93 bool ok
= verify_password(passwd
, spwd
, password
);
94 memset(password
, 0, sizeof(password
));
97 return B_PERMISSION_DENIED
;
105 get_from(const char* host
)
110 static char buffer
[64];
111 snprintf(buffer
, sizeof(buffer
), " from %s", host
);
119 fprintf(stderr
, "usage: %s [-fp] [-h hostname] [username]\n", kProgramName
);
125 main(int argc
, char *argv
[])
127 bool noAuthentification
= false;
128 bool preserveEnvironment
= false;
129 const char* fromHost
= NULL
;
132 while ((c
= getopt(argc
, argv
, "fh:p")) != -1) {
135 noAuthentification
= true;
138 if (geteuid() != 0) {
139 fprintf(stderr
, "%s: %s\n", kProgramName
,
140 strerror(B_NOT_ALLOWED
));
147 preserveEnvironment
= true;
159 const char* user
= NULL
;
166 openlog(kProgramName
, 0, LOG_AUTH
);
168 uint32 retries
= kRetries
;
169 status_t status
= B_ERROR
;
170 struct passwd
* passwd
= NULL
;
172 while (retries
> 0) {
173 status
= login(user
, &passwd
);
178 fprintf(stderr
, "Login failed.\n");
182 // ask for the user name as well after the first failure
187 if (status
< B_OK
|| passwd
== NULL
) {
189 syslog(LOG_NOTICE
, "login%s failed for \"%s\"", get_from(fromHost
),
194 // setup environment for the user
196 status
= setup_environment(passwd
, preserveEnvironment
);
199 fprintf(stderr
, "%s: Refused login. Setting up environment failed: %s\n",
200 kProgramName
, strerror(status
));
201 syslog(LOG_NOTICE
, "login%s refused for \"%s\"", get_from(fromHost
),
206 syslog(LOG_INFO
, "login%s as \"%s\"", get_from(fromHost
), passwd
->pw_name
);
210 const char* args
[] = {getenv("SHELL"), "-login", NULL
};
211 execv(args
[0], (char **)args
);
215 execv(args
[0], (char **)args
);
217 fprintf(stderr
, "%s: starting the shell failed: %s", kProgramName
,