51#if !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERROR_MESSAGE
52#define error_message(code) krb5_get_error_message(kparam.context,code)
53#elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_TEXT
54#define error_message(code) krb5_get_err_text(kparam.context,code)
55#elif !HAVE_ERROR_MESSAGE
56static char err_code[17];
57const char *KRB5_CALLCONV
58error_message(
long code) {
59 snprintf(err_code,16,
"%ld",code);
68 margs->
nlist =
nullptr;
69 margs->
glist =
nullptr;
70 margs->
llist =
nullptr;
71 margs->
ulist =
nullptr;
72 margs->
tlist =
nullptr;
73 margs->
luser =
nullptr;
74 margs->
lpass =
nullptr;
75 margs->
lbind =
nullptr;
76 margs->
lurl =
nullptr;
84 margs->
ndoms =
nullptr;
96 struct gdstruct *p =
nullptr, *pp =
nullptr;
117 struct ndstruct *p =
nullptr, *pp =
nullptr;
138 struct lsstruct *p =
nullptr, *pp =
nullptr;
175 clean_nd(margs->
ndoms);
176 margs->
ndoms =
nullptr;
188main(
int argc,
char *
const argv[])
191 char *user, *
domain, *group;
192 char *up=
nullptr, *dp=
nullptr, *np=
nullptr;
193 char *nuser, *nuser8 =
nullptr, *netbios;
197 krb5_error_code code = 0;
199 kparam.context =
nullptr;
202 setbuf(stdout,
nullptr);
203 setbuf(stdin,
nullptr);
207 while (-1 != (opt =
getopt(argc, argv,
"diasng:D:N:P:S:u:U:t:T:p:l:b:m:h"))) {
266 fprintf(stderr,
"Usage: \n");
267 fprintf(stderr,
"squid_kerb_ldap [-d] [-i] -g group list [-D domain] [-N netbios domain map] [-P service principal name] [-s] [-u ldap user] [-p ldap user password] [-l ldap url] [-b ldap bind path] [-a] [-m max depth] [-h]\n");
268 fprintf(stderr,
"-d full debug\n");
269 fprintf(stderr,
"-i informational messages\n");
270 fprintf(stderr,
"-n do not use Kerberos to authenticate to AD. Requires -u , -p and -l option\n");
271 fprintf(stderr,
"-g group list\n");
272 fprintf(stderr,
"-t group list (only group name hex UTF-8 format)\n");
273 fprintf(stderr,
"-T group list (all in hex UTF-8 format - except separator @)\n");
274 fprintf(stderr,
"-D default domain\n");
275 fprintf(stderr,
"-N netbios to dns domain map\n");
276 fprintf(stderr,
"-P service principal name to be used from keytab\n");
277 fprintf(stderr,
"-S ldap server to dns domain map\n");
278 fprintf(stderr,
"-u ldap user\n");
279 fprintf(stderr,
"-p ldap user password\n");
280 fprintf(stderr,
"-l ldap url\n");
281 fprintf(stderr,
"-b ldap bind path\n");
282 fprintf(stderr,
"-s use SSL encryption with Kerberos authentication\n");
283 fprintf(stderr,
"-a allow SSL without cert verification\n");
284 fprintf(stderr,
"-m maximal depth for recursive searches\n");
285 fprintf(stderr,
"-h help\n");
286 fprintf(stderr,
"The ldap url, ldap user and ldap user password details are only used if the kerberised\n");
287 fprintf(stderr,
"access fails(e.g. unknown domain) or if the username does not contain a domain part\n");
288 fprintf(stderr,
"and no default domain is provided.\n");
289 fprintf(stderr,
"If the ldap url starts with ldaps:// it is either start_tls or simple SSL\n");
290 fprintf(stderr,
"The group list can be:\n");
291 fprintf(stderr,
"group - In this case group can be used for all keberised and non kerberised ldap servers\n");
292 fprintf(stderr,
"group@ - In this case group can be used for all keberised ldap servers\n");
293 fprintf(stderr,
"group@domain - In this case group can be used for ldap servers of domain domain\n");
294 fprintf(stderr,
"group1@domain1:group2@domain2:group3@:group4 - A list is build with a colon as separator\n");
295 fprintf(stderr,
"Group membership is determined with AD servers through the users memberof attribute which\n");
296 fprintf(stderr,
"is followed to the top (e.g. if the group is a member of a group)\n");
297 fprintf(stderr,
"Group membership is determined with non AD servers through the users memberuid (assuming\n");
298 fprintf(stderr,
"PosixGroup) or primary group membership (assuming PosixAccount)\n");
299 fprintf(stderr,
"The ldap server list can be:\n");
300 fprintf(stderr,
"server - In this case server can be used for all Kerberos domains\n");
301 fprintf(stderr,
"server@ - In this case server can be used for all Kerberos domains\n");
302 fprintf(stderr,
"server@domain - In this case server can be used for Kerberos domain domain\n");
303 fprintf(stderr,
"server1a@domain1:server1b@domain1:server2@domain2:server3@:server4 - A list is build with a colon as separator\n");
314 if ( margs.
glist !=
nullptr ) {
320 debug((
char *)
"%s| %s: INFO: no group list given expect it from stdin\n",
LogTime(),
PROGRAM);
342 code = krb5_init_context(&kparam.context);
343 for (
int i=0; i<MAX_DOMAINS; i++) {
344 kparam.mem_ccache[i]=
nullptr;
345 kparam.cc[i]=
nullptr;
349 error((
char *)
"%s| %s: ERROR: Error while initialising Kerberos library : %s\n",
LogTime(),
PROGRAM, error_message(code));
358 if (fgets(buf,
sizeof(buf) - 1, stdin) ==
nullptr) {
360 debug((
char *)
"%s| %s: FATAL: fgets() failed! dying..... errno=%d (%s)\n",
LogTime(),
PROGRAM, ferror(stdin),
377 c = (
char *) memchr(buf,
'\n',
sizeof(buf) - 1);
381 SEND_BH(
"Invalid input. CR missing");
386 user = strtok(buf,
" \n");
389 SEND_BH(
"Invalid request. No Username");
393 nuser = strchr(user,
'\\');
395 nuser8 = strstr(user,
"%5C");
396 if (!nuser && !nuser8)
397 nuser8 = strstr(user,
"%5c");
398 domain = strrchr(user,
'@');
399 if (nuser || nuser8) {
411 debug((
char *)
"%s| %s: INFO: Got User: %s Netbios Name: %s\n",
LogTime(),
PROGRAM, up, np);
413 log((
char *)
"%s| %s: INFO: Got User: %s Netbios Name: %s\n",
LogTime(),
PROGRAM, up, np);
426 if (!domain && margs.
ddomain) {
430 debug((
char *)
"%s| %s: INFO: Got User: %s set default domain: %s\n",
LogTime(),
PROGRAM, up, dp);
432 log((
char *)
"%s| %s: INFO: Got User: %s set default domain: %s\n",
LogTime(),
PROGRAM, up, dp);
435 debug((
char *)
"%s| %s: INFO: Got User: %s Domain: %s\n",
LogTime(),
PROGRAM, up, domain ? dp :
"NULL");
437 log((
char *)
"%s| %s: INFO: Got User: %s Domain: %s\n",
LogTime(),
PROGRAM, up, domain ? dp :
"NULL");
441 if (!strcmp(user,
"QQ") && domain && !strcmp(domain,
"QQ")) {
450 if ((group = strtok(
nullptr,
" \n")) !=
nullptr) {
451 debug((
char *)
"%s| %s: INFO: Read group list %s from stdin\n",
LogTime(),
PROGRAM, group);
459 SEND_BH(
"Error in group list");
464 SEND_BH(
"No group list received on stdin");
484 *s = (char)toupper((
unsigned char) *s);
494 setbuf(stdout,
nullptr);
495 setbuf(stdin,
nullptr);
498 if (fgets(buf,
sizeof(buf) - 1, stdin) ==
nullptr) {
500 fprintf(stdout,
"ERR\n");
501 fprintf(stderr,
"LDAP group authorisation not supported\n");
int create_gd(struct main_args *margs)
void log(char *format,...)
void error(char *format,...)
void warn(char *format,...)
#define KERBEROS_LDAP_GROUP_VERSION
int create_nd(struct main_args *margs)
const char * LogTime(void)
void clean_args(struct main_args *margs)
int create_ls(struct main_args *margs)
char * get_netbios_name(struct main_args *margs, char *netbios)
int check_memberof(struct main_args *margs, char *user, char *domain)
void init_args(struct main_args *margs)
void debug(const char *format,...)
int getopt(int nargc, char *const *nargv, const char *ostr)
#define rfc1738_escape(x)
void rfc1738_unescape(char *url)