"Fossies" - the Fresh Open Source Software Archive

Member "leafnode-1.12.0/validatefqdn.c" (28 Dec 2021, 4799 Bytes) of package /linux/misc/leafnode-1.12.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "validatefqdn.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.11.12_vs_1.12.0.

    1 /*
    2  * validatefqdn.c -- part of leafnode.
    3  *
    4  * Copyright (C) 2002 - 2010 Matthias Andree <matthias.andree@gmx.de>.
    5  * All rights reserved.
    6  *
    7 
    8    Redistribution and use in source and binary forms, with or without
    9    modification, are permitted provided that the following conditions are
   10    met:
   11 
   12      * Redistributions of source code must retain the above copyright notice,
   13        this list of conditions and the following disclaimer.
   14      * Redistributions in binary form must reproduce the above copyright
   15        notice, this list of conditions and the following disclaimer in the
   16        documentation and/or other materials provided with the distribution.
   17      * Neither the name of Matthias Andree nor the names of its
   18        contributors may be used to endorse or promote products derived from
   19        this software without specific prior written permission.
   20 
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   22    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   23    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   24    PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
   25    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   26    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   27    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   28    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   29    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   30    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   31    POSSIBILITY OF SUCH DAMAGE.
   32 */
   33 
   34 /* This function checks that the hostname contains at least a dot, does
   35  * not start with localhost and does not start with 127.0.0. -- if it
   36  * does, syslog, print to stderr and die.
   37  */
   38 
   39 #include "leafnode.h"
   40 
   41 #include <stdio.h>
   42 #include <stdlib.h>
   43 #include <string.h>
   44 #ifdef HAVE_STRINGS_H
   45 #include <strings.h>
   46 #endif
   47 #include <signal.h>
   48 
   49 #include <syslog.h>
   50 
   51 #include "validatefqdn.h"
   52 
   53 static int strcasecmpsuffix(const char *string, const char *suffix)
   54 {
   55     size_t lin = strlen(string);
   56     size_t lsu = strlen(suffix);
   57 
   58     if (lsu > lin) return -1;
   59 
   60     return strcasecmp(string + lin - lsu, suffix);
   61 }
   62 
   63 /** Check the supplied FQDN for validity.
   64  * \return 0 if invalid, 1 if valid
   65  */
   66 int is_validfqdn(const char *f) {
   67     /* do not let us fool by trailing dots */
   68     char *fqdn = strdup(f);
   69     if (!fqdn) return 0;
   70     while (fqdn[strlen(fqdn)-1] == '.')
   71     fqdn[strlen(fqdn)-1] = '\0';
   72 
   73     if (/* reject unqualified names */
   74     !(strchr(fqdn, '.'))
   75     /* Red Hat list the FQDN on the same line as localhost, thus,
   76      * the qualification returns two "localhost*" aliases */
   77     || 0 == strncasecmp(fqdn, "localhost", 9)
   78     /* protect against broken hosts or DNS */
   79     || 0 == strncmp(fqdn, "127.", 4)
   80     /* SuSE default hostname on some installs is linux.local */
   81     || 0 == strcasecmp(fqdn, "linux.local")
   82     /* kill RFC 2606 second- and top-level domains */
   83     || 0 == strcasecmpsuffix(fqdn, "example.org")
   84     || 0 == strcasecmpsuffix(fqdn, "example.com")
   85     || 0 == strcasecmpsuffix(fqdn, "example.net")
   86     || 0 == strcasecmpsuffix(fqdn, ".example")
   87     || 0 == strcasecmpsuffix(fqdn, ".invalid")
   88     || 0 == strcasecmpsuffix(fqdn, ".local")
   89     || 0 == strcasecmpsuffix(fqdn, ".localdomain")
   90     || 0 == strcasecmpsuffix(fqdn, ".localhost")
   91     || 0 == strcasecmpsuffix(fqdn, ".test")
   92     || 0 == strcasecmpsuffix(fqdn, ".site")
   93     )
   94     {
   95     free(fqdn);
   96     return 0;
   97     }
   98     free(fqdn);
   99     return 1;
  100 }
  101 
  102 void
  103 validatefqdn(int logtostdout)
  104 {
  105     /* kill bogus fqdn */
  106     if (!is_validfqdn(fqdn)) {
  107     const char *const fmt =
  108         "\nLeafnode must have a fully-qualified and globally unique domain name,\n"
  109         "not just \"%s\".\nEdit your /etc/hosts file to add "
  110         "a unique, fully qualified domain name.\n"
  111         "\"localhost.localdomain\" or thereabouts "
  112         "will not work;\nit's qualified, but not unique.\n"
  113         "Please see the README-FQDN file for details.\n\n";
  114 
  115     if (logtostdout) {
  116         printf("503 Leafnode must have a unique fully-qualified domain name. "
  117                "Have its administrator fix the configuration. "
  118                "More detail is in the logs.\r\n");
  119         fflush(stdout);
  120     }
  121     syslog(LOG_CRIT, fmt, fqdn);    /* RATS: ignore */
  122     fprintf(stderr, fmt, fqdn); /* RATS: ignore */
  123     raise(SIGKILL);
  124     }
  125 }
  126 
  127 #ifdef TEST
  128 int verbose = 0;
  129 int debug = 0;
  130 char fqdn[FQDNLEN + 1] = "";
  131 
  132 int main(int argc, char **argv) {
  133     int i = 1;
  134     if (argc == 1) {
  135     while(fgets(fqdn, sizeof(fqdn), stdin)) {
  136         fqdn[strcspn(fqdn, " \t\r\n")] = '\0';
  137         printf("validatefqdn(\"%s\") = %d\n", fqdn, is_validfqdn(fqdn));
  138     }
  139     } else {
  140 
  141     while (i < argc && argv[i]) {
  142         printf("validatefqdn(\"%s\") = %d\n", argv[i], is_validfqdn(argv[i]));
  143         i++;
  144     }
  145     }
  146     return 0;
  147 }
  148 #endif