~ubuntu-branches/ubuntu/utopic/strongswan/utopic

« back to all changes in this revision

Viewing changes to src/frontends/android/src/org/strongswan/android/data/VpnProfileDataSource.java

  • Committer: Package Import Robot
  • Author(s): Jonathan Davies
  • Date: 2014-01-20 19:00:59 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20140120190059-z8e4dl3g8cd09yi5
Tags: 5.1.2~dr3+git20130120-0ubuntu1
* Upstream Git snapshot for build fixes with regards to entropy.
* debian/rules:
  - Enforcing DEB_BUILD_OPTIONS=nostrip for library integrity checking.
  - Set TESTS_REDUCED_KEYLENGTHS to one generate smallest key-lengths in
    tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Tobias Brunner
 
3
 * Copyright (C) 2012 Giuliano Grassi
 
4
 * Copyright (C) 2012 Ralf Sager
 
5
 * Hochschule fuer Technik Rapperswil
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License as published by the
 
9
 * Free Software Foundation; either version 2 of the License, or (at your
 
10
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
15
 * for more details.
 
16
 */
 
17
 
 
18
package org.strongswan.android.data;
 
19
 
 
20
import java.util.ArrayList;
 
21
import java.util.List;
 
22
 
 
23
import android.content.ContentValues;
 
24
import android.content.Context;
 
25
import android.database.Cursor;
 
26
import android.database.SQLException;
 
27
import android.database.sqlite.SQLiteDatabase;
 
28
import android.database.sqlite.SQLiteOpenHelper;
 
29
import android.database.sqlite.SQLiteQueryBuilder;
 
30
import android.util.Log;
 
31
 
 
32
public class VpnProfileDataSource
 
33
{
 
34
        private static final String TAG = VpnProfileDataSource.class.getSimpleName();
 
35
        public static final String KEY_ID = "_id";
 
36
        public static final String KEY_NAME = "name";
 
37
        public static final String KEY_GATEWAY = "gateway";
 
38
        public static final String KEY_VPN_TYPE = "vpn_type";
 
39
        public static final String KEY_USERNAME = "username";
 
40
        public static final String KEY_PASSWORD = "password";
 
41
        public static final String KEY_CERTIFICATE = "certificate";
 
42
        public static final String KEY_USER_CERTIFICATE = "user_certificate";
 
43
 
 
44
        private DatabaseHelper mDbHelper;
 
45
        private SQLiteDatabase mDatabase;
 
46
        private final Context mContext;
 
47
 
 
48
        private static final String DATABASE_NAME = "strongswan.db";
 
49
        private static final String TABLE_VPNPROFILE = "vpnprofile";
 
50
 
 
51
        private static final int DATABASE_VERSION = 4;
 
52
 
 
53
        public static final String DATABASE_CREATE =
 
54
                                                        "CREATE TABLE " + TABLE_VPNPROFILE + " (" +
 
55
                                                                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
 
56
                                                                KEY_NAME + " TEXT NOT NULL," +
 
57
                                                                KEY_GATEWAY + " TEXT NOT NULL," +
 
58
                                                                KEY_VPN_TYPE + " TEXT NOT NULL," +
 
59
                                                                KEY_USERNAME + " TEXT," +
 
60
                                                                KEY_PASSWORD + " TEXT," +
 
61
                                                                KEY_CERTIFICATE + " TEXT," +
 
62
                                                                KEY_USER_CERTIFICATE + " TEXT" +
 
63
                                                        ");";
 
64
        private static final String[] ALL_COLUMNS = new String[] {
 
65
                                                                KEY_ID,
 
66
                                                                KEY_NAME,
 
67
                                                                KEY_GATEWAY,
 
68
                                                                KEY_VPN_TYPE,
 
69
                                                                KEY_USERNAME,
 
70
                                                                KEY_PASSWORD,
 
71
                                                                KEY_CERTIFICATE,
 
72
                                                                KEY_USER_CERTIFICATE,
 
73
                                                        };
 
74
 
 
75
        private static class DatabaseHelper extends SQLiteOpenHelper
 
76
        {
 
77
                public DatabaseHelper(Context context)
 
78
                {
 
79
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
 
80
                }
 
81
 
 
82
                @Override
 
83
                public void onCreate(SQLiteDatabase database)
 
84
                {
 
85
                        database.execSQL(DATABASE_CREATE);
 
86
                }
 
87
 
 
88
                @Override
 
89
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
 
90
                {
 
91
                        Log.w(TAG, "Upgrading database from version " + oldVersion +
 
92
                                  " to " + newVersion);
 
93
                        if (oldVersion < 2)
 
94
                        {
 
95
                                db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
 
96
                                                   " TEXT;");
 
97
                        }
 
98
                        if (oldVersion < 3)
 
99
                        {
 
100
                                db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_VPN_TYPE +
 
101
                                                   " TEXT DEFAULT '';");
 
102
                        }
 
103
                        if (oldVersion < 4)
 
104
                        {       /* remove NOT NULL constraint from username column */
 
105
                                updateColumns(db);
 
106
                        }
 
107
                }
 
108
 
 
109
                private void updateColumns(SQLiteDatabase db)
 
110
                {
 
111
                        db.beginTransaction();
 
112
                        try
 
113
                        {
 
114
                                db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " RENAME TO tmp_" + TABLE_VPNPROFILE + ";");
 
115
                                db.execSQL(DATABASE_CREATE);
 
116
                                StringBuilder insert = new StringBuilder("INSERT INTO " + TABLE_VPNPROFILE + " SELECT ");
 
117
                                SQLiteQueryBuilder.appendColumns(insert, ALL_COLUMNS);
 
118
                                db.execSQL(insert.append(" FROM tmp_" + TABLE_VPNPROFILE + ";").toString());
 
119
                                db.execSQL("DROP TABLE tmp_" + TABLE_VPNPROFILE + ";");
 
120
                                db.setTransactionSuccessful();
 
121
                        }
 
122
                        finally
 
123
                        {
 
124
                                db.endTransaction();
 
125
                        }
 
126
                }
 
