Branch data Line data Source code
1 : : /* server.c --- SASL mechanism SECURID from RFC 2808, server side.
2 : : * Copyright (C) 2002-2012 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 "securid.h"
29 : :
30 : : /* Get malloc, free. */
31 : : #include <stdlib.h>
32 : :
33 : : /* Get memchr, strdup, strlen. */
34 : : #include <string.h>
35 : :
36 : : #define PASSCODE "passcode"
37 : : #define PIN "pin"
38 : :
39 : : int
40 : 19 : _gsasl_securid_server_step (Gsasl_session * sctx,
41 : : void *mech_data,
42 : : const char *input, size_t input_len,
43 : : char **output, size_t * output_len)
44 : : {
45 : 19 : const char *authorization_id = NULL;
46 : 19 : const char *authentication_id = NULL;
47 : 19 : const char *passcode = NULL;
48 : : const char *suggestedpin;
49 : 19 : char *pin = NULL;
50 : : int res;
51 : : size_t len;
52 : :
53 [ + + ]: 19 : if (input_len == 0)
54 : : {
55 : 1 : *output_len = 0;
56 : 1 : *output = NULL;
57 : 1 : return GSASL_NEEDS_MORE;
58 : : }
59 : :
60 : 18 : authorization_id = input;
61 : 18 : authentication_id = memchr (input, '\0', input_len - 1);
62 [ + + ]: 18 : if (authentication_id)
63 : : {
64 : 14 : authentication_id++;
65 : 14 : passcode = memchr (authentication_id, '\0',
66 : 14 : input_len - strlen (authorization_id) - 1 - 1);
67 [ + - ]: 14 : if (passcode)
68 : : {
69 : 14 : passcode++;
70 : 28 : pin = memchr (passcode, '\0', input_len -
71 : 28 : strlen (authorization_id) - 1 -
72 : 14 : strlen (authentication_id) - 1 - 1);
73 [ + + ]: 14 : if (pin)
74 : : {
75 : 3 : pin++;
76 [ + - ][ - + ]: 3 : if (pin && !*pin)
77 : 0 : pin = NULL;
78 : : }
79 : : }
80 : : }
81 : :
82 [ + + ]: 18 : if (passcode == NULL)
83 : 4 : return GSASL_MECHANISM_PARSE_ERROR;
84 : :
85 : 14 : gsasl_property_set (sctx, GSASL_AUTHID, authentication_id);
86 : 14 : gsasl_property_set (sctx, GSASL_AUTHZID, authorization_id);
87 : 14 : gsasl_property_set (sctx, GSASL_PASSCODE, passcode);
88 [ + + ]: 14 : if (pin)
89 : 3 : gsasl_property_set (sctx, GSASL_PIN, pin);
90 : : else
91 : 11 : gsasl_property_set (sctx, GSASL_PIN, NULL);
92 : :
93 : 14 : res = gsasl_callback (NULL, sctx, GSASL_VALIDATE_SECURID);
94 [ + + + ]: 14 : switch (res)
95 : : {
96 : : case GSASL_SECURID_SERVER_NEED_ADDITIONAL_PASSCODE:
97 : 2 : *output = strdup (PASSCODE);
98 [ - + ]: 2 : if (!*output)
99 : 0 : return GSASL_MALLOC_ERROR;
100 : 2 : *output_len = strlen (PASSCODE);
101 : 2 : res = GSASL_NEEDS_MORE;
102 : 2 : break;
103 : :
104 : : case GSASL_SECURID_SERVER_NEED_NEW_PIN:
105 : 3 : suggestedpin = gsasl_property_get (sctx, GSASL_SUGGESTED_PIN);
106 [ + + ]: 3 : if (suggestedpin)
107 : 2 : len = strlen (suggestedpin);
108 : : else
109 : 1 : len = 0;
110 : 3 : *output_len = strlen (PIN) + len;
111 : 3 : *output = malloc (*output_len);
112 [ - + ]: 3 : if (!*output)
113 : 0 : return GSASL_MALLOC_ERROR;
114 : 3 : memcpy (*output, PIN, strlen (PIN));
115 [ + + ]: 3 : if (suggestedpin)
116 : 2 : memcpy (*output + strlen (PIN), suggestedpin, len);
117 : 3 : res = GSASL_NEEDS_MORE;
118 : 3 : break;
119 : :
120 : : default:
121 : 9 : *output_len = 0;
122 : 9 : *output = NULL;
123 : 9 : break;
124 : : }
125 : :
126 : 19 : return res;
127 : : }
|