~carifio/seahorse/seahorse-remove-broken-help-buttons

« back to all changes in this revision

Viewing changes to ssh/seahorse-ssh-key.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-07-22 09:20:59 UTC
  • mto: (3.2.1 sid) (1.4.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 59.
  • Revision ID: james.westby@ubuntu.com-20080722092059-q336t49r9uaveij3
Tags: upstream-2.23.5
ImportĀ upstreamĀ versionĀ 2.23.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Seahorse
 
3
 *
 
4
 * Copyright (C) 2005 Stefan Walter
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
14
 * See the GNU General Public License for more details.
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the
 
17
 * Free Software Foundation, Inc.,
 
18
 * 59 Temple Place, Suite 330,
 
19
 * Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <string.h>
 
25
 
 
26
#include <glib.h>
 
27
#include <glib/gi18n.h>
 
28
 
 
29
#include "seahorse-context.h"
 
30
#include "seahorse-source.h"
 
31
#include "seahorse-ssh-source.h"
 
32
#include "seahorse-ssh-key.h"
 
33
#include "seahorse-ssh-operation.h"
 
34
#include "seahorse-gtkstock.h"
 
35
 
 
36
enum {
 
37
    PROP_0,
 
38
    PROP_KEY_DATA,
 
39
    PROP_DISPLAY_NAME,
 
40
    PROP_DISPLAY_ID,
 
41
    PROP_SIMPLE_NAME,
 
42
    PROP_FINGERPRINT,
 
43
    PROP_VALIDITY,
 
44
    PROP_TRUST,
 
45
    PROP_EXPIRES,
 
46
    PROP_LENGTH,
 
47
    PROP_STOCK_ID
 
48
};
 
49
 
 
50
struct _SeahorseSSHKeyPrivate {
 
51
    gchar *displayname;
 
52
    gchar *simplename;
 
53
};
 
54
 
 
55
G_DEFINE_TYPE (SeahorseSSHKey, seahorse_ssh_key, SEAHORSE_TYPE_KEY);
 
56
 
 
57
/* -----------------------------------------------------------------------------
 
58
 * INTERNAL 
 
59
 */
 
60
 
 
61
static gchar*
 
62
parse_first_word (const gchar *line) 
 
63
{
 
64
    int pos;
 
65
    
 
66
    #define PARSE_CHARS "\t \n@;,.\\?()[]{}+/"
 
67
    line += strspn (line, PARSE_CHARS);
 
68
    pos = strcspn (line, PARSE_CHARS);
 
69
    return pos == 0 ? NULL : g_strndup (line, pos);
 
70
}
 
71
 
 
72
static void
 
73
changed_key (SeahorseSSHKey *skey)
 
74
{
 
75
    SeahorseKey *key = SEAHORSE_KEY (skey);
 
76
    SeahorseObject *obj = SEAHORSE_OBJECT (skey);
 
77
 
 
78
    g_free (skey->priv->displayname);
 
79
    skey->priv->displayname = NULL;
 
80
    
 
81
    g_free (skey->priv->simplename);
 
82
    skey->priv->simplename = NULL;
 
83
    
 
84
    if (skey->keydata) {
 
85
 
 
86
        /* Try to make display and simple names */
 
87
        if (skey->keydata->comment) {
 
88
            skey->priv->displayname = g_strdup (skey->keydata->comment);
 
89
            skey->priv->simplename = parse_first_word (skey->keydata->comment);
 
90
            
 
91
        /* No names when not even the fingerpint loaded */
 
92
        } else if (!skey->keydata->fingerprint) {
 
93
            skey->priv->displayname = g_strdup (_("(Unreadable Secure Shell Key)"));
 
94
    
 
95
        /* No comment, but loaded */        
 
96
        } else {
 
97
            skey->priv->displayname = g_strdup (_("Secure Shell Key"));
 
98
        }
 
99
    
 
100
        if (skey->priv->simplename == NULL)
 
101
            skey->priv->simplename = g_strdup (_("Secure Shell Key"));
 
102
        
 
103
    }
 
104
    
 
105
    /* Now start setting the main SeahorseKey fields */
 
106
    obj->_tag = SEAHORSE_SSH;
 
107
    obj->_id = 0;
 
108
    
 
109
    if (!skey->keydata || !skey->keydata->fingerprint) {
 
110
        
 
111
        obj->_location = SEAHORSE_LOCATION_INVALID;
 
112
        obj->_usage = SEAHORSE_USAGE_NONE;
 
113
        key->loaded = SKEY_INFO_NONE;
 
114
        obj->_flags = SKEY_FLAG_DISABLED;
 
115
        key->keydesc = _("Invalid");
 
116
        key->rawid = NULL;
 
117
        
 
118
    } else {
 
119
    
 
120
        /* The key id */
 
121
        obj->_id = seahorse_ssh_key_get_cannonical_id (skey->keydata->fingerprint);
 
122
        key->rawid = skey->keydata->fingerprint;
 
123
        obj->_location = SEAHORSE_LOCATION_LOCAL;
 
124
        key->loaded = SKEY_INFO_COMPLETE;
 
125
        obj->_flags = skey->keydata->authorized ? SKEY_FLAG_TRUSTED : 0;
 
126
        
 
127
        if (skey->keydata->privfile) {
 
128
            obj->_usage = SEAHORSE_USAGE_PRIVATE_KEY;
 
129
            key->keydesc = _("Private Secure Shell Key");
 
130
        } else {
 
131
            obj->_usage = SEAHORSE_USAGE_PUBLIC_KEY;
 
132
            key->keydesc = _("Public Secure Shell Key");
 
133
        }
 
134
    }
 
135
    
 
136
    if (!obj->_id)
 
137
        obj->_id = g_quark_from_string (SEAHORSE_SSH_STR ":UNKNOWN ");
 
138
    
 
139
    seahorse_object_fire_changed (obj, SKEY_CHANGE_ALL);
 
140
}
 
141
 
 
142
static guint 
 
143
calc_validity (SeahorseSSHKey *skey)
 
144
{
 
145
    if (skey->keydata->privfile)
 
146
        return SEAHORSE_VALIDITY_ULTIMATE;
 
147
    return 0;
 
148
}
 
149
 
 
150
static guint
 
151
calc_trust (SeahorseSSHKey *skey)
 
152
{
 
153
    if (skey->keydata->authorized)
 
154
        return SEAHORSE_VALIDITY_FULL;
 
155
    return 0;
 
156
}
 
157
 
 
158
/* -----------------------------------------------------------------------------
 
159
 * OBJECT 
 
160
 */
 
161
 
 
162
static guint 
 
163
seahorse_ssh_key_get_num_names (SeahorseKey *key)
 
164
{
 
165
    return 1;
 
166
}
 
167
 
 
168
static gchar* 
 
169
seahorse_ssh_key_get_name (SeahorseKey *key, guint index)
 
170
{
 
171
    SeahorseSSHKey *skey;
 
172
    
 
173
    g_assert (SEAHORSE_IS_SSH_KEY (key));
 
174
    skey = SEAHORSE_SSH_KEY (key);
 
175
    
 
176
    g_assert (index == 0);
 
177
 
 
178
    return g_strdup (skey->priv->displayname);
 
179
}
 
180
 
 
181
static gchar* 
 
182
seahorse_ssh_key_get_name_cn (SeahorseKey *skey, guint index)
 
183
{
 
184
    g_assert (index != 0);
 
185
    return NULL;
 
186
}
 
187
 
 
188
static SeahorseValidity  
 
189
seahorse_ssh_key_get_name_validity  (SeahorseKey *skey, guint index)
 
190
{
 
191
    g_return_val_if_fail (index == 0, SEAHORSE_VALIDITY_UNKNOWN);
 
192
    return calc_validity (SEAHORSE_SSH_KEY (skey));
 
193
}
 
194
 
 
195
static void
 
196
seahorse_ssh_key_get_property (GObject *object, guint prop_id,
 
197
                               GValue *value, GParamSpec *pspec)
 
198
{
 
199
    SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (object);
 
200
    
 
201
    switch (prop_id) {
 
202
    case PROP_KEY_DATA:
 
203
        g_value_set_pointer (value, skey->keydata);
 
204
        break;
 
205
    case PROP_DISPLAY_NAME:
 
206
        g_value_set_string (value, skey->priv->displayname);
 
207
        break;
 
208
    case PROP_DISPLAY_ID:
 
209
        g_value_set_string (value, seahorse_key_get_short_keyid (SEAHORSE_KEY (skey))); 
 
210
        break;
 
211
    case PROP_SIMPLE_NAME:        
 
212
        g_value_set_string (value, skey->priv->simplename);
 
213
        break;
 
214
    case PROP_FINGERPRINT:
 
215
        g_value_set_string (value, skey->keydata ? skey->keydata->fingerprint : NULL);
 
216
        break;
 
217
    case PROP_VALIDITY:
 
218
        g_value_set_uint (value, calc_validity (skey));
 
219
        break;
 
220
    case PROP_TRUST:
 
221
        g_value_set_uint (value, calc_trust (skey));
 
222
        break;
 
223
    case PROP_EXPIRES:
 
224
        g_value_set_ulong (value, 0);
 
225
        break;
 
226
    case PROP_LENGTH:
 
227
        g_value_set_uint (value, skey->keydata ? skey->keydata->length : 0);
 
228
        break;
 
229
    case PROP_STOCK_ID:
 
230
        g_value_set_string (value, SEAHORSE_STOCK_KEY_SSH);
 
231
        break;
 
232
    }
 
233
}
 
234
 
 
235
static void
 
236
seahorse_ssh_key_set_property (GObject *object, guint prop_id, 
 
237
                               const GValue *value, GParamSpec *pspec)
 
238
{
 
239
    SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (object);
 
240
    SeahorseSSHKeyData *keydata;
 
241
    
 
242
    switch (prop_id) {
 
243
    case PROP_KEY_DATA:
 
244
        keydata = (SeahorseSSHKeyData*)g_value_get_pointer (value);
 
245
        if (skey->keydata != keydata) {
 
246
            seahorse_ssh_key_data_free (skey->keydata);
 
247
            skey->keydata = keydata;
 
248
        }
 
249
        changed_key (skey);
 
250
        break;
 
251
    }
 
252
}
 
253
 
 
254
/* Unrefs gpgme key and frees data */
 
255
static void
 
256
seahorse_ssh_key_finalize (GObject *gobject)
 
257
{
 
258
    SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (gobject);
 
259
    
 
260
    g_free (skey->priv->displayname);
 
261
    g_free (skey->priv->simplename);
 
262
    g_free (skey->priv);
 
263
    skey->priv = NULL;
 
264
    
 
265
    seahorse_ssh_key_data_free (skey->keydata);
 
266
    
 
267
    G_OBJECT_CLASS (seahorse_ssh_key_parent_class)->finalize (gobject);
 
268
}
 
269
 
 
270
static void
 
271
seahorse_ssh_key_init (SeahorseSSHKey *skey)
 
272
{
 
273
    /* init private vars */
 
274
    skey->priv = g_new0 (SeahorseSSHKeyPrivate, 1);
 
275
}
 
276
 
 
277
static void
 
278
seahorse_ssh_key_class_init (SeahorseSSHKeyClass *klass)
 
279
{
 
280
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
281
    SeahorseKeyClass *key_class = SEAHORSE_KEY_CLASS (klass);
 
282
    
 
283
    seahorse_ssh_key_parent_class = g_type_class_peek_parent (klass);
 
284
    
 
285
    gobject_class->finalize = seahorse_ssh_key_finalize;
 
286
    gobject_class->set_property = seahorse_ssh_key_set_property;
 
287
    gobject_class->get_property = seahorse_ssh_key_get_property;
 
288
 
 
289
    key_class->get_num_names = seahorse_ssh_key_get_num_names;
 
290
    key_class->get_name = seahorse_ssh_key_get_name;
 
291
    key_class->get_name_cn = seahorse_ssh_key_get_name_cn;
 
292
    key_class->get_name_validity = seahorse_ssh_key_get_name_validity;
 
293
    
 
294
    g_object_class_install_property (gobject_class, PROP_KEY_DATA,
 
295
        g_param_spec_pointer ("key-data", "SSH Key Data", "SSH key data pointer",
 
296
                              G_PARAM_READWRITE));
 
297
 
 
298
    g_object_class_install_property (gobject_class, PROP_DISPLAY_NAME,
 
299
        g_param_spec_string ("display-name", "Display Name", "User Displayable name for this key",
 
300
                             "", G_PARAM_READABLE));
 
301
 
 
302
    g_object_class_install_property (gobject_class, PROP_DISPLAY_ID,
 
303
        g_param_spec_string ("display-id", "Display ID", "User Displayable id for this key",
 
304
                             "", G_PARAM_READABLE));
 
305
                      
 
306
    g_object_class_install_property (gobject_class, PROP_SIMPLE_NAME,
 
307
        g_param_spec_string ("simple-name", "Simple Name", "Simple name for this key",
 
308
                             "", G_PARAM_READABLE));
 
309
                      
 
310
    g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
 
311
        g_param_spec_string ("fingerprint", "Fingerprint", "Unique fingerprint for this key",
 
312
                             "", G_PARAM_READABLE));
 
313
 
 
314
    g_object_class_install_property (gobject_class, PROP_VALIDITY,
 
315
        g_param_spec_uint ("validity", "Validity", "Validity of this key",
 
316
                           0, G_MAXUINT, 0, G_PARAM_READABLE));
 
317
 
 
318
    g_object_class_install_property (gobject_class, PROP_TRUST,
 
319
        g_param_spec_uint ("trust", "Trust", "Trust in this key",
 
320
                           0, G_MAXUINT, 0, G_PARAM_READABLE));
 
321
 
 
322
    g_object_class_install_property (gobject_class, PROP_EXPIRES,
 
323
        g_param_spec_ulong ("expires", "Expires On", "Date this key expires on",
 
324
                           0, G_MAXULONG, 0, G_PARAM_READABLE));
 
325
 
 
326
    g_object_class_install_property (gobject_class, PROP_LENGTH,
 
327
        g_param_spec_uint ("length", "Length of", "The length of this key",
 
328
                           0, G_MAXUINT, 0, G_PARAM_READABLE));
 
329
                           
 
330
    g_object_class_install_property (gobject_class, PROP_STOCK_ID,
 
331
        g_param_spec_string ("stock-id", "The stock icon", "The stock icon id",
 
332
                             NULL, G_PARAM_READABLE));
 
333
}
 
