📄 rlm_passwd.c
字号:
/* * rlm_passwd.c * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Copyright 2000 The FreeRADIUS server project */#include <stdio.h>#include <string.h>#include <stdlib.h>/*#include "autoconf.h"#include "libradius.h"*/#include "radiusd.h"#include "modules.h"struct mypasswd { struct mypasswd *next; char *listflag; char *field[1];};struct hashtable { int tablesize; int keyfield; int nfields; int islist; int ignorenis; char * filename; struct mypasswd **table; struct mypasswd *last_found; char buffer[1024]; FILE *fp; char *delimiter;};#ifdef TEST#define rad_malloc(s) malloc(s)void printpw(struct mypasswd *pw, int nfields){ int i; if (pw) { for( i = 0; i < nfields; i++ ) printf("%s:", pw->field[i]); printf("\n"); } else printf ("Not found\n"); fflush(stdout);}#endifstatic struct mypasswd * mypasswd_malloc(const char* buffer, int nfields, int* len){ struct mypasswd *t; /* reserve memory for (struct mypasswd) + listflag (nfields * sizeof (char*)) + ** fields (nfields * sizeof (char)) + strlen (inst->format) + 1 */ *len=sizeof (struct mypasswd) + nfields * sizeof (char*) + nfields * sizeof (char ) + strlen(buffer) + 1; t = (struct mypasswd *) rad_malloc(*len); if (t) memset(t, 0, *len); return (t);}static int string_to_entry(const char* string, int nfields, char delimiter, struct mypasswd *passwd, int bufferlen){ char *str; int len, i, fn=0; char *data_beg; len = strlen(string); if(!len) return 0; if (string[len-1] == '\n') len--; if(!len) return 0; if (string[len-1] == '\r') len--; if(!len) return 0; if (!len || !passwd || bufferlen < (len + nfields * sizeof (char*) + nfields * sizeof (char) + sizeof (struct mypasswd) + 1) ) return 0; passwd->next = NULL; data_beg=(char *)passwd + sizeof(struct mypasswd); str = data_beg + nfields * sizeof (char) + nfields * sizeof (char*); memcpy (str, string, len); str[len] = 0; passwd->field[fn++] = str; passwd->listflag = data_beg + nfields * sizeof (char *); for(i=0; i < len; i++){ if (str[i] == delimiter) { str[i] = 0; passwd->field[fn++] = str + i + 1; if (fn == nfields) break; } } for (; fn < nfields; fn++) passwd->field[fn] = NULL; return len + nfields * sizeof (char) + nfields * sizeof (char*) + sizeof (struct mypasswd) + 1;}static void destroy_password (struct mypasswd * pass){ struct mypasswd *p; while ((p=pass)!=NULL) { pass = pass->next; free(p); }}static unsigned int hash(const unsigned char * username, unsigned int tablesize){ int h=1; while (*username) { h = h * 7907 + *username++; } return h%tablesize;}static void release_hash_table(struct hashtable * ht){ int i; if (!ht) return; for (i=0; i<ht->tablesize; i++) if (ht->table[i]) destroy_password(ht->table[i]); if (ht->table) free(ht->table); if (ht->fp) fclose(ht->fp);}static void release_ht(struct hashtable * ht){ if (!ht) return; release_hash_table(ht); if (ht->filename) free(ht->filename); free(ht);}static struct hashtable * build_hash_table (const char * file, int nfields, int keyfield, int islist, int tablesize, int ignorenis, char * delimiter){#define passwd ((struct mypasswd *) ht->buffer) char buffer[1024]; struct hashtable* ht; int len; unsigned int h; struct mypasswd *hashentry, *hashentry1; char *list; char *nextlist=0; int i; ht = (struct hashtable *) rad_malloc(sizeof(struct hashtable)); if(!ht) { return NULL; } memset(ht, 0, sizeof(struct hashtable)); ht->filename = strdup(file); if(!ht->filename) { free(ht); return NULL; } ht->tablesize = tablesize; ht->nfields = nfields; ht->keyfield = keyfield; ht->islist = islist; ht->ignorenis = ignorenis; if (delimiter && *delimiter) ht->delimiter = delimiter; else ht->delimiter = ":"; if(!tablesize) return ht; if(!(ht->fp = fopen(file,"r"))) return NULL; memset(ht->buffer, 0, 1024); ht->table = (struct mypasswd **) rad_malloc (tablesize * sizeof(struct mypasswd *)); if (!ht->table) { /* * Unable allocate memory for hash table * Still work without it */ ht->tablesize = 0; return ht; } memset(ht->table, 0, tablesize * sizeof(struct mypasswd *)); while (fgets(buffer, 1024, ht->fp)) { if(*buffer && *buffer!='\n' && (!ignorenis || (*buffer != '+' && *buffer != '-')) ){ if(!(hashentry = mypasswd_malloc(buffer, nfields, &len))){ release_hash_table(ht); ht->tablesize = 0; return ht; } len = string_to_entry(buffer, nfields, *ht->delimiter, hashentry, len); if(!hashentry->field[keyfield] || *hashentry->field[keyfield] == '\0') { free(hashentry); continue; } if (islist) { list = hashentry->field[keyfield]; for (nextlist = list; *nextlist && *nextlist!=','; nextlist++); if (*nextlist) *nextlist++ = 0; else nextlist = 0; } h = hash(hashentry->field[keyfield], tablesize); hashentry->next = ht->table[h]; ht->table[h] = hashentry; if (islist) { for(list=nextlist; nextlist; list = nextlist){ for (nextlist = list; *nextlist && *nextlist!=','; nextlist++); if (*nextlist) *nextlist++ = 0; else nextlist = 0; if(!(hashentry1 = mypasswd_malloc("", nfields, &len))){ release_hash_table(ht); ht->tablesize = 0; return ht; } for (i=0; i<nfields; i++) hashentry1->field[i] = hashentry->field[i]; hashentry1->field[keyfield] = list; h = hash(list, tablesize); hashentry1->next = ht->table[h]; ht->table[h] = hashentry1; } } } } fclose(ht->fp); ht->fp = NULL; return ht;#undef passwd}static struct mypasswd * get_next(char *name, struct hashtable *ht){#define passwd ((struct mypasswd *) ht->buffer) struct mypasswd * hashentry; char buffer[1024]; int len; char *list, *nextlist; if (ht->tablesize > 0) { /* get saved address of next item to check from buffer */ hashentry = ht->last_found; for (; hashentry; hashentry = hashentry->next) { if (!strcmp(hashentry->field[ht->keyfield], name)) { /* save new address */ ht->last_found = hashentry->next; return hashentry; } } return NULL; } printf("try to find in file\n"); if (!ht->fp) return NULL; while (fgets(buffer, 1024,ht->fp)) { if(*buffer && *buffer!='\n' && (len = string_to_entry(buffer, ht->nfields, *ht->delimiter, passwd, sizeof(ht->buffer)-1)) && (!ht->ignorenis || (*buffer !='-' && *buffer != '+') ) ){ if(!ht->islist) { if(!strcmp(passwd->field[ht->keyfield], name)) return passwd; } else { for (list = passwd->field[ht->keyfield], nextlist = list; nextlist; list = nextlist) { for(nextlist = list; *nextlist && *nextlist!=','; nextlist++); if(!*nextlist)nextlist = 0; else *nextlist++ = 0; if(!strcmp(list, name)) return passwd; } } } } fclose(ht->fp); ht->fp = NULL; return NULL;#undef passwd}static struct mypasswd * get_pw_nam(char * name, struct hashtable* ht){ int h; struct mypasswd * hashentry; if (!ht || !name || *name == '\0') return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -