aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Formula/stunnel.rb
blob: 1390a6fabaebb913527492c90c168a4485abb051 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
require 'formula'

class Stunnel < Formula
  url 'ftp://ftp.stunnel.org/stunnel/stunnel-4.36.tar.gz'
  homepage 'http://www.stunnel.org/'
  md5 '600a09b03798424842b24548ca1e4235'

  # This patch installs a bogus .pem in lieu of interactive cert generation.
  # It also patches str.c so it can be compiled on OS X:
  # https://groups.google.com/group/mailing.unix.stunnel-users/browse_thread/thread/043d6bc93fcb098b
  # This patch was provided by the author, and may not be needed in 4.37
  def patches
    DATA
  end

  def install
    system "./configure", "--disable-dependency-tracking",
                          "--disable-libwrap",
                          "--prefix=#{prefix}",
                          "--sysconfdir=#{etc}"
                          "--mandir=#{man}"
    system "make install"
  end

  def caveats
    <<-EOS.undent
      A bogus SSL server certificate has been installed to:
        #{etc}/stunnel/stunnel.pem

      This certificate will be used by default unless a config file says otherwise!

      In your stunnel configuration, specify a SSL certificate with
      the "cert =" option for each service.
    EOS
  end
end


__END__
diff --git a/tools/stunnel.cnf b/tools/stunnel.cnf
index 274f9a0..d5d7cc0 100644
--- a/tools/stunnel.cnf
+++ b/tools/stunnel.cnf
@@ -7,6 +7,7 @@ default_bits = 1024
 encrypt_key = yes
 distinguished_name = req_dn
 x509_extensions = cert_type
+prompt = no
 
 [ req_dn ]
 countryName = Country Name (2 letter code)

--- a/src/str.c	2011-05-02 19:14:53.000000000 +0200
+++ b/src/str.c	2011-05-10 17:34:40.000000000 +0200
@@ -38,17 +38,128 @@
 #include "common.h"
 #include "prototypes.h"
 
-typedef struct str_struct {
-    struct str_struct *prev, *next;
+#ifndef va_copy
+#ifdef __va_copy
+#define va_copy(dst, src) __va_copy((dst), (src))
+#else /* __va_copy */
+#define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list))
+#endif /* __va_copy */
+#endif /* va_copy */
+
+typedef struct alloc_list {
+    struct alloc_list *prev, *next;
     size_t size;
     unsigned int magic;
-} STR;
-static void str_set(STR *);
-static STR *str_get();
+} ALLOC_LIST;
+
+static void set_alloc_head(ALLOC_LIST *);
+static ALLOC_LIST *get_alloc_head();
+
+char *str_dup(const char *str) {
+    char *retval;
+
+    retval=str_alloc(strlen(str)+1);
+    if(retval)
+        strcpy(retval, str);
+    return retval;
+}
+
+char *str_printf(const char *format, ...) {
+    char *txt;
+    va_list arglist;
+
+    va_start(arglist, format);
+    txt=str_vprintf(format, arglist);
+    va_end(arglist);
+    return txt;
+}
+
+char *str_vprintf(const char *format, va_list start_ap) {
+    int n, size=64;
+    char *p, *np;
+    va_list ap;
+
+    p=str_alloc(size);
+    if(!p)
+        return NULL;
+    for(;;) {
+        va_copy(ap, start_ap);
+        n=vsnprintf(p, size, format, ap);
+        if(n>-1 && n<size)
+            return p;
+        if(n>-1)      /* glibc 2.1 */
+            size=n+1; /* precisely what is needed */
+        else          /* glibc 2.0, WIN32, etc. */
+            size*=2;  /* twice the old size */
+        np=str_realloc(p, size);
+        if(!np) {
+            str_free(p);
+            return NULL;
+        }
+        p=np; /* LOL */
+    }
+}
+
+#ifdef USE_UCONTEXT
+
+static ALLOC_LIST *alloc_tls=NULL;
+
+void str_init() {
+}
+
+static void set_alloc_head(ALLOC_LIST *alloc_head) {
+    if(ready_head)
+        ready_head->tls=alloc_head;
+    else /* ucontext threads not initialized */
+        alloc_tls=alloc_head;
+}
+
+static ALLOC_LIST *get_alloc_head() {
+    if(ready_head)
+        return ready_head->tls;
+    else /* ucontext threads not initialized */
+        return alloc_tls;
+}
+
+#endif /* USE_UCONTEXT */
+
+#ifdef USE_FORK
+
+static ALLOC_LIST *alloc_tls=NULL;
+
+void str_init() {
+}
+
+static void set_alloc_head(ALLOC_LIST *alloc_head) {
+    alloc_tls=alloc_head;
+}
+
+static ALLOC_LIST *get_alloc_head() {
+    return alloc_tls;
+}
+
+#endif /* USE_FORK */
+
+#ifdef USE_PTHREAD
+
+static pthread_key_t pthread_key;
+
+void str_init() {
+    pthread_key_create(&pthread_key, NULL);
+}
+
+static void set_alloc_head(ALLOC_LIST *alloc_head) {
+    pthread_setspecific(pthread_key, alloc_head);
+}
+
+static ALLOC_LIST *get_alloc_head() {
+    return pthread_getspecific(pthread_key);
+}
+
+#endif /* USE_PTHREAD */
 
 #ifdef USE_WIN32
 
