summaryrefslogtreecommitdiff
path: root/usr/src/lib/nsswitch/files/common/getservent.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/lib/nsswitch/files/common/getservent.c')
-rw-r--r--usr/src/lib/nsswitch/files/common/getservent.c146
1 files changed, 119 insertions, 27 deletions
diff --git a/usr/src/lib/nsswitch/files/common/getservent.c b/usr/src/lib/nsswitch/files/common/getservent.c
index 855b543d55..32e73ea3f6 100644
--- a/usr/src/lib/nsswitch/files/common/getservent.c
+++ b/usr/src/lib/nsswitch/files/common/getservent.c
@@ -2,9 +2,8 @@
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License"). You may not use this file except in compliance
- * with the License.
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
@@ -20,10 +19,10 @@
* CDDL HEADER END
*/
/*
- * Copyright (c) 1988-1995 Sun Microsystems Inc
- * All Rights Reserved.
+ * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
*
- * files/getservent.c -- "files" backend for nsswitch "services" database
+ * files/getservent.c -- "files" backend for nsswitch "services" database
*/
#pragma ident "%Z%%M% %I% %E% SMI"
@@ -32,27 +31,85 @@
#include "files_common.h"
#include <sys/types.h>
#include <netinet/in.h>
+#include <inttypes.h>
#include <strings.h>
+#include <ctype.h>
+#include <stdlib.h>
static int
-check_name(args)
- nss_XbyY_args_t *args;
+check_name(nss_XbyY_args_t *argp, const char *line, int linelen)
{
- struct servent *serv = (struct servent *) args->returnval;
- const char *name = args->key.serv.serv.name;
- const char *proto = args->key.serv.proto;
- char **aliasp;
+ const char *limit, *linep, *keyp;
+ int name_match = 0;
- if (proto != 0 && strcmp(serv->s_proto, proto) != 0) {
- return (0);
+ linep = line;
+ limit = line + linelen;
+ keyp = argp->key.serv.serv.name;
+
+ /* compare name */
+ while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
+ keyp++;
+ linep++;
}
- if (strcmp(serv->s_name, name) == 0) {
- return (1);
+ if (*keyp == '\0' && linep < limit && isspace(*linep)) {
+ if (argp->key.serv.proto == NULL)
+ return (1);
+ else
+ name_match = 1;
}
- for (aliasp = serv->s_aliases; *aliasp != 0; aliasp++) {
- if (strcmp(*aliasp, name) == 0) {
+
+ /* skip remainder of the name, if any */
+ while (linep < limit && !isspace(*linep))
+ linep++;
+ /* skip the delimiting spaces */
+ while (linep < limit && isspace(*linep))
+ linep++;
+ /* skip port number */
+ while (linep < limit && !isspace(*linep) && *linep != '/')
+ linep++;
+ if (linep == limit || *linep != '/')
+ return (0);
+
+ linep++;
+ if ((keyp = argp->key.serv.proto) == NULL) {
+ /* skip protocol */
+ while (linep < limit && !isspace(*linep))
+ linep++;
+ } else {
+ /* compare protocol */
+ while (*keyp && linep < limit && !isspace(*linep) &&
+ *keyp == *linep) {
+ keyp++;
+ linep++;
+ }
+ /* no protocol match */
+ if (*keyp || (linep < limit && !isspace(*linep)))
+ return (0);
+ /* protocol and name match, return */
+ if (name_match)
return (1);
+ /* protocol match but name yet to be matched, so continue */
+ }
+
+ /* compare with the aliases */
+ while (linep < limit) {
+ /* skip the delimiting spaces */
+ while (linep < limit && isspace(*linep))
+ linep++;
+
+ /* compare with the alias name */
+ keyp = argp->key.serv.serv.name;
+ while (*keyp && linep < limit && !isspace(*linep) &&
+ *keyp == *linep) {
+ keyp++;
+ linep++;
}
+ if (*keyp == '\0' && (linep == limit || isspace(*linep)))
+ return (1);
+
+ /* skip remainder of the alias name, if any */
+ while (linep < limit && !isspace(*linep))
+ linep++;
}
return (0);
}
@@ -62,21 +119,56 @@ getbyname(be, a)
files_backend_ptr_t be;
void *a;
{
- nss_XbyY_args_t *argp = (nss_XbyY_args_t *) a;
+ nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
return (_nss_files_XY_all(be, argp, 1,
argp->key.serv.serv.name, check_name));
}
static int
-check_port(args)
- nss_XbyY_args_t *args;
+check_port(nss_XbyY_args_t *argp, const char *line, int linelen)
{
- struct servent *serv = (struct servent *) args->returnval;
- const char *proto = args->key.serv.proto;
+ const char *limit, *linep, *keyp, *numstart;
+ int numlen, s_port;
+ char numbuf[12], *numend;
+
+ linep = line;
+ limit = line + linelen;
+
+ /* skip name */
+ while (linep < limit && !isspace(*linep))
+ linep++;
+ /* skip the delimiting spaces */
+ while (linep < limit && isspace(*linep))
+ linep++;
+
+ /* compare port num */
+ numstart = linep;
+ while (linep < limit && !isspace(*linep) && *linep != '/')
+ linep++;
+ if (linep == limit || *linep != '/')
+ return (0);
+ numlen = linep - numstart;
+ if (numlen == 0 || numlen >= sizeof (numbuf))
+ return (0);
+ (void) memcpy(numbuf, numstart, numlen);
+ numbuf[numlen] = '\0';
+ s_port = htons((int)strtol(numbuf, &numend, 10));
+ if (*numend != '\0')
+ return (0);
+ if (s_port == argp->key.serv.serv.port) {
+ if ((keyp = argp->key.serv.proto) == NULL)
+ return (1);
+ } else
+ return (0);
- return (serv->s_port == args->key.serv.serv.port &&
- (proto == 0 || strcmp(serv->s_proto, proto) == 0));
+ /* compare protocol */
+ linep++;
+ while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
+ keyp++;
+ linep++;
+ }
+ return (*keyp == '\0' && (linep == limit || isspace(*linep)));
}
static nss_status_t
@@ -84,10 +176,10 @@ getbyport(be, a)
files_backend_ptr_t be;
void *a;
{
- nss_XbyY_args_t *argp = (nss_XbyY_args_t *) a;
+ nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
char portstr[12];
- sprintf(portstr, "%d", ntohs(argp->key.serv.serv.port));
+ (void) snprintf(portstr, 12, "%d", ntohs(argp->key.serv.serv.port));
return (_nss_files_XY_all(be, argp, 1, portstr, check_port));
}