DD_Access_sr.c 20.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
/*
 *-------------------- DD_ACCESS -------------------------------
 *
 * name:         DD_Access.c V4.0
 * author:       Alexander Grigoriev , Andrei Fedorov
 * Insitution:   IKI RAN lab.546 aug@afed.iki.rssi.ru
  * Description: Set of function for access to DD system
  *   Last revision:   Oct 10, 2002
 *  List of revisions:
 *                  March    1999  V1.0
 *                 20 Oct 1999 ; ShowTicket was corrected.   V2.0
 *                28 Oct 1999 ; Added LOGINS,ddcheck         V3.0
  *               Oct 10, 2002 - revised by A. Fedorov       V 4.0
 */
/*#define _XOPEN_SOURCE*/  /* for crypt function */

#define _XOPEN_SOURCE
#include <unistd.h>
#include <time.h>
#include <DD.h>
#include <netcdf.h>
#include <DD_comm.h>
#include <DD_Access.h>

/*=========== Global Variables ===============================*/
  size_t CashStart[2]={0L,0L};
  size_t CashCount[2]={1, MAXHOSTLENGTH};
  size_t CashCountInt[2]={1, 1};
  extern int Verbose;
  
/*############################################################################*/

/*----------------GETTICKET------------------------------------*/

/* Description: This function find the user data in user_cash.nc
 *              If O'k rewrite the line with new time.
 *              In case of a new user, remove the first line with 
 *              older time data then a current time + 4.5 hours.
                *
                Take 3 arguments: UserId, 
                                  IP-address(string,like "193.232.6.60\0") 
                                  DD_user_id(can see in user_info.nc)
                                  
                Returns 0 if O'k or NOPERMISSION  if error
                
                If DD_log_name equal NULL searching for UserId, IP-address
                If DD_log_name nonequal NULL searching for IP-address,DD_log_name*
                *
 */

