aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/configuration/ConfigurationActivator.java
blob: e6cee27543529c96a617a7ce5eef182fabcf8d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.configuration;

import com.sun.jna.*;

import net.java.sip.communicator.util.ServiceUtils;

import org.jitsi.service.configuration.*;
import org.jitsi.service.fileaccess.*;
import org.jitsi.service.libjitsi.*;
import org.jitsi.util.*;
import org.osgi.framework.*;

import java.io.*;

/**
 * @author Emil Ivov
 * @author Lyubomir Marinov
 */
public class ConfigurationActivator
    implements BundleActivator
{
    /**
     * The <tt>Logger</tt> used by the <tt>ConfigurationActivator</tt> class
     * for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(ConfigurationActivator.class);

    /**
     * The currently registered {@link ConfigurationService} instance.
     */
    private ConfigurationService cs;

    /**
     * Starts the configuration service
     *
     * @param bundleContext the <tt>BundleContext</tt> as provided by the OSGi
     * framework.
     * @throws Exception if anything goes wrong
     */
    public void start(BundleContext bundleContext)
        throws Exception
    {
        FileAccessService fas
            = ServiceUtils.getService(bundleContext, FileAccessService.class);
        if (fas != null)
        {
            File useDatabaseConfig = fas.getPrivatePersistentFile(
                ".usedatabaseconfig",
                FileCategory.PROFILE);

            // BETA: if the marker file exists, use the database configuration
            if (useDatabaseConfig.exists())
            {
                logger.info("Using database configuration store.");
                this.cs = new JdbcConfigService(fas);
            }
        }

        if (this.cs == null)
            this.cs = LibJitsi.getConfigurationService();

        bundleContext.registerService(
                ConfigurationService.class.getName(),
                this.cs,
                null);

        fixPermissions(this.cs);
    }

    /**
     * Causes the configuration service to store the properties object and
     * unregisters the configuration service.
     *
     * @param bundleContext <tt>BundleContext</tt>
     * @throws Exception if anything goes wrong while storing the properties
     * managed by the <tt>ConfigurationService</tt> implementation provided by
     * this bundle and while unregistering the service in question
     */
    public void stop(BundleContext bundleContext)
        throws Exception
    {
        this.cs.storeConfiguration();
        this.cs = null;
    }

    /**
     * Makes home folder and the configuration file readable and writable
     * only to the owner.
     * @param configurationService the config service instance to check
     *                             for home folder and name.
     */
    private static void fixPermissions(
        ConfigurationService configurationService)
    {
        if(!OSUtils.IS_LINUX && !OSUtils.IS_MAC)
            return;

        try
        {
            // let's check config file and config folder
            File homeFolder = new File(
                configurationService.getScHomeDirLocation(),
                configurationService.getScHomeDirName());

            CLibrary  libc = (CLibrary) Native.loadLibrary("c", CLibrary.class);
            libc.chmod(homeFolder.getAbsolutePath(), 0700);

            String fileName = configurationService.getConfigurationFilename();

            if(fileName != null)
            {
                File cf = new File(homeFolder, fileName);
                if(cf.exists())
                {
                    libc.chmod(cf.getAbsolutePath(), 0600);
                }
            }
        }
        catch(Throwable t)
        {
            logger.error(
                "Error creating c lib instance for fixing file permissions", t);
        }
    }

    /**
     * The jna interface to the c library and the chmod we use
     * to fix permissions of user files.
     */
    public interface CLibrary
        extends Library
    {
        /**
         * Changes file permissions.
         * @param path the path to file or folder which permissions we will
         *             change.
         * @param mode the mode operand
         * @return 0 shall be returned upon successful completion;
         *         otherwise, -1 shall be returned. If -1 is returned,
         *         no change to the file mode occurs.
         */
        public int chmod(String path, int mode);
    }
}