gsasl  2.2.1
plain/client.c
Go to the documentation of this file.
1 /* client.c --- SASL mechanism PLAIN as defined in RFC 2595, client side.
2  * Copyright (C) 2002-2024 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 #include <config.h>
24 
25 /* Get specification. */
26 #include "plain.h"
27 
28 /* Get memcpy, strdup, strlen. */
29 #include <string.h>
30 
31 /* Get malloc, free. */
32 #include <stdlib.h>
33 
34 int
36  void *mech_data _GL_UNUSED,
37  const char *input _GL_UNUSED,
38  size_t input_len _GL_UNUSED,
39  char **output, size_t *output_len)
40 {
41  const char *authzid = gsasl_property_get (sctx, GSASL_AUTHZID);
42  const char *authid = gsasl_property_get (sctx, GSASL_AUTHID);
43  const char *password = gsasl_property_get (sctx, GSASL_PASSWORD);
44  size_t authzidlen = 0, authidlen = 0, passwordlen = 0;
45  char *out;
46 
47  if (authzid)
48  authzidlen = strlen (authzid);
49 
50  if (authid)
51  authidlen = strlen (authid);
52  else
53  return GSASL_NO_AUTHID;
54 
55  if (password)
56  passwordlen = strlen (password);
57  else
58  return GSASL_NO_PASSWORD;
59 
60  *output_len = authzidlen + 1 + authidlen + 1 + passwordlen;
61  *output = out = malloc (*output_len);
62  if (!out)
63  return GSASL_MALLOC_ERROR;
64 
65  if (authzid)
66  {
67  memcpy (out, authzid, authzidlen);
68  out += authzidlen;
69  }
70 
71  *out++ = '\0';
72 
73  memcpy (out, authid, authidlen);
74  out += authidlen;
75 
76  *out++ = '\0';
77 
78  memcpy (out, password, passwordlen);
79 
80  return GSASL_OK;
81 }
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
@ GSASL_NO_PASSWORD
Definition: gsasl.h:146
@ GSASL_NO_AUTHID
Definition: gsasl.h:144
_GSASL_API const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:292
@ GSASL_AUTHZID
Definition: gsasl.h:225
@ GSASL_PASSWORD
Definition: gsasl.h:226
@ GSASL_AUTHID
Definition: gsasl.h:224
int _gsasl_plain_client_step(Gsasl_session *sctx, void *mech_data _GL_UNUSED, const char *input _GL_UNUSED, size_t input_len _GL_UNUSED, char **output, size_t *output_len)
Definition: plain/client.c:35