getnetgrent.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 612 行 · 第 1/2 页
C
612 行
break; case(4): sprintf((char *)key, "*.*"); break; } return(1);}#endif/* * Search for a match in a netgroup. */intinnetgr(group, host, user, dom) const char *group, *host, *user, *dom;{ char *hst, *usr, *dm;#ifdef YP char *result; int resultlen; int rv;#endif /* Sanity check */ if (group == NULL || !strlen(group)) return (0);#ifdef YP _yp_innetgr = 1;#endif setnetgrent(group);#ifdef YP _yp_innetgr = 0; /* * If we're in NIS-only mode, do the search using * NIS 'reverse netgroup' lookups. */ if (_use_only_yp) { char _key[MAXHOSTNAMELEN]; int rot = 0; if(yp_get_default_domain(&_netgr_yp_domain)) return(0); while(_buildkey(_key, user ? user : host, dom, &rot)) { if (!yp_match(_netgr_yp_domain, user? "netgroup.byuser": "netgroup.byhost", _key, strlen(_key), &result, &resultlen)) { rv = _listmatch(result, group, resultlen); free(result); if (rv) return(1); else return(0); } }#ifdef CHARITABLE } /* * Couldn't match using NIS-exclusive mode -- try * standard mode. */ setnetgrent(group);#else return(0); }#endif /* CHARITABLE */#endif /* YP */ while (getnetgrent(&hst, &usr, &dm)) if ((host == NULL || hst == NULL || !strcmp(host, hst)) && (user == NULL || usr == NULL || !strcmp(user, usr)) && ( dom == NULL || dm == NULL || !strcmp(dom, dm))) { endnetgrent(); return (1); } endnetgrent(); return (0);}/* * Parse the netgroup file setting up the linked lists. */static intparse_netgrp(group) char *group;{ register char *spos, *epos; register int len, strpos;#ifdef DEBUG register int fields;#endif char *pos, *gpos; struct netgrp *grp; struct linelist *lp = linehead; /* * First, see if the line has already been read in. */ while (lp) { if (!strcmp(group, lp->l_groupname)) break; lp = lp->l_next; } if (lp == (struct linelist *)0 && (lp = read_for_group(group)) == (struct linelist *)0) return (1); if (lp->l_parsed) {#ifdef DEBUG /* * This error message is largely superflous since the * code handles the error condition sucessfully, and * spewing it out from inside libc can actually hose * certain programs. */ fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);#endif return (1); } else lp->l_parsed = 1; pos = lp->l_line; /* Watch for null pointer dereferences, dammit! */ while (pos != NULL && *pos != '\0') { if (*pos == '(') { grp = (struct netgrp *)malloc(sizeof (struct netgrp)); bzero((char *)grp, sizeof (struct netgrp)); grp->ng_next = grouphead.gr; grouphead.gr = grp; pos++; gpos = strsep(&pos, ")");#ifdef DEBUG fields = 0;#endif for (strpos = 0; strpos < 3; strpos++) { if ((spos = strsep(&gpos, ","))) {#ifdef DEBUG fields++;#endif while (*spos == ' ' || *spos == '\t') spos++; if ((epos = strpbrk(spos, " \t"))) { *epos = '\0'; len = epos - spos; } else len = strlen(spos); if (len > 0) { grp->ng_str[strpos] = (char *) malloc(len + 1); bcopy(spos, grp->ng_str[strpos], len + 1); } } else { /* * All other systems I've tested * return NULL for empty netgroup * fields. It's up to user programs * to handle the NULLs appropriately. */ grp->ng_str[strpos] = NULL; } }#ifdef DEBUG /* * Note: on other platforms, malformed netgroup * entries are not normally flagged. While we * can catch bad entries and report them, we should * stay silent by default for compatibility's sake. */ if (fields < 3) fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n", grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST], grp->ng_str[NG_USER] == NULL ? "" : ",", grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER], grp->ng_str[NG_DOM] == NULL ? "" : ",", grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM], lp->l_groupname);#endif } else { spos = strsep(&pos, ", \t"); if (parse_netgrp(spos)) continue; } /* Watch for null pointer dereferences, dammit! */ if (pos != NULL) while (*pos == ' ' || *pos == ',' || *pos == '\t') pos++; } return (0);}/* * Read the netgroup file and save lines until the line for the netgroup * is found. Return 1 if eof is encountered. */static struct linelist *read_for_group(group) char *group;{ register char *pos, *spos, *linep, *olinep; register int len, olen; int cont; struct linelist *lp; char line[LINSIZ + 2];#ifdef YP char *result; int resultlen; while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { if (_netgr_yp_enabled) { if(!_netgr_yp_domain) if(yp_get_default_domain(&_netgr_yp_domain)) continue; if (yp_match(_netgr_yp_domain, "netgroup", group, strlen(group), &result, &resultlen)) { free(result); if (_use_only_yp) return ((struct linelist *)0); else { _netgr_yp_enabled = 0; continue; } } snprintf(line, LINSIZ, "%s %s", group, result); free(result); }#else while (fgets(line, LINSIZ, netf) != NULL) {#endif pos = (char *)&line;#ifdef YP if (*pos == '+') { _netgr_yp_enabled = 1; continue; }#endif if (*pos == '#') continue; while (*pos == ' ' || *pos == '\t') pos++; spos = pos; while (*pos != ' ' && *pos != '\t' && *pos != '\n' && *pos != '\0') pos++; len = pos - spos; while (*pos == ' ' || *pos == '\t') pos++; if (*pos != '\n' && *pos != '\0') { lp = (struct linelist *)malloc(sizeof (*lp)); lp->l_parsed = 0; lp->l_groupname = (char *)malloc(len + 1); bcopy(spos, lp->l_groupname, len); *(lp->l_groupname + len) = '\0'; len = strlen(pos); olen = 0; /* * Loop around handling line continuations. */ do { if (*(pos + len - 1) == '\n') len--; if (*(pos + len - 1) == '\\') { len--; cont = 1; } else cont = 0; if (len > 0) { linep = (char *)malloc(olen + len + 1); if (olen > 0) { bcopy(olinep, linep, olen); free(olinep); } bcopy(pos, linep + olen, len); olen += len; *(linep + olen) = '\0'; olinep = linep; } if (cont) { if (fgets(line, LINSIZ, netf)) { pos = line; len = strlen(pos); } else cont = 0; } } while (cont); lp->l_line = linep; lp->l_next = linehead; linehead = lp; /* * If this is the one we wanted, we are done. */ if (!strcmp(lp->l_groupname, group)) return (lp); } }#ifdef YP /* * Yucky. The recursive nature of this whole mess might require * us to make more than one pass through the netgroup file. * This might be best left outside the #ifdef YP, but YP is * defined by default anyway, so I'll leave it like this * until I know better. */ rewind(netf);#endif return ((struct linelist *)0);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?