gsasl  2.2.1
login/client.c
Go to the documentation of this file.
1 /* client.c --- Non-standard SASL mechanism LOGIN, 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 malloc, free. */
26 #include <stdlib.h>
27 
28 /* Get strlen. */
29 #include <string.h>
30 
31 /* Get specification. */
32 #include "login.h"
33 
35 {
36  int step;
37 };
38 
39 int
40 _gsasl_login_client_start (Gsasl_session *sctx _GL_UNUSED, void **mech_data)
41 {
42  struct _Gsasl_login_client_state *state;
43 
44  state = malloc (sizeof (*state));
45  if (state == NULL)
46  return GSASL_MALLOC_ERROR;
47 
48  state->step = 0;
49 
50  *mech_data = state;
51 
52  return GSASL_OK;
53 }
54 
55 int
57  void *mech_data,
58  const char *input _GL_UNUSED,
59  size_t input_len _GL_UNUSED,
60  char **output, size_t *output_len)
61 {
62  struct _Gsasl_login_client_state *state = mech_data;
63  const char *p;
64  int res;
65 
66  switch (state->step)
67  {
68  case 0:
69  p = gsasl_property_get (sctx, GSASL_AUTHID);
70  if (!p)
71  return GSASL_NO_AUTHID;
72 
73  *output = strdup (p);
74  *output_len = strlen (p);
75 
76  state->step++;
77  res = GSASL_NEEDS_MORE;
78  break;
79 
80  case 1:
82  if (!p)
83  return GSASL_NO_PASSWORD;
84 
85  *output = strdup (p);
86  if (!*output)
87  return GSASL_MALLOC_ERROR;
88  *output_len = strlen (*output);
89 
90  state->step++;
91  res = GSASL_OK;
92  break;
93 
94  default:
96  break;
97  }
98 
99  return res;
100 }
101 
102 void
103 _gsasl_login_client_finish (Gsasl_session *sctx _GL_UNUSED, void *mech_data)
104 {
105  struct _Gsasl_login_client_state *state = mech_data;
106 
107  if (!state)
108  return;
109 
110  free (state);
111 }
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_NEEDS_MORE
Definition: gsasl.h:130
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
@ GSASL_NO_PASSWORD
Definition: gsasl.h:146
@ GSASL_MECHANISM_CALLED_TOO_MANY_TIMES
Definition: gsasl.h:132
@ 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_PASSWORD
Definition: gsasl.h:226
@ GSASL_AUTHID
Definition: gsasl.h:224
void _gsasl_login_client_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
Definition: login/client.c:103
int _gsasl_login_client_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
Definition: login/client.c:40
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:56