tcp: Add APICall trace entry and move TRACEs into locked parts.
[haiku.git] / src / add-ons / network_settings / sshd / SSHServiceAddOn.cpp
blob70885a51cc37e96647ad7d264fd43f8c91c1e6d9
1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, <axeld@pinc-software.de>
7 * Ingo Weinhold <ingo_weinhold@gmx.de>
8 */
11 #include <pwd.h>
13 #include <Button.h>
14 #include <Catalog.h>
15 #include <LayoutBuilder.h>
16 #include <NetworkSettings.h>
17 #include <NetworkSettingsAddOn.h>
18 #include <StringView.h>
19 #include <TextView.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 {
41 public:
42 SSHServiceAddOn(image_id image,
43 BNetworkSettings& settings);
44 virtual ~SSHServiceAddOn();
46 virtual BNetworkSettingsItem*
47 CreateNextItem(uint32& cookie);
51 class SSHServiceView : public ServiceView {
52 public:
53 SSHServiceView(BNetworkSettings& settings);
54 virtual ~SSHServiceView();
56 protected:
57 virtual void Enable();
61 class SSHServiceItem : public BNetworkSettingsItem {
62 public:
63 SSHServiceItem(BNetworkSettings& settings);
64 virtual ~SSHServiceItem();
66 virtual BNetworkSettingsType
67 Type() const;
69 virtual BListItem* ListItem();
70 virtual BView* View();
72 virtual status_t Revert();
73 virtual bool IsRevertable();
75 virtual void SettingsUpdated(uint32 which);
77 private:
78 BNetworkSettings& fSettings;
79 BListItem* fItem;
80 ServiceView* fView;
84 // #pragma mark -
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()
102 void
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.
109 gid_t groupID = 100;
111 // Find an unused user ID
112 uid_t userID = 1000;
113 while (getpwuid(userID) != NULL)
114 userID++;
116 const char* home = "/boot/home";
117 int expiration = 99999;
118 int inactive = -1;
119 const char* shell = "/bin/sh";
120 const char* realName = "";
122 int min = -1;
123 int max = -1;
124 int warn = 7;
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) {
144 // send the request
145 KMessage reply;
146 status_t error = send_authentication_request_to_registrar(message,
147 reply);
148 if (error != B_OK) {
149 fprintf(stderr, "Error: Failed to create user: %s\n",
150 strerror(error));
155 BNetworkServiceSettings settings;
156 settings.SetName("ssh");
157 settings.SetStandAlone(true);
159 settings.AddArgument("/bin/sshd");
160 settings.AddArgument("-D");
162 BMessage service;
163 if (settings.GetMessage(service) == B_OK)
164 fSettings.AddService(service);
168 // #pragma mark -
171 SSHServiceItem::SSHServiceItem(BNetworkSettings& settings)
173 fSettings(settings),
174 fItem(new ServiceListItem("ssh", B_TRANSLATE("SSH server"), settings)),
175 fView(NULL)
180 SSHServiceItem::~SSHServiceItem()
182 if (fView->Parent() == NULL)
183 delete fView;
185 delete fItem;
189 BNetworkSettingsType
190 SSHServiceItem::Type() const
192 return B_NETWORK_SETTINGS_TYPE_SERVICE;
196 BListItem*
197 SSHServiceItem::ListItem()
199 return fItem;
203 BView*
204 SSHServiceItem::View()
206 if (fView == NULL)
207 fView = new SSHServiceView(fSettings);
209 return fView;
213 status_t
214 SSHServiceItem::Revert()
216 return fView != NULL ? fView->Revert() : B_OK;
220 bool
221 SSHServiceItem::IsRevertable()
223 return fView != NULL ? fView->IsRevertable() : false;
227 void
228 SSHServiceItem::SettingsUpdated(uint32 which)
230 if (fView != NULL)
231 fView->SettingsUpdated(which);
235 // #pragma mark -
238 SSHServiceAddOn::SSHServiceAddOn(image_id image,
239 BNetworkSettings& settings)
241 BNetworkSettingsAddOn(image, settings)
246 SSHServiceAddOn::~SSHServiceAddOn()
251 BNetworkSettingsItem*
252 SSHServiceAddOn::CreateNextItem(uint32& cookie)
254 if (cookie++ == 0)
255 return new SSHServiceItem(Settings());
257 return NULL;
261 // #pragma mark -
264 extern "C"
265 BNetworkSettingsAddOn*
266 instantiate_network_settings_add_on(image_id image, BNetworkSettings& settings)
268 return new SSHServiceAddOn(image, settings);