Network: SSH service add-on now creates sshd user.
* If it does not exist yet, the sshd user is created upon enabling the service. * Also, it now uses kMsgIsServiceRunning to determine the current label/function of the enable/disable button.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
SubDir HAIKU_TOP src add-ons network_settings sshd ;
|
SubDir HAIKU_TOP src add-ons network_settings sshd ;
|
||||||
|
|
||||||
UsePublicHeaders [ FDirName add-ons network_settings ] ;
|
UsePublicHeaders [ FDirName add-ons network_settings ] ;
|
||||||
UsePrivateHeaders shared ;
|
UsePrivateHeaders app libroot kernel net shared ;
|
||||||
|
|
||||||
Addon SSHService :
|
Addon SSHService :
|
||||||
SSHServiceAddOn.cpp
|
SSHServiceAddOn.cpp
|
||||||
|
|||||||
@@ -4,17 +4,26 @@
|
|||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Axel Dörfler, <[email protected]>
|
* Axel Dörfler, <[email protected]>
|
||||||
|
* Ingo Weinhold <[email protected]>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <pwd.h>
|
||||||
|
|
||||||
#include <Button.h>
|
#include <Button.h>
|
||||||
#include <Catalog.h>
|
#include <Catalog.h>
|
||||||
#include <LayoutBuilder.h>
|
#include <LayoutBuilder.h>
|
||||||
|
#include <NetworkSettings.h>
|
||||||
#include <NetworkSettingsAddOn.h>
|
#include <NetworkSettingsAddOn.h>
|
||||||
#include <StringItem.h>
|
#include <StringItem.h>
|
||||||
#include <StringView.h>
|
#include <StringView.h>
|
||||||
#include <TextView.h>
|
#include <TextView.h>
|
||||||
|
|
||||||
|
#include <NetServer.h>
|
||||||
|
#include <RegistrarDefs.h>
|
||||||
|
#include <user_group.h>
|
||||||
|
#include <util/KMessage.h>
|
||||||
|
|
||||||
|
|
||||||
using namespace BNetworkKit;
|
using namespace BNetworkKit;
|
||||||
|
|
||||||
@@ -50,9 +59,11 @@ private:
|
|||||||
bool _IsEnabled() const;
|
bool _IsEnabled() const;
|
||||||
void _Enable();
|
void _Enable();
|
||||||
void _Disable();
|
void _Disable();
|
||||||
|
void _UpdateEnableButton();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BNetworkSettings& fSettings;
|
BNetworkSettings& fSettings;
|
||||||
|
BButton* fEnableButton;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -99,7 +110,7 @@ ServiceView::ServiceView(BNetworkSettings& settings)
|
|||||||
descriptionView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
descriptionView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
||||||
descriptionView->MakeEditable(false);
|
descriptionView->MakeEditable(false);
|
||||||
|
|
||||||
BButton* button = new BButton("toggler", B_TRANSLATE("Enable"),
|
fEnableButton = new BButton("toggler", B_TRANSLATE("Enable"),
|
||||||
new BMessage(kMsgToggleService));
|
new BMessage(kMsgToggleService));
|
||||||
|
|
||||||
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
||||||
@@ -107,9 +118,10 @@ ServiceView::ServiceView(BNetworkSettings& settings)
|
|||||||
.Add(descriptionView)
|
.Add(descriptionView)
|
||||||
.AddGroup(B_HORIZONTAL)
|
.AddGroup(B_HORIZONTAL)
|
||||||
.AddGlue()
|
.AddGlue()
|
||||||
.Add(button);
|
.Add(fEnableButton);
|
||||||
|
|
||||||
SetExplicitMinSize(BSize(200, B_SIZE_UNSET));
|
SetExplicitMinSize(BSize(200, B_SIZE_UNSET));
|
||||||
|
_UpdateEnableButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -121,6 +133,7 @@ ServiceView::~ServiceView()
|
|||||||
void
|
void
|
||||||
ServiceView::ConfigurationUpdated(const BMessage& message)
|
ServiceView::ConfigurationUpdated(const BMessage& message)
|
||||||
{
|
{
|
||||||
|
_UpdateEnableButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -133,6 +146,8 @@ ServiceView::MessageReceived(BMessage* message)
|
|||||||
_Disable();
|
_Disable();
|
||||||
else
|
else
|
||||||
_Enable();
|
_Enable();
|
||||||
|
|
||||||
|
_UpdateEnableButton();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
BView::MessageReceived(message);
|
BView::MessageReceived(message);
|
||||||
@@ -144,14 +159,72 @@ ServiceView::MessageReceived(BMessage* message)
|
|||||||
bool
|
bool
|
||||||
ServiceView::_IsEnabled() const
|
ServiceView::_IsEnabled() const
|
||||||
{
|
{
|
||||||
BNetworkServiceSettings settings = fSettings.Service("ssh");
|
BMessage request(kMsgIsServiceRunning);
|
||||||
return settings.IsEnabled();
|
request.AddString("name", "ssh");
|
||||||
|
|
||||||
|
BMessenger networkServer(kNetServerSignature);
|
||||||
|
BMessage reply;
|
||||||
|
status_t status = networkServer.SendMessage(&request, &reply);
|
||||||
|
if (status == B_OK)
|
||||||
|
return reply.GetBool("running");
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ServiceView::_Enable()
|
ServiceView::_Enable()
|
||||||
{
|
{
|
||||||
|
if (getpwnam("sshd") == NULL) {
|
||||||
|
// We need to create a dedicated user for the service
|
||||||
|
// The following code is copied from useradd, and should be moved
|
||||||
|
// to a public class.
|
||||||
|
gid_t groupID = 100;
|
||||||
|
|
||||||
|
// Find an unused user ID
|
||||||
|
uid_t userID = 1000;
|
||||||
|
while (getpwuid(userID) != NULL)
|
||||||
|
userID++;
|
||||||
|
|
||||||
|
const char* home = "/boot/home";
|
||||||
|
int expiration = 99999;
|
||||||
|
int inactive = -1;
|
||||||
|
const char* shell = "/bin/sh";
|
||||||
|
const char* realName = "";
|
||||||
|
|
||||||
|
int min = -1;
|
||||||
|
int max = -1;
|
||||||
|
int warn = 7;
|
||||||
|
|
||||||
|
// prepare request for the registrar
|
||||||
|
KMessage message(BPrivate::B_REG_UPDATE_USER);
|
||||||
|
if (message.AddInt32("uid", userID) == B_OK
|
||||||
|
&& message.AddInt32("gid", groupID) == B_OK
|
||||||
|
&& message.AddString("name", "sshd") == B_OK
|
||||||
|
&& message.AddString("password", "x") == B_OK
|
||||||
|
&& message.AddString("home", home) == B_OK
|
||||||
|
&& message.AddString("shell", shell) == B_OK
|
||||||
|
&& message.AddString("real name", realName) == B_OK
|
||||||
|
&& message.AddString("shadow password", "!") == B_OK
|
||||||
|
&& message.AddInt32("last changed", time(NULL)) == B_OK
|
||||||
|
&& message.AddInt32("min", min) == B_OK
|
||||||
|
&& message.AddInt32("max", max) == B_OK
|
||||||
|
&& message.AddInt32("warn", warn) == B_OK
|
||||||
|
&& message.AddInt32("inactive", inactive) == B_OK
|
||||||
|
&& message.AddInt32("expiration", expiration) == B_OK
|
||||||
|
&& message.AddInt32("flags", 0) == B_OK
|
||||||
|
&& message.AddBool("add user", true) == B_OK) {
|
||||||
|
// send the request
|
||||||
|
KMessage reply;
|
||||||
|
status_t error = send_authentication_request_to_registrar(message,
|
||||||
|
reply);
|
||||||
|
if (error != B_OK) {
|
||||||
|
fprintf(stderr, "Error: Failed to create user: %s\n",
|
||||||
|
strerror(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BNetworkServiceSettings settings;
|
BNetworkServiceSettings settings;
|
||||||
settings.SetName("ssh");
|
settings.SetName("ssh");
|
||||||
settings.SetStandAlone(true);
|
settings.SetStandAlone(true);
|
||||||
@@ -168,10 +241,19 @@ ServiceView::_Enable()
|
|||||||
void
|
void
|
||||||
ServiceView::_Disable()
|
ServiceView::_Disable()
|
||||||
{
|
{
|
||||||
|
// Since the sshd user may have been customized, we don't remove it
|
||||||
fSettings.RemoveService("ssh");
|
fSettings.RemoveService("ssh");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ServiceView::_UpdateEnableButton()
|
||||||
|
{
|
||||||
|
fEnableButton->SetLabel(_IsEnabled()
|
||||||
|
? B_TRANSLATE("Disable") : B_TRANSLATE("Enable"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark -
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user