source: osm/applications/editors/josm/plugins/native-password-manager/src/org/netbeans/modules/keyring/gnome/GnomeProvider.java@ 26335

Last change on this file since 26335 was 26335, checked in by bastik, 13 years ago

new plugin: native password manager

File size: 7.5 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
5 *
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
8 *
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
26 *
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
37 *
38 * Contributor(s):
39 *
40 * Portions Copyrighted 2009 Sun Microsystems, Inc.
41 */
42
43package org.netbeans.modules.keyring.gnome;
44
45import com.sun.jna.Pointer;
46import java.util.logging.Level;
47import java.util.logging.Logger;
48import static org.netbeans.modules.keyring.gnome.GnomeKeyringLibrary.*;
49import org.netbeans.spi.keyring.KeyringProvider;
50
51public class GnomeProvider implements KeyringProvider {
52
53 private static final Logger LOG = Logger.getLogger(GnomeProvider.class.getName());
54 private static final String KEY = "key"; // NOI18N
55
56 public @Override boolean enabled() {
57 if (Boolean.getBoolean("netbeans.keyring.no.native")) {
58 LOG.fine("native keyring integration disabled");
59 return false;
60 }
61 boolean envVarSet = false;
62 for (String key : System.getenv().keySet()) {
63 if (key.startsWith("GNOME_KEYRING_")) { // NOI18N
64 envVarSet = true;
65 break;
66 }
67 }
68 if (!envVarSet) {
69 LOG.fine("no GNOME_KEYRING_* environment variable set");
70 return false;
71 }
72 String appName = "JOSM";
73 try {
74 // Need to do this somewhere, or we get warnings on console.
75 // Also used by confirmation dialogs to give the app access to the login keyring.
76 LIBRARY.g_set_application_name(appName);
77 if (!LIBRARY.gnome_keyring_is_available()) {
78 return false;
79 }
80 // #178571: try to read some key just to make sure gnome_keyring_find_password_sync is bound:
81 read("NoNeXiStEnT"); // NOI18N
82 return true;
83 } catch (Throwable t) {
84 LOG.log(Level.FINE, null, t);
85 return false;
86 }
87 }
88
89 public @Override char[] read(String key) {
90 Pointer[] found = new Pointer[1];
91 Pointer attributes = LIBRARY.g_array_new(0, 0, GnomeKeyringAttribute_SIZE);
92 try {
93 LIBRARY.gnome_keyring_attribute_list_append_string(attributes, KEY, key);
94 error(GnomeKeyringLibrary.LIBRARY.gnome_keyring_find_items_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, attributes, found));
95 } finally {
96 LIBRARY.gnome_keyring_attribute_list_free(attributes);
97 }
98 if (found[0] != null) {
99 try {
100 if (LIBRARY.g_list_length(found[0]) > 0) {
101 GnomeKeyringFound result = LIBRARY.g_list_nth_data(found[0], 0);
102 if (result != null) {
103 if (result.secret != null) {
104 return result.secret.toCharArray();
105 } else {
106 LOG.warning("#183670: GnomeKeyringFound.secret == null");
107 delete(key);
108 }
109 } else {
110 LOG.warning("#183670: GList<GnomeKeyringFound>[0].result == null");
111 }
112 }
113 } finally {
114 LIBRARY.gnome_keyring_found_list_free(found[0]);
115 }
116 }
117 return null;
118 }
119
120 public @Override void save(String key, char[] password, String description) {
121 Pointer attributes = LIBRARY.g_array_new(0, 0, GnomeKeyringAttribute_SIZE);
122 try {
123 LIBRARY.gnome_keyring_attribute_list_append_string(attributes, KEY, key);
124 int[] item_id = new int[1];
125 error(GnomeKeyringLibrary.LIBRARY.gnome_keyring_item_create_sync(
126 null, GNOME_KEYRING_ITEM_GENERIC_SECRET, description != null ? description : key, attributes, new String(password), true, item_id));
127 } finally {
128 LIBRARY.gnome_keyring_attribute_list_free(attributes);
129 }
130 }
131
132 public @Override void delete(String key) {
133 Pointer[] found = new Pointer[1];
134 Pointer attributes = LIBRARY.g_array_new(0, 0, GnomeKeyringAttribute_SIZE);
135 try {
136 LIBRARY.gnome_keyring_attribute_list_append_string(attributes, KEY, key);
137 error(GnomeKeyringLibrary.LIBRARY.gnome_keyring_find_items_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, attributes, found));
138 } finally {
139 LIBRARY.gnome_keyring_attribute_list_free(attributes);
140 }
141 if (found[0] == null) {
142 return;
143 }
144 int id;
145 try {
146 if (LIBRARY.g_list_length(found[0]) > 0) {
147 GnomeKeyringFound result = LIBRARY.g_list_nth_data(found[0], 0);
148 id = result.item_id;
149 } else {
150 id = 0;
151 }
152 } finally {
153 LIBRARY.gnome_keyring_found_list_free(found[0]);
154 }
155 if (id > 0) {
156 if ("SunOS".equals(System.getProperty("os.name")) && "5.10".equals(System.getProperty("os.version"))) { // #185698
157 save(key, new char[0], null); // gnome_keyring_item_delete(null, id, null, null, null) does not seem to do anything
158 } else {
159 error(GnomeKeyringLibrary.LIBRARY.gnome_keyring_item_delete_sync(null, id));
160 }
161 }
162 }
163
164 private static String[] ERRORS = {
165 "OK", // NOI18N
166 "DENIED", // NOI18N
167 "NO_KEYRING_DAEMON", // NOI18N
168 "ALREADY_UNLOCKED", // NOI18N
169 "NO_SUCH_KEYRING", // NOI18N
170 "BAD_ARGUMENTS", // NOI18N
171 "IO_ERROR", // NOI18N
172 "CANCELLED", // NOI18N
173 "KEYRING_ALREADY_EXISTS", // NOI18N
174 "NO_MATCH", // NOI18N
175 };
176 private static void error(int code) {
177 if (code != 0 && code != 9) {
178 LOG.log(Level.WARNING, "gnome-keyring error: {0}", ERRORS[code]);
179 }
180 }
181
182}
Note: See TracBrowser for help on using the repository browser.