s3: Change SMB_VFS_CREATE_FILE to take a create_file_flags argument
[metze/samba/wip.git] / source3 / printing / nt_printing.c
index ec4e8c59d50965499673a95502c633472053d7a3..ba1fb4352c332455d07ce7f326b36b24252bd77d 100644 (file)
@@ -21,8 +21,6 @@
 
 #include "includes.h"
 
-extern struct current_user current_user;
-
 static TDB_CONTEXT *tdb_forms; /* used for forms files */
 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
 static TDB_CONTEXT *tdb_printers; /* used for printers files */
@@ -72,6 +70,15 @@ const struct generic_mapping printserver_std_mapping = {
        SERVER_ALL_ACCESS
 };
 
+/* Map generic permissions to job object specific permissions */
+
+const struct generic_mapping job_generic_mapping = {
+       JOB_READ,
+       JOB_WRITE,
+       JOB_EXECUTE,
+       JOB_ALL_ACCESS
+};
+
 /* We need one default form to support our default printer. Msoft adds the
 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
 array index). Letter is always first, so (for the current code) additions
@@ -273,7 +280,7 @@ static bool upgrade_to_version_3(void)
        DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
  
        for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
-                       newkey = tdb_nextkey(tdb_drivers, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
+                       newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
 
                dbuf = tdb_fetch(tdb_drivers, kbuf);
 
@@ -344,7 +351,6 @@ static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
        int result, i;
        uint32 sd_size;
        size_t size_new_sec;
-       DOM_SID sid;
 
        if (!data.dptr || data.dsize == 0) {
                return 0;
@@ -358,7 +364,7 @@ static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
 
        ZERO_STRUCT( ps );
 
-       prs_init( &ps, 0, ctx, UNMARSHALL );
+       prs_init_empty( &ps, ctx, UNMARSHALL );
        prs_give_memory( &ps, (char *)data.dptr, data.dsize, False );
 
        if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_orig, &ps, 1 ) ) {
@@ -405,10 +411,10 @@ static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
 
        /* create a new SEC_DESC with the appropriate owner and group SIDs */
 
-       string_to_sid(&sid, "S-1-5-32-544" );
        new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
-               &sid, &sid,
-               NULL, NULL, &size_new_sec );
+                                &global_sid_Builtin_Administrators,
+                                &global_sid_Builtin_Administrators,
+                                NULL, NULL, &size_new_sec );
        if (!new_sec) {
                prs_mem_free( &ps );
                return 0;
@@ -429,8 +435,12 @@ static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
 
        /* store it back */
        
-       sd_size = sec_desc_size(sd_store->sd) + sizeof(SEC_DESC_BUF);
-       prs_init(&ps, sd_size, ctx, MARSHALL);
+       sd_size = ndr_size_security_descriptor(sd_store->sd, 0)
+               + sizeof(SEC_DESC_BUF);
+       if ( !prs_init(&ps, sd_size, ctx, MARSHALL) ) {
+               DEBUG(0,("sec_desc_upg_fn: Failed to allocate prs memory for %s\n", key.dptr ));
+               return 0;
+       }
 
        if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_store, &ps, 1 ) ) {
                DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
@@ -649,7 +659,7 @@ bool nt_printing_init(struct messaging_context *msg_ctx)
        if ( lp_security() == SEC_ADS ) {
                win_rc = check_published_printers();
                if (!W_ERROR_IS_OK(win_rc))
-                       DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", dos_errstr(win_rc)));
+                       DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", win_errstr(win_rc)));
        }
 
        return True;
@@ -797,7 +807,7 @@ int get_ntforms(nt_forms_struct **list)
 
        for (kbuf = tdb_firstkey(tdb_forms);
             kbuf.dptr;
-            newkey = tdb_nextkey(tdb_forms, kbuf), safe_free(kbuf.dptr), kbuf=newkey) 
+            newkey = tdb_nextkey(tdb_forms, kbuf), free(kbuf.dptr), kbuf=newkey) 
        {
                if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0) 
                        continue;