-/* __thread does not work in mingw32 due to a bug in GCC */
 static DWORD tls_index; 
 
 void str_init() {
@@ -59,61 +170,43 @@
     }
 }
 
-static void str_set(STR *str) {
-    if(!TlsSetValue(tls_index, str)) {
+static void set_alloc_head(ALLOC_LIST *alloc_head) {
+    if(!TlsSetValue(tls_index, alloc_head)) {
         s_log(LOG_ERR, "TlsSetValue failed");
         die(1);
     }
 }
 
-static STR *str_get() {
-    STR *str;
+static ALLOC_LIST *get_alloc_head() {
+    ALLOC_LIST *alloc_head;
 
-    str=TlsGetValue(tls_index);
-    if(!str && GetLastError()!=ERROR_SUCCESS) {
+    alloc_head=TlsGetValue(tls_index);
+    if(!alloc_head && GetLastError()!=ERROR_SUCCESS) {
         s_log(LOG_ERR, "TlsGetValue failed");
         die(1);
     }
-    return str;
-}
-
-#else
-
-/* gcc Thread-Local Storage */
-static __thread STR *root_str=NULL;
-
-void str_init() {
-    if(root_str)
-        s_log(LOG_WARNING, "str_init: Non-empty allocation list");
+    return alloc_head;
 }
 
-static void str_set(STR *str) {
-    root_str=str;
-}
-
-static STR *str_get() {
-    return root_str;
-}
-
-#endif
+#endif /* USE_WIN32 */
 
 void str_cleanup() {
-    STR *str, *tmp;
+    ALLOC_LIST *alloc_head, *tmp;
 
-    str=str_get();
-    while(str) {
-        tmp=str;
-        str=tmp->next;
+    alloc_head=get_alloc_head();
+    while(alloc_head) {
+        tmp=alloc_head;
+        alloc_head=tmp->next;
         free(tmp);
     }
-    str_set(NULL);
+    set_alloc_head(NULL);
 }
 
 void str_stats() {
-    STR *tmp;
+    ALLOC_LIST *tmp;
     int blocks=0, bytes=0;
 
-    for(tmp=str_get(); tmp; tmp=tmp->next) {
+    for(tmp=get_alloc_head(); tmp; tmp=tmp->next) {
         ++blocks;
         bytes+=tmp->size;
     }
@@ -121,35 +214,37 @@
 }
 
 void *str_alloc(size_t size) {
-    STR *str, *tmp;
+    ALLOC_LIST *alloc_head, *tmp;
 
     if(size>=1024*1024) /* huge allocations are not allowed */
         return NULL;
-    tmp=calloc(1, sizeof(STR)+size);
+    tmp=calloc(1, sizeof(ALLOC_LIST)+size);
     if(!tmp)
         return NULL;
-    str=str_get();
+    alloc_head=get_alloc_head();
     tmp->prev=NULL;
-    tmp->next=str;
+    tmp->next=alloc_head;
     tmp->size=size;
     tmp->magic=0xdeadbeef;
-    if(str)
-        str->prev=tmp;
-    str_set(tmp);
+    if(alloc_head)
+        alloc_head->prev=tmp;
+    set_alloc_head(tmp);
     return tmp+1;
 }
 
 void *str_realloc(void *ptr, size_t size) {
-    STR *oldtmp, *tmp;
+    ALLOC_LIST *old_tmp, *tmp;
 
     if(!ptr)
         return str_alloc(size);
-    oldtmp=(STR *)ptr-1;
-    if(oldtmp->magic!=0xdeadbeef) { /* not allocated by str_alloc() */
+    old_tmp=(ALLOC_LIST *)ptr-1;
+    if(old_tmp->magic!=0xdeadbeef) { /* not allocated by str_alloc() */
         s_log(LOG_CRIT, "INTERNAL ERROR: str_realloc: Bad magic");
         die(1);
     }
-    tmp=realloc(oldtmp, sizeof(STR)+size);
+    if(size>=1024*1024) /* huge allocations are not allowed */
+        return NULL;
+    tmp=realloc(old_tmp, sizeof(ALLOC_LIST)+size);
     if(!tmp)
         return NULL;
     /* refresh all possibly invalidated pointers */
@@ -158,17 +253,17 @@
     if(tmp->prev)
         tmp->prev->next=tmp;
     tmp->size=size;
-    if(str_get()==oldtmp)
-        str_set(tmp);
+    if(get_alloc_head()==old_tmp)
+        set_alloc_head(tmp);
     return tmp+1;
 }
 
 void str_free(void *ptr) {
-    STR *tmp;
+    ALLOC_LIST *tmp;
 
     if(!ptr) /* do not attempt to free null pointers */
         return;
-    tmp=(STR *)ptr-1;
+    tmp=(ALLOC_LIST *)ptr-1;
     if(tmp->magic!=0xdeadbeef) { /* not allocated by str_alloc() */
         s_log(LOG_CRIT, "INTERNAL ERROR: str_free: Bad magic");
         die(1);
@@ -178,54 +273,9 @@
         tmp->next->prev=tmp->prev;
     if(tmp->prev)
         tmp->prev->next=tmp->next;
-    if(str_get()==tmp)
-        str_set(tmp->next);
+    if(get_alloc_head()==tmp)
+        set_alloc_head(tmp->next);
     free(tmp);
 }
 
-char *str_dup(const char *str) {
-    char *retval;
-
-    retval=str_alloc(strlen(str)+1);
-    if(retval)
-        strcpy(retval, str);
-    return retval;
-}
-
-char *str_vprintf(const char *format, va_list start_ap) {
-    int n, size=64;
-    char *p, *np;
-    va_list ap;
-
-    p=str_alloc(size);
-    if(!p)
-        return NULL;
-    for(;;) {
-        va_copy(ap, start_ap);
-        n=vsnprintf(p, size, format, ap);
-        if(n>-1 && n<size)
-            return p;
-        if(n>-1)      /* glibc 2.1 */
-            size=n+1; /* precisely what is needed */
-        else          /* glibc 2.0 */
-            size*=2;  /* twice the old size */
-        np=str_realloc(p, size);
-        if(!np) {
-            str_free(p);
-            return NULL;
-        }
-        p=np; /* LOL */
-    }
-}
-
-char *str_printf(const char *format, ...) {
-    char *txt;
-    va_list arglist;
-
-    va_start(arglist, format);
-    txt=str_vprintf(format, arglist);
-    va_end(arglist);
-    return txt;
-}
-
 /* end of str.c */