LCOV - code coverage report
Current view: top level - source3/libsmb - libsmb_printjob.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 0 163 0.0 %
Date: 2024-02-28 12:06:22 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/Netbios implementation.
       3             :    SMB client library implementation
       4             :    Copyright (C) Andrew Tridgell 1998
       5             :    Copyright (C) Richard Sharpe 2000, 2002
       6             :    Copyright (C) John Terpstra 2000
       7             :    Copyright (C) Tom Jansen (Ninja ISD) 2002
       8             :    Copyright (C) Derrell Lipman 2003-2008
       9             :    Copyright (C) Jeremy Allison 2007, 2008
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "libsmb/libsmb.h"
      27             : #include "libsmbclient.h"
      28             : #include "libsmb_internal.h"
      29             : 
      30             : 
      31             : /*
      32             :  * Open a print file to be written to by other calls
      33             :  */
      34             : 
      35             : SMBCFILE *
      36           0 : SMBC_open_print_job_ctx(SMBCCTX *context,
      37             :                         const char *fname)
      38             : {
      39           0 :         char *server = NULL;
      40           0 :         char *share = NULL;
      41           0 :         char *user = NULL;
      42           0 :         char *password = NULL;
      43           0 :         char *path = NULL;
      44           0 :         uint16_t port = 0;
      45           0 :         TALLOC_CTX *frame = talloc_stackframe();
      46             : 
      47           0 :         if (!context || !context->internal->initialized) {
      48           0 :                 errno = EINVAL;
      49           0 :                 TALLOC_FREE(frame);
      50           0 :                 return NULL;
      51             :         }
      52             : 
      53           0 :         if (!fname) {
      54           0 :                 errno = EINVAL;
      55           0 :                 TALLOC_FREE(frame);
      56           0 :                 return NULL;
      57             :         }
      58             : 
      59           0 :         DEBUG(4, ("SMBC_open_print_job_ctx(%s)\n", fname));
      60             : 
      61           0 :         if (SMBC_parse_path(frame,
      62             :                             context,
      63             :                             fname,
      64             :                             NULL,
      65             :                             &server,
      66             :                             &port,
      67             :                             &share,
      68             :                             &path,
      69             :                             &user,
      70             :                             &password,
      71             :                             NULL)) {
      72           0 :                 errno = EINVAL;
      73           0 :                 TALLOC_FREE(frame);
      74           0 :                 return NULL;
      75             :         }
      76             : 
      77             :         /* What if the path is empty, or the file exists? */
      78             : 
      79           0 :         TALLOC_FREE(frame);
      80           0 :         return smbc_getFunctionOpen(context)(context, fname, O_WRONLY, 666);
      81             : }
      82             : 
      83             : /*
      84             :  * Routine to print a file on a remote server ...
      85             :  *
      86             :  * We open the file, which we assume to be on a remote server, and then
      87             :  * copy it to a print file on the share specified by printq.
      88             :  */
      89             : 
      90             : int
      91           0 : SMBC_print_file_ctx(SMBCCTX *c_file,
      92             :                     const char *fname,
      93             :                     SMBCCTX *c_print,
      94             :                     const char *printq)
      95             : {
      96           0 :         SMBCFILE *fid1;
      97           0 :         SMBCFILE *fid2;
      98           0 :         smbc_open_fn f_open1;
      99           0 :         smbc_open_print_job_fn f_open_pj2;
     100           0 :         int bytes;
     101           0 :         int saverr;
     102           0 :         int tot_bytes = 0;
     103           0 :         char buf[4096];
     104           0 :         TALLOC_CTX *frame = talloc_stackframe();
     105             : 
     106           0 :         if (!c_file || !c_file->internal->initialized ||
     107           0 :             !c_print || !c_print->internal->initialized) {
     108           0 :                 errno = EINVAL;
     109           0 :                 TALLOC_FREE(frame);
     110           0 :                 return -1;
     111             :         }
     112             : 
     113           0 :         if (!fname && !printq) {
     114           0 :                 errno = EINVAL;
     115           0 :                 TALLOC_FREE(frame);
     116           0 :                 return -1;
     117             :         }
     118             : 
     119             :         /* Try to open the file for reading ... */
     120           0 :         f_open1 = smbc_getFunctionOpen(c_file);
     121           0 :         if (f_open1 == NULL) {
     122           0 :                 errno = EINVAL;
     123           0 :                 TALLOC_FREE(frame);
     124           0 :                 return -1;
     125             :         }
     126             : 
     127           0 :         fid1 = f_open1(c_file, fname, O_RDONLY, 0666);
     128           0 :         if (fid1 == NULL) {
     129           0 :                 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
     130           0 :                 TALLOC_FREE(frame);
     131           0 :                 return -1;  /* smbc_open sets errno */
     132             :         }
     133             : 
     134             :         /* Now, try to open the printer file for writing */
     135           0 :         f_open_pj2 = smbc_getFunctionOpenPrintJob(c_print);
     136           0 :         if (f_open_pj2 == NULL) {
     137           0 :                 errno = EINVAL;
     138           0 :                 TALLOC_FREE(frame);
     139           0 :                 return -1;
     140             :         }
     141             : 
     142           0 :         fid2 = f_open_pj2(c_print, printq);
     143           0 :         if (fid2 == NULL) {
     144           0 :                 saverr = errno;  /* Save errno */
     145           0 :                 smbc_getFunctionClose(c_file)(c_file, fid1);
     146           0 :                 errno = saverr;
     147           0 :                 TALLOC_FREE(frame);
     148           0 :                 return -1;
     149             :         }
     150             : 
     151           0 :         while ((bytes = smbc_getFunctionRead(c_file)(c_file, fid1,
     152           0 :                                                      buf, sizeof(buf))) > 0) {
     153           0 :                 tot_bytes += bytes;
     154             : 
     155           0 :                 if ((smbc_getFunctionWrite(c_print)(c_print, fid2,
     156             :                                                     buf, bytes)) < 0) {
     157           0 :                         saverr = errno;
     158           0 :                         smbc_getFunctionClose(c_file)(c_file, fid1);
     159           0 :                         smbc_getFunctionClose(c_print)(c_print, fid2);
     160           0 :                         errno = saverr;
     161             :                 }
     162             :         }
     163             : 
     164           0 :         saverr = errno;
     165             : 
     166           0 :         smbc_getFunctionClose(c_file)(c_file, fid1);
     167           0 :         smbc_getFunctionClose(c_print)(c_print, fid2);
     168             : 
     169           0 :         if (bytes < 0) {
     170           0 :                 errno = saverr;
     171           0 :                 TALLOC_FREE(frame);
     172           0 :                 return -1;
     173             :         }
     174             : 
     175           0 :         TALLOC_FREE(frame);
     176           0 :         return tot_bytes;
     177             : }
     178             : 
     179             : /*
     180             :  * Routine to list print jobs on a printer share ...
     181             :  */
     182             : 
     183             : int
     184           0 : SMBC_list_print_jobs_ctx(SMBCCTX *context,
     185             :                          const char *fname,
     186             :                          smbc_list_print_job_fn fn)
     187             : {
     188           0 :         SMBCSRV *srv = NULL;
     189           0 :         char *server = NULL;
     190           0 :         char *share = NULL;
     191           0 :         char *user = NULL;
     192           0 :         char *password = NULL;
     193           0 :         char *workgroup = NULL;
     194           0 :         char *path = NULL;
     195           0 :         uint16_t port = 0;
     196           0 :         TALLOC_CTX *frame = talloc_stackframe();
     197           0 :         NTSTATUS status;
     198             : 
     199           0 :         if (!context || !context->internal->initialized) {
     200           0 :                 errno = EINVAL;
     201           0 :                 TALLOC_FREE(frame);
     202           0 :                 return -1;
     203             :         }
     204             : 
     205           0 :         if (!fname) {
     206           0 :                 errno = EINVAL;
     207           0 :                 TALLOC_FREE(frame);
     208           0 :                 return -1;
     209             :         }
     210             : 
     211           0 :         DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
     212             : 
     213           0 :         if (SMBC_parse_path(frame,
     214             :                             context,
     215             :                             fname,
     216             :                             &workgroup,
     217             :                             &server,
     218             :                             &port,
     219             :                             &share,
     220             :                             &path,
     221             :                             &user,
     222             :                             &password,
     223             :                             NULL)) {
     224           0 :                 errno = EINVAL;
     225           0 :                 TALLOC_FREE(frame);
     226           0 :                 return -1;
     227             :         }
     228             : 
     229           0 :         if (!user || user[0] == (char)0) {
     230           0 :                 user = talloc_strdup(frame, smbc_getUser(context));
     231           0 :                 if (!user) {
     232           0 :                         errno = ENOMEM;
     233           0 :                         TALLOC_FREE(frame);
     234           0 :                         return -1;
     235             :                 }
     236             :         }
     237             : 
     238           0 :         srv = SMBC_server(frame, context, True,
     239             :                           server, port, share, &workgroup, &user, &password);
     240             : 
     241           0 :         if (!srv) {
     242           0 :                 TALLOC_FREE(frame);
     243           0 :                 return -1;  /* errno set by SMBC_server */
     244             :         }
     245             : 
     246           0 :         status = cli_print_queue(srv->cli,
     247             :                                  (void (*)(struct print_job_info *))fn);
     248           0 :         if (!NT_STATUS_IS_OK(status)) {
     249           0 :                 TALLOC_FREE(frame);
     250           0 :                 errno = cli_status_to_errno(status);
     251           0 :                 return -1;
     252             :         }
     253             : 
     254           0 :         TALLOC_FREE(frame);
     255           0 :         return 0;
     256             : }
     257             : 
     258             : /*
     259             :  * Delete a print job from a remote printer share
     260             :  */
     261             : 
     262             : int
     263           0 : SMBC_unlink_print_job_ctx(SMBCCTX *context,
     264             :                           const char *fname,
     265             :                           int id)
     266             : {
     267           0 :         SMBCSRV *srv = NULL;
     268           0 :         char *server = NULL;
     269           0 :         char *share = NULL;
     270           0 :         char *user = NULL;
     271           0 :         char *password = NULL;
     272           0 :         char *workgroup = NULL;
     273           0 :         char *path = NULL;
     274           0 :         uint16_t port = 0;
     275           0 :         TALLOC_CTX *frame = talloc_stackframe();
     276           0 :         NTSTATUS status;
     277             : 
     278           0 :         if (!context || !context->internal->initialized) {
     279           0 :                 errno = EINVAL;
     280           0 :                 TALLOC_FREE(frame);
     281           0 :                 return -1;
     282             :         }
     283             : 
     284           0 :         if (!fname) {
     285           0 :                 errno = EINVAL;
     286           0 :                 TALLOC_FREE(frame);
     287           0 :                 return -1;
     288             :         }
     289             : 
     290           0 :         DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
     291             : 
     292           0 :         if (SMBC_parse_path(frame,
     293             :                             context,
     294             :                             fname,
     295             :                             &workgroup,
     296             :                             &server,
     297             :                             &port,
     298             :                             &share,
     299             :                             &path,
     300             :                             &user,
     301             :                             &password,
     302             :                             NULL)) {
     303           0 :                 errno = EINVAL;
     304           0 :                 TALLOC_FREE(frame);
     305           0 :                 return -1;
     306             :         }
     307             : 
     308           0 :         if (!user || user[0] == (char)0) {
     309           0 :                 user = talloc_strdup(frame, smbc_getUser(context));
     310           0 :                 if (!user) {
     311           0 :                         errno = ENOMEM;
     312           0 :                         TALLOC_FREE(frame);
     313           0 :                         return -1;
     314             :                 }
     315             :         }
     316             : 
     317           0 :         srv = SMBC_server(frame, context, True,
     318             :                           server, port, share, &workgroup, &user, &password);
     319             : 
     320           0 :         if (!srv) {
     321           0 :                 TALLOC_FREE(frame);
     322           0 :                 return -1;  /* errno set by SMBC_server */
     323             :         }
     324             : 
     325           0 :         status = cli_printjob_del(srv->cli, id);
     326           0 :         if (!NT_STATUS_IS_OK(status)) {
     327           0 :                 errno = cli_status_to_errno(status);
     328           0 :                 TALLOC_FREE(frame);
     329           0 :                 return -1;
     330             :         }
     331             : 
     332           0 :         TALLOC_FREE(frame);
     333           0 :         return 0;
     334             : }
     335             : 

Generated by: LCOV version 1.14