home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / netname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  4.8 KB  |  243 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char sccsid[] =     "@(#)netname.c    2.2 88/08/10 4.0 RPCSRC; from 1.9 88/02/08 SMI";
  3. #endif
  4. /*
  5.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  6.  * unrestricted use provided that this legend is included on all tape
  7.  * media and as a part of the software program in whole or part.  Users
  8.  * may copy or modify Sun RPC without charge, but are not authorized
  9.  * to license or distribute it to anyone else except as part of a product or
  10.  * program developed by the user.
  11.  * 
  12.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  13.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  14.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  15.  * 
  16.  * Sun RPC is provided with no support and without any obligation on the
  17.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  18.  * modification or enhancement.
  19.  * 
  20.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  21.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  22.  * OR ANY PART THEREOF.
  23.  * 
  24.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  25.  * or profits or other special, indirect and consequential damages, even if
  26.  * Sun has been advised of the possibility of such damages.
  27.  * 
  28.  * Sun Microsystems, Inc.
  29.  * 2550 Garcia Avenue
  30.  * Mountain View, California  94043
  31.  */
  32.  
  33. /*
  34.  * Copyright (c) 1988 by Sun Microsystems, Inc.
  35.  */
  36.  
  37. /*
  38.  * netname utility routines
  39.  * convert from unix names to network names and vice-versa
  40.  * This module is operating system dependent!
  41.  * What we define here will work with any unix system that has adopted
  42.  * the sun yp domain architecture.
  43.  */
  44. #include <sys/param.h>
  45. #include <rpc/rpc.h>
  46. #include <ctype.h>
  47.  
  48.  
  49. extern int sprintf();
  50. extern char *strncpy();
  51.  
  52. static int atois();
  53.  
  54. static char OPSYS[] = "unix";
  55. static char NETID[] = "netid.byname";    
  56.  
  57. /*
  58.  * Convert network-name into unix credential
  59.  */
  60. netname2user(netname, uidp, gidp, gidlenp, gidlist)
  61.     char netname[MAXNETNAMELEN+1];
  62.     int *uidp;
  63.     int *gidp;
  64.     int *gidlenp;
  65.     int *gidlist;
  66. {
  67.     int stat;
  68.     char *val;
  69.     char *p;
  70.     int vallen;
  71.     char *domain;
  72.     int gidlen;
  73.  
  74.     stat = yp_get_default_domain(&domain);
  75.     if (stat != 0) {
  76.         return (0);
  77.     }
  78.     stat = yp_match(domain, NETID, netname, strlen(netname), &val, &vallen);
  79.     if (stat != 0) {
  80.         return (0);
  81.     }
  82.     val[vallen] = 0;
  83.     p = val;
  84.     *uidp = atois(&p);
  85.     if (p == NULL || *p++ != ':') {
  86.         free(val);
  87.         return (0);
  88.     }
  89.     *gidp = atois(&p);
  90.     if (p == NULL) {
  91.         free(val);
  92.         return (0);
  93.     }
  94.     gidlen = 0;
  95.     for (gidlen = 0; gidlen < NGROUPS; gidlen++) {    
  96.         if (*p++ != ',') {
  97.             break;
  98.         }
  99.         gidlist[gidlen] = atois(&p);
  100.         if (p == NULL) {
  101.             free(val);
  102.             return (0);
  103.         }
  104.     }
  105.     *gidlenp = gidlen;
  106.     free(val);
  107.     return (1);
  108. }
  109.  
  110. /*
  111.  * Convert network-name to hostname
  112.  */
  113. netname2host(netname, hostname, hostlen)
  114.     char netname[MAXNETNAMELEN+1];
  115.     char *hostname;
  116.     int hostlen;
  117. {
  118.     int stat;
  119.     char *val;
  120.     int vallen;
  121.     char *domain;
  122.  
  123.     stat = yp_get_default_domain(&domain);
  124.     if (stat != 0) {
  125.         return (0);
  126.     }
  127.     stat = yp_match(domain, NETID, netname, strlen(netname), &val, &vallen);
  128.     if (stat != 0) {
  129.         return (0);
  130.     }
  131.     val[vallen] = 0;
  132.     if (*val != '0') {
  133.         free(val);
  134.         return (0);
  135.     }    
  136.     if (val[1] != ':') {
  137.         free(val);
  138.         return (0);
  139.     }
  140.     (void) strncpy(hostname, val + 2, hostlen);
  141.     free(val);
  142.     return (1);
  143. }
  144.  
  145.  
  146. /*
  147.  * Figure out my fully qualified network name
  148.  */
  149. getnetname(name)
  150.     char name[MAXNETNAMELEN+1];
  151. {
  152.     int uid;
  153.  
  154.     uid = geteuid(); 
  155.     if (uid == 0) {
  156.         return (host2netname(name, (char *) NULL, (char *) NULL));
  157.     } else {
  158.         return (user2netname(name, uid, (char *) NULL));
  159.     }
  160. }
  161.  
  162.  
  163. /*
  164.  * Convert unix cred to network-name
  165.  */
  166. user2netname(netname, uid, domain)
  167.     char netname[MAXNETNAMELEN + 1];
  168.     int uid;
  169.     char *domain;
  170. {
  171.     char *dfltdom;
  172.  
  173. #define MAXIPRINT    (11)    /* max length of printed integer */
  174.  
  175.     if (domain == NULL) {
  176.         if (yp_get_default_domain(&dfltdom) != 0) {
  177.             return (0);
  178.         }
  179.         domain = dfltdom;
  180.     }
  181.     if (strlen(domain) + 1 + MAXIPRINT > MAXNETNAMELEN) {
  182.         return (0);
  183.     }
  184.     (void) sprintf(netname, "%s.%d@%s", OPSYS, uid, domain);    
  185.     return (1);
  186. }
  187.  
  188.  
  189. /*
  190.  * Convert host to network-name
  191.  */
  192. host2netname(netname, host, domain)
  193.     char netname[MAXNETNAMELEN + 1];
  194.     char *host;
  195.     char *domain;
  196. {
  197.     char *dfltdom;
  198.     char hostname[MAXHOSTNAMELEN+1]; 
  199.  
  200.     if (domain == NULL) {
  201.         if (yp_get_default_domain(&dfltdom) != 0) {
  202.             return (0);
  203.         }
  204.         domain = dfltdom;
  205.     }
  206.     if (host == NULL) {
  207.         (void) gethostname(hostname, sizeof(hostname));
  208.         host = hostname;
  209.     }
  210.     if (strlen(domain) + 1 + strlen(host) > MAXNETNAMELEN) {
  211.         return (0);
  212.     } 
  213.     (void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
  214.     return (1);
  215. }
  216.  
  217.  
  218. static
  219. atois(str)
  220.     char **str;
  221. {
  222.     char *p;
  223.     int n;
  224.     int sign;
  225.  
  226.     if (**str == '-') {
  227.         sign = -1;
  228.         (*str)++;
  229.     } else {
  230.         sign = 1;
  231.     }
  232.     n = 0;
  233.     for (p = *str; isdigit(*p); p++) {
  234.         n = (10 * n) + (*p - '0');
  235.     }
  236.     if (p == *str) {
  237.         *str = NULL;
  238.         return (0);
  239.     }
  240.     *str = p;    
  241.     return (n * sign);
  242. }
  243.