<commit_before>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/


#include "condor_common.h"

#if !defined(SKIP_AUTHENTICATION) && defined(HAVE_EXT_OPENSSL)
#define ouch(x) dprintf(D_SECURITY,"SSL Auth: %s",x)
#include "authentication.h"
#include "condor_auth_ssl.h"
#include "condor_config.h"
#include "condor_environ.h"
#include "CondorError.h"
#include "openssl/rand.h"
#include "condor_netdb.h"
#include "condor_sinful.h"

#if defined(DLOPEN_SECURITY_LIBS)
#include <dlfcn.h>
#include "condor_auth_kerberos.h"
#endif

#include <algorithm>
#include <sstream>
#include <string.h>

namespace {

bool hostname_match(const char *match_pattern, const char *hostname)
{
    char match_copy[256], host_copy[256];
    char *tok1, *tok2;
    char *run1, *run2;
    int idx;

    // Basic input sanity checks.
    if ((match_pattern == NULL || hostname == NULL) ||
       ((strlen(match_pattern) > 255) || strlen(hostname) > 255))
        return false;

    // All matching on hostnames must be case insensitive.
    for (idx = 0; match_pattern[idx]; idx++) {
        match_copy[idx] = tolower(match_pattern[idx]);
    }
    match_copy[idx] = '\0';
    for (idx = 0; hostname[idx]; idx++) {
        host_copy[idx] = tolower(hostname[idx]);
    }

    host_copy[idx] = '\0';

	// This is the same trick as done in condor_startd.V6/ResAttributes.cpp; as
	// noted there, it's not the exact same function but "close enough"
#ifdef WIN32
#define strtok_r strtok_s
#endif

    // Split the strings by '.' character, iterate through each sub-component.
    run1 = nullptr;
    run2 = nullptr;
    for (tok1 = strtok_r(match_copy, ".", &run1), tok2 = strtok_r(host_copy, ".", &run2);
         tok1 && tok2;
         tok1 = strtok_r(match_copy, ".", &run1), tok2 = strtok_r(host_copy, ".", &run2))
    {
        // Match non-wildcard bits
        while (*tok1 && *tok2 && *tok1 == *tok2) {
            if (*tok2 == '*')
               return false;
            if (*tok1 == '*')
                break;
            tok1++;
            tok2++;
        }

        /**
         * At this point, one of the following must be true:
         * - We hit a wildcard.  In this case, we accept the match as
         *   long as there is no non-wildcard after it.
         * - We hit a character that doesn't match.
         * - We hit the of at least one string.
         */
        if (*tok1 == '*') {
            tok1++;
            // Non-wildcard after wildcard -- not acceptable.
            if (*tok1 != '\0')
                return false;<commit_msg>Fix usage of `strtok_r`. #7030<commit_after>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/


#include "condor_common.h"

#if !defined(SKIP_AUTHENTICATION) && defined(HAVE_EXT_OPENSSL)
#define ouch(x) dprintf(D_SECURITY,"SSL Auth: %s",x)
#include "authentication.h"
#include "condor_auth_ssl.h"
#include "condor_config.h"
#include "condor_environ.h"
#include "CondorError.h"
#include "openssl/rand.h"
#include "condor_netdb.h"
#include "condor_sinful.h"

#if defined(DLOPEN_SECURITY_LIBS)
#include <dlfcn.h>
#include "condor_auth_kerberos.h"
#endif

#include <algorithm>
#include <sstream>
#include <string.h>

namespace {

bool hostname_match(const char *match_pattern, const char *hostname)
{
    char match_copy[256], host_copy[256];
    char *tok1, *tok2;
    char *run1, *run2;
    int idx;

    // Basic input sanity checks.
    if ((match_pattern == NULL || hostname == NULL) ||
       ((strlen(match_pattern) > 255) || strlen(hostname) > 255))
        return false;

    // All matching on hostnames must be case insensitive.
    for (idx = 0; match_pattern[idx]; idx++) {
        match_copy[idx] = tolower(match_pattern[idx]);
    }
    match_copy[idx] = '\0';
    for (idx = 0; hostname[idx]; idx++) {
        host_copy[idx] = tolower(hostname[idx]);
    }

    host_copy[idx] = '\0';

	// This is the same trick as done in condor_startd.V6/ResAttributes.cpp; as
	// noted there, it's not the exact same function but "close enough"
#ifdef WIN32
#define strtok_r strtok_s
#endif

    // Split the strings by '.' character, iterate through each sub-component.
    run1 = nullptr;
    run2 = nullptr;
    for (tok1 = strtok_r(match_copy, ".", &run1), tok2 = strtok_r(host_copy, ".", &run2);
         tok1 && tok2;
         tok1 = strtok_r(NULL, ".", &run1), tok2 = strtok_r(NULL, ".", &run2))
    {
        // Match non-wildcard bits
        while (*tok1 && *tok2 && *tok1 == *tok2) {
            if (*tok2 == '*')
               return false;
            if (*tok1 == '*')
                break;
            tok1++;
            tok2++;
        }

        /**
         * At this point, one of the following must be true:
         * - We hit a wildcard.  In this case, we accept the match as
         *   long as there is no non-wildcard after it.
         * - We hit a character that doesn't match.
         * - We hit the of at least one string.
         */
        if (*tok1 == '*') {
            tok1++;
            // Non-wildcard after wildcard -- not acceptable.
            if (*tok1 != '\0')
                return false;
{'fraction_non_alphanumeric': 0.15998785670916818, 'fraction_numerical': 0.018518518518518517, 'mean_word_length': 2.808670520231214, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537334', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2026, 'n_words': 643}