Project

General

Profile

Feature #3240 ยป getent-avoid-libproject.patch

Patched getent - Igor Pashev, 2012-09-29 10:29 PM

View differences:

libc/illumos-gate/usr/src/cmd/getent/Makefile 2012-09-30 00:08:34.973424388 +0400
46 46

  
47 47
SRCS=	$(OBJECTS:.o=.c)
48 48

  
49
LDLIBS	+= -lsocket -lnsl -lproject
49
LDLIBS	+= -lsocket -lnsl
50 50

  
51 51
#
52 52
# for message catalog
libc/illumos-gate/usr/src/cmd/getent/dogetproject.c 2012-09-30 01:06:48.842722893 +0400
29 29
#include <stdio.h>
30 30
#include <grp.h>
31 31
#include <stdlib.h>
32
#include <project.h>
32
#include <nss_dbdefs.h>
33 33
#include "getent.h"
34 34

  
35
/* BEGIN: project.h */
36
#define PROJF_PATH  "/etc/project"  /* pathname of the "project" file */
37
#define PROJNAME_MAX    64      /* maximum project name size */
38
#define PROJECT_BUFSZ   4096        /* default buffer size */
39

  
40
#define SETPROJ_ERR_TASK    (-1)    /* error creating new task */
41
#define SETPROJ_ERR_POOL    (-2)    /* error binding to pool */
42

  
43
struct project {
44
    char    *pj_name;   /* name of the project */
45
    projid_t pj_projid; /* numerical project id */
46
    char    *pj_comment;    /* project description */
47
    char    **pj_users; /* vector of pointers to project user names */
48
    char    **pj_groups;    /* vector of pointers to project group names */
49
    char    *pj_attr;   /* project attributes string */
50
};
51
/* END: project.h */
52

  
53
/* BEGIN: usr/src/lib/libproject/common/getprojent.c */
54
static DEFINE_NSS_DB_ROOT(db_root);
55
static DEFINE_NSS_GETENT(context);
56

  
57
static void
58
_nss_initf_project(nss_db_params_t *p)
59
{
60
    p->name = NSS_DBNAM_PROJECT;
61
    p->default_config = NSS_DEFCONF_PROJECT;
62
}
63

  
64
static void
65
setprojent(void)
66
{
67
    nss_setent(&db_root, _nss_initf_project, &context);
68
}
69

  
70
static void
71
endprojent(void)
72
{
73
    nss_endent(&db_root, _nss_initf_project, &context);
74
    nss_delete(&db_root);
75
}
76

  
77
static char *
78
gettok(char **nextpp, char sep)
79
{
80
    char *p = *nextpp;
81
    char *q = p;
82
    char c;
83

  
84
    if (p == NULL)
85
        return (NULL);
86
    while ((c = *q) != '\0' && c != sep)
87
        q++;
88
    if (c == '\0')
89
        *nextpp = 0;
90
    else {
91
        *q++ = '\0';
92
        *nextpp = q;
93
    }
94
    return (p);
95
}
96

  
97
/*
98
 * Return values: 0 = success, 1 = parse error, 2 = erange ...
99
 * The structure pointer passed in is a structure in the caller's space
100
 * wherein the field pointers would be set to areas in the buffer if
101
 * need be. instring and buffer should be separate areas.
102
 */