334
 
 
335
/* -----------------------------------------------------------------------------
 
336
 * PUBLIC METHODS
 
337
 */
 
338
 
 
339
SeahorseSSHKey* 
 
340
seahorse_ssh_key_new (SeahorseSource *sksrc, SeahorseSSHKeyData *data)
 
341
{
 
342
    SeahorseSSHKey *skey;
 
343
    skey = g_object_new (SEAHORSE_TYPE_SSH_KEY, "key-source", sksrc, 
 
344
                         "key-data", data, NULL);
 
345
    return skey;
 
346
}
 
347
 
 
348
guint 
 
349
seahorse_ssh_key_get_algo (SeahorseSSHKey *skey)
 
350
{
 
351
    g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), SSH_ALGO_UNK);
 
352
    return skey->keydata->algo;
 
353
}
 
354
 
 
355
const gchar*
 
356
seahorse_ssh_key_get_algo_str (SeahorseSSHKey *skey)
 
357
{
 
358
    g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), "");
 
359
    
 
360
    switch(skey->keydata->algo) {
 
361
    case SSH_ALGO_UNK:
 
362
        return "";
 
363
    case SSH_ALGO_RSA:
 
364
        return "RSA";
 
365
    case SSH_ALGO_DSA:
 
366
        return "DSA";
 
367
    default:
 
368
        g_assert_not_reached ();
 
369
        return NULL;
 
370
    };
 
