summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libnetworking/rtems_servers/ftpd.c
blob: 121a5b7762a39d6892ba662069ac64654af6536b (plain) (blame)
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/*
 *  FTP Server Daemon
 *
 *  Submitted by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
 *
 *  $Id$
 */

/*
 * Current state:
 *   To untar, put as "untar"
 * CWD uses chdir
 *   This is bad due to global setting of chdir.
 *
 * Stored files come into RAM and are saved later.  This is an artifact
 *   of a previous implementation (no filesystem -- had to do stuff with
 *   the "files" later).  This can be eliminated once all of Jake's stuff
 *   is moved to devices/filesystems.
 *
 * CLOSE(S) doesn't seem to work.  This causes problems in 
 *          several areas.  It lets too many file descriptors pile up
 *          and it doesn't seem to flush the stream.
 *
 * Is 'recv' what I want to use to get commands from the control port?
 *
 */

/**************************************************************************
 *                                 ftpd.c                                 *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This file contains the daemon which services requests that appear   *
 *    on the 'FTP' port.  This server is compatible with FTP, but it      *
 *    also provides services specific to the Erithacus system.            *
 *    This server is started at boot-time and runs forever.               *
 *                                                                        *
 *    Organization:                                                       *
 *                                                                        *
 *       The FTP daemon is started upon boot.  It runs all the time       *
 *       and waits for connections on the known FTP port (21).  When      *
 *       a connection is made, it starts a 'session' task.  That          *
 *       session then interacts with the remote host.  When the session   *
 *       is complete, the session task deletes itself.  The daemon still  *
 *       runs, however.                                                   *
 *                                                                        *
 *    Implementation Notes:                                               *
 *                                                                        *
 *       The 'current working directory' implementation utilizes the      *
 *       RTEMS filesystem cwd.  This is no good since other processes     *
 *       inherit the same cwd.                                            *
 *                                                                        *
 *                                                                        *
 * Supported commands are:                                                *
 *                                                                        *
 * RETR xxx     - Sends a file from the client.                           *
 * STOR xxx     - Receives a file from the client.  xxx = filename.       *
 * LIST xxx     - Sends a file list to the client.                        *
 *                (LIST xxx isn't working yet...)                         *
 * USER         - Does nothing.                                           *
 * PASS         - Does nothing.                                           *
 * SYST         - Replies with the system type (`RTEMS').                 *
 * DELE xxx     - Delete file xxx.                                        *
 * MKD xxx      - Create directory xxx.                                   *
 * RMD xxx      - Remove directory xxx.                                   *
 * PWD          - Print working directory.                                *
 * CWD xxx      - Change working directory.                               *
 * SITE CHMOD xxx yyy - Change permissions on file yyy to xxx.            *
 * PORT a,b,c,d,x,y   - Setup for a data port to IP address a.b.c.d       *
 *                      and port (x*256 + y).                             *
 *                                                                        *
 *                                                                        *
 *                                                                        *
 * The public routines contained in this file are:                        *
 *                                                                        *
 *    FTPD_Start - Starts the server daemon, then returns to its caller.  *
 *                                                                        *
 *                                                                        *
 * The private routines contained in this file are:                       *
 *                                                                        *
 *    FTPD_SendReply    - Sends a reply code and text through the control *
 *                        port.                                           *
 *    FTPD_CommandStore - Performs the "STOR" command.                    *
 *    FTPD_CommandList  - Performs the "LIST" command.                    *
 *    FTPD_CommandPort  - Opens a data port (the "PORT" command).         *
 *    FTPD_ParseCommand - Parses an incoming command.                     *
 *    FTPD_Session      - Begins a service session.                       *
 *    FTPD_Daemon       - Listens on the FTP port for service requests.   *
 *                                                                        *
 *                                                                        *
 *------------------------------------------------------------------------*
 *                                                                        *
 * Jake Janovetz                                                          *
 * University of Illinois                                                 *
 * 1406 West Green Street                                                 *
 * Urbana IL  61801                                                       *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/

/* Revision Control Information:
 *
 * $Source$
 * $Id$
 * $Log$
 * Revision 1.3  1998/05/19 21:28:17  erithacus
 * Update control socket to file I/O.
 *
 * Revision 1.2  1998/05/19 20:13:50  erithacus
 * Remodeled to be entirely reentrant.
 *
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>

#include <rtems.h>
#include <rtems/rtems_bsdnet.h>
#include <rtems/error.h>
#include <syslog.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/ftp.h>
#include <netinet/in.h>

#include "ftpd.h"
#include "untar.h"


/**************************************************************************
 * Meanings of first and second digits of reply codes:
 *
 * Reply:  Description:
 *-------- --------------
 *  1yz    Positive preliminary reply.  The action is being started but
 *         expect another reply before sending another command.
 *  2yz    Positive completion reply.  A new command can be sent.
 *  3yz    Positive intermediate reply.  The command has been accpeted
 *         but another command must be sent.
 *  4yz    Transient negative completion reply.  The requested action did
 *         not take place, but the error condition is temporary so the
 *         command can be reissued later.
 *  5yz    Permanent negative completion reply.  The command was not  
 *         accepted and should not be retried.
 *-------------------------------------------------------------------------
 *  x0z    Syntax errors.
 *  x1z    Information.
 *  x2z    Connections.  Replies referring to the control or data
 *         connections.
 *  x3z    Authentication and accounting.  Replies for the login or
 *         accounting commands.
 *  x4z    Unspecified.
 *  x5z    Filesystem status.
 *************************************************************************/


