summaryrefslogtreecommitdiffstats
path: root/server/nss/nsssrv_cmd.c
blob: a140048782f86dd6e0756a6045ee63477bbc1e43 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
   SSSD

   NSS Responder

   Copyright (C) Simo Sorce <ssorce@redhat.com>	2008

   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/>.
*/

#include "ldb.h"
#include "ldb_errors.h"
#include "util/util.h"
#include "nss/nsssrv.h"
#include "nss/nsssrv_ldb.h"

struct nss_cmd_ctx {
    struct cli_ctx *cctx;
};

struct nss_cmd_table {
    enum sss_nss_command cmd;
    int (*fn)(struct cli_ctx *cctx);
};

static int nss_cmd_get_version(struct cli_ctx *cctx)
{
    uint8_t *body;
    size_t blen;
    int ret;

    /* create response packet */
    ret = nss_packet_new(cctx->creq, sizeof(uint32_t),
                         nss_get_cmd(cctx->creq->in),
                         &cctx->creq->out);
    if (ret != RES_SUCCESS) {
        return ret;
    }
    nss_get_body(cctx->creq->out, &body, &blen);
    ((uint32_t *)body)[0] = SSS_NSS_VERSION;

    /* now that the packet is in place, unlock queue
     * making the event writable */
    EVENT_FD_WRITEABLE(cctx->cfde);

    return RES_SUCCESS;
}

static int nss_cmd_getpwnam_callback(void *ptr, int status,
                                     struct ldb_result *res)
{
    struct nss_cmd_ctx *nctx = talloc_get_type(ptr, struct nss_cmd_ctx);
    struct cli_ctx *cctx = nctx->cctx;
    struct ldb_message *msg;
    uint8_t *body;
    const char *name;
    const char *fullname;
    const char *homedir;
    const char *shell;
    uint64_t uid;
    uint64_t gid;
    size_t rsize, rp, rl, blen;
    int ret;

    if (res->count != 1) {
        if (res->count > 1) {
            DEBUG(1, ("getpwnam call returned more than oine result !?!\n"));
        }
        if (res->count == 0) {
            DEBUG(2, ("No results for getpwnam call"));
        }
        ret = nss_packet_new(cctx->creq, 2*sizeof(uint32_t),
                             nss_get_cmd(cctx->creq->in),
                             &cctx->creq->out);
        if (ret != RES_SUCCESS) {
            return ret;
        }
        nss_get_body(cctx->creq->out, &body, &blen);
        ((uint32_t *)body)[0] = 0; /* 0 results */
        ((uint32_t *)body)[1] = 0; /* reserved */
        goto done;
    }

    msg = res->msgs[0];

    name = ldb_msg_find_attr_as_string(msg, NSS_PW_NAME, NULL);
    fullname = ldb_msg_find_attr_as_string(msg, NSS_PW_FULLNAME, NULL);
    homedir = ldb_msg_find_attr_as_string(msg, NSS_PW_HOMEDIR, NULL);
    shell = ldb_msg_find_attr_as_string(msg, NSS_PW_SHELL, NULL);
    uid = ldb_msg_find_attr_as_uint64(msg, NSS_PW_UIDNUM, 0);
    gid = ldb_msg_find_attr_as_uint64(msg, NSS_PW_UIDNUM, 0);

    if (!name || !fullname || !homedir || !shell || !uid || !gid) {
        DEBUG(1, ("Incomplede user object?!? Aborting\n"));

        ret = nss_packet_new(cctx->creq, 2*sizeof(uint32_t),
                             nss_get_cmd(cctx->creq->in),
                             &cctx->creq->out);
        if (ret != RES_SUCCESS) {
            return ret;
        }
        nss_get_body(cctx->creq->out, &body, &blen);
        ((uint32_t *)body)[0] = 0; /* 0 results */
        ((uint32_t *)body)[1] = 0; /* reserved */
        goto done;
    }

    rsize = 2*sizeof(uint32_t);
    rsize += 2*sizeof(uint64_t);
    rsize += strlen(name) + 1;
    rsize += 2; /* password always set to 'x' */
    rsize += strlen(fullname) + 1;
    rsize += strlen(homedir) + 1;
    rsize += strlen(shell) + 1;

    /* create response packet */
    ret = nss_packet_new(cctx->creq, rsize,
                         nss_get_cmd(cctx->creq->in),
                         &cctx->creq->out);
    if (ret != RES_SUCCESS) {
        return ret;
    }
    nss_get_body(cctx->creq->out, &body, &blen);

    ((uint32_t *)body)[0] = 1; /* 1 result */
    ((uint32_t *)body)[1] = 0; /* reserved */
    ((uint64_t *)body)[1] = uid; /* first result uid */
    ((uint64_t *)body)[2] = gid; /* first result gid */

    rp = 2*sizeof(uint32_t) + 2*sizeof(uint64_t);
    rl = strlen(name)+1;
    memcpy(&body[rp], name, rl);
    rp += rl;
    rl = 2;
    memcpy(&body[rp], "x", rl);
    rp += rl;
    rl = strlen(fullname)+1;
    memcpy(&body[rp], fullname, rl);
    rp += rl;
    rl = strlen(homedir)+1;
    memcpy(&body[rp], homedir, rl);
    rp += rl;
    rl = strlen(shell)+1;
    memcpy(&body[rp], shell, rl);

done:
    /* now that the packet is in place, unlock queue
     * making the event writable */
    EVENT_FD_WRITEABLE(cctx->cfde);

    /* free all request related data through the talloc hierarchy */
    talloc_free(nctx);

    return RES_SUCCESS;
}

static int nss_cmd_getpwnam(struct cli_ctx *cctx)
{
    struct nss_cmd_ctx *nctx;
    uint8_t *body;
    size_t blen;
    int ret;
    const char *name;

    /* get user name to query */
    nss_get_body(cctx->creq->in, &body, &blen);
    name = (const char *)body;
    /* if not terminated fail */
    if (name[blen -1] != '\0') {
        return RES_INVALID_DATA;
    }

    nctx = talloc(cctx, struct nss_cmd_ctx);
    if (!nctx) {
        return RES_NOMEM;
    }
    nctx->cctx = cctx;

    ret = nss_ldb_getpwnam(nctx, cctx->ev, cctx->ldb, name,
                           nss_cmd_getpwnam_callback, nctx);

    return ret;
}

struct nss_cmd_table nss_cmds[] = {
    {SSS_NSS_GET_VERSION, nss_cmd_get_version},
    {SSS_NSS_GETPWNAM, nss_cmd_getpwnam},
    {SSS_NSS_NULL, NULL}
};

int nss_cmd_execute(struct cli_ctx *cctx)
{
    enum sss_nss_command cmd;
    int i;

    cmd = nss_get_cmd(cctx->creq->in);

    for (i = 0; nss_cmds[i].cmd != SSS_NSS_NULL; i++) {
        if (cmd == nss_cmds[i].cmd) {
            return nss_cmds[i].fn(cctx);
        }
    }

    return RES_INVALID_DATA;
}