File: interface_keys.c

package info (click to toggle)
cpm 0.26-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,292 kB
  • sloc: ansic: 7,239; makefile: 506; perl: 406; sh: 325
file content (278 lines) | stat: -rw-r--r-- 7,353 bytes parent folder | download | duplicates (5)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* #############################################################################
 * code for all things regarding the key list.
 * #############################################################################
 * Copyright (C) 2005-2009 Harry Brueckner
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contact: Harry Brueckner <harry_b@mm.st>
 *          Muenchener Strasse 12a
 *          85253 Kleinberghofen
 *          Germany
 * #############################################################################
 */

/* #############################################################################
 * includes
 */
#include "cpm.h"
#include "configuration.h"
#include "general.h"
#include "gpg.h"
#include "interface_keys.h"
#include "interface_utf8.h"
#include "listhandler.h"
#include "memory.h"


/* #############################################################################
 * global variables
 */
char**                  encrypttionkeylist;


/* #############################################################################
 *
 * Description    free the keys
 * Author         Harry Brueckner
 * Date           2005-03-30
 * Arguments      void
 * Return         void
 */
void freeKeys(void)
  {
    TRACE(99, "freeKeys()", NULL);

    encrypttionkeylist = listFree(encrypttionkeylist);
  }


/* #############################################################################
 *
 * Description    initialize the key handling
 * Author         Harry Brueckner
 * Date           2005-03-30
 * Arguments      void
 * Return         void
 */
void initKeys(void)
  {
    TRACE(99, "initKeys()", NULL);

    encrypttionkeylist = NULL;
  }


/* #############################################################################
 *
 * Description    add a key
 * Author         Harry Brueckner
 * Date           2005-03-30
 * Arguments      char* key   - the key to add
 * Return         int 1 if the key was added, otherwise 0
 */
int keyAdd(char* key)
  {
    char*               identifier;
    char*               tname;

    TRACE(99, "keyAdd()", NULL);

    if (!key || !strlen(key))
      { return 0; }

    tname = (char*)convert2xml(key);
    identifier = gpgValidateEncryptionKey(tname);

    if (identifier)
      {
        if (keyGetId(identifier) == -1)
          {   /* we don't know this key yet */
            encrypttionkeylist = listAdd(encrypttionkeylist, identifier);
            listSort(encrypttionkeylist);
          }

        /* we must free the identifier string */
        memFreeString(__FILE__, __LINE__, identifier);

        return 1;
      }
    else
      { return 0; }
  }


/* #############################################################################
 *
 * Description    modify a key
 * Author         Harry Brueckner
 * Date           2005-03-30
 * Arguments      int id      - id of the key to modify
 *                char* key   - new key value
 * Return         int 1 if the key was added, otherwise 0
 */
int keyChange(int id, char* key)
  {
    int                 entries;
    char*               identifier;
    char*               tname;

    TRACE(99, "keyChange()", NULL);

    if (!key || !strlen(key))
      { return 0; }

    entries = listCount(encrypttionkeylist);

    if (id < 0 ||
        id >= entries)
      { return 0; }

    tname = (char*)convert2xml(key);
    identifier = gpgValidateEncryptionKey(tname);
    if (identifier)
      {
        if (keyGetId(identifier) == -1)
          {   /* the key already exists, so we ignore it */
            memFreeString(__FILE__, __LINE__, identifier);
          }
        else
          {   /* we don't know this key */
            memFreeString(__FILE__, __LINE__, encrypttionkeylist[id]);
            encrypttionkeylist[id] = identifier;
            listSort(encrypttionkeylist);
          }

        return 1;
      }
    else
      { return 0; }
  }


/* #############################################################################
 *
 * Description    count how many keys we have
 * Author         Harry Brueckner
 * Date           2005-03-31
 * Arguments      void
 * Return         int number of currently available keys
 */
int keyCount(void)
  {
    TRACE(99, "keyCount()", NULL);

    return listCount(encrypttionkeylist);
  }


/* #############################################################################
 *
 * Description    set the used keys to the defaults found in the resource
 * Author         Harry Brueckner
 * Date           2005-03-31
 * Arguments      void
 * Return         void
 */
void keyDefaults(void)
  {
    int                 i;

    TRACE(99, "keyDefaults()", NULL);

    for (i = listCount(config -> defaultkeys); i > 0; i--)
      {
        if (!keyAdd(config -> defaultkeys[i - 1]))
          {
            fprintf(stderr,
                _("error: encryption key %s could not be validated; not using it.\n"),
                config -> defaultkeys[i - 1]);
          }
      }
  }


/* #############################################################################
 *
 * Description    delete a key
 * Author         Harry Brueckner
 * Date           2005-03-30
 * Arguments      int id  - id of the key to delete
 * Return         void
 */
void keyDelete(int id)
  {
    TRACE(99, "keyDelete()", NULL);

    encrypttionkeylist = listDelete(encrypttionkeylist, id);
  }


/* #############################################################################
 *
 * Description    get a key by its index
 * Author         Harry Brueckner
 * Date           2005-04-03
 * Arguments      int id  - id of the key to get
 * Return         char* pointing to the key
 */
char* keyGet(int id)
  {
    TRACE(99, "keyGet()", NULL);

    return encrypttionkeylist[id];
  }


/* #############################################################################
 *
 * Description    get a key id by its value
 * Author         Harry Brueckner
 * Date           2005-04-13
 * Arguments      char* key   - key name
 * Return         int id of the key, if its not found -1 is returned
 */
int keyGetId(char* key)
  {
    int                 i;

    TRACE(99, "keyGetId()", NULL);

    for (i = listCount(encrypttionkeylist); i > 0; i--)
      {
        if (!strcmp(encrypttionkeylist[i - 1], key))
            return i - 1;
      }

    return -1;
  }


/* #############################################################################
 *
 * Description    get the key list
 * Author         Harry Brueckner
 * Date           2005-04-03
 * Arguments      void
 * Return         char** list of all keys
 */
char** keyGetList(int id)
  {
    TRACE(99, "keyGetList()", NULL);

    return encrypttionkeylist;
  }


/* #############################################################################
 */