int GetTicket(int UID, char *HostName, int DD_user_id)
  {
    char *ddbase;
    char refname[PATHLENGTH];
    int TicketID, Comp=1, Flag,OldFlag=0, Find=0, dltt;
    int CurIDID,HostID,TimeID, DD_id_ID;   /* Variable IDs in the NC ticket file */
    int TimeFinish;
    int Clock;
    char CurHostName[MAXHOSTLENGTH];
    int CurID, CurTime,DD_id, MemTime,OldestStart;  /* Values to read and write to ticket file */
    size_t CurCashStart[2]={0,0};
    size_t HostCount[2] = {1,MAXHOSTLENGTH};
    int ACCESS = NOPERMISSION;
    int status; /* Return value of any  NC routine */
    
    if(Verbose) fprintf(stderr,"GetTicket: UID = %d, HostName <%s>, DD_user_id = <%d> is going to work\n",UID, HostName,DD_user_id); 

/*------------- OpenUserNC ---------------------------------*/

   if((ddbase = getenv("DDBASE")) == NULL)
   {
      if(Verbose) fprintf(stderr,"GETTICKET:  No DDBASE environment.\n");
      return(ENVIRERROR);
   }
   strcpy(refname,ddbase);
   strcat(refname,TICKET_PATH);
   if(Verbose) fprintf(stderr,"GETTICKET: TICKET_PATH is %s \n",refname);

   if((status = nc_open(refname, NC_WRITE,&TicketID))  !=  NC_NOERR)
   {
      if(Verbose) fprintf(stderr,"GETTICKET:  %s\n", nc_strerror(status));
      return(NOUSERSFILE);
   }

/*------------------ Working with -------------------------------*/
     
   status = nc_inq_varid(TicketID,"id",&CurIDID);
   status = nc_inq_varid(TicketID,"host",&HostID);
   status = nc_inq_varid(TicketID,"time",&TimeID);
   status = nc_inq_varid(TicketID,"DD_id",&DD_id_ID);
   status = nc_sync(TicketID);

   Clock = (int)time(NULL);
 
 //  TimeFinish = Clock+16200;  // 4.5 hours
   TimeFinish = Clock+172800;  // 48 hours
   HostName[strlen(HostName)] = '\0';
   CashStart[0]=0;
   OldestStart = 0;
 
   do
   {
     status = nc_get_var1_int(TicketID, CurIDID, CashStart,&CurID);
     nc_get_var1_int(TicketID, DD_id_ID, CashStart,&DD_id);
     nc_get_vara_text(TicketID, HostID,CashStart,HostCount,CurHostName);
     nc_get_var1_int(TicketID, TimeID,CashStart,&CurTime);

     if(CashStart[0] == 0) MemTime=CurTime;
   
     Comp=strcmp(CurHostName,HostName);
       
     if((CurID == UID)&&(Comp == 0)&&(DD_id == DD_user_id))
     {
        CurCashStart[0]=CashStart[0];
        Find = 1;
     }
     dltt = (CurTime - MemTime);
     
     if((dltt < 0) && (CurHostName[0] != '\0'))
     {
        OldestStart=CashStart[0]; 
        MemTime = CurTime; 
     }

     CashStart[0]++;
   }
   while((Find != 1) && (CurHostName[0] != '\0') && (CashStart[0] < USERCASHLEN));

   if(Verbose) fprintf(stderr,"FINISH TIME : %ld\n",TimeFinish);
  
   CashStart[0]--;
  
   if(MemTime < Clock) {
   OldFlag = 1;
   }
    if(Find == 1)
   {
     nc_put_var1_int(TicketID,TimeID,CurCashStart,&TimeFinish);
     if(Verbose) fprintf(stderr,"User: ID = %d, DD_id = %d, HostName <%s> continue work.\n ",UID,DD_user_id,HostName); 
     ACCESS = 0;
   }
   else
   if(OldFlag == 1)
   {
      CashStart[0]=OldestStart;

      nc_put_var1_int(TicketID,CurIDID, CashStart,&UID);
      nc_put_var1_int(TicketID,DD_id_ID, CashStart,&DD_user_id);
      nc_put_vara_text(TicketID,HostID,CashStart,HostCount,HostName);
      nc_put_var1_int(TicketID,TimeID,CashStart,&TimeFinish);
      if(Verbose) fprintf(stderr,"User: ID = %d, DD_id = %d, \n HostName <%s> started work.\n ",UID,DD_user_id,HostName); 
      ACCESS = 0;
   }
   else
   if((CashStart[0]<USERCASHLEN) && (CurHostName[0] == '\0'))
   {
      nc_put_var1_int(TicketID,CurIDID, CashStart,&UID);
      nc_put_var1_int(TicketID,DD_id_ID, CashStart,&DD_user_id);
      nc_put_vara_text(TicketID,HostID,CashStart,HostCount,HostName);
      nc_put_var1_int(TicketID,TimeID,CashStart,&TimeFinish);
      if(Verbose) fprintf(stderr,"User: ID = %d, DD_id = %d, \n HostName <%s> started work.\n ",UID,DD_user_id,HostName); 
      ACCESS = 0;
   } 
   else {fprintf(stderr,"Too many members!\n");ACCESS = NOPERMISSION;}
  
 
   /*if((CurID == UID)&&(Comp == 0)) fprintf(stderr,"Exists...\n");*/
  
   nc_sync(TicketID);
   nc_close(TicketID);
      
return((int)ACCESS);     
}      

/*############################################################################*/

/*-------------------CHECK TICKET-----------------------*/ 

/* Description: This function check the user for accessability
 *              in user_info.nc file. Returns DD_id if O'k. 
 *              Returns NOPERMISSION in case of unaccessability, neagative value (DD_access.h)
 *              in case of error.
 */
      
