summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-otpd/queue.c
blob: 730bbc40b864b9dc65a69049c0a0e516e3daac0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * FreeIPA 2FA companion daemon
 *
 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
 *
 * Copyright (C) 2013  Nathaniel McCallum, Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This file contains an implementation of a queue of request/response items.
 */

#include "internal.h"

struct otpd_queue_iter {
    struct otpd_queue_item *next;
    unsigned int qindx;
    const struct otpd_queue * const *queues;
};

krb5_error_code otpd_queue_item_new(krad_packet *req,
                                    struct otpd_queue_item **item)
{
    *item = calloc(1, sizeof(struct otpd_queue_item));
    if (*item == NULL)
        return ENOMEM;

    (*item)->req = req;
    (*item)->msgid = -1;
    return 0;
}

void otpd_queue_item_free(struct otpd_queue_item *item)
{
    if (item == NULL)
        return;

    ldap_memfree(item->user.dn);
    free(item->user.uid);
    free(item->user.ipatokenRadiusUserName);
    free(item->user.ipatokenRadiusConfigLink);
    free(item->user.other);
    free(item->radius.ipatokenRadiusServer);
    free(item->radius.ipatokenRadiusSecret);
    free(item->radius.ipatokenUserMapAttribute);
    free(item->error);
    krad_packet_free(item->req);
    krad_packet_free(item->rsp);
    free(item);
}

krb5_error_code otpd_queue_iter_new(const struct otpd_queue * const *queues,
                                    struct otpd_queue_iter **iter)
{
    *iter = calloc(1, sizeof(struct otpd_queue_iter));
    if (*iter == NULL)
        return ENOMEM;

    (*iter)->queues = queues;
    return 0;
}

/* This iterator function is used by krad to loop over all outstanding requests
 * to check for duplicates. Hence, we have to iterate over all the queues to
 * return all the outstanding requests as a flat list. */
const krad_packet *otpd_queue_iter_func(void *data, krb5_boolean cancel)
{
    struct otpd_queue_iter *iter = data;
    const struct otpd_queue *q;

    if (cancel) {
        free(iter);
        return NULL;
    }

    if (iter->next != NULL) {
        struct otpd_queue_item *tmp;
        tmp = iter->next;
        iter->next = tmp->next;
        return tmp->req;
    }

    q = iter->queues[iter->qindx++];
    if (q == NULL)
        return otpd_queue_iter_func(data, TRUE);

    iter->next = q->head;
    return otpd_queue_iter_func(data, FALSE);
}

void otpd_queue_push(struct otpd_queue *q, struct otpd_queue_item *item)
{
    if (item == NULL)
        return;

    if (q->tail == NULL)
        q->head = q->tail = item;
    else
        q->tail = q->tail->next = item;
}

void otpd_queue_push_head(struct otpd_queue *q, struct otpd_queue_item *item)
{
    if (item == NULL)
        return;

    if (q->head == NULL)
        q->tail = q->head = item;
    else {
        item->next = q->head;
        q->head = item;
    }
}

struct otpd_queue_item *otpd_queue_peek(struct otpd_queue *q)
{
    return q->head;
}

struct otpd_queue_item *otpd_queue_pop(struct otpd_queue *q)
{
    struct otpd_queue_item *item;

    if (q == NULL)
        return NULL;

    item = q->head;
    if (item != NULL)
        q->head = item->next;

    if (q->head == NULL)
        q->tail = NULL;

    return item;
}

/* Remove and return an item from the queue with the given msgid. */
struct otpd_queue_item *otpd_queue_pop_msgid(struct otpd_queue *q, int msgid)
{
    struct otpd_queue_item *item, **prev;

    for (item = q->head, prev = &q->head;
         item != NULL;
         item = item->next, prev = &item->next) {
        if (item->msgid == msgid) {
            *prev = item->next;
            if (q->head == NULL)
                q->tail = NULL;
            return item;
        }
    }

    return NULL;
}

void otpd_queue_free_items(struct otpd_queue *q)
{
    struct otpd_queue_item *item, *next;

    next = q->head;
    while (next != NULL) {
        item = next;
        next = next->next;
        otpd_queue_item_free(item);
    }

    q->head = NULL;
    q->tail = NULL;
}