PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/samba3/source/printing/pcap.c

https://gitlab.com/envieidoc/tomato
C | 263 lines | 140 code | 47 blank | 76 comment | 47 complexity | 4f022072ce0f9a22e3f45e5b59a24586 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. printcap parsing
  4. Copyright (C) Karl Auer 1993-1998
  5. Re-working by Martin Kiff, 1994
  6. Re-written again by Andrew Tridgell
  7. Modified for SVID support by Norm Jacobs, 1997
  8. Modified for CUPS support by Michael Sweet, 1999
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * This module contains code to parse and cache printcap data, possibly
  23. * in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
  24. *
  25. * The way this module looks at the printcap file is very simplistic.
  26. * Only the local printcap file is inspected (no searching of NIS
  27. * databases etc).
  28. *
  29. * There are assumed to be one or more printer names per record, held
  30. * as a set of sub-fields separated by vertical bar symbols ('|') in the
  31. * first field of the record. The field separator is assumed to be a colon
  32. * ':' and the record separator a newline.
  33. *
  34. * Lines ending with a backspace '\' are assumed to flag that the following
  35. * line is a continuation line so that a set of lines can be read as one
  36. * printcap entry.
  37. *
  38. * A line stating with a hash '#' is assumed to be a comment and is ignored
  39. * Comments are discarded before the record is strung together from the
  40. * set of continuation lines.
  41. *
  42. * Opening a pipe for "lpc status" and reading that would probably
  43. * be pretty effective. Code to do this already exists in the freely
  44. * distributable PCNFS server code.
  45. *
  46. * Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
  47. * in smb.conf under Solaris.
  48. *
  49. * Modified to call CUPS support if printcap name is set to "cups"
  50. * in smb.conf.
  51. *
  52. * Modified to call iPrint support if printcap name is set to "iprint"
  53. * in smb.conf.
  54. */
  55. #include "includes.h"
  56. typedef struct pcap_cache {
  57. char *name;
  58. char *comment;
  59. struct pcap_cache *next;
  60. } pcap_cache_t;
  61. static pcap_cache_t *pcap_cache = NULL;
  62. BOOL pcap_cache_add(const char *name, const char *comment)
  63. {
  64. pcap_cache_t *p;
  65. if (name == NULL || ((p = SMB_MALLOC_P(pcap_cache_t)) == NULL))
  66. return False;
  67. p->name = SMB_STRDUP(name);
  68. p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
  69. p->next = pcap_cache;
  70. pcap_cache = p;
  71. return True;
  72. }
  73. static void pcap_cache_destroy(pcap_cache_t *cache)
  74. {
  75. pcap_cache_t *p, *next;
  76. for (p = cache; p != NULL; p = next) {
  77. next = p->next;
  78. SAFE_FREE(p->name);
  79. SAFE_FREE(p->comment);
  80. SAFE_FREE(p);
  81. }
  82. }
  83. BOOL pcap_cache_loaded(void)
  84. {
  85. return (pcap_cache != NULL);
  86. }
  87. void pcap_cache_reload(void)
  88. {
  89. const char *pcap_name = lp_printcapname();
  90. BOOL pcap_reloaded = False;
  91. pcap_cache_t *tmp_cache = NULL;
  92. XFILE *pcap_file;
  93. char *pcap_line;
  94. DEBUG(3, ("reloading printcap cache\n"));
  95. /* only go looking if no printcap name supplied */
  96. if (pcap_name == NULL || *pcap_name == 0) {
  97. DEBUG(0, ("No printcap file name configured!\n"));
  98. return;
  99. }
  100. tmp_cache = pcap_cache;
  101. pcap_cache = NULL;
  102. #ifdef HAVE_CUPS
  103. if (strequal(pcap_name, "cups")) {
  104. pcap_reloaded = cups_cache_reload();
  105. goto done;
  106. }
  107. #endif
  108. #ifdef HAVE_IPRINT
  109. if (strequal(pcap_name, "iprint")) {
  110. pcap_reloaded = iprint_cache_reload();
  111. goto done;
  112. }
  113. #endif
  114. #if defined(SYSV) || defined(HPUX)
  115. if (strequal(pcap_name, "lpstat")) {
  116. pcap_reloaded = sysv_cache_reload();
  117. goto done;
  118. }
  119. #endif
  120. #ifdef AIX
  121. if (strstr_m(pcap_name, "/qconfig") != NULL) {
  122. pcap_reloaded = aix_cache_reload();
  123. goto done;
  124. }
  125. #endif
  126. /* handle standard printcap - moved from pcap_printer_fn() */
  127. if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
  128. DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
  129. goto done;
  130. }
  131. for (; (pcap_line = fgets_slash(NULL, sizeof(pstring), pcap_file)) != NULL; safe_free(pcap_line)) {
  132. pstring name, comment;
  133. char *p, *q;
  134. if (*pcap_line == '#' || *pcap_line == 0)
  135. continue;
  136. /* now we have a real printer line - cut at the first : */
  137. if ((p = strchr_m(pcap_line, ':')) != NULL)
  138. *p = 0;
  139. /*
  140. * now find the most likely printer name and comment
  141. * this is pure guesswork, but it's better than nothing
  142. */
  143. for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
  144. BOOL has_punctuation;
  145. if ((q = strchr_m(p, '|')) != NULL)
  146. *q++ = 0;
  147. has_punctuation = (strchr_m(p, ' ') ||
  148. strchr_m(p, '\t') ||
  149. strchr_m(p, '(') ||
  150. strchr_m(p, ')'));
  151. if (strlen(p) > strlen(comment) && has_punctuation) {
  152. pstrcpy(comment, p);
  153. continue;
  154. }
  155. if (strlen(p) <= MAXPRINTERLEN &&
  156. strlen(p) > strlen(name) && !has_punctuation) {
  157. if (!*comment)
  158. pstrcpy(comment, name);
  159. pstrcpy(name, p);
  160. continue;
  161. }
  162. if (!strchr_m(comment, ' ') &&
  163. strlen(p) > strlen(comment)) {
  164. pstrcpy(comment, p);
  165. continue;
  166. }
  167. }
  168. comment[60] = 0;
  169. name[MAXPRINTERLEN] = 0;
  170. if (*name && !pcap_cache_add(name, comment)) {
  171. x_fclose(pcap_file);
  172. goto done;
  173. }
  174. }
  175. x_fclose(pcap_file);
  176. pcap_reloaded = True;
  177. done:
  178. DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
  179. if (pcap_reloaded)
  180. pcap_cache_destroy(tmp_cache);
  181. else {
  182. pcap_cache_destroy(pcap_cache);
  183. pcap_cache = tmp_cache;
  184. }
  185. return;
  186. }
  187. BOOL pcap_printername_ok(const char *printername)
  188. {
  189. pcap_cache_t *p;
  190. for (p = pcap_cache; p != NULL; p = p->next)
  191. if (strequal(p->name, printername))
  192. return True;
  193. return False;
  194. }
  195. /***************************************************************************
  196. run a function on each printer name in the printcap file. The function is
  197. passed the primary name and the comment (if possible). Note the fn() takes
  198. strings in DOS codepage. This means the xxx_printer_fn() calls must be fixed
  199. to return DOS codepage. FIXME !! JRA.
  200. XXX: I'm not sure if this comment still applies.. Anyone? -Rob
  201. ***************************************************************************/
  202. void pcap_printer_fn(void (*fn)(char *, char *))
  203. {
  204. pcap_cache_t *p;
  205. for (p = pcap_cache; p != NULL; p = p->next)
  206. fn(p->name, p->comment);
  207. return;
  208. }