admin.cpp 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <iostream>
  19. #include <climits>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <ctime>
  23. #include <getopt.h>
  24. #include <kopano/memory.hpp>
  25. #include <mapidefs.h>
  26. #include <mapispi.h>
  27. #include <mapix.h>
  28. #include <mapiutil.h>
  29. #include <kopano/automapi.hpp>
  30. #include <kopano/IECServiceAdmin.h>
  31. #include <kopano/IECSecurity.h>
  32. #include <kopano/IECUnknown.h>
  33. #include "SSLUtil.h"
  34. #include <kopano/ECConfig.h>
  35. #include <kopano/ECLogger.h>
  36. #include <kopano/ECTags.h>
  37. #include <kopano/EMSAbTag.h>
  38. #include <kopano/ECGuid.h>
  39. #include <kopano/ECABEntryID.h>
  40. #include <kopano/CommonUtil.h>
  41. #include <kopano/stringutil.h>
  42. #include <kopano/ecversion.h>
  43. #include <kopano/mapiext.h>
  44. #include <kopano/Util.h>
  45. #include <kopano/ECRestriction.h>
  46. #include <kopano/charset/convert.h>
  47. #include "ConsoleTable.h"
  48. #include <kopano/mapi_ptr.h>
  49. #include "ECFeatures.h"
  50. #include "ECACL.h"
  51. #include "charset/localeutil.h"
  52. #include <kopano/MAPIErrors.h>
  53. #include <edkmdb.h>
  54. #include <edkguid.h>
  55. #include <libintl.h>
  56. #include "Archiver.h"
  57. #include <kopano/MAPIErrors.h> // for declaration of GetMAPIErrorMessage()
  58. using namespace std;
  59. using namespace KCHL;
  60. static int forcedExitCode = 0;
  61. enum modes {
  62. MODE_INVALID = 0, MODE_LIST_USERS, MODE_CREATE_PUBLIC,
  63. MODE_CREATE_USER, MODE_CREATE_STORE, MODE_HOOK_STORE, MODE_UNHOOK_STORE,
  64. MODE_DELETE_STORE, MODE_REMOVE_STORE, MODE_UPDATE_USER, MODE_DELETE_USER,
  65. MODE_CREATE_GROUP, MODE_UPDATE_GROUP, MODE_DELETE_GROUP,
  66. MODE_LIST_GROUP, MODE_ADDUSER_GROUP, MODE_DELETEUSER_GROUP,
  67. MODE_CREATE_COMPANY, MODE_UPDATE_COMPANY, MODE_DELETE_COMPANY, MODE_LIST_COMPANY,
  68. MODE_ADD_VIEW, MODE_DEL_VIEW, MODE_LIST_VIEW,
  69. MODE_ADD_ADMIN, MODE_DEL_ADMIN, MODE_LIST_ADMIN,
  70. MODE_ADD_USERQUOTA_RECIPIENT, MODE_DEL_USERQUOTA_RECIPIENT, MODE_LIST_USERQUOTA_RECIPIENT,
  71. MODE_ADD_COMPANYQUOTA_RECIPIENT, MODE_DEL_COMPANYQUOTA_RECIPIENT, MODE_LIST_COMPANYQUOTA_RECIPIENT,
  72. MODE_SYNC_USERS, MODE_DETAILS, MODE_LIST_SENDAS, MODE_HELP,
  73. MODE_SYSTEM_ADMIN, MODE_PURGE_SOFTDELETE, MODE_PURGE_DEFERRED, MODE_CLEAR_CACHE, MODE_LIST_ORPHANS,
  74. MODE_FORCE_RESYNC, MODE_USER_COUNT, MODE_RESET_FOLDER_COUNT
  75. };
  76. enum {
  77. OPT_CREATE_STORE = UCHAR_MAX + 1, // high to avoid clashes with modes
  78. OPT_DELETE_STORE,
  79. OPT_HOOK_STORE,
  80. OPT_UNHOOK_STORE,
  81. OPT_REMOVE_STORE,
  82. OPT_COPYTO_PUBLIC,
  83. OPT_HELP,
  84. OPT_HOST,
  85. OPT_SYNC_USERS,
  86. OPT_DETAILS,
  87. OPT_DETAILS_TYPE,
  88. OPT_USER_QUOTA_HARD,
  89. OPT_USER_QUOTA_SOFT,
  90. OPT_USER_QUOTA_WARN,
  91. OPT_USER_QUOTA_OVERRIDE,
  92. OPT_USER_DEFAULT_QUOTA_HARD,
  93. OPT_USER_DEFAULT_QUOTA_SOFT,
  94. OPT_USER_DEFAULT_QUOTA_WARN,
  95. OPT_USER_DEFAULT_QUOTA_OVERRIDE,
  96. OPT_LANG,
  97. OPT_MR_ACCEPT,
  98. OPT_MR_DECLINE_CONFLICT,
  99. OPT_MR_DECLINE_RECURRING,
  100. OPT_ADD_SENDAS,
  101. OPT_DEL_SENDAS,
  102. OPT_LIST_SENDAS,
  103. OPT_UPDATE_GROUP,
  104. OPT_CREATE_COMPANY,
  105. OPT_UPDATE_COMPANY,
  106. OPT_DELETE_COMPANY,
  107. OPT_LIST_COMPANY,
  108. OPT_ADD_VIEW,
  109. OPT_DEL_VIEW,
  110. OPT_LIST_VIEW,
  111. OPT_ADD_ADMIN,
  112. OPT_DEL_ADMIN,
  113. OPT_LIST_ADMIN,
  114. OPT_SYSTEM_ADMIN,
  115. OPT_ADD_UQUOTA_RECIPIENT,
  116. OPT_DEL_UQUOTA_RECIPIENT,
  117. OPT_LIST_UQUOTA_RECIPIENT,
  118. OPT_ADD_CQUOTA_RECIPIENT,
  119. OPT_DEL_CQUOTA_RECIPIENT,
  120. OPT_LIST_CQUOTA_RECIPIENT,
  121. OPT_PURGE_SOFTDELETE,
  122. OPT_PURGE_DEFERRED,
  123. OPT_CLEAR_CACHE,
  124. OPT_LIST_ORPHANS,
  125. OPT_CONFIG,
  126. OPT_UTF8,
  127. OPT_FORCE_RESYNC,
  128. OPT_USER_COUNT,
  129. OPT_ENABLE_FEATURE,
  130. OPT_DISABLE_FEATURE,
  131. OPT_SELECT_NODE,
  132. OPT_RESET_FOLDER_COUNT,
  133. OPT_VERBOSITY,
  134. OPT_VERSION,
  135. };
  136. static const struct option long_options[] = {
  137. { "create-store", 1, NULL, OPT_CREATE_STORE },
  138. { "delete-store", 1, NULL, OPT_DELETE_STORE },
  139. { "hook-store", 1, NULL, OPT_HOOK_STORE },
  140. { "unhook-store", 1, NULL, OPT_UNHOOK_STORE },
  141. { "remove-store", 1, NULL, OPT_REMOVE_STORE },
  142. { "copyto-public", 0, NULL, OPT_COPYTO_PUBLIC },
  143. { "list-orphans", 0, NULL, OPT_LIST_ORPHANS },
  144. { "details", 1, NULL, OPT_DETAILS },
  145. { "type", 1, NULL, OPT_DETAILS_TYPE },
  146. { "help", 0, NULL, OPT_HELP },
  147. { "host", 1, NULL, OPT_HOST },
  148. { "sync", 0, NULL, OPT_SYNC_USERS },
  149. { "qh", 1, NULL, OPT_USER_QUOTA_HARD },
  150. { "qs", 1, NULL, OPT_USER_QUOTA_SOFT },
  151. { "qw", 1, NULL, OPT_USER_QUOTA_WARN },
  152. { "qo", 1, NULL, OPT_USER_QUOTA_OVERRIDE },
  153. { "udqh", 1, NULL, OPT_USER_DEFAULT_QUOTA_HARD },
  154. { "udqs", 1, NULL, OPT_USER_DEFAULT_QUOTA_SOFT },
  155. { "udqw", 1, NULL, OPT_USER_DEFAULT_QUOTA_WARN },
  156. { "udqo", 1, NULL, OPT_USER_DEFAULT_QUOTA_OVERRIDE },
  157. { "lang", 1, NULL, OPT_LANG },
  158. { "mr-accept", 1, NULL, OPT_MR_ACCEPT },
  159. { "mr-decline-conflict", 1, NULL, OPT_MR_DECLINE_CONFLICT },
  160. { "mr-decline-recurring", 1, NULL, OPT_MR_DECLINE_RECURRING },
  161. { "add-sendas", 1, NULL, OPT_ADD_SENDAS },
  162. { "del-sendas", 1, NULL, OPT_DEL_SENDAS },
  163. { "list-sendas", 1, NULL, OPT_LIST_SENDAS },
  164. { "update-group", 1, NULL, OPT_UPDATE_GROUP },
  165. { "create-company", 1, NULL, OPT_CREATE_COMPANY },
  166. { "update-company", 1, NULL, OPT_UPDATE_COMPANY },
  167. { "delete-company", 1, NULL, OPT_DELETE_COMPANY },
  168. { "list-companies", 0, NULL, OPT_LIST_COMPANY },
  169. { "add-to-viewlist", 1, NULL, OPT_ADD_VIEW },
  170. { "del-from-viewlist", 1, NULL, OPT_DEL_VIEW },
  171. { "list-view", 0, NULL, OPT_LIST_VIEW },
  172. { "add-to-adminlist", 1, NULL, OPT_ADD_ADMIN },
  173. { "del-from-adminlist", 1, NULL, OPT_DEL_ADMIN },
  174. { "list-admin", 0, NULL, OPT_LIST_ADMIN },
  175. { "set-system-admin", 1, NULL, OPT_SYSTEM_ADMIN },
  176. { "add-userquota-recipient", 1, NULL, OPT_ADD_UQUOTA_RECIPIENT },
  177. { "del-userquota-recipient", 1, NULL, OPT_DEL_UQUOTA_RECIPIENT },
  178. { "list-userquota-recipients", 0, NULL, OPT_LIST_UQUOTA_RECIPIENT },
  179. { "add-companyquota-recipient", 1, NULL, OPT_ADD_CQUOTA_RECIPIENT },
  180. { "del-companyquota-recipient", 1, NULL, OPT_DEL_CQUOTA_RECIPIENT },
  181. { "list-companyquota-recipients", 0, NULL, OPT_LIST_CQUOTA_RECIPIENT },
  182. { "purge-softdelete", 1, NULL, OPT_PURGE_SOFTDELETE },
  183. { "purge-deferred", 0, NULL, OPT_PURGE_DEFERRED },
  184. { "clear-cache", 2, NULL, OPT_CLEAR_CACHE },
  185. { "config", 1, NULL, OPT_CONFIG },
  186. { "utf8", 0, NULL, OPT_UTF8 },
  187. { "force-resync", 0, NULL, OPT_FORCE_RESYNC },
  188. { "user-count", 0, NULL, OPT_USER_COUNT },
  189. { "enable-feature", 1, NULL, OPT_ENABLE_FEATURE },
  190. { "disable-feature", 1, NULL, OPT_DISABLE_FEATURE },
  191. { "node", 1, NULL, OPT_SELECT_NODE },
  192. { "reset-folder-count", 1, NULL, OPT_RESET_FOLDER_COUNT },
  193. { "verbose", required_argument, NULL, OPT_VERBOSITY },
  194. { "version", no_argument, NULL, OPT_VERSION },
  195. { NULL, 0, NULL, 0 }
  196. };
  197. /**
  198. * Prints all options on screen. This should always be in sync with reality.
  199. *
  200. * @param[in] name The name of the program (arg[0])
  201. */
  202. static void print_help(const char *name)
  203. {
  204. ConsoleTable ct(0,0);
  205. cout << "Usage:" << endl;
  206. cout << name << " [action] [options]" << endl << endl;
  207. cout << "Actions: [-s] | [[-c|-u|-d|-b|-B|--details] username] | [[-g|-G] groupname] | [-l|-L]" << endl;
  208. ct.Resize(15, 2);
  209. ct.AddColumn(0, "-s"); ct.AddColumn(1, "Create public store.");
  210. ct.AddColumn(0, "--sync"); ct.AddColumn(1, "Synchronize users and groups with external source.");
  211. ct.AddColumn(0, "--clear-cache"); ct.AddColumn(1, "Clear all caches in the server.");
  212. ct.AddColumn(0, "--purge-softdelete N"); ct.AddColumn(1, "Purge items in marked as softdeleted that are older than N days.");
  213. ct.AddColumn(0, "--purge-deferred"); ct.AddColumn(1, "Purge all items in the deferred update table.");
  214. ct.AddColumn(0, "-l"); ct.AddColumn(1, "List users. Use -I to list users of a specific company, if applicable.");
  215. ct.AddColumn(0, "-L"); ct.AddColumn(1, "List groups. Use -I to list groups of a specific company, if applicable.");
  216. ct.AddColumn(0, "--list-sendas name"); ct.AddColumn(1, "List all users who are allowed to send-as 'name'. Use --type to indicate the object type.");
  217. ct.AddColumn(0, "--list-companies"); ct.AddColumn(1, "List all companies.");
  218. ct.AddColumn(0, "--list-view"); ct.AddColumn(1, "List all companies in the remote-view list.");
  219. ct.AddColumn(0, "--list-admin"); ct.AddColumn(1, "List all users in the remote-admin list.");
  220. ct.AddColumn(0, "--list-userquota-recipients"); ct.AddColumn(1, "List all additional recipients for a userquota warning email.");
  221. ct.AddColumn(0, "--list-companyquota-recipients"); ct.AddColumn(1, "List all additional recipients for a companyquota warning email.");
  222. ct.AddColumn(0, "--details"); ct.AddColumn(1, "Show object details, use --type to indicate the object type.");
  223. ct.AddColumn(0, "--type type"); ct.AddColumn(1, "Set object type for --details. Values can be \"user\", \"group\" or \"company\".");
  224. ct.AddColumn(0, "--user-count"); ct.AddColumn(1, "Output the system users counts.");
  225. ct.PrintTable();
  226. cout << endl;
  227. cout << "Additional Actions when using the DB user plugin:" << endl;
  228. ct.Resize(8,2);
  229. ct.AddColumn(0, "-c user"); ct.AddColumn(1, "Create user, -p, -f, -e options required, -a and -n are optional. Quota options are optional.");
  230. ct.AddColumn(0, "-u user"); ct.AddColumn(1, "Update user, -U, -p, -f, -e, -n and -a optional. Quota options are optional.");
  231. ct.AddColumn(0, "-d user"); ct.AddColumn(1, "Delete user.");
  232. ct.AddColumn(0, "-g group"); ct.AddColumn(1, "Create group, -e options optional.");
  233. ct.AddColumn(0, "--update-group group"); ct.AddColumn(1, "Update group, -e optional.");
  234. ct.AddColumn(0, "-G group"); ct.AddColumn(1, "Delete group.");
  235. ct.AddColumn(0, "-b user"); ct.AddColumn(1, "Add user to a group, -i required.");
  236. ct.AddColumn(0, "-B user"); ct.AddColumn(1, "Delete user from a group, -i required.");
  237. ct.PrintTable();
  238. cout << endl;
  239. cout << "Additional Actions when using the Unix user plugin:" << endl;
  240. ct.Resize(2,2);
  241. ct.AddColumn(0, "-u user"); ct.AddColumn(1, "Update user, -e and -a optional. Quota options are optional.");
  242. ct.AddColumn(0, "--update-group group"); ct.AddColumn(1, "Update group, -e optional.");
  243. ct.PrintTable();
  244. cout << endl;
  245. cout << "Additional Actions when using the DB or Unix user plugin:" << endl;
  246. ct.Resize(2,2);
  247. ct.AddColumn(0, "--enable-feature feature"); ct.AddColumn(1, "Update user to explicitly enable a feature.");
  248. ct.AddColumn(0, "--disable-feature feature"); ct.AddColumn(1, "Update user to explicitly disable a feature.");
  249. ct.PrintTable();
  250. cout << endl;
  251. cout << "Additional Actions when using the DB user plugin in hosted mode:" << endl;
  252. ct.Resize(12,2);
  253. ct.AddColumn(0, "--create-company name"); ct.AddColumn(1, "Create company space");
  254. ct.AddColumn(0, "--update-company name"); ct.AddColumn(1, "Update company space");
  255. ct.AddColumn(0, "--delete-company name"); ct.AddColumn(1, "Delete company space");
  256. ct.AddColumn(0, "--set-system-admin name"); ct.AddColumn(1, "Set system administrator for the company specified by -I (does not grant Admin privileges)");
  257. ct.AddColumn(0, "--add-to-viewlist name"); ct.AddColumn(1, "Add company 'name' to remote-view list of company specified by -I");
  258. ct.AddColumn(0, "--del-from-viewlist name"); ct.AddColumn(1, "Delete company 'name' from remote-view list of company specified by -I");
  259. ct.AddColumn(0, "--add-to-adminlist name"); ct.AddColumn(1, "Add user 'name' to remote-admin list of company specified by -I");
  260. ct.AddColumn(0, "--del-from-adminlist name"); ct.AddColumn(1, "Delete user 'name' from remote-admin list of company specified by -I");
  261. ct.AddColumn(0, "--add-userquota-recipient user"); ct.AddColumn(1, "Add 'user' as recipient to userquota warning emails.");
  262. ct.AddColumn(0, "--del-userquota-recipient user"); ct.AddColumn(1, "Delete 'user' as recipient to userquota warning emails.");
  263. ct.AddColumn(0, "--add-companyquota-recipient user"); ct.AddColumn(1, "Add 'user' as recipient to companyquota warning emails.");
  264. ct.AddColumn(0, "--del-companyquota-recipient user"); ct.AddColumn(1, "Delete 'user' as recipient to companyquota warning emails.");
  265. ct.PrintTable();
  266. cout << endl;
  267. cout << "Options: [-U new username] [-P|-p password] [-f fullname] [-e emailaddress]" << endl;
  268. cout << " [-a [y|n]] [-n [y|n]] [-h path] [-i group] [--qo [y|n]] [--qh value] [--qs value] [--qw value]" << endl;
  269. ct.Resize(22,2);
  270. ct.AddColumn(0, "-U new username"); ct.AddColumn(1, "Rename username to new username");
  271. ct.AddColumn(0, "-P"); ct.AddColumn(1, "Prompt for password, can be substituted by '-p pass'");
  272. ct.AddColumn(0, "-p pass"); ct.AddColumn(1, "Set password to pass, can be substituted by '-P'");
  273. ct.AddColumn(0, "-f full"); ct.AddColumn(1, "Set fullname to full");
  274. ct.AddColumn(0, "-e addr"); ct.AddColumn(1, "Set email address to addr");
  275. ct.AddColumn(0, "-a [y|n]"); ct.AddColumn(1, "Set administrator level for user. yes/no, y/n or 2/1/0.");
  276. ct.AddColumn(0, "-n [y|n]"); ct.AddColumn(1, "set user to non-active. yes/no, y/n or 1/0.");
  277. ct.AddColumn(0, "--qo [y|n]"); ct.AddColumn(1, "Override server quota limits. yes/no, y/n or 1/0.");
  278. ct.AddColumn(0, "--qh hardquota"); ct.AddColumn(1, "Set hardquota limit in Mb");
  279. ct.AddColumn(0, "--qs softquota"); ct.AddColumn(1, "Set softquota limit in Mb");
  280. ct.AddColumn(0, "--qw warnquota"); ct.AddColumn(1, "Set warnquota limit in Mb");
  281. ct.AddColumn(0, "--udqo [y|n]"); ct.AddColumn(1, "Override user default server quota limits for specific company. yes/no, y/n or 1/0.");
  282. ct.AddColumn(0, "--udqh hardquota"); ct.AddColumn(1, "Set user default hardquota limit for specific company in Mb");
  283. ct.AddColumn(0, "--udqs softquota"); ct.AddColumn(1, "Set user default softquota limit for specific company in Mb");
  284. ct.AddColumn(0, "--udqw warnquota"); ct.AddColumn(1, "Set user default warnquota limit for specific company in Mb");
  285. ct.AddColumn(0, "-i group"); ct.AddColumn(1, "Name of the group");
  286. ct.AddColumn(0, "-I company"); ct.AddColumn(1, "Name of the company");
  287. ct.AddColumn(0, "--mr-accept"); ct.AddColumn(1, "(resource) auto-accept meeting requests. yes/no");
  288. ct.AddColumn(0, "--mr-decline-conflict"); ct.AddColumn(1, "(resource) decline meeting requests for conflicting times. yes/no");
  289. ct.AddColumn(0, "--mr-decline-recurring"); ct.AddColumn(1, "(resource) decline meeting requests for all recurring items. yes/no");
  290. ct.AddColumn(0, "--add-sendas name"); ct.AddColumn(1, "Add user 'name' to send-as list of user specified by -u or --update-group");
  291. ct.AddColumn(0, "--del-sendas name"); ct.AddColumn(1, "Delete user 'name' from send-as list of user specified by -u or --update-group");
  292. ct.PrintTable();
  293. cout << endl;
  294. cout << "The following functions are to control stores of users:" << endl;
  295. ct.Resize(10,2);
  296. ct.AddColumn(0, "--list-orphans"); ct.AddColumn(1, "List all users without stores and stores without users.");
  297. ct.AddColumn(0, "--remove-store storeguid"); ct.AddColumn(1, "Delete orphaned store of user that is deleted from external source.");
  298. ct.AddColumn(0, "--hook-store storeguid"); ct.AddColumn(1, "Hook orphaned store to a user or copy to a public.");
  299. ct.AddColumn(0, " -u username"); ct.AddColumn(1, "Update user to received orphaned store given in --hook-store.");
  300. ct.AddColumn(0, " --type"); ct.AddColumn(1, "Type of the user to hook. Defaults to 'user', can be 'group' or 'company' for public store. Use 'archive' for archive stores.");
  301. ct.AddColumn(0, " --copyto-public"); ct.AddColumn(1, "Copy the orphan store to the public folder.");
  302. ct.AddColumn(0, "--unhook-store username"); ct.AddColumn(1, "Unhook store from user.");
  303. ct.AddColumn(0, " --type"); ct.AddColumn(1, "Type of the user to hook. Defaults to 'user', can be 'group' or 'company' for public store. Use 'archive' for archive stores.");
  304. ct.AddColumn(0, ""); ct.AddColumn(1, "Use 'Everyone' as username with type 'group' to unhook the public store, or use the company name and type 'company'.");
  305. ct.AddColumn(0, "--force-resync [username [username [...]]]"); ct.AddColumn(1, "Force a resynchronisation of offline profiles for specified users.");
  306. ct.AddColumn(0, "--reset-folder-count username"); ct.AddColumn(1, "Reset the counters on all folder in the store.");
  307. ct.PrintTable();
  308. cout << endl;
  309. cout << "The following functions are for use from the create/delete user/group scripts:" << endl;
  310. ct.Resize(2,2);
  311. ct.AddColumn(0, "--create-store user"); ct.AddColumn(1, "Create store for user that exists in external source.");
  312. ct.AddColumn(0, "--lang language"); ct.AddColumn(1, "Create folders in a new store in this language (e.g. en_EN.UTF-8).");
  313. ct.PrintTable();
  314. cout << endl;
  315. cout << "Note: the list-orphans and create/remove/hook/unhook-store functions only work on the server you're connected to. The commands will not be redirected in a multi-server environment." << endl;
  316. cout << endl;
  317. cout << "Global options: [-h|--host path]" << endl;
  318. ct.Resize(4,2);
  319. ct.AddColumn(0, "--config file"); ct.AddColumn(1, "Use a configuration file");
  320. ct.AddColumn(0, "-h path"); ct.AddColumn(1, "Connect through <path>, e.g. file:///var/run/socket");
  321. ct.AddColumn(0, "--node name"); ct.AddColumn(1, "Execute the command on cluster node <name>");
  322. ct.AddColumn(0, "--utf8"); ct.AddColumn(1, "Force the current locale to UTF-8");
  323. ct.AddColumn(0, "-v"); ct.AddColumn(1, "Increase verbosity. A maximum of 7 is possible where 1=fatal errors only, 6=debug and 7=everything.");
  324. ct.AddColumn(0, "--verbosity x"); ct.AddColumn(1, "Set verbosity to value 'x': 0...7 (0 = disable)");
  325. ct.AddColumn(0, "-V"); ct.AddColumn(1, "Print version info.");
  326. ct.AddColumn(0, "--version"); ct.AddColumn(1, "Print version info.");
  327. ct.AddColumn(0, "--help"); ct.AddColumn(1, "Show this help text.");
  328. ct.PrintTable();
  329. cout << endl;
  330. }
  331. /**
  332. * Reads a password twice from stdin, and doesn't print on stdout.
  333. *
  334. * @return char The typed password if ok, or NULL when failed.
  335. */
  336. static char *get_password(void)
  337. {
  338. static char password[80] = {0};
  339. char *s = NULL;
  340. s = get_password("Type password:");
  341. if(s == NULL)
  342. return NULL;
  343. strncpy(password, s, sizeof(password)-1);
  344. s = get_password("Retype password:");
  345. if (s == NULL)
  346. return NULL;
  347. if (strcmp(password, s) != 0)
  348. return NULL;
  349. return password;
  350. }
  351. /**
  352. * Parse given string to return 1 for true (yes) or 0 for false (no).
  353. *
  354. * @note Does not accept uppercase 'yes'
  355. *
  356. * @param[in] char* String containing a boolean
  357. * @return int 1 for true (yes) or 0 for false (no).
  358. */
  359. static int parse_yesno(const char *opt)
  360. {
  361. return opt[0] == 'y' || opt[0] == '1';
  362. }
  363. /**
  364. * Filetime to string
  365. *
  366. * @param ft time to convert to string
  367. *
  368. * @return time string "m / %d / %y %T"
  369. */
  370. static std::string FiletimeToString(FILETIME ft)
  371. {
  372. time_t timestamp;
  373. tm local;
  374. char d[64];
  375. memset(d, 0, sizeof(d));
  376. FileTimeToUnixTime(ft, &timestamp);
  377. localtime_r(&timestamp, &local);
  378. strftime(d, sizeof(d), "%x %X", &local);
  379. return d;
  380. }
  381. static std::string UnixtimeToString(time_t timestamp)
  382. {
  383. tm local;
  384. char d[64];
  385. memset(d, 0, sizeof(d));
  386. localtime_r(&timestamp, &local);
  387. strftime(d, sizeof(d), "%c", &local);
  388. return d;
  389. }
  390. /**
  391. * Print quota levels and/or store size.
  392. *
  393. * @param[in] lpQuota Optional ECQuota object with a users quota settings
  394. * @param[in] lpQuotaStatus Optional ECQuotaStatus object with the storesize of a user
  395. */
  396. static void print_quota(const ECQUOTA *lpQuota,
  397. const ECQUOTASTATUS *lpQuotaStatus, bool isPublic = false)
  398. {
  399. if (lpQuota) {
  400. // watch the not:
  401. if (!isPublic)
  402. cout << "Current user store quota settings:" << endl;
  403. else
  404. cout << "Current public store quota settings:" << endl;
  405. cout << " Quota overrides:\t" << (!lpQuota->bUseDefaultQuota?"yes":"no") << endl;
  406. cout << " Warning level:\t\t" << str_storage(lpQuota->llWarnSize) << endl;
  407. if(!isPublic) {
  408. cout << " Soft level:\t\t" << str_storage(lpQuota->llSoftSize) << endl;
  409. cout << " Hard level:\t\t" << str_storage(lpQuota->llHardSize) << endl;
  410. }
  411. }
  412. if (lpQuotaStatus == nullptr)
  413. return;
  414. if (!isPublic)
  415. cout << "Current store size:\t";
  416. else
  417. cout << "Public store size:\t";
  418. cout << str_storage(lpQuotaStatus->llStoreSize, false) << endl;
  419. }
  420. /**
  421. * Set new quota levels for a given user (EntryID). This only works
  422. * for DB and Unix plugin.
  423. *
  424. * @param[in] lpServiceAdmin IECServiceAdmin object on the Admin store
  425. * @param[in] cbEid Size of lpEid
  426. * @param[in] lpEid EntryID of a user
  427. * @param[in] quota New yes/no quota override flag setting, or -1 for default quota settings for user
  428. * @param[in] udefault IsUserDefaultQuota setting (Default quota for users within company)
  429. * @param[in] warn New warning level for user, or -1 for default system settings
  430. * @param[in] soft New soft level for user, or -1 for default system settings
  431. * @param[in] hard New hard level for user, or -1 for default system settings
  432. * @param[in] print Prints old and new quota settings for a user (optional, default false)
  433. */
  434. static HRESULT setQuota(IECServiceAdmin *lpServiceAdmin, ULONG cbEid,
  435. LPENTRYID lpEid, int quota, bool udefault, long long warn, long long soft,
  436. long long hard, bool print = false, bool company = false)
  437. {
  438. HRESULT hr = hrSuccess;
  439. memory_ptr<ECQUOTASTATUS> lpsQuotaStatus;
  440. memory_ptr<ECQUOTA> lpsQuota;
  441. ECQUOTA sQuota;
  442. if (lpEid == NULL)
  443. return MAPI_E_INVALID_PARAMETER;
  444. hr = lpServiceAdmin->GetQuota(cbEid, lpEid, false, &~lpsQuota);
  445. if (hr != hrSuccess) {
  446. cerr << "Unable to update quota, probably not found." << endl;
  447. return hr;
  448. }
  449. if (print) {
  450. cout << "Old quota settings:" << endl;
  451. print_quota(lpsQuota, NULL, company);
  452. cout << endl;
  453. }
  454. if (quota == -1)
  455. sQuota.bUseDefaultQuota = lpsQuota->bUseDefaultQuota;
  456. else
  457. sQuota.bUseDefaultQuota = !quota;
  458. sQuota.bIsUserDefaultQuota = udefault;
  459. sQuota.llHardSize = (hard >= 0) ? hard : lpsQuota->llHardSize;
  460. sQuota.llSoftSize = (soft >= 0) ? soft : lpsQuota->llSoftSize;
  461. sQuota.llWarnSize = (warn >= 0) ? warn : lpsQuota->llWarnSize;
  462. hr = lpServiceAdmin->SetQuota(cbEid, lpEid, &sQuota);
  463. if(hr != hrSuccess) {
  464. cerr << "Unable to update quota information." << endl;
  465. return hr;
  466. }
  467. if (!print)
  468. return hrSuccess;
  469. hr = lpServiceAdmin->GetQuotaStatus(cbEid, lpEid, &~lpsQuotaStatus);
  470. if(hr != hrSuccess) {
  471. cerr << "Unable to request updated quota information: " <<
  472. GetMAPIErrorMessage(hr) << " (" <<
  473. stringify(hr, true) << ")" << endl;
  474. return hr;
  475. }
  476. cout << "New quota settings:" << endl;
  477. print_quota(&sQuota, lpsQuotaStatus, company);
  478. cout << endl;
  479. return hrSuccess;
  480. }
  481. /**
  482. * Returns the string PR_* name for a set of addressbook properties,
  483. * not using the type of the tag to compare.
  484. *
  485. * @param[in] ulPropTag A MAPI proptag
  486. * @return string The PR_ string of a property, or the number.
  487. */
  488. static string getMapiPropertyString(ULONG ulPropTag)
  489. {
  490. #define PROP_TO_STRING(__proptag) \
  491. case PROP_ID(__proptag): return #__proptag
  492. switch (PROP_ID(ulPropTag))
  493. {
  494. PROP_TO_STRING(PR_MANAGER_NAME);
  495. PROP_TO_STRING(PR_GIVEN_NAME);
  496. PROP_TO_STRING(PR_INITIALS);
  497. PROP_TO_STRING(PR_SURNAME);
  498. PROP_TO_STRING(PR_DISPLAY_NAME);
  499. PROP_TO_STRING(PR_ACCOUNT);
  500. PROP_TO_STRING(PR_STREET_ADDRESS);
  501. PROP_TO_STRING(PR_LOCALITY);
  502. PROP_TO_STRING(PR_STATE_OR_PROVINCE);
  503. PROP_TO_STRING(PR_POSTAL_CODE);
  504. PROP_TO_STRING(PR_COUNTRY);
  505. PROP_TO_STRING(PR_TITLE);
  506. PROP_TO_STRING(PR_COMPANY_NAME);
  507. PROP_TO_STRING(PR_DEPARTMENT_NAME);
  508. PROP_TO_STRING(PR_OFFICE_LOCATION);
  509. PROP_TO_STRING(PR_ASSISTANT);
  510. PROP_TO_STRING(PR_BUSINESS_TELEPHONE_NUMBER);
  511. PROP_TO_STRING(PR_BUSINESS2_TELEPHONE_NUMBER);
  512. PROP_TO_STRING(PR_BUSINESS_FAX_NUMBER);
  513. PROP_TO_STRING(PR_HOME_TELEPHONE_NUMBER);
  514. PROP_TO_STRING(PR_HOME2_TELEPHONE_NUMBER);
  515. PROP_TO_STRING(PR_MOBILE_TELEPHONE_NUMBER);
  516. PROP_TO_STRING(PR_PAGER_TELEPHONE_NUMBER);
  517. PROP_TO_STRING(PR_PRIMARY_FAX_NUMBER);
  518. PROP_TO_STRING(PR_COMMENT);
  519. PROP_TO_STRING(PR_EMS_AB_MANAGER);
  520. PROP_TO_STRING(PR_EMS_AB_REPORTS);
  521. PROP_TO_STRING(PR_EMS_AB_IS_MEMBER_OF_DL);
  522. PROP_TO_STRING(PR_EMS_AB_PROXY_ADDRESSES);
  523. PROP_TO_STRING(PR_EMS_AB_OWNER);
  524. PROP_TO_STRING(PR_EMS_AB_MEMBER);
  525. PROP_TO_STRING(PR_EMS_AB_X509_CERT);
  526. PROP_TO_STRING(PR_EC_ENABLED_FEATURES);
  527. PROP_TO_STRING(PR_EC_DISABLED_FEATURES);
  528. PROP_TO_STRING(PR_EC_ARCHIVE_SERVERS);
  529. PROP_TO_STRING(PR_EC_ARCHIVE_COUPLINGS);
  530. default:
  531. return stringify(ulPropTag, true);
  532. }
  533. }
  534. /**
  535. * Prints a list of companies, enter or comma separated.
  536. *
  537. * @param[in] cCompanies Number of companies in lpECCompanies
  538. * @param[in] lpECCompanies Array of ECCompany structs
  539. * @param[in] bList true to list with comma's separation, otherwise enters are used.
  540. */
  541. static void print_companies(unsigned int cCompanies,
  542. const ECCOMPANY *lpECCompanies, bool bList)
  543. {
  544. for (unsigned int i = 0; i< cCompanies; ++i) {
  545. if (!bList)
  546. cout << ((i > 0) ? ", " : "");
  547. else
  548. cout << "\t";
  549. cout << (LPSTR)lpECCompanies[i].lpszCompanyname;
  550. if (bList)
  551. cout << endl;
  552. }
  553. }
  554. /**
  555. * Prints a list of groups, enter or comma separated.
  556. *
  557. * @param[in] cGroups Number of groups in lpECGroups
  558. * @param[in] lpECGroups Array of ECGroup structs
  559. * @param[in] bList true to list with comma's separation, otherwise enters are used.
  560. */
  561. static void print_groups(unsigned int cGroups, const ECGROUP *lpECGroups,
  562. bool bList)
  563. {
  564. for (unsigned int i = 0; i < cGroups; ++i) {
  565. if (!bList)
  566. cout << ((i > 0) ? ", " : "");
  567. else
  568. cout << "\t";
  569. cout << (LPSTR)lpECGroups[i].lpszGroupname;
  570. if (bList)
  571. cout << endl;
  572. }
  573. }
  574. /**
  575. * Prints a list of users, enter or comma separated.
  576. *
  577. * @param[in] cUsers Number of users in lpECUsers
  578. * @param[in] lpECUsers Array of ECUser structs
  579. * @param[in] bShowHomeServer true to print home server in multiserver environment if available
  580. */
  581. static void print_users(unsigned int cUsers, const ECUSER *lpECUsers,
  582. bool bShowHomeServer = false)
  583. {
  584. ConsoleTable ct(cUsers, bShowHomeServer?3:2);
  585. ct.SetHeader(0, "Username");
  586. ct.SetHeader(1, "Fullname");
  587. if (bShowHomeServer)
  588. ct.SetHeader(2, "Homeserver");
  589. for (unsigned int i = 0; i < cUsers; ++i) {
  590. ct.SetColumn(i, 0, (LPSTR)lpECUsers[i].lpszUsername);
  591. ct.SetColumn(i, 1, (LPSTR)lpECUsers[i].lpszFullName);
  592. if (!bShowHomeServer)
  593. continue;
  594. if (lpECUsers[i].lpszServername != NULL && *reinterpret_cast<LPSTR>(lpECUsers[i].lpszServername) != '\0')
  595. ct.SetColumn(i, 2, (LPSTR)lpECUsers[i].lpszServername);
  596. else
  597. // make sure we fill in all table parts. not using "<unknown>" tag,
  598. // since bShowHomeServer can be set to true even on non-multiserver environments
  599. ct.SetColumn(i, 2, string());
  600. }
  601. ct.PrintTable();
  602. }
  603. /**
  604. * Prints extra addressbook properties of an addressbook object, if present.
  605. *
  606. * @param[in] lpPropmap SPROPMAP struct, custom addressbook properties
  607. * @param[in] lpMVPropmap MVSPROPMAP struct, custom addressbook multi-valued properties
  608. * @param[in] group Indicate if the caller of this function is print_group_settings
  609. */
  610. static void print_extra_settings(const SPROPMAP *lpPropmap,
  611. const MVPROPMAP *lpMVPropmap, bool is_group = false)
  612. {
  613. unsigned int c = 0;
  614. if (!lpPropmap->cEntries && !lpMVPropmap->cEntries)
  615. return;
  616. ConsoleTable ct(lpPropmap->cEntries + lpMVPropmap->cEntries, 2);
  617. for (unsigned int i = 0; i < lpPropmap->cEntries; ++i) {
  618. ct.SetColumn(c, 0, getMapiPropertyString(lpPropmap->lpEntries[i].ulPropId));
  619. if (PROP_TYPE(lpPropmap->lpEntries[i].ulPropId) == PT_BINARY)
  620. ct.SetColumn(c, 1, stringify(strlen((LPSTR)lpPropmap->lpEntries[i].lpszValue)) + " bytes");
  621. else
  622. ct.SetColumn(c, 1, (LPSTR)lpPropmap->lpEntries[i].lpszValue);
  623. ++c;
  624. }
  625. for (unsigned int i = 0; i < lpMVPropmap->cEntries; ++i) {
  626. string strMVValues;
  627. if (is_group && (lpMVPropmap->lpEntries[i].ulPropId == PR_EC_ENABLED_FEATURES_A ||
  628. lpMVPropmap->lpEntries[i].ulPropId == PR_EC_DISABLED_FEATURES_A))
  629. continue;
  630. ct.SetColumn(c, 0, getMapiPropertyString(lpMVPropmap->lpEntries[i].ulPropId));
  631. if (PROP_TYPE(lpMVPropmap->lpEntries[i].ulPropId) == PT_MV_BINARY) {
  632. strMVValues = stringify(lpMVPropmap->lpEntries[i].cValues) + " values";
  633. } else {
  634. for (int j = 0; j < lpMVPropmap->lpEntries[i].cValues; ++j) {
  635. if (!strMVValues.empty())
  636. strMVValues += "; ";
  637. strMVValues += (LPSTR)lpMVPropmap->lpEntries[i].lpszValues[j];
  638. }
  639. }
  640. ct.SetColumn(c, 1, strMVValues);
  641. ++c;
  642. }
  643. if (c == 0)
  644. return;
  645. cout << "Mapped properties:" << endl;
  646. ct.PrintTable();
  647. }
  648. /**
  649. * Prints company details
  650. *
  651. * @param[in] lpECCompany ECCompany struct
  652. * @param[in] lpECAdministrator ECUser struct with the administrator of this company
  653. */
  654. static void print_company_settings(const ECCOMPANY *lpECCompany,
  655. const ECUSER *lpECAdministrator)
  656. {
  657. cout << "Companyname:\t\t" << (LPSTR)lpECCompany->lpszCompanyname << endl;
  658. cout << "Sysadmin:\t\t" << (LPSTR)lpECAdministrator->lpszUsername << endl;
  659. if (lpECCompany->lpszServername != NULL && *reinterpret_cast<LPSTR>(lpECCompany->lpszServername) != '\0')
  660. cout << "Home server:\t\t" << (LPSTR)lpECCompany->lpszServername << endl;
  661. cout << "Address book:\t\t" << (lpECCompany->ulIsABHidden ? "Hidden" : "Visible") << endl;
  662. print_extra_settings(&lpECCompany->sPropmap, &lpECCompany->sMVPropmap);
  663. }
  664. /**
  665. * Prints group details
  666. *
  667. * @param[in] lpECGroups ECGroup struct
  668. */
  669. static void print_group_settings(const ECGROUP *lpECGroup)
  670. {
  671. cout << "Groupname:\t\t" << (LPSTR)lpECGroup->lpszGroupname << endl;
  672. cout << "Fullname:\t\t" << (LPSTR)lpECGroup->lpszFullname << endl;
  673. cout << "Emailaddress:\t\t" << (LPSTR)lpECGroup->lpszFullEmail << endl;
  674. cout << "Address book:\t\t" << (lpECGroup->ulIsABHidden ? "Hidden" : "Visible") << endl;
  675. print_extra_settings(&lpECGroup->sPropmap, &lpECGroup->sMVPropmap, true);
  676. }
  677. /**
  678. * Converts an objectclass_t (common/ECDefs.h) to a string.
  679. *
  680. * @param[in] eClass Returns a user readable string for this objectclass
  681. * @return string
  682. */
  683. static string ClassToString(objectclass_t eClass)
  684. {
  685. switch (eClass) {
  686. case ACTIVE_USER:
  687. return string("User");
  688. case NONACTIVE_USER:
  689. return string("Shared store");
  690. case NONACTIVE_ROOM:
  691. return string("Room");
  692. case NONACTIVE_EQUIPMENT:
  693. return string("Equipment");
  694. case NONACTIVE_CONTACT:
  695. return string("Contact");
  696. case DISTLIST_GROUP:
  697. return string("Group");
  698. case DISTLIST_SECURITY:
  699. return string("Security group");
  700. case DISTLIST_DYNAMIC:
  701. return string("Dynamic group");
  702. case CONTAINER_COMPANY:
  703. return string("Company");
  704. case CONTAINER_ADDRESSLIST:
  705. return string("Addresslist");
  706. default:
  707. return string("Unknown");
  708. };
  709. }
  710. static int FileTimeToTimestamp(const FILETIME &ft, time_t& ts, char* buf, size_t size) {
  711. struct tm *tm;
  712. FileTimeToUnixTime(ft, &ts);
  713. if ((tm = localtime(&ts)) == NULL) {
  714. perror("localtime");
  715. return -1;
  716. }
  717. strftime(buf, size, "%F %T", tm);
  718. return 0;
  719. }
  720. static void adm_oof_status(const SPropValue *const prop)
  721. {
  722. char start_buf[64] = {'\0'}, end_buf[64] = {'\0'};
  723. time_t ts, now = time(NULL);
  724. /* o = out of office marked as switched on */
  725. bool o = prop[2].ulPropTag == PR_EC_OUTOFOFFICE && prop[2].Value.b;
  726. /* a = is it currently active with respect to time interval */
  727. bool a = o;
  728. if (prop[3].ulPropTag == PR_EC_OUTOFOFFICE_FROM) {
  729. if (FileTimeToTimestamp(prop[3].Value.ft, ts, start_buf, sizeof(start_buf)) == -1)
  730. return;
  731. a &= ts <= now;
  732. }
  733. if (prop[4].ulPropTag == PR_EC_OUTOFOFFICE_UNTIL) {
  734. if (FileTimeToTimestamp(prop[4].Value.ft, ts, end_buf, sizeof(start_buf)) == -1)
  735. return;
  736. a &= now <= ts;
  737. }
  738. if (!o) {
  739. printf("Out Of Office: disabled\n");
  740. return;
  741. }
  742. if (start_buf[0] == '\0' && end_buf[0] == '\0') {
  743. printf("Out Of Office: enabled\n");
  744. return;
  745. }
  746. printf("Out Of Office: ");
  747. if (start_buf[0] != '\0')
  748. printf("from %s ", start_buf);
  749. if (end_buf[0] != '\0')
  750. printf("until %s ", end_buf);
  751. printf("(currently %s)\n", a ? "active" : "inactive");
  752. }
  753. /**
  754. * Print user details
  755. *
  756. * @param[in] lpStore Store of the user
  757. * @param[in] lpECUser ECUser struct with user details
  758. * @param[in] bAutoAccept Meeting request settings of user
  759. * @param[in] bDeclineConflict Meeting request settings of user
  760. * @param[in] bDeclineRecurring Meeting request settings of user
  761. * @param[in] lstArchives List of attached archives
  762. */
  763. static void print_user_settings(IMsgStore *lpStore, const ECUSER *lpECUser,
  764. bool bAutoAccept, bool bDeclineConflict, bool bDeclineRecur,
  765. const ArchiveList &lstArchives, const ECUSERCLIENTUPDATESTATUS *lpECUCUS)
  766. {
  767. memory_ptr<SPropValue> lpProps;
  768. static constexpr const SizedSPropTagArray(5, sptaProps) =
  769. {5, {PR_LAST_LOGON_TIME, PR_LAST_LOGOFF_TIME,
  770. PR_EC_OUTOFOFFICE, PR_EC_OUTOFOFFICE_FROM,
  771. PR_EC_OUTOFOFFICE_UNTIL}};
  772. ULONG cValues = 0;
  773. if (lpStore)
  774. lpStore->GetProps(sptaProps, 0, &cValues, &~lpProps);
  775. cout << "Username:\t\t" << (LPSTR)lpECUser->lpszUsername << endl;
  776. cout << "Fullname:\t\t" << (LPSTR)lpECUser->lpszFullName << endl;
  777. cout << "Emailaddress:\t\t" << (LPSTR)lpECUser->lpszMailAddress << endl;
  778. cout << "Active:\t\t\t" << ((lpECUser->ulObjClass==ACTIVE_USER) ? "yes" : "no") << endl;
  779. if (lpECUser->ulObjClass != ACTIVE_USER)
  780. cout << "Non-active type:\t" << ClassToString((objectclass_t)lpECUser->ulObjClass) << endl;
  781. if (lpECUser->ulObjClass == NONACTIVE_ROOM || lpECUser->ulObjClass == NONACTIVE_EQUIPMENT)
  782. cout << "Resource capacity:\t" << lpECUser->ulCapacity << endl;
  783. cout << "Administrator:\t\t" << ((lpECUser->ulIsAdmin >= 1) ? "yes" : "no") << ((lpECUser->ulIsAdmin == 2) ? " (system)" : "") << endl;
  784. cout << "Address book:\t\t" << (lpECUser->ulIsABHidden ? "Hidden" : "Visible") << endl;
  785. cout << "Auto-accept meeting req:" << (bAutoAccept ? "yes" : "no") << endl;
  786. if (bAutoAccept) {
  787. cout << "Decline dbl meetingreq:\t" << (bDeclineConflict ? "yes" : "no") << endl;
  788. cout << "Decline recur meet.req:\t" << (bDeclineRecur ? "yes" : "no") << endl;
  789. }
  790. if (lpECUser->lpszServername != NULL && *reinterpret_cast<LPSTR>(lpECUser->lpszServername) != '\0')
  791. cout << "Home server:\t\t" << (LPSTR)lpECUser->lpszServername << endl;
  792. if (lpProps) {
  793. time_t logon = 0, logoff = 0;
  794. char d[64];
  795. adm_oof_status(lpProps);
  796. if(lpProps[0].ulPropTag == PR_LAST_LOGON_TIME)
  797. FileTimeToUnixTime(lpProps[0].Value.ft, &logon);
  798. if(lpProps[1].ulPropTag == PR_LAST_LOGOFF_TIME)
  799. FileTimeToUnixTime(lpProps[1].Value.ft, &logoff);
  800. if(logon) {
  801. strftime(d, sizeof(d), "%x %X", localtime(&logon));
  802. cout << "Last logon:\t\t" << d << std::endl;
  803. }
  804. if(logoff) {
  805. strftime(d, sizeof(d), "%x %X", localtime(&logoff));
  806. cout << "Last logoff:\t\t" << d << std::endl;
  807. }
  808. }
  809. print_extra_settings(&lpECUser->sPropmap, &lpECUser->sMVPropmap);
  810. if (!lstArchives.empty()) {
  811. cout << "Attached archives:\t" << lstArchives.size() << endl;
  812. for (const auto &arc : lstArchives) {
  813. cout << "\t" << arc.FolderName << " in " << arc.StoreName << " (" << arc.StoreGuid << ")";
  814. if (arc.Rights != ARCHIVE_RIGHTS_ABSENT) {
  815. if (arc.Rights == ROLE_OWNER)
  816. cout << " [Read Write]";
  817. else if (arc.Rights == ROLE_REVIEWER)
  818. cout << " [Read Only]";
  819. else
  820. cout << " [Modified: " << AclRightsToString(arc.Rights) << "]";
  821. }
  822. cout << endl;
  823. }
  824. }
  825. if (lpECUCUS == nullptr || lpECUCUS->ulTrackId <= 0)
  826. return;
  827. cout << "Client update Information:" << endl;
  828. cout << " Trackid:\t\t" << ((lpECUCUS->ulTrackId != 0 ) ? stringify(lpECUCUS->ulTrackId, true).c_str() : "-" ) << endl;
  829. cout << " Last update:\t\t" << ( (lpECUCUS->tUpdatetime>0) ? UnixtimeToString(lpECUCUS->tUpdatetime) : "-" ) << endl;
  830. cout << " From version:\t\t" << ( (lpECUCUS->lpszCurrentversion) ? (LPSTR)lpECUCUS->lpszCurrentversion : "-" ) << endl;
  831. cout << " To version:\t\t" << ( (lpECUCUS->lpszLatestversion) ? (LPSTR)lpECUCUS->lpszLatestversion : "-" ) << endl;
  832. cout << " Computername:\t\t" << ( (lpECUCUS->lpszComputername) ? (LPSTR)lpECUCUS->lpszComputername : "-" ) << endl;
  833. if (lpECUCUS->ulStatus == UPDATE_STATUS_SUCCESS)
  834. cout << " Update:\t\tSuccess" << endl;
  835. else if (lpECUCUS->ulStatus == UPDATE_STATUS_PENDING)
  836. cout << " Update:\t\tPending" << endl;
  837. else if (lpECUCUS->ulStatus == UPDATE_STATUS_UNKNOWN)
  838. cout << " Update: \t\tUnknown" << endl;
  839. else
  840. cout << " Update:\t\tFailed" << endl;
  841. }
  842. /**
  843. * Print archive store details on local server
  844. *
  845. * @param[in] lpSession MAPI session of the internal Kopano System adminstrator user
  846. * @param[in] lpECMsgStore The IECUnknown PR_EC_OBJECT pointer, used as IECServiceAdmin and IExchangeManageStore interface
  847. * @param[in] lpszName Name to resolve, using type in ulClass
  848. * @return MAPI error code
  849. */
  850. static HRESULT print_archive_details(LPMAPISESSION lpSession,
  851. IECUnknown *lpECMsgStore, const char *lpszName)
  852. {
  853. HRESULT hr;
  854. ECServiceAdminPtr ptrServiceAdmin;
  855. ULONG cbArchiveId = 0;
  856. EntryIdPtr ptrArchiveId;
  857. MsgStorePtr ptrArchive;
  858. SPropValuePtr ptrArchiveSize;
  859. hr = lpECMsgStore->QueryInterface(IID_IECServiceAdmin, &~ptrServiceAdmin);
  860. if (hr != hrSuccess) {
  861. cerr << "Unable to get admin interface." << endl;
  862. return hr;
  863. }
  864. hr = ptrServiceAdmin->GetArchiveStoreEntryID((LPCTSTR)lpszName, NULL, 0, &cbArchiveId, &~ptrArchiveId);
  865. if (hr != hrSuccess) {
  866. cerr << "No archive found for user '" << lpszName << "'." << endl;
  867. return hr;
  868. }
  869. hr = lpSession->OpenMsgStore(0, cbArchiveId, ptrArchiveId, &ptrArchive.iid(), 0, &~ptrArchive);
  870. if (hr != hrSuccess) {
  871. cerr << "Unable to open archive." << endl;
  872. return hr;
  873. }
  874. hr = HrGetOneProp(ptrArchive, PR_MESSAGE_SIZE_EXTENDED, &~ptrArchiveSize);
  875. if (hr != hrSuccess) {
  876. cerr << "Unable to get archive store size." << endl;
  877. return hr;
  878. }
  879. cout << "Current store size:\t";
  880. cout << stringify_double((double)ptrArchiveSize->Value.li.QuadPart /1024.0 /1024.0, 2, true) << " MiB" << endl;
  881. return hrSuccess;
  882. }
  883. /**
  884. * Parse a server store entryid to client store entryid.
  885. *
  886. * This is a hack to open an orphan store. It will convert a server store entryid,
  887. * which doesn't include a server url and is not wrapped by the support object, to
  888. * a client side store entryid.
  889. *
  890. * @param[in] lpServerUrl ServerURL for open the orphan store.
  891. * @param[in] cbEntryID Size of the unwrapped orphan store entryid.
  892. * @param[in] lpEntryID unwrapped orphan store entryid without server URL.
  893. * @param[out] lpcbEntryID Size of the wrapped orphan store entryid.
  894. * @param[out] lppEntryID Pointer to the wrapped entryid from the orphan store entryid.
  895. */
  896. static HRESULT CreateOrphanStoreEntryID(const char *lpServerUrl,
  897. ULONG cbEntryID, LPENTRYID lpEntryID, ULONG *lpcbEntryID,
  898. LPENTRYID *lppEntryID)
  899. {
  900. HRESULT hr = hrSuccess;
  901. ULONG cbNewEntryID = 0;
  902. memory_ptr<ENTRYID> lpNewEntryID;
  903. ULONG cbServerURL = 0;
  904. if (lpServerUrl == nullptr || lpEntryID == nullptr ||
  905. lpcbEntryID == nullptr || lppEntryID == nullptr)
  906. return MAPI_E_INVALID_PARAMETER;
  907. cbServerURL = strlen(lpServerUrl);
  908. cbNewEntryID = cbEntryID + cbServerURL;
  909. hr = MAPIAllocateBuffer(cbNewEntryID, &~lpNewEntryID);
  910. if (hr != hrSuccess)
  911. return hr;
  912. memcpy(lpNewEntryID, lpEntryID, cbEntryID);
  913. memcpy(reinterpret_cast<unsigned char *>(lpNewEntryID.get()) + cbEntryID - 4, lpServerUrl, cbServerURL + 4);
  914. return WrapStoreEntryID(0, (LPTSTR)"zarafa6client.dll", cbNewEntryID,
  915. lpNewEntryID, lpcbEntryID, lppEntryID);
  916. }
  917. /**
  918. * Get the information for an orphan store
  919. *
  920. * @param[in] lpServiceAdmin Pointer to the service admin.
  921. * @param[in] lpStoreGuid Indentifier to the orphan store.
  922. * @param[in] lpServerUrl ServerURL for open the orphan store.
  923. * @param[out] strUsername A guess of the user name belongs to the orphan store.
  924. * @param[out] strCompanyName Company name belongs to the orphan store.
  925. * @param[out] lpcbEntryID The orphan store entryid size.
  926. * @param[out] lppEntryID Pointer to the entry of the orphan store.
  927. */
  928. static HRESULT GetOrphanStoreInfo(IECServiceAdmin *lpServiceAdmin,
  929. GUID *lpStoreGuid, const char *lpServerUrl, wstring &strUsername,
  930. wstring &strCompanyName, ULONG *lpcbEntryID, LPENTRYID *lppEntryID)
  931. {
  932. HRESULT hr;
  933. MAPITablePtr ptrTable;
  934. SRowSetPtr ptrRowSet;
  935. SPropValue sStoreGuid;
  936. static constexpr const SizedSSortOrderSet(1, tableSort) =
  937. { 1, 0, 0,
  938. {
  939. { PR_EC_STOREGUID, TABLE_SORT_ASCEND },
  940. }
  941. };
  942. hr = lpServiceAdmin->OpenUserStoresTable(MAPI_UNICODE, &~ptrTable);
  943. if (hr != hrSuccess)
  944. return hr;
  945. hr = ptrTable->SortTable(tableSort, 0);
  946. if (hr != hrSuccess)
  947. return hr;
  948. sStoreGuid.ulPropTag = PR_EC_STOREGUID;
  949. sStoreGuid.Value.bin.cb = sizeof(GUID);
  950. sStoreGuid.Value.bin.lpb = (BYTE*)lpStoreGuid;
  951. hr = ECPropertyRestriction(RELOP_EQ, PR_EC_STOREGUID, &sStoreGuid, ECRestriction::Cheap)
  952. .FindRowIn(ptrTable, BOOKMARK_BEGINNING, 0);
  953. if (hr != hrSuccess)
  954. return hr;
  955. hr = ptrTable->QueryRows(1, 0, &ptrRowSet);
  956. if (hr != hrSuccess)
  957. return hr;
  958. if (ptrRowSet.empty())
  959. return MAPI_E_NOT_FOUND;
  960. auto lpsName = PCpropFindProp(ptrRowSet[0].lpProps, ptrRowSet[0].cValues, PR_DISPLAY_NAME_W);
  961. if (lpsName != nullptr)
  962. strUsername = lpsName->Value.lpszW;
  963. lpsName = PCpropFindProp(ptrRowSet[0].lpProps, ptrRowSet[0].cValues, PR_EC_COMPANY_NAME_W);
  964. if (lpsName != nullptr)
  965. strCompanyName = lpsName->Value.lpszW;
  966. auto lpsPropEntryId = PCpropFindProp(ptrRowSet[0].lpProps, ptrRowSet[0].cValues, PR_STORE_ENTRYID);
  967. if (lpsPropEntryId == NULL)
  968. return MAPI_E_NOT_FOUND;
  969. return CreateOrphanStoreEntryID(lpServerUrl,lpsPropEntryId->Value.bin.cb,
  970. reinterpret_cast<LPENTRYID>(lpsPropEntryId->Value.bin.lpb),
  971. lpcbEntryID, lppEntryID);
  972. }
  973. /**
  974. * Open/create deleted stores folder in the public store.
  975. *
  976. * Open the deleted admin folder in a public store, if the folder not exist it will create the folder.
  977. * First it creates a folder 'Admin' in the top-level tree (IPM_SUBTREE). The permissions on the folder
  978. * are set to 'everyone' can not read the folder except an admin. A second folder 'Deleted stores' will
  979. * create without permissions because the inheritance of the permissions.
  980. *
  981. * @param[in] lpPublicStore Public store to open or create the 'deleted stores' folder
  982. * @param[out] Pointer to a pointer of folder 'Deleted stores'.
  983. */
  984. static HRESULT OpenDeletedStoresFolder(LPMDB lpPublicStore,
  985. LPMAPIFOLDER *lppFolderStores)
  986. {
  987. HRESULT hr = hrSuccess;
  988. object_ptr<IMAPIFolder> lpFolderSubTree, lpFolderAdmin, lpFolderDeletedStores;
  989. memory_ptr<SPropValue> lpsPropSubTree, lpPropValue, lpsPropMDB;
  990. ULONG ulObjType;
  991. IECUnknown *lpECFolder = NULL; // non reference
  992. ECPERMISSION sPermission = {0};
  993. object_ptr<IECSecurity> lpSecurity;
  994. ULONG ulPropTagSubtree = 0;
  995. if (lpPublicStore == nullptr || lppFolderStores == nullptr)
  996. return MAPI_E_INVALID_PARAMETER;
  997. hr = HrGetOneProp(lpPublicStore, PR_MDB_PROVIDER, &~lpsPropMDB);
  998. if (hr != hrSuccess)
  999. return hr;
  1000. // Workaround for companies, because a company is a delegate store!
  1001. if (lpsPropMDB->Value.bin.cb == sizeof(MAPIUID) && memcmp(lpsPropMDB->Value.bin.lpb, &KOPANO_STORE_PUBLIC_GUID, sizeof(MAPIUID)) == 0)
  1002. ulPropTagSubtree = PR_IPM_PUBLIC_FOLDERS_ENTRYID;
  1003. else
  1004. ulPropTagSubtree = PR_IPM_SUBTREE_ENTRYID;
  1005. // Open IPM_subtree
  1006. hr = HrGetOneProp(lpPublicStore, ulPropTagSubtree, &~lpsPropSubTree);
  1007. if (hr != hrSuccess)
  1008. return hr;
  1009. hr = lpPublicStore->OpenEntry(lpsPropSubTree->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpsPropSubTree->Value.bin.lpb), nullptr, MAPI_MODIFY, &ulObjType, &~lpFolderSubTree);
  1010. if (hr != hrSuccess)
  1011. return hr;
  1012. // Create/open folder Admin
  1013. hr = lpFolderSubTree->CreateFolder(FOLDER_GENERIC, (LPTSTR)"Admin", nullptr, nullptr, 0, &~lpFolderAdmin);
  1014. if (hr == hrSuccess) {
  1015. // Set permissions
  1016. hr = HrGetOneProp(lpFolderAdmin, PR_EC_OBJECT, &~lpPropValue);
  1017. if(hr != hrSuccess)
  1018. return hr;
  1019. lpECFolder = reinterpret_cast<IECUnknown *>(lpPropValue->Value.lpszA);
  1020. hr = lpECFolder->QueryInterface(IID_IECSecurity, &~lpSecurity);
  1021. if (hr != hrSuccess)
  1022. return hr;
  1023. sPermission.ulRights = 0;// No rights, only for admin
  1024. sPermission.sUserId.lpb = g_lpEveryoneEid; // group everyone
  1025. sPermission.sUserId.cb = g_cbEveryoneEid;
  1026. sPermission.ulState = RIGHT_NEW|RIGHT_AUTOUPDATE_DENIED;
  1027. sPermission.ulType = ACCESS_TYPE_GRANT;
  1028. hr = lpSecurity->SetPermissionRules(1, &sPermission);
  1029. } else if (hr == MAPI_E_COLLISION) {
  1030. hr = lpFolderSubTree->CreateFolder(FOLDER_GENERIC, (LPTSTR)"Admin", nullptr, nullptr, OPEN_IF_EXISTS, &~lpFolderAdmin);
  1031. }
  1032. if (hr != hrSuccess)
  1033. return hr;
  1034. // Create/open folder Deleted Stores
  1035. hr = lpFolderAdmin->CreateFolder(FOLDER_GENERIC, (LPTSTR)"Deleted stores", nullptr, nullptr, OPEN_IF_EXISTS, &~lpFolderDeletedStores);
  1036. if (hr != hrSuccess)
  1037. return hr;
  1038. return lpFolderDeletedStores->QueryInterface(IID_IMAPIFolder, (void**)lppFolderStores);
  1039. }
  1040. /**
  1041. * Get the public store
  1042. *
  1043. * Get the public store from a company or just the default public store. If a company name is given
  1044. * it will try to open the companies store. if it fails it won't fall back to the default store.
  1045. *
  1046. * @param[in] lpSession Pointer to a mapi session.
  1047. * @param[in] lpMsgStore Pointer to a random store to open the ExchangeManageStore object.
  1048. * @param[in] strCompanyname name whose belongs the public store. If empty it opens the default public store.
  1049. * @param[out] lppPublicStore Pointer to the public store
  1050. */
  1051. static HRESULT GetPublicStore(LPMAPISESSION lpSession, LPMDB lpMsgStore,
  1052. const wstring &strCompanyname, LPMDB *lppPublicStore)
  1053. {
  1054. HRESULT hr = hrSuccess;
  1055. ULONG cbEntryID = 0;
  1056. memory_ptr<ENTRYID> lpEntryID;
  1057. object_ptr<IExchangeManageStore> lpIEMS;
  1058. if (strCompanyname.empty())
  1059. return HrOpenECPublicStore(lpSession, lppPublicStore);
  1060. hr = lpMsgStore->QueryInterface(IID_IExchangeManageStore, &~lpIEMS);
  1061. if (hr != hrSuccess)
  1062. return hr;
  1063. hr = lpIEMS->CreateStoreEntryID((LPTSTR)L"", (LPTSTR)strCompanyname.c_str(), MAPI_UNICODE, &cbEntryID, &~lpEntryID);
  1064. if (hr != hrSuccess)
  1065. return hr;
  1066. return lpSession->OpenMsgStore(0, cbEntryID, lpEntryID, &IID_IMsgStore, MDB_WRITE, lppPublicStore);
  1067. }
  1068. static const char *StoreTypeToString(ULONG ulStoreType)
  1069. {
  1070. switch (ulStoreType) {
  1071. case ECSTORE_TYPE_PRIVATE:
  1072. return "private";
  1073. case ECSTORE_TYPE_ARCHIVE:
  1074. return "archive";
  1075. case ECSTORE_TYPE_PUBLIC:
  1076. return "public";
  1077. default:
  1078. return "unknown";
  1079. };
  1080. }
  1081. /**
  1082. * List users without a store, and stores without a user.
  1083. *
  1084. * Gets a list of users and stores. Because of the sorting chosen,
  1085. * stores without a user will be printed first, until the first user
  1086. * without a store is found. Then those are printed, until the first
  1087. * user with a store is found.
  1088. *
  1089. * @param[in] lpServiceAdmin Kopano Administrator service object
  1090. * @result HRESULT MAPI Error code
  1091. */
  1092. static HRESULT list_orphans(IECServiceAdmin *lpServiceAdmin)
  1093. {
  1094. HRESULT hr = hrSuccess;
  1095. ULONG i = 0;
  1096. object_ptr<IMAPITable> lpTable;
  1097. std::string strUsername;
  1098. bool bHeader = true;
  1099. ConsoleTable ct(50, 5);
  1100. static constexpr const SizedSSortOrderSet(2, tableSort) =
  1101. { 2, 0, 0,
  1102. {
  1103. { PR_EC_USERNAME, TABLE_SORT_ASCEND },
  1104. { PR_EC_STOREGUID, TABLE_SORT_ASCEND },
  1105. }
  1106. };
  1107. hr = lpServiceAdmin->OpenUserStoresTable(0, &~lpTable);
  1108. if (hr != hrSuccess) {
  1109. cerr << "Unable to open user/stores table" << endl;
  1110. return hr;
  1111. }
  1112. hr = lpTable->SortTable(tableSort, 0);
  1113. if (hr != hrSuccess) {
  1114. cerr << "Unable to sort user/stores table" << endl;
  1115. return hr;
  1116. }
  1117. ct.SetHeader(0, "Store guid");
  1118. ct.SetHeader(1, "Guessed username");
  1119. ct.SetHeader(2, "Last login");
  1120. ct.SetHeader(3, "Store size");
  1121. ct.SetHeader(4, "Store type");
  1122. // Because of the sort, we start with these stores
  1123. cout << "Stores without users:" << endl;
  1124. while(TRUE) {
  1125. rowset_ptr lpRowSet;
  1126. hr = lpTable->QueryRows(50, 0, &~lpRowSet);
  1127. if(hr != hrSuccess) {
  1128. cerr << "Unable to load user/stores table" << endl;
  1129. return hr;
  1130. }
  1131. if(lpRowSet->cRows == 0)
  1132. break;
  1133. for (i = 0; i < lpRowSet->cRows; ++i) {
  1134. auto lpStoreGuid = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_EC_STOREGUID);
  1135. auto lpUserName = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_EC_USERNAME_A);
  1136. auto lpModTime = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_LAST_MODIFICATION_TIME);
  1137. auto lpStoreSize = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_MESSAGE_SIZE_EXTENDED);
  1138. auto lpStoreType = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_EC_STORETYPE);
  1139. if (lpStoreGuid && lpUserName)
  1140. continue;
  1141. if (!lpUserName) {
  1142. // find "guessed" named
  1143. lpUserName = PCpropFindProp(lpRowSet->aRow[i].lpProps, lpRowSet->aRow[i].cValues, PR_DISPLAY_NAME_A);
  1144. if (lpUserName)
  1145. strUsername = lpUserName->Value.lpszA;
  1146. else
  1147. strUsername = "<unknown>";
  1148. } else {
  1149. // we had all stores without users, now the users without stores
  1150. if (bHeader) {
  1151. ct.PrintTable();
  1152. ct.Resize(50, 1);
  1153. ct.SetHeader(0, "Username");
  1154. cout << endl << "Users without stores:" << endl;
  1155. bHeader = false;
  1156. }
  1157. strUsername = lpUserName->Value.lpszA;
  1158. }
  1159. if (lpStoreGuid == nullptr) {
  1160. ct.AddColumn(0, strUsername);
  1161. continue;
  1162. }
  1163. ct.AddColumn(0, bin2hex(lpStoreGuid->Value.bin.cb, lpStoreGuid->Value.bin.lpb));
  1164. ct.AddColumn(1, strUsername);
  1165. if (lpModTime)
  1166. ct.AddColumn(2, FiletimeToString(lpModTime->Value.ft));
  1167. else
  1168. ct.AddColumn(2, "<unknown>");
  1169. if (lpStoreSize)
  1170. ct.AddColumn(3, str_storage(lpStoreSize->Value.li.QuadPart, false));
  1171. else
  1172. ct.AddColumn(3, "<unknown>");
  1173. if (lpStoreType)
  1174. ct.AddColumn(4, StoreTypeToString(lpStoreType->Value.ul));
  1175. else
  1176. ct.AddColumn(4, "<unknown>");
  1177. }
  1178. }
  1179. ct.PrintTable();
  1180. return hrSuccess;
  1181. }
  1182. static LPMVPROPMAPENTRY FindMVPropmapEntry(ECUSER *lpUser, ULONG ulPropTag)
  1183. {
  1184. for (unsigned i = 0; i < lpUser->sMVPropmap.cEntries; ++i)
  1185. if (lpUser->sMVPropmap.lpEntries[i].ulPropId == ulPropTag)
  1186. return &lpUser->sMVPropmap.lpEntries[i];
  1187. return NULL;
  1188. }
  1189. /**
  1190. * Print the defaults of any user object (user/group/company)
  1191. *
  1192. * Depending on the input ulClass, find the object on the server, and
  1193. * print the details of the object if found.
  1194. *
  1195. * @param[in] lpSession MAPI session of the internal Kopano System adminstrator user
  1196. * @param[in] lpECMsgStore The IECUnknown PR_EC_OBJECT pointer, used as IECServiceAdmin and IExchangeManageStore interface
  1197. * @param[in] ulClass addressbook objectclass of input lpszName
  1198. * @param[in] lpszName Name to resolve, using type in ulClass
  1199. * @return MAPI error code
  1200. */
  1201. static HRESULT print_details(LPMAPISESSION lpSession, IECUnknown *lpECMsgStore,
  1202. objectclass_t ulClass, const char *lpszName)
  1203. {
  1204. HRESULT hr = hrSuccess;
  1205. memory_ptr<ECUSER> lpECUser;
  1206. memory_ptr<ECGROUP> lpECGroup, lpECGroups;
  1207. memory_ptr<ECCOMPANY> lpECCompany, lpECViews;
  1208. memory_ptr<ECQUOTASTATUS> lpsQuotaStatus;
  1209. memory_ptr<ECQUOTA> lpsQuota;
  1210. memory_ptr<ECUSER> lpECUsers, lpECAdmins;
  1211. ULONG cGroups = 0;
  1212. ULONG cUsers = 0;
  1213. ULONG cAdmins = 0;
  1214. ULONG cViews = 0;
  1215. ULONG cbEntryID = 0;
  1216. memory_ptr<ENTRYID> lpEntryID;
  1217. object_ptr<IMsgStore> lpStore;
  1218. object_ptr<IExchangeManageStore> lpIEMS;
  1219. object_ptr<IECServiceAdmin> lpServiceAdmin;
  1220. bool bAutoAccept = false;
  1221. bool bDeclineConflict = false;
  1222. bool bDeclineRecurring = false;
  1223. ULONG cbObjectId = 0;
  1224. LPENTRYID lpObjectId = NULL;
  1225. ArchiveManagePtr ptrArchiveManage;
  1226. ArchiveList lstArchives;
  1227. ECUSERCLIENTUPDATESTATUS *lpECUCUS = NULL;
  1228. convert_context converter;
  1229. hr = lpECMsgStore->QueryInterface(IID_IECServiceAdmin, &~lpServiceAdmin);
  1230. if (hr != hrSuccess) {
  1231. cerr << "Unable to get admin interface." << endl;
  1232. return hr;
  1233. }
  1234. switch (ulClass) {
  1235. case OBJECTCLASS_CONTAINER:
  1236. case CONTAINER_COMPANY:
  1237. hr = lpServiceAdmin->ResolveCompanyName((LPTSTR)lpszName, 0, &cbObjectId, &lpObjectId);
  1238. if (hr != hrSuccess) {
  1239. cerr << "Unable to resolve company, " << getMapiCodeString(hr, lpszName) << endl;
  1240. return hr;
  1241. }
  1242. hr = lpServiceAdmin->GetCompany(cbObjectId, lpObjectId, 0, &~lpECCompany);
  1243. if (hr != hrSuccess) {
  1244. cerr << "Unable to show company details, " << getMapiCodeString(hr) << endl;
  1245. return hr;
  1246. }
  1247. hr = lpECMsgStore->QueryInterface(IID_IExchangeManageStore, &~lpIEMS);
  1248. if (hr != hrSuccess) {
  1249. cerr << "Unable to get admin interface." << endl;
  1250. return hr;
  1251. }
  1252. hr = lpIEMS->CreateStoreEntryID((LPTSTR)"", lpECCompany->lpszCompanyname, 0, &cbEntryID, &~lpEntryID);
  1253. if (hr != hrSuccess) {
  1254. cerr << "Unable to get company store entry id. Company possibly has no store." << endl;
  1255. return hr;
  1256. }
  1257. hr = lpSession->OpenMsgStore(0, cbEntryID, lpEntryID, &IID_IMsgStore, MDB_WRITE, &~lpStore);
  1258. if (hr != hrSuccess) {
  1259. cerr << "Unable to open company store." << endl;
  1260. return hr;
  1261. }
  1262. hr = lpServiceAdmin->GetUser(lpECCompany->sAdministrator.cb, (LPENTRYID)lpECCompany->sAdministrator.lpb, 0, &~lpECUser);
  1263. if (hr != hrSuccess) {
  1264. cerr << "Unable to resolve company administrator, " << getMapiCodeString(hr) << endl;
  1265. return hr;
  1266. }
  1267. hr = lpServiceAdmin->GetRemoteAdminList(cbObjectId, lpObjectId, 0, &cAdmins, &~lpECAdmins);
  1268. if (hr != hrSuccess) {
  1269. cerr << "Unable to display remote-admin list, " << getMapiCodeString(hr) << endl;
  1270. hr = hrSuccess; /* Don't make error fatal */
  1271. }
  1272. hr = lpServiceAdmin->GetRemoteViewList(cbObjectId, lpObjectId, 0, &cViews, &~lpECViews);
  1273. if (hr != hrSuccess) {
  1274. cerr << "Unable to display remote-view list, " << getMapiCodeString(hr) << endl;
  1275. hr = hrSuccess; /* Don't make error fatal */
  1276. }
  1277. print_company_settings(lpECCompany, lpECUser);
  1278. break;
  1279. case OBJECTCLASS_DISTLIST:
  1280. case DISTLIST_GROUP:
  1281. case DISTLIST_SECURITY:
  1282. case DISTLIST_DYNAMIC:
  1283. hr = lpServiceAdmin->ResolveGroupName((LPTSTR)lpszName, 0, &cbObjectId, &lpObjectId);
  1284. if (hr != hrSuccess) {
  1285. cerr << "Unable to resolve group, " << getMapiCodeString(hr, lpszName) << endl;
  1286. return hr;
  1287. }
  1288. hr = lpServiceAdmin->GetGroup(cbObjectId, lpObjectId, 0, &~lpECGroup);
  1289. if (hr != hrSuccess) {
  1290. cerr << "Unable to show group details, " << getMapiCodeString(hr) << endl;
  1291. return hr;
  1292. }
  1293. hr = lpServiceAdmin->GetUserListOfGroup(cbObjectId, lpObjectId, 0, &cUsers, &~lpECUsers);
  1294. if (hr != hrSuccess) {
  1295. cerr << "Unable to request users for group, " << getMapiCodeString(hr) << endl;
  1296. hr = hrSuccess; /* Don't make error fatal */
  1297. }
  1298. print_group_settings(lpECGroup);
  1299. break;
  1300. case OBJECTCLASS_USER:
  1301. case ACTIVE_USER:
  1302. case NONACTIVE_USER:
  1303. case NONACTIVE_ROOM:
  1304. case NONACTIVE_EQUIPMENT:
  1305. case NONACTIVE_CONTACT:
  1306. default:
  1307. hr = lpServiceAdmin->ResolveUserName((LPTSTR)lpszName, 0, &cbObjectId, &lpObjectId);
  1308. if (hr != hrSuccess) {
  1309. cerr << "Unable to resolve user, " << getMapiCodeString(hr, lpszName) << endl;
  1310. return hr;
  1311. }
  1312. hr = lpServiceAdmin->GetUser(cbObjectId, lpObjectId, 0, &~lpECUser);
  1313. if (hr != hrSuccess) {
  1314. cerr << "Unable to show user details, " << getMapiCodeString(hr) << endl;
  1315. return hr;
  1316. }
  1317. hr = lpECMsgStore->QueryInterface(IID_IExchangeManageStore, &~lpIEMS);
  1318. if (hr != hrSuccess) {
  1319. cerr << "Unable to get admin interface." << endl;
  1320. return hr;
  1321. }
  1322. hr = lpIEMS->CreateStoreEntryID((LPTSTR)"", lpECUser->lpszUsername, 0, &cbEntryID, &~lpEntryID);
  1323. if (hr != hrSuccess) {
  1324. cerr << "WARNING: Unable to get user store entry id. User possibly has no store." << endl << endl;
  1325. lpStore.reset();
  1326. forcedExitCode = 1;
  1327. }
  1328. else {
  1329. hr = lpSession->OpenMsgStore(0, cbEntryID, lpEntryID, &IID_IMsgStore, MDB_WRITE, &~lpStore);
  1330. if (hr != hrSuccess) {
  1331. cerr << "Unable to open user store." << endl;
  1332. return hr;
  1333. }
  1334. GetAutoAcceptSettings(lpStore, &bAutoAccept, &bDeclineConflict, &bDeclineRecurring);
  1335. /* Ignore return value */
  1336. }
  1337. hr = lpServiceAdmin->GetGroupListOfUser(cbObjectId, lpObjectId, 0, &cGroups, &~lpECGroups);
  1338. if (hr != hrSuccess) {
  1339. cerr << "Unable to request groups for user, " << getMapiCodeString(hr) << endl;
  1340. hr = hrSuccess; /* Don't make error fatal */
  1341. }
  1342. hr = ArchiveManage::Create(lpSession, NULL, converter.convert_to<LPTSTR>(lpszName), &ptrArchiveManage);
  1343. if (hr != hrSuccess) {
  1344. if (hr != MAPI_E_NOT_FOUND)
  1345. cerr << "Error while obtaining archive details, " << getMapiCodeString(hr) << endl;
  1346. hr = hrSuccess; /* Don't make error fatal */
  1347. }
  1348. if (ptrArchiveManage.get() != NULL) {
  1349. hr = ptrArchiveManage->ListArchives(&lstArchives, "Root Folder");
  1350. if (hr != hrSuccess) {
  1351. cerr << "Error while obtaining archive list, " << getMapiCodeString(hr) << endl;
  1352. hr = hrSuccess; /* Don't make error fatal */
  1353. }
  1354. }
  1355. hr = lpServiceAdmin->GetUserClientUpdateStatus(cbObjectId, lpObjectId, 0, &lpECUCUS);
  1356. if (hr != hrSuccess) {
  1357. cerr << "Unable to get auto update status: " <<
  1358. GetMAPIErrorMessage(hr) << " (" <<
  1359. stringify(hr, true) << ")" << endl;
  1360. hr = hrSuccess;
  1361. }
  1362. print_user_settings(lpStore, lpECUser, bAutoAccept, bDeclineConflict, bDeclineRecurring, lstArchives, lpECUCUS);
  1363. break;
  1364. }
  1365. /* Group quota is not completely implemented at this time on the server... */
  1366. if (ulClass != DISTLIST_GROUP) {
  1367. hr = lpServiceAdmin->GetQuota(cbObjectId, lpObjectId, false, &~lpsQuota);
  1368. if (hr != hrSuccess) {
  1369. cerr << "Unable to show object quota, " << getMapiCodeString(hr) << endl;
  1370. hr = hrSuccess; /* Don't make error fatal */
  1371. } else {
  1372. hr = Util::HrGetQuotaStatus(lpStore, lpsQuota, &~lpsQuotaStatus);
  1373. if (hr != hrSuccess) {
  1374. cerr << "Unable to show object quota information, " << getMapiCodeString(hr) << endl;
  1375. hr = hrSuccess; /* Don't make error fatal */
  1376. } else
  1377. print_quota(lpsQuota, lpsQuotaStatus, (ulClass == CONTAINER_COMPANY));
  1378. }
  1379. }
  1380. if (ulClass == CONTAINER_COMPANY) {
  1381. hr = lpServiceAdmin->GetQuota(cbObjectId, lpObjectId, true, &~lpsQuota);
  1382. if (hr != hrSuccess) {
  1383. cerr << "Unable to get user default quota for company, " << getMapiCodeString(hr) << endl;
  1384. hr = hrSuccess; /* not fatal */
  1385. } else
  1386. print_quota(lpsQuota, NULL, false);
  1387. }
  1388. if (cUsers) {
  1389. cout << "Users (" << cUsers << "):" << endl;
  1390. print_users(cUsers, lpECUsers, true);
  1391. cout << endl;
  1392. }
  1393. if (cGroups) {
  1394. cout << "Groups (" << cGroups << "):" << endl;
  1395. print_groups(cGroups, lpECGroups, true);
  1396. cout << endl;
  1397. }
  1398. if (cAdmins) {
  1399. cout << "Remote admins (" << cAdmins << "):" << endl;
  1400. print_users(cAdmins, lpECAdmins);
  1401. cout << endl;
  1402. }
  1403. if (cViews) {
  1404. cout << "Remote viewers (" << cViews << "):" << endl;
  1405. print_companies(cViews, lpECViews, true);
  1406. cout << endl;
  1407. }
  1408. if (lpECUser == nullptr)
  1409. return hr;
  1410. LPMVPROPMAPENTRY lpArchiveServers = FindMVPropmapEntry(lpECUser, PR_EC_ARCHIVE_SERVERS_A);
  1411. if (lpArchiveServers == nullptr || lpArchiveServers->cValues == 0)
  1412. return hr;
  1413. MsgStorePtr ptrAdminStore;
  1414. hr = lpECMsgStore->QueryInterface(IID_IMsgStore, &~ptrAdminStore);
  1415. if (hr != hrSuccess)
  1416. return hr;
  1417. for (int i = 0; i < lpArchiveServers->cValues; ++i) {
  1418. MsgStorePtr ptrRemoteAdminStore;
  1419. SPropValuePtr ptrPropValue;
  1420. IECUnknown *lpECRemoteAdminStore = NULL;
  1421. HRESULT hrTmp;
  1422. cout << "Archive details on node '" << (LPSTR)lpArchiveServers->lpszValues[i] << "':" << endl;
  1423. hrTmp = HrGetRemoteAdminStore(lpSession, ptrAdminStore, lpArchiveServers->lpszValues[i], 0, &~ptrRemoteAdminStore);
  1424. if (FAILED(hrTmp)) {
  1425. cerr << "Unable to access node '" <<
  1426. (LPSTR)lpArchiveServers->lpszValues[i] <<
  1427. "': " << GetMAPIErrorMessage(hr) <<
  1428. "(" << stringify(hrTmp, true) <<
  1429. ")" << endl;
  1430. continue;
  1431. }
  1432. hr = HrGetOneProp(ptrRemoteAdminStore, PR_EC_OBJECT, &~ptrPropValue);
  1433. if (hr != hrSuccess || !ptrPropValue || !ptrPropValue->Value.lpszA) {
  1434. cerr << "Admin object not found." << endl;
  1435. return hr;
  1436. }
  1437. lpECRemoteAdminStore = reinterpret_cast<IECUnknown *>(ptrPropValue->Value.lpszA);
  1438. print_archive_details(lpSession, lpECRemoteAdminStore, lpszName);
  1439. cout << endl;
  1440. }
  1441. return hr;
  1442. }
  1443. /**
  1444. * Print a list of all users within a company.
  1445. *
  1446. * @param[in] lpServiceAdmin IECServiceAdmin on SYSTEM store
  1447. * @param[in] lpCompany The company to request users from. NULL EntryID in a non-hosted environment
  1448. * @return MAPI Error code
  1449. */
  1450. static HRESULT ListUsers(IECServiceAdmin *lpServiceAdmin, ECCOMPANY *lpCompany)
  1451. {
  1452. HRESULT hr = hrSuccess;
  1453. memory_ptr<ECUSER> lpECUsers;
  1454. ULONG cUsers = 0;
  1455. hr = lpServiceAdmin->GetUserList(lpCompany->sCompanyId.cb, (LPENTRYID)lpCompany->sCompanyId.lpb, 0, &cUsers, &~lpECUsers);
  1456. if (hr != hrSuccess) {
  1457. cerr << "Unable to list users, " << getMapiCodeString(hr) << endl;
  1458. return hr;
  1459. }
  1460. cout << "User list for " << (LPSTR)lpCompany->lpszCompanyname << "("<< cUsers <<"):" << endl;
  1461. print_users(cUsers, lpECUsers, true);
  1462. cout << endl;
  1463. return hrSuccess;
  1464. }
  1465. /**
  1466. * Print a list of all groups within a company.
  1467. *
  1468. * @param[in] lpServiceAdmin IECServiceAdmin on SYSTEM store
  1469. * @param[in] lpCompany The company to request users from. NULL EntryID in a non-hosted environment
  1470. * @return HRESULT MAPI Error code
  1471. */
  1472. static HRESULT ListGroups(IECServiceAdmin *lpServiceAdmin,
  1473. ECCOMPANY *lpCompany)
  1474. {
  1475. HRESULT hr = hrSuccess;
  1476. memory_ptr<ECGROUP> lpECGroups;
  1477. ULONG cGroups = 0;
  1478. hr = lpServiceAdmin->GetGroupList(lpCompany->sCompanyId.cb, (LPENTRYID)lpCompany->sCompanyId.lpb, 0, &cGroups, &~lpECGroups);
  1479. if (hr != hrSuccess) {
  1480. cerr << "Unable to list groups, " << getMapiCodeString(hr) << endl;
  1481. return hr;
  1482. }
  1483. cout << "Group list for " << (LPSTR)lpCompany->lpszCompanyname << "("<< cGroups <<"):" << endl;
  1484. cout << "\t" << "groupname" << "" << endl;
  1485. cout << "\t-------------------------------------" << endl;
  1486. print_groups(cGroups, lpECGroups, true);
  1487. cout << endl;
  1488. return hrSuccess;
  1489. }
  1490. /**
  1491. * Call the sync function to flush user cache on the server.
  1492. *
  1493. * @param[in] lpServiceAdmin IECServiceAdmin on SYSTEM store
  1494. * @return HRESULT MAPI Error code
  1495. */
  1496. static HRESULT SyncUsers(IECServiceAdmin *lpServiceAdmin)
  1497. {
  1498. HRESULT hr;
  1499. // we don't sync one company, since the complete cache is flushed in the server
  1500. hr = lpServiceAdmin->SyncUsers(0, NULL);
  1501. if (hr != hrSuccess)
  1502. cerr << "User/group synchronization failed, " << getMapiCodeString(hr) << endl;
  1503. return hr;
  1504. }
  1505. /**
  1506. * Loop a function over one or a list of companies.
  1507. *
  1508. * @param[in] lpServiceAdmin IECServiceAdmin on SYSTEM store
  1509. * @param[in] lpszCompanyname Only work on given company. NULL to work on all companies available.
  1510. * @param[in] lpWork Function to call given any company found in this function.
  1511. * @return HRESULT MAPI Error code
  1512. */
  1513. static HRESULT ForEachCompany(IECServiceAdmin *lpServiceAdmin,
  1514. const char *lpszCompanyName,
  1515. HRESULT (*lpWork)(IECServiceAdmin *, ECCOMPANY *))
  1516. {
  1517. HRESULT hr = hrSuccess;
  1518. ULONG cbCompanyId = 0;
  1519. memory_ptr<ENTRYID> lpCompanyId;
  1520. ULONG cCompanies = 0;
  1521. ECCOMPANY *lpECCompanies = NULL;
  1522. memory_ptr<ECCOMPANY> lpECCompaniesAlloc;
  1523. ECCOMPANY sRootCompany = {{g_cbSystemEid, g_lpSystemEid}, (LPTSTR)"Default", NULL, {0, NULL}};
  1524. if (lpszCompanyName) {
  1525. hr = lpServiceAdmin->ResolveCompanyName((LPTSTR)lpszCompanyName, 0, &cbCompanyId, &~lpCompanyId);
  1526. if (hr != hrSuccess) {
  1527. cerr << "Failed to resolve company name, " << getMapiCodeString(hr, lpszCompanyName) << endl;
  1528. return hr;
  1529. }
  1530. cCompanies = 1;
  1531. sRootCompany.sCompanyId.cb = cbCompanyId;
  1532. sRootCompany.sCompanyId.lpb = reinterpret_cast<unsigned char *>(lpCompanyId.get());
  1533. sRootCompany.lpszCompanyname = (LPTSTR)lpszCompanyName;
  1534. lpECCompanies = &sRootCompany;
  1535. } else {
  1536. hr = lpServiceAdmin->GetCompanyList(0, &cCompanies, &~lpECCompaniesAlloc);
  1537. if (hr != hrSuccess) {
  1538. cCompanies = 1;
  1539. lpECCompanies = &sRootCompany;
  1540. hr = hrSuccess;
  1541. } else {
  1542. lpECCompanies = lpECCompaniesAlloc;
  1543. }
  1544. }
  1545. if (cCompanies == 0) {
  1546. cerr << "No companies found." << endl;
  1547. return hr;
  1548. }
  1549. for (unsigned int i = 0; i < cCompanies; ++i) {
  1550. hr = lpWork(lpServiceAdmin, &lpECCompanies[i]);
  1551. if (hr != hrSuccess)
  1552. return hr;
  1553. }
  1554. return hr;
  1555. }
  1556. static HRESULT ForceResyncFor(LPMAPISESSION lpSession, LPMDB lpAdminStore,
  1557. const char *lpszAccount, const char *lpszHomeMDB)
  1558. {
  1559. HRESULT hr;
  1560. ExchangeManageStorePtr ptrEMS;
  1561. ULONG cbEntryID = 0;
  1562. EntryIdPtr ptrEntryID;
  1563. MsgStorePtr ptrUserStore;
  1564. MAPIFolderPtr ptrRoot;
  1565. SPropValuePtr ptrPropResyncID;
  1566. ULONG ulType = 0;
  1567. hr = lpAdminStore->QueryInterface(ptrEMS.iid(), &~ptrEMS);
  1568. if (hr != hrSuccess)
  1569. return hr;
  1570. hr = ptrEMS->CreateStoreEntryID((LPTSTR)lpszHomeMDB, (LPTSTR)lpszAccount, 0, &cbEntryID, &~ptrEntryID);
  1571. if (hr != hrSuccess)
  1572. return hr;
  1573. hr = lpSession->OpenMsgStore(0, cbEntryID, ptrEntryID, NULL, MDB_WRITE|MAPI_DEFERRED_ERRORS, &~ptrUserStore);
  1574. if (hr != hrSuccess)
  1575. return hr;
  1576. hr = ptrUserStore->OpenEntry(0, nullptr, &ptrRoot.iid(), MAPI_MODIFY, &ulType, &~ptrRoot);
  1577. if (hr != hrSuccess)
  1578. return hr;
  1579. hr = HrGetOneProp(ptrRoot, PR_EC_RESYNC_ID, &~ptrPropResyncID);
  1580. if (hr == MAPI_E_NOT_FOUND) {
  1581. SPropValue sPropResyncID;
  1582. sPropResyncID.ulPropTag = PR_EC_RESYNC_ID;
  1583. sPropResyncID.Value.ul = 1;
  1584. return HrSetOneProp(ptrRoot, &sPropResyncID);
  1585. } else if (hr != hrSuccess) {
  1586. return hr;
  1587. }
  1588. ++ptrPropResyncID->Value.ul;
  1589. return HrSetOneProp(ptrRoot, ptrPropResyncID);
  1590. }
  1591. static HRESULT ForceResyncAll(LPMAPISESSION lpSession, LPMDB lpAdminStore)
  1592. {
  1593. HRESULT hr = hrSuccess;
  1594. AddrBookPtr ptrAdrBook;
  1595. ABContainerPtr ptrABContainer;
  1596. MAPITablePtr ptrTable;
  1597. SRowSetPtr ptrRows;
  1598. ULONG ulType = 0;
  1599. bool bFail = false;
  1600. static constexpr const SizedSPropTagArray(1, sGALProps) = {1, {PR_ENTRYID}};
  1601. SPropValue sGALPropVal;
  1602. static constexpr const SizedSPropTagArray(2, sContentsProps) =
  1603. {2, {PR_ACCOUNT, PR_EMS_AB_HOME_MDB}};
  1604. SPropValue sObjTypePropVal;
  1605. SPropValue sDispTypePropVal;
  1606. hr = lpSession->OpenAddressBook(0, &ptrAdrBook.iid(), AB_NO_DIALOG, &~ptrAdrBook);
  1607. if (hr != hrSuccess)
  1608. goto exit;
  1609. hr = ptrAdrBook->OpenEntry(0, nullptr, &ptrABContainer.iid(), 0, &ulType, &~ptrABContainer);
  1610. if (hr != hrSuccess)
  1611. goto exit;
  1612. hr = ptrABContainer->GetHierarchyTable(0, &~ptrTable);
  1613. if (hr != hrSuccess)
  1614. goto exit;
  1615. sGALPropVal.ulPropTag = PR_AB_PROVIDER_ID;
  1616. sGALPropVal.Value.bin.cb = sizeof(GUID);
  1617. sGALPropVal.Value.bin.lpb = (LPBYTE)&MUIDECSAB;
  1618. hr = ptrTable->SetColumns(sGALProps, TBL_BATCH);
  1619. if (hr != hrSuccess)
  1620. goto exit;
  1621. hr = ECPropertyRestriction(RELOP_EQ, PR_AB_PROVIDER_ID, &sGALPropVal, ECRestriction::Cheap)
  1622. .RestrictTable(ptrTable, TBL_BATCH);
  1623. if (hr != hrSuccess)
  1624. goto exit;
  1625. hr = ptrTable->QueryRows(1, 0, &ptrRows);
  1626. if (hr != hrSuccess)
  1627. goto exit;
  1628. if (ptrRows.size() != 1 || ptrRows[0].lpProps[0].ulPropTag != PR_ENTRYID) {
  1629. hr = MAPI_E_NOT_FOUND;
  1630. goto exit;
  1631. }
  1632. hr = ptrAdrBook->OpenEntry(ptrRows[0].lpProps[0].Value.bin.cb, reinterpret_cast<ENTRYID *>(ptrRows[0].lpProps[0].Value.bin.lpb),
  1633. &ptrABContainer.iid(), MAPI_BEST_ACCESS, &ulType, &~ptrABContainer);
  1634. if (hr != hrSuccess)
  1635. goto exit;
  1636. hr = ptrABContainer->GetContentsTable(0, &~ptrTable);
  1637. if (hr != hrSuccess)
  1638. goto exit;
  1639. sObjTypePropVal.ulPropTag = PR_OBJECT_TYPE;
  1640. sObjTypePropVal.Value.l = MAPI_MAILUSER;
  1641. sDispTypePropVal.ulPropTag = PR_DISPLAY_TYPE;
  1642. sDispTypePropVal.Value.l = DT_MAILUSER;
  1643. hr = ptrTable->SetColumns(sContentsProps, TBL_BATCH);
  1644. if (hr != hrSuccess)
  1645. goto exit;
  1646. hr = ECAndRestriction(
  1647. ECPropertyRestriction(RELOP_EQ, PR_OBJECT_TYPE, &sObjTypePropVal, ECRestriction::Cheap) +
  1648. ECPropertyRestriction(RELOP_EQ, PR_DISPLAY_TYPE, &sDispTypePropVal, ECRestriction::Cheap)
  1649. ).RestrictTable(ptrTable, TBL_BATCH);
  1650. if (hr != hrSuccess)
  1651. goto exit;
  1652. while (true) {
  1653. hr = ptrTable->QueryRows(50, 0, &ptrRows);
  1654. if (hr != hrSuccess)
  1655. goto exit;
  1656. if (ptrRows.empty())
  1657. break;
  1658. for (SRowSetPtr::size_type i = 0; i < ptrRows.size(); ++i) {
  1659. if (PROP_TYPE(ptrRows[i].lpProps[0].ulPropTag) == PT_ERROR ||
  1660. PROP_TYPE(ptrRows[i].lpProps[1].ulPropTag) == PT_ERROR)
  1661. {
  1662. cerr << "Ignoring incomplete entry." << endl;
  1663. continue;
  1664. }
  1665. hr = ForceResyncFor(lpSession, lpAdminStore, ptrRows[i].lpProps[0].Value.lpszA, ptrRows[i].lpProps[1].Value.lpszA);
  1666. if (hr == hrSuccess)
  1667. continue;
  1668. cerr << "Failed to force resync for user " <<
  1669. ptrRows[i].lpProps[0].Value.lpszA <<
  1670. ": " << GetMAPIErrorMessage(hr) <<
  1671. " (" << stringify(hr, true) << ")" <<
  1672. endl;
  1673. bFail = true;
  1674. }
  1675. }
  1676. exit:
  1677. if (!FAILED(hr) && bFail)
  1678. hr = MAPI_W_ERRORS_RETURNED;
  1679. return hr;
  1680. }
  1681. static HRESULT ForceResync(LPMAPISESSION lpSession, LPMDB lpAdminStore,
  1682. const list<string> &lstUsernames)
  1683. {
  1684. HRESULT hr = hrSuccess;
  1685. bool bFail = false;
  1686. for (const auto &user : lstUsernames) {
  1687. hr = ForceResyncFor(lpSession, lpAdminStore, user.c_str(), NULL);
  1688. if (hr == hrSuccess)
  1689. continue;
  1690. cerr << "Failed to force resync for user " <<
  1691. user << ": " << GetMAPIErrorMessage(hr) <<
  1692. " (" << stringify(hr, true) << ")" << endl;
  1693. bFail = true;
  1694. }
  1695. if (!FAILED(hr) && bFail)
  1696. hr = MAPI_W_ERRORS_RETURNED;
  1697. return hr;
  1698. }
  1699. static HRESULT DisplayUserCount(LPMDB lpAdminStore)
  1700. {
  1701. HRESULT hr;
  1702. MAPITablePtr ptrSystemTable;
  1703. SPropValue sPropDisplayName;
  1704. SRowSetPtr ptrRows;
  1705. ULONG ulLicensedUsers = (ULONG)-1; //!< active users allowed by license
  1706. ULONG ulActiveUsers = (ULONG)-1; //!< used active users
  1707. ULONG ulNonActiveTotal = (ULONG)-1; //!< used non-active users
  1708. ULONG ulNonActiveUsers = (ULONG)-1; //!< used sharedstores, subset of used non-active users
  1709. ULONG ulRooms = (ULONG)-1; //!< used rooms, subset of used non-active users
  1710. ULONG ulEquipment = (ULONG)-1; //!< used equipment, subset of used non-active users
  1711. ULONG ulMaxTotal = 0; //!< complete total of user objects allowed by license, aka ulNonActiveHigh limit
  1712. ULONG ulNonActiveLow = 0; //!< atleast non-active users allowed
  1713. ULONG ulActiveAsNonActive = 0; //!< non-active users taken from active count
  1714. ConsoleTable ct(3, 4);
  1715. ULONG ulExtraRow = 0;
  1716. ULONG ulExtraRows = 0;
  1717. static constexpr const SizedSPropTagArray(2, sptaStatsProps) =
  1718. {2, {PR_DISPLAY_NAME_A, PR_EC_STATS_SYSTEM_VALUE}};
  1719. enum {IDX_DISPLAY_NAME_A, IDX_EC_STATS_SYSTEM_VALUE};
  1720. enum {COL_ALLOWED=1, COL_USED, COL_AVAILABLE};
  1721. hr = lpAdminStore->OpenProperty(PR_EC_STATSTABLE_SYSTEM, &ptrSystemTable.iid(), 0, 0, &~ptrSystemTable);
  1722. if (hr != hrSuccess)
  1723. return hr;
  1724. sPropDisplayName.ulPropTag = PR_DISPLAY_NAME_A;
  1725. sPropDisplayName.Value.lpszA = const_cast<char *>("usercnt_");
  1726. hr = ECContentRestriction(FL_PREFIX, PR_DISPLAY_NAME_A, &sPropDisplayName, ECRestriction::Cheap)
  1727. .RestrictTable(ptrSystemTable, TBL_BATCH);
  1728. if (hr != hrSuccess)
  1729. return hr;
  1730. hr = ptrSystemTable->SetColumns(sptaStatsProps, TBL_BATCH);
  1731. if (hr != hrSuccess)
  1732. return hr;
  1733. hr = ptrSystemTable->QueryRows(0xffff, 0, &ptrRows);
  1734. if (hr != hrSuccess)
  1735. return hr;
  1736. // We expect at least the first 3
  1737. if (ptrRows.size() < 3)
  1738. return MAPI_E_NOT_FOUND;
  1739. for (SRowSetPtr::size_type i = 0; i < ptrRows.size(); ++i) {
  1740. const char *lpszDisplayName = ptrRows[i].lpProps[IDX_DISPLAY_NAME_A].Value.lpszA;
  1741. if (strcmp(lpszDisplayName, "usercnt_licensed") == 0)
  1742. ulLicensedUsers = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1743. else if (strcmp(lpszDisplayName, "usercnt_active") == 0)
  1744. ulActiveUsers = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1745. else if (strcmp(lpszDisplayName, "usercnt_nonactive") == 0)
  1746. ulNonActiveTotal = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1747. else if (strcmp(lpszDisplayName, "usercnt_na_user") == 0)
  1748. ulNonActiveUsers = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1749. else if (strcmp(lpszDisplayName, "usercnt_room") == 0)
  1750. ulRooms = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1751. else if (strcmp(lpszDisplayName, "usercnt_equipment") == 0)
  1752. ulEquipment = atoui(ptrRows[i].lpProps[IDX_EC_STATS_SYSTEM_VALUE].Value.lpszA);
  1753. }
  1754. if (ulLicensedUsers == static_cast<ULONG>(-1) ||
  1755. ulActiveUsers == static_cast<ULONG>(-1) ||
  1756. ulNonActiveTotal == static_cast<ULONG>(-1))
  1757. return MAPI_E_NOT_FOUND;
  1758. if (ulNonActiveUsers != (ULONG)-1)
  1759. ++ulExtraRows;
  1760. if (ulRooms != (ULONG)-1)
  1761. ++ulExtraRows;
  1762. if (ulEquipment != (ULONG)-1)
  1763. ++ulExtraRows;
  1764. if (ulExtraRows > 0)
  1765. ct.Resize(3 + ulExtraRows, 4);
  1766. ulMaxTotal = std::max(ulLicensedUsers + 25, (ulLicensedUsers *5)/ 2);
  1767. ulNonActiveLow = ulMaxTotal - ulLicensedUsers;
  1768. ulActiveAsNonActive = (ulNonActiveTotal > ulNonActiveLow) ? ulNonActiveTotal - ulNonActiveLow : 0;
  1769. cout << "User counts:" << endl;
  1770. ct.SetHeader(COL_ALLOWED, "Allowed");
  1771. ct.SetHeader(COL_USED, "Used");
  1772. ct.SetHeader(COL_AVAILABLE, "Available");
  1773. ct.SetColumn(0, 0, "Active");
  1774. ct.SetColumn(0, COL_USED, stringify(ulActiveUsers));
  1775. if (ulLicensedUsers == 0) {
  1776. ct.SetColumn(0, COL_ALLOWED, "no limit");
  1777. ct.SetColumn(0, COL_AVAILABLE, "-");
  1778. } else {
  1779. ct.SetColumn(0, COL_ALLOWED, stringify(ulLicensedUsers));
  1780. ct.SetColumn(0, COL_AVAILABLE, stringify(ulLicensedUsers - ulActiveUsers - ulActiveAsNonActive, false, true));
  1781. }
  1782. ct.SetColumn(1, 0, "Non-active");
  1783. if (ulNonActiveTotal > ulNonActiveLow)
  1784. ct.SetColumn(1, COL_USED, stringify(ulNonActiveLow) + " + " + stringify(ulActiveAsNonActive));
  1785. else
  1786. ct.SetColumn(1, COL_USED, stringify(ulNonActiveTotal));
  1787. if (ulLicensedUsers == 0) {
  1788. ct.SetColumn(1, COL_ALLOWED, "no limit");
  1789. ct.SetColumn(1, COL_AVAILABLE, "-");
  1790. } else {
  1791. ct.SetColumn(1, COL_ALLOWED, stringify(ulMaxTotal - ulLicensedUsers, false, true));
  1792. if (ulNonActiveTotal > ulNonActiveLow)
  1793. ct.SetColumn(1, COL_AVAILABLE, "0 (+" + stringify(ulLicensedUsers - ulActiveUsers - ulActiveAsNonActive, false, true) + ")");
  1794. else
  1795. ct.SetColumn(1, COL_AVAILABLE, stringify(ulNonActiveLow - ulNonActiveTotal, false, true) +
  1796. " (+" + stringify(ulLicensedUsers - ulActiveUsers - ulActiveAsNonActive, false, true) + ")");
  1797. }
  1798. if (ulNonActiveUsers != (ULONG)-1) {
  1799. ct.SetColumn(2 + ulExtraRow, 0, " Users");
  1800. ct.SetColumn(2 + ulExtraRow, COL_USED, stringify(ulNonActiveUsers));
  1801. ++ulExtraRow;
  1802. }
  1803. if (ulRooms != (ULONG)-1) {
  1804. ct.SetColumn(2 + ulExtraRow, 0, " Rooms");
  1805. ct.SetColumn(2 + ulExtraRow, COL_USED, stringify(ulRooms));
  1806. ++ulExtraRow;
  1807. }
  1808. if (ulEquipment != (ULONG)-1) {
  1809. ct.SetColumn(2 + ulExtraRow, 0, " Equipment");
  1810. ct.SetColumn(2 + ulExtraRow, COL_USED, stringify(ulEquipment));
  1811. ++ulExtraRow;
  1812. }
  1813. ct.SetColumn(2 + ulExtraRows, 0, "Total");
  1814. ct.SetColumn(2 + ulExtraRows, COL_USED, stringify(ulActiveUsers + ulNonActiveTotal));
  1815. // available & allowed columns are too confusing in totals field.
  1816. ct.SetColumn(2 + ulExtraRows, COL_AVAILABLE, string()); // add empty last column to make sure we print this row
  1817. ct.PrintTable();
  1818. return hrSuccess;
  1819. }
  1820. static HRESULT ResetFolderCount(LPMAPISESSION lpSession, LPMDB lpAdminStore,
  1821. const char *lpszAccount)
  1822. {
  1823. HRESULT hr = hrSuccess;
  1824. ExchangeManageStorePtr ptrEMS;
  1825. ULONG cbEntryID;
  1826. EntryIdPtr ptrEntryID;
  1827. ULONG ulType = 0;
  1828. MsgStorePtr ptrUserStore;
  1829. MAPIFolderPtr ptrRoot;
  1830. ECServiceAdminPtr ptrServiceAdmin;
  1831. SPropValuePtr ptrPropEntryID;
  1832. ULONG ulUpdates = 0;
  1833. ULONG bFailures = false;
  1834. ULONG ulTotalUpdates = 0;
  1835. MAPITablePtr ptrTable;
  1836. SRowSetPtr ptrRows;
  1837. static constexpr const SizedSPropTagArray(2, sptaTableProps) =
  1838. {2, {PR_DISPLAY_NAME_A, PR_ENTRYID}};
  1839. enum {IDX_DISPLAY_NAME, IDX_ENTRYID};
  1840. hr = lpAdminStore->QueryInterface(ptrEMS.iid(), &~ptrEMS);
  1841. if (hr != hrSuccess)
  1842. goto exit;
  1843. hr = ptrEMS->CreateStoreEntryID(NULL, (LPTSTR)lpszAccount, 0, &cbEntryID, &~ptrEntryID);
  1844. if (hr != hrSuccess) {
  1845. cerr << "Unable to resolve store for '" << lpszAccount << "'." << endl;
  1846. goto exit;
  1847. }
  1848. hr = lpSession->OpenMsgStore(0, cbEntryID, ptrEntryID, nullptr, MDB_WRITE, &~ptrUserStore);
  1849. if (hr != hrSuccess) {
  1850. cerr << "Unable to open store for '" << lpszAccount << "'." << endl;
  1851. goto exit;
  1852. }
  1853. hr = ptrUserStore->QueryInterface(ptrServiceAdmin.iid(), &~ptrServiceAdmin);
  1854. if (hr != hrSuccess)
  1855. goto exit;
  1856. hr = ptrUserStore->OpenEntry(0, nullptr, &ptrRoot.iid(), 0, &ulType, &~ptrRoot);
  1857. if (hr != hrSuccess)
  1858. goto exit;
  1859. hr = HrGetOneProp(ptrRoot, PR_ENTRYID, &~ptrPropEntryID);
  1860. if (hr != hrSuccess)
  1861. goto exit;
  1862. hr = ptrServiceAdmin->ResetFolderCount(ptrPropEntryID->Value.bin.cb, (LPENTRYID)ptrPropEntryID->Value.bin.lpb, &ulUpdates);
  1863. if (hr != hrSuccess) {
  1864. cerr << "Failed to update counters in the root folder." << endl;
  1865. bFailures = true;
  1866. hr = hrSuccess;
  1867. } else if (ulUpdates) {
  1868. cerr << "Updated " << ulUpdates << " counters in the root folder." << endl;
  1869. ulTotalUpdates += ulUpdates;
  1870. }
  1871. hr = ptrRoot->GetHierarchyTable(CONVENIENT_DEPTH, &~ptrTable);
  1872. if (hr != hrSuccess)
  1873. goto exit;
  1874. hr = HrQueryAllRows(ptrTable, sptaTableProps, NULL, NULL, 0, &ptrRows);
  1875. if (hr != hrSuccess)
  1876. goto exit;
  1877. for (SRowSetPtr::size_type i = 0; i < ptrRows.size(); ++i) {
  1878. const SRow &row = ptrRows[i];
  1879. const char* lpszName = "<Unknown>";
  1880. if (PROP_TYPE(row.lpProps[IDX_DISPLAY_NAME].ulPropTag) != PT_ERROR)
  1881. lpszName = row.lpProps[IDX_DISPLAY_NAME].Value.lpszA;
  1882. hr = ptrServiceAdmin->ResetFolderCount(row.lpProps[IDX_ENTRYID].Value.bin.cb,
  1883. (LPENTRYID)row.lpProps[IDX_ENTRYID].Value.bin.lpb,
  1884. &ulUpdates);
  1885. if (hr != hrSuccess) {
  1886. cerr << "Failed to update counters in folder '" << lpszName << "'." << endl;
  1887. bFailures = true;
  1888. hr = hrSuccess;
  1889. } else if (ulUpdates) {
  1890. cerr << "Updated " << ulUpdates << " counters in folder '" << lpszName << "'." << endl;
  1891. ulTotalUpdates += ulUpdates;
  1892. }
  1893. }
  1894. if (ulTotalUpdates == 0)
  1895. cerr << "No counters needed to be updated." << endl;
  1896. exit:
  1897. if (hr == hrSuccess && bFailures)
  1898. hr = MAPI_W_ERRORS_RETURNED;
  1899. return hr;
  1900. }
  1901. class InputValidator {
  1902. public:
  1903. bool Failed() const { return m_bFailure; }
  1904. /**
  1905. * Checks for 'invalid' input from the command prompt. Any
  1906. * non-printable or contol ascii character is not allowed.
  1907. *
  1908. * @param[in] szInput command line input string
  1909. *
  1910. * @return validated input or NULL
  1911. */
  1912. char* operator()(char *szInput) {
  1913. m_bFailure = true;
  1914. wstring strInput;
  1915. if (!szInput)
  1916. return NULL;
  1917. if (TryConvert(szInput, strInput) != hrSuccess)
  1918. return NULL;
  1919. for (auto c : strInput)
  1920. if (!iswprint(c))
  1921. return NULL;
  1922. m_bFailure = false;
  1923. return szInput;
  1924. }
  1925. private:
  1926. bool m_bFailure = false;
  1927. };
  1928. // compare function for set<tstring, ltstr>, fixes default wchar_t compare, and makes it case-insensitive
  1929. // used for PR_EC_*ABLED_FEATURES_A properties from TCHAR* strings in ECUSER struct
  1930. struct lstr {
  1931. bool operator()(const string &t1, const string &t2) const
  1932. {
  1933. return strcasecmp((char*)t1.c_str(), (char*)t2.c_str()) < 0;
  1934. }
  1935. };
  1936. static HRESULT fillMVPropmap(ECUSER &sECUser, ULONG ulPropTag, int index,
  1937. set<string, lstr> &sFeatures, void *lpBase)
  1938. {
  1939. HRESULT hr;
  1940. sECUser.sMVPropmap.lpEntries[index].ulPropId = ulPropTag;
  1941. sECUser.sMVPropmap.lpEntries[index].cValues = sFeatures.size();
  1942. sECUser.sMVPropmap.lpEntries[index].lpszValues = NULL;
  1943. if (sFeatures.size() == 0)
  1944. return hrSuccess;
  1945. hr = MAPIAllocateMore(sizeof(LPTSTR) * sFeatures.size(), lpBase, (void**)&sECUser.sMVPropmap.lpEntries[index].lpszValues);
  1946. if (hr != hrSuccess) {
  1947. cerr << "Memory error" << endl;
  1948. return hr;
  1949. }
  1950. int n = 0;
  1951. auto i = sFeatures.cbegin();
  1952. // @note we store char* data in a LPTSTR (whcar_t by -DUNICODE) pointer.
  1953. for (n = 0; i != sFeatures.cend(); ++i, ++n)
  1954. sECUser.sMVPropmap.lpEntries[index].lpszValues[n] = (TCHAR*)i->c_str();
  1955. return hrSuccess;
  1956. }
  1957. static void missing_quota(int hard, int warn, int soft)
  1958. {
  1959. if (hard == -1)
  1960. cerr << " hard quota (--qh)";
  1961. if (warn == -1)
  1962. cerr << " warn quota (--qw)";
  1963. if (soft == -1)
  1964. cerr << " soft quota (--qs)";
  1965. }
  1966. int main(int argc, char* argv[])
  1967. {
  1968. HRESULT hr = hrSuccess;
  1969. AutoMAPI mapiinit;
  1970. object_ptr<IMAPISession> lpSession;
  1971. object_ptr<IECUnknown> lpECMsgStore;
  1972. object_ptr<IMsgStore> lpMsgStore;
  1973. object_ptr<IECServiceAdmin> lpServiceAdmin;
  1974. ULONG cbUserId = 0;
  1975. memory_ptr<ENTRYID> lpUserId, lpSenderId, lpGroupId, lpCompanyId;
  1976. memory_ptr<ENTRYID> lpSetCompanyId, lpEntryID, lpUnWrappedEntry;
  1977. memory_ptr<ENTRYID> lpStoreId, lpRootId;
  1978. ULONG cbGroupId = 0;
  1979. ULONG cbSenderId = 0;
  1980. ULONG cbCompanyId = 0;
  1981. ULONG cbSetCompanyId = 0;
  1982. ULONG ulDays = 0;
  1983. memory_ptr<SPropValue> lpPropValue;
  1984. ULONG cbGUID = 0;
  1985. memory_ptr<GUID> lpGUID;
  1986. ECUSER sECUser;
  1987. memory_ptr<ECUSER> lpECUser, lpSenders;
  1988. ULONG cbStoreId = 0;
  1989. ULONG cbRootId = 0;
  1990. ULONG cbUnWrappedEntry = 0;
  1991. ECGROUP sECGroup;
  1992. memory_ptr<ECGROUP> lpECGroups;
  1993. ULONG cCompanies = 0;
  1994. ULONG cUsers = 0;
  1995. ECCOMPANY sECCompany;
  1996. ECCOMPANY *lpECCompanies = NULL;
  1997. memory_ptr<ECQUOTASTATUS> lpsQuotaStatus;
  1998. memory_ptr<ECQUOTA> lpsQuota;
  1999. memory_ptr<ECSVRNAMELIST> lpsServer;
  2000. memory_ptr<ECSERVERLIST> lpServerDetails;
  2001. ULONG cSenders = 0;
  2002. objectclass_t ulClass = OBJECTCLASS_UNKNOWN;
  2003. const char *detailstype = NULL;
  2004. char *username = NULL;
  2005. char *groupname = NULL;
  2006. char *companyname = NULL;
  2007. char *set_companyname = NULL;
  2008. char *password = NULL;
  2009. char *emailadr = NULL;
  2010. char *fullname = NULL;
  2011. char *new_username = NULL;
  2012. char *storeguid = NULL;
  2013. const char *path = NULL;
  2014. char *lang = NULL;
  2015. char *feature = NULL;
  2016. char *node = NULL;
  2017. bool bFeature = true;
  2018. set<string, lstr> sEnabled;
  2019. set<string, lstr> sDisabled;
  2020. int quota = -1;
  2021. long long quotahard = -1;
  2022. long long quotasoft = -1;
  2023. long long quotawarn = -1;
  2024. int ud_quota = -1;
  2025. long long ud_quotahard = -1;
  2026. long long ud_quotasoft = -1;
  2027. long long ud_quotawarn = -1;
  2028. int isadmin = -1;
  2029. int isnonactive = -1;
  2030. int mr_accept = -1;
  2031. int mr_decline_conflict = -1;
  2032. int mr_decline_recurring = -1;
  2033. char *sendas_user = NULL;
  2034. int sendas_action = -1;
  2035. modes mode = MODE_INVALID;
  2036. int passprompt = 0;
  2037. bool bCopyToPublic = false;
  2038. int nFolderId = 0;
  2039. list<string> lstUsernames;
  2040. bool bAutoAccept = false, bDeclineConflict = false, bDeclineRecurring = false;
  2041. ULONG cbEntryID = 0;
  2042. object_ptr<IMsgStore> lpPublicStore, lpUserStore;
  2043. object_ptr<IExchangeManageStore> lpIEMS;
  2044. wstring strUsername;
  2045. wstring strStorename;
  2046. wstring strStorenameTMP;
  2047. wstring strCompanyName;
  2048. object_ptr<IMAPIFolder> lpDeletedStoresFolder, lpRootFolder;
  2049. ULONG ulObjType = 0;
  2050. ULONG ulCachePurgeMode = PURGE_CACHE_ALL;
  2051. unsigned int loglevel = EC_LOGLEVEL_NONE;
  2052. object_ptr<ECLogger> lpLogger;
  2053. const configsetting_t lpDefaults[] = {
  2054. { "server_socket", "default:" },
  2055. { "sslkey_file", "" },
  2056. { "sslkey_pass", "", CONFIGSETTING_EXACT },
  2057. { NULL, NULL },
  2058. };
  2059. std::unique_ptr<ECConfig> lpsConfig(ECConfig::Create(lpDefaults));
  2060. bool bExplicitConfig = false;
  2061. ConsoleTable ct(0,0);
  2062. const char *szConfig = ECConfig::GetDefaultPath("admin.cfg");
  2063. // Set locale to system variables
  2064. setlocale(LC_MESSAGES, "");
  2065. setlocale(LC_CTYPE, "");
  2066. setlocale(LC_TIME, "");
  2067. if(argc < 2) {
  2068. print_help(argv[0]);
  2069. return 1;
  2070. }
  2071. int c;
  2072. while (1) {
  2073. InputValidator validateInput;
  2074. c = getopt_long(argc, argv, "VlLsc:u:d:U:Pp:f:e:a:h:g:G:b:B:i:I:n:v", long_options, NULL);
  2075. if (c == -1)
  2076. break;
  2077. switch (c) {
  2078. case OPT_VERBOSITY:
  2079. loglevel = strtoul(optarg, NULL, 0);
  2080. break;
  2081. case 'v':
  2082. if (loglevel < EC_LOGLEVEL_DEBUG + 1)
  2083. ++loglevel;
  2084. break;
  2085. case 'l':
  2086. mode = MODE_LIST_USERS;
  2087. break;
  2088. case 's':
  2089. mode = MODE_CREATE_PUBLIC;
  2090. break;
  2091. case 'c':
  2092. mode = MODE_CREATE_USER;
  2093. username = validateInput(optarg);
  2094. break;
  2095. case 'u':
  2096. if (mode != MODE_HOOK_STORE)
  2097. mode = MODE_UPDATE_USER;
  2098. username = validateInput(optarg);
  2099. break;
  2100. case 'd':
  2101. mode = MODE_DELETE_USER;
  2102. username = validateInput(optarg);
  2103. break;
  2104. case 'g':
  2105. mode = MODE_CREATE_GROUP;
  2106. groupname = validateInput(optarg);
  2107. break;
  2108. case 'G':
  2109. mode = MODE_DELETE_GROUP;
  2110. groupname = validateInput(optarg);
  2111. break;
  2112. case 'L':
  2113. mode = MODE_LIST_GROUP;
  2114. break;
  2115. case 'b':
  2116. mode = MODE_ADDUSER_GROUP;
  2117. username = validateInput(optarg);
  2118. break;
  2119. case 'B':
  2120. mode = MODE_DELETEUSER_GROUP;
  2121. username = validateInput(optarg);
  2122. break;
  2123. case 'U':
  2124. new_username = validateInput(optarg);
  2125. break;
  2126. case 'P':
  2127. passprompt = 1;
  2128. break;
  2129. case 'p':
  2130. password = validateInput(optarg);
  2131. break;
  2132. case 'f':
  2133. fullname = validateInput(optarg);
  2134. break;
  2135. case 'e':
  2136. emailadr = validateInput(optarg);
  2137. break;
  2138. case 'a':
  2139. isadmin = atoi(optarg);
  2140. if (isadmin == 0)
  2141. isadmin = parse_yesno(optarg);
  2142. else
  2143. isadmin = min(2, isadmin);
  2144. break;
  2145. case 'n':
  2146. isnonactive = parse_yesno(optarg);
  2147. break;
  2148. case 'i':
  2149. groupname = validateInput(optarg);
  2150. break;
  2151. case 'I':
  2152. companyname = validateInput(optarg);
  2153. break;
  2154. // error handling?
  2155. case '?':
  2156. break;
  2157. case OPT_HOST:
  2158. case 'h':
  2159. path = validateInput(optarg);
  2160. break;
  2161. case OPT_HELP:
  2162. mode = MODE_HELP;
  2163. break;
  2164. case OPT_CREATE_STORE:
  2165. mode = MODE_CREATE_STORE;
  2166. username = validateInput(optarg);
  2167. break;
  2168. case OPT_DELETE_STORE:
  2169. mode = MODE_DELETE_STORE;
  2170. username = validateInput(optarg);
  2171. break;
  2172. case OPT_HOOK_STORE:
  2173. mode = MODE_HOOK_STORE;
  2174. storeguid = validateInput(optarg);
  2175. break;
  2176. case OPT_UNHOOK_STORE:
  2177. mode = MODE_UNHOOK_STORE;
  2178. username = validateInput(optarg);
  2179. break;
  2180. case OPT_REMOVE_STORE:
  2181. mode = MODE_REMOVE_STORE;
  2182. storeguid = validateInput(optarg);
  2183. break;
  2184. case OPT_COPYTO_PUBLIC:
  2185. bCopyToPublic = true;
  2186. break;
  2187. case OPT_SYNC_USERS:
  2188. mode = MODE_SYNC_USERS;
  2189. break;
  2190. case OPT_DETAILS:
  2191. mode = MODE_DETAILS;
  2192. username = validateInput(optarg);
  2193. break;
  2194. case OPT_DETAILS_TYPE:
  2195. detailstype = validateInput(optarg);
  2196. break;
  2197. // Make values from Mb to bytes which the server wants
  2198. case OPT_USER_QUOTA_HARD:
  2199. quotahard = atoll(optarg) *1024*1024;
  2200. break;
  2201. case OPT_USER_QUOTA_SOFT:
  2202. quotasoft = atoll(optarg) *1024*1024;
  2203. break;
  2204. case OPT_USER_QUOTA_WARN:
  2205. quotawarn = atoll(optarg) *1024*1024;
  2206. break;
  2207. case OPT_USER_QUOTA_OVERRIDE:
  2208. quota = parse_yesno(optarg);
  2209. break;
  2210. case OPT_USER_DEFAULT_QUOTA_HARD:
  2211. ud_quotahard = atoll(optarg) * 1024 * 1024;
  2212. break;
  2213. case OPT_USER_DEFAULT_QUOTA_SOFT:
  2214. ud_quotasoft = atoll(optarg) * 1024 * 1024;
  2215. break;
  2216. case OPT_USER_DEFAULT_QUOTA_WARN:
  2217. ud_quotawarn = atoll(optarg) * 1024 * 1024;
  2218. break;
  2219. case OPT_USER_DEFAULT_QUOTA_OVERRIDE:
  2220. ud_quota = parse_yesno(optarg);
  2221. break;
  2222. case OPT_LANG:
  2223. // Use alternate language
  2224. lang = validateInput(optarg);
  2225. break;
  2226. case OPT_MR_ACCEPT:
  2227. mr_accept = parse_yesno(optarg);
  2228. break;
  2229. case OPT_MR_DECLINE_CONFLICT:
  2230. mr_decline_conflict = parse_yesno(optarg);
  2231. break;
  2232. case OPT_MR_DECLINE_RECURRING:
  2233. mr_decline_recurring = parse_yesno(optarg);
  2234. break;
  2235. case OPT_LIST_SENDAS:
  2236. mode = MODE_LIST_SENDAS;
  2237. username = validateInput(optarg);
  2238. break;
  2239. case OPT_ADD_SENDAS:
  2240. sendas_user = validateInput(optarg);
  2241. sendas_action = 1;
  2242. break;
  2243. case OPT_DEL_SENDAS:
  2244. sendas_user = validateInput(optarg);
  2245. sendas_action = 0;
  2246. break;
  2247. case OPT_UPDATE_GROUP:
  2248. mode = MODE_UPDATE_GROUP;
  2249. groupname = validateInput(optarg);
  2250. break;
  2251. case OPT_CREATE_COMPANY:
  2252. mode = MODE_CREATE_COMPANY;
  2253. companyname = validateInput(optarg);
  2254. break;
  2255. case OPT_UPDATE_COMPANY:
  2256. mode = MODE_UPDATE_COMPANY;
  2257. companyname = validateInput(optarg);
  2258. break;
  2259. case OPT_DELETE_COMPANY:
  2260. mode = MODE_DELETE_COMPANY;
  2261. companyname = validateInput(optarg);
  2262. break;
  2263. case OPT_LIST_COMPANY:
  2264. mode = MODE_LIST_COMPANY;
  2265. break;
  2266. case OPT_ADD_VIEW:
  2267. mode = MODE_ADD_VIEW;
  2268. set_companyname = validateInput(optarg);
  2269. break;
  2270. case OPT_DEL_VIEW:
  2271. mode = MODE_DEL_VIEW;
  2272. set_companyname = validateInput(optarg);
  2273. break;
  2274. case OPT_LIST_VIEW:
  2275. mode = MODE_LIST_VIEW;
  2276. break;
  2277. case OPT_ADD_ADMIN:
  2278. mode = MODE_ADD_ADMIN;
  2279. username = validateInput(optarg);
  2280. break;
  2281. case OPT_DEL_ADMIN:
  2282. mode = MODE_DEL_ADMIN;
  2283. username = validateInput(optarg);
  2284. break;
  2285. case OPT_LIST_ADMIN:
  2286. mode = MODE_LIST_ADMIN;
  2287. break;
  2288. case OPT_SYSTEM_ADMIN:
  2289. mode = MODE_SYSTEM_ADMIN;
  2290. username = validateInput(optarg);
  2291. break;
  2292. case OPT_ADD_UQUOTA_RECIPIENT:
  2293. mode = MODE_ADD_USERQUOTA_RECIPIENT;
  2294. username = validateInput(optarg);
  2295. break;
  2296. case OPT_DEL_UQUOTA_RECIPIENT:
  2297. mode = MODE_DEL_USERQUOTA_RECIPIENT;
  2298. username = validateInput(optarg);
  2299. break;
  2300. case OPT_LIST_UQUOTA_RECIPIENT:
  2301. mode = MODE_LIST_USERQUOTA_RECIPIENT;
  2302. break;
  2303. case OPT_ADD_CQUOTA_RECIPIENT:
  2304. mode = MODE_ADD_COMPANYQUOTA_RECIPIENT;
  2305. username = validateInput(optarg);
  2306. break;
  2307. case OPT_DEL_CQUOTA_RECIPIENT:
  2308. mode = MODE_DEL_COMPANYQUOTA_RECIPIENT;
  2309. username = validateInput(optarg);
  2310. break;
  2311. case OPT_LIST_CQUOTA_RECIPIENT:
  2312. mode = MODE_LIST_COMPANYQUOTA_RECIPIENT;
  2313. break;
  2314. case OPT_PURGE_SOFTDELETE:
  2315. ulDays = atoui(optarg);
  2316. mode = MODE_PURGE_SOFTDELETE;
  2317. break;
  2318. case OPT_CLEAR_CACHE:
  2319. mode = MODE_CLEAR_CACHE;
  2320. if (optarg)
  2321. ulCachePurgeMode = strtol(optarg, NULL, 0);
  2322. break;
  2323. case OPT_PURGE_DEFERRED:
  2324. mode = MODE_PURGE_DEFERRED;
  2325. break;
  2326. case OPT_LIST_ORPHANS:
  2327. mode = MODE_LIST_ORPHANS;
  2328. break;
  2329. case OPT_CONFIG:
  2330. szConfig = validateInput(optarg);
  2331. bExplicitConfig = true;
  2332. break;
  2333. case OPT_UTF8: {
  2334. // set early, so other arguments are parsed in this charset.
  2335. std::string locale;
  2336. if (!forceUTF8Locale(false, &locale)) {
  2337. cerr << "Your system does not have the '" << locale << "' locale installed." << endl;
  2338. cerr << "Please install this locale before creating new users." << endl;
  2339. return 1;
  2340. }
  2341. break;
  2342. }
  2343. case OPT_FORCE_RESYNC:
  2344. mode = MODE_FORCE_RESYNC;
  2345. break;
  2346. case OPT_USER_COUNT:
  2347. mode = MODE_USER_COUNT;
  2348. break;
  2349. case OPT_ENABLE_FEATURE:
  2350. case OPT_DISABLE_FEATURE:
  2351. if (feature) {
  2352. cerr << "Only one feature can be enabled/disabled at a time" << endl;
  2353. break;
  2354. }
  2355. if (!isFeature(optarg)) {
  2356. cerr << optarg << " is not a valid kopano feature" << endl;
  2357. break;
  2358. }
  2359. feature = optarg;
  2360. bFeature = (c == OPT_ENABLE_FEATURE);
  2361. break;
  2362. case OPT_VERSION:
  2363. case 'V':
  2364. cout << "Product version:\t" << PROJECT_VERSION_PROFADMIN_STR << endl
  2365. << "File version:\t\t" << PROJECT_SVN_REV_STR << endl;
  2366. return EXIT_SUCCESS;
  2367. case OPT_SELECT_NODE:
  2368. node = validateInput(optarg);
  2369. break;
  2370. case OPT_RESET_FOLDER_COUNT:
  2371. mode = MODE_RESET_FOLDER_COUNT;
  2372. username = validateInput(optarg);
  2373. break;
  2374. default:
  2375. break;
  2376. };
  2377. if (validateInput.Failed()) {
  2378. cerr << "Invalid input '" << optarg << "' found." << endl;
  2379. // no need to return, later input checking will print an error too
  2380. }
  2381. }
  2382. // check empty input
  2383. if (username && username[0] == 0x00) {
  2384. cerr << "Username (-u) cannot be empty" << endl;
  2385. return 1;
  2386. }
  2387. if (username && strcasecmp(username, "SYSTEM")==0) {
  2388. cerr << "Username (-u) cannot be SYSTEM" << endl;
  2389. return 1;
  2390. }
  2391. if (password && password[0] == 0x00) {
  2392. cerr << "Password (-p) cannot be empty" << endl;
  2393. return 1;
  2394. }
  2395. if (companyname && companyname[0] == 0x00){
  2396. cerr << "Companyname (-I) cannot be empty" << endl;
  2397. return 1;
  2398. }
  2399. if (groupname && groupname[0] == 0x00) {
  2400. cerr << "Groupname cannot be empty" << endl;
  2401. return 1;
  2402. }
  2403. if (fullname && fullname[0] == 0x00) {
  2404. cerr << "Fullname (-f) cannot be empty" << endl;
  2405. return 1;
  2406. }
  2407. if (emailadr && emailadr[0] == 0x00) {
  2408. cerr << "Email address (-e) cannot be empty" << endl;
  2409. return 1;
  2410. }
  2411. // --force-resync takes all left over arguments as usernames
  2412. if (mode == MODE_FORCE_RESYNC) {
  2413. assert(optind <= argc);
  2414. std::copy(argv + optind, argv + argc, std::back_inserter(lstUsernames));
  2415. optind = argc;
  2416. }
  2417. // check parameters
  2418. if (optind < argc) {
  2419. cerr << "Too many options given." << endl;
  2420. return 1;
  2421. }
  2422. if (mode == MODE_INVALID) {
  2423. cerr << "No correct command (e.g. -c for create user) given." << endl;
  2424. return 1;
  2425. }
  2426. if (mode == MODE_HELP) {
  2427. print_help(argv[0]);
  2428. cout << endl << "Please read kopano-admin(8) for detailed information. Enter `man kopano-admin` to view it." << endl << endl;
  2429. return 0;
  2430. }
  2431. // For the following modes we need a company name.
  2432. if (!companyname &&
  2433. (mode == MODE_ADD_VIEW || mode == MODE_DEL_VIEW || mode == MODE_LIST_VIEW ||
  2434. mode == MODE_ADD_ADMIN || mode == MODE_DEL_ADMIN || mode == MODE_LIST_ADMIN ||
  2435. mode == MODE_SYSTEM_ADMIN)){
  2436. cerr << "Missing companyname to perform action" << endl;
  2437. return 1;
  2438. }
  2439. if (mode == MODE_DETAILS && username == NULL) {
  2440. cerr << "Missing information to show user details." << endl;
  2441. return 1;
  2442. }
  2443. if (mode == MODE_CREATE_USER) {
  2444. bool has_username = username != NULL;
  2445. bool has_password = !(password == NULL && passprompt == 0 && isnonactive < 1);
  2446. bool has_emailaddr = emailadr != NULL;
  2447. bool has_fullname = fullname != NULL;
  2448. if (!has_username || !has_password || !has_emailaddr || !has_fullname) {
  2449. cerr << "Missing information to create user:";
  2450. if (!has_username)
  2451. cerr << " username (-u)";
  2452. if (!has_password)
  2453. cerr << " password (-p)";
  2454. if (!has_emailaddr)
  2455. cerr << " email address (-e)";
  2456. if (!has_fullname)
  2457. cerr << " full name (-f)";
  2458. cerr << endl;
  2459. return EXIT_FAILURE;
  2460. }
  2461. }
  2462. if (mode == MODE_CREATE_USER && quota == 1 && (quotahard == -1 || quotawarn == -1 || quotasoft == -1)) {
  2463. cerr << "Not all user specific quota levels are given." << endl;
  2464. cerr << "Missing information to create user:";
  2465. missing_quota(quotahard, quotawarn, quotasoft);
  2466. cerr << endl;
  2467. return 1;
  2468. }
  2469. if (mode == MODE_CREATE_COMPANY &&
  2470. ((quota == 1 && quotawarn == -1) ||
  2471. (ud_quota == 1 && (ud_quotahard == -1 || ud_quotasoft == -1 || ud_quotawarn == -1)))) {
  2472. cerr << "Not all company specific quota levels are given." << endl;
  2473. cerr << "Missing information to create company:";
  2474. if (quota == 1 && quotawarn == -1)
  2475. cerr << " warn quota (--qw)";
  2476. if (ud_quota == 1)
  2477. missing_quota(ud_quotahard, ud_quotawarn, ud_quotasoft);
  2478. cerr << endl;
  2479. return 1;
  2480. }
  2481. if (mode == MODE_CREATE_STORE && username == NULL) {
  2482. cerr << "Missing username (-u) to be able to create store." << endl;
  2483. return 1;
  2484. }
  2485. if (mode == MODE_DELETE_STORE) {
  2486. cerr << "Delete store action is not available anymore. Use --remove-store to remove a store from the database." << endl;
  2487. return 1;
  2488. }
  2489. if (mode == MODE_HOOK_STORE && (storeguid == NULL || (username == NULL && bCopyToPublic == false) ) ) {
  2490. cerr << "Missing information to hook store:";
  2491. if (storeguid == NULL)
  2492. cerr << " store GUID (--hook-store)";
  2493. if (username == NULL && bCopyToPublic == false)
  2494. cerr << " username (-u)";
  2495. cerr << endl;
  2496. return 1;
  2497. }
  2498. if (mode == MODE_UNHOOK_STORE && username == NULL) {
  2499. cerr << "Missing username (-u) to unhook store for." << endl;
  2500. return 1;
  2501. }
  2502. if (mode == MODE_REMOVE_STORE && storeguid == NULL) {
  2503. cerr << "Missing guid (--remove-store) to remove store for." << endl;
  2504. return 1;
  2505. }
  2506. if (mode == MODE_UPDATE_USER && password == NULL && passprompt == 0 &&
  2507. emailadr == NULL && fullname == NULL && new_username == NULL && isadmin == -1 &&
  2508. quota == -1 && quotahard == -1 && quotasoft == -1 && quotawarn == -1 &&
  2509. mr_accept == -1 && mr_decline_conflict == -1 && mr_decline_recurring == -1 &&
  2510. sendas_user == NULL && isnonactive == -1 && feature == NULL) {
  2511. cerr << "Missing information to update user (e.g. password, quota, see --help)." << endl;
  2512. return 1;
  2513. }
  2514. if (mode == MODE_DELETE_USER && username == NULL) {
  2515. cerr << "Missing username (-u) to delete." << endl;
  2516. return 1;
  2517. }
  2518. if (mode == MODE_CREATE_GROUP && groupname == NULL) {
  2519. cerr << "Missing name of group (-g) to create." << endl;
  2520. return 1;
  2521. }
  2522. if (mode == MODE_UPDATE_GROUP && (groupname == NULL || (emailadr == NULL && sendas_user == NULL) ) ) {
  2523. cerr << "Missing information to update group:";
  2524. if (!groupname)
  2525. cerr << " group name";
  2526. if (!emailadr && !sendas_user)
  2527. cerr << " either e-mail address (-e) or \"send-as user\" (--add-sendas)";
  2528. cerr << endl;
  2529. return 1;
  2530. }
  2531. if (mode == MODE_DELETE_GROUP && groupname == NULL) {
  2532. cerr << "Missing name of group (-G) to delete." << endl;
  2533. return 1;
  2534. }
  2535. if (mode == MODE_ADDUSER_GROUP && (groupname == NULL || username == NULL)) {
  2536. cerr << "Missing information to add user to group:";
  2537. if (!groupname)
  2538. cerr << " group name (-i)";
  2539. if (!username)
  2540. cerr << " user name";
  2541. cerr << endl;
  2542. return 1;
  2543. }
  2544. if (mode == MODE_DELETEUSER_GROUP && (groupname == NULL || username == NULL)) {
  2545. cerr << "Missing information to remove user from group:";
  2546. if (!groupname)
  2547. cerr << " group name (-i)";
  2548. if (!username)
  2549. cerr << " user name";
  2550. cerr << endl;
  2551. return 1;
  2552. }
  2553. if (mode == MODE_CREATE_COMPANY && companyname == NULL) {
  2554. cerr << "Missing name of company to create." << endl;
  2555. return 1;
  2556. }
  2557. if (mode == MODE_UPDATE_COMPANY &&
  2558. ((quota == 1 && quotawarn == -1) ||
  2559. (ud_quota == 1 && (ud_quotahard == -1 || ud_quotasoft == -1 || ud_quotawarn == -1)))) {
  2560. cerr << "Missing information to update company:";
  2561. if (quota == 1 && quotawarn == -1)
  2562. cerr << " warn quota (--qw)";
  2563. if (ud_quota == 1)
  2564. missing_quota(ud_quotahard, ud_quotawarn, ud_quotasoft);
  2565. cerr << endl;
  2566. return 1;
  2567. }
  2568. if (mode == MODE_DELETE_COMPANY && companyname == NULL) {
  2569. cerr << "Missing name of company to delete." << endl;
  2570. return 1;
  2571. }
  2572. if (mode == MODE_ADD_VIEW && set_companyname == NULL) {
  2573. cerr << "Missing company name to add remote view privilege to." << endl;
  2574. return 1;
  2575. }
  2576. if (mode == MODE_DEL_VIEW && set_companyname == NULL) {
  2577. cerr << "Missing company name to delete remote view privilege to." << endl;
  2578. return 1;
  2579. }
  2580. if (mode == MODE_ADD_ADMIN && username == NULL) {
  2581. cerr << "Missing username to add remote administrator to." << endl;
  2582. return 1;
  2583. }
  2584. if (mode == MODE_DEL_ADMIN && username == NULL) {
  2585. cerr << "Missing username to delete remote administration privilege for." << endl;
  2586. return 1;
  2587. }
  2588. if (mode == MODE_SYSTEM_ADMIN && username == NULL) {
  2589. cerr << "Missing username to set system administrator privilege for." << endl;
  2590. return 1;
  2591. }
  2592. if ((mode == MODE_ADD_USERQUOTA_RECIPIENT || mode == MODE_DEL_USERQUOTA_RECIPIENT ||
  2593. mode == MODE_ADD_COMPANYQUOTA_RECIPIENT || mode == MODE_DEL_COMPANYQUOTA_RECIPIENT) &&
  2594. username == NULL) {
  2595. cerr << "Missing username to edit quota recipients for." << endl;
  2596. return 1;
  2597. }
  2598. if (mode == MODE_RESET_FOLDER_COUNT && username == NULL) {
  2599. cerr << "Missing username to reset folder counts for." << endl;
  2600. return 1;
  2601. }
  2602. if (lang && mode != MODE_CREATE_STORE) {
  2603. cerr << "You can only use the --lang option in combination with --create-store. If you" << endl;
  2604. cerr << "wish to create a store in this language, you must edit the 00createstore" << endl;
  2605. cerr << "script, which is probably in /etc/kopano/userscripts/createuser.d and" << endl;
  2606. cerr << "specify the --lang option there." << endl;
  2607. return 1;
  2608. }
  2609. // check warnings
  2610. if (new_username != NULL && mode != MODE_UPDATE_USER) {
  2611. cerr << "WARNING: new username \"" << new_username << "\" will be ignored (only used for -U)." << endl;
  2612. }
  2613. if ((quota == 0 && (quotawarn >= 0 || quotasoft >= 0 || quotahard >= 0)) ||
  2614. (ud_quota == 0 && (ud_quotawarn >= 0 || ud_quotasoft >= 0 || ud_quotahard >= 0))) {
  2615. cerr << "Disabling quota override, but quota levels are provided." << endl;
  2616. cerr << "By disabling quota overrides the existing values will be reset," << endl;
  2617. cerr << "and these new values will be ignored." << endl;
  2618. }
  2619. if ((quota == -1 && (quotawarn >= 0 || quotasoft >= 0 || quotahard >= 0)) ||
  2620. (ud_quota == -1 && (ud_quotawarn >= 0 || ud_quotasoft >= 0 || ud_quotahard >= 0))) {
  2621. cerr << "Quota levels are provided, but not quota level override" << endl;
  2622. cerr << "Without an explicit quota override value the quota levels will be ignored." << endl;
  2623. }
  2624. // confirmations
  2625. if (mode == MODE_FORCE_RESYNC && lstUsernames.empty()) {
  2626. string response;
  2627. cout << "You requested a forced resync without arguments, are you sure you want" << endl;
  2628. cout << "force a resync of all offline profiles for all users? [y/N]: ";
  2629. cin >> response;
  2630. if (response.empty() || strcasecmp(response.c_str(), "n") == 0 || strcasecmp(response.c_str(), "no") == 0)
  2631. return 0;
  2632. if (strcasecmp(response.c_str(), "y") != 0 && strcasecmp(response.c_str(), "yes") != 0) {
  2633. cout << "Invalid response." << endl;
  2634. return 1;
  2635. }
  2636. }
  2637. if (szConfig) {
  2638. bool bHaveConfig = lpsConfig->LoadSettings(szConfig);
  2639. /* Special case on complaining errors in config file:
  2640. * - explicit config file given, but was not found
  2641. * - default config file loaded, but contains errors
  2642. * This makes that we do not complain for errors when an "invalid" config was given but did load ok.
  2643. * This is a trick that people can use to give the spooler or dagent a config for its client SSL settings,
  2644. * which are the only settings used by the admin program.
  2645. */
  2646. if ((!bHaveConfig && bExplicitConfig) || (bHaveConfig && !bExplicitConfig && lpsConfig->HasErrors())) {
  2647. cerr << "Error while reading configuration file " << szConfig << endl;
  2648. // create fatal logger without a timestamp to stderr
  2649. lpLogger.reset(new ECLogger_File(EC_LOGLEVEL_FATAL, 0, "-", false), false);
  2650. ec_log_set(lpLogger);
  2651. LogConfigErrors(lpsConfig.get());
  2652. return 1;
  2653. }
  2654. }
  2655. if(lang) {
  2656. char* locale = setlocale(LC_MESSAGES, lang);
  2657. if (!locale) {
  2658. cerr << "Your system does not have the '" << lang << "' locale installed." << endl;
  2659. cerr << "Please install this locale before creating new users." << endl;
  2660. // do not create store in wrong language, give admin a chance to make store in right locale
  2661. return 1;
  2662. }
  2663. }
  2664. if (loglevel > EC_LOGLEVEL_DEBUG)
  2665. loglevel = EC_LOGLEVEL_ALWAYS;
  2666. if (loglevel > EC_LOGLEVEL_NONE)
  2667. lpLogger.reset(new ECLogger_File(loglevel, 0, "-", false), false);
  2668. else
  2669. lpLogger.reset(new ECLogger_Null, false);
  2670. ec_log_set(lpLogger);
  2671. //Init mapi
  2672. hr = mapiinit.Initialize();
  2673. if (hr != hrSuccess) {
  2674. cerr << "Unable to initialize" << endl;
  2675. goto exit;
  2676. }
  2677. /*
  2678. * server path sequence is:
  2679. * 1. -h option from command line
  2680. * 2. KOPANO_SOCKET environment variable
  2681. * 3. config setting option
  2682. */
  2683. if (!path) {
  2684. path = lpsConfig->GetSetting("server_socket");
  2685. // environment variable may override setting variable
  2686. path = GetServerUnixSocket(path);
  2687. }
  2688. hr = HrOpenECAdminSession(&~lpSession, "admin", PROJECT_SVN_REV_STR,
  2689. path, EC_PROFILE_FLAGS_NO_NOTIFICATIONS,
  2690. lpsConfig->GetSetting("sslkey_file", "", NULL),
  2691. lpsConfig->GetSetting("sslkey_pass", "", NULL));
  2692. if(hr != hrSuccess) {
  2693. cerr << "Unable to open Admin session: " <<
  2694. GetMAPIErrorMessage(hr) << " (" <<
  2695. stringify(hr, true) << ")" << endl;
  2696. switch (hr) {
  2697. case MAPI_E_NETWORK_ERROR:
  2698. cerr << "The server is not running, or not accessible";
  2699. if (path != NULL && *path != '\0')
  2700. cerr << " through \"" << path << "\"";
  2701. cerr << "." << endl;
  2702. break;
  2703. case MAPI_E_LOGON_FAILED:
  2704. case MAPI_E_NO_ACCESS:
  2705. cerr << "Access was denied on " << path << "." << endl;
  2706. break;
  2707. default:
  2708. break;
  2709. };
  2710. goto exit;
  2711. }
  2712. hr = HrOpenDefaultStore(lpSession, &~lpMsgStore);
  2713. if(hr != hrSuccess) {
  2714. cerr << "Unable to open Admin store: " <<
  2715. GetMAPIErrorMessage(hr) << " (" <<
  2716. stringify(hr,true) << ")" << endl;
  2717. goto exit;
  2718. }
  2719. if (node != NULL && *node != '\0') {
  2720. MsgStorePtr ptrRemoteStore;
  2721. hr = HrGetRemoteAdminStore(lpSession, lpMsgStore, (LPTSTR)node, 0, &~ptrRemoteStore);
  2722. if (hr != hrSuccess) {
  2723. cerr << "Unable to connect to node '" << node << "':" <<
  2724. GetMAPIErrorMessage(hr) << " (" <<
  2725. stringify(hr, true) << ")" << endl;
  2726. switch (hr) {
  2727. case MAPI_E_NETWORK_ERROR:
  2728. cerr << "The server is not running, or not accessible." << endl;
  2729. break;
  2730. case MAPI_E_LOGON_FAILED:
  2731. case MAPI_E_NO_ACCESS:
  2732. cerr << "Access was denied." << endl;
  2733. break;
  2734. case MAPI_E_NOT_FOUND:
  2735. cerr << "Node '" << node << "' is unknown." << endl;
  2736. break;
  2737. default:
  2738. break;
  2739. }
  2740. goto exit;
  2741. }
  2742. lpMsgStore.reset(ptrRemoteStore.release(), false);
  2743. }
  2744. hr = HrGetOneProp(lpMsgStore, PR_EC_OBJECT, &~lpPropValue);
  2745. if(hr != hrSuccess || !lpPropValue || !lpPropValue->Value.lpszA) {
  2746. cerr << "Admin object not found." << endl;
  2747. goto exit;
  2748. }
  2749. lpECMsgStore.reset(reinterpret_cast<IECUnknown *>(lpPropValue->Value.lpszA));
  2750. hr = lpECMsgStore->QueryInterface(IID_IECServiceAdmin, &~lpServiceAdmin);
  2751. if(hr != hrSuccess) {
  2752. cerr << "Admin object query error." << endl;
  2753. goto exit;
  2754. }
  2755. switch (mode) {
  2756. case MODE_CREATE_PUBLIC:
  2757. if (companyname == nullptr) {
  2758. cbCompanyId = g_cbEveryoneEid;
  2759. hr = MAPIAllocateBuffer(g_cbEveryoneEid, &~lpCompanyId);
  2760. if (hr != hrSuccess)
  2761. goto exit;
  2762. memcpy(lpCompanyId, g_lpEveryoneEid, g_cbEveryoneEid);
  2763. break;
  2764. }
  2765. /* fallthrough */
  2766. case MODE_UPDATE_COMPANY:
  2767. case MODE_DELETE_COMPANY:
  2768. case MODE_ADD_VIEW:
  2769. case MODE_DEL_VIEW:
  2770. case MODE_LIST_VIEW:
  2771. case MODE_ADD_ADMIN:
  2772. case MODE_DEL_ADMIN:
  2773. case MODE_LIST_ADMIN:
  2774. case MODE_SYSTEM_ADMIN:
  2775. case MODE_ADD_USERQUOTA_RECIPIENT:
  2776. case MODE_DEL_USERQUOTA_RECIPIENT:
  2777. case MODE_LIST_USERQUOTA_RECIPIENT:
  2778. case MODE_ADD_COMPANYQUOTA_RECIPIENT:
  2779. case MODE_DEL_COMPANYQUOTA_RECIPIENT:
  2780. case MODE_LIST_COMPANYQUOTA_RECIPIENT:
  2781. hr = lpServiceAdmin->ResolveCompanyName((LPTSTR)companyname, 0, &cbCompanyId, &~lpCompanyId);
  2782. if (hr != hrSuccess) {
  2783. fprintf(stderr, "Failed to resolve company: %s\n", getMapiCodeString(hr, companyname).c_str());
  2784. goto exit;
  2785. }
  2786. break;
  2787. default:
  2788. break;
  2789. }
  2790. switch (mode) {
  2791. case MODE_CREATE_STORE:
  2792. case MODE_DELETE_USER:
  2793. case MODE_UPDATE_USER:
  2794. case MODE_ADDUSER_GROUP:
  2795. case MODE_DELETEUSER_GROUP:
  2796. case MODE_ADD_ADMIN:
  2797. case MODE_DEL_ADMIN:
  2798. case MODE_SYSTEM_ADMIN:
  2799. case MODE_ADD_USERQUOTA_RECIPIENT:
  2800. case MODE_DEL_USERQUOTA_RECIPIENT:
  2801. case MODE_ADD_COMPANYQUOTA_RECIPIENT:
  2802. case MODE_DEL_COMPANYQUOTA_RECIPIENT:
  2803. if (username == nullptr)
  2804. break;
  2805. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  2806. if (hr != hrSuccess) {
  2807. fprintf(stderr, "Failed to resolve user: %s\n", getMapiCodeString(hr, username).c_str());
  2808. goto exit;
  2809. }
  2810. break;
  2811. default:
  2812. break;
  2813. }
  2814. // fully logged on, action!
  2815. switch(mode) {
  2816. case MODE_LIST_USERS:
  2817. hr = ForEachCompany(lpServiceAdmin, companyname, ListUsers);
  2818. if (hr != hrSuccess)
  2819. goto exit;
  2820. break;
  2821. case MODE_LIST_ORPHANS:
  2822. hr = list_orphans(lpServiceAdmin);
  2823. if (hr != hrSuccess)
  2824. goto exit;
  2825. break;
  2826. case MODE_DETAILS:
  2827. if (detailstype == NULL || strcasecmp(detailstype, "user") == 0)
  2828. ulClass = ACTIVE_USER;
  2829. else if (strcasecmp(detailstype, "group") == 0)
  2830. ulClass = DISTLIST_GROUP;
  2831. else if (strcasecmp(detailstype, "company") == 0)
  2832. ulClass = CONTAINER_COMPANY;
  2833. else if (strcasecmp(detailstype, "archive") != 0) {
  2834. hr = MAPI_E_INVALID_TYPE;
  2835. cerr << "Unknown userobject type \"" << detailstype << "\"" << endl;
  2836. goto exit;
  2837. }
  2838. if (detailstype && strcasecmp(detailstype, "archive") == 0)
  2839. hr = print_archive_details(lpSession, lpECMsgStore, username);
  2840. else
  2841. hr = print_details(lpSession, lpECMsgStore, ulClass, username);
  2842. if (hr != hrSuccess)
  2843. goto exit;
  2844. break;
  2845. case MODE_CREATE_PUBLIC:
  2846. /* The public store is created for a particular company, to do this correctly we will
  2847. * pass the company id as the group id for the store. */
  2848. hr = lpServiceAdmin->CreateStore(ECSTORE_TYPE_PUBLIC, cbCompanyId, lpCompanyId, &cbStoreId, &~lpStoreId, &cbRootId, &~lpRootId);
  2849. if(hr != hrSuccess) {
  2850. cerr << "Unable to create store, " << getMapiCodeString(hr, "public") << endl;
  2851. goto exit;
  2852. }
  2853. cout << "Public created." << endl;
  2854. break;
  2855. case MODE_CREATE_USER:
  2856. memset(&sECUser, 0, sizeof(sECUser));
  2857. sECUser.sUserId.cb = g_cbDefaultEid;
  2858. sECUser.sUserId.lpb = g_lpDefaultEid;
  2859. sECUser.lpszUsername = (LPTSTR)username;
  2860. if (passprompt && isnonactive != 1) {
  2861. sECUser.lpszPassword = (LPTSTR)get_password();
  2862. if (sECUser.lpszPassword == NULL) {
  2863. cerr << "Passwords don't match" << endl;
  2864. return 1;
  2865. }
  2866. if (sECUser.lpszPassword[0] == 0x00) {
  2867. cerr << "Password cannot be empty" << endl;
  2868. return 1;
  2869. }
  2870. }
  2871. else if (isnonactive == 1)
  2872. sECUser.lpszPassword = NULL;
  2873. else
  2874. sECUser.lpszPassword = (LPTSTR)password;
  2875. sECUser.lpszMailAddress = (LPTSTR)emailadr;
  2876. sECUser.lpszFullName = (LPTSTR)fullname;
  2877. sECUser.ulIsAdmin = (isadmin != -1)?isadmin:0;
  2878. // FIXME: create user, room or equipment!
  2879. sECUser.ulObjClass = ACTIVE_USER;
  2880. if (isnonactive == 1)
  2881. sECUser.ulObjClass = NONACTIVE_USER; // NONACTIVE_ROOM, NONACTIVE_EQUIPMENT, (NONACTIVE_CONTACT?)
  2882. hr = lpServiceAdmin->CreateUser(&sECUser, 0, &cbUserId, &~lpUserId);
  2883. if (hr != hrSuccess) {
  2884. cerr << "Unable to create user, " << getMapiCodeString(hr, username) << endl;
  2885. cerr << "Check server.log for details." << endl;
  2886. goto exit;
  2887. }
  2888. // set quota data
  2889. if (quota != -1 || quotahard != -1 || quotasoft != -1 || quotawarn != -1) {
  2890. hr = setQuota(lpServiceAdmin, cbUserId, lpUserId, quota, false, quotawarn, quotasoft, quotahard);
  2891. if(hr != hrSuccess)
  2892. goto exit;
  2893. }
  2894. cout << "User created." << endl;
  2895. break;
  2896. case MODE_CREATE_STORE:
  2897. hr = lpServiceAdmin->CreateStore(ECSTORE_TYPE_PRIVATE, cbUserId, lpUserId, &cbStoreId, &~lpStoreId, &cbRootId, &~lpRootId);
  2898. if (hr != hrSuccess) {
  2899. cerr << "Unable to create store, " << getMapiCodeString(hr, "store") << endl;
  2900. goto exit;
  2901. }
  2902. cout << "User store '" << username << "' created." << endl;
  2903. break;
  2904. case MODE_DELETE_USER:
  2905. hr = lpServiceAdmin->DeleteUser(cbUserId, lpUserId);
  2906. if (hr != hrSuccess) {
  2907. cerr << "Unable to delete user, " << getMapiCodeString(hr, username) << endl;
  2908. goto exit;
  2909. }
  2910. cout << "User deleted." << endl;
  2911. break;
  2912. case MODE_DELETE_STORE:
  2913. // happy compiler
  2914. break;
  2915. case MODE_HOOK_STORE:
  2916. hr = Util::hex2bin(storeguid, sizeof(GUID)*2, &cbGUID, (&~lpGUID).as<unsigned char>());
  2917. if (hr != hrSuccess) {
  2918. cerr << "Incorrect store guid '" << storeguid << "'" << endl;
  2919. goto exit;
  2920. }
  2921. if (bCopyToPublic == true) {
  2922. // Find store entryid
  2923. hr = GetOrphanStoreInfo(lpServiceAdmin, lpGUID, path, strUsername, strCompanyName, &cbEntryID, &~lpEntryID);
  2924. if (hr != hrSuccess) {
  2925. cerr << "Unable to get the store information. store guid '" << storeguid << "'" << endl;
  2926. goto exit;
  2927. }
  2928. // Open store the orphan store
  2929. hr = lpSession->OpenMsgStore(0, cbEntryID, lpEntryID, nullptr, MAPI_BEST_ACCESS, &~lpUserStore);
  2930. if (hr != hrSuccess) {
  2931. cerr << "Unable to open the orphan store, " << getMapiCodeString(hr) << endl;
  2932. goto exit;
  2933. }
  2934. // Open the root container for copy the folders
  2935. hr = lpUserStore->OpenEntry(0, nullptr, nullptr, MAPI_BEST_ACCESS, &ulObjType, &~lpRootFolder);
  2936. if (hr != hrSuccess) {
  2937. cerr << "Unable to open root folder of the orphan store, " << getMapiCodeString(hr) << endl;
  2938. goto exit;
  2939. }
  2940. hr = HrGetOneProp(lpUserStore, PR_IPM_SUBTREE_ENTRYID, &~lpPropValue);
  2941. if (hr != hrSuccess) {
  2942. cerr << "Unable to open the orphans store sub-entry, " << getMapiCodeString(hr) << endl;
  2943. goto exit;
  2944. }
  2945. // Open the public store
  2946. hr = GetPublicStore(lpSession, lpUserStore, strCompanyName, &~lpPublicStore);
  2947. if (hr != hrSuccess) {
  2948. cerr << "Unable to open the public store, " << getMapiCodeString(hr) << endl;
  2949. goto exit;
  2950. }
  2951. // open/create folders admin/stores
  2952. hr = OpenDeletedStoresFolder(lpPublicStore, &~lpDeletedStoresFolder);
  2953. if (hr != hrSuccess) {
  2954. cerr << "Unable to open the folder \"deleted stores\", " << getMapiCodeString(hr) << endl;
  2955. goto exit;
  2956. }
  2957. // Copy everything to the public
  2958. strStorename = L"Deleted User - ";
  2959. if (strUsername.empty())
  2960. strStorename += convert_to<wstring>(storeguid);
  2961. else
  2962. strStorename += strUsername;
  2963. cerr << "Start to copy the orphan store to the public store to the folder '" << convert_to<string>(strStorename) << "'" << endl;
  2964. strStorenameTMP = strStorename;
  2965. nFolderId = 1;
  2966. while (true)
  2967. {
  2968. hr = lpRootFolder->CopyFolder(lpPropValue->Value.bin.cb, (LPENTRYID)lpPropValue->Value.bin.lpb,
  2969. NULL, lpDeletedStoresFolder, (LPTSTR)strStorenameTMP.c_str(), 0, NULL, COPY_SUBFOLDERS | MAPI_UNICODE);
  2970. if (hr == MAPI_E_COLLISION) {
  2971. if (nFolderId >= 1000) {
  2972. cerr << "Unable to copy the store to the public, maximum folder collisions exceeded" << endl;
  2973. goto exit;
  2974. }
  2975. strStorenameTMP = strStorename + std::to_wstring(nFolderId);
  2976. ++nFolderId;
  2977. cerr << "Folder already exist, retrying with foldername '" << convert_to<string>(strStorenameTMP) << "'" << endl;
  2978. } else if (FAILED(hr)) {
  2979. cerr << "Unable to copy the store to the public," << getMapiCodeString(hr) << endl;
  2980. goto exit;
  2981. } else if (hr != hrSuccess) {
  2982. cerr << "Warning, the copy succeeded, but not all entries were copied (" << getMapiCodeString(hr) << ")" << endl;
  2983. break;
  2984. } else {
  2985. cerr << "Copy succeeded" << endl;
  2986. break;
  2987. }
  2988. }
  2989. } else {
  2990. ULONG ulStoreType;
  2991. if (detailstype == NULL)
  2992. detailstype = "user";
  2993. if (strcmp(detailstype, "user") == 0) {
  2994. ulStoreType = ECSTORE_TYPE_PRIVATE;
  2995. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  2996. } else if (strcmp(detailstype, "archive") == 0) {
  2997. ulStoreType = ECSTORE_TYPE_ARCHIVE;
  2998. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  2999. } else if (strcmp(detailstype, "group") == 0) {
  3000. ulStoreType = ECSTORE_TYPE_PUBLIC;
  3001. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3002. } else if (strcmp(detailstype, "company") == 0) {
  3003. ulStoreType = ECSTORE_TYPE_PUBLIC;
  3004. hr = lpServiceAdmin->ResolveCompanyName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3005. } else {
  3006. cerr << "Unknown store type: '" << detailstype << "'." << endl;
  3007. goto exit;
  3008. }
  3009. if (hr != hrSuccess) {
  3010. cerr << "Unable to find " << detailstype << ", " << getMapiCodeString(hr, username) << endl;
  3011. goto exit;
  3012. }
  3013. if (strcmp(detailstype, "user") == 0) {
  3014. // check if this user should exist on the connected server. depending on --force, print a warning or an error
  3015. hr = lpServiceAdmin->GetUser(cbUserId, lpUserId, 0, &~lpECUser);
  3016. if (hr != hrSuccess) {
  3017. cerr << "Unable to load details, " << getMapiCodeString(hr, username) << endl;
  3018. goto exit;
  3019. }
  3020. // homeserver on single server installations is empty
  3021. if (lpECUser->lpszServername != NULL && *reinterpret_cast<LPSTR>(lpECUser->lpszServername) != '\0') {
  3022. // note, this has to be mapi allocated because GetServerDetails does a More allocation on this base pointer
  3023. if (MAPIAllocateBuffer(sizeof(ECSVRNAMELIST), &~lpsServer) != hrSuccess ||
  3024. MAPIAllocateMore(sizeof(LPTSTR), lpsServer, (void**)&lpsServer->lpszaServer) != hrSuccess) {
  3025. hr = MAPI_E_NOT_ENOUGH_MEMORY;
  3026. cerr << "Unable to allocate memory for server details" << endl;
  3027. goto exit;
  3028. }
  3029. lpsServer->cServers = 1;
  3030. lpsServer->lpszaServer[0] = lpECUser->lpszServername;
  3031. hr = lpServiceAdmin->GetServerDetails(lpsServer, 0, &~lpServerDetails);
  3032. if (hr != hrSuccess) {
  3033. cerr << "Unable to load server details, " << getMapiCodeString(hr, (char*)lpECUser->lpszServername) << endl;
  3034. goto exit;
  3035. }
  3036. if ((lpServerDetails->lpsaServer[0].ulFlags & EC_SDFLAG_IS_PEER) == 0)
  3037. // since we don't know which server we're connected to, don't print a server name.
  3038. cerr << "WARNING: Hooking store on non-homeserver of " << username << endl;
  3039. }
  3040. }
  3041. // the server won't let you hook public stores to users and vice-versa.
  3042. hr = lpServiceAdmin->HookStore(ulStoreType, cbUserId, lpUserId, lpGUID);
  3043. if (hr != hrSuccess) {
  3044. cerr << "Unable to hook store, " << getMapiCodeString(hr) << endl;
  3045. goto exit;
  3046. }
  3047. cout << "Store hooked." << endl;
  3048. }
  3049. break;
  3050. case MODE_UNHOOK_STORE: {
  3051. ULONG ulStoreType;
  3052. if (detailstype == NULL)
  3053. detailstype = "user";
  3054. if (strcmp(detailstype, "user") == 0) {
  3055. ulStoreType = ECSTORE_TYPE_PRIVATE;
  3056. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3057. } else if (strcmp(detailstype, "archive") == 0) {
  3058. ulStoreType = ECSTORE_TYPE_ARCHIVE;
  3059. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3060. } else if (strcmp(detailstype, "group") == 0) {
  3061. ulStoreType = ECSTORE_TYPE_PUBLIC;
  3062. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3063. } else if (strcmp(detailstype, "company") == 0) {
  3064. ulStoreType = ECSTORE_TYPE_PUBLIC;
  3065. strCompanyName = convert_to<wstring>(username);
  3066. hr = lpServiceAdmin->ResolveCompanyName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3067. } else {
  3068. cerr << "Unknown store type: '" << detailstype << "'." << endl;
  3069. goto exit;
  3070. }
  3071. if (hr != hrSuccess) {
  3072. cerr << "Unable to find " << detailstype << ", " << getMapiCodeString(hr, username) << endl;
  3073. goto exit;
  3074. }
  3075. if (ulStoreType != ECSTORE_TYPE_PUBLIC) {
  3076. if (ulStoreType == ECSTORE_TYPE_ARCHIVE) {
  3077. hr = lpServiceAdmin->GetArchiveStoreEntryID(reinterpret_cast<LPTSTR>(username), NULL, 0, &cbStoreId, &~lpStoreId);
  3078. if (hr != hrSuccess) {
  3079. cout << "Unable to unhook store, unable to retrieve store entryid, " << getMapiCodeString(hr, "entryid") << endl;
  3080. goto exit;
  3081. }
  3082. } else {
  3083. hr = lpMsgStore->QueryInterface(IID_IExchangeManageStore, &~lpIEMS);
  3084. if (hr != hrSuccess)
  3085. goto exit;
  3086. // do not redirect to another server, unhook works on the server it's connected to
  3087. hr = lpIEMS->CreateStoreEntryID(NULL, reinterpret_cast<LPTSTR>(username), OPENSTORE_OVERRIDE_HOME_MDB, &cbStoreId, &~lpStoreId);
  3088. if (hr != hrSuccess) {
  3089. if (hr == MAPI_E_NOT_FOUND)
  3090. cout << "Unable to unhook store. User '" << username << "' has no store attached." << endl;
  3091. else
  3092. cout << "Unable to unhook store. Can not create store entryid, " << getMapiCodeString(hr, "store") << endl;
  3093. goto exit;
  3094. }
  3095. }
  3096. hr = UnWrapStoreEntryID(cbStoreId, lpStoreId, &cbUnWrappedEntry, &~lpUnWrappedEntry);
  3097. if (hr != hrSuccess) {
  3098. cout << "Unable to unhook store. Unable to unwrap the store entryid, " << getMapiCodeString(hr, "entryid") << endl;
  3099. goto exit;
  3100. }
  3101. } else {
  3102. // ns__resolveUserStore (CreateStoreEntryID) does not work with normal (non-company) public store
  3103. hr = GetPublicStore(lpSession, lpMsgStore, strCompanyName, &~lpPublicStore);
  3104. if (hr != hrSuccess) {
  3105. cerr << "Unable to open public store, " << getMapiCodeString(hr, "public") << endl;
  3106. goto exit;
  3107. }
  3108. hr = HrGetOneProp(lpPublicStore, PR_STORE_ENTRYID, &~lpPropValue);
  3109. if (hr != hrSuccess) {
  3110. cerr << "Unable to get public store entryid, " << getMapiCodeString(hr, "store") << endl;
  3111. goto exit;
  3112. }
  3113. hr = UnWrapStoreEntryID(lpPropValue->Value.bin.cb, reinterpret_cast<ENTRYID *>(lpPropValue->Value.bin.lpb), &cbUnWrappedEntry, &~lpUnWrappedEntry);
  3114. if (hr != hrSuccess) {
  3115. cout << "Unable to unhook store. Unable to unwrap the store entryid, " << getMapiCodeString(hr, "entryid") << endl;
  3116. goto exit;
  3117. }
  3118. }
  3119. hr = lpServiceAdmin->UnhookStore(ulStoreType, cbUserId, lpUserId);
  3120. if (hr != hrSuccess) {
  3121. cerr << "Unable to unhook store, " << getMapiCodeString(hr) << endl;
  3122. goto exit;
  3123. }
  3124. cout << "Store unhooked. Store guid is " << bin2hex(sizeof(GUID), (unsigned char*)lpUnWrappedEntry->ab) << endl;
  3125. break;
  3126. }
  3127. case MODE_REMOVE_STORE:
  3128. hr = Util::hex2bin(storeguid, sizeof(GUID)*2, &cbGUID, (&~lpGUID).as<unsigned char>());
  3129. if (hr != hrSuccess) {
  3130. cerr << "Incorrect store guid '" << storeguid << "'" << endl;
  3131. goto exit;
  3132. }
  3133. hr = lpServiceAdmin->RemoveStore(lpGUID);
  3134. if (hr != hrSuccess) {
  3135. cerr << "Unable to remove store, " << getMapiCodeString(hr) << endl;
  3136. goto exit;
  3137. }
  3138. cout << "Store removed." << endl;
  3139. break;
  3140. case MODE_UPDATE_USER:
  3141. if (new_username) {
  3142. memory_ptr<ENTRYID> userid;
  3143. ULONG usize;
  3144. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(new_username), 0, &usize, &~userid);
  3145. if (hr == hrSuccess) {
  3146. cerr << "User with name '" << new_username << "' is already present." << endl;
  3147. hr = MAPI_E_COLLISION;
  3148. goto exit;
  3149. }
  3150. }
  3151. // get old features. we need these, because not setting them would mean: remove
  3152. hr = lpServiceAdmin->GetUser(cbUserId, lpUserId, 0, &~lpECUser);
  3153. if (hr != hrSuccess) {
  3154. cerr << "Unable to get user details, " << getMapiCodeString(hr, username) << endl;
  3155. goto exit;
  3156. }
  3157. // lpECUser memory will be kept alive to let the SetUser() call work
  3158. for (ULONG i = 0; i < lpECUser->sMVPropmap.cEntries; ++i) {
  3159. if (lpECUser->sMVPropmap.lpEntries[i].ulPropId == PR_EC_ENABLED_FEATURES_A)
  3160. sEnabled.insert((char**)lpECUser->sMVPropmap.lpEntries[i].lpszValues,
  3161. (char**)lpECUser->sMVPropmap.lpEntries[i].lpszValues + lpECUser->sMVPropmap.lpEntries[i].cValues);
  3162. else if (lpECUser->sMVPropmap.lpEntries[i].ulPropId == PR_EC_DISABLED_FEATURES_A)
  3163. sDisabled.insert((char**)lpECUser->sMVPropmap.lpEntries[i].lpszValues,
  3164. (char**)lpECUser->sMVPropmap.lpEntries[i].lpszValues + lpECUser->sMVPropmap.lpEntries[i].cValues);
  3165. }
  3166. if (feature) {
  3167. if (bFeature) {
  3168. sEnabled.insert(feature);
  3169. sDisabled.erase(feature);
  3170. } else {
  3171. sEnabled.erase(feature);
  3172. sDisabled.insert(feature);
  3173. }
  3174. }
  3175. if (new_username || password || passprompt || emailadr || fullname || isadmin != -1 || isnonactive != -1 || feature) {
  3176. memset(&sECUser, 0, sizeof(sECUser));
  3177. // if the user did not select an active/inactive state on the command-line,
  3178. // then check what the status was before admin started; that status
  3179. // will then be re-used
  3180. if (isnonactive == -1)
  3181. isnonactive = lpECUser -> ulObjClass == NONACTIVE_USER;
  3182. // copy static info
  3183. sECUser.sUserId.cb = cbUserId;
  3184. sECUser.sUserId.lpb = reinterpret_cast<unsigned char *>(lpUserId.get());
  3185. // possibly set new values
  3186. sECUser.lpszUsername = (LPTSTR)(new_username ? new_username : username);
  3187. if (passprompt) {
  3188. sECUser.lpszPassword = (LPTSTR)get_password();
  3189. if (sECUser.lpszPassword == NULL)
  3190. {
  3191. cerr << "Passwords don't match" << endl;
  3192. return 1;
  3193. }
  3194. if (sECUser.lpszPassword[0] == 0)
  3195. {
  3196. cerr << "Password cannot be empty" << endl;
  3197. return 1;
  3198. }
  3199. } else
  3200. sECUser.lpszPassword = (LPTSTR)password;
  3201. sECUser.lpszMailAddress = (LPTSTR)emailadr;
  3202. sECUser.lpszFullName = (LPTSTR)fullname;
  3203. sECUser.ulIsAdmin = isadmin;
  3204. sECUser.ulObjClass = ACTIVE_USER;
  3205. if (isnonactive == 1)
  3206. sECUser.ulObjClass = NONACTIVE_USER;
  3207. // sEnabled to sECUser.sMVPropmap ergens
  3208. sECUser.sMVPropmap.cEntries = 2; // @note: if we have more mv props than the feature lists, adjust this value!
  3209. // mapi allocate more on lpECUser, so this will be freed automatically at exit.
  3210. hr = MAPIAllocateMore(sizeof(MVPROPMAPENTRY) * sECUser.sMVPropmap.cEntries, lpECUser, (void**)&sECUser.sMVPropmap.lpEntries);
  3211. if (hr != hrSuccess) {
  3212. cerr << "Memory error" << endl;
  3213. goto exit;
  3214. }
  3215. if (fillMVPropmap(sECUser, PR_EC_ENABLED_FEATURES_A, 0, sEnabled, lpECUser) != hrSuccess ||
  3216. fillMVPropmap(sECUser, PR_EC_DISABLED_FEATURES_A, 1, sDisabled, lpECUser) != hrSuccess)
  3217. goto exit;
  3218. hr = lpServiceAdmin->SetUser(&sECUser, 0);
  3219. if (hr != hrSuccess) {
  3220. cerr << "Unable to update user information, " << getMapiCodeString(hr) << endl;
  3221. goto exit;
  3222. }
  3223. }
  3224. // update quota data
  3225. if (quota != -1 || quotahard != -1 || quotasoft != -1 || quotawarn != -1) {
  3226. hr = setQuota(lpServiceAdmin, cbUserId, lpUserId, quota, false, quotawarn, quotasoft, quotahard, true);
  3227. if (hr != hrSuccess)
  3228. goto exit;
  3229. }
  3230. if (mr_accept != -1 || mr_decline_conflict != -1 || mr_decline_recurring != -1)
  3231. {
  3232. hr = lpECMsgStore->QueryInterface(IID_IExchangeManageStore, &~lpIEMS);
  3233. if (hr != hrSuccess) {
  3234. cerr << "Unable to get admin interface." << endl;
  3235. goto exit;
  3236. }
  3237. hr = lpIEMS->CreateStoreEntryID((LPTSTR)"", (LPTSTR)username, 0, &cbEntryID, &~lpEntryID);
  3238. if (hr != hrSuccess) {
  3239. cerr << "Unable to get user store entry id. User has possibly has not store." << endl;
  3240. goto exit;
  3241. }
  3242. hr = lpSession->OpenMsgStore(0, cbEntryID, lpEntryID, &IID_IMsgStore, MDB_WRITE, &~lpUserStore);
  3243. if (hr != hrSuccess) {
  3244. cerr << "Unable to open user store." << endl;
  3245. goto exit;
  3246. }
  3247. hr = GetAutoAcceptSettings(lpUserStore, &bAutoAccept, &bDeclineConflict, &bDeclineRecurring);
  3248. if (hr != hrSuccess)
  3249. // ignore and assume 'false' for all values
  3250. hr = hrSuccess;
  3251. if (mr_accept != -1)
  3252. bAutoAccept = mr_accept;
  3253. if (mr_decline_conflict != -1)
  3254. bDeclineConflict = mr_decline_conflict;
  3255. if (mr_decline_recurring != -1)
  3256. bDeclineRecurring = mr_decline_recurring;
  3257. hr = SetAutoAcceptSettings(lpUserStore, bAutoAccept, bDeclineConflict, bDeclineRecurring);
  3258. if (hr != hrSuccess) {
  3259. cerr << "Unable to set auto-accept settings." << endl;
  3260. goto exit;
  3261. }
  3262. }
  3263. if (sendas_user) {
  3264. hr = lpServiceAdmin->ResolveUserName((LPTSTR)sendas_user, 0, &cbSenderId, &~lpSenderId);
  3265. if (hr != hrSuccess) {
  3266. cerr << "Unable to update user, sendas user not available, " << getMapiCodeString(hr, sendas_user) << endl;
  3267. goto exit;
  3268. }
  3269. if (sendas_action == 0)
  3270. // delete sendas user
  3271. hr = lpServiceAdmin->DelSendAsUser(cbUserId, lpUserId, cbSenderId, lpSenderId);
  3272. else if (sendas_action == 1)
  3273. // add sendas user
  3274. hr = lpServiceAdmin->AddSendAsUser(cbUserId, lpUserId, cbSenderId, lpSenderId);
  3275. switch (hr) {
  3276. case MAPI_E_NOT_FOUND:
  3277. // on del, not in list
  3278. if (sendas_action == 0)
  3279. cerr << "Unable to update user, sender " << sendas_user << " not found in sendas list" << endl;
  3280. else
  3281. cerr << "Unable to update user, sender " << sendas_user << " not allowed in sendas list" << endl;
  3282. goto exit;
  3283. case MAPI_E_COLLISION:
  3284. // on add, already added ... too bad an insert collision does not return this error :|
  3285. cerr << "Unable to update user, sender " << sendas_user << " already in sendas list" << endl;
  3286. goto exit;
  3287. default:
  3288. if (hr != hrSuccess) {
  3289. cerr << "Unable to update user, unable to update sendas list, " << getMapiCodeString(hr, username) << endl;
  3290. goto exit;
  3291. }
  3292. break;
  3293. };
  3294. }
  3295. cout << "User information updated." << endl;
  3296. break;
  3297. case MODE_CREATE_COMPANY:
  3298. memset(&sECCompany, 0, sizeof(sECCompany));
  3299. sECCompany.lpszCompanyname = (LPTSTR)companyname;
  3300. hr = lpServiceAdmin->CreateCompany(&sECCompany, 0, &cbCompanyId, &~lpCompanyId);
  3301. if (hr != hrSuccess) {
  3302. cerr << "Unable to create company, " << getMapiCodeString(hr, companyname) << endl;
  3303. goto exit;
  3304. }
  3305. if (quota != -1) {
  3306. // this is the company public quota, and only contains a warning, nothing more.
  3307. hr = setQuota(lpServiceAdmin, cbCompanyId, lpCompanyId, quota, false, quotawarn, 0, 0, false, true);
  3308. if(hr != hrSuccess)
  3309. goto exit;
  3310. }
  3311. if (ud_quota != -1) {
  3312. hr = setQuota(lpServiceAdmin, cbCompanyId, lpCompanyId, ud_quota, true, ud_quotawarn, ud_quotasoft, ud_quotahard);
  3313. if (hr != hrSuccess)
  3314. goto exit;
  3315. }
  3316. cout << "Company created" << endl;
  3317. break;
  3318. case MODE_UPDATE_COMPANY:
  3319. if (quota != -1 || quotahard != -1 || quotasoft != -1 || quotawarn != -1) {
  3320. hr = setQuota(lpServiceAdmin, cbCompanyId, lpCompanyId, quota, false, quotawarn, quotasoft, quotahard, true, true);
  3321. if (hr != hrSuccess)
  3322. goto exit;
  3323. }
  3324. if (ud_quota != -1 || ud_quotahard != -1 || ud_quotasoft != -1 || ud_quotawarn != -1) {
  3325. hr = setQuota(lpServiceAdmin, cbCompanyId, lpCompanyId, ud_quota, true, ud_quotawarn, ud_quotasoft, ud_quotahard);
  3326. if (hr != hrSuccess)
  3327. goto exit;
  3328. }
  3329. break;
  3330. case MODE_DELETE_COMPANY:
  3331. hr = lpServiceAdmin->DeleteCompany(cbCompanyId, lpCompanyId);
  3332. if (hr != hrSuccess) {
  3333. cerr << "Unable to delete company, " << getMapiCodeString(hr, companyname) << endl;
  3334. goto exit;
  3335. }
  3336. cout << "Company deleted" << endl;
  3337. break;
  3338. case MODE_LIST_COMPANY:
  3339. hr = lpServiceAdmin->GetCompanyList(0, &cCompanies, &lpECCompanies);
  3340. if(hr != hrSuccess) {
  3341. cerr << "Unable to list companies, " << getMapiCodeString(hr) << endl;
  3342. goto exit;
  3343. }
  3344. cout << "Company list ("<< cCompanies <<"):" << endl;
  3345. ct.Resize(cCompanies, 2);
  3346. ct.SetHeader(0, "Companyname");
  3347. ct.SetHeader(1, "System administrator");
  3348. for (unsigned int i = 0; i < cCompanies; ++i) {
  3349. ct.AddColumn(0, (LPSTR)lpECCompanies[i].lpszCompanyname);
  3350. hr = lpServiceAdmin->GetUser(lpECCompanies[i].sAdministrator.cb, reinterpret_cast<ENTRYID *>(lpECCompanies[i].sAdministrator.lpb), 0, &~lpECUser);
  3351. if (hr != hrSuccess) {
  3352. cerr << "Unable to get administrator details, " << getMapiCodeString(hr, "administrator") << endl;
  3353. goto exit;
  3354. }
  3355. ct.AddColumn(1, (LPSTR)lpECUser->lpszUsername);
  3356. }
  3357. ct.PrintTable();
  3358. break;
  3359. case MODE_CREATE_GROUP:
  3360. memset(&sECGroup, 0, sizeof(sECGroup));
  3361. sECGroup.lpszGroupname = (LPTSTR)groupname;
  3362. sECGroup.lpszFullname = (LPTSTR)groupname;
  3363. sECGroup.lpszFullEmail = (LPTSTR)emailadr;
  3364. hr = lpServiceAdmin->CreateGroup(&sECGroup, 0, &cbGroupId, &~lpGroupId);
  3365. if (hr != hrSuccess) {
  3366. cerr << "Unable to create group, " << getMapiCodeString(hr, groupname) << endl;
  3367. goto exit;
  3368. }
  3369. cout << "Group created." << endl;
  3370. break;
  3371. case MODE_UPDATE_GROUP:
  3372. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(groupname), 0, &cbGroupId, &~lpGroupId);
  3373. if (hr != hrSuccess) {
  3374. cerr << "Unable to update group, " << getMapiCodeString(hr, groupname) << endl;
  3375. goto exit;
  3376. }
  3377. if (emailadr) {
  3378. memset(&sECGroup, 0, sizeof(sECGroup));
  3379. // copy static info
  3380. sECGroup.sGroupId.cb = cbGroupId;
  3381. sECGroup.sGroupId.lpb = reinterpret_cast<unsigned char *>(lpGroupId.get());
  3382. // possibly set new values
  3383. sECGroup.lpszFullEmail = (LPTSTR)emailadr;
  3384. sECGroup.lpszGroupname = (LPTSTR)groupname;
  3385. sECGroup.lpszFullname = (LPTSTR)groupname;
  3386. hr = lpServiceAdmin->SetGroup(&sECGroup, 0);
  3387. if (hr != hrSuccess) {
  3388. cerr << "Unable to update group information, " << getMapiCodeString(hr) << endl;
  3389. goto exit;
  3390. }
  3391. }
  3392. if (sendas_user) {
  3393. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(sendas_user), 0, &cbSenderId, &~lpSenderId);
  3394. if (hr != hrSuccess) {
  3395. cerr << "Unable to update group, sendas user not available, " << getMapiCodeString(hr, sendas_user) << endl;
  3396. goto exit;
  3397. }
  3398. if (sendas_action == 0)
  3399. // delete sendas user
  3400. hr = lpServiceAdmin->DelSendAsUser(cbGroupId, lpGroupId, cbSenderId, lpSenderId);
  3401. else if (sendas_action == 1)
  3402. // add sendas user
  3403. hr = lpServiceAdmin->AddSendAsUser(cbGroupId, lpGroupId, cbSenderId, lpSenderId);
  3404. switch (hr) {
  3405. case MAPI_E_NOT_FOUND:
  3406. // on del, not in list
  3407. if (sendas_action == 0)
  3408. cerr << "Unable to update group, sender " << sendas_user << " not found in sendas list" << endl;
  3409. else
  3410. cerr << "Unable to update group, sender " << sendas_user << " not allowed in sendas list" << endl;
  3411. goto exit;
  3412. case MAPI_E_COLLISION:
  3413. // on add, already added ... too bad an insert collision does not return this error :|
  3414. cerr << "Unable to update group, sender " << sendas_user << " already in sendas list" << endl;
  3415. goto exit;
  3416. default:
  3417. if (hr != hrSuccess) {
  3418. cerr << "Unable to update group, unable to update sendas list, " << getMapiCodeString(hr, username) << endl;
  3419. goto exit;
  3420. }
  3421. break;
  3422. };
  3423. }
  3424. cout << "Group information updated." << endl;
  3425. break;
  3426. case MODE_DELETE_GROUP:
  3427. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(groupname), 0, &cbGroupId, &~lpGroupId);
  3428. if (hr != hrSuccess) {
  3429. cerr << "Unable to delete group, " << getMapiCodeString(hr, groupname) << endl;
  3430. goto exit;
  3431. }
  3432. hr = lpServiceAdmin->DeleteGroup(cbGroupId, lpGroupId);
  3433. if (hr != hrSuccess) {
  3434. cerr << "Unable to delete group." << endl;
  3435. goto exit;
  3436. }
  3437. cout << "Group deleted." << endl;
  3438. break;
  3439. case MODE_LIST_GROUP:
  3440. hr = ForEachCompany(lpServiceAdmin, companyname, ListGroups);
  3441. if (hr != hrSuccess)
  3442. goto exit;
  3443. break;
  3444. case MODE_ADDUSER_GROUP:
  3445. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(groupname), 0, &cbGroupId, &~lpGroupId);
  3446. if (hr != hrSuccess) {
  3447. cerr << "Unable to add user to group, " << getMapiCodeString(hr, groupname) << endl;
  3448. goto exit;
  3449. }
  3450. hr = lpServiceAdmin->AddGroupUser(cbGroupId, lpGroupId, cbUserId, lpUserId);
  3451. if (hr != hrSuccess) {
  3452. cerr << "Unable to add user to group." << endl;
  3453. goto exit;
  3454. }
  3455. cout << "User added to group." << endl;
  3456. break;
  3457. case MODE_DELETEUSER_GROUP:
  3458. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(groupname), 0, &cbGroupId, &~lpGroupId);
  3459. if (hr != hrSuccess) {
  3460. cerr << "Unable to remove user from group, " << getMapiCodeString(hr, groupname) << endl;
  3461. goto exit;
  3462. }
  3463. hr = lpServiceAdmin->DeleteGroupUser(cbGroupId, lpGroupId, cbUserId, lpUserId);
  3464. if (hr != hrSuccess) {
  3465. cerr << "Unable to remove user from group." << endl;
  3466. goto exit;
  3467. }
  3468. cout << "User removed from group." << endl;
  3469. break;
  3470. case MODE_SYNC_USERS:
  3471. hr = SyncUsers(lpServiceAdmin);
  3472. if (hr != hrSuccess)
  3473. goto exit;
  3474. cout << "Users and groups synchronized." << endl;
  3475. break;
  3476. case MODE_ADD_VIEW:
  3477. hr = lpServiceAdmin->ResolveCompanyName(reinterpret_cast<LPTSTR>(set_companyname), 0, &cbSetCompanyId, &~lpSetCompanyId);
  3478. if (hr != hrSuccess) {
  3479. cerr << "Failed to resolve company name, " << getMapiCodeString(hr, set_companyname) << endl;
  3480. goto exit;
  3481. }
  3482. hr = lpServiceAdmin->AddCompanyToRemoteViewList(cbSetCompanyId, lpSetCompanyId, cbCompanyId, lpCompanyId);
  3483. if (hr != hrSuccess) {
  3484. cerr << "Failed to add company to remote-view list" << endl;
  3485. goto exit;
  3486. }
  3487. cout << "Company " << set_companyname << " added to the remote-view list of " << companyname << endl;
  3488. break;
  3489. case MODE_DEL_VIEW:
  3490. hr = lpServiceAdmin->ResolveCompanyName(reinterpret_cast<LPTSTR>(set_companyname), 0, &cbSetCompanyId, &~lpSetCompanyId);
  3491. if (hr != hrSuccess) {
  3492. cerr << "Failed to resolve company name, " << getMapiCodeString(hr, set_companyname) << endl;
  3493. goto exit;
  3494. }
  3495. hr = lpServiceAdmin->DelCompanyFromRemoteViewList(cbSetCompanyId, lpSetCompanyId, cbCompanyId, lpCompanyId);
  3496. if (hr != hrSuccess) {
  3497. cerr << "Failed to remove company from remote-view list, " << getMapiCodeString(hr) << endl;
  3498. goto exit;
  3499. }
  3500. cout << "Company " << set_companyname << " removed from the remote-view list of " << companyname << endl;
  3501. break;
  3502. case MODE_LIST_VIEW:
  3503. hr = lpServiceAdmin->GetRemoteViewList(cbCompanyId, lpCompanyId, 0, &cCompanies, &lpECCompanies);
  3504. if (hr != hrSuccess) {
  3505. cerr << "Unable to display remote-view list, " << getMapiCodeString(hr) << endl;
  3506. goto exit;
  3507. }
  3508. cout << "remote-view list ("<< cCompanies <<"):" << endl;
  3509. cout << "\t" << "companyname" << "" << endl;
  3510. cout << "\t-------------------------------------" << endl;
  3511. print_companies(cCompanies, lpECCompanies, true);
  3512. break;
  3513. case MODE_ADD_ADMIN:
  3514. hr = lpServiceAdmin->AddUserToRemoteAdminList(cbUserId, lpUserId, cbCompanyId, lpCompanyId);
  3515. if (hr != hrSuccess) {
  3516. cerr << "Failed to add user to remote-admin list, " << getMapiCodeString(hr) << endl;
  3517. goto exit;
  3518. }
  3519. cout << "User " << username << " added to the remote-admin list of " << companyname << endl;
  3520. break;
  3521. case MODE_DEL_ADMIN:
  3522. hr = lpServiceAdmin->DelUserFromRemoteAdminList(cbUserId, lpUserId, cbCompanyId, lpCompanyId);
  3523. if (hr != hrSuccess) {
  3524. cerr << "Failed to delete user from remote-admin list, " << getMapiCodeString(hr) << endl;
  3525. goto exit;
  3526. }
  3527. cout << "User " << username << " removed from the remote-admin list of " << companyname << endl;
  3528. break;
  3529. case MODE_LIST_ADMIN:
  3530. hr = lpServiceAdmin->GetRemoteAdminList(cbCompanyId, lpCompanyId, 0, &cUsers, &~lpECUser);
  3531. if (hr != hrSuccess) {
  3532. cerr << "Unable to display remote-admin list, " << getMapiCodeString(hr) << endl;
  3533. goto exit;
  3534. }
  3535. cout << "remote-admin list ("<< cUsers <<"):" << endl;
  3536. print_users(cUsers, lpECUser);
  3537. break;
  3538. case MODE_SYSTEM_ADMIN:
  3539. memset(&sECCompany, 0, sizeof(sECCompany));
  3540. sECCompany.sAdministrator.cb = cbUserId;
  3541. sECCompany.sAdministrator.lpb = reinterpret_cast<unsigned char *>(lpUserId.get());
  3542. sECCompany.lpszCompanyname = (LPTSTR)companyname;
  3543. sECCompany.sCompanyId.cb = cbCompanyId;
  3544. sECCompany.sCompanyId.lpb = reinterpret_cast<unsigned char *>(lpCompanyId.get());
  3545. hr = lpServiceAdmin->SetCompany(&sECCompany, 0);
  3546. if (hr != hrSuccess) {
  3547. cerr << "Failed to set company system administrator" << endl;
  3548. goto exit;
  3549. }
  3550. cout << "User " << username << " is set as admin of company " << companyname << endl;
  3551. break;
  3552. case MODE_ADD_USERQUOTA_RECIPIENT:
  3553. hr = lpServiceAdmin->AddQuotaRecipient(cbCompanyId, lpCompanyId, cbUserId, lpUserId, ACTIVE_USER);
  3554. if (hr != hrSuccess) {
  3555. cerr << "Failed to add recipient to quota list." << endl;
  3556. goto exit;
  3557. }
  3558. cout << "User " << username << " added to user quota recipients list for company " << companyname << endl;
  3559. break;
  3560. case MODE_DEL_USERQUOTA_RECIPIENT:
  3561. hr = lpServiceAdmin->DeleteQuotaRecipient(cbCompanyId, lpCompanyId, cbUserId, lpUserId, ACTIVE_USER);
  3562. if (hr != hrSuccess) {
  3563. cerr << "Failed to remove company from quota list." << endl;
  3564. goto exit;
  3565. }
  3566. cout << "User " << username << " removed from user quota recipients list for company " << companyname << endl;
  3567. break;
  3568. case MODE_LIST_USERQUOTA_RECIPIENT:
  3569. /* HACK: request a user from the specified company, and request the recipients for that user. */
  3570. hr = lpServiceAdmin->GetUserList(cbCompanyId, lpCompanyId, 0, &cUsers, &~lpECUser);
  3571. if (hr != hrSuccess || cUsers <= 1) /* First user is always SYSTEM */ {
  3572. cerr << "Failed to get quota recipient list" << endl;
  3573. goto exit;
  3574. }
  3575. cbUserId = lpECUser[1].sUserId.cb;
  3576. hr = MAPIAllocateBuffer(cbUserId, &~lpUserId);
  3577. if (hr != hrSuccess)
  3578. goto exit;
  3579. memcpy(lpUserId, lpECUser[1].sUserId.lpb, cbUserId);
  3580. hr = lpServiceAdmin->GetQuotaRecipients(cbUserId, lpUserId, 0, &cUsers, &~lpECUser);
  3581. if (hr != hrSuccess) {
  3582. cerr << "Failed to get quota recipient list" << endl;
  3583. goto exit;
  3584. }
  3585. cout << "Recipient list ("<< cUsers-1 <<"):" << endl;
  3586. /* Skip the dummy entry we used to obtain the list */
  3587. print_users(cUsers - 1, &lpECUser[1]);
  3588. break;
  3589. case MODE_ADD_COMPANYQUOTA_RECIPIENT:
  3590. hr = lpServiceAdmin->AddQuotaRecipient(cbCompanyId, lpCompanyId, cbUserId, lpUserId, CONTAINER_COMPANY);
  3591. if (hr != hrSuccess) {
  3592. cerr << "Failed to add recipient to quota list." << endl;
  3593. goto exit;
  3594. }
  3595. cout << "User " << username << " added to company quota recipients list for company " << companyname << endl;
  3596. break;
  3597. case MODE_DEL_COMPANYQUOTA_RECIPIENT:
  3598. hr = lpServiceAdmin->DeleteQuotaRecipient(cbCompanyId, lpCompanyId, cbUserId, lpUserId, CONTAINER_COMPANY);
  3599. if (hr != hrSuccess) {
  3600. cerr << "Failed to delete recipient to quota list." << endl;
  3601. goto exit;
  3602. }
  3603. cout << "User " << username << " removed from company quota recipients list for company " << companyname << endl;
  3604. break;
  3605. case MODE_LIST_COMPANYQUOTA_RECIPIENT:
  3606. hr = lpServiceAdmin->GetQuotaRecipients(cbCompanyId, lpCompanyId, 0, &cUsers, &~lpECUser);
  3607. if (hr != hrSuccess) {
  3608. cerr << "Failed to get quota recipient list." << endl;
  3609. goto exit;
  3610. }
  3611. cout << "Recipient list ("<< cUsers-1 <<"):" << endl;
  3612. /* Skipt first entry, that is the company itself which will not get the mail */
  3613. print_users(cUsers - 1, &lpECUser[1]);
  3614. break;
  3615. case MODE_LIST_SENDAS:
  3616. if (detailstype == NULL || strcasecmp(detailstype, "user") == 0) {
  3617. hr = lpServiceAdmin->ResolveUserName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3618. detailstype = "user";
  3619. } else if (strcasecmp(detailstype, "group") == 0) {
  3620. hr = lpServiceAdmin->ResolveGroupName(reinterpret_cast<LPTSTR>(username), 0, &cbUserId, &~lpUserId);
  3621. } else {
  3622. hr = MAPI_E_INVALID_TYPE;
  3623. cerr << "Unknown object type \"" << detailstype << "\"" << endl;
  3624. goto exit;
  3625. }
  3626. if (hr != hrSuccess) {
  3627. cerr << "Failed to resolve "<< detailstype <<" name, " << getMapiCodeString(hr, username) << endl;
  3628. goto exit;
  3629. }
  3630. hr = lpServiceAdmin->GetSendAsList(cbUserId, lpUserId, 0, &cSenders, &~lpSenders);
  3631. if (hr != hrSuccess) {
  3632. cerr << "Failed to retrieve send-as list for " << detailstype << " " << username << endl;
  3633. goto exit;
  3634. }
  3635. cout << "Send-as list ("<< cSenders <<") for " << detailstype << " " << username << ":" << endl;
  3636. print_users(cSenders, lpSenders);
  3637. break;
  3638. case MODE_PURGE_SOFTDELETE:
  3639. hr = lpServiceAdmin->PurgeSoftDelete(ulDays);
  3640. if (hr != hrSuccess) {
  3641. cerr << "Softdelete purge failed" << endl;
  3642. goto exit;
  3643. }
  3644. cout << "Softdelete purge done." << endl;
  3645. break;
  3646. case MODE_CLEAR_CACHE:
  3647. hr = lpServiceAdmin->PurgeCache(ulCachePurgeMode);
  3648. if (hr != hrSuccess) {
  3649. cerr << "Cache clear failed" << endl;
  3650. goto exit;
  3651. }
  3652. if (ulCachePurgeMode != PURGE_CACHE_ALL)
  3653. cout << "Cache cleared with flags " << ulCachePurgeMode << endl;
  3654. else
  3655. cout << "Cache cleared." << endl;
  3656. break;
  3657. case MODE_PURGE_DEFERRED:
  3658. while(1) {
  3659. ULONG ulRemaining;
  3660. hr = lpServiceAdmin->PurgeDeferredUpdates(&ulRemaining);
  3661. if(hr == MAPI_E_NOT_FOUND)
  3662. break;
  3663. if(hr != hrSuccess) {
  3664. cerr << "Purge failed." << endl;
  3665. break;
  3666. }
  3667. cerr << "Remaining deferred records: " << ulRemaining << " \r";
  3668. }
  3669. cerr << endl;
  3670. cerr << "Done." << endl;
  3671. break;
  3672. case MODE_FORCE_RESYNC:
  3673. if (lstUsernames.empty())
  3674. hr = ForceResyncAll(lpSession, lpMsgStore);
  3675. else
  3676. hr = ForceResync(lpSession, lpMsgStore, lstUsernames);
  3677. if (hr != hrSuccess) {
  3678. cerr << "Failed to force resync." << endl;
  3679. goto exit;
  3680. }
  3681. cerr << "Successfully forced resync." << endl;
  3682. break;
  3683. case MODE_USER_COUNT:
  3684. hr = DisplayUserCount(lpMsgStore);
  3685. if (hr != hrSuccess) {
  3686. cerr << "Failed to get user statistics." << endl;
  3687. goto exit;
  3688. }
  3689. break;
  3690. case MODE_RESET_FOLDER_COUNT:
  3691. hr = ResetFolderCount(lpSession, lpMsgStore, username);
  3692. if (FAILED(hr)) {
  3693. cerr << "Failed to reset folder counters." << endl;
  3694. goto exit;
  3695. } else if (hr != hrSuccess) {
  3696. cerr << "Some folder counters could not be reset." << endl;
  3697. } else {
  3698. cerr << "Successfully reset folder counters." << endl;
  3699. }
  3700. case MODE_INVALID:
  3701. case MODE_HELP:
  3702. // happy compiler
  3703. break;
  3704. };
  3705. exit:
  3706. SSL_library_cleanup();
  3707. if (forcedExitCode > 0)
  3708. return forcedExitCode;
  3709. if (hr == hrSuccess)
  3710. return 0;
  3711. cerr << "Using the -v option (possibly multiple times) may "
  3712. << "give more hints." << endl;
  3713. return 1;
  3714. }