gsasl  2.2.2
login/client.c
Go to the documentation of this file.
1 /* client.c --- Non-standard SASL mechanism LOGIN, client side.
2  * Copyright (C) 2002-2025 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, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 
24 /* Get malloc, free. */
25 #include <stdlib.h>
26 
27 /* Get strlen. */
28 #include <string.h>
29 
30 /* Get specification. */
31 #include "login.h"
32 
34 {
35  int step;
36 };
37 
38 int
39 _gsasl_login_client_start (Gsasl_session *sctx _GL_UNUSED, void **mech_data)
40 {
41  struct _Gsasl_login_client_state *state;
42 
43  state = malloc (sizeof (*state));
44  if (state == NULL)
45  return GSASL_MALLOC_ERROR;
46 
47  state->step = 0;
48 
49  *mech_data = state;
50 
51  return GSASL_OK;
52 }
53 
54 int
56  void *mech_data,
57  const char *input _GL_UNUSED,
58  size_t input_len _GL_UNUSED,
59  char **output, size_t *output_len)
60 {
61  struct _Gsasl_login_client_state *state = mech_data;
62  const char *p;
63  int res;
64 
65  switch (state->step)
66  {
67  case 0:
68  p = gsasl_property_get (sctx, GSASL_AUTHID);
69  if (!p)
70  return GSASL_NO_AUTHID;
71 
72  *output = strdup (p);
73  *output_len = strlen (p);
74 
75  state->step++;
76  res = GSASL_NEEDS_MORE;
77  break;
78 
79  case 1:
81  if (!p)
82  return GSASL_NO_PASSWORD;
83 
84  *output = strdup (p);
85  if (!*output)
86  return GSASL_MALLOC_ERROR;
87  *output_len = strlen (*output);
88 
89  state->step++;
90  res = GSASL_OK;
91  break;
92 
93  default:
95  break;
96  }
97 
98  return res;
99 }
100 
101 void
102 _gsasl_login_client_finish (Gsasl_session *sctx _GL_UNUSED, void *mech_data)
103 {
104  struct _Gsasl_login_client_state *state = mech_data;
105 
106  if (!state)
107  return;
108 
109  free (state);
110 }
@ GSASL_OK
Definition: gsasl.h:128
@ GSASL_NEEDS_MORE
Definition: gsasl.h:129
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:132
@ GSASL_NO_PASSWORD
Definition: gsasl.h:145
@ GSASL_MECHANISM_CALLED_TOO_MANY_TIMES
Definition: gsasl.h:131
@ GSASL_NO_AUTHID
Definition: gsasl.h:143
_GSASL_API const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:291
@ GSASL_PASSWORD
Definition: gsasl.h:225
@ GSASL_AUTHID
Definition: gsasl.h:223
void _gsasl_login_client_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
Definition: login/client.c:102
int _gsasl_login_client_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
Definition: login/client.c:39
int _gsasl_login_client_step(Gsasl_session *sctx _GL_UNUSED, void *mech_data, const char *input _GL_UNUSED, size_t input_len _GL_UNUSED, char **output, size_t *output_len)
Definition: login/client.c:55