Ylianst / MeshCentral

A complete web-based remote monitoring and management web site. Once setup you can install agents and perform remote desktop session to devices on the local network or over the Internet.
https://meshcentral.com
Apache License 2.0
3.99k stars 537 forks source link

Add FreeBSD support to Mesh Agent #108

Closed ghost closed 5 years ago

ghost commented 5 years ago

Is this something you plan on adding in the future? I imagine it wouldn't be too hard to implement since FreeBSD is quite similar to Linux and MacOS in many ways. The only thing I can see right now that needs to be changed from the Linux agent implementation is to use FreeBSD's init system instead of Linux' systemd.

krayon007 commented 5 years ago

I don't imagine it would be too difficult, because we already have macos support, which is based on FreeBSD, likewise we have support for macos's launchctl.

The only issue I see right now is that all the FreeBSD specific code is wrapped with macos ifdefs in the code. In order to support FreeBSD I'd have to tweej the code to add explicit FreeBSD support without using macos specific libraries, like grandcentral dispatch.

It might take some time, but I'll get around to it. Do you have a link to an iso I can use to create a VM image? I'll need to start there before I can do any work...

ghost commented 5 years ago

@krayon007 Sure thing, here you go: https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/12.0/FreeBSD-12.0-RELEASE-amd64-bootonly.iso

ghost commented 5 years ago

@krayon007 Any progress on getting it to work on FreeBSD?

Ylianst commented 5 years ago

This one if going to take a while. We are in the middle of a few big ticket items. I git it at least a month before we look into this. Of course, the agent is open source on GitHub. If someone else wants to take a go at it.

krayon007 commented 5 years ago

I setup a FreeBSD image with Xorg and KDE, so I can start playing around with it. It might take some time to port everything. I used Xorg to make the KVM work easier. What are you using?

The first thing I need to take a look at is to see if I can reuse my service manager on FreeBSD, as I see FreeBSD only supports RC style services, which means auto restart on crash isn't there, so I'll have to look at alternatives.

ghost commented 5 years ago

@krayon007 I don't use a desktop environment on my FreeBSD machines. One is a pfSense installation (which is based on FreeBSD), the other simply acts as a file server for other VMs via NFS.

krayon007 commented 5 years ago

I have the agent working on FreeBSD, including support for Files, Terminal, and KVM. I just have a few outstanding issues to resolve before it's ready for release. Here's a preview tho:

BSD_KVM

krayon007 commented 5 years ago

Almost done. Only issue left is fixing auto-update so it works correctly on FreeBSD.

krayon007 commented 5 years ago

By the way, what hardware are you guys running? I created a new ARCHID of 30, which is for x86-64 on FreeBSD, so you can use: gmake freebsd ARCHID=30

ghost commented 5 years ago

@krayon007 I'm running FreeBSD on a server with two Xeon E5 2697v2 CPUs (i.e. x86-64). Have you been able to fix the auto-update feature on FreeBSD?

krayon007 commented 5 years ago

Yes, I did. Are you experiencing issues?

ghost commented 5 years ago

@krayon007 No, I wasn't aware that FreeBSD support had been fully implemented by now. I just tried installing MeshAgent on FreeBSD 12 and it exited unsuccessfully with "wget: No match" as the error message. I subsequently put the download URL between quotation marks and that worked. Can you add that fix to MeshCentral?

Edit: Actually, although the installation finished successfully and the meshagent service is running, the FreeBSD machine wasn't added to any device group. Here's the output from executing the installation command and a subsequent check of whether or not the meshagent service is running.

root@freebsd:~ # wget "https://meshcentral.datahoarder.dev/meshagents?script=1"
--2019-08-23 05:34:26--  https://meshcentral.datahoarder.dev/meshagents?script=1
Resolving meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)... 95.168.192.49
Connecting to meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)|95.168.192.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9194 (9.0K) [text/plain]
Saving to: 'meshagents?script=1'

meshagents?script=1                                                             100%[====================================================================================================================================================================================================>]   8.98K  --.-KB/s    in 0s

2019-08-23 05:34:27 (41.2 MB/s) - 'meshagents?script=1' saved [9194/9194]

root@freebsd:~ # (wget "https://meshcentral.datahoarder.dev/meshagents?script=1" --no-check-certificate -O ./meshinstall.sh || wget "https://meshcentral.datahoarder.dev/meshagents?script=1" --no-proxy --no-check-certificate -O ./meshinstall.sh) && chmod 755 ./meshinstall.sh && sudo ./meshinstall.sh https://meshcentral.datahoarder.dev '<redacted>'
--2019-08-23 05:35:39--  https://meshcentral.datahoarder.dev/meshagents?script=1
Resolving meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)... 95.168.192.49
Connecting to meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)|95.168.192.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9194 (9.0K) [text/plain]
Saving to: './meshinstall.sh'

./meshinstall.sh                                                                100%[====================================================================================================================================================================================================>]   8.98K  --.-KB/s    in 0s

2019-08-23 05:35:39 (33.4 MB/s) - './meshinstall.sh' saved [9194/9194]

meshagent does not exist in /etc/rc.d or the local startup
directories (/usr/local/etc/rc.d), or is not executable
Agent uninstalled.
Downloading Mesh agent #30...
--2019-08-23 05:35:39--  https://meshcentral.datahoarder.dev/meshagents?id=30
Resolving meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)... 95.168.192.49
Connecting to meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)|95.168.192.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4334496 (4.1M) [application/octet-stream]
Saving to: '/usr/local/mesh/meshagent'

/usr/local/mesh/meshagent                                                       100%[====================================================================================================================================================================================================>]   4.13M  --.-KB/s    in 0.06s

