00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "includes.h"
00028
00029 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
00030
00031
00032
00033
00034
00035
00036 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
00037 {
00038
00039 if (case_sensitive)
00040 return(strcmp(name1,name2) == 0);
00041
00042 return(strequal(name1,name2));
00043 }
00044
00045
00046
00047
00048
00049 static BOOL mangled_equal(const char *name1, const char *name2,
00050 const struct share_params *p)
00051 {
00052 pstring tmpname;
00053
00054 pstrcpy(tmpname, name2);
00055 mangle_map(tmpname, True, False, p);
00056 return strequal(name1, tmpname);
00057 }
00058
00059
00060
00061
00062
00063 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
00064 {
00065 const char *p;
00066
00067 if (!allow_wcard_last_component) {
00068
00069 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 p = strchr(name, '/');
00081
00082 if (!p && (ms_has_wild(name) || ISDOT(name))) {
00083
00084 return NT_STATUS_OBJECT_NAME_INVALID;
00085 } else {
00086
00087 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 NTSTATUS unix_convert(connection_struct *conn,
00121 pstring name,
00122 BOOL allow_wcard_last_component,
00123 char *saved_last_component,
00124 SMB_STRUCT_STAT *pst)
00125 {
00126 SMB_STRUCT_STAT st;
00127 char *start, *end;
00128 pstring dirpath;
00129 pstring orig_path;
00130 BOOL component_was_mangled = False;
00131 BOOL name_has_wildcard = False;
00132
00133 SET_STAT_INVALID(*pst);
00134
00135 *dirpath = 0;
00136
00137 if(saved_last_component) {
00138 *saved_last_component = 0;
00139 }
00140
00141 if (conn->printer) {
00142
00143
00144 return NT_STATUS_OK;
00145 }
00146
00147 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 #ifdef DEVELOPER
00159 SMB_ASSERT(*name != '/');
00160 #endif
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 if (!*name) {
00171 name[0] = '.';
00172 name[1] = '\0';
00173 if (SMB_VFS_STAT(conn,name,&st) == 0) {
00174 *pst = st;
00175 }
00176 DEBUG(5,("conversion finished \"\" -> %s\n",name));
00177 return NT_STATUS_OK;
00178 }
00179
00180 if (name[0] == '.' && (name[1] == '/' || name[1] == '\0')) {
00181
00182 if (name[1] == '\0' || name[2] == '\0') {
00183 return NT_STATUS_OBJECT_NAME_INVALID;
00184 } else {
00185 return determine_path_error(&name[2], allow_wcard_last_component);
00186 }
00187 }
00188
00189
00190
00191
00192
00193 if(saved_last_component) {
00194 end = strrchr_m(name, '/');
00195 if (end) {
00196 pstrcpy(saved_last_component, end + 1);
00197 } else {
00198 pstrcpy(saved_last_component, name);
00199 }
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
00212 strnorm(name, lp_defaultcase(SNUM(conn)));
00213 }
00214
00215 start = name;
00216 pstrcpy(orig_path, name);
00217
00218 if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
00219 *pst = st;
00220 return NT_STATUS_OK;
00221 }
00222
00223
00224
00225
00226
00227 if (SMB_VFS_STAT(conn,name,&st) == 0) {
00228
00229
00230 const char *p = strstr(name, "/.");
00231 if (p) {
00232 if (p[2] == '/') {
00233
00234 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00235 } else if (p[2] == '\0') {
00236
00237 return NT_STATUS_OBJECT_NAME_INVALID;
00238 }
00239 }
00240 stat_cache_add(orig_path, name, conn->case_sensitive);
00241 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
00242 *pst = st;
00243 return NT_STATUS_OK;
00244 }
00245
00246 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
00247
00248
00249
00250
00251
00252
00253 if (conn->case_sensitive &&
00254 !mangle_is_mangled(name, conn->params) &&
00255 !*lp_mangled_map(conn->params)) {
00256 return NT_STATUS_OK;
00257 }
00258
00259
00260
00261
00262
00263
00264 if (mangle_is_mangled(start, conn->params)) {
00265 component_was_mangled = True;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 for (; start ; start = (end?end+1:(char *)NULL)) {
00279
00280
00281
00282 end = strchr(start, '/');
00283
00284
00285
00286
00287 if (end) {
00288 *end = 0;
00289 }
00290
00291 if (saved_last_component != 0) {
00292 pstrcpy(saved_last_component, end ? end + 1 : start);
00293 }
00294
00295
00296
00297 if (ISDOT(start)) {
00298 if (!end) {
00299
00300 return NT_STATUS_OBJECT_NAME_INVALID;
00301 }
00302 return determine_path_error(end+1, allow_wcard_last_component);
00303 }
00304
00305
00306
00307
00308 name_has_wildcard = ms_has_wild(start);
00309
00310
00311 if (name_has_wildcard && !allow_wcard_last_component) {
00312 return NT_STATUS_OBJECT_NAME_INVALID;
00313 }
00314
00315
00316 if (name_has_wildcard && end) {
00317 return NT_STATUS_OBJECT_NAME_INVALID;
00318 }
00319
00320
00321
00322
00323
00324 if (SMB_VFS_STAT(conn,name, &st) == 0) {
00325
00326
00327
00328
00329 if (end && !(st.st_mode & S_IFDIR)) {
00330
00331
00332
00333 DEBUG(5,("Not a dir %s\n",start));
00334 *end = '/';
00335
00336
00337
00338
00339
00340
00341
00342 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00343 }
00344
00345 if (!end) {
00346
00347
00348
00349
00350
00351
00352 *pst = st;
00353 }
00354
00355 } else {
00356 pstring rest;
00357
00358
00359 SET_STAT_INVALID(st);
00360 *rest = 0;
00361
00362
00363
00364
00365
00366
00367 if (end) {
00368 pstrcpy(rest,end+1);
00369 }
00370
00371
00372 errno = 0;
00373
00374
00375
00376
00377
00378 if (name_has_wildcard ||
00379 !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
00380 if (end) {
00381
00382
00383
00384 DEBUG(5,("Intermediate not found %s\n",start));
00385 *end = '/';
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 if (errno == ENOENT ||
00400 errno == ENOTDIR ||
00401 errno == ELOOP) {
00402 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00403 }
00404 return map_nt_error_from_unix(errno);
00405 }
00406
00407
00408 if ((errno != 0) && (errno != ENOENT) && (errno != EACCES)) {
00409
00410
00411
00412 if (errno == ENOTDIR ||
00413 errno == ELOOP) {
00414 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
00415 }
00416 return map_nt_error_from_unix(errno);
00417 }
00418
00419
00420
00421
00422
00423
00424
00425 if (!conn->case_preserve ||
00426 (mangle_is_8_3(start, False, conn->params) &&
00427 !conn->short_case_preserve)) {
00428 strnorm(start, lp_defaultcase(SNUM(conn)));
00429 }
00430
00431
00432
00433
00434
00435
00436 if (mangle_is_mangled(start, conn->params)) {
00437 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
00438 }
00439
00440 DEBUG(5,("New file %s\n",start));
00441 return NT_STATUS_OK;
00442 }
00443
00444
00445
00446
00447
00448 if (end) {
00449 end = start + strlen(start);
00450 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
00451 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
00452 return map_nt_error_from_unix(ENAMETOOLONG);
00453 }
00454 *end = '\0';
00455 } else {
00456
00457
00458
00459
00460
00461
00462 if (SMB_VFS_STAT(conn,name, &st) == 0) {
00463 *pst = st;
00464 } else {
00465 SET_STAT_INVALID(st);
00466 }
00467 }
00468 }
00469
00470 #ifdef DEVELOPER
00471 if (VALID_STAT(st) && get_delete_on_close_flag(st.st_dev, st.st_ino)) {
00472 return NT_STATUS_DELETE_PENDING;
00473 }
00474 #endif
00475
00476
00477
00478
00479 if (*dirpath) {
00480 pstrcat(dirpath,"/");
00481 }
00482
00483 pstrcat(dirpath,start);
00484
00485
00486
00487
00488
00489
00490 if(!component_was_mangled && !name_has_wildcard) {
00491 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
00492 }
00493
00494
00495
00496
00497 if (end) {
00498 *end = '/';
00499 }
00500 }
00501
00502
00503
00504
00505
00506
00507 if(!component_was_mangled && !name_has_wildcard) {
00508 stat_cache_add(orig_path, name, conn->case_sensitive);
00509 }
00510
00511
00512
00513
00514
00515 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
00516 return NT_STATUS_OK;
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526 NTSTATUS check_name(connection_struct *conn, const pstring name)
00527 {
00528 if (IS_VETO_PATH(conn, name)) {
00529
00530 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
00531 DEBUG(5,("check_name: file path name %s vetoed\n",name));
00532 return map_nt_error_from_unix(ENOENT);
00533 }
00534 }
00535
00536 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
00537 NTSTATUS status = reduce_name(conn,name);
00538 if (!NT_STATUS_IS_OK(status)) {
00539 DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
00540 return status;
00541 }
00542 }
00543
00544 return NT_STATUS_OK;
00545 }
00546
00547
00548
00549
00550
00551
00552 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
00553 {
00554 struct smb_Dir *cur_dir;
00555 const char *dname;
00556 BOOL mangled;
00557 long curpos;
00558
00559 mangled = mangle_is_mangled(name, conn->params);
00560
00561
00562 if (*path == 0)
00563 path = ".";
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580 if (mangled && !conn->case_sensitive) {
00581 mangled = !mangle_check_cache( name, maxlength, conn->params);
00582 }
00583
00584
00585 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
00586 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
00587 return(False);
00588 }
00589
00590
00591 curpos = 0;
00592 while ((dname = ReadDirName(cur_dir, &curpos))) {
00593
00594
00595 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
00596 continue;
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
00611
00612 safe_strcpy(name, dname, maxlength);
00613 CloseDir(cur_dir);
00614 return(True);
00615 }
00616 }
00617
00618 CloseDir(cur_dir);
00619 errno = ENOENT;
00620 return(False);
00621 }