371
}
 
372
 
 
373
guint           
 
374
seahorse_ssh_key_get_strength (SeahorseSSHKey *skey)
 
375
{
 
376
    g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), 0);
 
377
    return skey->keydata ? skey->keydata->length : 0;
 
378
}
 
379
 
 
380
const gchar*    
 
381
seahorse_ssh_key_get_location (SeahorseSSHKey *skey)
 
382
{
 
383
    g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), NULL);
 
384
    if (!skey->keydata)
 
385
        return NULL;
 
386
    return skey->keydata->privfile ? 
 
387
                    skey->keydata->privfile : skey->keydata->pubfile;
 
388
}
 
389
 
 
390
GQuark
 
391
seahorse_ssh_key_get_cannonical_id (const gchar *id)
 
392
{
 
393
    #define SSH_ID_SIZE 16
 
394
    gchar *hex, *canonical_id = g_malloc0 (SSH_ID_SIZE + 1);
 
395
    gint i, off, len = strlen (id);
 
396
    GQuark ret;
 
397
 
 
398
    /* Strip out all non alpha numeric chars and limit length to SSH_ID_SIZE */
 
399
    for (i = len, off = SSH_ID_SIZE; i >= 0 && off > 0; --i) {
 
400
         if (g_ascii_isalnum (id[i]))
 
401
             canonical_id[--off] = g_ascii_toupper (id[i]);
 
402
    }
 
403
    
 
404
    /* Not enough characters */
 
405
    g_return_val_if_fail (off == 0, 0);
 
406
 
 
407
    hex = g_strdup_printf ("%s:%s", SEAHORSE_SSH_STR, canonical_id);
 
408
    ret = g_quark_from_string (hex);
 
409
    
 
410
    g_free (canonical_id);
 
411
    g_free (hex);
 
412
    
 
413
    return ret;
 
414
}
 
415