authstaticlistsearch.c
上传用户:s81996212
上传日期:2007-01-04
资源大小:722k
文件大小:2k
- /*
- ** Copyright 2000 Double Precision, Inc. See COPYING for
- ** distribution information.
- */
- #include "auth.h"
- #include "authstaticlist.h"
- #include <stdio.h>
- #include <errno.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #if HAVE_FCNTL_H
- #include <fcntl.h>
- #endif
- #if HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- static const char rcsid[]="$Id: authstaticlistsearch.c,v 1.1 2000/02/28 05:02:27 mrsam Exp $";
- static char *configfile=0;
- static int has_init;
- static void openconfigfile(const char *filename)
- {
- int fd=open(filename, O_RDONLY);
- char buf[BUFSIZ];
- int i;
- has_init=1;
- if (fd < 0)
- {
- if (errno != ENOENT)
- perror(filename);
- return;
- }
- i=read(fd, buf, sizeof(buf));
- close(fd);
- if (i < 0)
- {
- perror(filename);
- return;
- }
- if ((configfile=malloc(i+1)) == 0)
- {
- perror("malloc");
- return;
- }
- memcpy(configfile, buf, i);
- configfile[i]=0;
- }
- struct callback_func {
- int (*callback)(struct authinfo *, void *);
- /* Original callback func */
- void *callback_arg; /* Original callback arg */
- unsigned i; /* Current index in authstaticlist */
- } ;
- static int my_callback(struct authinfo *a, void *p)
- {
- struct callback_func *f=(struct callback_func *)p;
- a->staticindex=f->i;
- return ( (*f->callback)(a, f->callback_arg));
- }
- int authstaticlist_search(const char *userid, const char *service,
- const char *filename,
- int (*callback)(struct authinfo *, void *),
- void *callback_arg)
- {
- int i, rc;
- const char *p;
- char namebuf[32]; /* Names of authentication modules should fit in here */
- struct callback_func c;
- c.callback=callback;
- c.callback_arg=callback_arg;
- if (!has_init) openconfigfile(filename);
- if (!configfile)
- {
- for (i=0; authstaticlist[i].auth_name; i++)
- {
- c.i=i;
- if ((rc=(*authstaticlist[i].auth_prefunc)(userid,
- service,
- &my_callback, &c)) == 0)
- return (0);
- if (rc > 0) return (rc);
- }
- return (-1);
- }
- p=configfile;
- while (*p)
- {
- if ( isspace((int)(unsigned char)*p))
- {
- ++p;
- continue;
- }
- for (i=0; p[i] && !isspace((int)(unsigned char)p[i]); i++)
- ;
- namebuf[0]=0;
- strncat(namebuf, p, i < sizeof(namebuf)-1 ? i:
- sizeof(namebuf)-1);
- p += i;
- for (i=0; authstaticlist[i].auth_name; i++)
- {
- if (strcmp(authstaticlist[i].auth_name, namebuf))
- continue;
- c.i=i;
- if ((rc=(*authstaticlist[i].auth_prefunc)(userid,
- service,
- &my_callback, &c)) >= 0)
- return (rc);
- break;
- }
- }
- return (-1);
- }