Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

FTP Connect, Examine Server Certificate, and then Authenticate

See more FTP Examples

Demonstrates how to connect to an FTP server, examine the server's SSL/TLS certificate, and then, if it meets the application's security requirements, proceed to authenticate.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes Chilkat Ftp2 to have been previously unlocked.
    // See Unlock Ftp2 for sample code.

    const ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();

    ftp.setHostname("www.authtls-ftps-server.com");
    ftp.setUsername("FTP_LOGIN");
    ftp.setPassword("FTP_PASSWORD");
    ftp.setAuthTls(true);
    ftp.setPort(21);

    // Connect to the FTP server using explicit TLS (AUTH TLS).
    ftp.connectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Get the FTP server's certificate.
    const server_cert = try chilkat.Cert.init();
    defer server_cert.deinit();
    ftp.getServerCert(server_cert) catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Now that we have the certificate, we can check it in any way we desire.
    // (See the online reference documentation for the certificate object's methods
    // and properties)...

    // Assuming the certificate is OK, proceed to authenticate with the FTP server.
    ftp.loginAfterConnectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Proceed with uploading/download files, etc...
    //

    ftp.disconnect() catch {};
    std.debug.print("Success.\n", .{});
}