Skip to content
This repository
tag: v144
Fetching contributors…

Cannot retrieve contributors at this time

file 628 lines (566 sloc) 17.965 kb
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
-generate server (* -*-erlang-*- *)

%
% Copyright © 2011 MLstate
%
% This file is part of OPA.
%
% OPA is free software: you can redistribute it and/or modify it under the
% terms of the GNU Affero General Public License, version 3, as published by
% the Free Software Foundation.
%
% OPA is distributed in the hope that it will be useful, but WITHOUT ANY
% WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
% FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
% more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with OPA. If not, see <http://www.gnu.org/licenses/>.
%
-debugvar PROTOCOL_DEBUG
-protocol FTP

% ftpServer:
% o Conforms to RFC1123 updated minimal server requirements.
%
  
% TODO: (-=todo, /=partial, +=done)
% o RFC959
% + USER <SP> <username> <CRLF>
% + PASS <SP> <password> <CRLF>
% + ACCT <SP> <account-information> <CRLF>
% + CWD <SP> <pathname> <CRLF>
% + CDUP <CRLF>
% - SMNT <SP> <pathname> <CRLF>
% + QUIT <CRLF>
% - REIN <CRLF>
% + PORT <SP> <host-port> <CRLF>
% + PASV <CRLF>
% / TYPE <SP> <type-code> <CRLF>
% / STRU <SP> <structure-code> <CRLF>
% / MODE <SP> <mode-code> <CRLF>
% + RETR <SP> <pathname> <CRLF>
% + STOR <SP> <pathname> <CRLF>
% + STOU <CRLF>
% + APPE <SP> <pathname> <CRLF>
% + ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
% + REST <SP> <marker> <CRLF>
% + RNFR <SP> <pathname> <CRLF>
% + RNTO <SP> <pathname> <CRLF>
% - ABOR <CRLF>
% + DELE <SP> <pathname> <CRLF>
% + RMD <SP> <pathname> <CRLF>
% + MKD <SP> <pathname> <CRLF>
% + PWD <CRLF>
% + LIST [<SP> <pathname>] <CRLF>
% + NLST [<SP> <pathname>] <CRLF>
% - SITE <SP> <string> <CRLF>
% + SYST <CRLF>
% / STAT [<SP> <pathname>] <CRLF>
% + HELP [<SP> <string>] <CRLF>
% + NOOP <CRLF>
% o RFC2389
% - FEAT <CRLF>
% - OPTS <SP> command-name [ <SP> command-options ] <CRLF>
% o RFC3659
% / SIZE <SP> <pathname> <CRLF>
% / MDTM <SP> <pathname> <CRLF>
% - REST
% - TVFS
% - MLST
% - MLSD
% o all commands in no login
% o records
% o stat for files/dirs
% o check for RFC1123 errata compliance

-open Printf
-open FtpServerType

-include "libnet/ftpResponses.proto"
-include "libnet/ftpMessages.proto"
-type payload = unit
-include "libnet/rt_proto.proto"

