home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / fingerd / fingerd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-17  |  4.3 KB  |  168 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)fingerd.c    5.6 (Berkeley) 6/1/90";*/
  42. static char rcsid[] = "$Id: fingerd.c,v 1.1 1994/05/23 09:03:33 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. #include <stdio.h>
  46. #include "pathnames.h"
  47. #include <pwd.h>
  48. #include <syslog.h>
  49. #include <sys/utsname.h>
  50. #include <string.h>
  51. #include <netdb.h>
  52.  
  53.  
  54. main(argc, argv)
  55.     int argc;
  56.     char *argv[];
  57. {
  58.     extern int opterr, optind;
  59.     struct passwd *pw;
  60.     char *sp;
  61.     register FILE *fp;
  62.     register int ch, ca;
  63.     register char *lp;
  64.     int p[2];
  65. #define    ENTRIES    50
  66.     char **ap, *av[ENTRIES + 1], line[1024], *strtok();
  67.     int welcome = 0;
  68.  
  69.     opterr = 0;
  70.     while ((ca = getopt(argc, argv, "w")) != EOF)
  71.         switch(ca) {
  72.         case 'w':
  73.             welcome = 1;
  74.             break;
  75.         case '?':
  76.         default:
  77.             syslog(LOG_ERR, "usage: fingerd [-w]");
  78.             exit(1);
  79.         }
  80.     argc -= optind;
  81.     argv += optind;
  82.  
  83. #ifdef LOGGING                    /* unused for now */
  84. #include <netinet/in.h>
  85.     struct sockaddr_in sin;
  86.     int sval;
  87.  
  88.     sval = sizeof(sin);
  89.     if (getpeername(0, &sin, &sval) < 0)
  90.         fatal("getpeername");
  91. #endif
  92.  
  93.     if (!fgets(line, sizeof(line), stdin))
  94.         exit(1);
  95.  
  96.     if(welcome) {
  97.         char myname[128];
  98.         struct hostent *hp;
  99.         struct utsname utsname;
  100.  
  101.         (void) uname(&utsname);
  102.         gethostname(myname, sizeof(myname));
  103.         if ((hp = gethostbyname(myname)))
  104.             strcpy(myname, hp->h_name);
  105.         printf("\r\nWelcome to %s version %s at %s !\r\n\n",
  106.                 utsname.sysname, utsname.release, myname);
  107.         fflush(stdout);
  108.         sprintf(myname, "%s -c %s", _PATH_SH, _PATH_UPTIME);
  109.         system(myname);
  110.         fflush(stdout);
  111.         printf("\r\n");
  112.         fflush(stdout);
  113.     }
  114.  
  115.     av[0] = "finger";
  116.     for (lp = line, ap = &av[1];;) {
  117.         *ap = strtok(lp, " \t\r\n");
  118.         if (!*ap)
  119.             break;
  120.         /* RFC742: "/[Ww]" == "-l" */
  121.         if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w'))
  122.             *ap = "-l";
  123.         if (++ap == av + ENTRIES)
  124.             break;
  125.         lp = NULL;
  126.     }
  127.  
  128.     if (pipe(p) < 0)
  129.         fatal("pipe");
  130.  
  131.     if ((pw = getpwnam("nobody")) != NULL) {
  132.         (void) setgid(pw->pw_gid);
  133.         (void) setuid(pw->pw_uid);
  134.     }
  135.  
  136.     switch(fork()) {
  137.     case 0:
  138.         (void)close(p[0]);
  139.         if (p[1] != 1) {
  140.             (void)dup2(p[1], 1);
  141.             (void)close(p[1]);
  142.         }
  143.         execv(_PATH_FINGER, av);
  144.         _exit(1);
  145.     case -1:
  146.         fatal("fork");
  147.     }
  148.     (void)close(p[1]);
  149.     if (!(fp = fdopen(p[0], "r")))
  150.         fatal("fdopen");
  151.     while ((ch = getc(fp)) != EOF) {
  152.         if (ch == '\n')
  153.             putchar('\r');
  154.         putchar(ch);
  155.     }
  156.     exit(0);
  157. }
  158.  
  159. fatal(msg)
  160.     char *msg;
  161. {
  162.     extern int errno;
  163.     char *strerror();
  164.  
  165.     fprintf(stderr, "fingerd: %s: %s\r\n", msg, strerror(errno));
  166.     exit(1);
  167. }
  168.