[BACK]Return to tcfsaddgroup.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tcfs

Diff for /src/usr.bin/tcfs/Attic/tcfsaddgroup.c between version 1.7 and 1.8

version 1.7, 2000/06/19 21:23:44 version 1.8, 2000/06/19 22:42:28
Line 54 
Line 54 
 };  };
   
 int  int
 tcfsgetuid (char *login)  tcfsgetuid(char *login)
 {  {
         struct passwd *entry;          struct passwd *entry;
   
         setpwent();          setpwent();
   
         while ((entry = getpwent()) != NULL) {          while ((entry = getpwent()) != NULL) {
                 if (strcmp (login, entry->pw_name) == 0)                  if (strcmp(login, entry->pw_name) == 0)
                         return entry->pw_uid;                          return (entry->pw_uid);
         }          }
   
         endpwent();          endpwent();
         free(entry);          free(entry);
   
         return -1;          return (-1);
 }  }
   
 void  void
 gencoeff (void)  gencoeff(void)
 {  {
         int i, j;          int i, j;
   
         for (i = 0; i < KEYSIZE; i++){          for (i = 0; i < KEYSIZE; i++) {
                 for (j = 1; j < threshold; j++){                  for (j = 1; j < threshold; j++) {
                         coeff[j][i] = arc4random();                          coeff[j][i] = arc4random();
                 }                  }
         }          }
 }  }
   
 unsigned char *  unsigned char *
 gengrpkey (char *login)  gengrpkey(char *login)
 {  {
         int x1, i, j, k=0;          int x1, i, j, k=0;
         unsigned int x;          unsigned int x;
Line 95 
Line 95 
   
         res = (unsigned char*)calloc(KEYSIZE + KEYSIZE/8, sizeof(char));          res = (unsigned char*)calloc(KEYSIZE + KEYSIZE/8, sizeof(char));
         if (!res)          if (!res)
                 tcfs_error (ER_MEM, NULL);                  tcfs_error(ER_MEM, NULL);
   
         x1 = tcfsgetuid(login);          x1 = tcfsgetuid(login);
         x = (x1 % 257);          x = (x1 % 257);
   
 #ifdef DEBUG_TCFS  #ifdef DEBUG_TCFS
         printf ("La chiave utente di %u e':\n", x);          printf("La chiave utente di %u e':\n", x);
 #endif  #endif
   
         for (i = 0; i < KEYSIZE; i++) {          for (i = 0; i < KEYSIZE; i++) {
                 tmp = 0;                  tmp = 0;
                 for (j = 1; j < threshold; j++) {                  for (j = 1; j < threshold; j++) {
                         tmp += (eleva(x1,j,257)*coeff[j][i]) % 257;                          tmp += (eleva(x1, j, 257) * coeff[j][i]) % 257;
 #ifdef DEBUG_TCFS  #ifdef DEBUG_TCFS
                         printf ("x1= %u\tj=%d\tcoeff[%d][%d]=%u\ttmp=%u\tchiave: ", x1, j, j, i, coeff[j][i], tmp);                          printf("x1= %u\tj=%d\tcoeff[%d][%d]=%u\ttmp=%u\tchiave: ", x1, j, j, i, coeff[j][i], tmp);
 #endif  #endif
                         }                  }
   
                 tmp += (unsigned int)S[i];                  tmp += (unsigned int)S[i];
                 tmp %= 257;                  tmp %= 257;
   
                 memcpy (res+k++, &tmp, 1);                  memcpy(res+k++, &tmp, 1);
 #ifdef DEBUG_TCFS  #ifdef DEBUG_TCFS
                 printf ("%u\n", *(res+k-1));                  printf("%u\n", *(res+k-1));
 #endif  #endif
                 switch (i % 8){                  switch (i % 8) {
                         case 0:                  case 0:
                                 obits.bf.b1=tmp>>8;                          obits.bf.b1=tmp>>8;
                                 break;                          break;
                         case 1:                  case 1:
                                 obits.bf.b2=tmp>>8;                          obits.bf.b2=tmp>>8;
                                 break;                          break;
                         case 2:                  case 2:
                                 obits.bf.b3=tmp>>8;                          obits.bf.b3=tmp>>8;
                                 break;                          break;
                         case 3:                  case 3:
                                 obits.bf.b4=tmp>>8;                          obits.bf.b4=tmp>>8;
                                 break;                          break;
                         case 4:                  case 4:
                                 obits.bf.b5=tmp>>8;                          obits.bf.b5=tmp>>8;
                                 break;                          break;
                         case 5:                  case 5:
                                 obits.bf.b6=tmp>>8;                          obits.bf.b6=tmp>>8;
                                 break;                          break;
                         case 6:                  case 6:
                                 obits.bf.b7=tmp>>8;                          obits.bf.b7=tmp>>8;
                                 break;                          break;
                         case 7:                  case 7:
                                 obits.bf.b8=tmp>>8;                          obits.bf.b8=tmp>>8;
                                 break;                          break;
                 }                  }
   
                 if ((i%8) == 7) {                  if ((i % 8) == 7) {
                         res[k] = obits.byte;                          res[k] = obits.byte;
                         k++;                          k++;
   
 #ifdef DEBUG_TCFS  #ifdef DEBUG_TCFS
                         printf ("%u\n", res[k-1]);                          printf("%u\n", res[k-1]);
 #endif  #endif
   
                         obits.byte=0;                          obits.byte = 0;
                 }                  }
         }          }
   
         /*          /*
         res[KEYSIZE]=obits.byte;          res[KEYSIZE]=obits.byte;
         */          */
         return res;          return (res);
 }  }
   
 int  int
 addgroup_main (int argn, char *argv[])  addgroup_main(int argn, char *argv[])
 {  {
         int val;          int val;
         gid_t gid;          gid_t gid;
Line 177 
Line 178 
         /*          /*
          * Going to check the arguments           * Going to check the arguments
          */           */
         while ((val = getopt (argn, argv, "vg:m:t:h")) != EOF)          while ((val = getopt(argn, argv, "vg:m:t:h")) != EOF)
                 switch (val) {                  switch (val) {
                 case 'm':                  case 'm':
                         members = atoi(optarg);                          members = atoi(optarg);
Line 190 
Line 191 
   
                                 group_id = getgrnam(optarg);                                  group_id = getgrnam(optarg);
                                 if (!group_id)                                  if (!group_id)
                                         tcfs_error (ER_CUSTOM, "Nonexistent group.");                                          tcfs_error(ER_CUSTOM, "Nonexistent group.");
   
                                 gid=group_id->gr_gid;                                  gid = group_id->gr_gid;
                         }                          }
   
                         have_gid = TRUE;                          have_gid = TRUE;
Line 202 
Line 203 
                         have_threshold = TRUE;                          have_threshold = TRUE;
                         break;                          break;
                 case 'h':                  case 'h':
                         show_usage (addgroup_usage, argv[0]);                          show_usage(addgroup_usage, argv[0]);
                         exit (OK);                          exit(OK);
                 case 'v':                  case 'v':
                         be_verbose = TRUE;                          be_verbose = TRUE;
                         break;                          break;
                 default:                  default:
                         fprintf (stderr, "Try %s --help for more information.\n", argv[0]);                          fprintf(stderr, "Try %s --help for more information.\n", argv[0]);
                         exit (ER_UNKOPT);                          exit(ER_UNKOPT);
                 }                  }
   
         if (argn-optind)          if (argn-optind)
                 tcfs_error (ER_UNKOPT, NULL);                  tcfs_error(ER_UNKOPT, NULL);
   
         if (!have_gid) {          if (!have_gid) {
                 char *buff = NULL;                  char *buff = NULL;
Line 221 
Line 222 
   
                 buff = (char*)calloc(2048, sizeof(char));                  buff = (char*)calloc(2048, sizeof(char));
                 if (!buff)                  if (!buff)
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 printf ("Group id [or name] of the TCFS group to add to the database: ");                  printf("Group id [or name] of the TCFS group to add to the database: ");
                 fgets (buff, 2048, stdin);                  fgets(buff, 2048, stdin);
                 len = strlen(buff) - 1;                  len = strlen(buff) - 1;
                 buff[len] = buff[len] == '\n' ? 0 : buff[len];                  buff[len] = buff[len] == '\n' ? 0 : buff[len];
                 gid = atoi(buff);                  gid = atoi(buff);
Line 234 
Line 235 
   
                         group_id = getgrnam(buff);                          group_id = getgrnam(buff);
                         if (!group_id)                          if (!group_id)
                                 tcfs_error (ER_CUSTOM, "Nonexistent group.");                                  tcfs_error(ER_CUSTOM, "Nonexistent group.");
   
                         gid = group_id->gr_gid;                          gid = group_id->gr_gid;
                 }                  }
   
                 if (gid <= 0)                  if (gid <= 0)
                         tcfs_error (ER_CUSTOM, "A positive ID please!");                          tcfs_error(ER_CUSTOM, "A positive ID please!");
   
                 free (buff);                  free(buff);
         }          }
   
         if (!have_members) {          if (!have_members) {
                 char *buff = NULL;                  char *buff = NULL;
                 int len;                  int len;
   
                 buff=(char*)calloc(2048, sizeof(char));                  buff = (char*)calloc(2048, sizeof(char));
                 if (!buff)                  if (!buff)
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 printf ("Number of members for the TCFS group ID #%d: ", gid);                  printf("Number of members for the TCFS group ID #%d: ", gid);
                 fgets (buff, 2048, stdin);                  fgets(buff, 2048, stdin);
                 len = strlen(buff) - 1;                  len = strlen(buff) - 1;
                 buff[len] = buff[len] == '\n' ? 0 : buff[len];                  buff[len] = buff[len] == '\n' ? 0 : buff[len];
                 members = atoi(buff);                  members = atoi(buff);
Line 268 
Line 269 
   
                 buff = (char*)calloc(2048, sizeof(char));                  buff = (char*)calloc(2048, sizeof(char));
                 if (!buff)                  if (!buff)
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 printf ("Threshold for the TCFS group ID #%d: ", gid);                  printf("Threshold for the TCFS group ID #%d: ", gid);
                 fgets (buff, 2048, stdin);                  fgets(buff, 2048, stdin);
                 len = strlen(buff) - 1;                  len = strlen(buff) - 1;
                 buff[len] = buff[len] == '\n' ? 0 : buff[len];                  buff[len] = buff[len] == '\n' ? 0 : buff[len];
                 threshold = atoi(buff);                  threshold = atoi(buff);
   
                 free (buff);                  free(buff);
         }          }
   
         if (members < 2)          if (members < 2)
                 tcfs_error (ER_CUSTOM, "At least two members!");                  tcfs_error(ER_CUSTOM, "At least two members!");
   
         if (threshold > members || threshold <= 0)          if (threshold > members || threshold <= 0)
                 tcfs_error (ER_CUSTOM, "The threshold must be no greater than the number of members and greater than zero!");                  tcfs_error(ER_CUSTOM, "The threshold must be no greater than the number of members and greater than zero!");
   
         S = gentcfskey();          S = gentcfskey();
 #ifdef DEBUG_TCFS  #ifdef DEBUG_TCFS
         {          {
                 int i;                  int i;
   
                 printf ("La chiave segreta e':\n");                  printf("La chiave segreta e':\n");
   
                 for (i=0;i<KEYSIZE;i++)                  for (i = 0; i < KEYSIZE; i++)
                         printf ("%u:", S[i]);                          printf("%u:", S[i]);
   
                 printf ("\n");                  printf("\n");
         }          }
 #endif  #endif
   
