Commit 9ffb4efa 9ffb4efac1b4e6a30cc3b6e202679e168c1f2a5d by Sergey Poznyakoff

_app_getpwnam is a list of getpwnam functions.

mu_getpwnam calls each function in succession until it finds one
which returns non-NULL value.
1 parent ab6586ae
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
30 #include <unistd.h> 30 #include <unistd.h>
31 31
32 #include <mailutils/mutil.h> 32 #include <mailutils/mutil.h>
33 #include <mailutils/iterator.h>
33 34
34 /* convert a sequence of hex characters into an integer */ 35 /* convert a sequence of hex characters into an integer */
35 36
...@@ -325,21 +326,79 @@ util_cpystr (char *dst, const char *src, size_t size) ...@@ -325,21 +326,79 @@ util_cpystr (char *dst, const char *src, size_t size)
325 return len; 326 return len;
326 } 327 }
327 328
328 static struct passwd *(*_app_getpwnam) __P((const char *)) = NULL; 329 static list_t *_app_getpwnam = NULL;
329 330
330 void 331 void
331 mu_register_getpwnam (struct passwd *(*fun) __P((const char *))) 332 mu_register_getpwnam (struct passwd *(*fun) __P((const char *)))
332 { 333 {
333 _app_getpwnam = fun; 334 if (!_app_getpwnam && list_create (&_app_getpwnam))
335 return;
336 list_append (_app_getpwnam, fun);
334 } 337 }
335 338
336 struct passwd * 339 struct passwd *
337 mu_getpwnam (const char *name) 340 mu_getpwnam (const char *name)
338 { 341 {
339 struct passwd *p; 342 struct passwd *p;
343 iterator_t itr;
340 344
341 p = getpwnam (name); 345 p = getpwnam (name);
342 if (!p && _app_getpwnam) 346
343 p = (*_app_getpwnam)(name); 347 if (!p && iterator_create (&itr, _app_getpwnam) == 0)
348 {
349 struct passwd *(*fun) __P((char *));
350 for (iterator_first (itr); !p && !iterator_is_done (itr);
351 iterator_next (itr))
352 {
353 iterator_current (itr, (void **)&fun);
354 p = (*fun) (name);
355 }
356
357 iterator_destroy (&itr);
358 }
344 return p; 359 return p;
345 } 360 }
361
362 #ifdef USE_VIRTUAL_DOMAINS
363
364 int mu_virtual_domain;
365
366 struct passwd *
367 getpwnam_virtual (const char *u)
368 {
369 struct passwd *pw = NULL;
370 FILE *pfile;
371 int i = 0, len = strlen (u), delim = 0;
372 char *filename;
373
374 mu_virtual_domain = 0;
375 for (i = 0; i < len && delim == 0; i++)
376 if (u[i] == '!' || u[i] == ':' || u[i] == '@')
377 delim = i;
378
379 if (delim == 0)
380 return NULL;
381
382 filename = malloc (strlen (SITE_VIRTUAL_PWDDIR) +
383 strlen (&u[delim + 1]) + 1);
384 if (filename == NULL)
385 return NULL;
386
387 sprintf (filename, "%s/%s", SITE_VIRTUAL_PWDDIR, &u[delim + 1]);
388 pfile = fopen (filename, "r");
389 free (filename);
390
391 if (pfile)
392 while ((pw = fgetpwent (pfile)) != NULL)
393 {
394 if (strlen (pw->pw_name) == delim && !strncmp (u, pw->pw_name, delim))
395 {
396 mu_virtual_domain = 1;
397 break;
398 }
399 }
400
401 return NULL;
402 }
403
404 #endif
......