db.js 291 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240
  1. /**
  2. * @description MeshCentral database module
  3. * @author Ylian Saint-Hilaire
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.2
  7. */
  8. /*xjslint node: true */
  9. /*xjslint plusplus: true */
  10. /*xjslint maxlen: 256 */
  11. /*jshint node: true */
  12. /*jshint strict: false */
  13. /*jshint esversion: 6 */
  14. "use strict";
  15. //
  16. // Construct Meshcentral database object
  17. //
  18. // The default database is NeDB
  19. // https://github.com/louischatriot/nedb
  20. //
  21. // Alternativety, MongoDB can be used
  22. // https://www.mongodb.com/
  23. // Just run with --mongodb [connectionstring], where the connection string is documented here: https://docs.mongodb.com/manual/reference/connection-string/
  24. // The default collection is "meshcentral", but you can override it using --mongodbcol [collection]
  25. //
  26. module.exports.CreateDB = function (parent, func) {
  27. var obj = {};
  28. var Datastore = null;
  29. var expireEventsSeconds = (60 * 60 * 24 * 20); // By default, expire events after 20 days (1728000). (Seconds * Minutes * Hours * Days)
  30. var expirePowerEventsSeconds = (60 * 60 * 24 * 10); // By default, expire power events after 10 days (864000). (Seconds * Minutes * Hours * Days)
  31. var expireServerStatsSeconds = (60 * 60 * 24 * 30); // By default, expire server stats after 30 days (2592000). (Seconds * Minutes * Hours * Days)
  32. const common = require('./common.js');
  33. const path = require('path');
  34. const fs = require('fs');
  35. const DB_NEDB = 1, DB_MONGOJS = 2, DB_MONGODB = 3,DB_MARIADB = 4, DB_MYSQL = 5, DB_POSTGRESQL = 6, DB_ACEBASE = 7, DB_SQLITE = 8;
  36. const DB_LIST = ['None', 'NeDB', 'MongoJS', 'MongoDB', 'MariaDB', 'MySQL', 'PostgreSQL', 'AceBase', 'SQLite']; //for the info command
  37. let databaseName = 'meshcentral';
  38. let datapathParentPath = path.dirname(parent.datapath);
  39. let datapathFoldername = path.basename(parent.datapath);
  40. const SQLITE_AUTOVACUUM = ['none', 'full', 'incremental'];
  41. const SQLITE_SYNCHRONOUS = ['off', 'normal', 'full', 'extra'];
  42. obj.sqliteConfig = {
  43. maintenance: '',
  44. startupVacuum: false,
  45. autoVacuum: 'full',
  46. incrementalVacuum: 100,
  47. journalMode: 'delete',
  48. journalSize: 4096000,
  49. synchronous: 'full',
  50. };
  51. obj.performingBackup = false;
  52. const BACKUPFAIL_ZIPCREATE = 0x0001;
  53. const BACKUPFAIL_ZIPMODULE = 0x0010;
  54. const BACKUPFAIL_DBDUMP = 0x0100;
  55. obj.backupStatus = 0x0;
  56. obj.newAutoBackupFile = null;
  57. obj.newDBDumpFile = null;
  58. obj.identifier = null;
  59. obj.dbKey = null;
  60. obj.dbRecordsEncryptKey = null;
  61. obj.dbRecordsDecryptKey = null;
  62. obj.changeStream = false;
  63. obj.pluginsActive = ((parent.config) && (parent.config.settings) && (parent.config.settings.plugins != null) && (parent.config.settings.plugins != false) && ((typeof parent.config.settings.plugins != 'object') || (parent.config.settings.plugins.enabled != false)));
  64. obj.dbCounters = {
  65. fileSet: 0,
  66. fileRemove: 0,
  67. powerSet: 0,
  68. eventsSet: 0
  69. }
  70. // MongoDB bulk operations state
  71. if (parent.config.settings.mongodbbulkoperations) {
  72. // Added counters
  73. obj.dbCounters.fileSetPending = 0;
  74. obj.dbCounters.fileSetBulk = 0;
  75. obj.dbCounters.fileRemovePending = 0;
  76. obj.dbCounters.fileRemoveBulk = 0;
  77. obj.dbCounters.powerSetPending = 0;
  78. obj.dbCounters.powerSetBulk = 0;
  79. obj.dbCounters.eventsSetPending = 0;
  80. obj.dbCounters.eventsSetBulk = 0;
  81. /// Added bulk accumulators
  82. obj.filePendingGet = null;
  83. obj.filePendingGets = null;
  84. obj.filePendingRemove = null;
  85. obj.filePendingRemoves = null;
  86. obj.filePendingSet = false;
  87. obj.filePendingSets = null;
  88. obj.filePendingCb = null;
  89. obj.filePendingCbs = null;
  90. obj.powerFilePendingSet = false;
  91. obj.powerFilePendingSets = null;
  92. obj.powerFilePendingCb = null;
  93. obj.powerFilePendingCbs = null;
  94. obj.eventsFilePendingSet = false;
  95. obj.eventsFilePendingSets = null;
  96. obj.eventsFilePendingCb = null;
  97. obj.eventsFilePendingCbs = null;
  98. }
  99. obj.SetupDatabase = function (func) {
  100. // Check if the database unique identifier is present
  101. // This is used to check that in server peering mode, everyone is using the same database.
  102. obj.Get('DatabaseIdentifier', function (err, docs) {
  103. if (err != null) { parent.debug('db', 'ERROR (Get DatabaseIdentifier): ' + err); }
  104. if ((err == null) && (docs.length == 1) && (docs[0].value != null)) {
  105. obj.identifier = docs[0].value;
  106. } else {
  107. obj.identifier = Buffer.from(require('crypto').randomBytes(48), 'binary').toString('hex');
  108. obj.Set({ _id: 'DatabaseIdentifier', value: obj.identifier });
  109. }
  110. });
  111. // Load database schema version and check if we need to update
  112. obj.Get('SchemaVersion', function (err, docs) {
  113. if (err != null) { parent.debug('db', 'ERROR (Get SchemaVersion): ' + err); }
  114. var ver = 0;
  115. if ((err == null) && (docs.length == 1)) { ver = docs[0].value; }
  116. if (ver == 1) { console.log('This is an unsupported beta 1 database, delete it to create a new one.'); process.exit(0); }
  117. // TODO: Any schema upgrades here...
  118. obj.Set({ _id: 'SchemaVersion', value: 2 });
  119. func(ver);
  120. });
  121. };
  122. // Perform database maintenance
  123. obj.maintenance = function () {
  124. parent.debug('db', 'Entering database maintenance');
  125. if (obj.databaseType == DB_NEDB) { // NeDB will not remove expired records unless we try to access them. This will force the removal.
  126. obj.eventsfile.remove({ time: { '$lt': new Date(Date.now() - (expireEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  127. obj.powerfile.remove({ time: { '$lt': new Date(Date.now() - (expirePowerEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  128. obj.serverstatsfile.remove({ time: { '$lt': new Date(Date.now() - (expireServerStatsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  129. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL)) { // MariaDB or MySQL
  130. sqlDbQuery('DELETE FROM events WHERE time < ?', [new Date(Date.now() - (expireEventsSeconds * 1000))], function (doc, err) { }); // Delete events older than expireEventsSeconds
  131. sqlDbQuery('DELETE FROM power WHERE time < ?', [new Date(Date.now() - (expirePowerEventsSeconds * 1000))], function (doc, err) { }); // Delete events older than expirePowerSeconds
  132. sqlDbQuery('DELETE FROM serverstats WHERE expire < ?', [new Date()], function (doc, err) { }); // Delete events where expiration date is in the past
  133. sqlDbQuery('DELETE FROM smbios WHERE expire < ?', [new Date()], function (doc, err) { }); // Delete events where expiration date is in the past
  134. } else if (obj.databaseType == DB_ACEBASE) { // AceBase
  135. //console.log('Performing AceBase maintenance');
  136. obj.file.query('events').filter('time', '<', new Date(Date.now() - (expireEventsSeconds * 1000))).remove().then(function () {
  137. obj.file.query('stats').filter('time', '<', new Date(Date.now() - (expireServerStatsSeconds * 1000))).remove().then(function () {
  138. obj.file.query('power').filter('time', '<', new Date(Date.now() - (expirePowerEventsSeconds * 1000))).remove().then(function () {
  139. //console.log('AceBase maintenance done');
  140. });
  141. });
  142. });
  143. } else if (obj.databaseType == DB_SQLITE) { // SQLite3
  144. //sqlite does not return rows affected for INSERT, UPDATE or DELETE statements, see https://www.sqlite.org/pragma.html#pragma_count_changes
  145. obj.file.serialize(function () {
  146. obj.file.run('DELETE FROM events WHERE time < ?', [new Date(Date.now() - (expireEventsSeconds * 1000))]);
  147. obj.file.run('DELETE FROM power WHERE time < ?', [new Date(Date.now() - (expirePowerEventsSeconds * 1000))]);
  148. obj.file.run('DELETE FROM serverstats WHERE expire < ?', [new Date()]);
  149. obj.file.run('DELETE FROM smbios WHERE expire < ?', [new Date()]);
  150. obj.file.exec(obj.sqliteConfig.maintenance, function (err) {
  151. if (err) {console.log('Maintenance error: ' + err.message)};
  152. if (parent.config.settings.debug) {
  153. sqliteGetPragmas(['freelist_count', 'page_size', 'page_count', 'cache_size' ], function (pragma, pragmaValue) {
  154. parent.debug('db', 'SQLite Maintenance: ' + pragma + '=' + pragmaValue);
  155. });
  156. };
  157. });
  158. });
  159. }
  160. obj.removeInactiveDevices();
  161. }
  162. // Remove inactive devices
  163. obj.removeInactiveDevices = function (showall, cb) {
  164. // Get a list of domains and what their inactive device removal setting is
  165. var removeInactiveDevicesPerDomain = {}, minRemoveInactiveDevicesPerDomain = {}, minRemoveInactiveDevice = 9999;
  166. for (var i in parent.config.domains) {
  167. if (typeof parent.config.domains[i].autoremoveinactivedevices == 'number') {
  168. var v = parent.config.domains[i].autoremoveinactivedevices;
  169. if ((v >= 1) && (v <= 2000)) {
  170. if (v < minRemoveInactiveDevice) { minRemoveInactiveDevice = v; }
  171. removeInactiveDevicesPerDomain[i] = v;
  172. minRemoveInactiveDevicesPerDomain[i] = v;
  173. }
  174. }
  175. }
  176. // Check if any device groups have a inactive device removal setting
  177. for (var i in parent.webserver.meshes) {
  178. if (typeof parent.webserver.meshes[i].expireDevs == 'number') {
  179. var v = parent.webserver.meshes[i].expireDevs;
  180. if ((v >= 1) && (v <= 2000)) {
  181. if (v < minRemoveInactiveDevice) { minRemoveInactiveDevice = v; }
  182. if ((minRemoveInactiveDevicesPerDomain[parent.webserver.meshes[i].domain] == null) || (minRemoveInactiveDevicesPerDomain[parent.webserver.meshes[i].domain] > v)) {
  183. minRemoveInactiveDevicesPerDomain[parent.webserver.meshes[i].domain] = v;
  184. }
  185. } else {
  186. delete parent.webserver.meshes[i].expireDevs;
  187. }
  188. }
  189. }
  190. // If there are no such settings for any domain, we can exit now.
  191. if (minRemoveInactiveDevice == 9999) { if (cb) { cb("No device removal policy set, nothing to do."); } return; }
  192. const now = Date.now();
  193. // For each domain with a inactive device removal setting, get a list of last device connections
  194. for (var domainid in minRemoveInactiveDevicesPerDomain) {
  195. obj.GetAllTypeNoTypeField('lastconnect', domainid, function (err, docs) {
  196. if ((err != null) || (docs == null)) return;
  197. for (var j in docs) {
  198. const days = Math.floor((now - docs[j].time) / 86400000); // Calculate the number of inactive days
  199. var expireDays = -1;
  200. if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; }
  201. const mesh = parent.webserver.meshes[docs[j].meshid];
  202. if (mesh && (typeof mesh.expireDevs == 'number')) { expireDays = mesh.expireDevs; }
  203. var remove = false;
  204. if (expireDays > 0) {
  205. if (expireDays < days) { remove = true; }
  206. if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
  207. if (remove) {
  208. // Check if this device is connected right now
  209. const nodeid = docs[j]._id.substring(2);
  210. const conn = parent.GetConnectivityState(nodeid);
  211. if (conn == null) {
  212. // Remove the device
  213. obj.Get(nodeid, function (err, docs) {
  214. if (err != null) return;
  215. if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
  216. const node = docs[0];
  217. // Delete this node including network interface information, events and timeline
  218. obj.Remove(node._id); // Remove node with that id
  219. obj.Remove('if' + node._id); // Remove interface information
  220. obj.Remove('nt' + node._id); // Remove notes
  221. obj.Remove('lc' + node._id); // Remove last connect time
  222. obj.Remove('si' + node._id); // Remove system information
  223. obj.Remove('al' + node._id); // Remove error log last time
  224. if (obj.RemoveSMBIOS) { obj.RemoveSMBIOS(node._id); } // Remove SMBios data
  225. obj.RemoveAllNodeEvents(node._id); // Remove all events for this node
  226. obj.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
  227. if (typeof node.pmt == 'string') { obj.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
  228. obj.Get('ra' + node._id, function (err, nodes) {
  229. if ((nodes != null) && (nodes.length == 1)) { obj.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
  230. obj.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
  231. });
  232. // Remove any user node links
  233. if (node.links != null) {
  234. for (var i in node.links) {
  235. if (i.startsWith('user/')) {
  236. var cuser = parent.webserver.users[i];
  237. if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) {
  238. // Remove the user link & save the user
  239. delete cuser.links[node._id];
  240. if (Object.keys(cuser.links).length == 0) { delete cuser.links; }
  241. obj.SetUser(cuser);
  242. // Notify user change
  243. var targets = ['*', 'server-users', cuser._id];
  244. var event = { etype: 'user', userid: cuser._id, username: cuser.name, action: 'accountchange', msgid: 86, msgArgs: [cuser.name], msg: 'Removed user device rights for ' + cuser.name, domain: node.domain, account: parent.webserver.CloneSafeUser(cuser) };
  245. if (obj.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
  246. parent.DispatchEvent(targets, obj, event);
  247. }
  248. } else if (i.startsWith('ugrp/')) {
  249. var cusergroup = parent.userGroups[i];
  250. if ((cusergroup != null) && (cusergroup.links != null) && (cusergroup.links[node._id] != null)) {
  251. // Remove the user link & save the user
  252. delete cusergroup.links[node._id];
  253. if (Object.keys(cusergroup.links).length == 0) { delete cusergroup.links; }
  254. obj.Set(cusergroup);
  255. // Notify user change
  256. var targets = ['*', 'server-users', cusergroup._id];
  257. var event = { etype: 'ugrp', ugrpid: cusergroup._id, name: cusergroup.name, desc: cusergroup.desc, action: 'usergroupchange', links: cusergroup.links, msgid: 163, msgArgs: [node.name, cusergroup.name], msg: 'Removed device ' + node.name + ' from user group ' + cusergroup.name };
  258. if (obj.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
  259. parent.DispatchEvent(targets, obj, event);
  260. }
  261. }
  262. }
  263. }
  264. // Event node deletion
  265. var meshname = '(unknown)';
  266. if ((parent.webserver.meshes[node.meshid] != null) && (parent.webserver.meshes[node.meshid].name != null)) { meshname = parent.webserver.meshes[node.meshid].name; }
  267. var event = { etype: 'node', action: 'removenode', nodeid: node._id, msgid: 87, msgArgs: [node.name, meshname], msg: 'Removed device ' + node.name + ' from device group ' + meshname, domain: node.domain };
  268. // TODO: We can't use the changeStream for node delete because we will not know the meshid the device was in.
  269. //if (obj.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to remove the node. Another event will come.
  270. parent.DispatchEvent(parent.webserver.CreateNodeDispatchTargets(node.meshid, node._id), obj, event);
  271. });
  272. }
  273. }
  274. }
  275. }
  276. });
  277. }
  278. }
  279. // Remove all reference to a domain from the database
  280. obj.removeDomain = function (domainName, func) {
  281. var pendingCalls;
  282. // Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
  283. if (obj.databaseType == DB_ACEBASE) {
  284. // AceBase
  285. pendingCalls = 3;
  286. obj.file.query('meshcentral').filter('domain', '==', domainName).remove().then(function () { if (--pendingCalls == 0) { func(); } });
  287. obj.file.query('events').filter('domain', '==', domainName).remove().then(function () { if (--pendingCalls == 0) { func(); } });
  288. obj.file.query('power').filter('domain', '==', domainName).remove().then(function () { if (--pendingCalls == 0) { func(); } });
  289. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL) || (obj.databaseType == DB_POSTGRESQL)) {
  290. // MariaDB, MySQL or PostgreSQL
  291. pendingCalls = 2;
  292. sqlDbQuery('DELETE FROM main WHERE domain = $1', [domainName], function () { if (--pendingCalls == 0) { func(); } });
  293. sqlDbQuery('DELETE FROM events WHERE domain = $1', [domainName], function () { if (--pendingCalls == 0) { func(); } });
  294. } else if (obj.databaseType == DB_MONGODB) {
  295. // MongoDB
  296. pendingCalls = 3;
  297. obj.file.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  298. obj.eventsfile.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  299. obj.powerfile.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  300. } else {
  301. // NeDB or MongoJS
  302. pendingCalls = 3;
  303. obj.file.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  304. obj.eventsfile.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  305. obj.powerfile.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
  306. }
  307. }
  308. obj.cleanup = function (func) {
  309. // TODO: Remove all mesh links to invalid users
  310. // TODO: Remove all meshes that dont have any links
  311. // Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
  312. if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL) || (obj.databaseType == DB_POSTGRESQL)) {
  313. // MariaDB, MySQL or PostgreSQL
  314. obj.RemoveAllOfType('event', function () { });
  315. obj.RemoveAllOfType('power', function () { });
  316. obj.RemoveAllOfType('smbios', function () { });
  317. } else if (obj.databaseType == DB_MONGODB) {
  318. // MongoDB
  319. obj.file.deleteMany({ type: 'event' }, { multi: true });
  320. obj.file.deleteMany({ type: 'power' }, { multi: true });
  321. obj.file.deleteMany({ type: 'smbios' }, { multi: true });
  322. } else if ((obj.databaseType == DB_NEDB) || (obj.databaseType == DB_MONGOJS)) {
  323. // NeDB or MongoJS
  324. obj.file.remove({ type: 'event' }, { multi: true });
  325. obj.file.remove({ type: 'power' }, { multi: true });
  326. obj.file.remove({ type: 'smbios' }, { multi: true });
  327. }
  328. // List of valid identifiers
  329. var validIdentifiers = {}
  330. // Load all user groups
  331. obj.GetAllType('ugrp', function (err, docs) {
  332. if (err != null) { parent.debug('db', 'ERROR (GetAll user): ' + err); }
  333. if ((err == null) && (docs.length > 0)) {
  334. for (var i in docs) {
  335. // Add this as a valid user identifier
  336. validIdentifiers[docs[i]._id] = 1;
  337. }
  338. }
  339. // Fix all of the creating & login to ticks by seconds, not milliseconds.
  340. obj.GetAllType('user', function (err, docs) {
  341. if (err != null) { parent.debug('db', 'ERROR (GetAll user): ' + err); }
  342. if ((err == null) && (docs.length > 0)) {
  343. for (var i in docs) {
  344. var fixed = false;
  345. // Add this as a valid user identifier
  346. validIdentifiers[docs[i]._id] = 1;
  347. // Fix email address capitalization
  348. if (docs[i].email && (docs[i].email != docs[i].email.toLowerCase())) {
  349. docs[i].email = docs[i].email.toLowerCase(); fixed = true;
  350. }
  351. // Fix account creation
  352. if (docs[i].creation) {
  353. if (docs[i].creation > 1300000000000) { docs[i].creation = Math.floor(docs[i].creation / 1000); fixed = true; }
  354. if ((docs[i].creation % 1) != 0) { docs[i].creation = Math.floor(docs[i].creation); fixed = true; }
  355. }
  356. // Fix last account login
  357. if (docs[i].login) {
  358. if (docs[i].login > 1300000000000) { docs[i].login = Math.floor(docs[i].login / 1000); fixed = true; }
  359. if ((docs[i].login % 1) != 0) { docs[i].login = Math.floor(docs[i].login); fixed = true; }
  360. }
  361. // Fix last password change
  362. if (docs[i].passchange) {
  363. if (docs[i].passchange > 1300000000000) { docs[i].passchange = Math.floor(docs[i].passchange / 1000); fixed = true; }
  364. if ((docs[i].passchange % 1) != 0) { docs[i].passchange = Math.floor(docs[i].passchange); fixed = true; }
  365. }
  366. // Fix subscriptions
  367. if (docs[i].subscriptions != null) { delete docs[i].subscriptions; fixed = true; }
  368. // Save the user if needed
  369. if (fixed) { obj.Set(docs[i]); }
  370. }
  371. // Remove all objects that have a "meshid" that no longer points to a valid mesh.
  372. // Fix any incorrectly escaped user identifiers
  373. obj.GetAllType('mesh', function (err, docs) {
  374. if (err != null) { parent.debug('db', 'ERROR (GetAll mesh): ' + err); }
  375. var meshlist = [];
  376. if ((err == null) && (docs.length > 0)) {
  377. for (var i in docs) {
  378. var meshChange = false;
  379. docs[i] = common.unEscapeLinksFieldName(docs[i]);
  380. meshlist.push(docs[i]._id);
  381. // Make sure all mesh types are number type, if not, fix it.
  382. if (typeof docs[i].mtype == 'string') { docs[i].mtype = parseInt(docs[i].mtype); meshChange = true; }
  383. // If the device group is deleted, remove any invite codes
  384. if (docs[i].deleted && docs[i].invite) { delete docs[i].invite; meshChange = true; }
  385. // Take a look at the links
  386. if (docs[i].links != null) {
  387. for (var j in docs[i].links) {
  388. if (validIdentifiers[j] == null) {
  389. // This identifier is not known, let see if we can fix it.
  390. var xid = j, xid2 = common.unEscapeFieldName(xid);
  391. while ((xid != xid2) && (validIdentifiers[xid2] == null)) { xid = xid2; xid2 = common.unEscapeFieldName(xid2); }
  392. if (validIdentifiers[xid2] == 1) {
  393. //console.log('Fixing id: ' + j + ' to ' + xid2);
  394. docs[i].links[xid2] = docs[i].links[j];
  395. delete docs[i].links[j];
  396. meshChange = true;
  397. } else {
  398. // TODO: here, we may want to clean up links to users and user groups that do not exist anymore.
  399. //console.log('Unknown id: ' + j);
  400. }
  401. }
  402. }
  403. }
  404. // Save the updated device group if needed
  405. if (meshChange) { obj.Set(docs[i]); }
  406. }
  407. }
  408. if (obj.databaseType == DB_SQLITE) {
  409. // SQLite
  410. } else if (obj.databaseType == DB_ACEBASE) {
  411. // AceBase
  412. } else if (obj.databaseType == DB_POSTGRESQL) {
  413. // Postgres
  414. sqlDbQuery('DELETE FROM Main WHERE ((extra != NULL) AND (extra LIKE (\'mesh/%\')) AND (extra != ANY ($1)))', [meshlist], function (err, response) { });
  415. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL)) {
  416. // MariaDB
  417. sqlDbQuery('DELETE FROM Main WHERE (extra LIKE ("mesh/%") AND (extra NOT IN ?)', [meshlist], function (err, response) { });
  418. } else if (obj.databaseType == DB_MONGODB) {
  419. // MongoDB
  420. obj.file.deleteMany({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
  421. } else {
  422. // NeDB or MongoJS
  423. obj.file.remove({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
  424. }
  425. // We are done
  426. validIdentifiers = null;
  427. if (func) { func(); }
  428. });
  429. }
  430. });
  431. });
  432. };
  433. // Get encryption key
  434. obj.getEncryptDataKey = function (password, salt, iterations) {
  435. if (typeof password != 'string') return null;
  436. let key;
  437. try {
  438. key = parent.crypto.pbkdf2Sync(password, salt, iterations, 32, 'sha384');
  439. } catch (ex) {
  440. // If this previous call fails, it's probably because older pbkdf2 did not specify the hashing function, just use the default.
  441. key = parent.crypto.pbkdf2Sync(password, salt, iterations, 32);
  442. }
  443. return key
  444. }
  445. // Encrypt data
  446. obj.encryptData = function (password, plaintext) {
  447. let encryptionVersion = 0x01;
  448. let iterations = 100000
  449. const iv = parent.crypto.randomBytes(16);
  450. var key = obj.getEncryptDataKey(password, iv, iterations);
  451. if (key == null) return null;
  452. const aes = parent.crypto.createCipheriv('aes-256-gcm', key, iv);
  453. var ciphertext = aes.update(plaintext);
  454. let versionbuf = Buffer.allocUnsafe(2);
  455. versionbuf.writeUInt16BE(encryptionVersion);
  456. let iterbuf = Buffer.allocUnsafe(4);
  457. iterbuf.writeUInt32BE(iterations);
  458. let encryptedBuf = aes.final();
  459. ciphertext = Buffer.concat([versionbuf, iterbuf, aes.getAuthTag(), iv, ciphertext, encryptedBuf]);
  460. return ciphertext.toString('base64');
  461. }
  462. // Decrypt data
  463. obj.decryptData = function (password, ciphertext) {
  464. // Adding an encryption version lets us avoid try catching in the future
  465. let ciphertextBytes = Buffer.from(ciphertext, 'base64');
  466. let encryptionVersion = ciphertextBytes.readUInt16BE(0);
  467. try {
  468. switch (encryptionVersion) {
  469. case 0x01:
  470. let iterations = ciphertextBytes.readUInt32BE(2);
  471. let authTag = ciphertextBytes.slice(6, 22);
  472. const iv = ciphertextBytes.slice(22, 38);
  473. const data = ciphertextBytes.slice(38);
  474. let key = obj.getEncryptDataKey(password, iv, iterations);
  475. if (key == null) return null;
  476. const aes = parent.crypto.createDecipheriv('aes-256-gcm', key, iv);
  477. aes.setAuthTag(authTag);
  478. let plaintextBytes = Buffer.from(aes.update(data));
  479. plaintextBytes = Buffer.concat([plaintextBytes, aes.final()]);
  480. return plaintextBytes;
  481. default:
  482. return obj.oldDecryptData(password, ciphertextBytes);
  483. }
  484. } catch (ex) { return obj.oldDecryptData(password, ciphertextBytes); }
  485. }
  486. // Encrypt data
  487. // The older encryption system uses CBC without integraty checking.
  488. // This method is kept only for testing
  489. obj.oldEncryptData = function (password, plaintext) {
  490. let key = parent.crypto.createHash('sha384').update(password).digest('raw').slice(0, 32);
  491. if (key == null) return null;
  492. const iv = parent.crypto.randomBytes(16);
  493. const aes = parent.crypto.createCipheriv('aes-256-cbc', key, iv);
  494. var ciphertext = aes.update(plaintext);
  495. ciphertext = Buffer.concat([iv, ciphertext, aes.final()]);
  496. return ciphertext.toString('base64');
  497. }
  498. // Decrypt data
  499. // The older encryption system uses CBC without integraty checking.
  500. // This method is kept only to convert the old encryption to the new one.
  501. obj.oldDecryptData = function (password, ciphertextBytes) {
  502. if (typeof password != 'string') return null;
  503. try {
  504. const iv = ciphertextBytes.slice(0, 16);
  505. const data = ciphertextBytes.slice(16);
  506. let key = parent.crypto.createHash('sha384').update(password).digest('raw').slice(0, 32);
  507. const aes = parent.crypto.createDecipheriv('aes-256-cbc', key, iv);
  508. let plaintextBytes = Buffer.from(aes.update(data));
  509. plaintextBytes = Buffer.concat([plaintextBytes, aes.final()]);
  510. return plaintextBytes;
  511. } catch (ex) { return null; }
  512. }
  513. // Get the number of records in the database for various types, this is the slow NeDB way.
  514. // WARNING: This is a terrible query for database performance. Only do this when needed. This query will look at almost every document in the database.
  515. obj.getStats = function (func) {
  516. if (obj.databaseType == DB_ACEBASE) {
  517. // AceBase
  518. // TODO
  519. } else if (obj.databaseType == DB_POSTGRESQL) {
  520. // PostgreSQL
  521. // TODO
  522. } else if (obj.databaseType == DB_MYSQL) {
  523. // MySQL
  524. // TODO
  525. } else if (obj.databaseType == DB_MARIADB) {
  526. // MariaDB
  527. // TODO
  528. } else if (obj.databaseType == DB_MONGODB) {
  529. // MongoDB
  530. obj.file.aggregate([{ "$group": { _id: "$type", count: { $sum: 1 } } }]).toArray(function (err, docs) {
  531. var counters = {}, totalCount = 0;
  532. if (err == null) { for (var i in docs) { if (docs[i]._id != null) { counters[docs[i]._id] = docs[i].count; totalCount += docs[i].count; } } }
  533. func(counters);
  534. });
  535. } else if (obj.databaseType == DB_MONGOJS) {
  536. // MongoJS
  537. obj.file.aggregate([{ "$group": { _id: "$type", count: { $sum: 1 } } }], function (err, docs) {
  538. var counters = {}, totalCount = 0;
  539. if (err == null) { for (var i in docs) { if (docs[i]._id != null) { counters[docs[i]._id] = docs[i].count; totalCount += docs[i].count; } } }
  540. func(counters);
  541. });
  542. } else if (obj.databaseType == DB_NEDB) {
  543. // NeDB version
  544. obj.file.count({ type: 'node' }, function (err, nodeCount) {
  545. obj.file.count({ type: 'mesh' }, function (err, meshCount) {
  546. obj.file.count({ type: 'user' }, function (err, userCount) {
  547. obj.file.count({ type: 'sysinfo' }, function (err, sysinfoCount) {
  548. obj.file.count({ type: 'note' }, function (err, noteCount) {
  549. obj.file.count({ type: 'iploc' }, function (err, iplocCount) {
  550. obj.file.count({ type: 'ifinfo' }, function (err, ifinfoCount) {
  551. obj.file.count({ type: 'cfile' }, function (err, cfileCount) {
  552. obj.file.count({ type: 'lastconnect' }, function (err, lastconnectCount) {
  553. obj.file.count({}, function (err, totalCount) {
  554. func({ node: nodeCount, mesh: meshCount, user: userCount, sysinfo: sysinfoCount, iploc: iplocCount, note: noteCount, ifinfo: ifinfoCount, cfile: cfileCount, lastconnect: lastconnectCount, total: totalCount });
  555. });
  556. });
  557. });
  558. });
  559. });
  560. });
  561. });
  562. });
  563. });
  564. });
  565. }
  566. }
  567. // This is used to rate limit a number of operation per day. Returns a startValue each new days, but you can substract it and save the value in the db.
  568. obj.getValueOfTheDay = function (id, startValue, func) { obj.Get(id, function (err, docs) { var date = new Date(), t = date.toLocaleDateString(); if ((err == null) && (docs.length == 1)) { var r = docs[0]; if (r.day == t) { func({ _id: id, value: r.value, day: t }); return; } } func({ _id: id, value: startValue, day: t }); }); };
  569. obj.escapeBase64 = function escapeBase64(val) { return (val.replace(/\+/g, '@').replace(/\//g, '$')); }
  570. // Encrypt an database object
  571. obj.performRecordEncryptionRecode = function (func) {
  572. var count = 0;
  573. obj.GetAllType('user', function (err, docs) {
  574. if (err != null) { parent.debug('db', 'ERROR (performRecordEncryptionRecode): ' + err); }
  575. if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
  576. obj.GetAllType('node', function (err, docs) {
  577. if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
  578. obj.GetAllType('mesh', function (err, docs) {
  579. if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
  580. if (obj.databaseType == DB_NEDB) { // If we are using NeDB, compact the database.
  581. obj.file.compactDatafile();
  582. obj.file.on('compaction.done', function () { func(count); }); // It's important to wait for compaction to finish before exit, otherwise NeDB may corrupt.
  583. } else {
  584. func(count); // For all other databases, normal exit.
  585. }
  586. });
  587. });
  588. });
  589. }
  590. // Encrypt an database object
  591. function performTypedRecordDecrypt(data) {
  592. if ((data == null) || (obj.dbRecordsDecryptKey == null) || (typeof data != 'object')) return data;
  593. for (var i in data) {
  594. if ((data[i] == null) || (typeof data[i] != 'object')) continue;
  595. data[i] = performPartialRecordDecrypt(data[i]);
  596. if ((data[i].intelamt != null) && (typeof data[i].intelamt == 'object') && (data[i].intelamt._CRYPT)) { data[i].intelamt = performPartialRecordDecrypt(data[i].intelamt); }
  597. if ((data[i].amt != null) && (typeof data[i].amt == 'object') && (data[i].amt._CRYPT)) { data[i].amt = performPartialRecordDecrypt(data[i].amt); }
  598. if ((data[i].kvm != null) && (typeof data[i].kvm == 'object') && (data[i].kvm._CRYPT)) { data[i].kvm = performPartialRecordDecrypt(data[i].kvm); }
  599. }
  600. return data;
  601. }
  602. // Encrypt an database object
  603. function performTypedRecordEncrypt(data) {
  604. if (obj.dbRecordsEncryptKey == null) return data;
  605. if (data.type == 'user') { return performPartialRecordEncrypt(Clone(data), ['otpkeys', 'otphkeys', 'otpsecret', 'salt', 'hash', 'oldpasswords']); }
  606. else if ((data.type == 'node') && (data.ssh || data.rdp || data.intelamt)) {
  607. var xdata = Clone(data);
  608. if (data.ssh || data.rdp) { xdata = performPartialRecordEncrypt(xdata, ['ssh', 'rdp']); }
  609. if (data.intelamt) { xdata.intelamt = performPartialRecordEncrypt(xdata.intelamt, ['pass', 'mpspass']); }
  610. return xdata;
  611. }
  612. else if ((data.type == 'mesh') && (data.amt || data.kvm)) {
  613. var xdata = Clone(data);
  614. if (data.amt) { xdata.amt = performPartialRecordEncrypt(xdata.amt, ['password']); }
  615. if (data.kvm) { xdata.kvm = performPartialRecordEncrypt(xdata.kvm, ['pass']); }
  616. return xdata;
  617. }
  618. return data;
  619. }
  620. // Encrypt an object and return a buffer.
  621. function performPartialRecordEncrypt(plainobj, encryptNames) {
  622. if (typeof plainobj != 'object') return plainobj;
  623. var enc = {}, enclen = 0;
  624. for (var i in encryptNames) { if (plainobj[encryptNames[i]] != null) { enclen++; enc[encryptNames[i]] = plainobj[encryptNames[i]]; delete plainobj[encryptNames[i]]; } }
  625. if (enclen > 0) { plainobj._CRYPT = performRecordEncrypt(enc); } else { delete plainobj._CRYPT; }
  626. return plainobj;
  627. }
  628. // Encrypt an object and return a buffer.
  629. function performPartialRecordDecrypt(plainobj) {
  630. if ((typeof plainobj != 'object') || (plainobj._CRYPT == null)) return plainobj;
  631. var enc = performRecordDecrypt(plainobj._CRYPT);
  632. if (enc != null) { for (var i in enc) { plainobj[i] = enc[i]; } }
  633. delete plainobj._CRYPT;
  634. return plainobj;
  635. }
  636. // Encrypt an object and return a base64.
  637. function performRecordEncrypt(plainobj) {
  638. if (obj.dbRecordsEncryptKey == null) return null;
  639. const iv = parent.crypto.randomBytes(12);
  640. const aes = parent.crypto.createCipheriv('aes-256-gcm', obj.dbRecordsEncryptKey, iv);
  641. var ciphertext = aes.update(JSON.stringify(plainobj));
  642. var cipherfinal = aes.final();
  643. ciphertext = Buffer.concat([iv, aes.getAuthTag(), ciphertext, cipherfinal]);
  644. return ciphertext.toString('base64');
  645. }
  646. // Takes a base64 and return an object.
  647. function performRecordDecrypt(ciphertext) {
  648. if (obj.dbRecordsDecryptKey == null) return null;
  649. const ciphertextBytes = Buffer.from(ciphertext, 'base64');
  650. const iv = ciphertextBytes.slice(0, 12);
  651. const data = ciphertextBytes.slice(28);
  652. const aes = parent.crypto.createDecipheriv('aes-256-gcm', obj.dbRecordsDecryptKey, iv);
  653. aes.setAuthTag(ciphertextBytes.slice(12, 28));
  654. var plaintextBytes, r;
  655. try {
  656. plaintextBytes = Buffer.from(aes.update(data));
  657. plaintextBytes = Buffer.concat([plaintextBytes, aes.final()]);
  658. r = JSON.parse(plaintextBytes.toString());
  659. } catch (e) { throw "Incorrect DbRecordsDecryptKey/DbRecordsEncryptKey or invalid database _CRYPT data: " + e; }
  660. return r;
  661. }
  662. // Clone an object (TODO: Make this more efficient)
  663. function Clone(v) { return JSON.parse(JSON.stringify(v)); }
  664. // Read expiration time from configuration file
  665. if (typeof parent.args.dbexpire == 'object') {
  666. if (typeof parent.args.dbexpire.events == 'number') { expireEventsSeconds = parent.args.dbexpire.events; }
  667. if (typeof parent.args.dbexpire.powerevents == 'number') { expirePowerEventsSeconds = parent.args.dbexpire.powerevents; }
  668. if (typeof parent.args.dbexpire.statsevents == 'number') { expireServerStatsSeconds = parent.args.dbexpire.statsevents; }
  669. }
  670. // If a DB record encryption key is provided, perform database record encryption
  671. if ((typeof parent.args.dbrecordsencryptkey == 'string') && (parent.args.dbrecordsencryptkey.length != 0)) {
  672. // Hash the database password into a AES256 key and setup encryption and decryption.
  673. obj.dbRecordsEncryptKey = obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsencryptkey).digest('raw').slice(0, 32);
  674. }
  675. // If a DB record decryption key is provided, perform database record decryption
  676. if ((typeof parent.args.dbrecordsdecryptkey == 'string') && (parent.args.dbrecordsdecryptkey.length != 0)) {
  677. // Hash the database password into a AES256 key and setup encryption and decryption.
  678. obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsdecryptkey).digest('raw').slice(0, 32);
  679. }
  680. function createTablesIfNotExist(dbname) {
  681. var useDatabase = 'USE ' + dbname;
  682. sqlDbQuery(useDatabase, null, function (err, docs) {
  683. if (err != null) {
  684. console.log("Unable to connect to database: " + err);
  685. process.exit();
  686. }
  687. if (err == null) {
  688. parent.debug('db', 'Checking tables...');
  689. sqlDbBatchExec([
  690. 'CREATE TABLE IF NOT EXISTS main (id VARCHAR(256) NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
  691. 'CREATE TABLE IF NOT EXISTS events (id INT NOT NULL AUTO_INCREMENT, time DATETIME, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK(json_valid(doc)))',
  692. 'CREATE TABLE IF NOT EXISTS eventids (fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
  693. 'CREATE TABLE IF NOT EXISTS serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
  694. 'CREATE TABLE IF NOT EXISTS power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
  695. 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
  696. 'CREATE TABLE IF NOT EXISTS plugin (id INT NOT NULL AUTO_INCREMENT, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))'
  697. ], function (err) {
  698. parent.debug('db', 'Checking indexes...');
  699. sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
  700. sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
  701. sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
  702. sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
  703. sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
  704. sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
  705. sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
  706. sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
  707. sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
  708. sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
  709. sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
  710. sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
  711. setupFunctions(func);
  712. });
  713. }
  714. });
  715. }
  716. if (parent.args.sqlite3) {
  717. // SQLite3 database setup
  718. obj.databaseType = DB_SQLITE;
  719. const sqlite3 = require('sqlite3');
  720. let configParams = parent.config.settings.sqlite3;
  721. if (typeof configParams == 'string') {databaseName = configParams} else {databaseName = configParams.name ? configParams.name : 'meshcentral';};
  722. obj.sqliteConfig.startupVacuum = configParams.startupvacuum ? configParams.startupvacuum : false;
  723. obj.sqliteConfig.autoVacuum = configParams.autovacuum ? configParams.autovacuum.toLowerCase() : 'incremental';
  724. obj.sqliteConfig.incrementalVacuum = configParams.incrementalvacuum ? configParams.incrementalvacuum : 100;
  725. obj.sqliteConfig.journalMode = configParams.journalmode ? configParams.journalmode.toLowerCase() : 'delete';
  726. //allowed modes, 'none' excluded because not usefull for this app, maybe also remove 'memory'?
  727. if (!(['delete', 'truncate', 'persist', 'memory', 'wal'].includes(obj.sqliteConfig.journalMode))) { obj.sqliteConfig.journalMode = 'delete'};
  728. obj.sqliteConfig.journalSize = configParams.journalsize ? configParams.journalsize : 409600;
  729. //wal can use the more performant 'normal' mode, see https://www.sqlite.org/pragma.html#pragma_synchronous
  730. obj.sqliteConfig.synchronous = (obj.sqliteConfig.journalMode == 'wal') ? 'normal' : 'full';
  731. if (obj.sqliteConfig.journalMode == 'wal') {obj.sqliteConfig.maintenance += 'PRAGMA wal_checkpoint(PASSIVE);'};
  732. if (obj.sqliteConfig.autoVacuum == 'incremental') {obj.sqliteConfig.maintenance += 'PRAGMA incremental_vacuum(' + obj.sqliteConfig.incrementalVacuum + ');'};
  733. obj.sqliteConfig.maintenance += 'PRAGMA optimize;';
  734. parent.debug('db', 'SQlite config options: ' + JSON.stringify(obj.sqliteConfig, null, 4));
  735. if (obj.sqliteConfig.journalMode == 'memory') { console.log('[WARNING] journal_mode=memory: this can lead to database corruption if there is a crash during a transaction. See https://www.sqlite.org/pragma.html#pragma_journal_mode') };
  736. //.cached not usefull
  737. obj.file = new sqlite3.Database(path.join(parent.datapath, databaseName + '.sqlite'), sqlite3.OPEN_READWRITE, function (err) {
  738. if (err && (err.code == 'SQLITE_CANTOPEN')) {
  739. // Database needs to be created
  740. obj.file = new sqlite3.Database(path.join(parent.datapath, databaseName + '.sqlite'), function (err) {
  741. if (err) { console.log("SQLite Error: " + err); process.exit(1); }
  742. obj.file.exec(`
  743. CREATE TABLE main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON);
  744. CREATE TABLE events(id INTEGER PRIMARY KEY, time TIMESTAMP, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON);
  745. CREATE TABLE eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT);
  746. CREATE TABLE serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON);
  747. CREATE TABLE power (id INTEGER PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON);
  748. CREATE TABLE smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON);
  749. CREATE TABLE plugin (id INTEGER PRIMARY KEY, doc JSON);
  750. CREATE INDEX ndxtypedomainextra ON main (type, domain, extra);
  751. CREATE INDEX ndxextra ON main (extra);
  752. CREATE INDEX ndxextraex ON main (extraex);
  753. CREATE INDEX ndxeventstime ON events(time);
  754. CREATE INDEX ndxeventsusername ON events(domain, userid, time);
  755. CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time);
  756. CREATE INDEX ndxeventids ON eventids(target);
  757. CREATE INDEX ndxserverstattime ON serverstats (time);
  758. CREATE INDEX ndxserverstatexpire ON serverstats (expire);
  759. CREATE INDEX ndxpowernodeidtime ON power (nodeid, time);
  760. CREATE INDEX ndxsmbiostime ON smbios (time);
  761. CREATE INDEX ndxsmbiosexpire ON smbios (expire);
  762. `, function (err) {
  763. // Completed DB creation of SQLite3
  764. sqliteSetOptions(func);
  765. //setupFunctions could be put in the sqliteSetupOptions, but left after it for clarity
  766. setupFunctions(func);
  767. }
  768. );
  769. });
  770. return;
  771. } else if (err) { console.log("SQLite Error: " + err); process.exit(0); }
  772. //for existing db's
  773. sqliteSetOptions();
  774. //setupFunctions could be put in the sqliteSetupOptions, but left after it for clarity
  775. setupFunctions(func);
  776. });
  777. } else if (parent.args.acebase) {
  778. // AceBase database setup
  779. obj.databaseType = DB_ACEBASE;
  780. const { AceBase } = require('acebase');
  781. // For information on AceBase sponsor: https://github.com/appy-one/acebase/discussions/100
  782. obj.file = new AceBase('meshcentral', { sponsor: ((typeof parent.args.acebase == 'object') && (parent.args.acebase.sponsor)), logLevel: 'error', storage: { path: parent.datapath } });
  783. // Get all the databases ready
  784. obj.file.ready(function () {
  785. // Create AceBase indexes
  786. obj.file.indexes.create('meshcentral', 'type', { include: ['domain', 'meshid'] });
  787. obj.file.indexes.create('meshcentral', 'email');
  788. obj.file.indexes.create('meshcentral', 'meshid');
  789. obj.file.indexes.create('meshcentral', 'intelamt.uuid');
  790. obj.file.indexes.create('events', 'userid', { include: ['action'] });
  791. obj.file.indexes.create('events', 'domain', { include: ['nodeid', 'time'] });
  792. obj.file.indexes.create('events', 'ids', { include: ['time'] });
  793. obj.file.indexes.create('events', 'time');
  794. obj.file.indexes.create('power', 'nodeid', { include: ['time'] });
  795. obj.file.indexes.create('power', 'time');
  796. obj.file.indexes.create('stats', 'time');
  797. obj.file.indexes.create('stats', 'expire');
  798. // Completed setup of AceBase
  799. setupFunctions(func);
  800. });
  801. } else if (parent.args.mariadb || parent.args.mysql) {
  802. var connectinArgs = (parent.args.mariadb) ? parent.args.mariadb : parent.args.mysql;
  803. if (typeof connectinArgs == 'string') {
  804. const parts = connectinArgs.split(/[:@/]+/);
  805. var connectionObject = {
  806. "user": parts[1],
  807. "password": parts[2],
  808. "host": parts[3],
  809. "port": parts[4],
  810. "database": parts[5]
  811. };
  812. var dbname = (connectionObject.database != null) ? connectionObject.database : 'meshcentral';
  813. } else {
  814. var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
  815. // Including the db name in the connection obj will cause a connection faliure if it does not exist
  816. var connectionObject = Clone(connectinArgs);
  817. delete connectionObject.database;
  818. try {
  819. if (connectinArgs.ssl) {
  820. if (connectinArgs.ssl.dontcheckserveridentity == true) { connectionObject.ssl.checkServerIdentity = function (name, cert) { return undefined; } };
  821. if (connectinArgs.ssl.cacertpath) { connectionObject.ssl.ca = [require('fs').readFileSync(connectinArgs.ssl.cacertpath, 'utf8')]; }
  822. if (connectinArgs.ssl.clientcertpath) { connectionObject.ssl.cert = [require('fs').readFileSync(connectinArgs.ssl.clientcertpath, 'utf8')]; }
  823. if (connectinArgs.ssl.clientkeypath) { connectionObject.ssl.key = [require('fs').readFileSync(connectinArgs.ssl.clientkeypath, 'utf8')]; }
  824. }
  825. } catch (ex) {
  826. console.log('Error loading SQL Connector certificate: ' + ex);
  827. process.exit();
  828. }
  829. }
  830. if (parent.args.mariadb) {
  831. // Use MariaDB
  832. obj.databaseType = DB_MARIADB;
  833. var tempDatastore = require('mariadb').createPool(connectionObject);
  834. tempDatastore.getConnection().then(function (conn) {
  835. conn.query('CREATE DATABASE IF NOT EXISTS ' + dbname).then(function (result) {
  836. conn.release();
  837. }).catch(function (ex) { console.log('Auto-create database failed: ' + ex); });
  838. }).catch(function (ex) { console.log('Auto-create database failed: ' + ex); });
  839. setTimeout(function () { tempDatastore.end(); }, 2000);
  840. connectionObject.database = dbname;
  841. Datastore = require('mariadb').createPool(connectionObject);
  842. createTablesIfNotExist(dbname);
  843. } else if (parent.args.mysql) {
  844. // Use MySQL
  845. obj.databaseType = DB_MYSQL;
  846. var tempDatastore = require('mysql2').createPool(connectionObject);
  847. tempDatastore.query('CREATE DATABASE IF NOT EXISTS ' + dbname, function (error) {
  848. if (error != null) {
  849. console.log('Auto-create database failed: ' + error);
  850. }
  851. connectionObject.database = dbname;
  852. Datastore = require('mysql2').createPool(connectionObject);
  853. createTablesIfNotExist(dbname);
  854. });
  855. setTimeout(function () { tempDatastore.end(); }, 2000);
  856. }
  857. } else if (parent.args.postgres) {
  858. // Postgres SQL
  859. let connectionArgs = parent.args.postgres;
  860. connectionArgs.database = (databaseName = (connectionArgs.database != null) ? connectionArgs.database : 'meshcentral');
  861. let DatastoreTest;
  862. obj.databaseType = DB_POSTGRESQL;
  863. const { Client } = require('pg');
  864. Datastore = new Client(connectionArgs);
  865. // Check if we should skip database creation check
  866. if (connectionArgs.createdatabase === false ) {
  867. // Skip database check/creation, just connect and run the SELECT query
  868. Datastore.connect();
  869. Datastore.query('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, res) {
  870. if (err == null) {
  871. (res.rowCount == 0) ? postgreSqlCreateTables(func) : setupFunctions(func);
  872. } else if (err.code == '42P01') { //42P01 = undefined table
  873. postgreSqlCreateTables(func);
  874. } else {
  875. console.log('Postgresql connection error: ', err.message);
  876. process.exit(0);
  877. }
  878. });
  879. } else {
  880. //Connect to and check pg db first to check if own db exists. Otherwise errors out on 'database does not exist'
  881. connectionArgs.database = 'postgres';
  882. DatastoreTest = new Client(connectionArgs);
  883. DatastoreTest.connect();
  884. connectionArgs.database = databaseName; //put the name back for backupconfig info
  885. DatastoreTest.query('SELECT 1 FROM pg_catalog.pg_database WHERE datname = $1', [databaseName], function (err, res) { // check database exists first before creating
  886. if (res.rowCount != 0) { // database exists now check tables exists
  887. DatastoreTest.end();
  888. Datastore.connect();
  889. Datastore.query('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, res) {
  890. if (err == null) {
  891. (res.rowCount ==0) ? postgreSqlCreateTables(func) : setupFunctions(func)
  892. } else
  893. if (err.code == '42P01') { //42P01 = undefined table, https://www.postgresql.org/docs/current/errcodes-appendix.html
  894. postgreSqlCreateTables(func);
  895. } else {
  896. console.log('Postgresql database exists, other error: ', err.message); process.exit(0);
  897. };
  898. });
  899. } else { // If not present, create the tables and indexes
  900. //not needed, just use a create db statement: const pgtools = require('pgtools');
  901. DatastoreTest.query('CREATE DATABASE "'+ databaseName + '";', [], function (err, res) {
  902. if (err == null) {
  903. // Create the tables and indexes
  904. DatastoreTest.end();
  905. Datastore.connect();
  906. postgreSqlCreateTables(func);
  907. } else {
  908. console.log('Postgresql database create error: ', err.message);
  909. process.exit(0);
  910. }
  911. });
  912. }
  913. });
  914. }
  915. } else if (parent.args.mongodb) {
  916. // Use MongoDB
  917. obj.databaseType = DB_MONGODB;
  918. // If running an older NodeJS version, TextEncoder/TextDecoder is required
  919. if (global.TextEncoder == null) { global.TextEncoder = require('util').TextEncoder; }
  920. if (global.TextDecoder == null) { global.TextDecoder = require('util').TextDecoder; }
  921. require('mongodb').MongoClient.connect(parent.args.mongodb, { useNewUrlParser: true, useUnifiedTopology: true, enableUtf8Validation: false }, function (err, client) {
  922. if (err != null) { console.log("Unable to connect to database: " + err); process.exit(); return; }
  923. Datastore = client;
  924. parent.debug('db', 'Connected to MongoDB database...');
  925. // Get the database name and setup the database client
  926. var dbname = 'meshcentral';
  927. if (parent.args.mongodbname) { dbname = parent.args.mongodbname; }
  928. const dbcollectionname = (parent.args.mongodbcol) ? (parent.args.mongodbcol) : 'meshcentral';
  929. const db = client.db(dbname);
  930. // Check the database version
  931. db.admin().serverInfo(function (err, info) {
  932. if ((err != null) || (info == null) || (info.versionArray == null) || (Array.isArray(info.versionArray) == false) || (info.versionArray.length < 2) || (typeof info.versionArray[0] != 'number') || (typeof info.versionArray[1] != 'number')) {
  933. console.log('WARNING: Unable to check MongoDB version.');
  934. } else {
  935. if ((info.versionArray[0] < 3) || ((info.versionArray[0] == 3) && (info.versionArray[1] < 6))) {
  936. // We are running with mongoDB older than 3.6, this is not good.
  937. parent.addServerWarning("Current version of MongoDB (" + info.version + ") is too old, please upgrade to MongoDB 3.6 or better.", true);
  938. }
  939. }
  940. });
  941. // Setup MongoDB main collection and indexes
  942. obj.file = db.collection(dbcollectionname);
  943. obj.file.indexes(function (err, indexes) {
  944. // Check if we need to reset indexes
  945. var indexesByName = {}, indexCount = 0;
  946. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  947. if ((indexCount != 5) || (indexesByName['TypeDomainMesh1'] == null) || (indexesByName['Email1'] == null) || (indexesByName['Mesh1'] == null) || (indexesByName['AmtUuid1'] == null)) {
  948. console.log('Resetting main indexes...');
  949. obj.file.dropIndexes(function (err) {
  950. obj.file.createIndex({ type: 1, domain: 1, meshid: 1 }, { sparse: 1, name: 'TypeDomainMesh1' }); // Speeds up GetAllTypeNoTypeField() and GetAllTypeNoTypeFieldMeshFiltered()
  951. obj.file.createIndex({ email: 1 }, { sparse: 1, name: 'Email1' }); // Speeds up GetUserWithEmail() and GetUserWithVerifiedEmail()
  952. obj.file.createIndex({ meshid: 1 }, { sparse: 1, name: 'Mesh1' }); // Speeds up RemoveMesh()
  953. obj.file.createIndex({ 'intelamt.uuid': 1 }, { sparse: 1, name: 'AmtUuid1' }); // Speeds up getAmtUuidMeshNode()
  954. });
  955. }
  956. });
  957. // Setup the changeStream on the MongoDB main collection if possible
  958. if (parent.args.mongodbchangestream == true) {
  959. obj.dbCounters.changeStream = { change: 0, update: 0, insert: 0, delete: 0 };
  960. if (typeof obj.file.watch != 'function') {
  961. console.log('WARNING: watch() is not a function, MongoDB ChangeStream not supported.');
  962. } else {
  963. obj.fileChangeStream = obj.file.watch([{ $match: { $or: [{ 'fullDocument.type': { $in: ['node', 'mesh', 'user', 'ugrp'] } }, { 'operationType': 'delete' }] } }], { fullDocument: 'updateLookup' });
  964. obj.fileChangeStream.on('change', function (change) {
  965. obj.dbCounters.changeStream.change++;
  966. if ((change.operationType == 'update') || (change.operationType == 'replace')) {
  967. obj.dbCounters.changeStream.update++;
  968. switch (change.fullDocument.type) {
  969. case 'node': { dbNodeChange(change, false); break; } // A node has changed
  970. case 'mesh': { dbMeshChange(change, false); break; } // A device group has changed
  971. case 'user': { dbUserChange(change, false); break; } // A user account has changed
  972. case 'ugrp': { dbUGrpChange(change, false); break; } // A user account has changed
  973. }
  974. } else if (change.operationType == 'insert') {
  975. obj.dbCounters.changeStream.insert++;
  976. switch (change.fullDocument.type) {
  977. case 'node': { dbNodeChange(change, true); break; } // A node has added
  978. case 'mesh': { dbMeshChange(change, true); break; } // A device group has created
  979. case 'user': { dbUserChange(change, true); break; } // A user account has created
  980. case 'ugrp': { dbUGrpChange(change, true); break; } // A user account has created
  981. }
  982. } else if (change.operationType == 'delete') {
  983. obj.dbCounters.changeStream.delete++;
  984. if ((change.documentKey == null) || (change.documentKey._id == null)) return;
  985. var splitId = change.documentKey._id.split('/');
  986. switch (splitId[0]) {
  987. case 'node': {
  988. //Not Good: Problem here is that we don't know what meshid the node belonged to before the delete.
  989. //parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: 'removenode', nodeid: change.documentKey._id, domain: splitId[1] });
  990. break;
  991. }
  992. case 'mesh': {
  993. parent.DispatchEvent(['*', change.documentKey._id], obj, { etype: 'mesh', action: 'deletemesh', meshid: change.documentKey._id, domain: splitId[1] });
  994. break;
  995. }
  996. case 'user': {
  997. //Not Good: This is not a perfect user removal because we don't know what groups the user was in.
  998. //parent.DispatchEvent(['*', 'server-users'], obj, { etype: 'user', action: 'accountremove', userid: change.documentKey._id, domain: splitId[1], username: splitId[2] });
  999. break;
  1000. }
  1001. case 'ugrp': {
  1002. parent.DispatchEvent(['*', change.documentKey._id], obj, { etype: 'ugrp', action: 'deleteusergroup', ugrpid: change.documentKey._id, domain: splitId[1] });
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. });
  1008. obj.changeStream = true;
  1009. }
  1010. }
  1011. // Setup MongoDB events collection and indexes
  1012. obj.eventsfile = db.collection('events'); // Collection containing all events
  1013. obj.eventsfile.indexes(function (err, indexes) {
  1014. // Check if we need to reset indexes
  1015. var indexesByName = {}, indexCount = 0;
  1016. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1017. if ((indexCount != 5) || (indexesByName['UseridAction1'] == null) || (indexesByName['DomainNodeTime1'] == null) || (indexesByName['IdsAndTime1'] == null) || (indexesByName['ExpireTime1'] == null)) {
  1018. // Reset all indexes
  1019. console.log("Resetting events indexes...");
  1020. obj.eventsfile.dropIndexes(function (err) {
  1021. obj.eventsfile.createIndex({ userid: 1, action: 1 }, { sparse: 1, name: 'UseridAction1' });
  1022. obj.eventsfile.createIndex({ domain: 1, nodeid: 1, time: -1 }, { sparse: 1, name: 'DomainNodeTime1' });
  1023. obj.eventsfile.createIndex({ ids: 1, time: -1 }, { sparse: 1, name: 'IdsAndTime1' });
  1024. obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
  1025. });
  1026. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireEventsSeconds) {
  1027. // Reset the timeout index
  1028. console.log("Resetting events expire index...");
  1029. obj.eventsfile.dropIndex('ExpireTime1', function (err) {
  1030. obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
  1031. });
  1032. }
  1033. });
  1034. // Setup MongoDB power events collection and indexes
  1035. obj.powerfile = db.collection('power'); // Collection containing all power events
  1036. obj.powerfile.indexes(function (err, indexes) {
  1037. // Check if we need to reset indexes
  1038. var indexesByName = {}, indexCount = 0;
  1039. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1040. if ((indexCount != 3) || (indexesByName['NodeIdAndTime1'] == null) || (indexesByName['ExpireTime1'] == null)) {
  1041. // Reset all indexes
  1042. console.log("Resetting power events indexes...");
  1043. obj.powerfile.dropIndexes(function (err) {
  1044. // Create all indexes
  1045. obj.powerfile.createIndex({ nodeid: 1, time: 1 }, { sparse: 1, name: 'NodeIdAndTime1' });
  1046. obj.powerfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expirePowerEventsSeconds, name: 'ExpireTime1' });
  1047. });
  1048. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expirePowerEventsSeconds) {
  1049. // Reset the timeout index
  1050. console.log("Resetting power events expire index...");
  1051. obj.powerfile.dropIndex('ExpireTime1', function (err) {
  1052. // Reset the expire power events index
  1053. obj.powerfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expirePowerEventsSeconds, name: 'ExpireTime1' });
  1054. });
  1055. }
  1056. });
  1057. // Setup MongoDB smbios collection, no indexes needed
  1058. obj.smbiosfile = db.collection('smbios'); // Collection containing all smbios information
  1059. // Setup MongoDB server stats collection
  1060. obj.serverstatsfile = db.collection('serverstats'); // Collection of server stats
  1061. obj.serverstatsfile.indexes(function (err, indexes) {
  1062. // Check if we need to reset indexes
  1063. var indexesByName = {}, indexCount = 0;
  1064. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1065. if ((indexCount != 3) || (indexesByName['ExpireTime1'] == null)) {
  1066. // Reset all indexes
  1067. console.log("Resetting server stats indexes...");
  1068. obj.serverstatsfile.dropIndexes(function (err) {
  1069. // Create all indexes
  1070. obj.serverstatsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireServerStatsSeconds, name: 'ExpireTime1' });
  1071. obj.serverstatsfile.createIndex({ 'expire': 1 }, { expireAfterSeconds: 0, name: 'ExpireTime2' }); // Auto-expire events
  1072. });
  1073. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireServerStatsSeconds) {
  1074. // Reset the timeout index
  1075. console.log("Resetting server stats expire index...");
  1076. obj.serverstatsfile.dropIndex('ExpireTime1', function (err) {
  1077. // Reset the expire server stats index
  1078. obj.serverstatsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireServerStatsSeconds, name: 'ExpireTime1' });
  1079. });
  1080. }
  1081. });
  1082. // Setup plugin info collection
  1083. if (obj.pluginsActive) { obj.pluginsfile = db.collection('plugins'); }
  1084. setupFunctions(func); // Completed setup of MongoDB
  1085. });
  1086. } else if (parent.args.xmongodb) {
  1087. // Use MongoJS, this is the old system.
  1088. obj.databaseType = DB_MONGOJS;
  1089. Datastore = require('mongojs');
  1090. var db = Datastore(parent.args.xmongodb);
  1091. var dbcollection = 'meshcentral';
  1092. if (parent.args.mongodbcol) { dbcollection = parent.args.mongodbcol; }
  1093. // Setup MongoDB main collection and indexes
  1094. obj.file = db.collection(dbcollection);
  1095. obj.file.getIndexes(function (err, indexes) {
  1096. // Check if we need to reset indexes
  1097. var indexesByName = {}, indexCount = 0;
  1098. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1099. if ((indexCount != 5) || (indexesByName['TypeDomainMesh1'] == null) || (indexesByName['Email1'] == null) || (indexesByName['Mesh1'] == null) || (indexesByName['AmtUuid1'] == null)) {
  1100. console.log("Resetting main indexes...");
  1101. obj.file.dropIndexes(function (err) {
  1102. obj.file.createIndex({ type: 1, domain: 1, meshid: 1 }, { sparse: 1, name: 'TypeDomainMesh1' }); // Speeds up GetAllTypeNoTypeField() and GetAllTypeNoTypeFieldMeshFiltered()
  1103. obj.file.createIndex({ email: 1 }, { sparse: 1, name: 'Email1' }); // Speeds up GetUserWithEmail() and GetUserWithVerifiedEmail()
  1104. obj.file.createIndex({ meshid: 1 }, { sparse: 1, name: 'Mesh1' }); // Speeds up RemoveMesh()
  1105. obj.file.createIndex({ 'intelamt.uuid': 1 }, { sparse: 1, name: 'AmtUuid1' }); // Speeds up getAmtUuidMeshNode()
  1106. });
  1107. }
  1108. });
  1109. // Setup MongoDB events collection and indexes
  1110. obj.eventsfile = db.collection('events'); // Collection containing all events
  1111. obj.eventsfile.getIndexes(function (err, indexes) {
  1112. // Check if we need to reset indexes
  1113. var indexesByName = {}, indexCount = 0;
  1114. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1115. if ((indexCount != 5) || (indexesByName['UseridAction1'] == null) || (indexesByName['DomainNodeTime1'] == null) || (indexesByName['IdsAndTime1'] == null) || (indexesByName['ExpireTime1'] == null)) {
  1116. // Reset all indexes
  1117. console.log("Resetting events indexes...");
  1118. obj.eventsfile.dropIndexes(function (err) {
  1119. obj.eventsfile.createIndex({ userid: 1, action: 1 }, { sparse: 1, name: 'UseridAction1' });
  1120. obj.eventsfile.createIndex({ domain: 1, nodeid: 1, time: -1 }, { sparse: 1, name: 'DomainNodeTime1' });
  1121. obj.eventsfile.createIndex({ ids: 1, time: -1 }, { sparse: 1, name: 'IdsAndTime1' });
  1122. obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
  1123. });
  1124. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireEventsSeconds) {
  1125. // Reset the timeout index
  1126. console.log("Resetting events expire index...");
  1127. obj.eventsfile.dropIndex('ExpireTime1', function (err) {
  1128. obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
  1129. });
  1130. }
  1131. });
  1132. // Setup MongoDB power events collection and indexes
  1133. obj.powerfile = db.collection('power'); // Collection containing all power events
  1134. obj.powerfile.getIndexes(function (err, indexes) {
  1135. // Check if we need to reset indexes
  1136. var indexesByName = {}, indexCount = 0;
  1137. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1138. if ((indexCount != 3) || (indexesByName['NodeIdAndTime1'] == null) || (indexesByName['ExpireTime1'] == null)) {
  1139. // Reset all indexes
  1140. console.log("Resetting power events indexes...");
  1141. obj.powerfile.dropIndexes(function (err) {
  1142. // Create all indexes
  1143. obj.powerfile.createIndex({ nodeid: 1, time: 1 }, { sparse: 1, name: 'NodeIdAndTime1' });
  1144. obj.powerfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expirePowerEventsSeconds, name: 'ExpireTime1' });
  1145. });
  1146. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expirePowerEventsSeconds) {
  1147. // Reset the timeout index
  1148. console.log("Resetting power events expire index...");
  1149. obj.powerfile.dropIndex('ExpireTime1', function (err) {
  1150. // Reset the expire power events index
  1151. obj.powerfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expirePowerEventsSeconds, name: 'ExpireTime1' });
  1152. });
  1153. }
  1154. });
  1155. // Setup MongoDB smbios collection, no indexes needed
  1156. obj.smbiosfile = db.collection('smbios'); // Collection containing all smbios information
  1157. // Setup MongoDB server stats collection
  1158. obj.serverstatsfile = db.collection('serverstats'); // Collection of server stats
  1159. obj.serverstatsfile.getIndexes(function (err, indexes) {
  1160. // Check if we need to reset indexes
  1161. var indexesByName = {}, indexCount = 0;
  1162. for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
  1163. if ((indexCount != 3) || (indexesByName['ExpireTime1'] == null)) {
  1164. // Reset all indexes
  1165. console.log("Resetting server stats indexes...");
  1166. obj.serverstatsfile.dropIndexes(function (err) {
  1167. // Create all indexes
  1168. obj.serverstatsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireServerStatsSeconds, name: 'ExpireTime1' });
  1169. obj.serverstatsfile.createIndex({ 'expire': 1 }, { expireAfterSeconds: 0, name: 'ExpireTime2' }); // Auto-expire events
  1170. });
  1171. } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireServerStatsSeconds) {
  1172. // Reset the timeout index
  1173. console.log("Resetting server stats expire index...");
  1174. obj.serverstatsfile.dropIndex('ExpireTime1', function (err) {
  1175. // Reset the expire server stats index
  1176. obj.serverstatsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireServerStatsSeconds, name: 'ExpireTime1' });
  1177. });
  1178. }
  1179. });
  1180. // Setup plugin info collection
  1181. if (obj.pluginsActive) { obj.pluginsfile = db.collection('plugins'); }
  1182. setupFunctions(func); // Completed setup of MongoJS
  1183. } else {
  1184. // Use NeDB (The default)
  1185. obj.databaseType = DB_NEDB;
  1186. try { Datastore = require('@seald-io/nedb'); } catch (ex) { } // This is the NeDB with Node 23 support.
  1187. if (Datastore == null) {
  1188. try { Datastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
  1189. if (Datastore == null) { Datastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
  1190. }
  1191. var datastoreOptions = { filename: parent.getConfigFilePath('meshcentral.db'), autoload: true };
  1192. // If a DB encryption key is provided, perform database encryption
  1193. if ((typeof parent.args.dbencryptkey == 'string') && (parent.args.dbencryptkey.length != 0)) {
  1194. // Hash the database password into a AES256 key and setup encryption and decryption.
  1195. obj.dbKey = parent.crypto.createHash('sha384').update(parent.args.dbencryptkey).digest('raw').slice(0, 32);
  1196. datastoreOptions.afterSerialization = function (plaintext) {
  1197. const iv = parent.crypto.randomBytes(16);
  1198. const aes = parent.crypto.createCipheriv('aes-256-cbc', obj.dbKey, iv);
  1199. var ciphertext = aes.update(plaintext);
  1200. ciphertext = Buffer.concat([iv, ciphertext, aes.final()]);
  1201. return ciphertext.toString('base64');
  1202. }
  1203. datastoreOptions.beforeDeserialization = function (ciphertext) {
  1204. const ciphertextBytes = Buffer.from(ciphertext, 'base64');
  1205. const iv = ciphertextBytes.slice(0, 16);
  1206. const data = ciphertextBytes.slice(16);
  1207. const aes = parent.crypto.createDecipheriv('aes-256-cbc', obj.dbKey, iv);
  1208. var plaintextBytes = Buffer.from(aes.update(data));
  1209. plaintextBytes = Buffer.concat([plaintextBytes, aes.final()]);
  1210. return plaintextBytes.toString();
  1211. }
  1212. }
  1213. // Start NeDB main collection and setup indexes
  1214. obj.file = new Datastore(datastoreOptions);
  1215. obj.file.setAutocompactionInterval(86400000); // Compact once a day
  1216. obj.file.ensureIndex({ fieldName: 'type' });
  1217. obj.file.ensureIndex({ fieldName: 'domain' });
  1218. obj.file.ensureIndex({ fieldName: 'meshid', sparse: true });
  1219. obj.file.ensureIndex({ fieldName: 'nodeid', sparse: true });
  1220. obj.file.ensureIndex({ fieldName: 'email', sparse: true });
  1221. // Setup the events collection and setup indexes
  1222. obj.eventsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-events.db'), autoload: true, corruptAlertThreshold: 1 });
  1223. obj.eventsfile.setAutocompactionInterval(86400000); // Compact once a day
  1224. obj.eventsfile.ensureIndex({ fieldName: 'ids' }); // TODO: Not sure if this is a good index, this is a array field.
  1225. obj.eventsfile.ensureIndex({ fieldName: 'nodeid', sparse: true });
  1226. obj.eventsfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expireEventsSeconds });
  1227. obj.eventsfile.remove({ time: { '$lt': new Date(Date.now() - (expireEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  1228. // Setup the power collection and setup indexes
  1229. obj.powerfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-power.db'), autoload: true, corruptAlertThreshold: 1 });
  1230. obj.powerfile.setAutocompactionInterval(86400000); // Compact once a day
  1231. obj.powerfile.ensureIndex({ fieldName: 'nodeid' });
  1232. obj.powerfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expirePowerEventsSeconds });
  1233. obj.powerfile.remove({ time: { '$lt': new Date(Date.now() - (expirePowerEventsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  1234. // Setup the SMBIOS collection, for NeDB we don't setup SMBIOS since NeDB will corrupt the database. Remove any existing ones.
  1235. //obj.smbiosfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-smbios.db'), autoload: true, corruptAlertThreshold: 1 });
  1236. fs.unlink(parent.getConfigFilePath('meshcentral-smbios.db'), function () { });
  1237. // Setup the server stats collection and setup indexes
  1238. obj.serverstatsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-stats.db'), autoload: true, corruptAlertThreshold: 1 });
  1239. obj.serverstatsfile.setAutocompactionInterval(86400000); // Compact once a day
  1240. obj.serverstatsfile.ensureIndex({ fieldName: 'time', expireAfterSeconds: expireServerStatsSeconds });
  1241. obj.serverstatsfile.ensureIndex({ fieldName: 'expire', expireAfterSeconds: 0 }); // Auto-expire events
  1242. obj.serverstatsfile.remove({ time: { '$lt': new Date(Date.now() - (expireServerStatsSeconds * 1000)) } }, { multi: true }); // Force delete older events
  1243. // Setup plugin info collection
  1244. if (obj.pluginsActive) {
  1245. obj.pluginsfile = new Datastore({ filename: parent.getConfigFilePath('meshcentral-plugins.db'), autoload: true });
  1246. obj.pluginsfile.setAutocompactionInterval(86400000); // Compact once a day
  1247. }
  1248. setupFunctions(func); // Completed setup of NeDB
  1249. }
  1250. function sqliteSetOptions(func) {
  1251. //get current auto_vacuum mode for comparison
  1252. obj.file.get('PRAGMA auto_vacuum;', function(err, current){
  1253. let pragma = 'PRAGMA journal_mode=' + obj.sqliteConfig.journalMode + ';' +
  1254. 'PRAGMA synchronous='+ obj.sqliteConfig.synchronous + ';' +
  1255. 'PRAGMA journal_size_limit=' + obj.sqliteConfig.journalSize + ';' +
  1256. 'PRAGMA auto_vacuum=' + obj.sqliteConfig.autoVacuum + ';' +
  1257. 'PRAGMA incremental_vacuum=' + obj.sqliteConfig.incrementalVacuum + ';' +
  1258. 'PRAGMA optimize=0x10002;';
  1259. //check new autovacuum mode, if changing from or to 'none', a VACUUM needs to be done to activate it. See https://www.sqlite.org/pragma.html#pragma_auto_vacuum
  1260. if ( obj.sqliteConfig.startupVacuum
  1261. || (current.auto_vacuum == 0 && obj.sqliteConfig.autoVacuum !='none')
  1262. || (current.auto_vacuum != 0 && obj.sqliteConfig.autoVacuum =='none'))
  1263. {
  1264. pragma += 'VACUUM;';
  1265. };
  1266. parent.debug ('db', 'Config statement: ' + pragma);
  1267. obj.file.exec( pragma,
  1268. function (err) {
  1269. if (err) { parent.debug('db', 'Config pragma error: ' + (err.message)) };
  1270. sqliteGetPragmas(['journal_mode', 'journal_size_limit', 'freelist_count', 'auto_vacuum', 'page_size', 'wal_autocheckpoint', 'synchronous'], function (pragma, pragmaValue) {
  1271. parent.debug('db', 'PRAGMA: ' + pragma + '=' + pragmaValue);
  1272. });
  1273. });
  1274. });
  1275. //setupFunctions(func);
  1276. }
  1277. function sqliteGetPragmas (pragmas, func){
  1278. //pragmas can only be gotting one by one
  1279. pragmas.forEach (function (pragma) {
  1280. obj.file.get('PRAGMA ' + pragma + ';', function(err, res){
  1281. if (pragma == 'auto_vacuum') { res[pragma] = SQLITE_AUTOVACUUM[res[pragma]] };
  1282. if (pragma == 'synchronous') { res[pragma] = SQLITE_SYNCHRONOUS[res[pragma]] };
  1283. if (func) { func (pragma, res[pragma]); }
  1284. });
  1285. });
  1286. }
  1287. // Create the PostgreSQL tables
  1288. function postgreSqlCreateTables(func) {
  1289. // Database was created, create the tables
  1290. parent.debug('db', 'Creating tables...');
  1291. sqlDbBatchExec([
  1292. 'CREATE TABLE IF NOT EXISTS main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON)',
  1293. 'CREATE TABLE IF NOT EXISTS events(id SERIAL PRIMARY KEY, time TIMESTAMP, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON)',
  1294. 'CREATE TABLE IF NOT EXISTS eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
  1295. 'CREATE TABLE IF NOT EXISTS serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON)',
  1296. 'CREATE TABLE IF NOT EXISTS power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON)',
  1297. 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
  1298. 'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
  1299. ], function (results) {
  1300. parent.debug('db', 'Creating indexes...');
  1301. sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
  1302. sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
  1303. sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
  1304. sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
  1305. sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
  1306. sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
  1307. sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
  1308. sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
  1309. sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
  1310. sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
  1311. sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
  1312. sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
  1313. setupFunctions(func);
  1314. });
  1315. }
  1316. // Check the object names for a "."
  1317. function checkObjectNames(r, tag) {
  1318. if (typeof r != 'object') return;
  1319. for (var i in r) {
  1320. if (i.indexOf('.') >= 0) { throw ('BadDbName (' + tag + '): ' + JSON.stringify(r)); }
  1321. checkObjectNames(r[i], tag);
  1322. }
  1323. }
  1324. // Query the database
  1325. function sqlDbQuery(query, args, func, debug) {
  1326. if (obj.databaseType == DB_SQLITE) { // SQLite
  1327. if (args == null) { args = []; }
  1328. obj.file.all(query, args, function (err, docs) {
  1329. if (err != null) { console.log(query, args, err, docs); }
  1330. if (docs != null) {
  1331. for (var i in docs) {
  1332. if (typeof docs[i].doc == 'string') {
  1333. try { docs[i] = JSON.parse(docs[i].doc); } catch (ex) {
  1334. console.log(query, args, docs[i]);
  1335. }
  1336. }
  1337. }
  1338. }
  1339. if (func) { func(err, docs); }
  1340. });
  1341. } else if (obj.databaseType == DB_MARIADB) { // MariaDB
  1342. Datastore.getConnection()
  1343. .then(function (conn) {
  1344. conn.query(query, args)
  1345. .then(function (rows) {
  1346. conn.release();
  1347. var docs = [];
  1348. for (var i in rows) {
  1349. if (rows[i].doc) {
  1350. docs.push(performTypedRecordDecrypt((typeof rows[i].doc == 'object') ? rows[i].doc : JSON.parse(rows[i].doc)));
  1351. } else if ((rows.length == 1) && (rows[i]['COUNT(doc)'] != null)) {
  1352. // This is a SELECT COUNT() operation
  1353. docs = parseInt(rows[i]['COUNT(doc)']);
  1354. }
  1355. }
  1356. if (func) try { func(null, docs); } catch (ex) { console.log('SQLERR1', ex); }
  1357. })
  1358. .catch(function (err) { conn.release(); if (func) try { func(err); } catch (ex) { console.log('SQLERR2', ex); } });
  1359. }).catch(function (err) { if (func) { try { func(err); } catch (ex) { console.log('SQLERR3', ex); } } });
  1360. } else if (obj.databaseType == DB_MYSQL) { // MySQL
  1361. Datastore.query(query, args, function (error, results, fields) {
  1362. if (error != null) {
  1363. if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
  1364. } else {
  1365. var docs = [];
  1366. for (var i in results) {
  1367. if (results[i].doc) {
  1368. if (typeof results[i].doc == 'string') {
  1369. docs.push(JSON.parse(results[i].doc));
  1370. } else {
  1371. docs.push(results[i].doc);
  1372. }
  1373. } else if ((results.length == 1) && (results[i]['COUNT(doc)'] != null)) {
  1374. // This is a SELECT COUNT() operation
  1375. docs = results[i]['COUNT(doc)'];
  1376. }
  1377. }
  1378. if (func) { try { func(null, docs); } catch (ex) { console.log('SQLERR5', ex); } }
  1379. }
  1380. });
  1381. } else if (obj.databaseType == DB_POSTGRESQL) { // Postgres SQL
  1382. Datastore.query(query, args, function (error, results) {
  1383. if (error != null) {
  1384. if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
  1385. } else {
  1386. var docs = [];
  1387. if ((results.command == 'INSERT') && (results.rows != null) && (results.rows.length == 1)) { docs = results.rows[0]; }
  1388. else if (results.command == 'SELECT') {
  1389. for (var i in results.rows) {
  1390. if (results.rows[i].doc) {
  1391. if (typeof results.rows[i].doc == 'string') {
  1392. docs.push(JSON.parse(results.rows[i].doc));
  1393. } else {
  1394. docs.push(results.rows[i].doc);
  1395. }
  1396. } else if (results.rows[i].count && (results.rows.length == 1)) {
  1397. // This is a SELECT COUNT() operation
  1398. docs = parseInt(results.rows[i].count);
  1399. }
  1400. }
  1401. }
  1402. if (func) { try { func(null, docs, results); } catch (ex) { console.log('SQLERR5', ex); } }
  1403. }
  1404. });
  1405. }
  1406. }
  1407. // Exec on the database
  1408. function sqlDbExec(query, args, func) {
  1409. if (obj.databaseType == DB_MARIADB) { // MariaDB
  1410. Datastore.getConnection()
  1411. .then(function (conn) {
  1412. conn.query(query, args)
  1413. .then(function (rows) {
  1414. conn.release();
  1415. if (func) try { func(null, rows[0]); } catch (ex) { console.log(ex); }
  1416. })
  1417. .catch(function (err) { conn.release(); if (func) try { func(err); } catch (ex) { console.log(ex); } });
  1418. }).catch(function (err) { if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
  1419. } else if ((obj.databaseType == DB_MYSQL) || (obj.databaseType == DB_POSTGRESQL)) { // MySQL or Postgres SQL
  1420. Datastore.query(query, args, function (error, results, fields) {
  1421. if (func) try { func(error, results ? results[0] : null); } catch (ex) { console.log(ex); }
  1422. });
  1423. }
  1424. }
  1425. // Execute a batch of commands on the database
  1426. function sqlDbBatchExec(queries, func) {
  1427. if (obj.databaseType == DB_MARIADB) { // MariaDB
  1428. Datastore.getConnection()
  1429. .then(function (conn) {
  1430. var Promises = [];
  1431. for (var i in queries) { if (typeof queries[i] == 'string') { Promises.push(conn.query(queries[i])); } else { Promises.push(conn.query(queries[i][0], queries[i][1])); } }
  1432. Promise.all(Promises)
  1433. .then(function (rows) { conn.release(); if (func) { try { func(null); } catch (ex) { console.log(ex); } } })
  1434. .catch(function (err) { conn.release(); if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
  1435. })
  1436. .catch(function (err) { if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
  1437. } else if (obj.databaseType == DB_MYSQL) { // MySQL
  1438. Datastore.getConnection(function(err, connection) {
  1439. if (err) { if (func) { try { func(err); } catch (ex) { console.log(ex); } } return; }
  1440. var Promises = [];
  1441. for (var i in queries) { if (typeof queries[i] == 'string') { Promises.push(connection.promise().query(queries[i])); } else { Promises.push(connection.promise().query(queries[i][0], queries[i][1])); } }
  1442. Promise.all(Promises)
  1443. .then(function (error, results, fields) { connection.release(); if (func) { try { func(error, results); } catch (ex) { console.log(ex); } } })
  1444. .catch(function (error, results, fields) { connection.release(); if (func) { try { func(error); } catch (ex) { console.log(ex); } } });
  1445. });
  1446. } else if (obj.databaseType == DB_POSTGRESQL) { // Postgres
  1447. var Promises = [];
  1448. for (var i in queries) { if (typeof queries[i] == 'string') { Promises.push(Datastore.query(queries[i])); } else { Promises.push(Datastore.query(queries[i][0], queries[i][1])); } }
  1449. Promise.all(Promises)
  1450. .then(function (error, results, fields) { if (func) { try { func(error, results); } catch (ex) { console.log(ex); } } })
  1451. .catch(function (error, results, fields) { if (func) { try { func(error); } catch (ex) { console.log(ex); } } });
  1452. }
  1453. }
  1454. function setupFunctions(func) {
  1455. if (obj.databaseType == DB_SQLITE) {
  1456. // Database actions on the main collection. SQLite3: https://www.linode.com/docs/guides/getting-started-with-nodejs-sqlite/
  1457. obj.Set = function (value, func) {
  1458. obj.dbCounters.fileSet++;
  1459. var extra = null, extraex = null;
  1460. value = common.escapeLinksFieldNameEx(value);
  1461. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  1462. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  1463. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  1464. sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
  1465. }
  1466. obj.SetRaw = function (value, func) {
  1467. obj.dbCounters.fileSet++;
  1468. var extra = null, extraex = null;
  1469. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  1470. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  1471. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  1472. sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
  1473. }
  1474. obj.Get = function (_id, func) {
  1475. sqlDbQuery('SELECT doc FROM main WHERE id = $1', [_id], function (err, docs) {
  1476. if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1477. func(err, performTypedRecordDecrypt(docs));
  1478. });
  1479. }
  1480. obj.GetAll = function (func) {
  1481. sqlDbQuery('SELECT domain, doc FROM main', null, function (err, docs) {
  1482. if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1483. func(err, performTypedRecordDecrypt(docs));
  1484. });
  1485. }
  1486. obj.GetHash = function (id, func) {
  1487. sqlDbQuery('SELECT doc FROM main WHERE id = $1', [id], function (err, docs) {
  1488. if ((docs != null) && (docs.length > 0)) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1489. func(err, performTypedRecordDecrypt(docs));
  1490. });
  1491. }
  1492. obj.GetAllTypeNoTypeField = function (type, domain, func) {
  1493. sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2', [type, domain], function (err, docs) {
  1494. if ((docs != null) && (docs.length > 0)) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1495. func(err, performTypedRecordDecrypt(docs));
  1496. });
  1497. };
  1498. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  1499. if (limit == 0) { limit = -1; } // In SQLite, no limit is -1
  1500. if (id && (id != '')) {
  1501. sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN (' + dbMergeSqlArray(meshes) + ')) LIMIT $4 OFFSET $5', [id, type, domain, limit, skip], function (err, docs) {
  1502. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1503. func(err, performTypedRecordDecrypt(docs));
  1504. });
  1505. } else {
  1506. if (extrasids == null) {
  1507. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN (' + dbMergeSqlArray(meshes) + ')) LIMIT $3 OFFSET $4', [type, domain, limit, skip], function (err, docs) {
  1508. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1509. func(err, performTypedRecordDecrypt(docs));
  1510. });
  1511. } else {
  1512. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND ((extra IN (' + dbMergeSqlArray(meshes) + ')) OR (id IN (' + dbMergeSqlArray(extrasids) + '))) LIMIT $3 OFFSET $4', [type, domain, limit, skip], function (err, docs) {
  1513. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1514. func(err, performTypedRecordDecrypt(docs));
  1515. });
  1516. }
  1517. }
  1518. };
  1519. obj.CountAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
  1520. if (id && (id != '')) {
  1521. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN (' + dbMergeSqlArray(meshes) + '))', [id, type, domain], function (err, docs) {
  1522. func(err, (err == null) ? docs[0]['COUNT(doc)'] : null);
  1523. });
  1524. } else {
  1525. if (extrasids == null) {
  1526. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN (' + dbMergeSqlArray(meshes) + '))', [type, domain], function (err, docs) {
  1527. func(err, (err == null) ? docs[0]['COUNT(doc)'] : null);
  1528. });
  1529. } else {
  1530. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (type = $1) AND (domain = $2) AND ((extra IN (' + dbMergeSqlArray(meshes) + ')) OR (id IN (' + dbMergeSqlArray(extrasids) + ')))', [type, domain], function (err, docs) {
  1531. func(err, (err == null) ? docs[0]['COUNT(doc)'] : null);
  1532. });
  1533. }
  1534. }
  1535. };
  1536. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  1537. if (id && (id != '')) {
  1538. sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN (' + dbMergeSqlArray(nodes) + '))', [id, type, domain], function (err, docs) {
  1539. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1540. func(err, performTypedRecordDecrypt(docs));
  1541. });
  1542. } else {
  1543. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN (' + dbMergeSqlArray(nodes) + '))', [type, domain], function (err, docs) {
  1544. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1545. func(err, performTypedRecordDecrypt(docs));
  1546. });
  1547. }
  1548. };
  1549. obj.GetAllType = function (type, func) {
  1550. sqlDbQuery('SELECT doc FROM main WHERE type = $1', [type], function (err, docs) {
  1551. if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1552. func(err, performTypedRecordDecrypt(docs));
  1553. });
  1554. }
  1555. obj.GetAllIdsOfType = function (ids, domain, type, func) {
  1556. sqlDbQuery('SELECT doc FROM main WHERE (id IN (' + dbMergeSqlArray(ids) + ')) AND domain = $1 AND type = $2', [domain, type], function (err, docs) {
  1557. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1558. func(err, performTypedRecordDecrypt(docs));
  1559. });
  1560. }
  1561. obj.GetUserWithEmail = function (domain, email, func) {
  1562. sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) {
  1563. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1564. func(err, performTypedRecordDecrypt(docs));
  1565. });
  1566. }
  1567. obj.GetUserWithVerifiedEmail = function (domain, email, func) {
  1568. sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) {
  1569. if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1570. func(err, performTypedRecordDecrypt(docs));
  1571. });
  1572. }
  1573. obj.Remove = function (id, func) { sqlDbQuery('DELETE FROM main WHERE id = $1', [id], func); };
  1574. obj.RemoveAll = function (func) { sqlDbQuery('DELETE FROM main', null, func); };
  1575. obj.RemoveAllOfType = function (type, func) { sqlDbQuery('DELETE FROM main WHERE type = $1', [type], func); };
  1576. obj.InsertMany = function (data, func) { var pendingOps = 0; for (var i in data) { pendingOps++; obj.SetRaw(data[i], function () { if (--pendingOps == 0) { func(); } }); } }; // Insert records directly, no link escaping
  1577. obj.RemoveMeshDocuments = function (id, func) { sqlDbQuery('DELETE FROM main WHERE extra = $1', [id], function () { sqlDbQuery('DELETE FROM main WHERE id = $1', ['nt' + id], func); }); };
  1578. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  1579. obj.DeleteDomain = function (domain, func) { sqlDbQuery('DELETE FROM main WHERE domain = $1', [domain], func); };
  1580. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  1581. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  1582. obj.getLocalAmtNodes = function (func) {
  1583. sqlDbQuery('SELECT doc FROM main WHERE (type = \'node\') AND (extraex IS NULL)', null, function (err, docs) {
  1584. if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1585. var r = []; if (err == null) { for (var i in docs) { if (docs[i].host != null && docs[i].intelamt != null) { r.push(docs[i]); } } } func(err, r);
  1586. });
  1587. };
  1588. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) {
  1589. sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extraex = $2', [domainid, 'uuid/' + uuid], function (err, docs) {
  1590. if (docs != null) { for (var i in docs) { if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } }
  1591. func(err, docs);
  1592. });
  1593. };
  1594. obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { sqlDbExec('SELECT COUNT(id) FROM main WHERE domain = $1 AND type = $2', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
  1595. // Database actions on the events collection
  1596. obj.GetAllEvents = function (func) {
  1597. sqlDbQuery('SELECT doc FROM events', null, func);
  1598. };
  1599. obj.StoreEvent = function (event, func) {
  1600. obj.dbCounters.eventsSet++;
  1601. sqlDbQuery('INSERT INTO events VALUES (NULL, $1, $2, $3, $4, $5, $6) RETURNING id', [event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, JSON.stringify(event)], function (err, docs) {
  1602. if(func){ func(); }
  1603. if ((err == null) && (docs[0].id)) {
  1604. for (var i in event.ids) {
  1605. if (event.ids[i] != '*') {
  1606. obj.pendingTransfer++;
  1607. sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs[0].id, event.ids[i]], function(){ if(func){ func(); } });
  1608. }
  1609. }
  1610. }
  1611. });
  1612. };
  1613. obj.GetEvents = function (ids, domain, filter, func) {
  1614. var query = "SELECT doc FROM events ";
  1615. var dataarray = [domain];
  1616. if (ids.indexOf('*') >= 0) {
  1617. query = query + "WHERE (domain = $1";
  1618. if (filter != null) {
  1619. query = query + " AND action = $2";
  1620. dataarray.push(filter);
  1621. }
  1622. query = query + ") ORDER BY time DESC";
  1623. } else {
  1624. query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))';
  1625. if (filter != null) {
  1626. query = query + " AND action = $2";
  1627. dataarray.push(filter);
  1628. }
  1629. query = query + ") GROUP BY id ORDER BY time DESC ";
  1630. }
  1631. sqlDbQuery(query, dataarray, func);
  1632. };
  1633. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  1634. var query = "SELECT doc FROM events ";
  1635. var dataarray = [domain];
  1636. if (ids.indexOf('*') >= 0) {
  1637. query = query + "WHERE (domain = $1";
  1638. if (filter != null) {
  1639. query = query + " AND action = $2) ORDER BY time DESC LIMIT $3";
  1640. dataarray.push(filter);
  1641. } else {
  1642. query = query + ") ORDER BY time DESC LIMIT $2";
  1643. }
  1644. } else {
  1645. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (" + dbMergeSqlArray(ids) + "))";
  1646. if (filter != null) {
  1647. query = query + " AND action = $2) GROUP BY id ORDER BY time DESC LIMIT $3";
  1648. dataarray.push(filter);
  1649. } else {
  1650. query = query + ") GROUP BY id ORDER BY time DESC LIMIT $2";
  1651. }
  1652. }
  1653. dataarray.push(limit);
  1654. sqlDbQuery(query, dataarray, func);
  1655. };
  1656. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  1657. var query = "SELECT doc FROM events ";
  1658. var dataarray = [domain, userid];
  1659. if (ids.indexOf('*') >= 0) {
  1660. query = query + "WHERE (domain = $1 AND userid = $2";
  1661. if (filter != null) {
  1662. query = query + " AND action = $3";
  1663. dataarray.push(filter);
  1664. }
  1665. query = query + ") ORDER BY time DESC";
  1666. } else {
  1667. query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))';
  1668. if (filter != null) {
  1669. query = query + " AND action = $3";
  1670. dataarray.push(filter);
  1671. }
  1672. query = query + ") GROUP BY id ORDER BY time DESC";
  1673. }
  1674. sqlDbQuery(query, dataarray, func);
  1675. };
  1676. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  1677. var query = "SELECT doc FROM events ";
  1678. var dataarray = [domain, userid];
  1679. if (ids.indexOf('*') >= 0) {
  1680. query = query + "WHERE (domain = $1 AND userid = $2";
  1681. if (filter != null) {
  1682. query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
  1683. dataarray.push(filter);
  1684. } else {
  1685. query = query + ") ORDER BY time DESC LIMIT $3";
  1686. }
  1687. } else {
  1688. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (" + dbMergeSqlArray(ids) + "))";
  1689. if (filter != null) {
  1690. query = query + " AND action = $3) GROUP BY id ORDER BY time DESC LIMIT $4";
  1691. dataarray.push(filter);
  1692. } else {
  1693. query = query + ") GROUP BY id ORDER BY time DESC LIMIT $3";
  1694. }
  1695. }
  1696. dataarray.push(limit);
  1697. sqlDbQuery(query, dataarray, func);
  1698. };
  1699. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
  1700. if (ids.indexOf('*') >= 0) {
  1701. sqlDbQuery('SELECT doc FROM events WHERE ((domain = $1) AND (time BETWEEN $2 AND $3)) ORDER BY time', [domain, start, end], func);
  1702. } else {
  1703. sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target IN (' + dbMergeSqlArray(ids) + ')) AND (time BETWEEN $2 AND $3)) GROUP BY id ORDER BY time', [domain, start, end], func);
  1704. }
  1705. };
  1706. //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
  1707. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  1708. var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2";
  1709. var dataarray = [nodeid, domain];
  1710. if (filter != null) {
  1711. query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
  1712. dataarray.push(filter);
  1713. } else {
  1714. query = query + ") ORDER BY time DESC LIMIT $3";
  1715. }
  1716. dataarray.push(limit);
  1717. sqlDbQuery(query, dataarray, func);
  1718. };
  1719. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  1720. var query = "SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ";
  1721. var dataarray = [nodeid, domain, userid];
  1722. if (filter != null) {
  1723. query = query + "AND (action = $4) ORDER BY time DESC LIMIT $5";
  1724. dataarray.push(filter);
  1725. } else {
  1726. query = query + "ORDER BY time DESC LIMIT $4";
  1727. }
  1728. dataarray.push(limit);
  1729. sqlDbQuery(query, dataarray, func);
  1730. };
  1731. obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
  1732. obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); };
  1733. obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); };
  1734. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbQuery('SELECT COUNT(*) FROM events WHERE action = \'authfail\' AND domain = $1 AND userid = $2 AND time > $3', [domainid, userid, lastlogin], function (err, response) { func(err == null ? response[0]['COUNT(*)'] : 0); }); }
  1735. // Database actions on the power collection
  1736. obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
  1737. obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUES (NULL, $1, $2, $3)', [event.time, event.nodeid ? event.nodeid : null, JSON.stringify(event)], func); };
  1738. obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = \'*\')) ORDER BY time ASC', [nodeid], func); };
  1739. obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); };
  1740. obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; sqlDbQuery('DELETE FROM power WHERE nodeid = $1', [nodeid], function (err, docs) { }); };
  1741. // Database actions on the SMBIOS collection
  1742. obj.GetAllSMBIOS = function (func) { sqlDbQuery('SELECT doc FROM smbios', null, func); };
  1743. obj.SetSMBIOS = function (smbios, func) { var expire = new Date(smbios.time); expire.setMonth(expire.getMonth() + 6); sqlDbQuery('INSERT INTO smbios VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO UPDATE SET time = $2, expire = $3, doc = $4', [smbios._id, smbios.time, expire, JSON.stringify(smbios)], func); };
  1744. obj.RemoveSMBIOS = function (id) { sqlDbQuery('DELETE FROM smbios WHERE id = $1', [id], function (err, docs) { }); };
  1745. obj.GetSMBIOS = function (id, func) { sqlDbQuery('SELECT doc FROM smbios WHERE id = $1', [id], func); };
  1746. // Database actions on the Server Stats collection
  1747. obj.SetServerStats = function (data, func) { sqlDbQuery('INSERT INTO serverstats VALUES ($1, $2, $3) ON CONFLICT (time) DO UPDATE SET expire = $2, doc = $3', [data.time, data.expire, JSON.stringify(data)], func); };
  1748. obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); sqlDbQuery('SELECT doc FROM serverstats WHERE time > $1', [t], func); }; // TODO: Expire old entries
  1749. // Read a configuration file from the database
  1750. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  1751. // Write a configuration file to the database
  1752. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  1753. // List all configuration files
  1754. obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
  1755. // Get database information (TODO: Complete this)
  1756. obj.getDbStats = function (func) {
  1757. obj.stats = { c: 4 };
  1758. sqlDbQuery('SELECT COUNT(*) FROM main', null, function (err, response) { obj.stats.meshcentral = (err == null ? response[0]['COUNT(*)'] : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  1759. sqlDbQuery('SELECT COUNT(*) FROM serverstats', null, function (err, response) { obj.stats.serverstats = (err == null ? response[0]['COUNT(*)'] : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  1760. sqlDbQuery('SELECT COUNT(*) FROM power', null, function (err, response) { obj.stats.power = (err == null ? response[0]['COUNT(*)'] : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  1761. sqlDbQuery('SELECT COUNT(*) FROM smbios', null, function (err, response) { obj.stats.smbios = (err == null ? response[0]['COUNT(*)'] : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  1762. }
  1763. // Plugin operations
  1764. if (obj.pluginsActive) {
  1765. obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUES (NULL, $1)', [JSON.stringify(plugin)], func); }; // Add a plugin
  1766. obj.getPlugins = function (func) { sqlDbQuery('SELECT JSON_INSERT(doc, "$._id", id) as doc FROM plugin', null, func); }; // Get all plugins
  1767. obj.getPlugin = function (id, func) { sqlDbQuery('SELECT JSON_INSERT(doc, "$._id", id) as doc FROM plugin WHERE id = $1', [id], func); }; // Get plugin
  1768. obj.deletePlugin = function (id, func) { sqlDbQuery('DELETE FROM plugin WHERE id = $1', [id], func); }; // Delete plugin
  1769. obj.setPluginStatus = function (id, status, func) { sqlDbQuery('UPDATE plugin SET doc=JSON_SET(doc,"$.status",$1) WHERE id=$2', [status,id], func); };
  1770. obj.updatePlugin = function (id, args, func) { delete args._id; sqlDbQuery('UPDATE plugin SET doc=json_patch(doc,$1) WHERE id=$2', [JSON.stringify(args),id], func); };
  1771. }
  1772. } else if (obj.databaseType == DB_ACEBASE) {
  1773. // Database actions on the main collection. AceBase: https://github.com/appy-one/acebase
  1774. obj.Set = function (data, func) {
  1775. data = common.escapeLinksFieldNameEx(data);
  1776. var xdata = performTypedRecordEncrypt(data);
  1777. obj.dbCounters.fileSet++;
  1778. obj.file.ref('meshcentral').child(encodeURIComponent(xdata._id)).set(common.aceEscapeFieldNames(xdata)).then(function (ref) { if (func) { func(); } })
  1779. };
  1780. obj.Get = function (id, func) {
  1781. obj.file.ref('meshcentral').child(encodeURIComponent(id)).get(function (snapshot) {
  1782. if (snapshot.exists()) { func(null, performTypedRecordDecrypt([common.aceUnEscapeFieldNames(snapshot.val())])); } else { func(null, []); }
  1783. });
  1784. };
  1785. obj.GetAll = function (func) {
  1786. obj.file.ref('meshcentral').get(function(snapshot) {
  1787. const val = snapshot.val();
  1788. const docs = Object.keys(val).map(function(key) { return val[key]; });
  1789. func(null, common.aceUnEscapeAllFieldNames(docs));
  1790. });
  1791. };
  1792. obj.GetHash = function (id, func) {
  1793. obj.file.ref('meshcentral').child(encodeURIComponent(id)).get({ include: ['hash'] }, function (snapshot) {
  1794. if (snapshot.exists()) { func(null, snapshot.val()); } else { func(null, null); }
  1795. });
  1796. };
  1797. obj.GetAllTypeNoTypeField = function (type, domain, func) {
  1798. obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domain).get({ exclude: ['type'] }, function (snapshots) {
  1799. const docs = [];
  1800. for (var i in snapshots) { const x = snapshots[i].val(); docs.push(x); }
  1801. func(null, common.aceUnEscapeAllFieldNames(docs));
  1802. });
  1803. }
  1804. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  1805. if (meshes.length == 0) { func(null, []); return; }
  1806. var query = obj.file.query('meshcentral').skip(skip).take(limit).filter('type', '==', type).filter('domain', '==', domain);
  1807. if (id) { query = query.filter('_id', '==', id); }
  1808. if (extrasids == null) {
  1809. query = query.filter('meshid', 'in', meshes);
  1810. query.get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); });
  1811. } else {
  1812. // TODO: This is a slow query as we did not find a filter-or-filter, so we query everything and filter manualy.
  1813. query.get(function (snapshots) {
  1814. const docs = [];
  1815. for (var i in snapshots) { const x = snapshots[i].val(); if ((extrasids.indexOf(x._id) >= 0) || (meshes.indexOf(x.meshid) >= 0)) { docs.push(x); } }
  1816. func(null, performTypedRecordDecrypt(docs));
  1817. });
  1818. }
  1819. };
  1820. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  1821. var query = obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domain).filter('nodeid', 'in', nodes);
  1822. if (id) { query = query.filter('_id', '==', id); }
  1823. query.get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); });
  1824. };
  1825. obj.GetAllType = function (type, func) {
  1826. obj.file.query('meshcentral').filter('type', '==', type).get(function (snapshots) {
  1827. const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); }
  1828. func(null, common.aceUnEscapeAllFieldNames(performTypedRecordDecrypt(docs)));
  1829. });
  1830. };
  1831. obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.query('meshcentral').filter('_id', 'in', ids).filter('domain', '==', domain).filter('type', '==', type).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
  1832. obj.GetUserWithEmail = function (domain, email, func) { obj.file.query('meshcentral').filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
  1833. obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.query('meshcentral').filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).filter('emailVerified', '==', true).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
  1834. obj.Remove = function (id, func) { obj.file.ref('meshcentral').child(encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); };
  1835. obj.RemoveAll = function (func) { obj.file.query('meshcentral').remove().then(function () { if (func) { func(); } }); };
  1836. obj.RemoveAllOfType = function (type, func) { obj.file.query('meshcentral').filter('type', '==', type).remove().then(function () { if (func) { func(); } }); };
  1837. obj.InsertMany = function (data, func) { var r = {}; for (var i in data) { const ref = obj.file.ref('meshcentral').child(encodeURIComponent(data[i]._id)); r[ref.key] = common.aceEscapeFieldNames(data[i]); } obj.file.ref('meshcentral').set(r).then(function (ref) { func(); }); }; // Insert records directly, no link escaping
  1838. obj.RemoveMeshDocuments = function (id) { obj.file.query('meshcentral').filter('meshid', '==', id).remove(); obj.file.ref('meshcentral').child(encodeURIComponent('nt' + id)).remove(); };
  1839. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  1840. obj.DeleteDomain = function (domain, func) { obj.file.query('meshcentral').filter('domain', '==', domain).remove().then(function () { if (func) { func(); } }); };
  1841. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  1842. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  1843. obj.getLocalAmtNodes = function (func) { obj.file.query('meshcentral').filter('type', '==', 'node').filter('host', 'exists').filter('host', '!=', null).filter('intelamt', 'exists').get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
  1844. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.query('meshcentral').filter('type', '==', 'node').filter('domain', '==', domainid).filter('mtype', '!=', mtype).filter('intelamt.uuid', '==', uuid).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
  1845. obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domainid).get({ snapshots: false }, function (snapshots) { func((snapshots.length > max), snapshots.length); }); } }
  1846. // Database actions on the events collection
  1847. obj.GetAllEvents = function (func) {
  1848. obj.file.ref('events').get(function (snapshot) {
  1849. const val = snapshot.val();
  1850. const docs = Object.keys(val).map(function(key) { return val[key]; });
  1851. func(null, docs);
  1852. })
  1853. };
  1854. obj.StoreEvent = function (event, func) {
  1855. if (typeof event.account == 'object') { event = Object.assign({}, event); event.account = common.aceEscapeFieldNames(event.account); }
  1856. obj.dbCounters.eventsSet++;
  1857. obj.file.ref('events').push(event).then(function (userRef) { if (func) { func(); } });
  1858. };
  1859. obj.GetEvents = function (ids, domain, filter, func) {
  1860. // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
  1861. if (filter != null) {
  1862. obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
  1863. const docs = [];
  1864. for (var i in snapshots) {
  1865. const doc = snapshots[i].val();
  1866. if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
  1867. var found = false;
  1868. for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
  1869. if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
  1870. }
  1871. func(null, docs);
  1872. });
  1873. } else {
  1874. obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
  1875. const docs = [];
  1876. for (var i in snapshots) {
  1877. const doc = snapshots[i].val();
  1878. if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
  1879. var found = false;
  1880. for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
  1881. if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
  1882. }
  1883. func(null, docs);
  1884. });
  1885. }
  1886. };
  1887. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  1888. // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
  1889. // TODO: Request a new AceBase feature for a 'array:contains-one-of' filter:
  1890. // obj.file.indexes.create('events', 'ids', { type: 'array' });
  1891. // db.query('events').filter('ids', 'array:contains-one-of', ids)
  1892. if (filter != null) {
  1893. obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
  1894. const docs = [];
  1895. for (var i in snapshots) {
  1896. const doc = snapshots[i].val();
  1897. if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
  1898. var found = false;
  1899. for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
  1900. if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
  1901. }
  1902. func(null, docs);
  1903. });
  1904. } else {
  1905. obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
  1906. const docs = [];
  1907. for (var i in snapshots) {
  1908. const doc = snapshots[i].val();
  1909. if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
  1910. var found = false;
  1911. for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
  1912. if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
  1913. }
  1914. func(null, docs);
  1915. });
  1916. }
  1917. };
  1918. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  1919. if (filter != null) {
  1920. obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1921. } else {
  1922. obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1923. }
  1924. };
  1925. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  1926. if (filter != null) {
  1927. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1928. } else {
  1929. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1930. }
  1931. };
  1932. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
  1933. obj.file.query('events').filter('domain', '==', domain).filter('ids', 'in', ids).filter('msgid', 'in', msgids).filter('time', 'between', [start, end]).sort('time', false).get({ exclude: ['type', '_id', 'domain', 'node'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1934. };
  1935. obj.GetUserLoginEvents = function (domain, userid, func) {
  1936. obj.file.query('events').filter('domain', '==', domain).filter('action', 'in', ['authfail', 'login']).filter('userid', '==', userid).filter('msgArgs', 'exists').sort('time', false).get({ include: ['action', 'time', 'msgid', 'msgArgs', 'tokenName'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1937. };
  1938. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  1939. if (filter != null) {
  1940. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('action', '==', filter).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1941. } else {
  1942. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1943. }
  1944. };
  1945. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  1946. if (filter != null) {
  1947. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).filter('action', '==', filter).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1948. } else {
  1949. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1950. }
  1951. obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  1952. };
  1953. obj.RemoveAllEvents = function (domain) {
  1954. obj.file.query('events').filter('domain', '==', domain).remove().then(function () { if (func) { func(); } });;
  1955. };
  1956. obj.RemoveAllNodeEvents = function (domain, nodeid) {
  1957. if ((domain == null) || (nodeid == null)) return;
  1958. obj.file.query('events').filter('domain', '==', domain).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });;
  1959. };
  1960. obj.RemoveAllUserEvents = function (domain, userid) {
  1961. if ((domain == null) || (userid == null)) return;
  1962. obj.file.query('events').filter('domain', '==', domain).filter('userid', '==', userid).remove().then(function () { if (func) { func(); } });;
  1963. };
  1964. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) {
  1965. obj.file.query('events').filter('domain', '==', domainid).filter('userid', '==', userid).filter('time', '>', lastlogin).sort('time', false).get({ snapshots: false }, function (snapshots) { func(null, snapshots.length); });
  1966. }
  1967. // Database actions on the power collection
  1968. obj.getAllPower = function (func) {
  1969. obj.file.ref('power').get(function (snapshot) {
  1970. const val = snapshot.val();
  1971. const docs = Object.keys(val).map(function(key) { return val[key]; });
  1972. func(null, docs);
  1973. });
  1974. };
  1975. obj.storePowerEvent = function (event, multiServer, func) {
  1976. if (multiServer != null) { event.server = multiServer.serverid; }
  1977. obj.file.ref('power').push(event).then(function (userRef) { if (func) { func(); } });
  1978. };
  1979. obj.getPowerTimeline = function (nodeid, func) {
  1980. obj.file.query('power').filter('nodeid', 'in', ['*', nodeid]).sort('time').get({ exclude: ['_id', 'nodeid', 's'] }, function (snapshots) {
  1981. const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
  1982. });
  1983. };
  1984. obj.removeAllPowerEvents = function () {
  1985. obj.file.ref('power').remove().then(function () { if (func) { func(); } });
  1986. };
  1987. obj.removeAllPowerEventsForNode = function (nodeid) {
  1988. if (nodeid == null) return;
  1989. obj.file.query('power').filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });
  1990. };
  1991. // Database actions on the SMBIOS collection
  1992. if (obj.smbiosfile != null) {
  1993. obj.GetAllSMBIOS = function (func) {
  1994. obj.file.ref('smbios').get(function (snapshot) {
  1995. const val = snapshot.val();
  1996. const docs = Object.keys(val).map(function(key) { return val[key]; });
  1997. func(null, docs);
  1998. });
  1999. };
  2000. obj.SetSMBIOS = function (smbios, func) {
  2001. obj.file.ref('meshcentral/' + encodeURIComponent(smbios._id)).set(smbios).then(function (ref) { if (func) { func(); } })
  2002. };
  2003. obj.RemoveSMBIOS = function (id) {
  2004. obj.file.query('smbios').filter('_id', '==', id).remove().then(function () { if (func) { func(); } });
  2005. };
  2006. obj.GetSMBIOS = function (id, func) {
  2007. obj.file.query('smbios').filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
  2008. };
  2009. }
  2010. // Database actions on the Server Stats collection
  2011. obj.SetServerStats = function (data, func) {
  2012. obj.file.ref('stats').push(data).then(function (userRef) { if (func) { func(); } });
  2013. };
  2014. obj.GetServerStats = function (hours, func) {
  2015. var t = new Date();
  2016. t.setTime(t.getTime() - (60 * 60 * 1000 * hours));
  2017. obj.file.query('stats').filter('time', '>', t).get({ exclude: ['_id', 'cpu'] }, function (snapshots) {
  2018. const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
  2019. });
  2020. };
  2021. // Read a configuration file from the database
  2022. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  2023. // Write a configuration file to the database
  2024. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  2025. // List all configuration files
  2026. obj.listConfigFiles = function (func) {
  2027. obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
  2028. const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
  2029. });
  2030. }
  2031. // Get database information
  2032. obj.getDbStats = function (func) {
  2033. obj.stats = { c: 5 };
  2034. obj.file.ref('meshcentral').count().then(function (count) { obj.stats.meshcentral = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2035. obj.file.ref('events').count().then(function (count) { obj.stats.events = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2036. obj.file.ref('power').count().then(function (count) { obj.stats.power = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2037. obj.file.ref('smbios').count().then(function (count) { obj.stats.smbios = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2038. obj.file.ref('stats').count().then(function (count) { obj.stats.serverstats = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2039. }
  2040. // Plugin operations
  2041. if (obj.pluginsActive) {
  2042. obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.file.ref('plugin').child(encodeURIComponent(plugin._id)).set(plugin).then(function (ref) { if (func) { func(); } }) }; // Add a plugin
  2043. obj.getPlugins = function (func) {
  2044. obj.file.ref('plugin').get({ exclude: ['type'] }, function (snapshot) {
  2045. const val = snapshot.val();
  2046. const docs = Object.keys(val).map(function(key) { return val[key]; }).sort(function(a, b) { return a.name < b.name ? -1 : 1 });
  2047. func(null, docs);
  2048. });
  2049. }; // Get all plugins
  2050. obj.getPlugin = function (id, func) { obj.file.query('plugin').filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get plugin
  2051. obj.deletePlugin = function (id, func) { obj.file.ref('plugin').child(encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); }; // Delete plugin
  2052. obj.setPluginStatus = function (id, status, func) { obj.file.ref('plugin').child(encodeURIComponent(id)).update({ status: status }).then(function (ref) { if (func) { func(); } }) };
  2053. obj.updatePlugin = function (id, args, func) { delete args._id; obj.file.ref('plugin').child(encodeURIComponent(id)).set(args).then(function (ref) { if (func) { func(); } }) };
  2054. }
  2055. } else if (obj.databaseType == DB_POSTGRESQL) {
  2056. // Database actions on the main collection (Postgres)
  2057. obj.Set = function (value, func) {
  2058. obj.dbCounters.fileSet++;
  2059. var extra = null, extraex = null;
  2060. value = common.escapeLinksFieldNameEx(value);
  2061. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  2062. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  2063. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  2064. sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, performTypedRecordEncrypt(value)], func);
  2065. }
  2066. obj.SetRaw = function (value, func) {
  2067. obj.dbCounters.fileSet++;
  2068. var extra = null, extraex = null;
  2069. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  2070. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  2071. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  2072. sqlDbQuery('INSERT INTO main VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (id) DO UPDATE SET type = $2, domain = $3, extra = $4, extraex = $5, doc = $6;', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, performTypedRecordEncrypt(value)], func);
  2073. }
  2074. obj.Get = function (_id, func) { sqlDbQuery('SELECT doc FROM main WHERE id = $1', [_id], function (err, docs) { if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); } func(err, performTypedRecordDecrypt(docs)); }); }
  2075. obj.GetAll = function (func) { sqlDbQuery('SELECT domain, doc FROM main', null, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2076. obj.GetHash = function (id, func) { sqlDbQuery('SELECT doc FROM main WHERE id = $1', [id], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2077. obj.GetAllTypeNoTypeField = function (type, domain, func) { sqlDbQuery('SELECT doc FROM main WHERE type = $1 AND domain = $2', [type, domain], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); }); };
  2078. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  2079. if (limit == 0) { limit = 0xFFFFFFFF; }
  2080. if (id && (id != '')) {
  2081. sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra = ANY ($4)) LIMIT $5 OFFSET $6', [id, type, domain, meshes, limit, skip], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2082. } else {
  2083. if (extrasids == null) {
  2084. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3)) LIMIT $4 OFFSET $5', [type, domain, meshes, limit, skip], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); }, true);
  2085. } else {
  2086. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND ((extra = ANY ($3)) OR (id = ANY ($4))) LIMIT $5 OFFSET $6', [type, domain, meshes, extrasids, limit, skip], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2087. }
  2088. }
  2089. };
  2090. obj.CountAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
  2091. if (id && (id != '')) {
  2092. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra = ANY ($4))', [id, type, domain, meshes], function (err, docs) { func(err, docs); });
  2093. } else {
  2094. if (extrasids == null) {
  2095. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3))', [type, domain, meshes], function (err, docs) { func(err, docs); }, true);
  2096. } else {
  2097. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE (type = $1) AND (domain = $2) AND ((extra = ANY ($3)) OR (id = ANY ($4)))', [type, domain, meshes, extrasids], function (err, docs) { func(err, docs); });
  2098. }
  2099. }
  2100. };
  2101. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  2102. if (id && (id != '')) {
  2103. sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra = ANY ($4))', [id, type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2104. } else {
  2105. sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra = ANY ($3))', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2106. }
  2107. };
  2108. obj.GetAllType = function (type, func) { sqlDbQuery('SELECT doc FROM main WHERE type = $1', [type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2109. obj.GetAllIdsOfType = function (ids, domain, type, func) { sqlDbQuery('SELECT doc FROM main WHERE (id = ANY ($1)) AND domain = $2 AND type = $3', [ids, domain, type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2110. obj.GetUserWithEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2111. obj.GetUserWithVerifiedEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extra = $2', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2112. obj.Remove = function (id, func) { sqlDbQuery('DELETE FROM main WHERE id = $1', [id], func); };
  2113. obj.RemoveAll = function (func) { sqlDbQuery('DELETE FROM main', null, func); };
  2114. obj.RemoveAllOfType = function (type, func) { sqlDbQuery('DELETE FROM main WHERE type = $1', [type], func); };
  2115. obj.InsertMany = function (data, func) { var pendingOps = 0; for (var i in data) { pendingOps++; obj.SetRaw(data[i], function () { if (--pendingOps == 0) { func(); } }); } }; // Insert records directly, no link escaping
  2116. obj.RemoveMeshDocuments = function (id, func) { sqlDbQuery('DELETE FROM main WHERE extra = $1', [id], function () { sqlDbQuery('DELETE FROM main WHERE id = $1', ['nt' + id], func); }); };
  2117. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  2118. obj.DeleteDomain = function (domain, func) { sqlDbQuery('DELETE FROM main WHERE domain = $1', [domain], func); };
  2119. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  2120. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  2121. obj.getLocalAmtNodes = function (func) { sqlDbQuery('SELECT doc FROM main WHERE (type = \'node\') AND (extraex IS NULL)', null, function (err, docs) { var r = []; if (err == null) { for (var i in docs) { if (docs[i].host != null && docs[i].intelamt != null) { r.push(docs[i]); } } } func(err, r); }); };
  2122. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = $1 AND extraex = $2', [domainid, 'uuid/' + uuid], func); };
  2123. obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { sqlDbExec('SELECT COUNT(id) FROM main WHERE domain = $1 AND type = $2', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
  2124. // Database actions on the events collection
  2125. obj.GetAllEvents = function (func) { sqlDbQuery('SELECT doc FROM events', null, func); };
  2126. obj.StoreEvent = function (event, func) {
  2127. obj.dbCounters.eventsSet++;
  2128. sqlDbQuery('INSERT INTO events VALUES (DEFAULT, $1, $2, $3, $4, $5, $6) RETURNING id', [event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, event], function (err, docs) {
  2129. if(func){ func(); }
  2130. if (docs.id) {
  2131. for (var i in event.ids) {
  2132. if (event.ids[i] != '*') {
  2133. obj.pendingTransfer++;
  2134. sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs.id, event.ids[i]], function(){ if(func){ func(); } });
  2135. }
  2136. }
  2137. }
  2138. });
  2139. };
  2140. obj.GetEvents = function (ids, domain, filter, func) {
  2141. var query = "SELECT doc FROM events ";
  2142. var dataarray = [domain];
  2143. if (ids.indexOf('*') >= 0) {
  2144. query = query + "WHERE (domain = $1";
  2145. if (filter != null) {
  2146. query = query + " AND action = $2";
  2147. dataarray.push(filter);
  2148. }
  2149. query = query + ") ORDER BY time DESC";
  2150. } else {
  2151. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))";
  2152. dataarray.push(ids);
  2153. if (filter != null) {
  2154. query = query + " AND action = $3";
  2155. dataarray.push(filter);
  2156. }
  2157. query = query + ") GROUP BY id ORDER BY time DESC";
  2158. }
  2159. sqlDbQuery(query, dataarray, func);
  2160. };
  2161. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  2162. var query = "SELECT doc FROM events ";
  2163. var dataarray = [domain];
  2164. if (ids.indexOf('*') >= 0) {
  2165. query = query + "WHERE (domain = $1";
  2166. if (filter != null) {
  2167. query = query + " AND action = $2) ORDER BY time DESC LIMIT $3";
  2168. dataarray.push(filter);
  2169. } else {
  2170. query = query + ") ORDER BY time DESC LIMIT $2";
  2171. }
  2172. } else {
  2173. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2174. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))";
  2175. dataarray.push(ids);
  2176. if (filter != null) {
  2177. query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
  2178. dataarray.push(filter);
  2179. } else {
  2180. query = query + ") ORDER BY time DESC LIMIT $3";
  2181. }
  2182. }
  2183. dataarray.push(limit);
  2184. sqlDbQuery(query, dataarray, func);
  2185. };
  2186. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  2187. var query = "SELECT doc FROM events ";
  2188. var dataarray = [domain, userid];
  2189. if (ids.indexOf('*') >= 0) {
  2190. query = query + "WHERE (domain = $1 AND userid = $2";
  2191. if (filter != null) {
  2192. query = query + " AND action = $3";
  2193. dataarray.push(filter);
  2194. }
  2195. query = query + ") ORDER BY time DESC";
  2196. } else {
  2197. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2198. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))";
  2199. dataarray.push(ids);
  2200. if (filter != null) {
  2201. query = query + " AND action = $4";
  2202. dataarray.push(filter);
  2203. }
  2204. query = query + ") GROUP BY id ORDER BY time DESC";
  2205. }
  2206. sqlDbQuery(query, dataarray, func);
  2207. };
  2208. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  2209. var query = "SELECT doc FROM events ";
  2210. var dataarray = [domain, userid];
  2211. if (ids.indexOf('*') >= 0) {
  2212. query = query + "WHERE (domain = $1 AND userid = $2";
  2213. if (filter != null) {
  2214. query = query + " AND action = $3) ORDER BY time DESC LIMIT $4 ";
  2215. dataarray.push(filter);
  2216. } else {
  2217. query = query + ") ORDER BY time DESC LIMIT $3";
  2218. }
  2219. } else {
  2220. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2221. query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))";
  2222. dataarray.push(ids);
  2223. if (filter != null) {
  2224. query = query + " AND action = $4) GROUP BY id ORDER BY time DESC LIMIT $5";
  2225. dataarray.push(filter);
  2226. } else {
  2227. query = query + ") GROUP BY id ORDER BY time DESC LIMIT $4";
  2228. }
  2229. }
  2230. dataarray.push(limit);
  2231. sqlDbQuery(query, dataarray, func);
  2232. };
  2233. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
  2234. if (ids.indexOf('*') >= 0) {
  2235. sqlDbQuery('SELECT doc FROM events WHERE ((domain = $1) AND (time BETWEEN $2 AND $3)) ORDER BY time', [domain, start, end], func);
  2236. } else {
  2237. sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target = ANY ($2)) AND (time BETWEEN $3 AND $4)) GROUP BY id ORDER BY time', [domain, ids, start, end], func);
  2238. }
  2239. };
  2240. //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
  2241. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  2242. var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2";
  2243. var dataarray = [nodeid, domain];
  2244. if (filter != null) {
  2245. query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
  2246. dataarray.push(filter);
  2247. } else {
  2248. query = query + ") ORDER BY time DESC LIMIT $3";
  2249. }
  2250. dataarray.push(limit);
  2251. sqlDbQuery(query, dataarray, func);
  2252. };
  2253. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  2254. var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2 AND ((userid = $3) OR (userid IS NULL))";
  2255. var dataarray = [nodeid, domain, userid];
  2256. if (filter != null) {
  2257. query = query + " AND action = $4) ORDER BY time DESC LIMIT $5";
  2258. dataarray.push(filter);
  2259. } else {
  2260. query = query + ") ORDER BY time DESC LIMIT $4";
  2261. }
  2262. dataarray.push(limit);
  2263. sqlDbQuery(query, dataarray, func);
  2264. };
  2265. obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
  2266. obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); };
  2267. obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); };
  2268. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbQuery('SELECT COUNT(*) FROM events WHERE action = \'authfail\' AND domain = $1 AND userid = $2 AND time > $3', [domainid, userid, lastlogin], function (err, response, raw) { func(err == null ? parseInt(raw.rows[0].count) : 0); }); }
  2269. // Database actions on the power collection
  2270. obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
  2271. obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUES (DEFAULT, $1, $2, $3)', [event.time, event.nodeid ? event.nodeid : null, event], func); };
  2272. obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = \'*\')) ORDER BY time ASC', [nodeid], func); };
  2273. obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); };
  2274. obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; sqlDbQuery('DELETE FROM power WHERE nodeid = $1', [nodeid], function (err, docs) { }); };
  2275. // Database actions on the SMBIOS collection
  2276. obj.GetAllSMBIOS = function (func) { sqlDbQuery('SELECT doc FROM smbios', null, func); };
  2277. obj.SetSMBIOS = function (smbios, func) { var expire = new Date(smbios.time); expire.setMonth(expire.getMonth() + 6); sqlDbQuery('INSERT INTO smbios VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO UPDATE SET time = $2, expire = $3, doc = $4', [smbios._id, smbios.time, expire, smbios], func); };
  2278. obj.RemoveSMBIOS = function (id) { sqlDbQuery('DELETE FROM smbios WHERE id = $1', [id], function (err, docs) { }); };
  2279. obj.GetSMBIOS = function (id, func) { sqlDbQuery('SELECT doc FROM smbios WHERE id = $1', [id], func); };
  2280. // Database actions on the Server Stats collection
  2281. obj.SetServerStats = function (data, func) { sqlDbQuery('INSERT INTO serverstats VALUES ($1, $2, $3) ON CONFLICT (time) DO UPDATE SET expire = $2, doc = $3', [data.time, data.expire, data], func); };
  2282. obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); sqlDbQuery('SELECT doc FROM serverstats WHERE time > $1', [t], func); }; // TODO: Expire old entries
  2283. // Read a configuration file from the database
  2284. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  2285. // Write a configuration file to the database
  2286. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  2287. // List all configuration files
  2288. obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
  2289. // Get database information (TODO: Complete this)
  2290. obj.getDbStats = function (func) {
  2291. obj.stats = { c: 4 };
  2292. sqlDbQuery('SELECT COUNT(*) FROM main', null, function (err, response, raw) { obj.stats.meshcentral = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2293. sqlDbQuery('SELECT COUNT(*) FROM serverstats', null, function (err, response, raw) { obj.stats.serverstats = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2294. sqlDbQuery('SELECT COUNT(*) FROM power', null, function (err, response, raw) { obj.stats.power = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2295. sqlDbQuery('SELECT COUNT(*) FROM smbios', null, function (err, response, raw) { obj.stats.smbios = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2296. }
  2297. // Plugin operations
  2298. if (obj.pluginsActive) {
  2299. obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUES (DEFAULT, $1)', [plugin], func); }; // Add a plugin
  2300. obj.getPlugins = function (func) { sqlDbQuery("SELECT doc::jsonb || ('{\"_id\":' || plugin.id || '}')::jsonb as doc FROM plugin", null, func); }; // Get all plugins
  2301. obj.getPlugin = function (id, func) { sqlDbQuery("SELECT doc::jsonb || ('{\"_id\":' || plugin.id || '}')::jsonb as doc FROM plugin WHERE id = $1", [id], func); }; // Get plugin
  2302. obj.deletePlugin = function (id, func) { sqlDbQuery('DELETE FROM plugin WHERE id = $1', [id], func); }; // Delete plugin
  2303. obj.setPluginStatus = function (id, status, func) { sqlDbQuery("UPDATE plugin SET doc= jsonb_set(doc::jsonb,'{status}',$1) WHERE id=$2", [status,id], func); };
  2304. obj.updatePlugin = function (id, args, func) { delete args._id; sqlDbQuery('UPDATE plugin SET doc= doc::jsonb || ($1) WHERE id=$2', [args,id], func); };
  2305. }
  2306. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL)) {
  2307. // Database actions on the main collection (MariaDB or MySQL)
  2308. obj.Set = function (value, func) {
  2309. obj.dbCounters.fileSet++;
  2310. var extra = null, extraex = null;
  2311. value = common.escapeLinksFieldNameEx(value);
  2312. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  2313. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  2314. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  2315. sqlDbQuery('REPLACE INTO main VALUE (?, ?, ?, ?, ?, ?)', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
  2316. }
  2317. obj.SetRaw = function (value, func) {
  2318. obj.dbCounters.fileSet++;
  2319. var extra = null, extraex = null;
  2320. if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
  2321. if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
  2322. if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
  2323. sqlDbQuery('REPLACE INTO main VALUE (?, ?, ?, ?, ?, ?)', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
  2324. }
  2325. obj.Get = function (_id, func) { sqlDbQuery('SELECT doc FROM main WHERE id = ?', [_id], function (err, docs) { if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); } func(err, performTypedRecordDecrypt(docs)); }); }
  2326. obj.GetAll = function (func) { sqlDbQuery('SELECT domain, doc FROM main', null, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2327. obj.GetHash = function (id, func) { sqlDbQuery('SELECT doc FROM main WHERE id = ?', [id], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2328. obj.GetAllTypeNoTypeField = function (type, domain, func) { sqlDbQuery('SELECT doc FROM main WHERE type = ? AND domain = ?', [type, domain], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); }); };
  2329. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  2330. if (limit == 0) { limit = 0xFFFFFFFF; }
  2331. if ((meshes == null) || (meshes.length == 0)) { meshes = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2332. if ((extrasids == null) || (extrasids.length == 0)) { extrasids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2333. if (id && (id != '')) {
  2334. sqlDbQuery('SELECT doc FROM main WHERE id = ? AND type = ? AND domain = ? AND extra IN (?) LIMIT ? OFFSET ?', [id, type, domain, meshes, limit, skip], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2335. } else {
  2336. sqlDbQuery('SELECT doc FROM main WHERE type = ? AND domain = ? AND (extra IN (?) OR id IN (?)) LIMIT ? OFFSET ?', [type, domain, meshes, extrasids, limit, skip], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2337. }
  2338. };
  2339. obj.CountAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
  2340. if ((meshes == null) || (meshes.length == 0)) { meshes = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2341. if ((extrasids == null) || (extrasids.length == 0)) { extrasids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2342. if (id && (id != '')) {
  2343. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE id = ? AND type = ? AND domain = ? AND extra IN (?)', [id, type, domain, meshes], function (err, docs) { func(err, docs); });
  2344. } else {
  2345. sqlDbQuery('SELECT COUNT(doc) FROM main WHERE type = ? AND domain = ? AND (extra IN (?) OR id IN (?))', [type, domain, meshes, extrasids], function (err, docs) { func(err, docs); });
  2346. }
  2347. };
  2348. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  2349. if ((nodes == null) || (nodes.length == 0)) { nodes = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2350. if (id && (id != '')) {
  2351. sqlDbQuery('SELECT doc FROM main WHERE id = ? AND type = ? AND domain = ? AND extra IN (?)', [id, type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2352. } else {
  2353. sqlDbQuery('SELECT doc FROM main WHERE type = ? AND domain = ? AND extra IN (?)', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, performTypedRecordDecrypt(docs)); });
  2354. }
  2355. };
  2356. obj.GetAllType = function (type, func) { sqlDbQuery('SELECT doc FROM main WHERE type = ?', [type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2357. obj.GetAllIdsOfType = function (ids, domain, type, func) {
  2358. if ((ids == null) || (ids.length == 0)) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2359. sqlDbQuery('SELECT doc FROM main WHERE id IN (?) AND domain = ? AND type = ?', [ids, domain, type], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2360. }
  2361. obj.GetUserWithEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = ? AND extra = ?', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2362. obj.GetUserWithVerifiedEmail = function (domain, email, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = ? AND extra = ?', [domain, 'email/' + email], function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); }
  2363. obj.Remove = function (id, func) { sqlDbQuery('DELETE FROM main WHERE id = ?', [id], func); };
  2364. obj.RemoveAll = function (func) { sqlDbQuery('DELETE FROM main', null, func); };
  2365. obj.RemoveAllOfType = function (type, func) { sqlDbQuery('DELETE FROM main WHERE type = ?', [type], func); };
  2366. obj.InsertMany = function (data, func) { var pendingOps = 0; for (var i in data) { pendingOps++; obj.SetRaw(data[i], function () { if (--pendingOps == 0) { func(); } }); } }; // Insert records directly, no link escaping
  2367. obj.RemoveMeshDocuments = function (id, func) { sqlDbQuery('DELETE FROM main WHERE extra = ?', [id], function () { sqlDbQuery('DELETE FROM main WHERE id = ?', ['nt' + id], func); } ); };
  2368. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  2369. obj.DeleteDomain = function (domain, func) { sqlDbQuery('DELETE FROM main WHERE domain = ?', [domain], func); };
  2370. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  2371. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  2372. obj.getLocalAmtNodes = function (func) { sqlDbQuery('SELECT doc FROM main WHERE (type = "node") AND (extraex IS NULL)', null, function (err, docs) { var r = []; if (err == null) { for (var i in docs) { if (docs[i].host != null && docs[i].intelamt != null) { r.push(docs[i]); } } } func(err, r); }); };
  2373. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = ? AND extraex = ?', [domainid, 'uuid/' + uuid], func); };
  2374. obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { sqlDbExec('SELECT COUNT(id) FROM main WHERE domain = ? AND type = ?', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
  2375. // Database actions on the events collection
  2376. obj.GetAllEvents = function (func) { sqlDbQuery('SELECT doc FROM events', null, func); };
  2377. obj.StoreEvent = function (event, func) {
  2378. obj.dbCounters.eventsSet++;
  2379. var batchQuery = [['INSERT INTO events VALUE (?, ?, ?, ?, ?, ?, ?)', [null, event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, JSON.stringify(event)]]];
  2380. for (var i in event.ids) { if (event.ids[i] != '*') { batchQuery.push(['INSERT INTO eventids VALUE (LAST_INSERT_ID(), ?)', [event.ids[i]]]); } }
  2381. sqlDbBatchExec(batchQuery, function (err, docs) { if (func != null) { func(err, docs); } });
  2382. };
  2383. obj.GetEvents = function (ids, domain, filter, func) {
  2384. var query = "SELECT doc FROM events ";
  2385. var dataarray = [domain];
  2386. if (ids.indexOf('*') >= 0) {
  2387. query = query + "WHERE (domain = ?";
  2388. if (filter != null) {
  2389. query = query + " AND action = ?";
  2390. dataarray.push(filter);
  2391. }
  2392. query = query + ") ORDER BY time DESC";
  2393. } else {
  2394. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2395. query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)";
  2396. dataarray.push(ids);
  2397. if (filter != null) {
  2398. query = query + " AND action = ?";
  2399. dataarray.push(filter);
  2400. }
  2401. query = query + ") GROUP BY id ORDER BY time DESC";
  2402. }
  2403. sqlDbQuery(query, dataarray, func);
  2404. };
  2405. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  2406. var query = "SELECT doc FROM events ";
  2407. var dataarray = [domain];
  2408. if (ids.indexOf('*') >= 0) {
  2409. query = query + "WHERE (domain = ?";
  2410. if (filter != null) {
  2411. query = query + " AND action = ? ";
  2412. dataarray.push(filter);
  2413. }
  2414. query = query + ") ORDER BY time DESC LIMIT ?";
  2415. } else {
  2416. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2417. query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)";
  2418. dataarray.push(ids);
  2419. if (filter != null) {
  2420. query = query + " AND action = ?";
  2421. dataarray.push(filter);
  2422. }
  2423. query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?";
  2424. }
  2425. dataarray.push(limit);
  2426. sqlDbQuery(query, dataarray, func);
  2427. };
  2428. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  2429. var query = "SELECT doc FROM events ";
  2430. var dataarray = [domain, userid];
  2431. if (ids.indexOf('*') >= 0) {
  2432. query = query + "WHERE (domain = ? AND userid = ?";
  2433. if (filter != null) {
  2434. query = query + " AND action = ?";
  2435. dataarray.push(filter);
  2436. }
  2437. query = query + ") ORDER BY time DESC";
  2438. } else {
  2439. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2440. query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)";
  2441. dataarray.push(ids);
  2442. if (filter != null) {
  2443. query = query + " AND action = ?";
  2444. dataarray.push(filter);
  2445. }
  2446. query = query + ") GROUP BY id ORDER BY time DESC";
  2447. }
  2448. sqlDbQuery(query, dataarray, func);
  2449. };
  2450. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  2451. var query = "SELECT doc FROM events ";
  2452. var dataarray = [domain, userid];
  2453. if (ids.indexOf('*') >= 0) {
  2454. query = query + "WHERE (domain = ? AND userid = ?";
  2455. if (filter != null) {
  2456. query = query + " AND action = ?";
  2457. dataarray.push(filter);
  2458. }
  2459. query = query + ") ORDER BY time DESC LIMIT ?";
  2460. } else {
  2461. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2462. query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)";
  2463. dataarray.push(ids);
  2464. if (filter != null) {
  2465. query = query + " AND action = ?";
  2466. dataarray.push(filter);
  2467. }
  2468. query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?";
  2469. }
  2470. dataarray.push(limit);
  2471. sqlDbQuery(query, dataarray, func);
  2472. };
  2473. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
  2474. if (ids.indexOf('*') >= 0) {
  2475. sqlDbQuery('SELECT doc FROM events WHERE ((domain = ?) AND (time BETWEEN ? AND ?)) ORDER BY time', [domain, start, end], func);
  2476. } else {
  2477. if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
  2478. sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = ?) AND (target IN (?)) AND (time BETWEEN ? AND ?)) GROUP BY id ORDER BY time', [domain, ids, start, end], func);
  2479. }
  2480. };
  2481. //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
  2482. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  2483. var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ?";
  2484. var dataarray = [nodeid, domain];
  2485. if (filter != null) {
  2486. query = query + " AND action = ?) ORDER BY time DESC LIMIT ?";
  2487. dataarray.push(filter);
  2488. } else {
  2489. query = query + ") ORDER BY time DESC LIMIT ?";
  2490. }
  2491. dataarray.push(limit);
  2492. sqlDbQuery(query, dataarray, func);
  2493. };
  2494. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  2495. var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ? AND ((userid = ?) OR (userid IS NULL))";
  2496. var dataarray = [nodeid, domain, userid];
  2497. if (filter != null) {
  2498. query = query + " AND action = ?) ORDER BY time DESC LIMIT ?";
  2499. dataarray.push(filter);
  2500. } else {
  2501. query = query + ") ORDER BY time DESC LIMIT ?";
  2502. }
  2503. dataarray.push(limit);
  2504. sqlDbQuery(query, dataarray, func);
  2505. };
  2506. obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
  2507. obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND nodeid = ?', [domain, nodeid], function (err, docs) { }); };
  2508. obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND userid = ?', [domain, userid], function (err, docs) { }); };
  2509. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbExec('SELECT COUNT(id) FROM events WHERE action = "authfail" AND domain = ? AND userid = ? AND time > ?', [domainid, userid, lastlogin], function (err, response) { func(err == null ? response['COUNT(id)'] : 0); }); }
  2510. // Database actions on the power collection
  2511. obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
  2512. obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUE (?, ?, ?, ?)', [null, event.time, event.nodeid ? event.nodeid : null, JSON.stringify(event)], func); };
  2513. obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = ?) OR (nodeid = "*")) ORDER BY time ASC', [nodeid], func); };
  2514. obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); };
  2515. obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; sqlDbQuery('DELETE FROM power WHERE nodeid = ?', [nodeid], function (err, docs) { }); };
  2516. // Database actions on the SMBIOS collection
  2517. obj.GetAllSMBIOS = function (func) { sqlDbQuery('SELECT doc FROM smbios', null, func); };
  2518. obj.SetSMBIOS = function (smbios, func) { var expire = new Date(smbios.time); expire.setMonth(expire.getMonth() + 6); sqlDbQuery('REPLACE INTO smbios VALUE (?, ?, ?, ?)', [smbios._id, smbios.time, expire, JSON.stringify(smbios)], func); };
  2519. obj.RemoveSMBIOS = function (id) { sqlDbQuery('DELETE FROM smbios WHERE id = ?', [id], function (err, docs) { }); };
  2520. obj.GetSMBIOS = function (id, func) { sqlDbQuery('SELECT doc FROM smbios WHERE id = ?', [id], func); };
  2521. // Database actions on the Server Stats collection
  2522. obj.SetServerStats = function (data, func) { sqlDbQuery('REPLACE INTO serverstats VALUE (?, ?, ?)', [data.time, data.expire, JSON.stringify(data)], func); };
  2523. obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); sqlDbQuery('SELECT doc FROM serverstats WHERE time > ?', [t], func); };
  2524. // Read a configuration file from the database
  2525. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  2526. // Write a configuration file to the database
  2527. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  2528. // List all configuration files
  2529. obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
  2530. // Get database information (TODO: Complete this)
  2531. obj.getDbStats = function (func) {
  2532. obj.stats = { c: 4 };
  2533. sqlDbExec('SELECT COUNT(id) FROM main', null, function (err, response) { obj.stats.meshcentral = Number(response['COUNT(id)']); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2534. sqlDbExec('SELECT COUNT(time) FROM serverstats', null, function (err, response) { obj.stats.serverstats = Number(response['COUNT(time)']); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2535. sqlDbExec('SELECT COUNT(id) FROM power', null, function (err, response) { obj.stats.power = Number(response['COUNT(id)']); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2536. sqlDbExec('SELECT COUNT(id) FROM smbios', null, function (err, response) { obj.stats.smbios = Number(response['COUNT(id)']); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
  2537. }
  2538. // Plugin operations
  2539. if (obj.pluginsActive) {
  2540. obj.addPlugin = function (plugin, func) { sqlDbQuery('INSERT INTO plugin VALUE (?, ?)', [null, JSON.stringify(plugin)], func); }; // Add a plugin
  2541. obj.getPlugins = function (func) { sqlDbQuery('SELECT JSON_INSERT(doc, "$._id", id) as doc FROM plugin', null, func); }; // Get all plugins
  2542. obj.getPlugin = function (id, func) { sqlDbQuery('SELECT JSON_INSERT(doc, "$._id", id) as doc FROM plugin WHERE id = ?', [id], func); }; // Get plugin
  2543. obj.deletePlugin = function (id, func) { sqlDbQuery('DELETE FROM plugin WHERE id = ?', [id], func); }; // Delete plugin
  2544. obj.setPluginStatus = function (id, status, func) { sqlDbQuery('UPDATE meshcentral.plugin SET doc=JSON_SET(doc,"$.status",?) WHERE id=?', [status,id], func); };
  2545. obj.updatePlugin = function (id, args, func) { delete args._id; sqlDbQuery('UPDATE meshcentral.plugin SET doc=JSON_MERGE_PATCH(doc,?) WHERE id=?', [JSON.stringify(args),id], func); };
  2546. }
  2547. } else if (obj.databaseType == DB_MONGODB) {
  2548. // Database actions on the main collection (MongoDB)
  2549. // Bulk operations
  2550. if (parent.config.settings.mongodbbulkoperations) {
  2551. obj.Set = function (data, func) { // Fast Set operation using bulkWrite(), this is much faster then using replaceOne()
  2552. if (obj.filePendingSet == false) {
  2553. // Perform the operation now
  2554. obj.dbCounters.fileSet++;
  2555. obj.filePendingSet = true; obj.filePendingSets = null;
  2556. if (func != null) { obj.filePendingCbs = [func]; }
  2557. obj.file.bulkWrite([{ replaceOne: { filter: { _id: data._id }, replacement: performTypedRecordEncrypt(common.escapeLinksFieldNameEx(data)), upsert: true } }], fileBulkWriteCompleted);
  2558. } else {
  2559. // Add this operation to the pending list
  2560. obj.dbCounters.fileSetPending++;
  2561. if (obj.filePendingSets == null) { obj.filePendingSets = {} }
  2562. obj.filePendingSets[data._id] = data;
  2563. if (func != null) { if (obj.filePendingCb == null) { obj.filePendingCb = [func]; } else { obj.filePendingCb.push(func); } }
  2564. }
  2565. };
  2566. obj.Get = function (id, func) { // Fast Get operation using a bulk find() to reduce round trips to the database.
  2567. // Encode arguments into return function if any are present.
  2568. var func2 = func;
  2569. if (arguments.length > 2) {
  2570. var parms = [func];
  2571. for (var parmx = 2; parmx < arguments.length; ++parmx) { parms.push(arguments[parmx]); }
  2572. var func2 = function _func2(arg1, arg2) {
  2573. var userCallback = _func2.userArgs.shift();
  2574. _func2.userArgs.unshift(arg2);
  2575. _func2.userArgs.unshift(arg1);
  2576. userCallback.apply(obj, _func2.userArgs);
  2577. };
  2578. func2.userArgs = parms;
  2579. }
  2580. if (obj.filePendingGets == null) {
  2581. // No pending gets, perform the operation now.
  2582. obj.filePendingGets = {};
  2583. obj.filePendingGets[id] = [func2];
  2584. obj.file.find({ _id: id }).toArray(fileBulkReadCompleted);
  2585. } else {
  2586. // Add get to pending list.
  2587. if (obj.filePendingGet == null) { obj.filePendingGet = {}; }
  2588. if (obj.filePendingGet[id] == null) { obj.filePendingGet[id] = [func2]; } else { obj.filePendingGet[id].push(func2); }
  2589. }
  2590. };
  2591. } else {
  2592. obj.Set = function (data, func) {
  2593. obj.dbCounters.fileSet++;
  2594. data = common.escapeLinksFieldNameEx(data);
  2595. obj.file.replaceOne({ _id: data._id }, performTypedRecordEncrypt(data), { upsert: true }, func);
  2596. };
  2597. obj.Get = function (id, func) {
  2598. if (arguments.length > 2) {
  2599. var parms = [func];
  2600. for (var parmx = 2; parmx < arguments.length; ++parmx) { parms.push(arguments[parmx]); }
  2601. var func2 = function _func2(arg1, arg2) {
  2602. var userCallback = _func2.userArgs.shift();
  2603. _func2.userArgs.unshift(arg2);
  2604. _func2.userArgs.unshift(arg1);
  2605. userCallback.apply(obj, _func2.userArgs);
  2606. };
  2607. func2.userArgs = parms;
  2608. obj.file.find({ _id: id }).toArray(function (err, docs) {
  2609. if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); }
  2610. func2(err, performTypedRecordDecrypt(docs));
  2611. });
  2612. } else {
  2613. obj.file.find({ _id: id }).toArray(function (err, docs) {
  2614. if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); }
  2615. func(err, performTypedRecordDecrypt(docs));
  2616. });
  2617. }
  2618. };
  2619. }
  2620. obj.GetAll = function (func) { obj.file.find({}).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2621. obj.GetHash = function (id, func) { obj.file.find({ _id: id }).project({ _id: 0, hash: 1 }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2622. obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }).project({ type: 0 }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2623. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  2624. if (extrasids == null) {
  2625. const x = { type: type, domain: domain, meshid: { $in: meshes } };
  2626. if (id) { x._id = id; }
  2627. var f = obj.file.find(x, { type: 0 });
  2628. if (skip > 0) f = f.skip(skip); // Skip records
  2629. if (limit > 0) f = f.limit(limit); // Limit records
  2630. f.toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2631. } else {
  2632. const x = { type: type, domain: domain, $or: [ { meshid: { $in: meshes } }, { _id: { $in: extrasids } } ] };
  2633. if (id) { x._id = id; }
  2634. var f = obj.file.find(x, { type: 0 });
  2635. if (skip > 0) f = f.skip(skip); // Skip records
  2636. if (limit > 0) f = f.limit(limit); // Limit records
  2637. f.toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2638. }
  2639. };
  2640. obj.CountAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
  2641. if (extrasids == null) {
  2642. const x = { type: type, domain: domain, meshid: { $in: meshes } };
  2643. if (id) { x._id = id; }
  2644. var f = obj.file.find(x, { type: 0 });
  2645. f.count(function (err, count) { func(err, count); });
  2646. } else {
  2647. const x = { type: type, domain: domain, $or: [{ meshid: { $in: meshes } }, { _id: { $in: extrasids } }] };
  2648. if (id) { x._id = id; }
  2649. var f = obj.file.find(x, { type: 0 });
  2650. f.count(function (err, count) { func(err, count); });
  2651. }
  2652. };
  2653. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  2654. var x = { type: type, domain: domain, nodeid: { $in: nodes } };
  2655. if (id) { x._id = id; }
  2656. obj.file.find(x, { type: 0 }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2657. };
  2658. obj.GetAllType = function (type, func) { obj.file.find({ type: type }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2659. obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.find({ type: type, domain: domain, _id: { $in: ids } }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2660. obj.GetUserWithEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2661. obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email, emailVerified: true }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2662. // Bulk operations
  2663. if (parent.config.settings.mongodbbulkoperations) {
  2664. obj.Remove = function (id, func) { // Fast remove operation using a bulk find() to reduce round trips to the database.
  2665. if (obj.filePendingRemoves == null) {
  2666. // No pending removes, perform the operation now.
  2667. obj.dbCounters.fileRemove++;
  2668. obj.filePendingRemoves = {};
  2669. obj.filePendingRemoves[id] = [func];
  2670. obj.file.deleteOne({ _id: id }, fileBulkRemoveCompleted);
  2671. } else {
  2672. // Add remove to pending list.
  2673. obj.dbCounters.fileRemovePending++;
  2674. if (obj.filePendingRemove == null) { obj.filePendingRemove = {}; }
  2675. if (obj.filePendingRemove[id] == null) { obj.filePendingRemove[id] = [func]; } else { obj.filePendingRemove[id].push(func); }
  2676. }
  2677. };
  2678. } else {
  2679. obj.Remove = function (id, func) { obj.dbCounters.fileRemove++; obj.file.deleteOne({ _id: id }, func); };
  2680. }
  2681. obj.RemoveAll = function (func) { obj.file.deleteMany({}, { multi: true }, func); };
  2682. obj.RemoveAllOfType = function (type, func) { obj.file.deleteMany({ type: type }, { multi: true }, func); };
  2683. obj.InsertMany = function (data, func) { obj.file.insertMany(data, func); }; // Insert records directly, no link escaping
  2684. obj.RemoveMeshDocuments = function (id) { obj.file.deleteMany({ meshid: id }, { multi: true }); obj.file.deleteOne({ _id: 'nt' + id }); };
  2685. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  2686. obj.DeleteDomain = function (domain, func) { obj.file.deleteMany({ domain: domain }, { multi: true }, func); };
  2687. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  2688. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  2689. obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }).toArray(func); };
  2690. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.find({ type: 'node', domain: domainid, mtype: mtype, 'intelamt.uuid': uuid }).toArray(func); };
  2691. // TODO: Starting in MongoDB 4.0.3, you should use countDocuments() instead of count() that is deprecated. We should detect MongoDB version and switch.
  2692. // https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/
  2693. //obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.countDocuments({ type: type, domain: domainid }, function (err, count) { func((err != null) || (count > max)); }); } }
  2694. obj.isMaxType = function (max, type, domainid, func) {
  2695. if (obj.file.countDocuments) {
  2696. if (max == null) { func(false); } else { obj.file.countDocuments({ type: type, domain: domainid }, function (err, count) { func((err != null) || (count > max), count); }); }
  2697. } else {
  2698. if (max == null) { func(false); } else { obj.file.count({ type: type, domain: domainid }, function (err, count) { func((err != null) || (count > max), count); }); }
  2699. }
  2700. }
  2701. // Database actions on the events collection
  2702. obj.GetAllEvents = function (func) { obj.eventsfile.find({}).toArray(func); };
  2703. // Bulk operations
  2704. if (parent.config.settings.mongodbbulkoperations) {
  2705. obj.StoreEvent = function (event, func) { // Fast MongoDB event store using bulkWrite()
  2706. if (obj.eventsFilePendingSet == false) {
  2707. // Perform the operation now
  2708. obj.dbCounters.eventsSet++;
  2709. obj.eventsFilePendingSet = true; obj.eventsFilePendingSets = null;
  2710. if (func != null) { obj.eventsFilePendingCbs = [func]; }
  2711. obj.eventsfile.bulkWrite([{ insertOne: { document: event } }], eventsFileBulkWriteCompleted);
  2712. } else {
  2713. // Add this operation to the pending list
  2714. obj.dbCounters.eventsSetPending++;
  2715. if (obj.eventsFilePendingSets == null) { obj.eventsFilePendingSets = [] }
  2716. obj.eventsFilePendingSets.push(event);
  2717. if (func != null) { if (obj.eventsFilePendingCb == null) { obj.eventsFilePendingCb = [func]; } else { obj.eventsFilePendingCb.push(func); } }
  2718. }
  2719. };
  2720. } else {
  2721. obj.StoreEvent = function (event, func) { obj.dbCounters.eventsSet++; obj.eventsfile.insertOne(event, func); };
  2722. }
  2723. obj.GetEvents = function (ids, domain, filter, func) {
  2724. var finddata = { domain: domain, ids: { $in: ids } };
  2725. if (filter != null) finddata.action = filter;
  2726. obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func);
  2727. };
  2728. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  2729. var finddata = { domain: domain, ids: { $in: ids } };
  2730. if (filter != null) finddata.action = filter;
  2731. obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
  2732. };
  2733. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  2734. var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
  2735. if (filter != null) finddata.action = filter;
  2736. obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func);
  2737. };
  2738. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  2739. var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
  2740. if (filter != null) finddata.action = filter;
  2741. obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
  2742. };
  2743. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }], msgid: { $in: msgids }, time: { $gte: start, $lte: end } }).project({ type: 0, _id: 0, domain: 0, node: 0 }).sort({ time: 1 }).toArray(func); };
  2744. obj.GetUserLoginEvents = function (domain, userid, func) { obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }).project({ action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }).toArray(func); };
  2745. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  2746. var finddata = { domain: domain, nodeid: nodeid };
  2747. if (filter != null) finddata.action = filter;
  2748. obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
  2749. };
  2750. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  2751. var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } };
  2752. if (filter != null) finddata.action = filter;
  2753. obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
  2754. };
  2755. obj.RemoveAllEvents = function (domain) { obj.eventsfile.deleteMany({ domain: domain }, { multi: true }); };
  2756. obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.deleteMany({ domain: domain, nodeid: nodeid }, { multi: true }); };
  2757. obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.deleteMany({ domain: domain, userid: userid }, { multi: true }); };
  2758. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) {
  2759. if (obj.eventsfile.countDocuments) {
  2760. obj.eventsfile.countDocuments({ action: 'authfail', userid: userid, domain: domainid, time: { "$gte": lastlogin } }, function (err, count) { func((err == null) ? count : 0); });
  2761. } else {
  2762. obj.eventsfile.count({ action: 'authfail', userid: userid, domain: domainid, time: { "$gte": lastlogin } }, function (err, count) { func((err == null) ? count : 0); });
  2763. }
  2764. }
  2765. // Database actions on the power collection
  2766. obj.getAllPower = function (func) { obj.powerfile.find({}).toArray(func); };
  2767. // Bulk operations
  2768. if (parent.config.settings.mongodbbulkoperations) {
  2769. obj.storePowerEvent = function (event, multiServer, func) { // Fast MongoDB event store using bulkWrite()
  2770. if (multiServer != null) { event.server = multiServer.serverid; }
  2771. if (obj.powerFilePendingSet == false) {
  2772. // Perform the operation now
  2773. obj.dbCounters.powerSet++;
  2774. obj.powerFilePendingSet = true; obj.powerFilePendingSets = null;
  2775. if (func != null) { obj.powerFilePendingCbs = [func]; }
  2776. obj.powerfile.bulkWrite([{ insertOne: { document: event } }], powerFileBulkWriteCompleted);
  2777. } else {
  2778. // Add this operation to the pending list
  2779. obj.dbCounters.powerSetPending++;
  2780. if (obj.powerFilePendingSets == null) { obj.powerFilePendingSets = [] }
  2781. obj.powerFilePendingSets.push(event);
  2782. if (func != null) { if (obj.powerFilePendingCb == null) { obj.powerFilePendingCb = [func]; } else { obj.powerFilePendingCb.push(func); } }
  2783. }
  2784. };
  2785. } else {
  2786. obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insertOne(event, func); };
  2787. }
  2788. obj.getPowerTimeline = function (nodeid, func) { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }).project({ _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }).toArray(func); };
  2789. obj.removeAllPowerEvents = function () { obj.powerfile.deleteMany({}, { multi: true }); };
  2790. obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; obj.powerfile.deleteMany({ nodeid: nodeid }, { multi: true }); };
  2791. // Database actions on the SMBIOS collection
  2792. obj.GetAllSMBIOS = function (func) { obj.smbiosfile.find({}).toArray(func); };
  2793. obj.SetSMBIOS = function (smbios, func) { obj.smbiosfile.updateOne({ _id: smbios._id }, { $set: smbios }, { upsert: true }, func); };
  2794. obj.RemoveSMBIOS = function (id) { obj.smbiosfile.deleteOne({ _id: id }); };
  2795. obj.GetSMBIOS = function (id, func) { obj.smbiosfile.find({ _id: id }).toArray(func); };
  2796. // Database actions on the Server Stats collection
  2797. obj.SetServerStats = function (data, func) { obj.serverstatsfile.insertOne(data, func); };
  2798. obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); obj.serverstatsfile.find({ time: { $gt: t } }, { _id: 0, cpu: 0 }).toArray(func); };
  2799. // Read a configuration file from the database
  2800. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  2801. // Write a configuration file to the database
  2802. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  2803. // List all configuration files
  2804. obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); }
  2805. // Get database information
  2806. obj.getDbStats = function (func) {
  2807. obj.stats = { c: 6 };
  2808. obj.getStats(function (r) { obj.stats.recordTypes = r; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } })
  2809. obj.file.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  2810. obj.eventsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  2811. obj.powerfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  2812. obj.smbiosfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  2813. obj.serverstatsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  2814. }
  2815. // Correct database information of obj.getDbStats before returning it
  2816. function getDbStatsEx(data) {
  2817. var r = {};
  2818. if (data.recordTypes != null) { r = data.recordTypes; }
  2819. try { r.smbios = data['meshcentral.smbios'].count; } catch (ex) { }
  2820. try { r.power = data['meshcentral.power'].count; } catch (ex) { }
  2821. try { r.events = data['meshcentral.events'].count; } catch (ex) { }
  2822. try { r.serverstats = data['meshcentral.serverstats'].count; } catch (ex) { }
  2823. return r;
  2824. }
  2825. // Plugin operations
  2826. if (obj.pluginsActive) {
  2827. obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.pluginsfile.insertOne(plugin, func); }; // Add a plugin
  2828. obj.getPlugins = function (func) { obj.pluginsfile.find({ type: 'plugin' }).project({ type: 0 }).sort({ name: 1 }).toArray(func); }; // Get all plugins
  2829. obj.getPlugin = function (id, func) { id = require('mongodb').ObjectId(id); obj.pluginsfile.find({ _id: id }).sort({ name: 1 }).toArray(func); }; // Get plugin
  2830. obj.deletePlugin = function (id, func) { id = require('mongodb').ObjectId(id); obj.pluginsfile.deleteOne({ _id: id }, func); }; // Delete plugin
  2831. obj.setPluginStatus = function (id, status, func) { id = require('mongodb').ObjectId(id); obj.pluginsfile.updateOne({ _id: id }, { $set: { status: status } }, func); };
  2832. obj.updatePlugin = function (id, args, func) { delete args._id; id = require('mongodb').ObjectId(id); obj.pluginsfile.updateOne({ _id: id }, { $set: args }, func); };
  2833. }
  2834. } else {
  2835. // Database actions on the main collection (NeDB and MongoJS)
  2836. obj.Set = function (data, func) {
  2837. obj.dbCounters.fileSet++;
  2838. data = common.escapeLinksFieldNameEx(data);
  2839. var xdata = performTypedRecordEncrypt(data); obj.file.update({ _id: xdata._id }, xdata, { upsert: true }, func);
  2840. };
  2841. obj.Get = function (id, func) {
  2842. if (arguments.length > 2) {
  2843. var parms = [func];
  2844. for (var parmx = 2; parmx < arguments.length; ++parmx) { parms.push(arguments[parmx]); }
  2845. var func2 = function _func2(arg1, arg2) {
  2846. var userCallback = _func2.userArgs.shift();
  2847. _func2.userArgs.unshift(arg2);
  2848. _func2.userArgs.unshift(arg1);
  2849. userCallback.apply(obj, _func2.userArgs);
  2850. };
  2851. func2.userArgs = parms;
  2852. obj.file.find({ _id: id }, function (err, docs) {
  2853. if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); }
  2854. func2(err, performTypedRecordDecrypt(docs));
  2855. });
  2856. } else {
  2857. obj.file.find({ _id: id }, function (err, docs) {
  2858. if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); }
  2859. func(err, performTypedRecordDecrypt(docs));
  2860. });
  2861. }
  2862. };
  2863. obj.GetAll = function (func) { obj.file.find({}, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2864. obj.GetHash = function (id, func) { obj.file.find({ _id: id }, { _id: 0, hash: 1 }, func); };
  2865. obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }, { type: 0 }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2866. //obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, domain, type, id, skip, limit, func) {
  2867. //var x = { type: type, domain: domain, meshid: { $in: meshes } };
  2868. //if (id) { x._id = id; }
  2869. //obj.file.find(x, { type: 0 }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2870. //};
  2871. obj.CountAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
  2872. if (extrasids == null) {
  2873. const x = { type: type, domain: domain, meshid: { $in: meshes } };
  2874. if (id) { x._id = id; }
  2875. obj.file.count(x, function (err, count) { func(err, count); });
  2876. } else {
  2877. const x = { type: type, domain: domain, $or: [{ meshid: { $in: meshes } }, { _id: { $in: extrasids } }] };
  2878. if (id) { x._id = id; }
  2879. obj.file.count(x, function (err, count) { func(err, count); });
  2880. }
  2881. };
  2882. obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
  2883. if (extrasids == null) {
  2884. const x = { type: type, domain: domain, meshid: { $in: meshes } };
  2885. if (id) { x._id = id; }
  2886. obj.file.find(x).skip(skip).limit(limit).exec(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2887. } else {
  2888. const x = { type: type, domain: domain, $or: [{ meshid: { $in: meshes } }, { _id: { $in: extrasids } }] };
  2889. if (id) { x._id = id; }
  2890. obj.file.find(x).skip(skip).limit(limit).exec(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2891. }
  2892. };
  2893. obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
  2894. var x = { type: type, domain: domain, nodeid: { $in: nodes } };
  2895. if (id) { x._id = id; }
  2896. obj.file.find(x, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); });
  2897. };
  2898. obj.GetAllType = function (type, func) { obj.file.find({ type: type }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2899. obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.find({ type: type, domain: domain, _id: { $in: ids } }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2900. obj.GetUserWithEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email }, { type: 0 }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2901. obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email, emailVerified: true }, function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
  2902. obj.Remove = function (id, func) { obj.file.remove({ _id: id }, func); };
  2903. obj.RemoveAll = function (func) { obj.file.remove({}, { multi: true }, func); };
  2904. obj.RemoveAllOfType = function (type, func) { obj.file.remove({ type: type }, { multi: true }, func); };
  2905. obj.InsertMany = function (data, func) { obj.file.insert(data, func); }; // Insert records directly, no link escaping
  2906. obj.RemoveMeshDocuments = function (id) { obj.file.remove({ meshid: id }, { multi: true }); obj.file.remove({ _id: 'nt' + id }); };
  2907. obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
  2908. obj.DeleteDomain = function (domain, func) { obj.file.remove({ domain: domain }, { multi: true }, func); };
  2909. obj.SetUser = function (user) { if (user == null) return; if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
  2910. obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
  2911. obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }, func); };
  2912. obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.find({ type: 'node', domain: domainid, mtype: mtype, 'intelamt.uuid': uuid }, func); };
  2913. obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.count({ type: type, domain: domainid }, function (err, count) { func((err != null) || (count > max), count); }); } }
  2914. // Database actions on the events collection
  2915. obj.GetAllEvents = function (func) { obj.eventsfile.find({}, func); };
  2916. obj.StoreEvent = function (event, func) { obj.eventsfile.insert(event, func); };
  2917. obj.GetEvents = function (ids, domain, filter, func) {
  2918. var finddata = { domain: domain, ids: { $in: ids } };
  2919. if (filter != null) finddata.action = filter;
  2920. if (obj.databaseType == DB_NEDB) {
  2921. obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func);
  2922. } else {
  2923. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func);
  2924. }
  2925. };
  2926. obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
  2927. var finddata = { domain: domain, ids: { $in: ids } };
  2928. if (filter != null) finddata.action = filter;
  2929. if (obj.databaseType == DB_NEDB) {
  2930. obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func);
  2931. } else {
  2932. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func);
  2933. }
  2934. };
  2935. obj.GetUserEvents = function (ids, domain, userid, filter, func) {
  2936. var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
  2937. if (filter != null) finddata.action = filter;
  2938. if (obj.databaseType == DB_NEDB) {
  2939. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func);
  2940. } else {
  2941. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func);
  2942. }
  2943. };
  2944. obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
  2945. var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
  2946. if (filter != null) finddata.action = filter;
  2947. if (obj.databaseType == DB_NEDB) {
  2948. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func);
  2949. } else {
  2950. obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func);
  2951. }
  2952. };
  2953. obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
  2954. if (obj.databaseType == DB_NEDB) {
  2955. obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }], msgid: { $in: msgids }, time: { $gte: start, $lte: end } }, { type: 0, _id: 0, domain: 0, node: 0 }).sort({ time: 1 }).exec(func);
  2956. } else {
  2957. obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }], msgid: { $in: msgids }, time: { $gte: start, $lte: end } }, { type: 0, _id: 0, domain: 0, node: 0 }).sort({ time: 1 }, func);
  2958. }
  2959. };
  2960. obj.GetUserLoginEvents = function (domain, userid, func) {
  2961. if (obj.databaseType == DB_NEDB) {
  2962. obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }, { action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }).exec(func);
  2963. } else {
  2964. obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }, { action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }, func);
  2965. }
  2966. };
  2967. obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
  2968. var finddata = { domain: domain, nodeid: nodeid };
  2969. if (filter != null) finddata.action = filter;
  2970. if (obj.databaseType == DB_NEDB) {
  2971. obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func);
  2972. } else {
  2973. obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func);
  2974. }
  2975. };
  2976. obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
  2977. var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } };
  2978. if (filter != null) finddata.action = filter;
  2979. if (obj.databaseType == DB_NEDB) {
  2980. obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func);
  2981. } else {
  2982. obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func);
  2983. }
  2984. };
  2985. obj.RemoveAllEvents = function (domain) { obj.eventsfile.remove({ domain: domain }, { multi: true }); };
  2986. obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.remove({ domain: domain, nodeid: nodeid }, { multi: true }); };
  2987. obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.remove({ domain: domain, userid: userid }, { multi: true }); };
  2988. obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { obj.eventsfile.count({ action: 'authfail', userid: userid, domain: domainid, time: { "$gte": lastlogin } }, function (err, count) { func((err == null) ? count : 0); }); }
  2989. // Database actions on the power collection
  2990. obj.getAllPower = function (func) { obj.powerfile.find({}, func); };
  2991. obj.storePowerEvent = function (event, multiServer, func) { if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insert(event, func); };
  2992. obj.getPowerTimeline = function (nodeid, func) { if (obj.databaseType == DB_NEDB) { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }).exec(func); } else { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }, func); } };
  2993. obj.removeAllPowerEvents = function () { obj.powerfile.remove({}, { multi: true }); };
  2994. obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; obj.powerfile.remove({ nodeid: nodeid }, { multi: true }); };
  2995. // Database actions on the SMBIOS collection
  2996. if (obj.smbiosfile != null) {
  2997. obj.GetAllSMBIOS = function (func) { obj.smbiosfile.find({}, func); };
  2998. obj.SetSMBIOS = function (smbios, func) { obj.smbiosfile.update({ _id: smbios._id }, smbios, { upsert: true }, func); };
  2999. obj.RemoveSMBIOS = function (id) { obj.smbiosfile.remove({ _id: id }); };
  3000. obj.GetSMBIOS = function (id, func) { obj.smbiosfile.find({ _id: id }, func); };
  3001. }
  3002. // Database actions on the Server Stats collection
  3003. obj.SetServerStats = function (data, func) { obj.serverstatsfile.insert(data, func); };
  3004. obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); obj.serverstatsfile.find({ time: { $gt: t } }, { _id: 0, cpu: 0 }, func); };
  3005. // Read a configuration file from the database
  3006. obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
  3007. // Write a configuration file to the database
  3008. obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
  3009. // List all configuration files
  3010. obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); }
  3011. // Get database information
  3012. obj.getDbStats = function (func) {
  3013. obj.stats = { c: 5 };
  3014. obj.getStats(function (r) { obj.stats.recordTypes = r; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } })
  3015. obj.file.count({}, function (err, count) { obj.stats.meshcentral = { count: count }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  3016. obj.eventsfile.count({}, function (err, count) { obj.stats.events = { count: count }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  3017. obj.powerfile.count({}, function (err, count) { obj.stats.power = { count: count }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  3018. obj.serverstatsfile.count({}, function (err, count) { obj.stats.serverstats = { count: count }; if (--obj.stats.c == 0) { delete obj.stats.c; func(getDbStatsEx(obj.stats)); } });
  3019. }
  3020. // Correct database information of obj.getDbStats before returning it
  3021. function getDbStatsEx(data) {
  3022. var r = {};
  3023. if (data.recordTypes != null) { r = data.recordTypes; }
  3024. try { r.smbios = data['smbios'].count; } catch (ex) { }
  3025. try { r.power = data['power'].count; } catch (ex) { }
  3026. try { r.events = data['events'].count; } catch (ex) { }
  3027. try { r.serverstats = data['serverstats'].count; } catch (ex) { }
  3028. return r;
  3029. }
  3030. // Plugin operations
  3031. if (obj.pluginsActive) {
  3032. obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.pluginsfile.insert(plugin, func); }; // Add a plugin
  3033. obj.getPlugins = function (func) { obj.pluginsfile.find({ 'type': 'plugin' }, { 'type': 0 }).sort({ name: 1 }).exec(func); }; // Get all plugins
  3034. obj.getPlugin = function (id, func) { obj.pluginsfile.find({ _id: id }).sort({ name: 1 }).exec(func); }; // Get plugin
  3035. obj.deletePlugin = function (id, func) { obj.pluginsfile.remove({ _id: id }, func); }; // Delete plugin
  3036. obj.setPluginStatus = function (id, status, func) { obj.pluginsfile.update({ _id: id }, { $set: { status: status } }, func); };
  3037. obj.updatePlugin = function (id, args, func) { delete args._id; obj.pluginsfile.update({ _id: id }, { $set: args }, func); };
  3038. }
  3039. }
  3040. // Get all configuration files
  3041. obj.getAllConfigFiles = function (password, func) {
  3042. obj.GetAllType('cfile', function (err, docs) {
  3043. if (err != null) { func(null); return; }
  3044. var r = null;
  3045. for (var i = 0; i < docs.length; i++) {
  3046. var name = docs[i]._id.split('/')[1];
  3047. var data = obj.decryptData(password, docs[i].data);
  3048. if (data != null) { if (r == null) { r = {}; } r[name] = data; }
  3049. }
  3050. func(r);
  3051. });
  3052. }
  3053. func(obj); // Completed function setup
  3054. }
  3055. // Return a human readable string with current backup configuration
  3056. obj.getBackupConfig = function () {
  3057. var r = '', backupPath = parent.backuppath;
  3058. let dbname = 'meshcentral';
  3059. if (parent.args.mongodbname) { dbname = parent.args.mongodbname; }
  3060. else if ((typeof parent.args.mariadb == 'object') && (typeof parent.args.mariadb.database == 'string')) { dbname = parent.args.mariadb.database; }
  3061. else if ((typeof parent.args.mysql == 'object') && (typeof parent.args.mysql.database == 'string')) { dbname = parent.args.mysql.database; }
  3062. else if ((typeof parent.args.postgres == 'object') && (typeof parent.args.postgres.database == 'string')) { dbname = parent.args.postgres.database; }
  3063. else if (typeof parent.config.settings.sqlite3 == 'string') {dbname = parent.config.settings.sqlite3 + '.sqlite'};
  3064. const currentDate = new Date();
  3065. const fileSuffix = currentDate.getFullYear() + '-' + padNumber(currentDate.getMonth() + 1, 2) + '-' + padNumber(currentDate.getDate(), 2) + '-' + padNumber(currentDate.getHours(), 2) + '-' + padNumber(currentDate.getMinutes(), 2);
  3066. obj.newAutoBackupFile = parent.config.settings.autobackup.backupname + fileSuffix;
  3067. r += 'DB Name: ' + dbname + '\r\n';
  3068. r += 'DB Type: ' + DB_LIST[obj.databaseType] + '\r\n';
  3069. if (parent.config.settings.autobackup.backupintervalhours == -1) {
  3070. r += 'Backup disabled\r\n';
  3071. } else {
  3072. r += 'BackupPath: ' + backupPath + '\r\n';
  3073. r += 'BackupFile: ' + obj.newAutoBackupFile + '.zip\r\n';
  3074. if (parent.config.settings.autobackup.backuphour != null && parent.config.settings.autobackup.backuphour != -1) {
  3075. r += 'Backup between: ' + parent.config.settings.autobackup.backuphour + 'H-' + (parent.config.settings.autobackup.backuphour + 1) + 'H\r\n';
  3076. }
  3077. r += 'Backup Interval (Hours): ' + parent.config.settings.autobackup.backupintervalhours + '\r\n';
  3078. if (parent.config.settings.autobackup.keeplastdaysbackup != null) {
  3079. r += 'Keep Last Backups (Days): ' + parent.config.settings.autobackup.keeplastdaysbackup + '\r\n';
  3080. }
  3081. if (parent.config.settings.autobackup.zippassword != null) {
  3082. r += 'ZIP Password: ';
  3083. if (typeof parent.config.settings.autobackup.zippassword != 'string') { r += 'Bad zippassword type, Backups will not be encrypted\r\n'; }
  3084. else if (parent.config.settings.autobackup.zippassword == "") { r += 'Blank zippassword, Backups will fail\r\n'; }
  3085. else { r += 'Set\r\n'; }
  3086. }
  3087. if (parent.config.settings.autobackup.mongodumppath != null) {
  3088. r += 'MongoDump Path: ';
  3089. if (typeof parent.config.settings.autobackup.mongodumppath != 'string') { r += 'Bad mongodumppath type\r\n'; }
  3090. else { r += parent.config.settings.autobackup.mongodumppath + '\r\n'; }
  3091. }
  3092. if (parent.config.settings.autobackup.mysqldumppath != null) {
  3093. r += 'MySqlDump Path: ';
  3094. if (typeof parent.config.settings.autobackup.mysqldumppath != 'string') { r += 'Bad mysqldump type\r\n'; }
  3095. else { r += parent.config.settings.autobackup.mysqldumppath + '\r\n'; }
  3096. }
  3097. if (parent.config.settings.autobackup.pgdumppath != null) {
  3098. r += 'pgDump Path: ';
  3099. if (typeof parent.config.settings.autobackup.pgdumppath != 'string') { r += 'Bad pgdump type\r\n'; }
  3100. else { r += parent.config.settings.autobackup.pgdumppath + '\r\n'; }
  3101. }
  3102. if (parent.config.settings.autobackup.backupotherfolders) {
  3103. r += 'Backup other folders: ';
  3104. r += parent.filespath + ', ' + parent.recordpath + '\r\n';
  3105. }
  3106. if (parent.config.settings.autobackup.backupwebfolders) {
  3107. r += 'Backup webfolders: ';
  3108. if (parent.webViewsOverridePath) {r += parent.webViewsOverridePath };
  3109. if (parent.webPublicOverridePath) {r += ', '+ parent.webPublicOverridePath};
  3110. if (parent.webEmailsOverridePath) {r += ',' + parent.webEmailsOverridePath};
  3111. r+= '\r\n';
  3112. }
  3113. if (parent.config.settings.autobackup.backupignorefilesglob != []) {
  3114. r += 'Backup IgnoreFilesGlob: ';
  3115. { r += parent.config.settings.autobackup.backupignorefilesglob + '\r\n'; }
  3116. }
  3117. if (parent.config.settings.autobackup.backupskipfoldersglob != []) {
  3118. r += 'Backup SkipFoldersGlob: ';
  3119. { r += parent.config.settings.autobackup.backupskipfoldersglob + '\r\n'; }
  3120. }
  3121. if (typeof parent.config.settings.autobackup.s3 == 'object') {
  3122. r += 'S3 Backups: Enabled\r\n';
  3123. }
  3124. if (typeof parent.config.settings.autobackup.webdav == 'object') {
  3125. r += 'WebDAV Backups: Enabled\r\n';
  3126. r += 'WebDAV backup path: ' + ((typeof parent.config.settings.autobackup.webdav.foldername == 'string') ? parent.config.settings.autobackup.webdav.foldername : 'MeshCentral-Backups') + '\r\n';
  3127. r += 'WebDAV maximum files: '+ ((typeof parent.config.settings.autobackup.webdav.maxfiles == 'number') ? parent.config.settings.autobackup.webdav.maxfiles : 'no limit') + '\r\n';
  3128. }
  3129. if (typeof parent.config.settings.autobackup.googledrive == 'object') {
  3130. r += 'Google Drive Backups: Enabled\r\n';
  3131. }
  3132. }
  3133. return r;
  3134. }
  3135. function buildSqlDumpCommand() {
  3136. var props = (obj.databaseType == DB_MARIADB) ? parent.args.mariadb : parent.args.mysql;
  3137. var mysqldumpPath = 'mysqldump';
  3138. if (parent.config.settings.autobackup && parent.config.settings.autobackup.mysqldumppath) {
  3139. mysqldumpPath = path.normalize(parent.config.settings.autobackup.mysqldumppath);
  3140. }
  3141. var cmd = '\"' + mysqldumpPath + '\" --user=\'' + props.user + '\'';
  3142. // Windows will treat ' as part of the pw. Linux/Unix requires it to escape.
  3143. cmd += (parent.platform == 'win32') ? ' --password=\"' + props.password + '\"' : ' --password=\'' + props.password + '\'';
  3144. if (props.host) { cmd += ' -h ' + props.host; }
  3145. if (props.port) { cmd += ' -P ' + props.port; }
  3146. if (props.awsrds) { cmd += ' --single-transaction'; }
  3147. // SSL options different on mariadb/mysql
  3148. var sslOptions = '';
  3149. if (obj.databaseType == DB_MARIADB) {
  3150. if (props.ssl) {
  3151. sslOptions = ' --ssl';
  3152. if (props.ssl.cacertpath) sslOptions = ' --ssl-ca=' + props.ssl.cacertpath;
  3153. if (props.ssl.dontcheckserveridentity != true) {sslOptions += ' --ssl-verify-server-cert'} else {sslOptions += ' --ssl-verify-server-cert=false'};
  3154. if (props.ssl.clientcertpath) sslOptions += ' --ssl-cert=' + props.ssl.clientcertpath;
  3155. if (props.ssl.clientkeypath) sslOptions += ' --ssl-key=' + props.ssl.clientkeypath;
  3156. }
  3157. } else {
  3158. if (props.ssl) {
  3159. sslOptions = ' --ssl-mode=required';
  3160. if (props.ssl.cacertpath) sslOptions = ' --ssl-ca=' + props.ssl.cacertpath;
  3161. if (props.ssl.dontcheckserveridentity != true) sslOptions += ' --ssl-mode=verify_identity';
  3162. else sslOptions += ' --ssl-mode=required';
  3163. if (props.ssl.clientcertpath) sslOptions += ' --ssl-cert=' + props.ssl.clientcertpath;
  3164. if (props.ssl.clientkeypath) sslOptions += ' --ssl-key=' + props.ssl.clientkeypath;
  3165. }
  3166. }
  3167. cmd += sslOptions;
  3168. var dbname = (props.database) ? props.database : 'meshcentral';
  3169. cmd += ' ' + dbname
  3170. return cmd;
  3171. }
  3172. function buildMongoDumpCommand() {
  3173. const dburl = parent.args.mongodb;
  3174. var mongoDumpPath = 'mongodump';
  3175. if (parent.config.settings.autobackup && parent.config.settings.autobackup.mongodumppath) {
  3176. mongoDumpPath = path.normalize(parent.config.settings.autobackup.mongodumppath);
  3177. }
  3178. var cmd = '"' + mongoDumpPath + '"';
  3179. if (dburl) { cmd = '\"' + mongoDumpPath + '\" --uri=\"' + dburl + '\"'; }
  3180. if (parent.config.settings.autobackup?.mongodumpargs) {
  3181. cmd = '\"' + mongoDumpPath + '\" ' + parent.config.settings.autobackup.mongodumpargs;
  3182. if (!parent.config.settings.autobackup.mongodumpargs.includes("--db=")) {cmd += ' --db=' + (parent.config.settings.mongodbname ? parent.config.settings.mongodbname : 'meshcentral')};
  3183. }
  3184. return cmd;
  3185. }
  3186. // Check that the server is capable of performing a backup
  3187. // Tries configured custom location with fallback to default location
  3188. // Now runs after autobackup config init in meshcentral.js so config options are checked
  3189. obj.checkBackupCapability = function (func) {
  3190. if (parent.config.settings.autobackup.backupintervalhours == -1) { return; };
  3191. //block backup until validated. Gets put back if all checks are ok.
  3192. let backupInterval = parent.config.settings.autobackup.backupintervalhours;
  3193. parent.config.settings.autobackup.backupintervalhours = -1;
  3194. let backupPath = parent.backuppath;
  3195. if (backupPath.startsWith(parent.datapath)) {
  3196. func(1, "Backup path can't be set within meshcentral-data folder. No backups will be made.");
  3197. return;
  3198. }
  3199. // Check create/write backupdir
  3200. try { fs.mkdirSync(backupPath); }
  3201. catch (e) {
  3202. // EEXIST error = dir already exists
  3203. if (e.code != 'EEXIST' ) {
  3204. //Unable to create backuppath
  3205. console.error(e.message);
  3206. func(1, 'Unable to create ' + backupPath + '. No backups will be made. Error: ' + e.message);
  3207. return;
  3208. }
  3209. }
  3210. const currentDate = new Date();
  3211. const fileSuffix = currentDate.getFullYear() + '-' + padNumber(currentDate.getMonth() + 1, 2) + '-' + padNumber(currentDate.getDate(), 2) + '-' + padNumber(currentDate.getHours(), 2) + '-' + padNumber(currentDate.getMinutes(), 2);
  3212. const testFile = path.join(backupPath, parent.config.settings.autobackup.backupname + fileSuffix + '.zip');
  3213. try { fs.writeFileSync( testFile, "DeleteMe"); }
  3214. catch (e) {
  3215. //Unable to create file
  3216. console.error (e.message);
  3217. func(1, "Backuppath (" + backupPath + ") can't be written to. No backups will be made. Error: " + e.message);
  3218. return;
  3219. }
  3220. try { fs.unlinkSync(testFile); parent.debug('backup', 'Backuppath ' + backupPath + ' accesscheck successful');}
  3221. catch (e) {
  3222. console.error (e.message);
  3223. func(1, "Backuppathtestfile (" + testFile + ") can't be deleted, check filerights. Error: " + e.message);
  3224. // Assume write rights, no delete rights. Continue with warning.
  3225. //return;
  3226. }
  3227. // Check database dumptools
  3228. if ((obj.databaseType == DB_MONGOJS) || (obj.databaseType == DB_MONGODB)) {
  3229. // Check that we have access to MongoDump
  3230. var cmd = buildMongoDumpCommand();
  3231. cmd += (parent.platform == 'win32') ? ' --archive=\"nul\"' : ' --archive=\"/dev/null\"';
  3232. const child_process = require('child_process');
  3233. child_process.exec(cmd, { cwd: backupPath }, function (error, stdout, stderr) {
  3234. if ((error != null) && (error != '')) {
  3235. func(1, "Mongodump error, backup will not be performed. Check path or use mongodumppath & mongodumpargs");
  3236. return;
  3237. } else {parent.config.settings.autobackup.backupintervalhours = backupInterval;}
  3238. });
  3239. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL)) {
  3240. // Check that we have access to mysqldump
  3241. var cmd = buildSqlDumpCommand();
  3242. cmd += ' > ' + ((parent.platform == 'win32') ? '\"nul\"' : '\"/dev/null\"');
  3243. const child_process = require('child_process');
  3244. child_process.exec(cmd, { cwd: backupPath, timeout: 1000*30 }, function(error, stdout, stdin) {
  3245. if ((error != null) && (error != '')) {
  3246. func(1, "mysqldump error, backup will not be performed. Check path or use mysqldumppath");
  3247. return;
  3248. } else {parent.config.settings.autobackup.backupintervalhours = backupInterval;}
  3249. });
  3250. } else if (obj.databaseType == DB_POSTGRESQL) {
  3251. // Check that we have access to pg_dump
  3252. parent.config.settings.autobackup.pgdumppath = path.normalize(parent.config.settings.autobackup.pgdumppath ? parent.config.settings.autobackup.pgdumppath : 'pg_dump');
  3253. let cmd = '"' + parent.config.settings.autobackup.pgdumppath + '"'
  3254. + ' --dbname=postgresql://' + encodeURIComponent(parent.config.settings.postgres.user) + ":" + encodeURIComponent(parent.config.settings.postgres.password)
  3255. + "@" + parent.config.settings.postgres.host + ":" + parent.config.settings.postgres.port + "/" + encodeURIComponent(databaseName)
  3256. + ' > ' + ((parent.platform == 'win32') ? '\"nul\"' : '\"/dev/null\"');
  3257. const child_process = require('child_process');
  3258. child_process.exec(cmd, { cwd: backupPath }, function(error, stdout, stdin) {
  3259. if ((error != null) && (error != '')) {
  3260. func(1, "pg_dump error, backup will not be performed. Check path or use pgdumppath.");
  3261. return;
  3262. } else {parent.config.settings.autobackup.backupintervalhours = backupInterval;}
  3263. });
  3264. } else {
  3265. //all ok, enable backup
  3266. parent.config.settings.autobackup.backupintervalhours = backupInterval;}
  3267. }
  3268. // MongoDB pending bulk read operation, perform fast bulk document reads.
  3269. function fileBulkReadCompleted(err, docs) {
  3270. // Send out callbacks with results
  3271. if (docs != null) {
  3272. for (var i in docs) {
  3273. if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); }
  3274. const id = docs[i]._id;
  3275. if (obj.filePendingGets[id] != null) {
  3276. for (var j in obj.filePendingGets[id]) {
  3277. if (typeof obj.filePendingGets[id][j] == 'function') { obj.filePendingGets[id][j](err, performTypedRecordDecrypt([docs[i]])); }
  3278. }
  3279. delete obj.filePendingGets[id];
  3280. }
  3281. }
  3282. }
  3283. // If there are not results, send out a null callback
  3284. for (var i in obj.filePendingGets) { for (var j in obj.filePendingGets[i]) { obj.filePendingGets[i][j](err, []); } }
  3285. // Move on to process any more pending get operations
  3286. obj.filePendingGets = obj.filePendingGet;
  3287. obj.filePendingGet = null;
  3288. if (obj.filePendingGets != null) {
  3289. var findlist = [];
  3290. for (var i in obj.filePendingGets) { findlist.push(i); }
  3291. obj.file.find({ _id: { $in: findlist } }).toArray(fileBulkReadCompleted);
  3292. }
  3293. }
  3294. // MongoDB pending bulk remove operation, perform fast bulk document removes.
  3295. function fileBulkRemoveCompleted(err) {
  3296. // Send out callbacks
  3297. for (var i in obj.filePendingRemoves) {
  3298. for (var j in obj.filePendingRemoves[i]) {
  3299. if (typeof obj.filePendingRemoves[i][j] == 'function') { obj.filePendingRemoves[i][j](err); }
  3300. }
  3301. }
  3302. // Move on to process any more pending get operations
  3303. obj.filePendingRemoves = obj.filePendingRemove;
  3304. obj.filePendingRemove = null;
  3305. if (obj.filePendingRemoves != null) {
  3306. obj.dbCounters.fileRemoveBulk++;
  3307. var findlist = [], count = 0;
  3308. for (var i in obj.filePendingRemoves) { findlist.push(i); count++; }
  3309. obj.file.deleteMany({ _id: { $in: findlist } }, { multi: true }, fileBulkRemoveCompleted);
  3310. }
  3311. }
  3312. // MongoDB pending bulk write operation, perform fast bulk document replacement.
  3313. function fileBulkWriteCompleted() {
  3314. // Callbacks
  3315. if (obj.filePendingCbs != null) {
  3316. for (var i in obj.filePendingCbs) { if (typeof obj.filePendingCbs[i] == 'function') { obj.filePendingCbs[i](); } }
  3317. obj.filePendingCbs = null;
  3318. }
  3319. if (obj.filePendingSets != null) {
  3320. // Perform pending operations
  3321. obj.dbCounters.fileSetBulk++;
  3322. var ops = [];
  3323. obj.filePendingCbs = obj.filePendingCb;
  3324. obj.filePendingCb = null;
  3325. for (var i in obj.filePendingSets) { ops.push({ replaceOne: { filter: { _id: i }, replacement: performTypedRecordEncrypt(common.escapeLinksFieldNameEx(obj.filePendingSets[i])), upsert: true } }); }
  3326. obj.file.bulkWrite(ops, fileBulkWriteCompleted);
  3327. obj.filePendingSets = null;
  3328. } else {
  3329. // All done, no pending operations.
  3330. obj.filePendingSet = false;
  3331. }
  3332. }
  3333. // MongoDB pending bulk write operation, perform fast bulk document replacement.
  3334. function eventsFileBulkWriteCompleted() {
  3335. // Callbacks
  3336. if (obj.eventsFilePendingCbs != null) { for (var i in obj.eventsFilePendingCbs) { obj.eventsFilePendingCbs[i](); } obj.eventsFilePendingCbs = null; }
  3337. if (obj.eventsFilePendingSets != null) {
  3338. // Perform pending operations
  3339. obj.dbCounters.eventsSetBulk++;
  3340. var ops = [];
  3341. for (var i in obj.eventsFilePendingSets) { ops.push({ insertOne: { document: obj.eventsFilePendingSets[i] } }); }
  3342. obj.eventsFilePendingCbs = obj.eventsFilePendingCb;
  3343. obj.eventsFilePendingCb = null;
  3344. obj.eventsFilePendingSets = null;
  3345. obj.eventsfile.bulkWrite(ops, eventsFileBulkWriteCompleted);
  3346. } else {
  3347. // All done, no pending operations.
  3348. obj.eventsFilePendingSet = false;
  3349. }
  3350. }
  3351. // MongoDB pending bulk write operation, perform fast bulk document replacement.
  3352. function powerFileBulkWriteCompleted() {
  3353. // Callbacks
  3354. if (obj.powerFilePendingCbs != null) { for (var i in obj.powerFilePendingCbs) { obj.powerFilePendingCbs[i](); } obj.powerFilePendingCbs = null; }
  3355. if (obj.powerFilePendingSets != null) {
  3356. // Perform pending operations
  3357. obj.dbCounters.powerSetBulk++;
  3358. var ops = [];
  3359. for (var i in obj.powerFilePendingSets) { ops.push({ insertOne: { document: obj.powerFilePendingSets[i] } }); }
  3360. obj.powerFilePendingCbs = obj.powerFilePendingCb;
  3361. obj.powerFilePendingCb = null;
  3362. obj.powerFilePendingSets = null;
  3363. obj.powerfile.bulkWrite(ops, powerFileBulkWriteCompleted);
  3364. } else {
  3365. // All done, no pending operations.
  3366. obj.powerFilePendingSet = false;
  3367. }
  3368. }
  3369. // Perform a server backup
  3370. obj.performBackup = function (func) {
  3371. parent.debug('backup','Entering performBackup');
  3372. try {
  3373. if (obj.performingBackup) return 'Backup alreay in progress.';
  3374. if (parent.config.settings.autobackup.backupintervalhours == -1) { if (func) { func('Backup disabled.'); return 'Backup disabled.' }};
  3375. obj.performingBackup = true;
  3376. let backupPath = parent.backuppath;
  3377. let dataPath = parent.datapath;
  3378. const currentDate = new Date();
  3379. const fileSuffix = currentDate.getFullYear() + '-' + padNumber(currentDate.getMonth() + 1, 2) + '-' + padNumber(currentDate.getDate(), 2) + '-' + padNumber(currentDate.getHours(), 2) + '-' + padNumber(currentDate.getMinutes(), 2);
  3380. obj.newAutoBackupFile = path.join(backupPath, parent.config.settings.autobackup.backupname + fileSuffix + '.zip');
  3381. parent.debug('backup','newAutoBackupFile=' + obj.newAutoBackupFile);
  3382. if ((obj.databaseType == DB_MONGOJS) || (obj.databaseType == DB_MONGODB)) {
  3383. // Perform a MongoDump
  3384. const dbname = (parent.args.mongodbname) ? (parent.args.mongodbname) : 'meshcentral';
  3385. const dburl = parent.args.mongodb;
  3386. obj.newDBDumpFile = path.join(backupPath, (dbname + '-mongodump-' + fileSuffix + '.archive'));
  3387. var cmd = buildMongoDumpCommand();
  3388. cmd += (dburl) ? ' --archive=\"' + obj.newDBDumpFile + '\"' :
  3389. ' --db=\"' + dbname + '\" --archive=\"' + obj.newDBDumpFile + '\"';
  3390. parent.debug('backup','Mongodump cmd: ' + cmd);
  3391. const child_process = require('child_process');
  3392. const dumpProcess = child_process.exec(
  3393. cmd,
  3394. { cwd: parent.parentpath },
  3395. (error)=> {if (error) {obj.backupStatus |= BACKUPFAIL_DBDUMP; console.error('ERROR: Unable to perform MongoDB backup: ' + error + '\r\n'); obj.createBackupfile(func);}}
  3396. );
  3397. dumpProcess.on('exit', (code) => {
  3398. if (code != 0) {console.log(`Mongodump child process exited with code ${code}`); obj.backupStatus |= BACKUPFAIL_DBDUMP;}
  3399. obj.createBackupfile(func);
  3400. });
  3401. } else if ((obj.databaseType == DB_MARIADB) || (obj.databaseType == DB_MYSQL)) {
  3402. // Perform a MySqlDump backup
  3403. const newBackupFile = 'mysqldump-' + fileSuffix;
  3404. obj.newDBDumpFile = path.join(backupPath, newBackupFile + '.sql');
  3405. var cmd = buildSqlDumpCommand();
  3406. cmd += ' --result-file=\"' + obj.newDBDumpFile + '\"';
  3407. parent.debug('backup','Maria/MySQLdump cmd: ' + cmd);
  3408. const child_process = require('child_process');
  3409. const dumpProcess = child_process.exec(
  3410. cmd,
  3411. { cwd: parent.parentpath },
  3412. (error)=> {if (error) {obj.backupStatus |= BACKUPFAIL_DBDUMP; console.error('ERROR: Unable to perform MySQL backup: ' + error + '\r\n'); obj.createBackupfile(func);}}
  3413. );
  3414. dumpProcess.on('exit', (code) => {
  3415. if (code != 0) {console.error(`MySQLdump child process exited with code ${code}`); obj.backupStatus |= BACKUPFAIL_DBDUMP;}
  3416. obj.createBackupfile(func);
  3417. });
  3418. } else if (obj.databaseType == DB_SQLITE) {
  3419. //.db3 suffix to escape escape backupfile glob to exclude the sqlite db files
  3420. obj.newDBDumpFile = path.join(backupPath, databaseName + '-sqlitedump-' + fileSuffix + '.db3');
  3421. // do a VACUUM INTO in favor of the backup API to compress the export, see https://www.sqlite.org/backup.html
  3422. parent.debug('backup','SQLitedump: VACUUM INTO ' + obj.newDBDumpFile);
  3423. obj.file.exec('VACUUM INTO \'' + obj.newDBDumpFile + '\'', function (err) {
  3424. if (err) { console.error('SQLite backup error: ' + err); obj.backupStatus |=BACKUPFAIL_DBDUMP;};
  3425. //always finish/clean up
  3426. obj.createBackupfile(func);
  3427. });
  3428. } else if (obj.databaseType == DB_POSTGRESQL) {
  3429. // Perform a PostgresDump backup
  3430. const newBackupFile = 'pgdump-' + fileSuffix + '.sql';
  3431. obj.newDBDumpFile = path.join(backupPath, newBackupFile);
  3432. let cmd = '"' + parent.config.settings.autobackup.pgdumppath + '"'
  3433. + ' --dbname=postgresql://' + encodeURIComponent(parent.config.settings.postgres.user) + ":" + encodeURIComponent(parent.config.settings.postgres.password)
  3434. + "@" + parent.config.settings.postgres.host + ":" + parent.config.settings.postgres.port + "/" + encodeURIComponent(databaseName)
  3435. + " --file=" + obj.newDBDumpFile;
  3436. parent.debug('backup','Postgresqldump cmd: ' + cmd);
  3437. const child_process = require('child_process');
  3438. const dumpProcess = child_process.exec(
  3439. cmd,
  3440. { cwd: dataPath },
  3441. (error)=> {if (error) {obj.backupStatus |= BACKUPFAIL_DBDUMP; console.log('ERROR: Unable to perform PostgreSQL dump: ' + error.message + '\r\n'); obj.createBackupfile(func);}}
  3442. );
  3443. dumpProcess.on('exit', (code) => {
  3444. if (code != 0) {console.log(`PostgreSQLdump child process exited with code: ` + code); obj.backupStatus |= BACKUPFAIL_DBDUMP;}
  3445. obj.createBackupfile(func);
  3446. });
  3447. } else {
  3448. // NeDB/Acebase backup, no db dump needed, just make a file backup
  3449. obj.createBackupfile(func);
  3450. }
  3451. } catch (ex) { console.error(ex); parent.addServerWarning( 'Something went wrong during performBackup, check errorlog: ' +ex.message, true); };
  3452. return 'Starting auto-backup...';
  3453. };
  3454. obj.createBackupfile = function(func) {
  3455. parent.debug('backup', 'Entering createBackupfile');
  3456. let archiver = require('archiver');
  3457. let archive = null;
  3458. let zipLevel = Math.min(Math.max(Number(parent.config.settings.autobackup.zipcompression ? parent.config.settings.autobackup.zipcompression : 5),1),9);
  3459. //if password defined, create encrypted zip
  3460. if (parent.config.settings.autobackup && (typeof parent.config.settings.autobackup.zippassword == 'string')) {
  3461. try {
  3462. //Only register format once, otherwise it triggers an error
  3463. if (archiver.isRegisteredFormat('zip-encrypted') == false) { archiver.registerFormat('zip-encrypted', require('archiver-zip-encrypted')); }
  3464. archive = archiver.create('zip-encrypted', { zlib: { level: zipLevel }, encryptionMethod: 'aes256', password: parent.config.settings.autobackup.zippassword });
  3465. if (func) { func('Creating encrypted ZIP'); }
  3466. } catch (ex) { // registering encryption failed, do not fall back to non-encrypted, fail backup and skip old backup removal as a precaution to not lose any backups
  3467. obj.backupStatus |= BACKUPFAIL_ZIPMODULE;
  3468. if (func) { func('Zipencryptionmodule failed, aborting');}
  3469. console.error('Zipencryptionmodule failed, aborting');
  3470. }
  3471. } else {
  3472. if (func) { func('Creating a NON-ENCRYPTED ZIP'); }
  3473. archive = archiver('zip', { zlib: { level: zipLevel } });
  3474. }
  3475. //original behavior, just a filebackup if dbdump fails : (obj.backupStatus == 0 || obj.backupStatus == BACKUPFAIL_DBDUMP)
  3476. if (obj.backupStatus == 0) {
  3477. // Zip the data directory with the dbdump|NeDB files
  3478. let output = fs.createWriteStream(obj.newAutoBackupFile);
  3479. // Archive finalized and closed
  3480. output.on('close', function () {
  3481. if (obj.backupStatus == 0) {
  3482. let mesg = 'Auto-backup completed: ' + obj.newAutoBackupFile + ', backup-size: ' + ((archive.pointer() / 1048576).toFixed(2)) + "Mb";
  3483. console.log(mesg);
  3484. if (func) { func(mesg); };
  3485. obj.performCloudBackup(obj.newAutoBackupFile, func);
  3486. obj.removeExpiredBackupfiles(func);
  3487. } else {
  3488. let mesg = 'Zipbackup failed (' + obj.backupStatus.toString(2).slice(-8) + '), deleting incomplete backup: ' + obj.newAutoBackupFile;
  3489. if (func) { func(mesg) }
  3490. else { parent.addServerWarning(mesg, true ) };
  3491. if (fs.existsSync(obj.newAutoBackupFile)) { fs.unlink(obj.newAutoBackupFile, function (err) { if (err) {console.error('Failed to clean up backupfile: ' + err.message)} }) };
  3492. };
  3493. if (obj.databaseType != DB_NEDB) {
  3494. //remove dump archive file, because zipped and otherwise fills up
  3495. if (fs.existsSync(obj.newDBDumpFile)) { fs.unlink(obj.newDBDumpFile, function (err) { if (err) {console.error('Failed to clean up dbdump file: ' + err.message) } }) };
  3496. };
  3497. obj.performingBackup = false;
  3498. obj.backupStatus = 0x0;
  3499. }
  3500. );
  3501. output.on('end', function () { });
  3502. output.on('error', function (err) {
  3503. if ((obj.backupStatus & BACKUPFAIL_ZIPCREATE) == 0) {
  3504. console.error('Output error: ' + err.message);
  3505. if (func) { func('Output error: ' + err.message); };
  3506. obj.backupStatus |= BACKUPFAIL_ZIPCREATE;
  3507. archive.abort();
  3508. };
  3509. });
  3510. archive.on('warning', function (err) {
  3511. //if files added to the archiver object aren't reachable anymore (e.g. sqlite-journal files)
  3512. //an ENOENT warning is given, but the archiver module has no option to/does not skip/resume
  3513. //so the backup needs te be aborted as it otherwise leaves an incomplete zip and never 'ends'
  3514. if ((obj.backupStatus & BACKUPFAIL_ZIPCREATE) == 0) {
  3515. console.log('Zip warning: ' + err.message);
  3516. if (func) { func('Zip warning: ' + err.message); };
  3517. obj.backupStatus |= BACKUPFAIL_ZIPCREATE;
  3518. archive.abort();
  3519. };
  3520. });
  3521. archive.on('error', function (err) {
  3522. if ((obj.backupStatus & BACKUPFAIL_ZIPCREATE) == 0) {
  3523. console.error('Zip error: ' + err.message);
  3524. if (func) { func('Zip error: ' + err.message); };
  3525. obj.backupStatus |= BACKUPFAIL_ZIPCREATE;
  3526. archive.abort();
  3527. }
  3528. });
  3529. archive.pipe(output);
  3530. let globIgnoreFiles;
  3531. //slice in case exclusion gets pushed
  3532. globIgnoreFiles = parent.config.settings.autobackup.backupignorefilesglob ? parent.config.settings.autobackup.backupignorefilesglob.slice() : [];
  3533. if (parent.config.settings.sqlite3) { globIgnoreFiles.push (datapathFoldername + '/' + databaseName + '.sqlite*'); }; //skip sqlite database file, and temp files with ext -journal, -wal & -shm
  3534. //archiver.glob doesn't seem to use the third param, archivesubdir. Bug?
  3535. //workaround: go up a dir and add data dir explicitly to keep the zip tidy
  3536. archive.glob((datapathFoldername + '/**'), {
  3537. cwd: datapathParentPath,
  3538. ignore: globIgnoreFiles,
  3539. skip: (parent.config.settings.autobackup.backupskipfoldersglob ? parent.config.settings.autobackup.backupskipfoldersglob : [])
  3540. });
  3541. if (parent.config.settings.autobackup.backupwebfolders) {
  3542. if (parent.webViewsOverridePath) { archive.directory(parent.webViewsOverridePath, 'meshcentral-views'); }
  3543. if (parent.webPublicOverridePath) { archive.directory(parent.webPublicOverridePath, 'meshcentral-public'); }
  3544. if (parent.webEmailsOverridePath) { archive.directory(parent.webEmailsOverridePath, 'meshcentral-emails'); }
  3545. };
  3546. if (parent.config.settings.autobackup.backupotherfolders) {
  3547. archive.directory(parent.filespath, 'meshcentral-files');
  3548. archive.directory(parent.recordpath, 'meshcentral-recordings');
  3549. };
  3550. //add dbdump to the root of the zip
  3551. if (obj.newDBDumpFile != null) archive.file(obj.newDBDumpFile, { name: path.basename(obj.newDBDumpFile) });
  3552. archive.finalize();
  3553. } else {
  3554. //failed somewhere before zipping
  3555. console.error('Backup failed ('+ obj.backupStatus.toString(2).slice(-8) + ')');
  3556. if (func) { func('Backup failed ('+ obj.backupStatus.toString(2).slice(-8) + ')') }
  3557. else {
  3558. parent.addServerWarning('Backup failed ('+ obj.backupStatus.toString(2).slice(-8) + ')', true);
  3559. }
  3560. //Just in case something's there
  3561. if (fs.existsSync(obj.newDBDumpFile)) { fs.unlink(obj.newDBDumpFile, function (err) { if (err) {console.error('Failed to clean up dbdump file: ' + err.message) } }); };
  3562. obj.backupStatus = 0x0;
  3563. obj.performingBackup = false;
  3564. };
  3565. };
  3566. // Remove expired backupfiles by filenamedate
  3567. obj.removeExpiredBackupfiles = function (func) {
  3568. if (parent.config.settings.autobackup && (typeof parent.config.settings.autobackup.keeplastdaysbackup == 'number')) {
  3569. let cutoffDate = new Date();
  3570. cutoffDate.setDate(cutoffDate.getDate() - parent.config.settings.autobackup.keeplastdaysbackup);
  3571. fs.readdir(parent.backuppath, function (err, dir) {
  3572. try {
  3573. if (err == null) {
  3574. if (dir.length > 0) {
  3575. let fileName = parent.config.settings.autobackup.backupname;
  3576. let checked = 0;
  3577. let removed = 0;
  3578. for (var i in dir) {
  3579. var name = dir[i];
  3580. parent.debug('backup', "checking file: ", path.join(parent.backuppath, name));
  3581. if (name.startsWith(fileName) && name.endsWith('.zip')) {
  3582. var timex = name.substring(fileName.length, name.length - 4).split('-');
  3583. if (timex.length == 5) {
  3584. checked++;
  3585. var fileDate = new Date(parseInt(timex[0]), parseInt(timex[1]) - 1, parseInt(timex[2]), parseInt(timex[3]), parseInt(timex[4]));
  3586. if (fileDate && (cutoffDate > fileDate)) {
  3587. console.log("Removing expired backup file: ", path.join(parent.backuppath, name));
  3588. fs.unlink(path.join(parent.backuppath, name), function (err) { if (err) { console.error(err.message); if (func) {func('Error removing: ' + err.message); } } });
  3589. removed++;
  3590. }
  3591. }
  3592. else { parent.debug('backup', "file: " + name + " timestamp failure: ", timex); }
  3593. }
  3594. }
  3595. let mesg= 'Checked ' + checked + ' candidates in ' + parent.backuppath + '. Removed ' + removed + ' expired backupfiles using cutoffDate: '+ cutoffDate.toLocaleString('default', { dateStyle: 'short', timeStyle: 'short' });
  3596. parent.debug (mesg);
  3597. if (func) { func(mesg); }
  3598. } else { console.error('No files found in ' + parent.backuppath + '. There should be at least one.')}
  3599. }
  3600. else
  3601. { console.error(err); parent.addServerWarning( 'Reading files in backup directory ' + parent.backuppath + ' failed, check errorlog: ' + err.message, true); }
  3602. } catch (ex) { console.error(ex); parent.addServerWarning( 'Something went wrong during removeExpiredBackupfiles, check errorlog: ' +ex.message, true); }
  3603. });
  3604. }
  3605. }
  3606. async function webDAVBackup(filename, func) {
  3607. try {
  3608. const webDAV = await import ('webdav');
  3609. const wdConfig = parent.config.settings.autobackup.webdav;
  3610. const client = webDAV.createClient(wdConfig.url, {
  3611. username: wdConfig.username,
  3612. password: wdConfig.password,
  3613. maxContentLength: Infinity,
  3614. maxBodyLength: Infinity
  3615. });
  3616. if (await client.exists(wdConfig.foldername) === false) {
  3617. await client.createDirectory(wdConfig.foldername, { recursive: true});
  3618. } else {
  3619. // Clean up our WebDAV folder
  3620. if ((typeof wdConfig.maxfiles == 'number') && (wdConfig.maxfiles > 1)) {
  3621. const fileName = parent.config.settings.autobackup.backupname;
  3622. //only files matching our backupfilename
  3623. let files = await client.getDirectoryContents(wdConfig.foldername, { deep: false, glob: "/**/" + fileName + "*.zip" });
  3624. const xdateTimeSort = function (a, b) { if (a.xdate > b.xdate) return 1; if (a.xdate < b.xdate) return -1; return 0; }
  3625. for (const i in files) { files[i].xdate = new Date(files[i].lastmod); }
  3626. files.sort(xdateTimeSort);
  3627. while (files.length >= wdConfig.maxfiles) {
  3628. let delFile = files.shift().filename;
  3629. await client.deleteFile(delFile);
  3630. console.log('WebDAV file deleted: ' + delFile); if (func) { func('WebDAV file deleted: ' + delFile); }
  3631. }
  3632. }
  3633. }
  3634. // Upload to the WebDAV folder
  3635. const { pipeline } = require('stream/promises');
  3636. await pipeline(fs.createReadStream(filename), client.createWriteStream( wdConfig.foldername + path.basename(filename)));
  3637. console.log('WebDAV upload completed: ' + wdConfig.foldername + path.basename(filename)); if (func) { func('WebDAV upload completed: ' + wdConfig.foldername + path.basename(filename)); }
  3638. }
  3639. catch(err) {
  3640. console.error('WebDAV error: ' + err.message); if (func) { func('WebDAV error: ' + err.message);}
  3641. }
  3642. }
  3643. // Perform cloud backup
  3644. obj.performCloudBackup = function (filename, func) {
  3645. // WebDAV Backup
  3646. if ((typeof parent.config.settings.autobackup == 'object') && (typeof parent.config.settings.autobackup.webdav == 'object')) {
  3647. parent.debug( 'backup', 'Entering WebDAV backup'); if (func) { func('Entering WebDAV backup.'); }
  3648. webDAVBackup(filename, func);
  3649. }
  3650. // Google Drive Backup
  3651. if ((typeof parent.config.settings.autobackup == 'object') && (typeof parent.config.settings.autobackup.googledrive == 'object')) {
  3652. parent.debug( 'backup', 'Entering Google Drive backup');
  3653. obj.Get('GoogleDriveBackup', function (err, docs) {
  3654. if ((err != null) || (docs.length != 1) || (docs[0].state != 3)) return;
  3655. if (func) { func('Attempting Google Drive upload...'); }
  3656. const {google} = require('googleapis');
  3657. const oAuth2Client = new google.auth.OAuth2(docs[0].clientid, docs[0].clientsecret, "urn:ietf:wg:oauth:2.0:oob");
  3658. oAuth2Client.on('tokens', function (tokens) { if (tokens.refresh_token) { docs[0].token = tokens.refresh_token; parent.db.Set(docs[0]); } }); // Update the token in the database
  3659. oAuth2Client.setCredentials(docs[0].token);
  3660. const drive = google.drive({ version: 'v3', auth: oAuth2Client });
  3661. const createdTimeSort = function (a, b) { if (a.createdTime > b.createdTime) return 1; if (a.createdTime < b.createdTime) return -1; return 0; }
  3662. // Called once we know our folder id, clean up and upload a backup.
  3663. var useGoogleDrive = function (folderid) {
  3664. // List files to see if we need to delete older ones
  3665. if (typeof parent.config.settings.autobackup.googledrive.maxfiles == 'number') {
  3666. drive.files.list({
  3667. q: 'trashed = false and \'' + folderid + '\' in parents',
  3668. fields: 'nextPageToken, files(id, name, size, createdTime)',
  3669. }, function (err, res) {
  3670. if (err) {
  3671. console.log('GoogleDrive (files.list) error: ' + err);
  3672. if (func) { func('GoogleDrive (files.list) error: ' + err); }
  3673. return;
  3674. }
  3675. // Delete any old files if more than 10 files are present in the backup folder.
  3676. res.data.files.sort(createdTimeSort);
  3677. while (res.data.files.length >= parent.config.settings.autobackup.googledrive.maxfiles) { drive.files.delete({ fileId: res.data.files.shift().id }, function (err, res) { }); }
  3678. });
  3679. }
  3680. //console.log('Uploading...');
  3681. if (func) { func('Uploading to Google Drive...'); }
  3682. // Upload the backup
  3683. drive.files.create({
  3684. requestBody: { name: require('path').basename(filename), mimeType: 'text/plain', parents: [folderid] },
  3685. media: { mimeType: 'application/zip', body: require('fs').createReadStream(filename) },
  3686. }, function (err, res) {
  3687. if (err) {
  3688. console.log('GoogleDrive (files.create) error: ' + err);
  3689. if (func) { func('GoogleDrive (files.create) error: ' + err); }
  3690. return;
  3691. }
  3692. //console.log('Upload done.');
  3693. if (func) { func('Google Drive upload completed.'); }
  3694. });
  3695. }
  3696. // Fetch the folder name
  3697. var folderName = 'MeshCentral-Backups';
  3698. if (typeof parent.config.settings.autobackup.googledrive.foldername == 'string') { folderName = parent.config.settings.autobackup.googledrive.foldername; }
  3699. // Find our backup folder, create one if needed.
  3700. drive.files.list({
  3701. q: 'mimeType = \'application/vnd.google-apps.folder\' and name=\'' + folderName + '\' and trashed = false',
  3702. fields: 'nextPageToken, files(id, name)',
  3703. }, function (err, res) {
  3704. if (err) {
  3705. console.log('GoogleDrive error: ' + err);
  3706. if (func) { func('GoogleDrive error: ' + err); }
  3707. return;
  3708. }
  3709. if (res.data.files.length == 0) {
  3710. // Create a folder
  3711. drive.files.create({ resource: { 'name': folderName, 'mimeType': 'application/vnd.google-apps.folder' }, fields: 'id' }, function (err, file) {
  3712. if (err) {
  3713. console.log('GoogleDrive (folder.create) error: ' + err);
  3714. if (func) { func('GoogleDrive (folder.create) error: ' + err); }
  3715. return;
  3716. }
  3717. useGoogleDrive(file.data.id);
  3718. });
  3719. } else { useGoogleDrive(res.data.files[0].id); }
  3720. });
  3721. });
  3722. }
  3723. // S3 Backup
  3724. if ((typeof parent.config.settings.autobackup == 'object') && (typeof parent.config.settings.autobackup.s3 == 'object')) {
  3725. parent.debug( 'backup', 'Entering S3 backup');
  3726. var s3folderName = 'MeshCentral-Backups';
  3727. if (typeof parent.config.settings.autobackup.s3.foldername == 'string') { s3folderName = parent.config.settings.autobackup.s3.foldername; }
  3728. // Construct the config object
  3729. var accessKey = parent.config.settings.autobackup.s3.accesskey,
  3730. secretKey = parent.config.settings.autobackup.s3.secretkey,
  3731. endpoint = parent.config.settings.autobackup.s3.endpoint ? parent.config.settings.autobackup.s3.endpoint : 's3.amazonaws.com',
  3732. port = parent.config.settings.autobackup.s3.port ? parent.config.settings.autobackup.s3.port : 443,
  3733. useSsl = parent.config.settings.autobackup.s3.ssl ? parent.config.settings.autobackup.s3.ssl : true,
  3734. bucketName = parent.config.settings.autobackup.s3.bucketname,
  3735. pathPrefix = s3folderName,
  3736. threshold = parent.config.settings.autobackup.s3.maxfiles ? parent.config.settings.autobackup.s3.maxfiles : 0,
  3737. fileToUpload = filename;
  3738. // Create a MinIO client
  3739. const Minio = require('minio');
  3740. var minioClient = new Minio.Client({
  3741. endPoint: endpoint,
  3742. port: port,
  3743. useSSL: useSsl,
  3744. accessKey: accessKey,
  3745. secretKey: secretKey
  3746. });
  3747. // List objects in the specified bucket and path prefix
  3748. var listObjectsPromise = new Promise(function(resolve, reject) {
  3749. var items = [];
  3750. var stream = minioClient.listObjects(bucketName, pathPrefix, true);
  3751. stream.on('data', function(item) {
  3752. if (!item.name.endsWith('/')) { // Exclude directories
  3753. items.push(item);
  3754. }
  3755. });
  3756. stream.on('end', function() {
  3757. resolve(items);
  3758. });
  3759. stream.on('error', function(err) {
  3760. reject(err);
  3761. });
  3762. });
  3763. listObjectsPromise.then(function(objects) {
  3764. // Count the number of files
  3765. var fileCount = objects.length;
  3766. // Return if no files to carry on uploading
  3767. if (fileCount === 0) { return Promise.resolve(); }
  3768. // Sort the files by LastModified date (oldest first)
  3769. objects.sort(function(a, b) { return new Date(a.lastModified) - new Date(b.lastModified); });
  3770. // Check if the threshold is zero and return if
  3771. if (threshold === 0) { return Promise.resolve(); }
  3772. // Check if the number of files exceeds the threshold (maxfiles) is 0
  3773. if (fileCount >= threshold) {
  3774. // Calculate how many files need to be deleted to make space for the new file
  3775. var filesToDelete = fileCount - threshold + 1; // +1 to make space for the new file
  3776. if (func) { func('Deleting ' + filesToDelete + ' older ' + (filesToDelete == 1 ? 'file' : 'files') + ' from S3 ...'); }
  3777. // Create an array of promises for deleting files
  3778. var deletePromises = objects.slice(0, filesToDelete).map(function(fileToDelete) {
  3779. return new Promise(function(resolve, reject) {
  3780. minioClient.removeObject(bucketName, fileToDelete.name, function(err) {
  3781. if (err) {
  3782. reject(err);
  3783. } else {
  3784. if (func) { func('Deleted file: ' + fileToDelete.name + ' from S3'); }
  3785. resolve();
  3786. }
  3787. });
  3788. });
  3789. });
  3790. // Wait for all deletions to complete
  3791. return Promise.all(deletePromises);
  3792. } else {
  3793. return Promise.resolve(); // No deletion needed
  3794. }
  3795. }).then(function() {
  3796. // Determine the upload path by combining the pathPrefix with the filename
  3797. var fileName = require('path').basename(fileToUpload);
  3798. var uploadPath = require('path').join(pathPrefix, fileName);
  3799. // Upload a new file
  3800. var uploadPromise = new Promise(function(resolve, reject) {
  3801. if (func) { func('Uploading file ' + uploadPath + ' to S3'); }
  3802. minioClient.fPutObject(bucketName, uploadPath, fileToUpload, function(err, etag) {
  3803. if (err) {
  3804. reject(err);
  3805. } else {
  3806. if (func) { func('Uploaded file: ' + uploadPath + ' to S3'); }
  3807. resolve(etag);
  3808. }
  3809. });
  3810. });
  3811. return uploadPromise;
  3812. }).catch(function(error) {
  3813. if (func) { func('Error managing files in S3: ' + error); }
  3814. });
  3815. }
  3816. }
  3817. // Transfer NeDB data into the current database
  3818. obj.nedbtodb = function (func) {
  3819. var nedbDatastore = null;
  3820. try { nedbDatastore = require('@seald-io/nedb'); } catch (ex) { } // This is the NeDB with Node 23 support.
  3821. if (nedbDatastore == null) {
  3822. try { nedbDatastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
  3823. if (nedbDatastore == null) { nedbDatastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
  3824. }
  3825. var datastoreOptions = { filename: parent.getConfigFilePath('meshcentral.db'), autoload: true };
  3826. // If a DB encryption key is provided, perform database encryption
  3827. if ((typeof parent.args.dbencryptkey == 'string') && (parent.args.dbencryptkey.length != 0)) {
  3828. // Hash the database password into a AES256 key and setup encryption and decryption.
  3829. var nedbKey = parent.crypto.createHash('sha384').update(parent.args.dbencryptkey).digest('raw').slice(0, 32);
  3830. datastoreOptions.afterSerialization = function (plaintext) {
  3831. const iv = parent.crypto.randomBytes(16);
  3832. const aes = parent.crypto.createCipheriv('aes-256-cbc', nedbKey, iv);
  3833. var ciphertext = aes.update(plaintext);
  3834. ciphertext = Buffer.concat([iv, ciphertext, aes.final()]);
  3835. return ciphertext.toString('base64');
  3836. }
  3837. datastoreOptions.beforeDeserialization = function (ciphertext) {
  3838. const ciphertextBytes = Buffer.from(ciphertext, 'base64');
  3839. const iv = ciphertextBytes.slice(0, 16);
  3840. const data = ciphertextBytes.slice(16);
  3841. const aes = parent.crypto.createDecipheriv('aes-256-cbc', nedbKey, iv);
  3842. var plaintextBytes = Buffer.from(aes.update(data));
  3843. plaintextBytes = Buffer.concat([plaintextBytes, aes.final()]);
  3844. return plaintextBytes.toString();
  3845. }
  3846. }
  3847. // Setup all NeDB collections
  3848. var nedbfile = new nedbDatastore(datastoreOptions);
  3849. var nedbeventsfile = new nedbDatastore({ filename: parent.getConfigFilePath('meshcentral-events.db'), autoload: true, corruptAlertThreshold: 1 });
  3850. var nedbpowerfile = new nedbDatastore({ filename: parent.getConfigFilePath('meshcentral-power.db'), autoload: true, corruptAlertThreshold: 1 });
  3851. var nedbserverstatsfile = new nedbDatastore({ filename: parent.getConfigFilePath('meshcentral-stats.db'), autoload: true, corruptAlertThreshold: 1 });
  3852. // Transfered record counts
  3853. var normalRecordsTransferCount = 0;
  3854. var eventRecordsTransferCount = 0;
  3855. var powerRecordsTransferCount = 0;
  3856. var statsRecordsTransferCount = 0;
  3857. obj.pendingTransfer = 0;
  3858. // Transfer the data from main database
  3859. nedbfile.find({}, function (err, docs) {
  3860. if ((err == null) && (docs.length > 0)) {
  3861. performTypedRecordDecrypt(docs)
  3862. for (var i in docs) {
  3863. obj.pendingTransfer++;
  3864. normalRecordsTransferCount++;
  3865. obj.Set(common.unEscapeLinksFieldName(docs[i]), function () { obj.pendingTransfer--; });
  3866. }
  3867. }
  3868. // Transfer events
  3869. nedbeventsfile.find({}, function (err, docs) {
  3870. if ((err == null) && (docs.length > 0)) {
  3871. for (var i in docs) {
  3872. obj.pendingTransfer++;
  3873. eventRecordsTransferCount++;
  3874. obj.StoreEvent(docs[i], function () { obj.pendingTransfer--; });
  3875. }
  3876. }
  3877. // Transfer power events
  3878. nedbpowerfile.find({}, function (err, docs) {
  3879. if ((err == null) && (docs.length > 0)) {
  3880. for (var i in docs) {
  3881. obj.pendingTransfer++;
  3882. powerRecordsTransferCount++;
  3883. obj.storePowerEvent(docs[i], null, function () { obj.pendingTransfer--; });
  3884. }
  3885. }
  3886. // Transfer server stats
  3887. nedbserverstatsfile.find({}, function (err, docs) {
  3888. if ((err == null) && (docs.length > 0)) {
  3889. for (var i in docs) {
  3890. obj.pendingTransfer++;
  3891. statsRecordsTransferCount++;
  3892. obj.SetServerStats(docs[i], function () { obj.pendingTransfer--; });
  3893. }
  3894. }
  3895. // Only exit when all the records are stored.
  3896. setInterval(function () {
  3897. if (obj.pendingTransfer == 0) { func("Done. " + normalRecordsTransferCount + " record(s), " + eventRecordsTransferCount + " event(s), " + powerRecordsTransferCount + " power change(s), " + statsRecordsTransferCount + " stat(s)."); }
  3898. }, 200)
  3899. });
  3900. });
  3901. });
  3902. });
  3903. }
  3904. function padNumber(number, digits) { return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number; }
  3905. // Called when a node has changed
  3906. function dbNodeChange(nodeChange, added) {
  3907. if (parent.webserver == null) return;
  3908. common.unEscapeLinksFieldName(nodeChange.fullDocument);
  3909. const node = performTypedRecordDecrypt([nodeChange.fullDocument])[0];
  3910. parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: (added ? 'addnode' : 'changenode'), node: parent.webserver.CloneSafeNode(node), nodeid: node._id, domain: node.domain, nolog: 1 });
  3911. }
  3912. // Called when a device group has changed
  3913. function dbMeshChange(meshChange, added) {
  3914. if (parent.webserver == null) return;
  3915. common.unEscapeLinksFieldName(meshChange.fullDocument);
  3916. const mesh = performTypedRecordDecrypt([meshChange.fullDocument])[0];
  3917. // Update the mesh object in memory
  3918. const mmesh = parent.webserver.meshes[mesh._id];
  3919. if (mmesh != null) {
  3920. // Update an existing device group
  3921. for (var i in mesh) { mmesh[i] = mesh[i]; }
  3922. for (var i in mmesh) { if (mesh[i] == null) { delete mmesh[i]; } }
  3923. } else {
  3924. // Device group not present, create it.
  3925. parent.webserver.meshes[mesh._id] = mesh;
  3926. }
  3927. // Send the mesh update
  3928. var mesh2 = Object.assign({}, mesh); // Shallow clone
  3929. if (mesh2.deleted) { mesh2.action = 'deletemesh'; } else { mesh2.action = (added ? 'createmesh' : 'meshchange'); }
  3930. mesh2.meshid = mesh2._id;
  3931. mesh2.nolog = 1;
  3932. delete mesh2.type;
  3933. delete mesh2._id;
  3934. parent.DispatchEvent(['*', mesh2.meshid], obj, parent.webserver.CloneSafeMesh(mesh2));
  3935. }
  3936. // Called when a user account has changed
  3937. function dbUserChange(userChange, added) {
  3938. if (parent.webserver == null) return;
  3939. common.unEscapeLinksFieldName(userChange.fullDocument);
  3940. const user = performTypedRecordDecrypt([userChange.fullDocument])[0];
  3941. // Update the user object in memory
  3942. const muser = parent.webserver.users[user._id];
  3943. if (muser != null) {
  3944. // Update an existing user
  3945. for (var i in user) { muser[i] = user[i]; }
  3946. for (var i in muser) { if (user[i] == null) { delete muser[i]; } }
  3947. } else {
  3948. // User not present, create it.
  3949. parent.webserver.users[user._id] = user;
  3950. }
  3951. // Send the user update
  3952. var targets = ['*', 'server-users', user._id];
  3953. if (user.groups) { for (var i in user.groups) { targets.push('server-users:' + i); } }
  3954. parent.DispatchEvent(targets, obj, { etype: 'user', userid: user._id, username: user.name, account: parent.webserver.CloneSafeUser(user), action: (added ? 'accountcreate' : 'accountchange'), domain: user.domain, nolog: 1 });
  3955. }
  3956. // Called when a user group has changed
  3957. function dbUGrpChange(ugrpChange, added) {
  3958. if (parent.webserver == null) return;
  3959. common.unEscapeLinksFieldName(ugrpChange.fullDocument);
  3960. const usergroup = ugrpChange.fullDocument;
  3961. // Update the user group object in memory
  3962. const uusergroup = parent.webserver.userGroups[usergroup._id];
  3963. if (uusergroup != null) {
  3964. // Update an existing user group
  3965. for (var i in usergroup) { uusergroup[i] = usergroup[i]; }
  3966. for (var i in uusergroup) { if (usergroup[i] == null) { delete uusergroup[i]; } }
  3967. } else {
  3968. // Usergroup not present, create it.
  3969. parent.webserver.userGroups[usergroup._id] = usergroup;
  3970. }
  3971. // Send the user group update
  3972. var usergroup2 = Object.assign({}, usergroup); // Shallow clone
  3973. usergroup2.action = (added ? 'createusergroup' : 'usergroupchange');
  3974. usergroup2.ugrpid = usergroup2._id;
  3975. usergroup2.nolog = 1;
  3976. delete usergroup2.type;
  3977. delete usergroup2._id;
  3978. parent.DispatchEvent(['*', usergroup2.ugrpid], obj, usergroup2);
  3979. }
  3980. function dbMergeSqlArray(arr) {
  3981. var x = '';
  3982. for (var i in arr) { if (x != '') { x += ','; } x += '\'' + arr[i] + '\''; }
  3983. return x;
  3984. }
  3985. return obj;
  3986. };