forked from sba1/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgp.c
161 lines (138 loc) · 4.13 KB
/
pgp.c
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
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
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
(at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/*
** pgp.c
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
#include "lists.h"
#include "pgp.h"
#include "support.h"
#include "support_indep.h"
static struct list pgp_list;
/******************************************************************
Free's the pgp list
*******************************************************************/
static void pgp_free_list(void)
{
struct pgp_key *key;
while ((key = (struct pgp_key*)list_remove_tail(&pgp_list)))
{
array_free(key->userids);
free(key);
}
}
/******************************************************************
Returns a list with struct key_node entries
*******************************************************************/
int pgp_update_key_list(void)
{
int rc = 0;
char *tmpname;
pgp_free_list();
tmpname = tmpnam(NULL);
if (!pgp_operate("-kv",tmpname))
{
FILE *fh = fopen(tmpname,"rb");
if (fh)
{
char buf[512];
buf[9] = buf[23] = 0;
while (fgets(buf,512,fh))
{
if (buf[9] == '/' && buf[23] == '/')
{
struct pgp_key *key = (struct pgp_key*)malloc(sizeof(struct pgp_key));
if (key)
{
key->userids = NULL;
key->keyid = strtoul(&buf[10],NULL,16);
key->userids = array_add_string(key->userids,&buf[29]);
list_insert_tail(&pgp_list,&key->node);
}
buf[9] = buf[23] = 0;
}
}
fclose(fh);
}
remove(tmpname);
}
return rc;
}
/******************************************************************
Returns a list with struct key_node entries
*******************************************************************/
struct pgp_key *pgp_first(void)
{
return (struct pgp_key *)list_first(&pgp_list);
}
/******************************************************************
Returns a list with struct key_node entries
*******************************************************************/
struct pgp_key *pgp_next(struct pgp_key *next)
{
return (struct pgp_key *)node_next(&next->node);
}
/******************************************************************
Duplicate a given pgp key entry.
*******************************************************************/
struct pgp_key *pgp_duplicate(struct pgp_key *key)
{
struct pgp_key *new_key;
if (!key) return NULL;
if ((new_key = (struct pgp_key*)malloc(sizeof(struct pgp_key))))
{
*new_key = *key;
new_key->userids = array_duplicate(key->userids);
}
return new_key;
}
/******************************************************************
Frees all memory associated with a pgp entry.
*******************************************************************/
void pgp_dispose(struct pgp_key *key)
{
if (key)
{
array_free(key->userids);
free(key);
}
}
/******************************************************************
Starts a pgp operation. Needs to be generalized
*******************************************************************/
int pgp_operate(char *options, char *output)
{
char *path = sm_getenv("PGPPATH");
int len = mystrlen(path)+10+mystrlen(options);
int rc;
char *buf = malloc(len);
FILE *fh;
if (!buf) return 0;
if (path) strcpy(buf,path);
else buf[0] = 0;
sm_add_part(buf,"pgp",len);
if ((fh = fopen(buf,"rb")))
{
fclose(fh);
strcat(buf," ");
} else strcpy(buf,"pgp ");
strcat(buf,options);
rc = sm_system(buf,output);
free(buf);
return rc;
}