127
        }
 
128
 
 
129
        /**
 
130
         * Construct a new VPN profile data source. The context is used to
 
131
         * open/create the database.
 
132
         * @param context context used to access the database
 
133
         */
 
134
        public VpnProfileDataSource(Context context)
 
135
        {
 
136
                this.mContext = context;
 
137
        }
 
138
 
 
139
        /**
 
140
         * Open the VPN profile data source. The database is automatically created
 
141
         * if it does not yet exist. If that fails an exception is thrown.
 
142
         * @return itself (allows to chain initialization calls)
 
143
         * @throws SQLException if the database could not be opened or created
 
144
         */
 
145
        public VpnProfileDataSource open() throws SQLException
 
146
        {
 
147
                if (mDbHelper == null)
 
148
                {
 
149
                        mDbHelper = new DatabaseHelper(mContext);
 
150
                        mDatabase = mDbHelper.getWritableDatabase();
 
151
                }
 
152
                return this;
 
153
        }
 
154
 
 
155
        /**
 
156
         * Close the data source.
 
157
         */
 
158
        public void close()
 
159
        {
 
160
                if (mDbHelper != null)
 
161
                {
 
162
                        mDbHelper.close();
 
163
                        mDbHelper = null;
 
164
                }
 
165
        }
 
166
 
 
167
        /**
 
168
         * Insert the given VPN profile into the database.  On success the Id of
 
169
         * the object is updated and the object returned.
 
170
         *
 
171
         * @param profile the profile to add
 
172
         * @return the added VPN profile or null, if failed
 
173
         */
 
174
        public VpnProfile insertProfile(VpnProfile profile)
 
175
        {
 
176
                ContentValues values = ContentValuesFromVpnProfile(profile);
 
177
                long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
 
178
                if (insertId == -1)
 
179
                {
 
180
                        return null;
 
181
                }
 
182
                profile.setId(insertId);
 
183
                return profile;
 
184
        }
 
185
 
 
186
        /**
 
187
         * Updates the given VPN profile in the database.
 
188
         * @param profile the profile to update
 
189
         * @return true if update succeeded, false otherwise
 
190
         */
 
191
        public boolean updateVpnProfile(VpnProfile profile)
 
192
        {
 
193
                long id = profile.getId();
 
194
                ContentValues values = ContentValuesFromVpnProfile(profile);
 
195
                return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
 
196
        }
 
197
 
 
198
        /**
 
199
         * Delete the given VPN profile from the database.
 
200
         * @param profile the profile to delete
 
201
         * @return true if deleted, false otherwise
 
202
         */
 
203
        public boolean deleteVpnProfile(VpnProfile profile)
 
204
        {
 
205
                long id = profile.getId();
 
206
                return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
 
207
        }
 
208
 
 
209
        /**
 
210
         * Get a single VPN profile from the database.
 
211
         * @param id the ID of the VPN profile
 
212
         * @return the profile or null, if not found
 
213
         */
 
214
        public VpnProfile getVpnProfile(long id)
 
215
        {
 
216
                VpnProfile profile = null;
 
217
                Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
 
218
                                                                                KEY_ID + "=" + id, null, null, null, null);
 
219
                if (cursor.moveToFirst())
 
220
                {
 
221
                        profile = VpnProfileFromCursor(cursor);
 
222
                }
 
223
                cursor.close();
 
224
                return profile;
 
225
        }
 
226
 
 
227
        /**
 
228
         * Get a list of all VPN profiles stored in the database.
 
229
         * @return list of VPN profiles
 
230
         */
 
231
        public List<VpnProfile> getAllVpnProfiles()
 
232
        {
 
233
                List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
 
234
 
 
235
                Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
 
236
                cursor.moveToFirst();
 
237
                while (!cursor.isAfterLast())
 
238
                {
 
239
                        VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
 
240
                        vpnProfiles.add(vpnProfile);
 
241
                        cursor.moveToNext();
 
242
                }
 
243
                cursor.close();
 
244
                return vpnProfiles;
 
245
        }
 
246
 
 
247
        private VpnProfile VpnProfileFromCursor(Cursor cursor)
 
248
        {
 
249
                VpnProfile profile = new VpnProfile();
 
250
                profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
 
251
                profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
 
252
                profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
 
253
                profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
 
254
                profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
 
255
                profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
 
256
                profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
 
257
                profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
 
258
                return profile;
 
259
        }
 
260
 
 
261
        private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
 
262
        {
 
263
                ContentValues values = new ContentValues();
 
264
                values.put(KEY_NAME, profile.getName());
 
265
                values.put(KEY_GATEWAY, profile.getGateway());
 
266
                values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
 
267
                values.put(KEY_USERNAME, profile.getUsername());
 
268
                values.put(KEY_PASSWORD, profile.getPassword());
 
269
                values.put(KEY_CERTIFICATE, profile.getCertificateAlias());
 
270
                values.put(KEY_USER_CERTIFICATE, profile.getUserCertificateAlias());
 
271
                return values;
 
272
        }
 
273
}