README.md
1# SFTPGo on Windows with Active Directory Integration + Caddy Static File Server Example
2
3[![SFTPGo on Windows with Active Directory Integration + Caddy Static File Server Example](https://img.youtube.com/vi/M5UcJI8t4AI/0.jpg)](https://www.youtube.com/watch?v=M5UcJI8t4AI)
4
5This is similar to the ldapauthserver example, but is more specific to using Active Directory along with using SFTPGo on a Windows Server.
6
7The Youtube Walkthrough/Tutorial video above goes into considerable more detail, but in short, it walks through setting up SFTPGo on a new Windows Server, and enables the External Authentication feature within SFTPGo, along with my `sftpgo-ldap-http-server` project, to allow for user authentication into SFTPGo to occur through one or more Active Directory connections.
8
9Additionally, I go through using the Caddy web server, to help enable serving of static files, if this is something that would be of interest for you.
10
11To get started, you'll want to download the latest release ZIP package from the [sftpgo-ldap-http-server repository](https://github.com/orware/sftpgo-ldap-http-server).
12
13The ZIP itself contains the `sftpgo-ldap-http-server.exe` file, along with an `OpenLDAP` folder (mainly to help if you want to use TLS for your LDAP connections), and a `Data` which contains a logs folder, a configuration.example.php file, a functions.php file, and the LICENSE and README files.
14
15The video above goes through the whole process, but to get started you'll want to install SFTPGo on your server, and then extract the `sftpgo-ldap-http-server` ZIP file on the server as well into a separate folder. Then you'll want to copy the configuration.example.php file and name it `configuration.php` and begin customizing the settings (e.g. add in your own LDAP settings, along with how you may want to have your folders be created). At the very minimum you'll want to make sure that the home directories are set correctly to how you want the folders to be created for your environment (you don't have to use the virtual folders or really any of the other functionality if you don't need it).
16
17Once configured, from a command prompt window, if you are already in the same folder as where you extracted the `sftpgo-ldap-http-server` ZIP, you may simply call the `sftpgo-ldap-http-server.exe` and it should start up a simple HTTP server on Port 9001 running on localhost (the port can be adjusted via the `configuration.php` file as well). Now all you have to do is point SFTPGo's `external_auth_hook` option to point to `http://localhost:9001/` and you should be able to run some authentication tests (assuming you have all of your settings correct and there are no intermediate issues).
18
19The video above definitely goes through some troubleshooting situations you might find yourself coming across, so while it is long (at about 1 hour, 42 minutes), it may be helpful to review and avoid some issues and just to learn a bit more about SFTPGo and the integration above.
20
21## Example Virtual Folders Configuration (Allowing for Both a Public and Private Folder)
22
23The following can be utilized if you'd like to assign your users both a Private Virtual Folder and Public Virtual Folder.
24
25By itself, the Public Virtual Folder isn't necessarily public, so keep that in mind. Only by combining things together with the Caddy web server (and Caddyfile example configuration down below) can you be successful in making the `F:\files\public` folder from the example public.
26
27```php
28$virtual_folders['example'] = [
29 [
30 //"id" => 0,
31 "name" => "private-#USERNAME#",
32 "mapped_path" => 'F:\files\private\#USERNAME#',
33 //"used_quota_size" => 0,
34 //"used_quota_files" => 0,
35 //"last_quota_update" => 0,
36 "virtual_path" => "/_private",
37 "quota_size" => -1,
38 "quota_files" => -1
39 ],
40 [
41 //"id" => 0,
42 "name" => "public-#USERNAME#",
43 "mapped_path" => 'F:\files\public\#USERNAME#',
44 //"used_quota_size" => 0,
45 //"used_quota_files" => 0,
46 //"last_quota_update" => 0,
47 "virtual_path" => "/_public",
48 "quota_size" => -1,
49 "quota_files" => -1
50 ]
51];
52```
53
54## Example Connection "Output Object" Allowing For No Files in the User's Home Directory ("Root Directory") but Allowing for Files in the Public/Private Virtual Folders
55
56The magic here happens in the "permissions" value, by limiting the root/home directory to just the list/download permissions, and then allowing all permissions on the Public/Private virtual folders.
57
58```php
59$connection_output_objects['example'] = [
60 'status' => 1,
61 'username' => '',
62 'expiration_date' => 0,
63 'home_dir' => '',
64 'uid' => 0,
65 'gid' => 0,
66 'max_sessions' => 0,
67 'quota_size' => 0,
68 'quota_files' => 100000,
69 'permissions' => [
70 "/" => ["list", "download"],
71 "/_private" => ["*"],
72 "/_public" => ["*"],
73 ],
74 'upload_bandwidth' => 0,
75 'download_bandwidth' => 0,
76 'filters' => [
77 'allowed_ip' => [],
78 'denied_ip' => [],
79 ],
80 'public_keys' => [],
81];
82```
83
84## Recommended Usage of Automatic Groups Mode (Limiting by Group Prefix)
85
86The `sftpgo-ldap-http-server` project is able to automatically create virtual folders for any groups your user is a memberof if the automatic mode is turned on. However, by having a specific set of allowed prefixes defined, you can limit things to just those groups that begin with the prefixes you've listed, which can be helpful. The prefix itself will be removed from the group name when added as a virtual folder for the user.
87
88```php
89// If automatic groups mode is disabled, then you have to manually add the allowed groups into $allowed_groups down below:
90// If enabled, then any groups you are a memberof will automatically be added in using the template below.
91$auto_groups_mode = true;
92
93$auto_groups_mode_virtual_folder_template = [
94 [
95 //"id" => 0,
96 "name" => "groups-#GROUP#",
97 "mapped_path" => 'F:\files\groups\#GROUP#',
98 //"used_quota_size" => 0,
99 //"used_quota_files" => 0,
100 //"last_quota_update" => 0,
101 "virtual_path" => "/groups/#GROUP#",
102 "quota_size" => 0,
103 "quota_files" => 100000
104 ]
105];
106
107// Used only when auto groups mode is enabled and will help prevent all your groups from being
108// added into SFTPGo since only groups with the prefixes defined here will be automatically added
109// with prefixes automatically removed when listed as a virtual folder (e.g. a group with name
110// "sftpgo-example" would simply become "example").
111$allowed_group_prefixes = [
112 'sftpgo-'
113];
114```
115
116## Example Caddyfile Configuration You Can Adapt for Your Needs
117
118```shell
119### Re-usable snippets:
120
121(add_static_file_serving_features) {
122
123 # Allow accessing files without requiring .html:
124 try_files {path} {path}.html
125
126 # Enable Static File Server and Directory Browsing:
127 file_server browse
128
129 # Enable templating functionality:
130 templates
131
132 # Enable Compression for Output:
133 encode zstd gzip
134
135 handle_errors {
136 respond "<pre>{http.error.status_code} {http.error.status_text}</pre>"
137 }
138}
139
140(add_hsts_headers) {
141 header {
142 # Enable HTTP Strict Transport Security (HSTS) to force clients to always
143
144 # connect via HTTPS (do not use if only testing)
145 Strict-Transport-Security "max-age=31536000; includeSubDomains"
146
147 # Enable cross-site filter (XSS) and tell browser to block detected attacks
148 X-XSS-Protection "1; mode=block"
149
150 # Prevent some browsers from MIME-sniffing a response away from the declared Content-Type
151 X-Content-Type-Options "nosniff"
152
153 # Disallow the site to be rendered within a frame (clickjacking protection)
154 X-Frame-Options "DENY"
155
156 # keep referrer data off of HTTP connections
157 Referrer-Policy no-referrer-when-downgrade
158 }
159}
160
161(add_logging_with_path) {
162 log {
163 output file "{args.0}" {
164 roll_size 100mb
165 roll_keep 5
166 roll_keep_for 720h
167 }
168
169 format json
170 #format console
171 #format single_field common_log
172 }
173}
174
175### Site Definitions:
176
177public.example.com {
178
179 # Site Root:
180 root * F:\files\public
181
182 import add_logging_with_path "F:\caddy\logs\public_example_com_access.log"
183 import add_static_file_serving_features
184 import add_hsts_headers
185}
186
187
188### Reverse Proxy Definitions:
189
190webdav.example.com {
191 reverse_proxy localhost:9000
192
193 import add_logging_with_path "F:\caddy\logs\webdav_example_com_access.log"
194}
195```
196