Skip to content

Commit

Permalink
Fetch SNMP, back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Jun 26, 2024
1 parent 8299113 commit 5cf5e09
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
45 changes: 42 additions & 3 deletions Protest/Front/deviceview.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DeviceView extends View {
"owner", "owner name", "location",

["mono/directory.svg", "Domain information"],
"guid", "distinguished name", "dns hostname", "created on dc", "fqdn",
"object guid", "distinguished name", "dns hostname", "created on dc", "fqdn",

["mono/credential.svg", "credentials"],
"domain", "username", "password", "ssh username", "ssh password", "anydesk id", "anydesk password"
Expand Down Expand Up @@ -2658,8 +2658,6 @@ class DeviceView extends View {
snmpInput.disabled = true;
grid.appendChild(snmpInput);

//TODO: implement SNMP

const wmiToggle = this.CreateToggle("WMI", true, grid);
wmiToggle.label.style.gridArea = "3 / 2";

Expand Down Expand Up @@ -2690,6 +2688,31 @@ class DeviceView extends View {
extendedOption.text = "Extended (1-8191)";
portScanInput.appendChild(extendedOption);

setTimeout(async ()=>{
const snmpProfiles = await this.GetSnmpProfiles();
if (snmpProfiles === null || snmpProfiles.length === 0) return;

snmpToggle.checkbox.disabled = false;

for (let i = 0; i < snmpProfiles.length; i++) {
const option = document.createElement("option");
option.value = snmpProfiles[i].guid;
option.text = snmpProfiles[i].name;
snmpInput.appendChild(option);
}

if (this.link && "snmp profile" in this.link) {
snmpToggle.checkbox.checked = true;
snmpInput.disabled = false;
snmpInput.value = this.link["snmp profile"].v;
}
else if (this.link && ".snmp profile" in this.link) {
snmpToggle.checkbox.checked = true;
snmpInput.disabled = false;
snmpInput.value = this.link[".snmp profile"].v;
}
}, 0);

snmpToggle.checkbox.onchange = ()=> {
snmpInput.disabled = !snmpToggle.checkbox.checked;
};
Expand Down Expand Up @@ -2808,6 +2831,22 @@ class DeviceView extends View {
dialog.okButton.focus();
}

async GetSnmpProfiles() {
try {
const response = await fetch("config/snmpprofiles/list");

if (response.status !== 200) return null;

const json = await response.json();
if (json.error) throw(json.error);

return json;
}
catch (ex) {
return null;
}
}

Copy() { //overrides
new DeviceView({copy: this.args.file});
}
Expand Down
4 changes: 2 additions & 2 deletions Protest/Front/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ class Fetch extends Tabs {

const guidOption = document.createElement("option");
guidOption.text = "Same GUID";
guidOption.value = "guid";
guidOption.value = "object guid";
conflictConditionInput.appendChild(guidOption);

conflictConditionInput.value = "ip";
Expand All @@ -1113,7 +1113,7 @@ class Fetch extends Tabs {
conflictConditionInput.appendChild(usernameOption);
const guidOption = document.createElement("option");
guidOption.text = "Same GUID";
guidOption.value = "guid";
guidOption.value = "object guid";
conflictConditionInput.appendChild(guidOption);

conflictConditionInput.value = "username";
Expand Down
2 changes: 1 addition & 1 deletion Protest/Front/userview.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UserView extends View {
"e-mail", "secondary e-mail", "telephone number", "office number", "mobile number", "internal extension", "mobile extension", "fax",

["mono/directory.svg", "Domain information"],
"guid", "distinguished name",
"object guid", "distinguished name",

["mono/sim.svg", "sim information"],
"sim", "puk", "voicemail"
Expand Down
2 changes: 1 addition & 1 deletion Protest/Protocols/Kerberos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public static Dictionary<string, string> AdFetch(SearchResult result) {

ContentBuilderAddValue(result, "facsimiletelephonenumber", "fax", data, null);

data.Add("guid", new Guid((byte[])result.Properties["objectGuid"][0]).ToString());
data.Add("object guid", new Guid((byte[])result.Properties["objectGuid"][0]).ToString());

return data;
}
Expand Down
59 changes: 37 additions & 22 deletions Protest/Tasks/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Protest.Tasks;
internal static class Fetch {

enum Type : byte {
none = 0,
none = 0,
devices = 1,
users = 2,
users = 2,
}

struct Result {
Expand All @@ -48,23 +48,44 @@ public static byte[] SingleDeviceSerialize(Dictionary<string, string> parameters
parameters.TryGetValue("target", out string target);
parameters.TryGetValue("wmi", out string wmi);
parameters.TryGetValue("kerberos", out string kerberos);
parameters.TryGetValue("snmp2", out string snmp2);
parameters.TryGetValue("snmp3", out string snmp3);
parameters.TryGetValue("snmp", out string snmp);
parameters.TryGetValue("portscan", out string portScan);

if (target is null) {
return Data.CODE_INVALID_ARGUMENT.Array;
}

ConcurrentDictionary<string, string[]> data = SingleDevice(target, true, wmi == "true", kerberos == "true", snmp2=="true", null, snmp3=="true", null, portScan, asynchronous, CancellationToken.None);
SnmpProfiles.Profile snmpProfile = null;
if (snmp is not null && Guid.TryParse(snmp, out Guid guid)) {
SnmpProfiles.Profile[] profiles = SnmpProfiles.Load();
for (int i = 0; i< profiles.Length; i++) {
if (profiles[i].guid == guid) {
snmpProfile = profiles[i];
break;
}
}
}

SnmpProfiles.Profile[] snmpProfiles = snmpProfile is null ? null : new SnmpProfiles.Profile[] { snmpProfile };

ConcurrentDictionary<string, string[]> data = SingleDevice(
target,
true,
wmi=="true",
kerberos=="true",
snmpProfiles,
portScan,
asynchronous,
CancellationToken.None
);

if (data is null || data.IsEmpty) {
return "{\"error\":\"Failed to fetch data.\"}"u8.ToArray();
}

return JsonSerializer.SerializeToUtf8Bytes(data, fetchSerializerOptions);
}
public static async Task<ConcurrentDictionary<string, string[]>> SingleDeviceAsync(string target, bool useDns, bool useWmi, bool useKerberos, bool snmp2, string[] snmp2Profiles, bool snmp3, string[] snmp3Profiles, string argPortScan, bool asynchronous, CancellationToken cancellationToken) {
public static async Task<ConcurrentDictionary<string, string[]>> SingleDeviceAsync(string target, bool useDns, bool useWmi, bool useKerberos, SnmpProfiles.Profile[] snmpProfiles, string argPortScan, bool asynchronous, CancellationToken cancellationToken) {
PingReply reply = null;
try {
using Ping ping = new Ping();
Expand All @@ -76,13 +97,13 @@ public static async Task<ConcurrentDictionary<string, string[]>> SingleDeviceAsy
catch { }

if (reply?.Status == IPStatus.Success) {
ConcurrentDictionary<string, string[]> data = SingleDevice(target, useDns, useWmi, useKerberos, snmp2, snmp2Profiles, snmp3, snmp3Profiles, argPortScan, asynchronous, cancellationToken);
ConcurrentDictionary<string, string[]> data = SingleDevice(target, useDns, useWmi, useKerberos, snmpProfiles , argPortScan, asynchronous, cancellationToken);
return data;
}

return null;
}
public static ConcurrentDictionary<string, string[]> SingleDevice(string target, bool useDns, bool useWmi, bool useKerberos, bool snmp2, string[] snmp2Profiles, bool snmp3, string[] snmp3Profiles, string argPortScan, bool asynchronous, CancellationToken cancellationToken) {
public static ConcurrentDictionary<string, string[]> SingleDevice(string target, bool useDns, bool useWmi, bool useKerberos, SnmpProfiles.Profile[] snmpProfiles, string argPortScan, bool asynchronous, CancellationToken cancellationToken) {
if (target.Contains(';')) {
target = target.Split(';')[0].Trim();
}
Expand Down Expand Up @@ -186,7 +207,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
if (value.Length > 0) ad.Add("CHANGED ON DC", value);
}*/

ad.Add("guid", new Guid((byte[])result.Properties["objectGuid"][0]).ToString());
ad.Add("object guid", new Guid((byte[])result.Properties["objectGuid"][0]).ToString());
});
}

Expand Down Expand Up @@ -247,7 +268,6 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,

foreach (KeyValuePair<string, string> o in ad) {
string key = o.Key;

if (key == "operating system") { //os not found in ad, use wmi
if (!wmi.ContainsKey("operating system")) {
data.TryAdd(key, new string[] { o.Value, "Kerberos", string.Empty });
Expand Down Expand Up @@ -362,7 +382,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
return null;
}

if (snmp2) {
if (snmpProfiles is not null) {
if (data.TryGetValue("type", out string[] type)) {
switch (type[0]) {
case "fax":
Expand Down Expand Up @@ -392,10 +412,6 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
//TODO: generic OID
}

if (snmp3) {
//TODO:
}

if (cancellationToken.IsCancellationRequested) {
return null;
}
Expand Down Expand Up @@ -454,9 +470,11 @@ public static byte[] DevicesTask(HttpListenerContext ctx, Dictionary<string, str
string portscan = null;
string retriesStr = null;
string intervalStr = null;

string snmp2 = null;
string snmp3 = null;
string[] snmp2Profiles = null;

string snmp3 = null;
string[] snmp3Profiles = null;

for (int i = 0; i < payloadLines.Length; i++) {
Expand Down Expand Up @@ -571,17 +589,14 @@ public static byte[] DevicesTask(HttpListenerContext ctx, Dictionary<string, str
dns == "true",
wmi == "true",
kerberos == "true",
snmp2 == "true",
snmp2Profiles,
snmp3 == "true",
snmp3Profiles,
null,
portscan,
retries,
interval,
origin
);
}
public static byte[] DevicesTask(string[] hosts, bool dns, bool wmi, bool kerberos, bool snmp2, string[] snmp2Profiles, bool snmp3, string[] snmp3Profiles, string portscan, int retries, float interval, string origin) {
public static byte[] DevicesTask(string[] hosts, bool dns, bool wmi, bool kerberos, SnmpProfiles.Profile[] snmpProfiles, string portscan, int retries, float interval, string origin) {
if (task is not null) return Data.CODE_OTHER_TASK_IN_PROGRESS.Array;
if (result is not null) return Data.CODE_OTHER_TASK_IN_PROGRESS.Array;

Expand All @@ -604,7 +619,7 @@ public static byte[] DevicesTask(string[] hosts, bool dns, bool wmi, bool kerber

List<Task<ConcurrentDictionary<string, string[]>>> tasks = new List<Task<ConcurrentDictionary<string, string[]>>>();
for (int i = 0; i < size; i++) {
tasks.Add(SingleDeviceAsync(queue[i], dns, wmi, kerberos, snmp2, snmp2Profiles, snmp3, snmp3Profiles, portscan, false, task.cancellationToken));
tasks.Add(SingleDeviceAsync(queue[i], dns, wmi, kerberos, snmpProfiles, portscan, false, task.cancellationToken));
}

ConcurrentDictionary<string, string[]>[] result = await Task.WhenAll(tasks);
Expand Down

0 comments on commit 5cf5e09

Please sign in to comment.