int CheckTicket(char *NameUsr, char *Ticket)
{
   int i=0;
   int DD_id;
   int Right = NOPERMISSION;
   char salt[2],newkey[USRLENGTH];
  
   int UsrRefID;  /* ID of reference file which consists description of all users */
   char *ddbase;
   char refname[PATHLENGTH];
  
   int UserDimID,UserLenDimID,PasLenDimID;   /* ID of dimensions in NC file */
   int MemID, PasID;                                              /* ID of variables in NC file */
   size_t MaxRecords, UserLength, PasswdLength;   /* Dimensions */
   size_t start[2] = {0,0}, UserCount[2] = {1,0}, PasswdCount[2] = {1,0};
   static char UserDimName[] = "user", UserLenDimName[] = "UserLength";  /* Dimensions names */
   static char PasLenDimName[] = "PasswdLength";                                       /* Dimensions names */
   static char MemName[] = "member", PasName[] = "passwd";                      /* Variable Names */
   char PasChar[USRLENGTH],MemChar[USRLENGTH];
    int status;   /* Return of any NC call */

/*------------- Open User reference NC ---------------------------------*/
   if((ddbase = getenv("DDBASE")) == NULL)
   {
      if(Verbose) fprintf(stderr,"CHECKTICKET:  No DDBASE environment.\n");
      return(ENVIRERROR);
   }
   strcpy(refname,ddbase);
   strcat(refname,USERREFNAME);
   if(Verbose) fprintf(stderr," CHECHTICKET: PATH is %s \n",refname);
// sleep(40);
   if((status = nc_open(refname, NC_WRITE,&UsrRefID))  !=  NC_NOERR)
   {
      if(Verbose) fprintf(stderr,"CHECHTICKET:  %s\n", nc_strerror(status));
      return(NOUSERSFILE);
   }

/*------------------ Define all dimensions -------------------------------*/

   status = nc_inq_dimid(UsrRefID,UserDimName,&UserDimID);
   status = nc_inq_dimlen(UsrRefID, UserDimID,  &MaxRecords);
   status = nc_inq_dimid(UsrRefID,UserLenDimName,&UserLenDimID);
   status = nc_inq_dimlen(UsrRefID, UserLenDimID,  &UserLength);
   UserCount[1] = UserLength;
   status = nc_inq_dimid(UsrRefID,PasLenDimName,&PasLenDimID);
   status = nc_inq_dimlen(UsrRefID, PasLenDimID,  &PasswdLength);
   PasswdCount[1] = PasswdLength;

/*fprintf(stderr,"CheckTicket:Rec: %d %d %d\n",MaxRecords,UserLength,PasswdLength);*/
  
/*------------------ Define all variables ID -------------------------------*/
   status = nc_inq_varid(UsrRefID, MemName,&MemID);
   status = nc_inq_varid(UsrRefID, PasName,&PasID);
    
  
   for(i=0;i<MaxRecords;i++)
   {
      start[0]=(size_t)i;
      nc_get_vara_text(UsrRefID, MemID, start,UserCount, MemChar);

      if(strcmp(MemChar,NameUsr)==0)
      {
         nc_get_vara_text(UsrRefID, PasID, start, PasswdCount, PasChar);

         /*fprintf(stderr,"Your dd_id is %u\n",i);*/
         DD_id = i;
         Ticket[strlen(Ticket)]='\0';
         NameUsr[strlen(NameUsr)]='\0';
         newkey[PasswdLength-1]='\0';
       
         strncpy(salt,PasChar,2);

         strcpy(newkey,(char *)crypt(Ticket,salt));

         if(strcmp(PasChar,newkey) == 0) 
         {
            if(Verbose) fprintf(stderr,"User %s is logged in\n",NameUsr);
            Right = DD_id;
         }
         else
         {
            if(Verbose) fprintf(stderr,"for user %s permission denied.\n",NameUsr);
            Right = NOPERMISSION;
         }
      }
   }
   nc_close(UsrRefID);
return((int)Right); 
}

/*       
 *-------------------- SHOW TICKET -------------------------------*/
/* 
 * Description: Library function. Used by DD_Server. It reads the 
 *              content of user_ticket.nc file. Compares the information
 *              with your {id,hostname,DD_id,time}.
 *  If id and hostname(IP_address) and DD_id exists in table of registarated
 *  users, check the working time. If one has any time for work, returns 0,
 *  or a negative value "NOPERMISSION".
 *   It IS DD_Check !
 */
  
