Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source3/libsmb/libsmb_file.c

    r414 r745  
    88   Copyright (C) Derrell Lipman 2003-2008
    99   Copyright (C) Jeremy Allison 2007, 2008
    10    
     10
    1111   This program is free software; you can redistribute it and/or modify
    1212   it under the terms of the GNU General Public License as published by
    1313   the Free Software Foundation; either version 3 of the License, or
    1414   (at your option) any later version.
    15    
     15
    1616   This program is distributed in the hope that it will be useful,
    1717   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1818   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1919   GNU General Public License for more details.
    20    
     20
    2121   You should have received a copy of the GNU General Public License
    2222   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     
    2424
    2525#include "includes.h"
     26#include "libsmb/libsmb.h"
    2627#include "libsmbclient.h"
    2728#include "libsmb_internal.h"
     
    5152        NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
    5253        TALLOC_CTX *frame = talloc_stackframe();
    53        
     54
    5455        if (!context || !context->internal->initialized) {
    55                
    5656                errno = EINVAL;  /* Best I can think of ... */
    5757                TALLOC_FREE(frame);
    5858                return NULL;
    59                
    60         }
    61        
     59        }
     60
    6261        if (!fname) {
    63                
    6462                errno = EINVAL;
    6563                TALLOC_FREE(frame);
    6664                return NULL;
    67                
    68         }
    69        
     65        }
     66
    7067        if (SMBC_parse_path(frame,
    7168                            context,
     
    8279                return NULL;
    8380        }
    84        
     81
    8582        if (!user || user[0] == (char)0) {
    8683                user = talloc_strdup(frame, smbc_getUser(context));
     
    9188                }
    9289        }
    93        
     90
    9491        srv = SMBC_server(frame, context, True,
    9592                          server, share, &workgroup, &user, &password);
    96        
    9793        if (!srv) {
    9894                if (errno == EPERM) errno = EACCES;
     
    10096                return NULL;  /* SMBC_server sets errno */
    10197        }
    102        
     98
    10399        /* Hmmm, the test for a directory is suspect here ... FIXME */
    104        
     100
    105101        if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
    106102                status = NT_STATUS_OBJECT_PATH_INVALID;
    107103        } else {
    108104                file = SMB_MALLOC_P(SMBCFILE);
    109                
    110105                if (!file) {
    111106                        errno = ENOMEM;
     
    113108                        return NULL;
    114109                }
    115                
     110
    116111                ZERO_STRUCTP(file);
    117                
     112
    118113                /*d_printf(">>>open: resolving %s\n", path);*/
    119114                if (!cli_resolve_path(frame, "", context->internal->auth_info,
     
    127122                }
    128123                /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
    129                
     124
    130125                status = cli_open(targetcli, targetpath, flags,
    131126                                   context->internal->share_mode, &fd);
    132127                if (!NT_STATUS_IS_OK(status)) {
    133                        
     128
    134129                        /* Handle the error ... */
    135                        
     130
    136131                        SAFE_FREE(file);
    137132                        errno = SMBC_errno(context, targetcli);
    138133                        TALLOC_FREE(frame);
    139134                        return NULL;
    140                        
    141135                }
    142                
     136
    143137                /* Fill in file struct */
    144                
     138
    145139                file->cli_fd  = fd;
    146140                file->fname   = SMB_STRDUP(fname);
     
    148142                file->offset  = 0;
    149143                file->file    = True;
    150                
     144
    151145                DLIST_ADD(context->internal->files, file);
    152                
     146
    153147                /*
    154148                 * If the file was opened in O_APPEND mode, all write
     
    181175                        }
    182176                }
    183                
     177
    184178                TALLOC_FREE(frame);
    185179                return file;
    186                
    187         }
    188        
     180        }
     181
    189182        /* Check if opendir needed ... */
    190        
     183
    191184        if (!NT_STATUS_IS_OK(status)) {
    192185                int eno = 0;
    193                
     186
    194187                eno = SMBC_errno(context, srv->cli);
    195188                file = smbc_getFunctionOpendir(context)(context, fname);
     
    197190                TALLOC_FREE(frame);
    198191                return file;
    199                
    200         }
    201        
     192        }
     193
    202194        errno = EINVAL; /* FIXME, correct errno ? */
    203195        TALLOC_FREE(frame);
    204196        return NULL;
    205        
    206197}
    207198
     
    215206               mode_t mode)
    216207{
    217        
    218208        if (!context || !context->internal->initialized) {
    219                
    220209                errno = EINVAL;
    221210                return NULL;
    222                
    223         }
    224        
     211        }
     212
    225213        return SMBC_open_ctx(context, path,
    226214                             O_WRONLY | O_CREAT | O_TRUNC, mode);
     
    243231        struct cli_state *targetcli = NULL;
    244232        TALLOC_CTX *frame = talloc_stackframe();
    245        
     233
    246234        /*
    247235         * offset:
     
    254242         */
    255243        off_t offset;
    256        
     244
    257245        if (!context || !context->internal->initialized) {
    258                
    259                 errno = EINVAL;
    260                 TALLOC_FREE(frame);
    261                 return -1;
    262                
    263         }
    264        
     246                errno = EINVAL;
     247                TALLOC_FREE(frame);
     248                return -1;
     249        }
     250
    265251        DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
    266        
     252
    267253        if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
    268254                errno = EBADF;
    269255                TALLOC_FREE(frame);
    270256                return -1;
    271                
    272         }
    273        
     257        }
     258
    274259        offset = file->offset;
    275        
     260
    276261        /* Check that the buffer exists ... */
    277        
     262
    278263        if (buf == NULL) {
    279264                errno = EINVAL;
    280265                TALLOC_FREE(frame);
    281266                return -1;
    282                
    283         }
    284        
     267        }
     268
    285269        /*d_printf(">>>read: parsing %s\n", file->fname);*/
    286270        if (SMBC_parse_path(frame,
     
    298282                return -1;
    299283        }
    300        
     284
    301285        /*d_printf(">>>read: resolving %s\n", path);*/
    302286        if (!cli_resolve_path(frame, "", context->internal->auth_info,
     
    309293        }
    310294        /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
    311        
     295
    312296        ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
    313        
     297
    314298        if (ret < 0) {
    315                
    316299                errno = SMBC_errno(context, targetcli);
    317300                TALLOC_FREE(frame);
    318301                return -1;
    319                
    320         }
    321        
     302        }
     303
    322304        file->offset += ret;
    323        
     305
    324306        DEBUG(4, ("  --> %d\n", ret));
    325        
     307
    326308        TALLOC_FREE(frame);
    327309        return ret;  /* Success, ret bytes of data ... */
    328        
    329310}
    330311
     
    339320               size_t count)
    340321{
    341         int ret;
    342322        off_t offset;
    343323        char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
     
    346326        struct cli_state *targetcli = NULL;
    347327        TALLOC_CTX *frame = talloc_stackframe();
    348        
     328        NTSTATUS status;
     329
    349330        /* First check all pointers before dereferencing them */
    350        
     331
    351332        if (!context || !context->internal->initialized) {
    352                
    353                 errno = EINVAL;
    354                 TALLOC_FREE(frame);
    355                 return -1;
    356                
    357         }
    358        
     333                errno = EINVAL;
     334                TALLOC_FREE(frame);
     335                return -1;
     336        }
     337
    359338        if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
    360339                errno = EBADF;
     
    362341                return -1;
    363342        }
    364        
     343
    365344        /* Check that the buffer exists ... */
    366        
     345
    367346        if (buf == NULL) {
    368347                errno = EINVAL;
    369348                TALLOC_FREE(frame);
    370349                return -1;
    371                
    372         }
    373        
     350        }
     351
    374352        offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
    375        
     353
    376354        /*d_printf(">>>write: parsing %s\n", file->fname);*/
    377355        if (SMBC_parse_path(frame,
     
    400378        }
    401379        /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
    402        
    403         ret = cli_write(targetcli, file->cli_fd,
    404                         0, (char *)buf, offset, count);
    405        
    406         if (ret <= 0) {
    407                 errno = SMBC_errno(context, targetcli);
    408                 TALLOC_FREE(frame);
    409                 return -1;
    410                
    411         }
    412        
    413         file->offset += ret;
    414        
     380
     381        status = cli_writeall(targetcli, file->cli_fd,
     382                              0, (uint8_t *)buf, offset, count, NULL);
     383        if (!NT_STATUS_IS_OK(status)) {
     384                errno = map_errno_from_nt_status(status);
     385                TALLOC_FREE(frame);
     386                return -1;
     387        }
     388
     389        file->offset += count;
     390
    415391        TALLOC_FREE(frame);
    416         return ret;  /* Success, 0 bytes of data ... */
     392        return count;  /* Success, 0 bytes of data ... */
    417393}
    418394
     
    431407        struct cli_state *targetcli = NULL;
    432408        TALLOC_CTX *frame = talloc_stackframe();
    433        
     409
    434410        if (!context || !context->internal->initialized) {
    435                
    436                 errno = EINVAL;
    437                 TALLOC_FREE(frame);
    438                 return -1;
    439         }
    440        
     411                errno = EINVAL;
     412                TALLOC_FREE(frame);
     413                return -1;
     414        }
     415
    441416        if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
    442417                errno = EBADF;
     
    444419                return -1;
    445420        }
    446        
     421
    447422        /* IS a dir ... */
    448423        if (!file->file) {
     
    450425                return smbc_getFunctionClosedir(context)(context, file);
    451426        }
    452        
     427
    453428        /*d_printf(">>>close: parsing %s\n", file->fname);*/
    454429        if (SMBC_parse_path(frame,
     
    466441                return -1;
    467442        }
    468        
     443
    469444        /*d_printf(">>>close: resolving %s\n", path);*/
    470445        if (!cli_resolve_path(frame, "", context->internal->auth_info,
     
    477452        }
    478453        /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
    479        
     454
    480455        if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
    481                
    482456                DEBUG(3, ("cli_close failed on %s. purging server.\n",
    483457                          file->fname));
     
    492466                TALLOC_FREE(frame);
    493467                return -1;
    494                
    495         }
    496        
     468        }
     469
    497470        DLIST_REMOVE(context->internal->files, file);
    498471        SAFE_FREE(file->fname);
    499472        SAFE_FREE(file);
    500473        TALLOC_FREE(frame);
    501        
    502474        return 0;
    503475}
     
    524496        time_t write_time;
    525497        TALLOC_CTX *frame = talloc_stackframe();
    526        
     498
    527499        if (!context || !context->internal->initialized) {
    528                
    529500                errno = EINVAL;
    530501                TALLOC_FREE(frame);
    531502                return False;
    532503        }
    533        
     504
    534505        /* path fixup for . and .. */
    535506        if (strequal(path, ".") || strequal(path, "..")) {
     
    551522        }
    552523        DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
    553        
     524
    554525        if (!cli_resolve_path(frame, "", context->internal->auth_info,
    555526                        srv->cli, fixedpath,
     
    560531                return False;
    561532        }
    562        
     533
    563534        if (!srv->no_pathinfo2 &&
    564             cli_qpathinfo2(targetcli, targetpath,
     535            NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
    565536                           create_time_ts,
    566537                           access_time_ts,
    567538                           write_time_ts,
    568539                           change_time_ts,
    569                            size, mode, ino)) {
     540                           size, mode, ino))) {
    570541                TALLOC_FREE(frame);
    571542                return True;
    572543        }
    573        
     544
    574545        /* if this is NT then don't bother with the getatr */
    575546        if (targetcli->capabilities & CAP_NT_SMBS) {
     
    578549                return False;
    579550        }
    580        
     551
    581552        if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
    582                
    583553                struct timespec w_time_ts;
    584                
     554
    585555                w_time_ts = convert_time_t_to_timespec(write_time);
    586                
    587556                if (write_time_ts != NULL) {
    588557                        *write_time_ts = w_time_ts;
    589558                }
    590                
    591559                if (create_time_ts != NULL) {
    592560                        *create_time_ts = w_time_ts;
    593561                }
    594                
    595562                if (access_time_ts != NULL) {
    596563                        *access_time_ts = w_time_ts;
    597564                }
    598                
    599565                if (change_time_ts != NULL) {
    600566                        *change_time_ts = w_time_ts;
    601567                }
    602                
    603568                srv->no_pathinfo2 = True;
    604569                TALLOC_FREE(frame);
    605570                return True;
    606571        }
    607        
     572
    608573        errno = EPERM;
    609574        TALLOC_FREE(frame);
    610575        return False;
    611        
    612576}
    613577
     
    633597        int ret;
    634598        TALLOC_CTX *frame = talloc_stackframe();
    635        
     599
    636600        /*
    637601         * First, try setpathinfo (if qpathinfo succeeded), for it is the
     
    641605         */
    642606        if (srv->no_pathinfo ||
    643             ! cli_setpathinfo(srv->cli, path,
    644                               create_time,
    645                               access_time,
    646                               write_time,
    647                               change_time,
    648                               mode)) {
    649                
     607            !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
     608                                                   create_time,
     609                                                   access_time,
     610                                                   write_time,
     611                                                   change_time,
     612                                                   mode))) {
     613
    650614                /*
    651615                 * setpathinfo is not supported; go to plan B.
     
    657621                 * supports both times.
    658622                 */
    659                
     623
    660624                /* Don't try {q,set}pathinfo() again, with this server */
    661625                srv->no_pathinfo = True;
    662                
     626
    663627                /* Open the file */
    664628                if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
    665                        
    666629                        errno = SMBC_errno(context, srv->cli);
    667630                        TALLOC_FREE(frame);
    668631                        return -1;
    669632                }
    670                
     633
    671634                /* Set the new attributes */
    672635                ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
     
    674637                                   access_time,
    675638                                   write_time));
    676                
     639
    677640                /* Close the file */
    678641                cli_close(srv->cli, fd);
    679                
     642
    680643                /*
    681644                 * Unfortunately, setattrE() doesn't have a provision for
     
    687650                        ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
    688651                }
    689                
     652
    690653                if (! ret) {
    691654                        errno = SMBC_errno(context, srv->cli);
     
    694657                }
    695658        }
    696        
     659
    697660        TALLOC_FREE(frame);
    698661        return True;
     
    715678        struct cli_state *targetcli = NULL;
    716679        TALLOC_CTX *frame = talloc_stackframe();
    717        
     680
    718681        if (!context || !context->internal->initialized) {
    719                
    720                 errno = EINVAL;
    721                 TALLOC_FREE(frame);
    722                 return -1;
    723         }
    724        
     682                errno = EINVAL;
     683                TALLOC_FREE(frame);
     684                return -1;
     685        }
     686
    725687        if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
    726                
    727688                errno = EBADF;
    728689                TALLOC_FREE(frame);
    729690                return -1;
    730                
    731         }
    732        
     691        }
     692
    733693        if (!file->file) {
    734                
    735694                errno = EINVAL;
    736695                TALLOC_FREE(frame);
    737696                return -1;      /* Can't lseek a dir ... */
    738                
    739         }
    740        
     697        }
     698
    741699        switch (whence) {
    742700        case SEEK_SET:
    743701                file->offset = offset;
    744702                break;
    745                
    746703        case SEEK_CUR:
    747704                file->offset += offset;
    748705                break;
    749                
    750706        case SEEK_END:
    751707                /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
     
    764720                        return -1;
    765721                }
    766                
     722
    767723                /*d_printf(">>>lseek: resolving %s\n", path);*/
    768724                if (!cli_resolve_path(frame, "", context->internal->auth_info,
     
    774730                        return -1;
    775731                }
     732
    776733                /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
    777                
    778                 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
    779                                    &size, NULL, NULL, NULL, NULL, NULL))
    780                 {
     734                if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
     735                                             targetcli, file->cli_fd, NULL,
     736                                             &size, NULL, NULL, NULL, NULL,
     737                                             NULL))) {
    781738                        SMB_OFF_T b_size = size;
    782739                        if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
     
    790747                file->offset = size + offset;
    791748                break;
    792                
    793749        default:
    794750                errno = EINVAL;
    795751                break;
    796                
    797         }
    798        
     752        }
     753
    799754        TALLOC_FREE(frame);
    800755        return file->offset;
    801        
    802756}
    803757
     
    821775        struct cli_state *targetcli = NULL;
    822776        TALLOC_CTX *frame = talloc_stackframe();
    823        
     777
    824778        if (!context || !context->internal->initialized) {
    825                
    826                 errno = EINVAL;
    827                 TALLOC_FREE(frame);
    828                 return -1;
    829         }
    830        
     779                errno = EINVAL;
     780                TALLOC_FREE(frame);
     781                return -1;
     782        }
     783
    831784        if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
    832785                errno = EBADF;
     
    834787                return -1;
    835788        }
    836        
     789
    837790        if (!file->file) {
    838791                errno = EINVAL;
     
    840793                return -1;
    841794        }
    842        
     795
    843796        /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
    844797        if (SMBC_parse_path(frame,
     
    856809                return -1;
    857810        }
    858        
     811
    859812        /*d_printf(">>>fstat: resolving %s\n", path);*/
    860813        if (!cli_resolve_path(frame, "", context->internal->auth_info,
     
    867820        }
    868821        /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
    869        
     822
    870823        if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {
    871824                errno = EINVAL;
     
    873826                return -1;
    874827        }
    875        
     828
    876829        TALLOC_FREE(frame);
    877830        return 0;
    878        
    879831}
Note: See TracChangeset for help on using the changeset viewer.