2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
7 * Augustin Cavalier <waddlesplash>
11 #include <SupportDefs.h>
23 #include "multiuser_utils.h"
26 extern const char* __progname
;
27 const char* kProgramName
= __progname
;
29 const uint32 kRetries
= 3;
35 fprintf(stderr
, "usage: %s [-pl] [-c command] [username]\n", kProgramName
);
41 main(int argc
, char *argv
[])
43 bool loginShell
= false;
44 const char* command
= NULL
;
47 while ((c
= getopt(argc
, argv
, "c:l")) != -1) {
66 const char* user
= NULL
;
72 // aka 'root' on Haiku
76 openlog(kProgramName
, 0, LOG_AUTH
);
78 status_t status
= B_ERROR
;
79 struct passwd
* passwd
= NULL
;
81 status
= authenticate_user("password: ", user
, &passwd
, NULL
,
84 if (status
< B_OK
|| !passwd
) {
86 syslog(LOG_NOTICE
, "su failed for \"%s\"", passwd
->pw_name
);
88 syslog(LOG_NOTICE
, "su attempt for non-existent user \"%s\"", user
);
92 // setup environment for the user
94 status
= setup_environment(passwd
, true, false);
97 fprintf(stderr
, "%s: Refused login. Setting up environment failed: %s\n",
98 kProgramName
, strerror(status
));
99 syslog(LOG_NOTICE
, "su refused for \"%s\"", passwd
->pw_name
);
103 syslog(LOG_INFO
, "su as \"%s\"", passwd
->pw_name
);
106 const char* args
[] = {getenv("SHELL"), NULL
, NULL
, NULL
, NULL
};
109 args
[nextarg
++] = "-login";
111 if (command
!= NULL
) {
112 args
[nextarg
++] = "-c";
113 args
[nextarg
++] = command
;
116 execv(args
[0], (char **)args
);
120 execv(args
[0], (char **)args
);
122 fprintf(stderr
, "%s: starting the shell failed: %s", kProgramName
,