Back to index

glibc  2.9
netname.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 1999, 2002, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <rpc/rpc.h>
00024 
00025 #include "nsswitch.h"
00026 
00027 #define       OPSYS_LEN 4
00028 #define       MAXIPRINT (11)              /* max length of printed integer */
00029 static const char OPSYS[] = "unix";
00030 
00031 int
00032 user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid,
00033              const char *domain)
00034 {
00035   char dfltdom[MAXNETNAMELEN + 1];
00036   size_t i;
00037 
00038   if (domain == NULL)
00039     {
00040       if (getdomainname (dfltdom, sizeof (dfltdom)) < 0)
00041        return 0;
00042     }
00043   else
00044     {
00045       strncpy (dfltdom, domain, MAXNETNAMELEN);
00046       dfltdom[MAXNETNAMELEN] = '\0';
00047     }
00048 
00049   if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN)
00050     return 0;
00051 
00052   sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
00053   i = strlen (netname);
00054   if (netname[i - 1] == '.')
00055     netname[i - 1] = '\0';
00056   return 1;
00057 }
00058 libc_hidden_def (user2netname)
00059 
00060 int
00061 host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
00062              const char *domain)
00063 {
00064   char *p;
00065   char hostname[MAXHOSTNAMELEN + 1];
00066   char domainname[MAXHOSTNAMELEN + 1];
00067   char *dot_in_host;
00068   size_t i;
00069 
00070   netname[0] = '\0';        /* make null first (no need for memset) */
00071 
00072   if (host == NULL)
00073     __gethostname (hostname, MAXHOSTNAMELEN);
00074   else
00075     {
00076       strncpy (hostname, host, MAXHOSTNAMELEN);
00077       hostname[MAXHOSTNAMELEN] = '\0';
00078     }
00079 
00080   dot_in_host = strchr (hostname, '.');
00081   if (domain == NULL)
00082     {
00083       p = dot_in_host;
00084       if (p)
00085        {
00086          ++p;
00087          strncpy (domainname, p, MAXHOSTNAMELEN);
00088          domainname[MAXHOSTNAMELEN] = '\0';
00089        }
00090       else
00091        {
00092          domainname[0] = 0;
00093          getdomainname (domainname, MAXHOSTNAMELEN);
00094        }
00095     }
00096   else
00097     {
00098       strncpy (domainname, domain, MAXHOSTNAMELEN);
00099       domainname[MAXHOSTNAMELEN] = '\0';
00100     }
00101 
00102   i = strlen (domainname);
00103   if (i == 0)
00104     /* No domainname */
00105     return 0;
00106   if (domainname[i - 1] == '.')
00107     domainname[i - 1] = 0;
00108 
00109   if (dot_in_host)          /* strip off rest of name */
00110     *dot_in_host = '\0';
00111 
00112   if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
00113       > MAXNETNAMELEN)
00114     return 0;
00115 
00116   sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
00117   return 1;
00118 }
00119 libc_hidden_def (host2netname)
00120 
00121 int
00122 getnetname (char name[MAXNETNAMELEN + 1])
00123 {
00124   uid_t uid;
00125   int dummy;
00126 
00127   uid = __geteuid ();
00128   if (uid == 0)
00129     dummy = host2netname (name, NULL, NULL);
00130   else
00131     dummy = user2netname (name, uid, NULL);
00132   return (dummy);
00133 }
00134 libc_hidden_def (getnetname)
00135 
00136 /* Type of the lookup function for netname2user.  */
00137 typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
00138                                   uid_t *, gid_t *, int *, gid_t *);
00139 /* The lookup function for the first entry of this service.  */
00140 extern int __nss_publickey_lookup (service_user ** nip, const char *name,
00141                                void **fctp) internal_function;
00142 
00143 int
00144 netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
00145              int *gidlenp, gid_t * gidlist)
00146 {
00147   static service_user *startp;
00148   static netname2user_function start_fct;
00149   service_user *nip;
00150   union
00151   {
00152     netname2user_function f;
00153     void *ptr;
00154   } fct;
00155   enum nss_status status = NSS_STATUS_UNAVAIL;
00156   int no_more;
00157 
00158   if (startp == NULL)
00159     {
00160       no_more = __nss_publickey_lookup (&nip, "netname2user", &fct.ptr);
00161       if (no_more)
00162        startp = (service_user *) - 1;
00163       else
00164        {
00165          startp = nip;
00166          start_fct = fct.f;
00167        }
00168     }
00169   else
00170     {
00171       fct.f = start_fct;
00172       no_more = (nip = startp) == (service_user *) - 1;
00173     }
00174 
00175   while (!no_more)
00176     {
00177       status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
00178 
00179       no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
00180     }
00181 
00182   return status == NSS_STATUS_SUCCESS;
00183 }
00184 libc_hidden_def (netname2user)
00185 
00186 int
00187 netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
00188              const int hostlen)
00189 {
00190   char *p1, *p2;
00191   char buffer[MAXNETNAMELEN + 1];
00192 
00193   p1 = strchr (buffer, '.');
00194   if (p1 == NULL)
00195     return 0;
00196   p1++;
00197 
00198   p2 = strchr (p1, '@');
00199   if (p2 == NULL)
00200     return 0;
00201   *p2 = '\0';
00202 
00203   if (hostlen > MAXNETNAMELEN)
00204     return 0;
00205 
00206   strncpy (hostname, p1, hostlen);
00207   hostname[hostlen] = '\0';
00208 
00209   return 1;
00210 }