Class RoleBasedFTPHandler
- All Implemented Interfaces:
FTPConnectionHandler
This handler uses a Realm to authenticate users and check
role membership for authorization decisions. Users are granted
permissions based on their membership in standard FTP roles.
Role Hierarchy
- ftp-admin - Full access to all operations
- ftp-delete - Can delete, rename (implies ftp-write)
- ftp-write - Can upload, create directories (implies ftp-read)
- ftp-read - Read-only access
Configuration Example
<realm id="ftpRealm" class="org.bluezoo.gumdrop.BasicRealm">
<property name="href">ftp-users.xml</property>
</realm>
<service class="org.bluezoo.gumdrop.ftp.file.RoleBasedFTPService">
<property name="realm" ref="#ftpRealm"/>
<property name="root-directory">/var/ftp</property>
<listener class="org.bluezoo.gumdrop.ftp.FTPListener" port="21"/>
</service>
- Author:
- Chris Burdess
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRoleBasedFTPHandler(Realm realm, FTPFileSystem fileSystem) Creates a new role-based FTP handler. -
Method Summary
Modifier and TypeMethodDescriptionauthenticate(String username, String password, String account, FTPConnectionMetadata metadata) Handles FTP authentication (USER/PASS/ACCT command sequence).connected(FTPConnectionMetadata metadata) Notifies that a new client connection has been established.voiddisconnected(FTPConnectionMetadata metadata) Notifies that the client connection has been closed.getFileSystem(FTPConnectionMetadata metadata) Provides the file system implementation for this connection.Gets the quota manager for this handler, if quota enforcement is enabled.handleSiteCommand(String command, FTPConnectionMetadata metadata) Handles SITE-specific commands.booleanisAuthorized(FTPOperation operation, String path, FTPConnectionMetadata metadata) Checks if the authenticated user is authorized to perform an operation.voidsetQuotaManager(QuotaManager quotaManager) Sets the quota manager for quota enforcement.voidsetWelcomeMessage(String welcomeMessage) Sets a custom welcome message.voidtransferCompleted(String path, boolean upload, long totalBytesTransferred, boolean success, FTPConnectionMetadata metadata) Called when a data transfer has completed.voidtransferProgress(String path, boolean upload, ByteBuffer data, long totalBytesTransferred, FTPConnectionMetadata metadata) Called during data transfer to provide transfer progress updates.voidtransferStarting(String path, boolean upload, long size, FTPConnectionMetadata metadata) Called when a data transfer (upload/download) is starting.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.bluezoo.gumdrop.ftp.FTPConnectionHandler
canStore, getQuota, recordBytesAdded, recordBytesRemoved
-
Constructor Details
-
RoleBasedFTPHandler
Creates a new role-based FTP handler.- Parameters:
realm- the realm for authentication and role checkingfileSystem- the file system to use for file operations
-
-
Method Details
-
setWelcomeMessage
Sets a custom welcome message.- Parameters:
welcomeMessage- the welcome message to display on connection
-
setQuotaManager
Sets the quota manager for quota enforcement.- Parameters:
quotaManager- the quota manager
-
getQuotaManager
Description copied from interface:FTPConnectionHandlerGets the quota manager for this handler, if quota enforcement is enabled.When a quota manager is provided, the FTP connection will check quotas before allowing STOR and APPE commands, and will update usage after successful uploads and deletes.
- Specified by:
getQuotaManagerin interfaceFTPConnectionHandler- Returns:
- the quota manager, or null if quota enforcement is disabled
- See Also:
-
connected
Description copied from interface:FTPConnectionHandlerNotifies that a new client connection has been established.This is called immediately after the TCP connection is accepted and any SSL/TLS handshake is completed. It provides an opportunity to perform early connection-level filtering, logging, or initialization.
The handler can send a custom welcome banner by returning a string, or return null to use the default welcome message.
- Specified by:
connectedin interfaceFTPConnectionHandler- Parameters:
metadata- connection metadata with client and security information- Returns:
- custom welcome banner, or null for default
-
authenticate
public FTPAuthenticationResult authenticate(String username, String password, String account, FTPConnectionMetadata metadata) Description copied from interface:FTPConnectionHandlerHandles FTP authentication (USER/PASS/ACCT command sequence).This method is called to authenticate a user with the provided credentials. The implementation should validate the user against whatever authentication backend is appropriate (database, LDAP, etc.).
Authentication considerations:
- Anonymous FTP (user="anonymous" or "ftp")
- Standard username/password authentication
- Account information for systems that require it
- Rate limiting for failed attempts
- Account status checking (enabled, expired, etc.)
- Specified by:
authenticatein interfaceFTPConnectionHandler- Parameters:
username- the username from USER commandpassword- the password from PASS command (can be null if not provided yet)account- the account from ACCT command (can be null if not provided)metadata- complete connection context- Returns:
- authentication result indicating success, failure, or need for more information
-
getFileSystem
Description copied from interface:FTPConnectionHandlerProvides the file system implementation for this connection.This method is called after successful authentication to get the file system that should be used for this user's session. Different users may get different file system implementations with different access rights, root directories, etc.
Examples:
- Anonymous users get read-only access to public directory
- Regular users get access to their home directory
- Admin users get full file system access
- Virtual users get database-backed file systems
- Specified by:
getFileSystemin interfaceFTPConnectionHandler- Parameters:
metadata- connection context including authenticated user- Returns:
- file system implementation for this user
-
isAuthorized
Description copied from interface:FTPConnectionHandlerChecks if the authenticated user is authorized to perform an operation.This method is called before executing any file or directory operation to verify that the user has the required permissions. The default implementation allows all operations for backward compatibility.
Implementations can use this for:
- Role-based access control using
FTPRoles - Path-based access control (restrict access to certain directories)
- Time-based restrictions (maintenance windows)
- Quota enforcement
Example role-based implementation:
public boolean isAuthorized(FTPOperation operation, String path, FTPConnectionMetadata metadata) { String user = metadata.getAuthenticatedUser(); // Admins can do anything if (realm.isUserInRole(user, FTPRoles.ADMIN)) { return true; } switch (operation) { case READ: case NAVIGATE: return realm.isUserInRole(user, FTPRoles.READ); case WRITE: case CREATE_DIR: return realm.isUserInRole(user, FTPRoles.WRITE); case DELETE: case DELETE_DIR: case RENAME: return realm.isUserInRole(user, FTPRoles.DELETE); default: return false; } }- Specified by:
isAuthorizedin interfaceFTPConnectionHandler- Parameters:
operation- the type of operation being attemptedpath- the file or directory path (may be null for general queries)metadata- connection context including authenticated user- Returns:
- true if the operation is allowed, false to deny with 550 response
- See Also:
- Role-based access control using
-
transferStarting
public void transferStarting(String path, boolean upload, long size, FTPConnectionMetadata metadata) Description copied from interface:FTPConnectionHandlerCalled when a data transfer (upload/download) is starting.This provides an opportunity to set up logging, progress tracking, virus scanning, or other transfer-related processing.
- Specified by:
transferStartingin interfaceFTPConnectionHandler- Parameters:
path- the file path being transferredupload- true for upload (STOR), false for download (RETR)size- expected transfer size in bytes (-1 if unknown)metadata- connection context
-
transferProgress
public void transferProgress(String path, boolean upload, ByteBuffer data, long totalBytesTransferred, FTPConnectionMetadata metadata) Description copied from interface:FTPConnectionHandlerCalled during data transfer to provide transfer progress updates.This method is called periodically during file transfers with chunks of data. It can be used for:
- Progress tracking and reporting
- Real-time virus scanning
- Content filtering
- Transfer rate limiting
- Custom logging
The handler should process the data quickly to avoid slowing down the transfer.
- Specified by:
transferProgressin interfaceFTPConnectionHandler- Parameters:
path- the file path being transferredupload- true for upload, false for downloaddata- the data chunk being transferredtotalBytesTransferred- total bytes transferred so farmetadata- connection context
-
transferCompleted
public void transferCompleted(String path, boolean upload, long totalBytesTransferred, boolean success, FTPConnectionMetadata metadata) Description copied from interface:FTPConnectionHandlerCalled when a data transfer has completed.This provides an opportunity for cleanup, final processing, statistics recording, etc.
- Specified by:
transferCompletedin interfaceFTPConnectionHandler- Parameters:
path- the file path that was transferredupload- true for upload, false for downloadtotalBytesTransferred- total bytes successfully transferredsuccess- true if transfer completed successfully, false if aborted/failedmetadata- connection context
-
handleSiteCommand
Description copied from interface:FTPConnectionHandlerHandles SITE-specific commands.The SITE command allows servers to implement custom, non-standard commands. This method receives the SITE command arguments and can implement whatever custom functionality is needed.
Examples:
- SITE CHMOD - change file permissions
- SITE DISK - show disk usage
- SITE HELP - show custom help
- Specified by:
handleSiteCommandin interfaceFTPConnectionHandler- Parameters:
command- the SITE command and argumentsmetadata- connection context- Returns:
- operation result, or NOT_SUPPORTED if command is not recognized
-
disconnected
Description copied from interface:FTPConnectionHandlerNotifies that the client connection has been closed.This is called when the client disconnects (gracefully with QUIT or abruptly due to network issues). The handler should perform final cleanup and resource management.
Cleanup actions might include:
- Closing files or streams
- Updating connection statistics
- Logging connection summary
- Releasing any held resources
- Specified by:
disconnectedin interfaceFTPConnectionHandler- Parameters:
metadata- final connection context with duration and statistics
-