summaryrefslogtreecommitdiffstats
path: root/cpukit/ftpd/ftpd.c
blob: 28e803b628273f7c38f83d13abf7974d09badb38 (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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
/* FIXME: 1. Parse command is a hack.  We can do better.
 *        2. Some sort of access control?
 *        3. OSV: Timeouts on sockets.
 *        4. OSV: hooks support seems to be a joke, as it requires storing of
 *           entire input file in memory. Seem to be better to change it to
 *           something more reasonable, like having
 *           'hook_write(void const *buf, int count)' routine that will be
 *           called multiple times while file is being received.
 *        5. OSV: Remove hack with "/dev/null"?
 *        6. OSV: data_addr field of SessionInfo structure is not initialized.
 *           This will lead to undefined behavior if FTP client doesn't send
 *           PORT command. Seems we need to implement usage of default port
 *           (ftp port - 1). I didn't find any FTP client that doesn't send
 *           PORT command though.
 *        7. OSV: while server claims TYPE ASCII command succeeds (to make
 *           happy some clients), ASCII mode isn't actually implemented.
 *        8. Passive Mode?
 *
 *  FTP Server Daemon
 *
 *  Submitted by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
 *
 *  Changed by:   Sergei Organov <osv@javad.ru> (OSV)
 *    Changes:
 *      - use pool of pre-created threads to handle sessions
 *      - LIST output now similar to what "/bin/ls -al" would output, thus
 *        FTP clients could parse it.
 *      - LIST NAME now works (both for files and directories)
 *      - keep track of CWD for every session separately
 *      - ability to specify root directory name in configuration table
 *      - options sent in commands are ignored, thus LIST -al FILE works
 *      - added support for NLST, CDUP and MDTM commands
 *      - buffers are allocated on stack instead of heap where possible
 *      - drop using of task notepad to pass parameters - use function
 *        arguments instead
 *      - various bug-fixes, e.g., use of PF_INET in socket() instead of
 *        AF_INET, use snprintf() instead of sprintf() everywhere for safety,
 *        etc.
 *
 *  $Id$
 */

/**************************************************************************
 *                                 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 'hooks' to make it usable in situations where files   *
 *    are not used/necessary.  Once the server is started, it runs        *
 *    forever.                                                            *
 *                                                                        *
 *                                                                        *
 *    Organization:                                                       *
 *                                                                        *
 *       The FTP daemon is started upon boot along with a (configurable)  *
 *       number of tasks to handle sessions. It runs all the time and     *
 *       waits for connections on the known FTP port (21).  When          *
 *       a connection is made, it wakeups a 'session' task.  That         *
 *       session then interacts with the remote host.  When the session   *
 *       is complete, the session task goes to sleep.  The daemon still   *
 *       runs, however.                                                   *
 *                                                                        *
 *                                                                        *
 * 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.                        *
 * NLST xxx     - Sends a file list to the client.                        *
 * 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.                               *
 * CDUP         - Change to upper 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).                             *
 * MDTM xxx     - Send date/time to the client. xxx = filename.           *
 *                                                                        *
 *                                                                        *
 * The public routines contained in this file are:                        *
 *                                                                        *
 *    rtems_initialize_ftpd - Initializes and starts the server daemon,   *
 *                            then returns to its caller.                 *
 *                                                                        *
 *------------------------------------------------------------------------*
 * Jake Janovetz                                                          *
 * University of Illinois                                                 *
 * 1406 West Green Street                                                 *
 * Urbana IL  61801                                                       *
 **************************************************************************
 * Change History:                                                        *
 *  12/01/97   - Creation (JWJ)                                           *
 *  2001-01-08 - Changes by OSV                                           *
 *************************************************************************/

/**************************************************************************
 * 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.
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.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"


#ifdef __GNUC__
/* change to #if 1 to disable syslog entirely */
#if 0
#undef  syslog
#define syslog(a, b, ...) while(0){}
#endif
#endif

#define FTPD_SERVER_MESSAGE  "RTEMS FTP server (Version 1.1-JWJ) ready."

#define FTPD_SYSTYPE "RTEMS"

/* Seem to be unused */
#if 0
#define FTPD_WELCOME_MESSAGE \
   "Welcome to the RTEMS FTP server.\n" \
   "\n" \
   "Login accepted.\n"
#endif

/* Various buffer sizes */
enum
{
  FTPD_BUFSIZE  = 256,       /* Size for temporary buffers */
  FTPD_DATASIZE = 1024,      /* Size for file transfer buffers */
  FTPD_STACKSIZE = 8 * 1024, /* Tasks stack size */
};

/* Event to be used by session tasks for waiting */
enum
{
  FTPD_RTEMS_EVENT = RTEMS_EVENT_1
};

/* Configuration table */
extern struct rtems_ftpd_configuration rtems_ftpd_configuration;

/* this is not prototyped in strict ansi mode */
FILE *fdopen (int fildes, const char *mode);

/*
 * SessionInfo structure.
 *
 * The following structure is allocated for each session.
 */
typedef struct
{
  struct sockaddr_in  data_addr;   /* Data address for PORT commands */
  FILE                *ctrl_fp;    /* File pointer for control connection */
  int                 socket;      /* Socket descriptor for ctrl connection */
  char                cwd[FTPD_BUFSIZE];  /* Current working directory */
  /* Login -- future use -- */
  int                 xfer_mode;   /* Transfer mode (ASCII/binary) */
  rtems_id            tid;         /* Task id */
} FTPD_SessionInfo_t;


/*
 * TaskPool structure.
 */
typedef struct
{
  FTPD_SessionInfo_t    *info;
  FTPD_SessionInfo_t    **queue;
  int                   count;
  int                   head;
  int                   tail;
  rtems_id              mutex;
  rtems_id              sem;
} FTPD_TaskPool_t;

/*
 * Task pool instance.
 */
static FTPD_TaskPool_t task_pool;

/*
 * Root node for FTPD without trailing slash. Even '/' node is denoted as
 * empty string here.
 */
static char root[FTPD_BUFSIZE];


/*PAGE
 *
 * serr
 *
 * Return errror string corresponding to current 'errno'.
 *
 */
static char const*
serr(void)
{
  return strerror(errno);
}


/*
 *  Utility routines to manage root directory and session local
 *  current working directory.
 */


/*PAGE
 *
 * squeeze_path
 *
 * Squeezes path according to OS rules, i.e., eliminates /./, /../, and //
 * from the path. Does nothing if the path is relative, i.e. doesn't begin
 * with '/'. The trailing slash is always removed, even when alone, i.e. "/"
 * will be "" after squeeze.
 *
 * Input parameters:
 *   path - the path to be squeezed
 *
 * Output parameters:
 *   path - squeezed path
 *
 */
static void
squeeze_path(char* path)
{
  if(path[0] == '/')
  {
    char* e = path + 1;
    int rest = strlen(e);
    while(rest >= 0)
    {
      int len;
      char* s = e;
      e = strchr(s, '/');
      if(e)
        ++e;
      else
        e = s + rest + 1;
      len = e - s;
      rest -= len;
      if(len == 1 || (len == 2 && s[0] == '.'))
      {
        if(rest >= 0)
          memmove(s, e, rest + 1);
        else
          *s++ = '\0';
        e = s;
      }
      else if(len == 3 && s[0] == '.' && s[1] == '.')
      {
        char* ps = s;
        if(ps - 1 > path) {
          do
            --ps;
          while(ps[-1] != '/');
        }
        if(rest >= 0)
          memmove(ps, e, rest + 1);
        else
          *ps++ = '\0';
        e = ps;
      }
    }
    if(e[-2] == '/')
      e[-2] = '\0';
  }
}


/*PAGE
 *
 * make_path
 *
 * Makes full path given file name, current working directory and root
 * directory (file scope variable 'root').
 *
 * Input parameters:
 *   cwd  - current working directory
 *   name - file name
 *   root (file scope variable) - FTPD root directory
 *
 * Output parameters:
 *   buf - full path
 *   returns pointer to non-root part of the 'buf', i.e. to first character
 *           different from '/' after root part.
 *
 */
static char const*
make_path(char* buf, char const* cwd, char const* name)
{
  char* res = NULL;

  int rlen = strlen(root);
  int clen = strlen(cwd);
  int nlen = strlen(name);
  int len = rlen + nlen;

  if (name[0] != '/')
  {
    ++len;
    if (clen > 0)
      len += clen + 1;
  }

  if (FTPD_BUFSIZE > len)
  {
    char* b = buf;
    memcpy(b, root, rlen); b += rlen;
    if (name[0] != '/')
    {
      *b++ = '/';
      if (clen > 0)
      {
        memcpy(b, cwd, clen); b += clen;
        *b++ = '/';
      }
    }
    memcpy(b, name, nlen); b += nlen;
    *b = '\0';

    res = buf + rlen;
    while(rlen-- > 0 && res[-1] == '/')
      --res;
    squeeze_path(res);
  }

  return res;
}

/*
 * Task pool management routines
 */


/*PAGE
 *
 * task_pool_done
 *
 * Cleanup task pool.
 *
 * Input parameters:
 *   count - number of entries in task pool to cleanup
 *
 * Output parameters:
 *   NONE
 *
 */
static void
task_pool_done(int count)
{
  int i;
  for(i = 0; i < count; ++i)
    rtems_task_delete(task_pool.info[i].tid);
  if(task_pool.info)
    free(task_pool.info);
  if(task_pool.queue)
    free(task_pool.queue);
  if(task_pool.mutex != (rtems_id)-1)
    rtems_semaphore_delete(task_pool.mutex);
  if(task_pool.sem != (rtems_id)-1)
    rtems_semaphore_delete(task_pool.sem);
  task_pool.info = 0;
  task_pool.queue = 0;
  task_pool.count = 0;
  task_pool.sem = -1;
  task_pool.mutex = -1;
}

/* Forward declare */
static void session(rtems_task_argument arg);

/*PAGE
 *
 * task_pool_init
 *
 * Initialize task pool.
 *
 * Input parameters:
 *   count    - number of entries in task pool to create
 *   priority - priority tasks are started with
 *
 * Output parameters:
 *   returns 1 on success, 0 on failure.
 *
 */
static int
task_pool_init(int count, rtems_task_priority priority)
{
  int i;
  rtems_status_code sc;
  char id = 'a';

  task_pool.count = 0;
  task_pool.head = task_pool.tail = 0;
  task_pool.mutex = (rtems_id)-1;
  task_pool.sem   = (rtems_id)-1;

  sc = rtems_semaphore_create(
    rtems_build_name('F', 'T', 'P', 'M'),
    1,
    RTEMS_DEFAULT_ATTRIBUTES
    | RTEMS_BINARY_SEMAPHORE
    | RTEMS_INHERIT_PRIORITY
    | RTEMS_PRIORITY,
    RTEMS_NO_PRIORITY,
    &task_pool.mutex);

  if(sc == RTEMS_SUCCESSFUL)
    sc = rtems_semaphore_create(
      rtems_build_name('F', 'T', 'P', 'S'),
      count,
      RTEMS_DEFAULT_ATTRIBUTES,
      RTEMS_NO_PRIORITY,
      &task_pool.sem);

  if(sc != RTEMS_SUCCESSFUL) {
    task_pool_done(0);
    syslog(LOG_ERR, "ftpd: Can not create semaphores");
    return 0;
  }

  task_pool.info = (FTPD_SessionInfo_t*)
    malloc(sizeof(FTPD_SessionInfo_t) * count);
  task_pool.queue = (FTPD_SessionInfo_t**)
    malloc(sizeof(FTPD_SessionInfo_t*) * count);
  if (NULL == task_pool.info || NULL == task_pool.queue)
  {
    task_pool_done(0);
    syslog(LOG_ERR, "ftpd: Not enough memory");
    return 0;
  }

  for(i = 0; i < count; ++i)
  {
    FTPD_SessionInfo_t *info = &task_pool.info[i];
    sc = rtems_task_create(rtems_build_name('F', 'T', 'P', id),
      priority, FTPD_STACKSIZE,
      RTEMS_PREEMPT | RTEMS_NO_TIMESLICE |
      RTEMS_NO_ASR | RTEMS_INTERRUPT_LEVEL(0),
      RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
      &info->tid);
    if (sc == RTEMS_SUCCESSFUL)
    {
      sc = rtems_task_start(
        info->tid, session, (rtems_task_argument)info);
      if (sc != RTEMS_SUCCESSFUL)
        task_pool_done(i);
    }
    else
      task_pool_done(i + 1);
    if (sc != RTEMS_SUCCESSFUL)
    {
      syslog(LOG_ERR, "ftpd: Could not create/start FTPD session: %s",
        rtems_status_text(sc));
      return 0;
    }
    task_pool.queue[i] = task_pool.info + i;
    info->ctrl_fp = NULL;
    info->socket  = -1;
    if (++id > 'z')
      id = 'a';
  }
  task_pool.count = count;
  return 1;
}

/*PAGE
 *
 * task_pool_obtain
 *
 * Obtain free task from task pool.
 *
 * Input parameters:
 *   NONE
 *
 * Output parameters:
 *   returns pointer to the corresponding SessionInfo structure on success,
 *           NULL if there are no free tasks in the pool.
 *
 */
static FTPD_SessionInfo_t*
task_pool_obtain()
{
  FTPD_SessionInfo_t* info = 0;
  rtems_status_code sc;
  sc = rtems_semaphore_obtain(task_pool.sem, RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT);
  if (sc == RTEMS_SUCCESSFUL)
  {
    rtems_semaphore_obtain(task_pool.mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    info = task_pool.queue[task_pool.head];
    if(++task_pool.head >= task_pool.count)
      task_pool.head = 0;
    info->socket = -1;
    info->ctrl_fp = NULL;
    rtems_semaphore_release(task_pool.mutex);
  }
  return info;
}

/*PAGE
 *
 * task_pool_release
 *
 * Return task obtained by 'obtain()' back to the task pool.
 *
 * Input parameters:
 *   info  - pointer to corresponding SessionInfo structure.
 *
 * Output parameters:
 *   NONE
 *
 */
static void
task_pool_release(FTPD_SessionInfo_t* info)
{
  rtems_semaphore_obtain(task_pool.mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  task_pool.queue[task_pool.tail] = info;
  if(++task_pool.tail >= task_pool.count)
    task_pool.tail = 0;
  rtems_semaphore_release(task_pool.mutex);
  rtems_semaphore_release(task_pool.sem);
}

/*
 * End of task pool routines
 */

/*PAGE
 *
 * close_socket
 *
 * Close socket.
 *
 * Input parameters:
 *   s - socket descriptor to be closed.
 *
 * Output parameters:
 *   returns 1 on success, 0 on failure
 *
 */
static int
close_socket(int s)
{
  if (0 <= s)
  {
    if (0 != close(s))
    {
      shutdown(s, 2);
      if (0 != close(s))
        return 0;
    }
  }
  return 1;
}

/*PAGE
 *
 * close_stream
 *
 * Close data stream of session.
 *
 * Input parameters:
 *   info - corresponding SessionInfo structure
 *
 * Output parameters:
 *   NONE
 *
 */
static void
close_stream(FTPD_SessionInfo_t* info)
{
  if (NULL != info->ctrl_fp)
  {
    if (0 != fclose(info->ctrl_fp))
    {
      syslog(LOG_ERR, "ftpd: Could not close control stream: %s", serr());
    }
    else
      info->socket = -1;
  }

  if (!close_socket(info->socket))
    syslog(LOG_ERR, "ftpd: Could not close control socket: %s", serr());

  info->ctrl_fp = NULL;
  info->socket = -1;
}


/**************************************************************************
 * Function: send_reply                                                   *
 **************************************************************************
 * 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                                                                *
 *                                                                        *
 *************************************************************************/
static void
send_reply(FTPD_SessionInfo_t  *info, int code, char *text)
{
  char const* s;


  /***********************************************************************
   * 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.
   **********************************************************************/
  s  = (info->xfer_mode == TYPE_A) ? "\r" : "";
  if (text != NULL)
    fprintf(info->ctrl_fp, "%d %.70s%s\n", code, text, s);
  else
    fprintf(info->ctrl_fp, "%d%s\n", code, s);
  fflush(info->ctrl_fp);
}


/*PAGE
 *
 * send_mode_reply
 *
 * Sends BINARY/ASCII reply string depending on current transfer mode.
 *
 * Input parameters:
 *   info - corresponding SessionInfo structure
 *
 * Output parameters:
 *   NONE
 *
 */
static void
send_mode_reply(FTPD_SessionInfo_t *info)
{
  if(info->xfer_mode == TYPE_I)
    send_reply(info, 150, "Opening BINARY mode data connection.");
  else
    send_reply(info, 150, "Opening ASCII mode data connection.");
}

/*PAGE
 *
 * data_socket
 *
 * Create data socket for session.
 *
 * Input parameters:
 *   info - corresponding SessionInfo structure
 *
 * Output parameters:
 *   returns socket descriptor, or -1 if failure
 *
 */
static int
data_socket(FTPD_SessionInfo_t *info)
{
  int s = socket(PF_INET, SOCK_STREAM, 0);
  if(0 > s)
    send_reply(info, 420, "Server error - could not create socket.");
  else if(0 > connect(s, (struct sockaddr *)&info->data_addr,
    sizeof(struct sockaddr)))
  {
    send_reply(info, 420, "Server error - could not connect socket.");
    close_socket(s);
    s = -1;
  }
  return s;
}

/**************************************************************************
 * Function: command_retrieve                                             *
 **************************************************************************
 * 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.                                         *
 *                                                                        *
 *************************************************************************/
static int
command_retrieve(FTPD_SessionInfo_t  *info, char *filename)
{
  int                 s = -1;
  int                 n;
  int                 fd = -1;
  char                buf[FTPD_DATASIZE];
  int                 res = 0;

  char const* r = make_path(buf, info->cwd, filename);

  if (NULL == r || 0 > (fd = open(buf, O_RDONLY)))
  {
    send_reply(info, 450, "Error opening file.");
    return(res);
  }

  send_mode_reply(info);

  /***********************************************************************
   * Connect to the data connection (PORT made in an earlier PORT call).
   **********************************************************************/
  s = data_socket(info);
  if (0 <= s)
  {
    /***********************************************************************
     * Send the data over the ether.
     **********************************************************************/
    while ((n = read(fd, buf, FTPD_DATASIZE)) > 0)
    {
      send(s, buf, n, 0);
    }

    if (0 == n)
    {
      if (0 == close(fd))
      {
        fd = -1;
        res = 1;
      }
    }
  }

  if (0 == res)
    send_reply(info, 450, "Retrieve failed.");
  else
    send_reply(info, 210, "File sent successfully.");

  if (-1 != fd)
    close(fd);

  if (!close_socket(s))
    syslog(LOG_ERR, "ftpd: Error closing data socket");

  return(res);
}


/**************************************************************************
 * Function: command_store                                                *
 **************************************************************************
 * 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.                                               *
 *                                                                        *
 *************************************************************************/
static int
command_store(FTPD_SessionInfo_t *info, char *filename)
{
  int                    s;
  int                    n;
  unsigned long          size = 0;
  struct rtems_ftpd_hook *usehook = NULL;
  char                   buf[FTPD_DATASIZE];

  int null = !strcmp("/dev/null", filename);

  if(!null)
  {
    if (NULL == make_path(buf, info->cwd, filename))
    {
      send_reply(info, 450, "Error creating file.");
      return(0);
    }
  }

  send_mode_reply(info);

  s = data_socket(info);
  if(0 > s)
  {
    return(1);
  }


  /***********************************************************************
   * File: "/dev/null" just throws the data away.
   * Otherwise, search our list of hooks to see if we need to do something
   *   special.
   * OSV: FIXME: this is hack. Using /dev/null filesystem entry would be
   *             better. However, it's not clear how to handle root directory
   *             other than '/' then.
   **********************************************************************/
  if (null)
  {
    while ((n = read(s, buf, FTPD_DATASIZE)) > 0);
  }
  else if (rtems_ftpd_configuration.hooks != NULL)
  {
    struct rtems_ftpd_hook *hook;
    int i;

    i = 0;
    hook = &rtems_ftpd_configuration.hooks[i++];
    while (hook->filename != NULL)
    {
      if (!strcmp(hook->filename, buf))
      {
        usehook = hook;
        break;
      }
      hook = &rtems_ftpd_configuration.hooks[i++];
    }
  }

  if (usehook != NULL)
  {
    /*
     * OSV: FIXME: Small buffer could be used and hook routine
     * called multiple times instead. Alternatively, the support could be
     * removed entirely in favor of configuring RTEMS pseudo-device with
     * given name.
     */

    char                *bigBufr;
    size_t filesize = rtems_ftpd_configuration.max_hook_filesize + 1;

    /***********************************************************************
     * Allocate space for our "file".
     **********************************************************************/
    bigBufr = (char *)malloc(filesize);
    if (bigBufr == NULL)
    {
      send_reply(info, 440, "Server error - malloc fail.");
      return(1);
    }

    /***********************************************************************
     * Retrieve the file into our buffer space.
     **********************************************************************/
    size = 0;
    while ((n = read(s, bigBufr + size, filesize - size)) > 0)
    {
      size += n;
    }
    if (size >= filesize)
    {
      send_reply(info, 440, "Server error - Buffer size exceeded.");
      free(bigBufr);
      close_socket(s);
      return(1);
    }
    close_socket(s);

    /***********************************************************************
     * Call our hook.
     **********************************************************************/
    if ((usehook->hook_function)(bigBufr, size) == 0)
    {
      send_reply(info, 210, "File transferred successfully.");
    }
    else
    {
      send_reply(info, 440, "File transfer failed.");
    }
    free(bigBufr);
  }
  else
  {
    int fd =
      creat(buf, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    if (0 > fd)
    {
      send_reply(info, 450, "Error creating file.");
      close_socket(s);
      return(0);
    }

    while ((n = read(s, buf, FTPD_DATASIZE)) > 0)
    {
      if (0 > write(fd, buf, n))
      {
        send_reply(info, 450, "Error during write.");
        close(fd);
        close_socket(s);
        return(1);
      }
    }
    if (0 > close(fd))
    {
      send_reply(info, 450, "Error during write.");
      close_socket(s);
      return(1);
    }
    close_socket(s);
    send_reply(info, 226, "Transfer complete.");
  }

  return(0);
}


/*PAGE
 *
 * send_dirline
 *
 * Sends one line of LIST command reply corresponding to single file.
 *
 * Input parameters:
 *   s - socket descriptor to send data to
 *   wide - if 0, send only file name. If not 0, send 'stat' info as well in
 *          "ls -l" format.
 *   curTime - current time
 *   path - path to be prepended to what is given by 'add'
 *   add  - path to be appended to what is given by 'path', the resulting path
 *          is then passed to 'stat()' routine
 *   name - file name to be reported in output
 *   buf  - buffer for temporary data
 *
 * Output parameters:
 *   NONE
 *
 */
static void
send_dirline(int s, int wide, time_t curTime, char const* path,
  char const* add, char const* fname, char* buf)
{
  if(wide)
  {
    struct stat stat_buf;

    int plen = strlen(path);
    int alen = strlen(add);
    if(plen == 0)
    {
      buf[plen++] = '/';
      buf[plen] = '\0';
    }
    else
    {
      strcpy(buf, path);
      if(alen > 0 && buf[plen - 1] != '/')
      {
        buf[plen++] = '/';
        if(plen >= FTPD_BUFSIZE)
          return;
        buf[plen] = '\0';
      }
    }
    if(plen + alen >= FTPD_BUFSIZE)
      return;
    strcpy(buf + plen, add);

    if (stat(buf, &stat_buf) == 0)
    {
      int len;
      struct tm bt;
      time_t tf = stat_buf.st_mtime;
      enum { SIZE = 80 };
      enum { SIX_MONTHS = 365*24*60*60/2 };
      char timeBuf[SIZE];
      gmtime_r(&tf, &bt);
      if(curTime > tf + SIX_MONTHS || tf > curTime + SIX_MONTHS)
        strftime (timeBuf, SIZE, "%b %d  %Y", &bt);
      else
        strftime (timeBuf, SIZE, "%b %d %H:%M", &bt);

      len = snprintf(buf, FTPD_BUFSIZE,
        "%c%c%c%c%c%c%c%c%c%c  1 %5d %5d %11u %s %s\r\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,
        timeBuf,
        fname
      );

      send(s, buf, len, 0);
    }
  }
  else
  {
    int len = snprintf(buf, FTPD_BUFSIZE, "%s\r\n", fname);
    send(s, buf, len, 0);
  }
}

/**************************************************************************
 * Function: command_list                                      *
 **************************************************************************
 * 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                                                                *
 *                                                                        *
 *************************************************************************/
static void
command_list(FTPD_SessionInfo_t *info, char const *fname, int wide)
{
  int                 s;
  DIR                 *dirp = 0;
  struct dirent       *dp = 0;
  struct stat         stat_buf;
  char                buf[FTPD_BUFSIZE];
  char                path[FTPD_BUFSIZE];
  time_t curTime;
  char const* res;
  char const* cwd = info->cwd;

  send_reply(info, 150, "Opening ASCII mode data connection for LIST.");

  s = data_socket(info);
  if(0 > s)
  {
    syslog(LOG_ERR, "ftpd: Error connecting to data socket.");
    return;
  }

  if(fname[0] == '\0')
    fname = ".";

  res = make_path(path, cwd, fname);

  if (NULL == res || 0 > stat(path, &stat_buf))
  {
    snprintf(buf, FTPD_BUFSIZE,
      "%s: No such file or directory.\r\n", fname);
    send(s, buf, strlen(buf), 0);
  }
  else if (S_ISDIR(stat_buf.st_mode) && (NULL == (dirp = opendir(path))))
  {
    snprintf(buf, FTPD_BUFSIZE,
      "%s: Can not open directory.\r\n", fname);
    send(s, buf, strlen(buf), 0);
  }
  else
  {
    time(&curTime);
    if(!dirp)
      send_dirline(s, wide, curTime, path, "", fname, buf);
    else {
      /* FIXME: need "." and ".." only when '-a' option is given */
      send_dirline(s, wide, curTime, path, "", ".", buf);
      if(!strcmp(path, root))
        send_dirline(s, wide, curTime, path, "", "..", buf);
      else
        send_dirline(s, wide, curTime, path, "..", "..", buf);
      while ((dp = readdir(dirp)) != NULL)
        send_dirline(s, wide, curTime, path, dp->d_name, dp->d_name, buf);
    }
  }

  if(dirp)
    closedir(dirp);
  close_socket(s);
  send_reply(info, 226, "Transfer complete.");
}


/*PAGE
 *
 * rtems_ftpd_cwd
 *
 * Change current working directory. We use 'chdir' here only to validate the
 * new directory. We keep track of current working directory ourselves because
 * current working directory in RTEMS isn't thread local, but we need it to be
 * session local.
 *
 * Input parameters:
 *   info - corresponding SessionInfo structure
 *   dir  - directory name passed in CWD command
 *
 * Output parameters:
 *   info->cwd is set to new CWD value.
 *
 */
static void
rtems_ftpd_cwd(FTPD_SessionInfo_t  *info, char *dir)
{
  char buf[FTPD_BUFSIZE];
  char const* cwd = make_path(buf, info->cwd, dir);
  if(cwd && chdir(buf) == 0)
  {
    send_reply(info, 250, "CWD command successful.");
    strcpy(info->cwd, cwd);
  }
  else
  {
    send_reply(info, 550, "CWD command failed.");
  }
}



/*PAGE
 *
 * command_mdtm
 *
 * Handle FTP MDTM command
 *
 * Input parameters:
 *   info - corresponding SessionInfo structure
 *   fname - file name passed in MDTM command
 *
 * Output parameters:
 *   info->cwd is set to new CWD value.
 *
 */
static void
command_mdtm(FTPD_SessionInfo_t  *info, char const* fname)
{
  struct stat stbuf;
  char buf[FTPD_BUFSIZE];

  if(*fname == '\0')
    fname = ".";

  if (stat(fname, &stbuf) < 0)
  {
    snprintf(buf, FTPD_BUFSIZE, "%s: %s.", fname, serr());
    send_reply(info, 550, buf);
  }
  else
  {
    struct tm *t = gmtime(&stbuf.st_mtime);
    snprintf(buf, FTPD_BUFSIZE, "%04d%02d%02d%02d%02d%02d",
      1900 + t->tm_year,
      t->tm_mon+1, t->tm_mday,
      t->tm_hour, t->tm_min, t->tm_sec);
    send_reply(info, 213, buf);
  }
}

/**************************************************************************
 * Function: command_port                                                 *
 **************************************************************************
 * 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                                                                *
 *                                                                        *
 *************************************************************************/
static void
command_port(FTPD_SessionInfo_t *info, char *bufr)
{
  char                *ip;
  char                *port;
  int                 ip0, ip1, ip2, ip3, port0, port1;

  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;
}


/*PAGE
 *
 * skip_options
 *
 * Utility routine to skip options (if any) from input command.
 *
 * Input parameters:
 *   p  - pointer to pointer to command
 *
 * Output parameters:
 *   p  - is changed to point to first non-option argument
 *
 */
static void
skip_options(char **p)
{
  char* buf = *p;
  while(1) {
    while(*buf == ' ')
      ++buf;
    if(*buf == '-') {
      if(*++buf == '-') { /* `--' should terminate options */
        ++buf;
        while(*buf == ' ')
          ++buf;
        break;
      }
      while(*buf != ' ' && *buf != '\0')
        ++buf;
    }
    else
      break;
  }
  *p = buf;
}

/**************************************************************************
 * Function: parse_command                                     *
 **************************************************************************
 * Description:                                                           *
 *                                                                        *
 *    Here, we parse the commands that have come through the control      *
 *    connection.                                                         *
 *                                                                        *
 * FIXME: This section is somewhat of a hack.  We should have a better    *
 *        way to parse commands.                                          *
 *                                                                        *
 * Inputs:                                                                *
 *                                                                        *
 *    char *bufr     - Pointer to the buffer which contains the command   *
 *                     text.                                              *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 *************************************************************************/
static void
parse_command(FTPD_SessionInfo_t *info, char *bufr)
{
  char fname[FTPD_BUFSIZE];

  if (!strncmp("PORT", bufr, 4))
  {
    send_reply(info, 200, "PORT command successful.");
    command_port(info, &bufr[5]);
  }
  else if (!strncmp("RETR", bufr, 4))
  {
    sscanf(&bufr[5], "%254s", fname);
    command_retrieve(info, fname);
  }
  else if (!strncmp("STOR", bufr, 4))
  {
    sscanf(&bufr[5], "%254s", fname);
    command_store(info, fname);
  }
  else if (!strncmp("LIST", bufr, 4))
  {
    bufr += 4;
    skip_options(&bufr);
    sscanf(bufr, "%254s", fname);
    command_list(info, fname, 1);
  }
  else if (!strncmp("NLST", bufr, 4))
  {
    bufr += 4;
    skip_options(&bufr);
    sscanf(bufr, "%254s", fname);
    command_list(info, fname, 0);
  }
  else if (!strncmp("MDTM", bufr, 4))
  {
    bufr += 4;
    skip_options(&bufr);
    sscanf(bufr, "%254s", fname);
    command_mdtm(info, fname);
  }
  else if (!strncmp("USER", bufr, 4))
  {
    send_reply(info, 230, "User logged in.");
  }
  else if (!strncmp("SYST", bufr, 4))
  {
    send_reply(info, 240, FTPD_SYSTYPE);
  }
  else if (!strncmp("TYPE", bufr, 4))
  {
    if (bufr[5] == 'I')
    {
      info->xfer_mode = TYPE_I;
      send_reply(info, 200, "Type set to I.");
    }
    else if (bufr[5] == 'A')
    {
      /* FIXME: ASCII mode isn't actually supported yet. */
      info->xfer_mode = TYPE_A;
      send_reply(info, 200, "Type set to A.");
    }
    else
    {
      info->xfer_mode = TYPE_I;
      send_reply(info, 504, "Type not implemented.  Set to I.");
    }
  }
  else if (!strncmp("PASS", bufr, 4))
  {
    send_reply(info, 230, "User logged in.");
  }
  else if (!strncmp("DELE", bufr, 4))
  {
    sscanf(&bufr[4], "%254s", fname);
    if (unlink(fname) == 0)
    {
      send_reply(info, 257, "DELE successful.");
    }
    else
    {
      send_reply(info, 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)
    {
      send_reply(info, 257, "CHMOD successful.");
    }
    else
    {
      send_reply(info, 550, "CHMOD failed.");
    }
  }
  else if (!strncmp("RMD", bufr, 3))
  {
    sscanf(&bufr[4], "%254s", fname);
    if (rmdir(fname) == 0)
    {
      send_reply(info, 257, "RMD successful.");
    }
    else
    {
      send_reply(info, 550, "RMD failed.");
    }
  }
  else if (!strncmp("MKD", bufr, 3))
  {
    sscanf(&bufr[4], "%254s", fname);
    if (mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
    {
      send_reply(info, 257, "MKD successful.");
    }
    else
    {
      send_reply(info, 550, "MKD failed.");
    }
  }
  else if (!strncmp("CWD", bufr, 3))
  {
    sscanf(&bufr[4], "%254s", fname);
    rtems_ftpd_cwd(info, fname);
  }
  else if (!strncmp("CDUP", bufr, 4))
  {
    rtems_ftpd_cwd(info, "..");
  }
  else if (!strncmp("PWD", bufr, 3))
  {
    char const* cwd = "/";
    if(info->cwd[0])
      cwd = info->cwd;
    snprintf(bufr, FTPD_BUFSIZE,
      "\"%s\" is the current directory.", cwd);
    send_reply(info, 250, bufr);
  }
  else
  {
    send_reply(info, 500, "Unrecognized/unsupported command.");
  }
}


/**************************************************************************
 * Function: 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 daemon task passes the socket         *
 *                              which serves as the control connection.   *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    none                                                                *
 *                                                                        *
 *************************************************************************/
static void
session(rtems_task_argument arg)
{
  char cmd[FTPD_BUFSIZE];
  FTPD_SessionInfo_t  *info = (FTPD_SessionInfo_t  *)arg;
  rtems_event_set set;

  while(1) {
    rtems_event_receive(FTPD_RTEMS_EVENT, RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT,
      &set);

    send_reply(info, 220, FTPD_SERVER_MESSAGE);

    info->cwd[0] = 0;
    info->xfer_mode = TYPE_I;

    while (1)
    {
      if (fgets(cmd, FTPD_BUFSIZE, info->ctrl_fp) == NULL)
      {
        syslog(LOG_INFO, "ftpd: Connection aborted.");
        break;
      }

      if (!strncmp("QUIT", cmd, 4))
      {
        send_reply(info, 221, "Goodbye.");
        break;
      }
      else
      {
        parse_command(info, cmd);
      }
    }

    /* Close connection and put ourselves back into the task pool. */
    close_stream(info);
    task_pool_release(info);
  }
}


/**************************************************************************
 * Function: 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                                                                *
 *                                                                        *
 *************************************************************************/
static void
daemon()
{
  int                 s;
  int                 addrLen;
  struct sockaddr_in  remoteAddr;
  struct sockaddr_in  localAddr;
  char                sessionID;
  FTPD_SessionInfo_t  *info = NULL;


  sessionID = 'a';

  s = socket(PF_INET, SOCK_STREAM, 0);
  if (s < 0)
    syslog(LOG_ERR, "ftpd: Error creating socket: %s", serr());

  localAddr.sin_family      = AF_INET;
  localAddr.sin_port        = htons(rtems_ftpd_configuration.port);
  localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  memset(localAddr.sin_zero, '\0', sizeof(localAddr.sin_zero));

  if (0 > bind(s, (struct sockaddr *)&localAddr, sizeof(localAddr)))
    syslog(LOG_ERR, "ftpd: Error binding control socket: %s", serr());

  if (0 > listen(s, 1))
    syslog(LOG_ERR, "ftpd: Error listening on control socket: %s", serr());

  while (1)
  {
    int ss;
    addrLen = sizeof(remoteAddr);
    ss = accept(s, (struct sockaddr *)&remoteAddr, &addrLen);
    if (0 > ss)
    {
      syslog(LOG_ERR, "ftpd: Error accepting control connection: %s", serr());
    }
    else
    {
      info = task_pool_obtain();
      if (NULL == info)
      {
        close_socket(ss);
      }
      else
      {
        info->socket = ss;
        if ((info->ctrl_fp = fdopen(info->socket, "r+")) == NULL)
        {
          syslog(LOG_ERR, "ftpd: fdopen() on socket failed: %s", serr());
          close_stream(info);
          task_pool_release(info);
        }
        else
        {
          /* Wakeup the session task. The task will call task_pool_release
             after it closes connection. */
          rtems_event_send(info->tid, FTPD_RTEMS_EVENT);
        }
      }
    }
  }
}


/**************************************************************************
 * Function: rtems_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:                                                                *
 *                                                                        *
 * Output:                                                                *
 *                                                                        *
 *    int - RTEMS_SUCCESSFUL on successful start of the daemon.           *
 *                                                                        *
 *************************************************************************/
int
rtems_initialize_ftpd()
{
  rtems_status_code   sc;
  rtems_id            tid;
  rtems_task_priority priority;
  int count;

  if (rtems_ftpd_configuration.port == 0)
  {
    rtems_ftpd_configuration.port = FTPD_CONTROL_PORT;
  }

  if (rtems_ftpd_configuration.priority == 0)
  {
    rtems_ftpd_configuration.priority = 40;
  }
  priority = rtems_ftpd_configuration.priority;

  if (rtems_ftpd_configuration.tasks_count <= 0)
    rtems_ftpd_configuration.tasks_count = 1;
  count = rtems_ftpd_configuration.tasks_count;

  if (!task_pool_init(count, priority))
  {
    syslog(LOG_ERR, "ftpd: Could not initialize task pool.");
    return RTEMS_UNSATISFIED;
  }

  sc = rtems_task_create(rtems_build_name('F', 'T', 'P', 'D'),
    priority, FTPD_STACKSIZE,
    RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR |
    RTEMS_INTERRUPT_LEVEL(0),
    RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
    &tid);

  if (sc == RTEMS_SUCCESSFUL)
  {
    sc = rtems_task_start(tid, daemon, 0);
    if (sc != RTEMS_SUCCESSFUL)
      rtems_task_delete(tid);
  }

  if (sc != RTEMS_SUCCESSFUL)
  {
    task_pool_done(count);
    syslog(LOG_ERR, "ftpd: Could not create/start FTP daemon: %s",
      rtems_status_text(sc));
    return RTEMS_UNSATISFIED;
  }

  root[0] = '\0';
  if (
    rtems_ftpd_configuration.root &&
    strlen(rtems_ftpd_configuration.root) < FTPD_BUFSIZE &&
    rtems_ftpd_configuration.root[0] == '/')
  {
    strcpy(root, rtems_ftpd_configuration.root);
    squeeze_path(root);
    rtems_ftpd_configuration.root = root;
  }

  syslog(LOG_INFO, "ftpd: FTP daemon started (%d session%s max)",
    count, ((count > 1) ? "s" : ""));
  return RTEMS_SUCCESSFUL;
}