File: license.c

package info (click to toggle)
dx 1%3A4.4.4-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 55,184 kB
  • ctags: 54,432
  • sloc: ansic: 365,478; cpp: 156,582; sh: 10,872; java: 10,641; makefile: 2,291; awk: 444; yacc: 327
file content (684 lines) | stat: -rw-r--r-- 16,171 bytes parent folder | download | duplicates (7)
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/***********************************************************************/
/* Open Visualization Data Explorer                                    */
/* (C) Copyright IBM Corp. 1989,1999                                   */
/* ALL RIGHTS RESERVED                                                 */
/* This code licensed under the                                        */
/*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
/***********************************************************************/

#include <dxconfig.h>


/* this has to be before the ifdef, because it defines DXD_LICENSED_VERSION 
 * if this arch supports the license manager.
 */
#include <dx/dx.h>

#if defined(HAVE_CRYPT_H)
#include <crypt.h>
#endif

#if defined(HAVE_TIME_H)
#include <time.h>
#endif

#include <stdio.h>

#if defined(HAVE_STRING_H)
#include <string.h>
#define DXD_STLIB_INCLUDES
#endif

#if defined(HAVE_TIME_H)
#include <time.h>
#endif

#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#endif

#if defined(HAVE_STRING_H)
#include <string.h>
#endif

#if defined(HAVE_SYS_STAT_H)
#include <sys/stat.h>
#endif

#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#endif

#if defined(HAVE_SYS_TIMEB_H)
#include <sys/timeb.h>
#endif

#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif

#include "config.h"

#ifdef DXD_LICENSED_VERSION
#include "license.h"

#define NL_LIC  1
#define CON_LIC 2

static int shadow_fd_dxlic = -1;
static int shadow_fd_mplic = -1;
static int trial_file_exists  = FALSE; 
static int MPlic_tried	   = FALSE;
struct lic_info {
    int child;
    int type;
};
struct lic_info dxlic = {-1, -1};
struct lic_info mplic = {-1, -1};

static Error system_hostname(char *buf);
static Error checkexp(char *root, lictype ltype);
static char messagebuf[1024];

int _dxfCheckLicenseChild(int child)
{
    if ( child == dxlic.child) {
      if(dxlic.type == NL_LIC)
          return(0);
      return(child);
    }
    else if ( child == mplic.child) {
      if(mplic.type == NL_LIC)
          return(0);
      return(child);
    }
    else
        return(-1);
}

Error ExLicenseDied(int fd, Pointer junk)
{
    /* We should only be called if dxshadow dies,  */
    /* then send UNAUTHORIZED MESSAGE to UI        */
    /* or die if were in script mode 		 */
    

    DXRegisterInputHandler(NULL, fd, NULL);

    if ( fd == shadow_fd_dxlic)
      DXMessage("License Error: Exec lost DX license and is terminating\n");
    else if ( fd == shadow_fd_mplic)
      DXMessage("License Error: Exec lost MP license and is terminating\n");
    else
      DXMessage("License Error: unknown license error\n");

    _dxd_ExHasLicense = FALSE;
    if (_dxd_exRemote) {
	DXUIMessage("LICENSE","UNAUTHORIZED");
	return OK;
    }
    
    if(_dxd_exTerminating) {
        *_dxd_exTerminating = TRUE;
        return (-1);
    }
    /* when _dxd_exTerminating is NULL we are being called from the */
    /* developer's kit, just exit */
    exit(-1);
    
    /* NOTREACHED */
}

void _dxf_ExReleaseLicense()
{
    if(shadow_fd_dxlic >= 0)
        close(shadow_fd_dxlic);
    if(shadow_fd_mplic >= 0)
        close(shadow_fd_mplic);
}


