Coverage Report

Created: 2026-07-25 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/printing/print_standard.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   printcap parsing
4
   Copyright (C) Karl Auer 1993-1998
5
6
   Re-working by Martin Kiff, 1994
7
8
   Re-written again by Andrew Tridgell
9
10
   Modified for SVID support by Norm Jacobs, 1997
11
12
   Modified for CUPS support by Michael Sweet, 1999
13
14
   This program is free software; you can redistribute it and/or modify
15
   it under the terms of the GNU General Public License as published by
16
   the Free Software Foundation; either version 3 of the License, or
17
   (at your option) any later version.
18
19
   This program is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
   GNU General Public License for more details.
23
24
   You should have received a copy of the GNU General Public License
25
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
*/
27
28
/*
29
 *  This module contains code to parse and cache printcap data, possibly
30
 *  in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
31
 *
32
 *  The way this module looks at the printcap file is very simplistic.
33
 *  Only the local printcap file is inspected (no searching of NIS
34
 *  databases etc).
35
 *
36
 *  There are assumed to be one or more printer names per record, held
37
 *  as a set of sub-fields separated by vertical bar symbols ('|') in the
38
 *  first field of the record. The field separator is assumed to be a colon
39
 *  ':' and the record separator a newline.
40
 *
41
 *  Lines ending with a backspace '\' are assumed to flag that the following
42
 *  line is a continuation line so that a set of lines can be read as one
43
 *  printcap entry.
44
 *
45
 *  A line stating with a hash '#' is assumed to be a comment and is ignored
46
 *  Comments are discarded before the record is strung together from the
47
 *  set of continuation lines.
48
 *
49
 *  Opening a pipe for "lpc status" and reading that would probably
50
 *  be pretty effective. Code to do this already exists in the freely
51
 *  distributable PCNFS server code.
52
 */
53
54
/* printcap parsing specific code moved here from printing/pcap.c */
55
56
57
#include "includes.h"
58
#include "lib/util/util_file.h"
59
#include "system/filesys.h"
60
#include "printing/pcap.h"
61
62
/* handle standard printcap - moved from pcap_printer_fn() */
63
bool std_pcap_cache_reload(const char *pcap_name, struct pcap_cache **_pcache)
64
0
{
65
0
  TALLOC_CTX *frame = talloc_stackframe();
66
0
  FILE *pcap_file;
67
0
  char *pcap_line;
68
0
  struct pcap_cache *pcache = NULL;
69
0
  bool print_warning = false;
70
71
0
  if ((pcap_file = fopen(pcap_name, "r")) == NULL) {
72
0
    DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
73
0
    talloc_free(frame);
74
0
    return false;
75
0
  }
76
77
0
  while ((pcap_line = fgets_slash(frame, NULL, 1024,
78
0
          pcap_file)) != NULL) {
79
0
    char *name = NULL;
80
0
    char *comment = NULL;
81
0
    char *p, *q;
82
83
0
    if (*pcap_line == '#' || *pcap_line == 0) {
84
0
      TALLOC_FREE(pcap_line);
85
0
      continue;
86
0
    }
87
88
    /* now we have a real printer line - cut at the first : */
89
0
    if ((p = strchr_m(pcap_line, ':')) != NULL)
90
0
      *p = 0;
91
92
    /*
93
     * now find the most likely printer name and comment
94
     * this is pure guesswork, but it's better than nothing
95
     */
96
0
    for (p = pcap_line; p != NULL; p = q) {
97
0
      bool has_punctuation = false;
98
99
0
      if ((q = strchr_m(p, '|')) != NULL)
100
0
        *q++ = 0;
101
102
0
      has_punctuation = (strchr_m(p, ' ') ||
103
0
                         strchr_m(p, '\t') ||
104
0
             strchr_m(p, '"') ||
105
0
             strchr_m(p, '\'') ||
106
0
             strchr_m(p, ';') ||
107
0
             strchr_m(p, ',') ||
108
0
                         strchr_m(p, '(') ||
109
0
                         strchr_m(p, ')'));
110
111
0
      if (name == NULL && !has_punctuation) {
112
0
        name = talloc_strdup(frame, p);
113
0
        TALLOC_FREE(pcap_line);
114
0
        continue;
115
0
      }
116
117
0
      if (has_punctuation) {
118
0
        comment = talloc_strdup(frame, p);
119
0
        TALLOC_FREE(pcap_line);
120
0
        continue;
121
0
      }
122
0
    }
123
124
0
    if (name != NULL) {
125
0
      bool ok;
126
127
0
      if (!print_warning && strlen(name) > MAXPRINTERLEN) {
128
0
        print_warning = true;
129
0
      }
130
131
0
      ok = pcap_cache_add_specific(&pcache,
132
0
                 name,
133
0
                 comment,
134
0
                 NULL);
135
0
      if (!ok) {
136
0
        fclose(pcap_file);
137
0
        pcap_cache_destroy_specific(&pcache);
138
0
        talloc_free(frame);
139
0
        return false;
140
0
      }
141
0
    }
142
0
    TALLOC_FREE(name);
143
0
    TALLOC_FREE(comment);
144
0
    TALLOC_FREE(pcap_line);
145
0
  }
146
147
0
  if (print_warning) {
148
0
    DBG_WARNING("WARNING: You have some printer names that are "
149
0
          "longer than %u characters. These may not be "
150
0
          "accessible to some older clients!\n",
151
0
          (unsigned int)MAXPRINTERLEN);
152
0
  }
153
154
0
  fclose(pcap_file);
155
0
  *_pcache = pcache;
156
0
  talloc_free(frame);
157
  return true;
158
0
}