/*ARGSUSED*/ static int _dns_initshells(void *rv, void *cb_data, va_list ap) { char shellname[] = "shells-XXXXX"; int hsindex, hpi, r; char **hp; void *context; if (sl) sl_free(sl, 1); sl = sl_init(); r = NS_UNAVAIL; if (hesiod_init(&context) == -1) return (r); for (hsindex = 0; ; hsindex++) { snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex); hp = hesiod_resolve(context, shellname, "shells"); if (hp == NULL) { if (errno == ENOENT) { if (hsindex == 0) r = NS_NOTFOUND; else r = NS_SUCCESS; } break; } else { for (hpi = 0; hp[hpi]; hpi++) sl_add(sl, hp[hpi]); free(hp); } } hesiod_end(context); return (r); }
void * _nss_hesiod_init (void) { void *context; if (hesiod_init (&context) == -1) return NULL; /* Use the default (per-thread) resolver state. */ __hesiod_res_set (context, &_res, NULL); return context; }
int __grstart_dns(struct __grstate_dns *state) { _DIAGASSERT(state != NULL); state->num = 0; if (state->context == NULL) { /* setup Hesiod */ if (hesiod_init(&state->context) == -1) return NS_UNAVAIL; } return NS_SUCCESS; }
static char * hesiod_get_pobox (const char **user) { void *context; struct hesiod_postoffice *hpo; char *ret = NULL; if(hesiod_init (&context) != 0) err (1, "hesiod_init"); hpo = hesiod_getmailhost (context, *user); if (hpo == NULL) { warn ("hesiod_getmailhost %s", *user); } else { if (strcasecmp(hpo->hesiod_po_type, "pop") != 0) errx (1, "Unsupported po type %s", hpo->hesiod_po_type); ret = estrdup(hpo->hesiod_po_host); *user = estrdup(hpo->hesiod_po_name); hesiod_free_postoffice (context, hpo); } hesiod_end (context); return ret; }
int main(int argc, char **argv) { char **list, **p, *bindname, *name, *type; int lflag = 0, errflg = 0, bflag = 0, c; void *context; while ((c = getopt(argc, argv, "lb")) != -1) { switch (c) { case 'l': lflag = 1; break; case 'b': bflag = 1; break; default: errflg++; break; } } if (argc - optind != 2 || errflg) { fprintf(stderr, "usage: hesinfo [-bl] name type\n"); fprintf(stderr, "\t-l selects long format\n"); fprintf(stderr, "\t-b also does hes_to_bind conversion\n"); exit(2); } name = argv[optind]; type = argv[optind + 1]; if (hesiod_init(&context) < 0) { if (errno == ENOEXEC) warnx( "hesiod_init: Invalid Hesiod configuration file."); else warn("hesiod_init"); } /* Display bind name if requested. */ if (bflag) { if (lflag) printf("hes_to_bind(%s, %s) expands to\n", name, type); bindname = hesiod_to_bind(context, name, type); if (!bindname) { if (lflag) printf("nothing\n"); if (errno == ENOENT) warnx("hesiod_to_bind: Unknown rhs-extension."); else warn("hesiod_to_bind"); exit(1); } printf("%s\n", bindname); free(bindname); if (lflag) printf("which "); } if (lflag) printf("resolves to\n"); /* Do the hesiod resolve and check for errors. */ list = hesiod_resolve(context, name, type); if (!list) { if (lflag) printf("nothing\n"); if (errno == ENOENT) warnx("hesiod_resolve: Hesiod name not found."); else warn("hesiod_resolve"); exit(1); } /* Display the results. */ for (p = list; *p; p++) printf("%s\n", *p); hesiod_free_list(context, list); hesiod_end(context); exit(0); }
/* Retrieve the user's hesiod groups and stuff them into *groups, with a * count in *ngroups. Also put the user's primary gid into *primary_gid. * Return 0 on success and -1 on failure. */ static int retrieve_hesgroups(const char *username, struct hesgroup **groups, int *ngroups, gid_t *primary_gid) { char **grplistvec, **primarygidvec, *primary_name, buf[64], *p, *q; int n, len; struct hesgroup *hesgroups; struct passwd *pwd; void *hescontext; /* Look up the user's primary group in hesiod to retrieve the primary * group name. Start by finding the gid. */ pwd = al__getpwnam(username); if (!pwd) return -1; *primary_gid = pwd->pw_gid; al__free_passwd(pwd); /* Initialize the hesiod context. */ if (hesiod_init(&hescontext) != 0) return -1; /* Now do the hesiod resolve. If it fails with ENOENT, assume the user * has a local account and return no groups. */ sprintf(buf, "%lu", (unsigned long) *primary_gid); primarygidvec = hesiod_resolve(hescontext, buf, "gid"); if (!primarygidvec && errno == ENOENT) { *groups = NULL; *ngroups = 0; hesiod_end(hescontext); return 0; } if (!primarygidvec || !*primarygidvec || **primarygidvec == ':') { if (primarygidvec) hesiod_free_list(hescontext, primarygidvec); hesiod_end(hescontext); return -1; } /* Copy the name part into primary_name. */ p = strchr(*primarygidvec, ':'); len = (p) ? p - *primarygidvec : strlen(*primarygidvec); primary_name = malloc(len + 1); if (!primary_name) { hesiod_free_list(hescontext, primarygidvec); hesiod_end(hescontext); return -1; } memcpy(primary_name, *primarygidvec, len); primary_name[len] = 0; hesiod_free_list(hescontext, primarygidvec); /* Look up the Hesiod group list. It's okay if there isn't one. */ grplistvec = hesiod_resolve(hescontext, username, "grplist"); if ((!grplistvec && errno != ENOENT) || (grplistvec && !*grplistvec)) { if (grplistvec) hesiod_free_list(hescontext, grplistvec); hesiod_end(hescontext); free(primary_name); return -1; } /* Get a close upper bound on the number of group entries we'll need. */ if (grplistvec) { n = 0; for (p = *grplistvec; *p; p++) { if (*p == ':') n++; } n = (n + 1) / 2 + 1; } else n = 1; /* Allocate memory. */ hesgroups = malloc(n * sizeof(struct hesgroup)); if (!hesgroups) { if (grplistvec) hesiod_free_list(hescontext, grplistvec); hesiod_end(hescontext); free(primary_name); return -1; } /* Start off the list with the primary gid. */ hesgroups[0].name = primary_name; hesgroups[0].gid = *primary_gid; hesgroups[0].present = 0; n = 1; /* Now get the entries from grplistvec, if we got one. */ p = (grplistvec) ? *grplistvec : NULL; while (p) { /* Find the end of the group name. Stop if we hit the end, if we * have a zero-length group name, or if we have a non-numeric gid. */ q = strchr(p, ':'); if (!q || q == p || !isdigit((unsigned char)*(q + 1))) break; if (atoi(q + 1) >= MIN_HES_GROUP) { hesgroups[n].name = malloc(q - p + 1); if (!hesgroups[n].name) { free_hesgroups(hesgroups, n); hesiod_free_list(hescontext, grplistvec); hesiod_end(hescontext); return -1; } memcpy(hesgroups[n].name, p, q - p); hesgroups[n].name[q - p] = 0; hesgroups[n].gid = atoi(q + 1); hesgroups[n].present = 0; n++; } p = strchr(q + 1, ':'); if (p) p++; } /* Clean up allocated memory and return. */ if (grplistvec) hesiod_free_list(hescontext, grplistvec); hesiod_end(hescontext); *ngroups = n; *groups = hesgroups; return 0; }