Line 320 
Line 321 
                 group_info[members-1]->n = members;                  group_info[members-1]->n = members;
                 group_info[members-1]->soglia = threshold;                  group_info[members-1]->soglia = threshold;
   
                 if (!unix_auth (&user, &passwd, FALSE)) {                  if (!unix_auth(&user, &passwd, FALSE)) {
                         fprintf (stderr, "Invalid password or the user does not exist.\n");                          fprintf(stderr, "Invalid password or the user does not exist.\n");
                         continue;                          continue;
                 }                  }
   
                 if (tcfs_ggetpwnam (user, gid, &tmp))                  if (tcfs_ggetpwnam(user, gid, &tmp))
                         tcfs_error(ER_CUSTOM, "Group already exists.");                          tcfs_error(ER_CUSTOM, "Group already exists.");
   
                 while (tmpmemb > members) {                  while (tmpmemb > members) {
                         if (!strcmp (user, group_info[tmpmemb-1]->user)) {                          if (!strcmp(user, group_info[tmpmemb-1]->user)) {
                                 fprintf (stderr, "User already present into the group.\n");                                  fprintf(stderr, "User already present into the group.\n");
                                 cont = 1;                                  cont = 1;
                                 break;                                  break;
                         }                          }
Line 340 
Line 341 
                 if (cont)                  if (cont)
                         continue;                          continue;
   
                 strcpy (group_info[members-1]->user, user);                  strcpy(group_info[members-1]->user, user);
   
                 newkey = (unsigned char*)calloc(GKEYSIZE + 1, sizeof (char));                  newkey = (unsigned char*)calloc(GKEYSIZE + 1, sizeof(char));
                 if (!newkey)                  if (!newkey)
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 cryptedkey = (unsigned char*)calloc(UUGKEYSIZE, sizeof(char));                  cryptedkey = (unsigned char*)calloc(UUGKEYSIZE, sizeof(char));
                 if (!cryptedkey)                  if (!cryptedkey)
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 memcpy (newkey, gengrpkey (user), GKEYSIZE);                  memcpy(newkey, gengrpkey(user), GKEYSIZE);
                 newkey[GKEYSIZE] = '\0';                  newkey[GKEYSIZE] = '\0';
   
                 /*                  /*
                  * Encrypt the just generated key with the user password                   * Encrypt the just generated key with the user password
                  */                   */
                 if (!tcfs_encrypt_key (passwd, newkey, GKEYSIZE,                  if (!tcfs_encrypt_key(passwd, newkey, GKEYSIZE, cryptedkey,
                     cryptedkey, UUGKEYSIZE))                      UUGKEYSIZE))
                         tcfs_error (ER_MEM, NULL);                          tcfs_error(ER_MEM, NULL);
   
                 free (newkey);                  free(newkey);
   
                 strlcpy (group_info[members - 1]->gkey, cryptedkey,                  strlcpy(group_info[members - 1]->gkey, cryptedkey,
                          GKEYSIZE + 1);                      GKEYSIZE + 1);
                 free (cryptedkey);                  free(cryptedkey);
   
                 members--;                  members--;
         }          }