@@ -1014,7 +1024,7 @@ int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
 
        for (kbuf = tdb_firstkey(tdb_drivers);
             kbuf.dptr;
-            newkey = tdb_nextkey(tdb_drivers, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
+            newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
 
                if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
                        continue;
@@ -1096,7 +1106,7 @@ static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32
        }
 
        /* Skip OEM header (if any) and the DOS stub to start of Windows header */
-       if (SMB_VFS_LSEEK(fsp, fsp->fh->fd, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
+       if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
                DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
                                fname, errno));
                /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
@@ -1117,7 +1127,7 @@ static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32
                unsigned int section_table_bytes;
 
                /* Just skip over optional header to get to section table */
-               if (SMB_VFS_LSEEK(fsp, fsp->fh->fd,
+               if (SMB_VFS_LSEEK(fsp,
                                SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
                                SEEK_CUR) == (SMB_OFF_T)-1) {
                        DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
@@ -1163,7 +1173,7 @@ static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32
                                }
 
                                /* Seek to the start of the .rsrc section info */
-                               if (SMB_VFS_LSEEK(fsp, fsp->fh->fd, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
+                               if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
                                        DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
                                                        fname, errno));
                                        goto error_exit;
@@ -1259,7 +1269,7 @@ static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32
                                 * twice, as it is simpler to read the code. */
                                if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
                                        /* Compute skip alignment to next long address */
-                                       int skip = -(SMB_VFS_LSEEK(fsp, fsp->fh->fd, 0, SEEK_CUR) - (byte_count - i) +
+                                       int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
                                                                 sizeof(VS_SIGNATURE)) & 3;
                                        if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
 
@@ -1334,14 +1344,24 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
                goto error_exit;
        }
 
-       status = open_file_ntcreate(conn, NULL, filepath, &stat_buf,
-                               FILE_GENERIC_READ,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE,
-                               FILE_OPEN,
-                               0,
-                               FILE_ATTRIBUTE_NORMAL,
-                               INTERNAL_OPEN_ONLY,
-                               NULL, &fsp);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               NULL,                                   /* req */
+               0,                                      /* root_dir_fid */
+               filepath,                               /* fname */
+               0,                                      /* create_file_flags */
+               FILE_GENERIC_READ,                      /* access_mask */
+               FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               NULL,                                   /* pinfo */
+               &stat_buf);                             /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                /* Old file not found, so by definition new file is in fact newer */
@@ -1359,14 +1379,15 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
                        DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
                                         old_file));
                        use_version = false;
-                       if (SMB_VFS_FSTAT(fsp, fsp->fh->fd, &st) == -1) {
+                       if (SMB_VFS_FSTAT(fsp, &st) == -1) {
                                 goto error_exit;
                        }
                        old_create_time = st.st_mtime;
                        DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", old_create_time));
                }
        }
-       close_file(fsp, NORMAL_CLOSE);
+       close_file(NULL, fsp, NORMAL_CLOSE);
+       fsp = NULL;
 
        /* Get file version info (if available) for new file */
        filepath = driver_unix_convert(conn,new_file,&stat_buf);
@@ -1374,14 +1395,24 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
                goto error_exit;
        }
 
-       status = open_file_ntcreate(conn, NULL, filepath, &stat_buf,
-                               FILE_GENERIC_READ,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE,
-                               FILE_OPEN,
-                               0,
-                               FILE_ATTRIBUTE_NORMAL,
-                               INTERNAL_OPEN_ONLY,
-                               NULL, &fsp);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               NULL,                                   /* req */
+               0,                                      /* root_dir_fid */
+               filepath,                               /* fname */
+               0,                                      /* create_file_flags */
+               FILE_GENERIC_READ,                      /* access_mask */
+               FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               NULL,                                   /* pinfo */
+               &stat_buf);                             /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                /* New file not found, this shouldn't occur if the caller did its job */
@@ -1399,14 +1430,15 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
                        DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
                                         new_file));
                        use_version = false;
-                       if (SMB_VFS_FSTAT(fsp, fsp->fh->fd, &st) == -1) {
+                       if (SMB_VFS_FSTAT(fsp, &st) == -1) {
                                goto error_exit;
                        }
                        new_create_time = st.st_mtime;
                        DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", new_create_time));
                }
        }
-       close_file(fsp, NORMAL_CLOSE);
+       close_file(NULL, fsp, NORMAL_CLOSE);
+       fsp = NULL;
 
        if (use_version && (new_major != old_major || new_minor != old_minor)) {
                /* Compare versions and choose the larger version number */
@@ -1435,25 +1467,28 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
 
        error_exit:
                if(fsp)
-                       close_file(fsp, NORMAL_CLOSE);
+                       close_file(NULL, fsp, NORMAL_CLOSE);
                return -1;
 }
 
 /****************************************************************************
 Determine the correct cVersion associated with an architecture and driver
 ****************************************************************************/
-static uint32 get_correct_cversion(const char *architecture, fstring driverpath_in,
-                                  struct current_user *user, WERROR *perr)
+static uint32 get_correct_cversion(struct pipes_struct *p,
+                                  const char *architecture,
+                                  fstring driverpath_in,
+                                  WERROR *perr)
 {
        int               cversion;
        NTSTATUS          nt_status;
        char *driverpath = NULL;
-       DATA_BLOB         null_pw;
-       fstring           res_type;
        files_struct      *fsp = NULL;
        SMB_STRUCT_STAT   st;
-       connection_struct *conn;
+       connection_struct *conn = NULL;
        NTSTATUS status;
+       char *oldcwd;
+       fstring printdollar;
+       int printdollar_snum;
 
        SET_STAT_INVALID(st);
 
@@ -1473,28 +1508,21 @@ static uint32 get_correct_cversion(const char *architecture, fstring driverpath_
                return 3;
        }
 
-       /*
-        * Connect to the print$ share under the same account as the user connected
-        * to the rpc pipe. Note we must still be root to do this.
-        */
+       fstrcpy(printdollar, "print$");
 
-       /* Null password is ok - we are already an authenticated user... */
-       null_pw = data_blob_null;
-       fstrcpy(res_type, "A:");
-       become_root();
-       conn = make_connection_with_chdir("print$", null_pw, res_type, user->vuid, &nt_status);
-       unbecome_root();
-
-       if (conn == NULL) {
-               DEBUG(0,("get_correct_cversion: Unable to connect\n"));
-               *perr = ntstatus_to_werror(nt_status);
+       printdollar_snum = find_service(printdollar);
+       if (printdollar_snum == -1) {
+               *perr = WERR_NO_SUCH_SHARE;
                return -1;
        }
 
-       /* We are temporarily becoming the connection user. */
-       if (!become_user(conn, user->vuid)) {
-               DEBUG(0,("get_correct_cversion: Can't become user!\n"));
-               *perr = WERR_ACCESS_DENIED;
+       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+                                      lp_pathname(printdollar_snum),
+                                      p->server_info, &oldcwd);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0,("get_correct_cversion: create_conn_struct "
+                        "returned %s\n", nt_errstr(nt_status)));
+               *perr = ntstatus_to_werror(nt_status);
                return -1;
        }
 
