gsasl  1.10.0
About: GNU SASL is an implementation of the Simple Authentication and Security Layer (SASL). Development version.
  Fossies Dox: gsasl-1.10.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

server.c
Go to the documentation of this file.
1 /* server.c --- Non-standard SASL mechanism LOGIN, server side.
2  * Copyright (C) 2002-2021 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GNU SASL Library; if not, write to the Free
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 /* Get malloc, free. */
28 #include <stdlib.h>
29 
30 /* Get strdup, strlen. */
31 #include <string.h>
32 
33 /* Get specification. */
34 #include "login.h"
35 
37 {
38  int step;
39  char *username;
40  char *password;
41 };
42 
43 #define CHALLENGE_USERNAME "User Name"
44 #define CHALLENGE_PASSWORD "Password"
45 
46 int
47 _gsasl_login_server_start (Gsasl_session * sctx _GL_UNUSED, void **mech_data)
48 {
49  struct _Gsasl_login_server_state *state;
50 
51  state = calloc (1, sizeof (*state));
52  if (state == NULL)
53  return GSASL_MALLOC_ERROR;
54 
55  *mech_data = state;
56 
57  return GSASL_OK;
58 }
59 
60 int
62  void *mech_data,
63  const char *input, size_t input_len,
64  char **output, size_t *output_len)
65 {
66  struct _Gsasl_login_server_state *state = mech_data;
67  int res;
68 
69  switch (state->step)
70  {
71  case 0:
72  *output = strdup (CHALLENGE_USERNAME);
73  if (!*output)
74  return GSASL_MALLOC_ERROR;
75  *output_len = strlen (CHALLENGE_USERNAME);
76 
77  state->step++;
79  break;
80 
81  case 1:
82  if (input_len == 0)
84 
85  state->username = strndup (input, input_len);
86  if (state->username == NULL)
87  return GSASL_MALLOC_ERROR;
88 
89  if (input_len != strlen (state->username))
91 
92  *output = strdup (CHALLENGE_PASSWORD);
93  if (!*output)
94  return GSASL_MALLOC_ERROR;
95  *output_len = strlen (CHALLENGE_PASSWORD);
96 
97  state->step++;
99  break;
100 
101  case 2:
102  if (input_len == 0)
104 
105  state->password = strndup (input, input_len);
106  if (state->password == NULL)
107  return GSASL_MALLOC_ERROR;
108 
109  if (input_len != strlen (state->password))
111 
112  gsasl_property_set (sctx, GSASL_AUTHID, state->username);
114 
116  if (res == GSASL_NO_CALLBACK)
117  {
118  const char *key;
119 
122 
123  key = gsasl_property_get (sctx, GSASL_PASSWORD);
124 
125  if (key && strlen (state->password) == strlen (key) &&
126  strcmp (state->password, key) == 0)
127  res = GSASL_OK;
128  else
130  }
131 
132  *output_len = 0;
133  *output = NULL;
134  state->step++;
135  break;
136 
137  default:
139  break;
140  }
141 
142  return res;
143 }
144 
145 void
146 _gsasl_login_server_finish (Gsasl_session * sctx _GL_UNUSED, void *mech_data)
147 {
148  struct _Gsasl_login_server_state *state = mech_data;
149 
150  if (!state)
151  return;
152 
153  free (state->username);
154  free (state->password);
155  free (state);
156 }
int gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
Definition: callback.c:75
#define NULL
Definition: stddef.in.h:72
const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:263
void gsasl_property_set(Gsasl_session *sctx, Gsasl_property prop, const char *data)
Definition: property.c:158
@ GSASL_NO_CALLBACK
Definition: gsasl.h:184
@ GSASL_OK
Definition: gsasl.h:171
@ GSASL_AUTHENTICATION_ERROR
Definition: gsasl.h:180
@ GSASL_NEEDS_MORE
Definition: gsasl.h:172
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:175
@ GSASL_MECHANISM_CALLED_TOO_MANY_TIMES
Definition: gsasl.h:174
@ GSASL_MECHANISM_PARSE_ERROR
Definition: gsasl.h:179
@ GSASL_AUTHZID
Definition: gsasl.h:336
@ GSASL_VALIDATE_SIMPLE
Definition: gsasl.h:363
@ GSASL_PASSWORD
Definition: gsasl.h:337
@ GSASL_AUTHID
Definition: gsasl.h:335
#define CHALLENGE_USERNAME
Definition: server.c:43
int _gsasl_login_server_step(Gsasl_session *sctx, void *mech_data, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: server.c:61
void _gsasl_login_server_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
Definition: server.c:146
#define CHALLENGE_PASSWORD
Definition: server.c:44
int _gsasl_login_server_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
Definition: server.c:47
int res
Definition: mbrtowc-impl.h:45
char * strdup(const char *s)
Definition: strdup.c:39
char * strndup(char const *s, size_t n)
Definition: strndup.c:26