2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, <axeld@pinc-software.de>
7 * Ingo Weinhold <ingo_weinhold@gmx.de>
15 #include <LayoutBuilder.h>
16 #include <NetworkSettings.h>
17 #include <NetworkSettingsAddOn.h>
18 #include <StringView.h>
21 #include <NetServer.h>
22 #include <RegistrarDefs.h>
23 #include <user_group.h>
24 #include <util/KMessage.h>
26 #include "ServiceListItem.h"
27 #include "ServiceView.h"
30 using namespace BNetworkKit
;
33 #undef B_TRANSLATION_CONTEXT
34 #define B_TRANSLATION_CONTEXT "SSHServiceAddOn"
37 static const uint32 kMsgToggleService
= 'tgls';
40 class SSHServiceAddOn
: public BNetworkSettingsAddOn
{
42 SSHServiceAddOn(image_id image
,
43 BNetworkSettings
& settings
);
44 virtual ~SSHServiceAddOn();
46 virtual BNetworkSettingsItem
*
47 CreateNextItem(uint32
& cookie
);
51 class SSHServiceView
: public ServiceView
{
53 SSHServiceView(BNetworkSettings
& settings
);
54 virtual ~SSHServiceView();
57 virtual void Enable();
61 class SSHServiceItem
: public BNetworkSettingsItem
{
63 SSHServiceItem(BNetworkSettings
& settings
);
64 virtual ~SSHServiceItem();
66 virtual BNetworkSettingsType
69 virtual BListItem
* ListItem();
70 virtual BView
* View();
72 virtual status_t
Revert();
73 virtual bool IsRevertable();
75 virtual void SettingsUpdated(uint32 which
);
78 BNetworkSettings
& fSettings
;
87 SSHServiceView::SSHServiceView(BNetworkSettings
& settings
)
89 ServiceView("ssh", NULL
, B_TRANSLATE("SSH server"), B_TRANSLATE(
90 "The SSH server allows you to "
91 "remotely access your machine with a terminal session, as well as "
92 "file access using the SCP and SFTP protocols."), settings
)
97 SSHServiceView::~SSHServiceView()
103 SSHServiceView::Enable()
105 if (getpwnam("sshd") == NULL
) {
106 // We need to create a dedicated user for the service
107 // The following code is copied from useradd, and should be moved
108 // to a public class.
111 // Find an unused user ID
113 while (getpwuid(userID
) != NULL
)
116 const char* home
= "/boot/home";
117 int expiration
= 99999;
119 const char* shell
= "/bin/sh";
120 const char* realName
= "";
126 // prepare request for the registrar
127 KMessage
message(BPrivate::B_REG_UPDATE_USER
);
128 if (message
.AddInt32("uid", userID
) == B_OK
129 && message
.AddInt32("gid", groupID
) == B_OK
130 && message
.AddString("name", "sshd") == B_OK
131 && message
.AddString("password", "x") == B_OK
132 && message
.AddString("home", home
) == B_OK
133 && message
.AddString("shell", shell
) == B_OK
134 && message
.AddString("real name", realName
) == B_OK
135 && message
.AddString("shadow password", "!") == B_OK
136 && message
.AddInt32("last changed", time(NULL
)) == B_OK
137 && message
.AddInt32("min", min
) == B_OK
138 && message
.AddInt32("max", max
) == B_OK
139 && message
.AddInt32("warn", warn
) == B_OK
140 && message
.AddInt32("inactive", inactive
) == B_OK
141 && message
.AddInt32("expiration", expiration
) == B_OK
142 && message
.AddInt32("flags", 0) == B_OK
143 && message
.AddBool("add user", true) == B_OK
) {
146 status_t error
= send_authentication_request_to_registrar(message
,
149 fprintf(stderr
, "Error: Failed to create user: %s\n",
155 BNetworkServiceSettings settings
;
156 settings
.SetName("ssh");
157 settings
.SetStandAlone(true);
159 settings
.AddArgument("/bin/sshd");
160 settings
.AddArgument("-D");
163 if (settings
.GetMessage(service
) == B_OK
)
164 fSettings
.AddService(service
);
171 SSHServiceItem::SSHServiceItem(BNetworkSettings
& settings
)
174 fItem(new ServiceListItem("ssh", B_TRANSLATE("SSH server"), settings
)),
180 SSHServiceItem::~SSHServiceItem()
182 if (fView
->Parent() == NULL
)
190 SSHServiceItem::Type() const
192 return B_NETWORK_SETTINGS_TYPE_SERVICE
;
197 SSHServiceItem::ListItem()
204 SSHServiceItem::View()
207 fView
= new SSHServiceView(fSettings
);
214 SSHServiceItem::Revert()
216 return fView
!= NULL
? fView
->Revert() : B_OK
;
221 SSHServiceItem::IsRevertable()
223 return fView
!= NULL
? fView
->IsRevertable() : false;
228 SSHServiceItem::SettingsUpdated(uint32 which
)
231 fView
->SettingsUpdated(which
);
238 SSHServiceAddOn::SSHServiceAddOn(image_id image
,
239 BNetworkSettings
& settings
)
241 BNetworkSettingsAddOn(image
, settings
)
246 SSHServiceAddOn::~SSHServiceAddOn()
251 BNetworkSettingsItem
*
252 SSHServiceAddOn::CreateNextItem(uint32
& cookie
)
255 return new SSHServiceItem(Settings());
265 BNetworkSettingsAddOn
*
266 instantiate_network_settings_add_on(image_id image
, BNetworkSettings
& settings
)
268 return new SSHServiceAddOn(image
, settings
);