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
  
396
  
397
  
/* 
 * $Id$ 
 * 
 * Create an initial administration interface server. 
 */ 
 
int mkdirhier(string from) 
{ 
  string a, b; 
  array f; 
 
  f=(from/"/"); 
  b=""; 
 
  foreach(f[0..sizeof(f)-2], a) 
  { 
    mkdir(b+a); 
    b+=a+"/"; 
  } 
} 
 
class Readline 
{ 
  inherit Stdio.Readline; 
 
  void trap_signal(int n) 
  { 
    werror("Interrupted, exit.\r\n"); 
    destruct(this_object()); 
    exit(1); 
  } 
 
  void destroy() 
  { 
    get_input_controller()->dumb = 0; 
    ::destroy(); 
    signal(signum("SIGINT")); 
  } 
 
    private string safe_value(string r) 
    { 
        if(!r) 
        { 
            /* C-d? */ 
            werror("\nTerminal closed, exit.\n"); 
            destruct(this_object()); 
            exit(1); 
        } 
         
        return r; 
    } 
     
  string read(mixed ... args) 
  { 
    return safe_value(::read(@args)); 
  } 
 
  string edit(mixed ... args) 
  { 
    return safe_value(::edit(@args)); 
  } 
   
  void create(mixed ... args) 
  { 
    signal(signum("SIGINT"), trap_signal); 
    ::create(@args); 
  } 
} 
 
mapping(string:string) batch; 
 
string read_string(Readline rl, string prompt, 
                   string|void batch_symbol, string|void def) 
{ 
  string res; 
  if (batch && batch_symbol) { 
    res = batch[batch_symbol] || def; 
    if (res) return res; 
  } 
 
  res = rl->edit(def || "", prompt+" ", ({ "bold" })); 
  if( def && !strlen(res-" ") ) 
    res = def; 
  return res; 
} 
 
#if constant(Crypto.Password) 
constant hash_password = Crypto.Password.hash; 
#else 
constant hash_password = Crypto.make_crypt_md5; 
#endif 
 
