PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/samba-3.5.6/source3/libsmb/cliprint.c

https://github.com/theuni/XBMC-deps
C | 260 lines | 175 code | 46 blank | 39 comment | 26 complexity | b18ad7d2160ebb31ea7ab04c92e9a887 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. client print routines
  4. Copyright (C) Andrew Tridgell 1994-1998
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "includes.h"
  17. /*****************************************************************************
  18. Convert a character pointer in a cli_call_api() response to a form we can use.
  19. This function contains code to prevent core dumps if the server returns
  20. invalid data.
  21. *****************************************************************************/
  22. static const char *fix_char_ptr(unsigned int datap, unsigned int converter,
  23. char *rdata, int rdrcnt)
  24. {
  25. if (datap == 0) {
  26. /* turn NULL pointers into zero length strings */
  27. return "";
  28. } else {
  29. unsigned int offset = datap - converter;
  30. if (offset >= rdrcnt) {
  31. DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
  32. datap, converter, rdrcnt));
  33. return "<ERROR>";
  34. } else {
  35. return &rdata[offset];
  36. }
  37. }
  38. }
  39. /****************************************************************************
  40. call fn() on each entry in a print queue
  41. ****************************************************************************/
  42. int cli_print_queue(struct cli_state *cli,
  43. void (*fn)(struct print_job_info *))
  44. {
  45. char *rparam = NULL;
  46. char *rdata = NULL;
  47. char *p;
  48. unsigned int rdrcnt, rprcnt;
  49. char param[1024];
  50. int result_code=0;
  51. int i = -1;
  52. memset(param,'\0',sizeof(param));
  53. p = param;
  54. SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
  55. p += 2;
  56. safe_strcpy_base(p,"zWrLeh", param, sizeof(param)); /* parameter description? */
  57. p = skip_string(param,sizeof(param),p);
  58. safe_strcpy_base(p,"WWzWWDDzz", param, sizeof(param)); /* returned data format */
  59. p = skip_string(param,sizeof(param),p);
  60. safe_strcpy_base(p,cli->share, param, sizeof(param)); /* name of queue */
  61. p = skip_string(param,sizeof(param),p);
  62. SSVAL(p,0,2); /* API function level 2, PRJINFO_2 data structure */
  63. SSVAL(p,2,1000); /* size of bytes of returned data buffer */
  64. p += 4;
  65. safe_strcpy_base(p,"", param,sizeof(param)); /* subformat */
  66. p = skip_string(param,sizeof(param),p);
  67. DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
  68. if (cli_api(cli,
  69. param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
  70. NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
  71. &rparam, &rprcnt, /* return params, length */
  72. &rdata, &rdrcnt)) { /* return data, length */
  73. int converter;
  74. result_code = SVAL(rparam,0);
  75. converter = SVAL(rparam,2); /* conversion factor */
  76. if (result_code == 0) {
  77. struct print_job_info job;
  78. p = rdata;
  79. for (i = 0; i < SVAL(rparam,4); ++i) {
  80. job.id = SVAL(p,0);
  81. job.priority = SVAL(p,2);
  82. fstrcpy(job.user,
  83. fix_char_ptr(SVAL(p,4), converter,
  84. rdata, rdrcnt));
  85. job.t = cli_make_unix_date3(cli, p + 12);
  86. job.size = IVAL(p,16);
  87. fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
  88. converter,
  89. rdata, rdrcnt));
  90. fn(&job);
  91. p += 28;
  92. }
  93. }
  94. }
  95. /* If any parameters or data were returned, free the storage. */
  96. SAFE_FREE(rparam);
  97. SAFE_FREE(rdata);
  98. return i;
  99. }
  100. /****************************************************************************
  101. cancel a print job
  102. ****************************************************************************/
  103. int cli_printjob_del(struct cli_state *cli, int job)
  104. {
  105. char *rparam = NULL;
  106. char *rdata = NULL;
  107. char *p;
  108. unsigned int rdrcnt,rprcnt;
  109. int ret = -1;
  110. char param[1024];
  111. memset(param,'\0',sizeof(param));
  112. p = param;
  113. SSVAL(p,0,81); /* DosPrintJobDel() */
  114. p += 2;
  115. safe_strcpy_base(p,"W", param,sizeof(param));
  116. p = skip_string(param,sizeof(param),p);
  117. safe_strcpy_base(p,"", param,sizeof(param));
  118. p = skip_string(param,sizeof(param),p);
  119. SSVAL(p,0,job);
  120. p += 2;
  121. if (cli_api(cli,
  122. param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
  123. NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
  124. &rparam, &rprcnt, /* return params, length */
  125. &rdata, &rdrcnt)) { /* return data, length */
  126. ret = SVAL(rparam,0);
  127. }
  128. SAFE_FREE(rparam);
  129. SAFE_FREE(rdata);
  130. return ret;
  131. }
  132. /****************************************************************************
  133. Open a spool file
  134. ****************************************************************************/
  135. int cli_spl_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
  136. {
  137. char *p;
  138. unsigned openfn=0;
  139. unsigned accessmode=0;
  140. if (flags & O_CREAT)
  141. openfn |= (1<<4);
  142. if (!(flags & O_EXCL)) {
  143. if (flags & O_TRUNC)
  144. openfn |= (1<<1);
  145. else
  146. openfn |= (1<<0);
  147. }
  148. accessmode = (share_mode<<4);
  149. if ((flags & O_ACCMODE) == O_RDWR) {
  150. accessmode |= 2;
  151. } else if ((flags & O_ACCMODE) == O_WRONLY) {
  152. accessmode |= 1;
  153. }
  154. #if defined(O_SYNC)
  155. if ((flags & O_SYNC) == O_SYNC) {
  156. accessmode |= (1<<14);
  157. }
  158. #endif /* O_SYNC */
  159. if (share_mode == DENY_FCB) {
  160. accessmode = 0xFF;
  161. }
  162. memset(cli->outbuf,'\0',smb_size);
  163. memset(cli->inbuf,'\0',smb_size);
  164. cli_set_message(cli->outbuf,15,0,True);
  165. SCVAL(cli->outbuf,smb_com,SMBsplopen);
  166. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  167. cli_setup_packet(cli);
  168. SSVAL(cli->outbuf,smb_vwv0,0xFF);
  169. SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
  170. SSVAL(cli->outbuf,smb_vwv3,accessmode);
  171. SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
  172. SSVAL(cli->outbuf,smb_vwv5,0);
  173. SSVAL(cli->outbuf,smb_vwv8,openfn);
  174. if (cli->use_oplocks) {
  175. /* if using oplocks then ask for a batch oplock via
  176. core and extended methods */
  177. SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
  178. FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
  179. SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
  180. }
  181. p = smb_buf(cli->outbuf);
  182. p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
  183. cli_setup_bcc(cli, p);
  184. cli_send_smb(cli);
  185. if (!cli_receive_smb(cli)) {
  186. return -1;
  187. }
  188. if (cli_is_error(cli)) {
  189. return -1;
  190. }
  191. return SVAL(cli->inbuf,smb_vwv2);
  192. }
  193. /****************************************************************************
  194. Close a file.
  195. ****************************************************************************/
  196. bool cli_spl_close(struct cli_state *cli, uint16_t fnum)
  197. {
  198. memset(cli->outbuf,'\0',smb_size);
  199. memset(cli->inbuf,'\0',smb_size);
  200. cli_set_message(cli->outbuf,3,0,True);
  201. SCVAL(cli->outbuf,smb_com,SMBsplclose);
  202. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  203. cli_setup_packet(cli);
  204. SSVAL(cli->outbuf,smb_vwv0,fnum);
  205. SIVALS(cli->outbuf,smb_vwv1,-1);
  206. cli_send_smb(cli);
  207. if (!cli_receive_smb(cli)) {
  208. return False;
  209. }
  210. return !cli_is_error(cli);
  211. }