103
static int
104
str2project(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
105
{
106
    struct project *project = ent;
107
    char *p, *next;
108
    char *users, *groups;
109
    char **uglist;
110
    char **limit;
111

  
112
    if (lenstr + 1 > buflen)
113
        return (NSS_STR_PARSE_ERANGE);
114
    /*
115
     * We copy the input string into the output buffer and
116
     * operate on it in place.
117
     */
118
    (void) memcpy(buffer, instr, lenstr);
119
    buffer[lenstr] = '\0';
120
    next = buffer;
121

  
122
    limit = (char **)ROUND_DOWN(buffer + buflen, sizeof (char *));
123

  
124
    /*
125
     * Parsers for passwd and group have always been pretty rigid;
126
     * we wouldn't want to buck a Unix tradition
127
     */
128
    p = gettok(&next, ':');
129
    if (p == NULL || *p == '\0' || strlen(p) > PROJNAME_MAX) {
130
        /*
131
         * empty or very long project names are not allowed
132
         */
133
        return (NSS_STR_PARSE_ERANGE);
134
    }
135
    project->pj_name = p;
136

  
137
    p = gettok(&next, ':');
138
    if (p == NULL || *p == '\0') {
139
        /*
140
         * projid field shouldn't be empty
141
         */
142
        return (NSS_STR_PARSE_PARSE);
143
    }
144
    project->pj_projid = (projid_t)strtol(p, NULL, 10);
145
    if (project->pj_projid < 0) {
146
        /*
147
         * projids should be positive number
148
         */
149
        project->pj_projid = 0;
150
        return (NSS_STR_PARSE_PARSE);
151
    }
152

  
153
    p = gettok(&next, ':');
154
    if (p == NULL) {
155
        /*
156
         * comment field can be empty but should not be last field
157
         */
158
        return (NSS_STR_PARSE_PARSE);
159
    }
160
    project->pj_comment = p;
161

  
162
    if ((users = gettok(&next, ':')) == NULL) {
163
        /*
164
         * users field should not be last field
165
         */
166
        return (NSS_STR_PARSE_PARSE);
167
    }
168

  
169
    if ((groups = gettok(&next, ':')) == NULL) {
170
        /*
171
         * groups field should not be last field
172
         */
173
        return (NSS_STR_PARSE_PARSE);
174
    }
175

  
176
    if (next == NULL) {
177
        /*
178
         * attributes field should be last
179
         */
180
        return (NSS_STR_PARSE_PARSE);
181
    }
182

  
183
    project->pj_attr = next;
184

  
185
    uglist = (char **)ROUND_UP(buffer + lenstr + 1, sizeof (char *));
186
    *uglist = NULL;
187
    project->pj_users = uglist;
188
    while (uglist < limit) {
189
        p = gettok(&users, ',');
190
        if (p == NULL || *p == '\0') {
191
            *uglist = 0;
192
            break;
193
        }
194
        *uglist++ = p;
195
    }
196
    if (uglist >= limit)
197
        return (NSS_STR_PARSE_ERANGE);
198

  
199
    uglist++;
200
    *uglist = NULL;
201
    project->pj_groups = uglist;
202
    while (uglist < limit) {
203
        p = gettok(&groups, ',');
204
        if (p == NULL || *p == '\0') {
205
            *uglist = 0;
206
            break;
207
        }
208
        *uglist++ = p;
209
    }
210
    if (uglist >= limit)
211
        return (NSS_STR_PARSE_ERANGE);
212

  
213
    return (NSS_STR_PARSE_SUCCESS);
214
}
215

  
216
static struct project *
217
getprojent(struct project *result, void *buffer, size_t buflen)
218
{
219
    nss_XbyY_args_t arg;
220

  
221
    NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
222
    (void) nss_getent(&db_root, _nss_initf_project, &context, &arg);
223
    return ((struct project *)NSS_XbyY_FINI(&arg));
224
}
225

  
226
struct project *
227
getprojbyname(const char *name, struct project *result,
228
    void *buffer, size_t buflen)
229
{
230
    nss_XbyY_args_t arg;
231
    NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
232
    arg.key.name = name;
233
    (void) nss_search(&db_root, _nss_initf_project,
234
        NSS_DBOP_PROJECT_BYNAME, &arg);
235
    return ((struct project *)NSS_XbyY_FINI(&arg));
236
}
237

  
238
struct project *
239
getprojbyid(projid_t projid, struct project *result,
240
    void *buffer, size_t buflen)
241
{
242
    nss_XbyY_args_t arg;
243

  
244
    NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
245
    arg.key.projid = projid;
246
    (void) nss_search(&db_root, _nss_initf_project,
247
        NSS_DBOP_PROJECT_BYID, &arg);
248
    return ((struct project *)NSS_XbyY_FINI(&arg));
249
}
250

  
251

  
252

  
253
/* END: usr/src/lib/libproject/common/getprojent.c */
254

  
35 255
static int
36 256
putprojent(const struct project *proj, FILE *fp)
37 257
{
    (1-1/1)