int ShowTicket(int userID, char *hostname, char *DD_name)
  {
    char *ddbase;
    char refname[PATHLENGTH];
    int TicketID;    /* Ticket file ID */
    int Comp=1, Time_is=0, Find=0;
    int CurIDID,HostID,TimeID, DD_id_ID;   /* NC variables ID */
    time_t Clock; 
    char CurHostName[MAXHOSTLENGTH];
    int CurID, CurTime, RefTime, DD_id;   /* Variables */
    size_t CurCashStart[2]={0,0};
    size_t HostCount[2] = {1,MAXHOSTLENGTH};
    int ServerReply = NOPERMISSION;
    int DD_user_id = -1;
    int status;   /* Return of any NC call */

   hostname[strlen(hostname)]='\0';
   DD_name[strlen(DD_name)]='\0';
   if((strcmp(DD_name,NONAME))!=0) DD_user_id = CheckID(DD_name);
 
/*------------- OpenUserNC ---------------------------------*/

   if((ddbase = getenv("DDBASE")) == NULL)
   {
      if(Verbose) fprintf(stderr,"SHOWTICKET:  No DDBASE environment.\n");
      return(ENVIRERROR);
   }
   strcpy(refname,ddbase);
   strcat(refname,TICKET_PATH);
   if(Verbose) fprintf(stderr,"SHOWTICKET: TICKET_PATH is %s \n",refname);

   if((status = nc_open(refname, NC_WRITE,&TicketID))  !=  NC_NOERR)
   {
      if(Verbose) fprintf(stderr,"SHOWTICKET:  %s\n", nc_strerror(status));
      return(NOUSERSFILE);
   }

/*------------------Info about source.file-------------------------------*/
   status = nc_inq_varid(TicketID,"id",&CurIDID);
   status = nc_inq_varid(TicketID,"host",&HostID);
   status = nc_inq_varid(TicketID,"time",&TimeID);
   status = nc_inq_varid(TicketID,"DD_id",&DD_id_ID);

   nc_sync(TicketID);
   CashStart[0]=0;

  do
   {
     status = nc_get_var1_int(TicketID, CurIDID, CashStart,&CurID);
     status = nc_get_var1_int(TicketID, DD_id_ID, CashStart,&DD_id);
     status = nc_get_vara_text(TicketID, HostID,CashStart,HostCount,CurHostName);
     status = nc_get_var1_int(TicketID, TimeID,CashStart,&CurTime);
 
     Comp=strcmp(CurHostName,hostname);
     if((strcmp(DD_name,NONAME)) == 0) DD_user_id = DD_id;

     if((CurID == userID) && (Comp == 0) && (DD_id == DD_user_id))
     {
        RefTime = CurTime;
        CurCashStart[0]=CashStart[0];
        Find = 1;
     }

     CashStart[0]++;
   }
   while((CurHostName[0] != '\0') && (CashStart[0] < USERCASHLEN));

   if(Find == 1)
   {
     CashStart[0]=CurCashStart[0];
     status = nc_get_var1_int(TicketID, CurIDID, CashStart,&CurID);
     status = nc_get_var1_int(TicketID, DD_id_ID, CashStart,&DD_id);
     status = nc_get_vara_text(TicketID, HostID,CashStart,HostCount,CurHostName);
     status = nc_get_var1_int(TicketID, TimeID,CashStart,&CurTime);
     if((strcmp(DD_name,NONAME))==0)  DD_user_id = (int)DD_id;
   }
   CashStart[0]--;
      
   Clock=(int)time(NULL);

   if(CurTime > Clock) Time_is = 1;

   if((Find == 1) && (Time_is == 1))
   {
     fprintf(stderr,"User: ID = %d, DD_id = %d, host %s, timework=%d, is connected. \n",CurID,DD_user_id,hostname,Time_is);   
     ServerReply = 0;
   }
   else {

         fprintf(stderr,"User: ID = %d, DD_id = %d, host %s, timework=%d, Permission denied!. \n",CurID,DD_user_id,hostname,Time_is);   
         ServerReply=NOPERMISSION;
         }
  
   nc_sync(TicketID);
   nc_close(TicketID);
   return ServerReply;
}      

/*       
 *------------------------ SETUSER -------------------------------*/
/* 
 * Description: Library function for client-server using.  
 *              Send ID, HostName to server.
 *              Compares the information with your {id,hostname}.
 *              Returns 1 if O'k, or a negative value in case 
 *              of "NOPERMISSION".
 *
 */