Error ExGetLicense(lictype ltype, int forceNetLS)
{
    int i;
    int child;
    int pid;
    int in[2],out[2];
    char remname[256];
    char auth_msg[AUTH_MSG_LEN];
    char ckey[9];
    char c_buf[32],p_buf[32];	/* hold crypted msgs for comaparison */
    static char envbuf[32];	/* static 'cause it goes into the env */
    char salt[3];
    time_t timenow;
   
 
    char *root = getenv ("DXEXECROOT");
    if (root == NULL) 
	root = getenv ("DXROOT");
    if (root == NULL)
	root = "/usr/share/dx";

    if (ltype == MPLIC) {
	MPlic_tried = TRUE;
	forceNetLS = FALSE;
    }
    

    strcpy(messagebuf,"");

    if (checkexp (root, ltype))    /* check for an old syle trial license */
	return OK;
    else if ( ltype == MPLIC && trial_file_exists )  /* don't try to get an MP floating lic if DX lic */ 
	return ERROR;				/* is a uniprocessor nodelocked */  
	
    /* didn't find a trial license so spawn the NetLS licensing process */
    
    /* Set up two pipes */
    if (pipe(in) < 0) {
	perror("pipe(in)");
	exit (-1);
    }
    if (pipe(out) < 0) {
	perror("pipe(out)");
	exit (-1);
    }
    
    timenow = time(NULL);
    
    sprintf(envbuf,"_DX_%d=", getpid());
    sprintf(c_buf,"%x", timenow);
    strcat(envbuf, c_buf+4);
    putenv(envbuf);
   
#if DXD_HAS_VFORK
    /* if it's available, use vfork() so we don't copy the entire data
     * segment when forking - we are going to exec right away
     * anyway so we aren't going to use anything from the data seg.
     */
    pid = vfork();
#else
    pid = fork();
#endif
    child = 0xFFFF & pid;       /* force "child id" to be < 4 hex chars */
	
    if (pid == 0) {	/* Child */
	char arg1[32], arg3[32];
		
	if (in[1])
	    close (in[1]);
	if (out[0])
	    close (out[0]);
	if (dup2(in[0], 0) < 0) {
	    perror("dup2");
	    exit (-1);
	}
	if (dup2(out[1], 1) < 0) {
	    perror("dup2");
	    exit (-1);
	}
	
	/* should we close other file descriptors here ? */
	
	if (getenv("DXSHADOW")) 
	    strcpy(remname,getenv("DXSHADOW"));
	else 
	    sprintf(remname,"%s/bin_%s/dxshadow",root,DXD_ARCHNAME);

	switch (ltype) {
	    case MPLIC:	strcpy(arg1,"-mp");	break;
	    case DXLIC:	strcpy(arg1,"-dev");	break;
	    case RTLIC:	strcpy(arg1,"-rt");	break;
	    case RTLIBLIC:	strcpy(arg1,"-rtlib");	break;
	    default:    sprintf(arg1,"%d",ltype); break;
	}
	if (forceNetLS)
	    strcat(arg1,"only");

	sprintf(arg3,"%d.%d.%d", DXD_VERSION, DXD_RELEASE, DXD_MODIFICATION);
	execlp(remname, "dxshadow", arg1 , "-version", arg3 ,NULL);

	perror("Could not execute license process");
	exit (-1);
    }
    else if (pid > 0) {   /* parent */
 	int netls_type;
	const char *lic_name;
	if (in[0]) 
	    close (in[0]);
	if (out[1]) 
	    close (out[1]);

	/* wait for response from the child */
	i = read (out[0], auth_msg, AUTH_MSG_LEN);
	if (i != AUTH_MSG_LEN) {
	    perror ("License Error:Bad message read from dxshadow");
	    exit (-1);
	}
	
        if (ltype == MPLIC) 
            mplic.child = child;
        else
            dxlic.child = child;

	/* decipher license message here */
	
	child = (child < 4096) ? (child+4096)
	                       : (child);       /* forces to be 4 0x chars */
	
	strcpy(ckey, c_buf+4);
	sprintf(ckey+4, "%x", child);
	
	salt[0] = '7';
	salt[1] = 'q';
	salt[2] = '\0';
	
	strcpy(p_buf, crypt(ckey, salt));;
	
	for(i=0;i<13;i++)
	    c_buf[i] = auth_msg[(i*29)+5];
	c_buf[13] = '\0';
	
	if (strcmp(c_buf, p_buf)) {
	    /* Bad message from child */
	    perror("License Error: invalid message from license process.");
	    exit (-1);
	}

	/* valid message so we extract license type */
	for(i=0; i<8; i++)
	    c_buf[i] = auth_msg[(i*3)+37];
	
	c_buf[8] = '\0';
	sscanf(c_buf, "%x", &i);
	netls_type = 0xffff & (i^child);
	i = i >> 16;
	ltype = 0xffff & (i^child);
#if LIC_DEBUG 
	fprintf(stderr,"Received...\n"
		"c_buf = '%s', ltype = 0x%x, netls_type = 0x%x, mask = 0x%x\n",
				c_buf,ltype,netls_type,child);
#endif

   	switch (ltype) {
	    case DXLIC: lic_name = "DX development"; break;
	    case RTLIC: lic_name = "DX run-time"; break;
	    case MPLIC: lic_name = "MP"; break;
	    case RTLIBLIC: lic_name = "DX Library run-time"; break;
	    default: return FALSE; break;	/* bad license */ 
  	}

	switch (netls_type) {
	    
	  case GOT_NODELOCKED:
	   
	    if (ltype == MPLIC) {
		sprintf(messagebuf,"Exec got nodelocked %s license.",lic_name);
                mplic.type = NL_LIC;
	    } else {
		DXMessage("Exec got nodelocked %s license.", lic_name);
                dxlic.type = NL_LIC;
		_dxd_ExHasLicense = TRUE;
	    }
	    return OK;
		
	  case GOT_CONCURRENT:
	    /* do concurrent stuff here */
        
	    if (ltype == MPLIC) {
	        shadow_fd_mplic = out[0];
		sprintf(messagebuf,"Exec got concurrent use %s license.",
							lic_name);	
		/* we save message and will register input handler 
		 * in ExLicenseFinish 
		 * after it's safe to call DX routines 
		 */
                mplic.type = CON_LIC;
	    } else {
		DXMessage("Exec got concurrent use %s license.",lic_name);
	        shadow_fd_dxlic = out[0];
		DXRegisterInputHandler(ExLicenseDied,shadow_fd_dxlic,NULL);
                dxlic.type = CON_LIC;
		_dxd_ExHasLicense = TRUE;
   	    }

	    return OK;
	    
	  case GOT_NONE:
	    
	    if (ltype == MPLIC)
	      sprintf(messagebuf,"Exec could not get a %s license,"
				" running in uniprocessor mode.",lic_name);
            else 
	      DXMessage("Exec could not get a %s license.",lic_name);
	    return ERROR;
	    
	  default:
	    /* invalid license type */
	    perror("License Error: Invalid message from license process.");
	    exit (-1);
	}
    }
    else {
	perror("fork");
	exit (-1);
    }

    /* NOTREACHED */
}  