/**************************************************************************
 * Maximum buffer size for use by the transfer protocol.
 *  This will be eliminated when the filesystem is complete enough that
 *  we don't have to store the received data until we have something to
 *  do with it.
 *************************************************************************/
#define FTPD_MAX_RECEIVESIZE  (512*1024)

/**************************************************************************
 * SessionInfo structure.
 *
 * The following structure is allocated for each session.  The pointer
 * to this structure is contained in the tasks notepad entry.
 *************************************************************************/
typedef struct
{
   struct sockaddr_in  data_addr;   /* Data address for PORT commands */
   int                 ctrl_sock;   /* Control connection socker */
   char                cwd[255];    /* Current working directory */
                                    /* Login -- future use -- */
   int                 xfer_mode;   /* Transfer mode (ASCII/binary) */
} FTPD_SessionInfo_t;


#define FTPD_WELCOME_MESSAGE \
   "Welcome to the RTEMS FTP server.\n" \
   "\n" \
   "Login accepted.\n"


/**************************************************************************
 * Function: FTPD_SendReply                                               *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This procedure sends a reply to the client via the control          *
 *    connection.                                                         *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    int  code  - The 3-digit reply code.                                *
 *    char *text - Reply text.                                            *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_SendReply(int code, char *text)
{
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;
   char                str[80];


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   /***********************************************************************
    * code must be a 3-digit number.
    **********************************************************************/
   if ((code < 100) || (code > 999))
   {
      syslog(LOG_ERR, "ftpd: Code not 3-digits.");
      return;
   }

   /***********************************************************************
    * If a text reply exists, add it to the reply data.
    **********************************************************************/
   if (text != NULL)
   {
      sprintf(str, "%d %.70s\r\n", code, text);
   }
   else
   {
      sprintf(str, "%d\r\n", code);
   }
   send(info->ctrl_sock, str, strlen(str), 0);
}


/**************************************************************************
 * Function: FTPD_CommandRetrieve                                         *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This performs the "RETR" command.  A data connection must already   *
 *    be open (via the "PORT" command.)  Here, we send the data to the    *
 *    connection.                                                         *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *filename   - Source filename.                                 *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    int  - 0 for reply sent.                                            *
 *           1 for no reply sent.                                         *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  04/29/98 - Creation (JWJ)                                             *
 *************************************************************************/
static int
FTPD_CommandRetrieve(char *filename)
{
   int                 s;
   int                 n;

   FILE                *fp;
   unsigned char       *bufr;
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);


   if ((fp = fopen(filename, "r")) == NULL)
   {
      FTPD_SendReply(450, "Error opening file.");
      return(0);
   }

   bufr = (unsigned char *)malloc(BUFSIZ);
   if (bufr == NULL)
   {
      FTPD_SendReply(440, "Server error - malloc fail.");
      fclose(fp);
      return(0);
   }

   /***********************************************************************
    * Connect to the data connection (PORT made in an earlier PORT call).
    **********************************************************************/
   FTPD_SendReply(150, "BINARY data connection.");
   s = socket(AF_INET, SOCK_STREAM, 0);
   if (connect(s, (struct sockaddr *)&info->data_addr,
               sizeof(struct sockaddr)) < 0)
   {
      FTPD_SendReply(420, "Server error - could not connect socket.");
      free(bufr);
      fclose(fp);
      close(s);
      return(1);
   }

   /***********************************************************************
    * Send the data over the ether.
    **********************************************************************/
   while ((n = fread(bufr, 1, BUFSIZ, fp)) != 0)
   {
      send(s, bufr, n, 0);
   }

   if (feof(fp))
   {
      FTPD_SendReply(210, "File sent successfully.");
   }
   else
   {
      FTPD_SendReply(450, "Retrieve failed.");
   }

   if (close(s) != 0)
   {
      syslog(LOG_ERR, "ftpd: Error closing data socket");
   }

   free(bufr);
   fclose(fp);
   return(0);
}


