| 1 | /* |
| 2 | * Copyright (c) 1997 Metro Link Incorporated |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF |
| 19 | * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | * |
| 22 | * Except as contained in this notice, the name of the Metro Link shall not be |
| 23 | * used in advertising or otherwise to promote the sale, use or other dealings |
| 24 | * in this Software without prior written authorization from Metro Link. |
| 25 | * |
| 26 | */ |
| 27 | /* |
| 28 | * Copyright (c) 1997-2003 by The XFree86 Project, Inc. |
| 29 | * |
| 30 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 31 | * copy of this software and associated documentation files (the "Software"), |
| 32 | * to deal in the Software without restriction, including without limitation |
| 33 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 34 | * and/or sell copies of the Software, and to permit persons to whom the |
| 35 | * Software is furnished to do so, subject to the following conditions: |
| 36 | * |
| 37 | * The above copyright notice and this permission notice shall be included in |
| 38 | * all copies or substantial portions of the Software. |
| 39 | * |
| 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 41 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 42 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 43 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 44 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 45 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 46 | * OTHER DEALINGS IN THE SOFTWARE. |
| 47 | * |
| 48 | * Except as contained in this notice, the name of the copyright holder(s) |
| 49 | * and author(s) shall not be used in advertising or otherwise to promote |
| 50 | * the sale, use or other dealings in this Software without prior written |
| 51 | * authorization from the copyright holder(s) and author(s). |
| 52 | */ |
| 53 | |
| 54 | #ifdef HAVE_XORG_CONFIG_H |
| 55 | #include <xorg-config.h> |
| 56 | #endif |
| 57 | |
| 58 | #include "os.h" |
| 59 | #include "xf86Parser.h" |
| 60 | #include "xf86tokens.h" |
| 61 | #include "Configint.h" |
| 62 | |
| 63 | #include <unistd.h> |
| 64 | #include <sys/types.h> |
| 65 | #include <sys/wait.h> |
| 66 | #include <signal.h> |
| 67 | #include <errno.h> |
| 68 | |
| 69 | #if defined(HAVE_SETEUID) && defined(_POSIX_SAVED_IDS) && _POSIX_SAVED_IDS > 0 |
| 70 | #define HAS_SAVED_IDS_AND_SETEUID |
| 71 | #endif |
| 72 | #if defined(WIN32) |
| 73 | #define HAS_NO_UIDS |
| 74 | #endif |
| 75 | |
| 76 | #ifdef HAS_NO_UIDS |
| 77 | #define doWriteConfigFile xf86writeConfigFile |
| 78 | #define Local /**/ |
| 79 | #else |
| 80 | #define Local static |
| 81 | #endif |
| 82 | |
| 83 | Local int |
| 84 | doWriteConfigFile(const char *filename, XF86ConfigPtr cptr) |
| 85 | { |
| 86 | FILE *cf; |
| 87 | |
| 88 | if ((cf = fopen(filename, "w")) == NULL) { |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | if (cptr->conf_comment) |
| 93 | fprintf(cf, "%s\n", cptr->conf_comment); |
| 94 | |
| 95 | xf86printLayoutSection(cf, cptr->conf_layout_lst); |
| 96 | |
| 97 | if (cptr->conf_files != NULL) { |
| 98 | fprintf(cf, "Section \"Files\"\n"); |
| 99 | xf86printFileSection(cf, cptr->conf_files); |
| 100 | fprintf(cf, "EndSection\n\n"); |
| 101 | } |
| 102 | |
| 103 | if (cptr->conf_modules != NULL) { |
| 104 | fprintf(cf, "Section \"Module\"\n"); |
| 105 | xf86printModuleSection(cf, cptr->conf_modules); |
| 106 | fprintf(cf, "EndSection\n\n"); |
| 107 | } |
| 108 | |
| 109 | xf86printVendorSection(cf, cptr->conf_vendor_lst); |
| 110 | |
| 111 | xf86printServerFlagsSection(cf, cptr->conf_flags); |
| 112 | |
| 113 | xf86printInputSection(cf, cptr->conf_input_lst); |
| 114 | |
| 115 | xf86printInputClassSection(cf, cptr->conf_inputclass_lst); |
| 116 | |
| 117 | xf86printVideoAdaptorSection(cf, cptr->conf_videoadaptor_lst); |
| 118 | |
| 119 | xf86printModesSection(cf, cptr->conf_modes_lst); |
| 120 | |
| 121 | xf86printMonitorSection(cf, cptr->conf_monitor_lst); |
| 122 | |
| 123 | xf86printDeviceSection(cf, cptr->conf_device_lst); |
| 124 | |
| 125 | xf86printScreenSection(cf, cptr->conf_screen_lst); |
| 126 | |
| 127 | xf86printDRISection(cf, cptr->conf_dri); |
| 128 | |
| 129 | xf86printExtensionsSection(cf, cptr->conf_extensions); |
| 130 | |
| 131 | fclose(cf); |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | #ifndef HAS_NO_UIDS |
| 136 | |
| 137 | int |
| 138 | xf86writeConfigFile(const char *filename, XF86ConfigPtr cptr) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | #if !defined(HAS_SAVED_IDS_AND_SETEUID) |
| 143 | int pid, p; |
| 144 | int status; |
| 145 | void (*csig) (int); |
| 146 | #else |
| 147 | int ruid, euid; |
| 148 | #endif |
| 149 | |
| 150 | if (getuid() != geteuid()) { |
| 151 | |
| 152 | #if !defined(HAS_SAVED_IDS_AND_SETEUID) |
| 153 | /* Need to fork to change ruid without loosing euid */ |
| 154 | csig = signal(SIGCHLD, SIG_DFL); |
| 155 | switch ((pid = fork())) { |
| 156 | case -1: |
| 157 | ErrorF("xf86writeConfigFile(): fork failed (%s)\n", |
| 158 | strerror(errno)); |
| 159 | return 0; |
| 160 | case 0: /* child */ |
| 161 | if (setuid(getuid()) == -1) |
| 162 | FatalError("xf86writeConfigFile(): " |
| 163 | "setuid failed(%s)\n", strerror(errno)); |
| 164 | ret = doWriteConfigFile(filename, cptr); |
| 165 | exit(ret); |
| 166 | break; |
| 167 | default: /* parent */ |
| 168 | do { |
| 169 | p = waitpid(pid, &status, 0); |
| 170 | } while (p == -1 && errno == EINTR); |
| 171 | } |
| 172 | signal(SIGCHLD, csig); |
| 173 | if (p != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0) |
| 174 | return 1; /* success */ |
| 175 | else |
| 176 | return 0; |
| 177 | |
| 178 | #else /* HAS_SAVED_IDS_AND_SETEUID */ |
| 179 | |
| 180 | ruid = getuid(); |
| 181 | euid = geteuid(); |
| 182 | |
| 183 | if (seteuid(ruid) == -1) { |
| 184 | ErrorF("xf86writeConfigFile(): seteuid(%d) failed (%s)\n", |
| 185 | ruid, strerror(errno)); |
| 186 | return 0; |
| 187 | } |
| 188 | ret = doWriteConfigFile(filename, cptr); |
| 189 | |
| 190 | if (seteuid(euid) == -1) { |
| 191 | ErrorF("xf86writeConfigFile(): seteuid(%d) failed (%s)\n", |
| 192 | euid, strerror(errno)); |
| 193 | } |
| 194 | return ret; |
| 195 | |
| 196 | #endif /* HAS_SAVED_IDS_AND_SETEUID */ |
| 197 | |
| 198 | } |
| 199 | else { |
| 200 | return doWriteConfigFile(filename, cptr); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #endif /* !HAS_NO_UIDS */ |