/* * CopyKey() -- worker function implementing RegDupKeyAlt(). * * Note: - assumes target does not exist (i.e., deleted by RegDupKeyAlt()) * - no cleanup on failure (i.e., assumes done by RegDupKeyAlt()) */ static long CopyKey(const char *sourceKey, const char *targetKey) { long status; HKEY srcKey, dupKey; /* open source key */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, sourceKey, KEY_READ, 0, &srcKey, NULL); if (status == ERROR_SUCCESS) { /* create target key */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, targetKey, KEY_ALL_ACCESS, 1 /* create */, &dupKey, NULL); if (status == ERROR_SUCCESS) { /* copy values and their data from source to target */ status = CopyValues(srcKey, dupKey); if (status == ERROR_SUCCESS) { /* copy subkeys from source to target */ status = CopySubkeys(sourceKey, srcKey, targetKey, dupKey); } (void) RegCloseKey(dupKey); } (void) RegCloseKey(srcKey); } return status; }
/* * vpt_RemoveEntry() -- Remove vice partition table entry. * * RETURN CODES: 0 success, -1 failed (errno set) */ int vpt_RemoveEntry(const char *vpname) { long status; HKEY tabKey; if (!vpt_PartitionNameValid(vpname)) { errno = EINVAL; return -1; } /* open canonical Afstab key */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY, KEY_WRITE, 0, &tabKey, NULL); if (status == ERROR_SUCCESS) { /* delete key representing vice partition */ status = RegDeleteKey(tabKey, vpname); } if (status) { errno = nterr_nt2unix(status, EIO); return -1; } return 0; }
/* * AddToProviderOrder() -- add entry to network provider order */ BOOL AddToProviderOrder(char *pszWhatToAdd) { HKEY hKey; DWORD dwType; LONG result; int nLen; char *pszValue = 0; char *pszNewValue; BOOL bAlreadyAdded = FALSE; /* Open the key, creating it if necessary (but should always be there). */ result = RegOpenKeyAlt(AFSREG_NULL_KEY, NETWORK_PROVIDER_ORDER_KEY, KEY_SET_VALUE | KEY_ALL_ACCESS, TRUE, &hKey, 0); if (result != ERROR_SUCCESS) return FALSE; /* Get the old value */ result = RegQueryValueAlt(hKey, PROVIDER_ORDER_VALUE_NAME, &dwType, &pszValue, &nLen); if (result != ERROR_SUCCESS) { nLen = 0; } pszNewValue = malloc(nLen + strlen(pszWhatToAdd) + 1);/* Add 1 for comma */ *pszNewValue = 0; /* Add the new value */ if (result == ERROR_SUCCESS) { if (strstr(pszValue, pszWhatToAdd) != 0) bAlreadyAdded = TRUE; else { if (pszValue && *pszValue) { strcpy(pszNewValue, pszValue); strcat(pszNewValue, ","); } strcat(pszNewValue, pszWhatToAdd); } } else if (result == ERROR_FILE_NOT_FOUND) strcpy(pszNewValue, pszWhatToAdd); /* Set the new value in the registry */ if (((result == ERROR_SUCCESS) || (result == ERROR_FILE_NOT_FOUND)) && !bAlreadyAdded) result = RegSetValueEx(hKey, PROVIDER_ORDER_VALUE_NAME, 0, REG_SZ, pszNewValue, strlen(pszNewValue) + 1); free(pszNewValue); free(pszValue); RegCloseKey(hKey); return (result == ERROR_SUCCESS); }
/* * vpt_AddEntry() -- Add or update vice partition table entry. * * RETURN CODES: 0 success, -1 failed (errno set) */ int vpt_AddEntry(const struct vptab *vptabp) { long status; HKEY tabKey, vpKey; const char *vpName, *vpDev; vpName = vptabp->vp_name; vpDev = vptabp->vp_dev; if (!vpt_PartitionNameValid(vpName) || !vpt_DeviceNameValid(vpDev)) { errno = EINVAL; return -1; } /* open canonical Afstab key; create if doesn't exist */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY, KEY_WRITE, 1 /* create */, &tabKey, NULL); if (status == ERROR_SUCCESS) { /* open key representing vice partition; create if doesn't exist */ status = RegOpenKeyAlt(tabKey, vpName, KEY_WRITE, 1 /* create */, &vpKey, NULL); if (status == ERROR_SUCCESS) { /* write partition attributes */ status = RegSetValueEx(vpKey, AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE, 0, REG_SZ, vpDev, (DWORD)strlen(vpDev) + 1); RegCloseKey(vpKey); } RegCloseKey(tabKey); } if (status) { errno = nterr_nt2unix(status, EIO); return -1; } return 0; }
/* * RemoveFromProviderOrder() -- remove entry from network provider order */ BOOL RemoveFromProviderOrder(char *pszWhatToDel) { HKEY hKey; DWORD dwType; LONG result; int nLen; char *pszValue = 0; char *pszNewValue; BOOL bAlreadyRemoved = FALSE; /* Open the key, creating if necessary (but should always be there). */ result = RegOpenKeyAlt(AFSREG_NULL_KEY, NETWORK_PROVIDER_ORDER_KEY, KEY_SET_VALUE | KEY_ALL_ACCESS, TRUE, &hKey, 0); if (result != ERROR_SUCCESS) return FALSE; /* Get the old value */ result = RegQueryValueAlt(hKey, PROVIDER_ORDER_VALUE_NAME, &dwType, &pszValue, &nLen); if (result == ERROR_SUCCESS) { pszNewValue = malloc(nLen); /* bigger than we need, but that's ok */ *pszNewValue = 0; if (strstr(pszValue, pszWhatToDel) == 0) bAlreadyRemoved = TRUE; else { char *pszCur; pszCur = strtok(pszValue, ","); while (pszCur) { if (strcmp(pszCur, pszWhatToDel) != 0) { if (*pszNewValue) strcat(pszNewValue, ","); strcat(pszNewValue, pszCur); } pszCur = strtok(0, ","); } } /* Set the new value in the registry */ if (!bAlreadyRemoved) result = RegSetValueEx(hKey, PROVIDER_ORDER_VALUE_NAME, 0, REG_SZ, pszNewValue, strlen(pszNewValue) + 1); free(pszNewValue); free(pszValue); } RegCloseKey(hKey); return (result == ERROR_SUCCESS); }
/* * WriteRegEnv() -- write system environment variable to registry (NT only). */ static BOOL WriteRegEnv(char *pszEnvValue, char *pszEnvName) { LONG result; HKEY hKey; result = RegOpenKeyAlt(AFSREG_NULL_KEY, ENVIRONMENT_KEY, KEY_ALL_ACCESS, FALSE, &hKey, 0); if (result != ERROR_SUCCESS) return FALSE; result = RegSetValueEx(hKey, pszEnvName, 0, REG_EXPAND_SZ, pszEnvValue, strlen(pszEnvValue) + 1); RegCloseKey(hKey); return (result == ERROR_SUCCESS); }
/* * ReadRegEnv() -- read system enviornment variable from registry (NT only). */ static BOOL ReadRegEnv(char **ppszEnvValue, char *pszRegValueName) { HKEY hKey; DWORD dwType; LONG result; int nLen = 512; result = RegOpenKeyAlt(AFSREG_NULL_KEY, ENVIRONMENT_KEY, KEY_SET_VALUE | KEY_ALL_ACCESS, FALSE, &hKey, 0); if (result != ERROR_SUCCESS) return FALSE; *ppszEnvValue = 0; do { if (*ppszEnvValue) free(*ppszEnvValue); *ppszEnvValue = (char *)malloc(nLen); if (*ppszEnvValue == 0) { RegCloseKey(hKey); return FALSE; } /* If function fails to open the value and the error code says that * the value doesn't exist, then we will attempt to make it. */ result = RegQueryValueEx(hKey, pszRegValueName, 0, &dwType, *ppszEnvValue, &nLen); if (result == ERROR_FILE_NOT_FOUND) { result = RegSetValueEx(hKey, pszRegValueName, 0, REG_EXPAND_SZ, "", 0); **ppszEnvValue = '\0'; /* zero length string "read" */ } } while (result == ERROR_MORE_DATA); RegCloseKey(hKey); if (result != ERROR_SUCCESS || strlen(*ppszEnvValue) == 0) { /* Don't return empty strings; instead set buffer pointer to 0. */ free(*ppszEnvValue); *ppszEnvValue = 0; } return (result == ERROR_SUCCESS); }
int syscfg_GetIFInfo_2000(int *count, int *addrs, int *masks, int *mtus, int *flags) { int maxCount = *count; char *IFListBase = NULL; char *IFList, *ifname; HKEY skey; int i, n, nConfig; if (RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_IPSRV_KEY, KEY_READ, 0, &skey, NULL)) return -1; if ((nConfig = GetInterfaceList(skey, &IFListBase)) < 0) { (void) RegCloseKey(skey); return -1; } IFList = IFListBase; n = 0; while ((n < maxCount) && (ifname = GetNextInterface(IFList))) { if (!IsLoopback(ifname) && GetIP(skey, ifname, &addrs[n], &masks[n]) == 0 && addrs[n] != 0) { n++; } else { maxCount--; } IFList = ifname; } /* And until we get mtu's and flags */ for (i = 0; i < n; i++) { mtus[i] = 1500; flags[i] = 0; } (void) RegCloseKey(skey); free(IFListBase); *count = n; return nConfig; }
BOOL InNetworkProviderOrder(char *pszNetworkProvider, BOOL *pbIn) { HKEY hKey; LONG bResult; DWORD dwType; char *pszProviderOrder = 0; DWORD dwSize; bResult = FALSE; // Assume failure if (RegOpenKeyAlt(AFSREG_NULL_KEY, NETWORK_PROVIDER_ORDER_KEY, KEY_READ, FALSE, &hKey, 0) == ERROR_SUCCESS) { if (RegQueryValueAlt(hKey, PROVIDER_ORDER_VALUE_NAME, &dwType, &pszProviderOrder, &dwSize) == ERROR_SUCCESS) { *pbIn = strstr(pszProviderOrder, pszNetworkProvider) != 0; bResult = TRUE; free(pszProviderOrder); } RegCloseKey(hKey); } return bResult; }
static char *GetInstallDir() { HKEY hKey; LONG nResult; DWORD dwType; static char szInstallDir[256]; DWORD dwSize; dwSize = sizeof(szInstallDir); nResult = RegOpenKeyAlt(HKEY_LOCAL_MACHINE, APP_INSTALL_DIR_REG_SUBKEY, KEY_READ, FALSE, &hKey, 0); if (nResult == ERROR_SUCCESS) { nResult = RegQueryValueEx(hKey, APP_INSTALL_DIR_REG_VALUE, 0, &dwType, (PBYTE)szInstallDir, &dwSize); RegCloseKey(hKey); } if (nResult != ERROR_SUCCESS) szInstallDir[0] = 0; return szInstallDir; }
/* GetInterfaceList * * Get interface list; list is represented as a multistring. * Returns number of elements in interface list or -1 on error. */ static int GetInterfaceList(HKEY skey, char **list) { HKEY key; long status; char *str = NULL; int size; DWORD valType; if (RegOpenKeyAlt(skey, AFSREG_IPSRV_IFACELIST_SUBKEY, KEY_READ, 0, &key, NULL)) return -1; status = RegQueryValueAlt(key, AFSREG_IPSRV_IFACELIST_BIND_VALUE, &valType, &str, NULL); (void) RegCloseKey(key); if (status || (valType != REG_MULTI_SZ)) return -1; /* Count strings in multistring. */ size = 0; if (*str != '\0') { int i; for (i = 1; ; i++) { if (str[i] == '\0') { /* hit end of string */ size++; i++; if (str[i] == '\0') { /* hit end of multistring */ break; } } } } *list = str; return size; }
/* * RegDeleteEntryAlt() -- delete named key or value; if key, then all subkeys * and values are removed; entryName must be in canonical path format. */ long RegDeleteEntryAlt(const char *entryName, regentry_t entryType) { long status = ERROR_SUCCESS; char *entryBuf = strdup(entryName); if (entryBuf == NULL) { status = ERROR_NOT_ENOUGH_MEMORY; } else { char *keyPath = entryBuf; char *entryName = strrchr(entryBuf, '\\'); if (entryName == NULL) { status = ERROR_INVALID_PARAMETER; } else { HKEY key; *entryName = '\0'; entryName++; status = RegOpenKeyAlt(AFSREG_NULL_KEY, keyPath, KEY_ALL_ACCESS, 0, &key, NULL); if (status == ERROR_SUCCESS) { if (entryType == REGENTRY_KEY) { status = RegDeleteKeyAlt(key, entryName); } else if (entryType == REGENTRY_VALUE) { status = RegDeleteValue(key, entryName); } else { status = ERROR_INVALID_PARAMETER; } (void) RegCloseKey(key); } } free(entryBuf); } return status; }
int vpt_Start(struct vpt_iter *iterp) /* [out] iteration handle to initialize */ { long status; HKEY key; memset(iterp, 0, sizeof(*iterp)); /* open canonical Afstab key */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY, KEY_READ, 0, &key, NULL); if (status == ERROR_SUCCESS) { /* enumerate subkeys, each of which represents a vice partition */ status = RegEnumKeyAlt(key, &iterp->vpenum); (void)RegCloseKey(key); } if (status) { errno = nterr_nt2unix(status, EIO); return -1; } return 0; }
int vpt_NextEntry(struct vpt_iter *iterp, /* [in] valid iteration handle */ struct vptab *vptabp) /* [out] next partiton table entry */ { long status; HKEY tabKey, vpKey; char *nextNamep; if (iterp->vpenum == NULL) { /* no partition entries (or invalid iteration handle) */ errno = ENOENT; return -1; } /* find name of next partition entry to examine in multistring enum */ if (iterp->last == NULL) { /* first call */ nextNamep = iterp->last = iterp->vpenum; } else { /* subsequent call */ if (*iterp->last != '\0') { /* not at end of multistring; advance to next name */ iterp->last += strlen(iterp->last) + 1; } nextNamep = iterp->last; } if (*nextNamep == '\0') { /* end of multistring; no more entries */ errno = ENOENT; return -1; } if (strlen(nextNamep) >= VPTABSIZE_NAME) { /* invalid partition name entry */ errno = EINVAL; return -1; } strcpy(vptabp->vp_name, nextNamep); /* open canonical Afstab key */ status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SVC_AFSTAB_KEY, KEY_READ, 0, &tabKey, NULL); if (status == ERROR_SUCCESS) { /* open key representing vice partition */ status = RegOpenKeyAlt(tabKey, nextNamep, KEY_READ, 0, &vpKey, NULL); if (status == ERROR_SUCCESS) { /* read partition attributes */ DWORD dataType, dataSize = VPTABSIZE_DEV; status = RegQueryValueEx(vpKey, AFSREG_SVR_SVC_AFSTAB_DEVNAME_VALUE, NULL, &dataType, vptabp->vp_dev, &dataSize); if (status == ERROR_SUCCESS && dataType != REG_SZ) { /* invalid device name type */ status = ERROR_INVALID_PARAMETER; } (void)RegCloseKey(vpKey); } (void)RegCloseKey(tabKey); } if (status) { errno = nterr_nt2unix(status, EIO); return -1; } return 0; }
static int GetIP(HKEY skey, char *ifname, int *addr, int *mask) { HKEY key; char *s; long status; int len; char *ipStr = NULL; char *snMask = NULL; DWORD valType; DWORD dwDHCP; DWORD dwLease; DWORD dwSize; len = (int) strlen(ifname) + 1 + sizeof(AFSREG_IPSRV_ADAPTER_PARAM_SUBKEY); s = malloc(len); if (!s) return -1; sprintf(s, "%s\\%s", ifname, AFSREG_IPSRV_ADAPTER_PARAM_SUBKEY); status = RegOpenKeyAlt(skey, s, KEY_READ, 0, &key, NULL); free(s); if (status) return -1; dwSize = sizeof(DWORD); status = RegQueryValueEx(key, "EnableDHCP", NULL, &valType, (LPBYTE) &dwDHCP, &dwSize); if (status || (valType != REG_DWORD)) dwDHCP = 0; if (dwDHCP == 0) { status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_ADDR_VALUE, &valType, &ipStr, NULL); if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) { if (ipStr) free(ipStr); (void) RegCloseKey(key); return -1; } status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_MASK_VALUE, &valType, &snMask, NULL); if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) { if (snMask) free(snMask); snMask = NULL; } } else { /* adapter configured via DHCP; address/mask in alternate values */ dwSize = sizeof(DWORD); status = RegQueryValueEx(key, "Lease", NULL, &valType, (LPBYTE)&dwLease, &dwSize); if (status || (valType != REG_DWORD) || dwLease == 0) { (void) RegCloseKey(key); return -1; } status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_DHCPADDR_VALUE, &valType, &ipStr, NULL); if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) { if (ipStr) free(ipStr); (void) RegCloseKey(key); return -1; } status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_DHCPMASK_VALUE, &valType, &snMask, NULL); if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) { if (snMask) free(snMask); snMask = NULL; } } /* convert ip and subnet. */ *addr = (int)inet_addr(ipStr); *addr = ntohl(*addr); free(ipStr); if (snMask) { *mask = (int)inet_addr(snMask); *mask = ntohl(*mask); free(snMask); } else { *mask = 0; } (void) RegCloseKey(key); return 0; }