config: remove advanced from add_password()
[vlc.git] / modules / access / unc.c
1 /*****************************************************************************
2  * unc.c: Microsoft Windows Universal Naming Convention input module
3  *****************************************************************************
4  * Copyright (C) 2001-2015 VLC authors and VideoLAN
5  *
6  * Authors: Gildas Bazin <gbazin@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
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>
34
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>
43
44 #include "smb_common.h"
45
46 struct access_sys_t
47 {
48     int i_smb;
49     uint64_t size;
50     vlc_url_t url;
51 };
52
53 static void Win32AddConnection(stream_t *access, const char *server,
54                                const char *share, const char *user,
55                                const char *pwd, const char *domain)
56 {
57     NETRESOURCE net_resource;
58     char remote_name[MAX_PATH];
59
60     VLC_UNUSED(domain);
61
62     memset(&net_resource, 0, sizeof (net_resource));
63     net_resource.dwType = RESOURCETYPE_DISK;
64
65     snprintf(remote_name, sizeof (remote_name), "\\\\%s\\%s", server,
66              (share != NULL) ? share + 1 /* skip leading '/' */: "");
67
68     /* remove trailings '/' */
69     char *delim = strchr(remote_name, '/');
70     if (delim != NULL)
71         *delim = '\0';
72
73     const char *msg;
74     net_resource.lpRemoteName = remote_name;
75
76     switch (WNetAddConnection2(&net_resource, pwd, user, 0))
77     {
78         case NO_ERROR:
79             msg = "connected to %s";
80             break;
81         case ERROR_ALREADY_ASSIGNED:
82         case ERROR_DEVICE_ALREADY_REMEMBERED:
83             msg = "already connected to %s";
84             break;
85         default:
86             msg = "failed to connect to %s";
87     }
88     msg_Dbg(access, msg, remote_name);
89 }
90
91 /* Build an SMB URI
92  * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
93 static int smb_get_uri( stream_t *p_access, char **ppsz_uri,
94                         const char *psz_domain,
95                         const char *psz_user, const char *psz_pwd,
96                         const char *psz_server, const char *psz_share_path,
97                         const char *psz_name )
98 {
99     assert(psz_server);
100 #define PSZ_SHARE_PATH_OR_NULL psz_share_path ? psz_share_path : ""
101 #define PSZ_NAME_OR_NULL psz_name ? "/" : "", psz_name ? psz_name : ""
102     if( psz_user )
103         Win32AddConnection( p_access, psz_server, psz_share_path,
104                             psz_user, psz_pwd, psz_domain );
105     return asprintf( ppsz_uri, "//%s%s%s%s", psz_server, PSZ_SHARE_PATH_OR_NULL,
106                      PSZ_NAME_OR_NULL );
107 }
108
109 static int Seek( stream_t *p_access, uint64_t i_pos )
110 {
111     access_sys_t *p_sys = p_access->p_sys;
112     int64_t      i_ret;
113
114     if( i_pos >= INT64_MAX )
115         return VLC_EGENERIC;
116
117     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
118
119     i_ret = lseek( p_sys->i_smb, i_pos, SEEK_SET );
120     if( i_ret == -1 )
121     {
122         msg_Err( p_access, "seek failed (%s)", vlc_strerror_c(errno) );
123         return VLC_EGENERIC;
124     }
125
126     return VLC_SUCCESS;
127 }
128
129 static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
130 {
131     access_sys_t *p_sys = p_access->p_sys;
132     int i_read;
133
134     i_read = read( p_sys->i_smb, p_buffer, i_len );
135     if( i_read < 0 )
136     {
137         msg_Err( p_access, "read failed (%s)", vlc_strerror_c(errno) );
138         i_read = 0;
139     }
140
141     return i_read;
142 }
143
144 static int DirRead(stream_t *p_access, input_item_node_t *p_node)
145 {
146     access_sys_t *p_sys = p_access->p_sys;
147     int i_ret = VLC_SUCCESS;
148
149     struct vlc_readdir_helper rdh;
150     vlc_readdir_helper_init( &rdh, p_access, p_node );
151
152     // Handle share listing from here. Directory browsing is handled by the
153     // usual filesystem module.
154     SHARE_INFO_1 *p_info;
155     DWORD i_share_enum_res;
156     DWORD i_nb_elem;
157     DWORD i_resume_handle = 0;
158     DWORD i_total_elements; // Unused, but needs to be passed
159     wchar_t *wpsz_host = ToWide( p_sys->url.psz_host );
160     if( wpsz_host == NULL )
161         return VLC_ENOMEM;
162     do
163     {
164         i_share_enum_res = NetShareEnum( wpsz_host, 1, (LPBYTE*)&p_info,
165                               MAX_PREFERRED_LENGTH, &i_nb_elem,
166                               &i_total_elements, &i_resume_handle );
167         if( i_share_enum_res == ERROR_SUCCESS ||
168             i_share_enum_res == ERROR_MORE_DATA )
169         {
170             for ( DWORD i = 0; i < i_nb_elem; ++i )
171             {
172                 SHARE_INFO_1 *p_current = p_info + i;
173                 if( p_current->shi1_type & STYPE_SPECIAL )
174                     continue;
175                 char* psz_name = FromWide( p_current->shi1_netname );
176                 if( psz_name == NULL )
177                 {
178                     i_ret = VLC_ENOMEM;
179                     break;
180                 }
181
182                 char* psz_path;
183                 if( smb_get_uri( p_access, &psz_path, NULL, NULL, NULL,
184                                  p_sys->url.psz_host, p_sys->url.psz_path,
185                                  psz_name ) < 0 )
186                 {
187                     free( psz_name );
188                     i_ret = VLC_ENOMEM;
189                     break;
190                 }
191                 // We need to concatenate the scheme before, as the window version
192                 // of smb_get_uri generates a path (and the other call site needs
193                 // a path). The path is already prefixed by "//" so we just need
194                 // to add "file:"
195                 char* psz_uri;
196                 if( asprintf( &psz_uri, "file:%s", psz_path ) < 0 )
197                 {
198                     free( psz_name );
199                     free( psz_path );
200                     i_ret = VLC_ENOMEM;
201                     break;
202                 }
203                 free( psz_path );
204
205                 i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL,
206                                     psz_name, ITEM_TYPE_DIRECTORY, ITEM_NET );
207                 free( psz_name );
208                 free( psz_uri );
209             }
210         }
211         NetApiBufferFree( p_info );
212     } while( i_share_enum_res == ERROR_MORE_DATA && i_ret == VLC_SUCCESS );
213
214     free( wpsz_host );
215
216     vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
217
218     return i_ret;
219 }
220
221 static int Control( stream_t *p_access, int i_query, va_list args )
222 {
223     access_sys_t *sys = p_access->p_sys;
224
225     switch( i_query )
226     {
227     case STREAM_CAN_SEEK:
228     case STREAM_CAN_PAUSE:
229     case STREAM_CAN_CONTROL_PACE:
230         *va_arg( args, bool* ) = true;
231         break;
232
233     case STREAM_CAN_FASTSEEK:
234         *va_arg( args, bool* ) = false;
235         break;
236
237     case STREAM_GET_SIZE:
238         if( p_access->pf_readdir != NULL )
239             return VLC_EGENERIC;
240         *va_arg( args, uint64_t * ) = sys->size;
241         break;
242
243     case STREAM_GET_PTS_DELAY:
244         *va_arg( args, int64_t * ) = INT64_C(1000)
245             * var_InheritInteger( p_access, "network-caching" );
246         break;
247
248     case STREAM_SET_PAUSE_STATE:
249         /* Nothing to do */
250         break;
251
252     default:
253         return VLC_EGENERIC;
254     }
255
256     return VLC_SUCCESS;
257 }
258
259 static int Open(vlc_object_t *obj)
260 {
261     stream_t *access = (stream_t *)obj;
262     vlc_url_t url;
263     vlc_credential credential;
264     char *psz_decoded_path = NULL, *psz_uri = NULL, *psz_var_domain = NULL;
265     int fd;
266     uint64_t size;
267     bool is_dir;
268
269     if (vlc_UrlParseFixup(&url, access->psz_url) != 0)
270     {
271         vlc_UrlClean(&url);
272         return VLC_EGENERIC;
273     }
274
275     if (url.psz_path != NULL)
276     {
277         psz_decoded_path = vlc_uri_decode_duplicate(url.psz_path);
278         if (psz_decoded_path == NULL)
279         {
280             vlc_UrlClean(&url);
281             return VLC_EGENERIC;
282         }
283     }
284
285     vlc_credential_init(&credential, &url);
286     psz_var_domain = var_InheritString(access, "smb-domain");
287     credential.psz_realm = psz_var_domain;
288     vlc_credential_get(&credential, access, "smb-user", "smb-pwd", NULL, NULL);
289
290     for (;;)
291     {
292         struct stat st;
293
294         if (smb_get_uri(access, &psz_uri, credential.psz_realm,
295                         credential.psz_username, credential.psz_password,
296                         url.psz_host, psz_decoded_path, NULL ) == -1 )
297         {
298             vlc_credential_clean(&credential);
299             free(psz_var_domain);
300             free(psz_decoded_path);
301             vlc_UrlClean(&url);
302             return VLC_ENOMEM;
303         }
304
305         if (stat(psz_uri, &st) == 0)
306         {
307             is_dir = S_ISDIR(st.st_mode) != 0;
308             size = st.st_size;
309             break;
310         }
311
312         /* stat() fails with servers or shares. Assume directory. */
313         is_dir = true;
314         size = 0;
315
316         if (errno != EACCES)
317             break;
318
319         errno = 0;
320         if (!vlc_credential_get(&credential, access, "smb-user",
321                                 "smb-pwd", SMB_LOGIN_DIALOG_TITLE,
322                                 SMB_LOGIN_DIALOG_TEXT, url.psz_host))
323             break;
324     }
325
326     vlc_credential_store(&credential, access);
327     vlc_credential_clean(&credential);
328     free(psz_var_domain);
329     free(psz_decoded_path);
330
331     /* Init access */
332     access_sys_t *sys = vlc_obj_calloc(obj, 1, sizeof (*sys));
333     if (unlikely(sys == NULL))
334     {
335         free(psz_uri);
336         vlc_UrlClean(&url);
337         return VLC_ENOMEM;
338     }
339
340     access->p_sys = sys;
341
342     if (is_dir)
343     {
344         sys->url = url;
345         access->pf_readdir = DirRead;
346         access->pf_control = access_vaDirectoryControlHelper;
347     }
348     else
349     {
350         access->pf_read = Read;
351         access->pf_control = Control;
352         access->pf_seek = Seek;
353         fd = open(psz_uri, O_RDONLY, 0);
354         vlc_UrlClean(&url);
355     }
356     free(psz_uri);
357
358     sys->size = size;
359     sys->i_smb = fd;
360
361     return VLC_SUCCESS;
362 }
363
364 static void Close(vlc_object_t *obj)
365 {
366     stream_t *access = (stream_t *)obj;
367     access_sys_t *sys = access->p_sys;
368
369     vlc_UrlClean(&sys->url);
370     close(sys->i_smb);
371 }
372
373 vlc_module_begin()
374     set_shortname("UNC")
375     set_description(N_("UNC input"))
376     set_help(N_("Microsoft Windows networking (UNC) input"))
377     set_capability("access", 0)
378     set_category(CAT_INPUT)
379     set_subcategory(SUBCAT_INPUT_ACCESS)
380     add_string("smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT, false)
381     add_password("smb-pwd", NULL, SMB_PASS_TEXT, SMB_PASS_LONGTEXT)
382     add_string("smb-domain", NULL, SMB_DOMAIN_TEXT, SMB_DOMAIN_LONGTEXT, false)
383     add_shortcut("smb")
384     set_callbacks(Open, Close)
385 vlc_module_end()