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 |
|
---|
43 | package org.netbeans.modules.keyring.gnome;
|
---|
44 |
|
---|
45 | import com.sun.jna.DefaultTypeMapper;
|
---|
46 | import com.sun.jna.FromNativeContext;
|
---|
47 | import com.sun.jna.Library;
|
---|
48 | import com.sun.jna.Native;
|
---|
49 | import com.sun.jna.Pointer;
|
---|
50 | import com.sun.jna.Structure;
|
---|
51 | import com.sun.jna.ToNativeContext;
|
---|
52 | import com.sun.jna.TypeConverter;
|
---|
53 | import java.io.File;
|
---|
54 | import java.util.Arrays;
|
---|
55 | import java.util.Collections;
|
---|
56 | import java.util.List;
|
---|
57 | import java.util.Map;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * JNA wrapper for certain functions from GNOME Keyring API.
|
---|
61 | * #178571: must work against GNOME 2.6 to support JDS 3 (Solaris 10).
|
---|
62 | * @see <a href="http://library.gnome.org/devel/gnome-keyring/stable/">gnome-keyring API Reference</a>
|
---|
63 | */
|
---|
64 | public interface GnomeKeyringLibrary extends Library {
|
---|
65 |
|
---|
66 | class LibFinder {
|
---|
67 | private static final String GENERIC = "gnome-keyring";
|
---|
68 | // http://packages.ubuntu.com/search?suite=precise&arch=any&mode=exactfilename&searchon=contents&keywords=libgnome-keyring.so.0
|
---|
69 | private static final String EXPLICIT_ONEIRIC = "/usr/lib/libgnome-keyring.so.0";
|
---|
70 | private static Object load(Map<?,?> options) {
|
---|
71 | try {
|
---|
72 | return Native.loadLibrary(GENERIC, GnomeKeyringLibrary.class, options);
|
---|
73 | } catch (UnsatisfiedLinkError x) {
|
---|
74 | // #203735: on Oneiric, may have trouble finding right lib.
|
---|
75 | // Precise is using multiarch (#211401) which should work automatically using JNA 3.4+ (#211403).
|
---|
76 | // Unclear if this workaround is still needed for Oneiric with 3.4, but seems harmless to leave it in for now.
|
---|
77 | if (new File(EXPLICIT_ONEIRIC).isFile()) {
|
---|
78 | return Native.loadLibrary(EXPLICIT_ONEIRIC, GnomeKeyringLibrary.class, options);
|
---|
79 | } else {
|
---|
80 | throw x;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | private LibFinder() {}
|
---|
85 | }
|
---|
86 |
|
---|
87 | GnomeKeyringLibrary LIBRARY = (GnomeKeyringLibrary) LibFinder.load(Collections.singletonMap(OPTION_TYPE_MAPPER, new DefaultTypeMapper() {
|
---|
88 | {
|
---|
89 | addTypeConverter(Boolean.TYPE, new TypeConverter() { // #198921
|
---|
90 | @Override public Object toNative(Object value, ToNativeContext context) {
|
---|
91 | return Boolean.TRUE.equals(value) ? 1 : 0;
|
---|
92 | }
|
---|
93 | @Override public Object fromNative(Object value, FromNativeContext context) {
|
---|
94 | return ((Integer) value).intValue() != 0;
|
---|
95 | }
|
---|
96 | @Override public Class<?> nativeType() {
|
---|
97 | // gint is 32-bit int
|
---|
98 | return Integer.class;
|
---|
99 | }
|
---|
100 | });
|
---|
101 | }
|
---|
102 | }));
|
---|
103 |
|
---|
104 | boolean gnome_keyring_is_available();
|
---|
105 |
|
---|
106 | /*GnomeKeyringItemType*/int GNOME_KEYRING_ITEM_GENERIC_SECRET = 0;
|
---|
107 |
|
---|
108 | // GnomeKeyringAttributeList gnome_keyring_attribute_list_new() = g_array_new(FALSE, FALSE, sizeof(GnomeKeyringAttribute))
|
---|
109 | int GnomeKeyringAttribute_SIZE = Pointer.SIZE * 3; // conservatively: 2 pointers + 1 enum
|
---|
110 |
|
---|
111 | void gnome_keyring_attribute_list_append_string(
|
---|
112 | /*GnomeKeyringAttributeList*/Pointer attributes,
|
---|
113 | String name,
|
---|
114 | String value);
|
---|
115 |
|
---|
116 | void gnome_keyring_attribute_list_free(
|
---|
117 | /*GnomeKeyringAttributeList*/Pointer attributes);
|
---|
118 |
|
---|
119 | int gnome_keyring_item_create_sync(
|
---|
120 | String keyring,
|
---|
121 | /*GnomeKeyringItemType*/int type,
|
---|
122 | String display_name,
|
---|
123 | /*GnomeKeyringAttributeList*/Pointer attributes,
|
---|
124 | String secret,
|
---|
125 | boolean update_if_exists,
|
---|
126 | int[] item_id);
|
---|
127 |
|
---|
128 | int gnome_keyring_item_delete_sync(
|
---|
129 | String keyring,
|
---|
130 | int id);
|
---|
131 |
|
---|
132 | int gnome_keyring_find_items_sync(
|
---|
133 | /*GnomeKeyringItemType*/int type,
|
---|
134 | /*GnomeKeyringAttributeList*/Pointer attributes,
|
---|
135 | /*GList<GnomeKeyringFound>*/Pointer[] found);
|
---|
136 |
|
---|
137 | void gnome_keyring_found_list_free(
|
---|
138 | /*GList<GnomeKeyringFound>*/Pointer found_list);
|
---|
139 |
|
---|
140 | class GnomeKeyringFound extends Structure {
|
---|
141 | public String keyring;
|
---|
142 | public int item_id;
|
---|
143 | public /*GnomeKeyringAttributeList*/Pointer attributes;
|
---|
144 | public String secret;
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | protected List<String> getFieldOrder() {
|
---|
148 | return Arrays.asList( new String[] {
|
---|
149 | "keyring",
|
---|
150 | "item_id",
|
---|
151 | "attributes",
|
---|
152 | "secret",
|
---|
153 | } );
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** http://library.gnome.org/devel/glib/2.6/glib-Miscellaneous-Utility-Functions.html#g-set-application-name */
|
---|
158 | void g_set_application_name(String name);
|
---|
159 |
|
---|
160 | /** http://library.gnome.org/devel/glib/2.6/glib-Arrays.html */
|
---|
161 |
|
---|
162 | Pointer g_array_new(
|
---|
163 | /*gboolean*/int zero_terminated,
|
---|
164 | /*gboolean*/int clear,
|
---|
165 | int element_size);
|
---|
166 |
|
---|
167 | /** http://library.gnome.org/devel/glib/2.6/glib-Doubly-Linked-Lists.html */
|
---|
168 |
|
---|
169 | int g_list_length(
|
---|
170 | Pointer list);
|
---|
171 |
|
---|
172 | /*gpointer*/GnomeKeyringFound g_list_nth_data(
|
---|
173 | Pointer list,
|
---|
174 | int n);
|
---|
175 |
|
---|
176 | }
|
---|