/**************************************************************************
 * Function: FTPD_CommandStore                                            *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This performs the "STOR" command.  A data connection must already   *
 *    be open (via the "PORT" command.)  Here, we get the data from the   *
 *    connection and figure out what to do with it.                       *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *filename   - Destination filename.                            *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    int  - 0 for success.                                               *
 *           1 for failure.                                               *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static int
FTPD_CommandStore(char *filename)
{
   char                *bufr;
   char                *bigBufr;
   int                 s;
   int                 n;
   unsigned long       size = 0;
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   bufr = (char *)malloc(BUFSIZ * sizeof(char));
   if (bufr == NULL)
   {
      FTPD_SendReply(440, "Server error - malloc fail.");
      return(1);
   }

   bigBufr = (char *)malloc(FTPD_MAX_RECEIVESIZE * sizeof(char));
   if (bigBufr == NULL)
   {
      FTPD_SendReply(440, "Server error - malloc fail.");
      free(bufr);
      return(1);
   }

   FTPD_SendReply(150, "BINARY data connection.");

   s = socket(AF_INET, SOCK_STREAM, 0);
   if (connect(s, (struct sockaddr *)&info->data_addr,
               sizeof(struct sockaddr)) < 0)
   {
      free(bufr);
      free(bigBufr);
      close(s);
      return(1);
   }


   /***********************************************************************
    * File: "/dev/null" just throws the data away.
    **********************************************************************/
   if (!strncmp("/dev/null", filename, 9))
   {
      while ((n = read(s, bufr, BUFSIZ)) > 0);
   }
   else
   {
      /***********************************************************************
       * Retrieve the file into our buffer space.
       **********************************************************************/
      size = 0;
      while ((n = read(s, bufr, BUFSIZ)) > 0)
      {
         if (size + n > FTPD_MAX_RECEIVESIZE)
         {
            FTPD_SendReply(440, "Server error - Buffer size exceeded.");
            free(bufr);
            free(bigBufr);
            close(s);
            return(1);
         }
         memcpy(&bigBufr[size], bufr, n);
         size += n;
      }
   }
   free(bufr);
   close(s);


   /***********************************************************************
    * Figure out what to do with the data we just received.
    **********************************************************************/
   if (!strncmp("untar", filename, 5))
   {
      Untar_FromMemory(bigBufr, size);
      FTPD_SendReply(210, "Untar successful.");
   }
   else
   {
      FILE   *fp;
      size_t len;
      size_t written;

      fp = fopen(filename, "w");
      if (fp == NULL)
      {
         FTPD_SendReply(440, "Could not open file.");
         free(bigBufr);
         return(1);
      }
      
      n = 0;
      written = 0;
      while (n<size)
      {
         len = ((size-n>BUFSIZ)?(BUFSIZ):(size-n));
         written = fwrite(&bigBufr[n], 1, len, fp);
         n += written;
         if (written != len)
         {
            break;
         }
      }
      fclose(fp);

      if (n == size)
      {
         FTPD_SendReply(226, "Transfer complete.");
      }
      else
      {
         FTPD_SendReply(440, "Error during write.");
         free(bigBufr);
         return(1);
      }
   }

   free(bigBufr);
   return(0);
}


