home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Finger support...
- *
- * Finger server routines. Written by Michael T. Horne - KA7AXD.
- * Copyright 1988 by Michael T. Horne, All Rights Reserved.
- * Permission granted for non-commercial use and copying, provided
- * that this notice is retained.
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "global.h"
- #include "mbuf.h"
- #include "timer.h"
- #include "internet.h"
- #include "icmp.h"
- #include "netuser.h"
- #include "tcp.h"
- #include "finger.h"
- #include "session.h"
- #include "misc.h"
- #include "arc.h"
-
- static void sndmsg(struct tcb *, char *);
-
- struct tcb *fing_tcb = NULLTCB;
-
- int finger1(int argc, char **argv)
- {
- extern int32 ip_addr;
- struct socket lsocket;
-
- if (fing_tcb)
- return(0);
- /* start finger daemon */
- lsocket.address = ip_addr;
- if(argc < 2)
- lsocket.port = FINGER_PORT;
- else
- lsocket.port = atoi(argv[1]);
- fing_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, (void(*)())rcv_fing,
- NULLVFP, (void(*)())fing_state, 0, NULL);
- return(0);
- }
- /*
- * Handle incoming finger connections and closures.
- *
- */
- void fing_state(struct tcb *tcb, char old, char new)
- {
- struct finger *fing;
-
- old = old;
-
- switch(new){
- case ESTABLISHED:
- fing = (struct finger *) malloc(sizeof(struct finger));
-
- tcb->user = (char *)fing; /* Upward pointer */
- fing->tcb = tcb; /* Downward pointer */
- return;
- case CLOSED:
- if (tcb == fing_tcb)
- fing_tcb = NULLTCB;
- if (tcb->user != NULLCHAR)
- free(tcb->user);
- del_tcp(tcb);
- break;
- }
- }
-
- /*
- * Stop the finger server.
- */
-
- int finger0(void)
- {
- if (fing_tcb != NULLTCB) {
- close_tcp(fing_tcb);
- fing_tcb = NULLTCB;
- }
- return(0);
- }
-
- /*
- * Send a short message on a tcb
- */
-
- static void sndmsg(struct tcb *tcb, char *msg)
- {
- struct mbuf *bp;
-
- bp = qdata(msg,(int16)strlen(msg));
- send_tcp(tcb,bp);
- }
-
- /*
- * Finger receive upcall. This is the guts of the finger server.
- * The user to finger is read from the socket. If only a newline
- * is read, then send the remote host a list of all known 'users' on
- * this system.
- */
-
- void rcv_fing(register struct tcb *tcb, int16 ccnt)
- {
- struct finger *fing;
- FILE *fuser;
- struct mbuf *mbuf,
- *bp;
- char *buf,
- *who,
- *finger_file,
- *path,
- ch,
- temp[80],
- user[80];
- int cnt;
- int size;
-
- ccnt = ccnt;
-
- if ((fing = (struct finger *) tcb->user) == NULLFING) /* uh oh! */
- return;
- if(recv_tcp(tcb,&bp,FINGNAMELEN) == 0)
- return;
- if ((who = malloc(FINGNAMELEN + 1)) == NULL) {
- free_p(bp);
- return;
- }
-
- cnt = pullup(&bp, who, FINGNAMELEN); /* get 'user' name */
- who[cnt] = '\0'; /* NULL terminate it */
- free_p(bp); /* all done with bp */
-
- if (*who == '\015' || *who == '\012') { /* give him a user listing */
- int found = 0;
-
- path = (char *) malloc(strlen(fingerpath) + 5);
- /* create wildcard path to finger files */
- strcpy(path, fingerpath);
-
- sndmsg(tcb, "Known users on this system:\015\012");
- for (filedir(path, 0, user); user[0] != '\0';
- filedir (path, 1, user)) {
- found++;
- sprintf(temp, " %s\015\012", user);
- sndmsg(tcb, temp);
- }
- if (!found)
- sndmsg(tcb, "None!\015\012");
-
- free(path);
- }
- else {
- buf = who;
- while (*buf != '\015' && *buf != '\012' && *buf != '\0')
- buf++;
- *buf = '\0';
- /*
- * Create path to user's finger file and see if the
- * the file exists.
- */
- finger_file = malloc(strlen(fingerpath) + strlen(who) + 5);
- if (finger_file == NULL) { /* uh oh... */
- free(who); /* clean up */
- close_tcp(tcb); /* close socket */
- return;
- }
- sprintf(finger_file, "%s.%s", fingerpath, who);
-
- if ((fuser = fopen(finger_file, "r")) == (FILE *) NULL) {
- sprintf(temp, "User %s not known\015\012", who);
- sndmsg(tcb, temp);
- }
- else { /* valid 'user' */
- ch = fgetc(fuser); /* first get must be outside */
- while (!feof(fuser)) {
- size = tcb->window;
- if ((mbuf = alloc_mbuf(size)) == NULLBUF) {
- fclose(fuser); /* barf */
- free(who);
- free(finger_file);
- return;
- }
- buf = mbuf->data; /* pointer to buffer */
- while(!feof(fuser) && size--) { /* loop */
- switch(ch) {
- case '\032': /* NO ^Z's! */
- break;
- case '\012': /* EOL */
- *buf++ = '\015';
- *buf++ = '\012';
- mbuf->cnt += 2;
- break;
- case '\015':
- break;
- default:
- *buf++ = ch;
- mbuf->cnt++;
- break;
- }
- ch = fgetc(fuser);
- }
- send_tcp(tcb, mbuf); /* send info */
- }
- fclose(fuser);
- }
- free(finger_file);
- }
- free(who);
- close_tcp(tcb); /* close socket */
- return;
- }
-
-