/* finish the stuff that needs to happen after DXinit */
/* this is because GetLicense(MPLIC) is called before */
/* DXinit					      */

Error ExLicenseFinish() 
{

	if(!MPlic_tried)
	  return OK;
	
	if (shadow_fd_mplic >= 0)
	  DXRegisterInputHandler(ExLicenseDied,shadow_fd_mplic,NULL);

	if(_dxd_exRemoteSlave)
	  DXMessage("Host %s:%s",_dxd_exHostName,messagebuf);
	else
	  DXMessage(messagebuf);

	return OK;
}

Error ExGetPrimaryLicense()
{
    lictype lic;
    int force;

    if (_dxd_exForcedLicense == NOLIC) {
	lic = DXLIC;
	force = FALSE;
    } else {
	lic = _dxd_exForcedLicense;
	force = TRUE;
    }
    return ExGetLicense(lic,force);
}


static Error checkexp(char *root, lictype ltype)
{
    char   key[KEY_LEN];
    char   cryptHost[HOST_LEN];
    char   cryptTime[HOST_LEN];
    char   host[HOST_LEN];
    time_t timeOut;
    int    i;
    char  *myCryptHost;
    int    host_match;
    time_t timenow;
    char   fileName[HOST_LEN];
    char  *key_location, key_location_buf[1024];
    char  *cp;
    int    suppresslicmsg = 0;
    FILE   *f;
    
    for (i = 0; i < sizeof(key); ++i)
	key[i] = '\0';
    
    if (!system_hostname(host))
	return ERROR;

    timenow = time(NULL);
    
    /* if the DXSTARTUP env var is set to one, we are running
     * with a temp oneday trial key created by the startup ui.
     * suppress the messages about a trial key or when it expires
     * so the user can't tell how we are doing this.
     */
    cp = getenv("DXSTARTUP");
    if (cp && (atoi(cp) == 1))
	suppresslicmsg++;

  if (getenv("DXTRIALKEY")) {
        char *k = getenv("DXTRIALKEY");
	key_location = "DXTRIALKEY environment variable";
        strncpy(key,k,27);
        key[27] = '\0'; /* Make sure it is terminated */
  } else {
      char *fname;
      fname = getenv("DXTRIALKEYFILE");
      if (!fname) {
          sprintf(fileName, "%s/expiration", root);
          fname = fileName;
      }
      f = fopen(fname, "r");
      if (f)  {
	sprintf(key_location_buf,"file %s",fname);	
	key_location = key_location_buf;
        fgets(key, 27, f);
        fclose(f);
      } else {
        return ERROR;
      }
  }

    if (!trial_file_exists) {
        if (ltype == MPLIC) {
	  char buf[1024];
	  sprintf(buf,"Found Data Explorer trial password in %s.\n",
							key_location);
	  if (!suppresslicmsg)
	      strcat(messagebuf,buf);
	  trial_file_exists = TRUE;
	} else {
	    if (!suppresslicmsg)
	       DXMessage("Found Data Explorer trial password in %s.",key_location);
	}
    }
    
    
    if (strlen(key) != 26) {
        if (ltype == MPLIC) 
	  strcat(messagebuf,"You are running an expired Trial version of Data Explorer.\n");
	else
	  DXMessage("You are running an expired Trial version of Data Explorer.");

	return ERROR;
    }
    
    for (i = 0; i < 13; ++i) {
	cryptHost[i] = key[2 * i];
	cryptTime[i] = key[2 * i + 1];
    }
    cryptHost[i] = key[2 * i] = '\0';
    cryptTime[i] = key[2 * i + 1] = '\0';
    


    if (ltype == MPLIC) {
    	if (cryptTime[0] != 'A' ||
	    cryptTime[10] != '9' ||
	    cryptTime[12] != 'D') {
	    strcat(messagebuf,"You are running an Expired trial version of Data Explorer.\n");
	    return ERROR;
    	}
    	if (cryptTime[1] != 'm' ||
	    cryptTime[11] != 'l' ) { 
	    strcat(messagebuf,"Single processor trial key found, MP will not be enabled.\n");
	    return ERROR;
	}
    }
    else if (cryptTime[0] != 'A' || 
	cryptTime[10] != '9' || 
	cryptTime[12] != 'D') {
	DXMessage("You are running an Expired trial version of Data Explorer.");
	return ERROR;
    }
    
    myCryptHost = crypt(host, KEY1);
    host_match = strcmp(cryptHost, myCryptHost) == 0;
    if (!host_match) {
        myCryptHost = crypt(ANYWHERE_HOSTID, KEY1);
        host_match = strcmp(cryptHost, myCryptHost) == 0;
    }
    if (!host_match) {
        if (ltype == MPLIC)
	  strcat(messagebuf,
		"you are running a trial version of Data Explorer"
		" on an unlicensed host\n");
	else
	  DXMessage("You are running a trial version of Data Explorer"
		" on an unlicensed host.");
	return ERROR;

    }
    
    if(cryptTime[1]=='s')
      sscanf(cryptTime, "As%08x95D", &timeOut); 
    else if (cryptTime[1]=='m')
      sscanf(cryptTime, "Am%08x9lD", &timeOut);
      

    timeOut ^= 0x12345678;
    
    if (timenow > timeOut) {
        if (ltype == MPLIC) { 
	  strcat(messagebuf,
	    "You are running an expired trial version of Data Explorer.\n");
	} else {
	  DXMessage("You are running an expired trial version of Data "
			"Explorer.");
    	  DXMessage("This trial key expired on %s", ctime(&timeOut));
	}
	return ERROR;
    }
    
    if (ltype != MPLIC) {
	if (!suppresslicmsg)
	    DXMessage("This trial key expires on %s", ctime(&timeOut));
        _dxd_ExHasLicense = TRUE;
    }

    return OK;
}




