"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/header.c" (12 Oct 2016, 8229 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


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 "header.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : header.c
    4  *  Author    : Urs Janssen <urs@tin.org>
    5  *  Created   : 1997-03-10
    6  *  Updated   : 2016-07-29
    7  *
    8  * Copyright (c) 1997-2017 Urs Janssen <urs@tin.org>
    9  * All rights reserved.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. The name of the author may not be used to endorse or promote
   20  *    products derived from this software without specific prior written
   21  *    permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   34  */
   35 
   36 #ifndef TIN_H
   37 #   include "tin.h"
   38 #endif /* !TIN_H */
   39 #ifndef TNNTP_H
   40 #   include "tnntp.h"
   41 #endif /* !TNNTP_H */
   42 
   43 static const char *get_full_name(void);
   44 static const char *get_user_name(void);
   45 
   46 
   47 /* find hostname */
   48 const char *
   49 get_host_name(
   50     void)
   51 {
   52     char *ptr;
   53     static char hostname[MAXHOSTNAMELEN + 1]; /* need space for '\0' */
   54 
   55     hostname[0] = '\0';
   56 
   57 #ifdef HAVE_GETHOSTNAME
   58     gethostname(hostname, sizeof(hostname) - 1);
   59 #endif /* HAVE_GETHOSTNAME */
   60 #ifdef HAVE_SYS_UTSNAME_H
   61     if (!*hostname)
   62         my_strncpy(hostname, system_info.nodename, sizeof(hostname) - 1);
   63 #endif /* HAVE_SYS_UTSNAME_H */
   64     if (!*hostname) {
   65         if ((ptr = getenv("HOST")) != NULL)
   66             my_strncpy(hostname, ptr, sizeof(hostname) - 1);
   67         else {
   68             if ((ptr = getenv("HOSTNAME")) != NULL)
   69                 my_strncpy(hostname, ptr, sizeof(hostname) - 1);
   70             else
   71                 hostname[0] = '\0';
   72         }
   73     }
   74     hostname[MAXHOSTNAMELEN] = '\0';
   75     return hostname;
   76 }
   77 
   78 
   79 #ifdef DOMAIN_NAME
   80 /*
   81  * find domainname - check DOMAIN_NAME
   82  * TODO: check /etc/defaultdomain as a last resort?
   83  */
   84 const char *
   85 get_domain_name(
   86     void)
   87 {
   88     FILE *fp;
   89     char *ptr;
   90     char buff[MAXHOSTNAMELEN + 1];
   91     static char domain[8192];
   92 
   93     domain[0] = '\0';
   94 
   95     if (strlen(DOMAIN_NAME))
   96         strcpy(domain, DOMAIN_NAME);
   97 
   98     if (domain[0] == '/' && domain[1]) {
   99         /* read domainname from specified file */
  100         if ((fp = fopen(domain, "r")) != NULL) {
  101             while (fgets(buff, (int) sizeof(buff), fp) != NULL) {
  102                 if (buff[0] == '#' || buff[0] == '\n')
  103                     continue;
  104 
  105                 if ((ptr = strrchr(buff, '\n'))) {
  106                     *ptr = '\0';
  107                     strcpy(domain, buff);
  108                 }
  109             }
  110             if (domain[0] == '/')   /* '/' is not allowed in domainames -> file was empty */
  111                 domain[0] = '\0';
  112 
  113             fclose(fp);
  114         } else
  115             domain[0] = '\0';
  116     }
  117     domain[MAXHOSTNAMELEN + 1] = '\0';
  118     return domain;
  119 }
  120 #endif /* DOMAIN_NAME */
  121 
  122 
  123 #ifdef HAVE_GETHOSTBYNAME
  124 #   define MAXLINELEN   1024
  125 #   define WS   " \f\t\v"
  126 /* find FQDN - gethostbyaddr() */
  127 const char *
  128 get_fqdn(
  129     const char *host)
  130 {
  131     char *domain;
  132     char line[MAXLINELEN + 1];
  133     char name[MAXHOSTNAMELEN + 1];
  134     static char fqdn[1024];
  135     struct hostent *hp;
  136     struct in_addr in = {0};
  137 
  138     *fqdn = '\0';
  139     domain = NULL;
  140     name[MAXHOSTNAMELEN] = '\0';
  141 
  142     if (host) {
  143         if (strchr(host, '.'))
  144             return host;
  145         my_strncpy(name, host, sizeof(name) - 1);
  146     } else {
  147 #   ifdef HAVE_GETHOSTNAME
  148         if (gethostname(name, sizeof(name) - 1))
  149 #   endif /* HAVE_GETHOSTNAME */
  150             return NULL;
  151     }
  152 
  153 #   ifdef HAVE_INET_ADDR
  154     if ('0' <= *name && *name <= '9') {
  155         in_addr_t addr = inet_addr(name);
  156 
  157         if ((hp = gethostbyaddr((char *) &addr, 4, AF_INET)))
  158             in.s_addr = (*hp->h_addr_list[0]);
  159         return (hp && strchr(hp->h_name, '.') ? hp->h_name : inet_ntoa(in));
  160     }
  161 #   endif /* HAVE_INET_ADDR */
  162     if ((hp = gethostbyname(name)) && !strchr(hp->h_name, '.'))
  163         if ((hp = gethostbyaddr(hp->h_addr_list[0], hp->h_length, hp->h_addrtype)))
  164             in.s_addr = (*hp->h_addr_list[0]);
  165 
  166     snprintf(fqdn, sizeof(fqdn), "%s", hp
  167         ? strchr(hp->h_name, '.')
  168             ? hp->h_name : inet_ntoa(in)
  169         : "");
  170 
  171     if (!*fqdn || (fqdn[strlen(fqdn) - 1] <= '9')) {
  172         FILE *inf;
  173 
  174         *fqdn = '\0';
  175 
  176         if ((inf = fopen("/etc/resolv.conf", "r")) != NULL) {
  177             char *eos;
  178             int j;
  179 
  180             while (fgets(line, MAXLINELEN, inf)) {
  181                 if (line[0] == '#' || line[0] == '\n')
  182                     continue;
  183 
  184                 line[MAXLINELEN] = '\0';
  185 
  186                 if ((eos = strpbrk(line, WS)) != NULL) {
  187                     if ((j = eos - line)) {
  188                         if (!strncmp(line, "domain", j) || !strncmp(line, "search", j)) {
  189                             domain = strtok(eos, WS);
  190                             break;
  191                         }
  192                     }
  193                 }
  194             }
  195             if (domain)
  196                 snprintf(fqdn, sizeof(fqdn), "%s.%s", name, strip_line(domain));
  197 
  198             fclose(inf);
  199         }
  200     }
  201     return fqdn;
  202 }
  203 #endif /* HAVE_GETHOSTBYNAME */
  204 
  205 
  206 /*
  207  * Find username & fullname
  208  */
  209 void
  210 get_user_info(
  211     char *user_name,
  212     char *full_name)
  213 {
  214     const char *ptr;
  215 
  216     user_name[0] = '\0';
  217     full_name[0] = '\0';
  218 
  219     if ((ptr = get_full_name()))
  220         strcpy(full_name, ptr);
  221     if ((ptr = get_user_name()))
  222         strcpy(user_name, ptr);
  223 }
  224 
  225 
  226 static const char *
  227 get_user_name(
  228     void)
  229 {
  230     static char username[128];
  231     struct passwd *pw;
  232 
  233     username[0] = '\0';
  234 
  235     if ((pw = getpwuid(getuid())) != NULL)
  236         STRCPY(username, pw->pw_name);
  237     else {
  238         if (!*username)
  239             tin_done(EXIT_FAILURE, _(txt_error_passwd_missing));
  240     }
  241 
  242     return username;
  243 }
  244 
  245 
  246 static const char *
  247 get_full_name(
  248     void)
  249 {
  250     char *p;
  251     static char fullname[128];
  252 #   ifndef DONT_HAVE_PW_GECOS
  253     char buf[128];
  254     char tmp[128];
  255     struct passwd *pw;
  256 #   endif /* !DONT_HAVE_PW_GECOS */
  257 
  258     fullname[0] = '\0';
  259 
  260     if ((p = getenv("NAME")) != NULL) {
  261         my_strncpy(fullname, p, sizeof(fullname) - 1);
  262         return fullname;
  263     }
  264     if ((p = getenv("REALNAME")) != NULL) {
  265         my_strncpy(fullname, p, sizeof(fullname) - 1);
  266         return fullname;
  267     }
  268 
  269 #   ifndef DONT_HAVE_PW_GECOS
  270     if ((pw = getpwuid(getuid())) != NULL) {
  271         STRCPY(buf, pw->pw_gecos);
  272         if ((p = strchr(buf, ',')))
  273             *p = '\0';
  274         if ((p = strchr(buf, '&'))) {
  275             *p++ = '\0';
  276             STRCPY(tmp, pw->pw_name);
  277             if (*tmp && isalpha((int)(unsigned char) *tmp) && islower((int)(unsigned char) *tmp))
  278                 *tmp = toupper((int)(unsigned char) *tmp);
  279             snprintf(fullname, sizeof(fullname), "%s%s%s", buf, tmp, p);
  280         } else
  281             STRCPY(fullname, buf);
  282     }
  283 #   endif /* !DONT_HAVE_PW_GECOS */
  284     return fullname;
  285 }
  286 
  287 
  288 /*
  289  * FIXME to:
  290  * char *build_from(full_name, user_name, domain_name)
  291  */
  292 /*
  293  * build From: in 'name <user@host.doma.in>' format
  294  */
  295 void
  296 get_from_name(
  297     char *from_name,
  298     struct t_group *thisgrp)
  299 {
  300     char *fromhost = domain_name;
  301 
  302     if (thisgrp && *thisgrp->attribute->from != '\0') {
  303         strcpy(from_name, thisgrp->attribute->from);
  304         return;
  305     }
  306 
  307     sprintf(from_name, ((strpbrk(get_full_name(), "!()<>@,;:\\\".[]")) ? "\"%s\" <%s@%s>" : "%s <%s@%s>"), get_full_name(), get_user_name(), fromhost);
  308 
  309 #   ifdef DEBUG
  310     if (debug & DEBUG_MISC)
  311         error_message(2, "FROM=[%s] USER=[%s] HOST=[%s] NAME=[%s]", from_name, get_user_name(), domain_name, get_full_name());
  312 #   endif /* DEBUG */
  313 
  314 }
  315 
  316 
  317 /*
  318  * build_sender()
  319  * returns *(Full_Name <user@fq.domainna.me>)
  320  */
  321 #ifndef FORGERY
  322 char *
  323 build_sender(
  324     void)
  325 {
  326     const char *ptr;
  327     static char sender[8192];
  328 
  329     sender[0] = '\0';
  330 
  331     if ((ptr = get_full_name()))
  332         snprintf(sender, sizeof(sender), ((strpbrk(ptr, "\".:;<>@[]()\\")) ? "\"%s\"" : "%s "), ptr);
  333     if ((ptr = get_user_name())) {
  334         snprintf(sender + strlen(sender), sizeof(sender) - strlen(sender), "<%s@", ptr);
  335 
  336 #   ifdef HAVE_GETHOSTBYNAME
  337         if ((ptr = get_fqdn(get_host_name())))
  338 #   else
  339         if ((ptr = get_host_name()))
  340 #   endif /* HAVE_GETHOSTBYNAME */
  341             snprintf(sender + strlen(sender), sizeof(sender) - strlen(sender), "%s>", ptr);
  342         else
  343             return NULL;
  344     } else
  345         return NULL;
  346 
  347     return sender;
  348 }
  349 #endif /* !FORGERY */