Construct the server side early_data extension
[openssl.git] / util / shareable_image_wrap.c.in
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * This program allows for easy execution of programs in the OpenSSL build
12  * directory, in a manner that's similar to how util/shlib_wrap.sh.  Simply
13  * take the command you want to execute and prefix that with
14  * 'mcr [.util]shlib_wrap', for example:
15  *
16  * $ mcr [.util]shlib_wrap mcr [.apps]openssl s_client -connect www.openssl.org:443
17  */
18
19 #ifndef __VMS
20 # error "VMS ONLY!"
21 #endif
22
23 #include <stdio.h>
24 #include <descrip.h>
25 #include <ssdef.h>
26 #include <lib$routines.h>
27 {-
28     use File::Spec::Functions qw(rel2abs);
29     our $sv = sprintf "%02d%02d", $config{shlib_major}, $config{shlib_minor};
30     our $pz = $config{pointer_size};
31     our $bldd = rel2abs($config{builddir});
32     ""
33 -}
34 /* The logical name table we check and affect */
35 $DESCRIPTOR(lnm_process_table, "LNM$PROCESS_TABLE");
36
37 /* The first logical name we deal with, the buffer for its old value,
38  * and its temporary new value
39  */
40 const $DESCRIPTOR(lnm1, "OSSL$LIBCRYPTO{- $sv -}_SHR{- $pz -}");
41 char lnm1oldbuf[256]; short lnm1oldlen = 0;
42 $DESCRIPTOR(lnm1old, lnm1oldbuf);
43 const $DESCRIPTOR(lnm1new, "{- $bldd -}OSSL$LIBCRYPTO{- $sv -}_SHR{- $pz -}.EXE");
44
45 /* The second logical name we deal with, the buffer for its old value,
46  * and its temporary new value
47  */
48 const $DESCRIPTOR(lnm2, "OSSL$LIBSSL{- $sv -}_SHR{- $pz -}");
49 char lnm2oldbuf[256]; short lnm2oldlen = 0;
50 $DESCRIPTOR(lnm2old, lnm2oldbuf);
51 const $DESCRIPTOR(lnm2new, "{- $bldd -}OSSL$LIBSSL{- $sv -}_SHR{- $pz -}.EXE");
52
53 /* The foreign command we want to run with the logical names above set
54  * to their temporary values
55  */
56 char foreign_cmd_buf[4096]; short foreign_cmd_len = 0;
57 $DESCRIPTOR(foreign_cmd, foreign_cmd_buf);
58
59 int main()
60 {
61     int status = 0;
62     int lnm1status = 0, lnm2status = 0;
63
64     /* Fetch the command line.  lib$get_foreign() is nice enough to
65      * strip away the program name, thus only returning the arguments,
66      * which is exactly what we need.
67      */
68     lib$get_foreign(&foreign_cmd, 0, &foreign_cmd_len);
69     foreign_cmd.dsc$w_length = foreign_cmd_len;
70
71 #ifdef DEBUG
72     foreign_cmd_buf[foreign_cmd_len] = '\0';
73     printf("[%d] %s\n\n", foreign_cmd_len, foreign_cmd_buf);
74 #endif
75
76     /* Fetch the first logical name value and save the status */
77     lnm1status = lib$get_logical(&lnm1, &lnm1old, &lnm1oldlen,
78                                  &lnm_process_table);
79     if (lnm1status == SS$_NORMAL)
80         lnm1old.dsc$w_length = lnm1oldlen;
81     else if (lnm1status != SS$_NOLOGNAM)
82         return lnm1status;
83
84     /* Fetch the first logical name value and save the status */
85     lnm2status = lib$get_logical(&lnm2, &lnm2old, &lnm2oldlen,
86                                  &lnm_process_table);
87     if (lnm2status == SS$_NORMAL)
88         lnm2old.dsc$w_length = lnm2oldlen;
89     else if (lnm2status != SS$_NOLOGNAM)
90         return lnm2status;
91
92     /* Set the temporary new values for both logical names */
93     lib$set_logical(&lnm1, &lnm1new, &lnm_process_table);
94     lib$set_logical(&lnm2, &lnm2new, &lnm_process_table);
95
96     /* Execute the arguments as a command.  The better be a command! */
97     status = lib$spawn(&foreign_cmd);
98
99     /* If the logical names we set had old values, restore them.
100      * Otherwise, simply delete their current values.
101      */
102     if (lnm1status == SS$_NORMAL)
103         lib$set_logical(&lnm1, &lnm1old, &lnm_process_table);
104     else
105         lib$delete_logical(&lnm1, &lnm_process_table);
106     if (lnm2status == SS$_NORMAL)
107         lib$set_logical(&lnm2, &lnm2old, &lnm_process_table);
108     else
109         lib$delete_logical(&lnm2, &lnm_process_table);
110
111     /* Return the status from the execution of the foreign command */
112     return status;
113 }