int main(int argc, array argv) 
{ 
  Readline rl = Readline(); 
  string name = "Administration Interface"; 
  string user = "administrator"; 
  string password, configdir, port; 
  string passwd2; 
 
#if constant( SSL ) 
  string def_port = "https://*:"+(random(20000)+10000)+"/"; 
#else 
  string def_port = "http://*:"+(random(20000)+10000)+"/"; 
#endif 
 
  //werror("Argv: ({%{%O, %}})\n", argv); 
 
  if(has_value(argv, "--help")) { 
    write(#" 
Creates and initializes a Roxen WebServer configuration 
interface. Arguments: 
 
 -d dir   The location of the configuration interface. 
          Defaults to \"../configurations\". 
 -a       Only create a new administration user. 
          Useful when the administration password is lost. 
 --help   Displays this text. 
 --batch  Create a configuration interface in batch mode. 
          The --batch argument should be followed by an optional 
          list of value pairs, each pair representing the name 
          of a question field and the value to be filled in. 
          Available fields: 
      server_name    The name of the server. Defaults to 
                     \"Administration Interface\". 
      server_url     The server url, e.g. \"http://*:1234/\". 
                     Defaults to \"https://*:22202/\" in batch mode. 
      user           The name of the administrator. 
                     Defaults to \"administrator\". 
      password       The administrator password. 
                     NB: No default; if not specified, no 
                     administration user will be created. 
      ok             Require interactive user confirmation of the 
                     above information with the value pair \"ok n\". 
 
Example of batch installation with interactive password entry: 
 
 ./create_configinterface --batch server_name Admin server_url \\ 
   http://*:8080/ ok y user admin 
 
"); 
    return 0; 
  } 
 
  configdir = 
   Getopt.find_option(argv, "d",({"config-dir","configuration-directory" }), 
                      ({ "ROXEN_CONFIGDIR", "CONFIGURATIONS" }), 
                      "../configurations"); 
  int admin = has_value(argv, "-a"); 
 
//    werror("Admin mode: %O\n" 
//     "Argv: ({%{%O, %}})\n", admin, argv); 
 
  int batch_args = search(argv, "--batch"); 
  if(batch_args>=0) 
    batch = (mapping)(argv[batch_args+1..]/2); 
 
  if (batch) { 
    if (!batch->server_url) { 
      batch->server_url = "https://*:22202/"; 
    } 
    if (batch["__semicolon_separated__"]) { 
      // Used by Win32Installer.vbs:CreateConfigInterface(). 
      array(string) sections = batch["__semicolon_separated__"]/";"; 
      if (sizeof(sections) < 6) { 
        error("Too few sections in __semicolon_separated__: %O.\n", 
              batch["__semicolon_separated__"]); 
      } 
      cd(sections[0]);                          // SERVERDIR 
      batch->server_name = sections[1];         // SERVER_NAME 
      batch->server_url = sprintf("%s://*:%s/", 
                                  sections[2],  // SERVER_PROTOCOL 
                                  sections[3]);     // SERVER_PORT 
      batch->user = sections[4];                        // ADM_USER 
      batch->password = sections[5..]*";";  // ADM_PASS1 
    } 
  } 
 
  foreach( get_dir( configdir )||({}), string cf ) 
    catch  
    { 
      if( cf[-1]!='~' && 
          search( Stdio.read_file( configdir+"/"+cf ),  
                  "'config_filesystem#0'" ) != -1 ) 
      { 
        string server_version = Stdio.read_file("VERSION"); 
        if(server_version) 
          Stdio.write_file(configdir+"/server_version","server-"+server_version); 
        werror("   There is already an administration interface present in " 
               "this\n   server. A new one will not be created.\n"); 
        if(!admin++) exit( 1 ); 
      } 
    }; 
  if(admin==1) { 
    werror("   No administration interface was found. A new one will be created.\n"); 
    admin = 0; 
  } 
  if(configdir[-1] != '/') 
    configdir+="/"; 
  if(admin) 
    write( "   Creating an administrator user.\n\n" ); 
  else 
    write( "   Creating an administration interface server in\n"+ 
           "   "+combine_path(getcwd(), configdir)+".\n"); 
 
  do 
  { 
    password = passwd2 = 0; 
     
    if(!admin)  
    { 
      write("\n"); 
      do { 
        if (!sizeof(name)) name = "Administration Interface"; 
        name = read_string(rl, "Server name:", "server_name", name); 
        if (batch) m_delete(batch, "server_name"); 
      } while (!sizeof(name)); 
 
      int port_ok; 
      while( !port_ok ) 
      { 
        string protocol = "https"; 
        string host, path; 
 
        port = read_string(rl, "Port URL:", "server_url", def_port); 
        if( port == def_port ) 
          ; 
        else if( (int)port ) 
        { 
          int ok; 
          while( !ok ) 
          { 
            switch( protocol = lower_case(read_string(rl, "Protocol:", "protocol", protocol))) 
            { 
             case "": 
               protocol = "https"; 
             case "http": 
             case "https": 
               port = protocol+"://*:"+port+"/"; 
               ok=1; 
               break; 
             default: 
               write("\n   Only http and https are supported for the " 
                     "configuration interface.\n"); 
               if (batch) m_delete(batch, "protocol"); 
               break; 
            } 
          } 
        } 
 
        if( sscanf( port, "%[^:]://%[^/]%s", protocol, host, path ) == 3) 
        { 
          if( path == "" ) 
            path = "/"; 
          switch( lower_case(protocol) ) 
          { 
           case "http": 
           case "https": 
             // Verify hostname here... 
             port = lower_case( protocol )+"://"+host+path; 
             port_ok = 1; 
             break; 
           default: 
             write("\n   Only http and https are supported for the " 
                   "configuration interface.\n\n"); 
             if (batch) m_delete(batch, "server_url"); 
             break; 
          } 
        } 
      } 
    } 
 
    // NB: Don't create a user if batch mode and no password. 
    if (!batch || batch->password) { 
      do 
      { 
        user = read_string(rl, "Administrator user name:", "user", user); 
        if (batch) m_delete(batch, "user"); 
      } while(((search(user, "/") != -1) || (search(user, "\\") != -1)) && 
              write("User name may not contain slashes.\n")); 
 
      do 
      { 
        if(passwd2 && password) 
          write("\n   Please select a password with one or more characters. " 
                "You will\n   be asked to type the password twice for " 
                "verification.\n\n"); 
        rl->get_input_controller()->dumb=1; 
        password = read_string(rl, "Administrator password:", "password"); 
        passwd2 = read_string(rl, "Administrator password (again):", "password"); 
        rl->get_input_controller()->dumb=0; 
        if(batch) m_delete(batch, "password"); 
        else 
          write("\n"); 
      } while(!strlen(password) || (password != passwd2)); 
    } 
 
    if (!batch || has_prefix(lower_case(batch->ok || ""), "n")) { 
      passwd2 = read_string(rl, "Are the settings above correct [Y/n]?", 0, ""); 
      if (has_prefix(lower_case(passwd2), "n")) { 
        // Exit batch mode and retry interactively. 
        batch = 0; 
        continue; 
      } 
    } 
    break; 
  } while(1); 
 
  if( !admin ) 
  { 
    mkdirhier( configdir ); 
    string server_version = Stdio.read_file("VERSION"); 
    if(server_version) 
      Stdio.write_file(configdir+"/server_version", "server-"+server_version); 
    Stdio.write_file( configdir+replace( name, " ", "_" ), 
                      replace( 
#" 
<!-- -*- html -*- --> 
<?XML version=\"1.0\"?> 
<roxen-config> 
 
<region name='EnabledModules'> 
  <var name='config_filesystem#0'> <int>1</int>  </var> <!-- Configration Filesystem --> 
</region> 
 
<region name='pikescript#0'> 
  <var name='trusted'><int>1</int></var> 
</region> 
 
<region name='graphic_text#0'> 
  <var name='colorparse'>        <int>1</int> </var> 
</region> 
 
<region name='contenttypes#0'> 
  <var name='_priority'>         <int>0</int> </var> 
  <var name='default'>           <str>application/octet-stream</str> </var> 
  <var name='exts'><str># This will include the defaults from a file. 
# Feel free to add to this, but do it after the #include line if 
# you want to override any defaults 
 
#include %3cetc/extensions%3e 
tag text/html 
xml text/html 
rad text/html 
ent text/html 
 
</str></var> 
</region> 
 
<region name='spider#0'> 
  <var name='Domain'> <str></str> </var> 
  <var name='MyWorldLocation'><str></str></var> 
  <var name='URLs'> <a> <str>$URL$#ip=;nobind=0;</str></a> </var> 
 
  <var name='comment'> 
    <str>Automatically created by create_configuration</str> 
  </var> 
  <var name='compat_level'> 
    <str>6.2</str> 
  </var> 
 
  <var name='name'> 
    <str>$NAME$</str> 
  </var> 
</region> 
 
</roxen-config>", 
 ({ "$NAME$", "$URL$" }), 
 ({ name, port }) )); 
    write("\n   Administration interface created.\n"); 
  } 
 
  if (password) { 
    string ufile=(configdir+"_configinterface/settings/" + user + "_uid"); 
    mkdirhier( ufile ); 
    Stdio.File( ufile, "wct", 0770 ) 
      ->write( 
string_to_utf8(#"<?xml version=\"1.0\"  encoding=\"utf-8\"?> 
<map> 
  <str>permissions</str> : <a> <str>Everything</str> </a> 
  <str>real_name</str>   : <str>Administrator</str> 
  <str>password</str>    : <str>" + hash_password(password) + #"</str> 
  <str>name</str>        : <str>" + user + #"</str> 
</map>\n" )); 
 
    write("\n   Administrator user \"" + user + "\" created.\n"); 
  } else { 
    write(#" 
 
   NOTE: No administration user has been created. 
         To create an administration user later; run 
 
           create_configinterface -a\n"); 
  } 
}