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

  1. #include <rpc/rpc.h>
  2. #include <rpcsvc/ypclnt.h>
  3. #include <rpcsvc/ypupdate_prot.h>
  4.  
  5.  
  6. #define WINDOW 60
  7.  
  8. static struct timeval TIMEOUT = { 25, 0 };
  9.  
  10.     
  11. int
  12. yp_update(char *domain, char *map, unsigned ypop, 
  13.       char *key, int keylen, char *data, int datalen)
  14. {
  15.     union {
  16.     ypupdate_args update_args;
  17.     ypdelete_args delete_args;
  18.     } args;
  19.     xdrproc_t xdr_argument;
  20.     unsigned res = 0;
  21.     CLIENT *clnt;
  22.     char *master;
  23.     struct sockaddr saddr;
  24.     char servername[MAXNETNAMELEN + 1];
  25.     int r;
  26.   
  27.     if (!domain || !map || !key || (ypop != YPOP_DELETE && !data))
  28.       return YPERR_BADARGS;
  29.  
  30.     args.update_args.mapname = map;
  31.     args.update_args.key.yp_buf_len = keylen; 
  32.     args.update_args.key.yp_buf_val = key;
  33.     args.update_args.datum.yp_buf_len = datalen;
  34.     args.update_args.datum.yp_buf_val = data;
  35.  
  36.     if ((r = yp_master(domain, map, &master)) != 0)
  37.       return r;
  38.  
  39.     if (!host2netname(servername, master, domain)) {
  40.     fprintf(stderr, "yp_update: cannot convert host to netname\n");
  41.     return YPERR_YPERR;
  42.     }
  43.     if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL) {
  44.     clnt_pcreateerror ("yp_update: clnt_create");
  45.     return YPERR_RPC;
  46.     }
  47.     if (!clnt_control(clnt, CLGET_SERVER_ADDR, (char *)&saddr)) {
  48.     fprintf(stderr, "yp_update: cannot get server address\n");
  49.     return YPERR_RPC;
  50.     }
  51.     switch (ypop) {
  52.       case YPOP_CHANGE:
  53.       case YPOP_INSERT:
  54.       case YPOP_STORE:
  55.     xdr_argument = (xdrproc_t) xdr_ypupdate_args;
  56.     break;
  57.  
  58.       case YPOP_DELETE:
  59.     xdr_argument = (xdrproc_t) xdr_ypdelete_args;
  60.     break;
  61.  
  62.       default:
  63.     return YPERR_BADARGS;
  64.     break;
  65.     }
  66.     clnt->cl_auth = authdes_create(servername, WINDOW, &saddr, NULL);
  67.  
  68.     if (clnt->cl_auth == NULL)
  69.       clnt->cl_auth = authunix_create_default();
  70.  
  71.   again:    
  72.     r = clnt_call (clnt, ypop, xdr_argument, &args,
  73.            xdr_u_int, &res, TIMEOUT);
  74.  
  75.     if (r == RPC_AUTHERROR) {
  76.  
  77.     if (clnt->cl_auth->ah_cred.oa_flavor == AUTH_DES) { 
  78.         clnt->cl_auth = authunix_create_default();
  79.         goto again;
  80.     } else
  81.       return YPERR_ACCESS;
  82.     }
  83.     if (r != RPC_SUCCESS) {
  84.     clnt_perror(clnt, "yp_update: clnt_call");
  85.     return YPERR_RPC;
  86.     }
  87.     return res;
  88. }
  89.