Squid Web Cache v8/master
Loading...
Searching...
No Matches
support_netbios.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/*
10 * -----------------------------------------------------------------------------
11 *
12 * Author: Markus Moeller (markus_moeller at compuserve.com)
13 *
14 * Copyright (C) 2007 Markus Moeller. All rights reserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, see <https://www.gnu.org/licenses/>.
28 *
29 * -----------------------------------------------------------------------------
30 */
31
32#include "squid.h"
33#include "util.h"
34
35#if HAVE_LDAP
36
37#include "support.h"
38
39struct ndstruct *init_nd(void);
40void free_nd(struct ndstruct *ndsp);
41
42struct ndstruct *
43init_nd(void) {
44 struct ndstruct *ndsp;
45 ndsp = (struct ndstruct *) xmalloc(sizeof(struct ndstruct));
46 ndsp->netbios = nullptr;
47 ndsp->domain = nullptr;
48 ndsp->next = nullptr;
49 return ndsp;
50}
51
52void
53free_nd(struct ndstruct *ndsp)
54{
55 while (ndsp) {
56 struct ndstruct *ndspn = ndsp->next;
57 xfree(ndsp->netbios);
58 xfree(ndsp->domain);
59 xfree(ndsp);
60 ndsp = ndspn;
61 }
62}
63
64int
65create_nd(struct main_args *margs)
66{
67 char *np, *dp;
68 char *p;
69 struct ndstruct *ndsp = nullptr, *ndspn = nullptr;
70 /*
71 * netbios list format:
72 *
73 * nlist=Pattern1[:Pattern2]
74 *
75 * Pattern=NetbiosName@Domain Netbios Name for a specific Kerberos domain
76 * ndstruct.domain=Domain, ndstruct.netbios=NetbiosName
77 *
78 *
79 */
80 p = margs->nlist;
81 np = margs->nlist;
82 debug((char *) "%s| %s: DEBUG: Netbios list %s\n", LogTime(), PROGRAM, margs->nlist ? margs->nlist : "NULL");
83 dp = nullptr;
84
85 if (!p) {
86 debug((char *) "%s| %s: DEBUG: No netbios names defined.\n", LogTime(), PROGRAM);
87 return (0);
88 }
89 while (*p) { /* loop over group list */
90 if (*p == '\n' || *p == '\r') { /* Ignore CR and LF if exist */
91 ++p;
92 continue;
93 }
94 if (*p == '@') { /* end of group name - start of domain name */
95 if (p == np) { /* empty group name not allowed */
96 debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
97 free_nd(ndsp);
98 return (1);
99 }
100 if (dp) { /* end of domain name - twice */
101 debug((char *) "%s| %s: @ is not allowed in netbios name %s@%s\n",LogTime(), PROGRAM,np,dp);
102 free_nd(ndsp);
103 return(1);
104 }
105 *p = '\0';
106 ++p;
107 ndsp = init_nd();
108 ndsp->netbios = xstrdup(np);
109 ndsp->next = ndspn;
110 dp = p; /* after @ starts new domain name */
111 } else if (*p == ':') { /* end of group name or end of domain name */
112 if (p == np) { /* empty group name not allowed */
113 debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
114 free_nd(ndsp);
115 return (1);
116 }
117 *p = '\0';
118 ++p;
119 if (dp) { /* end of domain name */
120 ndsp->domain = xstrdup(dp);
121 dp = nullptr;
122 } else { /* end of group name and no domain name */
123 ndsp = init_nd();
124 ndsp->netbios = xstrdup(np);
125 ndsp->next = ndspn;
126 }
127 ndspn = ndsp;
128 np = p; /* after : starts new group name */
129 if (!ndsp->domain || !strcmp(ndsp->domain, "")) {
130 debug((char *) "%s| %s: DEBUG: No domain defined for netbios name %s\n", LogTime(), PROGRAM, ndsp->netbios);
131 free_nd(ndsp);
132 return (1);
133 }
134 debug((char *) "%s| %s: DEBUG: Netbios name %s Domain %s\n", LogTime(), PROGRAM, ndsp->netbios, ndsp->domain);
135 } else
136 ++p;
137 }
138 if (p == np) { /* empty group name not allowed */
139 debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
140 free_nd(ndsp);
141 return (1);
142 }
143 if (dp) { /* end of domain name */
144 ndsp->domain = xstrdup(dp);
145 } else { /* end of group name and no domain name */
146 ndsp = init_nd();
147 ndsp->netbios = xstrdup(np);
148 ndsp->next = ndspn;
149 }
150 if (!ndsp->domain || !strcmp(ndsp->domain, "")) {
151 debug((char *) "%s| %s: DEBUG: No domain defined for netbios name %s\n", LogTime(), PROGRAM, ndsp->netbios);
152 free_nd(ndsp);
153 return (1);
154 }
155 debug((char *) "%s| %s: DEBUG: Netbios name %s Domain %s\n", LogTime(), PROGRAM, ndsp->netbios, ndsp->domain);
156
157 margs->ndoms = ndsp;
158 return (0);
159}
160
161char *
162get_netbios_name(struct main_args *margs, char *netbios)
163{
164 struct ndstruct *nd;
165
166 nd = margs->ndoms;
167 while (nd && netbios) {
168 debug((char *) "%s| %s: DEBUG: Netbios domain loop: netbios@domain %s@%s\n", LogTime(), PROGRAM, nd->netbios, nd->domain);
169 if (nd->netbios && !strcasecmp(nd->netbios, netbios)) {
170 debug((char *) "%s| %s: DEBUG: Found netbios@domain %s@%s\n", LogTime(), PROGRAM, nd->netbios, nd->domain);
171 return (nd->domain);
172 }
173 nd = nd->next;
174 }
175
176 return nullptr;
177}
178#endif
179
#define PROGRAM
Definition support.h:168
int create_nd(struct main_args *margs)
const char * LogTime(void)
char * get_netbios_name(struct main_args *margs, char *netbios)
void debug(const char *format,...)
Definition debug.cc:19
#define xfree
#define xstrdup
#define xmalloc
struct ndstruct * ndoms
Definition support.h:90
char * nlist
Definition support.h:77
char * domain
Definition support.h:64
char * netbios
Definition support.h:63
struct ndstruct * next
Definition support.h:65