Back to index

glibc  2.9
nis_defaults.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 2004, 2006 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 <assert.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/types.h>
00025 #include <rpc/rpc.h>
00026 #include <rpcsvc/nis.h>
00027 
00028 #define DEFAULT_TTL 43200
00029 
00030 /*
00031 ** Some functions for parsing the -D param and NIS_DEFAULTS Environ
00032 */
00033 static nis_name
00034 searchXYX (char *str, const char *what)
00035 {
00036   assert (strlen (what) == 6);
00037   assert (strncmp (str, what, 6) == 0);
00038   str += 6;                 /* Points to the begin of the parameters.  */
00039 
00040   int i = 0;
00041   while (str[i] != '\0' && str[i] != ':')
00042     ++i;
00043   if (i == 0)               /* only "<WHAT>=" ? */
00044     return strdup ("");
00045 
00046   return strndup (str, i);
00047 }
00048 
00049 
00050 static nis_name
00051 searchgroup (char *str)
00052 {
00053   return searchXYX (str, "group=");
00054 }
00055 
00056 
00057 static nis_name
00058 searchowner (char *str)
00059 {
00060   return searchXYX (str, "owner=");
00061 }
00062 
00063 
00064 static uint32_t
00065 searchttl (char *str)
00066 {
00067   char buf[strlen (str) + 1];
00068   char *cptr, *dptr;
00069   uint32_t time;
00070   int i;
00071 
00072   dptr = strstr (str, "ttl=");
00073   if (dptr == NULL)         /* should (could) not happen */
00074     return DEFAULT_TTL;;
00075 
00076   dptr += 4;                /* points to the begin of the new ttl */
00077   i = 0;
00078   while (dptr[i] != '\0' && dptr[i] != ':')
00079     i++;
00080   if (i == 0)               /* only "ttl=" ? */
00081     return DEFAULT_TTL;
00082 
00083   strncpy (buf, dptr, i);
00084   buf[i] = '\0';
00085   time = 0;
00086 
00087   dptr = buf;
00088   cptr = strchr (dptr, 'd');
00089   if (cptr != NULL)
00090     {
00091       *cptr = '\0';
00092       cptr++;
00093       time += atoi (dptr) * 60 * 60 * 24;
00094       dptr = cptr;
00095     }
00096 
00097   cptr = strchr (dptr, 'h');
00098   if (cptr != NULL)
00099     {
00100       *cptr = '\0';
00101       cptr++;
00102       time += atoi (dptr) * 60 * 60;
00103       dptr = cptr;
00104     }
00105 
00106   cptr = strchr (dptr, 'm');
00107   if (cptr != NULL)
00108     {
00109       *cptr = '\0';
00110       cptr++;
00111       time += atoi (dptr) * 60;
00112       dptr = cptr;
00113     }
00114 
00115   cptr = strchr (dptr, 's');
00116   if (cptr != NULL)
00117     *cptr = '\0';
00118 
00119   time += atoi (dptr);
00120 
00121   return time;
00122 }
00123 
00124 static unsigned int
00125 searchaccess (char *str, unsigned int access)
00126 {
00127   char buf[strlen (str) + 1];
00128   char *cptr;
00129   unsigned int result = access;
00130   int i;
00131   int n, o, g, w;
00132 
00133   cptr = strstr (str, "access=");
00134   if (cptr == NULL)
00135     return 0;
00136 
00137   cptr += 7;                /* points to the begin of the access string */
00138   i = 0;
00139   while (cptr[i] != '\0' && cptr[i] != ':')
00140     i++;
00141   if (i == 0)               /* only "access=" ? */
00142     return 0;
00143 
00144   strncpy (buf, cptr, i);
00145   buf[i] = '\0';
00146 
00147   n = o = g = w = 0;
00148   cptr = buf;
00149   if (*cptr == ',') /* Fix for stupid Solaris scripts */
00150     ++cptr;
00151   while (*cptr != '\0')
00152     {
00153       switch (*cptr)
00154        {
00155        case 'n':
00156          n = 1;
00157          break;
00158        case 'o':
00159          o = 1;
00160          break;
00161        case 'g':
00162          g = 1;
00163          break;
00164        case 'w':
00165          w = 1;
00166          break;
00167        case 'a':
00168          o = g = w = 1;
00169          break;
00170        case '-':
00171          cptr++;            /* Remove "-" from beginning */
00172          while (*cptr != '\0' && *cptr != ',')
00173            {
00174              switch (*cptr)
00175               {
00176               case 'r':
00177                 if (n)
00178                   result = result & ~(NIS_READ_ACC << 24);
00179                 if (o)
00180                   result = result & ~(NIS_READ_ACC << 16);
00181                 if (g)
00182                   result = result & ~(NIS_READ_ACC << 8);
00183                 if (w)
00184                   result = result & ~(NIS_READ_ACC);
00185                 break;
00186               case 'm':
00187                 if (n)
00188                   result = result & ~(NIS_MODIFY_ACC << 24);
00189                 if (o)
00190                   result = result & ~(NIS_MODIFY_ACC << 16);
00191                 if (g)
00192                   result = result & ~(NIS_MODIFY_ACC << 8);
00193                 if (w)
00194                   result = result & ~(NIS_MODIFY_ACC);
00195                 break;
00196               case 'c':
00197                 if (n)
00198                   result = result & ~(NIS_CREATE_ACC << 24);
00199                 if (o)
00200                   result = result & ~(NIS_CREATE_ACC << 16);
00201                 if (g)
00202                   result = result & ~(NIS_CREATE_ACC << 8);
00203                 if (w)
00204                   result = result & ~(NIS_CREATE_ACC);
00205                 break;
00206               case 'd':
00207                 if (n)
00208                   result = result & ~(NIS_DESTROY_ACC << 24);
00209                 if (o)
00210                   result = result & ~(NIS_DESTROY_ACC << 16);
00211                 if (g)
00212                   result = result & ~(NIS_DESTROY_ACC << 8);
00213                 if (w)
00214                   result = result & ~(NIS_DESTROY_ACC);
00215                 break;
00216               default:
00217                 return (~0U);
00218               }
00219              cptr++;
00220            }
00221          n = o = g = w = 0;
00222          break;
00223        case '+':
00224          cptr++;            /* Remove "+" from beginning */
00225          while (*cptr != '\0' && *cptr != ',')
00226            {
00227              switch (*cptr)
00228               {
00229               case 'r':
00230                 if (n)
00231                   result = result | (NIS_READ_ACC << 24);
00232                 if (o)
00233                   result = result | (NIS_READ_ACC << 16);
00234                 if (g)
00235                   result = result | (NIS_READ_ACC << 8);
00236                 if (w)
00237                   result = result | (NIS_READ_ACC);
00238                 break;
00239               case 'm':
00240                 if (n)
00241                   result = result | (NIS_MODIFY_ACC << 24);
00242                 if (o)
00243                   result = result | (NIS_MODIFY_ACC << 16);
00244                 if (g)
00245                   result = result | (NIS_MODIFY_ACC << 8);
00246                 if (w)
00247                   result = result | (NIS_MODIFY_ACC);
00248                 break;
00249               case 'c':
00250                 if (n)
00251                   result = result | (NIS_CREATE_ACC << 24);
00252                 if (o)
00253                   result = result | (NIS_CREATE_ACC << 16);
00254                 if (g)
00255                   result = result | (NIS_CREATE_ACC << 8);
00256                 if (w)
00257                   result = result | (NIS_CREATE_ACC);
00258                 break;
00259               case 'd':
00260                 if (n)
00261                   result = result | (NIS_DESTROY_ACC << 24);
00262                 if (o)
00263                   result = result | (NIS_DESTROY_ACC << 16);
00264                 if (g)
00265                   result = result | (NIS_DESTROY_ACC << 8);
00266                 if (w)
00267                   result = result | (NIS_DESTROY_ACC);
00268                 break;
00269               default:
00270                 return (~0U);
00271               }
00272              cptr++;
00273            }
00274          n = o = g = w = 0;
00275          break;
00276        case '=':
00277          cptr++;            /* Remove "=" from beginning */
00278          /* Clear */
00279          if (n)
00280            result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
00281                              NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
00282 
00283          if (o)
00284            result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
00285                              NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
00286          if (g)
00287            result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
00288                              NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
00289          if (w)
00290            result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
00291                             NIS_CREATE_ACC + NIS_DESTROY_ACC);
00292          while (*cptr != '\0' && *cptr != ',')
00293            {
00294              switch (*cptr)
00295               {
00296               case 'r':
00297                 if (n)
00298                   result = result | (NIS_READ_ACC << 24);
00299                 if (o)
00300                   result = result | (NIS_READ_ACC << 16);
00301                 if (g)
00302                   result = result | (NIS_READ_ACC << 8);
00303                 if (w)
00304                   result = result | (NIS_READ_ACC);
00305                 break;
00306               case 'm':
00307                 if (n)
00308                   result = result | (NIS_MODIFY_ACC << 24);
00309                 if (o)
00310                   result = result | (NIS_MODIFY_ACC << 16);
00311                 if (g)
00312                   result = result | (NIS_MODIFY_ACC << 8);
00313                 if (w)
00314                   result = result | (NIS_MODIFY_ACC);
00315                 break;
00316               case 'c':
00317                 if (n)
00318                   result = result | (NIS_CREATE_ACC << 24);
00319                 if (o)
00320                   result = result | (NIS_CREATE_ACC << 16);
00321                 if (g)
00322                   result = result | (NIS_CREATE_ACC << 8);
00323                 if (w)
00324                   result = result | (NIS_CREATE_ACC);
00325                 break;
00326               case 'd':
00327                 if (n)
00328                   result = result | (NIS_DESTROY_ACC << 24);
00329                 if (o)
00330                   result = result | (NIS_DESTROY_ACC << 16);
00331                 if (g)
00332                   result = result | (NIS_DESTROY_ACC << 8);
00333                 if (w)
00334                   result = result | (NIS_DESTROY_ACC);
00335                 break;
00336               default:
00337                 return result = (~0U);
00338               }
00339              cptr++;
00340            }
00341          n = o = g = w = 0;
00342          break;
00343        default:
00344          return result = (~0U);
00345        }
00346       if (*cptr != '\0')
00347        cptr++;
00348     }
00349 
00350   return result;
00351 }
00352 
00353 
00354 nis_name
00355 __nis_default_owner (char *defaults)
00356 {
00357   char *default_owner = NULL;
00358 
00359   char *cptr = defaults;
00360   if (cptr == NULL)
00361     cptr = getenv ("NIS_DEFAULTS");
00362 
00363   if (cptr != NULL)
00364     {
00365       char *dptr = strstr (cptr, "owner=");
00366       if (dptr != NULL)
00367        {
00368          char *p = searchowner (dptr);
00369          if (p == NULL)
00370            return NULL;
00371          default_owner = strdupa (p);
00372          free (p);
00373        }
00374     }
00375 
00376   return strdup (default_owner ?: nis_local_principal ());
00377 }
00378 libnsl_hidden_def (__nis_default_owner)
00379 
00380 
00381 nis_name
00382 __nis_default_group (char *defaults)
00383 {
00384   char *default_group = NULL;
00385 
00386   char *cptr = defaults;
00387   if (cptr == NULL)
00388     cptr = getenv ("NIS_DEFAULTS");
00389 
00390   if (cptr != NULL)
00391     {
00392       char *dptr = strstr (cptr, "group=");
00393       if (dptr != NULL)
00394        {
00395          char *p = searchgroup (dptr);
00396          if (p == NULL)
00397            return NULL;
00398          default_group = strdupa (p);
00399          free (p);
00400        }
00401     }
00402 
00403   return strdup (default_group ?: nis_local_group ());
00404 }
00405 libnsl_hidden_def (__nis_default_group)
00406 
00407 
00408 uint32_t
00409 __nis_default_ttl (char *defaults)
00410 {
00411   char *cptr, *dptr;
00412 
00413   if (defaults != NULL)
00414     {
00415       dptr = strstr (defaults, "ttl=");
00416       if (dptr != NULL)
00417        return searchttl (defaults);
00418     }
00419 
00420   cptr = getenv ("NIS_DEFAULTS");
00421   if (cptr == NULL)
00422     return DEFAULT_TTL;
00423 
00424   dptr = strstr (cptr, "ttl=");
00425   if (dptr == NULL)
00426     return DEFAULT_TTL;
00427 
00428   return searchttl (cptr);
00429 }
00430 
00431 /* Default access rights are ----rmcdr---r---, but we could change
00432    this with the NIS_DEFAULTS variable. */
00433 unsigned int
00434 __nis_default_access (char *param, unsigned int defaults)
00435 {
00436   unsigned int result;
00437   char *cptr;
00438 
00439   if (defaults == 0)
00440     result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
00441   else
00442     result = defaults;
00443 
00444   if (param != NULL && strstr (param, "access=") != NULL)
00445     result = searchaccess (param, result);
00446   else
00447     {
00448       cptr = getenv ("NIS_DEFAULTS");
00449       if (cptr != NULL && strstr (cptr, "access=") != NULL)
00450        result = searchaccess (cptr, result);
00451     }
00452 
00453   return result;
00454 }
00455 libnsl_hidden_def (__nis_default_access)