int SetUser(int UserID, char *HostName, char *LogName)
{

   int SocketID = -1; /* Global socket id for this communication session */
   static DD_data_t dd = {DD_CHAR,0,NULL,0,NULL};
   static caddr_t buf = NULL;
   static XDR xdrs;
   int cc,i, hostlen, ticketlen, userlen;
   int op = SHOWTICKETREQ;
   int SHOW_ACCESS;
   
   SHOW_ACCESS = NOPERMISSION;
 /*  fprintf(stderr,"after CLIENT int SetUser: SocketID = %d\n",SocketID);
   fprintf(stderr,"after CLIENT int SetUser: User = %d  Host %s\n",UserID,HostName);
 */  

/*------------------CONNECTION to SERVER---------------------------*/
/*
 * If no connection, connect to server, try to order data set and return
 * ID if OK or ErrorNumber if not
 */


 /*  fprintf(stderr,"Try to connect Server\n");*/
/* If there is no connection, try to get it */
   if(SocketID < 0)
       if((SocketID = GetSocket()) < 0) return(NOCONNECTION);
 /*  fprintf(stderr,"Server3 connected\n");*/
   
   
/*---------------------REQUEST TO SERVER-----------------------------*/

/* Check the HostName length */
   if(strlen(HostName) > MAXHOSTLENGTH) hostlen = MAXHOSTLENGTH;
   else hostlen = strlen(HostName);
   if(strlen(LogName) > USRLENGTH) userlen = USRLENGTH;
   else userlen = strlen(LogName);

/* Allocation memory and stream */
   buf = (caddr_t)malloc(REQUESTLENGTH);
   xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);

/* Encoding the request */
   xdr_int(&xdrs,&op);
   xdr_int(&xdrs,&UserID);
   xdr_string(&xdrs, &HostName, hostlen);
   xdr_string(&xdrs, &LogName, userlen);
/* fprintf(stderr,"UserID, HostName  just more : %d %s\n",UserID,HostName);*/
/* Send request */
   if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
   {
      perror("DD_GetTicket:");
      free(buf);
      xdr_destroy(&xdrs);
      return(REQSENDERR);
   }
   free(buf);
   xdr_destroy(&xdrs);

