001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smack;
019
020import org.jivesoftware.smack.packet.Session;
021import org.jivesoftware.smack.proxy.ProxyInfo;
022import org.jivesoftware.smack.util.DNSUtil;
023import org.jivesoftware.smack.util.StringUtils;
024import org.jivesoftware.smack.util.dns.HostAddress;
025
026import javax.net.SocketFactory;
027import javax.net.ssl.HostnameVerifier;
028import javax.net.ssl.SSLContext;
029import javax.security.auth.callback.CallbackHandler;
030
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.List;
034
035/**
036 * Configuration to use while establishing the connection to the server. It is possible to
037 * configure the path to the trustore file that keeps the trusted CA root certificates and
038 * enable or disable all or some of the checkings done while verifying server certificates.<p>
039 *
040 * It is also possible to configure if TLS, SASL, and compression are used or not.
041 *
042 * @author Gaston Dombiak
043 */
044public class ConnectionConfiguration implements Cloneable {
045
046    /**
047     * Hostname of the XMPP server. Usually servers use the same service name as the name
048     * of the server. However, there are some servers like google where host would be
049     * talk.google.com and the serviceName would be gmail.com.
050     */
051    private String serviceName;
052
053    protected List<HostAddress> hostAddresses;
054
055    private String keystorePath;
056    private String keystoreType;
057    private String pkcs11Library;
058    private SSLContext customSSLContext;
059
060    private boolean compressionEnabled = false;
061
062    /**
063     * Used to get information from the user
064     */
065    private CallbackHandler callbackHandler;
066
067    private boolean debuggerEnabled = SmackConfiguration.DEBUG_ENABLED;
068
069    // Flag that indicates if a reconnection should be attempted when abruptly disconnected
070    private boolean reconnectionAllowed = true;
071    
072    // Holds the socket factory that is used to generate the socket in the connection
073    private SocketFactory socketFactory;
074    
075    // Holds the authentication information for future reconnections
076    private String username;
077    private String password;
078    private String resource;
079    private boolean sendPresence = true;
080    private boolean rosterLoadedAtLogin = true;
081    private boolean legacySessionDisabled = false;
082    private boolean useDnsSrvRr = true;
083    private SecurityMode securityMode = SecurityMode.enabled;
084
085    private HostnameVerifier hostnameVerifier;
086
087    /**
088     * Permanent store for the Roster, needed for roster versioning
089     */
090    private RosterStore rosterStore;
091
092    // Holds the proxy information (such as proxyhost, proxyport, username, password etc)
093    protected ProxyInfo proxy;
094
095    /**
096     * Creates a new ConnectionConfiguration for the specified service name.
097     * A DNS SRV lookup will be performed to find out the actual host address
098     * and port to use for the connection.
099     *
100     * @param serviceName the name of the service provided by an XMPP server.
101     */
102    public ConnectionConfiguration(String serviceName) {
103        init(serviceName, ProxyInfo.forDefaultProxy());
104    }
105
106     /**
107     * Creates a new ConnectionConfiguration for the specified service name 
108     * with specified proxy.
109     * A DNS SRV lookup will be performed to find out the actual host address
110     * and port to use for the connection.
111     *
112     * @param serviceName the name of the service provided by an XMPP server.
113     * @param proxy the proxy through which XMPP is to be connected
114     */
115    public ConnectionConfiguration(String serviceName,ProxyInfo proxy) {
116        init(serviceName, proxy);
117    }
118
119    /**
120     * Creates a new ConnectionConfiguration using the specified host, port and
121     * service name. This is useful for manually overriding the DNS SRV lookup
122     * process that's used with the {@link #ConnectionConfiguration(String)}
123     * constructor. For example, say that an XMPP server is running at localhost
124     * in an internal network on port 5222 but is configured to think that it's
125     * "example.com" for testing purposes. This constructor is necessary to connect
126     * to the server in that case since a DNS SRV lookup for example.com would not
127     * point to the local testing server.
128     *
129     * @param host the host where the XMPP server is running.
130     * @param port the port where the XMPP is listening.
131     * @param serviceName the name of the service provided by an XMPP server.
132     */
133    public ConnectionConfiguration(String host, int port, String serviceName) {
134        initHostAddresses(host, port);
135        init(serviceName, ProxyInfo.forDefaultProxy());
136    }
137        
138        /**
139     * Creates a new ConnectionConfiguration using the specified host, port and
140     * service name. This is useful for manually overriding the DNS SRV lookup
141     * process that's used with the {@link #ConnectionConfiguration(String)}
142     * constructor. For example, say that an XMPP server is running at localhost
143     * in an internal network on port 5222 but is configured to think that it's
144     * "example.com" for testing purposes. This constructor is necessary to connect
145     * to the server in that case since a DNS SRV lookup for example.com would not
146     * point to the local testing server.
147     *
148     * @param host the host where the XMPP server is running.
149     * @param port the port where the XMPP is listening.
150     * @param serviceName the name of the service provided by an XMPP server.
151     * @param proxy the proxy through which XMPP is to be connected
152     */
153    public ConnectionConfiguration(String host, int port, String serviceName, ProxyInfo proxy) {
154        initHostAddresses(host, port);
155        init(serviceName, proxy);
156    }
157
158    /**
159     * Creates a new ConnectionConfiguration for a connection that will connect
160     * to the desired host and port.
161     *
162     * @param host the host where the XMPP server is running.
163     * @param port the port where the XMPP is listening.
164     */
165    public ConnectionConfiguration(String host, int port) {
166        initHostAddresses(host, port);
167        init(host, ProxyInfo.forDefaultProxy());
168    }
169        
170        /**
171     * Creates a new ConnectionConfiguration for a connection that will connect
172     * to the desired host and port with desired proxy.
173     *
174     * @param host the host where the XMPP server is running.
175     * @param port the port where the XMPP is listening.
176     * @param proxy the proxy through which XMPP is to be connected
177     */
178    public ConnectionConfiguration(String host, int port, ProxyInfo proxy) {
179        initHostAddresses(host, port);
180        init(host, proxy);
181    }
182
183    protected void init(String serviceName, ProxyInfo proxy) {
184        if (StringUtils.isEmpty(serviceName)) {
185            throw new IllegalArgumentException("serviceName must not be the empty String");
186        }
187        this.serviceName = serviceName;
188        this.proxy = proxy;
189
190        keystorePath = System.getProperty("javax.net.ssl.keyStore");
191        keystoreType = "jks";
192        pkcs11Library = "pkcs11.config";
193                
194                //Setting the SocketFactory according to proxy supplied
195        socketFactory = proxy.getSocketFactory();
196    }
197
198    /**
199     * Sets the server name, also known as XMPP domain of the target server.
200     *
201     * @param serviceName the XMPP domain of the target server.
202     */
203    void setServiceName(String serviceName) {
204        this.serviceName = serviceName;
205    }
206
207    /**
208     * Returns the server name of the target server.
209     *
210     * @return the server name of the target server.
211     */
212    public String getServiceName() {
213        return serviceName;
214    }
215
216    /**
217     * Returns the TLS security mode used when making the connection. By default,
218     * the mode is {@link SecurityMode#enabled}.
219     *
220     * @return the security mode.
221     */
222    public SecurityMode getSecurityMode() {
223        return securityMode;
224    }
225
226    /**
227     * Sets the TLS security mode used when making the connection. By default,
228     * the mode is {@link SecurityMode#enabled}.
229     *
230     * @param securityMode the security mode.
231     */
232    public void setSecurityMode(SecurityMode securityMode) {
233        this.securityMode = securityMode;
234    }
235
236    /**
237     * Retuns the path to the keystore file. The key store file contains the 
238     * certificates that may be used to authenticate the client to the server,
239     * in the event the server requests or requires it.
240     *
241     * @return the path to the keystore file.
242     */
243    public String getKeystorePath() {
244        return keystorePath;
245    }
246
247    /**
248     * Sets the path to the keystore file. The key store file contains the 
249     * certificates that may be used to authenticate the client to the server,
250     * in the event the server requests or requires it.
251     *
252     * @param keystorePath the path to the keystore file.
253     */
254    public void setKeystorePath(String keystorePath) {
255        this.keystorePath = keystorePath;
256    }
257
258    /**
259     * Returns the keystore type, or <tt>null</tt> if it's not set.
260     *
261     * @return the keystore type.
262     */
263    public String getKeystoreType() {
264        return keystoreType;
265    }
266
267    /**
268     * Sets the keystore type.
269     *
270     * @param keystoreType the keystore type.
271     */
272    public void setKeystoreType(String keystoreType) {
273        this.keystoreType = keystoreType;
274    }
275
276
277    /**
278     * Returns the PKCS11 library file location, needed when the
279     * Keystore type is PKCS11.
280     *
281     * @return the path to the PKCS11 library file
282     */
283    public String getPKCS11Library() {
284        return pkcs11Library;
285    }
286
287    /**
288     * Sets the PKCS11 library file location, needed when the
289     * Keystore type is PKCS11
290     *
291     * @param pkcs11Library the path to the PKCS11 library file
292     */
293    public void setPKCS11Library(String pkcs11Library) {
294        this.pkcs11Library = pkcs11Library;
295    }
296
297    /**
298     * Gets the custom SSLContext previously set with {@link #setCustomSSLContext(SSLContext)} for
299     * SSL sockets. This is null by default.
300     *
301     * @return the custom SSLContext or null.
302     */
303    public SSLContext getCustomSSLContext() {
304        return this.customSSLContext;
305    }
306
307    /**
308     * Sets a custom SSLContext for creating SSL sockets.
309     * <p>
310     * For more information on how to create a SSLContext see <a href=
311     * "http://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#X509TrustManager"
312     * >Java Secure Socket Extension (JSEE) Reference Guide: Creating Your Own X509TrustManager</a>
313     *
314     * @param context the custom SSLContext for new sockets
315     */
316    public void setCustomSSLContext(SSLContext context) {
317        this.customSSLContext = context;
318    }
319
320    /**
321     * Set the HostnameVerifier used to verify the hostname of SSLSockets used by XMPP connections
322     * created with this ConnectionConfiguration.
323     * 
324     * @param verifier
325     */
326    public void setHostnameVerifier(HostnameVerifier verifier) {
327        hostnameVerifier = verifier;
328    }
329
330    /**
331     * Returns the configured HostnameVerifier of this ConnectionConfiguration or the Smack default
332     * HostnameVerifier configured with
333     * {@link SmackConfiguration#setDefaultHostnameVerifier(HostnameVerifier)}.
334     * 
335     * @return a configured HostnameVerifier or <code>null</code>
336     */
337    public HostnameVerifier getHostnameVerifier() {
338        if (hostnameVerifier != null)
339            return hostnameVerifier;
340        return SmackConfiguration.getDefaultHostnameVerifier();
341    }
342
343    /**
344     * Returns true if the connection is going to use stream compression. Stream compression
345     * will be requested after TLS was established (if TLS was enabled) and only if the server
346     * offered stream compression. With stream compression network traffic can be reduced
347     * up to 90%. By default compression is disabled.
348     *
349     * @return true if the connection is going to use stream compression.
350     */
351    public boolean isCompressionEnabled() {
352        return compressionEnabled;
353    }
354
355    /**
356     * Sets if the connection is going to use stream compression. Stream compression
357     * will be requested after TLS was established (if TLS was enabled) and only if the server
358     * offered stream compression. With stream compression network traffic can be reduced
359     * up to 90%. By default compression is disabled.
360     *
361     * @param compressionEnabled if the connection is going to use stream compression.
362     */
363    public void setCompressionEnabled(boolean compressionEnabled) {
364        this.compressionEnabled = compressionEnabled;
365    }
366
367    /**
368     * Returns true if the new connection about to be establish is going to be debugged. By
369     * default the value of {@link SmackConfiguration#DEBUG_ENABLED} is used.
370     *
371     * @return true if the new connection about to be establish is going to be debugged.
372     */
373    public boolean isDebuggerEnabled() {
374        return debuggerEnabled;
375    }
376
377    /**
378     * Sets if the new connection about to be establish is going to be debugged. By
379     * default the value of {@link SmackConfiguration#DEBUG_ENABLED} is used.
380     *
381     * @param debuggerEnabled if the new connection about to be establish is going to be debugged.
382     */
383    public void setDebuggerEnabled(boolean debuggerEnabled) {
384        this.debuggerEnabled = debuggerEnabled;
385    }
386    
387    /**
388     * Sets if the reconnection mechanism is allowed to be used. By default
389     * reconnection is allowed.
390     *
391     * @param isAllowed if the reconnection mechanism should be enabled for this connection.
392     */
393    public void setReconnectionAllowed(boolean isAllowed) {
394        this.reconnectionAllowed = isAllowed;
395    }
396
397    /**
398     * Returns if the reconnection mechanism is allowed to be used. By default reconnection is
399     * allowed. You can disable the reconnection mechanism with {@link
400     * #setReconnectionAllowed(boolean)}.
401     *
402     * @return true, if the reconnection mechanism is enabled.
403     */
404    public boolean isReconnectionAllowed() {
405        return this.reconnectionAllowed;
406    }
407
408    /**
409     * Sets the socket factory used to create new xmppConnection sockets.
410     * This is useful when connecting through SOCKS5 proxies.
411     *
412     * @param socketFactory used to create new sockets.
413     */
414    public void setSocketFactory(SocketFactory socketFactory) {
415        this.socketFactory = socketFactory;
416    }
417
418    /**
419     * Sets if an initial available presence will be sent to the server. By default
420     * an available presence will be sent to the server indicating that this presence
421     * is not online and available to receive messages. If you want to log in without
422     * being 'noticed' then pass a <tt>false</tt> value.
423     *
424     * @param sendPresence true if an initial available presence will be sent while logging in.
425     */
426    public void setSendPresence(boolean sendPresence) {
427        this.sendPresence = sendPresence;
428    }
429
430    /**
431     * Returns true if the roster will be loaded from the server when logging in. This
432     * is the common behaviour for clients but sometimes clients may want to differ this
433     * or just never do it if not interested in rosters.
434     *
435     * @return true if the roster will be loaded from the server when logging in.
436     */
437    public boolean isRosterLoadedAtLogin() {
438        return rosterLoadedAtLogin;
439    }
440
441    /**
442     * Sets if the roster will be loaded from the server when logging in. This
443     * is the common behaviour for clients but sometimes clients may want to differ this
444     * or just never do it if not interested in rosters.
445     *
446     * @param rosterLoadedAtLogin if the roster will be loaded from the server when logging in.
447     */
448    public void setRosterLoadedAtLogin(boolean rosterLoadedAtLogin) {
449        this.rosterLoadedAtLogin = rosterLoadedAtLogin;
450    }
451
452    /**
453     * Returns true if a {@link Session} will be requested on login if the server
454     * supports it. Although this was mandatory on RFC 3921, RFC 6120/6121 don't
455     * even mention this part of the protocol.
456     *
457     * @return true if a session has to be requested when logging in.
458     */
459    public boolean isLegacySessionDisabled() {
460        return legacySessionDisabled;
461    }
462
463    /**
464     * Sets if a {@link Session} will be requested on login if the server supports
465     * it. Although this was mandatory on RFC 3921, RFC 6120/6121 don't even
466     * mention this part of the protocol.
467     *
468     * @param legacySessionDisabled if a session has to be requested when logging in.
469     */
470    public void setLegacySessionDisabled(boolean legacySessionDisabled) {
471        this.legacySessionDisabled = legacySessionDisabled;
472    }
473
474    /**
475     * Returns a CallbackHandler to obtain information, such as the password or
476     * principal information during the SASL authentication. A CallbackHandler
477     * will be used <b>ONLY</b> if no password was specified during the login while
478     * using SASL authentication.
479     *
480     * @return a CallbackHandler to obtain information, such as the password or
481     * principal information during the SASL authentication.
482     */
483    public CallbackHandler getCallbackHandler() {
484        return callbackHandler;
485    }
486
487    /**
488     * Sets a CallbackHandler to obtain information, such as the password or
489     * principal information during the SASL authentication. A CallbackHandler
490     * will be used <b>ONLY</b> if no password was specified during the login while
491     * using SASL authentication.
492     *
493     * @param callbackHandler to obtain information, such as the password or
494     * principal information during the SASL authentication.
495     */
496    public void setCallbackHandler(CallbackHandler callbackHandler) {
497        this.callbackHandler = callbackHandler;
498    }
499
500    /**
501     * Returns the socket factory used to create new xmppConnection sockets.
502     * This is useful when connecting through SOCKS5 proxies.
503     * 
504     * @return socketFactory used to create new sockets.
505     */
506    public SocketFactory getSocketFactory() {
507        return this.socketFactory;
508    }
509
510    public List<HostAddress> getHostAddresses() {
511        return Collections.unmodifiableList(hostAddresses);
512    }
513
514    /**
515     * Set the permanent roster store
516     */
517    public void setRosterStore(RosterStore store) {
518        rosterStore = store;
519    }
520
521    /**
522     * Get the permanent roster store
523     */
524    public RosterStore getRosterStore() {
525        return rosterStore;
526    }
527
528
529    /**
530     * An enumeration for TLS security modes that are available when making a connection
531     * to the XMPP server.
532     */
533    public static enum SecurityMode {
534
535        /**
536         * Securirty via TLS encryption is required in order to connect. If the server
537         * does not offer TLS or if the TLS negotiaton fails, the connection to the server
538         * will fail.
539         */
540        required,
541
542        /**
543         * Security via TLS encryption is used whenever it's available. This is the
544         * default setting.
545         */
546        enabled,
547
548        /**
549         * Security via TLS encryption is disabled and only un-encrypted connections will
550         * be used. If only TLS encryption is available from the server, the connection
551         * will fail.
552         */
553        disabled
554    }
555
556    /**
557     * Returns the username to use when trying to reconnect to the server.
558     *
559     * @return the username to use when trying to reconnect to the server.
560     */
561    public String getUsername() {
562        return this.username;
563    }
564
565    /**
566     * Returns the password to use when trying to reconnect to the server.
567     *
568     * @return the password to use when trying to reconnect to the server.
569     */
570    public String getPassword() {
571        return this.password;
572    }
573
574    /**
575     * Returns the resource to use when trying to reconnect to the server.
576     *
577     * @return the resource to use when trying to reconnect to the server.
578     */
579    public String getResource() {
580        return resource;
581    }
582
583    /**
584     * Returns true if an available presence should be sent when logging in while reconnecting.
585     *
586     * @return true if an available presence should be sent when logging in while reconnecting
587     */
588    public boolean isSendPresence() {
589        return sendPresence;
590    }
591
592    void setLoginInfo(String username, String password, String resource) {
593        this.username = username;
594        this.password = password;
595        this.resource = resource;
596    }
597
598    void maybeResolveDns() throws Exception {
599        if (!useDnsSrvRr) return;
600        hostAddresses = DNSUtil.resolveXMPPDomain(serviceName);
601    }
602
603    private void initHostAddresses(String host, int port) {
604        if (StringUtils.isEmpty(host)) {
605            throw new IllegalArgumentException("host must not be the empty String");
606        }
607        hostAddresses = new ArrayList<HostAddress>(1);
608        HostAddress hostAddress;
609        hostAddress = new HostAddress(host, port);
610        hostAddresses.add(hostAddress);
611        useDnsSrvRr = false;
612    }
613}