forked from JasonAlt/UberFTP
-
Notifications
You must be signed in to change notification settings - Fork 2
/
errcode.c
269 lines (228 loc) · 6.19 KB
/
errcode.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
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
/*
* University of Illinois/NCSA Open Source License
*
* Copyright (c) 2003-2012 NCSA. All rights reserved.
*
* Developed by:
*
* Storage Enabling Technologies (SET)
*
* Nation Center for Supercomputing Applications (NCSA)
*
* http://dims.ncsa.uiuc.edu/set/uberftp
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the .Software.),
* to deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* + Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* + Neither the names of SET, NCSA
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED .AS IS., WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*/
#include <stdlib.h>
#include <gssapi.h>
#include <stdarg.h>
#include <stdio.h>
#include "settings.h"
#include "errcode.h"
#include "misc.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif /* DMALLOC */
struct errcode_s {
int refs;
int code;
int flags;
char ** errmsg;
OM_uint32 major;
OM_uint32 minor;
};
errcode_t
ec_create(OM_uint32 major,
OM_uint32 minor,
char * fmt,
...)
{
int len = 0;
int ret = 0;
va_list ap;
errcode_t errcode = (errcode_t) malloc(sizeof(struct errcode_s));
errcode->refs = 1;
errcode->errmsg = NULL;
errcode->major = major;
errcode->minor = minor;
errcode->flags = 0;
/* Now the user friendly message. */
if (fmt != NULL)
{
/*
* Initialize since Tru64 has a problem with it being NULL.
*/
len = 128;
errcode->errmsg = (char **) malloc(2*sizeof(char *));
errcode->errmsg[0] = malloc(len);
errcode->errmsg[1] = NULL;
while(1)
{
/* This will calculate the length. */
va_start (ap, fmt);
ret = vsnprintf(errcode->errmsg[0], len, fmt, ap);
va_end(ap);
if (ret == -1)
len += 128;
else if (ret < len)
break;
else
len = ret + 1;
errcode->errmsg[0] = (char *) realloc(errcode->errmsg[0], len);
}
}
return errcode;
}
void
ec_prepend_msg(errcode_t errcode, char * fmt, ...)
{
int len = 0;
int ret = 0;
int index = 0;
va_list ap;
for (index = 0; errcode->errmsg[index]; index++);
errcode->errmsg = (char **)realloc(errcode->errmsg,(index+2)*sizeof(char*));
errcode->errmsg[index + 1] = NULL;
if (fmt != NULL)
{
/*
* Initialize since Tru64 has a problem with it being NULL.
*/
len = 128;
errcode->errmsg[index] = malloc(len);
while(1)
{
/* This will calculate the length. */
va_start (ap, fmt);
ret = vsnprintf(errcode->errmsg[index], len, fmt, ap);
va_end(ap);
if (ret == -1)
len += 128;
else if (ret < len)
break;
else
len = ret + 1;
errcode->errmsg[index] = (char *) realloc(errcode->errmsg[index], len);
}
}
}
void
ec_set_flag(errcode_t errcode, int flag)
{
errcode->flags |= flag;
}
int
ec_retry(errcode_t errcode)
{
if (!errcode)
return 0;
if (errcode->flags & EC_FLAG_SHOULD_RETRY)
return 1;
if ((errcode->flags & EC_FLAG_CAN_RETRY) && s_retry())
return 1;
return 0;
}
void
ec_destroy(errcode_t errcode)
{
int c = 0;
if (errcode == EC_SUCCESS)
return;
if (errcode->refs-- > 1)
return;
for (c = 0; errcode->errmsg != NULL && errcode->errmsg[c] != NULL; c++)
FREE(errcode->errmsg[c]);
if (errcode->errmsg != NULL)
FREE(errcode->errmsg);
FREE(errcode);
return;
}
void
ec_print(errcode_t errcode)
{
int c = 0;
OM_uint32 mcntxt = 0;
OM_uint32 maj = GSS_S_COMPLETE;
OM_uint32 min = GSS_S_COMPLETE;
gss_buffer_desc msg;
if (errcode == EC_SUCCESS)
return;
/* Print in reverse order */
for (c = 0; errcode->errmsg != NULL && errcode->errmsg[c] != NULL; c++);
for (c--; errcode->errmsg != NULL && c >= 0; c--)
{
fprintf(stderr, "%s", errcode->errmsg[c]);
if (*(errcode->errmsg[c] + strlen(errcode->errmsg[c]) - 1) != '\n')
fprintf(stderr, "\n");
}
if (errcode->major != GSS_S_COMPLETE)
{
for (mcntxt = 0; !mcntxt;)
{
maj = gss_display_status(&min,
errcode->major,
GSS_C_GSS_CODE,
GSS_C_NULL_OID,
&mcntxt,
&msg);
if (maj == GSS_S_COMPLETE || maj == GSS_S_CONTINUE_NEEDED)
{
if (msg.value)
fprintf(stderr, "%s\n", (char *)msg.value);
}
gss_release_buffer(&min, &msg);
if (maj != GSS_S_CONTINUE_NEEDED)
break;
}
for (mcntxt = 0; !mcntxt;)
{
maj = gss_display_status(&min,
errcode->minor,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&mcntxt,
&msg);
if (maj == GSS_S_COMPLETE || maj == GSS_S_CONTINUE_NEEDED)
{
if (msg.value)
fprintf(stderr, "%s\n", (char *)msg.value);
}
gss_release_buffer(&min, &msg);
if (maj != GSS_S_CONTINUE_NEEDED)
break;
}
}
fflush(stderr);
}
errcode_t
ec_dup(errcode_t ec)
{
if (ec != EC_SUCCESS)
ec->refs++;
return ec;
}