Line 373 
Line 374 
   
         while (members) {          while (members) {
                 if (be_verbose)                  if (be_verbose)
                         printf ("Creating a new entry for group %d and user %s in the TCFS database...\n",                          printf("Creating a new entry for group %d and user %s in the TCFS database...\n",
                                 group_info[members-1]->gid,                                  group_info[members-1]->gid,
                                 group_info[members-1]->user);                                  group_info[members-1]->user);
   
                 if (!tcfs_gputpwnam (group_info[members-1]->user,                  if (!tcfs_gputpwnam(group_info[members-1]->user,
                                      group_info[members-1], U_NEW)) {                                       group_info[members-1], U_NEW)) {
                                 /* TODO: Remove the group entries saved before */                                  /* TODO: Remove the group entries saved before */
                         tcfs_error (ER_CUSTOM, "Error: cannot add a user to the group.");                          tcfs_error(ER_CUSTOM, "Error: cannot add a user to the group.");
                 }                  }
   
                 if (be_verbose)                  if (be_verbose)
                         printf ("TCFS group entry for user %s created.\n", group_info[members-1]->user);                          printf("TCFS group entry for user %s created.\n", group_info[members-1]->user);
   
                 members--;                  members--;
         }          }
   
         tcfs_error (ER_CUSTOM, "\nAll group keys generated.");          tcfs_error(ER_CUSTOM, "\nAll group keys generated.");
   
         return 0;          return (0);
 }  }
   
   
Line 399 
Line 400 
 eleva(int x, int y, int z)  eleva(int x, int y, int z)
 {  {
         int mask = 0x80000000;          int mask = 0x80000000;
         int res = 1,i;          int res = 1, i;
   
         for (i = 0; i < 32; i++) {          for (i = 0; i < 32; i++) {
                 res = (res*res)%z;                  res = (res * res) % z;
                 if (y & mask)                  if (y & mask)
                         res = (x*res)%z;                          res = (x * res) % z;
                 mask = mask >> 1;                  mask = mask >> 1;
         }          }
   
         return res;          return (res);
 }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8