/**************************************************************************
 * Function: FTPD_CommandList                                             *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    Sends a file list through a data connection.  The data              *
 *    connection must have already been opened with the "PORT" command.   *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *fname  - File (or directory) to list.                         *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_CommandList(char *fname)
{
   int                 s;
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;
   DIR                 *dirp;
   struct dirent       *dp;
   char                dirline[255];
   struct stat         stat_buf;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   FTPD_SendReply(150, "ASCII data connection for LIST.");

   s = socket(AF_INET, SOCK_STREAM, 0);
   if (connect(s, (struct sockaddr *)&info->data_addr,
               sizeof(struct sockaddr)) < 0)
   {
      syslog(LOG_ERR, "ftpd: Error connecting to data socket.");
      return;
   }

   if ((dirp = opendir(fname)) == NULL)
   {
      sprintf(dirline, "%s: No such file or directory.%s\n",
              fname, (info->xfer_mode==TYPE_A)?("\r"):(""));
      send(s, dirline, strlen(dirline), 0);
      close(s);
      FTPD_SendReply(226, "Transfer complete.");
      return;
   }
   while ((dp = readdir(dirp)) != NULL)
   {
      if (stat(dp->d_name, &stat_buf) == 0)
      {
         sprintf(dirline, "%c%c%c%c%c%c%c%c%c%c  %5d %5d %11d  %s%s\n",
                 (S_ISLNK(stat_buf.st_mode)?('l'):
                    (S_ISDIR(stat_buf.st_mode)?('d'):('-'))),
                 (stat_buf.st_mode & S_IRUSR)?('r'):('-'),
                 (stat_buf.st_mode & S_IWUSR)?('w'):('-'),
                 (stat_buf.st_mode & S_IXUSR)?('x'):('-'),
                 (stat_buf.st_mode & S_IRGRP)?('r'):('-'),
                 (stat_buf.st_mode & S_IWGRP)?('w'):('-'),
                 (stat_buf.st_mode & S_IXGRP)?('x'):('-'),
                 (stat_buf.st_mode & S_IROTH)?('r'):('-'),
                 (stat_buf.st_mode & S_IWOTH)?('w'):('-'),
                 (stat_buf.st_mode & S_IXOTH)?('x'):('-'),
                 (int)stat_buf.st_uid,
                 (int)stat_buf.st_gid,
                 (int)stat_buf.st_size,
                 dp->d_name,
                 (info->xfer_mode==TYPE_A)?("\r"):(""));
         send(s, dirline, strlen(dirline), 0);
      }
   }
   closedir(dirp);

   close(s);
   FTPD_SendReply(226, "Transfer complete.");
}


/*
 * Cheesy way to change directories
 */
static void
FTPD_CWD(char *dir)
{
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   if (chdir(dir) == 0)
   {
      FTPD_SendReply(250, "CWD command successful.");
   }
   else
   {
      FTPD_SendReply(550, "CWD command failed.");
   }
}


