login.c

wsh/src/login.c · 6.1 KB

From the archive wsh-147.tar.gz

Download the archive   « Back to the archive contents

/* See the file wizshell.c, wizshell.h or the files in docs/ for information */

#include "wizshell.h"

/* search through file for a line that starts with string+":" */
/* returns 0 for failure, if success, then fp points at ":" */
int find_entry(str,fp)
  char *str;
  FILE *fp;
{
  char *tmp,*tmp2;
  int i,match,c=1;

  while(c!=EOF) {
    tmp2=str;
    match=0;
    do {
      c=fgetc(fp);
      if(c==':' && *tmp2=='\0') return 1;
      match=(c==*tmp2);
      tmp2++;
    } while (c!=EOF && c!='\n' && match && c!=':' && tmp2!='\0');

    if(!match) while(c!=EOF && c!='\n') c=fgetc(fp);

  }

  return 0;
}

int check_passwd() {
  char attempt[MAX_FIELD];
  char tmp[MAX_FIELD];

  signal(SIGINT, SIG_IGN);
  system("stty -echo");
  fprintf(stderr,"Password: ");
  fgets(attempt,MAX_FIELD,stdin);
  fprintf(stderr,"\n");
  system("stty echo");
  signal(SIGINT, SIG_DFL);

#ifdef ENCRYPT
  return (!strcmp(crypt(attempt,envir.passwd),envir.passwd));
#else
  return (!strcmp(attempt,envir.passwd));
#endif

}

int get_field(fp,str)
  FILE *fp;
  char *str;
{
  int i=0,c;

  while((c=fgetc(fp))!=EOF && c!='\n' && c!=':')
    str[i++]=c;
  str[i]='\0';
  if(i==0 && c!=':') return 0;
  return 1;
}
#ifdef DOT_O_FILES

/* where can the .o file be found -- don't start filename with '/' */
char *get_wizard_file(name)
  char *name;
{
  char *tmp;
  MK_STR(tmp,50+strlen(name));  /* nice and big, regardless of method */
  /* pick a method */
  sprintf(tmp,"players/%s.o",name);             /* players/jubal.o */
/*  sprintf(tmp,"players/%c/%s.o",name[0],name);  /* players/j/jubal.o */
/*  sprintf(tmp,"w/%c/%s.o",name[0],name);        /* w/j/jubal.o */
  return tmp;
}

/* what access type do they get? (this doesn't have to be a switch) */
/* you can use the name here if you want to */
char *get_access_type(level,name)
  int level;
  char *name;
{
  switch(level) {
    case 21: return "WIZ";
    case 100: return "ELDER";
    case 500: return "ARCH";
    case 1000: return "GOD";
    default:
      FATAL("Couldn't find access type for your level\n")
  }
}

#endif

/* where is their home directory? */
/* This will be changed to use password files and .o files later */
char *get_home_dir(name)
  char *name;
{
  char *tmp;
  MK_STR(tmp,50+strlen(name));  /* nice and big, regardless of method */
  /* pick a method */
  sprintf(tmp,"/players/%s",name);             /* /players/jubal */
/*  sprintf(tmp,"players/%c/%s",name[0],name);  /* players/j/jubal */
/*  sprintf(tmp,"/w/%s",name);                    /* /w/jubal */
  return tmp;
}

