ut.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. *$Id$
  3. *
  4. * various general purpose functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <pwd.h>
  33. #include <grp.h>
  34. #include <stdlib.h>
  35. #include <time.h>
  36. #include <sys/utsname.h> /* uname() */
  37. #include <libgen.h>
  38. #include "ut.h"
  39. #include "mem/mem.h"
  40. #include "globals.h"
  41. /* converts a username into uid:gid,
  42. * returns -1 on error & 0 on success */
  43. int user2uid(int* uid, int* gid, char* user)
  44. {
  45. char* tmp;
  46. struct passwd *pw_entry;
  47. if (user){
  48. *uid=strtol(user, &tmp, 10);
  49. if ((tmp==0) ||(*tmp)){
  50. /* maybe it's a string */
  51. pw_entry=getpwnam(user);
  52. if (pw_entry==0){
  53. goto error;
  54. }
  55. *uid=pw_entry->pw_uid;
  56. if (gid) *gid=pw_entry->pw_gid;
  57. }
  58. return 0;
  59. }
  60. error:
  61. return -1;
  62. }
  63. /* converts a group name into a gid
  64. * returns -1 on error, 0 on success */
  65. int group2gid(int* gid, char* group)
  66. {
  67. char* tmp;
  68. struct group *gr_entry;
  69. if (group){
  70. *gid=strtol(group, &tmp, 10);
  71. if ((tmp==0) ||(*tmp)){
  72. /* maybe it's a string */
  73. gr_entry=getgrnam(group);
  74. if (gr_entry==0){
  75. goto error;
  76. }
  77. *gid=gr_entry->gr_gid;
  78. }
  79. return 0;
  80. }
  81. error:
  82. return -1;
  83. }
  84. /*
  85. * Replacement of timegm (does not exists on all platforms
  86. * Taken from
  87. * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
  88. */
  89. time_t _timegm(struct tm* t)
  90. {
  91. time_t tl, tb;
  92. struct tm* tg;
  93. t->tm_isdst = 0;
  94. tl = mktime(t);
  95. if (tl == -1) {
  96. t->tm_hour--;
  97. tl = mktime (t);
  98. if (tl == -1) {
  99. return -1; /* can't deal with output from strptime */
  100. }
  101. tl += 3600;
  102. }
  103. tg = gmtime(&tl);
  104. tg->tm_isdst = 0;
  105. tb = mktime(tg);
  106. if (tb == -1) {
  107. tg->tm_hour--;
  108. tb = mktime (tg);
  109. if (tb == -1) {
  110. return -1; /* can't deal with output from gmtime */
  111. }
  112. tb += 3600;
  113. }
  114. return (tl - (tb - tl));
  115. }
  116. /* Convert time_t value that is relative to local timezone to UTC */
  117. time_t local2utc(time_t in)
  118. {
  119. struct tm* tt;
  120. tt = gmtime(&in);
  121. tt->tm_isdst = -1;
  122. return mktime(tt);
  123. }
  124. /* Convert time_t value in UTC to to value relative to local time zone */
  125. time_t utc2local(time_t in)
  126. {
  127. struct tm* tt;
  128. tt = localtime(&in);
  129. #ifdef HAVE_TIMEGM
  130. return timegm(tt);
  131. #else
  132. return _timegm(tt);
  133. #endif
  134. }
  135. /*
  136. * Return str as zero terminated string allocated
  137. * using pkg_malloc
  138. */
  139. char* as_asciiz(str* s)
  140. {
  141. char* r;
  142. r = (char*)pkg_malloc(s->len + 1);
  143. if (!r) {
  144. ERR("Out of memory\n");
  145. return 0;
  146. }
  147. memcpy(r, s->s, s->len);
  148. r[s->len] = '\0';
  149. return r;
  150. }
  151. /* return system version (major.minor.minor2) as
  152. * (major<<16)|(minor)<<8|(minor2)
  153. * (if some of them are missing, they are set to 0)
  154. * if the parameters are not null they are set to the coresp. part
  155. */
  156. unsigned int get_sys_version(int* major, int* minor, int* minor2)
  157. {
  158. struct utsname un;
  159. int m1;
  160. int m2;
  161. int m3;
  162. char* p;
  163. memset (&un, 0, sizeof(un));
  164. m1=m2=m3=0;
  165. /* get sys version */
  166. uname(&un);
  167. m1=strtol(un.release, &p, 10);
  168. if (*p=='.'){
  169. p++;
  170. m2=strtol(p, &p, 10);
  171. if (*p=='.'){
  172. p++;
  173. m3=strtol(p, &p, 10);
  174. }
  175. }
  176. if (major) *major=m1;
  177. if (minor) *minor=m2;
  178. if (minor2) *minor2=m3;
  179. return ((m1<<16)|(m2<<8)|(m3));
  180. }
  181. char* get_abs_pathname(str* base, str* file)
  182. {
  183. str ser_cfg;
  184. char* buf, *dir, *res;
  185. int len;
  186. if (base == NULL) {
  187. ser_cfg.s = cfg_file;
  188. ser_cfg.len = strlen(cfg_file);
  189. base = &ser_cfg;
  190. }
  191. if (!base->s || base->len <= 0 || base->s[0] != '/') {
  192. BUG("get_abs_pathname: Base file must be absolute pathname: "
  193. "'%.*s'\n", STR_FMT(base));
  194. return NULL;
  195. }
  196. if (!file || !file->s || file->len <= 0) {
  197. BUG("get_abs_pathname: Invalid 'file' parameter\n");
  198. return NULL;
  199. }
  200. if (file->s[0] == '/') {
  201. /* This is an absolute pathname, make a zero terminated
  202. * copy and use it as it is */
  203. if ((res = malloc(file->len+1)) == NULL) {
  204. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  205. }
  206. memcpy(res, file->s, file->len);
  207. res[file->len]=0;
  208. } else {
  209. /* This is not an absolute pathname, make it relative
  210. * to the location of the base file
  211. */
  212. /* Make a copy, function dirname may modify the string */
  213. if ((buf = malloc(base->len+1)) == NULL) {
  214. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  215. return NULL;
  216. }
  217. memcpy(buf, base->s, base->len);
  218. buf[base->len]=0;
  219. dir = dirname(buf);
  220. len = strlen(dir);
  221. if ((res = malloc(len + 1 + file->len + 1)) == NULL) {
  222. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  223. free(buf);
  224. return NULL;
  225. }
  226. memcpy(res, dir, len);
  227. res[len] = '/';
  228. memcpy(res + len + 1, file->s, file->len);
  229. res[len + 1 + file->len] = '\0';
  230. free(buf);
  231. }
  232. return res;
  233. }