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 <sys/types.h>
  31. #include <pwd.h>
  32. #include <grp.h>
  33. #include <stdlib.h>
  34. #include <time.h>
  35. #include <sys/utsname.h> /* uname() */
  36. #include <libgen.h>
  37. #include "ut.h"
  38. #include "mem/mem.h"
  39. #include "globals.h"
  40. /* converts a username into uid:gid,
  41. * returns -1 on error & 0 on success */
  42. int user2uid(int* uid, int* gid, char* user)
  43. {
  44. char* tmp;
  45. struct passwd *pw_entry;
  46. if (user){
  47. *uid=strtol(user, &tmp, 10);
  48. if ((tmp==0) ||(*tmp)){
  49. /* maybe it's a string */
  50. pw_entry=getpwnam(user);
  51. if (pw_entry==0){
  52. goto error;
  53. }
  54. *uid=pw_entry->pw_uid;
  55. if (gid) *gid=pw_entry->pw_gid;
  56. }
  57. return 0;
  58. }
  59. error:
  60. return -1;
  61. }
  62. /* converts a group name into a gid
  63. * returns -1 on error, 0 on success */
  64. int group2gid(int* gid, char* group)
  65. {
  66. char* tmp;
  67. struct group *gr_entry;
  68. if (group){
  69. *gid=strtol(group, &tmp, 10);
  70. if ((tmp==0) ||(*tmp)){
  71. /* maybe it's a string */
  72. gr_entry=getgrnam(group);
  73. if (gr_entry==0){
  74. goto error;
  75. }
  76. *gid=gr_entry->gr_gid;
  77. }
  78. return 0;
  79. }
  80. error:
  81. return -1;
  82. }
  83. /*
  84. * Replacement of timegm (does not exists on all platforms
  85. * Taken from
  86. * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
  87. */
  88. time_t _timegm(struct tm* t)
  89. {
  90. time_t tl, tb;
  91. struct tm* tg;
  92. t->tm_isdst = 0;
  93. tl = mktime(t);
  94. if (tl == -1) {
  95. t->tm_hour--;
  96. tl = mktime (t);
  97. if (tl == -1) {
  98. return -1; /* can't deal with output from strptime */
  99. }
  100. tl += 3600;
  101. }
  102. tg = gmtime(&tl);
  103. tg->tm_isdst = 0;
  104. tb = mktime(tg);
  105. if (tb == -1) {
  106. tg->tm_hour--;
  107. tb = mktime (tg);
  108. if (tb == -1) {
  109. return -1; /* can't deal with output from gmtime */
  110. }
  111. tb += 3600;
  112. }
  113. return (tl - (tb - tl));
  114. }
  115. /* Convert time_t value that is relative to local timezone to UTC */
  116. time_t local2utc(time_t in)
  117. {
  118. struct tm* tt;
  119. tt = gmtime(&in);
  120. tt->tm_isdst = -1;
  121. return mktime(tt);
  122. }
  123. /* Convert time_t value in UTC to to value relative to local time zone */
  124. time_t utc2local(time_t in)
  125. {
  126. struct tm* tt;
  127. tt = localtime(&in);
  128. #ifdef HAVE_TIMEGM
  129. return timegm(tt);
  130. #else
  131. return _timegm(tt);
  132. #endif
  133. }
  134. /*
  135. * Return str as zero terminated string allocated
  136. * using pkg_malloc
  137. */
  138. char* as_asciiz(str* s)
  139. {
  140. char* r;
  141. r = (char*)pkg_malloc(s->len + 1);
  142. if (!r) {
  143. ERR("Out of memory\n");
  144. return 0;
  145. }
  146. memcpy(r, s->s, s->len);
  147. r[s->len] = '\0';
  148. return r;
  149. }
  150. /* return system version (major.minor.minor2) as
  151. * (major<<16)|(minor)<<8|(minor2)
  152. * (if some of them are missing, they are set to 0)
  153. * if the parameters are not null they are set to the coresp. part
  154. */
  155. unsigned int get_sys_version(int* major, int* minor, int* minor2)
  156. {
  157. struct utsname un;
  158. int m1;
  159. int m2;
  160. int m3;
  161. char* p;
  162. memset (&un, 0, sizeof(un));
  163. m1=m2=m3=0;
  164. /* get sys version */
  165. uname(&un);
  166. m1=strtol(un.release, &p, 10);
  167. if (*p=='.'){
  168. p++;
  169. m2=strtol(p, &p, 10);
  170. if (*p=='.'){
  171. p++;
  172. m3=strtol(p, &p, 10);
  173. }
  174. }
  175. if (major) *major=m1;
  176. if (minor) *minor=m2;
  177. if (minor2) *minor2=m3;
  178. return ((m1<<16)|(m2<<8)|(m3));
  179. }
  180. char* get_abs_pathname(str* base, str* file)
  181. {
  182. str ser_cfg;
  183. char* buf, *dir, *res;
  184. int len;
  185. if (base == NULL) {
  186. ser_cfg.s = cfg_file;
  187. ser_cfg.len = strlen(cfg_file);
  188. base = &ser_cfg;
  189. }
  190. if (!base->s || base->len <= 0 || base->s[0] != '/') {
  191. BUG("get_abs_pathname: Base file must be absolute pathname: "
  192. "'%.*s'\n", STR_FMT(base));
  193. return NULL;
  194. }
  195. if (!file || !file->s || file->len <= 0) {
  196. BUG("get_abs_pathname: Invalid 'file' parameter\n");
  197. return NULL;
  198. }
  199. if (file->s[0] == '/') {
  200. /* This is an absolute pathname, make a zero terminated
  201. * copy and use it as it is */
  202. if ((res = malloc(file->len+1)) == NULL) {
  203. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  204. }
  205. memcpy(res, file->s, file->len);
  206. res[file->len]=0;
  207. } else {
  208. /* This is not an absolute pathname, make it relative
  209. * to the location of the base file
  210. */
  211. /* Make a copy, function dirname may modify the string */
  212. if ((buf = malloc(base->len+1)) == NULL) {
  213. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  214. return NULL;
  215. }
  216. memcpy(buf, base->s, base->len);
  217. buf[base->len]=0;
  218. dir = dirname(buf);
  219. len = strlen(dir);
  220. if ((res = malloc(len + 1 + file->len + 1)) == NULL) {
  221. ERR("get_abs_pathname: No memory left (malloc failed)\n");
  222. free(buf);
  223. return NULL;
  224. }
  225. memcpy(res, dir, len);
  226. res[len] = '/';
  227. memcpy(res + len + 1, file->s, file->len);
  228. res[len + 1 + file->len] = '\0';
  229. free(buf);
  230. }
  231. return res;
  232. }