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
  
// This is a ChiliMoon protocol module. 
// Copyright © 2001, Roxen IS. 
 
// $Id: prot_https.pike,v 2.10 2004/04/04 14:24:53 mani Exp $ 
 
// --- Debug defines --- 
 
#ifdef SSL3_DEBUG 
# define SSL3_WERR(X) werror("SSL3: "+X+"\n") 
#else 
# define SSL3_WERR(X) 
#endif 
 
inherit SSLProtocol; 
 
constant supports_ipless = 0; 
constant name = "https"; 
constant prot_name = "https"; 
constant requesthandlerfile = "plugins/protocols/http.pike"; 
constant default_port = 443; 
 
 
class fallback_redirect_request 
{ 
  string in = ""; 
  string out; 
  string default_prefix; 
  int port; 
  Stdio.File f; 
 
  void die() 
  { 
    SSL3_WERR(sprintf("fallback_redirect_request::die()")); 
    f->set_blocking(); 
    f->close(); 
  } 
 
  void write_callback() 
  { 
    SSL3_WERR(sprintf("fallback_redirect_request::write_callback()")); 
    int written = f->write(out); 
    if (written <= 0) 
      die(); 
    else { 
      out = out[written..]; 
      if (!strlen(out)) 
        die(); 
    } 
  } 
 
  void read_callback(mixed ignored, string s) 
  { 
    SSL3_WERR(sprintf("fallback_redirect_request::read_callback(X, %O)\n", s)); 
    in += s; 
    string name; 
    string prefix; 
 
    if (has_value(in, "\r\n\r\n")) 
    { 
      //      werror("request = '%s'\n", in); 
      array(string) lines = in / "\r\n"; 
      array(string) req = replace(lines[0], "\t", " ") / " "; 
      if (sizeof(req) < 2) 
      { 
        out = "HTTP/1.0 400 Bad Request\r\n\r\n"; 
      } 
      else 
      { 
        if (sizeof(req) == 2) 
        { 
          name = req[1]; 
        } 
        else 
        { 
          name = req[1..sizeof(req)-2] * " "; 
          foreach(map(lines[1..], `/, ":"), array header) 
          { 
            if ( (sizeof(header) >= 2) && 
                 (lower_case(header[0]) == "host") ) 
              prefix = "https://" + header[1] - " "; 
          } 
        } 
        if (prefix) { 
          if (prefix[-1] == '/') 
            prefix = prefix[..strlen(prefix)-2]; 
          prefix = prefix + ":" + port; 
        } else { 
          /* default_prefix (aka MyWorldLocation) already contains the 
           * portnumber. 
           */ 
          if (!(prefix = default_prefix)) { 
            /* This case is most unlikely to occur, 
             * but better safe than sorry... 
             */ 
            string ip = (f->query_address(1)/" ")[0]; 
            prefix = "https://" + ip + ":" + port; 
          } else if (prefix[..4] == "http:") { 
            /* Broken MyWorldLocation -- fix. */ 
            prefix = "https:" + prefix[5..]; 
          } 
        } 
        out = sprintf("HTTP/1.0 301 Redirect to secure server\r\n" 
                      "Location: %s%s\r\n\r\n", prefix, name); 
      } 
      f->set_read_callback(0); 
      f->set_write_callback(write_callback); 
    } 
  } 
 
  void create(Stdio.File socket, string s, string l, int p) 
  { 
    SSL3_WERR(sprintf("fallback_redirect_request(X, %O, %O, %O)", s, l||"CONFIG PORT", p)); 
    f = socket; 
    default_prefix = l; 
    port = p; 
    f->set_nonblocking(read_callback, 0, die); 
    read_callback(f, s); 
  } 
 
  string _sprintf(int t) { 
    return t=='O' && sprintf("fallback_redirect_request(%O)", f); 
  } 
} 
 
class http_fallback 
{ 
  SSL.sslfile my_fd; 
 
  void ssl_alert_callback(object alert, object|int n, string data) 
  { 
    SSL3_WERR(sprintf("http_fallback(X, %O, %O)", n, data)); 
    //  trace(1); 
    if ( (my_fd->query_connection()->current_write_state->seq_num == 0) 
         && has_value(lower_case(data), "http")) 
    { 
      Stdio.File raw_fd = my_fd->shutdown(); 
 
      /* Redirect to a https-url */ 
      fallback_redirect_request(raw_fd, data, 
                                my_fd->config && 
                                my_fd->config->query("MyWorldLocation"), 
                                port); 
    } 
  } 
 
  void ssl_accept_callback(mixed ignored) 
  { 
    SSL3_WERR(sprintf("ssl_accept_callback(X)")); 
    my_fd->set_alert_callback(0); /* Forget about http_fallback */ 
    my_fd->set_accept_callback(0); 
    my_fd = 0;          /* Not needed any more */ 
  } 
 
  void create(SSL.sslfile fd) 
  { 
    my_fd = fd; 
    fd->set_alert_callback(ssl_alert_callback); 
    fd->set_accept_callback(ssl_accept_callback); 
  } 
 
  string _sprintf(int t) { 
    return t=='O' && sprintf("http_fallback(%O)", my_fd); 
  } 
} 
 
SSL.sslfile accept() 
{ 
  SSL.sslfile q = ::accept(); 
 
  if (q) { 
    http_fallback(q); 
  } 
  return q; 
} 
 
int set_cookie, set_cookie_only_once; 
void fix_cvars( Variable.Variable a ) 
{ 
  set_cookie = query( "set_cookie" ); 
  set_cookie_only_once = query( "set_cookie_only_once" ); 
} 
 
void create( mixed ... args ) 
{ 
  core.set_up_http_variables( this ); 
  if( variables[ "set_cookie" ] ) 
    variables[ "set_cookie" ]->set_changed_callback( fix_cvars ); 
  if( variables[ "set_cookie_only_once" ] ) 
    variables[ "set_cookie_only_once" ]->set_changed_callback( fix_cvars ); 
  fix_cvars(0); 
  ::create( @args ); 
}