/* system specific way to return the hostname we use for the license
 */

#if defined(alphax)
#include <stdio.h>              /* standard I/O */
#include <errno.h>              /* error numbers */
#endif

#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>          /* ioctls */
#endif

#if defined(HAVE_NET_IF_H)
#include <net/if.h>             /* generic interface structures */
#endif

#if defined(HAVE_SYS_SYSTEMINFO_H)
#include <sys/systeminfo.h>     /* maybe someday this will be implemented ...arg! */
#endif


#if defined(aviion) || defined(solaris) 
#include <sys/systeminfo.h>
#endif
#if defined(ibm6000) || defined(hp700)
#include <sys/utsname.h>
#endif

static Error system_hostname(char *host)
{

#if defined(ibm6000) || defined(hp700)
    struct utsname name;
#endif
#if defined(indigo) || defined(sun4) ||  defined(sgi) 
    long   name;
#endif

#if ibm6000
#define FOUND_ID 1
    uname(&name);
    name.machine[10] = '\0';
    strcpy(host, name.machine+2);
#endif
#if hp700
#define FOUND_ID 1
    uname(&name);
    name.idnumber[10] = '\0';
    strcpy(host, name.idnumber+2);
#endif
#if indigo || sgi        /* sgi does not like #if...#elif..#endif construct */
#define FOUND_ID 1
    name = sysid(NULL);
    sprintf(host, "%d", name);
    strcpy(host, host+2);
#endif
#if sun4 
#define FOUND_ID 1
    name = gethostid();
    sprintf(host, "%x", name);
#endif
#if aviion
#define FOUND_ID 1
    sysinfo(SI_HW_SERIAL,host,301);
#endif
#if solaris
#define FOUND_ID 1
    sysinfo(SI_HW_SERIAL,host,301);
    sprintf(host, "%x", atol(host));
#endif
#if alphax
#define FOUND_ID 1
    {
    char *device;
    char *dflt_devices[] = {"tu0","ln0", NULL };
    int s,i				/* On Alpha OSF/1 we use ethernet */;

    /* Get a socket */
    s = socket(AF_INET,SOCK_DGRAM,0);
    strcpy(host,"");
    if (s < 0) {
        perror("socket");
    } else {
	for (i=0, device=(char*)getenv("DXKEYDEVICE"); dflt_devices[i]; i++) {
	    static   struct  ifdevea  devea;  /* MAC address from and ioctl()*/
	    char *dev, buf[32];
	    if (!device) 
		dev = dflt_devices[i];
	    else
		dev = device;
	    strcpy(devea.ifr_name,dev);
	    if (ioctl(s,SIOCRPHYSADDR,&devea) >= 0)  {
		strcpy(host,"");
		for (i = 2; i < 6; i++){
		    sprintf(buf,"%x", devea.default_pa[i] );
		    strcat(host,buf);
		}
		break;
	    } 
	    if (device) break;
	}
	close(s);
    }
  }
#endif

#if !defined(FOUND_ID)
    /* Trial version not supported on this architecture */
    return ERROR;
#else
# undef FOUND_ID
#endif
    
    return OK;
}

#endif /* DXD_LICENSED_VERSION */