gsasl  2.2.1
securid/server.c
Go to the documentation of this file.
1 /* server.c --- SASL mechanism SECURID from RFC 2808, server 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 "securid.h"
27 
28 /* Get malloc, free. */
29 #include <stdlib.h>
30 
31 /* Get memchr, strdup, strlen. */
32 #include <string.h>
33 
34 #define PASSCODE "passcode"
35 #define PIN "pin"
36 
37 int
39  void *mech_data _GL_UNUSED,
40  const char *input, size_t input_len,
41  char **output, size_t *output_len)
42 {
43  const char *authorization_id = NULL;
44  const char *authentication_id = NULL;
45  const char *passcode = NULL;
46  const char *suggestedpin;
47  char *pin = NULL;
48  int res;
49  size_t len;
50 
51  if (input_len == 0)
52  {
53  *output_len = 0;
54  *output = NULL;
55  return GSASL_NEEDS_MORE;
56  }
57 
58  authorization_id = input;
59  authentication_id = memchr (input, '\0', input_len - 1);
60  if (authentication_id)
61  {
62  authentication_id++;
63  passcode = memchr (authentication_id, '\0',
64  input_len - strlen (authorization_id) - 1 - 1);
65  if (passcode)
66  {
67  passcode++;
68  pin = memchr (passcode, '\0', input_len -
69  strlen (authorization_id) - 1 -
70  strlen (authentication_id) - 1 - 1);
71  if (pin)
72  {
73  pin++;
74  if (pin && !*pin)
75  pin = NULL;
76  }
77  }
78  }
79 
80  if (passcode == NULL)
82 
83  res = gsasl_property_set (sctx, GSASL_AUTHID, authentication_id);
84  if (res != GSASL_OK)
85  return res;
86 
87  res = gsasl_property_set (sctx, GSASL_AUTHZID, authorization_id);
88  if (res != GSASL_OK)
89  return res;
90  res = gsasl_property_set (sctx, GSASL_PASSCODE, passcode);
91  if (res != GSASL_OK)
92  return res;
93 
94  if (pin)
95  res = gsasl_property_set (sctx, GSASL_PIN, pin);
96  else
97  res = gsasl_property_set (sctx, GSASL_PIN, NULL);
98  if (res != GSASL_OK)
99  return res;
100 
101  res = gsasl_callback (NULL, sctx, GSASL_VALIDATE_SECURID);
102  switch (res)
103  {
105  *output = strdup (PASSCODE);
106  if (!*output)
107  return GSASL_MALLOC_ERROR;
108  *output_len = strlen (PASSCODE);
109  res = GSASL_NEEDS_MORE;
110  break;
111 
113  suggestedpin = gsasl_property_get (sctx, GSASL_SUGGESTED_PIN);
114  if (suggestedpin)
115  len = strlen (suggestedpin);
116  else
117  len = 0;
118  *output_len = strlen (PIN) + len;
119  *output = malloc (*output_len);
120  if (!*output)
121  return GSASL_MALLOC_ERROR;
122  memcpy (*output, PIN, strlen (PIN));
123  if (suggestedpin)
124  memcpy (*output + strlen (PIN), suggestedpin, len);
125  res = GSASL_NEEDS_MORE;
126  break;
127 
128  default:
129  *output_len = 0;
130  *output = NULL;
131  break;
132  }
133 
134  return res;
135 }
int gsasl_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
Definition: callback.c:71
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_SECURID_SERVER_NEED_ADDITIONAL_PASSCODE
Definition: gsasl.h:166
@ GSASL_SECURID_SERVER_NEED_NEW_PIN
Definition: gsasl.h:167
@ GSASL_NEEDS_MORE
Definition: gsasl.h:130
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
@ GSASL_MECHANISM_PARSE_ERROR
Definition: gsasl.h:137
_GSASL_API int gsasl_property_set(Gsasl_session *sctx, Gsasl_property prop, const char *data)
Definition: property.c:189
_GSASL_API const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:292
@ GSASL_AUTHZID
Definition: gsasl.h:225
@ GSASL_PASSCODE
Definition: gsasl.h:231
@ GSASL_AUTHID
Definition: gsasl.h:224
@ GSASL_PIN
Definition: gsasl.h:233
@ GSASL_VALIDATE_SECURID
Definition: gsasl.h:257
@ GSASL_SUGGESTED_PIN
Definition: gsasl.h:232
int _gsasl_securid_server_step(Gsasl_session *sctx, void *mech_data _GL_UNUSED, const char *input, size_t input_len, char **output, size_t *output_len)
#define PASSCODE
#define PIN