1616 */
1717package com .zimbra .cs .service .admin ;
1818
19+ import java .io .BufferedReader ;
20+ import java .io .IOException ;
21+ import java .io .InputStream ;
22+ import java .io .InputStreamReader ;
23+ import java .util .ArrayList ;
24+ import java .util .Arrays ;
25+ import java .util .Collections ;
1926import java .util .List ;
2027import java .util .Map ;
28+ import java .util .stream .Collectors ;
29+ import java .util .stream .Stream ;
2130
22- import com .zimbra .cs .account .AccountServiceException ;
23- import com .zimbra .cs .account .Server ;
24- import com .zimbra .cs .account .Provisioning ;
25- import com .zimbra .cs .account .accesscontrol .AdminRight ;
26- import com .zimbra .cs .account .accesscontrol .Rights .Admin ;
2731import com .zimbra .common .account .Key ;
32+ import com .zimbra .common .localconfig .LC ;
2833import com .zimbra .common .service .ServiceException ;
29- import com .zimbra .common .util .ZimbraLog ;
3034import com .zimbra .common .soap .AdminConstants ;
3135import com .zimbra .common .soap .Element ;
36+ import com .zimbra .common .util .ZimbraLog ;
37+ import com .zimbra .cs .account .AccountServiceException ;
38+ import com .zimbra .cs .account .Provisioning ;
39+ import com .zimbra .cs .account .Server ;
40+ import com .zimbra .cs .account .ZAttrServer ;
41+ import com .zimbra .cs .account .accesscontrol .AdminRight ;
42+ import com .zimbra .cs .account .accesscontrol .Rights .Admin ;
3243import com .zimbra .soap .ZimbraSoapContext ;
3344
3445/**
@@ -58,6 +69,8 @@ public Element handle(Element request, Map<String, Object> context) throws Servi
5869 }
5970 checkRight (zsc , context , server , attrs );
6071
72+ startOrStopPostSRSd (server , attrs );
73+
6174 // pass in true to checkImmutable
6275 prov .modifyAttrs (server , attrs , true );
6376
@@ -74,4 +87,107 @@ public void docRights(List<AdminRight> relatedRights, List<String> notes) {
7487 notes .add (String .format (AdminRightCheckPoint .Notes .MODIFY_ENTRY ,
7588 Admin .R_modifyServer .getName (), "server" ));
7689 }
90+
91+ public void startOrStopPostSRSd (Server server , Map <String , Object > attrs ) throws ServiceException {
92+
93+ ZimbraLog .mailbox .info ("==============================" );
94+
95+ List <String > command = new ArrayList <>();
96+ List <String > response = new ArrayList <>();
97+ List <String > attrsUI = new ArrayList <>();
98+ List <String > attrsLDAP = new ArrayList <>();
99+ boolean UIWantsToEnablePostsrs = false ;
100+ boolean isPostsrsEnabledInLDAP = false ;
101+ final String POSTSRSD_EXE = LC .zimbra_home .value () + "/common/sbin/postsrsd" ;
102+ final String POSTSRSD_SECRET = "/opt/zimbra/common/etc/postsrsd.secret" ;
103+
104+ try {
105+
106+ if (!attrs .isEmpty ()) {
107+ Collections .addAll (attrsUI , (String []) attrs .get ("zimbraServiceEnabled" ));
108+ ZimbraLog .mailbox .info ("attrsUI: " + attrsUI );
109+ UIWantsToEnablePostsrs = attrsUI .contains ("postsrs" );
110+ }
111+
112+ if (!server .getAttrs ().isEmpty ()) {
113+ Collections .addAll (attrsLDAP , server .getServiceEnabled ());
114+ ZimbraLog .mailbox .info ("attrsLDAP: " + attrsLDAP );
115+ isPostsrsEnabledInLDAP = attrsLDAP .contains ("postsrs" );
116+ }
117+
118+ if (UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
119+ command = Stream .of (POSTSRSD_EXE , "-s" , POSTSRSD_SECRET , "-d" , "mydomain" , "-D" ).collect (Collectors .toList ());
120+ response = executeLinuxCommand (command );
121+ ZimbraLog .mailbox .info (response );
122+ ZimbraLog .mailbox .info ("postsrsd has been enabled" );
123+ }
124+ else if (UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
125+ ZimbraLog .mailbox .info ("postsrsd is already enabled" );
126+ }
127+ else if (!UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
128+ // There is no command to disable SRS so far. The only way is killing the process.
129+ command = Stream .of ("pgrep" , "-f" , "postsrsd" ).collect (Collectors .toList ());
130+ response = executeLinuxCommand (command );
131+ ZimbraLog .mailbox .info ("response: " + response );
132+ if (response .isEmpty ()) {
133+ ZimbraLog .mailbox .info ("postsrsd is already disabled" );
134+ }
135+ else {
136+ String postSrsdPID = response .get (0 );
137+ command .clear ();
138+ response .clear ();
139+ command = Stream .of ("kill" , "-9" , postSrsdPID ).collect (Collectors .toList ());
140+ response = executeLinuxCommand (command );
141+ ZimbraLog .mailbox .info ("response: " + response );
142+ ZimbraLog .mailbox .info ("postsrsd has been disabled" );
143+ }
144+ }
145+ else if (!UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
146+ ZimbraLog .mailbox .info ("postsrsd is already disabled" );
147+ }
148+
149+ } catch (IOException e ) {
150+ ZimbraLog .mailbox .warn (e );
151+ } catch (InterruptedException e ) {
152+ ZimbraLog .mailbox .warn (e );
153+ }
154+
155+
156+ ZimbraLog .mailbox .info ("==============================" );
157+
158+ }
159+
160+ public List <String > executeLinuxCommand (List <String > command ) throws IOException , InterruptedException {
161+
162+ InputStream is = null ;
163+ ProcessBuilder pb = null ;
164+ Process ps = null ;
165+ List <String > lines = new ArrayList <>();
166+
167+ ZimbraLog .mailbox .info ("command: " + command );
168+
169+ // Executing the linux command
170+ pb = new ProcessBuilder (command );
171+ ps = pb .start ();
172+ int exitValue = ps .waitFor ();
173+ ZimbraLog .mailbox .info ("command executed" );
174+ // Getting executed command response as List
175+ if (exitValue == 0 ) {
176+ is = ps .getInputStream ();
177+ }
178+ else {
179+ is = ps .getErrorStream ();
180+ }
181+ InputStreamReader isr = new InputStreamReader (is );
182+ BufferedReader br = new BufferedReader (isr );
183+ String line ;
184+ while ((line = br .readLine ()) != null ) {
185+ lines .add (line );
186+ }
187+ ps .destroy ();
188+
189+ return lines ;
190+
191+ }
192+
77193}
0 commit comments