README.Authentication-Modules 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ------------------------ AUTHENTICATION MODULES ------------------------
  2. Anybody can add new custom authentication methods to Pure-FTPd without
  3. recompiling anything, using "authentication modules".
  4. To enable it, you must ./configure with --with-extauth, or
  5. --with-everything. Linux binary packages have it enabled by default.
  6. Here's how they work:
  7. 1) A client connects to the FTP server and issues a login/password pair.
  8. 2) The FTP server connects to a local separate daemon, called 'pure-authd'.
  9. Data transmitted to that daemon is: user's login, user's password, the IP
  10. address that user connected to, the local port that user connected to and
  11. the user's remote IP address.
  12. 3) pure-authd spawns an authentication program. It can be anything,
  13. including a shell script. The program is given the collected info (login,
  14. password, IP addresses, etc) as environment variables.
  15. 4) The authentication program replies (to the standard output) with the
  16. user's home directory, quota, ratio, bandwidth and if authentication was
  17. successful or not.
  18. 5) pure-authd relays this info to pure-ftpd.
  19. This method is a bit slower than built-in authentication methods. But it's
  20. very flexible as anyone can easily write his own authentication programs.
  21. And they can run non-root, chrooted, with limited capabilities, etc.
  22. Communication between pure-ftpd and pure-authd is done through a local Unix
  23. socket. It's recommended to put that socket in a directory where non-trusted
  24. users have no write access to.
  25. Authentication programs can read the following environment variables to get
  26. info about the user trying to authenticate:
  27. AUTHD_ACCOUNT
  28. AUTHD_PASSWORD
  29. AUTHD_LOCAL_IP
  30. AUTHD_LOCAL_PORT
  31. AUTHD_REMOTE_IP
  32. AUTHD_ENCRYPTED
  33. AUTHD_CLIENT_SNI_NAME
  34. They are self-explanatory. Previous global environment variables aren't
  35. cleared when the script is called. The content of these variables is
  36. _not_ quoted. They may contain special characters. They are under the
  37. control of a possibly malicious remote user.
  38. The program must respond on the standard output with lines like:
  39. auth_ok:1
  40. uid:42
  41. gid:21
  42. dir:/home/j
  43. end
  44. Note the final 'end' keyword. It's mandatory.
  45. Here's the list of recognized tokens ('xxx' has of course to be filled):
  46. * auth_ok:xxx
  47. If xxx is 0, the user was not found (the next authentication method passed
  48. to pure\-ftpd will be tried) . If xxx is \-1, the user was found, but there
  49. was a fatal authentication error: user is root, password is wrong, account
  50. has expired, etc (next authentication methods will not be tried) . If xxx is
  51. 1, the user was found and successfully authenticated.
  52. * uid:xxx
  53. The system uid to be assigned to that user. Must be > 0.
  54. * gid:xxx
  55. The primary system gid. Must be > 0.
  56. * dir:xxx
  57. The absolute path to the home directory. Can contain /./ for a chroot jail.
  58. *slow_tilde_expansion:xxx (optional, default is 1)
  59. When the command 'cd ~user' is issued, it's handy to go to that user's home
  60. directory, as expected in a shell environment. But fetching account info can
  61. be an expensive operation for non-system accounts. If xxx is 0, 'cd ~user'
  62. will expand to the system user home directory. If xxx is 1, 'cd ~user' won't
  63. expand. You should use 1 in most cases with external authentication, when
  64. your FTP users don't match system users. You can also set xxx to 1 if you're
  65. using slow nss_* system authentication modules.
  66. * throttling_bandwidth_ul:xxx (optional)
  67. The allocated bandwidth for uploads, in bytes per second.
  68. * throttling_bandwidth_dl:xxx (optional)
  69. The allocated bandwidth for downloads, in bytes per second.
  70. *user_quota_size:xxx (optional)
  71. The maximal total size for this account, in bytes.
  72. * user_quota_files:xxx (optional)
  73. The maximal number of files for this account.
  74. * ratio_upload:xxx and radio_download:xxx (optional)
  75. The user must match a ratio_upload:ratio_download ratio.
  76. * per_user_max:xxx (optional)
  77. The maximal authorized number of concurrent sessions.
  78. ------------------------ EXAMPLE ------------------------
  79. Here's a very basic example. Our sample authentication program will only
  80. accept user 'john' with any password and return a fixed home directory and
  81. uid/gid.
  82. #! /bin/sh
  83. if test "$AUTHD_ACCOUNT" = "john"; then
  84. echo 'auth_ok:1'
  85. echo 'uid:69'
  86. echo 'gid:42'
  87. echo 'dir:/tmp'
  88. else
  89. echo 'auth_ok:0'
  90. fi
  91. echo 'end'
  92. Let's say we save this file as /usr/bin/ftp-auth-handler
  93. Now, we have to run pure-authd and pure-ftpd, to connect them through a
  94. local socket and to tell pure-ftpd to use our external authentication module:
  95. pure-authd -s /var/run/ftpd.sock -r /usr/bin/ftp-auth-handler &
  96. pure-ftpd -lextauth:/var/run/ftpd.sock &
  97. That's all. Now, we can only log in as 'john', as all FTP authentication is
  98. done by the shell script.