d3d9: fix compilation after opengl rework
[vlc.git] / modules / access / unc.c
blobf7b6a7be71c815df331fb23d433e9261460456ea
1 /*****************************************************************************
2 * unc.c: Microsoft Windows Universal Naming Convention input module
3 *****************************************************************************
4 * Copyright (C) 2001-2015 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <io.h>
32 #include <windows.h>
33 #include <lm.h>
35 #include <vlc_common.h>
36 #include <vlc_fs.h>
37 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_input_item.h>
40 #include <vlc_url.h>
41 #include <vlc_keystore.h>
42 #include <vlc_charset.h>
44 #include "smb_common.h"
46 typedef struct
48 int i_smb;
49 uint64_t size;
50 vlc_url_t url;
51 } access_sys_t;
53 static void Win32AddConnection(stream_t *access, const char *server,
54 const char *share, const char *user,
55 const char *pwd)
57 NETRESOURCE net_resource;
58 char remote_name[MAX_PATH];
60 memset(&net_resource, 0, sizeof (net_resource));
61 net_resource.dwType = RESOURCETYPE_DISK;
63 snprintf(remote_name, sizeof (remote_name), "\\\\%s\\%s", server,
64 (share != NULL) ? share + 1 /* skip leading '/' */: "");
66 /* remove trailings '/' */
67 char *delim = strchr(remote_name, '/');
68 if (delim != NULL)
69 *delim = '\0';
71 const char *msg;
72 net_resource.lpRemoteName = ToWide(remote_name);
74 wchar_t *wpwd = pwd ? ToWide(pwd) : NULL;
75 wchar_t *wuser = user ? ToWide(user) : NULL;
77 switch (WNetAddConnection2(&net_resource, wpwd, wuser, 0))
79 case NO_ERROR:
80 msg = "connected to %s";
81 break;
82 case ERROR_ALREADY_ASSIGNED:
83 case ERROR_DEVICE_ALREADY_REMEMBERED:
84 msg = "already connected to %s";
85 break;
86 default:
87 msg = "failed to connect to %s";
89 free(net_resource.lpRemoteName);
90 free(wpwd);
91 free(wuser);
92 msg_Dbg(access, msg, remote_name);
95 /* Build an SMB URI
96 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
97 static int smb_get_uri( stream_t *p_access, char **ppsz_uri,
98 const char *psz_user, const char *psz_pwd,
99 const char *psz_server, const char *psz_share_path,
100 const char *psz_name )
102 assert(psz_server);
103 #define PSZ_SHARE_PATH_OR_NULL psz_share_path ? psz_share_path : ""
104 #define PSZ_NAME_OR_NULL psz_name ? "/" : "", psz_name ? psz_name : ""
105 if( psz_user )
106 Win32AddConnection( p_access, psz_server, psz_share_path,
107 psz_user, psz_pwd );
108 return asprintf( ppsz_uri, "//%s%s%s%s", psz_server, PSZ_SHARE_PATH_OR_NULL,
109 PSZ_NAME_OR_NULL );
112 static int Seek( stream_t *p_access, uint64_t i_pos )
114 access_sys_t *p_sys = p_access->p_sys;
115 int64_t i_ret;
117 if( i_pos >= INT64_MAX )
118 return VLC_EGENERIC;
120 msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
122 i_ret = lseek( p_sys->i_smb, i_pos, SEEK_SET );
123 if( i_ret == -1 )
125 msg_Err( p_access, "seek failed (%s)", vlc_strerror_c(errno) );
126 return VLC_EGENERIC;
129 return VLC_SUCCESS;
132 static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
134 access_sys_t *p_sys = p_access->p_sys;
135 int i_read;
137 i_read = read( p_sys->i_smb, p_buffer, i_len );
138 if( i_read < 0 )
140 msg_Err( p_access, "read failed (%s)", vlc_strerror_c(errno) );
141 i_read = 0;
144 return i_read;
147 static int DirRead(stream_t *p_access, input_item_node_t *p_node)
149 access_sys_t *p_sys = p_access->p_sys;
150 int i_ret = VLC_SUCCESS;
152 struct vlc_readdir_helper rdh;
153 vlc_readdir_helper_init( &rdh, p_access, p_node );
155 // Handle share listing from here. Directory browsing is handled by the
156 // usual filesystem module.
157 SHARE_INFO_1 *p_info;
158 DWORD i_share_enum_res;
159 DWORD i_nb_elem;
160 DWORD i_resume_handle = 0;
161 DWORD i_total_elements; // Unused, but needs to be passed
162 wchar_t *wpsz_host = ToWide( p_sys->url.psz_host );
163 if( wpsz_host == NULL )
164 return VLC_ENOMEM;
167 i_share_enum_res = NetShareEnum( wpsz_host, 1, (LPBYTE*)&p_info,
168 MAX_PREFERRED_LENGTH, &i_nb_elem,
169 &i_total_elements, &i_resume_handle );
170 if( i_share_enum_res == ERROR_SUCCESS ||
171 i_share_enum_res == ERROR_MORE_DATA )
173 for ( DWORD i = 0; i < i_nb_elem; ++i )
175 SHARE_INFO_1 *p_current = p_info + i;
176 if( p_current->shi1_type & STYPE_SPECIAL )
177 continue;
178 char* psz_name = FromWide( p_current->shi1_netname );
179 if( psz_name == NULL )
181 i_ret = VLC_ENOMEM;
182 break;
185 char* psz_path;
186 if( smb_get_uri( p_access, &psz_path, NULL, NULL,
187 p_sys->url.psz_host, p_sys->url.psz_path,
188 psz_name ) < 0 )
190 free( psz_name );
191 i_ret = VLC_ENOMEM;
192 break;
194 // We need to concatenate the scheme before, as the window version
195 // of smb_get_uri generates a path (and the other call site needs
196 // a path). The path is already prefixed by "//" so we just need
197 // to add "file:"
198 char* psz_uri;
199 if( asprintf( &psz_uri, "file:%s", psz_path ) < 0 )
201 free( psz_name );
202 free( psz_path );
203 i_ret = VLC_ENOMEM;
204 break;
206 free( psz_path );
208 i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL,
209 psz_name, ITEM_TYPE_DIRECTORY, ITEM_NET );
210 free( psz_name );
211 free( psz_uri );
214 NetApiBufferFree( p_info );
215 } while( i_share_enum_res == ERROR_MORE_DATA && i_ret == VLC_SUCCESS );
217 free( wpsz_host );
219 vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
221 return i_ret;
224 static int Control( stream_t *p_access, int i_query, va_list args )
226 access_sys_t *sys = p_access->p_sys;
228 switch( i_query )
230 case STREAM_CAN_SEEK:
231 case STREAM_CAN_PAUSE:
232 case STREAM_CAN_CONTROL_PACE:
233 *va_arg( args, bool* ) = true;
234 break;
236 case STREAM_CAN_FASTSEEK:
237 *va_arg( args, bool* ) = false;
238 break;
240 case STREAM_GET_SIZE:
241 if( p_access->pf_readdir != NULL )
242 return VLC_EGENERIC;
243 *va_arg( args, uint64_t * ) = sys->size;
244 break;
246 case STREAM_GET_PTS_DELAY:
247 *va_arg( args, vlc_tick_t * ) = VLC_TICK_FROM_MS(
248 var_InheritInteger( p_access, "network-caching" ) );
249 break;
251 case STREAM_SET_PAUSE_STATE:
252 /* Nothing to do */
253 break;
255 default:
256 return VLC_EGENERIC;
259 return VLC_SUCCESS;
262 static int Open(vlc_object_t *obj)
264 stream_t *access = (stream_t *)obj;
265 vlc_url_t url;
266 vlc_credential credential;
267 char *psz_decoded_path = NULL, *psz_uri = NULL, *psz_var_domain = NULL;
268 int fd;
269 uint64_t size;
270 bool is_dir;
272 if (vlc_UrlParseFixup(&url, access->psz_url) != 0)
274 vlc_UrlClean(&url);
275 return VLC_EGENERIC;
278 if (url.psz_path != NULL)
280 psz_decoded_path = vlc_uri_decode_duplicate(url.psz_path);
281 if (psz_decoded_path == NULL)
283 vlc_UrlClean(&url);
284 return VLC_EGENERIC;
288 vlc_credential_init(&credential, &url);
289 psz_var_domain = var_InheritString(access, "smb-domain");
290 credential.psz_realm = psz_var_domain;
291 vlc_credential_get(&credential, access, "smb-user", "smb-pwd", NULL, NULL);
293 for (;;)
295 struct stat st;
297 if (smb_get_uri(access, &psz_uri,
298 credential.psz_username, credential.psz_password,
299 url.psz_host, psz_decoded_path, NULL ) == -1 )
301 vlc_credential_clean(&credential);
302 free(psz_var_domain);
303 free(psz_decoded_path);
304 vlc_UrlClean(&url);
305 return VLC_ENOMEM;
308 if (stat(psz_uri, &st) == 0)
310 is_dir = S_ISDIR(st.st_mode) != 0;
311 size = st.st_size;
312 break;
315 /* stat() fails with servers or shares. Assume directory. */
316 is_dir = true;
317 size = 0;
319 if (errno != EACCES)
320 break;
322 errno = 0;
323 if (!vlc_credential_get(&credential, access, "smb-user",
324 "smb-pwd", SMB_LOGIN_DIALOG_TITLE,
325 SMB_LOGIN_DIALOG_TEXT, url.psz_host))
326 break;
329 vlc_credential_store(&credential, access);
330 vlc_credential_clean(&credential);
331 free(psz_var_domain);
332 free(psz_decoded_path);
334 /* Init access */
335 access_sys_t *sys = vlc_obj_calloc(obj, 1, sizeof (*sys));
336 if (unlikely(sys == NULL))
338 free(psz_uri);
339 vlc_UrlClean(&url);
340 return VLC_ENOMEM;
343 access->p_sys = sys;
345 if (is_dir)
347 sys->url = url;
348 access->pf_readdir = DirRead;
349 access->pf_control = access_vaDirectoryControlHelper;
350 fd = -1;
352 else
354 access->pf_read = Read;
355 access->pf_control = Control;
356 access->pf_seek = Seek;
357 fd = open(psz_uri, O_RDONLY, 0);
358 vlc_UrlClean(&url);
360 free(psz_uri);
362 sys->size = size;
363 sys->i_smb = fd;
365 return VLC_SUCCESS;
368 static void Close(vlc_object_t *obj)
370 stream_t *access = (stream_t *)obj;
371 access_sys_t *sys = access->p_sys;
373 vlc_UrlClean(&sys->url);
374 close(sys->i_smb);
377 vlc_module_begin()
378 set_shortname("UNC")
379 set_description(N_("UNC input"))
380 set_help(N_("Microsoft Windows networking (UNC) input"))
381 set_capability("access", 0)
382 set_category(CAT_INPUT)
383 set_subcategory(SUBCAT_INPUT_ACCESS)
384 add_string("smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT, false)
385 add_password("smb-pwd", NULL, SMB_PASS_TEXT, SMB_PASS_LONGTEXT)
386 add_string("smb-domain", NULL, SMB_DOMAIN_TEXT, SMB_DOMAIN_LONGTEXT, false)
387 add_shortcut("smb")
388 set_callbacks(Open, Close)
389 vlc_module_end()