/**************************************************************************
 * Function: FTPD_CommandPort                                             *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This procedure opens up a data port given the IP address of the     *
 *    remote machine and the port on the remote machine.  This connection *
 *    will then be used to transfer data between the hosts.               *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *bufr - Arguments to the "PORT" command.                       *
 *                                                                        *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_CommandPort(char *bufr)
{
   char                *ip;
   char                *port;
   int                 ip0, ip1, ip2, ip3, port0, port1;
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   sscanf(bufr, "%d,%d,%d,%d,%d,%d", &ip0, &ip1, &ip2, &ip3, &port0, &port1);
   ip = (char *)&(info->data_addr.sin_addr);
   ip[0] = ip0 & 0xff;
   ip[1] = ip1 & 0xff;
   ip[2] = ip2 & 0xff;
   ip[3] = ip3 & 0xff;
   port = (char *)&(info->data_addr.sin_port);
   port[0] = port0 & 0xff;
   port[1] = port1 & 0xff;
   info->data_addr.sin_family = AF_INET;
}


/**************************************************************************
 * Function: FTPD_ParseCommand                                            *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    Here, we parse the commands that have come through the control      *
 *    connection.                                                         *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *bufr     - Pointer to the buffer which contains the command   *
 *                     text.                                              *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_ParseCommand(char *bufr)
{
   char fname[255];
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   if (!strncmp("PORT", bufr, 4))
   {
      FTPD_SendReply(200, "PORT command successful.");
      FTPD_CommandPort(&bufr[5]);
   }
   else if (!strncmp("RETR", bufr, 4))
   {
      sscanf(&bufr[5], "%254s", fname);
      FTPD_CommandRetrieve(fname);
   }
   else if (!strncmp("STOR", bufr, 4))
   {
      sscanf(&bufr[5], "%254s", fname);
      FTPD_CommandStore(fname);
   }
   else if (!strncmp("LIST", bufr, 4))
   {
      if (bufr[5] == '\n')
      {
         FTPD_CommandList(".");
      }
      else
      {
         sscanf(&bufr[5], "%254s", fname);
         FTPD_CommandList(fname);
      }
   }
   else if (!strncmp("USER", bufr, 4))
   {
      FTPD_SendReply(230, "User logged in.");
   }
   else if (!strncmp("SYST", bufr, 4))
   {
      FTPD_SendReply(240, "RTEMS");
   }
   else if (!strncmp("TYPE", bufr, 4))
   {
      if (bufr[5] == 'I')
      {
         info->xfer_mode = TYPE_I;
         FTPD_SendReply(200, "Type set to I.");
      }
      else if (bufr[5] == 'A')
      {
         info->xfer_mode = TYPE_A;
         FTPD_SendReply(200, "Type set to A.");
      } 
      else
      {
         info->xfer_mode = TYPE_I;
         FTPD_SendReply(504, "Type not implemented.  Set to I.");
      }
   }
   else if (!strncmp("PASS", bufr, 4))
   {
      FTPD_SendReply(230, "User logged in.");
   }
   else if (!strncmp("DELE", bufr, 4))
   {
      sscanf(&bufr[4], "%254s", fname);
      if (unlink(fname) == 0)
      {
         FTPD_SendReply(257, "DELE successful.");
      }
      else
      {
         FTPD_SendReply(550, "DELE failed.");
      }
   }
   else if (!strncmp("SITE CHMOD", bufr, 10))
   {
      int mask;

      sscanf(&bufr[11], "%o %254s", &mask, fname);
      if (chmod(fname, (mode_t)mask) == 0)
      {
         FTPD_SendReply(257, "CHMOD successful.");
      }
      else
      {
         FTPD_SendReply(550, "CHMOD failed.");
      }
   }
   else if (!strncmp("RMD", bufr, 3))
   {
      sscanf(&bufr[4], "%254s", fname);
      if (rmdir(fname) == 0)
      {
         FTPD_SendReply(257, "RMD successful.");
      }
      else
      {
         FTPD_SendReply(550, "RMD failed.");
      }
   }
   else if (!strncmp("MKD", bufr, 3))
   {
      sscanf(&bufr[4], "%254s", fname);
      if (mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
      {
         FTPD_SendReply(257, "MKD successful.");
      }
      else
      {
         FTPD_SendReply(550, "MKD failed.");
      }
   }
   else if (!strncmp("CWD", bufr, 3))
   {
      sscanf(&bufr[4], "%254s", fname);
      FTPD_CWD(fname);
   }
   else if (!strncmp("PWD", bufr, 3))
   {
      char *cwd = getcwd(0, 0);
      sprintf(bufr, "\"%s\" is the current directory.", cwd);
      FTPD_SendReply(250, bufr);
      free(cwd);
   }
   else
   {
      FTPD_SendReply(500, "Unrecognized/unsupported command.");
   }
}

/**************************************************************************
 * Function: FTPD_Session                                                 *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This task is started when the FTP daemon gets a service request     *
 *    from a remote machine.  Here, we watch for commands that will       *
 *    come through the "control" connection.  These commands are then     *
 *    parsed and executed until the connection is closed, either          *
 *    unintentionally or intentionally with the "QUIT" command.           *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    rtems_task_argument arg - The FTPD_Daemon task passes the socket    *
 *                              which serves as the control connection.   *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_Session(rtems_task_argument arg)
{
   char                cmd[256];
   rtems_status_code   sc;
   FTPD_SessionInfo_t  *info = NULL;


   sc = rtems_task_get_note(RTEMS_SELF, RTEMS_NOTEPAD_0,
                            (rtems_unsigned32 *)&info);

   FTPD_SendReply(220, "Erithacus FTP server (Version 1.0) ready.");

   /***********************************************************************
    * Set initial directory to "/".
    **********************************************************************/
   strcpy(info->cwd, "/");
   info->xfer_mode = TYPE_A;
   while (1)
   {
      if (recv(info->ctrl_sock, cmd, 256, 0) == -1)
      {
         syslog(LOG_INFO, "ftpd: Connection aborted.");
         break;
      }

      if (!strncmp("QUIT", cmd, 4))
      {
         FTPD_SendReply(221, "Goodbye.");
         break;
      }
      else
      {
         FTPD_ParseCommand(cmd);
      }
   }

   if (close(info->ctrl_sock) < 0)
   {
      syslog(LOG_ERR, "ftpd: Could not close session.");
   }


   /* Least we can do is put the CWD back to /. */
   chdir("/");

   /***********************************************************************
    * Free up the allocated SessionInfo struct and exit.
    **********************************************************************/
   free(info);
   sc = rtems_task_delete(RTEMS_SELF);
   syslog(LOG_ERR, "ftpd: Task deletion failed: %s",
          rtems_status_text(sc));
}


