Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / modules / rlm_passwd / rlm_passwd.c
index 8aa4fbd..632f8d7 100644 (file)
  *
  *   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
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * Copyright 2000  The FreeRADIUS server project
+ * Copyright 2000,2006  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"
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
 
+#include <freeradius-devel/radiusd.h>
+#include <freeradius-devel/modules.h>
 
 struct mypasswd {
        struct mypasswd *next;
        char *listflag;
-       char *field[0];
+       char *field[1];
 };
 
 struct hashtable {
@@ -44,7 +41,7 @@ struct hashtable {
        struct mypasswd *last_found;
        char buffer[1024];
        FILE *fp;
-       char *delimiter;
+       char delimiter;
 };
 
 
@@ -77,10 +74,11 @@ static struct mypasswd * mypasswd_malloc(const char* buffer, int nfields, int* l
 }
 
 static int string_to_entry(const char* string, int nfields, char delimiter,
-       struct mypasswd *passwd, int bufferlen)
+       struct mypasswd *passwd, size_t bufferlen)
 {
        char *str;
-       int len, i, fn=0;
+       size_t len, i;
+       int fn=0;
        char *data_beg;
 
 
@@ -90,7 +88,8 @@ static int string_to_entry(const char* string, int nfields, char delimiter,
        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;
+       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*);
@@ -132,12 +131,19 @@ static unsigned int hash(const unsigned char * username, unsigned int tablesize)
 static void release_hash_table(struct hashtable * ht){
        int i;
 
-       if (!ht) return;
-       for (i=0; i<ht->tablesize; i++)
+       if (ht == NULL) 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);
+       if (ht->table) {
+               free(ht->table);
+               ht->table = NULL;
+       }
+       if (ht->fp) {
+               fclose(ht->fp);
+               ht->fp = NULL;
+       }
+       ht->tablesize = 0;
 }
 
 static void release_ht(struct hashtable * ht){
@@ -148,12 +154,12 @@ static void release_ht(struct hashtable * ht){
 }
 
 static struct hashtable * build_hash_table (const char * file, int nfields,
-       int keyfield, int islist, int tablesize, int ignorenis, char delimiter)
+       int keyfield, int islist, int tablesize, int ignorenis, char delimiter)
 {
 #define passwd ((struct mypasswd *) ht->buffer)
        char buffer[1024];
        struct hashtable* ht;
-       int len;
+       size_t len;
        unsigned int h;
        struct mypasswd *hashentry, *hashentry1;
        char *list;
@@ -175,8 +181,8 @@ static struct hashtable * build_hash_table (const char * file, int nfields,
        ht->keyfield = keyfield;
        ht->islist = islist;
        ht->ignorenis = ignorenis;
-       if (delimiter && *delimiter) ht->delimiter = delimiter;
-       else ht->delimiter = ":";
+       if (delimiter) ht->delimiter = delimiter;
+       else ht->delimiter = ':';
        if(!tablesize) return ht;
        if(!(ht->fp = fopen(file,"r"))) return NULL;
        memset(ht->buffer, 0, 1024);
@@ -194,10 +200,9 @@ static struct hashtable * build_hash_table (const char * file, int nfields,
                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);
+                       len = string_to_entry(buffer, nfields, ht->delimiter, hashentry, len);
                        if(!hashentry->field[keyfield] || *hashentry->field[keyfield] == '\0') {
                                free(hashentry);
                                continue;
@@ -219,7 +224,6 @@ static struct hashtable * build_hash_table (const char * file, int nfields,
                                        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];
@@ -257,10 +261,10 @@ static struct mypasswd * get_next(char *name, struct hashtable *ht)
                }
                return NULL;
        }
-       printf("try to find in file\n");
+       /*      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)) &&
+               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))
@@ -340,7 +344,6 @@ struct passwd_instance {
        struct mypasswd *pwdfmt;
        char *filename;
        char *format;
-       char *authtype;
        char * delimiter;
        int allowmultiple;
        int ignorenislike;
@@ -353,13 +356,11 @@ struct passwd_instance {
        int ignoreempty;
 };
 
-static CONF_PARSER module_config[] = {
-       { "filename",   PW_TYPE_STRING_PTR,
+static const CONF_PARSER module_config[] = {
+       { "filename",   PW_TYPE_FILENAME,
           offsetof(struct passwd_instance, filename), NULL,  NULL },
        { "format",   PW_TYPE_STRING_PTR,
           offsetof(struct passwd_instance, format), NULL,  NULL },
-       { "authtype",   PW_TYPE_STRING_PTR,
-          offsetof(struct passwd_instance, authtype), NULL,  NULL },
        { "delimiter",   PW_TYPE_STRING_PTR,
           offsetof(struct passwd_instance, delimiter), NULL,  ":" },
        { "ignorenislike",   PW_TYPE_BOOLEAN,
@@ -431,7 +432,7 @@ static int passwd_instantiate(CONF_SECTION *conf, void **instance)
                radlog(L_ERR, "rlm_passwd: no field market as key in format: %s", inst->format);
                return -1;
        }
-       if (! (inst->ht = build_hash_table (inst->filename, nfields, keyfield, listable, inst->hashsize, inst->ignorenislike, inst->delimiter)) ){
+       if (! (inst->ht = build_hash_table (inst->filename, nfields, keyfield, listable, inst->hashsize, inst->ignorenislike, *inst->delimiter)) ){
                radlog(L_ERR, "rlm_passwd: can't build hashtable from passwd file");
                return -1;
        }
@@ -470,7 +471,7 @@ static int passwd_instantiate(CONF_SECTION *conf, void **instance)
        inst->nfields = nfields;
        inst->keyfield = keyfield;
        inst->listable = listable;
-       radlog(L_INFO, "rlm_passwd: nfields: %d keyfield %d(%s) listable: %s", nfields, keyfield, inst->pwdfmt->field[keyfield], listable?"yes":"no");
+       DEBUG("rlm_passwd: nfields: %d keyfield %d(%s) listable: %s", nfields, keyfield, inst->pwdfmt->field[keyfield], listable?"yes":"no");
        return 0;
 
 #undef inst
@@ -479,10 +480,6 @@ static int passwd_instantiate(CONF_SECTION *conf, void **instance)
 static int passwd_detach (void *instance) {
 #define inst ((struct passwd_instance *)instance)
        if(inst->ht) release_ht(inst->ht);
-        if (inst->filename != NULL)    free(inst->filename);
-        if (inst->format != NULL)      free(inst->format);
-        if (inst->authtype != NULL )   free(inst->authtype);
-        if (inst->delimiter != NULL)   free(inst->delimiter);
        free(instance);
        return 0;
 #undef inst
@@ -528,7 +525,7 @@ static int passwd_authorize(void *instance, REQUEST *request)
                                name = buffer;
                                break;
                        default:
-                               name = key->strvalue;
+                               name = key->vp_strvalue;
                }
                if (! (pw = get_pw_nam(name, inst->ht)) ) {
                        continue;
@@ -544,20 +541,17 @@ static int passwd_authorize(void *instance, REQUEST *request)
        if(!found) {
                return RLM_MODULE_NOTFOUND;
        }
-       if (inst->authtype && (key = pairmake ("Auth-Type", inst->authtype, T_OP_EQ))) {
-               radlog(L_INFO, "rlm_passwd: Adding Auth-Type: %s", inst->authtype);
-               pairadd (&request->config_items, key);
-       }
        return RLM_MODULE_OK;
 
 #undef inst
 }
 
 module_t rlm_passwd = {
+       RLM_MODULE_INIT,
        "passwd",
        RLM_TYPE_THREAD_SAFE,           /* type */
-       NULL,                           /* initialize */
        passwd_instantiate,             /* instantiation */
+       passwd_detach,                  /* detach */
        {
                NULL,                   /* authentication */
                passwd_authorize,       /* authorization */
@@ -568,7 +562,5 @@ module_t rlm_passwd = {
                NULL,                   /* post-proxy */
                NULL                    /* post-auth */
        },
-       passwd_detach,                  /* detach */
-       NULL                            /* destroy */
 };
 #endif /* TEST */