@@ -1520,14 +1548,24 @@ static uint32 get_correct_cversion(const char *architecture, fstring driverpath_
                goto error_exit;
        }
 
-       status = open_file_ntcreate(conn, NULL, driverpath, &st,
-                               FILE_GENERIC_READ,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE,
-                               FILE_OPEN,
-                               0,
-                               FILE_ATTRIBUTE_NORMAL,
-                               INTERNAL_OPEN_ONLY,
-                               NULL, &fsp);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               NULL,                                   /* req */
+               0,                                      /* root_dir_fid */
+               driverpath,                             /* fname */
+               0,                                      /* create_file_flags */
+               FILE_GENERIC_READ,                      /* access_mask */
+               FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               NULL,                                   /* pinfo */
+               &st);                                   /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = %d\n",
@@ -1571,27 +1609,28 @@ static uint32 get_correct_cversion(const char *architecture, fstring driverpath_
        DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
                driverpath, cversion));
 
-       close_file(fsp, NORMAL_CLOSE);
-       close_cnum(conn, user->vuid);
-       unbecome_user();
-       *perr = WERR_OK;
-       return cversion;
-
-
-  error_exit:
+       goto done;
 
-       if(fsp)
-               close_file(fsp, NORMAL_CLOSE);
-
-       close_cnum(conn, user->vuid);
-       unbecome_user();
-       return -1;
+ error_exit:
+       cversion = -1;
+ done:
+       if (fsp != NULL) {
+               close_file(NULL, fsp, NORMAL_CLOSE);
+       }
+       if (conn != NULL) {
+               vfs_ChDir(conn, oldcwd);
+               conn_free_internal(conn);
+       }
+       if (cversion != -1) {
+               *perr = WERR_OK;
+       }
+       return cversion;
 }
 
 /****************************************************************************
 ****************************************************************************/