/**************************************************************************
 * Function: FTPD_Daemon                                                  *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    This task runs in the background forever.  It waits for service     *
 *    requests on the FTP port (port 21).  When a request is received,    *
 *    it opens a new session to handle those requests until the           *
 *    connection is closed.                                               *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
static void
FTPD_Daemon()
{
   int                 s;
   int                 s1;
   int                 addrLen;
   struct sockaddr_in  remoteAddr;
   struct sockaddr_in  localAddr;
   char                sessionID;
   rtems_task_priority priority;
   rtems_status_code   sc;
   rtems_id            tid;
   FTPD_SessionInfo_t  *info = NULL;


   sessionID = 'a';

   s = socket(AF_INET, SOCK_STREAM, 0);
   if (s < 0)
   {
      perror("Creating socket");
   }

   localAddr.sin_family      = AF_INET;
   localAddr.sin_port        = FTPD_CONTROL_PORT;
   localAddr.sin_addr.s_addr = INADDR_ANY;
   memset(localAddr.sin_zero, '\0', sizeof(localAddr.sin_zero));
   if (bind(s, (struct sockaddr *)&localAddr,
            sizeof(localAddr)) < 0)
   {
      perror("Binding control socket");
   }

   if (listen(s, 2) < 0)
   {
      perror("Listening on control socket");
   }

   while (1)
   {
      /********************************************************************
       * Allocate a SessionInfo structure for the session task.
       *******************************************************************/
      info = (FTPD_SessionInfo_t *)malloc(sizeof(FTPD_SessionInfo_t));
      if (info == NULL)
      {
         syslog(LOG_ERR, "ftpd: Could not allocate session info struct.");
         rtems_panic("Malloc fail.");
      }

      /********************************************************************
       * Accept on the socket and start the session task.
       *******************************************************************/
      addrLen = sizeof(remoteAddr);
      s1 = accept(s, (struct sockaddr *)&remoteAddr, &addrLen);
      if (s1 < 0)
      {
         perror("Accepting control connection");
      }

      rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &priority);
      sc = rtems_task_create(rtems_build_name('F', 'T', 'P', sessionID),
                             priority, 8*1024,
                             RTEMS_PREEMPT | RTEMS_NO_TIMESLICE |
                             RTEMS_NO_ASR | RTEMS_INTERRUPT_LEVEL(0),
                             RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
                             &tid);
      if (sc != RTEMS_SUCCESSFUL)
      {
         syslog(LOG_ERR, "ftpd: Could not create FTPD session: %s", 
                rtems_status_text(sc));
      }

      if (sessionID == 'z')
      {
         sessionID = 'a';
      }
      else
      {
         sessionID++;
      }

      /********************************************************************
       * Send the socket on to the new session.
       *******************************************************************/
      info->ctrl_sock = s1;
      sc = rtems_task_set_note(tid, RTEMS_NOTEPAD_0,
                               (rtems_unsigned32)info);
      sc = rtems_task_start(tid, FTPD_Session, 0);
      if (sc != RTEMS_SUCCESSFUL)
      {
         syslog(LOG_ERR, "ftpd: Could not start FTPD session: %s",
                rtems_status_text(sc));
      }
   }
}


/**************************************************************************
 * Function: FTPD_Start                                                   *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    Here, we start the FTPD task which waits for FTP requests and       *
 *    services them.  This procedure returns to its caller once the       *
 *    task is started.                                                    *
 *                                                                        *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    rtems_task_priority priority - Priority to assign to this task.     *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97 - Creation (JWJ)                                             *
 *************************************************************************/
void
FTPD_Start(rtems_task_priority priority)
{
   rtems_status_code   sc;
   rtems_id            tid;


   sc = rtems_task_create(rtems_build_name('F', 'T', 'P', 'D'),
                          priority, 8*1024,
                          RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR |
                          RTEMS_INTERRUPT_LEVEL(0),
                          RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
                          &tid);
   if (sc != RTEMS_SUCCESSFUL)
   {
      syslog(LOG_ERR, "ftpd: Could not create FTP daemon: %s",
             rtems_status_text(sc));
   }

   sc = rtems_task_start(tid, FTPD_Daemon, 0);
   if (sc != RTEMS_SUCCESSFUL)
   {
      syslog(LOG_ERR, "ftpd: Could not start FTP daemon: %s",
             rtems_status_text(sc));
   }   

   syslog(LOG_INFO, "ftpd: FTP daemon started.");
}