GSS_S_PROMPTING_NEEDED is a bit
[cyrus-sasl.git] / saslauthd / auth_sasldb.c
1 /* MODULE: auth_sasldb */
2
3 /* COPYRIGHT
4  * Copyright (c) 1997-2000 Messaging Direct Ltd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY MESSAGING DIRECT LTD. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL MESSAGING DIRECT LTD. OR
20  * ITS EMPLOYEES OR AGENTS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27  * DAMAGE.
28  * END COPYRIGHT */
29
30 /* SYNOPSIS
31  * crypt(3) based passwd file validation
32  * END SYNOPSIS */
33
34 #ifdef __GNUC__
35 #ident "$Id: auth_sasldb.c,v 1.5 2002/07/27 18:44:46 rjs3 Exp $"
36 #endif
37
38 /* PUBLIC DEPENDENCIES */
39 #include "mechanisms.h"
40
41 #include <string.h>
42 #include <stdlib.h>
43 #include <pwd.h>
44 /* END PUBLIC DEPENDENCIES */
45
46 #define RETURN(x) return strdup(x)
47 \f
48
49 #ifdef AUTH_SASLDB
50 #include "../include/sasl.h"
51 #include "../include/saslplug.h"
52 #include "../sasldb/sasldb.h"
53
54 static int
55 vf(void *context __attribute__((unused)),
56    char *file  __attribute__((unused)),
57    int type  __attribute__((unused)))
58 {
59     /* always say ok */ 
60     return SASL_OK;
61 }
62
63 static int lame_getcallback(sasl_conn_t *conn __attribute__((unused)),
64                             unsigned long callbackid,
65                             int (**pproc)(),
66                             void **pcontext)
67 {
68     if(callbackid == SASL_CB_VERIFYFILE) {
69         *pproc = vf;
70         *pcontext = NULL;
71         return SASL_OK;
72     }
73         
74     return SASL_FAIL;
75 }
76
77 static void lame_log(sasl_conn_t *conn, int level, const char *fmt, ...) 
78 {
79     return;
80 }
81
82 static void lame_seterror(sasl_conn_t *conn, unsigned flags,
83                           const char *fmt, ...) 
84 {
85     return;
86 }
87
88 /* FUNCTION: init_lame_utils */
89 /* This sets up a very minimal sasl_utils_t for use only with the
90  * database functions */
91 static void init_lame_utils(sasl_utils_t *utils) 
92 {
93     memset(utils, 0, sizeof(sasl_utils_t));
94
95     utils->malloc=(sasl_malloc_t *)malloc;
96     utils->calloc=(sasl_calloc_t *)calloc;
97     utils->realloc=(sasl_realloc_t *)realloc;
98     utils->free=(sasl_free_t *)free;
99
100     utils->getcallback=lame_getcallback;
101     utils->log=lame_log;
102     utils->seterror=lame_seterror;
103
104     return;
105 }
106
107 /* END FUNCTION: init_lame_utils */
108         
109 #endif /* AUTH_SASLDB */
110
111 /* FUNCTION: auth_sasldb */
112
113 char *                                  /* R: allocated response string */
114 auth_sasldb (
115   /* PARAMETERS */
116 #ifdef AUTH_SASLDB
117   const char *login,                    /* I: plaintext authenticator */
118   const char *password,                 /* I: plaintext password */
119   const char *service __attribute__((unused)),
120   const char *realm
121 #else
122   const char *login __attribute__((unused)),/* I: plaintext authenticator */
123   const char *password __attribute__((unused)),  /* I: plaintext password */
124   const char *service __attribute__((unused)),
125   const char *realm __attribute__((unused))
126 #endif
127   /* END PARAMETERS */
128   )
129 {
130 #ifdef AUTH_SASLDB
131     /* VARIABLES */
132     char pw[1024];                      /* pointer to passwd file entry */
133     sasl_utils_t utils;
134     int ret, outsize;
135     const char *use_realm;
136     char realm_buf[MAXHOSTNAMELEN];
137     /* END VARIABLES */
138
139     init_lame_utils(&utils);
140
141     _sasl_check_db(&utils, (void *)0x1);
142
143     if(!realm || !strlen(realm)) {
144         ret = gethostname(realm_buf,MAXHOSTNAMELEN);
145         if(ret) RETURN("NO");
146         use_realm = realm_buf;
147     } else {
148         use_realm = realm;
149     }
150     
151
152     ret = _sasldb_getdata(&utils, (void *)0x1, login, use_realm,
153                           "userPassword", pw, 1024, &outsize);
154
155     if (ret != SASL_OK) {
156         RETURN("NO");
157     }
158
159     if (strcmp(pw, password)) {
160         RETURN("NO");
161     }
162
163     RETURN("OK");
164 #else
165     RETURN("NO");
166 #endif
167 }
168
169 /* END FUNCTION: auth_sasldb */
170
171 /* END MODULE: auth_sasldb */