2 Unix SMB/CIFS implementation.
4 AIX loadable authentication mmodule, providing identification
5 routines against Samba winbind/Windows NT Domain
7 Copyright (C) Tim Potter 2003
8 Copyright (C) Steve Roylance 2003
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this library; if not, write to the
22 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
31 #include "winbind_client.h"
33 #define MAX_GETPWENT_USERS 250
34 #define MAX_GETGRENT_USERS 250
35 /* #define WB_AIX_DEBUG */
37 static struct passwd *fill_pwent(struct winbindd_pw *pw)
39 struct passwd *result;
41 if (!(result = malloc(sizeof(struct passwd))))
48 if ((result->pw_name = malloc(strlen(pw->pw_name) + 1)) == NULL)
51 strcpy(result->pw_name, pw->pw_name);
55 if ((result->pw_passwd = malloc(strlen(pw->pw_passwd) + 1)) == NULL)
58 strcpy(result->pw_passwd, pw->pw_passwd);
62 result->pw_uid = pw->pw_uid;
63 result->pw_gid = pw->pw_gid;
67 if ((result->pw_gecos = malloc(strlen(pw->pw_gecos) + 1)) == NULL)
70 strcpy(result->pw_gecos, pw->pw_gecos);
74 if ((result->pw_dir = malloc(strlen(pw->pw_dir) + 1)) == NULL)
77 strcpy(result->pw_dir, pw->pw_dir);
81 if ((result->pw_shell = malloc(strlen(pw->pw_shell) + 1)) == NULL)
84 strcpy(result->pw_shell, pw->pw_shell);
86 printf("wb_aix - returning filled pwent %s, %d\n", result->pw_name, result->pw_uid);
90 /* A memory allocation failed, undo succesfull allocations and
94 SAFE_FREE(result->pw_dir);
95 SAFE_FREE(result->pw_gecos);
96 SAFE_FREE(result->pw_passwd);
97 SAFE_FREE(result->pw_name);
103 static BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
109 if (!ptr) return(False);
113 /* default to simple separators */
114 if (!sep) sep = " \t\n\r";
116 /* find the first non sep char */
117 while (*s && strchr(sep,*s)) s++;
120 if (! *s) return(False);
122 /* copy over the token */
123 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
132 *ptr = (*s) ? s+1 : s;
138 static struct group *fill_grent(struct winbindd_gr *gr, char *gr_mem)
143 struct group *result;
145 if (!(result = malloc(sizeof(struct group))))
148 ZERO_STRUCTP(result);
152 if ((result->gr_name = malloc(strlen(gr->gr_name) + 1)) == NULL)
155 strcpy(result->gr_name, gr->gr_name);
159 if ((result->gr_passwd = malloc(strlen(gr->gr_passwd) + 1)) == NULL)
162 strcpy(result->gr_passwd, gr->gr_passwd);
166 result->gr_gid = gr->gr_gid;
168 /* Group membership */
170 if ((gr->num_gr_mem < 0) || !gr_mem) {
174 if (gr->num_gr_mem == 0) {
178 *(result->gr_mem) = NULL;
182 if ((tst = malloc(((gr->num_gr_mem + 1) * sizeof(char *)))) == NULL)
185 result->gr_mem = (char **)tst;
187 /* Start looking at extra data */
191 while(next_token((char **)&gr_mem, name, ",", sizeof(fstring))) {
193 /* Allocate space for member */
195 if (((result->gr_mem)[i] =
196 malloc(strlen(name) + 1)) == NULL) {
197 for ( i -= 1; i >= 0; i--)
198 SAFE_FREE((result->gr_mem)[i]);
203 strcpy((result->gr_mem)[i], name);
209 (result->gr_mem)[i] = NULL;
212 printf("wb_aix - returning filled grent %s, %d\n", result->gr_name, result->gr_gid);
216 /* A memory allocation failed, undo succesfull allocations and
221 SAFE_FREE(result->gr_passwd);
222 SAFE_FREE(result->gr_name);
230 static struct group *
231 wb_aix_getgrgid (gid_t gid)
233 /* take a group id and return a filled struct group */
235 struct winbindd_response response;
236 struct winbindd_request request;
238 ZERO_STRUCT(response);
239 ZERO_STRUCT(request);
241 printf("wb_aix - getgrid for %d\n", gid);
244 request.data.gid = gid;
246 if (winbindd_request(WINBINDD_GETGRGID, &request, &response)
247 == NSS_STATUS_SUCCESS) {
249 printf("wb_aix - returned from winbind_request\n");
251 return fill_grent(&response.data.gr, response.extra_data);
257 static struct group *
258 wb_aix_getgrnam (const char *name)
260 /* take a group name and return a filled struct group */
262 struct winbindd_response response;
263 struct winbindd_request request;
265 ZERO_STRUCT(response);
266 ZERO_STRUCT(request);
269 printf("wb_aix - getgrnam for %s\n", name);
272 strncpy(request.data.groupname, name,
273 sizeof(request.data.groupname));
274 request.data.groupname
275 [sizeof(request.data.groupname) - 1] = '\0';
278 if (winbindd_request(WINBINDD_GETGRNAM, &request, &response)
279 == NSS_STATUS_SUCCESS) {
281 printf("wb_aix - returned from winbind_request\n");
283 return fill_grent(&response.data.gr, response.extra_data);
289 wb_aix_getgrset (const char *user)
291 /* take a username and return a string containing a comma-separated list of
292 group id numbers to which the user belongs */
294 struct winbindd_response response;
295 struct winbindd_request request;
297 char *tmpbuf, *result;
301 printf("wb_aix - getgrset for %s\n", user);
303 strncpy(request.data.username, user,
304 sizeof(request.data.username) - 1);
305 request.data.username
306 [sizeof(request.data.username) - 1] = '\0';
309 if (winbindd_request(WINBINDD_GETGROUPS, &request, &response)
310 == NSS_STATUS_SUCCESS) {
311 int num_gids = response.data.num_entries;
312 gid_t *gid_list = (gid_t *)response.extra_data;
314 printf("wb_aix - returned from winbind_request\n");
317 /* allocate a space large enough to contruct the string */
318 if (!(tmpbuf = malloc(num_gids*12))) {
321 idx += sprintf(tmpbuf, "%d", gid_list[0]);
322 for (i = 1; i < num_gids; i++) {
324 idx += sprintf(tmpbuf+idx, "%d", gid_list[i]);
327 if (!(result = malloc(idx+1))) {
328 /* allocate a string the right size to return, but
329 if that fails may as well return our working buffer
330 because it contains the same thing */
333 strcpy(result, tmpbuf);
340 static struct passwd *
341 wb_aix_getpwuid (uid_t uid)
343 /* take a uid and return a filled struct passwd */
345 struct winbindd_response response;
346 struct winbindd_request request;
348 ZERO_STRUCT(response);
349 ZERO_STRUCT(request);
352 printf("wb_aix - getpwid for %d\n", uid);
355 request.data.uid = uid;
357 if (winbindd_request(WINBINDD_GETPWUID, &request, &response)
358 == NSS_STATUS_SUCCESS) {
360 printf("wb_aix - returned from winbind_request\n");
362 return fill_pwent(&response.data.pw);
367 static struct passwd *
368 wb_aix_getpwnam (const char *name)
370 /* take a username and return a filled struct passwd */
372 struct winbindd_response response;
373 struct winbindd_request request;
375 ZERO_STRUCT(response);
376 ZERO_STRUCT(request);
378 printf("wb_aix - getpwnam for %s\n", name);
380 strncpy(request.data.username, name,
381 sizeof(request.data.username) - 1);
382 request.data.username
383 [sizeof(request.data.username) - 1] = '\0';
386 if (winbindd_request(WINBINDD_GETPWNAM, &request, &response)
387 == NSS_STATUS_SUCCESS) {
389 printf("wb_aix - returned from winbind_request\n");
391 return fill_pwent(&response.data.pw);
397 wb_aix_init (struct secmethod_table *methods)
399 memset(methods, 0, sizeof(*methods));
401 /* identification methods, this is the minimum requried for a
404 methods->method_getgrgid = wb_aix_getgrgid;
405 methods->method_getgrnam = wb_aix_getgrnam;
406 methods->method_getgrset = wb_aix_getgrset;
407 methods->method_getpwnam = wb_aix_getpwnam;
408 methods->method_getpwuid = wb_aix_getpwuid;