-type runtime = {
    rt_plim : int;
    rt_dialog_name : string;
    rt_on_close : Scheduler.t -> unit;
    rt_proto : rt_proto;
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% General states %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+on_connection(state : FtpServerType.state):
  {{ Printf.eprintf "on_connection\n" }}
% listen ({{ (Network.make_port_spec (Unix.inet_addr_of_string "127.0.0.1") 12345 Network.Unsecured) }}, listen_port2) ;
  let hello = {{ get_hello_message state }}
  send(Dummy hello);
  wait_for_login(state)

wait_for_login(state):
  receive
  | User str ->
    if {{ str = "admin" }} then
       let state = {{ {state with is_admin = true; user = Some str} }}
       send(ReqPass str);
       wait_for_pass(state)
    else
       let state = {{ {state with is_admin = false; user = Some str} }}
       send(OkLogin);
       wait_for_request(state)
  | Syst ->
        {{ Printf.eprintf "Syst\n%!" }}
        send(OkSyst);
        wait_for_login(state)
  | Quit ->
        let str = {{ state.goodbye_message }}
        send(Bye str);
        -!-
  | msg ->
       {{ Printf.eprintf "Error %s\n%!" (string_of_msg msg) }}
       send(ErrLogin);
       wait_for_login(state)
  after state.timeout ->
       send(ErrTime);
       let str = {{ state.goodbye_message }}
       send(Bye str);
       -!-

wait_for_pass(state):
  receive
  | Pass str ->
    if {{ (str = "mlstate") }}
    then
      send(OkLogin);
      let state = {{ {state with is_admin = true} }}
      let _ = {{ cwd state state.default_folder }}
      wait_for_request(state)
    else
     send(ErrPass);
     let state = {{ {state with is_admin = false; user = None} }}
     wait_for_login(state)
  | _ -> send(ErrSyntax);
         wait_for_login(state)
  after state.timeout ->
send(ErrTime);
         wait_for_login(state);

% process_nlst:
% Open a connection to the current data_port_spec in the state and
% read the given directory's content. Dump a list of files
% down the link turning \n into \r\n if in ASCII mode.
%
process_nlst(state,str,list_fn):
  let nlst_ok = {{ list state.folder str list_fn }}
  let nlst = {{ fst nlst_ok }}
  if {{ snd nlst_ok }} then
    if {{ state.passive }}
    then
      send(DirList);
      match {{ !state.pasv_port_conn }} with
      | Some conn2 ->
        writeconn(conn2,nlst);
        {{ Scheduler.remove_connection sched conn2 }}
        {{ state.pasv_port_conn := None }}
        send(OkDir);
        wait_for_request(state)
      | None ->
        {{ prerr_endline("process_nlst: no pasv connection") }}
     send(ErrNoFact);
     wait_for_request(state)
      ;
    else
      connect({{state.data_port_spec,state.data_secure_mode}},process_nlst_pasv,state,nlst)
      %let conn2 = connect(state.data_port_spec,Network.Unsecured);
      %let state = {{ {state with data_conn = Some conn2} }}
      %send(DirList);
      %writeconn(conn2,nlst);
      %send(OkDir);
      %{{ Scheduler.remove_connection sched conn2 }}
      %let state = {{ {state with data_conn = None} }}
      %wait_for_request(state)
  else
    send(ErrNoFact);
    wait_for_request(state)
  end

process_nlst_pasv(state,nlst,conn2):
  send(DirList);
  writeconn(conn2,nlst);
  send(OkDir);
  {{ Scheduler.remove_connection sched conn2 }}
  let state = {{ {state with data_conn = None} }}
  wait_for_request(state)

% output_ascii_file:
% Given a connection and a filename, dump the file contents down the link. We
% need to take care with the RFC 959 data conventions. Currently, we only do
% crlf conversion. The blocksize is set in the state.
output_ascii_file(state,str,conn2):
  match {{ open_folder_read state str }} with
  | Some fd ->
    let bin = {{ get_binary_mode state }}
    send(BinConn bin);
    output_ascii_file_n(state,conn2,fd)
  | None ->
    send(ErrFailOpen);
    wait_for_request(state)

output_ascii_file_n(state,conn2,fd):
  match {{ read_folder state fd state.data_blocksize }} with
  | Some "" ->
    {{ close_folder fd }}
    {{ Scheduler.remove_connection sched conn2 }}
    let state = {{ {state with data_conn = None} }}
    send(OkFile "send");
    wait_for_request(state)
  | Some data ->
    writeconn(conn2,data);
    output_ascii_file_n(state,conn2,fd)
  | None ->
    send(ErrActNot);
    wait_for_request(state)

% Send file to data port
receive_file(state,str):
  if {{ state.passive }}
  then
    match {{ !(state.pasv_port_conn) }} with
    | Some conn2 ->
        output_ascii_file(state,str,conn2)
    | None ->
        {{ prerr_endline("receive_file: no pasv connection") }}
     send(ErrNoFact);
  else
    connect({{state.data_port_spec,state.data_secure_mode}},output_ascii_file,state,str)
    %let conn2 = connect(state.data_port_spec,Network.Unsecured)
    %let state = {{ {state with data_conn = Some conn2} }}
    %output_ascii_file(state,conn2,str)

% input_ascii_file:
% Given a connection and a filename, read the file contents from the link. As
% for output, we need to conform to the RFC 959 data conventions. Currently,
% we only do crlf conversion. The blocksize is also set in the state.
input_ascii_file(state,str,append,conn2):
  let fd = {{ (if append then open_folder_append else open_folder_write) state str }}
  %let bin = {{ get_binary_mode state }}
  %send(BinConn bin);
  send(OkFileConn str); % See RFC1123 4.1.2.9
  input_ascii_file_n(state,conn2,str,fd)

input_ascii_file_n(state,conn2,str,fd):
  let cnt_buff = readconn(conn2,state.data_blocksize);
  let cnt = {{ fst cnt_buff }}
  if {{ cnt > 0 }}
  then
    let buff = {{ snd cnt_buff }}
    let _ = {{ write_folder state fd buff cnt }}
    input_ascii_file_n(state,conn2,str,fd)
  else
    {{ close_folder fd }}
    {{ Scheduler.remove_connection sched conn2 }}
    let state = {{ {state with data_conn = None} }}
    let msg = {{ (sprintf "%s received" str) }}
    send(OkFile msg);
    wait_for_request(state)

% Stuff incoming data into the named file
store_file(state,str,append):
  if {{ state.passive }}
  then
    match {{ !(state.pasv_port_conn) }} with
    | Some conn2 ->
        input_ascii_file(state,str,append,conn2)
    | None ->
        {{ prerr_endline("store_file: no pasv connection") }}
     send(ErrNoFact);
  else
    connect({{state.data_port_spec,state.data_secure_mode}},input_ascii_file,state,str,append)
    %let conn2 = connect(state.data_port_spec,Network.Unsecured)
    %let state = {{ {state with data_conn = Some conn2} }}
    %input_ascii_file(state,conn2,str,append)

% Change directory
change_dir(state,str):
  if {{ valid_folder state str }}
  then
     if {{ cwd state str }}
     then
       let str = {{ pwd state }}
       send(OkCwd str);
       wait_for_request(state)
     else
       send(ErrCwd);
       wait_for_request(state)
  else
     send(ErrCwd);
     wait_for_request(state)

% Construct a standard help message
transmit_help(state):
  send(OkHelp1);
  let msg = {{ recognized_commands 10 }}
  send(Dummy msg);
  send(OkHelp2);
  wait_for_request(state)

% Generalised file/dir action
file_action(state,pathname,action):
  if {{ valid_folder state pathname }}
  then
    if {{ action state pathname }}
    then
      send(OkFileAct);
      wait_for_request(state)
    else
      send(ErrNoFact);
      wait_for_request(state)
  else
    send(ErrPerm);
    wait_for_request(state)

wait_for_request(state):
  let state = {{ {state with rename_string=None; start_position=0} }}
  do_wait_for_request(state)

do_wait_for_request(state):
  receive

  % Send list of files - currently not implemented
  | List ->
        let str = {{ Filename.current_dir_name }}
        process_nlst(state, str, ls_file)
  | ListS str ->
        process_nlst(state, str, ls_file)

  % Return directory listing
  | Nlst ->
        let str = {{ Filename.current_dir_name }}
        process_nlst(state, str, plain_file)
  | NlstS str ->
        process_nlst(state, str, plain_file)

  % Change current directory
  | Cwd str ->
        if {{ str = "/" }} % <- explicit_path this !!!
        then
          let str = {{ state.default_folder }}
          change_dir(state,str)
        else
          change_dir(state,str)

  % Change current directory
  | Cwd0 ->
        let str = {{ Filename.current_dir_name }}
        change_dir(state,str)

  % Change current directory
  | CwdX ->
        let str = {{ Filename.current_dir_name }}
        change_dir(state,str)

  % Change to parent directory
  | Cdup ->
        let str = {{ Filename.parent_dir_name }}
        change_dir(state,str)

  % Return current directory
  | Pwd ->
        let s = {{ pwd state }}
        send(OkPwd s);
        wait_for_request(state)

  % Go into passive mode
  | Pasv ->
        match {{ get_passive_port state sched }} with
| (state, Some (portstr,port_spec_opt,sec_mode_opt)) ->
          let port_sec_opt = {{
            match port_spec_opt,sec_mode_opt with
             | ((Some port_spec),(Some sec_mode)) -> Some (port_spec,sec_mode)
             | ((Some port_spec),None) -> Some (port_spec,Network.Unsecured)
             | (None,_) -> None
          }}
          begin
            match {{ port_sec_opt }} with
               | Some port_sec ->
                   let _key = listen({{port_sec}},set_pasv_port_conn,state) ;
send(OkPasv portstr);
let state = {{ { state with passive = true } }}
{{ prerr_endline (sprintf "Passive mode %b" state.passive) }}
                   wait_for_request(state)
               | None ->
{{ prerr_endline (sprintf "Couldn't enter passive mode\n") }}
                 wait_for_request(state)
          end
        | (state, None) ->
          % What do we send here??? Nothing in RFC959
{{ prerr_endline (sprintf "Couldn't enter passive mode\n") }}
          wait_for_request(state)
        ;

  % Define the data port, addr and port no.
  | Port str ->
let state_ok = {{ set_port state str }}
        let state = {{ fst state_ok }}
        if {{ snd state_ok }}
        then
          send(OkCmd "Port Command Accepted");
          wait_for_request(state)
else
          send(ErrSyntax2);
          wait_for_request(state)
end

  % Set the transfer type
  | Type str ->
let state_msg = {{ set_type state str }}
        let state = {{ fst state_msg }}
let msg = {{ snd state_msg }}
   if {{ msg = "200" }} then
send(OkCmd "Command okay.");
          wait_for_request(state)
else if {{ msg = "501" }} then
          send(ErrSyntax2);
          wait_for_request(state)
else
          send(ErrNoimp);
          wait_for_request(state)

  % Set the data structure
  | Stru str ->
let state_msg = {{ set_structure_code state str }}
        let state = {{ fst state_msg }}
let msg = {{ snd state_msg }}
   if {{ msg = "200" }} then
send(OkCmd "Command okay.");
          wait_for_request(state)
else if {{ msg = "501" }} then
          send(ErrSyntax2);
          wait_for_request(state)
else
          send(ErrNoimp);
          wait_for_request(state)

  % Set the transfer mode
  | Mode str ->
        match {{ set_transfer_mode state str }} with
| (state,"200") ->
send(OkCmd "Command okay.");
          wait_for_request(state)
| (state,"501") ->
          send(ErrSyntax2);
          wait_for_request(state)
| (state,_) ->
          send(ErrNoimp);
          wait_for_request(state)
        ;

  % Send file down data port
  | Retr str ->
        receive_file(state,str)

  % An RETR with no argument, don't look at me, firefox does this !!!
  | RetrX ->
        send(ErrFailOpen);
        wait_for_request(state)

  % Read file from data port
  | Stor str ->
        store_file(state,str,false)

  % Append file read from data port
  | Appe str ->
        store_file(state,str,true)

  % Read unique file from data port
  % RFC959 says no argument but some ftps seem to send one anyway...
  | Stou ->
let str = {{ get_unique_filename (Folder.current_folder state.folder) "ftp_" "" }}
store_file(state,str,false)
  | StouS str1 ->
let str = {{ get_unique_filename (Folder.current_folder state.folder) (str1^"_") "" }}
store_file(state,str,false)

  | Rest str ->
        match {{ set_start_position state str }} with
        | (state,numstr) ->
            send(ReqRest numstr);
            do_wait_for_request(state)
        ;

  % Rename file, part I
  | Rnfr str ->
        if {{ valid_folder state str && writable_folder state str }}
        then
          let state = {{ {state with rename_string=Some str} }}
          send(ReqPend);
          do_wait_for_request(state)
        else
          send(ErrPerm);
          wait_for_request(state)

  % Rename file, part II
  | Rnto str ->
        if {{ Option.is_some(state.rename_string) }}
        then
          if {{ valid_folder state str }}
          then
            if {{ rename_folder state (Option.get state.rename_string) str }}
            then
              send(OkFileAct);
              wait_for_request(state)
            else
              send(ErrPerm);
              wait_for_request(state)
          else
            send(ErrPerm);
            wait_for_request(state)
        else
          send(ErrBadSeq);
          wait_for_request(state)

  % Delete file
  | Dele str ->
        file_action(state,str,delete_folder)

  % Create directory.
  | Mkd str ->
        file_action(state,str,create_directory)

  % Create directory.*)
  | Rmd str ->
        file_action(state,str,delete_directory)

  % System type, just send the usual UNIX one.
  | Syst ->
        send(OkSyst);
        wait_for_request(state)

  % System type, just send the usual UNIX one.
  | Stat ->
        send(OkStat211a);
        let statstr = {{ server_status state conn }}
        send(Dummy statstr);
        send(OkStat211b);
        wait_for_request(state)

  % Seems to be redundant
  | Allo1 ->
        send(OkAllo);
        wait_for_request(state)
  | Allo2 _ ->
        send(OkAllo);
        wait_for_request(state)
  | Allo3 _ ->
        send(OkAllo);
        wait_for_request(state)

  % File size, with no argument???
  | Size ->
        send(ErrNoSize);
        wait_for_request(state)

  % File size, as it would be transmitted
  | SizeS _ ->
        send(ErrNoSize);
        wait_for_request(state)

  % File mdtm, with no argument???
  | Mdtm ->
        send(ErrNoMdtm);
        wait_for_request(state)

  % File modification time
  | MdtmS _ ->
        send(ErrNoMdtm);
        wait_for_request(state)

  % Help string, just send implemented commands.
  | Help ->
        transmit_help(state)

  | HelpS _ ->
        transmit_help(state)

  % Account, not implemented.
  | Acct _ ->
        send(ErrAcct);
        wait_for_request(state)

  % Do nothing, but it's in the minimal spec.
  | Noop ->
        send(OkCmd "Command okay.");
        wait_for_request(state)

  % Terminate the current connection
  | Quit ->
        let str = {{ state.goodbye_message }}
        send(Bye str);
        -!-

  % Catchall, we need this to pick up bad commands
  | _ ->
        send(ErrSyntax);
        wait_for_request(state)

   % Timeout
   after state.timeout ->
send(ErrTime);
        let str = {{ state.goodbye_message }}
send(Bye str);
-!-

set_pasv_port_conn(state):
  let _ = {{ sched }}
  {{ state.pasv_port_conn := Some conn }}

%listen_port2(state):
% receive
% | Other str ->
% {{ prerr_endline (sprintf "listen_port2: received %s" str) }}
% -!-
% catch
% | exn ->
% {{ prerr_endline (sprintf "listen_port2: caught %s" (Printexc.to_string exn)) }}

% end of file
Something went wrong with that request. Please try again.