README.PGSQL 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ----------------------- PostgreSQL SUPPORT ------------------------
  2. When PostgreSQL is enabled, all account info are fetched from a central
  3. Postgres database.
  4. To compile the server with PostgreSQL support, you first have to build and
  5. install the PostgreSQL client libraries. PostgreSQL is freely available from
  6. http://www.postgresql.org/ and binary packages are included in many major
  7. distributions. But if you choose a binary form, don't forget to also install
  8. the development packages if they are available separately.
  9. Then, configure Pure-FTPd with --with-pgsql and your favorite extra gadgets:
  10. ./configure --with-pgsql --with-everything
  11. If your PostgreSQL libraries are installed in a special path, you can specify
  12. it like this:
  13. ./configure --with-pgsql=/opt/pgsql
  14. In this example, headers (like pgsql.h) will be searched in
  15. /opt/pgsql/include and /opt/pgsql/include/pgsql, while related libraries
  16. will be searched in /opt/pgsql/lib and /opt/pgsql/lib/pgsql .
  17. Then, install the server as usual:
  18. make install
  19. ------------------------ PGSQL CONFIGURATION FILE ------------------------
  20. Before running the server, you have to create a configuration file. Why a
  21. configuration file instead of simple command-line options? you may ask.
  22. Because for security reasons, you may want to hide how to connect to your
  23. PostgreSQL server. And as command-line options can be discovered by local users
  24. (with 'ps auxwww' for instance), it's more secure to use a configuration
  25. file for sensitive data. Keep it readable only by root (chmod 600) .
  26. Here's a sample configuration file:
  27. PGSQLServer localhost
  28. PGSQLPort 5432
  29. PGSQLUser root
  30. PGSQLPassword rootpw
  31. PGSQLDatabase pureftpd
  32. PGSQLCrypt cleartext
  33. PGSQLGetPW SELECT "Password" FROM "users" WHERE "User"='\L'
  34. PGSQLGetUID SELECT "Uid" FROM "users" WHERE "User"='\L'
  35. PGSQLGetGID SELECT "Gid" FROM "users" WHERE "User"='\L'
  36. PGSQLGetDir SELECT "Dir" FROM "users" WHERE "User"='\L'
  37. Have a look at the sample pureftpd-pgsql.conf configuration file for
  38. explanations of every keyword.
  39. Save the configuration file anywhere. Let's say /etc/pureftpd-pgsql.conf .
  40. Then, you have to run the pure-ftpd command with '-l pgsql:' (it's an 'ell'
  41. not a 'one') followed by the path of that configuration file. Here's an
  42. example:
  43. pure-ftpd -l pgsql:/etc/pureftpd-pgsql.conf -B
  44. You can mix different authentication methods. For instance, if you want to
  45. use system (/etc/passwd) accounts when an account is not found in a PostgreSQL
  46. database, use -l pgsql:/etc/pureftpd-pgsql.conf -l unix
  47. ------------------------ TABLES STRUCTURES ------------------------
  48. Pure-FTPd is very flexible and users can be stored in any way in SQL tables.
  49. You just have to have fields with the following info:
  50. - The user's login.
  51. - The user's password, hashed using argon2, scrypt or crypt(3). SHA1 and MD5
  52. are also supported for legacy reasons, but shouldn't be used any more.
  53. Pure-FTPd also accepts the "any" value for the PGSQLCrypt field.
  54. With "any", all hash functions are sequentially tried.
  55. * RECOMMENDATION: Do not use SHA1, MD5, or, obviously, plaintext. Unless your
  56. system provides a decent crypt() function, use a PostgreSQL function to verify
  57. the hashed password or use argon2/scrypt.
  58. - The system uid to map the user to. This can be a numeric id or a user
  59. name, looked up at run-time.
  60. - The system gid (numeric or not) .
  61. - The home directory.
  62. Here's a dump of a simple table to handle this:
  63. CREATE TABLE "users" (
  64. "User" TEXT NOT NULL,
  65. "Password" TEXT NOT NULL,
  66. "Uid" INTEGER NOT NULL default '-1',
  67. "Gid" INTEGER NOT NULL default '-1',
  68. "Dir" TEXT NOT NULL,
  69. PRIMARY KEY ("User")
  70. ) WITHOUT OIDS;
  71. Uid and Gid can be VARCHAR instead of INTEGER if you want to use names instead
  72. of values.
  73. Then, in the pureftpd-pgsql.conf configuration file, you have to provide SQL
  74. templates to fetch the needed info.
  75. Let's take the previous example:
  76. PGSQLGetPW SELECT "Password" FROM "users" WHERE "User"='\L'
  77. PGSQLGetUID SELECT "Uid" FROM "users" WHERE "User"='\L'
  78. PGSQLGetGID SELECT "Gid" FROM "users" WHERE "User"='\L'
  79. PGSQLGetDir SELECT "Dir" FROM "users" WHERE "User"='\L'
  80. For each query:
  81. \L is replaced by the login of a user trying to authenticate.
  82. \I is replaced by the IP address the client connected to.
  83. \P is replaced by the port number the client connected to.
  84. \R is replaced by the remote IP address the client connected from.
  85. \D is replaced by the remote IPv4 address, as a long decimal number.
  86. You can mix all of these to store info in various tables. For instance, with
  87. \I, you can have a different table for every domain, so that joe@domain1
  88. won't be the same account than joe@domain2 . And with \R, you can restrict
  89. one account to one specific address.
  90. Please note that a login can only contains common characters: A...Z, a...z,
  91. 0...9, -, ., _, space, :, @ and ' . For security purposes, other characters
  92. are forbidden.
  93. You can also remove uid and gid fields in your tables and use default
  94. values instead (thus saving useless lookups) . Two directives are
  95. useful to serve that purpose: PGSQLDefaultUID and PGSQLDefaultGID.
  96. Obvious example:
  97. PGSQLDefaultUID 1000
  98. PGSQLDefaultGID 1000
  99. Using these directives overrides PGSQLGetUID and PGSQLGetGID.
  100. ------------------------ ARGON2 ------------------------
  101. Password hashed with argon2i and argon2id can be used, provided that pure-ftpd
  102. was linked to libsodium.
  103. They are expected to be provided as a string, as returned by the
  104. crypto_pwhash_str() function or by its bindings.
  105. ------------------------ SCRYPT ------------------------
  106. Password hashed with scrypt can be used, provided that pure-ftpd was linked to
  107. libsodium.
  108. They are expected to be provided in escrypt format, as returned by the
  109. crypto_pwhash_scryptsalsa208sha256_str() function or by its bindings.
  110. For example, the string $7$C6..../....YzvCLmJDYJpH76BxlZB9fCpCEj2AbGQHoLiG9I/VRO1$/enQ.o1BNtmxjxNc/8hbZq8W0JAqR5YpufJXGAdzmf3
  111. would verify the password "test".
  112. ------------------------ PER-USER SETTINGS ------------------------
  113. Individual settings can be set for every user, using optional queries.
  114. - PGSQLGetQTAFS is the maximal number of files a user can store in his home
  115. directory.
  116. Example:
  117. PGSQLGetQTAFS SELECT "QuotaFiles" FROM "users" WHERE "User"='\L'
  118. - PGSQLGetQTASZ is the maximal disk usage, in Megabytes.
  119. Example:
  120. PGSQLGetQTASZ SELECT "QuotaSize" FROM "users" WHERE "User"='\L'
  121. - PGSQLGetRatioUL and PGSQLGetRatioDL are optional ratios.
  122. Example:
  123. PGSQLGetRatioUL SELECT "ULRatio" FROM "users" WHERE "User"='\L'
  124. PGSQLGetRatioDL SELECT "DLRatio" FROM "users" WHERE "User"='\L'
  125. - PGSQLGetBandwidthUL and PGSQLGetBandwidthDL are optional upload and
  126. download bandwidth restrictions. Returned values should be in KB/s.
  127. Example:
  128. PGSQLGetBandwidthUL SELECT "ULBandwidth" FROM "users" WHERE "User"='\L'
  129. PGSQLGetBandwidthDL SELECT "DLBandwidth" FROM "users" WHERE "User"='\L'
  130. ------------------------ ANONYMOUS USERS ------------------------
  131. If you want to accept anonymous users on your FTP server, you don't need to
  132. have any 'ftp' user in the PGSQL directory. But you need to have a system
  133. 'ftp' account on the FTP server.
  134. ------------------------ ROOT USERS ------------------------
  135. If a PGSQL user entry has a root (0) uid and/or gid, Pure-FTPd will refuse
  136. to log them in.
  137. Without this preventive restriction, if your PGSQL server ever gets
  138. compromised, the attacker could also easily compromise the FTP server.
  139. Security barriers are also implemented to avoid bad implications if wrong
  140. data types (eg. binary blobs instead of plain text) are fetched with SQL
  141. queries.
  142. Hint:
  143. PostgreSQL supports views and it's common practice to define a new DB
  144. user, e.g., ftpd and a view of the 'real' user database with just the
  145. bits that the server needs. E.g., if you have virtual domains you
  146. could use:
  147. create view vftpd as select u.vuser, u.domain, u.passwd, d.uid, d.gid,
  148. '/virtual/' || u.domain || '/' || u.vuser || '/./' as homedir
  149. from vusers as u, vdomains as d where u.domain = v.domain;
  150. grant select on vftpd to ftpd;
  151. The definition of homedir shows how views can be used to enforce a
  152. canonical form for home directories - nothing short of defining this
  153. view will allow a user to drop the chroot from their home directory.