Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2c5cbb23bc2a28708fb1a078e15f1a41aed19d9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     LinuxPasswordProviderMessages.java
 *******************************************************************************/
#include <jni.h>
#include "keystoreLinuxNative.h"
#include <libsecret/secret.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

const SecretSchema *
equinox_get_schema (void)
{
    static const SecretSchema the_schema = {
        "org.eclipse.equinox", SECRET_SCHEMA_NONE,
        {
            {  NULL, 0 },
        }
    };
    return &the_schema;
}

#define EQUINOX_SCHEMA  equinox_get_schema ()

static void unlock_secret_service(JNIEnv *env)
{
	GError *error = NULL;
	GList *l, *ul;
	gchar* lbl;
	gint nu;
	//check that there is session dbus bus
	GDBusConnection* dbusconnection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
	if (error != NULL) {
		(*env)->ExceptionClear(env);
	 	char buffer [60];
		sprintf(buffer, "Unable to get secret service: %s", error->message);
	 	(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
	 	g_error_free (error);
	 	return;
	}
	SecretService*	secretservice = secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &error);
 	if (error != NULL) {
	  (*env)->ExceptionClear(env);
 	  char buffer [60];
		sprintf(buffer, "Unable to get secret service: %s", error->message);
 		(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
 		g_error_free (error);
 		return;
 	}
 
	SecretCollection*	defaultcollection = secret_collection_for_alias_sync(secretservice, SECRET_COLLECTION_DEFAULT, SECRET_COLLECTION_NONE, NULL, &error);
 	if (error != NULL) {
	  (*env)->ExceptionClear(env);
 	  char buffer [60];
		sprintf(buffer, "Unable to get secret service: %s", error->message);
 		(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
 		g_error_free (error);
 		return;
 	}

	if (secret_collection_get_locked(defaultcollection))
	{
		lbl = secret_collection_get_label(defaultcollection);
		l = NULL;
		l = g_list_append(l, defaultcollection);
		nu = secret_service_unlock_sync(secretservice, l, NULL, &ul, &error);
		g_list_free(l);
		g_list_free(ul);
		if (error != NULL) {
		  (*env)->ExceptionClear(env);
	 	  char buffer [60];
			sprintf(buffer, "Unable to unlock: %s", error->message);
	 		(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
	 		g_error_free (error);
	 		return;
	 	}
	 	
	}
	return;
}

JNIEXPORT jstring JNICALL Java_org_eclipse_equinox_internal_security_linux_LinuxPasswordProvider_getMasterPassword(JNIEnv *env, jobject this) {
  GError *error = NULL;
  jstring result;
  
  unlock_secret_service(env);
  if ((*env)->ExceptionOccurred(env)) {
    return NULL;
  }	
  
	gchar *password = secret_password_lookup_sync(EQUINOX_SCHEMA, NULL, &error, NULL);
	
	if (error != NULL) {
	    (*env)->ExceptionClear(env);
			char buffer [60];
			sprintf(buffer, "%s.  Result: %d", error->message, error->code);
			(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
	    g_error_free (error);
	    return NULL;
	} else if (password == NULL) {
	    (*env)->ExceptionClear(env);
			(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), "Unable to find password");
			return NULL;
	} else {
	    result = (*env)->NewStringUTF(env, password);
			free(password);
	    return result;
	}
}

JNIEXPORT void JNICALL Java_org_eclipse_equinox_internal_security_linux_LinuxPasswordProvider_saveMasterPassword(JNIEnv *env, jobject this, jstring password) {
	GError *error = NULL;
	
	unlock_secret_service(env);
	if ((*env)->ExceptionOccurred(env)) {
    return;
  }
  
	const char *passwordUTF = (*env)->GetStringUTFChars(env, password, NULL);
	secret_password_store_sync (EQUINOX_SCHEMA, SECRET_COLLECTION_DEFAULT,
	                            "Equinox master password", passwordUTF, NULL, &error,
	                            NULL);
	
	// free the UTF strings
	(*env)->ReleaseStringUTFChars( env, password, passwordUTF );

	if (error != NULL) {
    (*env)->ExceptionClear(env);
		char buffer [60];
		sprintf(buffer, "%s.  Result: %d", error->message, error->code);
		(*env)->ThrowNew(env, (* env)->FindClass(env, "java/lang/SecurityException"), buffer);
		g_error_free (error);
	}
}

Back to the top