00001 /* 00002 *$Id: ut.c 4891 2008-09-11 17:09:28Z henningw $ 00003 * 00004 * - various general purpose functions 00005 * 00006 * Copyright (C) 2001-2003 FhG Fokus 00007 * 00008 * This file is part of Kamailio, a free SIP server. 00009 * 00010 * Kamailio is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version 00014 * 00015 * Kamailio is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 * 00024 * History 00025 * ------ 00026 * 2006-09-25 created by movind user2uid and group2gid from main.c (bogdan) 00027 */ 00028 00029 /*! 00030 * \file 00031 * \brief Various utility functions, mostly related to string handling 00032 */ 00033 00034 00035 #include <stdlib.h> 00036 #include <pwd.h> 00037 #include <grp.h> 00038 #include "ut.h" 00039 00040 /*! buffer for int2str */ 00041 char int2str_buf[INT2STR_MAX_LEN]; 00042 00043 00044 /*! 00045 * \brief Converts a username into uid and gid values 00046 * \param uid user id 00047 * \param gid group id, set to null if you don't want this 00048 * \param user username 00049 * \return -1 on error, 0 on success 00050 */ 00051 int user2uid(int* uid, int* gid, char* user) 00052 { 00053 char* tmp; 00054 struct passwd *pw_entry; 00055 00056 if (user){ 00057 *uid=strtol(user, &tmp, 10); 00058 if ((tmp==0) ||(*tmp)){ 00059 /* maybe it's a string */ 00060 pw_entry=getpwnam(user); 00061 if (pw_entry==0){ 00062 goto error; 00063 } 00064 *uid=pw_entry->pw_uid; 00065 if (gid) *gid=pw_entry->pw_gid; 00066 } 00067 return 0; 00068 } 00069 error: 00070 return -1; 00071 } 00072 00073 00074 /*! 00075 * \brief Converts a groupname into gid value 00076 * \param gid group id 00077 * \param group group name 00078 * \return -1 on error, 0 on success 00079 */ 00080 int group2gid(int* gid, char* group) 00081 { 00082 char* tmp; 00083 struct group *gr_entry; 00084 00085 if (group){ 00086 *gid=strtol(group, &tmp, 10); 00087 if ((tmp==0) ||(*tmp)){ 00088 /* maybe it's a string */ 00089 gr_entry=getgrnam(group); 00090 if (gr_entry==0){ 00091 goto error; 00092 } 00093 *gid=gr_entry->gr_gid; 00094 } 00095 return 0; 00096 } 00097 error: 00098 return -1; 00099 } 00100 00101 /*! utility function to give each children a unique seed */ 00102 void seed_child(unsigned int seed) 00103 { 00104 srand(seed); 00105 }
1.5.6