/* Get reply header */

   buf = (caddr_t)malloc(REPLYLENGTH);
   i =0;
   while(((cc = recv(SocketID,buf,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
   if(cc < 0)
   {
      perror("DD_SetUser:");
      free(buf);
      return(REPLYRECERR);
   }

   xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
   xdr_int(&xdrs,&SHOW_ACCESS);

   free(buf);
   xdr_destroy(&xdrs);
   shutdown(SocketID,2);
   close(SocketID);
   /*SocketID = -1;*/
/*  fprintf(stderr,"SetUser,End; UserID, HostName  just more : %d %s\n",UserID,HostName);
  fprintf(stderr,"SHOW_ACCES %d \n",SHOW_ACCESS);
*/ 
 if(Verbose) fprintf(stderr,"SetUser: ACCES = %d \n",SHOW_ACCESS);

return((int)SHOW_ACCESS);
}

/*############################################################################*/

/*-------------------CHECK ID-----------------------*/ 

/* Description: This function check the user name in user_info.nc file. 
 *              Returns DD_id if exists. 
 *              Returns OK, or NOPERMISSION, or an ERROR
 */
      
int CheckID(char *NameUsr)
{
   int i=0,Find=0;
   int DD_id = NOPERMISSION;
  
   int UsrRefID;   /* ID of NC file */
   char *ddbase;
   char refname[PATHLENGTH];
   int UserDimID,UserLenDimID;    /* Dimennsions ID */
   int MemID;                                    /* Variable ID */
   size_t MaxRecords, UserLength;   /* Dimensions */
   size_t start[2]= {0,0};
   size_t UserCount[2] = {1,0};
   char UserDimName[] = "user", UserLenDimName[] = "UserLength";   /*   Dimensions names */
   char MemName[] = "member";                                      /* Variable name */
   char MemChar[USRLENGTH];
   int status;
 
/*------------- Open User Reference file ---------------------------------*/
   if((ddbase = getenv("DDBASE")) == NULL)
   {
      if(Verbose) fprintf(stderr,"CHECKID:  No DDBASE environment.\n");
      return(ENVIRERROR);
   }
   strcpy(refname,ddbase);
   strcat(refname,USERREFNAME);
   if(Verbose) fprintf(stderr," CHECKID: PATH is %s \n",refname);

   if((status = nc_open(refname, NC_WRITE,&UsrRefID))  !=  NC_NOERR)
   {
      if(Verbose) fprintf(stderr,"CHECKID:  %s\n", nc_strerror(status));
      return(NOUSERSFILE);
   }

/*------------------ Define all dimensions -------------------------------*/

   status = nc_inq_dimid(UsrRefID,UserDimName,&UserDimID);
   status = nc_inq_dimlen(UsrRefID, UserDimID,  &MaxRecords);
   status = nc_inq_dimid(UsrRefID,UserLenDimName,&UserLenDimID);
   status = nc_inq_dimlen(UsrRefID, UserLenDimID,  &UserLength);
   UserCount[1] = UserLength;

/*fprintf(stderr,"CheckTicket:Rec: %d %d %d\n",MaxRecords,UserLength,PasswdLength);*/

/*------------------ Define all variables ID -------------------------------*/
   status = nc_inq_varid(UsrRefID, MemName,&MemID);

   while((i<MaxRecords) && (Find != 1))
   {
      start[0]=i;
      status = nc_get_vara_text(UsrRefID, MemID, start,UserCount,MemChar);
      if(strcmp(MemChar,NameUsr)==0) {DD_id = i;Find=1;}
      i++;
   }
   nc_close(UsrRefID);
   return(DD_id);
}

/*----------------Put into LOG file------------------------------------*/

/* Description: This function requires ID of local host, IP-address of 
*               local host and Login Name.      
*
*/

void Put2Log(int UID, char *Host, char *LogName, int error)
  {
    FILE *log;
    time_t Clock; 
    char *ddbase;
    char refname[PATHLENGTH], right[]="   normal    \0",noright[]="no permission\0";

   if((ddbase = getenv("DDBASE")) == NULL)
   {
      fprintf(stderr,"No DDBASE info. Check your profile... \n");
      return;
   }
   strcpy(refname,ddbase);
   strcat(refname,LOGFILE);
   if(Verbose) fprintf(stderr,"LOG file: %s \n",refname);
   if((log = fopen(refname, "a")) == NULL)
   {
      fprintf(stderr,"Error in opening log file. \n");
      return;
   }

   Clock=time(NULL);
   
   if(error>=0) 
   fprintf(log,"%s\t%s\t%u  %s\t%s",Host,LogName,UID,right,ctime(&Clock));
   else fprintf(log,"%s\t%s\t%u  %s\t%s",Host,LogName,UID,noright,ctime(&Clock));

   fclose(log);
 
   return;
 }
   
/*---------------------LOGINS----------------------------------*/

/* Description: This function uses by ddcheck */
/*              Compare two passwords  */
  
int LOGINS(char *password)
{
  char salt[3],newkey[13];
  char pas[80];
  char PName[]="Administrator password: ";
  int ACCESS_ = 0;

/*--------------Password----------------------------*/
  
   strncpy(salt,password,2);
   salt[2]='\0';
   strcpy(pas,getpass(PName));
   pas[strlen(pas)] = '\0';
   strcpy(newkey,(char *)crypt(pas,salt));
   newkey[strlen(newkey)] = '\0';
   if(strcmp(newkey, password) == 0) ACCESS_=1;

   return ACCESS_;
}

/*---------------------DDCHECK----------------------------------*/

/* Description: This function check user for dd administrator access to DD_System
*               Require only dd password from you.
*               Returns 0 if o'k or -1 in case of unaccessability.
*
*/
  
int ddcheck()
{
  int FL=1,coun,i;
  char lines[256],ref[40];
  FILE *files;

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
   files=fopen("/etc/passwd","r");
   while(FL != 0)
   { 
     if(fgets(lines, 255, files) != NULL)
     {
       coun=(strlen(lines)-strlen(strchr(lines,':')));
       strncpy(ref,lines,coun);
       ref[(strlen(lines)-strlen(strchr(lines,':')))]='\0';
       if(strcmp(ref,"dd") == 0)
       {
         coun=(strlen(&(lines[0])+3)-strlen(strchr(&(lines[0])+3,':')));
         strncpy(ref,&lines[0]+3,coun);
         ref[coun]='\0';
         i=LOGINS(ref);
       }

     }
     else FL=0;
   }
  fclose(files);
  if(i == 0) return(-1);
  return(0);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/