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 --- SASL mechanism PLAIN as defined in RFC 2595, 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 specification. */
28 #include "plain.h"
29 
30 /* Get memcpy, memchr, strlen. */
31 #include <string.h>
32 
33 /* Get malloc, free. */
34 #include <stdlib.h>
35 
36 int
38  void *mech_data _GL_UNUSED,
39  const char *input, size_t input_len,
40  char **output, size_t *output_len)
41 {
42  const char *authzidptr = input;
43  char *authidptr = NULL;
44  char *passwordptr = NULL;
45  char *passwdz = NULL, *passprep = NULL, *authidprep = NULL;
46  int res;
47 
48  *output_len = 0;
49  *output = NULL;
50 
51  if (input_len == 0)
52  return GSASL_NEEDS_MORE;
53 
54  /* Parse input. */
55  {
56  size_t tmplen;
57 
58  authidptr = memchr (input, 0, input_len - 1);
59  if (authidptr)
60  {
61  authidptr++;
62  passwordptr = memchr (authidptr, 0, input_len - strlen (input) - 1);
63  if (passwordptr)
64  passwordptr++;
65  else
67  }
68  else
70 
71  /* As the NUL (U+0000) character is used as a deliminator, the NUL
72  (U+0000) character MUST NOT appear in authzid, authcid, or passwd
73  productions. */
74  tmplen = input_len - (size_t) (passwordptr - input);
75  if (memchr (passwordptr, 0, tmplen))
77  }
78 
79  /* Store authid, after preparing it... */
80  {
82  &authidprep, NULL);
83  if (res != GSASL_OK)
84  return res;
85 
86  gsasl_property_set (sctx, GSASL_AUTHID, authidprep);
87 
88  /* Store authzid, if absent, use SASLprep(authcid). */
89  if (*authzidptr == '\0')
90  gsasl_property_set (sctx, GSASL_AUTHZID, authidprep);
91  else
92  gsasl_property_set (sctx, GSASL_AUTHZID, authzidptr);
93 
94  free (authidprep);
95  }
96 
97  /* Store passwd, after preparing it... */
98  {
99  size_t passwdzlen = input_len - (size_t) (passwordptr - input);
100 
101  /* Need to zero terminate password... */
102  passwdz = malloc (passwdzlen + 1);
103  if (passwdz == NULL)
104  return GSASL_MALLOC_ERROR;
105  memcpy (passwdz, passwordptr, passwdzlen);
106  passwdz[passwdzlen] = '\0';
107 
108  res = gsasl_saslprep (passwdz, GSASL_ALLOW_UNASSIGNED, &passprep, NULL);
109  free (passwdz);
110  if (res != GSASL_OK)
111  return res;
112 
113  gsasl_property_set (sctx, GSASL_PASSWORD, passprep);
114  }
115 
116  /* Authorization. Let application verify credentials internally,
117  but fall back to deal with it locally... */
119  if (res == GSASL_NO_CALLBACK)
120  {
121  const char *key;
122  char *normkey;
123 
125  key = gsasl_property_get (sctx, GSASL_PASSWORD);
126  if (!key)
127  {
128  free (passprep);
129  return GSASL_NO_PASSWORD;
130  }
131 
132  /* Unassigned code points are not permitted. */
133  res = gsasl_saslprep (key, 0, &normkey, NULL);
134  if (res != GSASL_OK)
135  {
136  free (passprep);
137  return res;
138  }
139 
140  if (strcmp (normkey, passprep) == 0)
141  res = GSASL_OK;
142  else
144  free (normkey);
145  }
146  free (passprep);
147 
148  return res;
149 }
int gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
Definition: callback.c:75
void * memchr(void const *s, int c_in, size_t n)
Definition: memchr.c:59
#define NULL
Definition: stddef.in.h:72
const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:263
@ GSASL_ALLOW_UNASSIGNED
Definition: gsasl.h:272
void gsasl_property_set(Gsasl_session *sctx, Gsasl_property prop, const char *data)
Definition: property.c:158
int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags, char **out, int *stringpreprc)
@ 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_NO_PASSWORD
Definition: gsasl.h:188
@ 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
return() size_t(-2)
int res
Definition: mbrtowc-impl.h:45
int _gsasl_plain_server_step(Gsasl_session *sctx, void *mech_data _GL_UNUSED, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: server.c:37