-static WERROR clean_up_driver_struct_level_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver,
-                                                                                        struct current_user *user)
+static WERROR clean_up_driver_struct_level_3(struct pipes_struct *rpc_pipe,
+                                            NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
 {
        const char *architecture;
        fstring new_name;
@@ -1649,7 +1688,9 @@ static WERROR clean_up_driver_struct_level_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *dri
         *      NT 4: cversion=2
         *      NT2K: cversion=3
         */
-       if ((driver->cversion = get_correct_cversion( architecture, driver->driverpath, user, &err)) == -1)
+       if ((driver->cversion = get_correct_cversion(rpc_pipe, architecture,
+                                                    driver->driverpath,
+                                                    &err)) == -1)
                return err;
 
        return WERR_OK;
@@ -1657,7 +1698,8 @@ static WERROR clean_up_driver_struct_level_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *dri
        
 /****************************************************************************
 ****************************************************************************/
-static WERROR clean_up_driver_struct_level_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver, struct current_user *user)
+static WERROR clean_up_driver_struct_level_6(struct pipes_struct *rpc_pipe,
+                                            NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
 {
        const char *architecture;
        fstring new_name;
@@ -1716,7 +1758,9 @@ static WERROR clean_up_driver_struct_level_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *dri
         *      NT2K: cversion=3
         */
 
-       if ((driver->version = get_correct_cversion(architecture, driver->driverpath, user, &err)) == -1)
+       if ((driver->version = get_correct_cversion(rpc_pipe, architecture,
+                                                   driver->driverpath,
+                                                   &err)) == -1)
                        return err;
 
        return WERR_OK;
@@ -1724,21 +1768,24 @@ static WERROR clean_up_driver_struct_level_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *dri
 
 /****************************************************************************
 ****************************************************************************/
-WERROR clean_up_driver_struct(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
-                                                         uint32 level, struct current_user *user)
+WERROR clean_up_driver_struct(struct pipes_struct *rpc_pipe,
+                             NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
+                             uint32 level)
 {
        switch (level) {
                case 3:
                {
                        NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
                        driver=driver_abstract.info_3;
-                       return clean_up_driver_struct_level_3(driver, user);
+                       return clean_up_driver_struct_level_3(rpc_pipe,
+                                                             driver);
                }
                case 6:
                {
                        NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver;
                        driver=driver_abstract.info_6;
-                       return clean_up_driver_struct_level_6(driver, user);
+                       return clean_up_driver_struct_level_6(rpc_pipe,
+                                                             driver);
                }
                default:
                        return WERR_INVALID_PARAM;
@@ -1784,8 +1831,9 @@ static char* ffmt(unsigned char *c){
 
 /****************************************************************************
 ****************************************************************************/
-WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract, uint32 level, 
-                                 struct current_user *user, WERROR *perr)
+WERROR move_driver_to_download_area(struct pipes_struct *p,
+                                   NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
+                                   uint32 level, WERROR *perr)
 {
        NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
        NT_PRINTER_DRIVER_INFO_LEVEL_3 converted_driver;
@@ -1793,14 +1841,15 @@ WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract
        char *new_dir = NULL;
        char *old_name = NULL;
        char *new_name = NULL;
-       DATA_BLOB null_pw;
-       connection_struct *conn;
+       connection_struct *conn = NULL;
        NTSTATUS nt_status;
-       fstring res_type;
        SMB_STRUCT_STAT st;
        int i;
        TALLOC_CTX *ctx = talloc_tos();
        int ver = 0;
+       char *oldcwd;
+       fstring printdollar;
+       int printdollar_snum;
 
        *perr = WERR_OK;
 
@@ -1819,38 +1868,24 @@ WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract
                return WERR_UNKNOWN_PRINTER_DRIVER;
        }
 
-       /*
-        * Connect to the print$ share under the same account as the user connected to the rpc pipe.
-        * Note we must be root to do this.
-        */
-
-       null_pw = data_blob_null;
-       fstrcpy(res_type, "A:");
-       become_root();
-       conn = make_connection_with_chdir("print$", null_pw, res_type, user->vuid, &nt_status);
-       unbecome_root();
+       fstrcpy(printdollar, "print$");
 
-       if (conn == NULL) {
-               DEBUG(0,("move_driver_to_download_area: Unable to connect\n"));
-               *perr = ntstatus_to_werror(nt_status);
+       printdollar_snum = find_service(printdollar);
+       if (printdollar_snum == -1) {
+               *perr = WERR_NO_SUCH_SHARE;
                return WERR_NO_SUCH_SHARE;
        }
 
-       /*
-        * Save who we are - we are temporarily becoming the connection user.
-        */
-
-       if (!become_user(conn, conn->vuid)) {
-               DEBUG(0,("move_driver_to_download_area: Can't become user!\n"));
-               return WERR_ACCESS_DENIED;
+       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+                                      lp_pathname(printdollar_snum),
+                                      p->server_info, &oldcwd);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0,("move_driver_to_download_area: create_conn_struct "
+                        "returned %s\n", nt_errstr(nt_status)));
+               *perr = ntstatus_to_werror(nt_status);
+               return *perr;
        }
 
-       /* WE ARE NOW RUNNING AS USER conn->vuid !!!!! */
-
-       /*
-        * make the directories version and version\driver_name
-        * under the architecture directory.
-        */
        DEBUG(5,("Creating first directory\n"));
        new_dir = talloc_asprintf(ctx,
                                "%s/%d",
@@ -1866,7 +1901,7 @@ WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract
                goto err_exit;
        }
 
-       create_directory(conn, new_dir);
+       create_directory(conn, NULL, new_dir);
 
        /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
         * listed for this driver which has already been moved, skip it (note:
@@ -2080,8 +2115,10 @@ WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract
 
   err_exit:
 
-       close_cnum(conn, user->vuid);
-       unbecome_user();
+       if (conn != NULL) {
+               vfs_ChDir(conn, oldcwd);
+               conn_free_internal(conn);
+       }
 
        if (W_ERROR_EQUAL(*perr, WERR_OK)) {
                return WERR_OK;
@@ -3111,8 +3148,7 @@ static void map_single_multi_sz_into_ctr(REGVAL_CTR *ctr, const char *val_name,
        regval_ctr_delvalue(ctr, val_name);
        regval_ctr_addvalue(ctr, val_name, REG_MULTI_SZ, 
                            (char *) conv_strs, str_size);      
-       safe_free(conv_strs);
-       
+       SAFE_FREE(conv_strs);
 }
 
 /****************************************************************************
@@ -3209,7 +3245,7 @@ static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
 
        ZERO_STRUCT( unistr_guid );     
        
-       init_unistr2( &unistr_guid, smb_uuid_string(talloc_tos(), guid),
+       init_unistr2( &unistr_guid, GUID_string(talloc_tos(), &guid),
                      UNI_STR_TERMINATE );
 
        regval_ctr_addvalue(ctr, "objectGUID", REG_SZ, 
@@ -3230,6 +3266,7 @@ static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
        const char *attrs[] = {"objectGUID", NULL};
        struct GUID guid;
        WERROR win_rc = WERR_OK;
+       size_t converted_size;
 
        DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
 
@@ -3252,13 +3289,13 @@ static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
                return WERR_SERVER_UNAVAILABLE;
        }
        /* Now convert to CH_UNIX. */
-       if (pull_utf8_allocate(&srv_dn, srv_dn_utf8) == (size_t)-1) {
+       if (!pull_utf8_allocate(&srv_dn, srv_dn_utf8, &converted_size)) {
                ldap_memfree(srv_dn_utf8);
                ldap_memfree(srv_cn_utf8);
                ads_destroy(&ads);
                return WERR_SERVER_UNAVAILABLE;
        }
-       if (pull_utf8_allocate(&srv_cn_0, srv_cn_utf8[0]) == (size_t)-1) {
+       if (!pull_utf8_allocate(&srv_cn_0, srv_cn_utf8[0], &converted_size)) {
                ldap_memfree(srv_dn_utf8);
                ldap_memfree(srv_cn_utf8);
                ads_destroy(&ads);
@@ -3314,8 +3351,13 @@ static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
 
        /* publish it */
        ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
-       if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT)
+       if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
+               int i;
+               for (i=0; mods[i] != 0; i++)
+                       ;
+               mods[i] = (LDAPMod *)-1;
                ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
+       }
 
        if (!ADS_ERR_OK(ads_rc))
                DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
@@ -3518,7 +3560,7 @@ bool is_printer_published(Printer_entry *print_hnd, int snum,
                case REG_SZ:            
                        rpcstr_pull( guid_str, regval_data_p(guid_val), 
                                     sizeof(guid_str)-1, -1, STR_TERMINATE );
-                       ret = smb_string_to_uuid( guid_str, guid );
+                       ret = NT_STATUS_IS_OK(GUID_from_string( guid_str, guid ));
                        break;                  
                case REG_BINARY:
                        if ( regval_size(guid_val) != sizeof(struct GUID) ) {
@@ -3823,7 +3865,7 @@ static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int bu
                        memcpy( &guid, data_p, sizeof(struct GUID) );
 
                        init_unistr2( &unistr_guid,
-                                     smb_uuid_string(talloc_tos(), guid), 
+                                     GUID_string(talloc_tos(), &guid), 
                                      UNI_STR_TERMINATE );
 
                        regval_ctr_addvalue( printer_data->keys[key_index].values, 
@@ -3849,10 +3891,46 @@ static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int bu
 /****************************************************************************
  ***************************************************************************/
 
+static char *last_from;
+static char *last_to;
+
+static const char *get_last_from(void)
+{
+       if (!last_from) {
+               return "";
+       }
+       return last_from;
+}
+
+static const char *get_last_to(void)
+{
+       if (!last_to) {
+               return "";
+       }
+       return last_to;
+}
+
+static bool set_last_from_to(const char *from, const char *to)
+{
+       char *orig_from = last_from;
+       char *orig_to = last_to;
+
+       last_from = SMB_STRDUP(from);
+       last_to = SMB_STRDUP(to);
+
+       SAFE_FREE(orig_from);
+       SAFE_FREE(orig_to);
+
+       if (!last_from || !last_to) {
+               SAFE_FREE(last_from);
+               SAFE_FREE(last_to);
+               return false;
+       }
+       return true;
+}
+
 static void map_to_os2_driver(fstring drivername)
 {
-       static bool initialised=False;
-       static fstring last_from,last_to;
        char *mapfile = lp_os2_driver_map();
        char **lines = NULL;
        int numlines = 0;
@@ -3864,21 +3942,17 @@ static void map_to_os2_driver(fstring drivername)
        if (!*mapfile)
                return;
 
-       if (!initialised) {
-               *last_from = *last_to = 0;
-               initialised = True;
-       }
-
-       if (strequal(drivername,last_from)) {
-               DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",drivername,last_to));
-               fstrcpy(drivername,last_to);
+       if (strequal(drivername,get_last_from())) {
+               DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
+                       drivername,get_last_to()));
+               fstrcpy(drivername,get_last_to());
                return;
        }
 
-       lines = file_lines_load(mapfile, &numlines,0);
+       lines = file_lines_load(mapfile, &numlines,0,NULL);
        if (numlines == 0 || lines == NULL) {
                DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
-               SAFE_FREE(lines);
+               TALLOC_FREE(lines);
                return;
        }
 
@@ -3920,21 +3994,24 @@ static void map_to_os2_driver(fstring drivername)
 
                if (strequal(nt_name,drivername)) {
                        DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
-                       fstrcpy(last_from,drivername);
-                       fstrcpy(last_to,os2_name);
+                       set_last_from_to(drivername,os2_name);
                        fstrcpy(drivername,os2_name);
-                       file_lines_free(lines);
+                       TALLOC_FREE(lines);
                        return;
                }
        }
 
-       file_lines_free(lines);
+       TALLOC_FREE(lines);
 }
 
 /****************************************************************************
  Get a default printer info 2 struct.
 ****************************************************************************/
-static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info, const char *servername, const char* sharename)
+
+static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
+                               const char *servername,
+                               const char* sharename,
+                               bool get_loc_com)
 {
        int snum = lp_servicenumber(sharename);
 
@@ -3961,7 +4038,7 @@ static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info, const char
        fstrcpy(info->datatype, "RAW");
 
 #ifdef HAVE_CUPS
-       if ( (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {           
+       if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {             
                /* Pull the location and comment strings from cups if we don't
                   already have one */
                if ( !strlen(info->location) || !strlen(info->comment) )
@@ -4010,7 +4087,11 @@ fail:
 
 /****************************************************************************
 ****************************************************************************/
-static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info, const char *servername, const char *sharename)
+
+static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
+                               const char *servername,
+                               const char *sharename,
+                               bool get_loc_com)
 {
        int len = 0;
        int snum = lp_servicenumber(sharename);
@@ -4023,7 +4104,8 @@ static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info, const char *servern
 
        dbuf = tdb_fetch(tdb_printers, kbuf);
        if (!dbuf.dptr) {
-               return get_a_printer_2_default(info, servername, sharename);
+               return get_a_printer_2_default(info, servername,
+                                       sharename, get_loc_com);
        }
 
        len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
@@ -4071,7 +4153,7 @@ static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info, const char *servern
        fstrcpy(info->printername, printername);
 
 #ifdef HAVE_CUPS
-       if ( (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {           
+       if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
                /* Pull the location and comment strings from cups if we don't
                   already have one */
                if ( !strlen(info->location) || !strlen(info->comment) )
@@ -4514,7 +4596,7 @@ static bool convert_driver_init( TALLOC_CTX *ctx, NT_DEVICEMODE *nt_devmode, uin
 
        ZERO_STRUCT(devmode);
 
-       prs_init(&ps, 0, ctx, UNMARSHALL);
+       prs_init_empty(&ps, ctx, UNMARSHALL);
        ps.data_p      = (char *)data;
        ps.buffer_size = data_len;
 
@@ -4655,8 +4737,8 @@ WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *dat
 
 ****************************************************************************/
 
-WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level, 
-                       const char *sharename)
+static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level, 
+                       const char *sharename, bool get_loc_com)
 {
        WERROR result;
        fstring servername;
@@ -4684,15 +4766,15 @@ WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_print
                                                    sizeof(servername)-1 );
                        }
 
-                       result = get_a_printer_2( (*pp_printer)->info_2, servername, sharename );
-       
-                       
+                       result = get_a_printer_2( (*pp_printer)->info_2,
+                                       servername, sharename, get_loc_com);
+
                        /* we have a new printer now.  Save it with this handle */
-                       
+
                        if ( !W_ERROR_IS_OK(result) ) {
                                TALLOC_FREE( *pp_printer );
                                DEBUG(10,("get_a_printer: [%s] level %u returning %s\n", 
-                                       sharename, (unsigned int)level, dos_errstr(result)));
+                                       sharename, (unsigned int)level, win_errstr(result)));
                                return result;
                        }
 
@@ -4708,6 +4790,24 @@ WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_print
        return WERR_OK;
 }
 
+WERROR get_a_printer( Printer_entry *print_hnd,
+                       NT_PRINTER_INFO_LEVEL **pp_printer,
+                       uint32 level,
+                       const char *sharename)
+{
+       return get_a_printer_internal(print_hnd, pp_printer, level,
+                                       sharename, true);
+}
+
+WERROR get_a_printer_search( Printer_entry *print_hnd,
+                       NT_PRINTER_INFO_LEVEL **pp_printer,
+                       uint32 level,
+                       const char *sharename)
+{
+       return get_a_printer_internal(print_hnd, pp_printer, level,
+                                       sharename, false);
+}
+
 /****************************************************************************
  Deletes a NT_PRINTER_INFO_LEVEL struct.
 ****************************************************************************/
@@ -4926,10 +5026,15 @@ bool printer_driver_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3 )
 static bool drv_file_in_use( char* file, NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
 {
        int i = 0;
-       
+
        if ( !info )
                return False;
-               
+
+       /* mz: skip files that are in the list but already deleted */
+       if (!file || !file[0]) {
+               return false;
+       }
+
        if ( strequal(file, info->driverpath) )
                return True;
 
@@ -5042,6 +5147,12 @@ static bool trim_overlap_drv_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *src,
   
   Upon return, *info has been modified to only contain the driver files
   which are not in use
+
+  Fix from mz:
+
+  This needs to check all drivers to ensure that all files in use
+  have been removed from *info, not just the ones in the first
+  match.
 ****************************************************************************/
 
 bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
@@ -5051,7 +5162,8 @@ bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
        uint32                          version;
        fstring                         *list = NULL;
        NT_PRINTER_DRIVER_INFO_LEVEL    driver;
-       
+       bool in_use = false;
+
        if ( !info )
                return False;
        
@@ -5086,9 +5198,10 @@ bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
                        
                if ( !strequal(info->name, driver.info_3->name) ) {
                        if ( trim_overlap_drv_files(info, driver.info_3) ) {
-                               free_a_printer_driver(driver, 3);
-                               SAFE_FREE( list );
-                               return True;
+                               /* mz: Do not instantly return -
+                                * we need to ensure this file isn't
+                                * also in use by other drivers. */
+                               in_use = true;
                        }
                }
 
@@ -5104,7 +5217,7 @@ bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
        if ( DEBUGLEVEL >= 20 )
                dump_a_printer_driver( driver, 3 );
 
-       return False;
+       return in_use;
 }
 
 /****************************************************************************
@@ -5113,49 +5226,44 @@ bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
   this.
 ****************************************************************************/
 
-static bool delete_driver_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct current_user *user )
+static bool delete_driver_files(struct pipes_struct *rpc_pipe,
+                               NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3)
 {
        int i = 0;
        char *s;
        const char *file;
        connection_struct *conn;
-       DATA_BLOB null_pw;
        NTSTATUS nt_status;
-       fstring res_type;
        SMB_STRUCT_STAT  st;
+       char *oldcwd;
+       fstring printdollar;
+       int printdollar_snum;
+       bool ret = false;
 
        if ( !info_3 )
                return False;
 
        DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n", info_3->name, info_3->cversion));
 
-       /*
-        * Connect to the print$ share under the same account as the
-        * user connected to the rpc pipe. Note we must be root to
-        * do this.
-        */
+       fstrcpy(printdollar, "print$");
 
-       null_pw = data_blob_null;
-       fstrcpy(res_type, "A:");
-       become_root();
-        conn = make_connection_with_chdir( "print$", null_pw, res_type, user->vuid, &nt_status );
-       unbecome_root();
+       printdollar_snum = find_service(printdollar);
+       if (printdollar_snum == -1) {
+               return false;
+       }
 
-       if ( !conn ) {
-               DEBUG(0,("delete_driver_files: Unable to connect\n"));
-               return False;
+       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+                                      lp_pathname(printdollar_snum),
+                                      rpc_pipe->server_info, &oldcwd);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0,("delete_driver_files: create_conn_struct "
+                        "returned %s\n", nt_errstr(nt_status)));
+               return false;
        }
 
        if ( !CAN_WRITE(conn) ) {
                DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
-               return False;
-       }
-
-        /* Save who we are - we are temporarily becoming the connection user. */
-
-       if ( !become_user(conn, conn->vuid) ) {
-               DEBUG(0,("delete_driver_files: Can't become user!\n"));
-               return False;
+               goto fail;
        }
 
        /* now delete the files; must strip the '\print$' string from
@@ -5216,9 +5324,15 @@ static bool delete_driver_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct
                }
        }
 
-       unbecome_user();
-
-       return true;
+       goto done;
+ fail:
+       ret = false;
+ done:
+       if (conn != NULL) {
+               vfs_ChDir(conn, oldcwd);
+               conn_free_internal(conn);
+       }
+       return ret;
 }
 
 /****************************************************************************
@@ -5226,8 +5340,9 @@ static bool delete_driver_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct
  previously looked up.
  ***************************************************************************/
 
-WERROR delete_printer_driver( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct current_user *user,
-                              uint32 version, bool delete_files )
+WERROR delete_printer_driver(struct pipes_struct *rpc_pipe,
+                            NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3,
+                            uint32 version, bool delete_files )
 {
        char *key = NULL;
        const char     *arch;
@@ -5277,7 +5392,7 @@ WERROR delete_printer_driver( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct cur
         */
 
        if ( delete_files )
-               delete_driver_files( info_3, user );
+               delete_driver_files(rpc_pipe, info_3);
 
        DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
        SAFE_FREE(key);
@@ -5294,6 +5409,7 @@ WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
        SEC_DESC_BUF *new_secdesc_ctr = NULL;
        SEC_DESC_BUF *old_secdesc_ctr = NULL;
        prs_struct ps;
+       bool prs_init_done = false;
        TALLOC_CTX *mem_ctx = NULL;
        TDB_DATA kbuf;
        WERROR status;
@@ -5358,8 +5474,15 @@ WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
 
        /* Store the security descriptor in a tdb */
 
-       prs_init(&ps, (uint32)sec_desc_size(new_secdesc_ctr->sd) +
-                sizeof(SEC_DESC_BUF), mem_ctx, MARSHALL);
+       if (!prs_init(&ps,
+               (uint32)ndr_size_security_descriptor(new_secdesc_ctr->sd, 0)
+               + sizeof(SEC_DESC_BUF), mem_ctx, MARSHALL) ) {
+               status = WERR_NOMEM;
+               goto out;
+       }
+
+
+       prs_init_done = true;
 
        if (!sec_io_desc_buf("nt_printing_setsec", &new_secdesc_ctr,
                             &ps, 1)) {
@@ -5380,7 +5503,9 @@ WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
 
  out:
 
-       prs_mem_free(&ps);
+       if (prs_init_done) {
+               prs_mem_free(&ps);
+       }
        if (mem_ctx)
                talloc_destroy(mem_ctx);
        return status;
@@ -5394,7 +5519,7 @@ static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
 {
        SEC_ACE ace[5]; /* max number of ace entries */
        int i = 0;
-       SEC_ACCESS sa;
+       uint32_t sa;
        SEC_ACL *psa = NULL;
        SEC_DESC_BUF *sdb = NULL;
        SEC_DESC *psd = NULL;
@@ -5403,7 +5528,7 @@ static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
 
        /* Create an ACE where Everyone is allowed to print */
 
-       init_sec_access(&sa, PRINTER_ACE_PRINT);
+       sa = PRINTER_ACE_PRINT;
        init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
                     sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
 
@@ -5415,7 +5540,7 @@ static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
                sid_copy(&domadmins_sid, get_global_sam_sid());
                sid_append_rid(&domadmins_sid, DOMAIN_GROUP_RID_ADMINS);
                
-               init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
+               sa = PRINTER_ACE_FULL_CONTROL;
                init_sec_ace(&ace[i++], &domadmins_sid, 
                        SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 
                        SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
@@ -5425,7 +5550,7 @@ static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
        else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
                sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
 
-               init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
+               sa = PRINTER_ACE_FULL_CONTROL;
                init_sec_ace(&ace[i++], &adm_sid, 
                        SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 
                        SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
@@ -5435,7 +5560,7 @@ static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
 
        /* add BUILTIN\Administrators as FULL CONTROL */
 
-       init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
+       sa = PRINTER_ACE_FULL_CONTROL;
        init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, 
                SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 
                SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
@@ -5503,8 +5628,9 @@ bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, SEC_DESC_BUF **s
 
                /* Save default security descriptor for later */
 
-               prs_init(&ps, (uint32)sec_desc_size((*secdesc_ctr)->sd) +
-                               sizeof(SEC_DESC_BUF), ctx, MARSHALL);
+               if (!prs_init(&ps, (uint32)ndr_size_security_descriptor((*secdesc_ctr)->sd, 0) +
+                       sizeof(SEC_DESC_BUF), ctx, MARSHALL))
+                       return False;
 
                if (sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
                        tdb_prs_store(tdb_printers, kbuf, &ps);
@@ -5629,6 +5755,17 @@ void map_printer_permissions(SEC_DESC *sd)
        }
 }
 
+void map_job_permissions(SEC_DESC *sd)
+{
+       int i;
+
+       for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
+               se_map_generic(&sd->dacl->aces[i].access_mask,
+                              &job_generic_mapping);
+       }
+}
+
+
 /****************************************************************************
  Check a user has permissions to perform the given operation.  We use the
  permission constants defined in include/rpc_spoolss.h to check the various
@@ -5652,24 +5789,22 @@ void map_printer_permissions(SEC_DESC *sd)
     3)  "printer admins" (may result in numerous calls to winbind)
 
  ****************************************************************************/
-bool print_access_check(struct current_user *user, int snum, int access_type)
+bool print_access_check(struct auth_serversupplied_info *server_info, int snum,
+                       int access_type)
 {
        SEC_DESC_BUF *secdesc = NULL;
        uint32 access_granted;
        NTSTATUS status;
-       bool result;
        const char *pname;
        TALLOC_CTX *mem_ctx = NULL;
        SE_PRIV se_printop = SE_PRINT_OPERATOR;
        
        /* If user is NULL then use the current_user structure */
 
-       if (!user)
-               user = &current_user;
-
        /* Always allow root or SE_PRINT_OPERATROR to do anything */
 
-       if ( user->ut.uid == 0 || user_has_privileges(user->nt_user_token, &se_printop ) ) {
+       if (server_info->utok.uid == 0
+           || user_has_privileges(server_info->ptok, &se_printop ) ) {
                return True;
        }
 
@@ -5702,37 +5837,30 @@ bool print_access_check(struct current_user *user, int snum, int access_type)
                   against.  This is because print jobs are child objects
                   objects of a printer. */
 
-               secdesc = se_create_child_secdesc(mem_ctx, parent_secdesc->sd, False);
+               status = se_create_child_secdesc_buf(mem_ctx, &secdesc, parent_secdesc->sd, False);
 
-               if (!secdesc) {
+               if (!NT_STATUS_IS_OK(status)) {
                        talloc_destroy(mem_ctx);
-                       errno = ENOMEM;
+                       errno = map_errno_from_nt_status(status);
                        return False;
                }
 
-               /* Now this is the bit that really confuses me.  The access
-                  type needs to be changed from JOB_ACCESS_ADMINISTER to
-                  PRINTER_ACCESS_ADMINISTER for this to work.  Something
-                  to do with the child (job) object becoming like a
-                  printer??  -tpot */
-
-               access_type = PRINTER_ACCESS_ADMINISTER;
+               map_job_permissions(secdesc->sd);
+       } else {
+               map_printer_permissions(secdesc->sd);
        }
-       
-       /* Check access */
-       
-       map_printer_permissions(secdesc->sd);
 
-       result = se_access_check(secdesc->sd, user->nt_user_token, access_type,
-                                &access_granted, &status);
+       /* Check access */
+       status = se_access_check(secdesc->sd, server_info->ptok, access_type,
+                                &access_granted);
 
-       DEBUG(4, ("access check was %s\n", result ? "SUCCESS" : "FAILURE"));
+       DEBUG(4, ("access check was %s\n", NT_STATUS_IS_OK(status) ? "SUCCESS" : "FAILURE"));
 
         /* see if we need to try the printer admin list */
 
         if ((access_granted == 0) &&
-           (token_contains_name_in_list(uidtoname(user->ut.uid), NULL,
-                                        user->nt_user_token,
+           (token_contains_name_in_list(uidtoname(server_info->utok.uid),
+                                        NULL, NULL, server_info->ptok,
                                         lp_printer_admin(snum)))) {
                talloc_destroy(mem_ctx);
                return True;
@@ -5740,11 +5868,11 @@ bool print_access_check(struct current_user *user, int snum, int access_type)
 
        talloc_destroy(mem_ctx);
        
-       if (!result) {
+       if (!NT_STATUS_IS_OK(status)) {
                errno = EACCES;
        }
 
-       return result;
+       return NT_STATUS_IS_OK(status);
 }
 
 /****************************************************************************