int login(getlogin)
  int getlogin;
{
  FILE *fp;
  char c,*tmp;
  int i=0,j,lev,done;
#ifdef DOT_O_FILES
  char buf[MAX_BUF];
#endif

#ifndef DOT_O_FILES
  if(!(fp=fopen(PASSWD_FILE,"r")))
    FATAL("Could not open password file");
#endif

  if(getlogin) fprintf(stderr,"--WizShell   v%s\n",VERSION);
  while(i<LOGIN_ATTEMPTS) {
#ifndef DOT_O_FILES
    rewind(fp);
#endif
    if(getlogin) {
      fprintf(stderr,"login: ");
      if(fgets(envir.login,MAX_FIELD,stdin)==NULL) /* EOF in login is exit */
        exit(-1);
      else {
        j=0;
        while(envir.login[j++]!='\n');
        envir.login[j-1]='\0';          /* replace \n with \0 */
      }
    } else {
      tmp=(char *)getenv("WSHLOGIN");
      if(!tmp || tmp[0]=='\0' || tmp[0]=='#') {
        fprintf(stderr,"You haven't logged in yet.\n");
#ifndef DOT_O_FILES
        fclose(fp);
#endif
        return 0;
      }
      strcpy(envir.login,tmp);
    }

    if (envir.login[0]!='\0' && envir.login[0]!='#') { /*no nulls or comments*/
      i++;

#ifdef DOT_O_FILES
      /* okay, so this mem doesn't get freed.  Big deal ;) */
      if(fp=fopen(get_wizard_file(envir.login),"r")) {
        rewind(fp);
        done=0;
        envir.real_name[0]='\0';
        while(done!=3 && fgets(buf,MAX_BUF,fp)!=NULL)
          if(sscanf(buf,"password \"%[^\"]s\"\n",envir.passwd)) done++;
          else if(sscanf(buf,"level %d\n",&lev)) done++;
          else if(sscanf(buf,"real_name \"%[^\"]s\"\n",envir.real_name))
            done++;
        fclose(fp);
        if(done<2) {
          fprintf(stderr,"Incomplete .o file.\n");
          continue;
        }
        if(envir.real_name[0]=='\0')
          strcpy(envir.real_name,envir.login);
        strcpy(envir.access,get_access_type(lev,envir.login));
        tmp=get_home_dir(envir.login);
        if(!tmp) {
          fprintf(stderr,"Couldn't get home directory for %s.\n",envir.login);
          FREE(tmp);
          continue;
        }
        strcpy(envir.home,tmp);
        FREE(tmp);

#else  /* use passwd file */
      if(find_entry(envir.login,fp)) {

        if(!get_field(fp,envir.passwd) ||
           !get_field(fp,envir.access)) {
             fprintf(stderr,"Incomplete entry in passwd file\n");
             continue;
        }
        get_field(fp,envir.real_name);
        get_field(fp,envir.home);
#endif

        envir.mesg=MESG_DEFAULT;

        if(envir.passwd[0]=='\0' ||           /* no password */
           !getlogin || check_passwd()) {     /* successful login */
#ifndef DOT_O_FILES  /* etc/passwd hasn't been closed yet */
          fclose(fp);
#endif
          return 1;
        }
        fprintf(stderr,"Login incorrect\n");
      } else {      /* entry or .o file didn't exist */
        if(!getlogin) {
          fprintf(stderr,"Couldn't login as %s\n",envir.login);
#ifndef DOT_O_FILES  /* etc/passwd hasn't been closed yet */
          fclose(fp);
#endif
          return 0;
        }
        envir.passwd[0]='*';
        envir.passwd[1]='\0';
        check_passwd();    /* take time involved in checking a real login */
        fprintf(stderr,"Login incorrect\n");
      }
    }
  }
#ifndef DOT_O_FILES  /* etc/passwd hasn't been closed yet */
          fclose(fp);
#endif
  return 0;  /* login failed */
}


/* main  (for testing) 
void main(argc, argv, envp)
  int argc;
  char **argv, **envp;
{
  if (login()) {
    fprintf(stderr,"\n\n");
    fprintf(stderr,"login: %s\n",envir.login);
    fprintf(stderr,"passwd: %s\n",envir.passwd);
    fprintf(stderr,"access: %s\n",envir.access);
    fprintf(stderr,"real_name: %s\n",envir.real_name);
    fprintf(stderr,"home: %s\n",envir.home);

    fprintf(stderr,"\n\nWelcome to WizPort\n");
  }
}
/* */

Site Stats

Snippets: 1791
Codebases: 571