"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/send.c" (25 Nov 2002, 3879 Bytes) of package /linux/misc/old/pidentd-3.0.19.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 "send.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** send.c - Format and send IDENT protocol replies.
    3 **
    4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 
   15 #include "config.h"
   16 
   17 #include <stdio.h>
   18 #ifdef HAVE_UNISTD_H
   19 #include <unistd.h>
   20 #endif
   21 #include <string.h>
   22 #include <syslog.h>
   23 #include <pwd.h>
   24 
   25 #include <sys/types.h>
   26 #include <netinet/in.h>
   27 #include <arpa/inet.h>
   28 
   29 #include "pidentd.h"
   30 
   31 int uidonly_flag = 0;
   32 int noident_flag = 0;
   33 int result_syslog_level = -1;
   34 
   35 char *charset = NULL;
   36 char *opsys = "UNIX";
   37 
   38 
   39 static int
   40 check_noident(char *dir)
   41 {
   42     char buf[2048];
   43     size_t len;
   44 
   45     
   46     if (dir == NULL || strlen(dir) > 1024)
   47     return -1;
   48 
   49     len = strlen(dir);
   50     memcpy(buf, dir, len);
   51     if (len > 0 && buf[len-1] != '/')
   52     buf[len++] = '/';
   53     strcpy(buf+len, ".noident");
   54 
   55     if (access(buf, F_OK) == 0)
   56     return 1;
   57     else
   58     return 0;
   59 }
   60 
   61 
   62 void
   63 send_error(int fd,
   64        int l_port,
   65        int r_port,
   66        const char *what,
   67        struct sockaddr_gen *remote_addr)
   68 {
   69     char buf[1024];
   70     char buf2[32];
   71 
   72     
   73     s_snprintf(buf, sizeof(buf),
   74          "%d , %d : ERROR : %s\r\n", l_port, r_port, what);
   75     s_write(fd, buf, strlen(buf));
   76 
   77     if (result_syslog_level > -1)
   78     syslog(result_syslog_level,
   79            "reply to %s: %d , %d : ERROR: %s",
   80            s_inet_ntox(remote_addr, buf2, sizeof(buf2)),
   81            l_port, r_port, what);
   82 }
   83 
   84 
   85 void
   86 send_result(int fd,
   87         struct kernel *kp)
   88 {
   89     char buf[2048], pbuf[2048], buf2[32];
   90     struct passwd pwb, *pp = NULL;
   91     uid_t uid;
   92 
   93 
   94     if (debug)
   95     fprintf(stderr, "send_result(%d) - ruid = %ld, euid = %ld\n",
   96         fd, (long) kp->ruid, (long) kp->euid);
   97     
   98     if (kp->ruid == NO_UID)
   99     uid = kp->euid;
  100     else
  101     uid = kp->ruid;
  102 
  103     if (!uidonly_flag || noident_flag)
  104     (void) s_getpwuid_r(uid, &pwb, pbuf, sizeof(pbuf), &pp);
  105 
  106     if (noident_flag && pp != NULL && check_noident(pp->pw_dir) == 1)
  107     {
  108     syslog(LOG_INFO, "User %s elected to use .noident", pp->pw_name);
  109     
  110     send_error(fd,
  111            ntohs(SGPORT(kp->local)),
  112            ntohs(SGPORT(kp->remote)),
  113            "HIDDEN-USER",
  114            &kp->remote);
  115     return;
  116     }
  117 
  118 #ifdef HAVE_LIBDES
  119     if (encrypt_flag)
  120     {
  121     char buffer[33];
  122 
  123     pdes_encrypt(kp, buffer);
  124     s_snprintf(buf, sizeof(buf),
  125          "%d , %d : USERID : OTHER%s%s :[%s]\r\n",
  126          ntohs(SGPORT(kp->local)),
  127          ntohs(SGPORT(kp->remote)),
  128          charset != NULL ? " , " : "",
  129          charset != NULL ? charset : "",
  130          buffer);
  131     }
  132     else
  133 #endif
  134     
  135     if (!uidonly_flag && pp != NULL && strlen(pp->pw_name) < sizeof(buf)-128)
  136     {
  137     s_snprintf(buf, sizeof(buf),
  138          "%d , %d : USERID : %s%s%s :%s\r\n",
  139          ntohs(SGPORT(kp->local)),
  140          ntohs(SGPORT(kp->remote)),
  141          opsys,
  142          charset != NULL ? " , " : "",
  143          charset != NULL ? charset : "",
  144          pp->pw_name);
  145     }
  146     else
  147     {
  148     s_snprintf(buf, sizeof(buf),
  149          "%d , %d : USERID : OTHER :%ld\r\n",
  150          ntohs(SGPORT(kp->local)),
  151          ntohs(SGPORT(kp->remote)),
  152          (long) uid);
  153     }
  154     
  155     s_write(fd, buf, strlen(buf));
  156     
  157     if (result_syslog_level > -1)
  158     syslog(result_syslog_level,
  159            "reply to %s: %.*s",
  160            s_inet_ntox(&kp->remote, buf2, sizeof(buf2)),
  161            strlen(buf)-2, buf);
  162 }
  163 
  164 
  165 
  166 void
  167 send_version(int fd,
  168          struct sockaddr_gen *remote_addr)
  169 {
  170     char buf[1024], buf2[32];
  171     
  172     s_snprintf(buf, sizeof(buf),
  173          "0 , 0 : X-VERSION : pidentd %s for %s (%s %s)\r\n",
  174          server_version,
  175          osinfo_build, __DATE__, __TIME__);
  176     
  177     s_write(fd, buf, strlen(buf));
  178     
  179     if (result_syslog_level > -1)
  180     syslog(result_syslog_level,
  181            "reply to %s: %.*s",
  182            s_inet_ntox(remote_addr, buf2, sizeof(buf2)),
  183            strlen(buf)-2, buf);
  184 }