support_netbios.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
29  *
30  * -----------------------------------------------------------------------------
31  */
32 
33 #include "squid.h"
34 #include "util.h"
35 
36 #if HAVE_LDAP
37 
38 #include "support.h"
39 
40 struct ndstruct *init_nd(void);
41 void free_nd(struct ndstruct *ndsp);
42 
43 struct ndstruct *
44 init_nd(void) {
45  struct ndstruct *ndsp;
46  ndsp = (struct ndstruct *) xmalloc(sizeof(struct ndstruct));
47  ndsp->netbios = nullptr;
48  ndsp->domain = nullptr;
49  ndsp->next = nullptr;
50  return ndsp;
51 }
52 
53 void
54 free_nd(struct ndstruct *ndsp)
55 {
56  while (ndsp) {
57  struct ndstruct *ndspn = ndsp->next;
58  xfree(ndsp->netbios);
59  xfree(ndsp->domain);
60  xfree(ndsp);
61  ndsp = ndspn;
62  }
63 }
64 
65 int
66 create_nd(struct main_args *margs)
67 {
68  char *np, *dp;
69  char *p;
70  struct ndstruct *ndsp = nullptr, *ndspn = nullptr;
71  /*
72  * netbios list format:
73  *
74  * nlist=Pattern1[:Pattern2]
75  *
76  * Pattern=NetbiosName@Domain Netbios Name for a specific Kerberos domain
77  * ndstruct.domain=Domain, ndstruct.netbios=NetbiosName
78  *
79  *
80  */
81  p = margs->nlist;
82  np = margs->nlist;
83  debug((char *) "%s| %s: DEBUG: Netbios list %s\n", LogTime(), PROGRAM, margs->nlist ? margs->nlist : "NULL");
84  dp = nullptr;
85 
86  if (!p) {
87  debug((char *) "%s| %s: DEBUG: No netbios names defined.\n", LogTime(), PROGRAM);
88  return (0);
89  }
90  while (*p) { /* loop over group list */
91  if (*p == '\n' || *p == '\r') { /* Ignore CR and LF if exist */
92  ++p;
93  continue;
94  }
95  if (*p == '@') { /* end of group name - start of domain name */
96  if (p == np) { /* empty group name not allowed */
97  debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
98  free_nd(ndsp);
99  return (1);
100  }
101  if (dp) { /* end of domain name - twice */
102  debug((char *) "%s| %s: @ is not allowed in netbios name %s@%s\n",LogTime(), PROGRAM,np,dp);
103  free_nd(ndsp);
104  return(1);
105  }
106  *p = '\0';
107  ++p;
108  ndsp = init_nd();
109  ndsp->netbios = xstrdup(np);
110  ndsp->next = ndspn;
111  dp = p; /* after @ starts new domain name */
112  } else if (*p == ':') { /* end of group name or end of domain name */
113  if (p == np) { /* empty group name not allowed */
114  debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
115  free_nd(ndsp);
116  return (1);
117  }
118  *p = '\0';
119  ++p;
120  if (dp) { /* end of domain name */
121  ndsp->domain = xstrdup(dp);
122  dp = nullptr;
123  } else { /* end of group name and no domain name */
124  ndsp = init_nd();
125  ndsp->netbios = xstrdup(np);
126  ndsp->next = ndspn;
127  }
128  ndspn = ndsp;
129  np = p; /* after : starts new group name */
130  if (!ndsp->domain || !strcmp(ndsp->domain, "")) {
131  debug((char *) "%s| %s: DEBUG: No domain defined for netbios name %s\n", LogTime(), PROGRAM, ndsp->netbios);
132  free_nd(ndsp);
133  return (1);
134  }
135  debug((char *) "%s| %s: DEBUG: Netbios name %s Domain %s\n", LogTime(), PROGRAM, ndsp->netbios, ndsp->domain);
136  } else
137  ++p;
138  }
139  if (p == np) { /* empty group name not allowed */
140  debug((char *) "%s| %s: DEBUG: No netbios name defined for domain %s\n", LogTime(), PROGRAM, p);
141  free_nd(ndsp);
142  return (1);
143  }
144  if (dp) { /* end of domain name */
145  ndsp->domain = xstrdup(dp);
146  } else { /* end of group name and no domain name */
147  ndsp = init_nd();
148  ndsp->netbios = xstrdup(np);
149  ndsp->next = ndspn;
150  }
151  if (!ndsp->domain || !strcmp(ndsp->domain, "")) {
152  debug((char *) "%s| %s: DEBUG: No domain defined for netbios name %s\n", LogTime(), PROGRAM, ndsp->netbios);
153  free_nd(ndsp);
154  return (1);
155  }
156  debug((char *) "%s| %s: DEBUG: Netbios name %s Domain %s\n", LogTime(), PROGRAM, ndsp->netbios, ndsp->domain);
157 
158  margs->ndoms = ndsp;
159  return (0);
160 }
161 
162 char *
163 get_netbios_name(struct main_args *margs, char *netbios)
164 {
165  struct ndstruct *nd;
166 
167  nd = margs->ndoms;
168  while (nd && netbios) {
169  debug((char *) "%s| %s: DEBUG: Netbios domain loop: netbios@domain %s@%s\n", LogTime(), PROGRAM, nd->netbios, nd->domain);
170  if (nd->netbios && !strcasecmp(nd->netbios, netbios)) {
171  debug((char *) "%s| %s: DEBUG: Found netbios@domain %s@%s\n", LogTime(), PROGRAM, nd->netbios, nd->domain);
172  return (nd->domain);
173  }
174  nd = nd->next;
175  }
176 
177  return nullptr;
178 }
179 #endif
180 
#define xmalloc
int create_nd(struct main_args *margs)
#define PROGRAM
Definition: support.h:169
char * netbios
Definition: support.h:64
void debug(const char *format,...)
Definition: debug.cc:19
#define xstrdup
char * domain
Definition: support.h:65
char * get_netbios_name(struct main_args *margs, char *netbios)
const char * LogTime(void)
#define xfree
char * nlist
Definition: support.h:78
struct ndstruct * next
Definition: support.h:66
struct ndstruct * ndoms
Definition: support.h:91

 

Introduction

Documentation

Support

Miscellaneous