ucspi-ssl  0.12.7
ucspi-ssl
sslperl.c
Go to the documentation of this file.
1 #include <EXTERN.h>
2 #include <perl.h>
3 #include "exit.h"
4 #include "logmsg.h"
5 #include "stralloc.h"
6 #include "str.h"
7 #include "ucspissl.h"
8 
9 #ifndef eval_pv
10 #define eval_pv perl_eval_pv
11 #endif
12 
13 #ifndef call_argv
14 #define call_argv perl_call_argv
15 #endif
16 
17 extern char *Who = "PERL!";
18 
19 //extern const char *Who;
20 
21 /* ActiveState Perl requires this be called my_perl */
22 static PerlInterpreter *my_perl = 0;
23 
24 static void usage(void) {
25  logmsg(Who,100,USAGE,"sslargs file sub args");
26 }
27 
28 static stralloc newenv = {0};
29 static char *trivenv[] = { 0 };
30 static char **perlenv = trivenv;
31 static char **origenv = 0;
32 
33 void env_append(const char *c) {
34  if (!stralloc_append(&newenv,c))
35  logmsg(Who,111,FATAL,"out of memory");
36 }
37 
38 #define EXTERN_C extern
39 
40 EXTERN_C void xs_init() {
41 }
42 
43 void server(int argc,char **argv) {
44  char *prog[] = { "", *argv };
45  int i;
46  int j;
47  int split;
48  const char *x;
49 
50  ++argv; --argc;
51  if (!argv) usage();
52  if (!*argv) usage();
53 
54  origenv = environ;
55  environ = perlenv;
56 
57  if (!my_perl) {
58  my_perl = perl_alloc();
59  if (!my_perl) logmsg(Who,111,FATAL,"out of memory");
60  perl_construct(my_perl);
61  if (perl_parse(my_perl,xs_init,2,prog,trivenv))
62  logmsg(Who,111,FATAL,"perl_parse failed");
63 
64  if (perl_run(my_perl))
65  logmsg(Who,111,FATAL,"perl_run failed");
66  }
67 
68  if (!stralloc_copys(&newenv,"%ENV=("))
69  logmsg(Who,111,FATAL,"out of memory");
70 
71  for (i = 0; origenv[i]; ++i) {
72  x = origenv[i];
73  if (!x) continue;
74  split = str_chr(x,'=');
75  env_append("'");
76  for (j = 0; j < split; ++j) {
77  if (*x == '\'' || *x == '\\') env_append("\\");
78  env_append(x++);
79  }
80  env_append("'");
81  env_append(",");
82  env_append("'");
83  if (*x == '=') ++x;
84  while (*x) {
85  if (*x == '\'' || *x == '\\') env_append("\\");
86  env_append(x++);
87  }
88  env_append("'");
89  env_append(",");
90  }
91  env_append(")");
92  env_append("\0");
93 
94  ENTER;
95  SAVETMPS;
96  eval_pv(newenv.s,TRUE);
97  FREETMPS;
98  LEAVE;
99 
100  if (call_argv(*argv,G_VOID|G_DISCARD,argv + 1))
101  logmsg(Who,111,FATAL,"interpreter failed");
102 
103  perlenv = environ;
104  environ = origenv;
105 }
void usage(void)
Definition: sslclient.c:46
char * Who
EXTERN_C void xs_init()
Definition: sslperl.c:40
#define call_argv
Definition: sslperl.c:14
#define EXTERN_C
Definition: sslperl.c:38
void server(int argc, char **argv)
Definition: sslperl.c:43
void env_append(const char *c)
Definition: sslperl.c:33
#define eval_pv
Definition: sslperl.c:10
char *const * prog
Definition: sslserver.c:186
Header file to be used with sqmail; previously called ssl.h. (name clash)