2019-08-23 05:35:39 (73.5 MB/s) - '/usr/local/mesh/meshagent' saved [4334496/4334496]

Mesh agent downloaded.
--2019-08-23 05:35:39--  https://meshcentral.datahoarder.dev/meshsettings?id=<redacted>
Resolving meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)... 95.168.192.49
Connecting to meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)|95.168.192.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 305 [application/octet-stream]
Saving to: '/usr/local/mesh/meshagent.msh'

/usr/local/mesh/meshagent.msh                                                   100%[====================================================================================================================================================================================================>]     305  --.-KB/s    in 0s

2019-08-23 05:35:39 (27.2 MB/s) - '/usr/local/mesh/meshagent.msh' saved [305/305]

--2019-08-23 05:35:39--  https://meshcentral.datahoarder.dev/meshagents?script=5
Resolving meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)... 95.168.192.49
Connecting to meshcentral.datahoarder.dev (meshcentral.datahoarder.dev)|95.168.192.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 387 [text/plain]
Saving to: '/usr/local/etc/rc.d/meshagent'

/usr/local/etc/rc.d/meshagent                                                   100%[====================================================================================================================================================================================================>]     387  --.-KB/s    in 0s

2019-08-23 05:35:40 (30.2 MB/s) - '/usr/local/etc/rc.d/meshagent' saved [387/387]

Starting meshagent.
meshagent installed as BSD service.
To start service: sudo service meshagent start
To stop service: sudo service meshagent stop
Mesh agent started.
root@freebsd:~ # service meshagent status
meshagent is running as pid 41332.
krayon007 commented 5 years ago

can you stop the service, then cd to /usr/local/mesh then run the agent in console mode: ./meshagent

It will print a bunch of stuff to stdout, with regards to what it is doing... Sounds like it's simply unable to connect to the server for some reason...

(You'll probably have to sudo ./meshagent, because the db will require elevated privs to open)

krayon007 commented 5 years ago

By any chance do you need a proxy to connect to the server from the agent?

ghost commented 5 years ago

@krayon007 Here's the output from running the meshagent binary manually:

root@freebsd:~ # service meshagent stop
Stopping meshagent.
Waiting for PIDS: 47942.
root@freebsd:~ # cd /usr/local/mesh
root@freebsd:/usr/local/mesh # ./meshagent
CRITICALEXIT, FILE: microscript/ILibDuktape_ScriptContainer.c, LINE: 472

I don't need a proxy to connect to the server from the agent.

krayon007 commented 5 years ago

Interesting, looks like it's crashing. I'll take a look when I get in the office tomorrow.

krayon007 commented 5 years ago

What version of the server are you using?

ghost commented 5 years ago

@krayon007 I'm using MeshCentral v0.4.0-f (the latest version as of the time of this writing).

krayon007 commented 5 years ago

It's because you don't have /proc mounted on your system, which is required for the agent to run. Here are my notes I put in the makefile for FreeBSD:

Special note about running on FreeBSD systems:

  1. You'll need to mount procfs, which isn't mounted by default on FreeBSD. Add this line to /etc/fstab proc /proc procfs rw 0 0
  2. If you don't reboot, then you can manually mount with the command: mount -t procfs proc /proc
  3. In addition, it is recommended to install bash, which you can do with the following command: pkg install bash
ghost commented 5 years ago

@krayon007 I followed those steps and tried running the meshagent binary again; this time I received a different error message:

root@freebsd:/usr/local/mesh # ./meshagent
MeshCentral2 Agent
Connecting to: 7EF730E64E804FE508D404883C539243862A89BA2A92E05F9996E3CB2C9E537C45F9963D7C794F69ABB54DBCCB42D2BF
Segmentation fault (core dumped)

Edit: I've uploaded the meshagent.core file here: https://send.datahoarder.dev/download/9199d75bac9a8a48/#kUWWCMmweQNdUnuJafMxsA

krayon007 commented 5 years ago

Can you try this version, and if it crashes, post the core dump for it? This is the current build, and has debug symbols still in it, so the core dump should be more useful...

You'll probably need to rename it to meshagent, so it can use your existing configuration/settings/db. You should also probably edit the .msh file, and add the following line, so it doesn't update to the server version, which could screw up the core dump... disableUpdate=1

Link to current binary

ghost commented 5 years ago

@krayon007 Here's the new core dump: https://send.datahoarder.dev/download/dcec0011be02f7d2/#X0A-HMcSmUiXMIQRP4SYKA

krayon007 commented 5 years ago

Ok, I fixed it. My test machine happen to have a system proxy set. If a system proxy is not set, then that variable will be NULL, and the length is 0. Windows and Linux don't dereference the variable if the length is 0, but apparently FreeBSD will still dereference the variable, which in this case is NULL, and segfaults...

You can pull the updated binary from here, but I also pushed the fix to GIT as well... We're planning a windows and linux agent release imminently, so I'll include FreeBSD as well...

Updated Binary

ghost commented 5 years ago

@krayon007 Thanks, I can confirm that the device can connect to MeshCentral successfully with the new meshagent binary and that it then shows up in the correct device group. Can you also push a commit to fix the issue with the MeshAgent wget URL not being inbetween quotation marks?

vivaldi_2019-08-24_16-34-59

ghost commented 5 years ago

@krayon007 @Ylianst Any update on pushing a commit to put the two wget URLs inbetween quotation marks?

krayon007 commented 5 years ago

We're doing a major update this week, of the agent on all platforms, so stay tuned....

ghost commented 5 years ago

@krayon007 Thanks!