Changeset 1256


Ignore:
Timestamp:
01/16/10 19:00:23 (11 years ago)
Author:
Admin
Message:

Add cs_strncpy as a replacement for strncpy. This function always terminates the string and works pretty much like strlcpy but without return value.

Location:
branches/monitor-improvement
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/monitor-improvement/globals.h

    r1240 r1256  
    692692extern int safe_overwrite_with_bak(char *destfile, char *tmpfile, char *bakfile, int forceBakOverWrite);
    693693extern void fprintf_conf(FILE *f, int varnameWidth, const char *varname, const char *fmtstring, ...);
     694extern void cs_strncpy(char * destination, const char * source, size_t num);
    694695
    695696// oscam variables
  • branches/monitor-improvement/module-cccam.c

    r1228 r1256  
    847847  cs_debug("cccam: 'CCcam' xor");
    848848  memcpy(buf, "CCcam", 5);
    849   strncpy(pwd, reader[ridx].r_pwd, 63);
     849  cs_strncpy(pwd, reader[ridx].r_pwd, sizeof(reader[ridx].r_pwd));
    850850  cc_crypt(&cc->block[ENCRYPT], (uint8 *)pwd, strlen(pwd), ENCRYPT);
    851851  cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
  • branches/monitor-improvement/module-dvbapi.c

    r1189 r1256  
    10581058
    10591059    if (cfg->dvbapi_boxtype[0]==0) {
    1060         strncpy(cfg->dvbapi_boxtype, "dreambox", sizeof(cfg->dvbapi_boxtype)-1);
     1060        cs_strncpy(cfg->dvbapi_boxtype, "dreambox", sizeof(cfg->dvbapi_boxtype));
    10611061        cs_log("dvbapi: boxtype not set. Assume boxtype=%s.", cfg->dvbapi_boxtype);
    10621062    } else
  • branches/monitor-improvement/module-monitor.c

    r1232 r1256  
    205205  {
    206206    if (btxt[0]) monitor_send(btxt);
    207     strncpy(btxt, txt, sizeof(btxt));
     207    cs_strncpy(btxt, txt, sizeof(btxt));
    208208    return;
    209209  }
     
    213213    monitor_send(btxt);
    214214    txt[2]='E';
    215     strncpy(btxt, txt, sizeof(btxt));
     215    cs_strncpy(btxt, txt, sizeof(btxt));
    216216  }
    217217  else
    218218  {
    219219    if (txt)
    220       strncpy(btxt, txt, sizeof(btxt));
     220      cs_strncpy(btxt, txt, sizeof(btxt));
    221221    btxt[2]=(btxt[2]=='B') ? 'S' : 'E';
    222222  }
     
    247247            for (i=0; i<this->ncaid; i++)
    248248                if (this->caid[i] == caid)
    249                     strncpy(name, this->name, 32);
     249                    cs_strncpy(name, this->name, 32);
    250250
    251251    if (!name[0]) sprintf(name, "[%04X:%04X]", caid, srvid);
     
    590590    strtolower(argarray[0]);
    591591
    592     found = 0;
    593592    for (i = 0; i < 14; i++)
    594         if (!strcmp(argarray[0], token[i])) found = 1;
    595 
    596     if (found != 1){
     593        if (!strcmp(argarray[0], token[i])) break;
     594
     595    if (i < 14){
    597596        chk_t_global(token[i],argarray[1]);
    598597        sprintf(buf, "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
  • branches/monitor-improvement/module-serial.c

    r1117 r1256  
    191191      oscam_ser_baud=B9600;
    192192  }
    193   strncpy(oscam_ser_usr, usr, sizeof(oscam_ser_usr)-1);
    194   strncpy(oscam_ser_device, dev, sizeof(oscam_ser_device)-1);
     193  cs_strncpy(oscam_ser_usr, usr, sizeof(oscam_ser_usr));
     194  cs_strncpy(oscam_ser_device, dev, sizeof(oscam_ser_device));
    195195  return(oscam_ser_baud);
    196196}
  • branches/monitor-improvement/oscam-config.c

    r1250 r1256  
    317317{
    318318  if (!strcmp(token, "serverip")) { cfg->srvip=inet_addr(value); return; }
    319   if (!strcmp(token, "logfile")) { strncpy(logfile, value, sizeof(logfile)-1); return; }
    320   if (!strcmp(token, "pidfile")) { strncpy(cfg->pidfile, value, sizeof(cfg->pidfile)-1); return; }
    321   if (!strcmp(token, "usrfile")) { strncpy(cfg->usrfile, value, sizeof(cfg->usrfile)-1); return; }
    322   if (!strcmp(token, "cwlogdir")) { strncpy(cfg->cwlogdir, value, sizeof(cfg->cwlogdir)-1); return; }
     319  if (!strcmp(token, "logfile")) { cs_strncpy(logfile, value, sizeof(logfile)); return; }
     320  if (!strcmp(token, "pidfile")) { cs_strncpy(cfg->pidfile, value, sizeof(cfg->pidfile)); return; }
     321  if (!strcmp(token, "usrfile")) { cs_strncpy(cfg->usrfile, value, sizeof(cfg->usrfile)); return; }
     322  if (!strcmp(token, "cwlogdir")) { cs_strncpy(cfg->cwlogdir, value, sizeof(cfg->cwlogdir)); return; }
    323323  if (!strcmp(token, "clienttimeout"))
    324324  {
     
    411411  if (!strcmp(token, "aclogfile"))
    412412  {
    413     strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile)-1);
     413    cs_strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile));
    414414    return;
    415415  }
     
    442442  if (!strcmp(token, "monlevel")) { cfg->mon_level=atoi(value); return; }
    443443  if (!strcmp(token, "httpport")) { cfg->http_port=atoi(value); return; }
    444   if (!strcmp(token, "httpuser")) { strncpy(cfg->http_user, value, sizeof(cfg->http_user)-1); cfg->http_user[sizeof(cfg->http_user)-1] = '\0'; return; }
    445   if (!strcmp(token, "httppwd")) { strncpy(cfg->http_pwd, value, sizeof(cfg->http_pwd)-1); cfg->http_pwd[sizeof(cfg->http_pwd)-1] = '\0'; return; }
    446   if (!strcmp(token, "httpcss")) { strncpy(cfg->http_css, value, sizeof(cfg->http_css)-1); cfg->http_css[sizeof(cfg->http_css)-1] = '\0'; return; }
    447   if (!strcmp(token, "httptpl")) { strncpy(cfg->http_tpl, value, sizeof(cfg->http_tpl)-1); cfg->http_css[sizeof(cfg->http_tpl)-1] = '\0'; return; }
     444  if (!strcmp(token, "httpuser")) { cs_strncpy(cfg->http_user, value, sizeof(cfg->http_user)); return; }
     445  if (!strcmp(token, "httppwd")) { cs_strncpy(cfg->http_pwd, value, sizeof(cfg->http_pwd)); return; }
     446  if (!strcmp(token, "httpcss")) { cs_strncpy(cfg->http_css, value, sizeof(cfg->http_css)); return; }
     447  if (!strcmp(token, "httptpl")) {     
     448    cs_strncpy(cfg->http_tpl, value, sizeof(cfg->http_tpl));
     449    if(strlen(cfg->http_tpl) < (sizeof(cfg->http_tpl)-2) && cfg->http_tpl[strlen(cfg->http_tpl)-1] != '/'){
     450        cfg->http_tpl[strlen(cfg->http_tpl)] = '/';
     451        cfg->http_tpl[strlen(cfg->http_tpl)] = '\0';
     452    }
     453    return;
     454  }
    448455  if (!strcmp(token, "httprefresh")) { cfg->http_refresh=atoi(value); return; }
    449456  if (!strcmp(token, "httphideidleclients")) { cfg->http_hide_idle_clients=atoi(value); return; }
     
    511518  if (!strcmp(token, "serverip")) { cfg->rad_srvip=inet_addr(value); return; }
    512519  if (!strcmp(token, "allowed")) { chk_iprange(value, &cfg->rad_allowed); return; }
    513   if (!strcmp(token, "user")) { strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr)-1); return; }
     520  if (!strcmp(token, "user")) { cs_strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr)); return; }
    514521  if (token[0] != '#')
    515522    fprintf(stderr, "Warning: keyword '%s' in radegast section not recognized\n",token);
     
    523530    l=strlen(cfg->ser_device);
    524531    if (l) cfg->ser_device[l++]=1;  // use ctrl-a as delimiter
    525     strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-1-l);
     532    cs_strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-l);
    526533    return;
    527534  }
     
    536543  if (!strcmp(token, "password")) { cs_atob(cfg->gbox_pwd, value, 4); return; }
    537544  if (!strcmp(token, "maxdist")) { cfg->maxdist=atoi(value); return; }
    538   if (!strcmp(token, "ignorelist")) { strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)-1); return; }
    539   if (!strcmp(token, "onlineinfos")) { strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)-1); return; }
    540   if (!strcmp(token, "cardinfos")) { strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)-1); return; }
     545  if (!strcmp(token, "ignorelist")) { cs_strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)); return; }
     546  if (!strcmp(token, "onlineinfos")) { cs_strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)); return; }
     547  if (!strcmp(token, "cardinfos")) { cs_strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)); return; }
    541548  if (!strcmp(token, "locals"))
    542549  {
     
    567574    if (!strcmp(token, "enabled"))  { cfg->dvbapi_enabled=atoi(value); return; }
    568575    if (!strcmp(token, "au"))       { cfg->dvbapi_au=atoi(value); return; }
    569     if (!strcmp(token, "boxtype"))  { strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)-1); return; }
    570     if (!strcmp(token, "user"))     { strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)-1); return; }
    571     if (!strcmp(token, "priority")) { strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)-1); return; }
    572     if (!strcmp(token, "ignore"))   { strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)-1); return; }
     576    if (!strcmp(token, "boxtype"))  { cs_strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)); return; }
     577    if (!strcmp(token, "user"))     { cs_strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)); return; }
     578    if (!strcmp(token, "priority")) { cs_strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)); return; }
     579    if (!strcmp(token, "ignore"))   { cs_strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)); return; }
    573580
    574581    if (token[0] != '#')
     
    768775  char *ptr1;//, *ptr2;
    769776
    770   if (!strcmp(token, "user")) { strncpy(account->usr, value, sizeof(account->usr)-1); return; }
    771   if (!strcmp(token, "pwd")) { strncpy(account->pwd, value, sizeof(account->pwd)-1); return; }
    772   if (!strcmp(token, "hostname")) { strncpy((char *)account->dyndns, value, sizeof(account->dyndns)-1);return; }
     777  if (!strcmp(token, "user")) { cs_strncpy(account->usr, value, sizeof(account->usr)); return; }
     778  if (!strcmp(token, "pwd")) { cs_strncpy(account->pwd, value, sizeof(account->pwd)); return; }
     779  if (!strcmp(token, "hostname")) { cs_strncpy((char *)account->dyndns, value, sizeof(account->dyndns));return; }
    773780  if (!strcmp(token, "betatunnel")) { chk_tuntab(value, &account->ttab); return; }
    774781  if (!strcmp(token, "uniq")) { account->uniq=atoi(value); return; }
     
    12881295  ulong caid;
    12891296  char buf[strlen(value) + 1];
    1290   strncpy(buf, value, sizeof(buf));
     1297  cs_strncpy(buf, value, sizeof(buf));
    12911298  b=(what==1) ? sizeof(ulong) : sizeof(ushort);
    12921299  for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
     
    13791386      nr++;
    13801387      memset(sidtab, 0, sizeof(struct s_sidtab));
    1381       strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
     1388      cs_strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
    13821389      continue;
    13831390    }
     
    14401447            switch(i){
    14411448                case 0:
    1442                     strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov)-1);
     1449                    cs_strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov));
    14431450                    break;
    14441451                case 1:
    1445                     strncpy(srvid->name, trim(ptr1), sizeof(srvid->name)-1);
     1452                    cs_strncpy(srvid->name, trim(ptr1), sizeof(srvid->name));
    14461453                    break;
    14471454                case 2:
    1448                     strncpy(srvid->type, trim(ptr1), sizeof(srvid->type)-1);
     1455                    cs_strncpy(srvid->type, trim(ptr1), sizeof(srvid->type));
    14491456                    break;
    14501457                case 3:
    1451                     strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc)-1);
     1458                    cs_strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc));
    14521459                    break;
    14531460            }
     
    14891496      switch(i)
    14901497      {
    1491         case 0: strncpy(rdr->device, ptr, sizeof(rdr->device)-1); break;
     1498        case 0: cs_strncpy(rdr->device, ptr, sizeof(rdr->device)); break;
    14921499        case 1: rdr->r_port=atoi(ptr); break;
    14931500        case 2: rdr->l_port=atoi(ptr); break;
     
    15061513  }
    15071514#ifdef CS_WITH_GBOX
    1508   if (!strcmp(token, "password")) { strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
     1515  if (!strcmp(token, "password")) { cs_strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
    15091516  if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
    15101517#endif
     
    15161523      switch(i)
    15171524      {
    1518         case 0: strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)-1); break;
    1519         case 1: strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)-1); break;
     1525        case 0: cs_strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)); break;
     1526        case 1: cs_strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)); break;
    15201527      }
    15211528    }
    15221529    return;
    15231530  }
    1524   if( !strcmp(token, "pincode")) { strncpy(rdr->pincode, value, sizeof(rdr->pincode)-1); return; }
    1525   if (!strcmp(token, "readnano")) { strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)-1); return; }
     1531  if( !strcmp(token, "pincode")) { cs_strncpy(rdr->pincode, value, sizeof(rdr->pincode)); return; }
     1532  if (!strcmp(token, "readnano")) { cs_strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)); return; }
    15261533  /*
    15271534   *  case insensitive
     
    15341541  if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value);  return; }
    15351542
    1536   if (!strcmp(token, "label")) { strncpy(rdr->label, value, sizeof(rdr->label)-1); return; }
     1543  if (!strcmp(token, "label")) { cs_strncpy(rdr->label, value, sizeof(rdr->label)); return; }
    15371544  if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
    15381545  if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
     
    16841691    }
    16851692    bzero(rdr->cc_version, sizeof(rdr->cc_version));
    1686     strncpy(rdr->cc_version, value, sizeof(rdr->cc_version)-1);
     1693    cs_strncpy(rdr->cc_version, value, sizeof(rdr->cc_version));
    16871694    return;
    16881695  }
     
    16931700    }
    16941701    bzero(rdr->cc_build, sizeof(rdr->cc_build));
    1695     strncpy(rdr->cc_build, value, sizeof(rdr->cc_build)-1);
     1702    cs_strncpy(rdr->cc_build, value, sizeof(rdr->cc_build));
    16961703    return;
    16971704  }
  • branches/monitor-improvement/oscam-http-helpers.c

    r1251 r1256  
    132132char *tpl_getTplPath(const char *name, const char *path, char *result, unsigned int resultsize){
    133133    char *pch;
    134     if((strlen(path) + strlen(name) + 5) <= resultsize){
     134    if((strlen(path) + strlen(name) + 6) <= resultsize){
    135135        strcpy(result, path);
    136136        strcat(result, name);
     
    280280  fflush(stdout);
    281281  expectednonce =char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), NULL), MD5_DIGEST_LENGTH, hex2ascii);
    282   strncpy(result, expectednonce, resultlen);
    283   result[resultlen - 1] = '\0';
     282  cs_strncpy(result, expectednonce, resultlen);
    284283  free(noncetmp);
    285284    free(expectednonce);
  • branches/monitor-improvement/oscam-http.c

    r1238 r1256  
    664664    char user[128];
    665665
    666     if (strcmp(getParam(params, "action"), "Save As") == 0) strncpy(user, getParam(params, "newuser"), sizeof(user)/sizeof(char) - 1);
    667     else strncpy(user, getParam(params, "user"), sizeof(user)/sizeof(char) - 1);
    668     user[sizeof(user)/sizeof(char) - 1] = '\0';
     666    if (strcmp(getParam(params, "action"), "Save As") == 0) cs_strncpy(user, getParam(params, "newuser"), sizeof(user)/sizeof(char));
     667    else cs_strncpy(user, getParam(params, "user"), sizeof(user)/sizeof(char));
    669668
    670669    int i;
     
    691690      }
    692691      memset(account, 0, sizeof(struct s_auth));
    693             strncpy((char *)account->usr, user, sizeof(account->usr)-1);
     692            cs_strncpy((char *)account->usr, user, sizeof(account->usr));
    694693      account->au=(-1);
    695694      account->monlvl=cfg->mon_level;
     
    10531052    int i;
    10541053
    1055     strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char) - 1);
    1056     label[sizeof(label)/sizeof(char) - 1] = '\0';
     1054    cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label)/sizeof(char));
    10571055
    10581056    for (sidtab  = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next);
     
    10771075      }
    10781076      memset(sidtab, 0, sizeof(struct s_sidtab));
    1079             strncpy((char *)sidtab->label, label, sizeof(sidtab->label)-1);
     1077            cs_strncpy((char *)sidtab->label, label, sizeof(sidtab->label));
    10801078
    10811079            tpl_addVar(vars, 1, "MESSAGE", "<b>New service has been added</b><BR>");
  • branches/monitor-improvement/oscam-simples.c

    r1154 r1256  
    537537    }
    538538}
     539
     540/* Ordinary strncpy does not terminate the string if the source is exactly as long or longer as the specified size. This can raise security issues.
     541   This function is a replacement which makes sure that a \0 is always added. num should be the real size of char array (do not subtract -1). */
     542void cs_strncpy(char * destination, const char * source, size_t num){
     543    uint32 l, size = strlen(source);
     544    if(size > num - 1) l = num - 1;
     545    else l = size;
     546    memcpy(destination, source, l);
     547    destination[l] = '\0';
     548}
  • branches/monitor-improvement/oscam.c

    r1228 r1256  
    440440          if( cfg->ac_enabled )
    441441          {
    442             strncpy(usr, client[i].usr, sizeof(usr)-1);
     442            cs_strncpy(usr, client[i].usr, sizeof(usr));
    443443            ac_idx = client[i].ac_idx;
    444444            ac_limit = client[i].ac_limit;
     
    12491249  ptr[1]='\0';
    12501250  if ((client[cs_idx].typ=='c') || (client[cs_idx].typ=='m'))
    1251     strncpy(ptr, client[cs_idx].usr, 31);
    1252   strncpy(ptr+32, txt, CS_LOGHISTSIZE-33);
     1251    cs_strncpy(ptr, client[cs_idx].usr, 31);
     1252  cs_strncpy(ptr+32, txt, CS_LOGHISTSIZE-33);
    12531253  *loghistidx=(*loghistidx+1) % CS_MAXLOGHIST;
    12541254#endif
     
    13861386        for (this=cfg->srvid; this; this=this->next) {
    13871387            if (this->srvid==er->srvid) {
    1388                 strncpy(srvname, this->name, sizeof(srvname));
     1388                cs_strncpy(srvname, this->name, sizeof(srvname));
    13891389                srvname[sizeof(srvname)-1] = 0;
    13901390                for (i=0;srvname[i];i++)
     
    22072207      case 'b': bg=1;
    22082208                break;
    2209       case 'c': strncpy(cs_confdir, optarg, sizeof(cs_confdir)-1);
     2209      case 'c': cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
    22102210                break;
    22112211      case 'd': cs_dblevel=atoi(optarg);
     
    22132213      case 'm':
    22142214#ifdef CS_NOSHM
    2215                 strncpy(cs_memfile, optarg, sizeof(cs_memfile)-1);
     2215                cs_strncpy(cs_memfile, optarg, sizeof(cs_memfile));
    22162216                break;
    22172217#endif
  • branches/monitor-improvement/reader-cryptoworks.c

    r1140 r1256  
    260260  if (read_record(0xC0)>=16)
    261261  {
    262     strncpy(issuer, (const char *)cta_res+2, sizeof(issuer)-1);
     262    cs_strncpy(issuer, (const char *)cta_res+2, sizeof(issuer));
    263263    trim(issuer);
    264264  }
     
    568568    if (read_record(0xD6)>=16)
    569569    {
    570       strncpy(l_name+8, (const char *)cta_res+2, sizeof(l_name)-9);
     570      cs_strncpy(l_name+8, (const char *)cta_res+2, sizeof(l_name)-9);
    571571      l_name[sizeof(l_name)-1]=0;
    572572      trim(l_name+8);
Note: See TracChangeset for help on using the changeset viewer.